1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2020 NXP.
4  * NXP PCA9450 pmic driver
5  */
6 
7 #include <linux/err.h>
8 #include <linux/i2c.h>
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/driver.h>
16 #include <linux/regulator/machine.h>
17 #include <linux/regulator/of_regulator.h>
18 #include <linux/regulator/pca9450.h>
19 
20 struct pc9450_dvs_config {
21 	unsigned int run_reg; /* dvs0 */
22 	unsigned int run_mask;
23 	unsigned int standby_reg; /* dvs1 */
24 	unsigned int standby_mask;
25 };
26 
27 struct pca9450_regulator_desc {
28 	struct regulator_desc desc;
29 	const struct pc9450_dvs_config dvs;
30 };
31 
32 struct pca9450 {
33 	struct device *dev;
34 	struct regmap *regmap;
35 	enum pca9450_chip_type type;
36 	unsigned int rcnt;
37 	int irq;
38 };
39 
40 static const struct regmap_range pca9450_status_range = {
41 	.range_min = PCA9450_REG_INT1,
42 	.range_max = PCA9450_REG_PWRON_STAT,
43 };
44 
45 static const struct regmap_access_table pca9450_volatile_regs = {
46 	.yes_ranges = &pca9450_status_range,
47 	.n_yes_ranges = 1,
48 };
49 
50 static const struct regmap_config pca9450_regmap_config = {
51 	.reg_bits = 8,
52 	.val_bits = 8,
53 	.volatile_table = &pca9450_volatile_regs,
54 	.max_register = PCA9450_MAX_REGISTER - 1,
55 	.cache_type = REGCACHE_RBTREE,
56 };
57 
58 /*
59  * BUCK1/2/3
60  * BUCK1RAM[1:0] BUCK1 DVS ramp rate setting
61  * 00: 25mV/1usec
62  * 01: 25mV/2usec
63  * 10: 25mV/4usec
64  * 11: 25mV/8usec
65  */
66 static int pca9450_dvs_set_ramp_delay(struct regulator_dev *rdev,
67 				      int ramp_delay)
68 {
69 	int id = rdev_get_id(rdev);
70 	unsigned int ramp_value;
71 
72 	switch (ramp_delay) {
73 	case 1 ... 3125:
74 		ramp_value = BUCK1_RAMP_3P125MV;
75 		break;
76 	case 3126 ... 6250:
77 		ramp_value = BUCK1_RAMP_6P25MV;
78 		break;
79 	case 6251 ... 12500:
80 		ramp_value = BUCK1_RAMP_12P5MV;
81 		break;
82 	case 12501 ... 25000:
83 		ramp_value = BUCK1_RAMP_25MV;
84 		break;
85 	default:
86 		ramp_value = BUCK1_RAMP_25MV;
87 	}
88 
89 	return regmap_update_bits(rdev->regmap, PCA9450_REG_BUCK1CTRL + id * 3,
90 				  BUCK1_RAMP_MASK, ramp_value << 6);
91 }
92 
93 static struct regulator_ops pca9450_dvs_buck_regulator_ops = {
94 	.enable = regulator_enable_regmap,
95 	.disable = regulator_disable_regmap,
96 	.is_enabled = regulator_is_enabled_regmap,
97 	.list_voltage = regulator_list_voltage_linear_range,
98 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
99 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
100 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
101 	.set_ramp_delay = pca9450_dvs_set_ramp_delay,
102 };
103 
104 static struct regulator_ops pca9450_buck_regulator_ops = {
105 	.enable = regulator_enable_regmap,
106 	.disable = regulator_disable_regmap,
107 	.is_enabled = regulator_is_enabled_regmap,
108 	.list_voltage = regulator_list_voltage_linear_range,
109 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
110 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
111 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
112 };
113 
114 static struct regulator_ops pca9450_ldo_regulator_ops = {
115 	.enable = regulator_enable_regmap,
116 	.disable = regulator_disable_regmap,
117 	.is_enabled = regulator_is_enabled_regmap,
118 	.list_voltage = regulator_list_voltage_linear_range,
119 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
120 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
121 };
122 
123 /*
124  * BUCK1/2/3
125  * 0.60 to 2.1875V (12.5mV step)
126  */
127 static const struct linear_range pca9450_dvs_buck_volts[] = {
128 	REGULATOR_LINEAR_RANGE(600000,  0x00, 0x7F, 12500),
129 };
130 
131 /*
132  * BUCK4/5/6
133  * 0.6V to 3.4V (25mV step)
134  */
135 static const struct linear_range pca9450_buck_volts[] = {
136 	REGULATOR_LINEAR_RANGE(600000, 0x00, 0x70, 25000),
137 	REGULATOR_LINEAR_RANGE(3400000, 0x71, 0x7F, 0),
138 };
139 
140 /*
141  * LDO1
142  * 1.6 to 3.3V ()
143  */
144 static const struct linear_range pca9450_ldo1_volts[] = {
145 	REGULATOR_LINEAR_RANGE(1600000, 0x00, 0x03, 100000),
146 	REGULATOR_LINEAR_RANGE(3000000, 0x04, 0x07, 100000),
147 };
148 
149 /*
150  * LDO2
151  * 0.8 to 1.15V (50mV step)
152  */
153 static const struct linear_range pca9450_ldo2_volts[] = {
154 	REGULATOR_LINEAR_RANGE(800000, 0x00, 0x07, 50000),
155 };
156 
157 /*
158  * LDO3/4
159  * 0.8 to 3.3V (100mV step)
160  */
161 static const struct linear_range pca9450_ldo34_volts[] = {
162 	REGULATOR_LINEAR_RANGE(800000, 0x00, 0x19, 100000),
163 	REGULATOR_LINEAR_RANGE(3300000, 0x1A, 0x1F, 0),
164 };
165 
166 /*
167  * LDO5
168  * 1.8 to 3.3V (100mV step)
169  */
170 static const struct linear_range pca9450_ldo5_volts[] = {
171 	REGULATOR_LINEAR_RANGE(1800000,  0x00, 0x0F, 100000),
172 };
173 
174 static int buck_set_dvs(const struct regulator_desc *desc,
175 			struct device_node *np, struct regmap *regmap,
176 			char *prop, unsigned int reg, unsigned int mask)
177 {
178 	int ret, i;
179 	uint32_t uv;
180 
181 	ret = of_property_read_u32(np, prop, &uv);
182 	if (ret == -EINVAL)
183 		return 0;
184 	else if (ret)
185 		return ret;
186 
187 	for (i = 0; i < desc->n_voltages; i++) {
188 		ret = regulator_desc_list_voltage_linear_range(desc, i);
189 		if (ret < 0)
190 			continue;
191 		if (ret == uv) {
192 			i <<= ffs(desc->vsel_mask) - 1;
193 			ret = regmap_update_bits(regmap, reg, mask, i);
194 			break;
195 		}
196 	}
197 
198 	return ret;
199 }
200 
201 static int pca9450_set_dvs_levels(struct device_node *np,
202 			    const struct regulator_desc *desc,
203 			    struct regulator_config *cfg)
204 {
205 	struct pca9450_regulator_desc *data = container_of(desc,
206 					struct pca9450_regulator_desc, desc);
207 	const struct pc9450_dvs_config *dvs = &data->dvs;
208 	unsigned int reg, mask;
209 	char *prop;
210 	int i, ret = 0;
211 
212 	for (i = 0; i < PCA9450_DVS_LEVEL_MAX; i++) {
213 		switch (i) {
214 		case PCA9450_DVS_LEVEL_RUN:
215 			prop = "nxp,dvs-run-voltage";
216 			reg = dvs->run_reg;
217 			mask = dvs->run_mask;
218 			break;
219 		case PCA9450_DVS_LEVEL_STANDBY:
220 			prop = "nxp,dvs-standby-voltage";
221 			reg = dvs->standby_reg;
222 			mask = dvs->standby_mask;
223 			break;
224 		default:
225 			return -EINVAL;
226 		}
227 
228 		ret = buck_set_dvs(desc, np, cfg->regmap, prop, reg, mask);
229 		if (ret)
230 			break;
231 	}
232 
233 	return ret;
234 }
235 
236 static const struct pca9450_regulator_desc pca9450a_regulators[] = {
237 	{
238 		.desc = {
239 			.name = "buck1",
240 			.of_match = of_match_ptr("BUCK1"),
241 			.regulators_node = of_match_ptr("regulators"),
242 			.id = PCA9450_BUCK1,
243 			.ops = &pca9450_dvs_buck_regulator_ops,
244 			.type = REGULATOR_VOLTAGE,
245 			.n_voltages = PCA9450_BUCK1_VOLTAGE_NUM,
246 			.linear_ranges = pca9450_dvs_buck_volts,
247 			.n_linear_ranges = ARRAY_SIZE(pca9450_dvs_buck_volts),
248 			.vsel_reg = PCA9450_REG_BUCK1OUT_DVS0,
249 			.vsel_mask = BUCK1OUT_DVS0_MASK,
250 			.enable_reg = PCA9450_REG_BUCK1CTRL,
251 			.enable_mask = BUCK1_ENMODE_MASK,
252 			.owner = THIS_MODULE,
253 			.of_parse_cb = pca9450_set_dvs_levels,
254 		},
255 		.dvs = {
256 			.run_reg = PCA9450_REG_BUCK1OUT_DVS0,
257 			.run_mask = BUCK1OUT_DVS0_MASK,
258 			.standby_reg = PCA9450_REG_BUCK1OUT_DVS1,
259 			.standby_mask = BUCK1OUT_DVS1_MASK,
260 		},
261 	},
262 	{
263 		.desc = {
264 			.name = "buck2",
265 			.of_match = of_match_ptr("BUCK2"),
266 			.regulators_node = of_match_ptr("regulators"),
267 			.id = PCA9450_BUCK2,
268 			.ops = &pca9450_dvs_buck_regulator_ops,
269 			.type = REGULATOR_VOLTAGE,
270 			.n_voltages = PCA9450_BUCK2_VOLTAGE_NUM,
271 			.linear_ranges = pca9450_dvs_buck_volts,
272 			.n_linear_ranges = ARRAY_SIZE(pca9450_dvs_buck_volts),
273 			.vsel_reg = PCA9450_REG_BUCK2OUT_DVS0,
274 			.vsel_mask = BUCK2OUT_DVS0_MASK,
275 			.enable_reg = PCA9450_REG_BUCK2CTRL,
276 			.enable_mask = BUCK1_ENMODE_MASK,
277 			.owner = THIS_MODULE,
278 			.of_parse_cb = pca9450_set_dvs_levels,
279 		},
280 		.dvs = {
281 			.run_reg = PCA9450_REG_BUCK2OUT_DVS0,
282 			.run_mask = BUCK2OUT_DVS0_MASK,
283 			.standby_reg = PCA9450_REG_BUCK2OUT_DVS1,
284 			.standby_mask = BUCK2OUT_DVS1_MASK,
285 		},
286 	},
287 	{
288 		.desc = {
289 			.name = "buck3",
290 			.of_match = of_match_ptr("BUCK3"),
291 			.regulators_node = of_match_ptr("regulators"),
292 			.id = PCA9450_BUCK3,
293 			.ops = &pca9450_dvs_buck_regulator_ops,
294 			.type = REGULATOR_VOLTAGE,
295 			.n_voltages = PCA9450_BUCK3_VOLTAGE_NUM,
296 			.linear_ranges = pca9450_dvs_buck_volts,
297 			.n_linear_ranges = ARRAY_SIZE(pca9450_dvs_buck_volts),
298 			.vsel_reg = PCA9450_REG_BUCK3OUT_DVS0,
299 			.vsel_mask = BUCK3OUT_DVS0_MASK,
300 			.enable_reg = PCA9450_REG_BUCK3CTRL,
301 			.enable_mask = BUCK3_ENMODE_MASK,
302 			.owner = THIS_MODULE,
303 			.of_parse_cb = pca9450_set_dvs_levels,
304 		},
305 		.dvs = {
306 			.run_reg = PCA9450_REG_BUCK3OUT_DVS0,
307 			.run_mask = BUCK3OUT_DVS0_MASK,
308 			.standby_reg = PCA9450_REG_BUCK3OUT_DVS1,
309 			.standby_mask = BUCK3OUT_DVS1_MASK,
310 		},
311 	},
312 	{
313 		.desc = {
314 			.name = "buck4",
315 			.of_match = of_match_ptr("BUCK4"),
316 			.regulators_node = of_match_ptr("regulators"),
317 			.id = PCA9450_BUCK4,
318 			.ops = &pca9450_buck_regulator_ops,
319 			.type = REGULATOR_VOLTAGE,
320 			.n_voltages = PCA9450_BUCK4_VOLTAGE_NUM,
321 			.linear_ranges = pca9450_buck_volts,
322 			.n_linear_ranges = ARRAY_SIZE(pca9450_buck_volts),
323 			.vsel_reg = PCA9450_REG_BUCK4OUT,
324 			.vsel_mask = BUCK4OUT_MASK,
325 			.enable_reg = PCA9450_REG_BUCK4CTRL,
326 			.enable_mask = BUCK4_ENMODE_MASK,
327 			.owner = THIS_MODULE,
328 		},
329 	},
330 	{
331 		.desc = {
332 			.name = "buck5",
333 			.of_match = of_match_ptr("BUCK5"),
334 			.regulators_node = of_match_ptr("regulators"),
335 			.id = PCA9450_BUCK5,
336 			.ops = &pca9450_buck_regulator_ops,
337 			.type = REGULATOR_VOLTAGE,
338 			.n_voltages = PCA9450_BUCK5_VOLTAGE_NUM,
339 			.linear_ranges = pca9450_buck_volts,
340 			.n_linear_ranges = ARRAY_SIZE(pca9450_buck_volts),
341 			.vsel_reg = PCA9450_REG_BUCK5OUT,
342 			.vsel_mask = BUCK5OUT_MASK,
343 			.enable_reg = PCA9450_REG_BUCK5CTRL,
344 			.enable_mask = BUCK5_ENMODE_MASK,
345 			.owner = THIS_MODULE,
346 		},
347 	},
348 	{
349 		.desc = {
350 			.name = "buck6",
351 			.of_match = of_match_ptr("BUCK6"),
352 			.regulators_node = of_match_ptr("regulators"),
353 			.id = PCA9450_BUCK6,
354 			.ops = &pca9450_buck_regulator_ops,
355 			.type = REGULATOR_VOLTAGE,
356 			.n_voltages = PCA9450_BUCK6_VOLTAGE_NUM,
357 			.linear_ranges = pca9450_buck_volts,
358 			.n_linear_ranges = ARRAY_SIZE(pca9450_buck_volts),
359 			.vsel_reg = PCA9450_REG_BUCK6OUT,
360 			.vsel_mask = BUCK6OUT_MASK,
361 			.enable_reg = PCA9450_REG_BUCK6CTRL,
362 			.enable_mask = BUCK6_ENMODE_MASK,
363 			.owner = THIS_MODULE,
364 		},
365 	},
366 	{
367 		.desc = {
368 			.name = "ldo1",
369 			.of_match = of_match_ptr("LDO1"),
370 			.regulators_node = of_match_ptr("regulators"),
371 			.id = PCA9450_LDO1,
372 			.ops = &pca9450_ldo_regulator_ops,
373 			.type = REGULATOR_VOLTAGE,
374 			.n_voltages = PCA9450_LDO1_VOLTAGE_NUM,
375 			.linear_ranges = pca9450_ldo1_volts,
376 			.n_linear_ranges = ARRAY_SIZE(pca9450_ldo1_volts),
377 			.vsel_reg = PCA9450_REG_LDO1CTRL,
378 			.vsel_mask = LDO1OUT_MASK,
379 			.enable_reg = PCA9450_REG_LDO1CTRL,
380 			.enable_mask = LDO1_EN_MASK,
381 			.owner = THIS_MODULE,
382 		},
383 	},
384 	{
385 		.desc = {
386 			.name = "ldo2",
387 			.of_match = of_match_ptr("LDO2"),
388 			.regulators_node = of_match_ptr("regulators"),
389 			.id = PCA9450_LDO2,
390 			.ops = &pca9450_ldo_regulator_ops,
391 			.type = REGULATOR_VOLTAGE,
392 			.n_voltages = PCA9450_LDO2_VOLTAGE_NUM,
393 			.linear_ranges = pca9450_ldo2_volts,
394 			.n_linear_ranges = ARRAY_SIZE(pca9450_ldo2_volts),
395 			.vsel_reg = PCA9450_REG_LDO2CTRL,
396 			.vsel_mask = LDO2OUT_MASK,
397 			.enable_reg = PCA9450_REG_LDO2CTRL,
398 			.enable_mask = LDO2_EN_MASK,
399 			.owner = THIS_MODULE,
400 		},
401 	},
402 	{
403 		.desc = {
404 			.name = "ldo3",
405 			.of_match = of_match_ptr("LDO3"),
406 			.regulators_node = of_match_ptr("regulators"),
407 			.id = PCA9450_LDO3,
408 			.ops = &pca9450_ldo_regulator_ops,
409 			.type = REGULATOR_VOLTAGE,
410 			.n_voltages = PCA9450_LDO3_VOLTAGE_NUM,
411 			.linear_ranges = pca9450_ldo34_volts,
412 			.n_linear_ranges = ARRAY_SIZE(pca9450_ldo34_volts),
413 			.vsel_reg = PCA9450_REG_LDO3CTRL,
414 			.vsel_mask = LDO3OUT_MASK,
415 			.enable_reg = PCA9450_REG_LDO3CTRL,
416 			.enable_mask = LDO3_EN_MASK,
417 			.owner = THIS_MODULE,
418 		},
419 	},
420 	{
421 		.desc = {
422 			.name = "ldo4",
423 			.of_match = of_match_ptr("LDO4"),
424 			.regulators_node = of_match_ptr("regulators"),
425 			.id = PCA9450_LDO4,
426 			.ops = &pca9450_ldo_regulator_ops,
427 			.type = REGULATOR_VOLTAGE,
428 			.n_voltages = PCA9450_LDO4_VOLTAGE_NUM,
429 			.linear_ranges = pca9450_ldo34_volts,
430 			.n_linear_ranges = ARRAY_SIZE(pca9450_ldo34_volts),
431 			.vsel_reg = PCA9450_REG_LDO4CTRL,
432 			.vsel_mask = LDO4OUT_MASK,
433 			.enable_reg = PCA9450_REG_LDO4CTRL,
434 			.enable_mask = LDO4_EN_MASK,
435 			.owner = THIS_MODULE,
436 		},
437 	},
438 	{
439 		.desc = {
440 			.name = "ldo5",
441 			.of_match = of_match_ptr("LDO5"),
442 			.regulators_node = of_match_ptr("regulators"),
443 			.id = PCA9450_LDO5,
444 			.ops = &pca9450_ldo_regulator_ops,
445 			.type = REGULATOR_VOLTAGE,
446 			.n_voltages = PCA9450_LDO5_VOLTAGE_NUM,
447 			.linear_ranges = pca9450_ldo5_volts,
448 			.n_linear_ranges = ARRAY_SIZE(pca9450_ldo5_volts),
449 			.vsel_reg = PCA9450_REG_LDO5CTRL_H,
450 			.vsel_mask = LDO5HOUT_MASK,
451 			.enable_reg = PCA9450_REG_LDO5CTRL_H,
452 			.enable_mask = LDO5H_EN_MASK,
453 			.owner = THIS_MODULE,
454 		},
455 	},
456 };
457 
458 /*
459  * Buck3 removed on PCA9450B and connected with Buck1 internal for dual phase
460  * on PCA9450C as no Buck3.
461  */
462 static const struct pca9450_regulator_desc pca9450bc_regulators[] = {
463 	{
464 		.desc = {
465 			.name = "buck1",
466 			.of_match = of_match_ptr("BUCK1"),
467 			.regulators_node = of_match_ptr("regulators"),
468 			.id = PCA9450_BUCK1,
469 			.ops = &pca9450_dvs_buck_regulator_ops,
470 			.type = REGULATOR_VOLTAGE,
471 			.n_voltages = PCA9450_BUCK1_VOLTAGE_NUM,
472 			.linear_ranges = pca9450_dvs_buck_volts,
473 			.n_linear_ranges = ARRAY_SIZE(pca9450_dvs_buck_volts),
474 			.vsel_reg = PCA9450_REG_BUCK1OUT_DVS0,
475 			.vsel_mask = BUCK1OUT_DVS0_MASK,
476 			.enable_reg = PCA9450_REG_BUCK1CTRL,
477 			.enable_mask = BUCK1_ENMODE_MASK,
478 			.owner = THIS_MODULE,
479 			.of_parse_cb = pca9450_set_dvs_levels,
480 		},
481 		.dvs = {
482 			.run_reg = PCA9450_REG_BUCK1OUT_DVS0,
483 			.run_mask = BUCK1OUT_DVS0_MASK,
484 			.standby_reg = PCA9450_REG_BUCK1OUT_DVS1,
485 			.standby_mask = BUCK1OUT_DVS1_MASK,
486 		},
487 	},
488 	{
489 		.desc = {
490 			.name = "buck2",
491 			.of_match = of_match_ptr("BUCK2"),
492 			.regulators_node = of_match_ptr("regulators"),
493 			.id = PCA9450_BUCK2,
494 			.ops = &pca9450_dvs_buck_regulator_ops,
495 			.type = REGULATOR_VOLTAGE,
496 			.n_voltages = PCA9450_BUCK2_VOLTAGE_NUM,
497 			.linear_ranges = pca9450_dvs_buck_volts,
498 			.n_linear_ranges = ARRAY_SIZE(pca9450_dvs_buck_volts),
499 			.vsel_reg = PCA9450_REG_BUCK2OUT_DVS0,
500 			.vsel_mask = BUCK2OUT_DVS0_MASK,
501 			.enable_reg = PCA9450_REG_BUCK2CTRL,
502 			.enable_mask = BUCK1_ENMODE_MASK,
503 			.owner = THIS_MODULE,
504 			.of_parse_cb = pca9450_set_dvs_levels,
505 		},
506 		.dvs = {
507 			.run_reg = PCA9450_REG_BUCK2OUT_DVS0,
508 			.run_mask = BUCK2OUT_DVS0_MASK,
509 			.standby_reg = PCA9450_REG_BUCK2OUT_DVS1,
510 			.standby_mask = BUCK2OUT_DVS1_MASK,
511 		},
512 	},
513 	{
514 		.desc = {
515 			.name = "buck4",
516 			.of_match = of_match_ptr("BUCK4"),
517 			.regulators_node = of_match_ptr("regulators"),
518 			.id = PCA9450_BUCK4,
519 			.ops = &pca9450_buck_regulator_ops,
520 			.type = REGULATOR_VOLTAGE,
521 			.n_voltages = PCA9450_BUCK4_VOLTAGE_NUM,
522 			.linear_ranges = pca9450_buck_volts,
523 			.n_linear_ranges = ARRAY_SIZE(pca9450_buck_volts),
524 			.vsel_reg = PCA9450_REG_BUCK4OUT,
525 			.vsel_mask = BUCK4OUT_MASK,
526 			.enable_reg = PCA9450_REG_BUCK4CTRL,
527 			.enable_mask = BUCK4_ENMODE_MASK,
528 			.owner = THIS_MODULE,
529 		},
530 	},
531 	{
532 		.desc = {
533 			.name = "buck5",
534 			.of_match = of_match_ptr("BUCK5"),
535 			.regulators_node = of_match_ptr("regulators"),
536 			.id = PCA9450_BUCK5,
537 			.ops = &pca9450_buck_regulator_ops,
538 			.type = REGULATOR_VOLTAGE,
539 			.n_voltages = PCA9450_BUCK5_VOLTAGE_NUM,
540 			.linear_ranges = pca9450_buck_volts,
541 			.n_linear_ranges = ARRAY_SIZE(pca9450_buck_volts),
542 			.vsel_reg = PCA9450_REG_BUCK5OUT,
543 			.vsel_mask = BUCK5OUT_MASK,
544 			.enable_reg = PCA9450_REG_BUCK5CTRL,
545 			.enable_mask = BUCK5_ENMODE_MASK,
546 			.owner = THIS_MODULE,
547 		},
548 	},
549 	{
550 		.desc = {
551 			.name = "buck6",
552 			.of_match = of_match_ptr("BUCK6"),
553 			.regulators_node = of_match_ptr("regulators"),
554 			.id = PCA9450_BUCK6,
555 			.ops = &pca9450_buck_regulator_ops,
556 			.type = REGULATOR_VOLTAGE,
557 			.n_voltages = PCA9450_BUCK6_VOLTAGE_NUM,
558 			.linear_ranges = pca9450_buck_volts,
559 			.n_linear_ranges = ARRAY_SIZE(pca9450_buck_volts),
560 			.vsel_reg = PCA9450_REG_BUCK6OUT,
561 			.vsel_mask = BUCK6OUT_MASK,
562 			.enable_reg = PCA9450_REG_BUCK6CTRL,
563 			.enable_mask = BUCK6_ENMODE_MASK,
564 			.owner = THIS_MODULE,
565 		},
566 	},
567 	{
568 		.desc = {
569 			.name = "ldo1",
570 			.of_match = of_match_ptr("LDO1"),
571 			.regulators_node = of_match_ptr("regulators"),
572 			.id = PCA9450_LDO1,
573 			.ops = &pca9450_ldo_regulator_ops,
574 			.type = REGULATOR_VOLTAGE,
575 			.n_voltages = PCA9450_LDO1_VOLTAGE_NUM,
576 			.linear_ranges = pca9450_ldo1_volts,
577 			.n_linear_ranges = ARRAY_SIZE(pca9450_ldo1_volts),
578 			.vsel_reg = PCA9450_REG_LDO1CTRL,
579 			.vsel_mask = LDO1OUT_MASK,
580 			.enable_reg = PCA9450_REG_LDO1CTRL,
581 			.enable_mask = LDO1_EN_MASK,
582 			.owner = THIS_MODULE,
583 		},
584 	},
585 	{
586 		.desc = {
587 			.name = "ldo2",
588 			.of_match = of_match_ptr("LDO2"),
589 			.regulators_node = of_match_ptr("regulators"),
590 			.id = PCA9450_LDO2,
591 			.ops = &pca9450_ldo_regulator_ops,
592 			.type = REGULATOR_VOLTAGE,
593 			.n_voltages = PCA9450_LDO2_VOLTAGE_NUM,
594 			.linear_ranges = pca9450_ldo2_volts,
595 			.n_linear_ranges = ARRAY_SIZE(pca9450_ldo2_volts),
596 			.vsel_reg = PCA9450_REG_LDO2CTRL,
597 			.vsel_mask = LDO2OUT_MASK,
598 			.enable_reg = PCA9450_REG_LDO2CTRL,
599 			.enable_mask = LDO2_EN_MASK,
600 			.owner = THIS_MODULE,
601 		},
602 	},
603 	{
604 		.desc = {
605 			.name = "ldo3",
606 			.of_match = of_match_ptr("LDO3"),
607 			.regulators_node = of_match_ptr("regulators"),
608 			.id = PCA9450_LDO3,
609 			.ops = &pca9450_ldo_regulator_ops,
610 			.type = REGULATOR_VOLTAGE,
611 			.n_voltages = PCA9450_LDO3_VOLTAGE_NUM,
612 			.linear_ranges = pca9450_ldo34_volts,
613 			.n_linear_ranges = ARRAY_SIZE(pca9450_ldo34_volts),
614 			.vsel_reg = PCA9450_REG_LDO3CTRL,
615 			.vsel_mask = LDO3OUT_MASK,
616 			.enable_reg = PCA9450_REG_LDO3CTRL,
617 			.enable_mask = LDO3_EN_MASK,
618 			.owner = THIS_MODULE,
619 		},
620 	},
621 	{
622 		.desc = {
623 			.name = "ldo4",
624 			.of_match = of_match_ptr("LDO4"),
625 			.regulators_node = of_match_ptr("regulators"),
626 			.id = PCA9450_LDO4,
627 			.ops = &pca9450_ldo_regulator_ops,
628 			.type = REGULATOR_VOLTAGE,
629 			.n_voltages = PCA9450_LDO4_VOLTAGE_NUM,
630 			.linear_ranges = pca9450_ldo34_volts,
631 			.n_linear_ranges = ARRAY_SIZE(pca9450_ldo34_volts),
632 			.vsel_reg = PCA9450_REG_LDO4CTRL,
633 			.vsel_mask = LDO4OUT_MASK,
634 			.enable_reg = PCA9450_REG_LDO4CTRL,
635 			.enable_mask = LDO4_EN_MASK,
636 			.owner = THIS_MODULE,
637 		},
638 	},
639 	{
640 		.desc = {
641 			.name = "ldo5",
642 			.of_match = of_match_ptr("LDO5"),
643 			.regulators_node = of_match_ptr("regulators"),
644 			.id = PCA9450_LDO5,
645 			.ops = &pca9450_ldo_regulator_ops,
646 			.type = REGULATOR_VOLTAGE,
647 			.n_voltages = PCA9450_LDO5_VOLTAGE_NUM,
648 			.linear_ranges = pca9450_ldo5_volts,
649 			.n_linear_ranges = ARRAY_SIZE(pca9450_ldo5_volts),
650 			.vsel_reg = PCA9450_REG_LDO5CTRL_H,
651 			.vsel_mask = LDO5HOUT_MASK,
652 			.enable_reg = PCA9450_REG_LDO5CTRL_H,
653 			.enable_mask = LDO5H_EN_MASK,
654 			.owner = THIS_MODULE,
655 		},
656 	},
657 };
658 
659 static irqreturn_t pca9450_irq_handler(int irq, void *data)
660 {
661 	struct pca9450 *pca9450 = data;
662 	struct regmap *regmap = pca9450->regmap;
663 	unsigned int status;
664 	int ret;
665 
666 	ret = regmap_read(regmap, PCA9450_REG_INT1, &status);
667 	if (ret < 0) {
668 		dev_err(pca9450->dev,
669 			"Failed to read INT1(%d)\n", ret);
670 		return IRQ_NONE;
671 	}
672 
673 	if (status & IRQ_PWRON)
674 		dev_warn(pca9450->dev, "PWRON interrupt.\n");
675 
676 	if (status & IRQ_WDOGB)
677 		dev_warn(pca9450->dev, "WDOGB interrupt.\n");
678 
679 	if (status & IRQ_VR_FLT1)
680 		dev_warn(pca9450->dev, "VRFLT1 interrupt.\n");
681 
682 	if (status & IRQ_VR_FLT2)
683 		dev_warn(pca9450->dev, "VRFLT2 interrupt.\n");
684 
685 	if (status & IRQ_LOWVSYS)
686 		dev_warn(pca9450->dev, "LOWVSYS interrupt.\n");
687 
688 	if (status & IRQ_THERM_105)
689 		dev_warn(pca9450->dev, "IRQ_THERM_105 interrupt.\n");
690 
691 	if (status & IRQ_THERM_125)
692 		dev_warn(pca9450->dev, "IRQ_THERM_125 interrupt.\n");
693 
694 	return IRQ_HANDLED;
695 }
696 
697 static int pca9450_i2c_probe(struct i2c_client *i2c,
698 			     const struct i2c_device_id *id)
699 {
700 	enum pca9450_chip_type type = (unsigned int)(uintptr_t)
701 				      of_device_get_match_data(&i2c->dev);
702 	const struct pca9450_regulator_desc	*regulator_desc;
703 	struct regulator_config config = { };
704 	struct pca9450 *pca9450;
705 	unsigned int device_id, i;
706 	int ret;
707 
708 	if (!i2c->irq) {
709 		dev_err(&i2c->dev, "No IRQ configured?\n");
710 		return -EINVAL;
711 	}
712 
713 	pca9450 = devm_kzalloc(&i2c->dev, sizeof(struct pca9450), GFP_KERNEL);
714 	if (!pca9450)
715 		return -ENOMEM;
716 
717 	switch (type) {
718 	case PCA9450_TYPE_PCA9450A:
719 		regulator_desc = pca9450a_regulators;
720 		pca9450->rcnt = ARRAY_SIZE(pca9450a_regulators);
721 		break;
722 	case PCA9450_TYPE_PCA9450BC:
723 		regulator_desc = pca9450bc_regulators;
724 		pca9450->rcnt = ARRAY_SIZE(pca9450bc_regulators);
725 		break;
726 	default:
727 		dev_err(&i2c->dev, "Unknown device type");
728 		return -EINVAL;
729 	}
730 
731 	pca9450->irq = i2c->irq;
732 	pca9450->type = type;
733 	pca9450->dev = &i2c->dev;
734 
735 	dev_set_drvdata(&i2c->dev, pca9450);
736 
737 	pca9450->regmap = devm_regmap_init_i2c(i2c,
738 					       &pca9450_regmap_config);
739 	if (IS_ERR(pca9450->regmap)) {
740 		dev_err(&i2c->dev, "regmap initialization failed\n");
741 		return PTR_ERR(pca9450->regmap);
742 	}
743 
744 	ret = regmap_read(pca9450->regmap, PCA9450_REG_DEV_ID, &device_id);
745 	if (ret) {
746 		dev_err(&i2c->dev, "Read device id error\n");
747 		return ret;
748 	}
749 
750 	/* Check your board and dts for match the right pmic */
751 	if (((device_id >> 4) != 0x1 && type == PCA9450_TYPE_PCA9450A) ||
752 	    ((device_id >> 4) != 0x3 && type == PCA9450_TYPE_PCA9450BC)) {
753 		dev_err(&i2c->dev, "Device id(%x) mismatched\n",
754 			device_id >> 4);
755 		return -EINVAL;
756 	}
757 
758 	for (i = 0; i < pca9450->rcnt; i++) {
759 		const struct regulator_desc *desc;
760 		struct regulator_dev *rdev;
761 		const struct pca9450_regulator_desc *r;
762 
763 		r = &regulator_desc[i];
764 		desc = &r->desc;
765 
766 		config.regmap = pca9450->regmap;
767 		config.dev = pca9450->dev;
768 
769 		rdev = devm_regulator_register(pca9450->dev, desc, &config);
770 		if (IS_ERR(rdev)) {
771 			ret = PTR_ERR(rdev);
772 			dev_err(pca9450->dev,
773 				"Failed to register regulator(%s): %d\n",
774 				desc->name, ret);
775 			return ret;
776 		}
777 	}
778 
779 	ret = devm_request_threaded_irq(pca9450->dev, pca9450->irq, NULL,
780 					pca9450_irq_handler,
781 					(IRQF_TRIGGER_FALLING | IRQF_ONESHOT),
782 					"pca9450-irq", pca9450);
783 	if (ret != 0) {
784 		dev_err(pca9450->dev, "Failed to request IRQ: %d\n",
785 			pca9450->irq);
786 		return ret;
787 	}
788 	/* Unmask all interrupt except PWRON/WDOG/RSVD */
789 	ret = regmap_update_bits(pca9450->regmap, PCA9450_REG_INT1_MSK,
790 				IRQ_VR_FLT1 | IRQ_VR_FLT2 | IRQ_LOWVSYS |
791 				IRQ_THERM_105 | IRQ_THERM_125,
792 				IRQ_PWRON | IRQ_WDOGB | IRQ_RSVD);
793 	if (ret) {
794 		dev_err(&i2c->dev, "Unmask irq error\n");
795 		return ret;
796 	}
797 
798 	dev_info(&i2c->dev, "%s probed.\n",
799 		type == PCA9450_TYPE_PCA9450A ? "pca9450a" : "pca9450bc");
800 
801 	return 0;
802 }
803 
804 static const struct of_device_id pca9450_of_match[] = {
805 	{
806 		.compatible = "nxp,pca9450a",
807 		.data = (void *)PCA9450_TYPE_PCA9450A,
808 	},
809 	{
810 		.compatible = "nxp,pca9450b",
811 		.data = (void *)PCA9450_TYPE_PCA9450BC,
812 	},
813 	{
814 		.compatible = "nxp,pca9450c",
815 		.data = (void *)PCA9450_TYPE_PCA9450BC,
816 	},
817 	{ }
818 };
819 MODULE_DEVICE_TABLE(of, pca9450_of_match);
820 
821 static struct i2c_driver pca9450_i2c_driver = {
822 	.driver = {
823 		.name = "nxp-pca9450",
824 		.of_match_table = pca9450_of_match,
825 	},
826 	.probe = pca9450_i2c_probe,
827 };
828 
829 module_i2c_driver(pca9450_i2c_driver);
830 
831 MODULE_AUTHOR("Robin Gong <yibin.gong@nxp.com>");
832 MODULE_DESCRIPTION("NXP PCA9450 Power Management IC driver");
833 MODULE_LICENSE("GPL");
834