0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/err.h>
0013 #include <linux/regulator/driver.h>
0014 #include <linux/regulator/machine.h>
0015 #include <linux/regulator/of_regulator.h>
0016 #include <linux/of.h>
0017 #include <linux/of_device.h>
0018 #include <linux/pwm.h>
0019 #include <linux/gpio/consumer.h>
0020
0021 struct pwm_continuous_reg_data {
0022 unsigned int min_uV_dutycycle;
0023 unsigned int max_uV_dutycycle;
0024 unsigned int dutycycle_unit;
0025 };
0026
0027 struct pwm_regulator_data {
0028
0029 struct pwm_device *pwm;
0030
0031
0032 struct pwm_voltages *duty_cycle_table;
0033
0034
0035 struct pwm_continuous_reg_data continuous;
0036
0037
0038 struct regulator_desc desc;
0039
0040 int state;
0041
0042
0043 struct gpio_desc *enb_gpio;
0044 };
0045
0046 struct pwm_voltages {
0047 unsigned int uV;
0048 unsigned int dutycycle;
0049 };
0050
0051
0052
0053
0054 static void pwm_regulator_init_state(struct regulator_dev *rdev)
0055 {
0056 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
0057 struct pwm_state pwm_state;
0058 unsigned int dutycycle;
0059 int i;
0060
0061 pwm_get_state(drvdata->pwm, &pwm_state);
0062 dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);
0063
0064 for (i = 0; i < rdev->desc->n_voltages; i++) {
0065 if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
0066 drvdata->state = i;
0067 return;
0068 }
0069 }
0070 }
0071
0072 static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
0073 {
0074 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
0075
0076 if (drvdata->state < 0)
0077 pwm_regulator_init_state(rdev);
0078
0079 return drvdata->state;
0080 }
0081
0082 static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
0083 unsigned selector)
0084 {
0085 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
0086 struct pwm_state pstate;
0087 int ret;
0088
0089 pwm_init_state(drvdata->pwm, &pstate);
0090 pwm_set_relative_duty_cycle(&pstate,
0091 drvdata->duty_cycle_table[selector].dutycycle, 100);
0092
0093 ret = pwm_apply_state(drvdata->pwm, &pstate);
0094 if (ret) {
0095 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
0096 return ret;
0097 }
0098
0099 drvdata->state = selector;
0100
0101 return 0;
0102 }
0103
0104 static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
0105 unsigned selector)
0106 {
0107 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
0108
0109 if (selector >= rdev->desc->n_voltages)
0110 return -EINVAL;
0111
0112 return drvdata->duty_cycle_table[selector].uV;
0113 }
0114
0115 static int pwm_regulator_enable(struct regulator_dev *dev)
0116 {
0117 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
0118
0119 gpiod_set_value_cansleep(drvdata->enb_gpio, 1);
0120
0121 return pwm_enable(drvdata->pwm);
0122 }
0123
0124 static int pwm_regulator_disable(struct regulator_dev *dev)
0125 {
0126 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
0127
0128 pwm_disable(drvdata->pwm);
0129
0130 gpiod_set_value_cansleep(drvdata->enb_gpio, 0);
0131
0132 return 0;
0133 }
0134
0135 static int pwm_regulator_is_enabled(struct regulator_dev *dev)
0136 {
0137 struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev);
0138
0139 if (drvdata->enb_gpio && !gpiod_get_value_cansleep(drvdata->enb_gpio))
0140 return false;
0141
0142 return pwm_is_enabled(drvdata->pwm);
0143 }
0144
0145 static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
0146 {
0147 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
0148 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
0149 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
0150 unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
0151 int min_uV = rdev->constraints->min_uV;
0152 int max_uV = rdev->constraints->max_uV;
0153 int diff_uV = max_uV - min_uV;
0154 struct pwm_state pstate;
0155 unsigned int diff_duty;
0156 unsigned int voltage;
0157
0158 pwm_get_state(drvdata->pwm, &pstate);
0159
0160 voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit);
0161
0162
0163
0164
0165
0166
0167 if (max_uV_duty < min_uV_duty) {
0168 voltage = min_uV_duty - voltage;
0169 diff_duty = min_uV_duty - max_uV_duty;
0170 } else {
0171 voltage = voltage - min_uV_duty;
0172 diff_duty = max_uV_duty - min_uV_duty;
0173 }
0174
0175 voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);
0176
0177 return voltage + min_uV;
0178 }
0179
0180 static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
0181 int req_min_uV, int req_max_uV,
0182 unsigned int *selector)
0183 {
0184 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
0185 unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
0186 unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
0187 unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
0188 int min_uV = rdev->constraints->min_uV;
0189 int max_uV = rdev->constraints->max_uV;
0190 int diff_uV = max_uV - min_uV;
0191 struct pwm_state pstate;
0192 unsigned int diff_duty;
0193 unsigned int dutycycle;
0194 int ret;
0195
0196 pwm_init_state(drvdata->pwm, &pstate);
0197
0198
0199
0200
0201
0202
0203 if (max_uV_duty < min_uV_duty)
0204 diff_duty = min_uV_duty - max_uV_duty;
0205 else
0206 diff_duty = max_uV_duty - min_uV_duty;
0207
0208 dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
0209 diff_duty,
0210 diff_uV);
0211
0212 if (max_uV_duty < min_uV_duty)
0213 dutycycle = min_uV_duty - dutycycle;
0214 else
0215 dutycycle = min_uV_duty + dutycycle;
0216
0217 pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit);
0218
0219 ret = pwm_apply_state(drvdata->pwm, &pstate);
0220 if (ret) {
0221 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
0222 return ret;
0223 }
0224
0225 return 0;
0226 }
0227
0228 static const struct regulator_ops pwm_regulator_voltage_table_ops = {
0229 .set_voltage_sel = pwm_regulator_set_voltage_sel,
0230 .get_voltage_sel = pwm_regulator_get_voltage_sel,
0231 .list_voltage = pwm_regulator_list_voltage,
0232 .map_voltage = regulator_map_voltage_iterate,
0233 .enable = pwm_regulator_enable,
0234 .disable = pwm_regulator_disable,
0235 .is_enabled = pwm_regulator_is_enabled,
0236 };
0237
0238 static const struct regulator_ops pwm_regulator_voltage_continuous_ops = {
0239 .get_voltage = pwm_regulator_get_voltage,
0240 .set_voltage = pwm_regulator_set_voltage,
0241 .enable = pwm_regulator_enable,
0242 .disable = pwm_regulator_disable,
0243 .is_enabled = pwm_regulator_is_enabled,
0244 };
0245
0246 static const struct regulator_desc pwm_regulator_desc = {
0247 .name = "pwm-regulator",
0248 .type = REGULATOR_VOLTAGE,
0249 .owner = THIS_MODULE,
0250 .supply_name = "pwm",
0251 };
0252
0253 static int pwm_regulator_init_table(struct platform_device *pdev,
0254 struct pwm_regulator_data *drvdata)
0255 {
0256 struct device_node *np = pdev->dev.of_node;
0257 struct pwm_voltages *duty_cycle_table;
0258 unsigned int length = 0;
0259 int ret;
0260
0261 of_find_property(np, "voltage-table", &length);
0262
0263 if ((length < sizeof(*duty_cycle_table)) ||
0264 (length % sizeof(*duty_cycle_table))) {
0265 dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n",
0266 length);
0267 return -EINVAL;
0268 }
0269
0270 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
0271 if (!duty_cycle_table)
0272 return -ENOMEM;
0273
0274 ret = of_property_read_u32_array(np, "voltage-table",
0275 (u32 *)duty_cycle_table,
0276 length / sizeof(u32));
0277 if (ret) {
0278 dev_err(&pdev->dev, "Failed to read voltage-table: %d\n", ret);
0279 return ret;
0280 }
0281
0282 drvdata->state = -ENOTRECOVERABLE;
0283 drvdata->duty_cycle_table = duty_cycle_table;
0284 drvdata->desc.ops = &pwm_regulator_voltage_table_ops;
0285 drvdata->desc.n_voltages = length / sizeof(*duty_cycle_table);
0286
0287 return 0;
0288 }
0289
0290 static int pwm_regulator_init_continuous(struct platform_device *pdev,
0291 struct pwm_regulator_data *drvdata)
0292 {
0293 u32 dutycycle_range[2] = { 0, 100 };
0294 u32 dutycycle_unit = 100;
0295
0296 drvdata->desc.ops = &pwm_regulator_voltage_continuous_ops;
0297 drvdata->desc.continuous_voltage_range = true;
0298
0299 of_property_read_u32_array(pdev->dev.of_node,
0300 "pwm-dutycycle-range",
0301 dutycycle_range, 2);
0302 of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit",
0303 &dutycycle_unit);
0304
0305 if (dutycycle_range[0] > dutycycle_unit ||
0306 dutycycle_range[1] > dutycycle_unit)
0307 return -EINVAL;
0308
0309 drvdata->continuous.dutycycle_unit = dutycycle_unit;
0310 drvdata->continuous.min_uV_dutycycle = dutycycle_range[0];
0311 drvdata->continuous.max_uV_dutycycle = dutycycle_range[1];
0312
0313 return 0;
0314 }
0315
0316 static int pwm_regulator_probe(struct platform_device *pdev)
0317 {
0318 const struct regulator_init_data *init_data;
0319 struct pwm_regulator_data *drvdata;
0320 struct regulator_dev *regulator;
0321 struct regulator_config config = { };
0322 struct device_node *np = pdev->dev.of_node;
0323 enum gpiod_flags gpio_flags;
0324 int ret;
0325
0326 if (!np) {
0327 dev_err(&pdev->dev, "Device Tree node missing\n");
0328 return -EINVAL;
0329 }
0330
0331 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
0332 if (!drvdata)
0333 return -ENOMEM;
0334
0335 memcpy(&drvdata->desc, &pwm_regulator_desc, sizeof(drvdata->desc));
0336
0337 if (of_find_property(np, "voltage-table", NULL))
0338 ret = pwm_regulator_init_table(pdev, drvdata);
0339 else
0340 ret = pwm_regulator_init_continuous(pdev, drvdata);
0341 if (ret)
0342 return ret;
0343
0344 init_data = of_get_regulator_init_data(&pdev->dev, np,
0345 &drvdata->desc);
0346 if (!init_data)
0347 return -ENOMEM;
0348
0349 config.of_node = np;
0350 config.dev = &pdev->dev;
0351 config.driver_data = drvdata;
0352 config.init_data = init_data;
0353
0354 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
0355 if (IS_ERR(drvdata->pwm))
0356 return dev_err_probe(&pdev->dev, PTR_ERR(drvdata->pwm),
0357 "Failed to get PWM\n");
0358
0359 if (init_data->constraints.boot_on || init_data->constraints.always_on)
0360 gpio_flags = GPIOD_OUT_HIGH;
0361 else
0362 gpio_flags = GPIOD_OUT_LOW;
0363 drvdata->enb_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
0364 gpio_flags);
0365 if (IS_ERR(drvdata->enb_gpio)) {
0366 ret = PTR_ERR(drvdata->enb_gpio);
0367 dev_err(&pdev->dev, "Failed to get enable GPIO: %d\n", ret);
0368 return ret;
0369 }
0370
0371 ret = pwm_adjust_config(drvdata->pwm);
0372 if (ret)
0373 return ret;
0374
0375 regulator = devm_regulator_register(&pdev->dev,
0376 &drvdata->desc, &config);
0377 if (IS_ERR(regulator)) {
0378 ret = PTR_ERR(regulator);
0379 dev_err(&pdev->dev, "Failed to register regulator %s: %d\n",
0380 drvdata->desc.name, ret);
0381 return ret;
0382 }
0383
0384 return 0;
0385 }
0386
0387 static const struct of_device_id __maybe_unused pwm_of_match[] = {
0388 { .compatible = "pwm-regulator" },
0389 { },
0390 };
0391 MODULE_DEVICE_TABLE(of, pwm_of_match);
0392
0393 static struct platform_driver pwm_regulator_driver = {
0394 .driver = {
0395 .name = "pwm-regulator",
0396 .of_match_table = of_match_ptr(pwm_of_match),
0397 },
0398 .probe = pwm_regulator_probe,
0399 };
0400
0401 module_platform_driver(pwm_regulator_driver);
0402
0403 MODULE_LICENSE("GPL");
0404 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
0405 MODULE_DESCRIPTION("PWM Regulator Driver");
0406 MODULE_ALIAS("platform:pwm-regulator");