xref: /linux/drivers/regulator/da9121-regulator.c (revision 55d0969c451159cff86949b38c39171cab962069)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // DA9121 Single-channel dual-phase 10A buck converter
4 //
5 // Copyright (C) 2020 Axis Communications AB
6 //
7 // DA9130 Single-channel dual-phase 10A buck converter (Automotive)
8 // DA9217 Single-channel dual-phase  6A buck converter
9 // DA9122 Dual-channel single-phase  5A buck converter
10 // DA9131 Dual-channel single-phase  5A buck converter (Automotive)
11 // DA9220 Dual-channel single-phase  3A buck converter
12 // DA9132 Dual-channel single-phase  3A buck converter (Automotive)
13 //
14 // Copyright (C) 2020 Dialog Semiconductor
15 
16 #include <linux/of.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/regulator/of_regulator.h>
19 #include <linux/regulator/machine.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/module.h>
22 #include <linux/regmap.h>
23 #include <linux/err.h>
24 #include <linux/i2c.h>
25 #include <linux/regulator/da9121.h>
26 #include <linux/interrupt.h>
27 #include <linux/workqueue.h>
28 
29 #include "da9121-regulator.h"
30 
31 /* Chip data */
32 struct da9121 {
33 	struct device *dev;
34 	struct delayed_work work;
35 	struct da9121_pdata *pdata;
36 	struct regmap *regmap;
37 	struct regulator_dev *rdev[DA9121_IDX_MAX];
38 	unsigned int persistent[2];
39 	unsigned int passive_delay;
40 	int chip_irq;
41 	int variant_id;
42 	int subvariant_id;
43 };
44 
45 /* Define ranges for different variants, enabling translation to/from
46  * registers. Maximums give scope to allow for transients.
47  */
48 struct da9121_range {
49 	int val_min;
50 	int val_max;
51 	int val_stp;
52 	int reg_min;
53 	int reg_max;
54 };
55 
56 static const struct da9121_range da9121_10A_2phase_current = {
57 	.val_min =  7000000,
58 	.val_max = 20000000,
59 	.val_stp =  1000000,
60 	.reg_min = 1,
61 	.reg_max = 14,
62 };
63 
64 static const struct da9121_range da9121_6A_2phase_current = {
65 	.val_min =  7000000,
66 	.val_max = 12000000,
67 	.val_stp =  1000000,
68 	.reg_min = 1,
69 	.reg_max = 6,
70 };
71 
72 static const struct da9121_range da9121_5A_1phase_current = {
73 	.val_min =  3500000,
74 	.val_max = 10000000,
75 	.val_stp =   500000,
76 	.reg_min = 1,
77 	.reg_max = 14,
78 };
79 
80 static const struct da9121_range da9121_3A_1phase_current = {
81 	.val_min = 3500000,
82 	.val_max = 6000000,
83 	.val_stp =  500000,
84 	.reg_min = 1,
85 	.reg_max = 6,
86 };
87 
88 static const struct da9121_range da914x_40A_4phase_current = {
89 	.val_min = 26000000,
90 	.val_max = 78000000,
91 	.val_stp =  4000000,
92 	.reg_min = 1,
93 	.reg_max = 14,
94 };
95 
96 static const struct da9121_range da914x_20A_2phase_current = {
97 	.val_min = 13000000,
98 	.val_max = 39000000,
99 	.val_stp =  2000000,
100 	.reg_min = 1,
101 	.reg_max = 14,
102 };
103 
104 struct da9121_variant_info {
105 	int num_bucks;
106 	int num_phases;
107 	const struct da9121_range *current_range;
108 };
109 
110 static const struct da9121_variant_info variant_parameters[] = {
111 	{ 1, 2, &da9121_10A_2phase_current },	//DA9121_TYPE_DA9121_DA9130
112 	{ 2, 1, &da9121_3A_1phase_current  },	//DA9121_TYPE_DA9220_DA9132
113 	{ 2, 1, &da9121_5A_1phase_current  },	//DA9121_TYPE_DA9122_DA9131
114 	{ 1, 2, &da9121_6A_2phase_current  },	//DA9121_TYPE_DA9217
115 	{ 1, 4, &da914x_40A_4phase_current },   //DA9121_TYPE_DA9141
116 	{ 1, 2, &da914x_20A_2phase_current },   //DA9121_TYPE_DA9142
117 };
118 
119 struct da9121_field {
120 	unsigned int reg;
121 	unsigned int msk;
122 };
123 
124 static const struct da9121_field da9121_current_field[2] = {
125 	{ DA9121_REG_BUCK_BUCK1_2, DA9121_MASK_BUCK_BUCKx_2_CHx_ILIM },
126 	{ DA9xxx_REG_BUCK_BUCK2_2, DA9121_MASK_BUCK_BUCKx_2_CHx_ILIM },
127 };
128 
129 static const struct da9121_field da9121_mode_field[2] = {
130 	{ DA9121_REG_BUCK_BUCK1_4, DA9121_MASK_BUCK_BUCKx_4_CHx_A_MODE },
131 	{ DA9xxx_REG_BUCK_BUCK2_4, DA9121_MASK_BUCK_BUCKx_4_CHx_A_MODE },
132 };
133 
134 struct status_event_data {
135 	int buck_id; /* 0=core, 1/2-buck */
136 	int reg_index;  /* index for status/event/mask register selection */
137 	int status_bit; /* bit masks... */
138 	int event_bit;
139 	int mask_bit;
140 	unsigned long notification; /* Notification for status inception */
141 	char *warn; /* if NULL, notify - otherwise dev_warn this string */
142 };
143 
144 #define DA9121_STATUS(id, bank, name, notification, warning) \
145 	{ id, bank, \
146 	DA9121_MASK_SYS_STATUS_##bank##_##name, \
147 	DA9121_MASK_SYS_EVENT_##bank##_E_##name, \
148 	DA9121_MASK_SYS_MASK_##bank##_M_##name, \
149 	notification, warning }
150 
151 /* For second buck related event bits that are specific to DA9122, DA9220 variants */
152 #define DA9xxx_STATUS(id, bank, name, notification, warning) \
153 	{ id, bank, \
154 	DA9xxx_MASK_SYS_STATUS_##bank##_##name, \
155 	DA9xxx_MASK_SYS_EVENT_##bank##_E_##name, \
156 	DA9xxx_MASK_SYS_MASK_##bank##_M_##name, \
157 	notification, warning }
158 
159 /* The status signals that may need servicing, depending on device variant.
160  * After assertion, they persist; so event is notified, the IRQ disabled,
161  * and status polled until clear again and IRQ is reenabled.
162  *
163  * SG/PG1/PG2 should be set when device first powers up and should never
164  * re-occur. When this driver starts, it is expected that these will have
165  * self-cleared for when the IRQs are enabled, so these should never be seen.
166  * If seen, the implication is that the device has reset.
167  *
168  * GPIO0/1/2 are not configured for use by default, so should not be seen.
169  */
170 static const struct status_event_data status_event_handling[] = {
171 	DA9xxx_STATUS(0, 0, SG, 0, "Handled E_SG\n"),
172 	DA9121_STATUS(0, 0, TEMP_CRIT, (REGULATOR_EVENT_OVER_TEMP|REGULATOR_EVENT_DISABLE), NULL),
173 	DA9121_STATUS(0, 0, TEMP_WARN, REGULATOR_EVENT_OVER_TEMP, NULL),
174 	DA9121_STATUS(1, 1, PG1, 0, "Handled E_PG1\n"),
175 	DA9121_STATUS(1, 1, OV1, REGULATOR_EVENT_REGULATION_OUT, NULL),
176 	DA9121_STATUS(1, 1, UV1, REGULATOR_EVENT_UNDER_VOLTAGE, NULL),
177 	DA9121_STATUS(1, 1, OC1, REGULATOR_EVENT_OVER_CURRENT, NULL),
178 	DA9xxx_STATUS(2, 1, PG2, 0, "Handled E_PG2\n"),
179 	DA9xxx_STATUS(2, 1, OV2, REGULATOR_EVENT_REGULATION_OUT, NULL),
180 	DA9xxx_STATUS(2, 1, UV2, REGULATOR_EVENT_UNDER_VOLTAGE, NULL),
181 	DA9xxx_STATUS(2, 1, OC2, REGULATOR_EVENT_OVER_CURRENT, NULL),
182 	DA9121_STATUS(0, 2, GPIO0, 0, "Handled E_GPIO0\n"),
183 	DA9121_STATUS(0, 2, GPIO1, 0, "Handled E_GPIO1\n"),
184 	DA9121_STATUS(0, 2, GPIO2, 0, "Handled E_GPIO2\n"),
185 };
186 
187 static int da9121_get_current_limit(struct regulator_dev *rdev)
188 {
189 	struct da9121 *chip = rdev_get_drvdata(rdev);
190 	int id = rdev_get_id(rdev);
191 	const struct da9121_range *range =
192 		variant_parameters[chip->variant_id].current_range;
193 	unsigned int val = 0;
194 	int ret = 0;
195 
196 	ret = regmap_read(chip->regmap, da9121_current_field[id].reg, &val);
197 	if (ret < 0) {
198 		dev_err(chip->dev, "Cannot read BUCK register: %d\n", ret);
199 		goto error;
200 	}
201 
202 	if (val < range->reg_min) {
203 		ret = -EACCES;
204 		goto error;
205 	}
206 
207 	if (val > range->reg_max) {
208 		ret = -EINVAL;
209 		goto error;
210 	}
211 
212 	return range->val_min + (range->val_stp * (val - range->reg_min));
213 error:
214 	return ret;
215 }
216 
217 static int da9121_ceiling_selector(struct regulator_dev *rdev,
218 		int min, int max,
219 		unsigned int *selector)
220 {
221 	struct da9121 *chip = rdev_get_drvdata(rdev);
222 	const struct da9121_range *range =
223 		variant_parameters[chip->variant_id].current_range;
224 	unsigned int level;
225 	unsigned int i = 0;
226 	unsigned int sel = 0;
227 	int ret = 0;
228 
229 	if (range->val_min > max || range->val_max < min) {
230 		dev_err(chip->dev,
231 			"Requested current out of regulator capability\n");
232 		ret = -EINVAL;
233 		goto error;
234 	}
235 
236 	level = range->val_max;
237 	for (i = range->reg_max; i >= range->reg_min; i--) {
238 		if (level <= max) {
239 			sel = i;
240 			break;
241 		}
242 		level -= range->val_stp;
243 	}
244 
245 	if (level < min) {
246 		dev_err(chip->dev,
247 			"Best match falls below minimum requested current\n");
248 		ret = -EINVAL;
249 		goto error;
250 	}
251 
252 	*selector = sel;
253 error:
254 	return ret;
255 }
256 
257 static int da9121_set_current_limit(struct regulator_dev *rdev,
258 				int min_ua, int max_ua)
259 {
260 	struct da9121 *chip = rdev_get_drvdata(rdev);
261 	int id = rdev_get_id(rdev);
262 	const struct da9121_range *range =
263 		variant_parameters[chip->variant_id].current_range;
264 	unsigned int sel = 0;
265 	int ret = 0;
266 
267 	if (min_ua < range->val_min ||
268 	    max_ua > range->val_max) {
269 		ret = -EINVAL;
270 		goto error;
271 	}
272 
273 	if (rdev->desc->ops->is_enabled(rdev)) {
274 		ret = -EBUSY;
275 		goto error;
276 	}
277 
278 	ret = da9121_ceiling_selector(rdev, min_ua, max_ua, &sel);
279 	if (ret < 0)
280 		goto error;
281 
282 	ret = regmap_update_bits(chip->regmap,
283 				da9121_current_field[id].reg,
284 				da9121_current_field[id].msk,
285 				(unsigned int)sel);
286 	if (ret < 0)
287 		dev_err(chip->dev, "Cannot update BUCK current limit, err: %d\n", ret);
288 
289 error:
290 	return ret;
291 }
292 
293 static unsigned int da9121_map_mode(unsigned int mode)
294 {
295 	switch (mode) {
296 	case DA9121_BUCK_MODE_FORCE_PWM:
297 		return REGULATOR_MODE_FAST;
298 	case DA9121_BUCK_MODE_FORCE_PWM_SHEDDING:
299 		return REGULATOR_MODE_NORMAL;
300 	case DA9121_BUCK_MODE_AUTO:
301 		return REGULATOR_MODE_IDLE;
302 	case DA9121_BUCK_MODE_FORCE_PFM:
303 		return REGULATOR_MODE_STANDBY;
304 	default:
305 		return REGULATOR_MODE_INVALID;
306 	}
307 }
308 
309 static int da9121_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
310 {
311 	struct da9121 *chip = rdev_get_drvdata(rdev);
312 	int id = rdev_get_id(rdev);
313 	unsigned int val;
314 
315 	switch (mode) {
316 	case REGULATOR_MODE_FAST:
317 		val = DA9121_BUCK_MODE_FORCE_PWM;
318 		break;
319 	case REGULATOR_MODE_NORMAL:
320 		val = DA9121_BUCK_MODE_FORCE_PWM_SHEDDING;
321 		break;
322 	case REGULATOR_MODE_IDLE:
323 		val = DA9121_BUCK_MODE_AUTO;
324 		break;
325 	case REGULATOR_MODE_STANDBY:
326 		val = DA9121_BUCK_MODE_FORCE_PFM;
327 		break;
328 	default:
329 		return -EINVAL;
330 	}
331 
332 	return regmap_update_bits(chip->regmap,
333 				  da9121_mode_field[id].reg,
334 				  da9121_mode_field[id].msk,
335 				  val);
336 }
337 
338 static unsigned int da9121_buck_get_mode(struct regulator_dev *rdev)
339 {
340 	struct da9121 *chip = rdev_get_drvdata(rdev);
341 	int id = rdev_get_id(rdev);
342 	unsigned int val, mode;
343 	int ret = 0;
344 
345 	ret = regmap_read(chip->regmap, da9121_mode_field[id].reg, &val);
346 	if (ret < 0) {
347 		dev_err(chip->dev, "Cannot read BUCK register: %d\n", ret);
348 		return -EINVAL;
349 	}
350 
351 	mode = da9121_map_mode(val & da9121_mode_field[id].msk);
352 	if (mode == REGULATOR_MODE_INVALID)
353 		return -EINVAL;
354 
355 	return mode;
356 }
357 
358 static const struct regulator_ops da9121_buck_ops = {
359 	.enable = regulator_enable_regmap,
360 	.disable = regulator_disable_regmap,
361 	.is_enabled = regulator_is_enabled_regmap,
362 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
363 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
364 	.list_voltage = regulator_list_voltage_linear,
365 	.get_current_limit = da9121_get_current_limit,
366 	.set_current_limit = da9121_set_current_limit,
367 	.set_mode = da9121_buck_set_mode,
368 	.get_mode = da9121_buck_get_mode,
369 };
370 
371 static struct of_regulator_match da9121_matches[] = {
372 	[DA9121_IDX_BUCK1] = { .name = "buck1" },
373 	[DA9121_IDX_BUCK2] = { .name = "buck2" },
374 };
375 
376 static int da9121_of_parse_cb(struct device_node *np,
377 				const struct regulator_desc *desc,
378 				struct regulator_config *config)
379 {
380 	struct da9121 *chip = config->driver_data;
381 	struct da9121_pdata *pdata;
382 	struct gpio_desc *ena_gpiod;
383 
384 	if (chip->pdata == NULL) {
385 		pdata = devm_kzalloc(chip->dev, sizeof(*pdata), GFP_KERNEL);
386 		if (!pdata)
387 			return -ENOMEM;
388 	} else {
389 		pdata = chip->pdata;
390 	}
391 
392 	pdata->num_buck++;
393 
394 	if (pdata->num_buck > variant_parameters[chip->variant_id].num_bucks) {
395 		dev_err(chip->dev, "Error: excessive regulators for device\n");
396 		return -ENODEV;
397 	}
398 
399 	ena_gpiod = fwnode_gpiod_get_index(of_fwnode_handle(np), "enable", 0,
400 						GPIOD_OUT_HIGH |
401 						GPIOD_FLAGS_BIT_NONEXCLUSIVE,
402 						"da9121-enable");
403 	if (!IS_ERR(ena_gpiod))
404 		config->ena_gpiod = ena_gpiod;
405 
406 	if (variant_parameters[chip->variant_id].num_bucks == 2) {
407 		uint32_t ripple_cancel;
408 		uint32_t ripple_reg;
409 		int ret;
410 
411 		if (of_property_read_u32(da9121_matches[pdata->num_buck-1].of_node,
412 				"dlg,ripple-cancel", &ripple_cancel)) {
413 			if (pdata->num_buck > 1)
414 				ripple_reg = DA9xxx_REG_BUCK_BUCK2_7;
415 			else
416 				ripple_reg = DA9121_REG_BUCK_BUCK1_7;
417 
418 			ret = regmap_update_bits(chip->regmap, ripple_reg,
419 				DA9xxx_MASK_BUCK_BUCKx_7_CHx_RIPPLE_CANCEL,
420 				ripple_cancel);
421 			if (ret < 0)
422 				dev_err(chip->dev, "Cannot set ripple mode, err: %d\n", ret);
423 		}
424 	}
425 
426 	return 0;
427 }
428 
429 #define DA9121_MIN_MV		300
430 #define DA9121_MAX_MV		1900
431 #define DA9121_STEP_MV		10
432 #define DA9121_MIN_SEL		(DA9121_MIN_MV / DA9121_STEP_MV)
433 #define DA9121_N_VOLTAGES	(((DA9121_MAX_MV - DA9121_MIN_MV) / DA9121_STEP_MV) \
434 				 + 1 + DA9121_MIN_SEL)
435 
436 static const struct regulator_desc da9121_reg = {
437 	.id = DA9121_IDX_BUCK1,
438 	.name = "da9121",
439 	.of_match = "buck1",
440 	.of_parse_cb = da9121_of_parse_cb,
441 	.owner = THIS_MODULE,
442 	.regulators_node = "regulators",
443 	.of_map_mode = da9121_map_mode,
444 	.ops = &da9121_buck_ops,
445 	.type = REGULATOR_VOLTAGE,
446 	.n_voltages = DA9121_N_VOLTAGES,
447 	.min_uV = DA9121_MIN_MV * 1000,
448 	.uV_step = DA9121_STEP_MV * 1000,
449 	.linear_min_sel = DA9121_MIN_SEL,
450 	.vsel_reg = DA9121_REG_BUCK_BUCK1_5,
451 	.vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT,
452 	.enable_reg = DA9121_REG_BUCK_BUCK1_0,
453 	.enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN,
454 	/* Default value of BUCK_BUCK1_0.CH1_SRC_DVC_UP */
455 	.ramp_delay = 20000,
456 	/* tBUCK_EN */
457 	.enable_time = 20,
458 };
459 
460 static const struct regulator_desc da9220_reg[2] = {
461 	{
462 		.id = DA9121_IDX_BUCK1,
463 		.name = "DA9220/DA9132 BUCK1",
464 		.of_match = "buck1",
465 		.of_parse_cb = da9121_of_parse_cb,
466 		.owner = THIS_MODULE,
467 		.regulators_node = "regulators",
468 		.of_map_mode = da9121_map_mode,
469 		.ops = &da9121_buck_ops,
470 		.type = REGULATOR_VOLTAGE,
471 		.n_voltages = DA9121_N_VOLTAGES,
472 		.min_uV = DA9121_MIN_MV * 1000,
473 		.uV_step = DA9121_STEP_MV * 1000,
474 		.linear_min_sel = DA9121_MIN_SEL,
475 		.enable_reg = DA9121_REG_BUCK_BUCK1_0,
476 		.enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN,
477 		.vsel_reg = DA9121_REG_BUCK_BUCK1_5,
478 		.vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT,
479 	},
480 	{
481 		.id = DA9121_IDX_BUCK2,
482 		.name = "DA9220/DA9132 BUCK2",
483 		.of_match = "buck2",
484 		.of_parse_cb = da9121_of_parse_cb,
485 		.owner = THIS_MODULE,
486 		.regulators_node = "regulators",
487 		.of_map_mode = da9121_map_mode,
488 		.ops = &da9121_buck_ops,
489 		.type = REGULATOR_VOLTAGE,
490 		.n_voltages = DA9121_N_VOLTAGES,
491 		.min_uV = DA9121_MIN_MV * 1000,
492 		.uV_step = DA9121_STEP_MV * 1000,
493 		.linear_min_sel = DA9121_MIN_SEL,
494 		.enable_reg = DA9xxx_REG_BUCK_BUCK2_0,
495 		.enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN,
496 		.vsel_reg = DA9xxx_REG_BUCK_BUCK2_5,
497 		.vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT,
498 	}
499 };
500 
501 static const struct regulator_desc da9122_reg[2] = {
502 	{
503 		.id = DA9121_IDX_BUCK1,
504 		.name = "DA9122/DA9131 BUCK1",
505 		.of_match = "buck1",
506 		.of_parse_cb = da9121_of_parse_cb,
507 		.owner = THIS_MODULE,
508 		.regulators_node = "regulators",
509 		.of_map_mode = da9121_map_mode,
510 		.ops = &da9121_buck_ops,
511 		.type = REGULATOR_VOLTAGE,
512 		.n_voltages = DA9121_N_VOLTAGES,
513 		.min_uV = DA9121_MIN_MV * 1000,
514 		.uV_step = DA9121_STEP_MV * 1000,
515 		.linear_min_sel = DA9121_MIN_SEL,
516 		.enable_reg = DA9121_REG_BUCK_BUCK1_0,
517 		.enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN,
518 		.vsel_reg = DA9121_REG_BUCK_BUCK1_5,
519 		.vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT,
520 	},
521 	{
522 		.id = DA9121_IDX_BUCK2,
523 		.name = "DA9122/DA9131 BUCK2",
524 		.of_match = "buck2",
525 		.of_parse_cb = da9121_of_parse_cb,
526 		.owner = THIS_MODULE,
527 		.regulators_node = "regulators",
528 		.of_map_mode = da9121_map_mode,
529 		.ops = &da9121_buck_ops,
530 		.type = REGULATOR_VOLTAGE,
531 		.n_voltages = DA9121_N_VOLTAGES,
532 		.min_uV = DA9121_MIN_MV * 1000,
533 		.uV_step = DA9121_STEP_MV * 1000,
534 		.linear_min_sel = DA9121_MIN_SEL,
535 		.enable_reg = DA9xxx_REG_BUCK_BUCK2_0,
536 		.enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN,
537 		.vsel_reg = DA9xxx_REG_BUCK_BUCK2_5,
538 		.vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT,
539 	}
540 };
541 
542 static const struct regulator_desc da9217_reg = {
543 	.id = DA9121_IDX_BUCK1,
544 	.name = "DA9217 BUCK1",
545 	.of_match = "buck1",
546 	.of_parse_cb = da9121_of_parse_cb,
547 	.owner = THIS_MODULE,
548 	.regulators_node = "regulators",
549 	.of_map_mode = da9121_map_mode,
550 	.ops = &da9121_buck_ops,
551 	.type = REGULATOR_VOLTAGE,
552 	.n_voltages = DA9121_N_VOLTAGES,
553 	.min_uV = DA9121_MIN_MV * 1000,
554 	.uV_step = DA9121_STEP_MV * 1000,
555 	.linear_min_sel = DA9121_MIN_SEL,
556 	.enable_reg = DA9121_REG_BUCK_BUCK1_0,
557 	.enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN,
558 	.vsel_reg = DA9121_REG_BUCK_BUCK1_5,
559 	.vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT,
560 };
561 
562 #define DA914X_MIN_MV		500
563 #define DA914X_MAX_MV		1300
564 #define DA914X_STEP_MV		10
565 #define DA914X_MIN_SEL		(DA914X_MIN_MV / DA914X_STEP_MV)
566 #define DA914X_N_VOLTAGES	(((DA914X_MAX_MV - DA914X_MIN_MV) / DA914X_STEP_MV) \
567 				 + 1 + DA914X_MIN_SEL)
568 
569 static const struct regulator_desc da9141_reg = {
570 	.id = DA9121_IDX_BUCK1,
571 	.name = "DA9141",
572 	.of_match = "buck1",
573 	.of_parse_cb = da9121_of_parse_cb,
574 	.owner = THIS_MODULE,
575 	.regulators_node = "regulators",
576 	.of_map_mode = da9121_map_mode,
577 	.ops = &da9121_buck_ops,
578 	.type = REGULATOR_VOLTAGE,
579 	.n_voltages = DA914X_N_VOLTAGES,
580 	.min_uV = DA914X_MIN_MV * 1000,
581 	.uV_step = DA914X_STEP_MV * 1000,
582 	.linear_min_sel = DA914X_MIN_SEL,
583 	.vsel_reg = DA9121_REG_BUCK_BUCK1_5,
584 	.vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT,
585 	.enable_reg = DA9121_REG_BUCK_BUCK1_0,
586 	.enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN,
587 };
588 
589 static const struct regulator_desc da9142_reg = {
590 	.id = DA9121_IDX_BUCK1,
591 	.name = "DA9142 BUCK1",
592 	.of_match = "buck1",
593 	.of_parse_cb = da9121_of_parse_cb,
594 	.owner = THIS_MODULE,
595 	.regulators_node = "regulators",
596 	.of_map_mode = da9121_map_mode,
597 	.ops = &da9121_buck_ops,
598 	.type = REGULATOR_VOLTAGE,
599 	.n_voltages = DA914X_N_VOLTAGES,
600 	.min_uV = DA914X_MIN_MV * 1000,
601 	.uV_step = DA914X_STEP_MV * 1000,
602 	.linear_min_sel = DA914X_MIN_SEL,
603 	.enable_reg = DA9121_REG_BUCK_BUCK1_0,
604 	.enable_mask = DA9121_MASK_BUCK_BUCKx_0_CHx_EN,
605 	.vsel_reg = DA9121_REG_BUCK_BUCK1_5,
606 	.vsel_mask = DA9121_MASK_BUCK_BUCKx_5_CHx_A_VOUT,
607 };
608 
609 
610 static const struct regulator_desc *local_da9121_regulators[][DA9121_IDX_MAX] = {
611 	[DA9121_TYPE_DA9121_DA9130] = { &da9121_reg, NULL },
612 	[DA9121_TYPE_DA9220_DA9132] = { &da9220_reg[0], &da9220_reg[1] },
613 	[DA9121_TYPE_DA9122_DA9131] = { &da9122_reg[0], &da9122_reg[1] },
614 	[DA9121_TYPE_DA9217] = { &da9217_reg, NULL },
615 	[DA9121_TYPE_DA9141] = { &da9141_reg, NULL },
616 	[DA9121_TYPE_DA9142] = { &da9142_reg, NULL },
617 };
618 
619 static void da9121_status_poll_on(struct work_struct *work)
620 {
621 	struct da9121 *chip = container_of(work, struct da9121, work.work);
622 	int status[3] = {0};
623 	int clear[3] = {0};
624 	unsigned long delay;
625 	int i;
626 	int ret;
627 
628 	ret = regmap_bulk_read(chip->regmap, DA9121_REG_SYS_STATUS_0, status, 2);
629 	if (ret < 0) {
630 		dev_err(chip->dev,
631 			"Failed to read STATUS registers: %d\n", ret);
632 		goto error;
633 	}
634 
635 	/* Possible events are tested to be within range for the variant, potentially
636 	 * masked by the IRQ handler (not just warned about), as having been masked,
637 	 * and the respective state cleared - then flagged to unmask for next IRQ.
638 	 */
639 	for (i = 0; i < ARRAY_SIZE(status_event_handling); i++) {
640 		const struct status_event_data *item = &status_event_handling[i];
641 		int reg_idx = item->reg_index;
642 		bool relevant = (item->buck_id <= variant_parameters[chip->variant_id].num_bucks);
643 		bool supported = (item->warn == NULL);
644 		bool persisting = (chip->persistent[reg_idx] & item->event_bit);
645 		bool now_cleared = !(status[reg_idx] & item->status_bit);
646 
647 		if (relevant && supported && persisting && now_cleared) {
648 			clear[reg_idx] |= item->mask_bit;
649 			chip->persistent[reg_idx] &= ~item->event_bit;
650 		}
651 	}
652 
653 	for (i = 0; i < 2; i++) {
654 		if (clear[i]) {
655 			unsigned int reg = DA9121_REG_SYS_MASK_0 + i;
656 			unsigned int mbit = clear[i];
657 
658 			ret = regmap_update_bits(chip->regmap, reg, mbit, 0);
659 			if (ret < 0) {
660 				dev_err(chip->dev,
661 					"Failed to unmask 0x%02x %d\n",
662 					reg, ret);
663 				goto error;
664 			}
665 		}
666 	}
667 
668 	if (chip->persistent[0] | chip->persistent[1]) {
669 		delay = msecs_to_jiffies(chip->passive_delay);
670 		queue_delayed_work(system_freezable_wq, &chip->work, delay);
671 	}
672 
673 error:
674 	return;
675 }
676 
677 static irqreturn_t da9121_irq_handler(int irq, void *data)
678 {
679 	struct da9121 *chip = data;
680 	struct regulator_dev *rdev;
681 	int event[3] = {0};
682 	int handled[3] = {0};
683 	int mask[3] = {0};
684 	int ret = IRQ_NONE;
685 	int i;
686 	int err;
687 
688 	err = regmap_bulk_read(chip->regmap, DA9121_REG_SYS_EVENT_0, event, 3);
689 	if (err < 0) {
690 		dev_err(chip->dev, "Failed to read EVENT registers %d\n", err);
691 		ret = IRQ_NONE;
692 		goto error;
693 	}
694 
695 	err = regmap_bulk_read(chip->regmap, DA9121_REG_SYS_MASK_0, mask, 3);
696 	if (err < 0) {
697 		dev_err(chip->dev,
698 			"Failed to read MASK registers: %d\n", ret);
699 		ret = IRQ_NONE;
700 		goto error;
701 	}
702 
703 	rdev = chip->rdev[DA9121_IDX_BUCK1];
704 
705 	/* Possible events are tested to be within range for the variant, currently
706 	 * enabled, and having triggered this IRQ. The event may then be notified,
707 	 * or a warning given for unexpected events - those from device POR, and
708 	 * currently unsupported GPIO configurations.
709 	 */
710 	for (i = 0; i < ARRAY_SIZE(status_event_handling); i++) {
711 		const struct status_event_data *item = &status_event_handling[i];
712 		int reg_idx = item->reg_index;
713 		bool relevant = (item->buck_id <= variant_parameters[chip->variant_id].num_bucks);
714 		bool enabled = !(mask[reg_idx] & item->mask_bit);
715 		bool active = (event[reg_idx] & item->event_bit);
716 		bool notify = (item->warn == NULL);
717 
718 		if (relevant && enabled && active) {
719 			if (notify) {
720 				chip->persistent[reg_idx] |= item->event_bit;
721 				regulator_notifier_call_chain(rdev, item->notification, NULL);
722 			} else {
723 				dev_warn(chip->dev, item->warn);
724 				handled[reg_idx] |= item->event_bit;
725 				ret = IRQ_HANDLED;
726 			}
727 		}
728 	}
729 
730 	for (i = 0; i < 3; i++) {
731 		if (event[i] != handled[i]) {
732 			dev_warn(chip->dev,
733 				"Unhandled event(s) in bank%d 0x%02x\n", i,
734 				event[i] ^ handled[i]);
735 		}
736 	}
737 
738 	/* Mask the interrupts for persistent events OV, OC, UV, WARN, CRIT */
739 	for (i = 0; i < 2; i++) {
740 		if (handled[i]) {
741 			unsigned int reg = DA9121_REG_SYS_MASK_0 + i;
742 			unsigned int mbit = handled[i];
743 
744 			err = regmap_update_bits(chip->regmap, reg, mbit, mbit);
745 			if (err < 0) {
746 				dev_err(chip->dev,
747 					"Failed to mask 0x%02x interrupt %d\n",
748 					reg, err);
749 				ret = IRQ_NONE;
750 				goto error;
751 			}
752 		}
753 	}
754 
755 	/* clear the events */
756 	if (handled[0] | handled[1] | handled[2]) {
757 		err = regmap_bulk_write(chip->regmap, DA9121_REG_SYS_EVENT_0, handled, 3);
758 		if (err < 0) {
759 			dev_err(chip->dev, "Fail to write EVENTs %d\n", err);
760 			ret = IRQ_NONE;
761 			goto error;
762 		}
763 	}
764 
765 	queue_delayed_work(system_freezable_wq, &chip->work, 0);
766 error:
767 	return ret;
768 }
769 
770 static int da9121_set_regulator_config(struct da9121 *chip)
771 {
772 	struct regulator_config config = { };
773 	unsigned int max_matches = variant_parameters[chip->variant_id].num_bucks;
774 	int ret = 0;
775 	int i;
776 
777 	for (i = 0; i < max_matches; i++) {
778 		const struct regulator_desc *regl_desc =
779 			local_da9121_regulators[chip->variant_id][i];
780 
781 		config.dev = chip->dev;
782 		config.driver_data = chip;
783 		config.regmap = chip->regmap;
784 
785 		chip->rdev[i] = devm_regulator_register(chip->dev,
786 					regl_desc, &config);
787 		if (IS_ERR(chip->rdev[i])) {
788 			dev_err(chip->dev, "Failed to register regulator %s, %d/%d\n",
789 				regl_desc->name, (i+1), max_matches);
790 			ret = PTR_ERR(chip->rdev[i]);
791 			goto error;
792 		}
793 	}
794 
795 error:
796 	return ret;
797 }
798 
799 /* DA9121 chip register model */
800 static const struct regmap_range da9121_1ch_readable_ranges[] = {
801 	regmap_reg_range(DA9121_REG_SYS_STATUS_0, DA9121_REG_SYS_MASK_3),
802 	regmap_reg_range(DA9121_REG_SYS_CONFIG_2, DA9121_REG_SYS_CONFIG_3),
803 	regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1),
804 	regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_6),
805 	regmap_reg_range(DA9121_REG_OTP_DEVICE_ID, DA9121_REG_OTP_CONFIG_ID),
806 };
807 
808 static const struct regmap_access_table da9121_1ch_readable_table = {
809 	.yes_ranges = da9121_1ch_readable_ranges,
810 	.n_yes_ranges = ARRAY_SIZE(da9121_1ch_readable_ranges),
811 };
812 
813 static const struct regmap_range da9121_2ch_readable_ranges[] = {
814 	regmap_reg_range(DA9121_REG_SYS_STATUS_0, DA9121_REG_SYS_MASK_3),
815 	regmap_reg_range(DA9121_REG_SYS_CONFIG_2, DA9121_REG_SYS_CONFIG_3),
816 	regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1),
817 	regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_7),
818 	regmap_reg_range(DA9xxx_REG_BUCK_BUCK2_0, DA9xxx_REG_BUCK_BUCK2_7),
819 	regmap_reg_range(DA9121_REG_OTP_DEVICE_ID, DA9121_REG_OTP_CONFIG_ID),
820 };
821 
822 static const struct regmap_access_table da9121_2ch_readable_table = {
823 	.yes_ranges = da9121_2ch_readable_ranges,
824 	.n_yes_ranges = ARRAY_SIZE(da9121_2ch_readable_ranges),
825 };
826 
827 static const struct regmap_range da9121_1ch_writeable_ranges[] = {
828 	regmap_reg_range(DA9121_REG_SYS_EVENT_0, DA9121_REG_SYS_MASK_3),
829 	regmap_reg_range(DA9121_REG_SYS_CONFIG_2, DA9121_REG_SYS_CONFIG_3),
830 	regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1),
831 	regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_2),
832 	regmap_reg_range(DA9121_REG_BUCK_BUCK1_4, DA9121_REG_BUCK_BUCK1_6),
833 };
834 
835 static const struct regmap_access_table da9121_1ch_writeable_table = {
836 	.yes_ranges = da9121_1ch_writeable_ranges,
837 	.n_yes_ranges = ARRAY_SIZE(da9121_1ch_writeable_ranges),
838 };
839 
840 static const struct regmap_range da9121_2ch_writeable_ranges[] = {
841 	regmap_reg_range(DA9121_REG_SYS_EVENT_0, DA9121_REG_SYS_MASK_3),
842 	regmap_reg_range(DA9121_REG_SYS_CONFIG_2, DA9121_REG_SYS_CONFIG_3),
843 	regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1),
844 	regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_2),
845 	regmap_reg_range(DA9121_REG_BUCK_BUCK1_4, DA9121_REG_BUCK_BUCK1_7),
846 	regmap_reg_range(DA9xxx_REG_BUCK_BUCK2_0, DA9xxx_REG_BUCK_BUCK2_2),
847 	regmap_reg_range(DA9xxx_REG_BUCK_BUCK2_4, DA9xxx_REG_BUCK_BUCK2_7),
848 };
849 
850 static const struct regmap_access_table da9121_2ch_writeable_table = {
851 	.yes_ranges = da9121_2ch_writeable_ranges,
852 	.n_yes_ranges = ARRAY_SIZE(da9121_2ch_writeable_ranges),
853 };
854 
855 
856 static const struct regmap_range da9121_volatile_ranges[] = {
857 	regmap_reg_range(DA9121_REG_SYS_STATUS_0, DA9121_REG_SYS_EVENT_2),
858 	regmap_reg_range(DA9121_REG_SYS_GPIO0_0, DA9121_REG_SYS_GPIO2_1),
859 	regmap_reg_range(DA9121_REG_BUCK_BUCK1_0, DA9121_REG_BUCK_BUCK1_6),
860 };
861 
862 static const struct regmap_access_table da9121_volatile_table = {
863 	.yes_ranges = da9121_volatile_ranges,
864 	.n_yes_ranges = ARRAY_SIZE(da9121_volatile_ranges),
865 };
866 
867 /* DA9121 regmap config for 1 channel variants */
868 static const struct regmap_config da9121_1ch_regmap_config = {
869 	.reg_bits = 8,
870 	.val_bits = 8,
871 	.max_register = DA9121_REG_OTP_CONFIG_ID,
872 	.rd_table = &da9121_1ch_readable_table,
873 	.wr_table = &da9121_1ch_writeable_table,
874 	.volatile_table = &da9121_volatile_table,
875 	.cache_type = REGCACHE_MAPLE,
876 };
877 
878 /* DA9121 regmap config for 2 channel variants */
879 static const struct regmap_config da9121_2ch_regmap_config = {
880 	.reg_bits = 8,
881 	.val_bits = 8,
882 	.max_register = DA9121_REG_OTP_CONFIG_ID,
883 	.rd_table = &da9121_2ch_readable_table,
884 	.wr_table = &da9121_2ch_writeable_table,
885 	.volatile_table = &da9121_volatile_table,
886 	.cache_type = REGCACHE_MAPLE,
887 };
888 
889 static int da9121_check_device_type(struct i2c_client *i2c, struct da9121 *chip)
890 {
891 	u32 device_id;
892 	u32 variant_id;
893 	u8 variant_mrc, variant_vrc;
894 	char *type;
895 	bool config_match = false;
896 	int ret = 0;
897 
898 	ret = regmap_read(chip->regmap, DA9121_REG_OTP_DEVICE_ID, &device_id);
899 	if (ret < 0) {
900 		dev_err(chip->dev, "Cannot read device ID: %d\n", ret);
901 		goto error;
902 	}
903 
904 	ret = regmap_read(chip->regmap, DA9121_REG_OTP_VARIANT_ID, &variant_id);
905 	if (ret < 0) {
906 		dev_err(chip->dev, "Cannot read variant ID: %d\n", ret);
907 		goto error;
908 	}
909 
910 	if ((device_id != DA9121_DEVICE_ID) && (device_id != DA914x_DEVICE_ID)) {
911 		dev_err(chip->dev, "Invalid device ID: 0x%02x\n", device_id);
912 		ret = -ENODEV;
913 		goto error;
914 	}
915 
916 	variant_vrc = variant_id & DA9121_MASK_OTP_VARIANT_ID_VRC;
917 
918 	switch (chip->subvariant_id) {
919 	case DA9121_SUBTYPE_DA9121:
920 		type = "DA9121";
921 		config_match = (variant_vrc == DA9121_VARIANT_VRC);
922 		break;
923 	case DA9121_SUBTYPE_DA9130:
924 		type = "DA9130";
925 		config_match = (variant_vrc == DA9130_VARIANT_VRC);
926 		break;
927 	case DA9121_SUBTYPE_DA9220:
928 		type = "DA9220";
929 		config_match = (variant_vrc == DA9220_VARIANT_VRC);
930 		break;
931 	case DA9121_SUBTYPE_DA9132:
932 		type = "DA9132";
933 		config_match = (variant_vrc == DA9132_VARIANT_VRC);
934 		break;
935 	case DA9121_SUBTYPE_DA9122:
936 		type = "DA9122";
937 		config_match = (variant_vrc == DA9122_VARIANT_VRC);
938 		break;
939 	case DA9121_SUBTYPE_DA9131:
940 		type = "DA9131";
941 		config_match = (variant_vrc == DA9131_VARIANT_VRC);
942 		break;
943 	case DA9121_SUBTYPE_DA9217:
944 		type = "DA9217";
945 		config_match = (variant_vrc == DA9217_VARIANT_VRC);
946 		break;
947 	default:
948 		type = "Unknown";
949 		break;
950 	}
951 
952 	if (device_id == DA914x_DEVICE_ID) {
953 		switch (chip->subvariant_id) {
954 		case DA9121_SUBTYPE_DA9141:
955 			type = "DA9141";
956 			config_match = (variant_vrc == DA9141_VARIANT_VRC);
957 			break;
958 		case DA9121_SUBTYPE_DA9142:
959 			type = "DA9142";
960 			config_match = (variant_vrc == DA9142_VARIANT_VRC);
961 			break;
962 		default:
963 			type = "Unknown";
964 			break;
965 		}
966 	}
967 
968 	dev_info(chip->dev,
969 		 "Device detected (device-ID: 0x%02X, var-ID: 0x%02X, %s)\n",
970 		 device_id, variant_id, type);
971 
972 	if (!config_match) {
973 		dev_err(chip->dev, "Device tree configuration does not match detected device.\n");
974 		ret = -EINVAL;
975 		goto error;
976 	}
977 
978 	variant_mrc = (variant_id & DA9121_MASK_OTP_VARIANT_ID_MRC)
979 			>> DA9121_SHIFT_OTP_VARIANT_ID_MRC;
980 
981 	if (((device_id == DA9121_DEVICE_ID) &&
982 	     (variant_mrc < DA9121_VARIANT_MRC_BASE)) ||
983 	    ((device_id == DA914x_DEVICE_ID) &&
984 	     (variant_mrc != DA914x_VARIANT_MRC_BASE))) {
985 		dev_err(chip->dev,
986 			"Cannot support variant MRC: 0x%02X\n", variant_mrc);
987 		ret = -EINVAL;
988 	}
989 error:
990 	return ret;
991 }
992 
993 static int da9121_assign_chip_model(struct i2c_client *i2c,
994 			struct da9121 *chip)
995 {
996 	const struct regmap_config *regmap;
997 	int ret = 0;
998 
999 	chip->dev = &i2c->dev;
1000 
1001 	/* Use configured subtype to select the regulator descriptor index and
1002 	 * register map, common to both consumer and automotive grade variants
1003 	 */
1004 	switch (chip->subvariant_id) {
1005 	case DA9121_SUBTYPE_DA9121:
1006 	case DA9121_SUBTYPE_DA9130:
1007 		chip->variant_id = DA9121_TYPE_DA9121_DA9130;
1008 		regmap = &da9121_1ch_regmap_config;
1009 		break;
1010 	case DA9121_SUBTYPE_DA9217:
1011 		chip->variant_id = DA9121_TYPE_DA9217;
1012 		regmap = &da9121_1ch_regmap_config;
1013 		break;
1014 	case DA9121_SUBTYPE_DA9122:
1015 	case DA9121_SUBTYPE_DA9131:
1016 		chip->variant_id = DA9121_TYPE_DA9122_DA9131;
1017 		regmap = &da9121_2ch_regmap_config;
1018 		break;
1019 	case DA9121_SUBTYPE_DA9220:
1020 	case DA9121_SUBTYPE_DA9132:
1021 		chip->variant_id = DA9121_TYPE_DA9220_DA9132;
1022 		regmap = &da9121_2ch_regmap_config;
1023 		break;
1024 	case DA9121_SUBTYPE_DA9141:
1025 		chip->variant_id = DA9121_TYPE_DA9141;
1026 		regmap = &da9121_1ch_regmap_config;
1027 		break;
1028 	case DA9121_SUBTYPE_DA9142:
1029 		chip->variant_id = DA9121_TYPE_DA9142;
1030 		regmap = &da9121_2ch_regmap_config;
1031 		break;
1032 	default:
1033 		return -EINVAL;
1034 	}
1035 
1036 	/* Set these up for of_regulator_match call which may want .of_map_modes */
1037 	da9121_matches[0].desc = local_da9121_regulators[chip->variant_id][0];
1038 	da9121_matches[1].desc = local_da9121_regulators[chip->variant_id][1];
1039 
1040 	chip->regmap = devm_regmap_init_i2c(i2c, regmap);
1041 	if (IS_ERR(chip->regmap)) {
1042 		ret = PTR_ERR(chip->regmap);
1043 		dev_err(chip->dev, "Failed to configure a register map: %d\n",
1044 			ret);
1045 		return ret;
1046 	}
1047 
1048 	ret = da9121_check_device_type(i2c, chip);
1049 
1050 	return ret;
1051 }
1052 
1053 static int da9121_config_irq(struct i2c_client *i2c,
1054 			struct da9121 *chip)
1055 {
1056 	unsigned int p_delay = DA9121_DEFAULT_POLLING_PERIOD_MS;
1057 	const int mask_all[4] = { 0, 0, 0xFF, 0xFF };
1058 	int ret = 0;
1059 
1060 	chip->chip_irq = i2c->irq;
1061 
1062 	if (chip->chip_irq != 0) {
1063 		if (!of_property_read_u32(chip->dev->of_node,
1064 					  "dlg,irq-polling-delay-passive-ms",
1065 					  &p_delay)) {
1066 			if (p_delay < DA9121_MIN_POLLING_PERIOD_MS ||
1067 			    p_delay > DA9121_MAX_POLLING_PERIOD_MS) {
1068 				dev_warn(chip->dev,
1069 					 "Out-of-range polling period %d ms\n",
1070 					 p_delay);
1071 				p_delay = DA9121_DEFAULT_POLLING_PERIOD_MS;
1072 			}
1073 		}
1074 
1075 		chip->passive_delay = p_delay;
1076 
1077 		ret = request_threaded_irq(chip->chip_irq, NULL,
1078 					da9121_irq_handler,
1079 					IRQF_TRIGGER_LOW|IRQF_ONESHOT,
1080 					"da9121", chip);
1081 		if (ret != 0) {
1082 			dev_err(chip->dev, "Failed IRQ request: %d\n",
1083 				chip->chip_irq);
1084 			goto error;
1085 		}
1086 
1087 		ret = regmap_bulk_write(chip->regmap, DA9121_REG_SYS_MASK_0, mask_all, 4);
1088 		if (ret != 0) {
1089 			dev_err(chip->dev, "Failed to set IRQ masks: %d\n",
1090 				ret);
1091 			goto regmap_error;
1092 		}
1093 
1094 		INIT_DELAYED_WORK(&chip->work, da9121_status_poll_on);
1095 		dev_info(chip->dev, "Interrupt polling period set at %d ms\n",
1096 			 chip->passive_delay);
1097 	}
1098 error:
1099 	return ret;
1100 regmap_error:
1101 	free_irq(chip->chip_irq, chip);
1102 	return ret;
1103 }
1104 
1105 static const struct of_device_id da9121_dt_ids[] = {
1106 	{ .compatible = "dlg,da9121", .data = (void *) DA9121_SUBTYPE_DA9121 },
1107 	{ .compatible = "dlg,da9130", .data = (void *) DA9121_SUBTYPE_DA9130 },
1108 	{ .compatible = "dlg,da9217", .data = (void *) DA9121_SUBTYPE_DA9217 },
1109 	{ .compatible = "dlg,da9122", .data = (void *) DA9121_SUBTYPE_DA9122 },
1110 	{ .compatible = "dlg,da9131", .data = (void *) DA9121_SUBTYPE_DA9131 },
1111 	{ .compatible = "dlg,da9220", .data = (void *) DA9121_SUBTYPE_DA9220 },
1112 	{ .compatible = "dlg,da9132", .data = (void *) DA9121_SUBTYPE_DA9132 },
1113 	{ .compatible = "dlg,da9141", .data = (void *) DA9121_SUBTYPE_DA9141 },
1114 	{ .compatible = "dlg,da9142", .data = (void *) DA9121_SUBTYPE_DA9142 },
1115 	{ }
1116 };
1117 MODULE_DEVICE_TABLE(of, da9121_dt_ids);
1118 
1119 static int da9121_i2c_probe(struct i2c_client *i2c)
1120 {
1121 	struct da9121 *chip;
1122 	const int mask_all[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
1123 	int ret = 0;
1124 
1125 	chip = devm_kzalloc(&i2c->dev, sizeof(struct da9121), GFP_KERNEL);
1126 	if (!chip) {
1127 		ret = -ENOMEM;
1128 		goto error;
1129 	}
1130 
1131 	chip->pdata = i2c->dev.platform_data;
1132 	chip->subvariant_id = (enum da9121_subvariant)i2c_get_match_data(i2c);
1133 
1134 	ret = da9121_assign_chip_model(i2c, chip);
1135 	if (ret < 0)
1136 		goto error;
1137 
1138 	ret = regmap_bulk_write(chip->regmap, DA9121_REG_SYS_MASK_0, mask_all, 4);
1139 	if (ret != 0) {
1140 		dev_err(chip->dev, "Failed to set IRQ masks: %d\n", ret);
1141 		goto error;
1142 	}
1143 
1144 	ret = da9121_set_regulator_config(chip);
1145 	if (ret < 0)
1146 		goto error;
1147 
1148 	ret = da9121_config_irq(i2c, chip);
1149 
1150 error:
1151 	return ret;
1152 }
1153 
1154 static void da9121_i2c_remove(struct i2c_client *i2c)
1155 {
1156 	struct da9121 *chip = i2c_get_clientdata(i2c);
1157 	const int mask_all[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
1158 	int ret;
1159 
1160 	free_irq(chip->chip_irq, chip);
1161 	cancel_delayed_work_sync(&chip->work);
1162 
1163 	ret = regmap_bulk_write(chip->regmap, DA9121_REG_SYS_MASK_0, mask_all, 4);
1164 	if (ret != 0)
1165 		dev_err(chip->dev, "Failed to set IRQ masks: %d\n", ret);
1166 }
1167 
1168 static const struct i2c_device_id da9121_i2c_id[] = {
1169 	{"da9121", DA9121_TYPE_DA9121_DA9130},
1170 	{"da9130", DA9121_TYPE_DA9121_DA9130},
1171 	{"da9217", DA9121_TYPE_DA9217},
1172 	{"da9122", DA9121_TYPE_DA9122_DA9131},
1173 	{"da9131", DA9121_TYPE_DA9122_DA9131},
1174 	{"da9220", DA9121_TYPE_DA9220_DA9132},
1175 	{"da9132", DA9121_TYPE_DA9220_DA9132},
1176 	{"da9141", DA9121_TYPE_DA9141},
1177 	{"da9142", DA9121_TYPE_DA9142},
1178 	{},
1179 };
1180 MODULE_DEVICE_TABLE(i2c, da9121_i2c_id);
1181 
1182 static struct i2c_driver da9121_regulator_driver = {
1183 	.driver = {
1184 		.name = "da9121",
1185 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1186 		.of_match_table = da9121_dt_ids,
1187 	},
1188 	.probe = da9121_i2c_probe,
1189 	.remove = da9121_i2c_remove,
1190 	.id_table = da9121_i2c_id,
1191 };
1192 
1193 module_i2c_driver(da9121_regulator_driver);
1194 
1195 MODULE_DESCRIPTION("Dialog Semiconductor DA9121/DA9122/DA9220/DA9217/DA9130/DA9131/DA9132 regulator driver");
1196 MODULE_LICENSE("GPL v2");
1197