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