1 // SPDX-License-Identifier: GPL-2.0+
3 // pv88060-regulator.c - Regulator device driver for PV88060
4 // Copyright (C) 2015 Powerventure Semiconductor Ltd.
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 "pv88060-regulator.h"
19 #define PV88060_MAX_REGULATORS 14
21 /* PV88060 REGULATOR IDs */
44 struct pv88060_regulator
{
45 struct regulator_desc desc
;
46 unsigned int conf
; /* buck configuration register */
51 struct regmap
*regmap
;
52 struct regulator_dev
*rdev
[PV88060_MAX_REGULATORS
];
55 static const struct regmap_config pv88060_regmap_config
= {
60 /* Current limits array (in uA) for BUCK1
61 * Entry indexes corresponds to register values.
64 static const unsigned int pv88060_buck1_limits
[] = {
65 1496000, 2393000, 3291000, 4189000
68 static unsigned int pv88060_buck_get_mode(struct regulator_dev
*rdev
)
70 struct pv88060_regulator
*info
= rdev_get_drvdata(rdev
);
74 ret
= regmap_read(rdev
->regmap
, info
->conf
, &data
);
78 switch (data
& PV88060_BUCK_MODE_MASK
) {
79 case PV88060_BUCK_MODE_SYNC
:
80 mode
= REGULATOR_MODE_FAST
;
82 case PV88060_BUCK_MODE_AUTO
:
83 mode
= REGULATOR_MODE_NORMAL
;
85 case PV88060_BUCK_MODE_SLEEP
:
86 mode
= REGULATOR_MODE_STANDBY
;
93 static int pv88060_buck_set_mode(struct regulator_dev
*rdev
,
96 struct pv88060_regulator
*info
= rdev_get_drvdata(rdev
);
100 case REGULATOR_MODE_FAST
:
101 val
= PV88060_BUCK_MODE_SYNC
;
103 case REGULATOR_MODE_NORMAL
:
104 val
= PV88060_BUCK_MODE_AUTO
;
106 case REGULATOR_MODE_STANDBY
:
107 val
= PV88060_BUCK_MODE_SLEEP
;
113 return regmap_update_bits(rdev
->regmap
, info
->conf
,
114 PV88060_BUCK_MODE_MASK
, val
);
117 static const struct regulator_ops pv88060_buck_ops
= {
118 .get_mode
= pv88060_buck_get_mode
,
119 .set_mode
= pv88060_buck_set_mode
,
120 .enable
= regulator_enable_regmap
,
121 .disable
= regulator_disable_regmap
,
122 .is_enabled
= regulator_is_enabled_regmap
,
123 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
124 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
125 .list_voltage
= regulator_list_voltage_linear
,
126 .set_current_limit
= regulator_set_current_limit_regmap
,
127 .get_current_limit
= regulator_get_current_limit_regmap
,
130 static const struct regulator_ops pv88060_ldo_ops
= {
131 .enable
= regulator_enable_regmap
,
132 .disable
= regulator_disable_regmap
,
133 .is_enabled
= regulator_is_enabled_regmap
,
134 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
135 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
136 .list_voltage
= regulator_list_voltage_linear
,
139 static const struct regulator_ops pv88060_sw_ops
= {
140 .enable
= regulator_enable_regmap
,
141 .disable
= regulator_disable_regmap
,
142 .is_enabled
= regulator_is_enabled_regmap
,
145 #define PV88060_BUCK(chip, regl_name, min, step, max, limits_array) \
148 .id = chip##_ID_##regl_name,\
149 .name = __stringify(chip##_##regl_name),\
150 .of_match = of_match_ptr(#regl_name),\
151 .regulators_node = of_match_ptr("regulators"),\
152 .type = REGULATOR_VOLTAGE,\
153 .owner = THIS_MODULE,\
154 .ops = &pv88060_buck_ops,\
157 .n_voltages = ((max) - (min))/(step) + 1,\
158 .enable_reg = PV88060_REG_##regl_name##_CONF0,\
159 .enable_mask = PV88060_BUCK_EN, \
160 .vsel_reg = PV88060_REG_##regl_name##_CONF0,\
161 .vsel_mask = PV88060_VBUCK_MASK,\
162 .curr_table = limits_array,\
163 .n_current_limits = ARRAY_SIZE(limits_array),\
164 .csel_reg = PV88060_REG_##regl_name##_CONF1,\
165 .csel_mask = PV88060_BUCK_ILIM_MASK,\
167 .conf = PV88060_REG_##regl_name##_CONF1,\
170 #define PV88060_LDO(chip, regl_name, min, step, max) \
173 .id = chip##_ID_##regl_name,\
174 .name = __stringify(chip##_##regl_name),\
175 .of_match = of_match_ptr(#regl_name),\
176 .regulators_node = of_match_ptr("regulators"),\
177 .type = REGULATOR_VOLTAGE,\
178 .owner = THIS_MODULE,\
179 .ops = &pv88060_ldo_ops,\
182 .n_voltages = (step) ? ((max - min) / step + 1) : 1, \
183 .enable_reg = PV88060_REG_##regl_name##_CONF, \
184 .enable_mask = PV88060_LDO_EN, \
185 .vsel_reg = PV88060_REG_##regl_name##_CONF, \
186 .vsel_mask = PV88060_VLDO_MASK, \
190 #define PV88060_SW(chip, regl_name, max) \
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 = &pv88060_sw_ops,\
202 .enable_reg = PV88060_REG_##regl_name##_CONF,\
203 .enable_mask = PV88060_SW_EN,\
207 static const struct pv88060_regulator pv88060_regulator_info
[] = {
208 PV88060_BUCK(PV88060
, BUCK1
, 2800000, 12500, 4387500,
209 pv88060_buck1_limits
),
210 PV88060_LDO(PV88060
, LDO1
, 1200000, 50000, 3350000),
211 PV88060_LDO(PV88060
, LDO2
, 1200000, 50000, 3350000),
212 PV88060_LDO(PV88060
, LDO3
, 1200000, 50000, 3350000),
213 PV88060_LDO(PV88060
, LDO4
, 1200000, 50000, 3350000),
214 PV88060_LDO(PV88060
, LDO5
, 1200000, 50000, 3350000),
215 PV88060_LDO(PV88060
, LDO6
, 1200000, 50000, 3350000),
216 PV88060_LDO(PV88060
, LDO7
, 1200000, 50000, 3350000),
217 PV88060_SW(PV88060
, SW1
, 5000000),
218 PV88060_SW(PV88060
, SW2
, 5000000),
219 PV88060_SW(PV88060
, SW3
, 5000000),
220 PV88060_SW(PV88060
, SW4
, 5000000),
221 PV88060_SW(PV88060
, SW5
, 5000000),
222 PV88060_SW(PV88060
, SW6
, 5000000),
225 static irqreturn_t
pv88060_irq_handler(int irq
, void *data
)
227 struct pv88060
*chip
= data
;
228 int i
, reg_val
, err
, ret
= IRQ_NONE
;
230 err
= regmap_read(chip
->regmap
, PV88060_REG_EVENT_A
, ®_val
);
234 if (reg_val
& PV88060_E_VDD_FLT
) {
235 for (i
= 0; i
< PV88060_MAX_REGULATORS
; i
++) {
236 if (chip
->rdev
[i
] != NULL
) {
237 regulator_lock(chip
->rdev
[i
]);
238 regulator_notifier_call_chain(chip
->rdev
[i
],
239 REGULATOR_EVENT_UNDER_VOLTAGE
,
241 regulator_unlock(chip
->rdev
[i
]);
245 err
= regmap_write(chip
->regmap
, PV88060_REG_EVENT_A
,
253 if (reg_val
& PV88060_E_OVER_TEMP
) {
254 for (i
= 0; i
< PV88060_MAX_REGULATORS
; i
++) {
255 if (chip
->rdev
[i
] != NULL
) {
256 regulator_lock(chip
->rdev
[i
]);
257 regulator_notifier_call_chain(chip
->rdev
[i
],
258 REGULATOR_EVENT_OVER_TEMP
,
260 regulator_unlock(chip
->rdev
[i
]);
264 err
= regmap_write(chip
->regmap
, PV88060_REG_EVENT_A
,
265 PV88060_E_OVER_TEMP
);
275 dev_err(chip
->dev
, "I2C error : %d\n", err
);
280 * I2C driver interface functions
282 static int pv88060_i2c_probe(struct i2c_client
*i2c
,
283 const struct i2c_device_id
*id
)
285 struct regulator_init_data
*init_data
= dev_get_platdata(&i2c
->dev
);
286 struct pv88060
*chip
;
287 struct regulator_config config
= { };
288 int error
, i
, ret
= 0;
290 chip
= devm_kzalloc(&i2c
->dev
, sizeof(struct pv88060
), GFP_KERNEL
);
294 chip
->dev
= &i2c
->dev
;
295 chip
->regmap
= devm_regmap_init_i2c(i2c
, &pv88060_regmap_config
);
296 if (IS_ERR(chip
->regmap
)) {
297 error
= PTR_ERR(chip
->regmap
);
298 dev_err(chip
->dev
, "Failed to allocate register map: %d\n",
303 i2c_set_clientdata(i2c
, chip
);
306 ret
= regmap_write(chip
->regmap
, PV88060_REG_MASK_A
, 0xFF);
309 "Failed to mask A reg: %d\n", ret
);
313 ret
= regmap_write(chip
->regmap
, PV88060_REG_MASK_B
, 0xFF);
316 "Failed to mask B reg: %d\n", ret
);
320 ret
= regmap_write(chip
->regmap
, PV88060_REG_MASK_C
, 0xFF);
323 "Failed to mask C reg: %d\n", ret
);
327 ret
= devm_request_threaded_irq(&i2c
->dev
, i2c
->irq
, NULL
,
329 IRQF_TRIGGER_LOW
|IRQF_ONESHOT
,
332 dev_err(chip
->dev
, "Failed to request IRQ: %d\n",
337 ret
= regmap_update_bits(chip
->regmap
, PV88060_REG_MASK_A
,
338 PV88060_M_VDD_FLT
| PV88060_M_OVER_TEMP
, 0);
341 "Failed to update mask reg: %d\n", ret
);
346 dev_warn(chip
->dev
, "No IRQ configured\n");
349 config
.dev
= chip
->dev
;
350 config
.regmap
= chip
->regmap
;
352 for (i
= 0; i
< PV88060_MAX_REGULATORS
; i
++) {
354 config
.init_data
= &init_data
[i
];
356 config
.driver_data
= (void *)&pv88060_regulator_info
[i
];
357 chip
->rdev
[i
] = devm_regulator_register(chip
->dev
,
358 &pv88060_regulator_info
[i
].desc
, &config
);
359 if (IS_ERR(chip
->rdev
[i
])) {
361 "Failed to register PV88060 regulator\n");
362 return PTR_ERR(chip
->rdev
[i
]);
369 static const struct i2c_device_id pv88060_i2c_id
[] = {
373 MODULE_DEVICE_TABLE(i2c
, pv88060_i2c_id
);
376 static const struct of_device_id pv88060_dt_ids
[] = {
377 { .compatible
= "pvs,pv88060", .data
= &pv88060_i2c_id
[0] },
380 MODULE_DEVICE_TABLE(of
, pv88060_dt_ids
);
383 static struct i2c_driver pv88060_regulator_driver
= {
386 .of_match_table
= of_match_ptr(pv88060_dt_ids
),
388 .probe
= pv88060_i2c_probe
,
389 .id_table
= pv88060_i2c_id
,
392 module_i2c_driver(pv88060_regulator_driver
);
394 MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>");
395 MODULE_DESCRIPTION("Regulator device driver for Powerventure PV88060");
396 MODULE_LICENSE("GPL");