Commit | Line | Data |
---|---|---|
99cf3af5 JB |
1 | /* |
2 | * pv88080-regulator.c - Regulator device driver for PV88080 | |
3 | * Copyright (C) 2016 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> | |
99cf3af5 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> | |
99cf3af5 JB |
27 | #include "pv88080-regulator.h" |
28 | ||
29 | #define PV88080_MAX_REGULATORS 3 | |
30 | ||
31 | /* PV88080 REGULATOR IDs */ | |
32 | enum { | |
33 | /* BUCKs */ | |
34 | PV88080_ID_BUCK1, | |
35 | PV88080_ID_BUCK2, | |
36 | PV88080_ID_BUCK3, | |
37 | }; | |
38 | ||
39 | struct pv88080_regulator { | |
40 | struct regulator_desc desc; | |
41 | /* Current limiting */ | |
42 | unsigned int n_current_limits; | |
43 | const int *current_limits; | |
44 | unsigned int limit_mask; | |
45 | unsigned int conf; | |
46 | unsigned int conf2; | |
47 | unsigned int conf5; | |
48 | }; | |
49 | ||
50 | struct pv88080 { | |
51 | struct device *dev; | |
52 | struct regmap *regmap; | |
53 | struct regulator_dev *rdev[PV88080_MAX_REGULATORS]; | |
54 | }; | |
55 | ||
56 | struct pv88080_buck_voltage { | |
57 | int min_uV; | |
58 | int max_uV; | |
59 | int uV_step; | |
60 | }; | |
61 | ||
62 | static const struct regmap_config pv88080_regmap_config = { | |
63 | .reg_bits = 8, | |
64 | .val_bits = 8, | |
65 | }; | |
66 | ||
67 | /* Current limits array (in uA) for BUCK1, BUCK2, BUCK3. | |
68 | * Entry indexes corresponds to register values. | |
69 | */ | |
70 | ||
71 | static const int pv88080_buck1_limits[] = { | |
72 | 3230000, 5130000, 6960000, 8790000 | |
73 | }; | |
74 | ||
75 | static const int pv88080_buck23_limits[] = { | |
76 | 1496000, 2393000, 3291000, 4189000 | |
77 | }; | |
78 | ||
79 | static const struct pv88080_buck_voltage pv88080_buck_vol[2] = { | |
80 | { | |
81 | .min_uV = 600000, | |
82 | .max_uV = 1393750, | |
83 | .uV_step = 6250, | |
84 | }, | |
85 | { | |
86 | .min_uV = 1400000, | |
87 | .max_uV = 2193750, | |
88 | .uV_step = 6250, | |
89 | }, | |
90 | }; | |
91 | ||
92 | static unsigned int pv88080_buck_get_mode(struct regulator_dev *rdev) | |
93 | { | |
94 | struct pv88080_regulator *info = rdev_get_drvdata(rdev); | |
95 | unsigned int data; | |
96 | int ret, mode = 0; | |
97 | ||
98 | ret = regmap_read(rdev->regmap, info->conf, &data); | |
99 | if (ret < 0) | |
100 | return ret; | |
101 | ||
102 | switch (data & PV88080_BUCK1_MODE_MASK) { | |
103 | case PV88080_BUCK_MODE_SYNC: | |
104 | mode = REGULATOR_MODE_FAST; | |
105 | break; | |
106 | case PV88080_BUCK_MODE_AUTO: | |
107 | mode = REGULATOR_MODE_NORMAL; | |
108 | break; | |
109 | case PV88080_BUCK_MODE_SLEEP: | |
110 | mode = REGULATOR_MODE_STANDBY; | |
111 | break; | |
112 | default: | |
113 | return -EINVAL; | |
114 | } | |
115 | ||
116 | return mode; | |
117 | } | |
118 | ||
119 | static int pv88080_buck_set_mode(struct regulator_dev *rdev, | |
120 | unsigned int mode) | |
121 | { | |
122 | struct pv88080_regulator *info = rdev_get_drvdata(rdev); | |
123 | int val = 0; | |
124 | ||
125 | switch (mode) { | |
126 | case REGULATOR_MODE_FAST: | |
127 | val = PV88080_BUCK_MODE_SYNC; | |
128 | break; | |
129 | case REGULATOR_MODE_NORMAL: | |
130 | val = PV88080_BUCK_MODE_AUTO; | |
131 | break; | |
132 | case REGULATOR_MODE_STANDBY: | |
133 | val = PV88080_BUCK_MODE_SLEEP; | |
134 | break; | |
135 | default: | |
136 | return -EINVAL; | |
137 | } | |
138 | ||
139 | return regmap_update_bits(rdev->regmap, info->conf, | |
140 | PV88080_BUCK1_MODE_MASK, val); | |
141 | } | |
142 | ||
143 | static int pv88080_set_current_limit(struct regulator_dev *rdev, int min, | |
144 | int max) | |
145 | { | |
146 | struct pv88080_regulator *info = rdev_get_drvdata(rdev); | |
147 | int i; | |
148 | ||
149 | /* search for closest to maximum */ | |
150 | for (i = info->n_current_limits; i >= 0; i--) { | |
151 | if (min <= info->current_limits[i] | |
152 | && max >= info->current_limits[i]) { | |
153 | return regmap_update_bits(rdev->regmap, | |
154 | info->conf, | |
155 | info->limit_mask, | |
156 | i << PV88080_BUCK1_ILIM_SHIFT); | |
157 | } | |
158 | } | |
159 | ||
160 | return -EINVAL; | |
161 | } | |
162 | ||
163 | static int pv88080_get_current_limit(struct regulator_dev *rdev) | |
164 | { | |
165 | struct pv88080_regulator *info = rdev_get_drvdata(rdev); | |
166 | unsigned int data; | |
167 | int ret; | |
168 | ||
169 | ret = regmap_read(rdev->regmap, info->conf, &data); | |
170 | if (ret < 0) | |
171 | return ret; | |
172 | ||
173 | data = (data & info->limit_mask) >> PV88080_BUCK1_ILIM_SHIFT; | |
174 | return info->current_limits[data]; | |
175 | } | |
176 | ||
177 | static struct regulator_ops pv88080_buck_ops = { | |
178 | .get_mode = pv88080_buck_get_mode, | |
179 | .set_mode = pv88080_buck_set_mode, | |
180 | .enable = regulator_enable_regmap, | |
181 | .disable = regulator_disable_regmap, | |
182 | .is_enabled = regulator_is_enabled_regmap, | |
183 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
184 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
185 | .list_voltage = regulator_list_voltage_linear, | |
186 | .set_current_limit = pv88080_set_current_limit, | |
187 | .get_current_limit = pv88080_get_current_limit, | |
188 | }; | |
189 | ||
190 | #define PV88080_BUCK(chip, regl_name, min, step, max, limits_array) \ | |
191 | {\ | |
192 | .desc = {\ | |
193 | .id = chip##_ID_##regl_name,\ | |
194 | .name = __stringify(chip##_##regl_name),\ | |
195 | .of_match = of_match_ptr(#regl_name),\ | |
196 | .regulators_node = of_match_ptr("regulators"),\ | |
197 | .type = REGULATOR_VOLTAGE,\ | |
198 | .owner = THIS_MODULE,\ | |
199 | .ops = &pv88080_buck_ops,\ | |
200 | .min_uV = min, \ | |
201 | .uV_step = step, \ | |
202 | .n_voltages = ((max) - (min))/(step) + 1, \ | |
203 | .enable_reg = PV88080_REG_##regl_name##_CONF0, \ | |
204 | .enable_mask = PV88080_##regl_name##_EN, \ | |
205 | .vsel_reg = PV88080_REG_##regl_name##_CONF0, \ | |
206 | .vsel_mask = PV88080_V##regl_name##_MASK, \ | |
207 | },\ | |
208 | .current_limits = limits_array, \ | |
209 | .n_current_limits = ARRAY_SIZE(limits_array), \ | |
210 | .limit_mask = PV88080_##regl_name##_ILIM_MASK, \ | |
211 | .conf = PV88080_REG_##regl_name##_CONF1, \ | |
212 | .conf2 = PV88080_REG_##regl_name##_CONF2, \ | |
213 | .conf5 = PV88080_REG_##regl_name##_CONF5, \ | |
214 | } | |
215 | ||
216 | static struct pv88080_regulator pv88080_regulator_info[] = { | |
217 | PV88080_BUCK(PV88080, BUCK1, 600000, 6250, 1393750, | |
218 | pv88080_buck1_limits), | |
219 | PV88080_BUCK(PV88080, BUCK2, 600000, 6250, 1393750, | |
220 | pv88080_buck23_limits), | |
221 | PV88080_BUCK(PV88080, BUCK3, 600000, 6250, 1393750, | |
222 | pv88080_buck23_limits), | |
223 | }; | |
224 | ||
225 | static irqreturn_t pv88080_irq_handler(int irq, void *data) | |
226 | { | |
227 | struct pv88080 *chip = data; | |
228 | int i, reg_val, err, ret = IRQ_NONE; | |
229 | ||
230 | err = regmap_read(chip->regmap, PV88080_REG_EVENT_A, ®_val); | |
231 | if (err < 0) | |
232 | goto error_i2c; | |
233 | ||
234 | if (reg_val & PV88080_E_VDD_FLT) { | |
235 | for (i = 0; i < PV88080_MAX_REGULATORS; i++) { | |
236 | if (chip->rdev[i] != NULL) { | |
237 | regulator_notifier_call_chain(chip->rdev[i], | |
238 | REGULATOR_EVENT_UNDER_VOLTAGE, | |
239 | NULL); | |
240 | } | |
241 | } | |
242 | ||
243 | err = regmap_write(chip->regmap, PV88080_REG_EVENT_A, | |
244 | PV88080_E_VDD_FLT); | |
245 | if (err < 0) | |
246 | goto error_i2c; | |
247 | ||
248 | ret = IRQ_HANDLED; | |
249 | } | |
250 | ||
251 | if (reg_val & PV88080_E_OVER_TEMP) { | |
252 | for (i = 0; i < PV88080_MAX_REGULATORS; i++) { | |
253 | if (chip->rdev[i] != NULL) { | |
254 | regulator_notifier_call_chain(chip->rdev[i], | |
255 | REGULATOR_EVENT_OVER_TEMP, | |
256 | NULL); | |
257 | } | |
258 | } | |
259 | ||
260 | err = regmap_write(chip->regmap, PV88080_REG_EVENT_A, | |
261 | PV88080_E_OVER_TEMP); | |
262 | if (err < 0) | |
263 | goto error_i2c; | |
264 | ||
265 | ret = IRQ_HANDLED; | |
266 | } | |
267 | ||
268 | return ret; | |
269 | ||
270 | error_i2c: | |
271 | dev_err(chip->dev, "I2C error : %d\n", err); | |
272 | return IRQ_NONE; | |
273 | } | |
274 | ||
275 | /* | |
276 | * I2C driver interface functions | |
277 | */ | |
278 | static int pv88080_i2c_probe(struct i2c_client *i2c, | |
279 | const struct i2c_device_id *id) | |
280 | { | |
281 | struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev); | |
282 | struct pv88080 *chip; | |
283 | struct regulator_config config = { }; | |
284 | int i, error, ret; | |
285 | unsigned int conf2, conf5; | |
286 | ||
287 | chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88080), GFP_KERNEL); | |
288 | if (!chip) | |
289 | return -ENOMEM; | |
290 | ||
291 | chip->dev = &i2c->dev; | |
292 | chip->regmap = devm_regmap_init_i2c(i2c, &pv88080_regmap_config); | |
293 | if (IS_ERR(chip->regmap)) { | |
294 | error = PTR_ERR(chip->regmap); | |
295 | dev_err(chip->dev, "Failed to allocate register map: %d\n", | |
296 | error); | |
297 | return error; | |
298 | } | |
299 | ||
300 | i2c_set_clientdata(i2c, chip); | |
301 | ||
302 | if (i2c->irq != 0) { | |
303 | ret = regmap_write(chip->regmap, PV88080_REG_MASK_A, 0xFF); | |
304 | if (ret < 0) { | |
305 | dev_err(chip->dev, | |
306 | "Failed to mask A reg: %d\n", ret); | |
307 | return ret; | |
308 | } | |
309 | ret = regmap_write(chip->regmap, PV88080_REG_MASK_B, 0xFF); | |
310 | if (ret < 0) { | |
311 | dev_err(chip->dev, | |
312 | "Failed to mask B reg: %d\n", ret); | |
313 | return ret; | |
314 | } | |
315 | ret = regmap_write(chip->regmap, PV88080_REG_MASK_C, 0xFF); | |
316 | if (ret < 0) { | |
317 | dev_err(chip->dev, | |
318 | "Failed to mask C reg: %d\n", ret); | |
319 | return ret; | |
320 | } | |
321 | ||
322 | ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, | |
323 | pv88080_irq_handler, | |
324 | IRQF_TRIGGER_LOW|IRQF_ONESHOT, | |
325 | "pv88080", chip); | |
326 | if (ret != 0) { | |
327 | dev_err(chip->dev, "Failed to request IRQ: %d\n", | |
328 | i2c->irq); | |
329 | return ret; | |
330 | } | |
331 | ||
332 | ret = regmap_update_bits(chip->regmap, PV88080_REG_MASK_A, | |
333 | PV88080_M_VDD_FLT | PV88080_M_OVER_TEMP, 0); | |
334 | if (ret < 0) { | |
335 | dev_err(chip->dev, | |
336 | "Failed to update mask reg: %d\n", ret); | |
337 | return ret; | |
338 | } | |
339 | ||
340 | } else { | |
341 | dev_warn(chip->dev, "No IRQ configured\n"); | |
342 | } | |
343 | ||
344 | config.dev = chip->dev; | |
345 | config.regmap = chip->regmap; | |
346 | ||
347 | for (i = 0; i < PV88080_MAX_REGULATORS; i++) { | |
348 | if (init_data) | |
349 | config.init_data = &init_data[i]; | |
350 | ||
351 | ret = regmap_read(chip->regmap, | |
352 | pv88080_regulator_info[i].conf2, &conf2); | |
353 | if (ret < 0) | |
354 | return ret; | |
355 | ||
356 | conf2 = ((conf2 >> PV88080_BUCK_VDAC_RANGE_SHIFT) & | |
357 | PV88080_BUCK_VDAC_RANGE_MASK); | |
358 | ||
359 | ret = regmap_read(chip->regmap, | |
360 | pv88080_regulator_info[i].conf5, &conf5); | |
361 | if (ret < 0) | |
362 | return ret; | |
363 | ||
364 | conf5 = ((conf5 >> PV88080_BUCK_VRANGE_GAIN_SHIFT) & | |
365 | PV88080_BUCK_VRANGE_GAIN_MASK); | |
366 | ||
367 | pv88080_regulator_info[i].desc.min_uV = | |
368 | pv88080_buck_vol[conf2].min_uV * (conf5+1); | |
369 | pv88080_regulator_info[i].desc.uV_step = | |
370 | pv88080_buck_vol[conf2].uV_step * (conf5+1); | |
371 | pv88080_regulator_info[i].desc.n_voltages = | |
372 | ((pv88080_buck_vol[conf2].max_uV * (conf5+1)) | |
373 | - (pv88080_regulator_info[i].desc.min_uV)) | |
374 | /(pv88080_regulator_info[i].desc.uV_step) + 1; | |
375 | ||
376 | config.driver_data = (void *)&pv88080_regulator_info[i]; | |
377 | chip->rdev[i] = devm_regulator_register(chip->dev, | |
378 | &pv88080_regulator_info[i].desc, &config); | |
379 | if (IS_ERR(chip->rdev[i])) { | |
380 | dev_err(chip->dev, | |
381 | "Failed to register PV88080 regulator\n"); | |
382 | return PTR_ERR(chip->rdev[i]); | |
383 | } | |
384 | } | |
385 | ||
386 | return 0; | |
387 | } | |
388 | ||
389 | static const struct i2c_device_id pv88080_i2c_id[] = { | |
390 | {"pv88080", 0}, | |
391 | {}, | |
392 | }; | |
393 | MODULE_DEVICE_TABLE(i2c, pv88080_i2c_id); | |
394 | ||
395 | #ifdef CONFIG_OF | |
396 | static const struct of_device_id pv88080_dt_ids[] = { | |
397 | { .compatible = "pvs,pv88080", .data = &pv88080_i2c_id[0] }, | |
398 | {}, | |
399 | }; | |
400 | MODULE_DEVICE_TABLE(of, pv88080_dt_ids); | |
401 | #endif | |
402 | ||
403 | static struct i2c_driver pv88080_regulator_driver = { | |
404 | .driver = { | |
405 | .name = "pv88080", | |
406 | .of_match_table = of_match_ptr(pv88080_dt_ids), | |
407 | }, | |
408 | .probe = pv88080_i2c_probe, | |
409 | .id_table = pv88080_i2c_id, | |
410 | }; | |
411 | ||
412 | module_i2c_driver(pv88080_regulator_driver); | |
413 | ||
414 | MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>"); | |
415 | MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88080"); | |
416 | MODULE_LICENSE("GPL"); |