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