xref: /linux/drivers/mfd/88pm860x-core.c (revision 65c1736c8e9d06bfa5cc734b156518ff65926918)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Base driver for Marvell 88PM8607
4  *
5  * Copyright (C) 2009 Marvell International Ltd.
6  *
7  * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/irq.h>
15 #include <linux/interrupt.h>
16 #include <linux/irqdomain.h>
17 #include <linux/of.h>
18 #include <linux/of_platform.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/slab.h>
22 #include <linux/mfd/core.h>
23 #include <linux/mfd/88pm860x.h>
24 #include <linux/regulator/machine.h>
25 #include <linux/power/charger-manager.h>
26 
27 #define INT_STATUS_NUM			3
28 
29 static const struct resource bk0_resources[] = {
30 	{2, 2, "duty cycle", IORESOURCE_REG, },
31 	{3, 3, "always on",  IORESOURCE_REG, },
32 	{3, 3, "current",    IORESOURCE_REG, },
33 };
34 static const struct resource bk1_resources[] = {
35 	{4, 4, "duty cycle", IORESOURCE_REG, },
36 	{5, 5, "always on",  IORESOURCE_REG, },
37 	{5, 5, "current",    IORESOURCE_REG, },
38 };
39 static const struct resource bk2_resources[] = {
40 	{6, 6, "duty cycle", IORESOURCE_REG, },
41 	{7, 7, "always on",  IORESOURCE_REG, },
42 	{5, 5, "current",    IORESOURCE_REG, },
43 };
44 
45 static const struct resource led0_resources[] = {
46 	/* RGB1 Red LED */
47 	{0xd, 0xd, "control", IORESOURCE_REG, },
48 	{0xc, 0xc, "blink",   IORESOURCE_REG, },
49 };
50 static const struct resource led1_resources[] = {
51 	/* RGB1 Green LED */
52 	{0xe, 0xe, "control", IORESOURCE_REG, },
53 	{0xc, 0xc, "blink",   IORESOURCE_REG, },
54 };
55 static const struct resource led2_resources[] = {
56 	/* RGB1 Blue LED */
57 	{0xf, 0xf, "control", IORESOURCE_REG, },
58 	{0xc, 0xc, "blink",   IORESOURCE_REG, },
59 };
60 static const struct resource led3_resources[] = {
61 	/* RGB2 Red LED */
62 	{0x9, 0x9, "control", IORESOURCE_REG, },
63 	{0x8, 0x8, "blink",   IORESOURCE_REG, },
64 };
65 static const struct resource led4_resources[] = {
66 	/* RGB2 Green LED */
67 	{0xa, 0xa, "control", IORESOURCE_REG, },
68 	{0x8, 0x8, "blink",   IORESOURCE_REG, },
69 };
70 static const struct resource led5_resources[] = {
71 	/* RGB2 Blue LED */
72 	{0xb, 0xb, "control", IORESOURCE_REG, },
73 	{0x8, 0x8, "blink",   IORESOURCE_REG, },
74 };
75 
76 static const struct resource buck1_resources[] = {
77 	{0x24, 0x24, "buck set", IORESOURCE_REG, },
78 };
79 static const struct resource buck2_resources[] = {
80 	{0x25, 0x25, "buck set", IORESOURCE_REG, },
81 };
82 static const struct resource buck3_resources[] = {
83 	{0x26, 0x26, "buck set", IORESOURCE_REG, },
84 };
85 static const struct resource ldo1_resources[] = {
86 	{0x10, 0x10, "ldo set", IORESOURCE_REG, },
87 };
88 static const struct resource ldo2_resources[] = {
89 	{0x11, 0x11, "ldo set", IORESOURCE_REG, },
90 };
91 static const struct resource ldo3_resources[] = {
92 	{0x12, 0x12, "ldo set", IORESOURCE_REG, },
93 };
94 static const struct resource ldo4_resources[] = {
95 	{0x13, 0x13, "ldo set", IORESOURCE_REG, },
96 };
97 static const struct resource ldo5_resources[] = {
98 	{0x14, 0x14, "ldo set", IORESOURCE_REG, },
99 };
100 static const struct resource ldo6_resources[] = {
101 	{0x15, 0x15, "ldo set", IORESOURCE_REG, },
102 };
103 static const struct resource ldo7_resources[] = {
104 	{0x16, 0x16, "ldo set", IORESOURCE_REG, },
105 };
106 static const struct resource ldo8_resources[] = {
107 	{0x17, 0x17, "ldo set", IORESOURCE_REG, },
108 };
109 static const struct resource ldo9_resources[] = {
110 	{0x18, 0x18, "ldo set", IORESOURCE_REG, },
111 };
112 static const struct resource ldo10_resources[] = {
113 	{0x19, 0x19, "ldo set", IORESOURCE_REG, },
114 };
115 static const struct resource ldo12_resources[] = {
116 	{0x1a, 0x1a, "ldo set", IORESOURCE_REG, },
117 };
118 static const struct resource ldo_vibrator_resources[] = {
119 	{0x28, 0x28, "ldo set", IORESOURCE_REG, },
120 };
121 static const struct resource ldo14_resources[] = {
122 	{0x1b, 0x1b, "ldo set", IORESOURCE_REG, },
123 };
124 
125 static struct resource touch_resources[] = {
126 	{PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
127 };
128 
129 static struct resource onkey_resources[] = {
130 	{PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
131 };
132 
133 static struct resource codec_resources[] = {
134 	/* Headset microphone insertion or removal */
135 	{PM8607_IRQ_MICIN,   PM8607_IRQ_MICIN,   "micin",   IORESOURCE_IRQ,},
136 	/* Hook-switch press or release */
137 	{PM8607_IRQ_HOOK,    PM8607_IRQ_HOOK,    "hook",    IORESOURCE_IRQ,},
138 	/* Headset insertion or removal */
139 	{PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
140 	/* Audio short */
141 	{PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short",
142 	 IORESOURCE_IRQ,},
143 };
144 
145 static struct resource battery_resources[] = {
146 	{PM8607_IRQ_CC,  PM8607_IRQ_CC,  "columb counter", IORESOURCE_IRQ,},
147 	{PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery",        IORESOURCE_IRQ,},
148 };
149 
150 static struct resource charger_resources[] = {
151 	{PM8607_IRQ_CHG,  PM8607_IRQ_CHG,  "charger detect",  IORESOURCE_IRQ,},
152 	{PM8607_IRQ_CHG_DONE,  PM8607_IRQ_CHG_DONE,  "charging done",
153 	 IORESOURCE_IRQ,},
154 	{PM8607_IRQ_CHG_FAIL,  PM8607_IRQ_CHG_FAIL,  "charging timeout",
155 	 IORESOURCE_IRQ,},
156 	{PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging fault",
157 	 IORESOURCE_IRQ,},
158 	{PM8607_IRQ_GPADC1,    PM8607_IRQ_GPADC1,    "battery temperature",
159 	 IORESOURCE_IRQ,},
160 	{PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
161 	{PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage",    IORESOURCE_IRQ,},
162 };
163 
164 static struct resource rtc_resources[] = {
165 	{PM8607_IRQ_RTC, PM8607_IRQ_RTC, "rtc", IORESOURCE_IRQ,},
166 };
167 
168 static struct mfd_cell bk_devs[] = {
169 	{
170 		.name = "88pm860x-backlight",
171 		.id = 0,
172 		.num_resources = ARRAY_SIZE(bk0_resources),
173 		.resources = bk0_resources,
174 	}, {
175 		.name = "88pm860x-backlight",
176 		.id = 1,
177 		.num_resources = ARRAY_SIZE(bk1_resources),
178 		.resources = bk1_resources,
179 	}, {
180 		.name = "88pm860x-backlight",
181 		.id = 2,
182 		.num_resources = ARRAY_SIZE(bk2_resources),
183 		.resources = bk2_resources,
184 	},
185 };
186 
187 static struct mfd_cell led_devs[] = {
188 	{
189 		.name = "88pm860x-led",
190 		.id = 0,
191 		.num_resources = ARRAY_SIZE(led0_resources),
192 		.resources = led0_resources,
193 	}, {
194 		.name = "88pm860x-led",
195 		.id = 1,
196 		.num_resources = ARRAY_SIZE(led1_resources),
197 		.resources = led1_resources,
198 	}, {
199 		.name = "88pm860x-led",
200 		.id = 2,
201 		.num_resources = ARRAY_SIZE(led2_resources),
202 		.resources = led2_resources,
203 	}, {
204 		.name = "88pm860x-led",
205 		.id = 3,
206 		.num_resources = ARRAY_SIZE(led3_resources),
207 		.resources = led3_resources,
208 	}, {
209 		.name = "88pm860x-led",
210 		.id = 4,
211 		.num_resources = ARRAY_SIZE(led4_resources),
212 		.resources = led4_resources,
213 	}, {
214 		.name = "88pm860x-led",
215 		.id = 5,
216 		.num_resources = ARRAY_SIZE(led5_resources),
217 		.resources = led5_resources,
218 	},
219 };
220 
221 static struct mfd_cell reg_devs[] = {
222 	{
223 		.name = "88pm860x-regulator",
224 		.id = 0,
225 		.num_resources = ARRAY_SIZE(buck1_resources),
226 		.resources = buck1_resources,
227 	}, {
228 		.name = "88pm860x-regulator",
229 		.id = 1,
230 		.num_resources = ARRAY_SIZE(buck2_resources),
231 		.resources = buck2_resources,
232 	}, {
233 		.name = "88pm860x-regulator",
234 		.id = 2,
235 		.num_resources = ARRAY_SIZE(buck3_resources),
236 		.resources = buck3_resources,
237 	}, {
238 		.name = "88pm860x-regulator",
239 		.id = 3,
240 		.num_resources = ARRAY_SIZE(ldo1_resources),
241 		.resources = ldo1_resources,
242 	}, {
243 		.name = "88pm860x-regulator",
244 		.id = 4,
245 		.num_resources = ARRAY_SIZE(ldo2_resources),
246 		.resources = ldo2_resources,
247 	}, {
248 		.name = "88pm860x-regulator",
249 		.id = 5,
250 		.num_resources = ARRAY_SIZE(ldo3_resources),
251 		.resources = ldo3_resources,
252 	}, {
253 		.name = "88pm860x-regulator",
254 		.id = 6,
255 		.num_resources = ARRAY_SIZE(ldo4_resources),
256 		.resources = ldo4_resources,
257 	}, {
258 		.name = "88pm860x-regulator",
259 		.id = 7,
260 		.num_resources = ARRAY_SIZE(ldo5_resources),
261 		.resources = ldo5_resources,
262 	}, {
263 		.name = "88pm860x-regulator",
264 		.id = 8,
265 		.num_resources = ARRAY_SIZE(ldo6_resources),
266 		.resources = ldo6_resources,
267 	}, {
268 		.name = "88pm860x-regulator",
269 		.id = 9,
270 		.num_resources = ARRAY_SIZE(ldo7_resources),
271 		.resources = ldo7_resources,
272 	}, {
273 		.name = "88pm860x-regulator",
274 		.id = 10,
275 		.num_resources = ARRAY_SIZE(ldo8_resources),
276 		.resources = ldo8_resources,
277 	}, {
278 		.name = "88pm860x-regulator",
279 		.id = 11,
280 		.num_resources = ARRAY_SIZE(ldo9_resources),
281 		.resources = ldo9_resources,
282 	}, {
283 		.name = "88pm860x-regulator",
284 		.id = 12,
285 		.num_resources = ARRAY_SIZE(ldo10_resources),
286 		.resources = ldo10_resources,
287 	}, {
288 		.name = "88pm860x-regulator",
289 		.id = 13,
290 		.num_resources = ARRAY_SIZE(ldo12_resources),
291 		.resources = ldo12_resources,
292 	}, {
293 		.name = "88pm860x-regulator",
294 		.id = 14,
295 		.num_resources = ARRAY_SIZE(ldo_vibrator_resources),
296 		.resources = ldo_vibrator_resources,
297 	}, {
298 		.name = "88pm860x-regulator",
299 		.id = 15,
300 		.num_resources = ARRAY_SIZE(ldo14_resources),
301 		.resources = ldo14_resources,
302 	},
303 };
304 
305 static struct mfd_cell touch_devs[] = {
306 	{"88pm860x-touch", -1,},
307 };
308 
309 static struct mfd_cell onkey_devs[] = {
310 	{"88pm860x-onkey", -1,},
311 };
312 
313 static struct mfd_cell codec_devs[] = {
314 	{"88pm860x-codec", -1,},
315 };
316 
317 static struct regulator_consumer_supply preg_supply[] = {
318 	REGULATOR_SUPPLY("preg", "charger-manager"),
319 };
320 
321 static struct regulator_init_data preg_init_data = {
322 	.num_consumer_supplies	= ARRAY_SIZE(preg_supply),
323 	.consumer_supplies	= &preg_supply[0],
324 };
325 
326 static struct charger_regulator chg_desc_regulator_data[] = {
327 	{ .regulator_name = "preg", },
328 };
329 
330 static struct mfd_cell power_devs[] = {
331 	{"88pm860x-battery", -1,},
332 	{"88pm860x-charger", -1,},
333 	{"88pm860x-preg",    -1,},
334 	{"charger-manager", -1,},
335 };
336 
337 static struct mfd_cell rtc_devs[] = {
338 	{"88pm860x-rtc", -1,},
339 };
340 
341 
342 struct pm860x_irq_data {
343 	int	reg;
344 	int	mask_reg;
345 	int	enable;		/* enable or not */
346 	int	offs;		/* bit offset in mask register */
347 };
348 
349 static struct pm860x_irq_data pm860x_irqs[] = {
350 	[PM8607_IRQ_ONKEY] = {
351 		.reg		= PM8607_INT_STATUS1,
352 		.mask_reg	= PM8607_INT_MASK_1,
353 		.offs		= 1 << 0,
354 	},
355 	[PM8607_IRQ_EXTON] = {
356 		.reg		= PM8607_INT_STATUS1,
357 		.mask_reg	= PM8607_INT_MASK_1,
358 		.offs		= 1 << 1,
359 	},
360 	[PM8607_IRQ_CHG] = {
361 		.reg		= PM8607_INT_STATUS1,
362 		.mask_reg	= PM8607_INT_MASK_1,
363 		.offs		= 1 << 2,
364 	},
365 	[PM8607_IRQ_BAT] = {
366 		.reg		= PM8607_INT_STATUS1,
367 		.mask_reg	= PM8607_INT_MASK_1,
368 		.offs		= 1 << 3,
369 	},
370 	[PM8607_IRQ_RTC] = {
371 		.reg		= PM8607_INT_STATUS1,
372 		.mask_reg	= PM8607_INT_MASK_1,
373 		.offs		= 1 << 4,
374 	},
375 	[PM8607_IRQ_CC] = {
376 		.reg		= PM8607_INT_STATUS1,
377 		.mask_reg	= PM8607_INT_MASK_1,
378 		.offs		= 1 << 5,
379 	},
380 	[PM8607_IRQ_VBAT] = {
381 		.reg		= PM8607_INT_STATUS2,
382 		.mask_reg	= PM8607_INT_MASK_2,
383 		.offs		= 1 << 0,
384 	},
385 	[PM8607_IRQ_VCHG] = {
386 		.reg		= PM8607_INT_STATUS2,
387 		.mask_reg	= PM8607_INT_MASK_2,
388 		.offs		= 1 << 1,
389 	},
390 	[PM8607_IRQ_VSYS] = {
391 		.reg		= PM8607_INT_STATUS2,
392 		.mask_reg	= PM8607_INT_MASK_2,
393 		.offs		= 1 << 2,
394 	},
395 	[PM8607_IRQ_TINT] = {
396 		.reg		= PM8607_INT_STATUS2,
397 		.mask_reg	= PM8607_INT_MASK_2,
398 		.offs		= 1 << 3,
399 	},
400 	[PM8607_IRQ_GPADC0] = {
401 		.reg		= PM8607_INT_STATUS2,
402 		.mask_reg	= PM8607_INT_MASK_2,
403 		.offs		= 1 << 4,
404 	},
405 	[PM8607_IRQ_GPADC1] = {
406 		.reg		= PM8607_INT_STATUS2,
407 		.mask_reg	= PM8607_INT_MASK_2,
408 		.offs		= 1 << 5,
409 	},
410 	[PM8607_IRQ_GPADC2] = {
411 		.reg		= PM8607_INT_STATUS2,
412 		.mask_reg	= PM8607_INT_MASK_2,
413 		.offs		= 1 << 6,
414 	},
415 	[PM8607_IRQ_GPADC3] = {
416 		.reg		= PM8607_INT_STATUS2,
417 		.mask_reg	= PM8607_INT_MASK_2,
418 		.offs		= 1 << 7,
419 	},
420 	[PM8607_IRQ_AUDIO_SHORT] = {
421 		.reg		= PM8607_INT_STATUS3,
422 		.mask_reg	= PM8607_INT_MASK_3,
423 		.offs		= 1 << 0,
424 	},
425 	[PM8607_IRQ_PEN] = {
426 		.reg		= PM8607_INT_STATUS3,
427 		.mask_reg	= PM8607_INT_MASK_3,
428 		.offs		= 1 << 1,
429 	},
430 	[PM8607_IRQ_HEADSET] = {
431 		.reg		= PM8607_INT_STATUS3,
432 		.mask_reg	= PM8607_INT_MASK_3,
433 		.offs		= 1 << 2,
434 	},
435 	[PM8607_IRQ_HOOK] = {
436 		.reg		= PM8607_INT_STATUS3,
437 		.mask_reg	= PM8607_INT_MASK_3,
438 		.offs		= 1 << 3,
439 	},
440 	[PM8607_IRQ_MICIN] = {
441 		.reg		= PM8607_INT_STATUS3,
442 		.mask_reg	= PM8607_INT_MASK_3,
443 		.offs		= 1 << 4,
444 	},
445 	[PM8607_IRQ_CHG_FAIL] = {
446 		.reg		= PM8607_INT_STATUS3,
447 		.mask_reg	= PM8607_INT_MASK_3,
448 		.offs		= 1 << 5,
449 	},
450 	[PM8607_IRQ_CHG_DONE] = {
451 		.reg		= PM8607_INT_STATUS3,
452 		.mask_reg	= PM8607_INT_MASK_3,
453 		.offs		= 1 << 6,
454 	},
455 	[PM8607_IRQ_CHG_FAULT] = {
456 		.reg		= PM8607_INT_STATUS3,
457 		.mask_reg	= PM8607_INT_MASK_3,
458 		.offs		= 1 << 7,
459 	},
460 };
461 
pm860x_irq(int irq,void * data)462 static irqreturn_t pm860x_irq(int irq, void *data)
463 {
464 	struct pm860x_chip *chip = data;
465 	struct pm860x_irq_data *irq_data;
466 	struct i2c_client *i2c;
467 	int read_reg = -1, value = 0;
468 	int i;
469 
470 	i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
471 	for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
472 		irq_data = &pm860x_irqs[i];
473 		if (read_reg != irq_data->reg) {
474 			read_reg = irq_data->reg;
475 			value = pm860x_reg_read(i2c, irq_data->reg);
476 		}
477 		if (value & irq_data->enable)
478 			handle_nested_irq(chip->irq_base + i);
479 	}
480 	return IRQ_HANDLED;
481 }
482 
pm860x_irq_lock(struct irq_data * data)483 static void pm860x_irq_lock(struct irq_data *data)
484 {
485 	struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
486 
487 	mutex_lock(&chip->irq_lock);
488 }
489 
pm860x_irq_sync_unlock(struct irq_data * data)490 static void pm860x_irq_sync_unlock(struct irq_data *data)
491 {
492 	struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
493 	struct pm860x_irq_data *irq_data;
494 	struct i2c_client *i2c;
495 	static unsigned char cached[3] = {0x0, 0x0, 0x0};
496 	unsigned char mask[3];
497 	int i;
498 
499 	i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
500 	/* Load cached value. In initial, all IRQs are masked */
501 	for (i = 0; i < 3; i++)
502 		mask[i] = cached[i];
503 	for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
504 		irq_data = &pm860x_irqs[i];
505 		switch (irq_data->mask_reg) {
506 		case PM8607_INT_MASK_1:
507 			mask[0] &= ~irq_data->offs;
508 			mask[0] |= irq_data->enable;
509 			break;
510 		case PM8607_INT_MASK_2:
511 			mask[1] &= ~irq_data->offs;
512 			mask[1] |= irq_data->enable;
513 			break;
514 		case PM8607_INT_MASK_3:
515 			mask[2] &= ~irq_data->offs;
516 			mask[2] |= irq_data->enable;
517 			break;
518 		default:
519 			dev_err(chip->dev, "wrong IRQ\n");
520 			break;
521 		}
522 	}
523 	/* update mask into registers */
524 	for (i = 0; i < 3; i++) {
525 		if (mask[i] != cached[i]) {
526 			cached[i] = mask[i];
527 			pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
528 		}
529 	}
530 
531 	mutex_unlock(&chip->irq_lock);
532 }
533 
pm860x_irq_enable(struct irq_data * data)534 static void pm860x_irq_enable(struct irq_data *data)
535 {
536 	pm860x_irqs[data->hwirq].enable = pm860x_irqs[data->hwirq].offs;
537 }
538 
pm860x_irq_disable(struct irq_data * data)539 static void pm860x_irq_disable(struct irq_data *data)
540 {
541 	pm860x_irqs[data->hwirq].enable = 0;
542 }
543 
544 static struct irq_chip pm860x_irq_chip = {
545 	.name		= "88pm860x",
546 	.irq_bus_lock	= pm860x_irq_lock,
547 	.irq_bus_sync_unlock = pm860x_irq_sync_unlock,
548 	.irq_enable	= pm860x_irq_enable,
549 	.irq_disable	= pm860x_irq_disable,
550 };
551 
pm860x_irq_domain_map(struct irq_domain * d,unsigned int virq,irq_hw_number_t hw)552 static int pm860x_irq_domain_map(struct irq_domain *d, unsigned int virq,
553 				 irq_hw_number_t hw)
554 {
555 	irq_set_chip_data(virq, d->host_data);
556 	irq_set_chip_and_handler(virq, &pm860x_irq_chip, handle_edge_irq);
557 	irq_set_nested_thread(virq, 1);
558 	irq_set_noprobe(virq);
559 	return 0;
560 }
561 
562 static const struct irq_domain_ops pm860x_irq_domain_ops = {
563 	.map	= pm860x_irq_domain_map,
564 	.xlate	= irq_domain_xlate_onetwocell,
565 };
566 
device_irq_init(struct pm860x_chip * chip,struct pm860x_platform_data * pdata)567 static int device_irq_init(struct pm860x_chip *chip,
568 				     struct pm860x_platform_data *pdata)
569 {
570 	struct i2c_client *i2c = (chip->id == CHIP_PM8607) ?
571 		chip->client : chip->companion;
572 	unsigned char status_buf[INT_STATUS_NUM];
573 	unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
574 	int data, mask, ret = -EINVAL;
575 	int nr_irqs, irq_base = -1;
576 
577 	mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
578 		| PM8607_B0_MISC1_INT_MASK;
579 	data = 0;
580 	chip->irq_mode = 0;
581 	if (pdata && pdata->irq_mode) {
582 		/*
583 		 * irq_mode defines the way of clearing interrupt. If it's 1,
584 		 * clear IRQ by write. Otherwise, clear it by read.
585 		 * This control bit is valid from 88PM8607 B0 steping.
586 		 */
587 		data |= PM8607_B0_MISC1_INT_CLEAR;
588 		chip->irq_mode = 1;
589 	}
590 	ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
591 	if (ret < 0)
592 		goto out;
593 
594 	/* mask all IRQs */
595 	memset(status_buf, 0, INT_STATUS_NUM);
596 	ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
597 				INT_STATUS_NUM, status_buf);
598 	if (ret < 0)
599 		goto out;
600 
601 	if (chip->irq_mode) {
602 		/* clear interrupt status by write */
603 		memset(status_buf, 0xFF, INT_STATUS_NUM);
604 		ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
605 					INT_STATUS_NUM, status_buf);
606 	} else {
607 		/* clear interrupt status by read */
608 		ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
609 					INT_STATUS_NUM, status_buf);
610 	}
611 	if (ret < 0)
612 		goto out;
613 
614 	mutex_init(&chip->irq_lock);
615 
616 	if (pdata && pdata->irq_base)
617 		irq_base = pdata->irq_base;
618 	nr_irqs = ARRAY_SIZE(pm860x_irqs);
619 	chip->irq_base = irq_alloc_descs(irq_base, 0, nr_irqs, 0);
620 	if (chip->irq_base < 0) {
621 		dev_err(&i2c->dev, "Failed to allocate interrupts, ret:%d\n",
622 			chip->irq_base);
623 		ret = -EBUSY;
624 		goto out;
625 	}
626 	irq_domain_create_legacy(dev_fwnode(&i2c->dev), nr_irqs, chip->irq_base, 0,
627 				 &pm860x_irq_domain_ops, chip);
628 	chip->core_irq = i2c->irq;
629 	if (!chip->core_irq)
630 		goto out;
631 
632 	ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq,
633 				   flags | IRQF_ONESHOT, "88pm860x", chip);
634 	if (ret) {
635 		dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
636 		chip->core_irq = 0;
637 	}
638 
639 	return 0;
640 out:
641 	chip->core_irq = 0;
642 	return ret;
643 }
644 
device_irq_exit(struct pm860x_chip * chip)645 static void device_irq_exit(struct pm860x_chip *chip)
646 {
647 	if (chip->core_irq)
648 		free_irq(chip->core_irq, chip);
649 }
650 
pm8606_osc_enable(struct pm860x_chip * chip,unsigned short client)651 int pm8606_osc_enable(struct pm860x_chip *chip, unsigned short client)
652 {
653 	int ret = -EIO;
654 	struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
655 		chip->client : chip->companion;
656 
657 	dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
658 	dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
659 			__func__, chip->osc_vote,
660 			chip->osc_status);
661 
662 	mutex_lock(&chip->osc_lock);
663 	/* Update voting status */
664 	chip->osc_vote |= client;
665 	/* If reference group is off - turn on*/
666 	if (chip->osc_status != PM8606_REF_GP_OSC_ON) {
667 		chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
668 		/* Enable Reference group Vsys */
669 		if (pm860x_set_bits(i2c, PM8606_VSYS,
670 				PM8606_VSYS_EN, PM8606_VSYS_EN))
671 			goto out;
672 
673 		/*Enable Internal Oscillator */
674 		if (pm860x_set_bits(i2c, PM8606_MISC,
675 				PM8606_MISC_OSC_EN, PM8606_MISC_OSC_EN))
676 			goto out;
677 		/* Update status (only if writes succeed) */
678 		chip->osc_status = PM8606_REF_GP_OSC_ON;
679 	}
680 	mutex_unlock(&chip->osc_lock);
681 
682 	dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
683 			__func__, chip->osc_vote,
684 			chip->osc_status, ret);
685 	return 0;
686 out:
687 	mutex_unlock(&chip->osc_lock);
688 	return ret;
689 }
690 EXPORT_SYMBOL(pm8606_osc_enable);
691 
pm8606_osc_disable(struct pm860x_chip * chip,unsigned short client)692 int pm8606_osc_disable(struct pm860x_chip *chip, unsigned short client)
693 {
694 	int ret = -EIO;
695 	struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
696 		chip->client : chip->companion;
697 
698 	dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
699 	dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
700 			__func__, chip->osc_vote,
701 			chip->osc_status);
702 
703 	mutex_lock(&chip->osc_lock);
704 	/* Update voting status */
705 	chip->osc_vote &= ~(client);
706 	/*
707 	 * If reference group is off and this is the last client to release
708 	 * - turn off
709 	 */
710 	if ((chip->osc_status != PM8606_REF_GP_OSC_OFF) &&
711 			(chip->osc_vote == REF_GP_NO_CLIENTS)) {
712 		chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
713 		/* Disable Reference group Vsys */
714 		if (pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0))
715 			goto out;
716 		/* Disable Internal Oscillator */
717 		if (pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0))
718 			goto out;
719 		chip->osc_status = PM8606_REF_GP_OSC_OFF;
720 	}
721 	mutex_unlock(&chip->osc_lock);
722 
723 	dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
724 			__func__, chip->osc_vote,
725 			chip->osc_status, ret);
726 	return 0;
727 out:
728 	mutex_unlock(&chip->osc_lock);
729 	return ret;
730 }
731 EXPORT_SYMBOL(pm8606_osc_disable);
732 
device_osc_init(struct i2c_client * i2c)733 static void device_osc_init(struct i2c_client *i2c)
734 {
735 	struct pm860x_chip *chip = i2c_get_clientdata(i2c);
736 
737 	mutex_init(&chip->osc_lock);
738 	/* init portofino reference group voting and status */
739 	/* Disable Reference group Vsys */
740 	pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0);
741 	/* Disable Internal Oscillator */
742 	pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0);
743 
744 	chip->osc_vote = REF_GP_NO_CLIENTS;
745 	chip->osc_status = PM8606_REF_GP_OSC_OFF;
746 }
747 
device_bk_init(struct pm860x_chip * chip,struct pm860x_platform_data * pdata)748 static void device_bk_init(struct pm860x_chip *chip,
749 				     struct pm860x_platform_data *pdata)
750 {
751 	int ret, i;
752 
753 	if (pdata && pdata->backlight) {
754 		if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
755 			pdata->num_backlights = ARRAY_SIZE(bk_devs);
756 		for (i = 0; i < pdata->num_backlights; i++) {
757 			bk_devs[i].platform_data = &pdata->backlight[i];
758 			bk_devs[i].pdata_size =
759 				sizeof(struct pm860x_backlight_pdata);
760 		}
761 	}
762 	ret = mfd_add_devices(chip->dev, 0, bk_devs,
763 			      ARRAY_SIZE(bk_devs), NULL, 0, NULL);
764 	if (ret < 0)
765 		dev_err(chip->dev, "Failed to add backlight subdev\n");
766 }
767 
device_led_init(struct pm860x_chip * chip,struct pm860x_platform_data * pdata)768 static void device_led_init(struct pm860x_chip *chip,
769 				      struct pm860x_platform_data *pdata)
770 {
771 	int ret, i;
772 
773 	if (pdata && pdata->led) {
774 		if (pdata->num_leds > ARRAY_SIZE(led_devs))
775 			pdata->num_leds = ARRAY_SIZE(led_devs);
776 		for (i = 0; i < pdata->num_leds; i++) {
777 			led_devs[i].platform_data = &pdata->led[i];
778 			led_devs[i].pdata_size =
779 				sizeof(struct pm860x_led_pdata);
780 		}
781 	}
782 	ret = mfd_add_devices(chip->dev, 0, led_devs,
783 			      ARRAY_SIZE(led_devs), NULL, 0, NULL);
784 	if (ret < 0) {
785 		dev_err(chip->dev, "Failed to add led subdev\n");
786 		return;
787 	}
788 }
789 
device_regulator_init(struct pm860x_chip * chip,struct pm860x_platform_data * pdata)790 static void device_regulator_init(struct pm860x_chip *chip,
791 					    struct pm860x_platform_data *pdata)
792 {
793 	int ret;
794 
795 	if (pdata == NULL)
796 		return;
797 	if (pdata->buck1) {
798 		reg_devs[0].platform_data = pdata->buck1;
799 		reg_devs[0].pdata_size = sizeof(struct regulator_init_data);
800 	}
801 	if (pdata->buck2) {
802 		reg_devs[1].platform_data = pdata->buck2;
803 		reg_devs[1].pdata_size = sizeof(struct regulator_init_data);
804 	}
805 	if (pdata->buck3) {
806 		reg_devs[2].platform_data = pdata->buck3;
807 		reg_devs[2].pdata_size = sizeof(struct regulator_init_data);
808 	}
809 	if (pdata->ldo1) {
810 		reg_devs[3].platform_data = pdata->ldo1;
811 		reg_devs[3].pdata_size = sizeof(struct regulator_init_data);
812 	}
813 	if (pdata->ldo2) {
814 		reg_devs[4].platform_data = pdata->ldo2;
815 		reg_devs[4].pdata_size = sizeof(struct regulator_init_data);
816 	}
817 	if (pdata->ldo3) {
818 		reg_devs[5].platform_data = pdata->ldo3;
819 		reg_devs[5].pdata_size = sizeof(struct regulator_init_data);
820 	}
821 	if (pdata->ldo4) {
822 		reg_devs[6].platform_data = pdata->ldo4;
823 		reg_devs[6].pdata_size = sizeof(struct regulator_init_data);
824 	}
825 	if (pdata->ldo5) {
826 		reg_devs[7].platform_data = pdata->ldo5;
827 		reg_devs[7].pdata_size = sizeof(struct regulator_init_data);
828 	}
829 	if (pdata->ldo6) {
830 		reg_devs[8].platform_data = pdata->ldo6;
831 		reg_devs[8].pdata_size = sizeof(struct regulator_init_data);
832 	}
833 	if (pdata->ldo7) {
834 		reg_devs[9].platform_data = pdata->ldo7;
835 		reg_devs[9].pdata_size = sizeof(struct regulator_init_data);
836 	}
837 	if (pdata->ldo8) {
838 		reg_devs[10].platform_data = pdata->ldo8;
839 		reg_devs[10].pdata_size = sizeof(struct regulator_init_data);
840 	}
841 	if (pdata->ldo9) {
842 		reg_devs[11].platform_data = pdata->ldo9;
843 		reg_devs[11].pdata_size = sizeof(struct regulator_init_data);
844 	}
845 	if (pdata->ldo10) {
846 		reg_devs[12].platform_data = pdata->ldo10;
847 		reg_devs[12].pdata_size = sizeof(struct regulator_init_data);
848 	}
849 	if (pdata->ldo12) {
850 		reg_devs[13].platform_data = pdata->ldo12;
851 		reg_devs[13].pdata_size = sizeof(struct regulator_init_data);
852 	}
853 	if (pdata->ldo_vibrator) {
854 		reg_devs[14].platform_data = pdata->ldo_vibrator;
855 		reg_devs[14].pdata_size = sizeof(struct regulator_init_data);
856 	}
857 	if (pdata->ldo14) {
858 		reg_devs[15].platform_data = pdata->ldo14;
859 		reg_devs[15].pdata_size = sizeof(struct regulator_init_data);
860 	}
861 	ret = mfd_add_devices(chip->dev, 0, reg_devs,
862 			      ARRAY_SIZE(reg_devs), NULL, 0, NULL);
863 	if (ret < 0) {
864 		dev_err(chip->dev, "Failed to add regulator subdev\n");
865 		return;
866 	}
867 }
868 
device_rtc_init(struct pm860x_chip * chip,struct pm860x_platform_data * pdata)869 static void device_rtc_init(struct pm860x_chip *chip,
870 				      struct pm860x_platform_data *pdata)
871 {
872 	int ret;
873 
874 	if (!pdata)
875 		return;
876 
877 	rtc_devs[0].platform_data = pdata->rtc;
878 	rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
879 	rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
880 	rtc_devs[0].resources = &rtc_resources[0];
881 	ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
882 			      ARRAY_SIZE(rtc_devs), &rtc_resources[0],
883 			      chip->irq_base, NULL);
884 	if (ret < 0)
885 		dev_err(chip->dev, "Failed to add rtc subdev\n");
886 }
887 
device_touch_init(struct pm860x_chip * chip,struct pm860x_platform_data * pdata)888 static void device_touch_init(struct pm860x_chip *chip,
889 					struct pm860x_platform_data *pdata)
890 {
891 	int ret;
892 
893 	if (pdata == NULL)
894 		return;
895 
896 	touch_devs[0].platform_data = pdata->touch;
897 	touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
898 	touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
899 	touch_devs[0].resources = &touch_resources[0];
900 	ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
901 			      ARRAY_SIZE(touch_devs), &touch_resources[0],
902 			      chip->irq_base, NULL);
903 	if (ret < 0)
904 		dev_err(chip->dev, "Failed to add touch subdev\n");
905 }
906 
device_power_init(struct pm860x_chip * chip,struct pm860x_platform_data * pdata)907 static void device_power_init(struct pm860x_chip *chip,
908 					struct pm860x_platform_data *pdata)
909 {
910 	int ret;
911 
912 	if (pdata == NULL)
913 		return;
914 
915 	power_devs[0].platform_data = pdata->power;
916 	power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
917 	power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
918 	power_devs[0].resources = &battery_resources[0];
919 	ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
920 			      &battery_resources[0], chip->irq_base, NULL);
921 	if (ret < 0)
922 		dev_err(chip->dev, "Failed to add battery subdev\n");
923 
924 	power_devs[1].platform_data = pdata->power;
925 	power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
926 	power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
927 	power_devs[1].resources = &charger_resources[0];
928 	ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
929 			      &charger_resources[0], chip->irq_base, NULL);
930 	if (ret < 0)
931 		dev_err(chip->dev, "Failed to add charger subdev\n");
932 
933 	power_devs[2].platform_data = &preg_init_data;
934 	power_devs[2].pdata_size = sizeof(struct regulator_init_data);
935 	ret = mfd_add_devices(chip->dev, 0, &power_devs[2], 1,
936 			      NULL, chip->irq_base, NULL);
937 	if (ret < 0)
938 		dev_err(chip->dev, "Failed to add preg subdev\n");
939 
940 	if (pdata->chg_desc) {
941 		pdata->chg_desc->charger_regulators =
942 			&chg_desc_regulator_data[0];
943 		pdata->chg_desc->num_charger_regulators	=
944 			ARRAY_SIZE(chg_desc_regulator_data);
945 		power_devs[3].platform_data = pdata->chg_desc;
946 		power_devs[3].pdata_size = sizeof(*pdata->chg_desc);
947 		ret = mfd_add_devices(chip->dev, 0, &power_devs[3], 1,
948 				      NULL, chip->irq_base, NULL);
949 		if (ret < 0)
950 			dev_err(chip->dev, "Failed to add chg-manager subdev\n");
951 	}
952 }
953 
device_onkey_init(struct pm860x_chip * chip,struct pm860x_platform_data * pdata)954 static void device_onkey_init(struct pm860x_chip *chip,
955 					struct pm860x_platform_data *pdata)
956 {
957 	int ret;
958 
959 	onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
960 	onkey_devs[0].resources = &onkey_resources[0];
961 	ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
962 			      ARRAY_SIZE(onkey_devs), &onkey_resources[0],
963 			      chip->irq_base, NULL);
964 	if (ret < 0)
965 		dev_err(chip->dev, "Failed to add onkey subdev\n");
966 }
967 
device_codec_init(struct pm860x_chip * chip,struct pm860x_platform_data * pdata)968 static void device_codec_init(struct pm860x_chip *chip,
969 					struct pm860x_platform_data *pdata)
970 {
971 	int ret;
972 
973 	codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
974 	codec_devs[0].resources = &codec_resources[0];
975 	ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
976 			      ARRAY_SIZE(codec_devs), &codec_resources[0], 0,
977 			      NULL);
978 	if (ret < 0)
979 		dev_err(chip->dev, "Failed to add codec subdev\n");
980 }
981 
device_8607_init(struct pm860x_chip * chip,struct i2c_client * i2c,struct pm860x_platform_data * pdata)982 static void device_8607_init(struct pm860x_chip *chip,
983 				       struct i2c_client *i2c,
984 				       struct pm860x_platform_data *pdata)
985 {
986 	int data, ret;
987 
988 	ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
989 	if (ret < 0) {
990 		dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
991 		goto out;
992 	}
993 	switch (ret & PM8607_VERSION_MASK) {
994 	case 0x40:
995 	case 0x50:
996 		dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
997 			 ret);
998 		break;
999 	default:
1000 		dev_err(chip->dev,
1001 			"Failed to detect Marvell 88PM8607. Chip ID: %02x\n",
1002 			ret);
1003 		goto out;
1004 	}
1005 
1006 	ret = pm860x_reg_read(i2c, PM8607_BUCK3);
1007 	if (ret < 0) {
1008 		dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
1009 		goto out;
1010 	}
1011 	if (ret & PM8607_BUCK3_DOUBLE)
1012 		chip->buck3_double = 1;
1013 
1014 	ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
1015 	if (ret < 0) {
1016 		dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
1017 		goto out;
1018 	}
1019 
1020 	if (pdata && (pdata->i2c_port == PI2C_PORT))
1021 		data = PM8607_B0_MISC1_PI2C;
1022 	else
1023 		data = 0;
1024 	ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
1025 	if (ret < 0) {
1026 		dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
1027 		goto out;
1028 	}
1029 
1030 	ret = device_irq_init(chip, pdata);
1031 	if (ret < 0)
1032 		goto out;
1033 
1034 	device_regulator_init(chip, pdata);
1035 	device_rtc_init(chip, pdata);
1036 	device_onkey_init(chip, pdata);
1037 	device_touch_init(chip, pdata);
1038 	device_power_init(chip, pdata);
1039 	device_codec_init(chip, pdata);
1040 out:
1041 	return;
1042 }
1043 
device_8606_init(struct pm860x_chip * chip,struct i2c_client * i2c,struct pm860x_platform_data * pdata)1044 static void device_8606_init(struct pm860x_chip *chip,
1045 				       struct i2c_client *i2c,
1046 				       struct pm860x_platform_data *pdata)
1047 {
1048 	device_osc_init(i2c);
1049 	device_bk_init(chip, pdata);
1050 	device_led_init(chip, pdata);
1051 }
1052 
pm860x_device_init(struct pm860x_chip * chip,struct pm860x_platform_data * pdata)1053 static int pm860x_device_init(struct pm860x_chip *chip,
1054 					struct pm860x_platform_data *pdata)
1055 {
1056 	chip->core_irq = 0;
1057 
1058 	switch (chip->id) {
1059 	case CHIP_PM8606:
1060 		device_8606_init(chip, chip->client, pdata);
1061 		break;
1062 	case CHIP_PM8607:
1063 		device_8607_init(chip, chip->client, pdata);
1064 		break;
1065 	}
1066 
1067 	if (chip->companion) {
1068 		switch (chip->id) {
1069 		case CHIP_PM8607:
1070 			device_8606_init(chip, chip->companion, pdata);
1071 			break;
1072 		case CHIP_PM8606:
1073 			device_8607_init(chip, chip->companion, pdata);
1074 			break;
1075 		}
1076 	}
1077 
1078 	return 0;
1079 }
1080 
pm860x_device_exit(struct pm860x_chip * chip)1081 static void pm860x_device_exit(struct pm860x_chip *chip)
1082 {
1083 	device_irq_exit(chip);
1084 	mfd_remove_devices(chip->dev);
1085 }
1086 
verify_addr(struct i2c_client * i2c)1087 static int verify_addr(struct i2c_client *i2c)
1088 {
1089 	unsigned short addr_8607[] = {0x30, 0x34};
1090 	unsigned short addr_8606[] = {0x10, 0x11};
1091 	int size, i;
1092 
1093 	if (i2c == NULL)
1094 		return 0;
1095 	size = ARRAY_SIZE(addr_8606);
1096 	for (i = 0; i < size; i++) {
1097 		if (i2c->addr == *(addr_8606 + i))
1098 			return CHIP_PM8606;
1099 	}
1100 	size = ARRAY_SIZE(addr_8607);
1101 	for (i = 0; i < size; i++) {
1102 		if (i2c->addr == *(addr_8607 + i))
1103 			return CHIP_PM8607;
1104 	}
1105 	return 0;
1106 }
1107 
1108 static const struct regmap_config pm860x_regmap_config = {
1109 	.reg_bits = 8,
1110 	.val_bits = 8,
1111 };
1112 
pm860x_dt_init(struct device_node * np,struct device * dev,struct pm860x_platform_data * pdata)1113 static int pm860x_dt_init(struct device_node *np,
1114 				    struct device *dev,
1115 				    struct pm860x_platform_data *pdata)
1116 {
1117 	int ret;
1118 
1119 	pdata->irq_mode = of_property_read_bool(np, "marvell,88pm860x-irq-read-clr");
1120 	ret = of_property_read_u32(np, "marvell,88pm860x-slave-addr",
1121 				   &pdata->companion_addr);
1122 	if (ret) {
1123 		dev_err(dev,
1124 			"Not found \"marvell,88pm860x-slave-addr\" property\n");
1125 		pdata->companion_addr = 0;
1126 	}
1127 	return 0;
1128 }
1129 
pm860x_probe(struct i2c_client * client)1130 static int pm860x_probe(struct i2c_client *client)
1131 {
1132 	struct pm860x_platform_data *pdata = dev_get_platdata(&client->dev);
1133 	struct device_node *node = client->dev.of_node;
1134 	struct pm860x_chip *chip;
1135 	int ret;
1136 
1137 	if (node && !pdata) {
1138 		/* parse DT to get platform data */
1139 		pdata = devm_kzalloc(&client->dev,
1140 				     sizeof(struct pm860x_platform_data),
1141 				     GFP_KERNEL);
1142 		if (!pdata)
1143 			return -ENOMEM;
1144 		ret = pm860x_dt_init(node, &client->dev, pdata);
1145 		if (ret)
1146 			return ret;
1147 	} else if (!pdata) {
1148 		pr_info("No platform data in %s!\n", __func__);
1149 		return -EINVAL;
1150 	}
1151 
1152 	chip = devm_kzalloc(&client->dev,
1153 			    sizeof(struct pm860x_chip), GFP_KERNEL);
1154 	if (chip == NULL)
1155 		return -ENOMEM;
1156 
1157 	chip->id = verify_addr(client);
1158 	chip->regmap = devm_regmap_init_i2c(client, &pm860x_regmap_config);
1159 	if (IS_ERR(chip->regmap)) {
1160 		ret = PTR_ERR(chip->regmap);
1161 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
1162 				ret);
1163 		return ret;
1164 	}
1165 	chip->client = client;
1166 	i2c_set_clientdata(client, chip);
1167 	chip->dev = &client->dev;
1168 
1169 	/*
1170 	 * Both client and companion client shares same platform driver.
1171 	 * Driver distinguishes them by pdata->companion_addr.
1172 	 * pdata->companion_addr is only assigned if companion chip exists.
1173 	 * At the same time, the companion_addr shouldn't equal to client
1174 	 * address.
1175 	 */
1176 	if (pdata->companion_addr && (pdata->companion_addr != client->addr)) {
1177 		chip->companion_addr = pdata->companion_addr;
1178 		chip->companion = i2c_new_dummy_device(chip->client->adapter,
1179 						chip->companion_addr);
1180 		if (IS_ERR(chip->companion)) {
1181 			dev_err(&client->dev,
1182 				"Failed to allocate I2C companion device\n");
1183 			return PTR_ERR(chip->companion);
1184 		}
1185 		chip->regmap_companion = regmap_init_i2c(chip->companion,
1186 							&pm860x_regmap_config);
1187 		if (IS_ERR(chip->regmap_companion)) {
1188 			ret = PTR_ERR(chip->regmap_companion);
1189 			dev_err(&chip->companion->dev,
1190 				"Failed to allocate register map: %d\n", ret);
1191 			i2c_unregister_device(chip->companion);
1192 			return ret;
1193 		}
1194 		i2c_set_clientdata(chip->companion, chip);
1195 	}
1196 
1197 	pm860x_device_init(chip, pdata);
1198 	return 0;
1199 }
1200 
pm860x_remove(struct i2c_client * client)1201 static void pm860x_remove(struct i2c_client *client)
1202 {
1203 	struct pm860x_chip *chip = i2c_get_clientdata(client);
1204 
1205 	pm860x_device_exit(chip);
1206 	if (chip->companion) {
1207 		regmap_exit(chip->regmap_companion);
1208 		i2c_unregister_device(chip->companion);
1209 	}
1210 }
1211 
pm860x_suspend(struct device * dev)1212 static int pm860x_suspend(struct device *dev)
1213 {
1214 	struct i2c_client *client = to_i2c_client(dev);
1215 	struct pm860x_chip *chip = i2c_get_clientdata(client);
1216 
1217 	if (device_may_wakeup(dev) && chip->wakeup_flag)
1218 		enable_irq_wake(chip->core_irq);
1219 	return 0;
1220 }
1221 
pm860x_resume(struct device * dev)1222 static int pm860x_resume(struct device *dev)
1223 {
1224 	struct i2c_client *client = to_i2c_client(dev);
1225 	struct pm860x_chip *chip = i2c_get_clientdata(client);
1226 
1227 	if (device_may_wakeup(dev) && chip->wakeup_flag)
1228 		disable_irq_wake(chip->core_irq);
1229 	return 0;
1230 }
1231 
1232 static DEFINE_SIMPLE_DEV_PM_OPS(pm860x_pm_ops, pm860x_suspend, pm860x_resume);
1233 
1234 static const struct i2c_device_id pm860x_id_table[] = {
1235 	{ "88PM860x" },
1236 	{}
1237 };
1238 MODULE_DEVICE_TABLE(i2c, pm860x_id_table);
1239 
1240 static const struct of_device_id pm860x_dt_ids[] = {
1241 	{ .compatible = "marvell,88pm860x", },
1242 	{},
1243 };
1244 MODULE_DEVICE_TABLE(of, pm860x_dt_ids);
1245 
1246 static struct i2c_driver pm860x_driver = {
1247 	.driver	= {
1248 		.name	= "88PM860x",
1249 		.pm     = pm_sleep_ptr(&pm860x_pm_ops),
1250 		.of_match_table	= pm860x_dt_ids,
1251 	},
1252 	.probe		= pm860x_probe,
1253 	.remove		= pm860x_remove,
1254 	.id_table	= pm860x_id_table,
1255 };
1256 
pm860x_i2c_init(void)1257 static int __init pm860x_i2c_init(void)
1258 {
1259 	int ret;
1260 
1261 	ret = i2c_add_driver(&pm860x_driver);
1262 	if (ret != 0)
1263 		pr_err("Failed to register 88PM860x I2C driver: %d\n", ret);
1264 	return ret;
1265 }
1266 subsys_initcall(pm860x_i2c_init);
1267 
pm860x_i2c_exit(void)1268 static void __exit pm860x_i2c_exit(void)
1269 {
1270 	i2c_del_driver(&pm860x_driver);
1271 }
1272 module_exit(pm860x_i2c_exit);
1273 
1274 MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
1275 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
1276