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> |
| 20 | #include <soc/grf.h> |
| 21 | #include <soc/pwm.h> |
| 22 | |
| 23 | #include "pwm_regulator.h" |
| 24 | |
| 25 | /* |
| 26 | * Apparently a period of 3333 is determined by EEs to be ideal for our |
| 27 | * board design / resistors / capacitors / regulators but due to |
| 28 | * clock dividers we actually get 3337. |
| 29 | */ |
| 30 | #define PWM_PERIOD 3337 |
| 31 | #define PWM_DESIGN_VOLTAGE_MIN 8000 |
| 32 | #define PWM_DESIGN_VOLTAGE_MAX 15000 |
| 33 | |
| 34 | /* The min & max design voltages are different after kevin-r6 */ |
| 35 | int kevin_voltage_min_max_r6[][2] = { |
| 36 | [PWM_REGULATOR_GPU] = {7910, 12139}, |
| 37 | [PWM_REGULATOR_BIG] = {7986, 13057}, |
| 38 | [PWM_REGULATOR_LIT] = {7997, 13002}, |
| 39 | [PWM_REGULATOR_CENTERLOG] = {7996, 10507} |
| 40 | }; |
| 41 | |
| 42 | void pwm_regulator_configure(enum pwm_regulator pwm, int millivolt) |
| 43 | { |
| 44 | int duty_ns, voltage_max, voltage_min; |
| 45 | int voltage = millivolt * 10; /* for higer calculation accuracy */ |
| 46 | |
| 47 | switch (pwm) { |
| 48 | case PWM_REGULATOR_GPU: |
| 49 | write32(&rk3399_grf->iomux_pwm_0, IOMUX_PWM_0); |
| 50 | break; |
| 51 | case PWM_REGULATOR_BIG: |
| 52 | write32(&rk3399_grf->iomux_pwm_1, IOMUX_PWM_1); |
| 53 | break; |
| 54 | case PWM_REGULATOR_LIT: |
| 55 | write32(&rk3399_pmugrf->iomux_pwm_2, IOMUX_PWM_2); |
| 56 | break; |
| 57 | case PWM_REGULATOR_CENTERLOG: |
| 58 | write32(&rk3399_pmugrf->iomux_pwm_3a, IOMUX_PWM_3_A); |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | voltage_min = PWM_DESIGN_VOLTAGE_MIN; |
| 63 | voltage_max = PWM_DESIGN_VOLTAGE_MAX; |
| 64 | if (IS_ENABLED(CONFIG_BOARD_GOOGLE_KEVIN) && board_id() >= 6) { |
| 65 | voltage_min = kevin_voltage_min_max_r6[pwm][0]; |
| 66 | voltage_max = kevin_voltage_min_max_r6[pwm][1]; |
| 67 | } |
| 68 | |
| 69 | assert(voltage <= voltage_max && voltage >= voltage_min); |
| 70 | |
| 71 | /* |
| 72 | * Intentionally round down (higher volt) to be safe. |
| 73 | * eg, for the default min & max design voltage: |
| 74 | * period = 3337, volt = 1.1: 1906 |
| 75 | * period = 3337, volt = 1.0: 2383 |
| 76 | * period = 3337, volt = 0.9: 2860 |
| 77 | */ |
| 78 | duty_ns = PWM_PERIOD * (voltage_max - voltage) |
| 79 | / (voltage_max - voltage_min); |
| 80 | |
| 81 | pwm_init(pwm, PWM_PERIOD, duty_ns); |
| 82 | } |