xref: /openbmc/linux/drivers/mfd/88pm860x-core.c (revision 55692af5)
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/i2c.h>
15 #include <linux/irq.h>
16 #include <linux/interrupt.h>
17 #include <linux/platform_device.h>
18 #include <linux/mfd/core.h>
19 #include <linux/mfd/88pm860x.h>
20 #include <linux/regulator/machine.h>
21 
22 #define INT_STATUS_NUM			3
23 
24 static struct resource bk0_resources[] __devinitdata = {
25 	{2, 2, "duty cycle", IORESOURCE_REG, },
26 	{3, 3, "always on",  IORESOURCE_REG, },
27 	{3, 3, "current",    IORESOURCE_REG, },
28 };
29 static struct resource bk1_resources[] __devinitdata = {
30 	{4, 4, "duty cycle", IORESOURCE_REG, },
31 	{5, 5, "always on",  IORESOURCE_REG, },
32 	{5, 5, "current",    IORESOURCE_REG, },
33 };
34 static struct resource bk2_resources[] __devinitdata = {
35 	{6, 6, "duty cycle", IORESOURCE_REG, },
36 	{7, 7, "always on",  IORESOURCE_REG, },
37 	{5, 5, "current",    IORESOURCE_REG, },
38 };
39 
40 static struct resource led0_resources[] __devinitdata = {
41 	/* RGB1 Red LED */
42 	{0xd, 0xd, "control", IORESOURCE_REG, },
43 	{0xc, 0xc, "blink",   IORESOURCE_REG, },
44 };
45 static struct resource led1_resources[] __devinitdata = {
46 	/* RGB1 Green LED */
47 	{0xe, 0xe, "control", IORESOURCE_REG, },
48 	{0xc, 0xc, "blink",   IORESOURCE_REG, },
49 };
50 static struct resource led2_resources[] __devinitdata = {
51 	/* RGB1 Blue LED */
52 	{0xf, 0xf, "control", IORESOURCE_REG, },
53 	{0xc, 0xc, "blink",   IORESOURCE_REG, },
54 };
55 static struct resource led3_resources[] __devinitdata = {
56 	/* RGB2 Red LED */
57 	{0x9, 0x9, "control", IORESOURCE_REG, },
58 	{0x8, 0x8, "blink",   IORESOURCE_REG, },
59 };
60 static struct resource led4_resources[] __devinitdata = {
61 	/* RGB2 Green LED */
62 	{0xa, 0xa, "control", IORESOURCE_REG, },
63 	{0x8, 0x8, "blink",   IORESOURCE_REG, },
64 };
65 static struct resource led5_resources[] __devinitdata = {
66 	/* RGB2 Blue LED */
67 	{0xb, 0xb, "control", IORESOURCE_REG, },
68 	{0x8, 0x8, "blink",   IORESOURCE_REG, },
69 };
70 
71 static struct resource buck1_resources[] __devinitdata = {
72 	{0x24, 0x24, "buck set", IORESOURCE_REG, },
73 };
74 static struct resource buck2_resources[] __devinitdata = {
75 	{0x25, 0x25, "buck set", IORESOURCE_REG, },
76 };
77 static struct resource buck3_resources[] __devinitdata = {
78 	{0x26, 0x26, "buck set", IORESOURCE_REG, },
79 };
80 static struct resource ldo1_resources[] __devinitdata = {
81 	{0x10, 0x10, "ldo set", IORESOURCE_REG, },
82 };
83 static struct resource ldo2_resources[] __devinitdata = {
84 	{0x11, 0x11, "ldo set", IORESOURCE_REG, },
85 };
86 static struct resource ldo3_resources[] __devinitdata = {
87 	{0x12, 0x12, "ldo set", IORESOURCE_REG, },
88 };
89 static struct resource ldo4_resources[] __devinitdata = {
90 	{0x13, 0x13, "ldo set", IORESOURCE_REG, },
91 };
92 static struct resource ldo5_resources[] __devinitdata = {
93 	{0x14, 0x14, "ldo set", IORESOURCE_REG, },
94 };
95 static struct resource ldo6_resources[] __devinitdata = {
96 	{0x15, 0x15, "ldo set", IORESOURCE_REG, },
97 };
98 static struct resource ldo7_resources[] __devinitdata = {
99 	{0x16, 0x16, "ldo set", IORESOURCE_REG, },
100 };
101 static struct resource ldo8_resources[] __devinitdata = {
102 	{0x17, 0x17, "ldo set", IORESOURCE_REG, },
103 };
104 static struct resource ldo9_resources[] __devinitdata = {
105 	{0x18, 0x18, "ldo set", IORESOURCE_REG, },
106 };
107 static struct resource ldo10_resources[] __devinitdata = {
108 	{0x19, 0x19, "ldo set", IORESOURCE_REG, },
109 };
110 static struct resource ldo12_resources[] __devinitdata = {
111 	{0x1a, 0x1a, "ldo set", IORESOURCE_REG, },
112 };
113 static struct resource ldo_vibrator_resources[] __devinitdata = {
114 	{0x28, 0x28, "ldo set", IORESOURCE_REG, },
115 };
116 static struct resource ldo14_resources[] __devinitdata = {
117 	{0x1b, 0x1b, "ldo set", IORESOURCE_REG, },
118 };
119 
120 static struct resource touch_resources[] __devinitdata = {
121 	{PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
122 };
123 
124 static struct resource onkey_resources[] __devinitdata = {
125 	{PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
126 };
127 
128 static struct resource codec_resources[] __devinitdata = {
129 	/* Headset microphone insertion or removal */
130 	{PM8607_IRQ_MICIN,   PM8607_IRQ_MICIN,   "micin",   IORESOURCE_IRQ,},
131 	/* Hook-switch press or release */
132 	{PM8607_IRQ_HOOK,    PM8607_IRQ_HOOK,    "hook",    IORESOURCE_IRQ,},
133 	/* Headset insertion or removal */
134 	{PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
135 	/* Audio short */
136 	{PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short", IORESOURCE_IRQ,},
137 };
138 
139 static struct resource battery_resources[] __devinitdata = {
140 	{PM8607_IRQ_CC,  PM8607_IRQ_CC,  "columb counter", IORESOURCE_IRQ,},
141 	{PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery",        IORESOURCE_IRQ,},
142 };
143 
144 static struct resource charger_resources[] __devinitdata = {
145 	{PM8607_IRQ_CHG,  PM8607_IRQ_CHG,  "charger detect",  IORESOURCE_IRQ,},
146 	{PM8607_IRQ_CHG_DONE,  PM8607_IRQ_CHG_DONE,  "charging done",       IORESOURCE_IRQ,},
147 	{PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging timeout",    IORESOURCE_IRQ,},
148 	{PM8607_IRQ_GPADC1,    PM8607_IRQ_GPADC1,    "battery temperature", IORESOURCE_IRQ,},
149 	{PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
150 	{PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage",    IORESOURCE_IRQ,},
151 };
152 
153 static struct resource preg_resources[] __devinitdata = {
154 	{PM8606_ID_PREG,  PM8606_ID_PREG,  "preg",   IORESOURCE_REG,},
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[] = {
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[] = {
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[] = {
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 	struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
525 	pm860x_irqs[data->irq - chip->irq_base].enable
526 		= pm860x_irqs[data->irq - chip->irq_base].offs;
527 }
528 
529 static void pm860x_irq_disable(struct irq_data *data)
530 {
531 	struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
532 	pm860x_irqs[data->irq - chip->irq_base].enable = 0;
533 }
534 
535 static struct irq_chip pm860x_irq_chip = {
536 	.name		= "88pm860x",
537 	.irq_bus_lock	= pm860x_irq_lock,
538 	.irq_bus_sync_unlock = pm860x_irq_sync_unlock,
539 	.irq_enable	= pm860x_irq_enable,
540 	.irq_disable	= pm860x_irq_disable,
541 };
542 
543 static int __devinit device_gpadc_init(struct pm860x_chip *chip,
544 				       struct pm860x_platform_data *pdata)
545 {
546 	struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
547 				: chip->companion;
548 	int data;
549 	int ret;
550 
551 	/* initialize GPADC without activating it */
552 
553 	if (!pdata || !pdata->touch)
554 		return -EINVAL;
555 
556 	/* set GPADC MISC1 register */
557 	data = 0;
558 	data |= (pdata->touch->gpadc_prebias << 1) & PM8607_GPADC_PREBIAS_MASK;
559 	data |= (pdata->touch->slot_cycle << 3) & PM8607_GPADC_SLOT_CYCLE_MASK;
560 	data |= (pdata->touch->off_scale << 5) & PM8607_GPADC_OFF_SCALE_MASK;
561 	data |= (pdata->touch->sw_cal << 7) & PM8607_GPADC_SW_CAL_MASK;
562 	if (data) {
563 		ret = pm860x_reg_write(i2c, PM8607_GPADC_MISC1, data);
564 		if (ret < 0)
565 			goto out;
566 	}
567 	/* set tsi prebias time */
568 	if (pdata->touch->tsi_prebias) {
569 		data = pdata->touch->tsi_prebias;
570 		ret = pm860x_reg_write(i2c, PM8607_TSI_PREBIAS, data);
571 		if (ret < 0)
572 			goto out;
573 	}
574 	/* set prebias & prechg time of pen detect */
575 	data = 0;
576 	data |= pdata->touch->pen_prebias & PM8607_PD_PREBIAS_MASK;
577 	data |= (pdata->touch->pen_prechg << 5) & PM8607_PD_PRECHG_MASK;
578 	if (data) {
579 		ret = pm860x_reg_write(i2c, PM8607_PD_PREBIAS, data);
580 		if (ret < 0)
581 			goto out;
582 	}
583 
584 	ret = pm860x_set_bits(i2c, PM8607_GPADC_MISC1,
585 			      PM8607_GPADC_EN, PM8607_GPADC_EN);
586 out:
587 	return ret;
588 }
589 
590 static int __devinit device_irq_init(struct pm860x_chip *chip,
591 				     struct pm860x_platform_data *pdata)
592 {
593 	struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
594 				: chip->companion;
595 	unsigned char status_buf[INT_STATUS_NUM];
596 	unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
597 	int i, data, mask, ret = -EINVAL;
598 	int __irq;
599 
600 	if (!pdata || !pdata->irq_base) {
601 		dev_warn(chip->dev, "No interrupt support on IRQ base\n");
602 		return -EINVAL;
603 	}
604 
605 	mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
606 		| PM8607_B0_MISC1_INT_MASK;
607 	data = 0;
608 	chip->irq_mode = 0;
609 	if (pdata && pdata->irq_mode) {
610 		/*
611 		 * irq_mode defines the way of clearing interrupt. If it's 1,
612 		 * clear IRQ by write. Otherwise, clear it by read.
613 		 * This control bit is valid from 88PM8607 B0 steping.
614 		 */
615 		data |= PM8607_B0_MISC1_INT_CLEAR;
616 		chip->irq_mode = 1;
617 	}
618 	ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
619 	if (ret < 0)
620 		goto out;
621 
622 	/* mask all IRQs */
623 	memset(status_buf, 0, INT_STATUS_NUM);
624 	ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
625 				INT_STATUS_NUM, status_buf);
626 	if (ret < 0)
627 		goto out;
628 
629 	if (chip->irq_mode) {
630 		/* clear interrupt status by write */
631 		memset(status_buf, 0xFF, INT_STATUS_NUM);
632 		ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
633 					INT_STATUS_NUM, status_buf);
634 	} else {
635 		/* clear interrupt status by read */
636 		ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
637 					INT_STATUS_NUM, status_buf);
638 	}
639 	if (ret < 0)
640 		goto out;
641 
642 	mutex_init(&chip->irq_lock);
643 	chip->irq_base = pdata->irq_base;
644 	chip->core_irq = i2c->irq;
645 	if (!chip->core_irq)
646 		goto out;
647 
648 	/* register IRQ by genirq */
649 	for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
650 		__irq = i + chip->irq_base;
651 		irq_set_chip_data(__irq, chip);
652 		irq_set_chip_and_handler(__irq, &pm860x_irq_chip,
653 					 handle_edge_irq);
654 		irq_set_nested_thread(__irq, 1);
655 #ifdef CONFIG_ARM
656 		set_irq_flags(__irq, IRQF_VALID);
657 #else
658 		irq_set_noprobe(__irq);
659 #endif
660 	}
661 
662 	ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq, flags,
663 				   "88pm860x", chip);
664 	if (ret) {
665 		dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
666 		chip->core_irq = 0;
667 	}
668 
669 	return 0;
670 out:
671 	chip->core_irq = 0;
672 	return ret;
673 }
674 
675 static void device_irq_exit(struct pm860x_chip *chip)
676 {
677 	if (chip->core_irq)
678 		free_irq(chip->core_irq, chip);
679 }
680 
681 int pm8606_osc_enable(struct pm860x_chip *chip, unsigned short client)
682 {
683 	int ret = -EIO;
684 	struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
685 		chip->client : chip->companion;
686 
687 	dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
688 	dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
689 			__func__, chip->osc_vote,
690 			chip->osc_status);
691 
692 	mutex_lock(&chip->osc_lock);
693 	/* Update voting status */
694 	chip->osc_vote |= client;
695 	/* If reference group is off - turn on*/
696 	if (chip->osc_status != PM8606_REF_GP_OSC_ON) {
697 		chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
698 		/* Enable Reference group Vsys */
699 		if (pm860x_set_bits(i2c, PM8606_VSYS,
700 				PM8606_VSYS_EN, PM8606_VSYS_EN))
701 			goto out;
702 
703 		/*Enable Internal Oscillator */
704 		if (pm860x_set_bits(i2c, PM8606_MISC,
705 				PM8606_MISC_OSC_EN, PM8606_MISC_OSC_EN))
706 			goto out;
707 		/* Update status (only if writes succeed) */
708 		chip->osc_status = PM8606_REF_GP_OSC_ON;
709 	}
710 	mutex_unlock(&chip->osc_lock);
711 
712 	dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
713 			__func__, chip->osc_vote,
714 			chip->osc_status, ret);
715 	return 0;
716 out:
717 	mutex_unlock(&chip->osc_lock);
718 	return ret;
719 }
720 EXPORT_SYMBOL(pm8606_osc_enable);
721 
722 int pm8606_osc_disable(struct pm860x_chip *chip, unsigned short client)
723 {
724 	int ret = -EIO;
725 	struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
726 		chip->client : chip->companion;
727 
728 	dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
729 	dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
730 			__func__, chip->osc_vote,
731 			chip->osc_status);
732 
733 	mutex_lock(&chip->osc_lock);
734 	/*Update voting status */
735 	chip->osc_vote &= ~(client);
736 	/* If reference group is off and this is the last client to release
737 	 * - turn off */
738 	if ((chip->osc_status != PM8606_REF_GP_OSC_OFF) &&
739 			(chip->osc_vote == REF_GP_NO_CLIENTS)) {
740 		chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
741 		/* Disable Reference group Vsys */
742 		if (pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0))
743 			goto out;
744 		/* Disable Internal Oscillator */
745 		if (pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0))
746 			goto out;
747 		chip->osc_status = PM8606_REF_GP_OSC_OFF;
748 	}
749 	mutex_unlock(&chip->osc_lock);
750 
751 	dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
752 			__func__, chip->osc_vote,
753 			chip->osc_status, ret);
754 	return 0;
755 out:
756 	mutex_unlock(&chip->osc_lock);
757 	return ret;
758 }
759 EXPORT_SYMBOL(pm8606_osc_disable);
760 
761 static void __devinit device_osc_init(struct i2c_client *i2c)
762 {
763 	struct pm860x_chip *chip = i2c_get_clientdata(i2c);
764 
765 	mutex_init(&chip->osc_lock);
766 	/* init portofino reference group voting and status */
767 	/* Disable Reference group Vsys */
768 	pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0);
769 	/* Disable Internal Oscillator */
770 	pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0);
771 
772 	chip->osc_vote = REF_GP_NO_CLIENTS;
773 	chip->osc_status = PM8606_REF_GP_OSC_OFF;
774 }
775 
776 static void __devinit device_bk_init(struct pm860x_chip *chip,
777 				     struct pm860x_platform_data *pdata)
778 {
779 	int ret, i;
780 
781 	if (pdata && pdata->backlight) {
782 		if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
783 			pdata->num_backlights = ARRAY_SIZE(bk_devs);
784 		for (i = 0; i < pdata->num_backlights; i++) {
785 			bk_devs[i].platform_data = &pdata->backlight[i];
786 			bk_devs[i].pdata_size =
787 				sizeof(struct pm860x_backlight_pdata);
788 		}
789 	}
790 	ret = mfd_add_devices(chip->dev, 0, bk_devs,
791 			      ARRAY_SIZE(bk_devs), NULL, 0, NULL);
792 	if (ret < 0)
793 		dev_err(chip->dev, "Failed to add backlight subdev\n");
794 }
795 
796 static void __devinit device_led_init(struct pm860x_chip *chip,
797 				      struct pm860x_platform_data *pdata)
798 {
799 	int ret, i;
800 
801 	if (pdata && pdata->led) {
802 		if (pdata->num_leds > ARRAY_SIZE(led_devs))
803 			pdata->num_leds = ARRAY_SIZE(led_devs);
804 		for (i = 0; i < pdata->num_leds; i++) {
805 			led_devs[i].platform_data = &pdata->led[i];
806 			led_devs[i].pdata_size =
807 				sizeof(struct pm860x_led_pdata);
808 		}
809 	}
810 	ret = mfd_add_devices(chip->dev, 0, led_devs,
811 			      ARRAY_SIZE(led_devs), NULL, 0, NULL);
812 	if (ret < 0) {
813 		dev_err(chip->dev, "Failed to add led subdev\n");
814 		return;
815 	}
816 }
817 
818 static void __devinit device_regulator_init(struct pm860x_chip *chip,
819 					    struct pm860x_platform_data *pdata)
820 {
821 	int ret;
822 
823 	if (pdata == NULL)
824 		return;
825 	if (pdata->buck1) {
826 		reg_devs[0].platform_data = pdata->buck1;
827 		reg_devs[0].pdata_size = sizeof(struct regulator_init_data);
828 	}
829 	if (pdata->buck2) {
830 		reg_devs[1].platform_data = pdata->buck2;
831 		reg_devs[1].pdata_size = sizeof(struct regulator_init_data);
832 	}
833 	if (pdata->buck3) {
834 		reg_devs[2].platform_data = pdata->buck3;
835 		reg_devs[2].pdata_size = sizeof(struct regulator_init_data);
836 	}
837 	if (pdata->ldo1) {
838 		reg_devs[3].platform_data = pdata->ldo1;
839 		reg_devs[3].pdata_size = sizeof(struct regulator_init_data);
840 	}
841 	if (pdata->ldo2) {
842 		reg_devs[4].platform_data = pdata->ldo2;
843 		reg_devs[4].pdata_size = sizeof(struct regulator_init_data);
844 	}
845 	if (pdata->ldo3) {
846 		reg_devs[5].platform_data = pdata->ldo3;
847 		reg_devs[5].pdata_size = sizeof(struct regulator_init_data);
848 	}
849 	if (pdata->ldo4) {
850 		reg_devs[6].platform_data = pdata->ldo4;
851 		reg_devs[6].pdata_size = sizeof(struct regulator_init_data);
852 	}
853 	if (pdata->ldo5) {
854 		reg_devs[7].platform_data = pdata->ldo5;
855 		reg_devs[7].pdata_size = sizeof(struct regulator_init_data);
856 	}
857 	if (pdata->ldo6) {
858 		reg_devs[8].platform_data = pdata->ldo6;
859 		reg_devs[8].pdata_size = sizeof(struct regulator_init_data);
860 	}
861 	if (pdata->ldo7) {
862 		reg_devs[9].platform_data = pdata->ldo7;
863 		reg_devs[9].pdata_size = sizeof(struct regulator_init_data);
864 	}
865 	if (pdata->ldo8) {
866 		reg_devs[10].platform_data = pdata->ldo8;
867 		reg_devs[10].pdata_size = sizeof(struct regulator_init_data);
868 	}
869 	if (pdata->ldo9) {
870 		reg_devs[11].platform_data = pdata->ldo9;
871 		reg_devs[11].pdata_size = sizeof(struct regulator_init_data);
872 	}
873 	if (pdata->ldo10) {
874 		reg_devs[12].platform_data = pdata->ldo10;
875 		reg_devs[12].pdata_size = sizeof(struct regulator_init_data);
876 	}
877 	if (pdata->ldo12) {
878 		reg_devs[13].platform_data = pdata->ldo12;
879 		reg_devs[13].pdata_size = sizeof(struct regulator_init_data);
880 	}
881 	if (pdata->ldo_vibrator) {
882 		reg_devs[14].platform_data = pdata->ldo_vibrator;
883 		reg_devs[14].pdata_size = sizeof(struct regulator_init_data);
884 	}
885 	if (pdata->ldo14) {
886 		reg_devs[15].platform_data = pdata->ldo14;
887 		reg_devs[15].pdata_size = sizeof(struct regulator_init_data);
888 	}
889 	ret = mfd_add_devices(chip->dev, 0, reg_devs,
890 			      ARRAY_SIZE(reg_devs), NULL, 0, NULL);
891 	if (ret < 0) {
892 		dev_err(chip->dev, "Failed to add regulator subdev\n");
893 		return;
894 	}
895 }
896 
897 static void __devinit device_rtc_init(struct pm860x_chip *chip,
898 				      struct pm860x_platform_data *pdata)
899 {
900 	int ret;
901 
902 	if ((pdata == NULL))
903 		return;
904 
905 	rtc_devs[0].platform_data = pdata->rtc;
906 	rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
907 	rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
908 	rtc_devs[0].resources = &rtc_resources[0];
909 	ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
910 			      ARRAY_SIZE(rtc_devs), &rtc_resources[0],
911 			      chip->irq_base, NULL);
912 	if (ret < 0)
913 		dev_err(chip->dev, "Failed to add rtc subdev\n");
914 }
915 
916 static void __devinit device_touch_init(struct pm860x_chip *chip,
917 					struct pm860x_platform_data *pdata)
918 {
919 	int ret;
920 
921 	if (pdata == NULL)
922 		return;
923 
924 	touch_devs[0].platform_data = pdata->touch;
925 	touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
926 	touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
927 	touch_devs[0].resources = &touch_resources[0];
928 	ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
929 			      ARRAY_SIZE(touch_devs), &touch_resources[0],
930 			      chip->irq_base, NULL);
931 	if (ret < 0)
932 		dev_err(chip->dev, "Failed to add touch subdev\n");
933 }
934 
935 static void __devinit device_power_init(struct pm860x_chip *chip,
936 					struct pm860x_platform_data *pdata)
937 {
938 	int ret;
939 
940 	if (pdata == NULL)
941 		return;
942 
943 	power_devs[0].platform_data = pdata->power;
944 	power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
945 	power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
946 	power_devs[0].resources = &battery_resources[0],
947 	ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
948 			      &battery_resources[0], chip->irq_base, NULL);
949 	if (ret < 0)
950 		dev_err(chip->dev, "Failed to add battery subdev\n");
951 
952 	power_devs[1].platform_data = pdata->power;
953 	power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
954 	power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
955 	power_devs[1].resources = &charger_resources[0],
956 	ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
957 			      &charger_resources[0], chip->irq_base, NULL);
958 	if (ret < 0)
959 		dev_err(chip->dev, "Failed to add charger subdev\n");
960 
961 	power_devs[2].platform_data = &preg_init_data;
962 	power_devs[2].pdata_size = sizeof(struct regulator_init_data);
963 	power_devs[2].num_resources = ARRAY_SIZE(preg_resources);
964 	power_devs[2].resources = &preg_resources[0],
965 	ret = mfd_add_devices(chip->dev, 0, &power_devs[2], 1,
966 			      &preg_resources[0], chip->irq_base, NULL);
967 	if (ret < 0)
968 		dev_err(chip->dev, "Failed to add preg subdev\n");
969 }
970 
971 static void __devinit device_onkey_init(struct pm860x_chip *chip,
972 					struct pm860x_platform_data *pdata)
973 {
974 	int ret;
975 
976 	onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
977 	onkey_devs[0].resources = &onkey_resources[0],
978 	ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
979 			      ARRAY_SIZE(onkey_devs), &onkey_resources[0],
980 			      chip->irq_base, NULL);
981 	if (ret < 0)
982 		dev_err(chip->dev, "Failed to add onkey subdev\n");
983 }
984 
985 static void __devinit device_codec_init(struct pm860x_chip *chip,
986 					struct pm860x_platform_data *pdata)
987 {
988 	int ret;
989 
990 	codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
991 	codec_devs[0].resources = &codec_resources[0],
992 	ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
993 			      ARRAY_SIZE(codec_devs), &codec_resources[0], 0,
994 			      NULL);
995 	if (ret < 0)
996 		dev_err(chip->dev, "Failed to add codec subdev\n");
997 }
998 
999 static void __devinit device_8607_init(struct pm860x_chip *chip,
1000 				       struct i2c_client *i2c,
1001 				       struct pm860x_platform_data *pdata)
1002 {
1003 	int data, ret;
1004 
1005 	ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
1006 	if (ret < 0) {
1007 		dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
1008 		goto out;
1009 	}
1010 	switch (ret & PM8607_VERSION_MASK) {
1011 	case 0x40:
1012 	case 0x50:
1013 		dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
1014 			 ret);
1015 		break;
1016 	default:
1017 		dev_err(chip->dev, "Failed to detect Marvell 88PM8607. "
1018 			"Chip ID: %02x\n", ret);
1019 		goto out;
1020 	}
1021 
1022 	ret = pm860x_reg_read(i2c, PM8607_BUCK3);
1023 	if (ret < 0) {
1024 		dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
1025 		goto out;
1026 	}
1027 	if (ret & PM8607_BUCK3_DOUBLE)
1028 		chip->buck3_double = 1;
1029 
1030 	ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
1031 	if (ret < 0) {
1032 		dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
1033 		goto out;
1034 	}
1035 
1036 	if (pdata && (pdata->i2c_port == PI2C_PORT))
1037 		data = PM8607_B0_MISC1_PI2C;
1038 	else
1039 		data = 0;
1040 	ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
1041 	if (ret < 0) {
1042 		dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
1043 		goto out;
1044 	}
1045 
1046 	ret = device_gpadc_init(chip, pdata);
1047 	if (ret < 0)
1048 		goto out;
1049 
1050 	ret = device_irq_init(chip, pdata);
1051 	if (ret < 0)
1052 		goto out;
1053 
1054 	device_regulator_init(chip, pdata);
1055 	device_rtc_init(chip, pdata);
1056 	device_onkey_init(chip, pdata);
1057 	device_touch_init(chip, pdata);
1058 	device_power_init(chip, pdata);
1059 	device_codec_init(chip, pdata);
1060 out:
1061 	return;
1062 }
1063 
1064 static void __devinit device_8606_init(struct pm860x_chip *chip,
1065 				       struct i2c_client *i2c,
1066 				       struct pm860x_platform_data *pdata)
1067 {
1068 	device_osc_init(i2c);
1069 	device_bk_init(chip, pdata);
1070 	device_led_init(chip, pdata);
1071 }
1072 
1073 int __devinit pm860x_device_init(struct pm860x_chip *chip,
1074 		       struct pm860x_platform_data *pdata)
1075 {
1076 	chip->core_irq = 0;
1077 
1078 	switch (chip->id) {
1079 	case CHIP_PM8606:
1080 		device_8606_init(chip, chip->client, pdata);
1081 		break;
1082 	case CHIP_PM8607:
1083 		device_8607_init(chip, chip->client, pdata);
1084 		break;
1085 	}
1086 
1087 	if (chip->companion) {
1088 		switch (chip->id) {
1089 		case CHIP_PM8607:
1090 			device_8606_init(chip, chip->companion, pdata);
1091 			break;
1092 		case CHIP_PM8606:
1093 			device_8607_init(chip, chip->companion, pdata);
1094 			break;
1095 		}
1096 	}
1097 
1098 	return 0;
1099 }
1100 
1101 void __devexit pm860x_device_exit(struct pm860x_chip *chip)
1102 {
1103 	device_irq_exit(chip);
1104 	mfd_remove_devices(chip->dev);
1105 }
1106 
1107 MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
1108 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
1109 MODULE_LICENSE("GPL");
1110