2 * Motorola CPCAP PMIC battery charger driver
4 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
6 * Rewritten for Linux power framework with some parts based on
7 * on earlier driver found in the Motorola Linux kernel:
9 * Copyright (C) 2009-2010 Motorola, Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <linux/atomic.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
26 #include <linux/interrupt.h>
27 #include <linux/notifier.h>
29 #include <linux/of_platform.h>
30 #include <linux/platform_device.h>
31 #include <linux/power_supply.h>
32 #include <linux/regmap.h>
34 #include <linux/gpio/consumer.h>
35 #include <linux/usb/phy_companion.h>
36 #include <linux/phy/omap_usb.h>
37 #include <linux/usb/otg.h>
38 #include <linux/iio/consumer.h>
39 #include <linux/mfd/motorola-cpcap.h>
42 * CPCAP_REG_CRM register bits. For documentation of somewhat similar hardware,
43 * see NXP "MC13783 Power Management and Audio Circuit Users's Guide"
44 * MC13783UG.pdf chapter "8.5 Battery Interface Register Summary". The registers
45 * and values for CPCAP are different, but some of the internal components seem
46 * similar. Also see the Motorola Linux kernel cpcap-regbits.h. CPCAP_REG_CHRGR_1
47 * bits that seem to describe the CRM register.
49 #define CPCAP_REG_CRM_UNUSED_641_15 BIT(15) /* 641 = register number */
50 #define CPCAP_REG_CRM_UNUSED_641_14 BIT(14) /* 641 = register number */
51 #define CPCAP_REG_CRM_CHRG_LED_EN BIT(13) /* Charger LED */
52 #define CPCAP_REG_CRM_RVRSMODE BIT(12) /* USB VBUS output enable */
53 #define CPCAP_REG_CRM_ICHRG_TR1 BIT(11) /* Trickle charge current */
54 #define CPCAP_REG_CRM_ICHRG_TR0 BIT(10)
55 #define CPCAP_REG_CRM_FET_OVRD BIT(9) /* 0 = hardware, 1 = FET_CTRL */
56 #define CPCAP_REG_CRM_FET_CTRL BIT(8) /* BPFET 1 if FET_OVRD set */
57 #define CPCAP_REG_CRM_VCHRG3 BIT(7) /* Charge voltage bits */
58 #define CPCAP_REG_CRM_VCHRG2 BIT(6)
59 #define CPCAP_REG_CRM_VCHRG1 BIT(5)
60 #define CPCAP_REG_CRM_VCHRG0 BIT(4)
61 #define CPCAP_REG_CRM_ICHRG3 BIT(3) /* Charge current bits */
62 #define CPCAP_REG_CRM_ICHRG2 BIT(2)
63 #define CPCAP_REG_CRM_ICHRG1 BIT(1)
64 #define CPCAP_REG_CRM_ICHRG0 BIT(0)
66 /* CPCAP_REG_CRM trickle charge voltages */
67 #define CPCAP_REG_CRM_TR(val) (((val) & 0x3) << 10)
68 #define CPCAP_REG_CRM_TR_0A00 CPCAP_REG_CRM_TR(0x0)
69 #define CPCAP_REG_CRM_TR_0A24 CPCAP_REG_CRM_TR(0x1)
70 #define CPCAP_REG_CRM_TR_0A48 CPCAP_REG_CRM_TR(0x2)
71 #define CPCAP_REG_CRM_TR_0A72 CPCAP_REG_CRM_TR(0x4)
74 * CPCAP_REG_CRM charge voltages based on the ADC channel 1 values.
75 * Note that these register bits don't match MC13783UG.pdf VCHRG
78 #define CPCAP_REG_CRM_VCHRG(val) (((val) & 0xf) << 4)
79 #define CPCAP_REG_CRM_VCHRG_3V80 CPCAP_REG_CRM_VCHRG(0x0)
80 #define CPCAP_REG_CRM_VCHRG_4V10 CPCAP_REG_CRM_VCHRG(0x1)
81 #define CPCAP_REG_CRM_VCHRG_4V12 CPCAP_REG_CRM_VCHRG(0x2)
82 #define CPCAP_REG_CRM_VCHRG_4V15 CPCAP_REG_CRM_VCHRG(0x3)
83 #define CPCAP_REG_CRM_VCHRG_4V17 CPCAP_REG_CRM_VCHRG(0x4)
84 #define CPCAP_REG_CRM_VCHRG_4V20 CPCAP_REG_CRM_VCHRG(0x5)
85 #define CPCAP_REG_CRM_VCHRG_4V23 CPCAP_REG_CRM_VCHRG(0x6)
86 #define CPCAP_REG_CRM_VCHRG_4V25 CPCAP_REG_CRM_VCHRG(0x7)
87 #define CPCAP_REG_CRM_VCHRG_4V27 CPCAP_REG_CRM_VCHRG(0x8)
88 #define CPCAP_REG_CRM_VCHRG_4V30 CPCAP_REG_CRM_VCHRG(0x9)
89 #define CPCAP_REG_CRM_VCHRG_4V33 CPCAP_REG_CRM_VCHRG(0xa)
90 #define CPCAP_REG_CRM_VCHRG_4V35 CPCAP_REG_CRM_VCHRG(0xb)
91 #define CPCAP_REG_CRM_VCHRG_4V38 CPCAP_REG_CRM_VCHRG(0xc)
92 #define CPCAP_REG_CRM_VCHRG_4V40 CPCAP_REG_CRM_VCHRG(0xd)
93 #define CPCAP_REG_CRM_VCHRG_4V42 CPCAP_REG_CRM_VCHRG(0xe)
94 #define CPCAP_REG_CRM_VCHRG_4V44 CPCAP_REG_CRM_VCHRG(0xf)
97 * CPCAP_REG_CRM charge currents. These seem to match MC13783UG.pdf
98 * values in "Table 8-3. Charge Path Regulator Current Limit
99 * Characteristics" for the nominal values.
101 #define CPCAP_REG_CRM_ICHRG(val) (((val) & 0xf) << 0)
102 #define CPCAP_REG_CRM_ICHRG_0A000 CPCAP_REG_CRM_ICHRG(0x0)
103 #define CPCAP_REG_CRM_ICHRG_0A070 CPCAP_REG_CRM_ICHRG(0x1)
104 #define CPCAP_REG_CRM_ICHRG_0A177 CPCAP_REG_CRM_ICHRG(0x2)
105 #define CPCAP_REG_CRM_ICHRG_0A266 CPCAP_REG_CRM_ICHRG(0x3)
106 #define CPCAP_REG_CRM_ICHRG_0A355 CPCAP_REG_CRM_ICHRG(0x4)
107 #define CPCAP_REG_CRM_ICHRG_0A443 CPCAP_REG_CRM_ICHRG(0x5)
108 #define CPCAP_REG_CRM_ICHRG_0A532 CPCAP_REG_CRM_ICHRG(0x6)
109 #define CPCAP_REG_CRM_ICHRG_0A621 CPCAP_REG_CRM_ICHRG(0x7)
110 #define CPCAP_REG_CRM_ICHRG_0A709 CPCAP_REG_CRM_ICHRG(0x8)
111 #define CPCAP_REG_CRM_ICHRG_0A798 CPCAP_REG_CRM_ICHRG(0x9)
112 #define CPCAP_REG_CRM_ICHRG_0A886 CPCAP_REG_CRM_ICHRG(0xa)
113 #define CPCAP_REG_CRM_ICHRG_0A975 CPCAP_REG_CRM_ICHRG(0xb)
114 #define CPCAP_REG_CRM_ICHRG_1A064 CPCAP_REG_CRM_ICHRG(0xc)
115 #define CPCAP_REG_CRM_ICHRG_1A152 CPCAP_REG_CRM_ICHRG(0xd)
116 #define CPCAP_REG_CRM_ICHRG_1A596 CPCAP_REG_CRM_ICHRG(0xe)
117 #define CPCAP_REG_CRM_ICHRG_NO_LIMIT CPCAP_REG_CRM_ICHRG(0xf)
120 CPCAP_CHARGER_IIO_BATTDET
,
121 CPCAP_CHARGER_IIO_VOLTAGE
,
122 CPCAP_CHARGER_IIO_VBUS
,
123 CPCAP_CHARGER_IIO_CHRG_CURRENT
,
124 CPCAP_CHARGER_IIO_BATT_CURRENT
,
125 CPCAP_CHARGER_IIO_NR
,
128 struct cpcap_charger_ddata
{
131 struct list_head irq_list
;
132 struct delayed_work detect_work
;
133 struct delayed_work vbus_work
;
134 struct gpio_desc
*gpio
[2]; /* gpio_reven0 & 1 */
136 struct iio_channel
*channels
[CPCAP_CHARGER_IIO_NR
];
138 struct power_supply
*usb
;
140 struct phy_companion comparator
; /* For USB VBUS */
147 struct cpcap_interrupt_desc
{
149 struct list_head node
;
153 struct cpcap_charger_ints_state
{
166 static enum power_supply_property cpcap_charger_props
[] = {
167 POWER_SUPPLY_PROP_STATUS
,
168 POWER_SUPPLY_PROP_ONLINE
,
169 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
170 POWER_SUPPLY_PROP_CURRENT_NOW
,
173 static bool cpcap_charger_battery_found(struct cpcap_charger_ddata
*ddata
)
175 struct iio_channel
*channel
;
178 channel
= ddata
->channels
[CPCAP_CHARGER_IIO_BATTDET
];
179 error
= iio_read_channel_raw(channel
, &value
);
181 dev_warn(ddata
->dev
, "%s failed: %i\n", __func__
, error
);
189 static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata
*ddata
)
191 struct iio_channel
*channel
;
192 int error
, value
= 0;
194 channel
= ddata
->channels
[CPCAP_CHARGER_IIO_VOLTAGE
];
195 error
= iio_read_channel_processed(channel
, &value
);
197 dev_warn(ddata
->dev
, "%s failed: %i\n", __func__
, error
);
205 static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata
*ddata
)
207 struct iio_channel
*channel
;
208 int error
, value
= 0;
210 channel
= ddata
->channels
[CPCAP_CHARGER_IIO_CHRG_CURRENT
];
211 error
= iio_read_channel_processed(channel
, &value
);
213 dev_warn(ddata
->dev
, "%s failed: %i\n", __func__
, error
);
221 static int cpcap_charger_get_property(struct power_supply
*psy
,
222 enum power_supply_property psp
,
223 union power_supply_propval
*val
)
225 struct cpcap_charger_ddata
*ddata
= dev_get_drvdata(psy
->dev
.parent
);
228 case POWER_SUPPLY_PROP_STATUS
:
229 val
->intval
= ddata
->status
;
231 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
232 if (ddata
->status
== POWER_SUPPLY_STATUS_CHARGING
)
233 val
->intval
= cpcap_charger_get_charge_voltage(ddata
) *
238 case POWER_SUPPLY_PROP_CURRENT_NOW
:
239 if (ddata
->status
== POWER_SUPPLY_STATUS_CHARGING
)
240 val
->intval
= cpcap_charger_get_charge_current(ddata
) *
245 case POWER_SUPPLY_PROP_ONLINE
:
246 val
->intval
= ddata
->status
== POWER_SUPPLY_STATUS_CHARGING
;
255 static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata
*ddata
,
261 gpiod_set_value(ddata
->gpio
[0], enabled
);
264 static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata
*ddata
,
270 gpiod_set_value(ddata
->gpio
[1], enabled
);
273 static int cpcap_charger_set_state(struct cpcap_charger_ddata
*ddata
,
274 int max_voltage
, int charge_current
,
280 enable
= (charge_current
|| trickle_current
);
281 dev_dbg(ddata
->dev
, "%s enable: %i\n", __func__
, enable
);
284 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_CRM
,
286 CPCAP_REG_CRM_FET_OVRD
|
287 CPCAP_REG_CRM_FET_CTRL
);
289 ddata
->status
= POWER_SUPPLY_STATUS_UNKNOWN
;
293 ddata
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
298 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_CRM
, 0x3fff,
299 CPCAP_REG_CRM_CHRG_LED_EN
|
301 CPCAP_REG_CRM_FET_OVRD
|
302 CPCAP_REG_CRM_FET_CTRL
|
306 ddata
->status
= POWER_SUPPLY_STATUS_UNKNOWN
;
310 ddata
->status
= POWER_SUPPLY_STATUS_CHARGING
;
315 dev_err(ddata
->dev
, "%s failed with %i\n", __func__
, error
);
320 static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata
*ddata
)
322 int error
, value
= 0;
323 struct iio_channel
*channel
=
324 ddata
->channels
[CPCAP_CHARGER_IIO_VBUS
];
326 error
= iio_read_channel_processed(channel
, &value
);
328 return value
> 3900 ? true : false;
330 dev_err(ddata
->dev
, "error reading VBUS: %i\n", error
);
335 /* VBUS control functions for the USB PHY companion */
337 static void cpcap_charger_vbus_work(struct work_struct
*work
)
339 struct cpcap_charger_ddata
*ddata
;
343 ddata
= container_of(work
, struct cpcap_charger_ddata
,
346 if (ddata
->vbus_enabled
) {
347 vbus
= cpcap_charger_vbus_valid(ddata
);
349 dev_info(ddata
->dev
, "VBUS already provided\n");
354 cpcap_charger_set_cable_path(ddata
, false);
355 cpcap_charger_set_inductive_path(ddata
, false);
357 error
= cpcap_charger_set_state(ddata
, 0, 0, 0);
361 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_CRM
,
362 CPCAP_REG_CRM_RVRSMODE
,
363 CPCAP_REG_CRM_RVRSMODE
);
367 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_CRM
,
368 CPCAP_REG_CRM_RVRSMODE
, 0);
372 cpcap_charger_set_cable_path(ddata
, true);
373 cpcap_charger_set_inductive_path(ddata
, true);
379 dev_err(ddata
->dev
, "%s could not %s vbus: %i\n", __func__
,
380 ddata
->vbus_enabled
? "enable" : "disable", error
);
383 static int cpcap_charger_set_vbus(struct phy_companion
*comparator
,
386 struct cpcap_charger_ddata
*ddata
=
387 container_of(comparator
, struct cpcap_charger_ddata
,
390 ddata
->vbus_enabled
= enabled
;
391 schedule_delayed_work(&ddata
->vbus_work
, 0);
396 /* Charger interrupt handling functions */
398 static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata
*ddata
,
399 struct cpcap_charger_ints_state
*s
)
403 error
= regmap_read(ddata
->reg
, CPCAP_REG_INTS1
, &val
);
407 s
->chrg_det
= val
& BIT(13);
408 s
->rvrs_chrg
= val
& BIT(12);
409 s
->vbusov
= val
& BIT(11);
411 error
= regmap_read(ddata
->reg
, CPCAP_REG_INTS2
, &val
);
415 s
->chrg_se1b
= val
& BIT(13);
416 s
->rvrs_mode
= val
& BIT(6);
417 s
->chrgcurr1
= val
& BIT(4);
418 s
->vbusvld
= val
& BIT(3);
420 error
= regmap_read(ddata
->reg
, CPCAP_REG_INTS4
, &val
);
424 s
->battdetb
= val
& BIT(6);
429 static void cpcap_usb_detect(struct work_struct
*work
)
431 struct cpcap_charger_ddata
*ddata
;
432 struct cpcap_charger_ints_state s
;
435 ddata
= container_of(work
, struct cpcap_charger_ddata
,
438 error
= cpcap_charger_get_ints_state(ddata
, &s
);
442 if (cpcap_charger_vbus_valid(ddata
) && s
.chrgcurr1
) {
445 if (cpcap_charger_battery_found(ddata
))
446 max_current
= CPCAP_REG_CRM_ICHRG_1A596
;
448 max_current
= CPCAP_REG_CRM_ICHRG_0A532
;
450 error
= cpcap_charger_set_state(ddata
,
451 CPCAP_REG_CRM_VCHRG_4V35
,
456 error
= cpcap_charger_set_state(ddata
, 0, 0, 0);
461 power_supply_changed(ddata
->usb
);
465 dev_err(ddata
->dev
, "%s failed with %i\n", __func__
, error
);
468 static irqreturn_t
cpcap_charger_irq_thread(int irq
, void *data
)
470 struct cpcap_charger_ddata
*ddata
= data
;
472 if (!atomic_read(&ddata
->active
))
475 schedule_delayed_work(&ddata
->detect_work
, 0);
480 static int cpcap_usb_init_irq(struct platform_device
*pdev
,
481 struct cpcap_charger_ddata
*ddata
,
484 struct cpcap_interrupt_desc
*d
;
487 irq
= platform_get_irq_byname(pdev
, name
);
491 error
= devm_request_threaded_irq(ddata
->dev
, irq
, NULL
,
492 cpcap_charger_irq_thread
,
496 dev_err(ddata
->dev
, "could not get irq %s: %i\n",
502 d
= devm_kzalloc(ddata
->dev
, sizeof(*d
), GFP_KERNEL
);
508 list_add(&d
->node
, &ddata
->irq_list
);
513 static const char * const cpcap_charger_irqs
[] = {
515 "chrg_det", "rvrs_chrg",
518 "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr1", "vbusvld",
524 static int cpcap_usb_init_interrupts(struct platform_device
*pdev
,
525 struct cpcap_charger_ddata
*ddata
)
529 for (i
= 0; i
< ARRAY_SIZE(cpcap_charger_irqs
); i
++) {
530 error
= cpcap_usb_init_irq(pdev
, ddata
, cpcap_charger_irqs
[i
]);
538 static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata
*ddata
)
542 for (i
= 0; i
< 2; i
++) {
543 ddata
->gpio
[i
] = devm_gpiod_get_index(ddata
->dev
, "mode",
545 if (IS_ERR(ddata
->gpio
[i
])) {
546 dev_info(ddata
->dev
, "no mode change GPIO%i: %li\n",
547 i
, PTR_ERR(ddata
->gpio
[i
]));
548 ddata
->gpio
[i
] = NULL
;
553 static int cpcap_charger_init_iio(struct cpcap_charger_ddata
*ddata
)
555 const char * const names
[CPCAP_CHARGER_IIO_NR
] = {
556 "battdetb", "battp", "vbus", "chg_isense", "batti",
560 for (i
= 0; i
< CPCAP_CHARGER_IIO_NR
; i
++) {
561 ddata
->channels
[i
] = devm_iio_channel_get(ddata
->dev
,
563 if (IS_ERR(ddata
->channels
[i
])) {
564 error
= PTR_ERR(ddata
->channels
[i
]);
568 if (!ddata
->channels
[i
]->indio_dev
) {
577 dev_err(ddata
->dev
, "could not initialize VBUS or ID IIO: %i\n",
583 static const struct power_supply_desc cpcap_charger_usb_desc
= {
585 .type
= POWER_SUPPLY_TYPE_USB
,
586 .properties
= cpcap_charger_props
,
587 .num_properties
= ARRAY_SIZE(cpcap_charger_props
),
588 .get_property
= cpcap_charger_get_property
,
592 static const struct of_device_id cpcap_charger_id_table
[] = {
594 .compatible
= "motorola,mapphone-cpcap-charger",
598 MODULE_DEVICE_TABLE(of
, cpcap_charger_id_table
);
601 static int cpcap_charger_probe(struct platform_device
*pdev
)
603 struct cpcap_charger_ddata
*ddata
;
604 const struct of_device_id
*of_id
;
605 struct power_supply_config psy_cfg
= {};
608 of_id
= of_match_device(of_match_ptr(cpcap_charger_id_table
),
613 ddata
= devm_kzalloc(&pdev
->dev
, sizeof(*ddata
), GFP_KERNEL
);
617 ddata
->dev
= &pdev
->dev
;
619 ddata
->reg
= dev_get_regmap(ddata
->dev
->parent
, NULL
);
623 INIT_LIST_HEAD(&ddata
->irq_list
);
624 INIT_DELAYED_WORK(&ddata
->detect_work
, cpcap_usb_detect
);
625 INIT_DELAYED_WORK(&ddata
->vbus_work
, cpcap_charger_vbus_work
);
626 platform_set_drvdata(pdev
, ddata
);
628 error
= cpcap_charger_init_iio(ddata
);
632 atomic_set(&ddata
->active
, 1);
634 psy_cfg
.of_node
= pdev
->dev
.of_node
;
635 psy_cfg
.drv_data
= ddata
;
637 ddata
->usb
= devm_power_supply_register(ddata
->dev
,
638 &cpcap_charger_usb_desc
,
640 if (IS_ERR(ddata
->usb
)) {
641 error
= PTR_ERR(ddata
->usb
);
642 dev_err(ddata
->dev
, "failed to register USB charger: %i\n",
648 error
= cpcap_usb_init_interrupts(pdev
, ddata
);
652 ddata
->comparator
.set_vbus
= cpcap_charger_set_vbus
;
653 error
= omap_usb2_set_comparator(&ddata
->comparator
);
654 if (error
== -ENODEV
) {
655 dev_info(ddata
->dev
, "charger needs phy, deferring probe\n");
656 return -EPROBE_DEFER
;
659 cpcap_charger_init_optional_gpios(ddata
);
661 schedule_delayed_work(&ddata
->detect_work
, 0);
666 static int cpcap_charger_remove(struct platform_device
*pdev
)
668 struct cpcap_charger_ddata
*ddata
= platform_get_drvdata(pdev
);
671 atomic_set(&ddata
->active
, 0);
672 error
= omap_usb2_set_comparator(NULL
);
674 dev_warn(ddata
->dev
, "could not clear USB comparator: %i\n",
677 error
= cpcap_charger_set_state(ddata
, 0, 0, 0);
679 dev_warn(ddata
->dev
, "could not clear charger: %i\n",
681 cancel_delayed_work_sync(&ddata
->vbus_work
);
682 cancel_delayed_work_sync(&ddata
->detect_work
);
687 static struct platform_driver cpcap_charger_driver
= {
688 .probe
= cpcap_charger_probe
,
690 .name
= "cpcap-charger",
691 .of_match_table
= of_match_ptr(cpcap_charger_id_table
),
693 .remove
= cpcap_charger_remove
,
695 module_platform_driver(cpcap_charger_driver
);
697 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
698 MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
699 MODULE_LICENSE("GPL v2");
700 MODULE_ALIAS("platform:cpcap-charger");