Commit | Line | Data |
---|---|---|
aa66cc66 CZ |
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 { | |
aa66cc66 CZ |
24 | struct pwm_voltages *duty_cycle_table; |
25 | struct pwm_device *pwm; | |
aa66cc66 CZ |
26 | int state; |
27 | }; | |
28 | ||
29 | struct pwm_voltages { | |
30 | unsigned int uV; | |
31 | unsigned int dutycycle; | |
32 | }; | |
33 | ||
ab101e35 | 34 | static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev) |
aa66cc66 | 35 | { |
ab101e35 | 36 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
aa66cc66 CZ |
37 | |
38 | return drvdata->state; | |
39 | } | |
40 | ||
ab101e35 | 41 | static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev, |
aa66cc66 CZ |
42 | unsigned selector) |
43 | { | |
ab101e35 | 44 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
aa66cc66 CZ |
45 | 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) { | |
ab101e35 | 56 | dev_err(&rdev->dev, "Failed to configure PWM\n"); |
aa66cc66 CZ |
57 | return ret; |
58 | } | |
59 | ||
60 | drvdata->state = selector; | |
61 | ||
c779cebb LJ |
62 | ret = pwm_enable(drvdata->pwm); |
63 | if (ret) { | |
ab101e35 | 64 | dev_err(&rdev->dev, "Failed to enable PWM\n"); |
c779cebb | 65 | return ret; |
aa66cc66 CZ |
66 | } |
67 | ||
68 | return 0; | |
69 | } | |
70 | ||
ab101e35 | 71 | static int pwm_regulator_list_voltage(struct regulator_dev *rdev, |
aa66cc66 CZ |
72 | unsigned selector) |
73 | { | |
ab101e35 | 74 | struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev); |
aa66cc66 | 75 | |
ab101e35 | 76 | if (selector >= rdev->desc->n_voltages) |
aa66cc66 CZ |
77 | return -EINVAL; |
78 | ||
79 | return drvdata->duty_cycle_table[selector].uV; | |
80 | } | |
f9178dad | 81 | static struct regulator_ops pwm_regulator_voltage_table_ops = { |
aa66cc66 CZ |
82 | .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 | ||
b6f55e74 | 88 | static struct regulator_desc pwm_regulator_desc = { |
aa66cc66 | 89 | .name = "pwm-regulator", |
aa66cc66 CZ |
90 | .type = REGULATOR_VOLTAGE, |
91 | .owner = THIS_MODULE, | |
92 | .supply_name = "pwm", | |
93 | }; | |
94 | ||
f9178dad LJ |
95 | static 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 | ||
aa66cc66 CZ |
132 | static int pwm_regulator_probe(struct platform_device *pdev) |
133 | { | |
134 | struct pwm_regulator_data *drvdata; | |
aa66cc66 CZ |
135 | struct regulator_dev *regulator; |
136 | struct regulator_config config = { }; | |
137 | struct device_node *np = pdev->dev.of_node; | |
f9178dad | 138 | int ret; |
aa66cc66 CZ |
139 | |
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 | ||
f9178dad LJ |
149 | 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"); | |
aa66cc66 CZ |
155 | return -EINVAL; |
156 | } | |
157 | ||
072e78b1 | 158 | config.init_data = of_get_regulator_init_data(&pdev->dev, np, |
b6f55e74 | 159 | &pwm_regulator_desc); |
aa66cc66 CZ |
160 | 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, | |
b6f55e74 | 174 | &pwm_regulator_desc, &config); |
aa66cc66 CZ |
175 | if (IS_ERR(regulator)) { |
176 | dev_err(&pdev->dev, "Failed to register regulator %s\n", | |
b6f55e74 | 177 | pwm_regulator_desc.name); |
aa66cc66 CZ |
178 | return PTR_ERR(regulator); |
179 | } | |
180 | ||
181 | return 0; | |
182 | } | |
183 | ||
184 | static const struct of_device_id pwm_of_match[] = { | |
185 | { .compatible = "pwm-regulator" }, | |
186 | { }, | |
187 | }; | |
188 | MODULE_DEVICE_TABLE(of, pwm_of_match); | |
189 | ||
190 | static struct platform_driver pwm_regulator_driver = { | |
191 | .driver = { | |
192 | .name = "pwm-regulator", | |
aa66cc66 CZ |
193 | .of_match_table = of_match_ptr(pwm_of_match), |
194 | }, | |
195 | .probe = pwm_regulator_probe, | |
196 | }; | |
197 | ||
198 | module_platform_driver(pwm_regulator_driver); | |
199 | ||
200 | MODULE_LICENSE("GPL"); | |
201 | MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>"); | |
202 | MODULE_DESCRIPTION("PWM Regulator Driver"); | |
203 | MODULE_ALIAS("platform:pwm-regulator"); |