2 * Base driver for Marvell 88PM8607
4 * Copyright (C) 2009 Marvell International Ltd.
6 * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/irq.h>
18 #include <linux/interrupt.h>
19 #include <linux/irqdomain.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
25 #include <linux/mfd/core.h>
26 #include <linux/mfd/88pm860x.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/power/charger-manager.h>
30 #define INT_STATUS_NUM 3
32 static struct resource bk0_resources[] = {
33 {2, 2, "duty cycle", IORESOURCE_REG, },
34 {3, 3, "always on", IORESOURCE_REG, },
35 {3, 3, "current", IORESOURCE_REG, },
37 static struct resource bk1_resources[] = {
38 {4, 4, "duty cycle", IORESOURCE_REG, },
39 {5, 5, "always on", IORESOURCE_REG, },
40 {5, 5, "current", IORESOURCE_REG, },
42 static struct resource bk2_resources[] = {
43 {6, 6, "duty cycle", IORESOURCE_REG, },
44 {7, 7, "always on", IORESOURCE_REG, },
45 {5, 5, "current", IORESOURCE_REG, },
48 static struct resource led0_resources[] = {
50 {0xd, 0xd, "control", IORESOURCE_REG, },
51 {0xc, 0xc, "blink", IORESOURCE_REG, },
53 static struct resource led1_resources[] = {
55 {0xe, 0xe, "control", IORESOURCE_REG, },
56 {0xc, 0xc, "blink", IORESOURCE_REG, },
58 static struct resource led2_resources[] = {
60 {0xf, 0xf, "control", IORESOURCE_REG, },
61 {0xc, 0xc, "blink", IORESOURCE_REG, },
63 static struct resource led3_resources[] = {
65 {0x9, 0x9, "control", IORESOURCE_REG, },
66 {0x8, 0x8, "blink", IORESOURCE_REG, },
68 static struct resource led4_resources[] = {
70 {0xa, 0xa, "control", IORESOURCE_REG, },
71 {0x8, 0x8, "blink", IORESOURCE_REG, },
73 static struct resource led5_resources[] = {
75 {0xb, 0xb, "control", IORESOURCE_REG, },
76 {0x8, 0x8, "blink", IORESOURCE_REG, },
79 static struct resource buck1_resources[] = {
80 {0x24, 0x24, "buck set", IORESOURCE_REG, },
82 static struct resource buck2_resources[] = {
83 {0x25, 0x25, "buck set", IORESOURCE_REG, },
85 static struct resource buck3_resources[] = {
86 {0x26, 0x26, "buck set", IORESOURCE_REG, },
88 static struct resource ldo1_resources[] = {
89 {0x10, 0x10, "ldo set", IORESOURCE_REG, },
91 static struct resource ldo2_resources[] = {
92 {0x11, 0x11, "ldo set", IORESOURCE_REG, },
94 static struct resource ldo3_resources[] = {
95 {0x12, 0x12, "ldo set", IORESOURCE_REG, },
97 static struct resource ldo4_resources[] = {
98 {0x13, 0x13, "ldo set", IORESOURCE_REG, },
100 static struct resource ldo5_resources[] = {
101 {0x14, 0x14, "ldo set", IORESOURCE_REG, },
103 static struct resource ldo6_resources[] = {
104 {0x15, 0x15, "ldo set", IORESOURCE_REG, },
106 static struct resource ldo7_resources[] = {
107 {0x16, 0x16, "ldo set", IORESOURCE_REG, },
109 static struct resource ldo8_resources[] = {
110 {0x17, 0x17, "ldo set", IORESOURCE_REG, },
112 static struct resource ldo9_resources[] = {
113 {0x18, 0x18, "ldo set", IORESOURCE_REG, },
115 static struct resource ldo10_resources[] = {
116 {0x19, 0x19, "ldo set", IORESOURCE_REG, },
118 static struct resource ldo12_resources[] = {
119 {0x1a, 0x1a, "ldo set", IORESOURCE_REG, },
121 static struct resource ldo_vibrator_resources[] = {
122 {0x28, 0x28, "ldo set", IORESOURCE_REG, },
124 static struct resource ldo14_resources[] = {
125 {0x1b, 0x1b, "ldo set", IORESOURCE_REG, },
128 static struct resource touch_resources[] = {
129 {PM8607_IRQ_PEN, PM8607_IRQ_PEN, "touch", IORESOURCE_IRQ,},
132 static struct resource onkey_resources[] = {
133 {PM8607_IRQ_ONKEY, PM8607_IRQ_ONKEY, "onkey", IORESOURCE_IRQ,},
136 static struct resource codec_resources[] = {
137 /* Headset microphone insertion or removal */
138 {PM8607_IRQ_MICIN, PM8607_IRQ_MICIN, "micin", IORESOURCE_IRQ,},
139 /* Hook-switch press or release */
140 {PM8607_IRQ_HOOK, PM8607_IRQ_HOOK, "hook", IORESOURCE_IRQ,},
141 /* Headset insertion or removal */
142 {PM8607_IRQ_HEADSET, PM8607_IRQ_HEADSET, "headset", IORESOURCE_IRQ,},
144 {PM8607_IRQ_AUDIO_SHORT, PM8607_IRQ_AUDIO_SHORT, "audio-short",
148 static struct resource battery_resources[] = {
149 {PM8607_IRQ_CC, PM8607_IRQ_CC, "columb counter", IORESOURCE_IRQ,},
150 {PM8607_IRQ_BAT, PM8607_IRQ_BAT, "battery", IORESOURCE_IRQ,},
153 static struct resource charger_resources[] = {
154 {PM8607_IRQ_CHG, PM8607_IRQ_CHG, "charger detect", IORESOURCE_IRQ,},
155 {PM8607_IRQ_CHG_DONE, PM8607_IRQ_CHG_DONE, "charging done",
157 {PM8607_IRQ_CHG_FAIL, PM8607_IRQ_CHG_FAIL, "charging timeout",
159 {PM8607_IRQ_CHG_FAULT, PM8607_IRQ_CHG_FAULT, "charging fault",
161 {PM8607_IRQ_GPADC1, PM8607_IRQ_GPADC1, "battery temperature",
163 {PM8607_IRQ_VBAT, PM8607_IRQ_VBAT, "battery voltage", IORESOURCE_IRQ,},
164 {PM8607_IRQ_VCHG, PM8607_IRQ_VCHG, "vchg voltage", IORESOURCE_IRQ,},
167 static struct resource rtc_resources[] = {
168 {PM8607_IRQ_RTC, PM8607_IRQ_RTC, "rtc", IORESOURCE_IRQ,},
171 static struct mfd_cell bk_devs[] = {
173 .name = "88pm860x-backlight",
175 .num_resources = ARRAY_SIZE(bk0_resources),
176 .resources = bk0_resources,
178 .name = "88pm860x-backlight",
180 .num_resources = ARRAY_SIZE(bk1_resources),
181 .resources = bk1_resources,
183 .name = "88pm860x-backlight",
185 .num_resources = ARRAY_SIZE(bk2_resources),
186 .resources = bk2_resources,
190 static struct mfd_cell led_devs[] = {
192 .name = "88pm860x-led",
194 .num_resources = ARRAY_SIZE(led0_resources),
195 .resources = led0_resources,
197 .name = "88pm860x-led",
199 .num_resources = ARRAY_SIZE(led1_resources),
200 .resources = led1_resources,
202 .name = "88pm860x-led",
204 .num_resources = ARRAY_SIZE(led2_resources),
205 .resources = led2_resources,
207 .name = "88pm860x-led",
209 .num_resources = ARRAY_SIZE(led3_resources),
210 .resources = led3_resources,
212 .name = "88pm860x-led",
214 .num_resources = ARRAY_SIZE(led4_resources),
215 .resources = led4_resources,
217 .name = "88pm860x-led",
219 .num_resources = ARRAY_SIZE(led5_resources),
220 .resources = led5_resources,
224 static struct mfd_cell reg_devs[] = {
226 .name = "88pm860x-regulator",
228 .num_resources = ARRAY_SIZE(buck1_resources),
229 .resources = buck1_resources,
231 .name = "88pm860x-regulator",
233 .num_resources = ARRAY_SIZE(buck2_resources),
234 .resources = buck2_resources,
236 .name = "88pm860x-regulator",
238 .num_resources = ARRAY_SIZE(buck3_resources),
239 .resources = buck3_resources,
241 .name = "88pm860x-regulator",
243 .num_resources = ARRAY_SIZE(ldo1_resources),
244 .resources = ldo1_resources,
246 .name = "88pm860x-regulator",
248 .num_resources = ARRAY_SIZE(ldo2_resources),
249 .resources = ldo2_resources,
251 .name = "88pm860x-regulator",
253 .num_resources = ARRAY_SIZE(ldo3_resources),
254 .resources = ldo3_resources,
256 .name = "88pm860x-regulator",
258 .num_resources = ARRAY_SIZE(ldo4_resources),
259 .resources = ldo4_resources,
261 .name = "88pm860x-regulator",
263 .num_resources = ARRAY_SIZE(ldo5_resources),
264 .resources = ldo5_resources,
266 .name = "88pm860x-regulator",
268 .num_resources = ARRAY_SIZE(ldo6_resources),
269 .resources = ldo6_resources,
271 .name = "88pm860x-regulator",
273 .num_resources = ARRAY_SIZE(ldo7_resources),
274 .resources = ldo7_resources,
276 .name = "88pm860x-regulator",
278 .num_resources = ARRAY_SIZE(ldo8_resources),
279 .resources = ldo8_resources,
281 .name = "88pm860x-regulator",
283 .num_resources = ARRAY_SIZE(ldo9_resources),
284 .resources = ldo9_resources,
286 .name = "88pm860x-regulator",
288 .num_resources = ARRAY_SIZE(ldo10_resources),
289 .resources = ldo10_resources,
291 .name = "88pm860x-regulator",
293 .num_resources = ARRAY_SIZE(ldo12_resources),
294 .resources = ldo12_resources,
296 .name = "88pm860x-regulator",
298 .num_resources = ARRAY_SIZE(ldo_vibrator_resources),
299 .resources = ldo_vibrator_resources,
301 .name = "88pm860x-regulator",
303 .num_resources = ARRAY_SIZE(ldo14_resources),
304 .resources = ldo14_resources,
308 static struct mfd_cell touch_devs[] = {
309 {"88pm860x-touch", -1,},
312 static struct mfd_cell onkey_devs[] = {
313 {"88pm860x-onkey", -1,},
316 static struct mfd_cell codec_devs[] = {
317 {"88pm860x-codec", -1,},
320 static struct regulator_consumer_supply preg_supply[] = {
321 REGULATOR_SUPPLY("preg", "charger-manager"),
324 static struct regulator_init_data preg_init_data = {
325 .num_consumer_supplies = ARRAY_SIZE(preg_supply),
326 .consumer_supplies = &preg_supply[0],
329 static struct charger_regulator chg_desc_regulator_data[] = {
330 { .regulator_name = "preg", },
333 static struct mfd_cell power_devs[] = {
334 {"88pm860x-battery", -1,},
335 {"88pm860x-charger", -1,},
336 {"88pm860x-preg", -1,},
337 {"charger-manager", -1,},
340 static struct mfd_cell rtc_devs[] = {
341 {"88pm860x-rtc", -1,},
345 struct pm860x_irq_data {
348 int enable; /* enable or not */
349 int offs; /* bit offset in mask register */
352 static struct pm860x_irq_data pm860x_irqs[] = {
353 [PM8607_IRQ_ONKEY] = {
354 .reg = PM8607_INT_STATUS1,
355 .mask_reg = PM8607_INT_MASK_1,
358 [PM8607_IRQ_EXTON] = {
359 .reg = PM8607_INT_STATUS1,
360 .mask_reg = PM8607_INT_MASK_1,
364 .reg = PM8607_INT_STATUS1,
365 .mask_reg = PM8607_INT_MASK_1,
369 .reg = PM8607_INT_STATUS1,
370 .mask_reg = PM8607_INT_MASK_1,
374 .reg = PM8607_INT_STATUS1,
375 .mask_reg = PM8607_INT_MASK_1,
379 .reg = PM8607_INT_STATUS1,
380 .mask_reg = PM8607_INT_MASK_1,
383 [PM8607_IRQ_VBAT] = {
384 .reg = PM8607_INT_STATUS2,
385 .mask_reg = PM8607_INT_MASK_2,
388 [PM8607_IRQ_VCHG] = {
389 .reg = PM8607_INT_STATUS2,
390 .mask_reg = PM8607_INT_MASK_2,
393 [PM8607_IRQ_VSYS] = {
394 .reg = PM8607_INT_STATUS2,
395 .mask_reg = PM8607_INT_MASK_2,
398 [PM8607_IRQ_TINT] = {
399 .reg = PM8607_INT_STATUS2,
400 .mask_reg = PM8607_INT_MASK_2,
403 [PM8607_IRQ_GPADC0] = {
404 .reg = PM8607_INT_STATUS2,
405 .mask_reg = PM8607_INT_MASK_2,
408 [PM8607_IRQ_GPADC1] = {
409 .reg = PM8607_INT_STATUS2,
410 .mask_reg = PM8607_INT_MASK_2,
413 [PM8607_IRQ_GPADC2] = {
414 .reg = PM8607_INT_STATUS2,
415 .mask_reg = PM8607_INT_MASK_2,
418 [PM8607_IRQ_GPADC3] = {
419 .reg = PM8607_INT_STATUS2,
420 .mask_reg = PM8607_INT_MASK_2,
423 [PM8607_IRQ_AUDIO_SHORT] = {
424 .reg = PM8607_INT_STATUS3,
425 .mask_reg = PM8607_INT_MASK_3,
429 .reg = PM8607_INT_STATUS3,
430 .mask_reg = PM8607_INT_MASK_3,
433 [PM8607_IRQ_HEADSET] = {
434 .reg = PM8607_INT_STATUS3,
435 .mask_reg = PM8607_INT_MASK_3,
438 [PM8607_IRQ_HOOK] = {
439 .reg = PM8607_INT_STATUS3,
440 .mask_reg = PM8607_INT_MASK_3,
443 [PM8607_IRQ_MICIN] = {
444 .reg = PM8607_INT_STATUS3,
445 .mask_reg = PM8607_INT_MASK_3,
448 [PM8607_IRQ_CHG_FAIL] = {
449 .reg = PM8607_INT_STATUS3,
450 .mask_reg = PM8607_INT_MASK_3,
453 [PM8607_IRQ_CHG_DONE] = {
454 .reg = PM8607_INT_STATUS3,
455 .mask_reg = PM8607_INT_MASK_3,
458 [PM8607_IRQ_CHG_FAULT] = {
459 .reg = PM8607_INT_STATUS3,
460 .mask_reg = PM8607_INT_MASK_3,
465 static irqreturn_t pm860x_irq(int irq, void *data)
467 struct pm860x_chip *chip = data;
468 struct pm860x_irq_data *irq_data;
469 struct i2c_client *i2c;
470 int read_reg = -1, value = 0;
473 i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
474 for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
475 irq_data = &pm860x_irqs[i];
476 if (read_reg != irq_data->reg) {
477 read_reg = irq_data->reg;
478 value = pm860x_reg_read(i2c, irq_data->reg);
480 if (value & irq_data->enable)
481 handle_nested_irq(chip->irq_base + i);
486 static void pm860x_irq_lock(struct irq_data *data)
488 struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
490 mutex_lock(&chip->irq_lock);
493 static void pm860x_irq_sync_unlock(struct irq_data *data)
495 struct pm860x_chip *chip = irq_data_get_irq_chip_data(data);
496 struct pm860x_irq_data *irq_data;
497 struct i2c_client *i2c;
498 static unsigned char cached[3] = {0x0, 0x0, 0x0};
499 unsigned char mask[3];
502 i2c = (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
503 /* Load cached value. In initial, all IRQs are masked */
504 for (i = 0; i < 3; i++)
506 for (i = 0; i < ARRAY_SIZE(pm860x_irqs); i++) {
507 irq_data = &pm860x_irqs[i];
508 switch (irq_data->mask_reg) {
509 case PM8607_INT_MASK_1:
510 mask[0] &= ~irq_data->offs;
511 mask[0] |= irq_data->enable;
513 case PM8607_INT_MASK_2:
514 mask[1] &= ~irq_data->offs;
515 mask[1] |= irq_data->enable;
517 case PM8607_INT_MASK_3:
518 mask[2] &= ~irq_data->offs;
519 mask[2] |= irq_data->enable;
522 dev_err(chip->dev, "wrong IRQ\n");
526 /* update mask into registers */
527 for (i = 0; i < 3; i++) {
528 if (mask[i] != cached[i]) {
530 pm860x_reg_write(i2c, PM8607_INT_MASK_1 + i, mask[i]);
534 mutex_unlock(&chip->irq_lock);
537 static void pm860x_irq_enable(struct irq_data *data)
539 pm860x_irqs[data->hwirq].enable = pm860x_irqs[data->hwirq].offs;
542 static void pm860x_irq_disable(struct irq_data *data)
544 pm860x_irqs[data->hwirq].enable = 0;
547 static struct irq_chip pm860x_irq_chip = {
549 .irq_bus_lock = pm860x_irq_lock,
550 .irq_bus_sync_unlock = pm860x_irq_sync_unlock,
551 .irq_enable = pm860x_irq_enable,
552 .irq_disable = pm860x_irq_disable,
555 static int pm860x_irq_domain_map(struct irq_domain *d, unsigned int virq,
558 irq_set_chip_data(virq, d->host_data);
559 irq_set_chip_and_handler(virq, &pm860x_irq_chip, handle_edge_irq);
560 irq_set_nested_thread(virq, 1);
562 set_irq_flags(virq, IRQF_VALID);
564 irq_set_noprobe(virq);
569 static const struct irq_domain_ops pm860x_irq_domain_ops = {
570 .map = pm860x_irq_domain_map,
571 .xlate = irq_domain_xlate_onetwocell,
574 static int device_irq_init(struct pm860x_chip *chip,
575 struct pm860x_platform_data *pdata)
577 struct i2c_client *i2c = (chip->id == CHIP_PM8607) ?
578 chip->client : chip->companion;
579 unsigned char status_buf[INT_STATUS_NUM];
580 unsigned long flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
581 int data, mask, ret = -EINVAL;
582 int nr_irqs, irq_base = -1;
583 struct device_node *node = i2c->dev.of_node;
585 mask = PM8607_B0_MISC1_INV_INT | PM8607_B0_MISC1_INT_CLEAR
586 | PM8607_B0_MISC1_INT_MASK;
589 if (pdata && pdata->irq_mode) {
591 * irq_mode defines the way of clearing interrupt. If it's 1,
592 * clear IRQ by write. Otherwise, clear it by read.
593 * This control bit is valid from 88PM8607 B0 steping.
595 data |= PM8607_B0_MISC1_INT_CLEAR;
598 ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, mask, data);
603 memset(status_buf, 0, INT_STATUS_NUM);
604 ret = pm860x_bulk_write(i2c, PM8607_INT_MASK_1,
605 INT_STATUS_NUM, status_buf);
609 if (chip->irq_mode) {
610 /* clear interrupt status by write */
611 memset(status_buf, 0xFF, INT_STATUS_NUM);
612 ret = pm860x_bulk_write(i2c, PM8607_INT_STATUS1,
613 INT_STATUS_NUM, status_buf);
615 /* clear interrupt status by read */
616 ret = pm860x_bulk_read(i2c, PM8607_INT_STATUS1,
617 INT_STATUS_NUM, status_buf);
622 mutex_init(&chip->irq_lock);
624 if (pdata && pdata->irq_base)
625 irq_base = pdata->irq_base;
626 nr_irqs = ARRAY_SIZE(pm860x_irqs);
627 chip->irq_base = irq_alloc_descs(irq_base, 0, nr_irqs, 0);
628 if (chip->irq_base < 0) {
629 dev_err(&i2c->dev, "Failed to allocate interrupts, ret:%d\n",
634 irq_domain_add_legacy(node, nr_irqs, chip->irq_base, 0,
635 &pm860x_irq_domain_ops, chip);
636 chip->core_irq = i2c->irq;
640 ret = request_threaded_irq(chip->core_irq, NULL, pm860x_irq,
641 flags | IRQF_ONESHOT, "88pm860x", chip);
643 dev_err(chip->dev, "Failed to request IRQ: %d\n", ret);
653 static void device_irq_exit(struct pm860x_chip *chip)
656 free_irq(chip->core_irq, chip);
659 int pm8606_osc_enable(struct pm860x_chip *chip, unsigned short client)
662 struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
663 chip->client : chip->companion;
665 dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
666 dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
667 __func__, chip->osc_vote,
670 mutex_lock(&chip->osc_lock);
671 /* Update voting status */
672 chip->osc_vote |= client;
673 /* If reference group is off - turn on*/
674 if (chip->osc_status != PM8606_REF_GP_OSC_ON) {
675 chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
676 /* Enable Reference group Vsys */
677 if (pm860x_set_bits(i2c, PM8606_VSYS,
678 PM8606_VSYS_EN, PM8606_VSYS_EN))
681 /*Enable Internal Oscillator */
682 if (pm860x_set_bits(i2c, PM8606_MISC,
683 PM8606_MISC_OSC_EN, PM8606_MISC_OSC_EN))
685 /* Update status (only if writes succeed) */
686 chip->osc_status = PM8606_REF_GP_OSC_ON;
688 mutex_unlock(&chip->osc_lock);
690 dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
691 __func__, chip->osc_vote,
692 chip->osc_status, ret);
695 mutex_unlock(&chip->osc_lock);
698 EXPORT_SYMBOL(pm8606_osc_enable);
700 int pm8606_osc_disable(struct pm860x_chip *chip, unsigned short client)
703 struct i2c_client *i2c = (chip->id == CHIP_PM8606) ?
704 chip->client : chip->companion;
706 dev_dbg(chip->dev, "%s(B): client=0x%x\n", __func__, client);
707 dev_dbg(chip->dev, "%s(B): vote=0x%x status=%d\n",
708 __func__, chip->osc_vote,
711 mutex_lock(&chip->osc_lock);
712 /*Update voting status */
713 chip->osc_vote &= ~(client);
714 /* If reference group is off and this is the last client to release
716 if ((chip->osc_status != PM8606_REF_GP_OSC_OFF) &&
717 (chip->osc_vote == REF_GP_NO_CLIENTS)) {
718 chip->osc_status = PM8606_REF_GP_OSC_UNKNOWN;
719 /* Disable Reference group Vsys */
720 if (pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0))
722 /* Disable Internal Oscillator */
723 if (pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0))
725 chip->osc_status = PM8606_REF_GP_OSC_OFF;
727 mutex_unlock(&chip->osc_lock);
729 dev_dbg(chip->dev, "%s(A): vote=0x%x status=%d ret=%d\n",
730 __func__, chip->osc_vote,
731 chip->osc_status, ret);
734 mutex_unlock(&chip->osc_lock);
737 EXPORT_SYMBOL(pm8606_osc_disable);
739 static void device_osc_init(struct i2c_client *i2c)
741 struct pm860x_chip *chip = i2c_get_clientdata(i2c);
743 mutex_init(&chip->osc_lock);
744 /* init portofino reference group voting and status */
745 /* Disable Reference group Vsys */
746 pm860x_set_bits(i2c, PM8606_VSYS, PM8606_VSYS_EN, 0);
747 /* Disable Internal Oscillator */
748 pm860x_set_bits(i2c, PM8606_MISC, PM8606_MISC_OSC_EN, 0);
750 chip->osc_vote = REF_GP_NO_CLIENTS;
751 chip->osc_status = PM8606_REF_GP_OSC_OFF;
754 static void device_bk_init(struct pm860x_chip *chip,
755 struct pm860x_platform_data *pdata)
759 if (pdata && pdata->backlight) {
760 if (pdata->num_backlights > ARRAY_SIZE(bk_devs))
761 pdata->num_backlights = ARRAY_SIZE(bk_devs);
762 for (i = 0; i < pdata->num_backlights; i++) {
763 bk_devs[i].platform_data = &pdata->backlight[i];
764 bk_devs[i].pdata_size =
765 sizeof(struct pm860x_backlight_pdata);
768 ret = mfd_add_devices(chip->dev, 0, bk_devs,
769 ARRAY_SIZE(bk_devs), NULL, 0, NULL);
771 dev_err(chip->dev, "Failed to add backlight subdev\n");
774 static void device_led_init(struct pm860x_chip *chip,
775 struct pm860x_platform_data *pdata)
779 if (pdata && pdata->led) {
780 if (pdata->num_leds > ARRAY_SIZE(led_devs))
781 pdata->num_leds = ARRAY_SIZE(led_devs);
782 for (i = 0; i < pdata->num_leds; i++) {
783 led_devs[i].platform_data = &pdata->led[i];
784 led_devs[i].pdata_size =
785 sizeof(struct pm860x_led_pdata);
788 ret = mfd_add_devices(chip->dev, 0, led_devs,
789 ARRAY_SIZE(led_devs), NULL, 0, NULL);
791 dev_err(chip->dev, "Failed to add led subdev\n");
796 static void device_regulator_init(struct pm860x_chip *chip,
797 struct pm860x_platform_data *pdata)
804 reg_devs[0].platform_data = pdata->buck1;
805 reg_devs[0].pdata_size = sizeof(struct regulator_init_data);
808 reg_devs[1].platform_data = pdata->buck2;
809 reg_devs[1].pdata_size = sizeof(struct regulator_init_data);
812 reg_devs[2].platform_data = pdata->buck3;
813 reg_devs[2].pdata_size = sizeof(struct regulator_init_data);
816 reg_devs[3].platform_data = pdata->ldo1;
817 reg_devs[3].pdata_size = sizeof(struct regulator_init_data);
820 reg_devs[4].platform_data = pdata->ldo2;
821 reg_devs[4].pdata_size = sizeof(struct regulator_init_data);
824 reg_devs[5].platform_data = pdata->ldo3;
825 reg_devs[5].pdata_size = sizeof(struct regulator_init_data);
828 reg_devs[6].platform_data = pdata->ldo4;
829 reg_devs[6].pdata_size = sizeof(struct regulator_init_data);
832 reg_devs[7].platform_data = pdata->ldo5;
833 reg_devs[7].pdata_size = sizeof(struct regulator_init_data);
836 reg_devs[8].platform_data = pdata->ldo6;
837 reg_devs[8].pdata_size = sizeof(struct regulator_init_data);
840 reg_devs[9].platform_data = pdata->ldo7;
841 reg_devs[9].pdata_size = sizeof(struct regulator_init_data);
844 reg_devs[10].platform_data = pdata->ldo8;
845 reg_devs[10].pdata_size = sizeof(struct regulator_init_data);
848 reg_devs[11].platform_data = pdata->ldo9;
849 reg_devs[11].pdata_size = sizeof(struct regulator_init_data);
852 reg_devs[12].platform_data = pdata->ldo10;
853 reg_devs[12].pdata_size = sizeof(struct regulator_init_data);
856 reg_devs[13].platform_data = pdata->ldo12;
857 reg_devs[13].pdata_size = sizeof(struct regulator_init_data);
859 if (pdata->ldo_vibrator) {
860 reg_devs[14].platform_data = pdata->ldo_vibrator;
861 reg_devs[14].pdata_size = sizeof(struct regulator_init_data);
864 reg_devs[15].platform_data = pdata->ldo14;
865 reg_devs[15].pdata_size = sizeof(struct regulator_init_data);
867 ret = mfd_add_devices(chip->dev, 0, reg_devs,
868 ARRAY_SIZE(reg_devs), NULL, 0, NULL);
870 dev_err(chip->dev, "Failed to add regulator subdev\n");
875 static void device_rtc_init(struct pm860x_chip *chip,
876 struct pm860x_platform_data *pdata)
883 rtc_devs[0].platform_data = pdata->rtc;
884 rtc_devs[0].pdata_size = sizeof(struct pm860x_rtc_pdata);
885 rtc_devs[0].num_resources = ARRAY_SIZE(rtc_resources);
886 rtc_devs[0].resources = &rtc_resources[0];
887 ret = mfd_add_devices(chip->dev, 0, &rtc_devs[0],
888 ARRAY_SIZE(rtc_devs), &rtc_resources[0],
889 chip->irq_base, NULL);
891 dev_err(chip->dev, "Failed to add rtc subdev\n");
894 static void device_touch_init(struct pm860x_chip *chip,
895 struct pm860x_platform_data *pdata)
902 touch_devs[0].platform_data = pdata->touch;
903 touch_devs[0].pdata_size = sizeof(struct pm860x_touch_pdata);
904 touch_devs[0].num_resources = ARRAY_SIZE(touch_resources);
905 touch_devs[0].resources = &touch_resources[0];
906 ret = mfd_add_devices(chip->dev, 0, &touch_devs[0],
907 ARRAY_SIZE(touch_devs), &touch_resources[0],
908 chip->irq_base, NULL);
910 dev_err(chip->dev, "Failed to add touch subdev\n");
913 static void device_power_init(struct pm860x_chip *chip,
914 struct pm860x_platform_data *pdata)
921 power_devs[0].platform_data = pdata->power;
922 power_devs[0].pdata_size = sizeof(struct pm860x_power_pdata);
923 power_devs[0].num_resources = ARRAY_SIZE(battery_resources);
924 power_devs[0].resources = &battery_resources[0],
925 ret = mfd_add_devices(chip->dev, 0, &power_devs[0], 1,
926 &battery_resources[0], chip->irq_base, NULL);
928 dev_err(chip->dev, "Failed to add battery subdev\n");
930 power_devs[1].platform_data = pdata->power;
931 power_devs[1].pdata_size = sizeof(struct pm860x_power_pdata);
932 power_devs[1].num_resources = ARRAY_SIZE(charger_resources);
933 power_devs[1].resources = &charger_resources[0],
934 ret = mfd_add_devices(chip->dev, 0, &power_devs[1], 1,
935 &charger_resources[0], chip->irq_base, NULL);
937 dev_err(chip->dev, "Failed to add charger subdev\n");
939 power_devs[2].platform_data = &preg_init_data;
940 power_devs[2].pdata_size = sizeof(struct regulator_init_data);
941 ret = mfd_add_devices(chip->dev, 0, &power_devs[2], 1,
942 NULL, chip->irq_base, NULL);
944 dev_err(chip->dev, "Failed to add preg subdev\n");
946 if (pdata->chg_desc) {
947 pdata->chg_desc->charger_regulators =
948 &chg_desc_regulator_data[0];
949 pdata->chg_desc->num_charger_regulators =
950 ARRAY_SIZE(chg_desc_regulator_data),
951 power_devs[3].platform_data = pdata->chg_desc;
952 power_devs[3].pdata_size = sizeof(*pdata->chg_desc);
953 ret = mfd_add_devices(chip->dev, 0, &power_devs[3], 1,
954 NULL, chip->irq_base, NULL);
956 dev_err(chip->dev, "Failed to add chg-manager subdev\n");
960 static void device_onkey_init(struct pm860x_chip *chip,
961 struct pm860x_platform_data *pdata)
965 onkey_devs[0].num_resources = ARRAY_SIZE(onkey_resources);
966 onkey_devs[0].resources = &onkey_resources[0],
967 ret = mfd_add_devices(chip->dev, 0, &onkey_devs[0],
968 ARRAY_SIZE(onkey_devs), &onkey_resources[0],
969 chip->irq_base, NULL);
971 dev_err(chip->dev, "Failed to add onkey subdev\n");
974 static void device_codec_init(struct pm860x_chip *chip,
975 struct pm860x_platform_data *pdata)
979 codec_devs[0].num_resources = ARRAY_SIZE(codec_resources);
980 codec_devs[0].resources = &codec_resources[0],
981 ret = mfd_add_devices(chip->dev, 0, &codec_devs[0],
982 ARRAY_SIZE(codec_devs), &codec_resources[0], 0,
985 dev_err(chip->dev, "Failed to add codec subdev\n");
988 static void device_8607_init(struct pm860x_chip *chip,
989 struct i2c_client *i2c,
990 struct pm860x_platform_data *pdata)
994 ret = pm860x_reg_read(i2c, PM8607_CHIP_ID);
996 dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
999 switch (ret & PM8607_VERSION_MASK) {
1002 dev_info(chip->dev, "Marvell 88PM8607 (ID: %02x) detected\n",
1007 "Failed to detect Marvell 88PM8607. Chip ID: %02x\n",
1012 ret = pm860x_reg_read(i2c, PM8607_BUCK3);
1014 dev_err(chip->dev, "Failed to read BUCK3 register: %d\n", ret);
1017 if (ret & PM8607_BUCK3_DOUBLE)
1018 chip->buck3_double = 1;
1020 ret = pm860x_reg_read(i2c, PM8607_B0_MISC1);
1022 dev_err(chip->dev, "Failed to read MISC1 register: %d\n", ret);
1026 if (pdata && (pdata->i2c_port == PI2C_PORT))
1027 data = PM8607_B0_MISC1_PI2C;
1030 ret = pm860x_set_bits(i2c, PM8607_B0_MISC1, PM8607_B0_MISC1_PI2C, data);
1032 dev_err(chip->dev, "Failed to access MISC1:%d\n", ret);
1036 ret = device_irq_init(chip, pdata);
1040 device_regulator_init(chip, pdata);
1041 device_rtc_init(chip, pdata);
1042 device_onkey_init(chip, pdata);
1043 device_touch_init(chip, pdata);
1044 device_power_init(chip, pdata);
1045 device_codec_init(chip, pdata);
1050 static void device_8606_init(struct pm860x_chip *chip,
1051 struct i2c_client *i2c,
1052 struct pm860x_platform_data *pdata)
1054 device_osc_init(i2c);
1055 device_bk_init(chip, pdata);
1056 device_led_init(chip, pdata);
1059 static int pm860x_device_init(struct pm860x_chip *chip,
1060 struct pm860x_platform_data *pdata)
1066 device_8606_init(chip, chip->client, pdata);
1069 device_8607_init(chip, chip->client, pdata);
1073 if (chip->companion) {
1076 device_8606_init(chip, chip->companion, pdata);
1079 device_8607_init(chip, chip->companion, pdata);
1087 static void pm860x_device_exit(struct pm860x_chip *chip)
1089 device_irq_exit(chip);
1090 mfd_remove_devices(chip->dev);
1093 static int verify_addr(struct i2c_client *i2c)
1095 unsigned short addr_8607[] = {0x30, 0x34};
1096 unsigned short addr_8606[] = {0x10, 0x11};
1101 size = ARRAY_SIZE(addr_8606);
1102 for (i = 0; i < size; i++) {
1103 if (i2c->addr == *(addr_8606 + i))
1106 size = ARRAY_SIZE(addr_8607);
1107 for (i = 0; i < size; i++) {
1108 if (i2c->addr == *(addr_8607 + i))
1114 static const struct regmap_config pm860x_regmap_config = {
1119 static int pm860x_dt_init(struct device_node *np,
1121 struct pm860x_platform_data *pdata)
1125 if (of_get_property(np, "marvell,88pm860x-irq-read-clr", NULL))
1126 pdata->irq_mode = 1;
1127 ret = of_property_read_u32(np, "marvell,88pm860x-slave-addr",
1128 &pdata->companion_addr);
1131 "Not found \"marvell,88pm860x-slave-addr\" property\n");
1132 pdata->companion_addr = 0;
1137 static int pm860x_probe(struct i2c_client *client,
1138 const struct i2c_device_id *id)
1140 struct pm860x_platform_data *pdata = dev_get_platdata(&client->dev);
1141 struct device_node *node = client->dev.of_node;
1142 struct pm860x_chip *chip;
1145 if (node && !pdata) {
1146 /* parse DT to get platform data */
1147 pdata = devm_kzalloc(&client->dev,
1148 sizeof(struct pm860x_platform_data),
1152 ret = pm860x_dt_init(node, &client->dev, pdata);
1155 } else if (!pdata) {
1156 pr_info("No platform data in %s!\n", __func__);
1160 chip = devm_kzalloc(&client->dev,
1161 sizeof(struct pm860x_chip), GFP_KERNEL);
1165 chip->id = verify_addr(client);
1166 chip->regmap = devm_regmap_init_i2c(client, &pm860x_regmap_config);
1167 if (IS_ERR(chip->regmap)) {
1168 ret = PTR_ERR(chip->regmap);
1169 dev_err(&client->dev, "Failed to allocate register map: %d\n",
1173 chip->client = client;
1174 i2c_set_clientdata(client, chip);
1175 chip->dev = &client->dev;
1176 dev_set_drvdata(chip->dev, chip);
1179 * Both client and companion client shares same platform driver.
1180 * Driver distinguishes them by pdata->companion_addr.
1181 * pdata->companion_addr is only assigned if companion chip exists.
1182 * At the same time, the companion_addr shouldn't equal to client
1185 if (pdata->companion_addr && (pdata->companion_addr != client->addr)) {
1186 chip->companion_addr = pdata->companion_addr;
1187 chip->companion = i2c_new_dummy(chip->client->adapter,
1188 chip->companion_addr);
1189 if (!chip->companion) {
1190 dev_err(&client->dev,
1191 "Failed to allocate I2C companion device\n");
1194 chip->regmap_companion = regmap_init_i2c(chip->companion,
1195 &pm860x_regmap_config);
1196 if (IS_ERR(chip->regmap_companion)) {
1197 ret = PTR_ERR(chip->regmap_companion);
1198 dev_err(&chip->companion->dev,
1199 "Failed to allocate register map: %d\n", ret);
1200 i2c_unregister_device(chip->companion);
1203 i2c_set_clientdata(chip->companion, chip);
1206 pm860x_device_init(chip, pdata);
1210 static int pm860x_remove(struct i2c_client *client)
1212 struct pm860x_chip *chip = i2c_get_clientdata(client);
1214 pm860x_device_exit(chip);
1215 if (chip->companion) {
1216 regmap_exit(chip->regmap_companion);
1217 i2c_unregister_device(chip->companion);
1222 #ifdef CONFIG_PM_SLEEP
1223 static int pm860x_suspend(struct device *dev)
1225 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
1226 struct pm860x_chip *chip = i2c_get_clientdata(client);
1228 if (device_may_wakeup(dev) && chip->wakeup_flag)
1229 enable_irq_wake(chip->core_irq);
1233 static int pm860x_resume(struct device *dev)
1235 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
1236 struct pm860x_chip *chip = i2c_get_clientdata(client);
1238 if (device_may_wakeup(dev) && chip->wakeup_flag)
1239 disable_irq_wake(chip->core_irq);
1244 static SIMPLE_DEV_PM_OPS(pm860x_pm_ops, pm860x_suspend, pm860x_resume);
1246 static const struct i2c_device_id pm860x_id_table[] = {
1250 MODULE_DEVICE_TABLE(i2c, pm860x_id_table);
1252 static const struct of_device_id pm860x_dt_ids[] = {
1253 { .compatible = "marvell,88pm860x", },
1256 MODULE_DEVICE_TABLE(of, pm860x_dt_ids);
1258 static struct i2c_driver pm860x_driver = {
1261 .owner = THIS_MODULE,
1262 .pm = &pm860x_pm_ops,
1263 .of_match_table = pm860x_dt_ids,
1265 .probe = pm860x_probe,
1266 .remove = pm860x_remove,
1267 .id_table = pm860x_id_table,
1270 static int __init pm860x_i2c_init(void)
1273 ret = i2c_add_driver(&pm860x_driver);
1275 pr_err("Failed to register 88PM860x I2C driver: %d\n", ret);
1278 subsys_initcall(pm860x_i2c_init);
1280 static void __exit pm860x_i2c_exit(void)
1282 i2c_del_driver(&pm860x_driver);
1284 module_exit(pm860x_i2c_exit);
1286 MODULE_DESCRIPTION("PMIC Driver for Marvell 88PM860x");
1287 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
1288 MODULE_LICENSE("GPL");