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