Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / power / supply / cpcap-charger.c
1 /*
2  * Motorola CPCAP PMIC battery charger driver
3  *
4  * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
5  *
6  * Rewritten for Linux power framework with some parts based on
7  * on earlier driver found in the Motorola Linux kernel:
8  *
9  * Copyright (C) 2009-2010 Motorola, Inc.
10  *
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.
14  *
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.
19  */
20
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>
28 #include <linux/of.h>
29 #include <linux/of_platform.h>
30 #include <linux/platform_device.h>
31 #include <linux/power_supply.h>
32 #include <linux/regmap.h>
33
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>
40
41 /*
42  * CPCAP_REG_CRM register bits. For documentation of somewhat similar hardware,
43  * see NXP "MC13783 Power Management and Audio Circuit Users's Guide"
44  * MC13783UG.pdf chapter "8.5 Battery Interface Register Summary". The registers
45  * and values for CPCAP are different, but some of the internal components seem
46  * similar. Also see the Motorola Linux kernel cpcap-regbits.h. CPCAP_REG_CHRGR_1
47  * bits that seem to describe the CRM register.
48  */
49 #define CPCAP_REG_CRM_UNUSED_641_15     BIT(15) /* 641 = register number */
50 #define CPCAP_REG_CRM_UNUSED_641_14     BIT(14) /* 641 = register number */
51 #define CPCAP_REG_CRM_CHRG_LED_EN       BIT(13) /* Charger LED */
52 #define CPCAP_REG_CRM_RVRSMODE          BIT(12) /* USB VBUS output enable */
53 #define CPCAP_REG_CRM_ICHRG_TR1         BIT(11) /* Trickle charge current */
54 #define CPCAP_REG_CRM_ICHRG_TR0         BIT(10)
55 #define CPCAP_REG_CRM_FET_OVRD          BIT(9)  /* 0 = hardware, 1 = FET_CTRL */
56 #define CPCAP_REG_CRM_FET_CTRL          BIT(8)  /* BPFET 1 if FET_OVRD set */
57 #define CPCAP_REG_CRM_VCHRG3            BIT(7)  /* Charge voltage bits */
58 #define CPCAP_REG_CRM_VCHRG2            BIT(6)
59 #define CPCAP_REG_CRM_VCHRG1            BIT(5)
60 #define CPCAP_REG_CRM_VCHRG0            BIT(4)
61 #define CPCAP_REG_CRM_ICHRG3            BIT(3)  /* Charge current bits */
62 #define CPCAP_REG_CRM_ICHRG2            BIT(2)
63 #define CPCAP_REG_CRM_ICHRG1            BIT(1)
64 #define CPCAP_REG_CRM_ICHRG0            BIT(0)
65
66 /* CPCAP_REG_CRM trickle charge voltages */
67 #define CPCAP_REG_CRM_TR(val)           (((val) & 0x3) << 10)
68 #define CPCAP_REG_CRM_TR_0A00           CPCAP_REG_CRM_TR(0x0)
69 #define CPCAP_REG_CRM_TR_0A24           CPCAP_REG_CRM_TR(0x1)
70 #define CPCAP_REG_CRM_TR_0A48           CPCAP_REG_CRM_TR(0x2)
71 #define CPCAP_REG_CRM_TR_0A72           CPCAP_REG_CRM_TR(0x4)
72
73 /*
74  * CPCAP_REG_CRM charge voltages based on the ADC channel 1 values.
75  * Note that these register bits don't match MC13783UG.pdf VCHRG
76  * register bits.
77  */
78 #define CPCAP_REG_CRM_VCHRG(val)        (((val) & 0xf) << 4)
79 #define CPCAP_REG_CRM_VCHRG_3V80        CPCAP_REG_CRM_VCHRG(0x0)
80 #define CPCAP_REG_CRM_VCHRG_4V10        CPCAP_REG_CRM_VCHRG(0x1)
81 #define CPCAP_REG_CRM_VCHRG_4V12        CPCAP_REG_CRM_VCHRG(0x2)
82 #define CPCAP_REG_CRM_VCHRG_4V15        CPCAP_REG_CRM_VCHRG(0x3)
83 #define CPCAP_REG_CRM_VCHRG_4V17        CPCAP_REG_CRM_VCHRG(0x4)
84 #define CPCAP_REG_CRM_VCHRG_4V20        CPCAP_REG_CRM_VCHRG(0x5)
85 #define CPCAP_REG_CRM_VCHRG_4V23        CPCAP_REG_CRM_VCHRG(0x6)
86 #define CPCAP_REG_CRM_VCHRG_4V25        CPCAP_REG_CRM_VCHRG(0x7)
87 #define CPCAP_REG_CRM_VCHRG_4V27        CPCAP_REG_CRM_VCHRG(0x8)
88 #define CPCAP_REG_CRM_VCHRG_4V30        CPCAP_REG_CRM_VCHRG(0x9)
89 #define CPCAP_REG_CRM_VCHRG_4V33        CPCAP_REG_CRM_VCHRG(0xa)
90 #define CPCAP_REG_CRM_VCHRG_4V35        CPCAP_REG_CRM_VCHRG(0xb)
91 #define CPCAP_REG_CRM_VCHRG_4V38        CPCAP_REG_CRM_VCHRG(0xc)
92 #define CPCAP_REG_CRM_VCHRG_4V40        CPCAP_REG_CRM_VCHRG(0xd)
93 #define CPCAP_REG_CRM_VCHRG_4V42        CPCAP_REG_CRM_VCHRG(0xe)
94 #define CPCAP_REG_CRM_VCHRG_4V44        CPCAP_REG_CRM_VCHRG(0xf)
95
96 /*
97  * CPCAP_REG_CRM charge currents. These seem to match MC13783UG.pdf
98  * values in "Table 8-3. Charge Path Regulator Current Limit
99  * Characteristics" for the nominal values.
100  */
101 #define CPCAP_REG_CRM_ICHRG(val)        (((val) & 0xf) << 0)
102 #define CPCAP_REG_CRM_ICHRG_0A000       CPCAP_REG_CRM_ICHRG(0x0)
103 #define CPCAP_REG_CRM_ICHRG_0A070       CPCAP_REG_CRM_ICHRG(0x1)
104 #define CPCAP_REG_CRM_ICHRG_0A177       CPCAP_REG_CRM_ICHRG(0x2)
105 #define CPCAP_REG_CRM_ICHRG_0A266       CPCAP_REG_CRM_ICHRG(0x3)
106 #define CPCAP_REG_CRM_ICHRG_0A355       CPCAP_REG_CRM_ICHRG(0x4)
107 #define CPCAP_REG_CRM_ICHRG_0A443       CPCAP_REG_CRM_ICHRG(0x5)
108 #define CPCAP_REG_CRM_ICHRG_0A532       CPCAP_REG_CRM_ICHRG(0x6)
109 #define CPCAP_REG_CRM_ICHRG_0A621       CPCAP_REG_CRM_ICHRG(0x7)
110 #define CPCAP_REG_CRM_ICHRG_0A709       CPCAP_REG_CRM_ICHRG(0x8)
111 #define CPCAP_REG_CRM_ICHRG_0A798       CPCAP_REG_CRM_ICHRG(0x9)
112 #define CPCAP_REG_CRM_ICHRG_0A886       CPCAP_REG_CRM_ICHRG(0xa)
113 #define CPCAP_REG_CRM_ICHRG_0A975       CPCAP_REG_CRM_ICHRG(0xb)
114 #define CPCAP_REG_CRM_ICHRG_1A064       CPCAP_REG_CRM_ICHRG(0xc)
115 #define CPCAP_REG_CRM_ICHRG_1A152       CPCAP_REG_CRM_ICHRG(0xd)
116 #define CPCAP_REG_CRM_ICHRG_1A596       CPCAP_REG_CRM_ICHRG(0xe)
117 #define CPCAP_REG_CRM_ICHRG_NO_LIMIT    CPCAP_REG_CRM_ICHRG(0xf)
118
119 enum {
120         CPCAP_CHARGER_IIO_BATTDET,
121         CPCAP_CHARGER_IIO_VOLTAGE,
122         CPCAP_CHARGER_IIO_VBUS,
123         CPCAP_CHARGER_IIO_CHRG_CURRENT,
124         CPCAP_CHARGER_IIO_BATT_CURRENT,
125         CPCAP_CHARGER_IIO_NR,
126 };
127
128 struct cpcap_charger_ddata {
129         struct device *dev;
130         struct regmap *reg;
131         struct list_head irq_list;
132         struct delayed_work detect_work;
133         struct delayed_work vbus_work;
134         struct gpio_desc *gpio[2];              /* gpio_reven0 & 1 */
135
136         struct iio_channel *channels[CPCAP_CHARGER_IIO_NR];
137
138         struct power_supply *usb;
139
140         struct phy_companion comparator;        /* For USB VBUS */
141         bool vbus_enabled;
142         atomic_t active;
143
144         int status;
145 };
146
147 struct cpcap_interrupt_desc {
148         int irq;
149         struct list_head node;
150         const char *name;
151 };
152
153 struct cpcap_charger_ints_state {
154         bool chrg_det;
155         bool rvrs_chrg;
156         bool vbusov;
157
158         bool chrg_se1b;
159         bool rvrs_mode;
160         bool chrgcurr1;
161         bool vbusvld;
162
163         bool battdetb;
164 };
165
166 static enum power_supply_property cpcap_charger_props[] = {
167         POWER_SUPPLY_PROP_STATUS,
168         POWER_SUPPLY_PROP_ONLINE,
169         POWER_SUPPLY_PROP_VOLTAGE_NOW,
170         POWER_SUPPLY_PROP_CURRENT_NOW,
171 };
172
173 static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata)
174 {
175         struct iio_channel *channel;
176         int error, value;
177
178         channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET];
179         error = iio_read_channel_raw(channel, &value);
180         if (error < 0) {
181                 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
182
183                 return false;
184         }
185
186         return value == 1;
187 }
188
189 static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata)
190 {
191         struct iio_channel *channel;
192         int error, value = 0;
193
194         channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE];
195         error = iio_read_channel_processed(channel, &value);
196         if (error < 0) {
197                 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
198
199                 return 0;
200         }
201
202         return value;
203 }
204
205 static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata)
206 {
207         struct iio_channel *channel;
208         int error, value = 0;
209
210         channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT];
211         error = iio_read_channel_processed(channel, &value);
212         if (error < 0) {
213                 dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
214
215                 return 0;
216         }
217
218         return value;
219 }
220
221 static int cpcap_charger_get_property(struct power_supply *psy,
222                                       enum power_supply_property psp,
223                                       union power_supply_propval *val)
224 {
225         struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
226
227         switch (psp) {
228         case POWER_SUPPLY_PROP_STATUS:
229                 val->intval = ddata->status;
230                 break;
231         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
232                 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
233                         val->intval = cpcap_charger_get_charge_voltage(ddata) *
234                                 1000;
235                 else
236                         val->intval = 0;
237                 break;
238         case POWER_SUPPLY_PROP_CURRENT_NOW:
239                 if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
240                         val->intval = cpcap_charger_get_charge_current(ddata) *
241                                 1000;
242                 else
243                         val->intval = 0;
244                 break;
245         case POWER_SUPPLY_PROP_ONLINE:
246                 val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING;
247                 break;
248         default:
249                 return -EINVAL;
250         }
251
252         return 0;
253 }
254
255 static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
256                                          bool enabled)
257 {
258         if (!ddata->gpio[0])
259                 return;
260
261         gpiod_set_value(ddata->gpio[0], enabled);
262 }
263
264 static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata,
265                                              bool enabled)
266 {
267         if (!ddata->gpio[1])
268                 return;
269
270         gpiod_set_value(ddata->gpio[1], enabled);
271 }
272
273 static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata,
274                                    int max_voltage, int charge_current,
275                                    int trickle_current)
276 {
277         bool enable;
278         int error;
279
280         enable = (charge_current || trickle_current);
281         dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable);
282
283         if (!enable) {
284                 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
285                                            0x3fff,
286                                            CPCAP_REG_CRM_FET_OVRD |
287                                            CPCAP_REG_CRM_FET_CTRL);
288                 if (error) {
289                         ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
290                         goto out_err;
291                 }
292
293                 ddata->status = POWER_SUPPLY_STATUS_DISCHARGING;
294
295                 return 0;
296         }
297
298         error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff,
299                                    CPCAP_REG_CRM_CHRG_LED_EN |
300                                    trickle_current |
301                                    CPCAP_REG_CRM_FET_OVRD |
302                                    CPCAP_REG_CRM_FET_CTRL |
303                                    max_voltage |
304                                    charge_current);
305         if (error) {
306                 ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
307                 goto out_err;
308         }
309
310         ddata->status = POWER_SUPPLY_STATUS_CHARGING;
311
312         return 0;
313
314 out_err:
315         dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
316
317         return error;
318 }
319
320 static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata)
321 {
322         int error, value = 0;
323         struct iio_channel *channel =
324                 ddata->channels[CPCAP_CHARGER_IIO_VBUS];
325
326         error = iio_read_channel_processed(channel, &value);
327         if (error >= 0)
328                 return value > 3900 ? true : false;
329
330         dev_err(ddata->dev, "error reading VBUS: %i\n", error);
331
332         return false;
333 }
334
335 /* VBUS control functions for the USB PHY companion */
336
337 static void cpcap_charger_vbus_work(struct work_struct *work)
338 {
339         struct cpcap_charger_ddata *ddata;
340         bool vbus = false;
341         int error;
342
343         ddata = container_of(work, struct cpcap_charger_ddata,
344                              vbus_work.work);
345
346         if (ddata->vbus_enabled) {
347                 vbus = cpcap_charger_vbus_valid(ddata);
348                 if (vbus) {
349                         dev_info(ddata->dev, "VBUS already provided\n");
350
351                         return;
352                 }
353
354                 cpcap_charger_set_cable_path(ddata, false);
355                 cpcap_charger_set_inductive_path(ddata, false);
356
357                 error = cpcap_charger_set_state(ddata, 0, 0, 0);
358                 if (error)
359                         goto out_err;
360
361                 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
362                                            CPCAP_REG_CRM_RVRSMODE,
363                                            CPCAP_REG_CRM_RVRSMODE);
364                 if (error)
365                         goto out_err;
366         } else {
367                 error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
368                                            CPCAP_REG_CRM_RVRSMODE, 0);
369                 if (error)
370                         goto out_err;
371
372                 cpcap_charger_set_cable_path(ddata, true);
373                 cpcap_charger_set_inductive_path(ddata, true);
374         }
375
376         return;
377
378 out_err:
379         dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__,
380                 ddata->vbus_enabled ? "enable" : "disable", error);
381 }
382
383 static int cpcap_charger_set_vbus(struct phy_companion *comparator,
384                                   bool enabled)
385 {
386         struct cpcap_charger_ddata *ddata =
387                 container_of(comparator, struct cpcap_charger_ddata,
388                              comparator);
389
390         ddata->vbus_enabled = enabled;
391         schedule_delayed_work(&ddata->vbus_work, 0);
392
393         return 0;
394 }
395
396 /* Charger interrupt handling functions */
397
398 static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata,
399                                         struct cpcap_charger_ints_state *s)
400 {
401         int val, error;
402
403         error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
404         if (error)
405                 return error;
406
407         s->chrg_det = val & BIT(13);
408         s->rvrs_chrg = val & BIT(12);
409         s->vbusov = val & BIT(11);
410
411         error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
412         if (error)
413                 return error;
414
415         s->chrg_se1b = val & BIT(13);
416         s->rvrs_mode = val & BIT(6);
417         s->chrgcurr1 = val & BIT(4);
418         s->vbusvld = val & BIT(3);
419
420         error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
421         if (error)
422                 return error;
423
424         s->battdetb = val & BIT(6);
425
426         return 0;
427 }
428
429 static void cpcap_usb_detect(struct work_struct *work)
430 {
431         struct cpcap_charger_ddata *ddata;
432         struct cpcap_charger_ints_state s;
433         int error;
434
435         ddata = container_of(work, struct cpcap_charger_ddata,
436                              detect_work.work);
437
438         error = cpcap_charger_get_ints_state(ddata, &s);
439         if (error)
440                 return;
441
442         if (cpcap_charger_vbus_valid(ddata) && s.chrgcurr1) {
443                 int max_current;
444
445                 if (cpcap_charger_battery_found(ddata))
446                         max_current = CPCAP_REG_CRM_ICHRG_1A596;
447                 else
448                         max_current = CPCAP_REG_CRM_ICHRG_0A532;
449
450                 error = cpcap_charger_set_state(ddata,
451                                                 CPCAP_REG_CRM_VCHRG_4V35,
452                                                 max_current, 0);
453                 if (error)
454                         goto out_err;
455         } else {
456                 error = cpcap_charger_set_state(ddata, 0, 0, 0);
457                 if (error)
458                         goto out_err;
459         }
460
461         power_supply_changed(ddata->usb);
462         return;
463
464 out_err:
465         dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
466 }
467
468 static irqreturn_t cpcap_charger_irq_thread(int irq, void *data)
469 {
470         struct cpcap_charger_ddata *ddata = data;
471
472         if (!atomic_read(&ddata->active))
473                 return IRQ_NONE;
474
475         schedule_delayed_work(&ddata->detect_work, 0);
476
477         return IRQ_HANDLED;
478 }
479
480 static int cpcap_usb_init_irq(struct platform_device *pdev,
481                               struct cpcap_charger_ddata *ddata,
482                               const char *name)
483 {
484         struct cpcap_interrupt_desc *d;
485         int irq, error;
486
487         irq = platform_get_irq_byname(pdev, name);
488         if (irq < 0)
489                 return -ENODEV;
490
491         error = devm_request_threaded_irq(ddata->dev, irq, NULL,
492                                           cpcap_charger_irq_thread,
493                                           IRQF_SHARED,
494                                           name, ddata);
495         if (error) {
496                 dev_err(ddata->dev, "could not get irq %s: %i\n",
497                         name, error);
498
499                 return error;
500         }
501
502         d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
503         if (!d)
504                 return -ENOMEM;
505
506         d->name = name;
507         d->irq = irq;
508         list_add(&d->node, &ddata->irq_list);
509
510         return 0;
511 }
512
513 static const char * const cpcap_charger_irqs[] = {
514         /* REG_INT_0 */
515         "chrg_det", "rvrs_chrg",
516
517         /* REG_INT1 */
518         "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr1", "vbusvld",
519
520         /* REG_INT_3 */
521         "battdetb",
522 };
523
524 static int cpcap_usb_init_interrupts(struct platform_device *pdev,
525                                      struct cpcap_charger_ddata *ddata)
526 {
527         int i, error;
528
529         for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) {
530                 error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]);
531                 if (error)
532                         return error;
533         }
534
535         return 0;
536 }
537
538 static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata)
539 {
540         int i;
541
542         for (i = 0; i < 2; i++) {
543                 ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
544                                                       i, GPIOD_OUT_HIGH);
545                 if (IS_ERR(ddata->gpio[i])) {
546                         dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
547                                  i, PTR_ERR(ddata->gpio[i]));
548                         ddata->gpio[i] = NULL;
549                 }
550         }
551 }
552
553 static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
554 {
555         const char * const names[CPCAP_CHARGER_IIO_NR] = {
556                 "battdetb", "battp", "vbus", "chg_isense", "batti",
557         };
558         int error, i;
559
560         for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) {
561                 ddata->channels[i] = devm_iio_channel_get(ddata->dev,
562                                                           names[i]);
563                 if (IS_ERR(ddata->channels[i])) {
564                         error = PTR_ERR(ddata->channels[i]);
565                         goto out_err;
566                 }
567
568                 if (!ddata->channels[i]->indio_dev) {
569                         error = -ENXIO;
570                         goto out_err;
571                 }
572         }
573
574         return 0;
575
576 out_err:
577         if (error != -EPROBE_DEFER)
578                 dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
579                         error);
580
581         return error;
582 }
583
584 static const struct power_supply_desc cpcap_charger_usb_desc = {
585         .name           = "usb",
586         .type           = POWER_SUPPLY_TYPE_USB,
587         .properties     = cpcap_charger_props,
588         .num_properties = ARRAY_SIZE(cpcap_charger_props),
589         .get_property   = cpcap_charger_get_property,
590 };
591
592 #ifdef CONFIG_OF
593 static const struct of_device_id cpcap_charger_id_table[] = {
594         {
595                 .compatible = "motorola,mapphone-cpcap-charger",
596         },
597         {},
598 };
599 MODULE_DEVICE_TABLE(of, cpcap_charger_id_table);
600 #endif
601
602 static int cpcap_charger_probe(struct platform_device *pdev)
603 {
604         struct cpcap_charger_ddata *ddata;
605         const struct of_device_id *of_id;
606         struct power_supply_config psy_cfg = {};
607         int error;
608
609         of_id = of_match_device(of_match_ptr(cpcap_charger_id_table),
610                                 &pdev->dev);
611         if (!of_id)
612                 return -EINVAL;
613
614         ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
615         if (!ddata)
616                 return -ENOMEM;
617
618         ddata->dev = &pdev->dev;
619
620         ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
621         if (!ddata->reg)
622                 return -ENODEV;
623
624         INIT_LIST_HEAD(&ddata->irq_list);
625         INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
626         INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work);
627         platform_set_drvdata(pdev, ddata);
628
629         error = cpcap_charger_init_iio(ddata);
630         if (error)
631                 return error;
632
633         atomic_set(&ddata->active, 1);
634
635         psy_cfg.of_node = pdev->dev.of_node;
636         psy_cfg.drv_data = ddata;
637
638         ddata->usb = devm_power_supply_register(ddata->dev,
639                                                 &cpcap_charger_usb_desc,
640                                                 &psy_cfg);
641         if (IS_ERR(ddata->usb)) {
642                 error = PTR_ERR(ddata->usb);
643                 dev_err(ddata->dev, "failed to register USB charger: %i\n",
644                         error);
645
646                 return error;
647         }
648
649         error = cpcap_usb_init_interrupts(pdev, ddata);
650         if (error)
651                 return error;
652
653         ddata->comparator.set_vbus = cpcap_charger_set_vbus;
654         error = omap_usb2_set_comparator(&ddata->comparator);
655         if (error == -ENODEV) {
656                 dev_info(ddata->dev, "charger needs phy, deferring probe\n");
657                 return -EPROBE_DEFER;
658         }
659
660         cpcap_charger_init_optional_gpios(ddata);
661
662         schedule_delayed_work(&ddata->detect_work, 0);
663
664         return 0;
665 }
666
667 static int cpcap_charger_remove(struct platform_device *pdev)
668 {
669         struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev);
670         int error;
671
672         atomic_set(&ddata->active, 0);
673         error = omap_usb2_set_comparator(NULL);
674         if (error)
675                 dev_warn(ddata->dev, "could not clear USB comparator: %i\n",
676                          error);
677
678         error = cpcap_charger_set_state(ddata, 0, 0, 0);
679         if (error)
680                 dev_warn(ddata->dev, "could not clear charger: %i\n",
681                          error);
682         cancel_delayed_work_sync(&ddata->vbus_work);
683         cancel_delayed_work_sync(&ddata->detect_work);
684
685         return 0;
686 }
687
688 static struct platform_driver cpcap_charger_driver = {
689         .probe = cpcap_charger_probe,
690         .driver = {
691                 .name   = "cpcap-charger",
692                 .of_match_table = of_match_ptr(cpcap_charger_id_table),
693         },
694         .remove = cpcap_charger_remove,
695 };
696 module_platform_driver(cpcap_charger_driver);
697
698 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
699 MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
700 MODULE_LICENSE("GPL v2");
701 MODULE_ALIAS("platform:cpcap-charger");