1// SPDX-License-Identifier: GPL-2.0-only
2
3#include <linux/delay.h>
4#include <linux/gpio/consumer.h>
5#include <linux/i2c.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>
12
13#define RT6160_MODE_AUTO 0
14#define RT6160_MODE_FPWM 1
15
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)
22
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)
32
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)
38
39#define RT6160_I2CRDY_TIMEUS 100
40
41struct rt6160_priv {
42 struct regulator_desc desc;
43 struct gpio_desc *enable_gpio;
44 struct regmap *regmap;
45 bool enable_state;
46};
47
48static const unsigned int rt6160_ramp_tables[] = {
49 1000, 2500, 5000, 10000
50};
51
52static int rt6160_enable(struct regulator_dev *rdev)
53{
54 struct rt6160_priv *priv = rdev_get_drvdata(rdev);
55
56 if (!priv->enable_gpio)
57 return 0;
58
59 gpiod_set_value_cansleep(desc: priv->enable_gpio, value: 1);
60 priv->enable_state = true;
61
62 usleep_range(RT6160_I2CRDY_TIMEUS, RT6160_I2CRDY_TIMEUS + 100);
63
64 regcache_cache_only(map: priv->regmap, enable: false);
65 return regcache_sync(map: priv->regmap);
66}
67
68static int rt6160_disable(struct regulator_dev *rdev)
69{
70 struct rt6160_priv *priv = rdev_get_drvdata(rdev);
71
72 if (!priv->enable_gpio)
73 return -EINVAL;
74
75 /* Mark regcache as dirty and cache only before HW disabled */
76 regcache_cache_only(map: priv->regmap, enable: true);
77 regcache_mark_dirty(map: priv->regmap);
78
79 priv->enable_state = false;
80 gpiod_set_value_cansleep(desc: priv->enable_gpio, value: 0);
81
82 return 0;
83}
84
85static int rt6160_is_enabled(struct regulator_dev *rdev)
86{
87 struct rt6160_priv *priv = rdev_get_drvdata(rdev);
88
89 return priv->enable_state ? 1 : 0;
90}
91
92static int rt6160_set_mode(struct regulator_dev *rdev, unsigned int mode)
93{
94 struct regmap *regmap = rdev_get_regmap(rdev);
95 unsigned int mode_val;
96
97 switch (mode) {
98 case REGULATOR_MODE_FAST:
99 mode_val = RT6160_FPWM_MASK;
100 break;
101 case REGULATOR_MODE_NORMAL:
102 mode_val = 0;
103 break;
104 default:
105 dev_err(&rdev->dev, "mode not supported\n");
106 return -EINVAL;
107 }
108
109 return regmap_update_bits(map: regmap, RT6160_REG_CNTL, RT6160_FPWM_MASK, val: mode_val);
110}
111
112static unsigned int rt6160_get_mode(struct regulator_dev *rdev)
113{
114 struct regmap *regmap = rdev_get_regmap(rdev);
115 unsigned int val;
116 int ret;
117
118 ret = regmap_read(map: regmap, RT6160_REG_CNTL, val: &val);
119 if (ret)
120 return ret;
121
122 if (val & RT6160_FPWM_MASK)
123 return REGULATOR_MODE_FAST;
124
125 return REGULATOR_MODE_NORMAL;
126}
127
128static int rt6160_set_suspend_voltage(struct regulator_dev *rdev, int uV)
129{
130 struct regmap *regmap = rdev_get_regmap(rdev);
131 unsigned int suspend_vsel_reg;
132 int vsel;
133
134 vsel = regulator_map_voltage_linear(rdev, min_uV: uV, max_uV: uV);
135 if (vsel < 0)
136 return vsel;
137
138 if (rdev->desc->vsel_reg == RT6160_REG_VSELL)
139 suspend_vsel_reg = RT6160_REG_VSELH;
140 else
141 suspend_vsel_reg = RT6160_REG_VSELL;
142
143 return regmap_update_bits(map: regmap, reg: suspend_vsel_reg,
144 RT6160_VSEL_MASK, val: vsel);
145}
146
147static int rt6160_get_error_flags(struct regulator_dev *rdev, unsigned int *flags)
148{
149 struct regmap *regmap = rdev_get_regmap(rdev);
150 unsigned int val, events = 0;
151 int ret;
152
153 ret = regmap_read(map: regmap, RT6160_REG_STATUS, val: &val);
154 if (ret)
155 return ret;
156
157 if (val & (RT6160_HDSTAT_MASK | RT6160_TSDSTAT_MASK))
158 events |= REGULATOR_ERROR_OVER_TEMP;
159
160 if (val & RT6160_UVSTAT_MASK)
161 events |= REGULATOR_ERROR_UNDER_VOLTAGE;
162
163 if (val & RT6160_OCSTAT_MASK)
164 events |= REGULATOR_ERROR_OVER_CURRENT;
165
166 if (val & RT6160_PGSTAT_MASK)
167 events |= REGULATOR_ERROR_FAIL;
168
169 *flags = events;
170 return 0;
171}
172
173static 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,
177
178 .enable = rt6160_enable,
179 .disable = rt6160_disable,
180 .is_enabled = rt6160_is_enabled,
181
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,
187};
188
189static unsigned int rt6160_of_map_mode(unsigned int mode)
190{
191 switch (mode) {
192 case RT6160_MODE_FPWM:
193 return REGULATOR_MODE_FAST;
194 case RT6160_MODE_AUTO:
195 return REGULATOR_MODE_NORMAL;
196 }
197
198 return REGULATOR_MODE_INVALID;
199}
200
201static bool rt6160_is_accessible_reg(struct device *dev, unsigned int reg)
202{
203 if (reg >= RT6160_REG_CNTL && reg <= RT6160_REG_VSELH)
204 return true;
205 return false;
206}
207
208static bool rt6160_is_volatile_reg(struct device *dev, unsigned int reg)
209{
210 if (reg == RT6160_REG_STATUS)
211 return true;
212 return false;
213}
214
215static const struct regmap_config rt6160_regmap_config = {
216 .reg_bits = 8,
217 .val_bits = 8,
218 .max_register = RT6160_REG_VSELH,
219 .num_reg_defaults_raw = RT6160_NUM_REGS,
220 .cache_type = REGCACHE_FLAT,
221
222 .writeable_reg = rt6160_is_accessible_reg,
223 .readable_reg = rt6160_is_accessible_reg,
224 .volatile_reg = rt6160_is_volatile_reg,
225};
226
227static int rt6160_probe(struct i2c_client *i2c)
228{
229 struct rt6160_priv *priv;
230 struct regulator_config regulator_cfg = {};
231 struct regulator_dev *rdev;
232 bool vsel_active_low;
233 unsigned int devid;
234 int ret;
235
236 priv = devm_kzalloc(dev: &i2c->dev, size: sizeof(*priv), GFP_KERNEL);
237 if (!priv)
238 return -ENOMEM;
239
240 vsel_active_low =
241 device_property_present(dev: &i2c->dev, propname: "richtek,vsel-active-low");
242
243 priv->enable_gpio = devm_gpiod_get_optional(dev: &i2c->dev, con_id: "enable", flags: GPIOD_OUT_HIGH);
244 if (IS_ERR(ptr: priv->enable_gpio)) {
245 dev_err(&i2c->dev, "Failed to get 'enable' gpio\n");
246 return PTR_ERR(ptr: priv->enable_gpio);
247 }
248 priv->enable_state = true;
249
250 usleep_range(RT6160_I2CRDY_TIMEUS, RT6160_I2CRDY_TIMEUS + 100);
251
252 priv->regmap = devm_regmap_init_i2c(i2c, &rt6160_regmap_config);
253 if (IS_ERR(ptr: priv->regmap)) {
254 ret = PTR_ERR(ptr: priv->regmap);
255 dev_err(&i2c->dev, "Failed to init regmap (%d)\n", ret);
256 return ret;
257 }
258
259 ret = regmap_read(map: priv->regmap, RT6160_REG_DEVID, val: &devid);
260 if (ret)
261 return ret;
262
263 if ((devid & RT6160_VID_MASK) != RT6160_VENDOR_ID) {
264 dev_err(&i2c->dev, "VID not correct [0x%02x]\n", devid);
265 return -ENODEV;
266 }
267
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;
273 if (vsel_active_low)
274 priv->desc.vsel_reg = RT6160_REG_VSELL;
275 else
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;
285
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(dev: &i2c->dev, node: i2c->dev.of_node,
291 desc: &priv->desc);
292
293 rdev = devm_regulator_register(dev: &i2c->dev, regulator_desc: &priv->desc, config: &regulator_cfg);
294 if (IS_ERR(ptr: rdev)) {
295 dev_err(&i2c->dev, "Failed to register regulator\n");
296 return PTR_ERR(ptr: rdev);
297 }
298
299 return 0;
300}
301
302static const struct of_device_id __maybe_unused rt6160_of_match_table[] = {
303 { .compatible = "richtek,rt6160", },
304 {}
305};
306MODULE_DEVICE_TABLE(of, rt6160_of_match_table);
307
308static struct i2c_driver rt6160_driver = {
309 .driver = {
310 .name = "rt6160",
311 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
312 .of_match_table = rt6160_of_match_table,
313 },
314 .probe = rt6160_probe,
315};
316module_i2c_driver(rt6160_driver);
317
318MODULE_DESCRIPTION("Richtek RT6160 voltage regulator driver");
319MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
320MODULE_LICENSE("GPL v2");
321

source code of linux/drivers/regulator/rt6160-regulator.c