2 * Base driver for Marvell 88PM8607
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
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.
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>
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>
28 #define INT_STATUS_NUM 3
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, },
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, },
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, },
46 static struct resource led0_resources[] __devinitdata = {
48 {0xd, 0xd, "control", IORESOURCE_REG, },
49 {0xc, 0xc, "blink", IORESOURCE_REG, },
51 static struct resource led1_resources[] __devinitdata = {
53 {0xe, 0xe, "control", IORESOURCE_REG, },
54 {0xc, 0xc, "blink", IORESOURCE_REG, },
56 static struct resource led2_resources[] __devinitdata = {
58 {0xf, 0xf, "control", IORESOURCE_REG, },
59 {0xc, 0xc, "blink", IORESOURCE_REG, },
61 static struct resource led3_resources[] __devinitdata = {
63 {0x9, 0x9, "control", IORESOURCE_REG, },
64 {0x8, 0x8, "blink", IORESOURCE_REG, },
66 static struct resource led4_resources[] __devinitdata = {
68 {0xa, 0xa, "control", IORESOURCE_REG, },
69 {0x8, 0x8, "blink", IORESOURCE_REG, },
71 static struct resource led5_resources[] __devinitdata = {
73 {0xb, 0xb, "control", IORESOURCE_REG, },
74 {0x8, 0x8, "blink", IORESOURCE_REG, },
77 static struct resource buck1_resources[] __devinitdata = {
78 {0x24, 0x24, "buck set", IORESOURCE_REG, },
80 static struct resource buck2_resources[] __devinitdata = {
81 {0x25, 0x25, "buck set", IORESOURCE_REG, },
83 static struct resource buck3_resources[] __devinitdata = {
84 {0x26, 0x26, "buck set", IORESOURCE_REG, },
86 static struct resource ldo1_resources[] __devinitdata = {
87 {0x10, 0x10, "ldo set", IORESOURCE_REG, },
89 static struct resource ldo2_resources[] __devinitdata = {
90 {0x11, 0x11, "ldo set", IORESOURCE_REG, },
92 static struct resource ldo3_resources[] __devinitdata = {
93 {0x12, 0x12, "ldo set", IORESOURCE_REG, },
95 static struct resource ldo4_resources[] __devinitdata = {
96 {0x13, 0x13, "ldo set", IORESOURCE_REG, },
98 static struct resource ldo5_resources[] __devinitdata = {
99 {0x14, 0x14, "ldo set", IORESOURCE_REG, },
101 static struct resource ldo6_resources[] __devinitdata = {
102 {0x15, 0x15, "ldo set", IORESOURCE_REG, },
104 static struct resource ldo7_resources[] __devinitdata = {
105 {0x16, 0x16, "ldo set", IORESOURCE_REG, },
107 static struct resource ldo8_resources[] __devinitdata = {
108 {0x17, 0x17, "ldo set", IORESOURCE_REG, },
110 static struct resource ldo9_resources[] __devinitdata = {
111 {0x18, 0x18, "ldo set", IORESOURCE_REG, },
113 static struct resource ldo10_resources[] __devinitdata = {
114 {0x19, 0x19, "ldo set", IORESOURCE_REG, },
116 static struct resource ldo12_resources[] __devinitdata = {
117 {0x1a, 0x1a, "ldo set", IORESOURCE_REG, },
119 static struct resource ldo_vibrator_resources[] __devinitdata = {
120 {0x28, 0x28, "ldo set", IORESOURCE_REG, },
122 static struct resource ldo14_resources[] __devinitdata = {
123 {0x1b, 0x1b, "ldo set", IORESOURCE_REG, },
126 static struct resource touch_resources[] __devinitdata = {
127 {PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
130 static struct resource onkey_resources[] __devinitdata = {
131 {PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
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,},
142 {PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short", IORESOURCE_IRQ,},
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,},
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,},
159 static struct resource rtc_resources[] __devinitdata = {
160 {PM8607_IRQ_RTC, PM8607_IRQ_RTC, "rtc", IORESOURCE_IRQ,},
163 static struct mfd_cell bk_devs[] __devinitdata = {
165 .name = "88pm860x-backlight",
167 .num_resources = ARRAY_SIZE(bk0_resources),
168 .resources = bk0_resources,
170 .name = "88pm860x-backlight",
172 .num_resources = ARRAY_SIZE(bk1_resources),
173 .resources = bk1_resources,
175 .name = "88pm860x-backlight",
177 .num_resources = ARRAY_SIZE(bk2_resources),
178 .resources = bk2_resources,
182 static struct mfd_cell led_devs[] __devinitdata = {
184 .name = "88pm860x-led",
186 .num_resources = ARRAY_SIZE(led0_resources),
187 .resources = led0_resources,
189 .name = "88pm860x-led",
191 .num_resources = ARRAY_SIZE(led1_resources),
192 .resources = led1_resources,
194 .name = "88pm860x-led",
196 .num_resources = ARRAY_SIZE(led2_resources),
197 .resources = led2_resources,
199 .name = "88pm860x-led",
201 .num_resources = ARRAY_SIZE(led3_resources),
202 .resources = led3_resources,
204 .name = "88pm860x-led",
206 .num_resources = ARRAY_SIZE(led4_resources),
207 .resources = led4_resources,
209 .name = "88pm860x-led",
211 .num_resources = ARRAY_SIZE(led5_resources),
212 .resources = led5_resources,
216 static struct mfd_cell reg_devs[] __devinitdata = {
218 .name = "88pm860x-regulator",
220 .num_resources = ARRAY_SIZE(buck1_resources),
221 .resources = buck1_resources,
223 .name = "88pm860x-regulator",
225 .num_resources = ARRAY_SIZE(buck2_resources),
226 .resources = buck2_resources,
228 .name = "88pm860x-regulator",
230 .num_resources = ARRAY_SIZE(buck3_resources),
231 .resources = buck3_resources,
233 .name = "88pm860x-regulator",
235 .num_resources = ARRAY_SIZE(ldo1_resources),
236 .resources = ldo1_resources,
238 .name = "88pm860x-regulator",
240 .num_resources = ARRAY_SIZE(ldo2_resources),
241 .resources = ldo2_resources,
243 .name = "88pm860x-regulator",
245 .num_resources = ARRAY_SIZE(ldo3_resources),
246 .resources = ldo3_resources,
248 .name = "88pm860x-regulator",
250 .num_resources = ARRAY_SIZE(ldo4_resources),
251 .resources = ldo4_resources,
253 .name = "88pm860x-regulator",
255 .num_resources = ARRAY_SIZE(ldo5_resources),
256 .resources = ldo5_resources,
258 .name = "88pm860x-regulator",
260 .num_resources = ARRAY_SIZE(ldo6_resources),
261 .resources = ldo6_resources,
263 .name = "88pm860x-regulator",
265 .num_resources = ARRAY_SIZE(ldo7_resources),
266 .resources = ldo7_resources,
268 .name = "88pm860x-regulator",
270 .num_resources = ARRAY_SIZE(ldo8_resources),
271 .resources = ldo8_resources,
273 .name = "88pm860x-regulator",
275 .num_resources = ARRAY_SIZE(ldo9_resources),
276 .resources = ldo9_resources,
278 .name = "88pm860x-regulator",
280 .num_resources = ARRAY_SIZE(ldo10_resources),
281 .resources = ldo10_resources,
283 .name = "88pm860x-regulator",
285 .num_resources = ARRAY_SIZE(ldo12_resources),
286 .resources = ldo12_resources,
288 .name = "88pm860x-regulator",
290 .num_resources = ARRAY_SIZE(ldo_vibrator_resources),
291 .resources = ldo_vibrator_resources,
293 .name = "88pm860x-regulator",
295 .num_resources = ARRAY_SIZE(ldo14_resources),
296 .resources = ldo14_resources,
300 static struct mfd_cell touch_devs[] = {
301 {"88pm860x-touch", -1,},
304 static struct mfd_cell onkey_devs[] = {
305 {"88pm860x-onkey", -1,},
308 static struct mfd_cell codec_devs[] = {
309 {"88pm860x-codec", -1,},
312 static struct regulator_consumer_supply preg_supply[] = {
313 REGULATOR_SUPPLY("preg", "charger-manager"),
316 static struct regulator_init_data preg_init_data = {
317 .num_consumer_supplies = ARRAY_SIZE(preg_supply),
318 .consumer_supplies = &preg_supply[0],
321 static struct mfd_cell power_devs[] = {
322 {"88pm860x-battery", -1,},
323 {"88pm860x-charger", -1,},
324 {"88pm860x-preg", -1,},
327 static struct mfd_cell rtc_devs[] = {
328 {"88pm860x-rtc", -1,},
332 struct pm860x_irq_data {
335 int enable; /* enable or not */
336 int offs; /* bit offset in mask register */
339 static struct pm860x_irq_data pm860x_irqs[] = {
340 [PM8607_IRQ_ONKEY] = {
341 .reg = PM8607_INT_STATUS1,
342 .mask_reg = PM8607_INT_MASK_1,
345 [PM8607_IRQ_EXTON] = {
346 .reg = PM8607_INT_STATUS1,
347 .mask_reg = PM8607_INT_MASK_1,
351 .reg = PM8607_INT_STATUS1,
352 .mask_reg = PM8607_INT_MASK_1,
356 .reg = PM8607_INT_STATUS1,
357 .mask_reg = PM8607_INT_MASK_1,
361 .reg = PM8607_INT_STATUS1,
362 .mask_reg = PM8607_INT_MASK_1,
366 .reg = PM8607_INT_STATUS1,
367 .mask_reg = PM8607_INT_MASK_1,
370 [PM8607_IRQ_VBAT] = {
371 .reg = PM8607_INT_STATUS2,
372 .mask_reg = PM8607_INT_MASK_2,
375 [PM8607_IRQ_VCHG] = {
376 .reg = PM8607_INT_STATUS2,
377 .mask_reg = PM8607_INT_MASK_2,
380 [PM8607_IRQ_VSYS] = {
381 .reg = PM8607_INT_STATUS2,
382 .mask_reg = PM8607_INT_MASK_2,
385 [PM8607_IRQ_TINT] = {
386 .reg = PM8607_INT_STATUS2,
387 .mask_reg = PM8607_INT_MASK_2,
390 [PM8607_IRQ_GPADC0] = {
391 .reg = PM8607_INT_STATUS2,
392 .mask_reg = PM8607_INT_MASK_2,
395 [PM8607_IRQ_GPADC1] = {
396 .reg = PM8607_INT_STATUS2,
397 .mask_reg = PM8607_INT_MASK_2,
400 [PM8607_IRQ_GPADC2] = {
401 .reg = PM8607_INT_STATUS2,
402 .mask_reg = PM8607_INT_MASK_2,
405 [PM8607_IRQ_GPADC3] = {
406 .reg = PM8607_INT_STATUS2,
407 .mask_reg = PM8607_INT_MASK_2,
410 [PM8607_IRQ_AUDIO_SHORT] = {
411 .reg = PM8607_INT_STATUS3,
412 .mask_reg = PM8607_INT_MASK_3,
416 .reg = PM8607_INT_STATUS3,
417 .mask_reg = PM8607_INT_MASK_3,
420 [PM8607_IRQ_HEADSET] = {
421 .reg = PM8607_INT_STATUS3,
422 .mask_reg = PM8607_INT_MASK_3,
425 [PM8607_IRQ_HOOK] = {
426 .reg = PM8607_INT_STATUS3,
427 .mask_reg = PM8607_INT_MASK_3,
430 [PM8607_IRQ_MICIN] = {
431 .reg = PM8607_INT_STATUS3,
432 .mask_reg = PM8607_INT_MASK_3,
435 [PM8607_IRQ_CHG_FAIL] = {
436 .reg = PM8607_INT_STATUS3,
437 .mask_reg = PM8607_INT_MASK_3,
440 [PM8607_IRQ_CHG_DONE] = {
441 .reg = PM8607_INT_STATUS3,
442 .mask_reg = PM8607_INT_MASK_3,
445 [PM8607_IRQ_CHG_FAULT] = {
446 .reg = PM8607_INT_STATUS3,
447 .mask_reg = PM8607_INT_MASK_3,
452 static irqreturn_t pm860x_irq(int irq, void *data)
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;
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);
467 if (value & irq_data->enable)
468 handle_nested_irq(chip->irq_base + i);
473 static void pm860x_irq_lock(struct irq_data *data)
475 struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
477 mutex_lock(&chip->irq_lock);
480 static void pm860x_irq_sync_unlock(struct irq_data *data)
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];
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++)
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;
500 case PM8607_INT_MASK_2:
501 mask[1] &= ~irq_data->offs;
502 mask[1] |= irq_data->enable;
504 case PM8607_INT_MASK_3:
505 mask[2] &= ~irq_data->offs;
506 mask[2] |= irq_data->enable;
509 dev_err(chip->dev, "wrong IRQ\n");
513 /* update mask into registers */
514 for (i = 0; i < 3; i++) {
515 if (mask[i] != cached[i]) {
517 pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
521 mutex_unlock(&chip->irq_lock);
524 static void pm860x_irq_enable(struct irq_data *data)
526 pm860x_irqs[data->hwirq].enable = pm860x_irqs[data->hwirq].offs;
529 static void pm860x_irq_disable(struct irq_data *data)
531 pm860x_irqs[data->hwirq].enable = 0;
534 static struct irq_chip pm860x_irq_chip = {
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,
542 static int pm860x_irq_domain_map(struct irq_domain *d, unsigned int virq,
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);
549 set_irq_flags(virq, IRQF_VALID);
551 irq_set_noprobe(virq);
556 static struct irq_domain_ops pm860x_irq_domain_ops = {
557 .map = pm860x_irq_domain_map,
558 .xlate = irq_domain_xlate_onetwocell,
561 static int __devinit device_irq_init(struct pm860x_chip *chip,
562 struct pm860x_platform_data *pdata)
564 struct i2c_client *i2c = (chip->id == CHIP_PM8607) ? chip->client \
566 unsigned char status_buf[INT_STATUS_NUM];
567 unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
568 int data, mask, ret = -EINVAL;
569 int nr_irqs, irq_base = -1;
570 struct device_node *node = i2c->dev.of_node;
572 mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
573 | PM8607_B0_MISC1_INT_MASK;
576 if (pdata && pdata->irq_mode) {
578 * irq_mode defines the way of clearing interrupt. If it's 1,
579 * clear IRQ by write. Otherwise, clear it by read.
580 * This control bit is valid from 88PM8607 B0 steping.
582 data |= PM8607_B0_MISC1_INT_CLEAR;
585 ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
590 memset(status_buf, 0, INT_STATUS_NUM);
591 ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
592 INT_STATUS_NUM, status_buf);
596 if (chip->irq_mode) {
597 /* clear interrupt status by write */
598 memset(status_buf, 0xFF, INT_STATUS_NUM);
599 ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
600 INT_STATUS_NUM, status_buf);
602 /* clear interrupt status by read */
603 ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
604 INT_STATUS_NUM, status_buf);
609 mutex_init(&chip->irq_lock);
611 if (pdata && pdata->irq_base)
612 irq_base = pdata->irq_base;
613 nr_irqs = ARRAY_SIZE(pm860x_irqs);
614 chip->irq_base = irq_alloc_descs(irq_base, 0, nr_irqs, 0);
615 if (chip->irq_base < 0) {
616 dev_err(&i2c->dev, "Failed to allocate interrupts, ret:%d\n",
621 irq_domain_add_legacy(node, nr_irqs, chip->irq_base, 0,
622 &pm860x_irq_domain_ops, chip);
623 chip->core_irq = i2c->irq;
627 ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq, flags | IRQF_ONESHOT,
630 dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
640 static void device_irq_exit(struct pm860x_chip *chip)
643 free_irq(chip->core_irq, chip);
646 int pm8606_osc_enable(struct pm860x_chip *chip, unsigned short client)
649 struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
650 chip->client : chip->companion;
652 dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
653 dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
654 __func__, chip->osc_vote,
657 mutex_lock(&chip->osc_lock);
658 /* Update voting status */
659 chip->osc_vote |= client;
660 /* If reference group is off - turn on*/
661 if (chip->osc_status != PM8606_REF_GP_OSC_ON) {
662 chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
663 /* Enable Reference group Vsys */
664 if (pm860x_set_bits(i2c, PM8606_VSYS,
665 PM8606_VSYS_EN, PM8606_VSYS_EN))
668 /*Enable Internal Oscillator */
669 if (pm860x_set_bits(i2c, PM8606_MISC,
670 PM8606_MISC_OSC_EN, PM8606_MISC_OSC_EN))
672 /* Update status (only if writes succeed) */
673 chip->osc_status = PM8606_REF_GP_OSC_ON;
675 mutex_unlock(&chip->osc_lock);
677 dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
678 __func__, chip->osc_vote,
679 chip->osc_status, ret);
682 mutex_unlock(&chip->osc_lock);
685 EXPORT_SYMBOL(pm8606_osc_enable);
687 int pm8606_osc_disable(struct pm860x_chip *chip, unsigned short client)
690 struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
691 chip->client : chip->companion;
693 dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
694 dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
695 __func__, chip->osc_vote,
698 mutex_lock(&chip->osc_lock);
699 /*Update voting status */
700 chip->osc_vote &= ~(client);
701 /* If reference group is off and this is the last client to release
703 if ((chip->osc_status != PM8606_REF_GP_OSC_OFF) &&
704 (chip->osc_vote == REF_GP_NO_CLIENTS)) {
705 chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
706 /* Disable Reference group Vsys */
707 if (pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0))
709 /* Disable Internal Oscillator */
710 if (pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0))
712 chip->osc_status = PM8606_REF_GP_OSC_OFF;
714 mutex_unlock(&chip->osc_lock);
716 dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
717 __func__, chip->osc_vote,
718 chip->osc_status, ret);
721 mutex_unlock(&chip->osc_lock);
724 EXPORT_SYMBOL(pm8606_osc_disable);
726 static void __devinit device_osc_init(struct i2c_client *i2c)
728 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
730 mutex_init(&chip->osc_lock);
731 /* init portofino reference group voting and status */
732 /* Disable Reference group Vsys */
733 pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0);
734 /* Disable Internal Oscillator */
735 pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0);
737 chip->osc_vote = REF_GP_NO_CLIENTS;
738 chip->osc_status = PM8606_REF_GP_OSC_OFF;
741 static void __devinit device_bk_init(struct pm860x_chip *chip,
742 struct pm860x_platform_data *pdata)
746 if (pdata && pdata->backlight) {
747 if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
748 pdata->num_backlights = ARRAY_SIZE(bk_devs);
749 for (i = 0; i < pdata->num_backlights; i++) {
750 bk_devs[i].platform_data = &pdata->backlight[i];
751 bk_devs[i].pdata_size =
752 sizeof(struct pm860x_backlight_pdata);
755 ret = mfd_add_devices(chip->dev, 0, bk_devs,
756 ARRAY_SIZE(bk_devs), NULL, 0, NULL);
758 dev_err(chip->dev, "Failed to add backlight subdev\n");
761 static void __devinit device_led_init(struct pm860x_chip *chip,
762 struct pm860x_platform_data *pdata)
766 if (pdata && pdata->led) {
767 if (pdata->num_leds > ARRAY_SIZE(led_devs))
768 pdata->num_leds = ARRAY_SIZE(led_devs);
769 for (i = 0; i < pdata->num_leds; i++) {
770 led_devs[i].platform_data = &pdata->led[i];
771 led_devs[i].pdata_size =
772 sizeof(struct pm860x_led_pdata);
775 ret = mfd_add_devices(chip->dev, 0, led_devs,
776 ARRAY_SIZE(led_devs), NULL, 0, NULL);
778 dev_err(chip->dev, "Failed to add led subdev\n");
783 static void __devinit device_regulator_init(struct pm860x_chip *chip,
784 struct pm860x_platform_data *pdata)
791 reg_devs[0].platform_data = pdata->buck1;
792 reg_devs[0].pdata_size = sizeof(struct regulator_init_data);
795 reg_devs[1].platform_data = pdata->buck2;
796 reg_devs[1].pdata_size = sizeof(struct regulator_init_data);
799 reg_devs[2].platform_data = pdata->buck3;
800 reg_devs[2].pdata_size = sizeof(struct regulator_init_data);
803 reg_devs[3].platform_data = pdata->ldo1;
804 reg_devs[3].pdata_size = sizeof(struct regulator_init_data);
807 reg_devs[4].platform_data = pdata->ldo2;
808 reg_devs[4].pdata_size = sizeof(struct regulator_init_data);
811 reg_devs[5].platform_data = pdata->ldo3;
812 reg_devs[5].pdata_size = sizeof(struct regulator_init_data);
815 reg_devs[6].platform_data = pdata->ldo4;
816 reg_devs[6].pdata_size = sizeof(struct regulator_init_data);
819 reg_devs[7].platform_data = pdata->ldo5;
820 reg_devs[7].pdata_size = sizeof(struct regulator_init_data);
823 reg_devs[8].platform_data = pdata->ldo6;
824 reg_devs[8].pdata_size = sizeof(struct regulator_init_data);
827 reg_devs[9].platform_data = pdata->ldo7;
828 reg_devs[9].pdata_size = sizeof(struct regulator_init_data);
831 reg_devs[10].platform_data = pdata->ldo8;
832 reg_devs[10].pdata_size = sizeof(struct regulator_init_data);
835 reg_devs[11].platform_data = pdata->ldo9;
836 reg_devs[11].pdata_size = sizeof(struct regulator_init_data);
839 reg_devs[12].platform_data = pdata->ldo10;
840 reg_devs[12].pdata_size = sizeof(struct regulator_init_data);
843 reg_devs[13].platform_data = pdata->ldo12;
844 reg_devs[13].pdata_size = sizeof(struct regulator_init_data);
846 if (pdata->ldo_vibrator) {
847 reg_devs[14].platform_data = pdata->ldo_vibrator;
848 reg_devs[14].pdata_size = sizeof(struct regulator_init_data);
851 reg_devs[15].platform_data = pdata->ldo14;
852 reg_devs[15].pdata_size = sizeof(struct regulator_init_data);
854 ret = mfd_add_devices(chip->dev, 0, reg_devs,
855 ARRAY_SIZE(reg_devs), NULL, 0, NULL);
857 dev_err(chip->dev, "Failed to add regulator subdev\n");
862 static void __devinit device_rtc_init(struct pm860x_chip *chip,
863 struct pm860x_platform_data *pdata)
870 rtc_devs[0].platform_data = pdata->rtc;
871 rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
872 rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
873 rtc_devs[0].resources = &rtc_resources[0];
874 ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
875 ARRAY_SIZE(rtc_devs), &rtc_resources[0],
876 chip->irq_base, NULL);
878 dev_err(chip->dev, "Failed to add rtc subdev\n");
881 static void __devinit device_touch_init(struct pm860x_chip *chip,
882 struct pm860x_platform_data *pdata)
889 touch_devs[0].platform_data = pdata->touch;
890 touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
891 touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
892 touch_devs[0].resources = &touch_resources[0];
893 ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
894 ARRAY_SIZE(touch_devs), &touch_resources[0],
895 chip->irq_base, NULL);
897 dev_err(chip->dev, "Failed to add touch subdev\n");
900 static void __devinit device_power_init(struct pm860x_chip *chip,
901 struct pm860x_platform_data *pdata)
908 power_devs[0].platform_data = pdata->power;
909 power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
910 power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
911 power_devs[0].resources = &battery_resources[0],
912 ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
913 &battery_resources[0], chip->irq_base, NULL);
915 dev_err(chip->dev, "Failed to add battery subdev\n");
917 power_devs[1].platform_data = pdata->power;
918 power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
919 power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
920 power_devs[1].resources = &charger_resources[0],
921 ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
922 &charger_resources[0], chip->irq_base, NULL);
924 dev_err(chip->dev, "Failed to add charger subdev\n");
926 power_devs[2].platform_data = &preg_init_data;
927 power_devs[2].pdata_size = sizeof(struct regulator_init_data);
928 ret = mfd_add_devices(chip->dev, 0, &power_devs[2], 1,
929 NULL, chip->irq_base, NULL);
931 dev_err(chip->dev, "Failed to add preg subdev\n");
934 static void __devinit device_onkey_init(struct pm860x_chip *chip,
935 struct pm860x_platform_data *pdata)
939 onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
940 onkey_devs[0].resources = &onkey_resources[0],
941 ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
942 ARRAY_SIZE(onkey_devs), &onkey_resources[0],
943 chip->irq_base, NULL);
945 dev_err(chip->dev, "Failed to add onkey subdev\n");
948 static void __devinit device_codec_init(struct pm860x_chip *chip,
949 struct pm860x_platform_data *pdata)
953 codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
954 codec_devs[0].resources = &codec_resources[0],
955 ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
956 ARRAY_SIZE(codec_devs), &codec_resources[0], 0,
959 dev_err(chip->dev, "Failed to add codec subdev\n");
962 static void __devinit device_8607_init(struct pm860x_chip *chip,
963 struct i2c_client *i2c,
964 struct pm860x_platform_data *pdata)
968 ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
970 dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
973 switch (ret & PM8607_VERSION_MASK) {
976 dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
980 dev_err(chip->dev, "Failed to detect Marvell 88PM8607. "
981 "Chip ID: %02x\n", ret);
985 ret = pm860x_reg_read(i2c, PM8607_BUCK3);
987 dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
990 if (ret & PM8607_BUCK3_DOUBLE)
991 chip->buck3_double = 1;
993 ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
995 dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
999 if (pdata && (pdata->i2c_port == PI2C_PORT))
1000 data = PM8607_B0_MISC1_PI2C;
1003 ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
1005 dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
1009 ret = device_irq_init(chip, pdata);
1013 device_regulator_init(chip, pdata);
1014 device_rtc_init(chip, pdata);
1015 device_onkey_init(chip, pdata);
1016 device_touch_init(chip, pdata);
1017 device_power_init(chip, pdata);
1018 device_codec_init(chip, pdata);
1023 static void __devinit device_8606_init(struct pm860x_chip *chip,
1024 struct i2c_client *i2c,
1025 struct pm860x_platform_data *pdata)
1027 device_osc_init(i2c);
1028 device_bk_init(chip, pdata);
1029 device_led_init(chip, pdata);
1032 static int __devinit pm860x_device_init(struct pm860x_chip *chip,
1033 struct pm860x_platform_data *pdata)
1039 device_8606_init(chip, chip->client, pdata);
1042 device_8607_init(chip, chip->client, pdata);
1046 if (chip->companion) {
1049 device_8606_init(chip, chip->companion, pdata);
1052 device_8607_init(chip, chip->companion, pdata);
1060 static void __devexit pm860x_device_exit(struct pm860x_chip *chip)
1062 device_irq_exit(chip);
1063 mfd_remove_devices(chip->dev);
1066 static int verify_addr(struct i2c_client *i2c)
1068 unsigned short addr_8607[] = {0x30, 0x34};
1069 unsigned short addr_8606[] = {0x10, 0x11};
1074 size = ARRAY_SIZE(addr_8606);
1075 for (i = 0; i < size; i++) {
1076 if (i2c->addr == *(addr_8606 + i))
1079 size = ARRAY_SIZE(addr_8607);
1080 for (i = 0; i < size; i++) {
1081 if (i2c->addr == *(addr_8607 + i))
1087 static struct regmap_config pm860x_regmap_config = {
1092 static int __devinit pm860x_dt_init(struct device_node *np,
1094 struct pm860x_platform_data *pdata)
1098 if (of_get_property(np, "marvell,88pm860x-irq-read-clr", NULL))
1099 pdata->irq_mode = 1;
1100 ret = of_property_read_u32(np, "marvell,88pm860x-slave-addr",
1101 &pdata->companion_addr);
1103 dev_err(dev, "Not found \"marvell,88pm860x-slave-addr\" "
1105 pdata->companion_addr = 0;
1110 static int __devinit pm860x_probe(struct i2c_client *client,
1111 const struct i2c_device_id *id)
1113 struct pm860x_platform_data *pdata = client->dev.platform_data;
1114 struct device_node *node = client->dev.of_node;
1115 struct pm860x_chip *chip;
1118 if (node && !pdata) {
1119 /* parse DT to get platform data */
1120 pdata = devm_kzalloc(&client->dev,
1121 sizeof(struct pm860x_platform_data),
1125 ret = pm860x_dt_init(node, &client->dev, pdata);
1128 } else if (!pdata) {
1129 pr_info("No platform data in %s!\n", __func__);
1133 chip = kzalloc(sizeof(struct pm860x_chip), GFP_KERNEL);
1139 chip->id = verify_addr(client);
1140 chip->regmap = regmap_init_i2c(client, &pm860x_regmap_config);
1141 if (IS_ERR(chip->regmap)) {
1142 ret = PTR_ERR(chip->regmap);
1143 dev_err(&client->dev, "Failed to allocate register map: %d\n",
1148 chip->client = client;
1149 i2c_set_clientdata(client, chip);
1150 chip->dev = &client->dev;
1151 dev_set_drvdata(chip->dev, chip);
1154 * Both client and companion client shares same platform driver.
1155 * Driver distinguishes them by pdata->companion_addr.
1156 * pdata->companion_addr is only assigned if companion chip exists.
1157 * At the same time, the companion_addr shouldn't equal to client
1160 if (pdata->companion_addr && (pdata->companion_addr != client->addr)) {
1161 chip->companion_addr = pdata->companion_addr;
1162 chip->companion = i2c_new_dummy(chip->client->adapter,
1163 chip->companion_addr);
1164 chip->regmap_companion = regmap_init_i2c(chip->companion,
1165 &pm860x_regmap_config);
1166 if (IS_ERR(chip->regmap_companion)) {
1167 ret = PTR_ERR(chip->regmap_companion);
1168 dev_err(&chip->companion->dev,
1169 "Failed to allocate register map: %d\n", ret);
1172 i2c_set_clientdata(chip->companion, chip);
1175 pm860x_device_init(chip, pdata);
1179 devm_kfree(&client->dev, pdata);
1183 static int __devexit pm860x_remove(struct i2c_client *client)
1185 struct pm860x_chip *chip = i2c_get_clientdata(client);
1187 pm860x_device_exit(chip);
1188 if (chip->companion) {
1189 regmap_exit(chip->regmap_companion);
1190 i2c_unregister_device(chip->companion);
1192 regmap_exit(chip->regmap);
1197 #ifdef CONFIG_PM_SLEEP
1198 static int pm860x_suspend(struct device *dev)
1200 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
1201 struct pm860x_chip *chip = i2c_get_clientdata(client);
1203 if (device_may_wakeup(dev) && chip->wakeup_flag)
1204 enable_irq_wake(chip->core_irq);
1208 static int pm860x_resume(struct device *dev)
1210 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
1211 struct pm860x_chip *chip = i2c_get_clientdata(client);
1213 if (device_may_wakeup(dev) && chip->wakeup_flag)
1214 disable_irq_wake(chip->core_irq);
1219 static SIMPLE_DEV_PM_OPS(pm860x_pm_ops, pm860x_suspend, pm860x_resume);
1221 static const struct i2c_device_id pm860x_id_table[] = {
1225 MODULE_DEVICE_TABLE(i2c, pm860x_id_table);
1227 static const struct of_device_id pm860x_dt_ids[] = {
1228 { .compatible = "marvell,88pm860x", },
1231 MODULE_DEVICE_TABLE(of, pm860x_dt_ids);
1233 static struct i2c_driver pm860x_driver = {
1236 .owner = THIS_MODULE,
1237 .pm = &pm860x_pm_ops,
1238 .of_match_table = of_match_ptr(pm860x_dt_ids),
1240 .probe = pm860x_probe,
1241 .remove = __devexit_p(pm860x_remove),
1242 .id_table = pm860x_id_table,
1245 static int __init pm860x_i2c_init(void)
1248 ret = i2c_add_driver(&pm860x_driver);
1250 pr_err("Failed to register 88PM860x I2C driver: %d\n", ret);
1253 subsys_initcall(pm860x_i2c_init);
1255 static void __exit pm860x_i2c_exit(void)
1257 i2c_del_driver(&pm860x_driver);
1259 module_exit(pm860x_i2c_exit);
1261 MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
1262 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
1263 MODULE_LICENSE("GPL");