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
6 Add support for Richtek RT5190A PMIC.
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>
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
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.
24 +config REGULATOR_RT5190A
25 + tristate "Richtek RT5190A PMIC"
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.
34 config REGULATOR_RT6160
35 tristate "Richtek RT6160 BuckBoost voltage regulator"
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
48 +++ b/drivers/regulator/rt5190a-regulator.c
50 +// SPDX-License-Identifier: GPL-2.0+
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>
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
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)
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)
92 + RT5190A_IDX_BUCK1 = 0,
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];
107 +static int rt5190a_get_error_flags(struct regulator_dev *rdev,
108 + unsigned int *flags)
110 + struct regmap *regmap = rdev_get_regmap(rdev);
111 + int rid = rdev_get_id(rdev);
112 + unsigned int pgood_stat;
115 + ret = regmap_read(regmap, RT5190A_REG_PGSTAT, &pgood_stat);
119 + if (!(pgood_stat & RT5190A_RID_BITMASK(rid)))
120 + *flags = REGULATOR_ERROR_FAIL;
127 +static int rt5190a_fixed_buck_set_mode(struct regulator_dev *rdev,
130 + struct regmap *regmap = rdev_get_regmap(rdev);
131 + int rid = rdev_get_id(rdev);
132 + unsigned int mask = RT5190A_RID_BITMASK(rid), val;
135 + case REGULATOR_MODE_FAST:
138 + case REGULATOR_MODE_NORMAL:
145 + return regmap_update_bits(regmap, RT5190A_REG_DCDCCNTL, mask, val);
148 +static unsigned int rt5190a_fixed_buck_get_mode(struct regulator_dev *rdev)
150 + struct regmap *regmap = rdev_get_regmap(rdev);
151 + int rid = rdev_get_id(rdev);
155 + ret = regmap_read(regmap, RT5190A_REG_DCDCCNTL, &val);
157 + dev_err(&rdev->dev, "Failed to get mode [%d]\n", ret);
161 + if (val & RT5190A_RID_BITMASK(rid))
162 + return REGULATOR_MODE_FAST;
164 + return REGULATOR_MODE_NORMAL;
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,
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,
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,
196 +static irqreturn_t rt5190a_irq_handler(int irq, void *data)
198 + struct rt5190a_priv *priv = data;
200 + unsigned int events, fields;
201 + static const struct {
202 + unsigned int bitmask;
203 + unsigned int report;
205 + { RT5190A_OVERVOLT_MASK, REGULATOR_ERROR_REGULATION_OUT },
206 + { RT5190A_UNDERVOLT_MASK, REGULATOR_ERROR_UNDER_VOLTAGE }
210 + ret = regmap_raw_read(priv->regmap, RT5190A_REG_OVINT, &raws,
213 + dev_err(priv->dev, "Failed to read events\n");
217 + events = le32_to_cpu(raws);
219 + ret = regmap_raw_write(priv->regmap, RT5190A_REG_OVINT, &raws,
222 + dev_err(priv->dev, "Failed to write-clear events\n");
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;
229 + for (j = 0; j < RT5190A_MAX_IDX; j++) {
230 + if (!(fields & RT5190A_RID_BITMASK(j)))
233 + regulator_notifier_call_chain(priv->rdev[j],
234 + event_tbl[i].report,
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,
248 + /* Warning if CHIP OT occur */
249 + if (events & RT5190A_CHIPOT_MASK)
250 + dev_warn(priv->dev, "CHIP overheat\n");
252 + return IRQ_HANDLED;
255 +static unsigned int rt5190a_of_map_mode(unsigned int mode)
258 + case RT5190A_OPMODE_AUTO:
259 + return REGULATOR_MODE_NORMAL;
260 + case RT5190A_OPMODE_FPWM:
261 + return REGULATOR_MODE_FAST;
263 + return REGULATOR_MODE_INVALID;
267 +static int rt5190a_of_parse_cb(struct rt5190a_priv *priv, int rid,
268 + struct of_regulator_match *match)
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;
277 + case RT5190A_IDX_BUCK1:
278 + case RT5190A_IDX_BUCK4:
279 + case RT5190A_IDX_LDO:
280 + init_data->constraints.apply_uV = 0;
282 + if (init_data->constraints.min_uV ==
283 + init_data->constraints.max_uV)
284 + desc->fixed_uV = init_data->constraints.min_uV;
287 + "Variable voltage for fixed regulator\n");
295 + latchup_enable = of_property_read_bool(np, "richtek,latchup-enable");
297 + /* latchup: 0, default hiccup: 1 */
298 + val = !latchup_enable ? mask : 0;
300 + return regmap_update_bits(priv->regmap, RT5190A_REG_PROTMODE, mask, val);
303 +static void rt5190a_fillin_regulator_desc(struct regulator_desc *desc, int rid)
305 + static const char * const regu_name[] = { "buck1", "buck2",
308 + static const char * const supply[] = { NULL, "vin2", "vin3", "vin4",
311 + desc->name = regu_name[rid];
312 + desc->supply_name = supply[rid];
313 + desc->owner = THIS_MODULE;
314 + desc->type = REGULATOR_VOLTAGE;
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);
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;
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;
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;
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;
351 + case RT5190A_IDX_LDO:
352 + desc->n_voltages = 1;
353 + desc->ops = &rt5190a_fixed_ldo_ops;
358 +static struct of_regulator_match rt5190a_regulator_match[] = {
359 + { .name = "buck1", },
360 + { .name = "buck2", },
361 + { .name = "buck3", },
362 + { .name = "buck4", },
366 +static int rt5190a_parse_regulator_dt_data(struct rt5190a_priv *priv)
368 + struct device_node *regulator_np;
369 + struct regulator_desc *reg_desc;
370 + struct of_regulator_match *match;
373 + for (i = 0; i < RT5190A_MAX_IDX; i++) {
374 + reg_desc = priv->rdesc + i;
375 + match = rt5190a_regulator_match + i;
377 + rt5190a_fillin_regulator_desc(reg_desc, i);
379 + match->desc = reg_desc;
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");
388 + ret = of_regulator_match(priv->dev, regulator_np,
389 + rt5190a_regulator_match,
390 + ARRAY_SIZE(rt5190a_regulator_match));
392 + of_node_put(regulator_np);
396 + "Error parsing regulator init data: %d\n", ret);
400 + for (i = 0; i < RT5190A_MAX_IDX; i++) {
401 + match = rt5190a_regulator_match + i;
403 + ret = rt5190a_of_parse_cb(priv, i, match);
405 + dev_err(priv->dev, "Failed in [%d] of_parse_cb\n", i);
413 +static const struct reg_sequence rt5190a_init_patch[] = {
427 +static int rt5190a_device_initialize(struct rt5190a_priv *priv)
432 + ret = regmap_register_patch(priv->regmap, rt5190a_init_patch,
433 + ARRAY_SIZE(rt5190a_init_patch));
435 + dev_err(priv->dev, "Failed to do register patch\n");
439 + mute_enable = device_property_read_bool(priv->dev,
440 + "richtek,mute-enable");
443 + ret = regmap_write(priv->regmap, RT5190A_REG_MUTECNTL, 0x00);
445 + dev_err(priv->dev, "Failed to enable mute function\n");
453 +static int rt5190a_device_check(struct rt5190a_priv *priv)
458 + ret = regmap_raw_read(priv->regmap, RT5190A_REG_MANUFACTURE, &devid,
464 + dev_err(priv->dev, "Incorrect device id 0x%04x\n", devid);
471 +static const struct regmap_config rt5190a_regmap_config = {
474 + .max_register = RT5190A_REG_HOTDIEMASK,
477 +static int rt5190a_probe(struct i2c_client *i2c)
479 + struct rt5190a_priv *priv;
480 + struct regulator_config cfg = {};
483 + priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
487 + priv->dev = &i2c->dev;
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);
495 + ret = rt5190a_device_check(priv);
497 + dev_err(&i2c->dev, "Failed to check device %d\n", ret);
501 + ret = rt5190a_device_initialize(priv);
503 + dev_err(&i2c->dev, "Failed to initialize the device\n");
507 + ret = rt5190a_parse_regulator_dt_data(priv);
509 + dev_err(&i2c->dev, "Failed to parse regulator dt\n");
513 + cfg.dev = &i2c->dev;
514 + cfg.regmap = priv->regmap;
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;
520 + cfg.init_data = match->init_data;
521 + cfg.of_node = match->of_node;
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",
527 + return PTR_ERR(priv->rdev[i]);
532 + ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
533 + rt5190a_irq_handler,
535 + dev_name(&i2c->dev), priv);
537 + dev_err(&i2c->dev, "Failed to register interrupt\n");
545 +static const struct of_device_id __maybe_unused rt5190a_device_table[] = {
546 + { .compatible = "richtek,rt5190a", },
549 +MODULE_DEVICE_TABLE(of, rt5190a_device_table);
551 +static struct i2c_driver rt5190a_driver = {
554 + .of_match_table = rt5190a_device_table,
556 + .probe_new = rt5190a_probe,
558 +module_i2c_driver(rt5190a_driver);
560 +MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
561 +MODULE_DESCRIPTION("Richtek RT5190A Regulator Driver");
562 +MODULE_LICENSE("GPL v2");