blob: 25560fc519b757139fe868428ac827aec696cc12 [file] [log] [blame]
Chris Zhongaa66cc62014-09-28 10:28:53 +08001/*
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
23struct pwm_regulator_data {
Chris Zhongaa66cc62014-09-28 10:28:53 +080024 struct pwm_voltages *duty_cycle_table;
25 struct pwm_device *pwm;
Chris Zhongaa66cc62014-09-28 10:28:53 +080026 int state;
27};
28
29struct pwm_voltages {
30 unsigned int uV;
31 unsigned int dutycycle;
32};
33
Lee Jonesab101e32015-06-05 19:42:47 +010034static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
Chris Zhongaa66cc62014-09-28 10:28:53 +080035{
Lee Jonesab101e32015-06-05 19:42:47 +010036 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080037
38 return drvdata->state;
39}
40
Lee Jonesab101e32015-06-05 19:42:47 +010041static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080042 unsigned selector)
43{
Lee Jonesab101e32015-06-05 19:42:47 +010044 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080045 unsigned int pwm_reg_period;
46 int dutycycle;
47 int ret;
48
49 pwm_reg_period = pwm_get_period(drvdata->pwm);
50
51 dutycycle = (pwm_reg_period *
52 drvdata->duty_cycle_table[selector].dutycycle) / 100;
53
54 ret = pwm_config(drvdata->pwm, dutycycle, pwm_reg_period);
55 if (ret) {
Lee Jonesab101e32015-06-05 19:42:47 +010056 dev_err(&rdev->dev, "Failed to configure PWM\n");
Chris Zhongaa66cc62014-09-28 10:28:53 +080057 return ret;
58 }
59
60 drvdata->state = selector;
61
Lee Jonesc779ceb2015-06-05 19:42:46 +010062 ret = pwm_enable(drvdata->pwm);
63 if (ret) {
Lee Jonesab101e32015-06-05 19:42:47 +010064 dev_err(&rdev->dev, "Failed to enable PWM\n");
Lee Jonesc779ceb2015-06-05 19:42:46 +010065 return ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +080066 }
67
68 return 0;
69}
70
Lee Jonesab101e32015-06-05 19:42:47 +010071static int pwm_regulator_list_voltage(struct regulator_dev *rdev,
Chris Zhongaa66cc62014-09-28 10:28:53 +080072 unsigned selector)
73{
Lee Jonesab101e32015-06-05 19:42:47 +010074 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
Chris Zhongaa66cc62014-09-28 10:28:53 +080075
Lee Jonesab101e32015-06-05 19:42:47 +010076 if (selector >= rdev->desc->n_voltages)
Chris Zhongaa66cc62014-09-28 10:28:53 +080077 return -EINVAL;
78
79 return drvdata->duty_cycle_table[selector].uV;
80}
Lee Jonesf9178da2015-07-06 09:58:29 +010081static struct regulator_ops pwm_regulator_voltage_table_ops = {
Chris Zhongaa66cc62014-09-28 10:28:53 +080082 .set_voltage_sel = pwm_regulator_set_voltage_sel,
83 .get_voltage_sel = pwm_regulator_get_voltage_sel,
84 .list_voltage = pwm_regulator_list_voltage,
85 .map_voltage = regulator_map_voltage_iterate,
86};
87
Lee Jonesb6f55e72015-06-05 19:42:45 +010088static struct regulator_desc pwm_regulator_desc = {
Chris Zhongaa66cc62014-09-28 10:28:53 +080089 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +080090 .type = REGULATOR_VOLTAGE,
91 .owner = THIS_MODULE,
92 .supply_name = "pwm",
93};
94
Lee Jonesf9178da2015-07-06 09:58:29 +010095static int pwm_regulator_init_table(struct platform_device *pdev,
96 struct pwm_regulator_data *drvdata)
97{
98 struct device_node *np = pdev->dev.of_node;
99 struct pwm_voltages *duty_cycle_table;
100 int length;
101 int ret;
102
103 of_find_property(np, "voltage-table", &length);
104
105 if ((length < sizeof(*duty_cycle_table)) ||
106 (length % sizeof(*duty_cycle_table))) {
107 dev_err(&pdev->dev,
108 "voltage-table length(%d) is invalid\n",
109 length);
110 return -EINVAL;
111 }
112
113 duty_cycle_table = devm_kzalloc(&pdev->dev, length, GFP_KERNEL);
114 if (!duty_cycle_table)
115 return -ENOMEM;
116
117 ret = of_property_read_u32_array(np, "voltage-table",
118 (u32 *)duty_cycle_table,
119 length / sizeof(u32));
120 if (ret) {
121 dev_err(&pdev->dev, "Failed to read voltage-table\n");
122 return ret;
123 }
124
125 drvdata->duty_cycle_table = duty_cycle_table;
126 pwm_regulator_desc.ops = &pwm_regulator_voltage_table_ops;
127 pwm_regulator_desc.n_voltages = length / sizeof(*duty_cycle_table);
128
129 return 0;
130}
131
Chris Zhongaa66cc62014-09-28 10:28:53 +0800132static int pwm_regulator_probe(struct platform_device *pdev)
133{
134 struct pwm_regulator_data *drvdata;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800135 struct regulator_dev *regulator;
136 struct regulator_config config = { };
137 struct device_node *np = pdev->dev.of_node;
Lee Jonesf9178da2015-07-06 09:58:29 +0100138 int ret;
Chris Zhongaa66cc62014-09-28 10:28:53 +0800139
140 if (!np) {
141 dev_err(&pdev->dev, "Device Tree node missing\n");
142 return -EINVAL;
143 }
144
145 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
146 if (!drvdata)
147 return -ENOMEM;
148
Lee Jonesf9178da2015-07-06 09:58:29 +0100149 if (of_find_property(np, "voltage-table", NULL)) {
150 ret = pwm_regulator_init_table(pdev, drvdata);
151 if (ret)
152 return ret;
153 } else {
154 dev_err(&pdev->dev, "No \"voltage-table\" supplied\n");
Chris Zhongaa66cc62014-09-28 10:28:53 +0800155 return -EINVAL;
156 }
157
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100158 config.init_data = of_get_regulator_init_data(&pdev->dev, np,
Lee Jonesb6f55e72015-06-05 19:42:45 +0100159 &pwm_regulator_desc);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800160 if (!config.init_data)
161 return -ENOMEM;
162
163 config.of_node = np;
164 config.dev = &pdev->dev;
165 config.driver_data = drvdata;
166
167 drvdata->pwm = devm_pwm_get(&pdev->dev, NULL);
168 if (IS_ERR(drvdata->pwm)) {
169 dev_err(&pdev->dev, "Failed to get PWM\n");
170 return PTR_ERR(drvdata->pwm);
171 }
172
173 regulator = devm_regulator_register(&pdev->dev,
Lee Jonesb6f55e72015-06-05 19:42:45 +0100174 &pwm_regulator_desc, &config);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800175 if (IS_ERR(regulator)) {
176 dev_err(&pdev->dev, "Failed to register regulator %s\n",
Lee Jonesb6f55e72015-06-05 19:42:45 +0100177 pwm_regulator_desc.name);
Chris Zhongaa66cc62014-09-28 10:28:53 +0800178 return PTR_ERR(regulator);
179 }
180
181 return 0;
182}
183
184static const struct of_device_id pwm_of_match[] = {
185 { .compatible = "pwm-regulator" },
186 { },
187};
188MODULE_DEVICE_TABLE(of, pwm_of_match);
189
190static struct platform_driver pwm_regulator_driver = {
191 .driver = {
192 .name = "pwm-regulator",
Chris Zhongaa66cc62014-09-28 10:28:53 +0800193 .of_match_table = of_match_ptr(pwm_of_match),
194 },
195 .probe = pwm_regulator_probe,
196};
197
198module_platform_driver(pwm_regulator_driver);
199
200MODULE_LICENSE("GPL");
201MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
202MODULE_DESCRIPTION("PWM Regulator Driver");
203MODULE_ALIAS("platform:pwm-regulator");