power_supply: charger-manager: Use power_supply_*() API for accessing function attrs
[linux-2.6-block.git] / drivers / power / 88pm860x_charger.c
CommitLineData
a830d28b
JZ
1/*
2 * Battery driver for Marvell 88PM860x PMIC
3 *
4 * Copyright (c) 2012 Marvell International Ltd.
5 * Author: Jett Zhou <jtzhou@marvell.com>
6 * Haojian Zhuang <haojian.zhuang@marvell.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/slab.h>
17#include <linux/power_supply.h>
18#include <linux/mfd/88pm860x.h>
19#include <linux/delay.h>
20#include <linux/uaccess.h>
21#include <asm/div64.h>
22
23/* bit definitions of Status Query Interface 2 */
24#define STATUS2_CHG (1 << 2)
25
26/* bit definitions of Reset Out Register */
27#define RESET_SW_PD (1 << 7)
28
29/* bit definitions of PreReg 1 */
30#define PREREG1_90MA (0x0)
31#define PREREG1_180MA (0x1)
32#define PREREG1_450MA (0x4)
33#define PREREG1_540MA (0x5)
34#define PREREG1_1350MA (0xE)
35#define PREREG1_VSYS_4_5V (3 << 4)
36
37/* bit definitions of Charger Control 1 Register */
38#define CC1_MODE_OFF (0)
39#define CC1_MODE_PRECHARGE (1)
40#define CC1_MODE_FASTCHARGE (2)
41#define CC1_MODE_PULSECHARGE (3)
42#define CC1_ITERM_20MA (0 << 2)
43#define CC1_ITERM_60MA (2 << 2)
44#define CC1_VFCHG_4_2V (9 << 4)
45
46/* bit definitions of Charger Control 2 Register */
47#define CC2_ICHG_100MA (0x1)
48#define CC2_ICHG_500MA (0x9)
49#define CC2_ICHG_1000MA (0x13)
50
51/* bit definitions of Charger Control 3 Register */
52#define CC3_180MIN_TIMEOUT (0x6 << 4)
53#define CC3_270MIN_TIMEOUT (0x7 << 4)
54#define CC3_360MIN_TIMEOUT (0xA << 4)
55#define CC3_DISABLE_TIMEOUT (0xF << 4)
56
57/* bit definitions of Charger Control 4 Register */
58#define CC4_IPRE_40MA (7)
59#define CC4_VPCHG_3_2V (3 << 4)
60#define CC4_IFCHG_MON_EN (1 << 6)
61#define CC4_BTEMP_MON_EN (1 << 7)
62
63/* bit definitions of Charger Control 6 Register */
64#define CC6_BAT_OV_EN (1 << 2)
65#define CC6_BAT_UV_EN (1 << 3)
66#define CC6_UV_VBAT_SET (0x3 << 6) /* 2.8v */
67
68/* bit definitions of Charger Control 7 Register */
69#define CC7_BAT_REM_EN (1 << 3)
70#define CC7_IFSM_EN (1 << 7)
71
72/* bit definitions of Measurement Enable 1 Register */
73#define MEAS1_VBAT (1 << 0)
74
75/* bit definitions of Measurement Enable 3 Register */
76#define MEAS3_IBAT_EN (1 << 0)
77#define MEAS3_CC_EN (1 << 2)
78
79#define FSM_INIT 0
80#define FSM_DISCHARGE 1
81#define FSM_PRECHARGE 2
82#define FSM_FASTCHARGE 3
83
84#define PRECHARGE_THRESHOLD 3100
85#define POWEROFF_THRESHOLD 3400
86#define CHARGE_THRESHOLD 4000
87#define DISCHARGE_THRESHOLD 4180
88
89/* over-temperature on PM8606 setting */
90#define OVER_TEMP_FLAG (1 << 6)
91#define OVTEMP_AUTORECOVER (1 << 3)
92
93/* over-voltage protect on vchg setting mv */
94#define VCHG_NORMAL_LOW 4200
95#define VCHG_NORMAL_CHECK 5800
96#define VCHG_NORMAL_HIGH 6000
97#define VCHG_OVP_LOW 5500
98
99struct pm860x_charger_info {
100 struct pm860x_chip *chip;
101 struct i2c_client *i2c;
102 struct i2c_client *i2c_8606;
103 struct device *dev;
104
105 struct power_supply usb;
106 struct mutex lock;
107 int irq_nums;
108 int irq[7];
109 unsigned state:3; /* fsm state */
110 unsigned online:1; /* usb charger */
111 unsigned present:1; /* battery present */
112 unsigned allowed:1;
113};
114
115static char *pm860x_supplied_to[] = {
116 "battery-monitor",
117};
118
119static int measure_vchg(struct pm860x_charger_info *info, int *data)
120{
121 unsigned char buf[2];
122 int ret = 0;
123
124 ret = pm860x_bulk_read(info->i2c, PM8607_VCHG_MEAS1, 2, buf);
125 if (ret < 0)
126 return ret;
127
128 *data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f);
129 /* V_BATT_MEAS(mV) = value * 5 * 1.8 * 1000 / (2^12) */
130 *data = ((*data & 0xfff) * 9 * 125) >> 9;
131
132 dev_dbg(info->dev, "%s, vchg: %d mv\n", __func__, *data);
133
134 return ret;
135}
136
137static void set_vchg_threshold(struct pm860x_charger_info *info,
138 int min, int max)
139{
140 int data;
141
142 /* (tmp << 8) * / 5 / 1800 */
143 if (min <= 0)
144 data = 0;
145 else
146 data = (min << 5) / 1125;
147 pm860x_reg_write(info->i2c, PM8607_VCHG_LOWTH, data);
148 dev_dbg(info->dev, "VCHG_LOWTH:%dmv, 0x%x\n", min, data);
149
150 if (max <= 0)
151 data = 0xff;
152 else
153 data = (max << 5) / 1125;
154 pm860x_reg_write(info->i2c, PM8607_VCHG_HIGHTH, data);
155 dev_dbg(info->dev, "VCHG_HIGHTH:%dmv, 0x%x\n", max, data);
156
157}
158
159static void set_vbatt_threshold(struct pm860x_charger_info *info,
160 int min, int max)
161{
162 int data;
163
164 /* (tmp << 8) * 3 / 1800 */
165 if (min <= 0)
166 data = 0;
167 else
168 data = (min << 5) / 675;
169 pm860x_reg_write(info->i2c, PM8607_VBAT_LOWTH, data);
170 dev_dbg(info->dev, "VBAT Min:%dmv, LOWTH:0x%x\n", min, data);
171
172 if (max <= 0)
173 data = 0xff;
174 else
175 data = (max << 5) / 675;
176 pm860x_reg_write(info->i2c, PM8607_VBAT_HIGHTH, data);
177 dev_dbg(info->dev, "VBAT Max:%dmv, HIGHTH:0x%x\n", max, data);
178
179 return;
180}
181
182static int start_precharge(struct pm860x_charger_info *info)
183{
184 int ret;
185
186 dev_dbg(info->dev, "Start Pre-charging!\n");
187 set_vbatt_threshold(info, 0, 0);
188
189 ret = pm860x_reg_write(info->i2c_8606, PM8606_PREREGULATORA,
190 PREREG1_1350MA | PREREG1_VSYS_4_5V);
191 if (ret < 0)
192 goto out;
193 /* stop charging */
194 ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3,
195 CC1_MODE_OFF);
196 if (ret < 0)
197 goto out;
198 /* set 270 minutes timeout */
199 ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL3, (0xf << 4),
200 CC3_270MIN_TIMEOUT);
201 if (ret < 0)
202 goto out;
203 /* set precharge current, termination voltage, IBAT & TBAT monitor */
204 ret = pm860x_reg_write(info->i2c, PM8607_CHG_CTRL4,
205 CC4_IPRE_40MA | CC4_VPCHG_3_2V |
206 CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN);
207 if (ret < 0)
208 goto out;
209 ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL7,
210 CC7_BAT_REM_EN | CC7_IFSM_EN,
211 CC7_BAT_REM_EN | CC7_IFSM_EN);
212 if (ret < 0)
213 goto out;
214 /* trigger precharge */
215 ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3,
216 CC1_MODE_PRECHARGE);
217out:
218 return ret;
219}
220
221static int start_fastcharge(struct pm860x_charger_info *info)
222{
223 int ret;
224
225 dev_dbg(info->dev, "Start Fast-charging!\n");
226
227 /* set fastcharge termination current & voltage, disable charging */
228 ret = pm860x_reg_write(info->i2c, PM8607_CHG_CTRL1,
229 CC1_MODE_OFF | CC1_ITERM_60MA |
230 CC1_VFCHG_4_2V);
231 if (ret < 0)
232 goto out;
233 ret = pm860x_reg_write(info->i2c_8606, PM8606_PREREGULATORA,
234 PREREG1_540MA | PREREG1_VSYS_4_5V);
235 if (ret < 0)
236 goto out;
237 ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f,
238 CC2_ICHG_500MA);
239 if (ret < 0)
240 goto out;
241 /* set 270 minutes timeout */
242 ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL3, (0xf << 4),
243 CC3_270MIN_TIMEOUT);
244 if (ret < 0)
245 goto out;
246 /* set IBAT & TBAT monitor */
247 ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL4,
248 CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN,
249 CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN);
250 if (ret < 0)
251 goto out;
252 ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL6,
253 CC6_BAT_OV_EN | CC6_BAT_UV_EN |
254 CC6_UV_VBAT_SET,
255 CC6_BAT_OV_EN | CC6_BAT_UV_EN |
256 CC6_UV_VBAT_SET);
257 if (ret < 0)
258 goto out;
259 ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL7,
260 CC7_BAT_REM_EN | CC7_IFSM_EN,
261 CC7_BAT_REM_EN | CC7_IFSM_EN);
262 if (ret < 0)
263 goto out;
264 /* launch fast-charge */
265 ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3,
266 CC1_MODE_FASTCHARGE);
267 /* vchg threshold setting */
268 set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_NORMAL_HIGH);
269out:
270 return ret;
271}
272
273static void stop_charge(struct pm860x_charger_info *info, int vbatt)
274{
275 dev_dbg(info->dev, "Stop charging!\n");
276 pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3, CC1_MODE_OFF);
277 if (vbatt > CHARGE_THRESHOLD && info->online)
278 set_vbatt_threshold(info, CHARGE_THRESHOLD, 0);
279}
280
281static void power_off_notification(struct pm860x_charger_info *info)
282{
283 dev_dbg(info->dev, "Power-off notification!\n");
284}
285
286static int set_charging_fsm(struct pm860x_charger_info *info)
287{
288 struct power_supply *psy;
289 union power_supply_propval data;
290 unsigned char fsm_state[][16] = { "init", "discharge", "precharge",
291 "fastcharge",
292 };
293 int ret;
294 int vbatt;
295
296 psy = power_supply_get_by_name(pm860x_supplied_to[0]);
297 if (!psy)
298 return -EINVAL;
465c436b
KK
299 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW,
300 &data);
a830d28b
JZ
301 if (ret)
302 return ret;
303 vbatt = data.intval / 1000;
304
465c436b 305 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT, &data);
a830d28b
JZ
306 if (ret)
307 return ret;
308
309 mutex_lock(&info->lock);
310 info->present = data.intval;
311
312 dev_dbg(info->dev, "Entering FSM:%s, Charger:%s, Battery:%s, "
313 "Allowed:%d\n",
314 &fsm_state[info->state][0],
315 (info->online) ? "online" : "N/A",
316 (info->present) ? "present" : "N/A", info->allowed);
317 dev_dbg(info->dev, "set_charging_fsm:vbatt:%d(mV)\n", vbatt);
318
319 switch (info->state) {
320 case FSM_INIT:
321 if (info->online && info->present && info->allowed) {
322 if (vbatt < PRECHARGE_THRESHOLD) {
323 info->state = FSM_PRECHARGE;
324 start_precharge(info);
325 } else if (vbatt > DISCHARGE_THRESHOLD) {
326 info->state = FSM_DISCHARGE;
327 stop_charge(info, vbatt);
328 } else if (vbatt < DISCHARGE_THRESHOLD) {
329 info->state = FSM_FASTCHARGE;
330 start_fastcharge(info);
331 }
332 } else {
333 if (vbatt < POWEROFF_THRESHOLD) {
334 power_off_notification(info);
335 } else {
336 info->state = FSM_DISCHARGE;
337 stop_charge(info, vbatt);
338 }
339 }
340 break;
341 case FSM_PRECHARGE:
342 if (info->online && info->present && info->allowed) {
343 if (vbatt > PRECHARGE_THRESHOLD) {
344 info->state = FSM_FASTCHARGE;
345 start_fastcharge(info);
346 }
347 } else {
348 info->state = FSM_DISCHARGE;
349 stop_charge(info, vbatt);
350 }
351 break;
352 case FSM_FASTCHARGE:
353 if (info->online && info->present && info->allowed) {
354 if (vbatt < PRECHARGE_THRESHOLD) {
355 info->state = FSM_PRECHARGE;
356 start_precharge(info);
357 }
358 } else {
359 info->state = FSM_DISCHARGE;
360 stop_charge(info, vbatt);
361 }
362 break;
363 case FSM_DISCHARGE:
364 if (info->online && info->present && info->allowed) {
365 if (vbatt < PRECHARGE_THRESHOLD) {
366 info->state = FSM_PRECHARGE;
367 start_precharge(info);
368 } else if (vbatt < DISCHARGE_THRESHOLD) {
369 info->state = FSM_FASTCHARGE;
370 start_fastcharge(info);
371 }
372 } else {
373 if (vbatt < POWEROFF_THRESHOLD)
374 power_off_notification(info);
375 else if (vbatt > CHARGE_THRESHOLD && info->online)
376 set_vbatt_threshold(info, CHARGE_THRESHOLD, 0);
377 }
378 break;
379 default:
380 dev_warn(info->dev, "FSM meets wrong state:%d\n",
381 info->state);
382 break;
383 }
384 dev_dbg(info->dev,
385 "Out FSM:%s, Charger:%s, Battery:%s, Allowed:%d\n",
386 &fsm_state[info->state][0],
387 (info->online) ? "online" : "N/A",
388 (info->present) ? "present" : "N/A", info->allowed);
389 mutex_unlock(&info->lock);
390
391 return 0;
392}
393
394static irqreturn_t pm860x_charger_handler(int irq, void *data)
395{
396 struct pm860x_charger_info *info = data;
397 int ret;
398
399 mutex_lock(&info->lock);
400 ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
401 if (ret < 0) {
402 mutex_unlock(&info->lock);
403 goto out;
404 }
405 if (ret & STATUS2_CHG) {
406 info->online = 1;
407 info->allowed = 1;
408 } else {
409 info->online = 0;
410 info->allowed = 0;
411 }
412 mutex_unlock(&info->lock);
413 dev_dbg(info->dev, "%s, Charger:%s, Allowed:%d\n", __func__,
414 (info->online) ? "online" : "N/A", info->allowed);
415
416 set_charging_fsm(info);
417
418 power_supply_changed(&info->usb);
419out:
420 return IRQ_HANDLED;
421}
422
423static irqreturn_t pm860x_temp_handler(int irq, void *data)
424{
425 struct power_supply *psy;
426 struct pm860x_charger_info *info = data;
427 union power_supply_propval temp;
428 int value;
429 int ret;
430
431 psy = power_supply_get_by_name(pm860x_supplied_to[0]);
432 if (!psy)
433 goto out;
465c436b 434 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &temp);
a830d28b
JZ
435 if (ret)
436 goto out;
437 value = temp.intval / 10;
438
439 mutex_lock(&info->lock);
440 /* Temperature < -10 C or >40 C, Will not allow charge */
441 if (value < -10 || value > 40)
442 info->allowed = 0;
443 else
444 info->allowed = 1;
445 dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
446 mutex_unlock(&info->lock);
447
448 set_charging_fsm(info);
449out:
450 return IRQ_HANDLED;
451}
452
453static irqreturn_t pm860x_exception_handler(int irq, void *data)
454{
455 struct pm860x_charger_info *info = data;
456
457 mutex_lock(&info->lock);
458 info->allowed = 0;
459 mutex_unlock(&info->lock);
460 dev_dbg(info->dev, "%s, irq: %d\n", __func__, irq);
461
462 set_charging_fsm(info);
463 return IRQ_HANDLED;
464}
465
466static irqreturn_t pm860x_done_handler(int irq, void *data)
467{
468 struct pm860x_charger_info *info = data;
469 struct power_supply *psy;
470 union power_supply_propval val;
471 int ret;
472 int vbatt;
473
474 mutex_lock(&info->lock);
475 /* pre-charge done, will transimit to fast-charge stage */
476 if (info->state == FSM_PRECHARGE) {
477 info->allowed = 1;
478 goto out;
479 }
480 /*
481 * Fast charge done, delay to read
482 * the correct status of CHG_DET.
483 */
484 mdelay(5);
485 info->allowed = 0;
486 psy = power_supply_get_by_name(pm860x_supplied_to[0]);
487 if (!psy)
488 goto out;
465c436b
KK
489 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW,
490 &val);
a830d28b
JZ
491 if (ret)
492 goto out;
493 vbatt = val.intval / 1000;
494 /*
495 * CHG_DONE interrupt is faster than CHG_DET interrupt when
496 * plug in/out usb, So we can not rely on info->online, we
497 * need check pm8607 status register to check usb is online
498 * or not, then we can decide it is real charge done
499 * automatically or it is triggered by usb plug out;
500 */
501 ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
502 if (ret < 0)
503 goto out;
504 if (vbatt > CHARGE_THRESHOLD && ret & STATUS2_CHG)
465c436b
KK
505 power_supply_set_property(psy, POWER_SUPPLY_PROP_CHARGE_FULL,
506 &val);
a830d28b
JZ
507
508out:
509 mutex_unlock(&info->lock);
510 dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
511 set_charging_fsm(info);
512
513 return IRQ_HANDLED;
514}
515
516static irqreturn_t pm860x_vbattery_handler(int irq, void *data)
517{
518 struct pm860x_charger_info *info = data;
519
520 mutex_lock(&info->lock);
521
522 set_vbatt_threshold(info, 0, 0);
523
524 if (info->present && info->online)
525 info->allowed = 1;
526 else
527 info->allowed = 0;
528 mutex_unlock(&info->lock);
529 dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
530
531 set_charging_fsm(info);
532
533 return IRQ_HANDLED;
534}
535
536static irqreturn_t pm860x_vchg_handler(int irq, void *data)
537{
538 struct pm860x_charger_info *info = data;
539 int vchg = 0;
540
541 if (info->present)
542 goto out;
543
544 measure_vchg(info, &vchg);
545
546 mutex_lock(&info->lock);
547 if (!info->online) {
548 int status;
549 /* check if over-temp on pm8606 or not */
550 status = pm860x_reg_read(info->i2c_8606, PM8606_FLAGS);
551 if (status & OVER_TEMP_FLAG) {
552 /* clear over temp flag and set auto recover */
553 pm860x_set_bits(info->i2c_8606, PM8606_FLAGS,
554 OVER_TEMP_FLAG, OVER_TEMP_FLAG);
555 pm860x_set_bits(info->i2c_8606,
556 PM8606_VSYS,
557 OVTEMP_AUTORECOVER,
558 OVTEMP_AUTORECOVER);
559 dev_dbg(info->dev,
0b1587b1 560 "%s, pm8606 over-temp occurred\n", __func__);
a830d28b
JZ
561 }
562 }
563
564 if (vchg > VCHG_NORMAL_CHECK) {
565 set_vchg_threshold(info, VCHG_OVP_LOW, 0);
566 info->allowed = 0;
567 dev_dbg(info->dev,
0b1587b1 568 "%s,pm8607 over-vchg occurred,vchg = %dmv\n",
a830d28b
JZ
569 __func__, vchg);
570 } else if (vchg < VCHG_OVP_LOW) {
571 set_vchg_threshold(info, VCHG_NORMAL_LOW,
572 VCHG_NORMAL_HIGH);
573 info->allowed = 1;
574 dev_dbg(info->dev,
575 "%s,pm8607 over-vchg recover,vchg = %dmv\n",
576 __func__, vchg);
577 }
578 mutex_unlock(&info->lock);
579
580 dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
581 set_charging_fsm(info);
582out:
583 return IRQ_HANDLED;
584}
585
586static int pm860x_usb_get_prop(struct power_supply *psy,
587 enum power_supply_property psp,
588 union power_supply_propval *val)
589{
590 struct pm860x_charger_info *info =
591 dev_get_drvdata(psy->dev->parent);
592
593 switch (psp) {
594 case POWER_SUPPLY_PROP_STATUS:
595 if (info->state == FSM_FASTCHARGE ||
596 info->state == FSM_PRECHARGE)
597 val->intval = POWER_SUPPLY_STATUS_CHARGING;
598 else
599 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
600 break;
601 case POWER_SUPPLY_PROP_ONLINE:
602 val->intval = info->online;
603 break;
604 default:
605 return -ENODEV;
606 }
607 return 0;
608}
609
610static enum power_supply_property pm860x_usb_props[] = {
611 POWER_SUPPLY_PROP_STATUS,
612 POWER_SUPPLY_PROP_ONLINE,
613};
614
615static int pm860x_init_charger(struct pm860x_charger_info *info)
616{
617 int ret;
618
619 ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
620 if (ret < 0)
621 return ret;
622
623 mutex_lock(&info->lock);
624 info->state = FSM_INIT;
625 if (ret & STATUS2_CHG) {
626 info->online = 1;
627 info->allowed = 1;
628 } else {
629 info->online = 0;
630 info->allowed = 0;
631 }
632 mutex_unlock(&info->lock);
633
634 set_charging_fsm(info);
635 return 0;
636}
637
f1ade352 638static struct pm860x_irq_desc {
a830d28b
JZ
639 const char *name;
640 irqreturn_t (*handler)(int irq, void *data);
641} pm860x_irq_descs[] = {
642 { "usb supply detect", pm860x_charger_handler },
643 { "charge done", pm860x_done_handler },
644 { "charge timeout", pm860x_exception_handler },
645 { "charge fault", pm860x_exception_handler },
646 { "temperature", pm860x_temp_handler },
647 { "vbatt", pm860x_vbattery_handler },
648 { "vchg", pm860x_vchg_handler },
649};
650
c8afa640 651static int pm860x_charger_probe(struct platform_device *pdev)
a830d28b
JZ
652{
653 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
2dc9215d 654 struct power_supply_config psy_cfg = {};
a830d28b
JZ
655 struct pm860x_charger_info *info;
656 int ret;
657 int count;
658 int i;
659 int j;
660
661 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
662 if (!info)
663 return -ENOMEM;
664
665 count = pdev->num_resources;
666 for (i = 0, j = 0; i < count; i++) {
667 info->irq[j] = platform_get_irq(pdev, i);
668 if (info->irq[j] < 0)
669 continue;
670 j++;
671 }
672 info->irq_nums = j;
673
674 info->chip = chip;
675 info->i2c =
676 (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
677 info->i2c_8606 =
678 (chip->id == CHIP_PM8607) ? chip->companion : chip->client;
679 if (!info->i2c_8606) {
680 dev_err(&pdev->dev, "Missed I2C address of 88PM8606!\n");
681 ret = -EINVAL;
682 goto out;
683 }
684 info->dev = &pdev->dev;
685
686 /* set init value for the case we are not using battery */
687 set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_OVP_LOW);
688
689 mutex_init(&info->lock);
690 platform_set_drvdata(pdev, info);
691
692 info->usb.name = "usb";
693 info->usb.type = POWER_SUPPLY_TYPE_USB;
a830d28b
JZ
694 info->usb.properties = pm860x_usb_props;
695 info->usb.num_properties = ARRAY_SIZE(pm860x_usb_props);
696 info->usb.get_property = pm860x_usb_get_prop;
2dc9215d
KK
697 psy_cfg.supplied_to = pm860x_supplied_to;
698 psy_cfg.num_supplicants = ARRAY_SIZE(pm860x_supplied_to);
699 ret = power_supply_register(&pdev->dev, &info->usb, &psy_cfg);
a830d28b
JZ
700 if (ret)
701 goto out;
702
703 pm860x_init_charger(info);
704
705 for (i = 0; i < ARRAY_SIZE(info->irq); i++) {
706 ret = request_threaded_irq(info->irq[i], NULL,
707 pm860x_irq_descs[i].handler,
708 IRQF_ONESHOT, pm860x_irq_descs[i].name, info);
709 if (ret < 0) {
710 dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
711 info->irq[i], ret);
712 goto out_irq;
713 }
714 }
715 return 0;
716
717out_irq:
24727b45 718 power_supply_unregister(&info->usb);
a830d28b
JZ
719 while (--i >= 0)
720 free_irq(info->irq[i], info);
721out:
a830d28b
JZ
722 return ret;
723}
724
415ec69f 725static int pm860x_charger_remove(struct platform_device *pdev)
a830d28b
JZ
726{
727 struct pm860x_charger_info *info = platform_get_drvdata(pdev);
728 int i;
729
a830d28b
JZ
730 power_supply_unregister(&info->usb);
731 free_irq(info->irq[0], info);
732 for (i = 0; i < info->irq_nums; i++)
733 free_irq(info->irq[i], info);
a830d28b
JZ
734 return 0;
735}
736
737static struct platform_driver pm860x_charger_driver = {
738 .driver = {
739 .name = "88pm860x-charger",
a830d28b
JZ
740 },
741 .probe = pm860x_charger_probe,
28ea73f4 742 .remove = pm860x_charger_remove,
a830d28b
JZ
743};
744module_platform_driver(pm860x_charger_driver);
745
746MODULE_DESCRIPTION("Marvell 88PM860x Charger driver");
747MODULE_LICENSE("GPL");