]>
Commit | Line | Data |
---|---|---|
6ffc3270 LD |
1 | /* |
2 | * Regulator driver for RICOH RC5T583 power management chip. | |
3 | * | |
4 | * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved. | |
5 | * Author: Laxman dewangan <ldewangan@nvidia.com> | |
6 | * | |
7 | * based on code | |
8 | * Copyright (C) 2011 RICOH COMPANY,LTD | |
9 | * | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify it | |
12 | * under the terms and conditions of the GNU General Public License, | |
13 | * version 2, as published by the Free Software Foundation. | |
14 | * | |
15 | * This program is distributed in the hope it will be useful, but WITHOUT | |
16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
18 | * more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 | * | |
23 | */ | |
24 | ||
25 | #include <linux/module.h> | |
6ffc3270 LD |
26 | #include <linux/init.h> |
27 | #include <linux/slab.h> | |
28 | #include <linux/err.h> | |
29 | #include <linux/platform_device.h> | |
30 | #include <linux/regulator/driver.h> | |
31 | #include <linux/regulator/machine.h> | |
32 | #include <linux/gpio.h> | |
33 | #include <linux/mfd/rc5t583.h> | |
34 | ||
35 | struct rc5t583_regulator_info { | |
36 | int deepsleep_id; | |
37 | ||
38 | /* Regulator register address.*/ | |
6ffc3270 LD |
39 | uint8_t reg_disc_reg; |
40 | uint8_t disc_bit; | |
6ffc3270 LD |
41 | uint8_t deepsleep_reg; |
42 | ||
43 | /* Chip constraints on regulator behavior */ | |
44 | int min_uV; | |
45 | int max_uV; | |
46 | int step_uV; | |
6ffc3270 LD |
47 | |
48 | /* Regulator specific turn-on delay and voltage settling time*/ | |
49 | int enable_uv_per_us; | |
50 | int change_uv_per_us; | |
51 | ||
52 | /* Used by regulator core */ | |
53 | struct regulator_desc desc; | |
54 | }; | |
55 | ||
56 | struct rc5t583_regulator { | |
57 | struct rc5t583_regulator_info *reg_info; | |
58 | ||
59 | /* Devices */ | |
60 | struct device *dev; | |
61 | struct rc5t583 *mfd; | |
62 | struct regulator_dev *rdev; | |
63 | }; | |
64 | ||
6ffc3270 LD |
65 | static int rc5t583_list_voltage(struct regulator_dev *rdev, unsigned selector) |
66 | { | |
67 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); | |
68 | struct rc5t583_regulator_info *ri = reg->reg_info; | |
69 | return ri->min_uV + (ri->step_uV * selector); | |
70 | } | |
71 | ||
f604c10c AL |
72 | static int rc5t583_set_voltage(struct regulator_dev *rdev, |
73 | int min_uV, int max_uV, unsigned *selector) | |
6ffc3270 LD |
74 | { |
75 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); | |
76 | struct rc5t583_regulator_info *ri = reg->reg_info; | |
f604c10c AL |
77 | int sel, ret; |
78 | ||
79 | if (min_uV < ri->min_uV) | |
80 | min_uV = ri->min_uV; | |
81 | ||
82 | sel = DIV_ROUND_UP(min_uV - ri->min_uV, ri->step_uV); | |
83 | ||
84 | if (sel >= rdev->desc->n_voltages) { | |
85 | dev_err(&rdev->dev, "Invalid selector 0x%02x\n", sel); | |
6ffc3270 LD |
86 | return -EINVAL; |
87 | } | |
88 | ||
f604c10c AL |
89 | *selector = sel; |
90 | ||
15b397d7 AL |
91 | ret = rc5t583_update(reg->mfd->dev, rdev->desc->vsel_reg, sel, |
92 | rdev->desc->vsel_mask); | |
6ffc3270 | 93 | if (ret < 0) |
15b397d7 AL |
94 | dev_err(&rdev->dev, "Error in update voltage register 0x%02x\n", |
95 | rdev->desc->vsel_reg); | |
6ffc3270 LD |
96 | return ret; |
97 | } | |
98 | ||
6ffc3270 LD |
99 | static int rc5t583_regulator_enable_time(struct regulator_dev *rdev) |
100 | { | |
101 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); | |
15b397d7 | 102 | int vsel = regulator_get_voltage_sel_regmap(rdev); |
6ffc3270 | 103 | int curr_uV = rc5t583_list_voltage(rdev, vsel); |
15b397d7 | 104 | |
6ffc3270 LD |
105 | return DIV_ROUND_UP(curr_uV, reg->reg_info->enable_uv_per_us); |
106 | } | |
107 | ||
108 | static int rc5t583_set_voltage_time_sel(struct regulator_dev *rdev, | |
109 | unsigned int old_selector, unsigned int new_selector) | |
110 | { | |
111 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); | |
112 | int old_uV, new_uV; | |
113 | old_uV = rc5t583_list_voltage(rdev, old_selector); | |
114 | ||
115 | if (old_uV < 0) | |
116 | return old_uV; | |
117 | ||
118 | new_uV = rc5t583_list_voltage(rdev, new_selector); | |
119 | if (new_uV < 0) | |
120 | return new_uV; | |
121 | ||
122 | return DIV_ROUND_UP(abs(old_uV - new_uV), | |
123 | reg->reg_info->change_uv_per_us); | |
124 | } | |
125 | ||
126 | ||
127 | static struct regulator_ops rc5t583_ops = { | |
5bb6936c AL |
128 | .is_enabled = regulator_is_enabled_regmap, |
129 | .enable = regulator_enable_regmap, | |
130 | .disable = regulator_disable_regmap, | |
6ffc3270 | 131 | .enable_time = rc5t583_regulator_enable_time, |
15b397d7 | 132 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
f604c10c | 133 | .set_voltage = rc5t583_set_voltage, |
6ffc3270 LD |
134 | .list_voltage = rc5t583_list_voltage, |
135 | .set_voltage_time_sel = rc5t583_set_voltage_time_sel, | |
136 | }; | |
137 | ||
95e301ba AL |
138 | #define RC5T583_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, \ |
139 | _vout_mask, _min_mv, _max_mv, _step_uV, _enable_mv) \ | |
6ffc3270 | 140 | { \ |
6ffc3270 LD |
141 | .reg_disc_reg = RC5T583_REG_##_disc_reg, \ |
142 | .disc_bit = _disc_bit, \ | |
95e301ba | 143 | .deepsleep_reg = RC5T583_REG_##_id##DAC_DS, \ |
6ffc3270 LD |
144 | .min_uV = _min_mv * 1000, \ |
145 | .max_uV = _max_mv * 1000, \ | |
146 | .step_uV = _step_uV, \ | |
6ffc3270 LD |
147 | .enable_uv_per_us = _enable_mv * 1000, \ |
148 | .change_uv_per_us = 40 * 1000, \ | |
149 | .deepsleep_id = RC5T583_DS_##_id, \ | |
150 | .desc = { \ | |
151 | .name = "rc5t583-regulator-"#_id, \ | |
152 | .id = RC5T583_REGULATOR_##_id, \ | |
e3a7384c | 153 | .n_voltages = (_max_mv - _min_mv) * 1000 / _step_uV + 1, \ |
6ffc3270 LD |
154 | .ops = &rc5t583_ops, \ |
155 | .type = REGULATOR_VOLTAGE, \ | |
156 | .owner = THIS_MODULE, \ | |
15b397d7 AL |
157 | .vsel_reg = RC5T583_REG_##_id##DAC, \ |
158 | .vsel_mask = _vout_mask, \ | |
5bb6936c AL |
159 | .enable_reg = RC5T583_REG_##_en_reg, \ |
160 | .enable_mask = BIT(_en_bit), \ | |
6ffc3270 LD |
161 | }, \ |
162 | } | |
163 | ||
164 | static struct rc5t583_regulator_info rc5t583_reg_info[RC5T583_REGULATOR_MAX] = { | |
95e301ba AL |
165 | RC5T583_REG(DC0, DC0CTL, 0, DC0CTL, 1, 0x7F, 700, 1500, 12500, 4), |
166 | RC5T583_REG(DC1, DC1CTL, 0, DC1CTL, 1, 0x7F, 700, 1500, 12500, 14), | |
167 | RC5T583_REG(DC2, DC2CTL, 0, DC2CTL, 1, 0x7F, 900, 2400, 12500, 14), | |
168 | RC5T583_REG(DC3, DC3CTL, 0, DC3CTL, 1, 0x7F, 900, 2400, 12500, 14), | |
169 | RC5T583_REG(LDO0, LDOEN2, 0, LDODIS2, 0, 0x7F, 900, 3400, 25000, 160), | |
170 | RC5T583_REG(LDO1, LDOEN2, 1, LDODIS2, 1, 0x7F, 900, 3400, 25000, 160), | |
171 | RC5T583_REG(LDO2, LDOEN2, 2, LDODIS2, 2, 0x7F, 900, 3400, 25000, 160), | |
172 | RC5T583_REG(LDO3, LDOEN2, 3, LDODIS2, 3, 0x7F, 900, 3400, 25000, 160), | |
173 | RC5T583_REG(LDO4, LDOEN2, 4, LDODIS2, 4, 0x3F, 750, 1500, 12500, 133), | |
174 | RC5T583_REG(LDO5, LDOEN2, 5, LDODIS2, 5, 0x7F, 900, 3400, 25000, 267), | |
175 | RC5T583_REG(LDO6, LDOEN2, 6, LDODIS2, 6, 0x7F, 900, 3400, 25000, 133), | |
176 | RC5T583_REG(LDO7, LDOEN2, 7, LDODIS2, 7, 0x7F, 900, 3400, 25000, 233), | |
177 | RC5T583_REG(LDO8, LDOEN1, 0, LDODIS1, 0, 0x7F, 900, 3400, 25000, 233), | |
178 | RC5T583_REG(LDO9, LDOEN1, 1, LDODIS1, 1, 0x7F, 900, 3400, 25000, 133), | |
6ffc3270 LD |
179 | }; |
180 | ||
181 | static int __devinit rc5t583_regulator_probe(struct platform_device *pdev) | |
182 | { | |
183 | struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent); | |
184 | struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev); | |
185 | struct regulator_init_data *reg_data; | |
c172708d | 186 | struct regulator_config config = { }; |
6ffc3270 LD |
187 | struct rc5t583_regulator *reg = NULL; |
188 | struct rc5t583_regulator *regs; | |
189 | struct regulator_dev *rdev; | |
190 | struct rc5t583_regulator_info *ri; | |
191 | int ret; | |
192 | int id; | |
193 | ||
194 | if (!pdata) { | |
195 | dev_err(&pdev->dev, "No platform data, exiting...\n"); | |
196 | return -ENODEV; | |
197 | } | |
198 | ||
199 | regs = devm_kzalloc(&pdev->dev, RC5T583_REGULATOR_MAX * | |
200 | sizeof(struct rc5t583_regulator), GFP_KERNEL); | |
201 | if (!regs) { | |
202 | dev_err(&pdev->dev, "Memory allocation failed exiting..\n"); | |
203 | return -ENOMEM; | |
204 | } | |
205 | ||
206 | ||
207 | for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) { | |
208 | reg_data = pdata->reg_init_data[id]; | |
209 | ||
210 | /* No need to register if there is no regulator data */ | |
211 | if (!reg_data) | |
212 | continue; | |
213 | ||
214 | reg = ®s[id]; | |
215 | ri = &rc5t583_reg_info[id]; | |
216 | reg->reg_info = ri; | |
217 | reg->mfd = rc5t583; | |
218 | reg->dev = &pdev->dev; | |
219 | ||
220 | if (ri->deepsleep_id == RC5T583_DS_NONE) | |
221 | goto skip_ext_pwr_config; | |
222 | ||
223 | ret = rc5t583_ext_power_req_config(rc5t583->dev, | |
224 | ri->deepsleep_id, | |
225 | pdata->regulator_ext_pwr_control[id], | |
226 | pdata->regulator_deepsleep_slot[id]); | |
227 | /* | |
228 | * Configuring external control is not a major issue, | |
229 | * just give warning. | |
230 | */ | |
231 | if (ret < 0) | |
232 | dev_warn(&pdev->dev, | |
233 | "Failed to configure ext control %d\n", id); | |
234 | ||
235 | skip_ext_pwr_config: | |
c172708d MB |
236 | config.dev = &pdev->dev; |
237 | config.init_data = reg_data; | |
238 | config.driver_data = reg; | |
5bb6936c | 239 | config.regmap = rc5t583->regmap; |
c172708d MB |
240 | |
241 | rdev = regulator_register(&ri->desc, &config); | |
a69df8a1 | 242 | if (IS_ERR(rdev)) { |
6ffc3270 LD |
243 | dev_err(&pdev->dev, "Failed to register regulator %s\n", |
244 | ri->desc.name); | |
245 | ret = PTR_ERR(rdev); | |
246 | goto clean_exit; | |
247 | } | |
248 | reg->rdev = rdev; | |
249 | } | |
250 | platform_set_drvdata(pdev, regs); | |
251 | return 0; | |
252 | ||
253 | clean_exit: | |
a69df8a1 | 254 | while (--id >= 0) |
6ffc3270 LD |
255 | regulator_unregister(regs[id].rdev); |
256 | ||
257 | return ret; | |
258 | } | |
259 | ||
260 | static int __devexit rc5t583_regulator_remove(struct platform_device *pdev) | |
261 | { | |
262 | struct rc5t583_regulator *regs = platform_get_drvdata(pdev); | |
263 | int id; | |
264 | ||
265 | for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) | |
266 | regulator_unregister(regs[id].rdev); | |
267 | return 0; | |
268 | } | |
269 | ||
270 | static struct platform_driver rc5t583_regulator_driver = { | |
271 | .driver = { | |
272 | .name = "rc5t583-regulator", | |
273 | .owner = THIS_MODULE, | |
274 | }, | |
275 | .probe = rc5t583_regulator_probe, | |
276 | .remove = __devexit_p(rc5t583_regulator_remove), | |
277 | }; | |
278 | ||
279 | static int __init rc5t583_regulator_init(void) | |
280 | { | |
281 | return platform_driver_register(&rc5t583_regulator_driver); | |
282 | } | |
283 | subsys_initcall(rc5t583_regulator_init); | |
284 | ||
285 | static void __exit rc5t583_regulator_exit(void) | |
286 | { | |
287 | platform_driver_unregister(&rc5t583_regulator_driver); | |
288 | } | |
289 | module_exit(rc5t583_regulator_exit); | |
290 | ||
291 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); | |
292 | MODULE_DESCRIPTION("RC5T583 regulator driver"); | |
293 | MODULE_ALIAS("platform:rc5t583-regulator"); | |
4eb06453 | 294 | MODULE_LICENSE("GPL v2"); |