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