1*de20b747SChiYuan Huang // SPDX-License-Identifier: GPL-2.0-only
2*de20b747SChiYuan Huang 
3*de20b747SChiYuan Huang #include <linux/delay.h>
4*de20b747SChiYuan Huang #include <linux/gpio/consumer.h>
5*de20b747SChiYuan Huang #include <linux/i2c.h>
6*de20b747SChiYuan Huang #include <linux/kernel.h>
7*de20b747SChiYuan Huang #include <linux/module.h>
8*de20b747SChiYuan Huang #include <linux/property.h>
9*de20b747SChiYuan Huang #include <linux/regmap.h>
10*de20b747SChiYuan Huang #include <linux/regulator/driver.h>
11*de20b747SChiYuan Huang #include <linux/regulator/of_regulator.h>
12*de20b747SChiYuan Huang 
13*de20b747SChiYuan Huang #define RT6160_MODE_AUTO	0
14*de20b747SChiYuan Huang #define RT6160_MODE_FPWM	1
15*de20b747SChiYuan Huang 
16*de20b747SChiYuan Huang #define RT6160_REG_CNTL		0x01
17*de20b747SChiYuan Huang #define RT6160_REG_STATUS	0x02
18*de20b747SChiYuan Huang #define RT6160_REG_DEVID	0x03
19*de20b747SChiYuan Huang #define RT6160_REG_VSELL	0x04
20*de20b747SChiYuan Huang #define RT6160_REG_VSELH	0x05
21*de20b747SChiYuan Huang #define RT6160_NUM_REGS		(RT6160_REG_VSELH + 1)
22*de20b747SChiYuan Huang 
23*de20b747SChiYuan Huang #define RT6160_FPWM_MASK	BIT(3)
24*de20b747SChiYuan Huang #define RT6160_RAMPRATE_MASK	GENMASK(1, 0)
25*de20b747SChiYuan Huang #define RT6160_VID_MASK		GENMASK(7, 4)
26*de20b747SChiYuan Huang #define RT6160_VSEL_MASK	GENMASK(6, 0)
27*de20b747SChiYuan Huang #define RT6160_HDSTAT_MASK	BIT(4)
28*de20b747SChiYuan Huang #define RT6160_UVSTAT_MASK	BIT(3)
29*de20b747SChiYuan Huang #define RT6160_OCSTAT_MASK	BIT(2)
30*de20b747SChiYuan Huang #define RT6160_TSDSTAT_MASK	BIT(1)
31*de20b747SChiYuan Huang #define RT6160_PGSTAT_MASK	BIT(0)
32*de20b747SChiYuan Huang 
33*de20b747SChiYuan Huang #define RT6160_VENDOR_ID	0xA0
34*de20b747SChiYuan Huang #define RT6160_VOUT_MINUV	2025000
35*de20b747SChiYuan Huang #define RT6160_VOUT_MAXUV	5200000
36*de20b747SChiYuan Huang #define RT6160_VOUT_STPUV	25000
37*de20b747SChiYuan Huang #define RT6160_N_VOUTS		((RT6160_VOUT_MAXUV - RT6160_VOUT_MINUV) / RT6160_VOUT_STPUV + 1)
38*de20b747SChiYuan Huang 
39*de20b747SChiYuan Huang #define RT6160_I2CRDY_TIMEUS	100
40*de20b747SChiYuan Huang 
41*de20b747SChiYuan Huang struct rt6160_priv {
42*de20b747SChiYuan Huang 	struct regulator_desc desc;
43*de20b747SChiYuan Huang 	struct gpio_desc *enable_gpio;
44*de20b747SChiYuan Huang 	struct regmap *regmap;
45*de20b747SChiYuan Huang 	bool vsel_active_low;
46*de20b747SChiYuan Huang 	bool enable_state;
47*de20b747SChiYuan Huang };
48*de20b747SChiYuan Huang 
49*de20b747SChiYuan Huang static int rt6160_enable(struct regulator_dev *rdev)
50*de20b747SChiYuan Huang {
51*de20b747SChiYuan Huang 	struct rt6160_priv *priv = rdev_get_drvdata(rdev);
52*de20b747SChiYuan Huang 
53*de20b747SChiYuan Huang 	if (!priv->enable_gpio)
54*de20b747SChiYuan Huang 		return 0;
55*de20b747SChiYuan Huang 
56*de20b747SChiYuan Huang 	gpiod_set_value_cansleep(priv->enable_gpio, 1);
57*de20b747SChiYuan Huang 	priv->enable_state = true;
58*de20b747SChiYuan Huang 
59*de20b747SChiYuan Huang 	usleep_range(RT6160_I2CRDY_TIMEUS, RT6160_I2CRDY_TIMEUS + 100);
60*de20b747SChiYuan Huang 
61*de20b747SChiYuan Huang 	regcache_cache_only(priv->regmap, false);
62*de20b747SChiYuan Huang 	return regcache_sync(priv->regmap);
63*de20b747SChiYuan Huang }
64*de20b747SChiYuan Huang 
65*de20b747SChiYuan Huang static int rt6160_disable(struct regulator_dev *rdev)
66*de20b747SChiYuan Huang {
67*de20b747SChiYuan Huang 	struct rt6160_priv *priv = rdev_get_drvdata(rdev);
68*de20b747SChiYuan Huang 
69*de20b747SChiYuan Huang 	if (!priv->enable_gpio)
70*de20b747SChiYuan Huang 		return -EINVAL;
71*de20b747SChiYuan Huang 
72*de20b747SChiYuan Huang 	/* Mark regcache as dirty and cache only before HW disabled */
73*de20b747SChiYuan Huang 	regcache_cache_only(priv->regmap, true);
74*de20b747SChiYuan Huang 	regcache_mark_dirty(priv->regmap);
75*de20b747SChiYuan Huang 
76*de20b747SChiYuan Huang 	priv->enable_state = false;
77*de20b747SChiYuan Huang 	gpiod_set_value_cansleep(priv->enable_gpio, 0);
78*de20b747SChiYuan Huang 
79*de20b747SChiYuan Huang 	return 0;
80*de20b747SChiYuan Huang 
81*de20b747SChiYuan Huang }
82*de20b747SChiYuan Huang 
83*de20b747SChiYuan Huang static int rt6160_is_enabled(struct regulator_dev *rdev)
84*de20b747SChiYuan Huang {
85*de20b747SChiYuan Huang 	struct rt6160_priv *priv = rdev_get_drvdata(rdev);
86*de20b747SChiYuan Huang 
87*de20b747SChiYuan Huang 	return priv->enable_state ? 1 : 0;
88*de20b747SChiYuan Huang }
89*de20b747SChiYuan Huang 
90*de20b747SChiYuan Huang static int rt6160_set_mode(struct regulator_dev *rdev, unsigned int mode)
91*de20b747SChiYuan Huang {
92*de20b747SChiYuan Huang 	struct regmap *regmap = rdev_get_regmap(rdev);
93*de20b747SChiYuan Huang 	unsigned int mode_val;
94*de20b747SChiYuan Huang 
95*de20b747SChiYuan Huang 	switch (mode) {
96*de20b747SChiYuan Huang 	case REGULATOR_MODE_FAST:
97*de20b747SChiYuan Huang 		mode_val = RT6160_FPWM_MASK;
98*de20b747SChiYuan Huang 		break;
99*de20b747SChiYuan Huang 	case REGULATOR_MODE_NORMAL:
100*de20b747SChiYuan Huang 		mode_val = 0;
101*de20b747SChiYuan Huang 		break;
102*de20b747SChiYuan Huang 	default:
103*de20b747SChiYuan Huang 		dev_err(&rdev->dev, "mode not supported\n");
104*de20b747SChiYuan Huang 		return -EINVAL;
105*de20b747SChiYuan Huang 	}
106*de20b747SChiYuan Huang 
107*de20b747SChiYuan Huang 	return regmap_update_bits(regmap, RT6160_REG_CNTL, RT6160_FPWM_MASK, mode_val);
108*de20b747SChiYuan Huang }
109*de20b747SChiYuan Huang 
110*de20b747SChiYuan Huang static unsigned int rt6160_get_mode(struct regulator_dev *rdev)
111*de20b747SChiYuan Huang {
112*de20b747SChiYuan Huang 	struct regmap *regmap = rdev_get_regmap(rdev);
113*de20b747SChiYuan Huang 	unsigned int val;
114*de20b747SChiYuan Huang 	int ret;
115*de20b747SChiYuan Huang 
116*de20b747SChiYuan Huang 	ret = regmap_read(regmap, RT6160_REG_CNTL, &val);
117*de20b747SChiYuan Huang 	if (ret)
118*de20b747SChiYuan Huang 		return ret;
119*de20b747SChiYuan Huang 
120*de20b747SChiYuan Huang 	if (val & RT6160_FPWM_MASK)
121*de20b747SChiYuan Huang 		return REGULATOR_MODE_FAST;
122*de20b747SChiYuan Huang 
123*de20b747SChiYuan Huang 	return REGULATOR_MODE_NORMAL;
124*de20b747SChiYuan Huang }
125*de20b747SChiYuan Huang 
126*de20b747SChiYuan Huang static int rt6160_set_suspend_voltage(struct regulator_dev *rdev, int uV)
127*de20b747SChiYuan Huang {
128*de20b747SChiYuan Huang 	struct rt6160_priv *priv = rdev_get_drvdata(rdev);
129*de20b747SChiYuan Huang 	struct regmap *regmap = rdev_get_regmap(rdev);
130*de20b747SChiYuan Huang 	unsigned int reg = RT6160_REG_VSELH;
131*de20b747SChiYuan Huang 	int vsel;
132*de20b747SChiYuan Huang 
133*de20b747SChiYuan Huang 	vsel = regulator_map_voltage_linear(rdev, uV, uV);
134*de20b747SChiYuan Huang 	if (vsel < 0)
135*de20b747SChiYuan Huang 		return vsel;
136*de20b747SChiYuan Huang 
137*de20b747SChiYuan Huang 	if (priv->vsel_active_low)
138*de20b747SChiYuan Huang 		reg = RT6160_REG_VSELL;
139*de20b747SChiYuan Huang 
140*de20b747SChiYuan Huang 	return regmap_update_bits(regmap, reg, RT6160_VSEL_MASK, vsel);
141*de20b747SChiYuan Huang }
142*de20b747SChiYuan Huang 
143*de20b747SChiYuan Huang static int rt6160_set_ramp_delay(struct regulator_dev *rdev, int target)
144*de20b747SChiYuan Huang {
145*de20b747SChiYuan Huang 	struct regmap *regmap = rdev_get_regmap(rdev);
146*de20b747SChiYuan Huang 	const int ramp_tables[] = { 1000, 2500, 5000, 10000 };
147*de20b747SChiYuan Huang 	unsigned int i, sel;
148*de20b747SChiYuan Huang 
149*de20b747SChiYuan Huang 	/* Find closest larger or equal */
150*de20b747SChiYuan Huang 	for (i = 0; i < ARRAY_SIZE(ramp_tables); i++) {
151*de20b747SChiYuan Huang 		sel = i;
152*de20b747SChiYuan Huang 
153*de20b747SChiYuan Huang 		/* If ramp delay is equal to 0, directly set ramp speed to fastest */
154*de20b747SChiYuan Huang 		if (target == 0) {
155*de20b747SChiYuan Huang 			sel = ARRAY_SIZE(ramp_tables) - 1;
156*de20b747SChiYuan Huang 			break;
157*de20b747SChiYuan Huang 		}
158*de20b747SChiYuan Huang 
159*de20b747SChiYuan Huang 		if (target <= ramp_tables[i])
160*de20b747SChiYuan Huang 			break;
161*de20b747SChiYuan Huang 	}
162*de20b747SChiYuan Huang 
163*de20b747SChiYuan Huang 	sel <<= ffs(RT6160_RAMPRATE_MASK) - 1;
164*de20b747SChiYuan Huang 
165*de20b747SChiYuan Huang 	return regmap_update_bits(regmap, RT6160_REG_CNTL, RT6160_RAMPRATE_MASK, sel);
166*de20b747SChiYuan Huang }
167*de20b747SChiYuan Huang 
168*de20b747SChiYuan Huang static int rt6160_get_error_flags(struct regulator_dev *rdev, unsigned int *flags)
169*de20b747SChiYuan Huang {
170*de20b747SChiYuan Huang 	struct regmap *regmap = rdev_get_regmap(rdev);
171*de20b747SChiYuan Huang 	unsigned int val, events = 0;
172*de20b747SChiYuan Huang 	int ret;
173*de20b747SChiYuan Huang 
174*de20b747SChiYuan Huang 	ret = regmap_read(regmap, RT6160_REG_STATUS, &val);
175*de20b747SChiYuan Huang 	if (ret)
176*de20b747SChiYuan Huang 		return ret;
177*de20b747SChiYuan Huang 
178*de20b747SChiYuan Huang 	if (val & (RT6160_HDSTAT_MASK | RT6160_TSDSTAT_MASK))
179*de20b747SChiYuan Huang 		events |= REGULATOR_ERROR_OVER_TEMP;
180*de20b747SChiYuan Huang 
181*de20b747SChiYuan Huang 	if (val & RT6160_UVSTAT_MASK)
182*de20b747SChiYuan Huang 		events |= REGULATOR_ERROR_UNDER_VOLTAGE;
183*de20b747SChiYuan Huang 
184*de20b747SChiYuan Huang 	if (val & RT6160_OCSTAT_MASK)
185*de20b747SChiYuan Huang 		events |= REGULATOR_ERROR_OVER_CURRENT;
186*de20b747SChiYuan Huang 
187*de20b747SChiYuan Huang 	if (val & RT6160_PGSTAT_MASK)
188*de20b747SChiYuan Huang 		events |= REGULATOR_ERROR_FAIL;
189*de20b747SChiYuan Huang 
190*de20b747SChiYuan Huang 	*flags = events;
191*de20b747SChiYuan Huang 	return 0;
192*de20b747SChiYuan Huang }
193*de20b747SChiYuan Huang 
194*de20b747SChiYuan Huang static const struct regulator_ops rt6160_regulator_ops = {
195*de20b747SChiYuan Huang 	.list_voltage = regulator_list_voltage_linear,
196*de20b747SChiYuan Huang 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
197*de20b747SChiYuan Huang 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
198*de20b747SChiYuan Huang 
199*de20b747SChiYuan Huang 	.enable = rt6160_enable,
200*de20b747SChiYuan Huang 	.disable = rt6160_disable,
201*de20b747SChiYuan Huang 	.is_enabled = rt6160_is_enabled,
202*de20b747SChiYuan Huang 
203*de20b747SChiYuan Huang 	.set_mode = rt6160_set_mode,
204*de20b747SChiYuan Huang 	.get_mode = rt6160_get_mode,
205*de20b747SChiYuan Huang 	.set_suspend_voltage = rt6160_set_suspend_voltage,
206*de20b747SChiYuan Huang 	.set_ramp_delay = rt6160_set_ramp_delay,
207*de20b747SChiYuan Huang 	.get_error_flags = rt6160_get_error_flags,
208*de20b747SChiYuan Huang };
209*de20b747SChiYuan Huang 
210*de20b747SChiYuan Huang static unsigned int rt6160_of_map_mode(unsigned int mode)
211*de20b747SChiYuan Huang {
212*de20b747SChiYuan Huang 	switch (mode) {
213*de20b747SChiYuan Huang 	case RT6160_MODE_FPWM:
214*de20b747SChiYuan Huang 		return REGULATOR_MODE_FAST;
215*de20b747SChiYuan Huang 	case RT6160_MODE_AUTO:
216*de20b747SChiYuan Huang 		return REGULATOR_MODE_NORMAL;
217*de20b747SChiYuan Huang 	}
218*de20b747SChiYuan Huang 
219*de20b747SChiYuan Huang 	return REGULATOR_MODE_INVALID;
220*de20b747SChiYuan Huang }
221*de20b747SChiYuan Huang 
222*de20b747SChiYuan Huang static bool rt6160_is_accessible_reg(struct device *dev, unsigned int reg)
223*de20b747SChiYuan Huang {
224*de20b747SChiYuan Huang 	if (reg >= RT6160_REG_CNTL && reg <= RT6160_REG_VSELH)
225*de20b747SChiYuan Huang 		return true;
226*de20b747SChiYuan Huang 	return false;
227*de20b747SChiYuan Huang }
228*de20b747SChiYuan Huang 
229*de20b747SChiYuan Huang static bool rt6160_is_volatile_reg(struct device *dev, unsigned int reg)
230*de20b747SChiYuan Huang {
231*de20b747SChiYuan Huang 	if (reg == RT6160_REG_STATUS)
232*de20b747SChiYuan Huang 		return true;
233*de20b747SChiYuan Huang 	return false;
234*de20b747SChiYuan Huang }
235*de20b747SChiYuan Huang 
236*de20b747SChiYuan Huang static const struct regmap_config rt6160_regmap_config = {
237*de20b747SChiYuan Huang 	.reg_bits = 8,
238*de20b747SChiYuan Huang 	.val_bits = 8,
239*de20b747SChiYuan Huang 	.max_register = RT6160_REG_VSELH,
240*de20b747SChiYuan Huang 	.num_reg_defaults_raw = RT6160_NUM_REGS,
241*de20b747SChiYuan Huang 	.cache_type = REGCACHE_FLAT,
242*de20b747SChiYuan Huang 
243*de20b747SChiYuan Huang 	.writeable_reg = rt6160_is_accessible_reg,
244*de20b747SChiYuan Huang 	.readable_reg = rt6160_is_accessible_reg,
245*de20b747SChiYuan Huang 	.volatile_reg = rt6160_is_volatile_reg,
246*de20b747SChiYuan Huang };
247*de20b747SChiYuan Huang 
248*de20b747SChiYuan Huang static int rt6160_probe(struct i2c_client *i2c)
249*de20b747SChiYuan Huang {
250*de20b747SChiYuan Huang 	struct rt6160_priv *priv;
251*de20b747SChiYuan Huang 	struct regulator_config regulator_cfg = {};
252*de20b747SChiYuan Huang 	struct regulator_dev *rdev;
253*de20b747SChiYuan Huang 	unsigned int devid;
254*de20b747SChiYuan Huang 	int ret;
255*de20b747SChiYuan Huang 
256*de20b747SChiYuan Huang 	priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
257*de20b747SChiYuan Huang 	if (!priv)
258*de20b747SChiYuan Huang 		return -ENOMEM;
259*de20b747SChiYuan Huang 
260*de20b747SChiYuan Huang 	priv->vsel_active_low = device_property_present(&i2c->dev, "richtek,vsel-active-low");
261*de20b747SChiYuan Huang 
262*de20b747SChiYuan Huang 	priv->enable_gpio = devm_gpiod_get_optional(&i2c->dev, "enable", GPIOD_OUT_HIGH);
263*de20b747SChiYuan Huang 	if (IS_ERR(priv->enable_gpio)) {
264*de20b747SChiYuan Huang 		dev_err(&i2c->dev, "Failed to get 'enable' gpio\n");
265*de20b747SChiYuan Huang 		return PTR_ERR(priv->enable_gpio);
266*de20b747SChiYuan Huang 	}
267*de20b747SChiYuan Huang 	priv->enable_state = true;
268*de20b747SChiYuan Huang 
269*de20b747SChiYuan Huang 	usleep_range(RT6160_I2CRDY_TIMEUS, RT6160_I2CRDY_TIMEUS + 100);
270*de20b747SChiYuan Huang 
271*de20b747SChiYuan Huang 	priv->regmap = devm_regmap_init_i2c(i2c, &rt6160_regmap_config);
272*de20b747SChiYuan Huang 	if (IS_ERR(priv->regmap)) {
273*de20b747SChiYuan Huang 		ret = PTR_ERR(priv->regmap);
274*de20b747SChiYuan Huang 		dev_err(&i2c->dev, "Failed to init regmap (%d)\n", ret);
275*de20b747SChiYuan Huang 		return ret;
276*de20b747SChiYuan Huang 	}
277*de20b747SChiYuan Huang 
278*de20b747SChiYuan Huang 	ret = regmap_read(priv->regmap, RT6160_REG_DEVID, &devid);
279*de20b747SChiYuan Huang 	if (ret)
280*de20b747SChiYuan Huang 		return ret;
281*de20b747SChiYuan Huang 
282*de20b747SChiYuan Huang 	if ((devid & RT6160_VID_MASK) != RT6160_VENDOR_ID) {
283*de20b747SChiYuan Huang 		dev_err(&i2c->dev, "VID not correct [0x%02x]\n", devid);
284*de20b747SChiYuan Huang 		return -ENODEV;
285*de20b747SChiYuan Huang 	}
286*de20b747SChiYuan Huang 
287*de20b747SChiYuan Huang 	priv->desc.name = "rt6160-buckboost";
288*de20b747SChiYuan Huang 	priv->desc.type = REGULATOR_VOLTAGE;
289*de20b747SChiYuan Huang 	priv->desc.owner = THIS_MODULE;
290*de20b747SChiYuan Huang 	priv->desc.min_uV = RT6160_VOUT_MINUV;
291*de20b747SChiYuan Huang 	priv->desc.uV_step = RT6160_VOUT_STPUV;
292*de20b747SChiYuan Huang 	priv->desc.vsel_reg = RT6160_REG_VSELH;
293*de20b747SChiYuan Huang 	priv->desc.vsel_mask = RT6160_VSEL_MASK;
294*de20b747SChiYuan Huang 	priv->desc.n_voltages = RT6160_N_VOUTS;
295*de20b747SChiYuan Huang 	priv->desc.of_map_mode = rt6160_of_map_mode;
296*de20b747SChiYuan Huang 	priv->desc.ops = &rt6160_regulator_ops;
297*de20b747SChiYuan Huang 	if (priv->vsel_active_low)
298*de20b747SChiYuan Huang 		priv->desc.vsel_reg = RT6160_REG_VSELL;
299*de20b747SChiYuan Huang 
300*de20b747SChiYuan Huang 	regulator_cfg.dev = &i2c->dev;
301*de20b747SChiYuan Huang 	regulator_cfg.of_node = i2c->dev.of_node;
302*de20b747SChiYuan Huang 	regulator_cfg.regmap = priv->regmap;
303*de20b747SChiYuan Huang 	regulator_cfg.driver_data = priv;
304*de20b747SChiYuan Huang 	regulator_cfg.init_data = of_get_regulator_init_data(&i2c->dev, i2c->dev.of_node,
305*de20b747SChiYuan Huang 							     &priv->desc);
306*de20b747SChiYuan Huang 
307*de20b747SChiYuan Huang 	rdev = devm_regulator_register(&i2c->dev, &priv->desc, &regulator_cfg);
308*de20b747SChiYuan Huang 	if (IS_ERR(rdev)) {
309*de20b747SChiYuan Huang 		dev_err(&i2c->dev, "Failed to register regulator\n");
310*de20b747SChiYuan Huang 		return PTR_ERR(rdev);
311*de20b747SChiYuan Huang 	}
312*de20b747SChiYuan Huang 
313*de20b747SChiYuan Huang 	return 0;
314*de20b747SChiYuan Huang }
315*de20b747SChiYuan Huang 
316*de20b747SChiYuan Huang static const struct of_device_id __maybe_unused rt6160_of_match_table[] = {
317*de20b747SChiYuan Huang 	{ .compatible = "richtek,rt6160", },
318*de20b747SChiYuan Huang 	{}
319*de20b747SChiYuan Huang };
320*de20b747SChiYuan Huang MODULE_DEVICE_TABLE(of, rt6160_of_match_table);
321*de20b747SChiYuan Huang 
322*de20b747SChiYuan Huang static struct i2c_driver rt6160_driver = {
323*de20b747SChiYuan Huang 	.driver = {
324*de20b747SChiYuan Huang 		.name = "rt6160",
325*de20b747SChiYuan Huang 		.of_match_table = rt6160_of_match_table,
326*de20b747SChiYuan Huang 	},
327*de20b747SChiYuan Huang 	.probe_new = rt6160_probe,
328*de20b747SChiYuan Huang };
329*de20b747SChiYuan Huang module_i2c_driver(rt6160_driver);
330*de20b747SChiYuan Huang 
331*de20b747SChiYuan Huang MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
332*de20b747SChiYuan Huang MODULE_LICENSE("GPL v2");
333