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