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