blob: ec62c83566929901f5664cb201a601036fd3b5dc [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/platform_device.h>
18#include <linux/errno.h>
19#include <linux/mfd/pm8xxx/pm8921-charger.h>
20#include <linux/mfd/pm8xxx/pm8921-bms.h>
21#include <linux/mfd/pm8921-adc.h>
22#include <linux/mfd/pm8xxx/core.h>
23#include <linux/interrupt.h>
24#include <linux/power_supply.h>
25#include <linux/delay.h>
26#include <linux/bitops.h>
27#include <linux/workqueue.h>
28#include <linux/debugfs.h>
29#include <linux/slab.h>
30
31#include <mach/msm_xo.h>
32#include <mach/msm_hsusb.h>
33
34#define CHG_BUCK_CLOCK_CTRL 0x14
35
36#define PBL_ACCESS1 0x04
37#define PBL_ACCESS2 0x05
38#define SYS_CONFIG_1 0x06
39#define SYS_CONFIG_2 0x07
40#define CHG_CNTRL 0x204
41#define CHG_IBAT_MAX 0x205
42#define CHG_TEST 0x206
43#define CHG_BUCK_CTRL_TEST1 0x207
44#define CHG_BUCK_CTRL_TEST2 0x208
45#define CHG_BUCK_CTRL_TEST3 0x209
46#define COMPARATOR_OVERRIDE 0x20A
47#define PSI_TXRX_SAMPLE_DATA_0 0x20B
48#define PSI_TXRX_SAMPLE_DATA_1 0x20C
49#define PSI_TXRX_SAMPLE_DATA_2 0x20D
50#define PSI_TXRX_SAMPLE_DATA_3 0x20E
51#define PSI_CONFIG_STATUS 0x20F
52#define CHG_IBAT_SAFE 0x210
53#define CHG_ITRICKLE 0x211
54#define CHG_CNTRL_2 0x212
55#define CHG_VBAT_DET 0x213
56#define CHG_VTRICKLE 0x214
57#define CHG_ITERM 0x215
58#define CHG_CNTRL_3 0x216
59#define CHG_VIN_MIN 0x217
60#define CHG_TWDOG 0x218
61#define CHG_TTRKL_MAX 0x219
62#define CHG_TEMP_THRESH 0x21A
63#define CHG_TCHG_MAX 0x21B
64#define USB_OVP_CONTROL 0x21C
65#define DC_OVP_CONTROL 0x21D
66#define USB_OVP_TEST 0x21E
67#define DC_OVP_TEST 0x21F
68#define CHG_VDD_MAX 0x220
69#define CHG_VDD_SAFE 0x221
70#define CHG_VBAT_BOOT_THRESH 0x222
71#define USB_OVP_TRIM 0x355
72#define BUCK_CONTROL_TRIM1 0x356
73#define BUCK_CONTROL_TRIM2 0x357
74#define BUCK_CONTROL_TRIM3 0x358
75#define BUCK_CONTROL_TRIM4 0x359
76#define CHG_DEFAULTS_TRIM 0x35A
77#define CHG_ITRIM 0x35B
78#define CHG_TTRIM 0x35C
79#define CHG_COMP_OVR 0x20A
80
81enum chg_fsm_state {
82 FSM_STATE_OFF_0 = 0,
83 FSM_STATE_BATFETDET_START_12 = 12,
84 FSM_STATE_BATFETDET_END_16 = 16,
85 FSM_STATE_ON_CHG_HIGHI_1 = 1,
86 FSM_STATE_ATC_2A = 2,
87 FSM_STATE_ATC_2B = 18,
88 FSM_STATE_ON_BAT_3 = 3,
89 FSM_STATE_ATC_FAIL_4 = 4 ,
90 FSM_STATE_DELAY_5 = 5,
91 FSM_STATE_ON_CHG_AND_BAT_6 = 6,
92 FSM_STATE_FAST_CHG_7 = 7,
93 FSM_STATE_TRKL_CHG_8 = 8,
94 FSM_STATE_CHG_FAIL_9 = 9,
95 FSM_STATE_EOC_10 = 10,
96 FSM_STATE_ON_CHG_VREGOK_11 = 11,
97 FSM_STATE_ATC_PAUSE_13 = 13,
98 FSM_STATE_FAST_CHG_PAUSE_14 = 14,
99 FSM_STATE_TRKL_CHG_PAUSE_15 = 15,
100 FSM_STATE_START_BOOT = 20,
101 FSM_STATE_FLCB_VREGOK = 21,
102 FSM_STATE_FLCB = 22,
103};
104
105enum pmic_chg_interrupts {
106 USBIN_VALID_IRQ = 0,
107 USBIN_OV_IRQ,
108 BATT_INSERTED_IRQ,
109 VBATDET_LOW_IRQ,
110 USBIN_UV_IRQ,
111 VBAT_OV_IRQ,
112 CHGWDOG_IRQ,
113 VCP_IRQ,
114 ATCDONE_IRQ,
115 ATCFAIL_IRQ,
116 CHGDONE_IRQ,
117 CHGFAIL_IRQ,
118 CHGSTATE_IRQ,
119 LOOP_CHANGE_IRQ,
120 FASTCHG_IRQ,
121 TRKLCHG_IRQ,
122 BATT_REMOVED_IRQ,
123 BATTTEMP_HOT_IRQ,
124 CHGHOT_IRQ,
125 BATTTEMP_COLD_IRQ,
126 CHG_GONE_IRQ,
127 BAT_TEMP_OK_IRQ,
128 COARSE_DET_LOW_IRQ,
129 VDD_LOOP_IRQ,
130 VREG_OV_IRQ,
131 VBATDET_IRQ,
132 BATFET_IRQ,
133 PSI_IRQ,
134 DCIN_VALID_IRQ,
135 DCIN_OV_IRQ,
136 DCIN_UV_IRQ,
137 PM_CHG_MAX_INTS,
138};
139
140struct bms_notify {
141 int is_charging;
142 struct work_struct work;
143};
144
145/**
146 * struct pm8921_chg_chip -device information
147 * @dev: device pointer to access the parent
148 * @is_usb_path_used: indicates whether USB charging is used at all
149 * @is_usb_path_used: indicates whether DC charging is used at all
150 * @usb_present: present status of usb
151 * @dc_present: present status of dc
152 * @usb_charger_current: usb current to charge the battery with used when
153 * the usb path is enabled or charging is resumed
154 * @safety_time: max time for which charging will happen
155 * @update_time: how frequently the userland needs to be updated
156 * @max_voltage: the max volts the batt should be charged up to
157 * @min_voltage: the min battery voltage before turning the FETon
158 * @resume_voltage: the voltage at which the battery should resume
159 * charging
160 * @term_current: The charging based term current
161 *
162 */
163struct pm8921_chg_chip {
164 struct device *dev;
165 unsigned int usb_present;
166 unsigned int dc_present;
167 unsigned int usb_charger_current;
168 unsigned int pmic_chg_irq[PM_CHG_MAX_INTS];
169 unsigned int safety_time;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -0700170 unsigned int ttrkl_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700171 unsigned int update_time;
172 unsigned int max_voltage;
173 unsigned int min_voltage;
174 unsigned int resume_voltage;
175 unsigned int term_current;
176 unsigned int vbat_channel;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -0700177 unsigned int batt_temp_channel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 struct power_supply usb_psy;
179 struct power_supply dc_psy;
180 struct power_supply batt_psy;
181 struct dentry *dent;
182 struct bms_notify bms_notify;
183 DECLARE_BITMAP(enabled_irqs, PM_CHG_MAX_INTS);
184};
185
186static int charging_disabled;
187
188static struct pm8921_chg_chip *the_chip;
189
190static int pm_chg_masked_write(struct pm8921_chg_chip *chip, u16 addr,
191 u8 mask, u8 val)
192{
193 int rc;
194 u8 reg;
195
196 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
197 if (rc) {
198 pr_err("pm8xxx_readb failed: addr=%03X, rc=%d\n", addr, rc);
199 return rc;
200 }
201 reg &= ~mask;
202 reg |= val & mask;
203 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
204 if (rc) {
205 pr_err("pm8xxx_writeb failed: addr=%03X, rc=%d\n", addr, rc);
206 return rc;
207 }
208 return 0;
209}
210
211#define CAPTURE_FSM_STATE_CMD 0xC2
212#define READ_BANK_7 0x70
213#define READ_BANK_4 0x40
214static int pm_chg_get_fsm_state(struct pm8921_chg_chip *chip)
215{
216 u8 temp;
217 int err, ret = 0;
218
219 temp = CAPTURE_FSM_STATE_CMD;
220 err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
221 if (err) {
222 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
223 return err;
224 }
225
226 temp = READ_BANK_7;
227 err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
228 if (err) {
229 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
230 return err;
231 }
232
233 err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp);
234 if (err) {
235 pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err);
236 return err;
237 }
238 /* get the lower 4 bits */
239 ret = temp & 0xF;
240
241 temp = READ_BANK_4;
242 err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
243 if (err) {
244 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
245 return err;
246 }
247
248 err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp);
249 if (err) {
250 pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err);
251 return err;
252 }
253 /* get the upper 1 bit */
254 ret |= (temp & 0x1) << 4;
255 return ret;
256}
257
258#define CHG_USB_SUSPEND_BIT BIT(2)
259static int pm_chg_usb_suspend_enable(struct pm8921_chg_chip *chip, int enable)
260{
261 return pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_USB_SUSPEND_BIT,
262 enable ? CHG_USB_SUSPEND_BIT : 0);
263}
264
265#define CHG_EN_BIT BIT(7)
266static int pm_chg_auto_enable(struct pm8921_chg_chip *chip, int enable)
267{
268 return pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_EN_BIT,
269 enable ? CHG_EN_BIT : 0);
270}
271
272#define CHG_CHARGE_DIS_BIT BIT(1)
273static int pm_chg_charge_dis(struct pm8921_chg_chip *chip, int disable)
274{
275 return pm_chg_masked_write(chip, CHG_CNTRL, CHG_CHARGE_DIS_BIT,
276 disable ? CHG_CHARGE_DIS_BIT : 0);
277}
278
279#define PM8921_CHG_V_MIN_MV 3240
280#define PM8921_CHG_V_STEP_MV 20
281#define PM8921_CHG_VDDMAX_MAX 4500
282#define PM8921_CHG_VDDMAX_MIN 3400
283#define PM8921_CHG_V_MASK 0x7F
284static int pm_chg_vddmax_set(struct pm8921_chg_chip *chip, int voltage)
285{
286 u8 temp;
287
288 if (voltage < PM8921_CHG_VDDMAX_MIN
289 || voltage > PM8921_CHG_VDDMAX_MAX) {
290 pr_err("bad mV=%d asked to set\n", voltage);
291 return -EINVAL;
292 }
293 temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV;
294 pr_debug("voltage=%d setting %02x\n", voltage, temp);
295 return pm_chg_masked_write(chip, CHG_VDD_MAX, PM8921_CHG_V_MASK, temp);
296}
297
298#define PM8921_CHG_VDDSAFE_MIN 3400
299#define PM8921_CHG_VDDSAFE_MAX 4500
300static int pm_chg_vddsafe_set(struct pm8921_chg_chip *chip, int voltage)
301{
302 u8 temp;
303
304 if (voltage < PM8921_CHG_VDDSAFE_MIN
305 || voltage > PM8921_CHG_VDDSAFE_MAX) {
306 pr_err("bad mV=%d asked to set\n", voltage);
307 return -EINVAL;
308 }
309 temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV;
310 pr_debug("voltage=%d setting %02x\n", voltage, temp);
311 return pm_chg_masked_write(chip, CHG_VDD_SAFE, PM8921_CHG_V_MASK, temp);
312}
313
314#define PM8921_CHG_VBATDET_MIN 3240
315#define PM8921_CHG_VBATDET_MAX 5780
316static int pm_chg_vbatdet_set(struct pm8921_chg_chip *chip, int voltage)
317{
318 u8 temp;
319
320 if (voltage < PM8921_CHG_VBATDET_MIN
321 || voltage > PM8921_CHG_VBATDET_MAX) {
322 pr_err("bad mV=%d asked to set\n", voltage);
323 return -EINVAL;
324 }
325 temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV;
326 pr_debug("voltage=%d setting %02x\n", voltage, temp);
327 return pm_chg_masked_write(chip, CHG_VBAT_DET, PM8921_CHG_V_MASK, temp);
328}
329
330#define PM8921_CHG_IBATMAX_MIN 325
331#define PM8921_CHG_IBATMAX_MAX 2000
332#define PM8921_CHG_I_MIN_MA 225
333#define PM8921_CHG_I_STEP_MA 50
334#define PM8921_CHG_I_MASK 0x3F
335static int pm_chg_ibatmax_set(struct pm8921_chg_chip *chip, int chg_current)
336{
337 u8 temp;
338
339 if (chg_current < PM8921_CHG_IBATMAX_MIN
340 || chg_current > PM8921_CHG_IBATMAX_MAX) {
341 pr_err("bad mA=%d asked to set\n", chg_current);
342 return -EINVAL;
343 }
344 temp = (chg_current - PM8921_CHG_I_MIN_MA) / PM8921_CHG_I_STEP_MA;
345 return pm_chg_masked_write(chip, CHG_IBAT_MAX, PM8921_CHG_I_MASK, temp);
346}
347
348#define PM8921_CHG_IBATSAFE_MIN 225
349#define PM8921_CHG_IBATSAFE_MAX 3375
350static int pm_chg_ibatsafe_set(struct pm8921_chg_chip *chip, int chg_current)
351{
352 u8 temp;
353
354 if (chg_current < PM8921_CHG_IBATSAFE_MIN
355 || chg_current > PM8921_CHG_IBATSAFE_MAX) {
356 pr_err("bad mA=%d asked to set\n", chg_current);
357 return -EINVAL;
358 }
359 temp = (chg_current - PM8921_CHG_I_MIN_MA) / PM8921_CHG_I_STEP_MA;
360 return pm_chg_masked_write(chip, CHG_IBAT_SAFE,
361 PM8921_CHG_I_MASK, temp);
362}
363
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364#define PM8921_CHG_ITERM_MIN_MA 50
Abhijeet Dharmapurikar71816fd2011-08-12 17:14:10 -0700365#define PM8921_CHG_ITERM_MAX_MA 200
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700366#define PM8921_CHG_ITERM_STEP_MA 10
367#define PM8921_CHG_ITERM_MASK 0xF
368static int pm_chg_iterm_set(struct pm8921_chg_chip *chip, int chg_current)
369{
370 u8 temp;
371
Abhijeet Dharmapurikar71816fd2011-08-12 17:14:10 -0700372 if (chg_current < PM8921_CHG_ITERM_MIN_MA
373 || chg_current > PM8921_CHG_ITERM_MAX_MA) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700374 pr_err("bad mA=%d asked to set\n", chg_current);
375 return -EINVAL;
376 }
377
378 temp = (chg_current - PM8921_CHG_ITERM_MIN_MA)
379 / PM8921_CHG_ITERM_STEP_MA;
Abhijeet Dharmapurikar71816fd2011-08-12 17:14:10 -0700380 return pm_chg_masked_write(chip, CHG_ITERM, PM8921_CHG_ITERM_MASK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 temp);
382}
383
384#define PM8921_CHG_IUSB_MASK 0x1C
385#define PM8921_CHG_IUSB_MAX 7
386#define PM8921_CHG_IUSB_MIN 0
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -0700387static int pm_chg_iusbmax_set(struct pm8921_chg_chip *chip, int reg_val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388{
389 u8 temp;
390
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -0700391 if (reg_val < PM8921_CHG_IUSB_MIN || reg_val > PM8921_CHG_IUSB_MAX) {
392 pr_err("bad mA=%d asked to set\n", reg_val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393 return -EINVAL;
394 }
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -0700395 temp = reg_val << 2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396 return pm_chg_masked_write(chip, PBL_ACCESS2, PM8921_CHG_IUSB_MASK,
397 temp);
398}
399
400#define PM8921_CHG_WD_MASK 0x1F
401static int pm_chg_disable_wd(struct pm8921_chg_chip *chip)
402{
403 /* writing 0 to the wd timer disables it */
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -0700404 return pm_chg_masked_write(chip, CHG_TWDOG, PM8921_CHG_WD_MASK, 0);
405}
406
407#define PM8921_CHG_TCHG_MASK 0x3F
408#define PM8921_CHG_TCHG_MIN 4
409#define PM8921_CHG_TCHG_MAX 512
410#define PM8921_CHG_TCHG_STEP 4
411static int pm_chg_tchg_max_set(struct pm8921_chg_chip *chip, int minutes)
412{
413 u8 temp;
414
415 if (minutes < PM8921_CHG_TCHG_MIN || minutes > PM8921_CHG_TCHG_MAX) {
416 pr_err("bad max minutes =%d asked to set\n", minutes);
417 return -EINVAL;
418 }
419
420 temp = (minutes - 1)/PM8921_CHG_TCHG_STEP;
421 return pm_chg_masked_write(chip, CHG_TCHG_MAX, PM8921_CHG_TCHG_MASK,
422 temp);
423}
424
425#define PM8921_CHG_TTRKL_MASK 0x1F
426#define PM8921_CHG_TTRKL_MIN 1
427#define PM8921_CHG_TTRKL_MAX 64
428static int pm_chg_ttrkl_max_set(struct pm8921_chg_chip *chip, int minutes)
429{
430 u8 temp;
431
432 if (minutes < PM8921_CHG_TTRKL_MIN || minutes > PM8921_CHG_TTRKL_MAX) {
433 pr_err("bad max minutes =%d asked to set\n", minutes);
434 return -EINVAL;
435 }
436
437 temp = minutes - 1;
438 return pm_chg_masked_write(chip, CHG_TTRKL_MAX, PM8921_CHG_TTRKL_MASK,
439 temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440}
441
442static void pm8921_chg_enable_irq(struct pm8921_chg_chip *chip, int interrupt)
443{
444 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
445 dev_dbg(chip->dev, "%d\n", chip->pmic_chg_irq[interrupt]);
446 enable_irq(chip->pmic_chg_irq[interrupt]);
447 }
448}
449
450static void pm8921_chg_disable_irq(struct pm8921_chg_chip *chip, int interrupt)
451{
452 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
453 dev_dbg(chip->dev, "%d\n", chip->pmic_chg_irq[interrupt]);
454 disable_irq_nosync(chip->pmic_chg_irq[interrupt]);
455 }
456}
457
458static int pm_chg_get_rt_status(struct pm8921_chg_chip *chip, int irq_id)
459{
460 return pm8xxx_read_irq_stat(chip->dev->parent,
461 chip->pmic_chg_irq[irq_id]);
462}
463
464/* Treat OverVoltage/UnderVoltage as source missing */
465static int is_usb_chg_plugged_in(struct pm8921_chg_chip *chip)
466{
467 int pres, ov, uv;
468
469 pres = pm_chg_get_rt_status(chip, USBIN_VALID_IRQ);
470 ov = pm_chg_get_rt_status(chip, USBIN_OV_IRQ);
471 uv = pm_chg_get_rt_status(chip, USBIN_UV_IRQ);
472
473 return pres && !ov && !uv;
474}
475
476/* Treat OverVoltage/UnderVoltage as source missing */
477static int is_dc_chg_plugged_in(struct pm8921_chg_chip *chip)
478{
479 int pres, ov, uv;
480
481 pres = pm_chg_get_rt_status(chip, DCIN_VALID_IRQ);
482 ov = pm_chg_get_rt_status(chip, DCIN_OV_IRQ);
483 uv = pm_chg_get_rt_status(chip, DCIN_UV_IRQ);
484
485 return pres && !ov && !uv;
486}
487
488static int is_battery_charging(int fsm_state)
489{
490 switch (fsm_state) {
491 case FSM_STATE_ATC_2A:
492 case FSM_STATE_ATC_2B:
493 case FSM_STATE_ON_CHG_AND_BAT_6:
494 case FSM_STATE_FAST_CHG_7:
495 case FSM_STATE_TRKL_CHG_8:
496 return 1;
497 }
498 return 0;
499}
500
501static void bms_notify(struct work_struct *work)
502{
503 struct bms_notify *n = container_of(work, struct bms_notify, work);
504
505 if (n->is_charging)
506 pm8921_bms_charging_began();
507 else
508 pm8921_bms_charging_end();
509}
510
511static enum power_supply_property pm_power_props[] = {
512 POWER_SUPPLY_PROP_PRESENT,
Willie Ruan9ad6d832011-08-19 11:58:42 -0700513 POWER_SUPPLY_PROP_ONLINE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514};
515
516static char *pm_power_supplied_to[] = {
517 "battery",
518};
519
520static int pm_power_get_property(struct power_supply *psy,
521 enum power_supply_property psp,
522 union power_supply_propval *val)
523{
524 struct pm8921_chg_chip *chip;
525
526 switch (psp) {
527 case POWER_SUPPLY_PROP_PRESENT:
Willie Ruan9ad6d832011-08-19 11:58:42 -0700528 case POWER_SUPPLY_PROP_ONLINE:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529 if (psy->type == POWER_SUPPLY_TYPE_MAINS) {
530 chip = container_of(psy, struct pm8921_chg_chip,
531 dc_psy);
532 val->intval = is_dc_chg_plugged_in(chip);
533 }
534 if (psy->type == POWER_SUPPLY_TYPE_USB) {
535 chip = container_of(psy, struct pm8921_chg_chip,
536 usb_psy);
537 val->intval = is_usb_chg_plugged_in(chip);
538 }
539 break;
540 default:
541 return -EINVAL;
542 }
543 return 0;
544}
545
546static enum power_supply_property msm_batt_power_props[] = {
547 POWER_SUPPLY_PROP_STATUS,
548 POWER_SUPPLY_PROP_CHARGE_TYPE,
549 POWER_SUPPLY_PROP_HEALTH,
550 POWER_SUPPLY_PROP_PRESENT,
551 POWER_SUPPLY_PROP_TECHNOLOGY,
552 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
553 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
554 POWER_SUPPLY_PROP_VOLTAGE_NOW,
555 POWER_SUPPLY_PROP_CAPACITY,
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -0700556 POWER_SUPPLY_PROP_CURRENT_NOW,
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -0700557 POWER_SUPPLY_PROP_TEMP,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558};
559
560static int get_prop_battery_mvolts(struct pm8921_chg_chip *chip)
561{
562 int rc;
563 struct pm8921_adc_chan_result result;
564
565 rc = pm8921_adc_read(chip->vbat_channel, &result);
566 if (rc) {
567 pr_err("error reading adc channel = %d, rc = %d\n",
568 chip->vbat_channel, rc);
569 return rc;
570 }
571 pr_debug("mvolts phy = %lld meas = 0x%llx", result.physical,
572 result.measurement);
573 return (int)result.physical;
574}
575
576static int get_prop_batt_capacity(struct pm8921_chg_chip *chip)
577{
Abhijeet Dharmapurikar5a7df4e2011-07-27 13:16:24 -0700578 int percent_soc = pm8921_bms_get_percent_charge();
579
580 if (percent_soc <= 10)
581 pr_warn("low battery charge = %d%%\n", percent_soc);
582
583 return percent_soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584}
585
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -0700586static int get_prop_batt_current(struct pm8921_chg_chip *chip)
587{
588 int result_ma, rc;
589
590 rc = pm8921_bms_get_battery_current(&result_ma);
591 if (rc) {
592 pr_err("unable to get batt current rc = %d\n", rc);
593 return rc;
594 } else {
595 return result_ma;
596 }
597}
598
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599static int get_prop_batt_health(struct pm8921_chg_chip *chip)
600{
601 int temp;
602
603 temp = pm_chg_get_rt_status(chip, BATTTEMP_HOT_IRQ);
604 if (temp)
605 return POWER_SUPPLY_HEALTH_OVERHEAT;
606
607 temp = pm_chg_get_rt_status(chip, BATTTEMP_COLD_IRQ);
608 if (temp)
609 return POWER_SUPPLY_HEALTH_COLD;
610
611 return POWER_SUPPLY_HEALTH_GOOD;
612}
613
614static int get_prop_batt_present(struct pm8921_chg_chip *chip)
615{
616 return pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ);
617}
618
619static int get_prop_charge_type(struct pm8921_chg_chip *chip)
620{
621 int temp;
622
623 temp = pm_chg_get_rt_status(chip, TRKLCHG_IRQ);
624 if (temp)
625 return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
626
627 temp = pm_chg_get_rt_status(chip, FASTCHG_IRQ);
628 if (temp)
629 return POWER_SUPPLY_CHARGE_TYPE_FAST;
630
631 return POWER_SUPPLY_CHARGE_TYPE_NONE;
632}
633
634static int get_prop_batt_status(struct pm8921_chg_chip *chip)
635{
636 int temp = 0;
637
638 /* TODO reading the FSM state is more reliable */
639 temp = pm_chg_get_rt_status(chip, TRKLCHG_IRQ);
640
641 temp |= pm_chg_get_rt_status(chip, FASTCHG_IRQ);
642 if (temp)
643 return POWER_SUPPLY_STATUS_CHARGING;
644 /*
645 * The battery is not charging
646 * check the FET - if on battery is discharging
647 * - if off battery is isolated(full) and the system
648 * is being driven from a charger
649 */
650 temp = pm_chg_get_rt_status(chip, BATFET_IRQ);
651 if (temp)
652 return POWER_SUPPLY_STATUS_DISCHARGING;
653
654 return POWER_SUPPLY_STATUS_FULL;
655}
656
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -0700657static int get_prop_batt_temp(struct pm8921_chg_chip *chip)
658{
659 int rc;
660 struct pm8921_adc_chan_result result;
661
662 rc = pm8921_adc_read(chip->batt_temp_channel, &result);
663 if (rc) {
664 pr_err("error reading adc channel = %d, rc = %d\n",
665 chip->vbat_channel, rc);
666 return rc;
667 }
668 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
669 result.measurement);
670 return (int)result.physical;
671}
672
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700673static int pm_batt_power_get_property(struct power_supply *psy,
674 enum power_supply_property psp,
675 union power_supply_propval *val)
676{
677 struct pm8921_chg_chip *chip = container_of(psy, struct pm8921_chg_chip,
678 batt_psy);
679
680 switch (psp) {
681 case POWER_SUPPLY_PROP_STATUS:
682 val->intval = get_prop_batt_status(chip);
683 break;
684 case POWER_SUPPLY_PROP_CHARGE_TYPE:
685 val->intval = get_prop_charge_type(chip);
686 break;
687 case POWER_SUPPLY_PROP_HEALTH:
688 val->intval = get_prop_batt_health(chip);
689 break;
690 case POWER_SUPPLY_PROP_PRESENT:
691 val->intval = get_prop_batt_present(chip);
692 break;
693 case POWER_SUPPLY_PROP_TECHNOLOGY:
694 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
695 break;
696 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
697 val->intval = chip->max_voltage;
698 break;
699 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
700 val->intval = chip->min_voltage;
701 break;
702 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
703 val->intval = get_prop_battery_mvolts(chip);
704 break;
705 case POWER_SUPPLY_PROP_CAPACITY:
706 val->intval = get_prop_batt_capacity(chip);
707 break;
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -0700708 case POWER_SUPPLY_PROP_CURRENT_NOW:
709 val->intval = get_prop_batt_current(chip);
710 break;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -0700711 case POWER_SUPPLY_PROP_TEMP:
712 val->intval = get_prop_batt_temp(chip);
713 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714 default:
715 return -EINVAL;
716 }
717
718 return 0;
719}
720
721static void (*notify_vbus_state_func_ptr)(int);
722static int usb_chg_current;
723static DEFINE_SPINLOCK(vbus_lock);
724
725int pm8921_charger_register_vbus_sn(void (*callback)(int))
726{
727 pr_debug("%p\n", callback);
728 notify_vbus_state_func_ptr = callback;
729 return 0;
730}
731EXPORT_SYMBOL_GPL(pm8921_charger_register_vbus_sn);
732
733/* this is passed to the hsusb via platform_data msm_otg_pdata */
734void pm8921_charger_unregister_vbus_sn(void (*callback)(int))
735{
736 pr_debug("%p\n", callback);
737 notify_vbus_state_func_ptr = NULL;
738}
739EXPORT_SYMBOL_GPL(pm8921_charger_unregister_vbus_sn);
740
741static void notify_usb_of_the_plugin_event(int plugin)
742{
743 plugin = !!plugin;
744 if (notify_vbus_state_func_ptr) {
745 pr_debug("notifying plugin\n");
746 (*notify_vbus_state_func_ptr) (plugin);
747 } else {
748 pr_debug("unable to notify plugin\n");
749 }
750}
751
752struct usb_ma_limit_entry {
753 int usb_ma;
754 u8 chg_iusb_value;
755};
756
757static struct usb_ma_limit_entry usb_ma_table[] = {
758 {100, 0},
759 {500, 1},
760 {700, 2},
761 {850, 3},
762 {900, 4},
763 {1100, 5},
764 {1300, 6},
765 {1500, 7},
766};
767
768/* assumes vbus_lock is held */
769static void __pm8921_charger_vbus_draw(unsigned int mA)
770{
771 int i, rc;
772
773 if (mA > 0 && mA <= 2) {
774 usb_chg_current = 0;
775 rc = pm_chg_iusbmax_set(the_chip,
776 usb_ma_table[0].chg_iusb_value);
777 if (rc) {
778 pr_err("unable to set iusb to %d rc = %d\n",
779 usb_ma_table[0].chg_iusb_value, rc);
780 }
781 rc = pm_chg_usb_suspend_enable(the_chip, 1);
782 if (rc)
783 pr_err("fail to set suspend bit rc=%d\n", rc);
784 } else {
785 rc = pm_chg_usb_suspend_enable(the_chip, 0);
786 if (rc)
787 pr_err("fail to reset suspend bit rc=%d\n", rc);
788 for (i = ARRAY_SIZE(usb_ma_table) - 1; i >= 0; i--) {
789 if (usb_ma_table[i].usb_ma <= mA)
790 break;
791 }
792 if (i < 0)
793 i = 0;
794 rc = pm_chg_iusbmax_set(the_chip,
795 usb_ma_table[i].chg_iusb_value);
796 if (rc) {
797 pr_err("unable to set iusb to %d rc = %d\n",
798 usb_ma_table[i].chg_iusb_value, rc);
799 }
800 }
801}
802
803/* USB calls these to tell us how much max usb current the system can draw */
804void pm8921_charger_vbus_draw(unsigned int mA)
805{
806 unsigned long flags;
807
808 pr_debug("Enter charge=%d\n", mA);
809 spin_lock_irqsave(&vbus_lock, flags);
810 if (the_chip) {
811 __pm8921_charger_vbus_draw(mA);
812 } else {
813 /*
814 * called before pmic initialized,
815 * save this value and use it at probe
816 */
817 usb_chg_current = mA;
818 }
819 spin_unlock_irqrestore(&vbus_lock, flags);
820}
821EXPORT_SYMBOL_GPL(pm8921_charger_vbus_draw);
822
Abhijeet Dharmapurikar1f52c412011-08-12 17:11:45 -0700823int pm8921_charger_enable(bool enable)
824{
825 int rc;
826
827 if (!the_chip) {
828 pr_err("called before init\n");
829 return -EINVAL;
830 }
831 enable = !!enable;
832 rc = pm_chg_auto_enable(the_chip, enable);
833 if (rc)
834 pr_err("Failed rc=%d\n", rc);
835 return rc;
836}
837EXPORT_SYMBOL(pm8921_charger_enable);
838
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -0700839int pm8921_is_usb_chg_plugged_in(void)
840{
841 if (!the_chip) {
842 pr_err("called before init\n");
843 return -EINVAL;
844 }
845 return is_usb_chg_plugged_in(the_chip);
846}
847EXPORT_SYMBOL(pm8921_is_usb_chg_plugged_in);
848
849int pm8921_is_dc_chg_plugged_in(void)
850{
851 if (!the_chip) {
852 pr_err("called before init\n");
853 return -EINVAL;
854 }
855 return is_dc_chg_plugged_in(the_chip);
856}
857EXPORT_SYMBOL(pm8921_is_dc_chg_plugged_in);
858
859int pm8921_is_battery_present(void)
860{
861 if (!the_chip) {
862 pr_err("called before init\n");
863 return -EINVAL;
864 }
865 return get_prop_batt_present(the_chip);
866}
867EXPORT_SYMBOL(pm8921_is_battery_present);
868
869int pm8921_set_max_battery_charge_current(int ma)
870{
871 if (!the_chip) {
872 pr_err("called before init\n");
873 return -EINVAL;
874 }
875 return pm_chg_ibatmax_set(the_chip, ma);
876}
877EXPORT_SYMBOL(pm8921_set_max_battery_charge_current);
878
879int pm8921_disable_source_current(bool disable)
880{
881 if (!the_chip) {
882 pr_err("called before init\n");
883 return -EINVAL;
884 }
885 if (disable)
886 pr_warn("current drawn from chg=0, battery provides current\n");
887 return pm_chg_charge_dis(the_chip, disable);
888}
889EXPORT_SYMBOL(pm8921_disable_source_current);
890
891bool pm8921_is_battery_charging(int *source)
892{
893 int fsm_state, is_charging, dc_present, usb_present;
894
895 if (!the_chip) {
896 pr_err("called before init\n");
897 return -EINVAL;
898 }
899 fsm_state = pm_chg_get_fsm_state(the_chip);
900 is_charging = is_battery_charging(fsm_state);
901 if (is_charging == 0) {
902 *source = PM8921_CHG_SRC_NONE;
903 return is_charging;
904 }
905
906 if (source == NULL)
907 return is_charging;
908
909 /* the battery is charging, the source is requested, find it */
910 dc_present = is_dc_chg_plugged_in(the_chip);
911 usb_present = is_usb_chg_plugged_in(the_chip);
912
913 if (dc_present && !usb_present)
914 *source = PM8921_CHG_SRC_DC;
915
916 if (usb_present && !dc_present)
917 *source = PM8921_CHG_SRC_USB;
918
919 if (usb_present && dc_present)
920 /*
921 * The system always chooses dc for charging since it has
922 * higher priority.
923 */
924 *source = PM8921_CHG_SRC_DC;
925
926 return is_charging;
927}
928EXPORT_SYMBOL(pm8921_is_battery_charging);
929
930int pm8921_batt_temperature(void)
931{
932 if (!the_chip) {
933 pr_err("called before init\n");
934 return -EINVAL;
935 }
936 return get_prop_batt_temp(the_chip);
937}
938
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939static void handle_usb_insertion_removal(struct pm8921_chg_chip *chip)
940{
941 int usb_present;
942
943 usb_present = is_usb_chg_plugged_in(chip);
944 if (chip->usb_present ^ usb_present) {
945 notify_usb_of_the_plugin_event(usb_present);
946 chip->usb_present = usb_present;
947 power_supply_changed(&chip->usb_psy);
948 }
949}
950
951static void handle_dc_removal_insertion(struct pm8921_chg_chip *chip)
952{
953 int dc_present;
954
955 dc_present = is_dc_chg_plugged_in(chip);
956 if (chip->dc_present ^ dc_present) {
957 chip->dc_present = dc_present;
958 power_supply_changed(&chip->dc_psy);
959 }
960}
961
962static irqreturn_t usbin_valid_irq_handler(int irq, void *data)
963{
964 handle_usb_insertion_removal(data);
965 return IRQ_HANDLED;
966}
967
968static irqreturn_t usbin_ov_irq_handler(int irq, void *data)
969{
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -0700970 pr_err("USB OverVoltage\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971 handle_usb_insertion_removal(data);
972 return IRQ_HANDLED;
973}
974
975static irqreturn_t batt_inserted_irq_handler(int irq, void *data)
976{
977 struct pm8921_chg_chip *chip = data;
978 int status;
979
980 status = pm_chg_get_rt_status(chip,
981 BATT_INSERTED_IRQ);
982 pr_debug("battery present=%d", status);
983 power_supply_changed(&chip->batt_psy);
984 return IRQ_HANDLED;
985}
986/* this interrupt used to restart charging a battery */
987static irqreturn_t vbatdet_low_irq_handler(int irq, void *data)
988{
989 struct pm8921_chg_chip *chip = data;
990
991 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
992 power_supply_changed(&chip->batt_psy);
993 power_supply_changed(&chip->usb_psy);
994 power_supply_changed(&chip->dc_psy);
995 return IRQ_HANDLED;
996}
997
998static irqreturn_t usbin_uv_irq_handler(int irq, void *data)
999{
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -07001000 pr_err("USB UnderVoltage\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001001 handle_usb_insertion_removal(data);
1002 return IRQ_HANDLED;
1003}
1004
1005static irqreturn_t vbat_ov_irq_handler(int irq, void *data)
1006{
1007 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1008 return IRQ_HANDLED;
1009}
1010
1011static irqreturn_t chgwdog_irq_handler(int irq, void *data)
1012{
1013 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1014 return IRQ_HANDLED;
1015}
1016
1017static irqreturn_t vcp_irq_handler(int irq, void *data)
1018{
1019 pr_warning("VCP triggered BATDET forced on\n");
1020 pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
1021 return IRQ_HANDLED;
1022}
1023
1024static irqreturn_t atcdone_irq_handler(int irq, void *data)
1025{
1026 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1027 return IRQ_HANDLED;
1028}
1029
1030static irqreturn_t atcfail_irq_handler(int irq, void *data)
1031{
1032 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1033 return IRQ_HANDLED;
1034}
1035
1036static irqreturn_t chgdone_irq_handler(int irq, void *data)
1037{
1038 struct pm8921_chg_chip *chip = data;
1039
1040 pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
1041 power_supply_changed(&chip->batt_psy);
1042 power_supply_changed(&chip->usb_psy);
1043 power_supply_changed(&chip->dc_psy);
1044 return IRQ_HANDLED;
1045}
1046
1047static irqreturn_t chgfail_irq_handler(int irq, void *data)
1048{
1049 struct pm8921_chg_chip *chip = data;
1050
1051 pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
1052 power_supply_changed(&chip->batt_psy);
1053 power_supply_changed(&chip->usb_psy);
1054 power_supply_changed(&chip->dc_psy);
1055 return IRQ_HANDLED;
1056}
1057
1058static irqreturn_t chgstate_irq_handler(int irq, void *data)
1059{
1060 struct pm8921_chg_chip *chip = data;
1061 int new_is_charging = 0, fsm_state;
1062
1063 pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
1064 power_supply_changed(&chip->batt_psy);
1065 power_supply_changed(&chip->usb_psy);
1066 power_supply_changed(&chip->dc_psy);
1067
1068 fsm_state = pm_chg_get_fsm_state(chip);
1069 new_is_charging = is_battery_charging(fsm_state);
1070
1071 if (chip->bms_notify.is_charging ^ new_is_charging) {
1072 chip->bms_notify.is_charging = new_is_charging;
1073 schedule_work(&(chip->bms_notify.work));
1074 }
1075 return IRQ_HANDLED;
1076}
1077
1078static irqreturn_t loop_change_irq_handler(int irq, void *data)
1079{
1080 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1081 return IRQ_HANDLED;
1082}
1083
1084static irqreturn_t fastchg_irq_handler(int irq, void *data)
1085{
1086 struct pm8921_chg_chip *chip = data;
1087
1088 power_supply_changed(&chip->batt_psy);
1089 return IRQ_HANDLED;
1090}
1091
1092static irqreturn_t trklchg_irq_handler(int irq, void *data)
1093{
1094 struct pm8921_chg_chip *chip = data;
1095
1096 power_supply_changed(&chip->batt_psy);
1097 return IRQ_HANDLED;
1098}
1099
1100static irqreturn_t batt_removed_irq_handler(int irq, void *data)
1101{
1102 struct pm8921_chg_chip *chip = data;
1103 int status;
1104
1105 status = pm_chg_get_rt_status(chip, BATT_REMOVED_IRQ);
1106 pr_debug("battery present=%d state=%d", !status,
1107 pm_chg_get_fsm_state(data));
1108 power_supply_changed(&chip->batt_psy);
1109 return IRQ_HANDLED;
1110}
1111
1112static irqreturn_t batttemp_hot_irq_handler(int irq, void *data)
1113{
1114 struct pm8921_chg_chip *chip = data;
1115
1116 power_supply_changed(&chip->batt_psy);
1117 return IRQ_HANDLED;
1118}
1119
1120static irqreturn_t chghot_irq_handler(int irq, void *data)
1121{
1122 struct pm8921_chg_chip *chip = data;
1123
1124 pr_debug("Chg hot fsm_state=%d\n", pm_chg_get_fsm_state(data));
1125 power_supply_changed(&chip->batt_psy);
1126 power_supply_changed(&chip->usb_psy);
1127 power_supply_changed(&chip->dc_psy);
1128 return IRQ_HANDLED;
1129}
1130
1131static irqreturn_t batttemp_cold_irq_handler(int irq, void *data)
1132{
1133 struct pm8921_chg_chip *chip = data;
1134
1135 pr_debug("Batt cold fsm_state=%d\n", pm_chg_get_fsm_state(data));
1136 power_supply_changed(&chip->batt_psy);
1137 power_supply_changed(&chip->usb_psy);
1138 power_supply_changed(&chip->dc_psy);
1139 return IRQ_HANDLED;
1140}
1141
1142static irqreturn_t chg_gone_irq_handler(int irq, void *data)
1143{
1144 struct pm8921_chg_chip *chip = data;
1145
1146 pr_debug("Chg gone fsm_state=%d\n", pm_chg_get_fsm_state(data));
1147 power_supply_changed(&chip->batt_psy);
1148 power_supply_changed(&chip->usb_psy);
1149 power_supply_changed(&chip->dc_psy);
1150 return IRQ_HANDLED;
1151}
1152
1153static irqreturn_t bat_temp_ok_irq_handler(int irq, void *data)
1154{
1155 struct pm8921_chg_chip *chip = data;
1156
1157 pr_debug("batt temp ok fsm_state=%d\n", pm_chg_get_fsm_state(data));
1158 power_supply_changed(&chip->batt_psy);
1159 power_supply_changed(&chip->usb_psy);
1160 power_supply_changed(&chip->dc_psy);
1161 return IRQ_HANDLED;
1162}
1163
1164static irqreturn_t coarse_det_low_irq_handler(int irq, void *data)
1165{
1166 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1167 return IRQ_HANDLED;
1168}
1169
1170static irqreturn_t vdd_loop_irq_handler(int irq, void *data)
1171{
1172 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1173 return IRQ_HANDLED;
1174}
1175
1176static irqreturn_t vreg_ov_irq_handler(int irq, void *data)
1177{
1178 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1179 return IRQ_HANDLED;
1180}
1181
1182static irqreturn_t vbatdet_irq_handler(int irq, void *data)
1183{
1184 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1185 return IRQ_HANDLED;
1186}
1187
1188static irqreturn_t batfet_irq_handler(int irq, void *data)
1189{
1190 struct pm8921_chg_chip *chip = data;
1191
1192 pr_debug("vreg ov\n");
1193 power_supply_changed(&chip->batt_psy);
1194 return IRQ_HANDLED;
1195}
1196
1197static irqreturn_t dcin_valid_irq_handler(int irq, void *data)
1198{
1199 handle_dc_removal_insertion(data);
1200 return IRQ_HANDLED;
1201}
1202
1203static irqreturn_t dcin_ov_irq_handler(int irq, void *data)
1204{
1205 handle_dc_removal_insertion(data);
1206 return IRQ_HANDLED;
1207}
1208
1209static irqreturn_t dcin_uv_irq_handler(int irq, void *data)
1210{
1211 handle_dc_removal_insertion(data);
1212 return IRQ_HANDLED;
1213}
1214
Abhijeet Dharmapurikar1f52c412011-08-12 17:11:45 -07001215/**
1216 * set_disable_status_param -
1217 *
1218 * Internal function to disable battery charging and also disable drawing
1219 * any current from the source. The device is forced to run on a battery
1220 * after this.
1221 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001222static int set_disable_status_param(const char *val, struct kernel_param *kp)
1223{
1224 int ret;
1225 struct pm8921_chg_chip *chip = the_chip;
1226
1227 ret = param_set_int(val, kp);
1228 if (ret) {
1229 pr_err("error setting value %d\n", ret);
1230 return ret;
1231 }
1232 pr_info("factory set disable param to %d\n", charging_disabled);
1233 if (chip) {
1234 pm_chg_auto_enable(chip, !charging_disabled);
1235 pm_chg_charge_dis(chip, charging_disabled);
1236 }
1237 return 0;
1238}
1239module_param_call(disabled, set_disable_status_param, param_get_uint,
1240 &charging_disabled, 0644);
1241
1242static void free_irqs(struct pm8921_chg_chip *chip)
1243{
1244 int i;
1245
1246 for (i = 0; i < PM_CHG_MAX_INTS; i++)
1247 if (chip->pmic_chg_irq[i]) {
1248 free_irq(chip->pmic_chg_irq[i], chip);
1249 chip->pmic_chg_irq[i] = 0;
1250 }
1251}
1252
1253/* determines the initial present states and notifies msm_charger */
1254static void __devinit determine_initial_state(struct pm8921_chg_chip *chip)
1255{
1256 unsigned long flags;
1257 int fsm_state;
1258
1259 chip->dc_present = !!is_dc_chg_plugged_in(chip);
1260 chip->usb_present = !!is_usb_chg_plugged_in(chip);
1261
1262 notify_usb_of_the_plugin_event(chip->usb_present);
1263
1264 pm8921_chg_enable_irq(chip, DCIN_VALID_IRQ);
1265 pm8921_chg_enable_irq(chip, USBIN_VALID_IRQ);
1266 pm8921_chg_enable_irq(chip, BATT_REMOVED_IRQ);
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001267 pm8921_chg_enable_irq(chip, BATT_INSERTED_IRQ);
1268 pm8921_chg_enable_irq(chip, USBIN_OV_IRQ);
1269 pm8921_chg_enable_irq(chip, USBIN_UV_IRQ);
1270 pm8921_chg_enable_irq(chip, DCIN_OV_IRQ);
1271 pm8921_chg_enable_irq(chip, DCIN_UV_IRQ);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001272 pm8921_chg_enable_irq(chip, CHGSTATE_IRQ);
1273
1274 spin_lock_irqsave(&vbus_lock, flags);
1275 if (usb_chg_current) {
1276 /* reissue a vbus draw call */
1277 __pm8921_charger_vbus_draw(usb_chg_current);
1278 }
1279 spin_unlock_irqrestore(&vbus_lock, flags);
1280
1281 fsm_state = pm_chg_get_fsm_state(chip);
1282 if (is_battery_charging(fsm_state)) {
1283 chip->bms_notify.is_charging = 1;
1284 pm8921_bms_charging_began();
1285 }
1286
1287 pr_debug("usb = %d, dc = %d batt = %d state=%d\n",
1288 chip->usb_present,
1289 chip->dc_present,
1290 get_prop_batt_present(chip),
1291 fsm_state);
1292}
1293
1294struct pm_chg_irq_init_data {
1295 unsigned int irq_id;
1296 char *name;
1297 unsigned long flags;
1298 irqreturn_t (*handler)(int, void *);
1299};
1300
1301#define CHG_IRQ(_id, _flags, _handler) \
1302{ \
1303 .irq_id = _id, \
1304 .name = #_id, \
1305 .flags = _flags, \
1306 .handler = _handler, \
1307}
1308struct pm_chg_irq_init_data chg_irq_data[] = {
1309 CHG_IRQ(USBIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1310 usbin_valid_irq_handler),
1311 CHG_IRQ(USBIN_OV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1312 usbin_ov_irq_handler),
1313 CHG_IRQ(BATT_INSERTED_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1314 batt_inserted_irq_handler),
1315 CHG_IRQ(VBATDET_LOW_IRQ, IRQF_TRIGGER_RISING, vbatdet_low_irq_handler),
1316 CHG_IRQ(USBIN_UV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1317 usbin_uv_irq_handler),
1318 CHG_IRQ(VBAT_OV_IRQ, IRQF_TRIGGER_RISING, vbat_ov_irq_handler),
1319 CHG_IRQ(CHGWDOG_IRQ, IRQF_TRIGGER_RISING, chgwdog_irq_handler),
1320 CHG_IRQ(VCP_IRQ, IRQF_TRIGGER_RISING, vcp_irq_handler),
1321 CHG_IRQ(ATCDONE_IRQ, IRQF_TRIGGER_RISING, atcdone_irq_handler),
1322 CHG_IRQ(ATCFAIL_IRQ, IRQF_TRIGGER_RISING, atcfail_irq_handler),
1323 CHG_IRQ(CHGDONE_IRQ, IRQF_TRIGGER_RISING, chgdone_irq_handler),
1324 CHG_IRQ(CHGFAIL_IRQ, IRQF_TRIGGER_RISING, chgfail_irq_handler),
1325 CHG_IRQ(CHGSTATE_IRQ, IRQF_TRIGGER_RISING, chgstate_irq_handler),
1326 CHG_IRQ(LOOP_CHANGE_IRQ, IRQF_TRIGGER_RISING, loop_change_irq_handler),
1327 CHG_IRQ(FASTCHG_IRQ, IRQF_TRIGGER_RISING, fastchg_irq_handler),
1328 CHG_IRQ(TRKLCHG_IRQ, IRQF_TRIGGER_RISING, trklchg_irq_handler),
1329 CHG_IRQ(BATT_REMOVED_IRQ, IRQF_TRIGGER_RISING,
1330 batt_removed_irq_handler),
1331 CHG_IRQ(BATTTEMP_HOT_IRQ, IRQF_TRIGGER_RISING,
1332 batttemp_hot_irq_handler),
1333 CHG_IRQ(CHGHOT_IRQ, IRQF_TRIGGER_RISING, chghot_irq_handler),
1334 CHG_IRQ(BATTTEMP_COLD_IRQ, IRQF_TRIGGER_RISING,
1335 batttemp_cold_irq_handler),
1336 CHG_IRQ(CHG_GONE_IRQ, IRQF_TRIGGER_RISING, chg_gone_irq_handler),
1337 CHG_IRQ(BAT_TEMP_OK_IRQ, IRQF_TRIGGER_RISING, bat_temp_ok_irq_handler),
1338 CHG_IRQ(COARSE_DET_LOW_IRQ, IRQF_TRIGGER_RISING,
1339 coarse_det_low_irq_handler),
1340 CHG_IRQ(VDD_LOOP_IRQ, IRQF_TRIGGER_RISING, vdd_loop_irq_handler),
1341 CHG_IRQ(VREG_OV_IRQ, IRQF_TRIGGER_RISING, vreg_ov_irq_handler),
1342 CHG_IRQ(VBATDET_IRQ, IRQF_TRIGGER_RISING, vbatdet_irq_handler),
1343 CHG_IRQ(BATFET_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1344 batfet_irq_handler),
1345 CHG_IRQ(DCIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1346 dcin_valid_irq_handler),
1347 CHG_IRQ(DCIN_OV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1348 dcin_ov_irq_handler),
1349 CHG_IRQ(DCIN_UV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1350 dcin_uv_irq_handler),
1351};
1352
1353static int __devinit request_irqs(struct pm8921_chg_chip *chip,
1354 struct platform_device *pdev)
1355{
1356 struct resource *res;
1357 int ret, i;
1358
1359 ret = 0;
1360 bitmap_fill(chip->enabled_irqs, PM_CHG_MAX_INTS);
1361
1362 for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) {
1363 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1364 chg_irq_data[i].name);
1365 if (res == NULL) {
1366 pr_err("couldn't find %s\n", chg_irq_data[i].name);
1367 goto err_out;
1368 }
1369 ret = request_irq(res->start, chg_irq_data[i].handler,
1370 chg_irq_data[i].flags,
1371 chg_irq_data[i].name, chip);
1372 if (ret < 0) {
1373 pr_err("couldn't request %d (%s) %d\n", res->start,
1374 chg_irq_data[i].name, ret);
1375 goto err_out;
1376 }
1377 chip->pmic_chg_irq[chg_irq_data[i].irq_id] = res->start;
1378 pm8921_chg_disable_irq(chip, chg_irq_data[i].irq_id);
1379 }
1380 return 0;
1381
1382err_out:
1383 free_irqs(chip);
1384 return -EINVAL;
1385}
1386
1387#define ENUM_TIMER_STOP_BIT BIT(1)
1388#define BOOT_DONE_BIT BIT(6)
1389#define CHG_BATFET_ON_BIT BIT(3)
1390#define CHG_VCP_EN BIT(0)
1391#define CHG_BAT_TEMP_DIS_BIT BIT(2)
1392#define SAFE_CURRENT_MA 1500
1393static int __devinit pm8921_chg_hw_init(struct pm8921_chg_chip *chip)
1394{
1395 int rc;
1396
1397 rc = pm_chg_masked_write(chip, SYS_CONFIG_2,
1398 BOOT_DONE_BIT, BOOT_DONE_BIT);
1399 if (rc) {
1400 pr_err("Failed to set BOOT_DONE_BIT rc=%d\n", rc);
1401 return rc;
1402 }
1403
1404 rc = pm_chg_vddsafe_set(chip, chip->max_voltage);
1405 if (rc) {
1406 pr_err("Failed to set safe voltage to %d rc=%d\n",
1407 chip->max_voltage, rc);
1408 return rc;
1409 }
1410 rc = pm_chg_vbatdet_set(chip, chip->resume_voltage);
1411 if (rc) {
1412 pr_err("Failed to set vbatdet comprator voltage to %d rc=%d\n",
1413 chip->resume_voltage, rc);
1414 return rc;
1415 }
1416
1417 rc = pm_chg_vddmax_set(chip, chip->max_voltage);
1418 if (rc) {
1419 pr_err("Failed to set max voltage to %d rc=%d\n",
1420 chip->max_voltage, rc);
1421 return rc;
1422 }
1423 rc = pm_chg_ibatsafe_set(chip, SAFE_CURRENT_MA);
1424 if (rc) {
1425 pr_err("Failed to set max voltage to %d rc=%d\n",
1426 SAFE_CURRENT_MA, rc);
1427 return rc;
1428 }
1429
1430 /* TODO needs to be changed as per the temeperature of the battery */
1431 rc = pm_chg_ibatmax_set(chip, 400);
1432 if (rc) {
1433 pr_err("Failed to set max current to 400 rc=%d\n", rc);
1434 return rc;
1435 }
1436
1437 rc = pm_chg_iterm_set(chip, chip->term_current);
1438 if (rc) {
1439 pr_err("Failed to set term current to %d rc=%d\n",
1440 chip->term_current, rc);
1441 return rc;
1442 }
1443
1444 /* Disable the ENUM TIMER */
1445 rc = pm_chg_masked_write(chip, PBL_ACCESS2, ENUM_TIMER_STOP_BIT,
1446 ENUM_TIMER_STOP_BIT);
1447 if (rc) {
1448 pr_err("Failed to set enum timer stop rc=%d\n", rc);
1449 return rc;
1450 }
1451
1452 /* init with the lowest USB current */
1453 rc = pm_chg_iusbmax_set(chip, usb_ma_table[0].chg_iusb_value);
1454 if (rc) {
1455 pr_err("Failed to set usb max to %d rc=%d\n",
1456 usb_ma_table[0].chg_iusb_value, rc);
1457 return rc;
1458 }
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001459
1460 if (chip->safety_time != 0) {
1461 rc = pm_chg_tchg_max_set(chip, chip->safety_time);
1462 if (rc) {
1463 pr_err("Failed to set max time to %d minutes rc=%d\n",
1464 chip->safety_time, rc);
1465 return rc;
1466 }
1467 }
1468
1469 if (chip->ttrkl_time != 0) {
1470 rc = pm_chg_ttrkl_max_set(chip, chip->safety_time);
1471 if (rc) {
1472 pr_err("Failed to set trkl time to %d minutes rc=%d\n",
1473 chip->safety_time, rc);
1474 return rc;
1475 }
1476 }
1477
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001478 rc = pm_chg_disable_wd(chip);
1479 if (rc) {
1480 pr_err("Failed to disable wd rc=%d\n", rc);
1481 return rc;
1482 }
1483
1484 rc = pm_chg_masked_write(chip, CHG_CNTRL_2,
1485 CHG_BAT_TEMP_DIS_BIT, 0);
1486 if (rc) {
1487 pr_err("Failed to enable temp control chg rc=%d\n", rc);
1488 return rc;
1489 }
1490 /* switch to a 3.2Mhz for the buck */
1491 rc = pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CLOCK_CTRL, 0x15);
1492 if (rc) {
1493 pr_err("Failed to switch buck clk rc=%d\n", rc);
1494 return rc;
1495 }
1496
1497 /* Workarounds for die 1.1 and 1.0 */
1498 if (pm8xxx_get_revision(chip->dev->parent) < PM8XXX_REVISION_8921_2p0) {
1499 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST2, 0xF1);
1500 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0x8C);
1501 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xCE);
1502 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xD8);
Abhijeet Dharmapurikar305d7022011-08-10 17:07:44 -07001503 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xF1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001504 }
1505
1506 rc = pm_chg_charge_dis(chip, charging_disabled);
1507 if (rc) {
1508 pr_err("Failed to disable CHG_CHARGE_DIS bit rc=%d\n", rc);
1509 return rc;
1510 }
1511
1512 rc = pm_chg_auto_enable(chip, !charging_disabled);
1513 if (rc) {
1514 pr_err("Failed to enable charging rc=%d\n", rc);
1515 return rc;
1516 }
1517
1518 return 0;
1519}
1520
1521static int get_rt_status(void *data, u64 * val)
1522{
1523 int i = (int)data;
1524 int ret;
1525
1526 /* global irq number is passed in via data */
1527 ret = pm_chg_get_rt_status(the_chip, i);
1528 *val = ret;
1529 return 0;
1530}
1531DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
1532
1533static int get_fsm_status(void *data, u64 * val)
1534{
1535 u8 temp;
1536
1537 temp = pm_chg_get_fsm_state(the_chip);
1538 *val = temp;
1539 return 0;
1540}
1541DEFINE_SIMPLE_ATTRIBUTE(fsm_fops, get_fsm_status, NULL, "%llu\n");
1542
1543static int get_reg(void *data, u64 * val)
1544{
1545 int addr = (int)data;
1546 int ret;
1547 u8 temp;
1548
1549 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
1550 if (ret) {
1551 pr_err("pm8xxx_readb to %x value =%d errored = %d\n",
1552 addr, temp, ret);
1553 return -EAGAIN;
1554 }
1555 *val = temp;
1556 return 0;
1557}
1558
1559static int set_reg(void *data, u64 val)
1560{
1561 int addr = (int)data;
1562 int ret;
1563 u8 temp;
1564
1565 temp = (u8) val;
1566 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
1567 if (ret) {
1568 pr_err("pm8xxx_writeb to %x value =%d errored = %d\n",
1569 addr, temp, ret);
1570 return -EAGAIN;
1571 }
1572 return 0;
1573}
1574DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
1575
1576static void create_debugfs_entries(struct pm8921_chg_chip *chip)
1577{
1578 int i;
1579
1580 chip->dent = debugfs_create_dir("pm8921_chg", NULL);
1581
1582 if (IS_ERR(chip->dent)) {
1583 pr_err("pmic charger couldnt create debugfs dir\n");
1584 return;
1585 }
1586
1587 debugfs_create_file("CHG_CNTRL", 0644, chip->dent,
1588 (void *)CHG_CNTRL, &reg_fops);
1589 debugfs_create_file("CHG_CNTRL_2", 0644, chip->dent,
1590 (void *)CHG_CNTRL_2, &reg_fops);
1591 debugfs_create_file("CHG_CNTRL_3", 0644, chip->dent,
1592 (void *)CHG_CNTRL_3, &reg_fops);
1593 debugfs_create_file("PBL_ACCESS1", 0644, chip->dent,
1594 (void *)PBL_ACCESS1, &reg_fops);
1595 debugfs_create_file("PBL_ACCESS2", 0644, chip->dent,
1596 (void *)PBL_ACCESS2, &reg_fops);
1597 debugfs_create_file("SYS_CONFIG_1", 0644, chip->dent,
1598 (void *)SYS_CONFIG_1, &reg_fops);
1599 debugfs_create_file("SYS_CONFIG_2", 0644, chip->dent,
1600 (void *)SYS_CONFIG_2, &reg_fops);
1601 debugfs_create_file("CHG_VDD_MAX", 0644, chip->dent,
1602 (void *)CHG_VDD_MAX, &reg_fops);
1603 debugfs_create_file("CHG_VDD_SAFE", 0644, chip->dent,
1604 (void *)CHG_VDD_SAFE, &reg_fops);
1605 debugfs_create_file("CHG_VBAT_DET", 0644, chip->dent,
1606 (void *)CHG_VBAT_DET, &reg_fops);
1607 debugfs_create_file("CHG_IBAT_MAX", 0644, chip->dent,
1608 (void *)CHG_IBAT_MAX, &reg_fops);
1609 debugfs_create_file("CHG_IBAT_SAFE", 0644, chip->dent,
1610 (void *)CHG_IBAT_SAFE, &reg_fops);
1611 debugfs_create_file("CHG_VIN_MIN", 0644, chip->dent,
1612 (void *)CHG_VIN_MIN, &reg_fops);
1613 debugfs_create_file("CHG_VTRICKLE", 0644, chip->dent,
1614 (void *)CHG_VTRICKLE, &reg_fops);
1615 debugfs_create_file("CHG_ITRICKLE", 0644, chip->dent,
1616 (void *)CHG_ITRICKLE, &reg_fops);
1617 debugfs_create_file("CHG_ITERM", 0644, chip->dent,
1618 (void *)CHG_ITERM, &reg_fops);
1619 debugfs_create_file("CHG_TCHG_MAX", 0644, chip->dent,
1620 (void *)CHG_TCHG_MAX, &reg_fops);
1621 debugfs_create_file("CHG_TWDOG", 0644, chip->dent,
1622 (void *)CHG_TWDOG, &reg_fops);
1623 debugfs_create_file("CHG_TEMP_THRESH", 0644, chip->dent,
1624 (void *)CHG_TEMP_THRESH, &reg_fops);
1625 debugfs_create_file("CHG_COMP_OVR", 0644, chip->dent,
1626 (void *)CHG_COMP_OVR, &reg_fops);
1627 debugfs_create_file("CHG_BUCK_CTRL_TEST1", 0644, chip->dent,
1628 (void *)CHG_BUCK_CTRL_TEST1, &reg_fops);
1629 debugfs_create_file("CHG_BUCK_CTRL_TEST2", 0644, chip->dent,
1630 (void *)CHG_BUCK_CTRL_TEST2, &reg_fops);
1631 debugfs_create_file("CHG_BUCK_CTRL_TEST3", 0644, chip->dent,
1632 (void *)CHG_BUCK_CTRL_TEST3, &reg_fops);
1633 debugfs_create_file("CHG_TEST", 0644, chip->dent,
1634 (void *)CHG_TEST, &reg_fops);
1635
1636 debugfs_create_file("FSM_STATE", 0644, chip->dent, NULL,
1637 &fsm_fops);
1638
1639 for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) {
1640 if (chip->pmic_chg_irq[chg_irq_data[i].irq_id])
1641 debugfs_create_file(chg_irq_data[i].name, 0444,
1642 chip->dent,
1643 (void *)chg_irq_data[i].irq_id,
1644 &rt_fops);
1645 }
1646}
1647
1648static int __devinit pm8921_charger_probe(struct platform_device *pdev)
1649{
1650 int rc = 0;
1651 struct pm8921_chg_chip *chip;
1652 const struct pm8921_charger_platform_data *pdata
1653 = pdev->dev.platform_data;
1654
1655 if (!pdata) {
1656 pr_err("missing platform data\n");
1657 return -EINVAL;
1658 }
1659
1660 chip = kzalloc(sizeof(struct pm8921_chg_chip),
1661 GFP_KERNEL);
1662 if (!chip) {
1663 pr_err("Cannot allocate pm_chg_chip\n");
1664 return -ENOMEM;
1665 }
1666
1667 chip->dev = &pdev->dev;
1668 chip->safety_time = pdata->safety_time;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001669 chip->ttrkl_time = pdata->ttrkl_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670 chip->update_time = pdata->update_time;
1671 chip->max_voltage = pdata->max_voltage;
1672 chip->min_voltage = pdata->min_voltage;
1673 chip->resume_voltage = pdata->resume_voltage;
1674 chip->term_current = pdata->term_current;
1675 chip->vbat_channel = pdata->charger_cdata.vbat_channel;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001676 chip->batt_temp_channel = pdata->charger_cdata.batt_temp_channel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001677
1678 rc = pm8921_chg_hw_init(chip);
1679 if (rc) {
1680 pr_err("couldn't init hardware rc=%d\n", rc);
1681 goto free_chip;
1682 }
1683
1684 chip->usb_psy.name = "usb",
1685 chip->usb_psy.type = POWER_SUPPLY_TYPE_USB,
1686 chip->usb_psy.supplied_to = pm_power_supplied_to,
1687 chip->usb_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to),
1688 chip->usb_psy.properties = pm_power_props,
1689 chip->usb_psy.num_properties = ARRAY_SIZE(pm_power_props),
1690 chip->usb_psy.get_property = pm_power_get_property,
1691
1692 chip->dc_psy.name = "ac",
1693 chip->dc_psy.type = POWER_SUPPLY_TYPE_MAINS,
1694 chip->dc_psy.supplied_to = pm_power_supplied_to,
1695 chip->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to),
1696 chip->dc_psy.properties = pm_power_props,
1697 chip->dc_psy.num_properties = ARRAY_SIZE(pm_power_props),
1698 chip->dc_psy.get_property = pm_power_get_property,
1699
1700 chip->batt_psy.name = "battery",
1701 chip->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY,
1702 chip->batt_psy.properties = msm_batt_power_props,
1703 chip->batt_psy.num_properties = ARRAY_SIZE(msm_batt_power_props),
1704 chip->batt_psy.get_property = pm_batt_power_get_property,
1705
1706 rc = power_supply_register(chip->dev, &chip->usb_psy);
1707 if (rc < 0) {
1708 pr_err("power_supply_register usb failed rc = %d\n", rc);
1709 goto free_irq;
1710 }
1711
1712 rc = power_supply_register(chip->dev, &chip->dc_psy);
1713 if (rc < 0) {
1714 pr_err("power_supply_register dc failed rc = %d\n", rc);
1715 goto unregister_usb;
1716 }
1717
1718 rc = power_supply_register(chip->dev, &chip->batt_psy);
1719 if (rc < 0) {
1720 pr_err("power_supply_register batt failed rc = %d\n", rc);
1721 goto unregister_dc;
1722 }
1723
1724 rc = request_irqs(chip, pdev);
1725 if (rc) {
1726 pr_err("couldn't register interrupts rc=%d\n", rc);
1727 goto unregister_batt;
1728 }
1729
Abhijeet Dharmapurikare27d8092011-08-12 17:16:29 -07001730 enable_irq_wake(chip->pmic_chg_irq[USBIN_VALID_IRQ]);
1731 enable_irq_wake(chip->pmic_chg_irq[USBIN_OV_IRQ]);
1732 enable_irq_wake(chip->pmic_chg_irq[USBIN_UV_IRQ]);
1733
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001734 platform_set_drvdata(pdev, chip);
1735 the_chip = chip;
1736 create_debugfs_entries(chip);
1737
1738 INIT_WORK(&chip->bms_notify.work, bms_notify);
1739 /* determine what state the charger is in */
1740 determine_initial_state(chip);
1741
1742 return 0;
1743
1744free_irq:
1745 free_irqs(chip);
1746unregister_batt:
1747 power_supply_unregister(&chip->batt_psy);
1748unregister_dc:
1749 power_supply_unregister(&chip->dc_psy);
1750unregister_usb:
1751 power_supply_unregister(&chip->usb_psy);
1752free_chip:
1753 kfree(chip);
1754 return rc;
1755}
1756
1757static int __devexit pm8921_charger_remove(struct platform_device *pdev)
1758{
1759 struct pm8921_chg_chip *chip = platform_get_drvdata(pdev);
1760
1761 free_irqs(chip);
1762 platform_set_drvdata(pdev, NULL);
1763 the_chip = NULL;
1764 kfree(chip);
1765 return 0;
1766}
1767
1768static struct platform_driver pm8921_charger_driver = {
1769 .probe = pm8921_charger_probe,
1770 .remove = __devexit_p(pm8921_charger_remove),
1771 .driver = {
1772 .name = PM8921_CHARGER_DEV_NAME,
1773 .owner = THIS_MODULE,
1774 },
1775};
1776
1777static int __init pm8921_charger_init(void)
1778{
1779 return platform_driver_register(&pm8921_charger_driver);
1780}
1781
1782static void __exit pm8921_charger_exit(void)
1783{
1784 platform_driver_unregister(&pm8921_charger_driver);
1785}
1786
1787late_initcall(pm8921_charger_init);
1788module_exit(pm8921_charger_exit);
1789
1790MODULE_LICENSE("GPL v2");
1791MODULE_DESCRIPTION("PMIC8921 charger/battery driver");
1792MODULE_VERSION("1.0");
1793MODULE_ALIAS("platform:" PM8921_CHARGER_DEV_NAME);