1 // SPDX-License-Identifier: GPL-2.0-only
3 * Motorola CPCAP PMIC battery charger driver
5 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
7 * Rewritten for Linux power framework with some parts based on
8 * on earlier driver found in the Motorola Linux kernel:
10 * Copyright (C) 2009-2010 Motorola, Inc.
13 #include <linux/atomic.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/interrupt.h>
19 #include <linux/notifier.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/power_supply.h>
24 #include <linux/regmap.h>
26 #include <linux/gpio/consumer.h>
27 #include <linux/usb/phy_companion.h>
28 #include <linux/phy/omap_usb.h>
29 #include <linux/usb/otg.h>
30 #include <linux/iio/consumer.h>
31 #include <linux/mfd/motorola-cpcap.h>
34 * CPCAP_REG_CRM register bits. For documentation of somewhat similar hardware,
35 * see NXP "MC13783 Power Management and Audio Circuit Users's Guide"
36 * MC13783UG.pdf chapter "8.5 Battery Interface Register Summary". The registers
37 * and values for CPCAP are different, but some of the internal components seem
38 * similar. Also see the Motorola Linux kernel cpcap-regbits.h. CPCAP_REG_CHRGR_1
39 * bits that seem to describe the CRM register.
41 #define CPCAP_REG_CRM_UNUSED_641_15 BIT(15) /* 641 = register number */
42 #define CPCAP_REG_CRM_UNUSED_641_14 BIT(14) /* 641 = register number */
43 #define CPCAP_REG_CRM_CHRG_LED_EN BIT(13) /* Charger LED */
44 #define CPCAP_REG_CRM_RVRSMODE BIT(12) /* USB VBUS output enable */
45 #define CPCAP_REG_CRM_ICHRG_TR1 BIT(11) /* Trickle charge current */
46 #define CPCAP_REG_CRM_ICHRG_TR0 BIT(10)
47 #define CPCAP_REG_CRM_FET_OVRD BIT(9) /* 0 = hardware, 1 = FET_CTRL */
48 #define CPCAP_REG_CRM_FET_CTRL BIT(8) /* BPFET 1 if FET_OVRD set */
49 #define CPCAP_REG_CRM_VCHRG3 BIT(7) /* Charge voltage bits */
50 #define CPCAP_REG_CRM_VCHRG2 BIT(6)
51 #define CPCAP_REG_CRM_VCHRG1 BIT(5)
52 #define CPCAP_REG_CRM_VCHRG0 BIT(4)
53 #define CPCAP_REG_CRM_ICHRG3 BIT(3) /* Charge current bits */
54 #define CPCAP_REG_CRM_ICHRG2 BIT(2)
55 #define CPCAP_REG_CRM_ICHRG1 BIT(1)
56 #define CPCAP_REG_CRM_ICHRG0 BIT(0)
58 /* CPCAP_REG_CRM trickle charge voltages */
59 #define CPCAP_REG_CRM_TR(val) (((val) & 0x3) << 10)
60 #define CPCAP_REG_CRM_TR_0A00 CPCAP_REG_CRM_TR(0x0)
61 #define CPCAP_REG_CRM_TR_0A24 CPCAP_REG_CRM_TR(0x1)
62 #define CPCAP_REG_CRM_TR_0A48 CPCAP_REG_CRM_TR(0x2)
63 #define CPCAP_REG_CRM_TR_0A72 CPCAP_REG_CRM_TR(0x4)
66 * CPCAP_REG_CRM charge voltages based on the ADC channel 1 values.
67 * Note that these register bits don't match MC13783UG.pdf VCHRG
70 #define CPCAP_REG_CRM_VCHRG(val) (((val) & 0xf) << 4)
71 #define CPCAP_REG_CRM_VCHRG_3V80 CPCAP_REG_CRM_VCHRG(0x0)
72 #define CPCAP_REG_CRM_VCHRG_4V10 CPCAP_REG_CRM_VCHRG(0x1)
73 #define CPCAP_REG_CRM_VCHRG_4V12 CPCAP_REG_CRM_VCHRG(0x2)
74 #define CPCAP_REG_CRM_VCHRG_4V15 CPCAP_REG_CRM_VCHRG(0x3)
75 #define CPCAP_REG_CRM_VCHRG_4V17 CPCAP_REG_CRM_VCHRG(0x4)
76 #define CPCAP_REG_CRM_VCHRG_4V20 CPCAP_REG_CRM_VCHRG(0x5)
77 #define CPCAP_REG_CRM_VCHRG_4V23 CPCAP_REG_CRM_VCHRG(0x6)
78 #define CPCAP_REG_CRM_VCHRG_4V25 CPCAP_REG_CRM_VCHRG(0x7)
79 #define CPCAP_REG_CRM_VCHRG_4V27 CPCAP_REG_CRM_VCHRG(0x8)
80 #define CPCAP_REG_CRM_VCHRG_4V30 CPCAP_REG_CRM_VCHRG(0x9)
81 #define CPCAP_REG_CRM_VCHRG_4V33 CPCAP_REG_CRM_VCHRG(0xa)
82 #define CPCAP_REG_CRM_VCHRG_4V35 CPCAP_REG_CRM_VCHRG(0xb)
83 #define CPCAP_REG_CRM_VCHRG_4V38 CPCAP_REG_CRM_VCHRG(0xc)
84 #define CPCAP_REG_CRM_VCHRG_4V40 CPCAP_REG_CRM_VCHRG(0xd)
85 #define CPCAP_REG_CRM_VCHRG_4V42 CPCAP_REG_CRM_VCHRG(0xe)
86 #define CPCAP_REG_CRM_VCHRG_4V44 CPCAP_REG_CRM_VCHRG(0xf)
89 * CPCAP_REG_CRM charge currents. These seem to match MC13783UG.pdf
90 * values in "Table 8-3. Charge Path Regulator Current Limit
91 * Characteristics" for the nominal values.
93 #define CPCAP_REG_CRM_ICHRG(val) (((val) & 0xf) << 0)
94 #define CPCAP_REG_CRM_ICHRG_0A000 CPCAP_REG_CRM_ICHRG(0x0)
95 #define CPCAP_REG_CRM_ICHRG_0A070 CPCAP_REG_CRM_ICHRG(0x1)
96 #define CPCAP_REG_CRM_ICHRG_0A177 CPCAP_REG_CRM_ICHRG(0x2)
97 #define CPCAP_REG_CRM_ICHRG_0A266 CPCAP_REG_CRM_ICHRG(0x3)
98 #define CPCAP_REG_CRM_ICHRG_0A355 CPCAP_REG_CRM_ICHRG(0x4)
99 #define CPCAP_REG_CRM_ICHRG_0A443 CPCAP_REG_CRM_ICHRG(0x5)
100 #define CPCAP_REG_CRM_ICHRG_0A532 CPCAP_REG_CRM_ICHRG(0x6)
101 #define CPCAP_REG_CRM_ICHRG_0A621 CPCAP_REG_CRM_ICHRG(0x7)
102 #define CPCAP_REG_CRM_ICHRG_0A709 CPCAP_REG_CRM_ICHRG(0x8)
103 #define CPCAP_REG_CRM_ICHRG_0A798 CPCAP_REG_CRM_ICHRG(0x9)
104 #define CPCAP_REG_CRM_ICHRG_0A886 CPCAP_REG_CRM_ICHRG(0xa)
105 #define CPCAP_REG_CRM_ICHRG_0A975 CPCAP_REG_CRM_ICHRG(0xb)
106 #define CPCAP_REG_CRM_ICHRG_1A064 CPCAP_REG_CRM_ICHRG(0xc)
107 #define CPCAP_REG_CRM_ICHRG_1A152 CPCAP_REG_CRM_ICHRG(0xd)
108 #define CPCAP_REG_CRM_ICHRG_1A596 CPCAP_REG_CRM_ICHRG(0xe)
109 #define CPCAP_REG_CRM_ICHRG_NO_LIMIT CPCAP_REG_CRM_ICHRG(0xf)
112 CPCAP_CHARGER_IIO_BATTDET,
113 CPCAP_CHARGER_IIO_VOLTAGE,
114 CPCAP_CHARGER_IIO_VBUS,
115 CPCAP_CHARGER_IIO_CHRG_CURRENT,
116 CPCAP_CHARGER_IIO_BATT_CURRENT,
117 CPCAP_CHARGER_IIO_NR,
120 struct cpcap_charger_ddata {
123 struct list_head irq_list;
124 struct delayed_work detect_work;
125 struct delayed_work vbus_work;
126 struct gpio_desc *gpio[2]; /* gpio_reven0 & 1 */
128 struct iio_channel *channels[CPCAP_CHARGER_IIO_NR];
130 struct power_supply *usb;
132 struct phy_companion comparator; /* For USB VBUS */
139 struct cpcap_interrupt_desc {
141 struct list_head node;
145 struct cpcap_charger_ints_state {
158 static enum power_supply_property cpcap_charger_props[] = {
159 POWER_SUPPLY_PROP_STATUS,
160 POWER_SUPPLY_PROP_ONLINE,
161 POWER_SUPPLY_PROP_VOLTAGE_NOW,
162 POWER_SUPPLY_PROP_CURRENT_NOW,
165 static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata)
167 struct iio_channel *channel;
170 channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET];
171 error = iio_read_channel_raw(channel, &value);
173 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
181 static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata)
183 struct iio_channel *channel;
184 int error, value = 0;
186 channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE];
187 error = iio_read_channel_processed(channel, &value);
189 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
197 static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata)
199 struct iio_channel *channel;
200 int error, value = 0;
202 channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT];
203 error = iio_read_channel_processed(channel, &value);
205 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
213 static int cpcap_charger_get_property(struct power_supply *psy,
214 enum power_supply_property psp,
215 union power_supply_propval *val)
217 struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
220 case POWER_SUPPLY_PROP_STATUS:
221 val->intval = ddata->status;
223 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
224 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
225 val->intval = cpcap_charger_get_charge_voltage(ddata) *
230 case POWER_SUPPLY_PROP_CURRENT_NOW:
231 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
232 val->intval = cpcap_charger_get_charge_current(ddata) *
237 case POWER_SUPPLY_PROP_ONLINE:
238 val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING;
247 static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
253 gpiod_set_value(ddata->gpio[0], enabled);
256 static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata,
262 gpiod_set_value(ddata->gpio[1], enabled);
265 static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata,
266 int max_voltage, int charge_current,
272 enable = (charge_current || trickle_current);
273 dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable);
276 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
278 CPCAP_REG_CRM_FET_OVRD |
279 CPCAP_REG_CRM_FET_CTRL);
281 ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
285 ddata->status = POWER_SUPPLY_STATUS_DISCHARGING;
290 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff,
291 CPCAP_REG_CRM_CHRG_LED_EN |
293 CPCAP_REG_CRM_FET_OVRD |
294 CPCAP_REG_CRM_FET_CTRL |
298 ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
302 ddata->status = POWER_SUPPLY_STATUS_CHARGING;
307 dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
312 static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata)
314 int error, value = 0;
315 struct iio_channel *channel =
316 ddata->channels[CPCAP_CHARGER_IIO_VBUS];
318 error = iio_read_channel_processed(channel, &value);
320 return value > 3900 ? true : false;
322 dev_err(ddata->dev, "error reading VBUS: %i\n", error);
327 /* VBUS control functions for the USB PHY companion */
329 static void cpcap_charger_vbus_work(struct work_struct *work)
331 struct cpcap_charger_ddata *ddata;
335 ddata = container_of(work, struct cpcap_charger_ddata,
338 if (ddata->vbus_enabled) {
339 vbus = cpcap_charger_vbus_valid(ddata);
341 dev_info(ddata->dev, "VBUS already provided\n");
346 cpcap_charger_set_cable_path(ddata, false);
347 cpcap_charger_set_inductive_path(ddata, false);
349 error = cpcap_charger_set_state(ddata, 0, 0, 0);
353 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
354 CPCAP_REG_CRM_RVRSMODE,
355 CPCAP_REG_CRM_RVRSMODE);
359 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
360 CPCAP_REG_CRM_RVRSMODE, 0);
364 cpcap_charger_set_cable_path(ddata, true);
365 cpcap_charger_set_inductive_path(ddata, true);
371 dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__,
372 ddata->vbus_enabled ? "enable" : "disable", error);
375 static int cpcap_charger_set_vbus(struct phy_companion *comparator,
378 struct cpcap_charger_ddata *ddata =
379 container_of(comparator, struct cpcap_charger_ddata,
382 ddata->vbus_enabled = enabled;
383 schedule_delayed_work(&ddata->vbus_work, 0);
388 /* Charger interrupt handling functions */
390 static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata,
391 struct cpcap_charger_ints_state *s)
395 error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
399 s->chrg_det = val & BIT(13);
400 s->rvrs_chrg = val & BIT(12);
401 s->vbusov = val & BIT(11);
403 error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
407 s->chrg_se1b = val & BIT(13);
408 s->rvrs_mode = val & BIT(6);
409 s->chrgcurr1 = val & BIT(4);
410 s->vbusvld = val & BIT(3);
412 error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
416 s->battdetb = val & BIT(6);
421 static void cpcap_usb_detect(struct work_struct *work)
423 struct cpcap_charger_ddata *ddata;
424 struct cpcap_charger_ints_state s;
427 ddata = container_of(work, struct cpcap_charger_ddata,
430 error = cpcap_charger_get_ints_state(ddata, &s);
434 if (cpcap_charger_vbus_valid(ddata) && s.chrgcurr1) {
437 if (cpcap_charger_battery_found(ddata))
438 max_current = CPCAP_REG_CRM_ICHRG_1A596;
440 max_current = CPCAP_REG_CRM_ICHRG_0A532;
442 error = cpcap_charger_set_state(ddata,
443 CPCAP_REG_CRM_VCHRG_4V35,
448 error = cpcap_charger_set_state(ddata, 0, 0, 0);
453 power_supply_changed(ddata->usb);
457 dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
460 static irqreturn_t cpcap_charger_irq_thread(int irq, void *data)
462 struct cpcap_charger_ddata *ddata = data;
464 if (!atomic_read(&ddata->active))
467 schedule_delayed_work(&ddata->detect_work, 0);
472 static int cpcap_usb_init_irq(struct platform_device *pdev,
473 struct cpcap_charger_ddata *ddata,
476 struct cpcap_interrupt_desc *d;
479 irq = platform_get_irq_byname(pdev, name);
483 error = devm_request_threaded_irq(ddata->dev, irq, NULL,
484 cpcap_charger_irq_thread,
488 dev_err(ddata->dev, "could not get irq %s: %i\n",
494 d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
500 list_add(&d->node, &ddata->irq_list);
505 static const char * const cpcap_charger_irqs[] = {
507 "chrg_det", "rvrs_chrg",
510 "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr1", "vbusvld",
516 static int cpcap_usb_init_interrupts(struct platform_device *pdev,
517 struct cpcap_charger_ddata *ddata)
521 for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) {
522 error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]);
530 static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata)
534 for (i = 0; i < 2; i++) {
535 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
537 if (IS_ERR(ddata->gpio[i])) {
538 dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
539 i, PTR_ERR(ddata->gpio[i]));
540 ddata->gpio[i] = NULL;
545 static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
547 const char * const names[CPCAP_CHARGER_IIO_NR] = {
548 "battdetb", "battp", "vbus", "chg_isense", "batti",
552 for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) {
553 ddata->channels[i] = devm_iio_channel_get(ddata->dev,
555 if (IS_ERR(ddata->channels[i])) {
556 error = PTR_ERR(ddata->channels[i]);
560 if (!ddata->channels[i]->indio_dev) {
569 if (error != -EPROBE_DEFER)
570 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
576 static const struct power_supply_desc cpcap_charger_usb_desc = {
578 .type = POWER_SUPPLY_TYPE_USB,
579 .properties = cpcap_charger_props,
580 .num_properties = ARRAY_SIZE(cpcap_charger_props),
581 .get_property = cpcap_charger_get_property,
585 static const struct of_device_id cpcap_charger_id_table[] = {
587 .compatible = "motorola,mapphone-cpcap-charger",
591 MODULE_DEVICE_TABLE(of, cpcap_charger_id_table);
594 static int cpcap_charger_probe(struct platform_device *pdev)
596 struct cpcap_charger_ddata *ddata;
597 const struct of_device_id *of_id;
598 struct power_supply_config psy_cfg = {};
601 of_id = of_match_device(of_match_ptr(cpcap_charger_id_table),
606 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
610 ddata->dev = &pdev->dev;
612 ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
616 INIT_LIST_HEAD(&ddata->irq_list);
617 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
618 INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work);
619 platform_set_drvdata(pdev, ddata);
621 error = cpcap_charger_init_iio(ddata);
625 atomic_set(&ddata->active, 1);
627 psy_cfg.of_node = pdev->dev.of_node;
628 psy_cfg.drv_data = ddata;
630 ddata->usb = devm_power_supply_register(ddata->dev,
631 &cpcap_charger_usb_desc,
633 if (IS_ERR(ddata->usb)) {
634 error = PTR_ERR(ddata->usb);
635 dev_err(ddata->dev, "failed to register USB charger: %i\n",
641 error = cpcap_usb_init_interrupts(pdev, ddata);
645 ddata->comparator.set_vbus = cpcap_charger_set_vbus;
646 error = omap_usb2_set_comparator(&ddata->comparator);
647 if (error == -ENODEV) {
648 dev_info(ddata->dev, "charger needs phy, deferring probe\n");
649 return -EPROBE_DEFER;
652 cpcap_charger_init_optional_gpios(ddata);
654 schedule_delayed_work(&ddata->detect_work, 0);
659 static int cpcap_charger_remove(struct platform_device *pdev)
661 struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev);
664 atomic_set(&ddata->active, 0);
665 error = omap_usb2_set_comparator(NULL);
667 dev_warn(ddata->dev, "could not clear USB comparator: %i\n",
670 error = cpcap_charger_set_state(ddata, 0, 0, 0);
672 dev_warn(ddata->dev, "could not clear charger: %i\n",
674 cancel_delayed_work_sync(&ddata->vbus_work);
675 cancel_delayed_work_sync(&ddata->detect_work);
680 static struct platform_driver cpcap_charger_driver = {
681 .probe = cpcap_charger_probe,
683 .name = "cpcap-charger",
684 .of_match_table = of_match_ptr(cpcap_charger_id_table),
686 .remove = cpcap_charger_remove,
688 module_platform_driver(cpcap_charger_driver);
690 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
691 MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
692 MODULE_LICENSE("GPL v2");
693 MODULE_ALIAS("platform:cpcap-charger");