1 // SPDX-License-Identifier: GPL-2.0+
3 #include <dt-bindings/regulator/richtek,rt5190a-regulator.h>
4 #include <linux/bits.h>
6 #include <linux/interrupt.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
10 #include <linux/property.h>
11 #include <linux/regmap.h>
12 #include <linux/regulator/driver.h>
13 #include <linux/regulator/machine.h>
14 #include <linux/regulator/of_regulator.h>
16 #define RT5190A_REG_MANUFACTURE 0x00
17 #define RT5190A_REG_BUCK2VSEL 0x04
18 #define RT5190A_REG_BUCK3VSEL 0x05
19 #define RT5190A_REG_DCDCCNTL 0x06
20 #define RT5190A_REG_ENABLE 0x07
21 #define RT5190A_REG_DISCHARGE 0x09
22 #define RT5190A_REG_PROTMODE 0x0A
23 #define RT5190A_REG_MUTECNTL 0x0B
24 #define RT5190A_REG_PGSTAT 0x0F
25 #define RT5190A_REG_OVINT 0x10
26 #define RT5190A_REG_HOTDIEMASK 0x17
28 #define RT5190A_VSEL_MASK GENMASK(6, 0)
29 #define RT5190A_RID_BITMASK(rid) BIT(rid + 1)
30 #define RT5190A_BUCK1_DISCHG_MASK GENMASK(1, 0)
31 #define RT5190A_BUCK1_DISCHG_ONVAL 0x01
32 #define RT5190A_OVERVOLT_MASK GENMASK(7, 0)
33 #define RT5190A_UNDERVOLT_MASK GENMASK(15, 8)
34 #define RT5190A_CH234OT_MASK BIT(29)
35 #define RT5190A_CHIPOT_MASK BIT(28)
37 #define RT5190A_BUCK23_MINUV 600000
38 #define RT5190A_BUCK23_MAXUV 1400000
39 #define RT5190A_BUCK23_STEPUV 10000
40 #define RT5190A_BUCK23_STEPNUM ((1400000 - 600000) / 10000 + 1)
43 RT5190A_IDX_BUCK1 = 0,
53 struct regmap *regmap;
54 struct regulator_desc rdesc[RT5190A_MAX_IDX];
55 struct regulator_dev *rdev[RT5190A_MAX_IDX];
58 static int rt5190a_get_error_flags(struct regulator_dev *rdev,
61 struct regmap *regmap = rdev_get_regmap(rdev);
62 int rid = rdev_get_id(rdev);
63 unsigned int pgood_stat;
66 ret = regmap_read(regmap, RT5190A_REG_PGSTAT, &pgood_stat);
70 if (!(pgood_stat & RT5190A_RID_BITMASK(rid)))
71 *flags = REGULATOR_ERROR_FAIL;
78 static int rt5190a_fixed_buck_set_mode(struct regulator_dev *rdev,
81 struct regmap *regmap = rdev_get_regmap(rdev);
82 int rid = rdev_get_id(rdev);
83 unsigned int mask = RT5190A_RID_BITMASK(rid), val;
86 case REGULATOR_MODE_FAST:
89 case REGULATOR_MODE_NORMAL:
96 return regmap_update_bits(regmap, RT5190A_REG_DCDCCNTL, mask, val);
99 static unsigned int rt5190a_fixed_buck_get_mode(struct regulator_dev *rdev)
101 struct regmap *regmap = rdev_get_regmap(rdev);
102 int rid = rdev_get_id(rdev);
106 ret = regmap_read(regmap, RT5190A_REG_DCDCCNTL, &val);
108 dev_err(&rdev->dev, "Failed to get mode [%d]\n", ret);
112 if (val & RT5190A_RID_BITMASK(rid))
113 return REGULATOR_MODE_FAST;
115 return REGULATOR_MODE_NORMAL;
118 static const struct regulator_ops rt5190a_ranged_buck_ops = {
119 .enable = regulator_enable_regmap,
120 .disable = regulator_disable_regmap,
121 .is_enabled = regulator_is_enabled_regmap,
122 .set_voltage_sel = regulator_set_voltage_sel_regmap,
123 .get_voltage_sel = regulator_get_voltage_sel_regmap,
124 .list_voltage = regulator_list_voltage_linear,
125 .set_active_discharge = regulator_set_active_discharge_regmap,
126 .get_error_flags = rt5190a_get_error_flags,
129 static const struct regulator_ops rt5190a_fixed_buck_ops = {
130 .enable = regulator_enable_regmap,
131 .disable = regulator_disable_regmap,
132 .is_enabled = regulator_is_enabled_regmap,
133 .set_active_discharge = regulator_set_active_discharge_regmap,
134 .set_mode = rt5190a_fixed_buck_set_mode,
135 .get_mode = rt5190a_fixed_buck_get_mode,
136 .get_error_flags = rt5190a_get_error_flags,
139 static const struct regulator_ops rt5190a_fixed_ldo_ops = {
140 .enable = regulator_enable_regmap,
141 .disable = regulator_disable_regmap,
142 .is_enabled = regulator_is_enabled_regmap,
143 .set_active_discharge = regulator_set_active_discharge_regmap,
144 .get_error_flags = rt5190a_get_error_flags,
147 static irqreturn_t rt5190a_irq_handler(int irq, void *data)
149 struct rt5190a_priv *priv = data;
151 unsigned int events, fields;
152 static const struct {
153 unsigned int bitmask;
156 { RT5190A_OVERVOLT_MASK, REGULATOR_ERROR_REGULATION_OUT },
157 { RT5190A_UNDERVOLT_MASK, REGULATOR_ERROR_UNDER_VOLTAGE }
161 ret = regmap_raw_read(priv->regmap, RT5190A_REG_OVINT, &raws,
164 dev_err(priv->dev, "Failed to read events\n");
168 events = le32_to_cpu(raws);
170 ret = regmap_raw_write(priv->regmap, RT5190A_REG_OVINT, &raws,
173 dev_err(priv->dev, "Failed to write-clear events\n");
175 /* Handle OV,UV events */
176 for (i = 0; i < ARRAY_SIZE(event_tbl); i++) {
177 fields = events & event_tbl[i].bitmask;
178 fields >>= ffs(event_tbl[i].bitmask) - 1;
180 for (j = 0; j < RT5190A_MAX_IDX; j++) {
181 if (!(fields & RT5190A_RID_BITMASK(j)))
184 regulator_notifier_call_chain(priv->rdev[j],
190 /* Handle CH234 OT event */
191 if (events & RT5190A_CH234OT_MASK) {
192 for (j = RT5190A_IDX_BUCK2; j < RT5190A_IDX_LDO; j++) {
193 regulator_notifier_call_chain(priv->rdev[j],
194 REGULATOR_ERROR_OVER_TEMP,
199 /* Warning if CHIP OT occur */
200 if (events & RT5190A_CHIPOT_MASK)
201 dev_warn(priv->dev, "CHIP overheat\n");
206 static unsigned int rt5190a_of_map_mode(unsigned int mode)
209 case RT5190A_OPMODE_AUTO:
210 return REGULATOR_MODE_NORMAL;
211 case RT5190A_OPMODE_FPWM:
212 return REGULATOR_MODE_FAST;
214 return REGULATOR_MODE_INVALID;
218 static int rt5190a_of_parse_cb(struct rt5190a_priv *priv, int rid,
219 struct of_regulator_match *match)
221 struct regulator_desc *desc = priv->rdesc + rid;
222 struct regulator_init_data *init_data = match->init_data;
223 struct device_node *np = match->of_node;
225 unsigned int mask = RT5190A_RID_BITMASK(rid), val;
231 case RT5190A_IDX_BUCK1:
232 case RT5190A_IDX_BUCK4:
233 case RT5190A_IDX_LDO:
234 init_data->constraints.apply_uV = 0;
236 if (init_data->constraints.min_uV ==
237 init_data->constraints.max_uV)
238 desc->fixed_uV = init_data->constraints.min_uV;
241 "Variable voltage for fixed regulator\n");
249 latchup_enable = of_property_read_bool(np, "richtek,latchup-enable");
251 /* latchup: 0, default hiccup: 1 */
252 val = !latchup_enable ? mask : 0;
254 return regmap_update_bits(priv->regmap, RT5190A_REG_PROTMODE, mask, val);
257 static void rt5190a_fillin_regulator_desc(struct regulator_desc *desc, int rid)
259 static const char * const regu_name[] = { "buck1", "buck2",
262 static const char * const supply[] = { NULL, "vin2", "vin3", "vin4",
265 desc->name = regu_name[rid];
266 desc->supply_name = supply[rid];
267 desc->owner = THIS_MODULE;
268 desc->type = REGULATOR_VOLTAGE;
270 desc->enable_reg = RT5190A_REG_ENABLE;
271 desc->enable_mask = RT5190A_RID_BITMASK(rid);
272 desc->active_discharge_reg = RT5190A_REG_DISCHARGE;
273 desc->active_discharge_mask = RT5190A_RID_BITMASK(rid);
274 desc->active_discharge_on = RT5190A_RID_BITMASK(rid);
277 case RT5190A_IDX_BUCK1:
278 desc->active_discharge_mask = RT5190A_BUCK1_DISCHG_MASK;
279 desc->active_discharge_on = RT5190A_BUCK1_DISCHG_ONVAL;
280 desc->n_voltages = 1;
281 desc->ops = &rt5190a_fixed_buck_ops;
282 desc->of_map_mode = rt5190a_of_map_mode;
284 case RT5190A_IDX_BUCK2:
285 desc->vsel_reg = RT5190A_REG_BUCK2VSEL;
286 desc->vsel_mask = RT5190A_VSEL_MASK;
287 desc->min_uV = RT5190A_BUCK23_MINUV;
288 desc->uV_step = RT5190A_BUCK23_STEPUV;
289 desc->n_voltages = RT5190A_BUCK23_STEPNUM;
290 desc->ops = &rt5190a_ranged_buck_ops;
292 case RT5190A_IDX_BUCK3:
293 desc->vsel_reg = RT5190A_REG_BUCK3VSEL;
294 desc->vsel_mask = RT5190A_VSEL_MASK;
295 desc->min_uV = RT5190A_BUCK23_MINUV;
296 desc->uV_step = RT5190A_BUCK23_STEPUV;
297 desc->n_voltages = RT5190A_BUCK23_STEPNUM;
298 desc->ops = &rt5190a_ranged_buck_ops;
300 case RT5190A_IDX_BUCK4:
301 desc->n_voltages = 1;
302 desc->ops = &rt5190a_fixed_buck_ops;
303 desc->of_map_mode = rt5190a_of_map_mode;
305 case RT5190A_IDX_LDO:
306 desc->n_voltages = 1;
307 desc->ops = &rt5190a_fixed_ldo_ops;
312 static struct of_regulator_match rt5190a_regulator_match[] = {
313 { .name = "buck1", },
314 { .name = "buck2", },
315 { .name = "buck3", },
316 { .name = "buck4", },
320 static int rt5190a_parse_regulator_dt_data(struct rt5190a_priv *priv)
322 struct device_node *regulator_np;
323 struct regulator_desc *reg_desc;
324 struct of_regulator_match *match;
327 for (i = 0; i < RT5190A_MAX_IDX; i++) {
328 reg_desc = priv->rdesc + i;
329 match = rt5190a_regulator_match + i;
331 rt5190a_fillin_regulator_desc(reg_desc, i);
333 match->desc = reg_desc;
336 regulator_np = of_get_child_by_name(priv->dev->of_node, "regulators");
338 dev_err(priv->dev, "Could not find 'regulators' node\n");
342 ret = of_regulator_match(priv->dev, regulator_np,
343 rt5190a_regulator_match,
344 ARRAY_SIZE(rt5190a_regulator_match));
346 of_node_put(regulator_np);
350 "Error parsing regulator init data: %d\n", ret);
354 for (i = 0; i < RT5190A_MAX_IDX; i++) {
355 match = rt5190a_regulator_match + i;
357 ret = rt5190a_of_parse_cb(priv, i, match);
359 dev_err(priv->dev, "Failed in [%d] of_parse_cb\n", i);
367 static const struct reg_sequence rt5190a_init_patch[] = {
381 static int rt5190a_device_initialize(struct rt5190a_priv *priv)
386 ret = regmap_register_patch(priv->regmap, rt5190a_init_patch,
387 ARRAY_SIZE(rt5190a_init_patch));
389 dev_err(priv->dev, "Failed to do register patch\n");
393 mute_enable = device_property_read_bool(priv->dev,
394 "richtek,mute-enable");
397 ret = regmap_write(priv->regmap, RT5190A_REG_MUTECNTL, 0x00);
399 dev_err(priv->dev, "Failed to enable mute function\n");
407 static int rt5190a_device_check(struct rt5190a_priv *priv)
412 ret = regmap_raw_read(priv->regmap, RT5190A_REG_MANUFACTURE, &devid,
418 dev_err(priv->dev, "Incorrect device id 0x%04x\n", devid);
425 static const struct regmap_config rt5190a_regmap_config = {
428 .max_register = RT5190A_REG_HOTDIEMASK,
431 static int rt5190a_probe(struct i2c_client *i2c)
433 struct rt5190a_priv *priv;
434 struct regulator_config cfg = {};
437 priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
441 priv->dev = &i2c->dev;
443 priv->regmap = devm_regmap_init_i2c(i2c, &rt5190a_regmap_config);
444 if (IS_ERR(priv->regmap)) {
445 dev_err(&i2c->dev, "Failed to allocate regmap\n");
446 return PTR_ERR(priv->regmap);
449 ret = rt5190a_device_check(priv);
451 dev_err(&i2c->dev, "Failed to check device %d\n", ret);
455 ret = rt5190a_device_initialize(priv);
457 dev_err(&i2c->dev, "Failed to initialize the device\n");
461 ret = rt5190a_parse_regulator_dt_data(priv);
463 dev_err(&i2c->dev, "Failed to parse regulator dt\n");
468 cfg.regmap = priv->regmap;
470 for (i = 0; i < RT5190A_MAX_IDX; i++) {
471 struct regulator_desc *desc = priv->rdesc + i;
472 struct of_regulator_match *match = rt5190a_regulator_match + i;
474 cfg.init_data = match->init_data;
475 cfg.of_node = match->of_node;
477 priv->rdev[i] = devm_regulator_register(&i2c->dev, desc, &cfg);
478 if (IS_ERR(priv->rdev[i])) {
479 dev_err(&i2c->dev, "Failed to register regulator %s\n",
481 return PTR_ERR(priv->rdev[i]);
486 ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
489 dev_name(&i2c->dev), priv);
491 dev_err(&i2c->dev, "Failed to register interrupt\n");
499 static const struct of_device_id __maybe_unused rt5190a_device_table[] = {
500 { .compatible = "richtek,rt5190a", },
503 MODULE_DEVICE_TABLE(of, rt5190a_device_table);
505 static struct i2c_driver rt5190a_driver = {
508 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
509 .of_match_table = rt5190a_device_table,
511 .probe = rt5190a_probe,
513 module_i2c_driver(rt5190a_driver);
515 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
516 MODULE_DESCRIPTION("Richtek RT5190A Regulator Driver");
517 MODULE_LICENSE("GPL v2");