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 
41 struct rt6160_priv {
42 	struct regulator_desc desc;
43 	struct gpio_desc *enable_gpio;
44 	struct regmap *regmap;
45 	bool enable_state;
46 };
47 
48 static const unsigned int rt6160_ramp_tables[] = {
49 	1000, 2500, 5000, 10000
50 };
51 
52 static 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(priv->enable_gpio, 1);
60 	priv->enable_state = true;
61 
62 	usleep_range(RT6160_I2CRDY_TIMEUS, RT6160_I2CRDY_TIMEUS + 100);
63 
64 	regcache_cache_only(priv->regmap, false);
65 	return regcache_sync(priv->regmap);
66 }
67 
68 static 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(priv->regmap, true);
77 	regcache_mark_dirty(priv->regmap);
78 
79 	priv->enable_state = false;
80 	gpiod_set_value_cansleep(priv->enable_gpio, 0);
81 
82 	return 0;
83 }
84 
85 static 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 
92 static 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(regmap, RT6160_REG_CNTL, RT6160_FPWM_MASK, mode_val);
110 }
111 
112 static 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(regmap, RT6160_REG_CNTL, &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 
128 static 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, 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(regmap, suspend_vsel_reg,
144 				  RT6160_VSEL_MASK, vsel);
145 }
146 
147 static 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(regmap, RT6160_REG_STATUS, &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 
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,
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 
189 static 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 
201 static 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 
208 static 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 
215 static 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 
227 static 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(&i2c->dev, sizeof(*priv), GFP_KERNEL);
237 	if (!priv)
238 		return -ENOMEM;
239 
240 	vsel_active_low =
241 		device_property_present(&i2c->dev, "richtek,vsel-active-low");
242 
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);
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(priv->regmap)) {
254 		ret = PTR_ERR(priv->regmap);
255 		dev_err(&i2c->dev, "Failed to init regmap (%d)\n", ret);
256 		return ret;
257 	}
258 
259 	ret = regmap_read(priv->regmap, RT6160_REG_DEVID, &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(&i2c->dev, i2c->dev.of_node,
291 							     &priv->desc);
292 
293 	rdev = devm_regulator_register(&i2c->dev, &priv->desc, &regulator_cfg);
294 	if (IS_ERR(rdev)) {
295 		dev_err(&i2c->dev, "Failed to register regulator\n");
296 		return PTR_ERR(rdev);
297 	}
298 
299 	return 0;
300 }
301 
302 static const struct of_device_id __maybe_unused rt6160_of_match_table[] = {
303 	{ .compatible = "richtek,rt6160", },
304 	{}
305 };
306 MODULE_DEVICE_TABLE(of, rt6160_of_match_table);
307 
308 static 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 };
316 module_i2c_driver(rt6160_driver);
317 
318 MODULE_DESCRIPTION("Richtek RT6160 voltage regulator driver");
319 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
320 MODULE_LICENSE("GPL v2");
321