Loading...
1// SPDX-License-Identifier: GPL-2.0+
2//
3// pv88090-regulator.c - Regulator device driver for PV88090
4// Copyright (C) 2015 Powerventure Semiconductor Ltd.
5
6#include <linux/err.h>
7#include <linux/i2c.h>
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/slab.h>
11#include <linux/regulator/driver.h>
12#include <linux/regulator/machine.h>
13#include <linux/regmap.h>
14#include <linux/irq.h>
15#include <linux/interrupt.h>
16#include <linux/regulator/of_regulator.h>
17#include "pv88090-regulator.h"
18
19#define PV88090_MAX_REGULATORS 5
20
21/* PV88090 REGULATOR IDs */
22enum {
23 /* BUCKs */
24 PV88090_ID_BUCK1,
25 PV88090_ID_BUCK2,
26 PV88090_ID_BUCK3,
27
28 /* LDOs */
29 PV88090_ID_LDO1,
30 PV88090_ID_LDO2,
31};
32
33struct pv88090_regulator {
34 struct regulator_desc desc;
35 unsigned int conf;
36 unsigned int conf2;
37};
38
39struct pv88090 {
40 struct device *dev;
41 struct regmap *regmap;
42 struct regulator_dev *rdev[PV88090_MAX_REGULATORS];
43};
44
45struct pv88090_buck_voltage {
46 int min_uV;
47 int max_uV;
48 int uV_step;
49};
50
51static const struct regmap_config pv88090_regmap_config = {
52 .reg_bits = 8,
53 .val_bits = 8,
54};
55
56/* Current limits array (in uA) for BUCK1, BUCK2, BUCK3.
57 * Entry indexes corresponds to register values.
58 */
59
60static const unsigned int pv88090_buck1_limits[] = {
61 220000, 440000, 660000, 880000, 1100000, 1320000, 1540000, 1760000,
62 1980000, 2200000, 2420000, 2640000, 2860000, 3080000, 3300000, 3520000,
63 3740000, 3960000, 4180000, 4400000, 4620000, 4840000, 5060000, 5280000,
64 5500000, 5720000, 5940000, 6160000, 6380000, 6600000, 6820000, 7040000
65};
66
67static const unsigned int pv88090_buck23_limits[] = {
68 1496000, 2393000, 3291000, 4189000
69};
70
71static const struct pv88090_buck_voltage pv88090_buck_vol[3] = {
72 {
73 .min_uV = 600000,
74 .max_uV = 1393750,
75 .uV_step = 6250,
76 },
77
78 {
79 .min_uV = 1400000,
80 .max_uV = 2193750,
81 .uV_step = 6250,
82 },
83 {
84 .min_uV = 1250000,
85 .max_uV = 2837500,
86 .uV_step = 12500,
87 },
88};
89
90static unsigned int pv88090_buck_get_mode(struct regulator_dev *rdev)
91{
92 struct pv88090_regulator *info = rdev_get_drvdata(rdev);
93 unsigned int data;
94 int ret, mode = 0;
95
96 ret = regmap_read(rdev->regmap, info->conf, &data);
97 if (ret < 0)
98 return ret;
99
100 switch (data & PV88090_BUCK1_MODE_MASK) {
101 case PV88090_BUCK_MODE_SYNC:
102 mode = REGULATOR_MODE_FAST;
103 break;
104 case PV88090_BUCK_MODE_AUTO:
105 mode = REGULATOR_MODE_NORMAL;
106 break;
107 case PV88090_BUCK_MODE_SLEEP:
108 mode = REGULATOR_MODE_STANDBY;
109 break;
110 }
111
112 return mode;
113}
114
115static int pv88090_buck_set_mode(struct regulator_dev *rdev,
116 unsigned int mode)
117{
118 struct pv88090_regulator *info = rdev_get_drvdata(rdev);
119 int val = 0;
120
121 switch (mode) {
122 case REGULATOR_MODE_FAST:
123 val = PV88090_BUCK_MODE_SYNC;
124 break;
125 case REGULATOR_MODE_NORMAL:
126 val = PV88090_BUCK_MODE_AUTO;
127 break;
128 case REGULATOR_MODE_STANDBY:
129 val = PV88090_BUCK_MODE_SLEEP;
130 break;
131 default:
132 return -EINVAL;
133 }
134
135 return regmap_update_bits(rdev->regmap, info->conf,
136 PV88090_BUCK1_MODE_MASK, val);
137}
138
139static const struct regulator_ops pv88090_buck_ops = {
140 .get_mode = pv88090_buck_get_mode,
141 .set_mode = pv88090_buck_set_mode,
142 .enable = regulator_enable_regmap,
143 .disable = regulator_disable_regmap,
144 .is_enabled = regulator_is_enabled_regmap,
145 .set_voltage_sel = regulator_set_voltage_sel_regmap,
146 .get_voltage_sel = regulator_get_voltage_sel_regmap,
147 .list_voltage = regulator_list_voltage_linear,
148 .set_current_limit = regulator_set_current_limit_regmap,
149 .get_current_limit = regulator_get_current_limit_regmap,
150};
151
152static const struct regulator_ops pv88090_ldo_ops = {
153 .enable = regulator_enable_regmap,
154 .disable = regulator_disable_regmap,
155 .is_enabled = regulator_is_enabled_regmap,
156 .set_voltage_sel = regulator_set_voltage_sel_regmap,
157 .get_voltage_sel = regulator_get_voltage_sel_regmap,
158 .list_voltage = regulator_list_voltage_linear,
159};
160
161#define PV88090_BUCK(chip, regl_name, min, step, max, limits_array) \
162{\
163 .desc = {\
164 .id = chip##_ID_##regl_name,\
165 .name = __stringify(chip##_##regl_name),\
166 .of_match = of_match_ptr(#regl_name),\
167 .regulators_node = of_match_ptr("regulators"),\
168 .type = REGULATOR_VOLTAGE,\
169 .owner = THIS_MODULE,\
170 .ops = &pv88090_buck_ops,\
171 .min_uV = min, \
172 .uV_step = step, \
173 .n_voltages = ((max) - (min))/(step) + 1, \
174 .enable_reg = PV88090_REG_##regl_name##_CONF0, \
175 .enable_mask = PV88090_##regl_name##_EN, \
176 .vsel_reg = PV88090_REG_##regl_name##_CONF0, \
177 .vsel_mask = PV88090_V##regl_name##_MASK, \
178 .curr_table = limits_array, \
179 .n_current_limits = ARRAY_SIZE(limits_array), \
180 .csel_reg = PV88090_REG_##regl_name##_CONF1, \
181 .csel_mask = PV88090_##regl_name##_ILIM_MASK, \
182 },\
183 .conf = PV88090_REG_##regl_name##_CONF1, \
184 .conf2 = PV88090_REG_##regl_name##_CONF2, \
185}
186
187#define PV88090_LDO(chip, regl_name, min, step, max) \
188{\
189 .desc = {\
190 .id = chip##_ID_##regl_name,\
191 .name = __stringify(chip##_##regl_name),\
192 .of_match = of_match_ptr(#regl_name),\
193 .regulators_node = of_match_ptr("regulators"),\
194 .type = REGULATOR_VOLTAGE,\
195 .owner = THIS_MODULE,\
196 .ops = &pv88090_ldo_ops,\
197 .min_uV = min, \
198 .uV_step = step, \
199 .n_voltages = ((max) - (min))/(step) + 1, \
200 .enable_reg = PV88090_REG_##regl_name##_CONT, \
201 .enable_mask = PV88090_##regl_name##_EN, \
202 .vsel_reg = PV88090_REG_##regl_name##_CONT, \
203 .vsel_mask = PV88090_V##regl_name##_MASK, \
204 },\
205}
206
207static struct pv88090_regulator pv88090_regulator_info[] = {
208 PV88090_BUCK(PV88090, BUCK1, 600000, 6250, 1393750,
209 pv88090_buck1_limits),
210 PV88090_BUCK(PV88090, BUCK2, 600000, 6250, 1393750,
211 pv88090_buck23_limits),
212 PV88090_BUCK(PV88090, BUCK3, 600000, 6250, 1393750,
213 pv88090_buck23_limits),
214 PV88090_LDO(PV88090, LDO1, 1200000, 50000, 4350000),
215 PV88090_LDO(PV88090, LDO2, 650000, 25000, 2225000),
216};
217
218static irqreturn_t pv88090_irq_handler(int irq, void *data)
219{
220 struct pv88090 *chip = data;
221 int i, reg_val, err, ret = IRQ_NONE;
222
223 err = regmap_read(chip->regmap, PV88090_REG_EVENT_A, ®_val);
224 if (err < 0)
225 goto error_i2c;
226
227 if (reg_val & PV88090_E_VDD_FLT) {
228 for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
229 if (chip->rdev[i] != NULL)
230 regulator_notifier_call_chain(chip->rdev[i],
231 REGULATOR_EVENT_UNDER_VOLTAGE,
232 NULL);
233 }
234
235 err = regmap_write(chip->regmap, PV88090_REG_EVENT_A,
236 PV88090_E_VDD_FLT);
237 if (err < 0)
238 goto error_i2c;
239
240 ret = IRQ_HANDLED;
241 }
242
243 if (reg_val & PV88090_E_OVER_TEMP) {
244 for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
245 if (chip->rdev[i] != NULL)
246 regulator_notifier_call_chain(chip->rdev[i],
247 REGULATOR_EVENT_OVER_TEMP,
248 NULL);
249 }
250
251 err = regmap_write(chip->regmap, PV88090_REG_EVENT_A,
252 PV88090_E_OVER_TEMP);
253 if (err < 0)
254 goto error_i2c;
255
256 ret = IRQ_HANDLED;
257 }
258
259 return ret;
260
261error_i2c:
262 dev_err(chip->dev, "I2C error : %d\n", err);
263 return IRQ_NONE;
264}
265
266/*
267 * I2C driver interface functions
268 */
269static int pv88090_i2c_probe(struct i2c_client *i2c)
270{
271 struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
272 struct pv88090 *chip;
273 struct regulator_config config = { };
274 int error, i, ret = 0;
275 unsigned int conf2, range, index;
276
277 chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88090), GFP_KERNEL);
278 if (!chip)
279 return -ENOMEM;
280
281 chip->dev = &i2c->dev;
282 chip->regmap = devm_regmap_init_i2c(i2c, &pv88090_regmap_config);
283 if (IS_ERR(chip->regmap)) {
284 error = PTR_ERR(chip->regmap);
285 dev_err(chip->dev, "Failed to allocate register map: %d\n",
286 error);
287 return error;
288 }
289
290 i2c_set_clientdata(i2c, chip);
291
292 if (i2c->irq != 0) {
293 ret = regmap_write(chip->regmap, PV88090_REG_MASK_A, 0xFF);
294 if (ret < 0) {
295 dev_err(chip->dev,
296 "Failed to mask A reg: %d\n", ret);
297 return ret;
298 }
299
300 ret = regmap_write(chip->regmap, PV88090_REG_MASK_B, 0xFF);
301 if (ret < 0) {
302 dev_err(chip->dev,
303 "Failed to mask B reg: %d\n", ret);
304 return ret;
305 }
306
307 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
308 pv88090_irq_handler,
309 IRQF_TRIGGER_LOW|IRQF_ONESHOT,
310 "pv88090", chip);
311 if (ret != 0) {
312 dev_err(chip->dev, "Failed to request IRQ: %d\n",
313 i2c->irq);
314 return ret;
315 }
316
317 ret = regmap_update_bits(chip->regmap, PV88090_REG_MASK_A,
318 PV88090_M_VDD_FLT | PV88090_M_OVER_TEMP, 0);
319 if (ret < 0) {
320 dev_err(chip->dev,
321 "Failed to update mask reg: %d\n", ret);
322 return ret;
323 }
324
325 } else {
326 dev_warn(chip->dev, "No IRQ configured\n");
327 }
328
329 config.dev = chip->dev;
330 config.regmap = chip->regmap;
331
332 for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
333 if (init_data)
334 config.init_data = &init_data[i];
335
336 if (i == PV88090_ID_BUCK2 || i == PV88090_ID_BUCK3) {
337 ret = regmap_read(chip->regmap,
338 pv88090_regulator_info[i].conf2, &conf2);
339 if (ret < 0)
340 return ret;
341
342 conf2 = (conf2 >> PV88090_BUCK_VDAC_RANGE_SHIFT) &
343 PV88090_BUCK_VDAC_RANGE_MASK;
344
345 ret = regmap_read(chip->regmap,
346 PV88090_REG_BUCK_FOLD_RANGE, &range);
347 if (ret < 0)
348 return ret;
349
350 range = (range >>
351 (PV88090_BUCK_VRANGE_GAIN_SHIFT + i - 1)) &
352 PV88090_BUCK_VRANGE_GAIN_MASK;
353 index = ((range << 1) | conf2);
354 if (index > PV88090_ID_BUCK3) {
355 dev_err(chip->dev,
356 "Invalid index(%d)\n", index);
357 return -EINVAL;
358 }
359
360 pv88090_regulator_info[i].desc.min_uV
361 = pv88090_buck_vol[index].min_uV;
362 pv88090_regulator_info[i].desc.uV_step
363 = pv88090_buck_vol[index].uV_step;
364 pv88090_regulator_info[i].desc.n_voltages
365 = ((pv88090_buck_vol[index].max_uV)
366 - (pv88090_buck_vol[index].min_uV))
367 /(pv88090_buck_vol[index].uV_step) + 1;
368 }
369
370 config.driver_data = (void *)&pv88090_regulator_info[i];
371 chip->rdev[i] = devm_regulator_register(chip->dev,
372 &pv88090_regulator_info[i].desc, &config);
373 if (IS_ERR(chip->rdev[i])) {
374 dev_err(chip->dev,
375 "Failed to register PV88090 regulator\n");
376 return PTR_ERR(chip->rdev[i]);
377 }
378 }
379
380 return 0;
381}
382
383static const struct i2c_device_id pv88090_i2c_id[] = {
384 { "pv88090" },
385 {}
386};
387MODULE_DEVICE_TABLE(i2c, pv88090_i2c_id);
388
389#ifdef CONFIG_OF
390static const struct of_device_id pv88090_dt_ids[] = {
391 { .compatible = "pvs,pv88090", .data = &pv88090_i2c_id[0] },
392 {},
393};
394MODULE_DEVICE_TABLE(of, pv88090_dt_ids);
395#endif
396
397static struct i2c_driver pv88090_regulator_driver = {
398 .driver = {
399 .name = "pv88090",
400 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
401 .of_match_table = of_match_ptr(pv88090_dt_ids),
402 },
403 .probe = pv88090_i2c_probe,
404 .id_table = pv88090_i2c_id,
405};
406
407module_i2c_driver(pv88090_regulator_driver);
408
409MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
410MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88090");
411MODULE_LICENSE("GPL");
1/*
2 * pv88090-regulator.c - Regulator device driver for PV88090
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>
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>
27#include "pv88090-regulator.h"
28
29#define PV88090_MAX_REGULATORS 5
30
31/* PV88090 REGULATOR IDs */
32enum {
33 /* BUCKs */
34 PV88090_ID_BUCK1,
35 PV88090_ID_BUCK2,
36 PV88090_ID_BUCK3,
37
38 /* LDOs */
39 PV88090_ID_LDO1,
40 PV88090_ID_LDO2,
41};
42
43struct pv88090_regulator {
44 struct regulator_desc desc;
45 /* Current limiting */
46 unsigned n_current_limits;
47 const int *current_limits;
48 unsigned int limit_mask;
49 unsigned int conf;
50 unsigned int conf2;
51};
52
53struct pv88090 {
54 struct device *dev;
55 struct regmap *regmap;
56 struct regulator_dev *rdev[PV88090_MAX_REGULATORS];
57};
58
59struct pv88090_buck_voltage {
60 int min_uV;
61 int max_uV;
62 int uV_step;
63};
64
65static const struct regmap_config pv88090_regmap_config = {
66 .reg_bits = 8,
67 .val_bits = 8,
68};
69
70/* Current limits array (in uA) for BUCK1, BUCK2, BUCK3.
71 * Entry indexes corresponds to register values.
72 */
73
74static const int pv88090_buck1_limits[] = {
75 220000, 440000, 660000, 880000, 1100000, 1320000, 1540000, 1760000,
76 1980000, 2200000, 2420000, 2640000, 2860000, 3080000, 3300000, 3520000,
77 3740000, 3960000, 4180000, 4400000, 4620000, 4840000, 5060000, 5280000,
78 5500000, 5720000, 5940000, 6160000, 6380000, 6600000, 6820000, 7040000
79};
80
81static const int pv88090_buck23_limits[] = {
82 1496000, 2393000, 3291000, 4189000
83};
84
85static const struct pv88090_buck_voltage pv88090_buck_vol[3] = {
86 {
87 .min_uV = 600000,
88 .max_uV = 1393750,
89 .uV_step = 6250,
90 },
91
92 {
93 .min_uV = 1400000,
94 .max_uV = 2193750,
95 .uV_step = 6250,
96 },
97 {
98 .min_uV = 1250000,
99 .max_uV = 2837500,
100 .uV_step = 12500,
101 },
102};
103
104static unsigned int pv88090_buck_get_mode(struct regulator_dev *rdev)
105{
106 struct pv88090_regulator *info = rdev_get_drvdata(rdev);
107 unsigned int data;
108 int ret, mode = 0;
109
110 ret = regmap_read(rdev->regmap, info->conf, &data);
111 if (ret < 0)
112 return ret;
113
114 switch (data & PV88090_BUCK1_MODE_MASK) {
115 case PV88090_BUCK_MODE_SYNC:
116 mode = REGULATOR_MODE_FAST;
117 break;
118 case PV88090_BUCK_MODE_AUTO:
119 mode = REGULATOR_MODE_NORMAL;
120 break;
121 case PV88090_BUCK_MODE_SLEEP:
122 mode = REGULATOR_MODE_STANDBY;
123 break;
124 }
125
126 return mode;
127}
128
129static int pv88090_buck_set_mode(struct regulator_dev *rdev,
130 unsigned int mode)
131{
132 struct pv88090_regulator *info = rdev_get_drvdata(rdev);
133 int val = 0;
134
135 switch (mode) {
136 case REGULATOR_MODE_FAST:
137 val = PV88090_BUCK_MODE_SYNC;
138 break;
139 case REGULATOR_MODE_NORMAL:
140 val = PV88090_BUCK_MODE_AUTO;
141 break;
142 case REGULATOR_MODE_STANDBY:
143 val = PV88090_BUCK_MODE_SLEEP;
144 break;
145 default:
146 return -EINVAL;
147 }
148
149 return regmap_update_bits(rdev->regmap, info->conf,
150 PV88090_BUCK1_MODE_MASK, val);
151}
152
153static int pv88090_set_current_limit(struct regulator_dev *rdev, int min,
154 int max)
155{
156 struct pv88090_regulator *info = rdev_get_drvdata(rdev);
157 int i;
158
159 /* search for closest to maximum */
160 for (i = info->n_current_limits; i >= 0; i--) {
161 if (min <= info->current_limits[i]
162 && max >= info->current_limits[i]) {
163 return regmap_update_bits(rdev->regmap,
164 info->conf,
165 info->limit_mask,
166 i << PV88090_BUCK1_ILIM_SHIFT);
167 }
168 }
169
170 return -EINVAL;
171}
172
173static int pv88090_get_current_limit(struct regulator_dev *rdev)
174{
175 struct pv88090_regulator *info = rdev_get_drvdata(rdev);
176 unsigned int data;
177 int ret;
178
179 ret = regmap_read(rdev->regmap, info->conf, &data);
180 if (ret < 0)
181 return ret;
182
183 data = (data & info->limit_mask) >> PV88090_BUCK1_ILIM_SHIFT;
184 return info->current_limits[data];
185}
186
187static struct regulator_ops pv88090_buck_ops = {
188 .get_mode = pv88090_buck_get_mode,
189 .set_mode = pv88090_buck_set_mode,
190 .enable = regulator_enable_regmap,
191 .disable = regulator_disable_regmap,
192 .is_enabled = regulator_is_enabled_regmap,
193 .set_voltage_sel = regulator_set_voltage_sel_regmap,
194 .get_voltage_sel = regulator_get_voltage_sel_regmap,
195 .list_voltage = regulator_list_voltage_linear,
196 .set_current_limit = pv88090_set_current_limit,
197 .get_current_limit = pv88090_get_current_limit,
198};
199
200static struct regulator_ops pv88090_ldo_ops = {
201 .enable = regulator_enable_regmap,
202 .disable = regulator_disable_regmap,
203 .is_enabled = regulator_is_enabled_regmap,
204 .set_voltage_sel = regulator_set_voltage_sel_regmap,
205 .get_voltage_sel = regulator_get_voltage_sel_regmap,
206 .list_voltage = regulator_list_voltage_linear,
207};
208
209#define PV88090_BUCK(chip, regl_name, min, step, max, limits_array) \
210{\
211 .desc = {\
212 .id = chip##_ID_##regl_name,\
213 .name = __stringify(chip##_##regl_name),\
214 .of_match = of_match_ptr(#regl_name),\
215 .regulators_node = of_match_ptr("regulators"),\
216 .type = REGULATOR_VOLTAGE,\
217 .owner = THIS_MODULE,\
218 .ops = &pv88090_buck_ops,\
219 .min_uV = min, \
220 .uV_step = step, \
221 .n_voltages = ((max) - (min))/(step) + 1, \
222 .enable_reg = PV88090_REG_##regl_name##_CONF0, \
223 .enable_mask = PV88090_##regl_name##_EN, \
224 .vsel_reg = PV88090_REG_##regl_name##_CONF0, \
225 .vsel_mask = PV88090_V##regl_name##_MASK, \
226 },\
227 .current_limits = limits_array, \
228 .n_current_limits = ARRAY_SIZE(limits_array), \
229 .limit_mask = PV88090_##regl_name##_ILIM_MASK, \
230 .conf = PV88090_REG_##regl_name##_CONF1, \
231 .conf2 = PV88090_REG_##regl_name##_CONF2, \
232}
233
234#define PV88090_LDO(chip, regl_name, min, step, max) \
235{\
236 .desc = {\
237 .id = chip##_ID_##regl_name,\
238 .name = __stringify(chip##_##regl_name),\
239 .of_match = of_match_ptr(#regl_name),\
240 .regulators_node = of_match_ptr("regulators"),\
241 .type = REGULATOR_VOLTAGE,\
242 .owner = THIS_MODULE,\
243 .ops = &pv88090_ldo_ops,\
244 .min_uV = min, \
245 .uV_step = step, \
246 .n_voltages = ((max) - (min))/(step) + 1, \
247 .enable_reg = PV88090_REG_##regl_name##_CONT, \
248 .enable_mask = PV88090_##regl_name##_EN, \
249 .vsel_reg = PV88090_REG_##regl_name##_CONT, \
250 .vsel_mask = PV88090_V##regl_name##_MASK, \
251 },\
252}
253
254static struct pv88090_regulator pv88090_regulator_info[] = {
255 PV88090_BUCK(PV88090, BUCK1, 600000, 6250, 1393750,
256 pv88090_buck1_limits),
257 PV88090_BUCK(PV88090, BUCK2, 600000, 6250, 1393750,
258 pv88090_buck23_limits),
259 PV88090_BUCK(PV88090, BUCK3, 600000, 6250, 1393750,
260 pv88090_buck23_limits),
261 PV88090_LDO(PV88090, LDO1, 1200000, 50000, 4350000),
262 PV88090_LDO(PV88090, LDO2, 650000, 25000, 2225000),
263};
264
265static irqreturn_t pv88090_irq_handler(int irq, void *data)
266{
267 struct pv88090 *chip = data;
268 int i, reg_val, err, ret = IRQ_NONE;
269
270 err = regmap_read(chip->regmap, PV88090_REG_EVENT_A, ®_val);
271 if (err < 0)
272 goto error_i2c;
273
274 if (reg_val & PV88090_E_VDD_FLT) {
275 for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
276 if (chip->rdev[i] != NULL) {
277 regulator_notifier_call_chain(chip->rdev[i],
278 REGULATOR_EVENT_UNDER_VOLTAGE,
279 NULL);
280 }
281 }
282
283 err = regmap_write(chip->regmap, PV88090_REG_EVENT_A,
284 PV88090_E_VDD_FLT);
285 if (err < 0)
286 goto error_i2c;
287
288 ret = IRQ_HANDLED;
289 }
290
291 if (reg_val & PV88090_E_OVER_TEMP) {
292 for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
293 if (chip->rdev[i] != NULL) {
294 regulator_notifier_call_chain(chip->rdev[i],
295 REGULATOR_EVENT_OVER_TEMP,
296 NULL);
297 }
298 }
299
300 err = regmap_write(chip->regmap, PV88090_REG_EVENT_A,
301 PV88090_E_OVER_TEMP);
302 if (err < 0)
303 goto error_i2c;
304
305 ret = IRQ_HANDLED;
306 }
307
308 return ret;
309
310error_i2c:
311 dev_err(chip->dev, "I2C error : %d\n", err);
312 return IRQ_NONE;
313}
314
315/*
316 * I2C driver interface functions
317 */
318static int pv88090_i2c_probe(struct i2c_client *i2c,
319 const struct i2c_device_id *id)
320{
321 struct regulator_init_data *init_data = dev_get_platdata(&i2c->dev);
322 struct pv88090 *chip;
323 struct regulator_config config = { };
324 int error, i, ret = 0;
325 unsigned int conf2, range, index;
326
327 chip = devm_kzalloc(&i2c->dev, sizeof(struct pv88090), GFP_KERNEL);
328 if (!chip)
329 return -ENOMEM;
330
331 chip->dev = &i2c->dev;
332 chip->regmap = devm_regmap_init_i2c(i2c, &pv88090_regmap_config);
333 if (IS_ERR(chip->regmap)) {
334 error = PTR_ERR(chip->regmap);
335 dev_err(chip->dev, "Failed to allocate register map: %d\n",
336 error);
337 return error;
338 }
339
340 i2c_set_clientdata(i2c, chip);
341
342 if (i2c->irq != 0) {
343 ret = regmap_write(chip->regmap, PV88090_REG_MASK_A, 0xFF);
344 if (ret < 0) {
345 dev_err(chip->dev,
346 "Failed to mask A reg: %d\n", ret);
347 return ret;
348 }
349
350 ret = regmap_write(chip->regmap, PV88090_REG_MASK_B, 0xFF);
351 if (ret < 0) {
352 dev_err(chip->dev,
353 "Failed to mask B reg: %d\n", ret);
354 return ret;
355 }
356
357 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
358 pv88090_irq_handler,
359 IRQF_TRIGGER_LOW|IRQF_ONESHOT,
360 "pv88090", chip);
361 if (ret != 0) {
362 dev_err(chip->dev, "Failed to request IRQ: %d\n",
363 i2c->irq);
364 return ret;
365 }
366
367 ret = regmap_update_bits(chip->regmap, PV88090_REG_MASK_A,
368 PV88090_M_VDD_FLT | PV88090_M_OVER_TEMP, 0);
369 if (ret < 0) {
370 dev_err(chip->dev,
371 "Failed to update mask reg: %d\n", ret);
372 return ret;
373 }
374
375 } else {
376 dev_warn(chip->dev, "No IRQ configured\n");
377 }
378
379 config.dev = chip->dev;
380 config.regmap = chip->regmap;
381
382 for (i = 0; i < PV88090_MAX_REGULATORS; i++) {
383 if (init_data)
384 config.init_data = &init_data[i];
385
386 if (i == PV88090_ID_BUCK2 || i == PV88090_ID_BUCK3) {
387 ret = regmap_read(chip->regmap,
388 pv88090_regulator_info[i].conf2, &conf2);
389 if (ret < 0)
390 return ret;
391
392 conf2 = (conf2 >> PV88090_BUCK_VDAC_RANGE_SHIFT) &
393 PV88090_BUCK_VDAC_RANGE_MASK;
394
395 ret = regmap_read(chip->regmap,
396 PV88090_REG_BUCK_FOLD_RANGE, &range);
397 if (ret < 0)
398 return ret;
399
400 range = (range >>
401 (PV88080_BUCK_VRANGE_GAIN_SHIFT + i - 1)) &
402 PV88080_BUCK_VRANGE_GAIN_MASK;
403 index = ((range << 1) | conf2);
404
405 pv88090_regulator_info[i].desc.min_uV
406 = pv88090_buck_vol[index].min_uV;
407 pv88090_regulator_info[i].desc.uV_step
408 = pv88090_buck_vol[index].uV_step;
409 pv88090_regulator_info[i].desc.n_voltages
410 = ((pv88090_buck_vol[index].max_uV)
411 - (pv88090_buck_vol[index].min_uV))
412 /(pv88090_buck_vol[index].uV_step) + 1;
413 }
414
415 config.driver_data = (void *)&pv88090_regulator_info[i];
416 chip->rdev[i] = devm_regulator_register(chip->dev,
417 &pv88090_regulator_info[i].desc, &config);
418 if (IS_ERR(chip->rdev[i])) {
419 dev_err(chip->dev,
420 "Failed to register PV88090 regulator\n");
421 return PTR_ERR(chip->rdev[i]);
422 }
423 }
424
425 return 0;
426}
427
428static const struct i2c_device_id pv88090_i2c_id[] = {
429 {"pv88090", 0},
430 {},
431};
432MODULE_DEVICE_TABLE(i2c, pv88090_i2c_id);
433
434#ifdef CONFIG_OF
435static const struct of_device_id pv88090_dt_ids[] = {
436 { .compatible = "pvs,pv88090", .data = &pv88090_i2c_id[0] },
437 {},
438};
439MODULE_DEVICE_TABLE(of, pv88090_dt_ids);
440#endif
441
442static struct i2c_driver pv88090_regulator_driver = {
443 .driver = {
444 .name = "pv88090",
445 .of_match_table = of_match_ptr(pv88090_dt_ids),
446 },
447 .probe = pv88090_i2c_probe,
448 .id_table = pv88090_i2c_id,
449};
450
451module_i2c_driver(pv88090_regulator_driver);
452
453MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
454MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88090");
455MODULE_LICENSE("GPL");