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