Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Regulator driver for PWM Regulators |
| 3 | * |
| 4 | * Copyright (C) 2014 - STMicroelectronics Inc. |
| 5 | * |
| 6 | * Author: Lee Jones <lee.jones@linaro.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/regulator/driver.h> |
| 17 | #include <linux/regulator/machine.h> |
| 18 | #include <linux/regulator/of_regulator.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_device.h> |
| 21 | #include <linux/pwm.h> |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 22 | #include <linux/gpio/consumer.h> |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 23 | |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 24 | struct pwm_continuous_reg_data { |
| 25 | unsigned int min_uV_dutycycle; |
| 26 | unsigned int max_uV_dutycycle; |
| 27 | unsigned int dutycycle_unit; |
| 28 | }; |
| 29 | |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 30 | struct pwm_regulator_data { |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 31 | /* Shared */ |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 32 | struct pwm_device *pwm; |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 33 | |
| 34 | /* Voltage table */ |
| 35 | struct pwm_voltages *duty_cycle_table; |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 36 | |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 37 | /* Continuous mode info */ |
| 38 | struct pwm_continuous_reg_data continuous; |
| 39 | |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 40 | /* regulator descriptor */ |
| 41 | struct regulator_desc desc; |
| 42 | |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 43 | int state; |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 44 | |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 45 | /* Enable GPIO */ |
| 46 | struct gpio_desc *enb_gpio; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | struct pwm_voltages { |
| 50 | unsigned int uV; |
| 51 | unsigned int dutycycle; |
| 52 | }; |
| 53 | |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 54 | /** |
| 55 | * Voltage table call-backs |
| 56 | */ |
Boris Brezillon | 8724899 | 2016-06-14 11:13:19 +0200 | [diff] [blame] | 57 | static void pwm_regulator_init_state(struct regulator_dev *rdev) |
| 58 | { |
| 59 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
| 60 | struct pwm_state pwm_state; |
| 61 | unsigned int dutycycle; |
| 62 | int i; |
| 63 | |
| 64 | pwm_get_state(drvdata->pwm, &pwm_state); |
| 65 | dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100); |
| 66 | |
| 67 | for (i = 0; i < rdev->desc->n_voltages; i++) { |
| 68 | if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) { |
| 69 | drvdata->state = i; |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 75 | static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev) |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 76 | { |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 77 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 78 | |
Boris Brezillon | 8724899 | 2016-06-14 11:13:19 +0200 | [diff] [blame] | 79 | if (drvdata->state < 0) |
| 80 | pwm_regulator_init_state(rdev); |
| 81 | |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 82 | return drvdata->state; |
| 83 | } |
| 84 | |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 85 | static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev, |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 86 | unsigned selector) |
| 87 | { |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 88 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
Boris Brezillon | 3f4eb39 | 2016-06-14 11:13:18 +0200 | [diff] [blame] | 89 | struct pwm_state pstate; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 90 | int ret; |
| 91 | |
Boris Brezillon | 3f4eb39 | 2016-06-14 11:13:18 +0200 | [diff] [blame] | 92 | pwm_init_state(drvdata->pwm, &pstate); |
| 93 | pwm_set_relative_duty_cycle(&pstate, |
| 94 | drvdata->duty_cycle_table[selector].dutycycle, 100); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 95 | |
Boris Brezillon | 3f4eb39 | 2016-06-14 11:13:18 +0200 | [diff] [blame] | 96 | ret = pwm_apply_state(drvdata->pwm, &pstate); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 97 | if (ret) { |
Laxman Dewangan | 5bf59bd | 2016-03-14 18:20:13 +0530 | [diff] [blame] | 98 | dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | drvdata->state = selector; |
| 103 | |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 107 | static int pwm_regulator_list_voltage(struct regulator_dev *rdev, |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 108 | unsigned selector) |
| 109 | { |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 110 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 111 | |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 112 | if (selector >= rdev->desc->n_voltages) |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 113 | return -EINVAL; |
| 114 | |
| 115 | return drvdata->duty_cycle_table[selector].uV; |
| 116 | } |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 117 | |
Boris Brezillon | 1de7d80 | 2015-09-21 11:33:28 +0200 | [diff] [blame] | 118 | static int pwm_regulator_enable(struct regulator_dev *dev) |
| 119 | { |
| 120 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev); |
| 121 | |
Fabio Estevam | a4aae5a | 2017-07-23 23:10:48 -0300 | [diff] [blame] | 122 | gpiod_set_value_cansleep(drvdata->enb_gpio, 1); |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 123 | |
Boris Brezillon | 1de7d80 | 2015-09-21 11:33:28 +0200 | [diff] [blame] | 124 | return pwm_enable(drvdata->pwm); |
| 125 | } |
| 126 | |
| 127 | static int pwm_regulator_disable(struct regulator_dev *dev) |
| 128 | { |
| 129 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev); |
| 130 | |
| 131 | pwm_disable(drvdata->pwm); |
| 132 | |
Fabio Estevam | a4aae5a | 2017-07-23 23:10:48 -0300 | [diff] [blame] | 133 | gpiod_set_value_cansleep(drvdata->enb_gpio, 0); |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 134 | |
Boris Brezillon | 1de7d80 | 2015-09-21 11:33:28 +0200 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int pwm_regulator_is_enabled(struct regulator_dev *dev) |
| 139 | { |
| 140 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev); |
| 141 | |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 142 | if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio)) |
| 143 | return false; |
| 144 | |
Boris Brezillon | 1de7d80 | 2015-09-21 11:33:28 +0200 | [diff] [blame] | 145 | return pwm_is_enabled(drvdata->pwm); |
| 146 | } |
| 147 | |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 148 | static int pwm_regulator_get_voltage(struct regulator_dev *rdev) |
| 149 | { |
| 150 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 151 | unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle; |
| 152 | unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle; |
| 153 | unsigned int duty_unit = drvdata->continuous.dutycycle_unit; |
Boris Brezillon | d9070fd | 2016-06-14 11:13:20 +0200 | [diff] [blame] | 154 | int min_uV = rdev->constraints->min_uV; |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 155 | int max_uV = rdev->constraints->max_uV; |
| 156 | int diff_uV = max_uV - min_uV; |
Boris Brezillon | d9070fd | 2016-06-14 11:13:20 +0200 | [diff] [blame] | 157 | struct pwm_state pstate; |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 158 | unsigned int diff_duty; |
| 159 | unsigned int voltage; |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 160 | |
Boris Brezillon | d9070fd | 2016-06-14 11:13:20 +0200 | [diff] [blame] | 161 | pwm_get_state(drvdata->pwm, &pstate); |
| 162 | |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 163 | voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit); |
| 164 | |
| 165 | /* |
| 166 | * The dutycycle for min_uV might be greater than the one for max_uV. |
| 167 | * This is happening when the user needs an inversed polarity, but the |
| 168 | * PWM device does not support inversing it in hardware. |
| 169 | */ |
| 170 | if (max_uV_duty < min_uV_duty) { |
| 171 | voltage = min_uV_duty - voltage; |
| 172 | diff_duty = min_uV_duty - max_uV_duty; |
| 173 | } else { |
| 174 | voltage = voltage - min_uV_duty; |
| 175 | diff_duty = max_uV_duty - min_uV_duty; |
| 176 | } |
| 177 | |
| 178 | voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty); |
| 179 | |
| 180 | return voltage + min_uV; |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static int pwm_regulator_set_voltage(struct regulator_dev *rdev, |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 184 | int req_min_uV, int req_max_uV, |
| 185 | unsigned int *selector) |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 186 | { |
| 187 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 188 | unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle; |
| 189 | unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle; |
| 190 | unsigned int duty_unit = drvdata->continuous.dutycycle_unit; |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 191 | int min_uV = rdev->constraints->min_uV; |
| 192 | int max_uV = rdev->constraints->max_uV; |
| 193 | int diff_uV = max_uV - min_uV; |
Boris Brezillon | 3f4eb39 | 2016-06-14 11:13:18 +0200 | [diff] [blame] | 194 | struct pwm_state pstate; |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 195 | unsigned int diff_duty; |
| 196 | unsigned int dutycycle; |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 197 | int ret; |
| 198 | |
Boris Brezillon | 3f4eb39 | 2016-06-14 11:13:18 +0200 | [diff] [blame] | 199 | pwm_init_state(drvdata->pwm, &pstate); |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 200 | |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 201 | /* |
| 202 | * The dutycycle for min_uV might be greater than the one for max_uV. |
| 203 | * This is happening when the user needs an inversed polarity, but the |
| 204 | * PWM device does not support inversing it in hardware. |
| 205 | */ |
| 206 | if (max_uV_duty < min_uV_duty) |
| 207 | diff_duty = min_uV_duty - max_uV_duty; |
| 208 | else |
| 209 | diff_duty = max_uV_duty - min_uV_duty; |
| 210 | |
| 211 | dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) * |
| 212 | diff_duty, |
| 213 | diff_uV); |
| 214 | |
| 215 | if (max_uV_duty < min_uV_duty) |
| 216 | dutycycle = min_uV_duty - dutycycle; |
| 217 | else |
| 218 | dutycycle = min_uV_duty + dutycycle; |
| 219 | |
| 220 | pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit); |
Laxman Dewangan | fd786fb0 | 2016-04-05 15:09:48 +0530 | [diff] [blame] | 221 | |
Boris Brezillon | 3f4eb39 | 2016-06-14 11:13:18 +0200 | [diff] [blame] | 222 | ret = pwm_apply_state(drvdata->pwm, &pstate); |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 223 | if (ret) { |
Laxman Dewangan | 5bf59bd | 2016-03-14 18:20:13 +0530 | [diff] [blame] | 224 | dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret); |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 225 | return ret; |
| 226 | } |
| 227 | |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Axel Lin | 638aef7 | 2019-01-12 12:30:13 +0800 | [diff] [blame] | 231 | static const struct regulator_ops pwm_regulator_voltage_table_ops = { |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 232 | .set_voltage_sel = pwm_regulator_set_voltage_sel, |
| 233 | .get_voltage_sel = pwm_regulator_get_voltage_sel, |
| 234 | .list_voltage = pwm_regulator_list_voltage, |
| 235 | .map_voltage = regulator_map_voltage_iterate, |
Boris Brezillon | 1de7d80 | 2015-09-21 11:33:28 +0200 | [diff] [blame] | 236 | .enable = pwm_regulator_enable, |
| 237 | .disable = pwm_regulator_disable, |
| 238 | .is_enabled = pwm_regulator_is_enabled, |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 239 | }; |
| 240 | |
Axel Lin | 638aef7 | 2019-01-12 12:30:13 +0800 | [diff] [blame] | 241 | static const struct regulator_ops pwm_regulator_voltage_continuous_ops = { |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 242 | .get_voltage = pwm_regulator_get_voltage, |
| 243 | .set_voltage = pwm_regulator_set_voltage, |
Boris Brezillon | 1de7d80 | 2015-09-21 11:33:28 +0200 | [diff] [blame] | 244 | .enable = pwm_regulator_enable, |
| 245 | .disable = pwm_regulator_disable, |
| 246 | .is_enabled = pwm_regulator_is_enabled, |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 247 | }; |
| 248 | |
Axel Lin | 638aef7 | 2019-01-12 12:30:13 +0800 | [diff] [blame] | 249 | static const struct regulator_desc pwm_regulator_desc = { |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 250 | .name = "pwm-regulator", |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 251 | .type = REGULATOR_VOLTAGE, |
| 252 | .owner = THIS_MODULE, |
| 253 | .supply_name = "pwm", |
| 254 | }; |
| 255 | |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 256 | static int pwm_regulator_init_table(struct platform_device *pdev, |
| 257 | struct pwm_regulator_data *drvdata) |
| 258 | { |
| 259 | struct device_node *np = pdev->dev.of_node; |
| 260 | struct pwm_voltages *duty_cycle_table; |
Lee Jones | 60cb65e | 2015-07-10 08:45:36 +0100 | [diff] [blame] | 261 | unsigned int length = 0; |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 262 | int ret; |
| 263 | |
| 264 | of_find_property(np, "voltage-table", &length); |
| 265 | |
| 266 | if ((length < sizeof(*duty_cycle_table)) || |
| 267 | (length % sizeof(*duty_cycle_table))) { |
Laxman Dewangan | 5bf59bd | 2016-03-14 18:20:13 +0530 | [diff] [blame] | 268 | dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n", |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 269 | length); |
| 270 | return -EINVAL; |
| 271 | } |
| 272 | |
| 273 | duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL); |
| 274 | if (!duty_cycle_table) |
| 275 | return -ENOMEM; |
| 276 | |
| 277 | ret = of_property_read_u32_array(np, "voltage-table", |
| 278 | (u32 *)duty_cycle_table, |
| 279 | length / sizeof(u32)); |
| 280 | if (ret) { |
Laxman Dewangan | 5bf59bd | 2016-03-14 18:20:13 +0530 | [diff] [blame] | 281 | dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret); |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 282 | return ret; |
| 283 | } |
| 284 | |
Boris Brezillon | 8724899 | 2016-06-14 11:13:19 +0200 | [diff] [blame] | 285 | drvdata->state = -EINVAL; |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 286 | drvdata->duty_cycle_table = duty_cycle_table; |
Axel Lin | 638aef7 | 2019-01-12 12:30:13 +0800 | [diff] [blame] | 287 | drvdata->desc.ops = &pwm_regulator_voltage_table_ops; |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 288 | drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table); |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 293 | static int pwm_regulator_init_continuous(struct platform_device *pdev, |
| 294 | struct pwm_regulator_data *drvdata) |
| 295 | { |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 296 | u32 dutycycle_range[2] = { 0, 100 }; |
| 297 | u32 dutycycle_unit = 100; |
| 298 | |
Axel Lin | 638aef7 | 2019-01-12 12:30:13 +0800 | [diff] [blame] | 299 | drvdata->desc.ops = &pwm_regulator_voltage_continuous_ops; |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 300 | drvdata->desc.continuous_voltage_range = true; |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 301 | |
Boris Brezillon | ea398e2 | 2016-06-14 11:13:21 +0200 | [diff] [blame] | 302 | of_property_read_u32_array(pdev->dev.of_node, |
| 303 | "pwm-dutycycle-range", |
| 304 | dutycycle_range, 2); |
| 305 | of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit", |
| 306 | &dutycycle_unit); |
| 307 | |
| 308 | if (dutycycle_range[0] > dutycycle_unit || |
| 309 | dutycycle_range[1] > dutycycle_unit) |
| 310 | return -EINVAL; |
| 311 | |
| 312 | drvdata->continuous.dutycycle_unit = dutycycle_unit; |
| 313 | drvdata->continuous.min_uV_dutycycle = dutycycle_range[0]; |
| 314 | drvdata->continuous.max_uV_dutycycle = dutycycle_range[1]; |
| 315 | |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 319 | static int pwm_regulator_probe(struct platform_device *pdev) |
| 320 | { |
Lee Jones | 5ad2cb1 | 2015-07-07 16:06:53 +0100 | [diff] [blame] | 321 | const struct regulator_init_data *init_data; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 322 | struct pwm_regulator_data *drvdata; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 323 | struct regulator_dev *regulator; |
| 324 | struct regulator_config config = { }; |
| 325 | struct device_node *np = pdev->dev.of_node; |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 326 | enum gpiod_flags gpio_flags; |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 327 | int ret; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 328 | |
| 329 | if (!np) { |
| 330 | dev_err(&pdev->dev, "Device Tree node missing\n"); |
| 331 | return -EINVAL; |
| 332 | } |
| 333 | |
| 334 | drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL); |
| 335 | if (!drvdata) |
| 336 | return -ENOMEM; |
| 337 | |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 338 | memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc)); |
| 339 | |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 340 | if (of_find_property(np, "voltage-table", NULL)) |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 341 | ret = pwm_regulator_init_table(pdev, drvdata); |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 342 | else |
| 343 | ret = pwm_regulator_init_continuous(pdev, drvdata); |
| 344 | if (ret) |
| 345 | return ret; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 346 | |
Lee Jones | 5ad2cb1 | 2015-07-07 16:06:53 +0100 | [diff] [blame] | 347 | init_data = of_get_regulator_init_data(&pdev->dev, np, |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 348 | &drvdata->desc); |
Lee Jones | 5ad2cb1 | 2015-07-07 16:06:53 +0100 | [diff] [blame] | 349 | if (!init_data) |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 350 | return -ENOMEM; |
| 351 | |
| 352 | config.of_node = np; |
| 353 | config.dev = &pdev->dev; |
| 354 | config.driver_data = drvdata; |
Lee Jones | 5ad2cb1 | 2015-07-07 16:06:53 +0100 | [diff] [blame] | 355 | config.init_data = init_data; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 356 | |
| 357 | drvdata->pwm = devm_pwm_get(&pdev->dev, NULL); |
| 358 | if (IS_ERR(drvdata->pwm)) { |
Laxman Dewangan | 5bf59bd | 2016-03-14 18:20:13 +0530 | [diff] [blame] | 359 | ret = PTR_ERR(drvdata->pwm); |
| 360 | dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret); |
| 361 | return ret; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 362 | } |
| 363 | |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 364 | if (init_data->constraints.boot_on || init_data->constraints.always_on) |
| 365 | gpio_flags = GPIOD_OUT_HIGH; |
| 366 | else |
| 367 | gpio_flags = GPIOD_OUT_LOW; |
| 368 | drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable", |
| 369 | gpio_flags); |
| 370 | if (IS_ERR(drvdata->enb_gpio)) { |
| 371 | ret = PTR_ERR(drvdata->enb_gpio); |
| 372 | dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret); |
| 373 | return ret; |
| 374 | } |
| 375 | |
Boris Brezillon | fd4f99c | 2016-06-14 11:13:17 +0200 | [diff] [blame] | 376 | ret = pwm_adjust_config(drvdata->pwm); |
| 377 | if (ret) |
| 378 | return ret; |
Boris Brezillon | 8c12ad8 | 2016-04-14 21:17:27 +0200 | [diff] [blame] | 379 | |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 380 | regulator = devm_regulator_register(&pdev->dev, |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 381 | &drvdata->desc, &config); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 382 | if (IS_ERR(regulator)) { |
Laxman Dewangan | 5bf59bd | 2016-03-14 18:20:13 +0530 | [diff] [blame] | 383 | ret = PTR_ERR(regulator); |
| 384 | dev_err(&pdev->dev, "Failed to register regulator %s: %d\n", |
| 385 | drvdata->desc.name, ret); |
| 386 | return ret; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static const struct of_device_id pwm_of_match[] = { |
| 393 | { .compatible = "pwm-regulator" }, |
| 394 | { }, |
| 395 | }; |
| 396 | MODULE_DEVICE_TABLE(of, pwm_of_match); |
| 397 | |
| 398 | static struct platform_driver pwm_regulator_driver = { |
| 399 | .driver = { |
| 400 | .name = "pwm-regulator", |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 401 | .of_match_table = of_match_ptr(pwm_of_match), |
| 402 | }, |
| 403 | .probe = pwm_regulator_probe, |
| 404 | }; |
| 405 | |
| 406 | module_platform_driver(pwm_regulator_driver); |
| 407 | |
| 408 | MODULE_LICENSE("GPL"); |
| 409 | MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>"); |
| 410 | MODULE_DESCRIPTION("PWM Regulator Driver"); |
| 411 | MODULE_ALIAS("platform:pwm-regulator"); |