Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Rockchip Electronics Co., Ltd |
| 4 | * |
| 5 | * Based on kernel drivers/regulator/pwm-regulator.c |
| 6 | * Copyright (C) 2014 - STMicroelectronics Inc. |
| 7 | * Author: Lee Jones <lee.jones@linaro.org> |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <dm.h> |
| 12 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 14 | #include <pwm.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 17 | #include <power/regulator.h> |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | struct pwm_regulator_info { |
| 22 | /* pwm id corresponding to the PWM driver */ |
| 23 | int pwm_id; |
| 24 | /* the period of one PWM cycle */ |
| 25 | int period_ns; |
Kever Yang | 4c08742 | 2017-04-24 10:27:49 +0800 | [diff] [blame] | 26 | /* |
| 27 | * the polarity of one PWM |
| 28 | * 0: normal polarity |
| 29 | * 1: inverted polarity |
| 30 | */ |
| 31 | bool polarity; |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 32 | struct udevice *pwm; |
| 33 | /* initialize voltage of regulator */ |
Mark Kettenis | 416b6c5 | 2017-05-13 20:17:05 +0200 | [diff] [blame] | 34 | int init_voltage; |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 35 | /* the maximum voltage of regulator */ |
Mark Kettenis | 416b6c5 | 2017-05-13 20:17:05 +0200 | [diff] [blame] | 36 | int max_voltage; |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 37 | /* the minimum voltage of regulator */ |
Mark Kettenis | 416b6c5 | 2017-05-13 20:17:05 +0200 | [diff] [blame] | 38 | int min_voltage; |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 39 | /* the current voltage of regulator */ |
Mark Kettenis | 416b6c5 | 2017-05-13 20:17:05 +0200 | [diff] [blame] | 40 | int volt_uV; |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static int pwm_regulator_enable(struct udevice *dev, bool enable) |
| 44 | { |
| 45 | struct pwm_regulator_info *priv = dev_get_priv(dev); |
| 46 | |
| 47 | return pwm_set_enable(priv->pwm, priv->pwm_id, enable); |
| 48 | } |
| 49 | |
| 50 | static int pwm_voltage_to_duty_cycle_percentage(struct udevice *dev, int req_uV) |
| 51 | { |
| 52 | struct pwm_regulator_info *priv = dev_get_priv(dev); |
| 53 | int min_uV = priv->min_voltage; |
| 54 | int max_uV = priv->max_voltage; |
| 55 | int diff = max_uV - min_uV; |
| 56 | |
Kever Yang | 4c08742 | 2017-04-24 10:27:49 +0800 | [diff] [blame] | 57 | return ((req_uV * 100) - (min_uV * 100)) / diff; |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static int pwm_regulator_get_voltage(struct udevice *dev) |
| 61 | { |
| 62 | struct pwm_regulator_info *priv = dev_get_priv(dev); |
| 63 | |
| 64 | return priv->volt_uV; |
| 65 | } |
| 66 | |
| 67 | static int pwm_regulator_set_voltage(struct udevice *dev, int uvolt) |
| 68 | { |
| 69 | struct pwm_regulator_info *priv = dev_get_priv(dev); |
| 70 | int duty_cycle; |
| 71 | int ret = 0; |
| 72 | |
| 73 | duty_cycle = pwm_voltage_to_duty_cycle_percentage(dev, uvolt); |
| 74 | |
Kever Yang | 4c08742 | 2017-04-24 10:27:49 +0800 | [diff] [blame] | 75 | ret = pwm_set_invert(priv->pwm, priv->pwm_id, priv->polarity); |
| 76 | if (ret) { |
| 77 | dev_err(dev, "Failed to init PWM\n"); |
| 78 | return ret; |
| 79 | } |
| 80 | |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 81 | ret = pwm_set_config(priv->pwm, priv->pwm_id, |
Kever Yang | 6ea3e5d | 2017-07-19 19:54:21 +0800 | [diff] [blame] | 82 | priv->period_ns, (priv->period_ns / 100) * duty_cycle); |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 83 | if (ret) { |
| 84 | dev_err(dev, "Failed to configure PWM\n"); |
| 85 | return ret; |
| 86 | } |
| 87 | |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 88 | priv->volt_uV = uvolt; |
Kever Yang | cba88f1 | 2017-07-19 19:54:22 +0800 | [diff] [blame] | 89 | |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 90 | return ret; |
| 91 | } |
| 92 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 93 | static int pwm_regulator_of_to_plat(struct udevice *dev) |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 94 | { |
| 95 | struct pwm_regulator_info *priv = dev_get_priv(dev); |
Andy Yan | 0addcea | 2018-03-27 19:39:38 +0800 | [diff] [blame] | 96 | struct ofnode_phandle_args args; |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 97 | int ret; |
| 98 | |
Andy Yan | 0addcea | 2018-03-27 19:39:38 +0800 | [diff] [blame] | 99 | ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, &args); |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 100 | if (ret) { |
| 101 | debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret); |
| 102 | return ret; |
| 103 | } |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 104 | |
| 105 | priv->period_ns = args.args[1]; |
Kever Yang | 4c08742 | 2017-04-24 10:27:49 +0800 | [diff] [blame] | 106 | priv->polarity = args.args[2]; |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 107 | |
Andy Yan | 0addcea | 2018-03-27 19:39:38 +0800 | [diff] [blame] | 108 | priv->init_voltage = dev_read_u32_default(dev, "regulator-init-microvolt", -1); |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 109 | if (priv->init_voltage < 0) { |
| 110 | printf("Cannot find regulator pwm init_voltage\n"); |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | |
Andy Yan | 0addcea | 2018-03-27 19:39:38 +0800 | [diff] [blame] | 114 | ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm); |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 115 | if (ret) { |
| 116 | debug("%s: Cannot get PWM: ret=%d\n", __func__, ret); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int pwm_regulator_probe(struct udevice *dev) |
| 124 | { |
| 125 | struct pwm_regulator_info *priv = dev_get_priv(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 126 | struct dm_regulator_uclass_plat *uc_pdata; |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 127 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 128 | uc_pdata = dev_get_uclass_plat(dev); |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 129 | |
| 130 | uc_pdata->type = REGULATOR_TYPE_BUCK; |
| 131 | uc_pdata->mode_count = 0; |
| 132 | priv->max_voltage = uc_pdata->max_uV; |
| 133 | priv->min_voltage = uc_pdata->min_uV; |
| 134 | |
| 135 | if (priv->init_voltage) |
| 136 | pwm_regulator_set_voltage(dev, priv->init_voltage); |
| 137 | |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static const struct dm_regulator_ops pwm_regulator_ops = { |
| 142 | .get_value = pwm_regulator_get_voltage, |
| 143 | .set_value = pwm_regulator_set_voltage, |
| 144 | .set_enable = pwm_regulator_enable, |
| 145 | }; |
| 146 | |
| 147 | static const struct udevice_id pwm_regulator_ids[] = { |
| 148 | { .compatible = "pwm-regulator" }, |
| 149 | { } |
| 150 | }; |
| 151 | |
| 152 | U_BOOT_DRIVER(pwm_regulator) = { |
| 153 | .name = "pwm_regulator", |
| 154 | .id = UCLASS_REGULATOR, |
| 155 | .ops = &pwm_regulator_ops, |
| 156 | .probe = pwm_regulator_probe, |
| 157 | .of_match = pwm_regulator_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 158 | .of_to_plat = pwm_regulator_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 159 | .priv_auto = sizeof(struct pwm_regulator_info), |
Kever Yang | 8bbe1cc | 2016-09-23 15:57:19 +0800 | [diff] [blame] | 160 | }; |