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 * 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>
20 #include <linux/mod_devicetable.h>
21 #include <linux/platform_device.h>
22 #include <linux/power_supply.h>
23 #include <linux/regmap.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/usb/phy_companion.h>
27 #include <linux/phy/omap_usb.h>
28 #include <linux/usb/otg.h>
29 #include <linux/iio/consumer.h>
30 #include <linux/mfd/motorola-cpcap.h>
33 * CPCAP_REG_CRM register bits. For documentation of somewhat similar hardware,
34 * see NXP "MC13783 Power Management and Audio Circuit Users's Guide"
35 * MC13783UG.pdf chapter "8.5 Battery Interface Register Summary". The registers
36 * and values for CPCAP are different, but some of the internal components seem
37 * similar. Also see the Motorola Linux kernel cpcap-regbits.h. CPCAP_REG_CHRGR_1
38 * bits that seem to describe the CRM register.
40 #define CPCAP_REG_CRM_UNUSED_641_15 BIT(15) /* 641 = register number */
41 #define CPCAP_REG_CRM_UNUSED_641_14 BIT(14) /* 641 = register number */
42 #define CPCAP_REG_CRM_CHRG_LED_EN BIT(13) /* Charger LED */
43 #define CPCAP_REG_CRM_RVRSMODE BIT(12) /* USB VBUS output enable */
44 #define CPCAP_REG_CRM_ICHRG_TR1 BIT(11) /* Trickle charge current */
45 #define CPCAP_REG_CRM_ICHRG_TR0 BIT(10)
46 #define CPCAP_REG_CRM_FET_OVRD BIT(9) /* 0 = hardware, 1 = FET_CTRL */
47 #define CPCAP_REG_CRM_FET_CTRL BIT(8) /* BPFET 1 if FET_OVRD set */
48 #define CPCAP_REG_CRM_VCHRG3 BIT(7) /* Charge voltage bits */
49 #define CPCAP_REG_CRM_VCHRG2 BIT(6)
50 #define CPCAP_REG_CRM_VCHRG1 BIT(5)
51 #define CPCAP_REG_CRM_VCHRG0 BIT(4)
52 #define CPCAP_REG_CRM_ICHRG3 BIT(3) /* Charge current bits */
53 #define CPCAP_REG_CRM_ICHRG2 BIT(2)
54 #define CPCAP_REG_CRM_ICHRG1 BIT(1)
55 #define CPCAP_REG_CRM_ICHRG0 BIT(0)
57 /* CPCAP_REG_CRM trickle charge voltages */
58 #define CPCAP_REG_CRM_TR(val) (((val) & 0x3) << 10)
59 #define CPCAP_REG_CRM_TR_0A00 CPCAP_REG_CRM_TR(0x0)
60 #define CPCAP_REG_CRM_TR_0A24 CPCAP_REG_CRM_TR(0x1)
61 #define CPCAP_REG_CRM_TR_0A48 CPCAP_REG_CRM_TR(0x2)
62 #define CPCAP_REG_CRM_TR_0A72 CPCAP_REG_CRM_TR(0x4)
65 * CPCAP_REG_CRM charge voltages based on the ADC channel 1 values.
66 * Note that these register bits don't match MC13783UG.pdf VCHRG
69 #define CPCAP_REG_CRM_VCHRG(val) (((val) & 0xf) << 4)
70 #define CPCAP_REG_CRM_VCHRG_3V80 CPCAP_REG_CRM_VCHRG(0x0)
71 #define CPCAP_REG_CRM_VCHRG_4V10 CPCAP_REG_CRM_VCHRG(0x1)
72 #define CPCAP_REG_CRM_VCHRG_4V12 CPCAP_REG_CRM_VCHRG(0x2)
73 #define CPCAP_REG_CRM_VCHRG_4V15 CPCAP_REG_CRM_VCHRG(0x3)
74 #define CPCAP_REG_CRM_VCHRG_4V17 CPCAP_REG_CRM_VCHRG(0x4)
75 #define CPCAP_REG_CRM_VCHRG_4V20 CPCAP_REG_CRM_VCHRG(0x5)
76 #define CPCAP_REG_CRM_VCHRG_4V23 CPCAP_REG_CRM_VCHRG(0x6)
77 #define CPCAP_REG_CRM_VCHRG_4V25 CPCAP_REG_CRM_VCHRG(0x7)
78 #define CPCAP_REG_CRM_VCHRG_4V27 CPCAP_REG_CRM_VCHRG(0x8)
79 #define CPCAP_REG_CRM_VCHRG_4V30 CPCAP_REG_CRM_VCHRG(0x9)
80 #define CPCAP_REG_CRM_VCHRG_4V33 CPCAP_REG_CRM_VCHRG(0xa)
81 #define CPCAP_REG_CRM_VCHRG_4V35 CPCAP_REG_CRM_VCHRG(0xb)
82 #define CPCAP_REG_CRM_VCHRG_4V38 CPCAP_REG_CRM_VCHRG(0xc)
83 #define CPCAP_REG_CRM_VCHRG_4V40 CPCAP_REG_CRM_VCHRG(0xd)
84 #define CPCAP_REG_CRM_VCHRG_4V42 CPCAP_REG_CRM_VCHRG(0xe)
85 #define CPCAP_REG_CRM_VCHRG_4V44 CPCAP_REG_CRM_VCHRG(0xf)
88 * CPCAP_REG_CRM charge currents. These seem to match MC13783UG.pdf
89 * values in "Table 8-3. Charge Path Regulator Current Limit
90 * Characteristics" for the nominal values.
92 * Except 70mA and 1.596A and unlimited, these are simply 88.7mA / step.
94 #define CPCAP_REG_CRM_ICHRG(val) (((val) & 0xf) << 0)
95 #define CPCAP_REG_CRM_ICHRG_0A000 CPCAP_REG_CRM_ICHRG(0x0)
96 #define CPCAP_REG_CRM_ICHRG_0A070 CPCAP_REG_CRM_ICHRG(0x1)
97 #define CPCAP_REG_CRM_ICHRG_0A177 CPCAP_REG_CRM_ICHRG(0x2)
98 #define CPCAP_REG_CRM_ICHRG_0A266 CPCAP_REG_CRM_ICHRG(0x3)
99 #define CPCAP_REG_CRM_ICHRG_0A355 CPCAP_REG_CRM_ICHRG(0x4)
100 #define CPCAP_REG_CRM_ICHRG_0A443 CPCAP_REG_CRM_ICHRG(0x5)
101 #define CPCAP_REG_CRM_ICHRG_0A532 CPCAP_REG_CRM_ICHRG(0x6)
102 #define CPCAP_REG_CRM_ICHRG_0A621 CPCAP_REG_CRM_ICHRG(0x7)
103 #define CPCAP_REG_CRM_ICHRG_0A709 CPCAP_REG_CRM_ICHRG(0x8)
104 #define CPCAP_REG_CRM_ICHRG_0A798 CPCAP_REG_CRM_ICHRG(0x9)
105 #define CPCAP_REG_CRM_ICHRG_0A886 CPCAP_REG_CRM_ICHRG(0xa)
106 #define CPCAP_REG_CRM_ICHRG_0A975 CPCAP_REG_CRM_ICHRG(0xb)
107 #define CPCAP_REG_CRM_ICHRG_1A064 CPCAP_REG_CRM_ICHRG(0xc)
108 #define CPCAP_REG_CRM_ICHRG_1A152 CPCAP_REG_CRM_ICHRG(0xd)
109 #define CPCAP_REG_CRM_ICHRG_1A596 CPCAP_REG_CRM_ICHRG(0xe)
110 #define CPCAP_REG_CRM_ICHRG_NO_LIMIT CPCAP_REG_CRM_ICHRG(0xf)
112 /* CPCAP_REG_VUSBC register bits needed for VBUS */
113 #define CPCAP_BIT_VBUS_SWITCH BIT(0) /* VBUS boost to 5V */
116 CPCAP_CHARGER_IIO_BATTDET
,
117 CPCAP_CHARGER_IIO_VOLTAGE
,
118 CPCAP_CHARGER_IIO_VBUS
,
119 CPCAP_CHARGER_IIO_CHRG_CURRENT
,
120 CPCAP_CHARGER_IIO_BATT_CURRENT
,
121 CPCAP_CHARGER_IIO_NR
,
124 struct cpcap_charger_ddata
{
127 struct list_head irq_list
;
128 struct delayed_work detect_work
;
129 struct delayed_work vbus_work
;
130 struct gpio_desc
*gpio
[2]; /* gpio_reven0 & 1 */
132 struct iio_channel
*channels
[CPCAP_CHARGER_IIO_NR
];
134 struct power_supply
*usb
;
136 struct phy_companion comparator
; /* For USB VBUS */
137 unsigned int vbus_enabled
:1;
138 unsigned int feeding_vbus
:1;
146 struct cpcap_interrupt_desc
{
148 struct list_head node
;
152 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_CONSTANT_CHARGE_VOLTAGE
,
170 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
171 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
172 POWER_SUPPLY_PROP_CURRENT_NOW
,
175 static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata
*ddata
)
177 struct iio_channel
*channel
;
178 int error
, value
= 0;
180 channel
= ddata
->channels
[CPCAP_CHARGER_IIO_VOLTAGE
];
181 error
= iio_read_channel_processed(channel
, &value
);
183 dev_warn(ddata
->dev
, "%s failed: %i\n", __func__
, error
);
191 static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata
*ddata
)
193 struct iio_channel
*channel
;
194 int error
, value
= 0;
196 channel
= ddata
->channels
[CPCAP_CHARGER_IIO_CHRG_CURRENT
];
197 error
= iio_read_channel_processed(channel
, &value
);
199 dev_warn(ddata
->dev
, "%s failed: %i\n", __func__
, error
);
207 static int cpcap_charger_get_property(struct power_supply
*psy
,
208 enum power_supply_property psp
,
209 union power_supply_propval
*val
)
211 struct cpcap_charger_ddata
*ddata
= dev_get_drvdata(psy
->dev
.parent
);
214 case POWER_SUPPLY_PROP_STATUS
:
215 val
->intval
= ddata
->status
;
217 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
218 val
->intval
= ddata
->limit_current
;
220 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
221 val
->intval
= ddata
->voltage
;
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 int cpcap_charger_match_voltage(int voltage
)
250 case 0 ... 4100000 - 1: return 3800000;
251 case 4100000 ... 4120000 - 1: return 4100000;
252 case 4120000 ... 4150000 - 1: return 4120000;
253 case 4150000 ... 4170000 - 1: return 4150000;
254 case 4170000 ... 4200000 - 1: return 4170000;
255 case 4200000 ... 4230000 - 1: return 4200000;
256 case 4230000 ... 4250000 - 1: return 4230000;
257 case 4250000 ... 4270000 - 1: return 4250000;
258 case 4270000 ... 4300000 - 1: return 4270000;
259 case 4300000 ... 4330000 - 1: return 4300000;
260 case 4330000 ... 4350000 - 1: return 4330000;
261 case 4350000 ... 4380000 - 1: return 4350000;
262 case 4380000 ... 4400000 - 1: return 4380000;
263 case 4400000 ... 4420000 - 1: return 4400000;
264 case 4420000 ... 4440000 - 1: return 4420000;
265 case 4440000: return 4440000;
271 cpcap_charger_get_bat_const_charge_voltage(struct cpcap_charger_ddata
*ddata
)
273 union power_supply_propval prop
;
274 struct power_supply
*battery
;
275 int voltage
= ddata
->voltage
;
278 battery
= power_supply_get_by_name("battery");
280 error
= power_supply_get_property(battery
,
281 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
284 voltage
= prop
.intval
;
286 power_supply_put(battery
);
292 static int cpcap_charger_current_to_regval(int microamp
)
294 int miliamp
= microamp
/ 1000;
300 return CPCAP_REG_CRM_ICHRG(0x0);
302 return CPCAP_REG_CRM_ICHRG(0x1);
304 return CPCAP_REG_CRM_ICHRG(0xe);
306 res
= microamp
/ 88666;
309 return CPCAP_REG_CRM_ICHRG(res
);
312 static int cpcap_charger_set_property(struct power_supply
*psy
,
313 enum power_supply_property psp
,
314 const union power_supply_propval
*val
)
316 struct cpcap_charger_ddata
*ddata
= dev_get_drvdata(psy
->dev
.parent
);
317 int voltage
, batvolt
;
320 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
321 if (cpcap_charger_current_to_regval(val
->intval
) < 0)
323 ddata
->limit_current
= val
->intval
;
324 schedule_delayed_work(&ddata
->detect_work
, 0);
326 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
327 voltage
= cpcap_charger_match_voltage(val
->intval
);
328 batvolt
= cpcap_charger_get_bat_const_charge_voltage(ddata
);
329 if (voltage
> batvolt
)
331 ddata
->voltage
= voltage
;
332 schedule_delayed_work(&ddata
->detect_work
, 0);
341 static int cpcap_charger_property_is_writeable(struct power_supply
*psy
,
342 enum power_supply_property psp
)
345 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
346 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
353 static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata
*ddata
,
359 gpiod_set_value(ddata
->gpio
[0], enabled
);
362 static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata
*ddata
,
368 gpiod_set_value(ddata
->gpio
[1], enabled
);
371 static void cpcap_charger_update_state(struct cpcap_charger_ddata
*ddata
,
376 if (state
> POWER_SUPPLY_STATUS_FULL
) {
377 dev_warn(ddata
->dev
, "unknown state: %i\n", state
);
382 ddata
->status
= state
;
385 case POWER_SUPPLY_STATUS_DISCHARGING
:
386 status
= "DISCONNECTED";
388 case POWER_SUPPLY_STATUS_NOT_CHARGING
:
389 status
= "DETECTING";
391 case POWER_SUPPLY_STATUS_CHARGING
:
394 case POWER_SUPPLY_STATUS_FULL
:
401 dev_dbg(ddata
->dev
, "state: %s\n", status
);
404 static int cpcap_charger_disable(struct cpcap_charger_ddata
*ddata
)
408 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_CRM
, 0x3fff,
409 CPCAP_REG_CRM_FET_OVRD
|
410 CPCAP_REG_CRM_FET_CTRL
);
412 dev_err(ddata
->dev
, "%s failed with %i\n", __func__
, error
);
417 static int cpcap_charger_enable(struct cpcap_charger_ddata
*ddata
,
418 int max_voltage
, int charge_current
,
423 if (!max_voltage
|| !charge_current
)
426 dev_dbg(ddata
->dev
, "enable: %i %i %i\n",
427 max_voltage
, charge_current
, trickle_current
);
429 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_CRM
, 0x3fff,
430 CPCAP_REG_CRM_CHRG_LED_EN
|
432 CPCAP_REG_CRM_FET_OVRD
|
433 CPCAP_REG_CRM_FET_CTRL
|
437 dev_err(ddata
->dev
, "%s failed with %i\n", __func__
, error
);
442 static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata
*ddata
)
444 int error
, value
= 0;
445 struct iio_channel
*channel
=
446 ddata
->channels
[CPCAP_CHARGER_IIO_VBUS
];
448 error
= iio_read_channel_processed(channel
, &value
);
452 dev_err(ddata
->dev
, "error reading VBUS: %i\n", error
);
457 /* VBUS control functions for the USB PHY companion */
458 static void cpcap_charger_vbus_work(struct work_struct
*work
)
460 struct cpcap_charger_ddata
*ddata
;
464 ddata
= container_of(work
, struct cpcap_charger_ddata
,
467 if (ddata
->vbus_enabled
) {
468 vbus
= cpcap_charger_vbus_valid(ddata
);
470 dev_dbg(ddata
->dev
, "VBUS already provided\n");
475 ddata
->feeding_vbus
= true;
476 cpcap_charger_set_cable_path(ddata
, false);
477 cpcap_charger_set_inductive_path(ddata
, false);
479 error
= cpcap_charger_disable(ddata
);
483 cpcap_charger_update_state(ddata
,
484 POWER_SUPPLY_STATUS_DISCHARGING
);
486 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_VUSBC
,
487 CPCAP_BIT_VBUS_SWITCH
,
488 CPCAP_BIT_VBUS_SWITCH
);
492 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_CRM
,
493 CPCAP_REG_CRM_RVRSMODE
,
494 CPCAP_REG_CRM_RVRSMODE
);
498 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_VUSBC
,
499 CPCAP_BIT_VBUS_SWITCH
, 0);
503 error
= regmap_update_bits(ddata
->reg
, CPCAP_REG_CRM
,
504 CPCAP_REG_CRM_RVRSMODE
, 0);
508 cpcap_charger_set_cable_path(ddata
, true);
509 cpcap_charger_set_inductive_path(ddata
, true);
510 ddata
->feeding_vbus
= false;
516 cpcap_charger_update_state(ddata
, POWER_SUPPLY_STATUS_UNKNOWN
);
517 dev_err(ddata
->dev
, "%s could not %s vbus: %i\n", __func__
,
518 ddata
->vbus_enabled
? "enable" : "disable", error
);
521 static int cpcap_charger_set_vbus(struct phy_companion
*comparator
,
524 struct cpcap_charger_ddata
*ddata
=
525 container_of(comparator
, struct cpcap_charger_ddata
,
528 ddata
->vbus_enabled
= enabled
;
529 schedule_delayed_work(&ddata
->vbus_work
, 0);
534 /* Charger interrupt handling functions */
536 static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata
*ddata
,
537 struct cpcap_charger_ints_state
*s
)
541 error
= regmap_read(ddata
->reg
, CPCAP_REG_INTS1
, &val
);
545 s
->chrg_det
= val
& BIT(13);
546 s
->rvrs_chrg
= val
& BIT(12);
547 s
->vbusov
= val
& BIT(11);
549 error
= regmap_read(ddata
->reg
, CPCAP_REG_INTS2
, &val
);
553 s
->chrg_se1b
= val
& BIT(13);
554 s
->rvrs_mode
= val
& BIT(6);
555 s
->chrgcurr2
= val
& BIT(5);
556 s
->chrgcurr1
= val
& BIT(4);
557 s
->vbusvld
= val
& BIT(3);
559 error
= regmap_read(ddata
->reg
, CPCAP_REG_INTS4
, &val
);
563 s
->battdetb
= val
& BIT(6);
568 static int cpcap_charger_voltage_to_regval(int voltage
)
573 case 0 ... 4100000 - 1:
575 case 4100000 ... 4200000 - 1:
578 case 4200000 ... 4300000 - 1:
581 case 4300000 ... 4380000 - 1:
584 case 4380000 ... 4440000:
591 return ((voltage
- 4100000) / 20000) + offset
;
594 static void cpcap_charger_disconnect(struct cpcap_charger_ddata
*ddata
,
595 int state
, unsigned long delay
)
599 /* Update battery state before disconnecting the charger */
601 case POWER_SUPPLY_STATUS_DISCHARGING
:
602 case POWER_SUPPLY_STATUS_FULL
:
603 power_supply_changed(ddata
->usb
);
609 error
= cpcap_charger_disable(ddata
);
611 cpcap_charger_update_state(ddata
, POWER_SUPPLY_STATUS_UNKNOWN
);
615 cpcap_charger_update_state(ddata
, state
);
616 power_supply_changed(ddata
->usb
);
617 schedule_delayed_work(&ddata
->detect_work
, delay
);
620 static void cpcap_usb_detect(struct work_struct
*work
)
622 struct cpcap_charger_ddata
*ddata
;
623 struct cpcap_charger_ints_state s
;
624 int error
, new_state
;
626 ddata
= container_of(work
, struct cpcap_charger_ddata
,
629 error
= cpcap_charger_get_ints_state(ddata
, &s
);
633 /* Just init the state if a charger is connected with no chrg_det set */
634 if (!s
.chrg_det
&& s
.chrgcurr1
&& s
.vbusvld
) {
635 cpcap_charger_update_state(ddata
,
636 POWER_SUPPLY_STATUS_NOT_CHARGING
);
642 * If battery voltage is higher than charge voltage, it may have been
643 * charged to 4.35V by Android. Try again in 10 minutes.
645 if (cpcap_charger_get_charge_voltage(ddata
) > ddata
->voltage
) {
646 cpcap_charger_disconnect(ddata
,
647 POWER_SUPPLY_STATUS_NOT_CHARGING
,
653 /* Delay for 80ms to avoid vbus bouncing when usb cable is plugged in */
654 usleep_range(80000, 120000);
656 /* Throttle chrgcurr2 interrupt for charger done and retry */
657 switch (ddata
->status
) {
658 case POWER_SUPPLY_STATUS_CHARGING
:
661 new_state
= POWER_SUPPLY_STATUS_FULL
;
663 if (s
.chrgcurr1
&& s
.vbusvld
) {
664 cpcap_charger_disconnect(ddata
, new_state
, HZ
* 5);
668 case POWER_SUPPLY_STATUS_FULL
:
672 new_state
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
674 new_state
= POWER_SUPPLY_STATUS_DISCHARGING
;
676 cpcap_charger_disconnect(ddata
, new_state
, HZ
* 5);
683 if (!ddata
->feeding_vbus
&& cpcap_charger_vbus_valid(ddata
) &&
687 union power_supply_propval val
;
688 struct power_supply
*battery
;
690 battery
= power_supply_get_by_name("battery");
691 if (IS_ERR_OR_NULL(battery
)) {
692 dev_err(ddata
->dev
, "battery power_supply not available %li\n",
697 error
= power_supply_get_property(battery
, POWER_SUPPLY_PROP_PRESENT
, &val
);
698 power_supply_put(battery
);
703 max_current
= 1596000;
705 dev_info(ddata
->dev
, "battery not inserted, charging disabled\n");
709 if (max_current
> ddata
->limit_current
)
710 max_current
= ddata
->limit_current
;
712 ichrg
= cpcap_charger_current_to_regval(max_current
);
713 vchrg
= cpcap_charger_voltage_to_regval(ddata
->voltage
);
714 error
= cpcap_charger_enable(ddata
,
715 CPCAP_REG_CRM_VCHRG(vchrg
),
719 cpcap_charger_update_state(ddata
,
720 POWER_SUPPLY_STATUS_CHARGING
);
722 error
= cpcap_charger_disable(ddata
);
725 cpcap_charger_update_state(ddata
,
726 POWER_SUPPLY_STATUS_DISCHARGING
);
729 power_supply_changed(ddata
->usb
);
733 cpcap_charger_update_state(ddata
, POWER_SUPPLY_STATUS_UNKNOWN
);
734 dev_err(ddata
->dev
, "%s failed with %i\n", __func__
, error
);
737 static irqreturn_t
cpcap_charger_irq_thread(int irq
, void *data
)
739 struct cpcap_charger_ddata
*ddata
= data
;
741 if (!atomic_read(&ddata
->active
))
744 schedule_delayed_work(&ddata
->detect_work
, 0);
749 static int cpcap_usb_init_irq(struct platform_device
*pdev
,
750 struct cpcap_charger_ddata
*ddata
,
753 struct cpcap_interrupt_desc
*d
;
756 irq
= platform_get_irq_byname(pdev
, name
);
760 error
= devm_request_threaded_irq(ddata
->dev
, irq
, NULL
,
761 cpcap_charger_irq_thread
,
762 IRQF_SHARED
| IRQF_ONESHOT
,
765 dev_err(ddata
->dev
, "could not get irq %s: %i\n",
771 d
= devm_kzalloc(ddata
->dev
, sizeof(*d
), GFP_KERNEL
);
777 list_add(&d
->node
, &ddata
->irq_list
);
782 static const char * const cpcap_charger_irqs
[] = {
784 "chrg_det", "rvrs_chrg",
787 "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr2", "chrgcurr1", "vbusvld",
793 static int cpcap_usb_init_interrupts(struct platform_device
*pdev
,
794 struct cpcap_charger_ddata
*ddata
)
798 for (i
= 0; i
< ARRAY_SIZE(cpcap_charger_irqs
); i
++) {
799 error
= cpcap_usb_init_irq(pdev
, ddata
, cpcap_charger_irqs
[i
]);
807 static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata
*ddata
)
811 for (i
= 0; i
< 2; i
++) {
812 ddata
->gpio
[i
] = devm_gpiod_get_index(ddata
->dev
, "mode",
814 if (IS_ERR(ddata
->gpio
[i
])) {
815 dev_info(ddata
->dev
, "no mode change GPIO%i: %li\n",
816 i
, PTR_ERR(ddata
->gpio
[i
]));
817 ddata
->gpio
[i
] = NULL
;
822 static int cpcap_charger_init_iio(struct cpcap_charger_ddata
*ddata
)
824 const char * const names
[CPCAP_CHARGER_IIO_NR
] = {
825 "battdetb", "battp", "vbus", "chg_isense", "batti",
829 for (i
= 0; i
< CPCAP_CHARGER_IIO_NR
; i
++) {
830 ddata
->channels
[i
] = devm_iio_channel_get(ddata
->dev
,
832 if (IS_ERR(ddata
->channels
[i
])) {
833 error
= PTR_ERR(ddata
->channels
[i
]);
837 if (!ddata
->channels
[i
]->indio_dev
) {
846 if (error
!= -EPROBE_DEFER
)
847 dev_err(ddata
->dev
, "could not initialize VBUS or ID IIO: %i\n",
853 static char *cpcap_charger_supplied_to
[] = {
857 static const struct power_supply_desc cpcap_charger_usb_desc
= {
859 .type
= POWER_SUPPLY_TYPE_USB
,
860 .properties
= cpcap_charger_props
,
861 .num_properties
= ARRAY_SIZE(cpcap_charger_props
),
862 .get_property
= cpcap_charger_get_property
,
863 .set_property
= cpcap_charger_set_property
,
864 .property_is_writeable
= cpcap_charger_property_is_writeable
,
867 static const struct of_device_id cpcap_charger_id_table
[] = {
869 .compatible
= "motorola,mapphone-cpcap-charger",
873 MODULE_DEVICE_TABLE(of
, cpcap_charger_id_table
);
875 static int cpcap_charger_probe(struct platform_device
*pdev
)
877 struct cpcap_charger_ddata
*ddata
;
878 struct power_supply_config psy_cfg
= {};
881 ddata
= devm_kzalloc(&pdev
->dev
, sizeof(*ddata
), GFP_KERNEL
);
885 ddata
->dev
= &pdev
->dev
;
886 ddata
->voltage
= 4200000;
887 ddata
->limit_current
= 532000;
889 ddata
->reg
= dev_get_regmap(ddata
->dev
->parent
, NULL
);
893 INIT_LIST_HEAD(&ddata
->irq_list
);
894 INIT_DELAYED_WORK(&ddata
->detect_work
, cpcap_usb_detect
);
895 INIT_DELAYED_WORK(&ddata
->vbus_work
, cpcap_charger_vbus_work
);
896 platform_set_drvdata(pdev
, ddata
);
898 error
= cpcap_charger_init_iio(ddata
);
902 atomic_set(&ddata
->active
, 1);
904 psy_cfg
.of_node
= pdev
->dev
.of_node
;
905 psy_cfg
.drv_data
= ddata
;
906 psy_cfg
.supplied_to
= cpcap_charger_supplied_to
;
907 psy_cfg
.num_supplicants
= ARRAY_SIZE(cpcap_charger_supplied_to
);
909 ddata
->usb
= devm_power_supply_register(ddata
->dev
,
910 &cpcap_charger_usb_desc
,
912 if (IS_ERR(ddata
->usb
)) {
913 error
= PTR_ERR(ddata
->usb
);
914 dev_err(ddata
->dev
, "failed to register USB charger: %i\n",
920 error
= cpcap_usb_init_interrupts(pdev
, ddata
);
924 ddata
->comparator
.set_vbus
= cpcap_charger_set_vbus
;
925 error
= omap_usb2_set_comparator(&ddata
->comparator
);
926 if (error
== -ENODEV
) {
927 dev_info(ddata
->dev
, "charger needs phy, deferring probe\n");
928 return -EPROBE_DEFER
;
931 cpcap_charger_init_optional_gpios(ddata
);
933 schedule_delayed_work(&ddata
->detect_work
, 0);
938 static void cpcap_charger_shutdown(struct platform_device
*pdev
)
940 struct cpcap_charger_ddata
*ddata
= platform_get_drvdata(pdev
);
943 atomic_set(&ddata
->active
, 0);
944 error
= omap_usb2_set_comparator(NULL
);
946 dev_warn(ddata
->dev
, "could not clear USB comparator: %i\n",
949 error
= cpcap_charger_disable(ddata
);
951 cpcap_charger_update_state(ddata
, POWER_SUPPLY_STATUS_UNKNOWN
);
952 dev_warn(ddata
->dev
, "could not clear charger: %i\n",
955 cpcap_charger_update_state(ddata
, POWER_SUPPLY_STATUS_DISCHARGING
);
956 cancel_delayed_work_sync(&ddata
->vbus_work
);
957 cancel_delayed_work_sync(&ddata
->detect_work
);
960 static void cpcap_charger_remove(struct platform_device
*pdev
)
962 cpcap_charger_shutdown(pdev
);
965 static struct platform_driver cpcap_charger_driver
= {
966 .probe
= cpcap_charger_probe
,
968 .name
= "cpcap-charger",
969 .of_match_table
= cpcap_charger_id_table
,
971 .shutdown
= cpcap_charger_shutdown
,
972 .remove
= cpcap_charger_remove
,
974 module_platform_driver(cpcap_charger_driver
);
976 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
977 MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
978 MODULE_LICENSE("GPL v2");
979 MODULE_ALIAS("platform:cpcap-charger");