blob: 696f09a206db5028d319e4a74cc7eabe860f8612 [file] [log] [blame]
Eric Gao61e6c442016-07-29 12:34:32 +08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2016 Rockchip Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <arch/io.h>
17#include <assert.h>
18#include <boardid.h>
19#include <console/console.h>
Julius Werner7feb86b2016-09-02 11:25:56 -070020#include <gpio.h>
Eric Gao61e6c442016-07-29 12:34:32 +080021#include <soc/grf.h>
22#include <soc/pwm.h>
23
24#include "pwm_regulator.h"
25
26/*
27 * Apparently a period of 3333 is determined by EEs to be ideal for our
28 * board design / resistors / capacitors / regulators but due to
29 * clock dividers we actually get 3337.
30 */
Caesar Wang8c454aa2017-01-04 18:26:04 +080031#define PWM_PERIOD 3337
32#define PWM_DESIGN_VOLTAGE_MIN_OUTDATED 8000
33#define PWM_DESIGN_VOLTAGE_MAX_OUTDATED 15000
Eric Gao61e6c442016-07-29 12:34:32 +080034
Julius Wernerf52288f2016-09-01 11:50:18 -070035/* Later boards (Kevin rev6+, Gru rev2+) use different regulator ranges. */
Caesar Wang8c454aa2017-01-04 18:26:04 +080036int kevin6_pwm_design_voltage[][2] = {
Julius Wernerf52288f2016-09-01 11:50:18 -070037 [PWM_REGULATOR_GPU] = {7858, 12177},
38 [PWM_REGULATOR_BIG] = {7987, 13022},
39 [PWM_REGULATOR_LIT] = {7991, 13037},
40 [PWM_REGULATOR_CENTERLOG] = {8001, 10497}
Eric Gao61e6c442016-07-29 12:34:32 +080041};
42
Caesar Wang8c454aa2017-01-04 18:26:04 +080043int pwm_design_voltage[][2] = {
44 [PWM_REGULATOR_GPU] = {7864, 12177},
45 [PWM_REGULATOR_BIG] = {8001, 13022},
46 [PWM_REGULATOR_LIT] = {7977, 13078},
47 [PWM_REGULATOR_CENTERLOG] = {7994, 10499}
48};
49
Julius Werner6486e782017-07-14 14:30:29 -070050int pwm_enum_to_pwm_number[] = {
51 [PWM_REGULATOR_GPU] = 0,
52 [PWM_REGULATOR_LIT] = 2,
53#if IS_ENABLED(CONFIG_BOARD_GOOGLE_SCARLET)
54 [PWM_REGULATOR_BIG] = 3,
55 [PWM_REGULATOR_CENTERLOG] = -1, /* fixed regulator on Scarlet */
56#else
57 [PWM_REGULATOR_BIG] = 1,
58 [PWM_REGULATOR_CENTERLOG] = 3,
59#endif
60};
61
Eric Gao61e6c442016-07-29 12:34:32 +080062void pwm_regulator_configure(enum pwm_regulator pwm, int millivolt)
63{
64 int duty_ns, voltage_max, voltage_min;
65 int voltage = millivolt * 10; /* for higer calculation accuracy */
Julius Werner6486e782017-07-14 14:30:29 -070066 int pwm_number = pwm_enum_to_pwm_number[pwm];
Eric Gao61e6c442016-07-29 12:34:32 +080067
Caesar Wang8c454aa2017-01-04 18:26:04 +080068 voltage_min = pwm_design_voltage[pwm][0];
69 voltage_max = pwm_design_voltage[pwm][1];
70 if ((IS_ENABLED(CONFIG_BOARD_GOOGLE_KEVIN) && board_id() < 6) ||
71 (IS_ENABLED(CONFIG_BOARD_GOOGLE_GRU) && board_id() < 2)) {
72 voltage_min = PWM_DESIGN_VOLTAGE_MIN_OUTDATED;
73 voltage_max = PWM_DESIGN_VOLTAGE_MAX_OUTDATED;
Patrick Georgi96af0af2017-02-23 17:44:13 +010074 } else if (IS_ENABLED(CONFIG_BOARD_GOOGLE_KEVIN) && board_id() >= 6) {
Caesar Wang8c454aa2017-01-04 18:26:04 +080075 voltage_min = kevin6_pwm_design_voltage[pwm][0];
76 voltage_max = kevin6_pwm_design_voltage[pwm][1];
Eric Gao61e6c442016-07-29 12:34:32 +080077 }
78
79 assert(voltage <= voltage_max && voltage >= voltage_min);
80
81 /*
82 * Intentionally round down (higher volt) to be safe.
83 * eg, for the default min & max design voltage:
84 * period = 3337, volt = 1.1: 1906
85 * period = 3337, volt = 1.0: 2383
86 * period = 3337, volt = 0.9: 2860
87 */
88 duty_ns = PWM_PERIOD * (voltage_max - voltage)
89 / (voltage_max - voltage_min);
90
Julius Werner6486e782017-07-14 14:30:29 -070091 pwm_init(pwm_number, PWM_PERIOD, duty_ns);
Douglas Anderson1eb69b42016-09-06 13:51:03 -070092
Julius Werner6486e782017-07-14 14:30:29 -070093 switch (pwm_number) {
94 case 0:
Julius Werner7feb86b2016-09-02 11:25:56 -070095 gpio_input(GPIO(4, C, 2)); /* PWM0 remove pull-down */
Douglas Anderson1eb69b42016-09-06 13:51:03 -070096 write32(&rk3399_grf->iomux_pwm_0, IOMUX_PWM_0);
97 break;
Julius Werner6486e782017-07-14 14:30:29 -070098 case 1:
Julius Werner7feb86b2016-09-02 11:25:56 -070099 gpio_input(GPIO(4, C, 6)); /* PWM1 remove pull-down */
Douglas Anderson1eb69b42016-09-06 13:51:03 -0700100 write32(&rk3399_grf->iomux_pwm_1, IOMUX_PWM_1);
101 break;
Julius Werner6486e782017-07-14 14:30:29 -0700102 case 2:
Julius Werner7feb86b2016-09-02 11:25:56 -0700103 gpio_input(GPIO(1, C, 3)); /* PWM2 remove pull-down */
Douglas Anderson1eb69b42016-09-06 13:51:03 -0700104 write32(&rk3399_pmugrf->iomux_pwm_2, IOMUX_PWM_2);
105 break;
Julius Werner6486e782017-07-14 14:30:29 -0700106 case 3:
Julius Werner7feb86b2016-09-02 11:25:56 -0700107 gpio_input(GPIO(0, A, 6)); /* PWM3 remove pull-down */
Douglas Anderson1eb69b42016-09-06 13:51:03 -0700108 write32(&rk3399_pmugrf->iomux_pwm_3a, IOMUX_PWM_3_A);
109 break;
Julius Werner6486e782017-07-14 14:30:29 -0700110 default:
111 die("incorrect board configuration");
Douglas Anderson1eb69b42016-09-06 13:51:03 -0700112 }
Eric Gao61e6c442016-07-29 12:34:32 +0800113}