xref: /linux/drivers/regulator/rtq2208-regulator.c (revision 7b85bb4bc043d703c6d8bb35080b7753a1dd8021)
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <linux/bitops.h>
4 #include <linux/bitfield.h>
5 #include <linux/util_macros.h>
6 #include <linux/module.h>
7 #include <linux/i2c.h>
8 #include <linux/regmap.h>
9 #include <linux/regulator/driver.h>
10 #include <linux/regulator/machine.h>
11 #include <linux/regulator/of_regulator.h>
12 #include <linux/mod_devicetable.h>
13 
14 /* Register */
15 #define RTQ2208_REG_GLOBAL_INT1			0x12
16 #define RTQ2208_REG_FLT_RECORDBUCK_CB		0x18
17 #define RTQ2208_REG_GLOBAL_INT1_MASK		0x1D
18 #define RTQ2208_REG_FLT_MASKBUCK_CB		0x1F
19 #define RTQ2208_REG_BUCK_C_CFG0			0x32
20 #define RTQ2208_REG_BUCK_B_CFG0			0x42
21 #define RTQ2208_REG_BUCK_A_CFG0			0x52
22 #define RTQ2208_REG_BUCK_D_CFG0			0x62
23 #define RTQ2208_REG_BUCK_G_CFG0			0x72
24 #define RTQ2208_REG_BUCK_F_CFG0			0x82
25 #define RTQ2208_REG_BUCK_E_CFG0			0x92
26 #define RTQ2208_REG_BUCK_H_CFG0			0xA2
27 #define RTQ2208_REG_LDO1_CFG			0xB1
28 #define RTQ2208_REG_LDO2_CFG			0xC1
29 #define RTQ2208_REG_LDO_DVS_CTRL		0xD0
30 
31 /* Mask */
32 #define RTQ2208_BUCK_NR_MTP_SEL_MASK		GENMASK(7, 0)
33 #define RTQ2208_BUCK_EN_NR_MTP_SEL0_MASK	BIT(0)
34 #define RTQ2208_BUCK_EN_NR_MTP_SEL1_MASK	BIT(1)
35 #define RTQ2208_BUCK_RSPUP_MASK			GENMASK(6, 4)
36 #define RTQ2208_BUCK_RSPDN_MASK			GENMASK(2, 0)
37 #define RTQ2208_BUCK_NRMODE_MASK		BIT(5)
38 #define RTQ2208_BUCK_STRMODE_MASK		BIT(5)
39 #define RTQ2208_BUCK_EN_STR_MASK		BIT(0)
40 #define RTQ2208_LDO_EN_STR_MASK			BIT(7)
41 #define RTQ2208_EN_DIS_MASK			BIT(0)
42 #define RTQ2208_BUCK_RAMP_SEL_MASK		GENMASK(2, 0)
43 #define RTQ2208_HD_INT_MASK			BIT(0)
44 #define RTQ2208_LDO1_DISCHG_EN_MASK		BIT(4)
45 #define RTQ2208_LDO1_VOSEL_SD_MASK		BIT(5)
46 #define RTQ2208_LDO2_DISCHG_EN_MASK		BIT(6)
47 #define RTQ2208_LDO2_VOSEL_SD_MASK		BIT(7)
48 
49 /* Size */
50 #define RTQ2208_VOUT_MAXNUM			256
51 #define RTQ2208_BUCK_NUM_IRQ_REGS		5
52 #define RTQ2208_STS_NUM_IRQ_REGS		2
53 
54 /* Value */
55 #define RTQ2208_RAMP_VALUE_MIN_uV		500
56 #define RTQ2208_RAMP_VALUE_MAX_uV		16000
57 
58 #define RTQ2208_BUCK_MASK(uv_irq, ov_irq)	(1 << ((uv_irq) % 8) | 1 << ((ov_irq) % 8))
59 
60 enum {
61 	RTQ2208_BUCK_B = 0,
62 	RTQ2208_BUCK_C,
63 	RTQ2208_BUCK_D,
64 	RTQ2208_BUCK_A,
65 	RTQ2208_BUCK_F,
66 	RTQ2208_BUCK_G,
67 	RTQ2208_BUCK_H,
68 	RTQ2208_BUCK_E,
69 	RTQ2208_LDO2,
70 	RTQ2208_LDO1,
71 	RTQ2208_LDO_MAX,
72 };
73 
74 enum {
75 	RTQ2208_AUTO_MODE = 0,
76 	RTQ2208_FCCM,
77 };
78 
79 struct rtq2208_regulator_desc {
80 	struct regulator_desc desc;
81 	unsigned int mtp_sel_reg;
82 	unsigned int mtp_sel_mask;
83 	unsigned int mode_reg;
84 	unsigned int mode_mask;
85 	unsigned int suspend_config_reg;
86 	unsigned int suspend_enable_mask;
87 	unsigned int suspend_mode_mask;
88 };
89 
90 struct rtq2208_rdev_map {
91 	struct regulator_dev *rdev[RTQ2208_LDO_MAX];
92 	struct regmap *regmap;
93 	struct device *dev;
94 };
95 
96 /* set Normal Auto/FCCM mode */
rtq2208_set_mode(struct regulator_dev * rdev,unsigned int mode)97 static int rtq2208_set_mode(struct regulator_dev *rdev, unsigned int mode)
98 {
99 	const struct rtq2208_regulator_desc *rdesc =
100 		(const struct rtq2208_regulator_desc *)rdev->desc;
101 	unsigned int val, shift;
102 
103 	switch (mode) {
104 	case REGULATOR_MODE_NORMAL:
105 		val = RTQ2208_AUTO_MODE;
106 		break;
107 	case REGULATOR_MODE_FAST:
108 		val = RTQ2208_FCCM;
109 		break;
110 	default:
111 		return -EINVAL;
112 	}
113 
114 	shift = ffs(rdesc->mode_mask) - 1;
115 	return regmap_update_bits(rdev->regmap, rdesc->mode_reg,
116 				  rdesc->mode_mask, val << shift);
117 }
118 
rtq2208_get_mode(struct regulator_dev * rdev)119 static unsigned int rtq2208_get_mode(struct regulator_dev *rdev)
120 {
121 	const struct rtq2208_regulator_desc *rdesc =
122 		(const struct rtq2208_regulator_desc *)rdev->desc;
123 	unsigned int mode_val;
124 	int ret;
125 
126 	ret = regmap_read(rdev->regmap, rdesc->mode_reg, &mode_val);
127 	if (ret)
128 		return REGULATOR_MODE_INVALID;
129 
130 	return (mode_val & rdesc->mode_mask) ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
131 }
132 
rtq2208_set_ramp_delay(struct regulator_dev * rdev,int ramp_delay)133 static int rtq2208_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
134 {
135 	const struct regulator_desc *desc = rdev->desc;
136 	unsigned int sel = 0, val;
137 
138 	ramp_delay = max(ramp_delay, RTQ2208_RAMP_VALUE_MIN_uV);
139 	ramp_delay = min(ramp_delay, RTQ2208_RAMP_VALUE_MAX_uV);
140 
141 	ramp_delay /= RTQ2208_RAMP_VALUE_MIN_uV;
142 
143 	/*
144 	 * fls(ramp_delay) - 1: doing LSB shift, let it starts from 0
145 	 *
146 	 * RTQ2208_BUCK_RAMP_SEL_MASK - sel: doing descending order shifting.
147 	 * Because the relation of seleltion and value is like that
148 	 *
149 	 * seletion: value
150 	 * 010: 16mv
151 	 * ...
152 	 * 111: 0.5mv
153 	 *
154 	 * For example, if I would like to select 16mv, the fls(ramp_delay) - 1 will be 0b010,
155 	 * and I need to use 0b111 - sel to do the shifting
156 	 */
157 
158 	sel = fls(ramp_delay) - 1;
159 	sel = RTQ2208_BUCK_RAMP_SEL_MASK - sel;
160 
161 	val = FIELD_PREP(RTQ2208_BUCK_RSPUP_MASK, sel) | FIELD_PREP(RTQ2208_BUCK_RSPDN_MASK, sel);
162 
163 	return regmap_update_bits(rdev->regmap, desc->ramp_reg,
164 				  RTQ2208_BUCK_RSPUP_MASK | RTQ2208_BUCK_RSPDN_MASK, val);
165 }
166 
rtq2208_set_suspend_enable(struct regulator_dev * rdev)167 static int rtq2208_set_suspend_enable(struct regulator_dev *rdev)
168 {
169 	const struct rtq2208_regulator_desc *rdesc =
170 		(const struct rtq2208_regulator_desc *)rdev->desc;
171 
172 	return regmap_set_bits(rdev->regmap, rdesc->suspend_config_reg, rdesc->suspend_enable_mask);
173 }
174 
rtq2208_set_suspend_disable(struct regulator_dev * rdev)175 static int rtq2208_set_suspend_disable(struct regulator_dev *rdev)
176 {
177 	const struct rtq2208_regulator_desc *rdesc =
178 		(const struct rtq2208_regulator_desc *)rdev->desc;
179 
180 	return regmap_update_bits(rdev->regmap, rdesc->suspend_config_reg, rdesc->suspend_enable_mask, 0);
181 }
182 
rtq2208_set_suspend_mode(struct regulator_dev * rdev,unsigned int mode)183 static int rtq2208_set_suspend_mode(struct regulator_dev *rdev, unsigned int mode)
184 {
185 	const struct rtq2208_regulator_desc *rdesc =
186 		(const struct rtq2208_regulator_desc *)rdev->desc;
187 	unsigned int val, shift;
188 
189 	switch (mode) {
190 	case REGULATOR_MODE_NORMAL:
191 		val = RTQ2208_AUTO_MODE;
192 		break;
193 	case REGULATOR_MODE_FAST:
194 		val = RTQ2208_FCCM;
195 		break;
196 	default:
197 		return -EINVAL;
198 	}
199 
200 	shift = ffs(rdesc->suspend_mode_mask) - 1;
201 
202 	return regmap_update_bits(rdev->regmap, rdesc->suspend_config_reg,
203 			rdesc->suspend_mode_mask, val << shift);
204 }
205 
206 static const struct regulator_ops rtq2208_regulator_buck_ops = {
207 	.enable = regulator_enable_regmap,
208 	.disable = regulator_disable_regmap,
209 	.is_enabled = regulator_is_enabled_regmap,
210 	.list_voltage = regulator_list_voltage_linear_range,
211 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
212 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
213 	.set_mode = rtq2208_set_mode,
214 	.get_mode = rtq2208_get_mode,
215 	.set_ramp_delay = rtq2208_set_ramp_delay,
216 	.set_active_discharge = regulator_set_active_discharge_regmap,
217 	.set_suspend_enable = rtq2208_set_suspend_enable,
218 	.set_suspend_disable = rtq2208_set_suspend_disable,
219 	.set_suspend_mode = rtq2208_set_suspend_mode,
220 };
221 
222 static const struct regulator_ops rtq2208_regulator_ldo_fix_ops = {
223 	.enable = regulator_enable_regmap,
224 	.disable = regulator_disable_regmap,
225 	.is_enabled = regulator_is_enabled_regmap,
226 	.set_active_discharge = regulator_set_active_discharge_regmap,
227 	.set_suspend_enable = rtq2208_set_suspend_enable,
228 	.set_suspend_disable = rtq2208_set_suspend_disable,
229 };
230 
231 static const struct regulator_ops rtq2208_regulator_ldo_adj_ops = {
232 	.enable = regulator_enable_regmap,
233 	.disable = regulator_disable_regmap,
234 	.is_enabled = regulator_is_enabled_regmap,
235 	.list_voltage = regulator_list_voltage_table,
236 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
237 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
238 	.set_active_discharge = regulator_set_active_discharge_regmap,
239 	.set_suspend_enable = rtq2208_set_suspend_enable,
240 	.set_suspend_disable = rtq2208_set_suspend_disable,
241 };
242 
243 static const unsigned int rtq2208_ldo_volt_table[] = {
244 	1800000,
245 	3300000,
246 };
247 
248 static struct of_regulator_match rtq2208_ldo_match[] = {
249 	{.name = "ldo2", },
250 	{.name = "ldo1", },
251 };
252 
rtq2208_of_map_mode(unsigned int mode)253 static unsigned int rtq2208_of_map_mode(unsigned int mode)
254 {
255 	switch (mode) {
256 	case RTQ2208_AUTO_MODE:
257 		return REGULATOR_MODE_NORMAL;
258 	case RTQ2208_FCCM:
259 		return REGULATOR_MODE_FAST;
260 	default:
261 		return REGULATOR_MODE_INVALID;
262 	}
263 }
264 
rtq2208_init_irq_mask(struct rtq2208_rdev_map * rdev_map,unsigned int * buck_masks)265 static int rtq2208_init_irq_mask(struct rtq2208_rdev_map *rdev_map, unsigned int *buck_masks)
266 {
267 	unsigned char buck_clr_masks[5] = {0x33, 0x33, 0x33, 0x33, 0x33},
268 		      sts_clr_masks[2] = {0xE7, 0xF7}, sts_masks[2] = {0xE6, 0xF6};
269 	int ret;
270 
271 	/* write clear all buck irq once */
272 	ret = regmap_bulk_write(rdev_map->regmap, RTQ2208_REG_FLT_RECORDBUCK_CB, buck_clr_masks, 5);
273 	if (ret)
274 		return dev_err_probe(rdev_map->dev, ret, "Failed to clr buck irqs\n");
275 
276 	/* write clear general irq once */
277 	ret = regmap_bulk_write(rdev_map->regmap, RTQ2208_REG_GLOBAL_INT1, sts_clr_masks, 2);
278 	if (ret)
279 		return dev_err_probe(rdev_map->dev, ret, "Failed to clr general irqs\n");
280 
281 	/* unmask buck ov/uv irq */
282 	ret = regmap_bulk_write(rdev_map->regmap, RTQ2208_REG_FLT_MASKBUCK_CB, buck_masks, 5);
283 	if (ret)
284 		return dev_err_probe(rdev_map->dev, ret, "Failed to unmask buck irqs\n");
285 
286 	/* unmask needed general irq */
287 	return regmap_bulk_write(rdev_map->regmap, RTQ2208_REG_GLOBAL_INT1_MASK, sts_masks, 2);
288 }
289 
rtq2208_irq_handler(int irqno,void * devid)290 static irqreturn_t rtq2208_irq_handler(int irqno, void *devid)
291 {
292 	unsigned char buck_flags[RTQ2208_BUCK_NUM_IRQ_REGS], sts_flags[RTQ2208_STS_NUM_IRQ_REGS];
293 	int ret = 0, i, uv_bit, ov_bit;
294 	struct rtq2208_rdev_map *rdev_map = devid;
295 	struct regulator_dev *rdev;
296 
297 	if (!rdev_map)
298 		return IRQ_NONE;
299 
300 	/* read irq event */
301 	ret = regmap_bulk_read(rdev_map->regmap, RTQ2208_REG_FLT_RECORDBUCK_CB,
302 				buck_flags, ARRAY_SIZE(buck_flags));
303 	if (ret)
304 		return IRQ_NONE;
305 
306 	ret = regmap_bulk_read(rdev_map->regmap, RTQ2208_REG_GLOBAL_INT1,
307 				sts_flags, ARRAY_SIZE(sts_flags));
308 	if (ret)
309 		return IRQ_NONE;
310 
311 	/* clear irq event */
312 	ret = regmap_bulk_write(rdev_map->regmap, RTQ2208_REG_FLT_RECORDBUCK_CB,
313 				buck_flags, ARRAY_SIZE(buck_flags));
314 	if (ret)
315 		return IRQ_NONE;
316 
317 	ret = regmap_bulk_write(rdev_map->regmap, RTQ2208_REG_GLOBAL_INT1,
318 				sts_flags, ARRAY_SIZE(sts_flags));
319 	if (ret)
320 		return IRQ_NONE;
321 
322 	for (i = 0; i < RTQ2208_LDO_MAX; i++) {
323 		if (!rdev_map->rdev[i])
324 			continue;
325 
326 		rdev = rdev_map->rdev[i];
327 		/* uv irq */
328 		uv_bit = (i & 1) ? 4 : 0;
329 		if (buck_flags[i >> 1] & (1 << uv_bit))
330 			regulator_notifier_call_chain(rdev,
331 					REGULATOR_EVENT_UNDER_VOLTAGE, NULL);
332 		/* ov irq */
333 		ov_bit = uv_bit + 1;
334 		if (buck_flags[i >> 1] & (1 << ov_bit))
335 			regulator_notifier_call_chain(rdev,
336 					REGULATOR_EVENT_REGULATION_OUT, NULL);
337 
338 		/* hd irq */
339 		if (sts_flags[1] & RTQ2208_HD_INT_MASK)
340 			regulator_notifier_call_chain(rdev,
341 					REGULATOR_EVENT_OVER_TEMP, NULL);
342 	}
343 
344 	return IRQ_HANDLED;
345 }
346 
rtq2208_of_get_ldo_dvs_ability(struct device * dev)347 static int rtq2208_of_get_ldo_dvs_ability(struct device *dev)
348 {
349 	struct device_node *np;
350 	struct of_regulator_match *match;
351 	struct regulator_desc *desc;
352 	struct regulator_init_data *init_data;
353 	u32 fixed_uV;
354 	int ret, i;
355 
356 	if (!dev->of_node)
357 		return -ENODEV;
358 
359 	np = of_get_child_by_name(dev->of_node, "regulators");
360 	if (!np)
361 		np = dev->of_node;
362 
363 	ret = of_regulator_match(dev, np, rtq2208_ldo_match, ARRAY_SIZE(rtq2208_ldo_match));
364 
365 	of_node_put(np);
366 
367 	if (ret < 0)
368 		return ret;
369 
370 	for (i = 0; i < ARRAY_SIZE(rtq2208_ldo_match); i++) {
371 		match = rtq2208_ldo_match + i;
372 		init_data = match->init_data;
373 		desc = (struct regulator_desc *)match->desc;
374 
375 		if (!init_data || !desc)
376 			continue;
377 
378 		/* specify working fixed voltage if the propery exists */
379 		ret = of_property_read_u32(match->of_node, "richtek,fixed-microvolt", &fixed_uV);
380 
381 		if (!ret) {
382 			if (fixed_uV != init_data->constraints.min_uV ||
383 				fixed_uV != init_data->constraints.max_uV)
384 				return -EINVAL;
385 			desc->n_voltages = 1;
386 			desc->fixed_uV = fixed_uV;
387 			desc->fixed_uV = init_data->constraints.min_uV;
388 			desc->ops = &rtq2208_regulator_ldo_fix_ops;
389 		} else {
390 			desc->n_voltages = ARRAY_SIZE(rtq2208_ldo_volt_table);
391 			desc->volt_table = rtq2208_ldo_volt_table;
392 			desc->ops = &rtq2208_regulator_ldo_adj_ops;
393 		}
394 	}
395 
396 	return 0;
397 }
398 
399 
400 #define BUCK_INFO(_name, _id)						\
401 {									\
402 	.name = _name,							\
403 	.base = RTQ2208_REG_BUCK_##_id##_CFG0,				\
404 	.enable_reg = BUCK_RG_SHIFT(RTQ2208_REG_BUCK_##_id##_CFG0, 2),	\
405 	.dis_reg = RTQ2208_REG_BUCK_##_id##_CFG0,			\
406 }
407 
408 #define LDO_INFO(_name, _id)						\
409 {									\
410 	.name = _name,							\
411 	.base = RTQ2208_REG_LDO##_id##_CFG,				\
412 	.enable_reg = RTQ2208_REG_LDO##_id##_CFG,			\
413 	.dis_mask = RTQ2208_LDO##_id##_DISCHG_EN_MASK,			\
414 	.dis_on = RTQ2208_LDO##_id##_DISCHG_EN_MASK,			\
415 	.vsel_mask = RTQ2208_LDO##_id##_VOSEL_SD_MASK,			\
416 }
417 
418 #define BUCK_RG_SHIFT(_base, _shift)	(_base + _shift)
419 #define	VSEL_SHIFT(_sel)	(_sel ? 3 : 1)
420 #define MTP_SEL_MASK(_sel)	RTQ2208_BUCK_EN_NR_MTP_SEL##_sel##_MASK
421 
422 static const struct linear_range rtq2208_vout_range[] = {
423 	REGULATOR_LINEAR_RANGE(400000, 0, 180, 5000),
424 	REGULATOR_LINEAR_RANGE(1310000, 181, 255, 10000),
425 };
426 
rtq2208_init_regulator_desc(struct rtq2208_regulator_desc * rdesc,int mtp_sel,int idx)427 static void rtq2208_init_regulator_desc(struct rtq2208_regulator_desc *rdesc, int mtp_sel, int idx)
428 {
429 	struct regulator_desc *desc;
430 	static const struct {
431 		char *name;
432 		int base;
433 		int enable_reg;
434 		int dis_reg;
435 		int dis_mask;
436 		int dis_on;
437 		int vsel_mask;
438 	} regulator_info[] = {
439 		BUCK_INFO("buck-b", B),
440 		BUCK_INFO("buck-c", C),
441 		BUCK_INFO("buck-d", D),
442 		BUCK_INFO("buck-a", A),
443 		BUCK_INFO("buck-f", F),
444 		BUCK_INFO("buck-g", G),
445 		BUCK_INFO("buck-h", H),
446 		BUCK_INFO("buck-e", E),
447 		LDO_INFO("ldo2", 2),
448 		LDO_INFO("ldo1", 1),
449 	}, *curr_info;
450 
451 	curr_info = regulator_info + idx;
452 	desc = &rdesc->desc;
453 	desc->name = curr_info->name;
454 	desc->of_match = of_match_ptr(curr_info->name);
455 	desc->regulators_node = of_match_ptr("regulators");
456 	desc->id = idx;
457 	desc->owner = THIS_MODULE;
458 	desc->type = REGULATOR_VOLTAGE;
459 	desc->enable_mask = mtp_sel ? MTP_SEL_MASK(1) : MTP_SEL_MASK(0);
460 	desc->enable_reg = curr_info->enable_reg;
461 	desc->active_discharge_off = 0;
462 
463 	rdesc->mode_mask = RTQ2208_BUCK_NRMODE_MASK;
464 
465 	if (idx >= RTQ2208_BUCK_B && idx <= RTQ2208_BUCK_E) {
466 		/* init buck desc */
467 		desc->ops = &rtq2208_regulator_buck_ops;
468 		desc->vsel_reg = curr_info->base + VSEL_SHIFT(mtp_sel);
469 		desc->vsel_mask = RTQ2208_BUCK_NR_MTP_SEL_MASK;
470 		desc->n_voltages = RTQ2208_VOUT_MAXNUM;
471 		desc->linear_ranges = rtq2208_vout_range;
472 		desc->n_linear_ranges = ARRAY_SIZE(rtq2208_vout_range);
473 		desc->ramp_reg = BUCK_RG_SHIFT(curr_info->base, 5);
474 		desc->of_map_mode = rtq2208_of_map_mode;
475 		desc->active_discharge_reg = curr_info->dis_reg;
476 		desc->active_discharge_on = RTQ2208_EN_DIS_MASK;
477 		desc->active_discharge_mask = RTQ2208_EN_DIS_MASK;
478 
479 		rdesc->mode_reg = BUCK_RG_SHIFT(curr_info->base, 2);
480 		rdesc->suspend_config_reg = BUCK_RG_SHIFT(curr_info->base, 4);
481 		rdesc->suspend_enable_mask = RTQ2208_BUCK_EN_STR_MASK;
482 		rdesc->suspend_mode_mask = RTQ2208_BUCK_STRMODE_MASK;
483 	} else {
484 		/* init ldo desc */
485 		desc->active_discharge_reg = RTQ2208_REG_LDO_DVS_CTRL;
486 		desc->active_discharge_on = curr_info->dis_on;
487 		desc->active_discharge_mask = curr_info->dis_mask;
488 		desc->vsel_reg = RTQ2208_REG_LDO_DVS_CTRL;
489 		desc->vsel_mask = curr_info->vsel_mask;
490 
491 		rdesc->suspend_config_reg = curr_info->base;
492 		rdesc->suspend_enable_mask = RTQ2208_LDO_EN_STR_MASK;
493 	}
494 }
495 
rtq2208_parse_regulator_dt_data(int n_regulator,const unsigned int * regulator_idx_table,struct rtq2208_regulator_desc * rdesc[RTQ2208_LDO_MAX],struct device * dev)496 static int rtq2208_parse_regulator_dt_data(int n_regulator, const unsigned int *regulator_idx_table,
497 		struct rtq2208_regulator_desc *rdesc[RTQ2208_LDO_MAX], struct device *dev)
498 {
499 	int mtp_sel, i, idx, ret;
500 
501 	/* get mtp_sel0 or mtp_sel1 */
502 	mtp_sel = device_property_read_bool(dev, "richtek,mtp-sel-high");
503 
504 	for (i = 0; i < n_regulator; i++) {
505 		idx = regulator_idx_table[i];
506 
507 		rdesc[i] = devm_kcalloc(dev, 1, sizeof(*rdesc[0]), GFP_KERNEL);
508 		if (!rdesc[i])
509 			return -ENOMEM;
510 
511 		rtq2208_init_regulator_desc(rdesc[i], mtp_sel, idx);
512 
513 		/* init ldo dvs ability */
514 		if (idx >= RTQ2208_LDO2)
515 			rtq2208_ldo_match[idx - RTQ2208_LDO2].desc = &rdesc[i]->desc;
516 	}
517 
518 	/* init ldo fixed_uV */
519 	ret = rtq2208_of_get_ldo_dvs_ability(dev);
520 	if (ret)
521 		return dev_err_probe(dev, ret, "Failed to get ldo fixed_uV\n");
522 
523 	return 0;
524 
525 }
526 
527 /** different slave address corresponds different used bucks
528  * slave address 0x10: BUCK[BCA FGE]
529  * slave address 0x20: BUCK[BC FGHE]
530  * slave address 0x40: BUCK[C G]
531  */
rtq2208_regulator_check(int slave_addr,int * num,int * regulator_idx_table,unsigned int * buck_masks)532 static int rtq2208_regulator_check(int slave_addr, int *num,
533 				int *regulator_idx_table, unsigned int *buck_masks)
534 {
535 	static bool rtq2208_used_table[3][RTQ2208_LDO_MAX] = {
536 		/* BUCK[BCA FGE], LDO[12] */
537 		{1, 1, 0, 1, 1, 1, 0, 1, 1, 1},
538 		/* BUCK[BC FGHE], LDO[12]*/
539 		{1, 1, 0, 0, 1, 1, 1, 1, 1, 1},
540 		/* BUCK[C G], LDO[12] */
541 		{0, 1, 0, 0, 0, 1, 0, 0, 1, 1},
542 	};
543 	int i, idx = ffs(slave_addr >> 4) - 1;
544 	u8 mask;
545 
546 	for (i = 0; i < RTQ2208_LDO_MAX; i++) {
547 		if (!rtq2208_used_table[idx][i])
548 			continue;
549 
550 		regulator_idx_table[(*num)++] = i;
551 
552 		mask = RTQ2208_BUCK_MASK(4 * i, 4 * i + 1);
553 		buck_masks[i >> 1] &= ~mask;
554 	}
555 
556 	return 0;
557 }
558 
559 static const struct regmap_config rtq2208_regmap_config = {
560 	.reg_bits = 8,
561 	.val_bits = 8,
562 	.max_register = 0xEF,
563 };
564 
rtq2208_probe(struct i2c_client * i2c)565 static int rtq2208_probe(struct i2c_client *i2c)
566 {
567 	struct device *dev = &i2c->dev;
568 	struct regmap *regmap;
569 	struct rtq2208_regulator_desc *rdesc[RTQ2208_LDO_MAX];
570 	struct regulator_dev *rdev;
571 	struct regulator_config cfg = {};
572 	struct rtq2208_rdev_map *rdev_map;
573 	int i, ret = 0, idx, n_regulator = 0;
574 	unsigned int regulator_idx_table[RTQ2208_LDO_MAX],
575 		     buck_masks[RTQ2208_BUCK_NUM_IRQ_REGS] = {0x33, 0x33, 0x33, 0x33, 0x33};
576 
577 	rdev_map = devm_kzalloc(dev, sizeof(struct rtq2208_rdev_map), GFP_KERNEL);
578 	if (!rdev_map)
579 		return -ENOMEM;
580 
581 	regmap = devm_regmap_init_i2c(i2c, &rtq2208_regmap_config);
582 	if (IS_ERR(regmap))
583 		return dev_err_probe(dev, PTR_ERR(regmap), "Failed to allocate regmap\n");
584 
585 	/* get needed regulator */
586 	ret = rtq2208_regulator_check(i2c->addr, &n_regulator, regulator_idx_table, buck_masks);
587 	if (ret)
588 		return dev_err_probe(dev, ret, "Failed to check used regulators\n");
589 
590 	rdev_map->regmap = regmap;
591 	rdev_map->dev = dev;
592 
593 	cfg.dev = dev;
594 
595 	/* init regulator desc */
596 	ret = rtq2208_parse_regulator_dt_data(n_regulator, regulator_idx_table, rdesc, dev);
597 	if (ret)
598 		return ret;
599 
600 	for (i = 0; i < n_regulator; i++) {
601 		idx = regulator_idx_table[i];
602 
603 		/* register regulator */
604 		rdev = devm_regulator_register(dev, &rdesc[i]->desc, &cfg);
605 		if (IS_ERR(rdev))
606 			return PTR_ERR(rdev);
607 
608 		rdev_map->rdev[idx] = rdev;
609 	}
610 
611 	/* init interrupt mask */
612 	ret = rtq2208_init_irq_mask(rdev_map, buck_masks);
613 	if (ret)
614 		return ret;
615 
616 	/* register interrupt */
617 	return devm_request_threaded_irq(dev, i2c->irq, NULL, rtq2208_irq_handler,
618 					IRQF_ONESHOT, dev_name(dev), rdev_map);
619 }
620 
621 static const struct of_device_id rtq2208_device_tables[] = {
622 	{ .compatible = "richtek,rtq2208" },
623 	{}
624 };
625 MODULE_DEVICE_TABLE(of, rtq2208_device_tables);
626 
627 static struct i2c_driver rtq2208_driver = {
628 	.driver = {
629 		.name = "rtq2208",
630 		.of_match_table = rtq2208_device_tables,
631 	},
632 	.probe = rtq2208_probe,
633 };
634 module_i2c_driver(rtq2208_driver);
635 
636 MODULE_AUTHOR("Alina Yu <alina_yu@richtek.com>");
637 MODULE_DESCRIPTION("Richtek RTQ2208 Regulator Driver");
638 MODULE_LICENSE("GPL");
639