1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/delay.h>
4 #include <linux/gpio/consumer.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/property.h>
9 #include <linux/regmap.h>
10 #include <linux/regulator/driver.h>
11 #include <linux/regulator/of_regulator.h>
13 #define RT6160_MODE_AUTO 0
14 #define RT6160_MODE_FPWM 1
16 #define RT6160_REG_CNTL 0x01
17 #define RT6160_REG_STATUS 0x02
18 #define RT6160_REG_DEVID 0x03
19 #define RT6160_REG_VSELL 0x04
20 #define RT6160_REG_VSELH 0x05
21 #define RT6160_NUM_REGS (RT6160_REG_VSELH + 1)
23 #define RT6160_FPWM_MASK BIT(3)
24 #define RT6160_RAMPRATE_MASK GENMASK(1, 0)
25 #define RT6160_VID_MASK GENMASK(7, 4)
26 #define RT6160_VSEL_MASK GENMASK(6, 0)
27 #define RT6160_HDSTAT_MASK BIT(4)
28 #define RT6160_UVSTAT_MASK BIT(3)
29 #define RT6160_OCSTAT_MASK BIT(2)
30 #define RT6160_TSDSTAT_MASK BIT(1)
31 #define RT6160_PGSTAT_MASK BIT(0)
33 #define RT6160_VENDOR_ID 0xA0
34 #define RT6160_VOUT_MINUV 2025000
35 #define RT6160_VOUT_MAXUV 5200000
36 #define RT6160_VOUT_STPUV 25000
37 #define RT6160_N_VOUTS ((RT6160_VOUT_MAXUV - RT6160_VOUT_MINUV) / RT6160_VOUT_STPUV + 1)
39 #define RT6160_I2CRDY_TIMEUS 100
42 struct regulator_desc desc;
43 struct gpio_desc *enable_gpio;
44 struct regmap *regmap;
48 static const unsigned int rt6160_ramp_tables[] = {
49 1000, 2500, 5000, 10000
52 static int rt6160_enable(struct regulator_dev *rdev)
54 struct rt6160_priv *priv = rdev_get_drvdata(rdev);
56 if (!priv->enable_gpio)
59 gpiod_set_value_cansleep(priv->enable_gpio, 1);
60 priv->enable_state = true;
62 usleep_range(RT6160_I2CRDY_TIMEUS, RT6160_I2CRDY_TIMEUS + 100);
64 regcache_cache_only(priv->regmap, false);
65 return regcache_sync(priv->regmap);
68 static int rt6160_disable(struct regulator_dev *rdev)
70 struct rt6160_priv *priv = rdev_get_drvdata(rdev);
72 if (!priv->enable_gpio)
75 /* Mark regcache as dirty and cache only before HW disabled */
76 regcache_cache_only(priv->regmap, true);
77 regcache_mark_dirty(priv->regmap);
79 priv->enable_state = false;
80 gpiod_set_value_cansleep(priv->enable_gpio, 0);
85 static int rt6160_is_enabled(struct regulator_dev *rdev)
87 struct rt6160_priv *priv = rdev_get_drvdata(rdev);
89 return priv->enable_state ? 1 : 0;
92 static int rt6160_set_mode(struct regulator_dev *rdev, unsigned int mode)
94 struct regmap *regmap = rdev_get_regmap(rdev);
95 unsigned int mode_val;
98 case REGULATOR_MODE_FAST:
99 mode_val = RT6160_FPWM_MASK;
101 case REGULATOR_MODE_NORMAL:
105 dev_err(&rdev->dev, "mode not supported\n");
109 return regmap_update_bits(regmap, RT6160_REG_CNTL, RT6160_FPWM_MASK, mode_val);
112 static unsigned int rt6160_get_mode(struct regulator_dev *rdev)
114 struct regmap *regmap = rdev_get_regmap(rdev);
118 ret = regmap_read(regmap, RT6160_REG_CNTL, &val);
122 if (val & RT6160_FPWM_MASK)
123 return REGULATOR_MODE_FAST;
125 return REGULATOR_MODE_NORMAL;
128 static int rt6160_set_suspend_voltage(struct regulator_dev *rdev, int uV)
130 struct regmap *regmap = rdev_get_regmap(rdev);
131 unsigned int suspend_vsel_reg;
134 vsel = regulator_map_voltage_linear(rdev, uV, uV);
138 if (rdev->desc->vsel_reg == RT6160_REG_VSELL)
139 suspend_vsel_reg = RT6160_REG_VSELH;
141 suspend_vsel_reg = RT6160_REG_VSELL;
143 return regmap_update_bits(regmap, suspend_vsel_reg,
144 RT6160_VSEL_MASK, vsel);
147 static int rt6160_get_error_flags(struct regulator_dev *rdev, unsigned int *flags)
149 struct regmap *regmap = rdev_get_regmap(rdev);
150 unsigned int val, events = 0;
153 ret = regmap_read(regmap, RT6160_REG_STATUS, &val);
157 if (val & (RT6160_HDSTAT_MASK | RT6160_TSDSTAT_MASK))
158 events |= REGULATOR_ERROR_OVER_TEMP;
160 if (val & RT6160_UVSTAT_MASK)
161 events |= REGULATOR_ERROR_UNDER_VOLTAGE;
163 if (val & RT6160_OCSTAT_MASK)
164 events |= REGULATOR_ERROR_OVER_CURRENT;
166 if (val & RT6160_PGSTAT_MASK)
167 events |= REGULATOR_ERROR_FAIL;
173 static const struct regulator_ops rt6160_regulator_ops = {
174 .list_voltage = regulator_list_voltage_linear,
175 .set_voltage_sel = regulator_set_voltage_sel_regmap,
176 .get_voltage_sel = regulator_get_voltage_sel_regmap,
178 .enable = rt6160_enable,
179 .disable = rt6160_disable,
180 .is_enabled = rt6160_is_enabled,
182 .set_mode = rt6160_set_mode,
183 .get_mode = rt6160_get_mode,
184 .set_suspend_voltage = rt6160_set_suspend_voltage,
185 .set_ramp_delay = regulator_set_ramp_delay_regmap,
186 .get_error_flags = rt6160_get_error_flags,
189 static unsigned int rt6160_of_map_mode(unsigned int mode)
192 case RT6160_MODE_FPWM:
193 return REGULATOR_MODE_FAST;
194 case RT6160_MODE_AUTO:
195 return REGULATOR_MODE_NORMAL;
198 return REGULATOR_MODE_INVALID;
201 static bool rt6160_is_accessible_reg(struct device *dev, unsigned int reg)
203 if (reg >= RT6160_REG_CNTL && reg <= RT6160_REG_VSELH)
208 static bool rt6160_is_volatile_reg(struct device *dev, unsigned int reg)
210 if (reg == RT6160_REG_STATUS)
215 static const struct regmap_config rt6160_regmap_config = {
218 .max_register = RT6160_REG_VSELH,
219 .num_reg_defaults_raw = RT6160_NUM_REGS,
220 .cache_type = REGCACHE_FLAT,
222 .writeable_reg = rt6160_is_accessible_reg,
223 .readable_reg = rt6160_is_accessible_reg,
224 .volatile_reg = rt6160_is_volatile_reg,
227 static int rt6160_probe(struct i2c_client *i2c)
229 struct rt6160_priv *priv;
230 struct regulator_config regulator_cfg = {};
231 struct regulator_dev *rdev;
232 bool vsel_active_low;
236 priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
241 device_property_present(&i2c->dev, "richtek,vsel-active-low");
243 priv->enable_gpio = devm_gpiod_get_optional(&i2c->dev, "enable", GPIOD_OUT_HIGH);
244 if (IS_ERR(priv->enable_gpio)) {
245 dev_err(&i2c->dev, "Failed to get 'enable' gpio\n");
246 return PTR_ERR(priv->enable_gpio);
248 priv->enable_state = true;
250 usleep_range(RT6160_I2CRDY_TIMEUS, RT6160_I2CRDY_TIMEUS + 100);
252 priv->regmap = devm_regmap_init_i2c(i2c, &rt6160_regmap_config);
253 if (IS_ERR(priv->regmap)) {
254 ret = PTR_ERR(priv->regmap);
255 dev_err(&i2c->dev, "Failed to init regmap (%d)\n", ret);
259 ret = regmap_read(priv->regmap, RT6160_REG_DEVID, &devid);
263 if ((devid & RT6160_VID_MASK) != RT6160_VENDOR_ID) {
264 dev_err(&i2c->dev, "VID not correct [0x%02x]\n", devid);
268 priv->desc.name = "rt6160-buckboost";
269 priv->desc.type = REGULATOR_VOLTAGE;
270 priv->desc.owner = THIS_MODULE;
271 priv->desc.min_uV = RT6160_VOUT_MINUV;
272 priv->desc.uV_step = RT6160_VOUT_STPUV;
274 priv->desc.vsel_reg = RT6160_REG_VSELL;
276 priv->desc.vsel_reg = RT6160_REG_VSELH;
277 priv->desc.vsel_mask = RT6160_VSEL_MASK;
278 priv->desc.n_voltages = RT6160_N_VOUTS;
279 priv->desc.ramp_reg = RT6160_REG_CNTL;
280 priv->desc.ramp_mask = RT6160_RAMPRATE_MASK;
281 priv->desc.ramp_delay_table = rt6160_ramp_tables;
282 priv->desc.n_ramp_values = ARRAY_SIZE(rt6160_ramp_tables);
283 priv->desc.of_map_mode = rt6160_of_map_mode;
284 priv->desc.ops = &rt6160_regulator_ops;
286 regulator_cfg.dev = &i2c->dev;
287 regulator_cfg.of_node = i2c->dev.of_node;
288 regulator_cfg.regmap = priv->regmap;
289 regulator_cfg.driver_data = priv;
290 regulator_cfg.init_data = of_get_regulator_init_data(&i2c->dev, i2c->dev.of_node,
293 rdev = devm_regulator_register(&i2c->dev, &priv->desc, ®ulator_cfg);
295 dev_err(&i2c->dev, "Failed to register regulator\n");
296 return PTR_ERR(rdev);
302 static const struct of_device_id __maybe_unused rt6160_of_match_table[] = {
303 { .compatible = "richtek,rt6160", },
306 MODULE_DEVICE_TABLE(of, rt6160_of_match_table);
308 static struct i2c_driver rt6160_driver = {
311 .of_match_table = rt6160_of_match_table,
313 .probe_new = rt6160_probe,
315 module_i2c_driver(rt6160_driver);
317 MODULE_DESCRIPTION("Richtek RT6160 voltage regulator driver");
318 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
319 MODULE_LICENSE("GPL v2");