0e0f099e5058dfd407dae23ed186f72c9bebd4d8
[openwrt/staging/blogic.git] /
1 From 760423dfad53877b468490758fe7ea968ded9402 Mon Sep 17 00:00:00 2001
2 From: ChiYuan Huang <cy_huang@richtek.com>
3 Date: Wed, 9 Mar 2022 16:01:43 +0800
4 Subject: [PATCH 2/2] regulator: rt5190a: Add support for Richtek RT5190A PMIC
5
6 Add support for Richtek RT5190A PMIC.
7
8 Signed-off-by: ChiYuan Huang <cy_huang@richtek.com>
9 Link: https://lore.kernel.org/r/1646812903-32496-3-git-send-email-u0084500@gmail.com
10 Signed-off-by: Mark Brown <broonie@kernel.org>
11 ---
12 drivers/regulator/Kconfig | 10 +
13 drivers/regulator/Makefile | 1 +
14 drivers/regulator/rt5190a-regulator.c | 513 ++++++++++++++++++++++++++
15 3 files changed, 524 insertions(+)
16 create mode 100644 drivers/regulator/rt5190a-regulator.c
17
18 --- a/drivers/regulator/Kconfig
19 +++ b/drivers/regulator/Kconfig
20 @@ -1037,6 +1037,16 @@ config REGULATOR_RT5033
21 RT5033 PMIC. The device supports multiple regulators like
22 current source, LDO and Buck.
23
24 +config REGULATOR_RT5190A
25 + tristate "Richtek RT5190A PMIC"
26 + depends on I2C
27 + select REGMAP_I2C
28 + help
29 + This adds support for voltage regulator in Richtek RT5190A PMIC.
30 + It integratas 1 channel buck controller, 3 channels high efficiency
31 + buck converters, 1 LDO, mute AC OFF depop function, with the general
32 + I2C control interface.
33 +
34 config REGULATOR_RT6160
35 tristate "Richtek RT6160 BuckBoost voltage regulator"
36 depends on I2C
37 --- a/drivers/regulator/Makefile
38 +++ b/drivers/regulator/Makefile
39 @@ -125,6 +125,7 @@ obj-$(CONFIG_REGULATOR_ROHM) += rohm-reg
40 obj-$(CONFIG_REGULATOR_RT4801) += rt4801-regulator.o
41 obj-$(CONFIG_REGULATOR_RT4831) += rt4831-regulator.o
42 obj-$(CONFIG_REGULATOR_RT5033) += rt5033-regulator.o
43 +obj-$(CONFIG_REGULATOR_RT5190A) += rt5190a-regulator.o
44 obj-$(CONFIG_REGULATOR_RT6160) += rt6160-regulator.o
45 obj-$(CONFIG_REGULATOR_RT6245) += rt6245-regulator.o
46 obj-$(CONFIG_REGULATOR_RTMV20) += rtmv20-regulator.o
47 --- /dev/null
48 +++ b/drivers/regulator/rt5190a-regulator.c
49 @@ -0,0 +1,513 @@
50 +// SPDX-License-Identifier: GPL-2.0+
51 +
52 +#include <dt-bindings/regulator/richtek,rt5190a-regulator.h>
53 +#include <linux/bits.h>
54 +#include <linux/i2c.h>
55 +#include <linux/interrupt.h>
56 +#include <linux/kernel.h>
57 +#include <linux/module.h>
58 +#include <linux/of.h>
59 +#include <linux/property.h>
60 +#include <linux/regmap.h>
61 +#include <linux/regulator/driver.h>
62 +#include <linux/regulator/machine.h>
63 +#include <linux/regulator/of_regulator.h>
64 +
65 +#define RT5190A_REG_MANUFACTURE 0x00
66 +#define RT5190A_REG_BUCK2VSEL 0x04
67 +#define RT5190A_REG_BUCK3VSEL 0x05
68 +#define RT5190A_REG_DCDCCNTL 0x06
69 +#define RT5190A_REG_ENABLE 0x07
70 +#define RT5190A_REG_DISCHARGE 0x09
71 +#define RT5190A_REG_PROTMODE 0x0A
72 +#define RT5190A_REG_MUTECNTL 0x0B
73 +#define RT5190A_REG_PGSTAT 0x0F
74 +#define RT5190A_REG_OVINT 0x10
75 +#define RT5190A_REG_HOTDIEMASK 0x17
76 +
77 +#define RT5190A_VSEL_MASK GENMASK(6, 0)
78 +#define RT5190A_RID_BITMASK(rid) BIT(rid + 1)
79 +#define RT5190A_BUCK1_DISCHG_MASK GENMASK(1, 0)
80 +#define RT5190A_BUCK1_DISCHG_ONVAL 0x01
81 +#define RT5190A_OVERVOLT_MASK GENMASK(7, 0)
82 +#define RT5190A_UNDERVOLT_MASK GENMASK(15, 8)
83 +#define RT5190A_CH234OT_MASK BIT(29)
84 +#define RT5190A_CHIPOT_MASK BIT(28)
85 +
86 +#define RT5190A_BUCK23_MINUV 600000
87 +#define RT5190A_BUCK23_MAXUV 1400000
88 +#define RT5190A_BUCK23_STEPUV 10000
89 +#define RT5190A_BUCK23_STEPNUM ((1400000 - 600000) / 10000 + 1)
90 +
91 +enum {
92 + RT5190A_IDX_BUCK1 = 0,
93 + RT5190A_IDX_BUCK2,
94 + RT5190A_IDX_BUCK3,
95 + RT5190A_IDX_BUCK4,
96 + RT5190A_IDX_LDO,
97 + RT5190A_MAX_IDX
98 +};
99 +
100 +struct rt5190a_priv {
101 + struct device *dev;
102 + struct regmap *regmap;
103 + struct regulator_desc rdesc[RT5190A_MAX_IDX];
104 + struct regulator_dev *rdev[RT5190A_MAX_IDX];
105 +};
106 +
107 +static int rt5190a_get_error_flags(struct regulator_dev *rdev,
108 + unsigned int *flags)
109 +{
110 + struct regmap *regmap = rdev_get_regmap(rdev);
111 + int rid = rdev_get_id(rdev);
112 + unsigned int pgood_stat;
113 + int ret;
114 +
115 + ret = regmap_read(regmap, RT5190A_REG_PGSTAT, &pgood_stat);
116 + if (ret)
117 + return ret;
118 +
119 + if (!(pgood_stat & RT5190A_RID_BITMASK(rid)))
120 + *flags = REGULATOR_ERROR_FAIL;
121 + else
122 + *flags = 0;
123 +
124 + return 0;
125 +}
126 +
127 +static int rt5190a_fixed_buck_set_mode(struct regulator_dev *rdev,
128 + unsigned int mode)
129 +{
130 + struct regmap *regmap = rdev_get_regmap(rdev);
131 + int rid = rdev_get_id(rdev);
132 + unsigned int mask = RT5190A_RID_BITMASK(rid), val;
133 +
134 + switch (mode) {
135 + case REGULATOR_MODE_FAST:
136 + val = mask;
137 + break;
138 + case REGULATOR_MODE_NORMAL:
139 + val = 0;
140 + break;
141 + default:
142 + return -EINVAL;
143 + }
144 +
145 + return regmap_update_bits(regmap, RT5190A_REG_DCDCCNTL, mask, val);
146 +}
147 +
148 +static unsigned int rt5190a_fixed_buck_get_mode(struct regulator_dev *rdev)
149 +{
150 + struct regmap *regmap = rdev_get_regmap(rdev);
151 + int rid = rdev_get_id(rdev);
152 + unsigned int val;
153 + int ret;
154 +
155 + ret = regmap_read(regmap, RT5190A_REG_DCDCCNTL, &val);
156 + if (ret) {
157 + dev_err(&rdev->dev, "Failed to get mode [%d]\n", ret);
158 + return ret;
159 + }
160 +
161 + if (val & RT5190A_RID_BITMASK(rid))
162 + return REGULATOR_MODE_FAST;
163 +
164 + return REGULATOR_MODE_NORMAL;
165 +}
166 +
167 +static const struct regulator_ops rt5190a_ranged_buck_ops = {
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_active_discharge = regulator_set_active_discharge_regmap,
175 + .get_error_flags = rt5190a_get_error_flags,
176 +};
177 +
178 +static const struct regulator_ops rt5190a_fixed_buck_ops = {
179 + .enable = regulator_enable_regmap,
180 + .disable = regulator_disable_regmap,
181 + .is_enabled = regulator_is_enabled_regmap,
182 + .set_active_discharge = regulator_set_active_discharge_regmap,
183 + .set_mode = rt5190a_fixed_buck_set_mode,
184 + .get_mode = rt5190a_fixed_buck_get_mode,
185 + .get_error_flags = rt5190a_get_error_flags,
186 +};
187 +
188 +static const struct regulator_ops rt5190a_fixed_ldo_ops = {
189 + .enable = regulator_enable_regmap,
190 + .disable = regulator_disable_regmap,
191 + .is_enabled = regulator_is_enabled_regmap,
192 + .set_active_discharge = regulator_set_active_discharge_regmap,
193 + .get_error_flags = rt5190a_get_error_flags,
194 +};
195 +
196 +static irqreturn_t rt5190a_irq_handler(int irq, void *data)
197 +{
198 + struct rt5190a_priv *priv = data;
199 + __le32 raws;
200 + unsigned int events, fields;
201 + static const struct {
202 + unsigned int bitmask;
203 + unsigned int report;
204 + } event_tbl[] = {
205 + { RT5190A_OVERVOLT_MASK, REGULATOR_ERROR_REGULATION_OUT },
206 + { RT5190A_UNDERVOLT_MASK, REGULATOR_ERROR_UNDER_VOLTAGE }
207 + };
208 + int i, j, ret;
209 +
210 + ret = regmap_raw_read(priv->regmap, RT5190A_REG_OVINT, &raws,
211 + sizeof(raws));
212 + if (ret) {
213 + dev_err(priv->dev, "Failed to read events\n");
214 + return IRQ_NONE;
215 + }
216 +
217 + events = le32_to_cpu(raws);
218 +
219 + ret = regmap_raw_write(priv->regmap, RT5190A_REG_OVINT, &raws,
220 + sizeof(raws));
221 + if (ret)
222 + dev_err(priv->dev, "Failed to write-clear events\n");
223 +
224 + /* Handle OV,UV events */
225 + for (i = 0; i < ARRAY_SIZE(event_tbl); i++) {
226 + fields = events & event_tbl[i].bitmask;
227 + fields >>= ffs(event_tbl[i].bitmask) - 1;
228 +
229 + for (j = 0; j < RT5190A_MAX_IDX; j++) {
230 + if (!(fields & RT5190A_RID_BITMASK(j)))
231 + continue;
232 +
233 + regulator_notifier_call_chain(priv->rdev[j],
234 + event_tbl[i].report,
235 + NULL);
236 + }
237 + }
238 +
239 + /* Handle CH234 OT event */
240 + if (events & RT5190A_CH234OT_MASK) {
241 + for (j = RT5190A_IDX_BUCK2; j < RT5190A_IDX_LDO; j++) {
242 + regulator_notifier_call_chain(priv->rdev[j],
243 + REGULATOR_ERROR_OVER_TEMP,
244 + NULL);
245 + }
246 + }
247 +
248 + /* Warning if CHIP OT occur */
249 + if (events & RT5190A_CHIPOT_MASK)
250 + dev_warn(priv->dev, "CHIP overheat\n");
251 +
252 + return IRQ_HANDLED;
253 +}
254 +
255 +static unsigned int rt5190a_of_map_mode(unsigned int mode)
256 +{
257 + switch (mode) {
258 + case RT5190A_OPMODE_AUTO:
259 + return REGULATOR_MODE_NORMAL;
260 + case RT5190A_OPMODE_FPWM:
261 + return REGULATOR_MODE_FAST;
262 + default:
263 + return REGULATOR_MODE_INVALID;
264 + }
265 +}
266 +
267 +static int rt5190a_of_parse_cb(struct rt5190a_priv *priv, int rid,
268 + struct of_regulator_match *match)
269 +{
270 + struct regulator_desc *desc = priv->rdesc + rid;
271 + struct regulator_init_data *init_data = match->init_data;
272 + struct device_node *np = match->of_node;
273 + bool latchup_enable;
274 + unsigned int mask = RT5190A_RID_BITMASK(rid), val;
275 +
276 + switch (rid) {
277 + case RT5190A_IDX_BUCK1:
278 + case RT5190A_IDX_BUCK4:
279 + case RT5190A_IDX_LDO:
280 + init_data->constraints.apply_uV = 0;
281 +
282 + if (init_data->constraints.min_uV ==
283 + init_data->constraints.max_uV)
284 + desc->fixed_uV = init_data->constraints.min_uV;
285 + else {
286 + dev_err(priv->dev,
287 + "Variable voltage for fixed regulator\n");
288 + return -EINVAL;
289 + }
290 + break;
291 + default:
292 + break;
293 + }
294 +
295 + latchup_enable = of_property_read_bool(np, "richtek,latchup-enable");
296 +
297 + /* latchup: 0, default hiccup: 1 */
298 + val = !latchup_enable ? mask : 0;
299 +
300 + return regmap_update_bits(priv->regmap, RT5190A_REG_PROTMODE, mask, val);
301 +}
302 +
303 +static void rt5190a_fillin_regulator_desc(struct regulator_desc *desc, int rid)
304 +{
305 + static const char * const regu_name[] = { "buck1", "buck2",
306 + "buck3", "buck4",
307 + "ldo" };
308 + static const char * const supply[] = { NULL, "vin2", "vin3", "vin4",
309 + "vinldo" };
310 +
311 + desc->name = regu_name[rid];
312 + desc->supply_name = supply[rid];
313 + desc->owner = THIS_MODULE;
314 + desc->type = REGULATOR_VOLTAGE;
315 + desc->id = rid;
316 + desc->enable_reg = RT5190A_REG_ENABLE;
317 + desc->enable_mask = RT5190A_RID_BITMASK(rid);
318 + desc->active_discharge_reg = RT5190A_REG_DISCHARGE;
319 + desc->active_discharge_mask = RT5190A_RID_BITMASK(rid);
320 + desc->active_discharge_on = RT5190A_RID_BITMASK(rid);
321 +
322 + switch (rid) {
323 + case RT5190A_IDX_BUCK1:
324 + desc->active_discharge_mask = RT5190A_BUCK1_DISCHG_MASK;
325 + desc->active_discharge_on = RT5190A_BUCK1_DISCHG_ONVAL;
326 + desc->n_voltages = 1;
327 + desc->ops = &rt5190a_fixed_buck_ops;
328 + desc->of_map_mode = rt5190a_of_map_mode;
329 + break;
330 + case RT5190A_IDX_BUCK2:
331 + desc->vsel_reg = RT5190A_REG_BUCK2VSEL;
332 + desc->vsel_mask = RT5190A_VSEL_MASK;
333 + desc->min_uV = RT5190A_BUCK23_MINUV;
334 + desc->uV_step = RT5190A_BUCK23_STEPUV;
335 + desc->n_voltages = RT5190A_BUCK23_STEPNUM;
336 + desc->ops = &rt5190a_ranged_buck_ops;
337 + break;
338 + case RT5190A_IDX_BUCK3:
339 + desc->vsel_reg = RT5190A_REG_BUCK3VSEL;
340 + desc->vsel_mask = RT5190A_VSEL_MASK;
341 + desc->min_uV = RT5190A_BUCK23_MINUV;
342 + desc->uV_step = RT5190A_BUCK23_STEPUV;
343 + desc->n_voltages = RT5190A_BUCK23_STEPNUM;
344 + desc->ops = &rt5190a_ranged_buck_ops;
345 + break;
346 + case RT5190A_IDX_BUCK4:
347 + desc->n_voltages = 1;
348 + desc->ops = &rt5190a_fixed_buck_ops;
349 + desc->of_map_mode = rt5190a_of_map_mode;
350 + break;
351 + case RT5190A_IDX_LDO:
352 + desc->n_voltages = 1;
353 + desc->ops = &rt5190a_fixed_ldo_ops;
354 + break;
355 + }
356 +}
357 +
358 +static struct of_regulator_match rt5190a_regulator_match[] = {
359 + { .name = "buck1", },
360 + { .name = "buck2", },
361 + { .name = "buck3", },
362 + { .name = "buck4", },
363 + { .name = "ldo", }
364 +};
365 +
366 +static int rt5190a_parse_regulator_dt_data(struct rt5190a_priv *priv)
367 +{
368 + struct device_node *regulator_np;
369 + struct regulator_desc *reg_desc;
370 + struct of_regulator_match *match;
371 + int i, ret;
372 +
373 + for (i = 0; i < RT5190A_MAX_IDX; i++) {
374 + reg_desc = priv->rdesc + i;
375 + match = rt5190a_regulator_match + i;
376 +
377 + rt5190a_fillin_regulator_desc(reg_desc, i);
378 +
379 + match->desc = reg_desc;
380 + }
381 +
382 + regulator_np = of_get_child_by_name(priv->dev->of_node, "regulators");
383 + if (!regulator_np) {
384 + dev_err(priv->dev, "Could not find 'regulators' node\n");
385 + return -ENODEV;
386 + }
387 +
388 + ret = of_regulator_match(priv->dev, regulator_np,
389 + rt5190a_regulator_match,
390 + ARRAY_SIZE(rt5190a_regulator_match));
391 +
392 + of_node_put(regulator_np);
393 +
394 + if (ret < 0) {
395 + dev_err(priv->dev,
396 + "Error parsing regulator init data: %d\n", ret);
397 + return ret;
398 + }
399 +
400 + for (i = 0; i < RT5190A_MAX_IDX; i++) {
401 + match = rt5190a_regulator_match + i;
402 +
403 + ret = rt5190a_of_parse_cb(priv, i, match);
404 + if (ret) {
405 + dev_err(priv->dev, "Failed in [%d] of_parse_cb\n", i);
406 + return ret;
407 + }
408 + }
409 +
410 + return 0;
411 +}
412 +
413 +static const struct reg_sequence rt5190a_init_patch[] = {
414 + { 0x09, 0x3d, },
415 + { 0x0a, 0x3e, },
416 + { 0x0b, 0x01, },
417 + { 0x10, 0xff, },
418 + { 0x11, 0xff, },
419 + { 0x12, 0xff, },
420 + { 0x13, 0xff, },
421 + { 0x14, 0, },
422 + { 0x15, 0, },
423 + { 0x16, 0x3e, },
424 + { 0x17, 0, }
425 +};
426 +
427 +static int rt5190a_device_initialize(struct rt5190a_priv *priv)
428 +{
429 + bool mute_enable;
430 + int ret;
431 +
432 + ret = regmap_register_patch(priv->regmap, rt5190a_init_patch,
433 + ARRAY_SIZE(rt5190a_init_patch));
434 + if (ret) {
435 + dev_err(priv->dev, "Failed to do register patch\n");
436 + return ret;
437 + }
438 +
439 + mute_enable = device_property_read_bool(priv->dev,
440 + "richtek,mute-enable");
441 +
442 + if (mute_enable) {
443 + ret = regmap_write(priv->regmap, RT5190A_REG_MUTECNTL, 0x00);
444 + if (ret) {
445 + dev_err(priv->dev, "Failed to enable mute function\n");
446 + return ret;
447 + }
448 + }
449 +
450 + return 0;
451 +}
452 +
453 +static int rt5190a_device_check(struct rt5190a_priv *priv)
454 +{
455 + u16 devid;
456 + int ret;
457 +
458 + ret = regmap_raw_read(priv->regmap, RT5190A_REG_MANUFACTURE, &devid,
459 + sizeof(devid));
460 + if (ret)
461 + return ret;
462 +
463 + if (devid) {
464 + dev_err(priv->dev, "Incorrect device id 0x%04x\n", devid);
465 + return -ENODEV;
466 + }
467 +
468 + return 0;
469 +}
470 +
471 +static const struct regmap_config rt5190a_regmap_config = {
472 + .reg_bits = 8,
473 + .val_bits = 8,
474 + .max_register = RT5190A_REG_HOTDIEMASK,
475 +};
476 +
477 +static int rt5190a_probe(struct i2c_client *i2c)
478 +{
479 + struct rt5190a_priv *priv;
480 + struct regulator_config cfg = {};
481 + int i, ret;
482 +
483 + priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
484 + if (!priv)
485 + return -ENOMEM;
486 +
487 + priv->dev = &i2c->dev;
488 +
489 + priv->regmap = devm_regmap_init_i2c(i2c, &rt5190a_regmap_config);
490 + if (IS_ERR(priv->regmap)) {
491 + dev_err(&i2c->dev, "Failed to allocate regmap\n");
492 + return PTR_ERR(priv->regmap);
493 + }
494 +
495 + ret = rt5190a_device_check(priv);
496 + if (ret) {
497 + dev_err(&i2c->dev, "Failed to check device %d\n", ret);
498 + return ret;
499 + }
500 +
501 + ret = rt5190a_device_initialize(priv);
502 + if (ret) {
503 + dev_err(&i2c->dev, "Failed to initialize the device\n");
504 + return ret;
505 + }
506 +
507 + ret = rt5190a_parse_regulator_dt_data(priv);
508 + if (ret) {
509 + dev_err(&i2c->dev, "Failed to parse regulator dt\n");
510 + return ret;
511 + }
512 +
513 + cfg.dev = &i2c->dev;
514 + cfg.regmap = priv->regmap;
515 +
516 + for (i = 0; i < RT5190A_MAX_IDX; i++) {
517 + struct regulator_desc *desc = priv->rdesc + i;
518 + struct of_regulator_match *match = rt5190a_regulator_match + i;
519 +
520 + cfg.init_data = match->init_data;
521 + cfg.of_node = match->of_node;
522 +
523 + priv->rdev[i] = devm_regulator_register(&i2c->dev, desc, &cfg);
524 + if (IS_ERR(priv->rdev[i])) {
525 + dev_err(&i2c->dev, "Failed to register regulator %s\n",
526 + desc->name);
527 + return PTR_ERR(priv->rdev[i]);
528 + }
529 + }
530 +
531 + if (i2c->irq) {
532 + ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
533 + rt5190a_irq_handler,
534 + IRQF_ONESHOT,
535 + dev_name(&i2c->dev), priv);
536 + if (ret) {
537 + dev_err(&i2c->dev, "Failed to register interrupt\n");
538 + return ret;
539 + }
540 + }
541 +
542 + return 0;
543 +}
544 +
545 +static const struct of_device_id __maybe_unused rt5190a_device_table[] = {
546 + { .compatible = "richtek,rt5190a", },
547 + {}
548 +};
549 +MODULE_DEVICE_TABLE(of, rt5190a_device_table);
550 +
551 +static struct i2c_driver rt5190a_driver = {
552 + .driver = {
553 + .name = "rt5190a",
554 + .of_match_table = rt5190a_device_table,
555 + },
556 + .probe_new = rt5190a_probe,
557 +};
558 +module_i2c_driver(rt5190a_driver);
559 +
560 +MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
561 +MODULE_DESCRIPTION("Richtek RT5190A Regulator Driver");
562 +MODULE_LICENSE("GPL v2");