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