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