Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 1 | /* |
| 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 Werner | 7feb86b | 2016-09-02 11:25:56 -0700 | [diff] [blame] | 20 | #include <gpio.h> |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 21 | #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 Wang | 8c454aa | 2017-01-04 18:26:04 +0800 | [diff] [blame] | 31 | #define PWM_PERIOD 3337 |
| 32 | #define PWM_DESIGN_VOLTAGE_MIN_OUTDATED 8000 |
| 33 | #define PWM_DESIGN_VOLTAGE_MAX_OUTDATED 15000 |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 34 | |
Lin Huang | c93d79b | 2017-07-31 15:12:15 +0800 | [diff] [blame] | 35 | /* Applies for Kevin rev6+ */ |
Caesar Wang | 8c454aa | 2017-01-04 18:26:04 +0800 | [diff] [blame] | 36 | int kevin6_pwm_design_voltage[][2] = { |
Julius Werner | f52288f | 2016-09-01 11:50:18 -0700 | [diff] [blame] | 37 | [PWM_REGULATOR_GPU] = {7858, 12177}, |
| 38 | [PWM_REGULATOR_BIG] = {7987, 13022}, |
| 39 | [PWM_REGULATOR_LIT] = {7991, 13037}, |
| 40 | [PWM_REGULATOR_CENTERLOG] = {8001, 10497} |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 41 | }; |
| 42 | |
Philip Chen | a061820 | 2017-08-23 18:02:25 -0700 | [diff] [blame] | 43 | /* Applies for Gru rev2+, Bob, and Nefario. */ |
Caesar Wang | 8c454aa | 2017-01-04 18:26:04 +0800 | [diff] [blame] | 44 | int pwm_design_voltage[][2] = { |
| 45 | [PWM_REGULATOR_GPU] = {7864, 12177}, |
| 46 | [PWM_REGULATOR_BIG] = {8001, 13022}, |
| 47 | [PWM_REGULATOR_LIT] = {7977, 13078}, |
| 48 | [PWM_REGULATOR_CENTERLOG] = {7994, 10499} |
| 49 | }; |
| 50 | |
Ege Mihmanli | 75b1543 | 2017-11-15 17:19:58 -0800 | [diff] [blame^] | 51 | /* Applies for Scarlet-based boards. */ |
Lin Huang | c93d79b | 2017-07-31 15:12:15 +0800 | [diff] [blame] | 52 | int scarlet_pwm_design_voltage[][2] = { |
| 53 | [PWM_REGULATOR_GPU] = {7996, 10990}, |
| 54 | [PWM_REGULATOR_BIG] = {8000, 12992}, |
| 55 | [PWM_REGULATOR_LIT] = {8021, 11996}, |
| 56 | }; |
| 57 | |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 58 | int pwm_enum_to_pwm_number[] = { |
| 59 | [PWM_REGULATOR_GPU] = 0, |
| 60 | [PWM_REGULATOR_LIT] = 2, |
Philip Chen | a061820 | 2017-08-23 18:02:25 -0700 | [diff] [blame] | 61 | #if IS_ENABLED(CONFIG_GRU_HAS_CENTERLOG_PWM) |
| 62 | [PWM_REGULATOR_CENTERLOG] = 3, |
| 63 | #else |
| 64 | [PWM_REGULATOR_CENTERLOG] = -1, |
| 65 | #endif |
Ege Mihmanli | 75b1543 | 2017-11-15 17:19:58 -0800 | [diff] [blame^] | 66 | #if IS_ENABLED(CONFIG_GRU_BASEBOARD_SCARLET) |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 67 | [PWM_REGULATOR_BIG] = 3, |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 68 | #else |
| 69 | [PWM_REGULATOR_BIG] = 1, |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 70 | #endif |
| 71 | }; |
| 72 | |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 73 | void pwm_regulator_configure(enum pwm_regulator pwm, int millivolt) |
| 74 | { |
| 75 | int duty_ns, voltage_max, voltage_min; |
| 76 | int voltage = millivolt * 10; /* for higer calculation accuracy */ |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 77 | int pwm_number = pwm_enum_to_pwm_number[pwm]; |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 78 | |
Caesar Wang | 8c454aa | 2017-01-04 18:26:04 +0800 | [diff] [blame] | 79 | voltage_min = pwm_design_voltage[pwm][0]; |
| 80 | voltage_max = pwm_design_voltage[pwm][1]; |
| 81 | if ((IS_ENABLED(CONFIG_BOARD_GOOGLE_KEVIN) && board_id() < 6) || |
| 82 | (IS_ENABLED(CONFIG_BOARD_GOOGLE_GRU) && board_id() < 2)) { |
| 83 | voltage_min = PWM_DESIGN_VOLTAGE_MIN_OUTDATED; |
| 84 | voltage_max = PWM_DESIGN_VOLTAGE_MAX_OUTDATED; |
Patrick Georgi | 96af0af | 2017-02-23 17:44:13 +0100 | [diff] [blame] | 85 | } else if (IS_ENABLED(CONFIG_BOARD_GOOGLE_KEVIN) && board_id() >= 6) { |
Caesar Wang | 8c454aa | 2017-01-04 18:26:04 +0800 | [diff] [blame] | 86 | voltage_min = kevin6_pwm_design_voltage[pwm][0]; |
| 87 | voltage_max = kevin6_pwm_design_voltage[pwm][1]; |
Ege Mihmanli | 75b1543 | 2017-11-15 17:19:58 -0800 | [diff] [blame^] | 88 | } else if (IS_ENABLED(CONFIG_GRU_BASEBOARD_SCARLET)) { |
Lin Huang | c93d79b | 2017-07-31 15:12:15 +0800 | [diff] [blame] | 89 | voltage_min = scarlet_pwm_design_voltage[pwm][0]; |
| 90 | voltage_max = scarlet_pwm_design_voltage[pwm][1]; |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | assert(voltage <= voltage_max && voltage >= voltage_min); |
| 94 | |
| 95 | /* |
| 96 | * Intentionally round down (higher volt) to be safe. |
| 97 | * eg, for the default min & max design voltage: |
| 98 | * period = 3337, volt = 1.1: 1906 |
| 99 | * period = 3337, volt = 1.0: 2383 |
| 100 | * period = 3337, volt = 0.9: 2860 |
| 101 | */ |
| 102 | duty_ns = PWM_PERIOD * (voltage_max - voltage) |
| 103 | / (voltage_max - voltage_min); |
| 104 | |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 105 | pwm_init(pwm_number, PWM_PERIOD, duty_ns); |
Douglas Anderson | 1eb69b4 | 2016-09-06 13:51:03 -0700 | [diff] [blame] | 106 | |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 107 | switch (pwm_number) { |
| 108 | case 0: |
Julius Werner | 7feb86b | 2016-09-02 11:25:56 -0700 | [diff] [blame] | 109 | gpio_input(GPIO(4, C, 2)); /* PWM0 remove pull-down */ |
Douglas Anderson | 1eb69b4 | 2016-09-06 13:51:03 -0700 | [diff] [blame] | 110 | write32(&rk3399_grf->iomux_pwm_0, IOMUX_PWM_0); |
| 111 | break; |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 112 | case 1: |
Julius Werner | 7feb86b | 2016-09-02 11:25:56 -0700 | [diff] [blame] | 113 | gpio_input(GPIO(4, C, 6)); /* PWM1 remove pull-down */ |
Douglas Anderson | 1eb69b4 | 2016-09-06 13:51:03 -0700 | [diff] [blame] | 114 | write32(&rk3399_grf->iomux_pwm_1, IOMUX_PWM_1); |
| 115 | break; |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 116 | case 2: |
Julius Werner | 7feb86b | 2016-09-02 11:25:56 -0700 | [diff] [blame] | 117 | gpio_input(GPIO(1, C, 3)); /* PWM2 remove pull-down */ |
Douglas Anderson | 1eb69b4 | 2016-09-06 13:51:03 -0700 | [diff] [blame] | 118 | write32(&rk3399_pmugrf->iomux_pwm_2, IOMUX_PWM_2); |
| 119 | break; |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 120 | case 3: |
Julius Werner | 7feb86b | 2016-09-02 11:25:56 -0700 | [diff] [blame] | 121 | gpio_input(GPIO(0, A, 6)); /* PWM3 remove pull-down */ |
Douglas Anderson | 1eb69b4 | 2016-09-06 13:51:03 -0700 | [diff] [blame] | 122 | write32(&rk3399_pmugrf->iomux_pwm_3a, IOMUX_PWM_3_A); |
| 123 | break; |
Julius Werner | 6486e78 | 2017-07-14 14:30:29 -0700 | [diff] [blame] | 124 | default: |
| 125 | die("incorrect board configuration"); |
Douglas Anderson | 1eb69b4 | 2016-09-06 13:51:03 -0700 | [diff] [blame] | 126 | } |
Eric Gao | 61e6c44 | 2016-07-29 12:34:32 +0800 | [diff] [blame] | 127 | } |