]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/power/supply/cpcap-charger.c
88bbab6e62f076523b4c8cc5a1981ae9d1c2e861
[linux.git] / drivers / power / supply / cpcap-charger.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Motorola CPCAP PMIC battery charger driver
4  *
5  * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
6  *
7  * Rewritten for Linux power framework with some parts based on
8  * on earlier driver found in the Motorola Linux kernel:
9  *
10  * Copyright (C) 2009-2010 Motorola, Inc.
11  */
12
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/of.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/power_supply.h>
24 #include <linux/regmap.h>
25
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>
32
33 /*
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.
40  */
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)
57
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)
64
65 /*
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
68  * register bits.
69  */
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)
87
88 /*
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.
92  */
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)
110
111 /* CPCAP_REG_VUSBC register bits needed for VBUS */
112 #define CPCAP_BIT_VBUS_SWITCH           BIT(0)  /* VBUS boost to 5V */
113
114 enum {
115         CPCAP_CHARGER_IIO_BATTDET,
116         CPCAP_CHARGER_IIO_VOLTAGE,
117         CPCAP_CHARGER_IIO_VBUS,
118         CPCAP_CHARGER_IIO_CHRG_CURRENT,
119         CPCAP_CHARGER_IIO_BATT_CURRENT,
120         CPCAP_CHARGER_IIO_NR,
121 };
122
123 enum {
124         CPCAP_CHARGER_DISCONNECTED,
125         CPCAP_CHARGER_DETECTING,
126         CPCAP_CHARGER_CHARGING,
127         CPCAP_CHARGER_DONE,
128 };
129
130 struct cpcap_charger_ddata {
131         struct device *dev;
132         struct regmap *reg;
133         struct list_head irq_list;
134         struct delayed_work detect_work;
135         struct delayed_work vbus_work;
136         struct gpio_desc *gpio[2];              /* gpio_reven0 & 1 */
137
138         struct iio_channel *channels[CPCAP_CHARGER_IIO_NR];
139
140         struct power_supply *usb;
141
142         struct phy_companion comparator;        /* For USB VBUS */
143         unsigned int vbus_enabled:1;
144         unsigned int feeding_vbus:1;
145         atomic_t active;
146
147         int status;
148         int state;
149         int voltage;
150 };
151
152 struct cpcap_interrupt_desc {
153         int irq;
154         struct list_head node;
155         const char *name;
156 };
157
158 struct cpcap_charger_ints_state {
159         bool chrg_det;
160         bool rvrs_chrg;
161         bool vbusov;
162
163         bool chrg_se1b;
164         bool rvrs_mode;
165         bool chrgcurr2;
166         bool chrgcurr1;
167         bool vbusvld;
168
169         bool battdetb;
170 };
171
172 static enum power_supply_property cpcap_charger_props[] = {
173         POWER_SUPPLY_PROP_STATUS,
174         POWER_SUPPLY_PROP_ONLINE,
175         POWER_SUPPLY_PROP_VOLTAGE_NOW,
176         POWER_SUPPLY_PROP_CURRENT_NOW,
177 };
178
179 static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata)
180 {
181         struct iio_channel *channel;
182         int error, value;
183
184         channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET];
185         error = iio_read_channel_raw(channel, &value);
186         if (error < 0) {
187                 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
188
189                 return false;
190         }
191
192         return value == 1;
193 }
194
195 static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata)
196 {
197         struct iio_channel *channel;
198         int error, value = 0;
199
200         channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE];
201         error = iio_read_channel_processed(channel, &value);
202         if (error < 0) {
203                 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
204
205                 return 0;
206         }
207
208         return value;
209 }
210
211 static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata)
212 {
213         struct iio_channel *channel;
214         int error, value = 0;
215
216         channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT];
217         error = iio_read_channel_processed(channel, &value);
218         if (error < 0) {
219                 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
220
221                 return 0;
222         }
223
224         return value;
225 }
226
227 static int cpcap_charger_get_property(struct power_supply *psy,
228                                       enum power_supply_property psp,
229                                       union power_supply_propval *val)
230 {
231         struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
232
233         switch (psp) {
234         case POWER_SUPPLY_PROP_STATUS:
235                 val->intval = ddata->status;
236                 break;
237         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
238                 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
239                         val->intval = cpcap_charger_get_charge_voltage(ddata) *
240                                 1000;
241                 else
242                         val->intval = 0;
243                 break;
244         case POWER_SUPPLY_PROP_CURRENT_NOW:
245                 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
246                         val->intval = cpcap_charger_get_charge_current(ddata) *
247                                 1000;
248                 else
249                         val->intval = 0;
250                 break;
251         case POWER_SUPPLY_PROP_ONLINE:
252                 val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING;
253                 break;
254         default:
255                 return -EINVAL;
256         }
257
258         return 0;
259 }
260
261 static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
262                                          bool enabled)
263 {
264         if (!ddata->gpio[0])
265                 return;
266
267         gpiod_set_value(ddata->gpio[0], enabled);
268 }
269
270 static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata,
271                                              bool enabled)
272 {
273         if (!ddata->gpio[1])
274                 return;
275
276         gpiod_set_value(ddata->gpio[1], enabled);
277 }
278
279 static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata,
280                                    int max_voltage, int charge_current,
281                                    int trickle_current)
282 {
283         bool enable;
284         int error;
285
286         enable = (charge_current || trickle_current);
287         dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable);
288
289         if (!enable) {
290                 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
291                                            0x3fff,
292                                            CPCAP_REG_CRM_FET_OVRD |
293                                            CPCAP_REG_CRM_FET_CTRL);
294                 if (error) {
295                         ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
296                         goto out_err;
297                 }
298
299                 ddata->status = POWER_SUPPLY_STATUS_DISCHARGING;
300
301                 return 0;
302         }
303
304         error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff,
305                                    CPCAP_REG_CRM_CHRG_LED_EN |
306                                    trickle_current |
307                                    CPCAP_REG_CRM_FET_OVRD |
308                                    CPCAP_REG_CRM_FET_CTRL |
309                                    max_voltage |
310                                    charge_current);
311         if (error) {
312                 ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
313                 goto out_err;
314         }
315
316         ddata->status = POWER_SUPPLY_STATUS_CHARGING;
317
318         return 0;
319
320 out_err:
321         dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
322
323         return error;
324 }
325
326 static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata)
327 {
328         int error, value = 0;
329         struct iio_channel *channel =
330                 ddata->channels[CPCAP_CHARGER_IIO_VBUS];
331
332         error = iio_read_channel_processed(channel, &value);
333         if (error >= 0)
334                 return value > 3900 ? true : false;
335
336         dev_err(ddata->dev, "error reading VBUS: %i\n", error);
337
338         return false;
339 }
340
341 /* VBUS control functions for the USB PHY companion */
342 static void cpcap_charger_vbus_work(struct work_struct *work)
343 {
344         struct cpcap_charger_ddata *ddata;
345         bool vbus = false;
346         int error;
347
348         ddata = container_of(work, struct cpcap_charger_ddata,
349                              vbus_work.work);
350
351         if (ddata->vbus_enabled) {
352                 vbus = cpcap_charger_vbus_valid(ddata);
353                 if (vbus) {
354                         dev_info(ddata->dev, "VBUS already provided\n");
355
356                         return;
357                 }
358
359                 ddata->feeding_vbus = true;
360                 cpcap_charger_set_cable_path(ddata, false);
361                 cpcap_charger_set_inductive_path(ddata, false);
362
363                 error = cpcap_charger_set_state(ddata, 0, 0, 0);
364                 if (error)
365                         goto out_err;
366
367                 error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC,
368                                            CPCAP_BIT_VBUS_SWITCH,
369                                            CPCAP_BIT_VBUS_SWITCH);
370                 if (error)
371                         goto out_err;
372
373                 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
374                                            CPCAP_REG_CRM_RVRSMODE,
375                                            CPCAP_REG_CRM_RVRSMODE);
376                 if (error)
377                         goto out_err;
378         } else {
379                 error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC,
380                                            CPCAP_BIT_VBUS_SWITCH, 0);
381                 if (error)
382                         goto out_err;
383
384                 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
385                                            CPCAP_REG_CRM_RVRSMODE, 0);
386                 if (error)
387                         goto out_err;
388
389                 cpcap_charger_set_cable_path(ddata, true);
390                 cpcap_charger_set_inductive_path(ddata, true);
391                 ddata->feeding_vbus = false;
392         }
393
394         return;
395
396 out_err:
397         dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__,
398                 ddata->vbus_enabled ? "enable" : "disable", error);
399 }
400
401 static int cpcap_charger_set_vbus(struct phy_companion *comparator,
402                                   bool enabled)
403 {
404         struct cpcap_charger_ddata *ddata =
405                 container_of(comparator, struct cpcap_charger_ddata,
406                              comparator);
407
408         ddata->vbus_enabled = enabled;
409         schedule_delayed_work(&ddata->vbus_work, 0);
410
411         return 0;
412 }
413
414 /* Charger interrupt handling functions */
415
416 static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata,
417                                         struct cpcap_charger_ints_state *s)
418 {
419         int val, error;
420
421         error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
422         if (error)
423                 return error;
424
425         s->chrg_det = val & BIT(13);
426         s->rvrs_chrg = val & BIT(12);
427         s->vbusov = val & BIT(11);
428
429         error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
430         if (error)
431                 return error;
432
433         s->chrg_se1b = val & BIT(13);
434         s->rvrs_mode = val & BIT(6);
435         s->chrgcurr2 = val & BIT(5);
436         s->chrgcurr1 = val & BIT(4);
437         s->vbusvld = val & BIT(3);
438
439         error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
440         if (error)
441                 return error;
442
443         s->battdetb = val & BIT(6);
444
445         return 0;
446 }
447
448 static void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata,
449                                        int state)
450 {
451         const char *status;
452
453         if (state > CPCAP_CHARGER_DONE) {
454                 dev_warn(ddata->dev, "unknown state: %i\n", state);
455
456                 return;
457         }
458
459         ddata->state = state;
460
461         switch (state) {
462         case CPCAP_CHARGER_DISCONNECTED:
463                 status = "DISCONNECTED";
464                 break;
465         case CPCAP_CHARGER_DETECTING:
466                 status = "DETECTING";
467                 break;
468         case CPCAP_CHARGER_CHARGING:
469                 status = "CHARGING";
470                 break;
471         case CPCAP_CHARGER_DONE:
472                 status = "DONE";
473                 break;
474         default:
475                 return;
476         }
477
478         dev_dbg(ddata->dev, "state: %s\n", status);
479 }
480
481 int cpcap_charger_voltage_to_regval(int voltage)
482 {
483         int offset;
484
485         switch (voltage) {
486         case 0 ... 4100000 - 1:
487                 return 0;
488         case 4100000 ... 4200000 - 1:
489                 offset = 1;
490                 break;
491         case 4200000 ... 4300000 - 1:
492                 offset = 0;
493                 break;
494         case 4300000 ... 4380000 - 1:
495                 offset = -1;
496                 break;
497         case 4380000 ... 4440000:
498                 offset = -2;
499                 break;
500         default:
501                 return 0;
502         }
503
504         return ((voltage - 4100000) / 20000) + offset;
505 }
506
507 static void cpcap_charger_disconnect(struct cpcap_charger_ddata *ddata,
508                                      int state, unsigned long delay)
509 {
510         int error;
511
512         error = cpcap_charger_set_state(ddata, 0, 0, 0);
513         if (error)
514                 return;
515
516         cpcap_charger_update_state(ddata, state);
517         power_supply_changed(ddata->usb);
518         schedule_delayed_work(&ddata->detect_work, delay);
519 }
520
521 static void cpcap_usb_detect(struct work_struct *work)
522 {
523         struct cpcap_charger_ddata *ddata;
524         struct cpcap_charger_ints_state s;
525         int error;
526
527         ddata = container_of(work, struct cpcap_charger_ddata,
528                              detect_work.work);
529
530         error = cpcap_charger_get_ints_state(ddata, &s);
531         if (error)
532                 return;
533
534         /* Just init the state if a charger is connected with no chrg_det set */
535         if (!s.chrg_det && s.chrgcurr1 && s.vbusvld) {
536                 cpcap_charger_update_state(ddata, CPCAP_CHARGER_DETECTING);
537
538                 return;
539         }
540
541         /*
542          * If battery voltage is higher than charge voltage, it may have been
543          * charged to 4.35V by Android. Try again in 10 minutes.
544          */
545         if (cpcap_charger_get_charge_voltage(ddata) > ddata->voltage) {
546                 cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING,
547                                          HZ * 60 * 10);
548
549                 return;
550         }
551
552         /* Throttle chrgcurr2 interrupt for charger done and retry */
553         switch (ddata->state) {
554         case CPCAP_CHARGER_CHARGING:
555                 if (s.chrgcurr2)
556                         break;
557                 if (s.chrgcurr1 && s.vbusvld) {
558                         cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DONE,
559                                                  HZ * 5);
560                         return;
561                 }
562                 break;
563         case CPCAP_CHARGER_DONE:
564                 if (!s.chrgcurr2)
565                         break;
566                 cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING,
567                                          HZ * 5);
568                 return;
569         default:
570                 break;
571         }
572
573         if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) &&
574             s.chrgcurr1) {
575                 int max_current;
576                 int vchrg;
577
578                 if (cpcap_charger_battery_found(ddata))
579                         max_current = CPCAP_REG_CRM_ICHRG_1A596;
580                 else
581                         max_current = CPCAP_REG_CRM_ICHRG_0A532;
582
583                 vchrg = cpcap_charger_voltage_to_regval(ddata->voltage);
584                 error = cpcap_charger_set_state(ddata,
585                                                 CPCAP_REG_CRM_VCHRG(vchrg),
586                                                 max_current, 0);
587                 if (error)
588                         goto out_err;
589                 cpcap_charger_update_state(ddata, CPCAP_CHARGER_CHARGING);
590         } else {
591                 error = cpcap_charger_set_state(ddata, 0, 0, 0);
592                 if (error)
593                         goto out_err;
594                 cpcap_charger_update_state(ddata, CPCAP_CHARGER_DISCONNECTED);
595         }
596
597         power_supply_changed(ddata->usb);
598         return;
599
600 out_err:
601         dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
602 }
603
604 static irqreturn_t cpcap_charger_irq_thread(int irq, void *data)
605 {
606         struct cpcap_charger_ddata *ddata = data;
607
608         if (!atomic_read(&ddata->active))
609                 return IRQ_NONE;
610
611         schedule_delayed_work(&ddata->detect_work, 0);
612
613         return IRQ_HANDLED;
614 }
615
616 static int cpcap_usb_init_irq(struct platform_device *pdev,
617                               struct cpcap_charger_ddata *ddata,
618                               const char *name)
619 {
620         struct cpcap_interrupt_desc *d;
621         int irq, error;
622
623         irq = platform_get_irq_byname(pdev, name);
624         if (irq < 0)
625                 return -ENODEV;
626
627         error = devm_request_threaded_irq(ddata->dev, irq, NULL,
628                                           cpcap_charger_irq_thread,
629                                           IRQF_SHARED,
630                                           name, ddata);
631         if (error) {
632                 dev_err(ddata->dev, "could not get irq %s: %i\n",
633                         name, error);
634
635                 return error;
636         }
637
638         d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
639         if (!d)
640                 return -ENOMEM;
641
642         d->name = name;
643         d->irq = irq;
644         list_add(&d->node, &ddata->irq_list);
645
646         return 0;
647 }
648
649 static const char * const cpcap_charger_irqs[] = {
650         /* REG_INT_0 */
651         "chrg_det", "rvrs_chrg",
652
653         /* REG_INT1 */
654         "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr2", "chrgcurr1", "vbusvld",
655
656         /* REG_INT_3 */
657         "battdetb",
658 };
659
660 static int cpcap_usb_init_interrupts(struct platform_device *pdev,
661                                      struct cpcap_charger_ddata *ddata)
662 {
663         int i, error;
664
665         for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) {
666                 error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]);
667                 if (error)
668                         return error;
669         }
670
671         return 0;
672 }
673
674 static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata)
675 {
676         int i;
677
678         for (i = 0; i < 2; i++) {
679                 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
680                                                       i, GPIOD_OUT_HIGH);
681                 if (IS_ERR(ddata->gpio[i])) {
682                         dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
683                                  i, PTR_ERR(ddata->gpio[i]));
684                         ddata->gpio[i] = NULL;
685                 }
686         }
687 }
688
689 static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
690 {
691         const char * const names[CPCAP_CHARGER_IIO_NR] = {
692                 "battdetb", "battp", "vbus", "chg_isense", "batti",
693         };
694         int error, i;
695
696         for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) {
697                 ddata->channels[i] = devm_iio_channel_get(ddata->dev,
698                                                           names[i]);
699                 if (IS_ERR(ddata->channels[i])) {
700                         error = PTR_ERR(ddata->channels[i]);
701                         goto out_err;
702                 }
703
704                 if (!ddata->channels[i]->indio_dev) {
705                         error = -ENXIO;
706                         goto out_err;
707                 }
708         }
709
710         return 0;
711
712 out_err:
713         if (error != -EPROBE_DEFER)
714                 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
715                         error);
716
717         return error;
718 }
719
720 static const struct power_supply_desc cpcap_charger_usb_desc = {
721         .name           = "usb",
722         .type           = POWER_SUPPLY_TYPE_USB,
723         .properties     = cpcap_charger_props,
724         .num_properties = ARRAY_SIZE(cpcap_charger_props),
725         .get_property   = cpcap_charger_get_property,
726 };
727
728 #ifdef CONFIG_OF
729 static const struct of_device_id cpcap_charger_id_table[] = {
730         {
731                 .compatible = "motorola,mapphone-cpcap-charger",
732         },
733         {},
734 };
735 MODULE_DEVICE_TABLE(of, cpcap_charger_id_table);
736 #endif
737
738 static int cpcap_charger_probe(struct platform_device *pdev)
739 {
740         struct cpcap_charger_ddata *ddata;
741         const struct of_device_id *of_id;
742         struct power_supply_config psy_cfg = {};
743         int error;
744
745         of_id = of_match_device(of_match_ptr(cpcap_charger_id_table),
746                                 &pdev->dev);
747         if (!of_id)
748                 return -EINVAL;
749
750         ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
751         if (!ddata)
752                 return -ENOMEM;
753
754         ddata->dev = &pdev->dev;
755         ddata->voltage = 4200000;
756
757         ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
758         if (!ddata->reg)
759                 return -ENODEV;
760
761         INIT_LIST_HEAD(&ddata->irq_list);
762         INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
763         INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work);
764         platform_set_drvdata(pdev, ddata);
765
766         error = cpcap_charger_init_iio(ddata);
767         if (error)
768                 return error;
769
770         atomic_set(&ddata->active, 1);
771
772         psy_cfg.of_node = pdev->dev.of_node;
773         psy_cfg.drv_data = ddata;
774
775         ddata->usb = devm_power_supply_register(ddata->dev,
776                                                 &cpcap_charger_usb_desc,
777                                                 &psy_cfg);
778         if (IS_ERR(ddata->usb)) {
779                 error = PTR_ERR(ddata->usb);
780                 dev_err(ddata->dev, "failed to register USB charger: %i\n",
781                         error);
782
783                 return error;
784         }
785
786         error = cpcap_usb_init_interrupts(pdev, ddata);
787         if (error)
788                 return error;
789
790         ddata->comparator.set_vbus = cpcap_charger_set_vbus;
791         error = omap_usb2_set_comparator(&ddata->comparator);
792         if (error == -ENODEV) {
793                 dev_info(ddata->dev, "charger needs phy, deferring probe\n");
794                 return -EPROBE_DEFER;
795         }
796
797         cpcap_charger_init_optional_gpios(ddata);
798
799         schedule_delayed_work(&ddata->detect_work, 0);
800
801         return 0;
802 }
803
804 static int cpcap_charger_remove(struct platform_device *pdev)
805 {
806         struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev);
807         int error;
808
809         atomic_set(&ddata->active, 0);
810         error = omap_usb2_set_comparator(NULL);
811         if (error)
812                 dev_warn(ddata->dev, "could not clear USB comparator: %i\n",
813                          error);
814
815         error = cpcap_charger_set_state(ddata, 0, 0, 0);
816         if (error)
817                 dev_warn(ddata->dev, "could not clear charger: %i\n",
818                          error);
819         cancel_delayed_work_sync(&ddata->vbus_work);
820         cancel_delayed_work_sync(&ddata->detect_work);
821
822         return 0;
823 }
824
825 static struct platform_driver cpcap_charger_driver = {
826         .probe = cpcap_charger_probe,
827         .driver = {
828                 .name   = "cpcap-charger",
829                 .of_match_table = of_match_ptr(cpcap_charger_id_table),
830         },
831         .remove = cpcap_charger_remove,
832 };
833 module_platform_driver(cpcap_charger_driver);
834
835 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
836 MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
837 MODULE_LICENSE("GPL v2");
838 MODULE_ALIAS("platform:cpcap-charger");