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> |
| 22 | |
| 23 | struct pwm_regulator_data { |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 24 | struct pwm_voltages *duty_cycle_table; |
| 25 | struct pwm_device *pwm; |
| 26 | bool enabled; |
| 27 | int state; |
| 28 | }; |
| 29 | |
| 30 | struct pwm_voltages { |
| 31 | unsigned int uV; |
| 32 | unsigned int dutycycle; |
| 33 | }; |
| 34 | |
| 35 | static int pwm_regulator_get_voltage_sel(struct regulator_dev *dev) |
| 36 | { |
| 37 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev); |
| 38 | |
| 39 | return drvdata->state; |
| 40 | } |
| 41 | |
| 42 | static int pwm_regulator_set_voltage_sel(struct regulator_dev *dev, |
| 43 | unsigned selector) |
| 44 | { |
| 45 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev); |
| 46 | unsigned int pwm_reg_period; |
| 47 | int dutycycle; |
| 48 | int ret; |
| 49 | |
| 50 | pwm_reg_period = pwm_get_period(drvdata->pwm); |
| 51 | |
| 52 | dutycycle = (pwm_reg_period * |
| 53 | drvdata->duty_cycle_table[selector].dutycycle) / 100; |
| 54 | |
| 55 | ret = pwm_config(drvdata->pwm, dutycycle, pwm_reg_period); |
| 56 | if (ret) { |
| 57 | dev_err(&dev->dev, "Failed to configure PWM\n"); |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | drvdata->state = selector; |
| 62 | |
| 63 | if (!drvdata->enabled) { |
| 64 | ret = pwm_enable(drvdata->pwm); |
| 65 | if (ret) { |
| 66 | dev_err(&dev->dev, "Failed to enable PWM\n"); |
| 67 | return ret; |
| 68 | } |
| 69 | drvdata->enabled = true; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int pwm_regulator_list_voltage(struct regulator_dev *dev, |
| 76 | unsigned selector) |
| 77 | { |
| 78 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(dev); |
| 79 | |
Lee Jones | b6f55e7 | 2015-06-05 19:42:45 +0100 | [diff] [blame^] | 80 | if (selector >= dev->desc->n_voltages) |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 81 | return -EINVAL; |
| 82 | |
| 83 | return drvdata->duty_cycle_table[selector].uV; |
| 84 | } |
| 85 | |
| 86 | static struct regulator_ops pwm_regulator_voltage_ops = { |
| 87 | .set_voltage_sel = pwm_regulator_set_voltage_sel, |
| 88 | .get_voltage_sel = pwm_regulator_get_voltage_sel, |
| 89 | .list_voltage = pwm_regulator_list_voltage, |
| 90 | .map_voltage = regulator_map_voltage_iterate, |
| 91 | }; |
| 92 | |
Lee Jones | b6f55e7 | 2015-06-05 19:42:45 +0100 | [diff] [blame^] | 93 | static struct regulator_desc pwm_regulator_desc = { |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 94 | .name = "pwm-regulator", |
| 95 | .ops = &pwm_regulator_voltage_ops, |
| 96 | .type = REGULATOR_VOLTAGE, |
| 97 | .owner = THIS_MODULE, |
| 98 | .supply_name = "pwm", |
| 99 | }; |
| 100 | |
| 101 | static int pwm_regulator_probe(struct platform_device *pdev) |
| 102 | { |
| 103 | struct pwm_regulator_data *drvdata; |
| 104 | struct property *prop; |
| 105 | struct regulator_dev *regulator; |
| 106 | struct regulator_config config = { }; |
| 107 | struct device_node *np = pdev->dev.of_node; |
| 108 | int length, ret; |
| 109 | |
| 110 | if (!np) { |
| 111 | dev_err(&pdev->dev, "Device Tree node missing\n"); |
| 112 | return -EINVAL; |
| 113 | } |
| 114 | |
| 115 | drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL); |
| 116 | if (!drvdata) |
| 117 | return -ENOMEM; |
| 118 | |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 119 | /* determine the number of voltage-table */ |
| 120 | prop = of_find_property(np, "voltage-table", &length); |
| 121 | if (!prop) { |
| 122 | dev_err(&pdev->dev, "No voltage-table\n"); |
| 123 | return -EINVAL; |
| 124 | } |
| 125 | |
| 126 | if ((length < sizeof(*drvdata->duty_cycle_table)) || |
| 127 | (length % sizeof(*drvdata->duty_cycle_table))) { |
| 128 | dev_err(&pdev->dev, "voltage-table length(%d) is invalid\n", |
| 129 | length); |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | |
Lee Jones | b6f55e7 | 2015-06-05 19:42:45 +0100 | [diff] [blame^] | 133 | pwm_regulator_desc.n_voltages = length / sizeof(*drvdata->duty_cycle_table); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 134 | |
| 135 | drvdata->duty_cycle_table = devm_kzalloc(&pdev->dev, |
| 136 | length, GFP_KERNEL); |
| 137 | if (!drvdata->duty_cycle_table) |
| 138 | return -ENOMEM; |
| 139 | |
| 140 | /* read voltage table from DT property */ |
| 141 | ret = of_property_read_u32_array(np, "voltage-table", |
| 142 | (u32 *)drvdata->duty_cycle_table, |
| 143 | length / sizeof(u32)); |
| 144 | if (ret < 0) { |
| 145 | dev_err(&pdev->dev, "read voltage-table failed\n"); |
| 146 | return ret; |
| 147 | } |
| 148 | |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 149 | config.init_data = of_get_regulator_init_data(&pdev->dev, np, |
Lee Jones | b6f55e7 | 2015-06-05 19:42:45 +0100 | [diff] [blame^] | 150 | &pwm_regulator_desc); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 151 | if (!config.init_data) |
| 152 | return -ENOMEM; |
| 153 | |
| 154 | config.of_node = np; |
| 155 | config.dev = &pdev->dev; |
| 156 | config.driver_data = drvdata; |
| 157 | |
| 158 | drvdata->pwm = devm_pwm_get(&pdev->dev, NULL); |
| 159 | if (IS_ERR(drvdata->pwm)) { |
| 160 | dev_err(&pdev->dev, "Failed to get PWM\n"); |
| 161 | return PTR_ERR(drvdata->pwm); |
| 162 | } |
| 163 | |
| 164 | regulator = devm_regulator_register(&pdev->dev, |
Lee Jones | b6f55e7 | 2015-06-05 19:42:45 +0100 | [diff] [blame^] | 165 | &pwm_regulator_desc, &config); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 166 | if (IS_ERR(regulator)) { |
| 167 | dev_err(&pdev->dev, "Failed to register regulator %s\n", |
Lee Jones | b6f55e7 | 2015-06-05 19:42:45 +0100 | [diff] [blame^] | 168 | pwm_regulator_desc.name); |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 169 | return PTR_ERR(regulator); |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static const struct of_device_id pwm_of_match[] = { |
| 176 | { .compatible = "pwm-regulator" }, |
| 177 | { }, |
| 178 | }; |
| 179 | MODULE_DEVICE_TABLE(of, pwm_of_match); |
| 180 | |
| 181 | static struct platform_driver pwm_regulator_driver = { |
| 182 | .driver = { |
| 183 | .name = "pwm-regulator", |
Chris Zhong | aa66cc6 | 2014-09-28 10:28:53 +0800 | [diff] [blame] | 184 | .of_match_table = of_match_ptr(pwm_of_match), |
| 185 | }, |
| 186 | .probe = pwm_regulator_probe, |
| 187 | }; |
| 188 | |
| 189 | module_platform_driver(pwm_regulator_driver); |
| 190 | |
| 191 | MODULE_LICENSE("GPL"); |
| 192 | MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>"); |
| 193 | MODULE_DESCRIPTION("PWM Regulator Driver"); |
| 194 | MODULE_ALIAS("platform:pwm-regulator"); |