Angel Pons | 16c851f | 2020-04-05 13:21:38 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 2 | |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 3 | #include <device/mmio.h> |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 4 | #include <assert.h> |
| 5 | #include <boardid.h> |
| 6 | #include <console/console.h> |
Julius Werner | 7feb86b | 2016-09-02 11:25:56 -0700 | [diff] [blame] | 7 | #include <gpio.h> |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 8 | #include <soc/grf.h> |
| 9 | #include <soc/pwm.h> |
| 10 | |
| 11 | #include "pwm_regulator.h" |
| 12 | |
| 13 | /* |
| 14 | * Apparently a period of 3333 is determined by EEs to be ideal for our |
| 15 | * board design / resistors / capacitors / regulators but due to |
| 16 | * clock dividers we actually get 3337. |
| 17 | */ |
Caesar Wang | 8c454aa | 2017-01-04 18:26:04 +0800 | [diff] [blame] | 18 | #define PWM_PERIOD 3337 |
| 19 | #define PWM_DESIGN_VOLTAGE_MIN_OUTDATED 8000 |
| 20 | #define PWM_DESIGN_VOLTAGE_MAX_OUTDATED 15000 |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 21 | |
Lin Huang | c93d79b | 2017-07-31 15:12:15 +0800 | [diff] [blame] | 22 | /* Applies for Kevin rev6+ */ |
Caesar Wang | 8c454aa | 2017-01-04 18:26:04 +0800 | [diff] [blame] | 23 | int kevin6_pwm_design_voltage[][2] = { |
Julius Werner | f52288f | 2016-09-01 11:50:18 -0700 | [diff] [blame] | 24 | [PWM_REGULATOR_GPU] = {7858, 12177}, |
| 25 | [PWM_REGULATOR_BIG] = {7987, 13022}, |
| 26 | [PWM_REGULATOR_LIT] = {7991, 13037}, |
| 27 | [PWM_REGULATOR_CENTERLOG] = {8001, 10497} |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 28 | }; |
| 29 | |
Philip Chen | a061820 | 2017-08-23 18:02:25 -0700 | [diff] [blame] | 30 | /* Applies for Gru rev2+, Bob, and Nefario. */ |
Caesar Wang | 8c454aa | 2017-01-04 18:26:04 +0800 | [diff] [blame] | 31 | int pwm_design_voltage[][2] = { |
| 32 | [PWM_REGULATOR_GPU] = {7864, 12177}, |
| 33 | [PWM_REGULATOR_BIG] = {8001, 13022}, |
| 34 | [PWM_REGULATOR_LIT] = {7977, 13078}, |
| 35 | [PWM_REGULATOR_CENTERLOG] = {7994, 10499} |
| 36 | }; |
| 37 | |
Ege Mihmanli | 75b1543 | 2017-11-15 17:19:58 -0800 | [diff] [blame] | 38 | /* Applies for Scarlet-based boards. */ |
Lin Huang | c93d79b | 2017-07-31 15:12:15 +0800 | [diff] [blame] | 39 | int scarlet_pwm_design_voltage[][2] = { |
| 40 | [PWM_REGULATOR_GPU] = {7996, 10990}, |
| 41 | [PWM_REGULATOR_BIG] = {8000, 12992}, |
| 42 | [PWM_REGULATOR_LIT] = {8021, 11996}, |
| 43 | }; |
| 44 | |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 45 | int pwm_enum_to_pwm_number[] = { |
| 46 | [PWM_REGULATOR_GPU] = 0, |
| 47 | [PWM_REGULATOR_LIT] = 2, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 48 | #if CONFIG(GRU_HAS_CENTERLOG_PWM) |
Philip Chen | a061820 | 2017-08-23 18:02:25 -0700 | [diff] [blame] | 49 | [PWM_REGULATOR_CENTERLOG] = 3, |
| 50 | #else |
| 51 | [PWM_REGULATOR_CENTERLOG] = -1, |
| 52 | #endif |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 53 | #if CONFIG(GRU_BASEBOARD_SCARLET) |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 54 | [PWM_REGULATOR_BIG] = 3, |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 55 | #else |
| 56 | [PWM_REGULATOR_BIG] = 1, |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 57 | #endif |
| 58 | }; |
| 59 | |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 60 | void pwm_regulator_configure(enum pwm_regulator pwm, int millivolt) |
| 61 | { |
| 62 | int duty_ns, voltage_max, voltage_min; |
Martin Roth | 50863da | 2021-10-01 14:37:30 -0600 | [diff] [blame^] | 63 | int voltage = millivolt * 10; /* for higher calculation accuracy */ |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 64 | int pwm_number = pwm_enum_to_pwm_number[pwm]; |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 65 | |
Caesar Wang | 8c454aa | 2017-01-04 18:26:04 +0800 | [diff] [blame] | 66 | voltage_min = pwm_design_voltage[pwm][0]; |
| 67 | voltage_max = pwm_design_voltage[pwm][1]; |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 68 | if ((CONFIG(BOARD_GOOGLE_KEVIN) && board_id() < 6) || |
| 69 | (CONFIG(BOARD_GOOGLE_GRU) && board_id() < 2)) { |
Caesar Wang | 8c454aa | 2017-01-04 18:26:04 +0800 | [diff] [blame] | 70 | voltage_min = PWM_DESIGN_VOLTAGE_MIN_OUTDATED; |
| 71 | voltage_max = PWM_DESIGN_VOLTAGE_MAX_OUTDATED; |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 72 | } else if (CONFIG(BOARD_GOOGLE_KEVIN) && board_id() >= 6) { |
Caesar Wang | 8c454aa | 2017-01-04 18:26:04 +0800 | [diff] [blame] | 73 | voltage_min = kevin6_pwm_design_voltage[pwm][0]; |
| 74 | voltage_max = kevin6_pwm_design_voltage[pwm][1]; |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 75 | } else if (CONFIG(GRU_BASEBOARD_SCARLET)) { |
Lin Huang | c93d79b | 2017-07-31 15:12:15 +0800 | [diff] [blame] | 76 | voltage_min = scarlet_pwm_design_voltage[pwm][0]; |
| 77 | voltage_max = scarlet_pwm_design_voltage[pwm][1]; |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | assert(voltage <= voltage_max && voltage >= voltage_min); |
| 81 | |
| 82 | /* |
| 83 | * Intentionally round down (higher volt) to be safe. |
| 84 | * eg, for the default min & max design voltage: |
| 85 | * period = 3337, volt = 1.1: 1906 |
| 86 | * period = 3337, volt = 1.0: 2383 |
| 87 | * period = 3337, volt = 0.9: 2860 |
| 88 | */ |
| 89 | duty_ns = PWM_PERIOD * (voltage_max - voltage) |
| 90 | / (voltage_max - voltage_min); |
| 91 | |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 92 | pwm_init(pwm_number, PWM_PERIOD, duty_ns); |
Douglas Anderson | 1eb69b4 | 2016-09-06 13:51:03 -0700 | [diff] [blame] | 93 | |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 94 | switch (pwm_number) { |
| 95 | case 0: |
Julius Werner | 7feb86b | 2016-09-02 11:25:56 -0700 | [diff] [blame] | 96 | gpio_input(GPIO(4, C, 2)); /* PWM0 remove pull-down */ |
Douglas Anderson | 1eb69b4 | 2016-09-06 13:51:03 -0700 | [diff] [blame] | 97 | write32(&rk3399_grf->iomux_pwm_0, IOMUX_PWM_0); |
| 98 | break; |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 99 | case 1: |
Julius Werner | 7feb86b | 2016-09-02 11:25:56 -0700 | [diff] [blame] | 100 | gpio_input(GPIO(4, C, 6)); /* PWM1 remove pull-down */ |
Douglas Anderson | 1eb69b4 | 2016-09-06 13:51:03 -0700 | [diff] [blame] | 101 | write32(&rk3399_grf->iomux_pwm_1, IOMUX_PWM_1); |
| 102 | break; |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 103 | case 2: |
Julius Werner | 7feb86b | 2016-09-02 11:25:56 -0700 | [diff] [blame] | 104 | gpio_input(GPIO(1, C, 3)); /* PWM2 remove pull-down */ |
Douglas Anderson | 1eb69b4 | 2016-09-06 13:51:03 -0700 | [diff] [blame] | 105 | write32(&rk3399_pmugrf->iomux_pwm_2, IOMUX_PWM_2); |
| 106 | break; |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 107 | case 3: |
Julius Werner | 7feb86b | 2016-09-02 11:25:56 -0700 | [diff] [blame] | 108 | gpio_input(GPIO(0, A, 6)); /* PWM3 remove pull-down */ |
Douglas Anderson | 1eb69b4 | 2016-09-06 13:51:03 -0700 | [diff] [blame] | 109 | write32(&rk3399_pmugrf->iomux_pwm_3a, IOMUX_PWM_3_A); |
| 110 | break; |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 111 | default: |
| 112 | die("incorrect board configuration"); |
Douglas Anderson | 1eb69b4 | 2016-09-06 13:51:03 -0700 | [diff] [blame] | 113 | } |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 114 | } |