1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2022 Richtek Technology Corp.
5 * Author: ChiYuan Huang <cy_huang@richtek.com>
9 #include <linux/bits.h>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/of_regulator.h>
23 #define RT6190_REG_VID 0x00
24 #define RT6190_REG_OUTV 0x01
25 #define RT6190_REG_OUTC 0x03
26 #define RT6190_REG_SET1 0x0D
27 #define RT6190_REG_SET2 0x0E
28 #define RT6190_REG_SET4 0x10
29 #define RT6190_REG_RATIO 0x11
30 #define RT6190_REG_OUT_VOLT_L 0x12
31 #define RT6190_REG_TEMP_H 0x1B
32 #define RT6190_REG_STAT1 0x1C
33 #define RT6190_REG_ALERT1 0x1E
34 #define RT6190_REG_ALERT2 0x1F
35 #define RT6190_REG_MASK2 0x21
36 #define RT6190_REG_OCPEN 0x28
37 #define RT6190_REG_SET5 0x29
38 #define RT6190_REG_VBUSC_ADC 0x32
39 #define RT6190_REG_BUSC_VOLT_L 0x33
40 #define RT6190_REG_BUSC_VOLT_H 0x34
41 #define RT6190_REG_STAT3 0x37
42 #define RT6190_REG_ALERT3 0x38
43 #define RT6190_REG_MASK3 0x39
45 #define RT6190_ENPWM_MASK BIT(7)
46 #define RT6190_ENDCHG_MASK BIT(4)
47 #define RT6190_ALERT_OTPEVT BIT(6)
48 #define RT6190_ALERT_UVPEVT BIT(5)
49 #define RT6190_ALERT_OVPEVT BIT(4)
50 #define RT6190_ENGCP_MASK BIT(1)
51 #define RT6190_FCCM_MASK BIT(7)
53 #define RICHTEK_VID 0x82
54 #define RT6190_OUT_MIN_UV 3000000
55 #define RT6190_OUT_MAX_UV 32000000
56 #define RT6190_OUT_STEP_UV 20000
57 #define RT6190_OUT_N_VOLT (RT6190_OUT_MAX_UV / RT6190_OUT_STEP_UV + 1)
58 #define RT6190_OUTV_MINSEL 150
59 #define RT6190_OUT_MIN_UA 306000
60 #define RT6190_OUT_MAX_UA 12114000
61 #define RT6190_OUT_STEP_UA 24000
62 #define RT6190_OUTC_MINSEL 19
63 #define RT6190_EN_TIME_US 500
65 #define RT6190_PSM_MODE 0
66 #define RT6190_FCCM_MODE 1
70 struct regmap *regmap;
71 struct gpio_desc *enable_gpio;
72 unsigned int cached_alert_evt;
75 static int rt6190_out_set_voltage_sel(struct regulator_dev *rdev,
76 unsigned int selector)
78 struct regmap *regmap = rdev_get_regmap(rdev);
79 __le16 le_sel = cpu_to_le16(selector);
81 return regmap_raw_write(regmap, RT6190_REG_OUTV, &le_sel,
85 static int rt6190_out_get_voltage_sel(struct regulator_dev *rdev)
87 struct regmap *regmap = rdev_get_regmap(rdev);
91 ret = regmap_raw_read(regmap, RT6190_REG_OUTV, &le_sel, sizeof(le_sel));
93 return ret ?: le16_to_cpu(le_sel);
96 static int rt6190_out_enable(struct regulator_dev *rdev)
98 struct rt6190_data *data = rdev_get_drvdata(rdev);
99 struct regmap *regmap = rdev_get_regmap(rdev);
103 pm_runtime_get_sync(data->dev);
106 * From off to on, vout config will restore to IC default.
107 * Read vout configs before enable, and restore them after enable
109 ret = regmap_raw_read(regmap, RT6190_REG_OUTV, out_cfg,
114 ret = regulator_enable_regmap(rdev);
118 ret = regmap_raw_write(regmap, RT6190_REG_OUTV, out_cfg,
123 return regmap_update_bits(regmap, RT6190_REG_SET5, RT6190_ENGCP_MASK,
127 static int rt6190_out_disable(struct regulator_dev *rdev)
129 struct rt6190_data *data = rdev_get_drvdata(rdev);
130 struct regmap *regmap = rdev_get_regmap(rdev);
133 ret = regmap_update_bits(regmap, RT6190_REG_SET5, RT6190_ENGCP_MASK, 0);
137 ret = regulator_disable_regmap(rdev);
141 /* cleared cached alert event */
142 data->cached_alert_evt = 0;
144 pm_runtime_put(data->dev);
149 static int rt6190_out_set_current_limit(struct regulator_dev *rdev, int min_uA,
152 struct regmap *regmap = rdev_get_regmap(rdev);
156 if (min_uA < RT6190_OUT_MIN_UA || max_uA > RT6190_OUT_MAX_UA)
159 csel = DIV_ROUND_UP(min_uA - RT6190_OUT_MIN_UA, RT6190_OUT_STEP_UA);
161 clim = RT6190_OUT_MIN_UA + RT6190_OUT_STEP_UA * csel;
165 csel += RT6190_OUTC_MINSEL;
166 le_csel = cpu_to_le16(csel);
168 return regmap_raw_write(regmap, RT6190_REG_OUTC, &le_csel,
172 static int rt6190_out_get_current_limit(struct regulator_dev *rdev)
174 struct regmap *regmap = rdev_get_regmap(rdev);
178 ret = regmap_raw_read(regmap, RT6190_REG_OUTC, &le_csel,
183 csel = le16_to_cpu(le_csel);
184 csel -= RT6190_OUTC_MINSEL;
186 return RT6190_OUT_MIN_UA + RT6190_OUT_STEP_UA * csel;
189 static int rt6190_out_set_mode(struct regulator_dev *rdev, unsigned int mode)
191 struct regmap *regmap = rdev_get_regmap(rdev);
195 case REGULATOR_MODE_FAST:
196 val = RT6190_FCCM_MASK;
198 case REGULATOR_MODE_NORMAL:
205 return regmap_update_bits(regmap, RT6190_REG_SET1, RT6190_FCCM_MASK,
209 static unsigned int rt6190_out_get_mode(struct regulator_dev *rdev)
211 struct regmap *regmap = rdev_get_regmap(rdev);
215 ret = regmap_read(regmap, RT6190_REG_SET1, &config);
217 return REGULATOR_MODE_INVALID;
219 if (config & RT6190_FCCM_MASK)
220 return REGULATOR_MODE_FAST;
222 return REGULATOR_MODE_NORMAL;
225 static int rt6190_out_get_error_flags(struct regulator_dev *rdev,
228 struct rt6190_data *data = rdev_get_drvdata(rdev);
229 unsigned int state, rpt_flags = 0;
232 ret = regmap_read(data->regmap, RT6190_REG_STAT1, &state);
236 state |= data->cached_alert_evt;
238 if (state & RT6190_ALERT_OTPEVT)
239 rpt_flags |= REGULATOR_ERROR_OVER_TEMP;
241 if (state & RT6190_ALERT_UVPEVT)
242 rpt_flags |= REGULATOR_ERROR_UNDER_VOLTAGE;
244 if (state & RT6190_ALERT_OVPEVT)
245 rpt_flags |= REGULATOR_ERROR_REGULATION_OUT;
252 static unsigned int rt6190_out_of_map_mode(unsigned int mode)
255 case RT6190_PSM_MODE:
256 return REGULATOR_MODE_NORMAL;
257 case RT6190_FCCM_MODE:
258 return REGULATOR_MODE_FAST;
260 return REGULATOR_MODE_INVALID;
264 static const struct regulator_ops rt6190_regulator_ops = {
265 .list_voltage = regulator_list_voltage_linear,
266 .set_voltage_sel = rt6190_out_set_voltage_sel,
267 .get_voltage_sel = rt6190_out_get_voltage_sel,
268 .enable = rt6190_out_enable,
269 .disable = rt6190_out_disable,
270 .is_enabled = regulator_is_enabled_regmap,
271 .set_current_limit = rt6190_out_set_current_limit,
272 .get_current_limit = rt6190_out_get_current_limit,
273 .set_active_discharge = regulator_set_active_discharge_regmap,
274 .set_mode = rt6190_out_set_mode,
275 .get_mode = rt6190_out_get_mode,
276 .get_error_flags = rt6190_out_get_error_flags,
279 static const struct regulator_desc rt6190_regulator_desc = {
280 .name = "rt6190-regulator",
281 .type = REGULATOR_VOLTAGE,
282 .owner = THIS_MODULE,
283 .ops = &rt6190_regulator_ops,
284 .min_uV = RT6190_OUT_MIN_UV,
285 .uV_step = RT6190_OUT_STEP_UV,
286 .n_voltages = RT6190_OUT_N_VOLT,
287 .linear_min_sel = RT6190_OUTV_MINSEL,
288 .enable_reg = RT6190_REG_SET2,
289 .enable_mask = RT6190_ENPWM_MASK,
290 .active_discharge_reg = RT6190_REG_SET2,
291 .active_discharge_mask = RT6190_ENDCHG_MASK,
292 .active_discharge_on = RT6190_ENDCHG_MASK,
293 .of_map_mode = rt6190_out_of_map_mode,
296 static bool rt6190_is_volatile_reg(struct device *dev, unsigned int reg)
299 case RT6190_REG_OUT_VOLT_L ... RT6190_REG_ALERT2:
300 case RT6190_REG_BUSC_VOLT_L ... RT6190_REG_BUSC_VOLT_H:
301 case RT6190_REG_STAT3 ... RT6190_REG_ALERT3:
308 static const struct regmap_config rt6190_regmap_config = {
310 .cache_type = REGCACHE_FLAT,
313 .max_register = RT6190_REG_MASK3,
314 .num_reg_defaults_raw = RT6190_REG_MASK3 + 1,
315 .volatile_reg = rt6190_is_volatile_reg,
318 static irqreturn_t rt6190_irq_handler(int irq, void *devid)
320 struct regulator_dev *rdev = devid;
321 struct rt6190_data *data = rdev_get_drvdata(rdev);
325 ret = regmap_read(data->regmap, RT6190_REG_ALERT1, &alert);
329 /* Write clear alert events */
330 ret = regmap_write(data->regmap, RT6190_REG_ALERT1, alert);
334 data->cached_alert_evt |= alert;
336 if (alert & RT6190_ALERT_OTPEVT)
337 regulator_notifier_call_chain(rdev, REGULATOR_EVENT_OVER_TEMP, NULL);
339 if (alert & RT6190_ALERT_UVPEVT)
340 regulator_notifier_call_chain(rdev, REGULATOR_EVENT_UNDER_VOLTAGE, NULL);
342 if (alert & RT6190_ALERT_OVPEVT)
343 regulator_notifier_call_chain(rdev, REGULATOR_EVENT_REGULATION_OUT, NULL);
348 static int rt6190_init_registers(struct regmap *regmap)
353 ret = regmap_write(regmap, RT6190_REG_SET4, 0x82);
357 /* Config default VOUT ratio to be higher */
358 ret = regmap_write(regmap, RT6190_REG_RATIO, 0x20);
360 /* Mask unused alert */
361 ret = regmap_write(regmap, RT6190_REG_MASK2, 0);
366 ret = regmap_write(regmap, RT6190_REG_OCPEN, 0);
370 /* Enable VBUSC ADC */
371 return regmap_write(regmap, RT6190_REG_VBUSC_ADC, 0x02);
374 static int rt6190_probe(struct i2c_client *i2c)
376 struct device *dev = &i2c->dev;
377 struct rt6190_data *data;
378 struct gpio_desc *enable_gpio;
379 struct regmap *regmap;
380 struct regulator_dev *rdev;
381 struct regulator_config cfg = {};
385 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
389 enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH);
390 if (IS_ERR(enable_gpio))
391 return dev_err_probe(dev, PTR_ERR(enable_gpio), "Failed to get 'enable' gpio\n");
392 else if (enable_gpio)
393 usleep_range(RT6190_EN_TIME_US, RT6190_EN_TIME_US * 2);
395 regmap = devm_regmap_init_i2c(i2c, &rt6190_regmap_config);
397 return dev_err_probe(dev, PTR_ERR(regmap), "Failed to init regmap\n");
400 data->enable_gpio = enable_gpio;
401 data->regmap = regmap;
402 i2c_set_clientdata(i2c, data);
404 ret = regmap_read(regmap, RT6190_REG_VID, &vid);
406 return dev_err_probe(dev, ret, "Failed to read VID\n");
408 if (vid != RICHTEK_VID)
409 return dev_err_probe(dev, -ENODEV, "Incorrect VID 0x%02x\n", vid);
411 ret = rt6190_init_registers(regmap);
413 return dev_err_probe(dev, ret, "Failed to init registers\n");
415 pm_runtime_set_active(dev);
416 ret = devm_pm_runtime_enable(dev);
418 return dev_err_probe(dev, ret, "Failed to set pm_runtime enable\n");
421 cfg.of_node = dev->of_node;
422 cfg.driver_data = data;
423 cfg.init_data = of_get_regulator_init_data(dev, dev->of_node,
424 &rt6190_regulator_desc);
426 rdev = devm_regulator_register(dev, &rt6190_regulator_desc, &cfg);
428 return dev_err_probe(dev, PTR_ERR(rdev), "Failed to register regulator\n");
431 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
433 IRQF_ONESHOT, dev_name(dev),
436 return dev_err_probe(dev, ret, "Failed to register interrupt\n");
442 static int rt6190_runtime_suspend(struct device *dev)
444 struct rt6190_data *data = dev_get_drvdata(dev);
445 struct regmap *regmap = data->regmap;
447 if (!data->enable_gpio)
450 regcache_cache_only(regmap, true);
451 regcache_mark_dirty(regmap);
453 gpiod_set_value(data->enable_gpio, 0);
458 static int rt6190_runtime_resume(struct device *dev)
460 struct rt6190_data *data = dev_get_drvdata(dev);
461 struct regmap *regmap = data->regmap;
463 if (!data->enable_gpio)
466 gpiod_set_value(data->enable_gpio, 1);
467 usleep_range(RT6190_EN_TIME_US, RT6190_EN_TIME_US * 2);
469 regcache_cache_only(regmap, false);
470 return regcache_sync(regmap);
473 static const struct dev_pm_ops __maybe_unused rt6190_dev_pm = {
474 RUNTIME_PM_OPS(rt6190_runtime_suspend, rt6190_runtime_resume, NULL)
477 static const struct of_device_id rt6190_of_dev_table[] = {
478 { .compatible = "richtek,rt6190" },
481 MODULE_DEVICE_TABLE(of, rt6190_of_dev_table);
483 static struct i2c_driver rt6190_driver = {
486 .of_match_table = rt6190_of_dev_table,
487 .pm = pm_ptr(&rt6190_dev_pm),
489 .probe_new = rt6190_probe,
491 module_i2c_driver(rt6190_driver);
493 MODULE_DESCRIPTION("Richtek RT6190 regulator driver");
494 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
495 MODULE_LICENSE("GPL");