Commit | Line | Data |
---|---|---|
f307a7e9 JB |
1 | /* |
2 | * pv88060-regulator.c - Regulator device driver for PV88060 | |
3 | * Copyright (C) 2015 Powerventure Semiconductor Ltd. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License | |
7 | * as published by the Free Software Foundation; either version 2 | |
8 | * of the License, or (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | */ | |
15 | ||
16 | #include <linux/err.h> | |
f307a7e9 JB |
17 | #include <linux/i2c.h> |
18 | #include <linux/module.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/regulator/driver.h> | |
22 | #include <linux/regulator/machine.h> | |
23 | #include <linux/regmap.h> | |
24 | #include <linux/irq.h> | |
25 | #include <linux/interrupt.h> | |
26 | #include <linux/regulator/of_regulator.h> | |
f307a7e9 JB |
27 | #include "pv88060-regulator.h" |
28 | ||
29 | #define PV88060_MAX_REGULATORS 14 | |
30 | ||
31 | /* PV88060 REGULATOR IDs */ | |
32 | enum { | |
33 | /* BUCKs */ | |
34 | PV88060_ID_BUCK1, | |
35 | ||
36 | /* LDOs */ | |
37 | PV88060_ID_LDO1, | |
38 | PV88060_ID_LDO2, | |
39 | PV88060_ID_LDO3, | |
40 | PV88060_ID_LDO4, | |
41 | PV88060_ID_LDO5, | |
42 | PV88060_ID_LDO6, | |
43 | PV88060_ID_LDO7, | |
44 | ||
45 | /* SWTs */ | |
46 | PV88060_ID_SW1, | |
47 | PV88060_ID_SW2, | |
48 | PV88060_ID_SW3, | |
49 | PV88060_ID_SW4, | |
50 | PV88060_ID_SW5, | |
51 | PV88060_ID_SW6, | |
52 | }; | |
53 | ||
54 | struct pv88060_regulator { | |
55 | struct regulator_desc desc; | |
56 | /* Current limiting */ | |
57 | unsigned n_current_limits; | |
58 | const int *current_limits; | |
59 | unsigned int limit_mask; | |
60 | unsigned int conf; /* buck configuration register */ | |
61 | }; | |
62 | ||
63 | struct pv88060 { | |
64 | struct device *dev; | |
65 | struct regmap *regmap; | |
66 | struct regulator_dev *rdev[PV88060_MAX_REGULATORS]; | |
67 | }; | |
68 | ||
69 | static const struct regmap_config pv88060_regmap_config = { | |
70 | .reg_bits = 8, | |
71 | .val_bits = 8, | |
72 | }; | |
73 | ||
74 | /* Current limits array (in uA) for BUCK1 | |
75 | * Entry indexes corresponds to register values. | |
76 | */ | |
77 | ||
78 | static const int pv88060_buck1_limits[] = { | |
79 | 1496000, 2393000, 3291000, 4189000 | |
80 | }; | |
81 | ||
82 | static unsigned int pv88060_buck_get_mode(struct regulator_dev *rdev) | |
83 | { | |
84 | struct pv88060_regulator *info = rdev_get_drvdata(rdev); | |
85 | unsigned int data; | |
86 | int ret, mode = 0; | |
87 | ||
88 | ret = regmap_read(rdev->regmap, info->conf, &data); | |
89 | if (ret < 0) | |
90 | return ret; | |
91 | ||
92 | switch (data & PV88060_BUCK_MODE_MASK) { | |
93 | case PV88060_BUCK_MODE_SYNC: | |
94 | mode = REGULATOR_MODE_FAST; | |
95 | break; | |
96 | case PV88060_BUCK_MODE_AUTO: | |
97 | mode = REGULATOR_MODE_NORMAL; | |
98 | break; | |
99 | case PV88060_BUCK_MODE_SLEEP: | |
100 | mode = REGULATOR_MODE_STANDBY; | |
101 | break; | |
102 | } | |
103 | ||
104 | return mode; | |
105 | } | |
106 | ||
107 | static int pv88060_buck_set_mode(struct regulator_dev *rdev, | |
108 | unsigned int mode) | |
109 | { | |
110 | struct pv88060_regulator *info = rdev_get_drvdata(rdev); | |
111 | int val = 0; | |
112 | ||
113 | switch (mode) { | |
114 | case REGULATOR_MODE_FAST: | |
115 | val = PV88060_BUCK_MODE_SYNC; | |
116 | break; | |
117 | case REGULATOR_MODE_NORMAL: | |
118 | val = PV88060_BUCK_MODE_AUTO; | |
119 | break; | |
120 | case REGULATOR_MODE_STANDBY: | |
121 | val = PV88060_BUCK_MODE_SLEEP; | |
122 | break; | |
123 | default: | |
124 | return -EINVAL; | |
125 | } | |
126 | ||
127 | return regmap_update_bits(rdev->regmap, info->conf, | |
128 | PV88060_BUCK_MODE_MASK, val); | |
129 | } | |
130 | ||
131 | static int pv88060_set_current_limit(struct regulator_dev *rdev, int min, | |
132 | int max) | |
133 | { | |
134 | struct pv88060_regulator *info = rdev_get_drvdata(rdev); | |
135 | int i; | |
136 | ||
137 | /* search for closest to maximum */ | |
7cd415f8 | 138 | for (i = info->n_current_limits - 1; i >= 0; i--) { |
f307a7e9 JB |
139 | if (min <= info->current_limits[i] |
140 | && max >= info->current_limits[i]) { | |
141 | return regmap_update_bits(rdev->regmap, | |
142 | info->conf, | |
143 | info->limit_mask, | |
144 | i << PV88060_BUCK_ILIM_SHIFT); | |
145 | } | |
146 | } | |
147 | ||
148 | return -EINVAL; | |
149 | } | |
150 | ||
151 | static int pv88060_get_current_limit(struct regulator_dev *rdev) | |
152 | { | |
153 | struct pv88060_regulator *info = rdev_get_drvdata(rdev); | |
154 | unsigned int data; | |
155 | int ret; | |
156 | ||
157 | ret = regmap_read(rdev->regmap, info->conf, &data); | |
158 | if (ret < 0) | |
159 | return ret; | |
160 | ||
161 | data = (data & info->limit_mask) >> PV88060_BUCK_ILIM_SHIFT; | |
162 | return info->current_limits[data]; | |
163 | } | |
164 | ||
ea51874c | 165 | static const struct regulator_ops pv88060_buck_ops = { |
f307a7e9 JB |
166 | .get_mode = pv88060_buck_get_mode, |
167 | .set_mode = pv88060_buck_set_mode, | |
168 | .enable = regulator_enable_regmap, | |
169 | .disable = regulator_disable_regmap, | |
170 | .is_enabled = regulator_is_enabled_regmap, | |
171 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
172 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
173 | .list_voltage = regulator_list_voltage_linear, | |
174 | .set_current_limit = pv88060_set_current_limit, | |
175 | .get_current_limit = pv88060_get_current_limit, | |
176 | }; | |
177 | ||
ea51874c | 178 | static const struct regulator_ops pv88060_ldo_ops = { |
f307a7e9 JB |
179 | .enable = regulator_enable_regmap, |
180 | .disable = regulator_disable_regmap, | |
181 | .is_enabled = regulator_is_enabled_regmap, | |
182 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
183 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
184 | .list_voltage = regulator_list_voltage_linear, | |
185 | }; | |
186 | ||
55e72728 AL |
187 | static const struct regulator_ops pv88060_sw_ops = { |
188 | .enable = regulator_enable_regmap, | |
189 | .disable = regulator_disable_regmap, | |
190 | .is_enabled = regulator_is_enabled_regmap, | |
191 | }; | |
192 | ||
f307a7e9 JB |
193 | #define PV88060_BUCK(chip, regl_name, min, step, max, limits_array) \ |
194 | {\ | |
195 | .desc = {\ | |
196 | .id = chip##_ID_##regl_name,\ | |
197 | .name = __stringify(chip##_##regl_name),\ | |
198 | .of_match = of_match_ptr(#regl_name),\ | |
199 | .regulators_node = of_match_ptr("regulators"),\ | |
200 | .type = REGULATOR_VOLTAGE,\ | |
201 | .owner = THIS_MODULE,\ | |
202 | .ops = &pv88060_buck_ops,\ | |
203 | .min_uV = min,\ | |
204 | .uV_step = step,\ | |
205 | .n_voltages = ((max) - (min))/(step) + 1,\ | |
206 | .enable_reg = PV88060_REG_##regl_name##_CONF0,\ | |
207 | .enable_mask = PV88060_BUCK_EN, \ | |
208 | .vsel_reg = PV88060_REG_##regl_name##_CONF0,\ | |
209 | .vsel_mask = PV88060_VBUCK_MASK,\ | |
210 | },\ | |
211 | .current_limits = limits_array,\ | |
212 | .n_current_limits = ARRAY_SIZE(limits_array),\ | |
213 | .limit_mask = PV88060_BUCK_ILIM_MASK, \ | |
214 | .conf = PV88060_REG_##regl_name##_CONF1,\ | |
215 | } | |
216 | ||
217 | #define PV88060_LDO(chip, regl_name, min, step, max) \ | |
218 | {\ | |
219 | .desc = {\ | |
220 | .id = chip##_ID_##regl_name,\ | |
221 | .name = __stringify(chip##_##regl_name),\ | |
222 | .of_match = of_match_ptr(#regl_name),\ | |
223 | .regulators_node = of_match_ptr("regulators"),\ | |
224 | .type = REGULATOR_VOLTAGE,\ | |
225 | .owner = THIS_MODULE,\ | |
226 | .ops = &pv88060_ldo_ops,\ | |
227 | .min_uV = min, \ | |
228 | .uV_step = step, \ | |
229 | .n_voltages = (step) ? ((max - min) / step + 1) : 1, \ | |
230 | .enable_reg = PV88060_REG_##regl_name##_CONF, \ | |
231 | .enable_mask = PV88060_LDO_EN, \ | |
232 | .vsel_reg = PV88060_REG_##regl_name##_CONF, \ | |
233 | .vsel_mask = PV88060_VLDO_MASK, \ | |
234 | },\ | |
235 | } | |
236 | ||
237 | #define PV88060_SW(chip, regl_name, max) \ | |
238 | {\ | |
239 | .desc = {\ | |
240 | .id = chip##_ID_##regl_name,\ | |
241 | .name = __stringify(chip##_##regl_name),\ | |
242 | .of_match = of_match_ptr(#regl_name),\ | |
243 | .regulators_node = of_match_ptr("regulators"),\ | |
244 | .type = REGULATOR_VOLTAGE,\ | |
245 | .owner = THIS_MODULE,\ | |
55e72728 AL |
246 | .ops = &pv88060_sw_ops,\ |
247 | .fixed_uV = max,\ | |
f307a7e9 JB |
248 | .n_voltages = 1,\ |
249 | .enable_reg = PV88060_REG_##regl_name##_CONF,\ | |
250 | .enable_mask = PV88060_SW_EN,\ | |
251 | },\ | |
252 | } | |
253 | ||
254 | static const struct pv88060_regulator pv88060_regulator_info[] = { | |
255 | PV88060_BUCK(PV88060, BUCK1, 2800000, 12500, 4387500, | |
256 | pv88060_buck1_limits), | |
257 | PV88060_LDO(PV88060, LDO1, 1200000, 50000, 3350000), | |
258 | PV88060_LDO(PV88060, LDO2, 1200000, 50000, 3350000), | |
259 | PV88060_LDO(PV88060, LDO3, 1200000, 50000, 3350000), | |
260 | PV88060_LDO(PV88060, LDO4, 1200000, 50000, 3350000), | |
261 | PV88060_LDO(PV88060, LDO5, 1200000, 50000, 3350000), | |
262 | PV88060_LDO(PV88060, LDO6, 1200000, 50000, 3350000), | |
263 | PV88060_LDO(PV88060, LDO7, 1200000, 50000, 3350000), | |
264 | PV88060_SW(PV88060, SW1, 5000000), | |
265 | PV88060_SW(PV88060, SW2, 5000000), | |
266 | PV88060_SW(PV88060, SW3, 5000000), | |
267 | PV88060_SW(PV88060, SW4, 5000000), | |
268 | PV88060_SW(PV88060, SW5, 5000000), | |
269 | PV88060_SW(PV88060, SW6, 5000000), | |
270 | }; | |
271 | ||
272 | static irqreturn_t pv88060_irq_handler(int irq, void *data) | |
273 | { | |
274 | struct pv88060 *chip = data; | |
275 | int i, reg_val, err, ret = IRQ_NONE; | |
276 | ||
277 | err = regmap_read(chip->regmap, PV88060_REG_EVENT_A, ®_val); | |
278 | if (err < 0) | |
279 | goto error_i2c; | |
280 | ||
281 | if (reg_val & PV88060_E_VDD_FLT) { | |
282 | for (i = 0; i < PV88060_MAX_REGULATORS; i++) { | |
283 | if (chip->rdev[i] != NULL) { | |
284 | regulator_notifier_call_chain(chip->rdev[i], | |
285 | REGULATOR_EVENT_UNDER_VOLTAGE, | |
286 | NULL); | |
287 | } | |
288 | } | |
289 | ||
a7c2ded6 JB |
290 | err = regmap_write(chip->regmap, PV88060_REG_EVENT_A, |
291 | PV88060_E_VDD_FLT); | |
f307a7e9 JB |
292 | if (err < 0) |
293 | goto error_i2c; | |
294 | ||
295 | ret = IRQ_HANDLED; | |
296 | } | |
297 | ||
298 | if (reg_val & PV88060_E_OVER_TEMP) { | |
299 | for (i = 0; i < PV88060_MAX_REGULATORS; i++) { | |
300 | if (chip->rdev[i] != NULL) { | |
301 | regulator_notifier_call_chain(chip->rdev[i], | |
302 | REGULATOR_EVENT_OVER_TEMP, | |
303 | NULL); | |
304 | } | |
305 | } | |
306 | ||
a7c2ded6 JB |
307 | err = regmap_write(chip->regmap, PV88060_REG_EVENT_A, |
308 | PV88060_E_OVER_TEMP); | |
f307a7e9 JB |
309 | if (err < 0) |
310 | goto error_i2c; | |
311 | ||
312 | ret = IRQ_HANDLED; | |
313 | } | |
314 | ||
315 | return ret; | |
316 | ||
317 | error_i2c: | |
318 | dev_err(chip->dev, "I2C error : %d\n", err); | |
319 | return IRQ_NONE; | |
320 | } | |
321 | ||
322 | /* | |
323 | * I2C driver interface functions | |
324 | */ | |
325 | static int pv88060_i2c_probe(struct i2c_client *i2c, | |
326 | const struct i2c_device_id *id) | |
327 | { | |
328 | struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev); | |
329 | struct pv88060 *chip; | |
330 | struct regulator_config config = { }; | |
331 | int error, i, ret = 0; | |
332 | ||
333 | chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88060), GFP_KERNEL); | |
334 | if (!chip) | |
335 | return -ENOMEM; | |
336 | ||
337 | chip->dev = &i2c->dev; | |
338 | chip->regmap = devm_regmap_init_i2c(i2c, &pv88060_regmap_config); | |
339 | if (IS_ERR(chip->regmap)) { | |
340 | error = PTR_ERR(chip->regmap); | |
341 | dev_err(chip->dev, "Failed to allocate register map: %d\n", | |
342 | error); | |
343 | return error; | |
344 | } | |
345 | ||
346 | i2c_set_clientdata(i2c, chip); | |
347 | ||
348 | if (i2c->irq != 0) { | |
349 | ret = regmap_write(chip->regmap, PV88060_REG_MASK_A, 0xFF); | |
350 | if (ret < 0) { | |
351 | dev_err(chip->dev, | |
352 | "Failed to mask A reg: %d\n", ret); | |
353 | return ret; | |
354 | } | |
355 | ||
3c0a2f64 | 356 | ret = regmap_write(chip->regmap, PV88060_REG_MASK_B, 0xFF); |
f307a7e9 JB |
357 | if (ret < 0) { |
358 | dev_err(chip->dev, | |
359 | "Failed to mask B reg: %d\n", ret); | |
360 | return ret; | |
361 | } | |
362 | ||
3c0a2f64 | 363 | ret = regmap_write(chip->regmap, PV88060_REG_MASK_C, 0xFF); |
f307a7e9 JB |
364 | if (ret < 0) { |
365 | dev_err(chip->dev, | |
366 | "Failed to mask C reg: %d\n", ret); | |
367 | return ret; | |
368 | } | |
369 | ||
88467943 | 370 | ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, |
f307a7e9 JB |
371 | pv88060_irq_handler, |
372 | IRQF_TRIGGER_LOW|IRQF_ONESHOT, | |
373 | "pv88060", chip); | |
374 | if (ret != 0) { | |
375 | dev_err(chip->dev, "Failed to request IRQ: %d\n", | |
376 | i2c->irq); | |
377 | return ret; | |
378 | } | |
379 | ||
380 | ret = regmap_update_bits(chip->regmap, PV88060_REG_MASK_A, | |
381 | PV88060_M_VDD_FLT | PV88060_M_OVER_TEMP, 0); | |
382 | if (ret < 0) { | |
383 | dev_err(chip->dev, | |
384 | "Failed to update mask reg: %d\n", ret); | |
385 | return ret; | |
386 | } | |
387 | ||
388 | } else { | |
389 | dev_warn(chip->dev, "No IRQ configured\n"); | |
390 | } | |
391 | ||
392 | config.dev = chip->dev; | |
393 | config.regmap = chip->regmap; | |
394 | ||
395 | for (i = 0; i < PV88060_MAX_REGULATORS; i++) { | |
396 | if (init_data) | |
397 | config.init_data = &init_data[i]; | |
398 | ||
399 | config.driver_data = (void *)&pv88060_regulator_info[i]; | |
400 | chip->rdev[i] = devm_regulator_register(chip->dev, | |
401 | &pv88060_regulator_info[i].desc, &config); | |
402 | if (IS_ERR(chip->rdev[i])) { | |
403 | dev_err(chip->dev, | |
404 | "Failed to register PV88060 regulator\n"); | |
405 | return PTR_ERR(chip->rdev[i]); | |
406 | } | |
407 | } | |
408 | ||
409 | return 0; | |
410 | } | |
411 | ||
412 | static const struct i2c_device_id pv88060_i2c_id[] = { | |
413 | {"pv88060", 0}, | |
414 | {}, | |
415 | }; | |
416 | MODULE_DEVICE_TABLE(i2c, pv88060_i2c_id); | |
417 | ||
418 | #ifdef CONFIG_OF | |
419 | static const struct of_device_id pv88060_dt_ids[] = { | |
420 | { .compatible = "pvs,pv88060", .data = &pv88060_i2c_id[0] }, | |
421 | {}, | |
422 | }; | |
423 | MODULE_DEVICE_TABLE(of, pv88060_dt_ids); | |
424 | #endif | |
425 | ||
426 | static struct i2c_driver pv88060_regulator_driver = { | |
427 | .driver = { | |
428 | .name = "pv88060", | |
429 | .of_match_table = of_match_ptr(pv88060_dt_ids), | |
430 | }, | |
431 | .probe = pv88060_i2c_probe, | |
432 | .id_table = pv88060_i2c_id, | |
433 | }; | |
434 | ||
435 | module_i2c_driver(pv88060_regulator_driver); | |
436 | ||
437 | MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>"); | |
438 | MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88060"); | |
439 | MODULE_LICENSE("GPL"); |