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 | |
| 25 | struct pwm_regulator_data { |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 26 | /* Shared */ |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 27 | struct pwm_device *pwm; |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 28 | |
| 29 | /* Voltage table */ |
| 30 | struct pwm_voltages *duty_cycle_table; |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 31 | |
| 32 | /* regulator descriptor */ |
| 33 | struct regulator_desc desc; |
| 34 | |
| 35 | /* Regulator ops */ |
| 36 | struct regulator_ops ops; |
| 37 | |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 38 | int state; |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 39 | |
| 40 | /* Continuous voltage */ |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 41 | int volt_uV; |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 42 | |
| 43 | /* Enable GPIO */ |
| 44 | struct gpio_desc *enb_gpio; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct pwm_voltages { |
| 48 | unsigned int uV; |
| 49 | unsigned int dutycycle; |
| 50 | }; |
| 51 | |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 52 | /** |
| 53 | * Voltage table call-backs |
| 54 | */ |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 55 | static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev) |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 56 | { |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 57 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 58 | |
| 59 | return drvdata->state; |
| 60 | } |
| 61 | |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 62 | static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev, |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 63 | unsigned selector) |
| 64 | { |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 65 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
Boris Brezillon | 8c12ad8 | 2016-04-14 21:17:27 +0200 | [diff] [blame] | 66 | struct pwm_args pargs; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 67 | int dutycycle; |
| 68 | int ret; |
| 69 | |
Boris Brezillon | 8c12ad8 | 2016-04-14 21:17:27 +0200 | [diff] [blame] | 70 | pwm_get_args(drvdata->pwm, &pargs); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 71 | |
Boris Brezillon | 8c12ad8 | 2016-04-14 21:17:27 +0200 | [diff] [blame] | 72 | dutycycle = (pargs.period * |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 73 | drvdata->duty_cycle_table[selector].dutycycle) / 100; |
| 74 | |
Boris Brezillon | 8c12ad8 | 2016-04-14 21:17:27 +0200 | [diff] [blame] | 75 | ret = pwm_config(drvdata->pwm, dutycycle, pargs.period); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 76 | if (ret) { |
Laxman Dewangan | 5bf59bd | 2016-03-14 18:20:13 +0530 | [diff] [blame] | 77 | dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | drvdata->state = selector; |
| 82 | |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 86 | static int pwm_regulator_list_voltage(struct regulator_dev *rdev, |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 87 | unsigned selector) |
| 88 | { |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 89 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 90 | |
Lee Jones | ab101e3 | 2015-06-05 19:42:47 +0100 | [diff] [blame] | 91 | if (selector >= rdev->desc->n_voltages) |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 92 | return -EINVAL; |
| 93 | |
| 94 | return drvdata->duty_cycle_table[selector].uV; |
| 95 | } |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 96 | |
Boris Brezillon | 1de7d80 | 2015-09-21 11:33:28 +0200 | [diff] [blame] | 97 | static int pwm_regulator_enable(struct regulator_dev *dev) |
| 98 | { |
| 99 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev); |
| 100 | |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 101 | if (drvdata->enb_gpio) |
| 102 | gpiod_set_value_cansleep(drvdata->enb_gpio, 1); |
| 103 | |
Boris Brezillon | 1de7d80 | 2015-09-21 11:33:28 +0200 | [diff] [blame] | 104 | return pwm_enable(drvdata->pwm); |
| 105 | } |
| 106 | |
| 107 | static int pwm_regulator_disable(struct regulator_dev *dev) |
| 108 | { |
| 109 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev); |
| 110 | |
| 111 | pwm_disable(drvdata->pwm); |
| 112 | |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 113 | if (drvdata->enb_gpio) |
| 114 | gpiod_set_value_cansleep(drvdata->enb_gpio, 0); |
| 115 | |
Boris Brezillon | 1de7d80 | 2015-09-21 11:33:28 +0200 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int pwm_regulator_is_enabled(struct regulator_dev *dev) |
| 120 | { |
| 121 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev); |
| 122 | |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 123 | if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio)) |
| 124 | return false; |
| 125 | |
Boris Brezillon | 1de7d80 | 2015-09-21 11:33:28 +0200 | [diff] [blame] | 126 | return pwm_is_enabled(drvdata->pwm); |
| 127 | } |
| 128 | |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 129 | static int pwm_regulator_get_voltage(struct regulator_dev *rdev) |
| 130 | { |
| 131 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
| 132 | |
| 133 | return drvdata->volt_uV; |
| 134 | } |
| 135 | |
| 136 | static int pwm_regulator_set_voltage(struct regulator_dev *rdev, |
| 137 | int min_uV, int max_uV, |
| 138 | unsigned *selector) |
| 139 | { |
| 140 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
| 141 | unsigned int ramp_delay = rdev->constraints->ramp_delay; |
Boris Brezillon | 8c12ad8 | 2016-04-14 21:17:27 +0200 | [diff] [blame] | 142 | struct pwm_args pargs; |
Laxman Dewangan | fd786fb0 | 2016-04-05 15:09:48 +0530 | [diff] [blame] | 143 | unsigned int req_diff = min_uV - rdev->constraints->min_uV; |
| 144 | unsigned int diff; |
| 145 | unsigned int duty_pulse; |
| 146 | u64 req_period; |
| 147 | u32 rem; |
Douglas Anderson | c258839 | 2016-07-06 11:42:01 -0700 | [diff] [blame] | 148 | int old_uV = pwm_regulator_get_voltage(rdev); |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 149 | int ret; |
| 150 | |
Boris Brezillon | 8c12ad8 | 2016-04-14 21:17:27 +0200 | [diff] [blame] | 151 | pwm_get_args(drvdata->pwm, &pargs); |
Laxman Dewangan | fd786fb0 | 2016-04-05 15:09:48 +0530 | [diff] [blame] | 152 | diff = rdev->constraints->max_uV - rdev->constraints->min_uV; |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 153 | |
Laxman Dewangan | fd786fb0 | 2016-04-05 15:09:48 +0530 | [diff] [blame] | 154 | /* First try to find out if we get the iduty cycle time which is |
| 155 | * factor of PWM period time. If (request_diff_to_min * pwm_period) |
| 156 | * is perfect divided by voltage_range_diff then it is possible to |
| 157 | * get duty cycle time which is factor of PWM period. This will help |
| 158 | * to get output voltage nearer to requested value as there is no |
| 159 | * calculation loss. |
| 160 | */ |
Mark Brown | bc0868c | 2016-05-03 15:09:56 +0100 | [diff] [blame] | 161 | req_period = req_diff * pargs.period; |
Laxman Dewangan | fd786fb0 | 2016-04-05 15:09:48 +0530 | [diff] [blame] | 162 | div_u64_rem(req_period, diff, &rem); |
| 163 | if (!rem) { |
| 164 | do_div(req_period, diff); |
| 165 | duty_pulse = (unsigned int)req_period; |
| 166 | } else { |
Mark Brown | bc0868c | 2016-05-03 15:09:56 +0100 | [diff] [blame] | 167 | duty_pulse = (pargs.period / 100) * ((req_diff * 100) / diff); |
Laxman Dewangan | fd786fb0 | 2016-04-05 15:09:48 +0530 | [diff] [blame] | 168 | } |
| 169 | |
Mark Brown | bc0868c | 2016-05-03 15:09:56 +0100 | [diff] [blame] | 170 | ret = pwm_config(drvdata->pwm, duty_pulse, pargs.period); |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 171 | if (ret) { |
Laxman Dewangan | 5bf59bd | 2016-03-14 18:20:13 +0530 | [diff] [blame] | 172 | dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret); |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 173 | return ret; |
| 174 | } |
| 175 | |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 176 | drvdata->volt_uV = min_uV; |
| 177 | |
Douglas Anderson | c258839 | 2016-07-06 11:42:01 -0700 | [diff] [blame] | 178 | if ((ramp_delay == 0) || !pwm_regulator_is_enabled(rdev)) |
| 179 | return 0; |
| 180 | |
| 181 | /* Ramp delay is in uV/uS. Adjust to uS and delay */ |
| 182 | ramp_delay = DIV_ROUND_UP(abs(min_uV - old_uV), ramp_delay); |
| 183 | usleep_range(ramp_delay, ramp_delay + DIV_ROUND_UP(ramp_delay, 10)); |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 188 | static struct regulator_ops pwm_regulator_voltage_table_ops = { |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 189 | .set_voltage_sel = pwm_regulator_set_voltage_sel, |
| 190 | .get_voltage_sel = pwm_regulator_get_voltage_sel, |
| 191 | .list_voltage = pwm_regulator_list_voltage, |
| 192 | .map_voltage = regulator_map_voltage_iterate, |
Boris Brezillon | 1de7d80 | 2015-09-21 11:33:28 +0200 | [diff] [blame] | 193 | .enable = pwm_regulator_enable, |
| 194 | .disable = pwm_regulator_disable, |
| 195 | .is_enabled = pwm_regulator_is_enabled, |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 196 | }; |
| 197 | |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 198 | static struct regulator_ops pwm_regulator_voltage_continuous_ops = { |
| 199 | .get_voltage = pwm_regulator_get_voltage, |
| 200 | .set_voltage = pwm_regulator_set_voltage, |
Boris Brezillon | 1de7d80 | 2015-09-21 11:33:28 +0200 | [diff] [blame] | 201 | .enable = pwm_regulator_enable, |
| 202 | .disable = pwm_regulator_disable, |
| 203 | .is_enabled = pwm_regulator_is_enabled, |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 204 | }; |
| 205 | |
Lee Jones | b6f55e7 | 2015-06-05 19:42:45 +0100 | [diff] [blame] | 206 | static struct regulator_desc pwm_regulator_desc = { |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 207 | .name = "pwm-regulator", |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 208 | .type = REGULATOR_VOLTAGE, |
| 209 | .owner = THIS_MODULE, |
| 210 | .supply_name = "pwm", |
| 211 | }; |
| 212 | |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 213 | static int pwm_regulator_init_table(struct platform_device *pdev, |
| 214 | struct pwm_regulator_data *drvdata) |
| 215 | { |
| 216 | struct device_node *np = pdev->dev.of_node; |
| 217 | struct pwm_voltages *duty_cycle_table; |
Lee Jones | 60cb65e | 2015-07-10 08:45:36 +0100 | [diff] [blame] | 218 | unsigned int length = 0; |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 219 | int ret; |
| 220 | |
| 221 | of_find_property(np, "voltage-table", &length); |
| 222 | |
| 223 | if ((length < sizeof(*duty_cycle_table)) || |
| 224 | (length % sizeof(*duty_cycle_table))) { |
Laxman Dewangan | 5bf59bd | 2016-03-14 18:20:13 +0530 | [diff] [blame] | 225 | dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n", |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 226 | length); |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
| 230 | duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL); |
| 231 | if (!duty_cycle_table) |
| 232 | return -ENOMEM; |
| 233 | |
| 234 | ret = of_property_read_u32_array(np, "voltage-table", |
| 235 | (u32 *)duty_cycle_table, |
| 236 | length / sizeof(u32)); |
| 237 | if (ret) { |
Laxman Dewangan | 5bf59bd | 2016-03-14 18:20:13 +0530 | [diff] [blame] | 238 | dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret); |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | drvdata->duty_cycle_table = duty_cycle_table; |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 243 | memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops, |
| 244 | sizeof(drvdata->ops)); |
| 245 | drvdata->desc.ops = &drvdata->ops; |
| 246 | drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table); |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 251 | static int pwm_regulator_init_continuous(struct platform_device *pdev, |
| 252 | struct pwm_regulator_data *drvdata) |
| 253 | { |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 254 | memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops, |
| 255 | sizeof(drvdata->ops)); |
| 256 | drvdata->desc.ops = &drvdata->ops; |
| 257 | drvdata->desc.continuous_voltage_range = true; |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 262 | static int pwm_regulator_probe(struct platform_device *pdev) |
| 263 | { |
Lee Jones | 5ad2cb1 | 2015-07-07 16:06:53 +0100 | [diff] [blame] | 264 | const struct regulator_init_data *init_data; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 265 | struct pwm_regulator_data *drvdata; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 266 | struct regulator_dev *regulator; |
| 267 | struct regulator_config config = { }; |
| 268 | struct device_node *np = pdev->dev.of_node; |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 269 | enum gpiod_flags gpio_flags; |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 270 | int ret; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 271 | |
| 272 | if (!np) { |
| 273 | dev_err(&pdev->dev, "Device Tree node missing\n"); |
| 274 | return -EINVAL; |
| 275 | } |
| 276 | |
| 277 | drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL); |
| 278 | if (!drvdata) |
| 279 | return -ENOMEM; |
| 280 | |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 281 | memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc)); |
| 282 | |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 283 | if (of_find_property(np, "voltage-table", NULL)) |
Lee Jones | f9178da | 2015-07-06 09:58:29 +0100 | [diff] [blame] | 284 | ret = pwm_regulator_init_table(pdev, drvdata); |
Lee Jones | 4773be1 | 2015-07-07 16:06:51 +0100 | [diff] [blame] | 285 | else |
| 286 | ret = pwm_regulator_init_continuous(pdev, drvdata); |
| 287 | if (ret) |
| 288 | return ret; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 289 | |
Lee Jones | 5ad2cb1 | 2015-07-07 16:06:53 +0100 | [diff] [blame] | 290 | init_data = of_get_regulator_init_data(&pdev->dev, np, |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 291 | &drvdata->desc); |
Lee Jones | 5ad2cb1 | 2015-07-07 16:06:53 +0100 | [diff] [blame] | 292 | if (!init_data) |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 293 | return -ENOMEM; |
| 294 | |
| 295 | config.of_node = np; |
| 296 | config.dev = &pdev->dev; |
| 297 | config.driver_data = drvdata; |
Lee Jones | 5ad2cb1 | 2015-07-07 16:06:53 +0100 | [diff] [blame] | 298 | config.init_data = init_data; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 299 | |
| 300 | drvdata->pwm = devm_pwm_get(&pdev->dev, NULL); |
| 301 | if (IS_ERR(drvdata->pwm)) { |
Laxman Dewangan | 5bf59bd | 2016-03-14 18:20:13 +0530 | [diff] [blame] | 302 | ret = PTR_ERR(drvdata->pwm); |
| 303 | dev_err(&pdev->dev, "Failed to get PWM: %d\n", ret); |
| 304 | return ret; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 305 | } |
| 306 | |
Alexandre Courbot | 27bfa88 | 2016-06-23 16:39:44 +0900 | [diff] [blame] | 307 | if (init_data->constraints.boot_on || init_data->constraints.always_on) |
| 308 | gpio_flags = GPIOD_OUT_HIGH; |
| 309 | else |
| 310 | gpio_flags = GPIOD_OUT_LOW; |
| 311 | drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable", |
| 312 | gpio_flags); |
| 313 | if (IS_ERR(drvdata->enb_gpio)) { |
| 314 | ret = PTR_ERR(drvdata->enb_gpio); |
| 315 | dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret); |
| 316 | return ret; |
| 317 | } |
| 318 | |
Boris Brezillon | fd4f99c | 2016-06-14 11:13:17 +0200 | [diff] [blame^] | 319 | ret = pwm_adjust_config(drvdata->pwm); |
| 320 | if (ret) |
| 321 | return ret; |
Boris Brezillon | 8c12ad8 | 2016-04-14 21:17:27 +0200 | [diff] [blame] | 322 | |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 323 | regulator = devm_regulator_register(&pdev->dev, |
Laxman Dewangan | f907a0a | 2016-03-08 16:23:22 +0530 | [diff] [blame] | 324 | &drvdata->desc, &config); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 325 | if (IS_ERR(regulator)) { |
Laxman Dewangan | 5bf59bd | 2016-03-14 18:20:13 +0530 | [diff] [blame] | 326 | ret = PTR_ERR(regulator); |
| 327 | dev_err(&pdev->dev, "Failed to register regulator %s: %d\n", |
| 328 | drvdata->desc.name, ret); |
| 329 | return ret; |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static const struct of_device_id pwm_of_match[] = { |
| 336 | { .compatible = "pwm-regulator" }, |
| 337 | { }, |
| 338 | }; |
| 339 | MODULE_DEVICE_TABLE(of, pwm_of_match); |
| 340 | |
| 341 | static struct platform_driver pwm_regulator_driver = { |
| 342 | .driver = { |
| 343 | .name = "pwm-regulator", |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 344 | .of_match_table = of_match_ptr(pwm_of_match), |
| 345 | }, |
| 346 | .probe = pwm_regulator_probe, |
| 347 | }; |
| 348 | |
| 349 | module_platform_driver(pwm_regulator_driver); |
| 350 | |
| 351 | MODULE_LICENSE("GPL"); |
| 352 | MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>"); |
| 353 | MODULE_DESCRIPTION("PWM Regulator Driver"); |
| 354 | MODULE_ALIAS("platform:pwm-regulator"); |