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