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>
41 /* CPCAP_REG_CRM register bits */
42 #define CPCAP_REG_CRM_UNUSED_641_15 BIT(15) /* 641 = register number */
43 #define CPCAP_REG_CRM_UNUSED_641_14 BIT(14) /* 641 = register number */
44 #define CPCAP_REG_CRM_CHRG_LED_EN BIT(13)
45 #define CPCAP_REG_CRM_RVRSMODE BIT(12)
46 #define CPCAP_REG_CRM_ICHRG_TR1 BIT(11)
47 #define CPCAP_REG_CRM_ICHRG_TR0 BIT(10)
48 #define CPCAP_REG_CRM_FET_OVRD BIT(9)
49 #define CPCAP_REG_CRM_FET_CTRL BIT(8)
50 #define CPCAP_REG_CRM_VCHRG3 BIT(7)
51 #define CPCAP_REG_CRM_VCHRG2 BIT(6)
52 #define CPCAP_REG_CRM_VCHRG1 BIT(5)
53 #define CPCAP_REG_CRM_VCHRG0 BIT(4)
54 #define CPCAP_REG_CRM_ICHRG3 BIT(3)
55 #define CPCAP_REG_CRM_ICHRG2 BIT(2)
56 #define CPCAP_REG_CRM_ICHRG1 BIT(1)
57 #define CPCAP_REG_CRM_ICHRG0 BIT(0)
59 /* CPCAP_REG_CRM trickle charge voltages */
60 #define CPCAP_REG_CRM_TR(val) (((val) & 0x3) << 10)
61 #define CPCAP_REG_CRM_TR_0A00 CPCAP_REG_CRM_TR(0x0)
62 #define CPCAP_REG_CRM_TR_0A24 CPCAP_REG_CRM_TR(0x1)
63 #define CPCAP_REG_CRM_TR_0A48 CPCAP_REG_CRM_TR(0x2)
64 #define CPCAP_REG_CRM_TR_0A72 CPCAP_REG_CRM_TR(0x4)
66 /* CPCAP_REG_CRM charge voltages */
67 #define CPCAP_REG_CRM_VCHRG(val) (((val) & 0xf) << 4)
68 #define CPCAP_REG_CRM_VCHRG_3V80 CPCAP_REG_CRM_VCHRG(0x0)
69 #define CPCAP_REG_CRM_VCHRG_4V10 CPCAP_REG_CRM_VCHRG(0x1)
70 #define CPCAP_REG_CRM_VCHRG_4V15 CPCAP_REG_CRM_VCHRG(0x2)
71 #define CPCAP_REG_CRM_VCHRG_4V20 CPCAP_REG_CRM_VCHRG(0x3)
72 #define CPCAP_REG_CRM_VCHRG_4V22 CPCAP_REG_CRM_VCHRG(0x4)
73 #define CPCAP_REG_CRM_VCHRG_4V24 CPCAP_REG_CRM_VCHRG(0x5)
74 #define CPCAP_REG_CRM_VCHRG_4V26 CPCAP_REG_CRM_VCHRG(0x6)
75 #define CPCAP_REG_CRM_VCHRG_4V28 CPCAP_REG_CRM_VCHRG(0x7)
76 #define CPCAP_REG_CRM_VCHRG_4V30 CPCAP_REG_CRM_VCHRG(0x8)
77 #define CPCAP_REG_CRM_VCHRG_4V32 CPCAP_REG_CRM_VCHRG(0x9)
78 #define CPCAP_REG_CRM_VCHRG_4V34 CPCAP_REG_CRM_VCHRG(0xa)
79 #define CPCAP_REG_CRM_VCHRG_4V35 CPCAP_REG_CRM_VCHRG(0xb)
80 #define CPCAP_REG_CRM_VCHRG_4V38 CPCAP_REG_CRM_VCHRG(0xc)
81 #define CPCAP_REG_CRM_VCHRG_4V40 CPCAP_REG_CRM_VCHRG(0xd)
82 #define CPCAP_REG_CRM_VCHRG_4V42 CPCAP_REG_CRM_VCHRG(0xe)
83 #define CPCAP_REG_CRM_VCHRG_4V44 CPCAP_REG_CRM_VCHRG(0xf)
85 /* CPCAP_REG_CRM charge currents */
86 #define CPCAP_REG_CRM_ICHRG(val) (((val) & 0xf) << 0)
87 #define CPCAP_REG_CRM_ICHRG_0A000 CPCAP_REG_CRM_ICHRG(0x0)
88 #define CPCAP_REG_CRM_ICHRG_0A070 CPCAP_REG_CRM_ICHRG(0x1)
89 #define CPCAP_REG_CRM_ICHRG_0A176 CPCAP_REG_CRM_ICHRG(0x2)
90 #define CPCAP_REG_CRM_ICHRG_0A264 CPCAP_REG_CRM_ICHRG(0x3)
91 #define CPCAP_REG_CRM_ICHRG_0A352 CPCAP_REG_CRM_ICHRG(0x4)
92 #define CPCAP_REG_CRM_ICHRG_0A440 CPCAP_REG_CRM_ICHRG(0x5)
93 #define CPCAP_REG_CRM_ICHRG_0A528 CPCAP_REG_CRM_ICHRG(0x6)
94 #define CPCAP_REG_CRM_ICHRG_0A616 CPCAP_REG_CRM_ICHRG(0x7)
95 #define CPCAP_REG_CRM_ICHRG_0A704 CPCAP_REG_CRM_ICHRG(0x8)
96 #define CPCAP_REG_CRM_ICHRG_0A792 CPCAP_REG_CRM_ICHRG(0x9)
97 #define CPCAP_REG_CRM_ICHRG_0A880 CPCAP_REG_CRM_ICHRG(0xa)
98 #define CPCAP_REG_CRM_ICHRG_0A968 CPCAP_REG_CRM_ICHRG(0xb)
99 #define CPCAP_REG_CRM_ICHRG_1A056 CPCAP_REG_CRM_ICHRG(0xc)
100 #define CPCAP_REG_CRM_ICHRG_1A144 CPCAP_REG_CRM_ICHRG(0xd)
101 #define CPCAP_REG_CRM_ICHRG_1A584 CPCAP_REG_CRM_ICHRG(0xe)
102 #define CPCAP_REG_CRM_ICHRG_NO_LIMIT CPCAP_REG_CRM_ICHRG(0xf)
105 CPCAP_CHARGER_IIO_BATTDET,
106 CPCAP_CHARGER_IIO_VOLTAGE,
107 CPCAP_CHARGER_IIO_VBUS,
108 CPCAP_CHARGER_IIO_CHRG_CURRENT,
109 CPCAP_CHARGER_IIO_BATT_CURRENT,
110 CPCAP_CHARGER_IIO_NR,
113 struct cpcap_charger_ddata {
116 struct list_head irq_list;
117 struct delayed_work detect_work;
118 struct delayed_work vbus_work;
119 struct gpio_desc *gpio[2]; /* gpio_reven0 & 1 */
121 struct iio_channel *channels[CPCAP_CHARGER_IIO_NR];
123 struct power_supply *usb;
125 struct phy_companion comparator; /* For USB VBUS */
132 struct cpcap_interrupt_desc {
134 struct list_head node;
138 struct cpcap_charger_ints_state {
151 static enum power_supply_property cpcap_charger_props[] = {
152 POWER_SUPPLY_PROP_STATUS,
153 POWER_SUPPLY_PROP_ONLINE,
154 POWER_SUPPLY_PROP_VOLTAGE_NOW,
155 POWER_SUPPLY_PROP_CURRENT_NOW,
158 static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata)
160 struct iio_channel *channel;
163 channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET];
164 error = iio_read_channel_raw(channel, &value);
166 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
174 static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata)
176 struct iio_channel *channel;
177 int error, value = 0;
179 channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE];
180 error = iio_read_channel_processed(channel, &value);
182 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
190 static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata)
192 struct iio_channel *channel;
193 int error, value = 0;
195 channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT];
196 error = iio_read_channel_processed(channel, &value);
198 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
206 static int cpcap_charger_get_property(struct power_supply *psy,
207 enum power_supply_property psp,
208 union power_supply_propval *val)
210 struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
213 case POWER_SUPPLY_PROP_STATUS:
214 val->intval = ddata->status;
216 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
217 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
218 val->intval = cpcap_charger_get_charge_voltage(ddata) *
223 case POWER_SUPPLY_PROP_CURRENT_NOW:
224 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
225 val->intval = cpcap_charger_get_charge_current(ddata) *
230 case POWER_SUPPLY_PROP_ONLINE:
231 val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING;
240 static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
246 gpiod_set_value(ddata->gpio[0], enabled);
249 static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata,
255 gpiod_set_value(ddata->gpio[1], enabled);
258 static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata,
259 int max_voltage, int charge_current,
265 enable = (charge_current || trickle_current);
266 dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable);
269 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
271 CPCAP_REG_CRM_FET_OVRD |
272 CPCAP_REG_CRM_FET_CTRL);
274 ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
278 ddata->status = POWER_SUPPLY_STATUS_DISCHARGING;
283 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff,
284 CPCAP_REG_CRM_CHRG_LED_EN |
286 CPCAP_REG_CRM_FET_OVRD |
287 CPCAP_REG_CRM_FET_CTRL |
291 ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
295 ddata->status = POWER_SUPPLY_STATUS_CHARGING;
300 dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
305 static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata)
307 int error, value = 0;
308 struct iio_channel *channel =
309 ddata->channels[CPCAP_CHARGER_IIO_VBUS];
311 error = iio_read_channel_processed(channel, &value);
313 return value > 3900 ? true : false;
315 dev_err(ddata->dev, "error reading VBUS: %i\n", error);
320 /* VBUS control functions for the USB PHY companion */
322 static void cpcap_charger_vbus_work(struct work_struct *work)
324 struct cpcap_charger_ddata *ddata;
328 ddata = container_of(work, struct cpcap_charger_ddata,
331 if (ddata->vbus_enabled) {
332 vbus = cpcap_charger_vbus_valid(ddata);
334 dev_info(ddata->dev, "VBUS already provided\n");
339 cpcap_charger_set_cable_path(ddata, false);
340 cpcap_charger_set_inductive_path(ddata, false);
342 error = cpcap_charger_set_state(ddata, 0, 0, 0);
346 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
347 CPCAP_REG_CRM_RVRSMODE,
348 CPCAP_REG_CRM_RVRSMODE);
352 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
353 CPCAP_REG_CRM_RVRSMODE, 0);
357 cpcap_charger_set_cable_path(ddata, true);
358 cpcap_charger_set_inductive_path(ddata, true);
364 dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__,
365 ddata->vbus_enabled ? "enable" : "disable", error);
368 static int cpcap_charger_set_vbus(struct phy_companion *comparator,
371 struct cpcap_charger_ddata *ddata =
372 container_of(comparator, struct cpcap_charger_ddata,
375 ddata->vbus_enabled = enabled;
376 schedule_delayed_work(&ddata->vbus_work, 0);
381 /* Charger interrupt handling functions */
383 static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata,
384 struct cpcap_charger_ints_state *s)
388 error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
392 s->chrg_det = val & BIT(13);
393 s->rvrs_chrg = val & BIT(12);
394 s->vbusov = val & BIT(11);
396 error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
400 s->chrg_se1b = val & BIT(13);
401 s->rvrs_mode = val & BIT(6);
402 s->chrgcurr1 = val & BIT(4);
403 s->vbusvld = val & BIT(3);
405 error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
409 s->battdetb = val & BIT(6);
414 static void cpcap_usb_detect(struct work_struct *work)
416 struct cpcap_charger_ddata *ddata;
417 struct cpcap_charger_ints_state s;
420 ddata = container_of(work, struct cpcap_charger_ddata,
423 error = cpcap_charger_get_ints_state(ddata, &s);
427 if (cpcap_charger_vbus_valid(ddata) && s.chrgcurr1) {
430 if (cpcap_charger_battery_found(ddata))
431 max_current = CPCAP_REG_CRM_ICHRG_1A584;
433 max_current = CPCAP_REG_CRM_ICHRG_0A528;
435 error = cpcap_charger_set_state(ddata,
436 CPCAP_REG_CRM_VCHRG_4V35,
441 error = cpcap_charger_set_state(ddata, 0, 0, 0);
449 dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
452 static irqreturn_t cpcap_charger_irq_thread(int irq, void *data)
454 struct cpcap_charger_ddata *ddata = data;
456 if (!atomic_read(&ddata->active))
459 schedule_delayed_work(&ddata->detect_work, 0);
464 static int cpcap_usb_init_irq(struct platform_device *pdev,
465 struct cpcap_charger_ddata *ddata,
468 struct cpcap_interrupt_desc *d;
471 irq = platform_get_irq_byname(pdev, name);
475 error = devm_request_threaded_irq(ddata->dev, irq, NULL,
476 cpcap_charger_irq_thread,
480 dev_err(ddata->dev, "could not get irq %s: %i\n",
486 d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
492 list_add(&d->node, &ddata->irq_list);
497 static const char * const cpcap_charger_irqs[] = {
499 "chrg_det", "rvrs_chrg",
502 "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr1", "vbusvld",
508 static int cpcap_usb_init_interrupts(struct platform_device *pdev,
509 struct cpcap_charger_ddata *ddata)
513 for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) {
514 error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]);
522 static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata)
526 for (i = 0; i < 2; i++) {
527 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
529 if (IS_ERR(ddata->gpio[i])) {
530 dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
531 i, PTR_ERR(ddata->gpio[i]));
532 ddata->gpio[i] = NULL;
537 static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
539 const char * const names[CPCAP_CHARGER_IIO_NR] = {
540 "battdetb", "battp", "vbus", "chg_isense", "batti",
544 for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) {
545 ddata->channels[i] = devm_iio_channel_get(ddata->dev,
547 if (IS_ERR(ddata->channels[i])) {
548 error = PTR_ERR(ddata->channels[i]);
552 if (!ddata->channels[i]->indio_dev) {
561 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
567 static const struct power_supply_desc cpcap_charger_usb_desc = {
569 .type = POWER_SUPPLY_TYPE_USB,
570 .properties = cpcap_charger_props,
571 .num_properties = ARRAY_SIZE(cpcap_charger_props),
572 .get_property = cpcap_charger_get_property,
576 static const struct of_device_id cpcap_charger_id_table[] = {
578 .compatible = "motorola,mapphone-cpcap-charger",
582 MODULE_DEVICE_TABLE(of, cpcap_charger_id_table);
585 static int cpcap_charger_probe(struct platform_device *pdev)
587 struct cpcap_charger_ddata *ddata;
588 const struct of_device_id *of_id;
591 of_id = of_match_device(of_match_ptr(cpcap_charger_id_table),
596 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
600 ddata->dev = &pdev->dev;
602 ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
606 INIT_LIST_HEAD(&ddata->irq_list);
607 INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
608 INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work);
609 platform_set_drvdata(pdev, ddata);
611 error = cpcap_charger_init_iio(ddata);
615 atomic_set(&ddata->active, 1);
617 ddata->usb = devm_power_supply_register(ddata->dev,
618 &cpcap_charger_usb_desc,
620 if (IS_ERR(ddata->usb)) {
621 error = PTR_ERR(ddata->usb);
622 dev_err(ddata->dev, "failed to register USB charger: %i\n",
628 error = cpcap_usb_init_interrupts(pdev, ddata);
632 ddata->comparator.set_vbus = cpcap_charger_set_vbus;
633 error = omap_usb2_set_comparator(&ddata->comparator);
634 if (error == -ENODEV) {
635 dev_info(ddata->dev, "charger needs phy, deferring probe\n");
636 return -EPROBE_DEFER;
639 cpcap_charger_init_optional_gpios(ddata);
641 schedule_delayed_work(&ddata->detect_work, 0);
646 static int cpcap_charger_remove(struct platform_device *pdev)
648 struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev);
651 atomic_set(&ddata->active, 0);
652 error = omap_usb2_set_comparator(NULL);
654 dev_warn(ddata->dev, "could not clear USB comparator: %i\n",
657 error = cpcap_charger_set_state(ddata, 0, 0, 0);
659 dev_warn(ddata->dev, "could not clear charger: %i\n",
661 cancel_delayed_work_sync(&ddata->vbus_work);
662 cancel_delayed_work_sync(&ddata->detect_work);
667 static struct platform_driver cpcap_charger_driver = {
668 .probe = cpcap_charger_probe,
670 .name = "cpcap-charger",
671 .of_match_table = of_match_ptr(cpcap_charger_id_table),
673 .remove = cpcap_charger_remove,
675 module_platform_driver(cpcap_charger_driver);
677 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
678 MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
679 MODULE_LICENSE("GPL v2");
680 MODULE_ALIAS("platform:cpcap-charger");