blob: bed032a988a53bcf412fbe3fdfb83b643945a24f [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010-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
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/platform_device.h>
17#include <linux/errno.h>
18#include <linux/mfd/pmic8058.h>
19#include <linux/interrupt.h>
20#include <linux/delay.h>
21#include <linux/bitops.h>
22#include <linux/workqueue.h>
23#include <linux/msm-charger.h>
24#include <linux/debugfs.h>
25#include <linux/slab.h>
26#include <linux/msm_adc.h>
27#include <linux/notifier.h>
28#include <linux/pmic8058-batt-alarm.h>
29
30#include <mach/msm_xo.h>
31#include <mach/msm_hsusb.h>
32
33/* Config Regs and their bits*/
34#define PM8058_CHG_TEST 0x75
35#define IGNORE_LL 2
36#define PM8058_CHG_TEST_2 0xEA
37#define PM8058_CHG_TEST_3 0xEB
38#define PM8058_OVP_TEST_REG 0xF6
39#define FORCE_OVP_OFF 3
40
41#define PM8058_CHG_CNTRL 0x1E
42#define CHG_TRICKLE_EN 7
43#define CHG_USB_SUSPEND 6
44#define CHG_IMON_CAL 5
45#define CHG_IMON_GAIN 4
46#define CHG_CHARGE_BAT 3
47#define CHG_VBUS_FROM_BOOST_OVRD 2
48#define CHG_CHARGE_DIS 1
49#define CHG_VCP_EN 0
50
51#define PM8058_CHG_CNTRL_2 0xD8
52#define ATC_DIS 7 /* coincell backed */
53#define CHARGE_AUTO_DIS 6
54#define DUMB_CHG_OVRD 5 /* coincell backed */
55#define ENUM_DONE 4
56#define CHG_TEMP_MODE 3
57#define CHG_BATT_TEMP_DIS 1 /* coincell backed */
58#define CHG_FAILED_CLEAR 0
59
60#define PM8058_CHG_VMAX_SEL 0x21
61#define PM8058_CHG_VBAT_DET 0xD9
62#define PM8058_CHG_IMAX 0x1F
63#define PM8058_CHG_TRICKLE 0xDB
64#define PM8058_CHG_ITERM 0xDC
65#define PM8058_CHG_TTRKL_MAX 0xE1
66#define PM8058_CHG_TCHG_MAX 0xE4
67#define PM8058_CHG_TEMP_THRESH 0xE2
68#define PM8058_CHG_TEMP_REG 0xE3
69#define PM8058_CHG_PULSE 0x22
70
71/* IRQ STATUS and CLEAR */
72#define PM8058_CHG_STATUS_CLEAR_IRQ_1 0x31
73#define PM8058_CHG_STATUS_CLEAR_IRQ_3 0x33
74#define PM8058_CHG_STATUS_CLEAR_IRQ_10 0xB3
75#define PM8058_CHG_STATUS_CLEAR_IRQ_11 0xB4
76
77/* IRQ MASKS */
78#define PM8058_CHG_MASK_IRQ_1 0x38
79
80#define PM8058_CHG_MASK_IRQ_3 0x3A
81#define PM8058_CHG_MASK_IRQ_10 0xBA
82#define PM8058_CHG_MASK_IRQ_11 0xBB
83
84/* IRQ Real time status regs */
85#define PM8058_CHG_STATUS_RT_1 0x3F
86#define STATUS_RTCHGVAL 7
87#define STATUS_RTCHGINVAL 6
88#define STATUS_RTBATT_REPLACE 5
89#define STATUS_RTVBATDET_LOW 4
90#define STATUS_RTCHGILIM 3
91#define STATUS_RTPCTDONE 1
92#define STATUS_RTVCP 0
93#define PM8058_CHG_STATUS_RT_3 0x41
94#define PM8058_CHG_STATUS_RT_10 0xC1
95#define PM8058_CHG_STATUS_RT_11 0xC2
96
97/* VTRIM */
98#define PM8058_CHG_VTRIM 0x1D
99#define PM8058_CHG_VBATDET_TRIM 0x1E
100#define PM8058_CHG_ITRIM 0x1F
101#define PM8058_CHG_TTRIM 0x20
102
103#define AUTO_CHARGING_VMAXSEL 4200
104#define AUTO_CHARGING_FAST_TIME_MAX_MINUTES 512
105#define AUTO_CHARGING_TRICKLE_TIME_MINUTES 30
106#define AUTO_CHARGING_VEOC_ITERM 100
107#define AUTO_CHARGING_IEOC_ITERM 160
108#define AUTO_CHARGING_RESUME_MV 4100
109
110#define AUTO_CHARGING_VBATDET 4150
111#define AUTO_CHARGING_VBATDET_DEBOUNCE_TIME_MS 3000
112#define AUTO_CHARGING_VEOC_VBATDET 4100
113#define AUTO_CHARGING_VEOC_TCHG 16
114#define AUTO_CHARGING_VEOC_TCHG_FINAL_CYCLE 32
115#define AUTO_CHARGING_VEOC_BEGIN_TIME_MS 5400000
116
117#define AUTO_CHARGING_VEOC_VBAT_LOW_CHECK_TIME_MS 60000
118#define AUTO_CHARGING_RESUME_CHARGE_DETECTION_COUNTER 5
119
120#define AUTO_CHARGING_DONE_CHECK_TIME_MS 1000
121
122#define PM8058_CHG_I_STEP_MA 50
123#define PM8058_CHG_I_MIN_MA 50
124#define PM8058_CHG_T_TCHG_SHIFT 2
125#define PM8058_CHG_I_TERM_STEP_MA 10
126#define PM8058_CHG_V_STEP_MV 25
127#define PM8058_CHG_V_MIN_MV 2400
128/*
129 * enum pmic_chg_interrupts: pmic interrupts
130 * @CHGVAL_IRQ: charger V between 3.3 and 7.9
131 * @CHGINVAL_IRQ: charger V outside 3.3 and 7.9
132 * @VBATDET_LOW_IRQ: VBAT < VBATDET
133 * @VCP_IRQ: VDD went below VBAT: BAT_FET is turned on
134 * @CHGILIM_IRQ: mA consumed>IMAXSEL: chgloop draws less mA
135 * @ATC_DONE_IRQ: Auto Trickle done
136 * @ATCFAIL_IRQ: Auto Trickle fail
137 * @AUTO_CHGDONE_IRQ: Auto chg done
138 * @AUTO_CHGFAIL_IRQ: time exceeded w/o reaching term current
139 * @CHGSTATE_IRQ: something happend causing a state change
140 * @FASTCHG_IRQ: trkl charging completed: moving to fastchg
141 * @CHG_END_IRQ: mA has dropped to termination current
142 * @BATTTEMP_IRQ: batt temp is out of range
143 * @CHGHOT_IRQ: the pass device is too hot
144 * @CHGTLIMIT_IRQ: unused
145 * @CHG_GONE_IRQ: charger was removed
146 * @VCPMAJOR_IRQ: vcp major
147 * @VBATDET_IRQ: VBAT >= VBATDET
148 * @BATFET_IRQ: BATFET closed
149 * @BATT_REPLACE_IRQ:
150 * @BATTCONNECT_IRQ:
151 */
152enum pmic_chg_interrupts {
153 CHGVAL_IRQ,
154 CHGINVAL_IRQ,
155 VBATDET_LOW_IRQ,
156 VCP_IRQ,
157 CHGILIM_IRQ,
158 ATC_DONE_IRQ,
159 ATCFAIL_IRQ,
160 AUTO_CHGDONE_IRQ,
161 AUTO_CHGFAIL_IRQ,
162 CHGSTATE_IRQ,
163 FASTCHG_IRQ,
164 CHG_END_IRQ,
165 BATTTEMP_IRQ,
166 CHGHOT_IRQ,
167 CHGTLIMIT_IRQ,
168 CHG_GONE_IRQ,
169 VCPMAJOR_IRQ,
170 VBATDET_IRQ,
171 BATFET_IRQ,
172 BATT_REPLACE_IRQ,
173 BATTCONNECT_IRQ,
174 PMIC_CHG_MAX_INTS
175};
176
177struct pm8058_charger {
178 struct pmic_charger_pdata *pdata;
179 struct pm8058_chip *pm_chip;
180 struct device *dev;
181
182 int pmic_chg_irq[PMIC_CHG_MAX_INTS];
183 DECLARE_BITMAP(enabled_irqs, PMIC_CHG_MAX_INTS);
184
185 struct delayed_work chg_done_check_work;
186 struct delayed_work check_vbat_low_work;
187 struct delayed_work veoc_begin_work;
188 struct delayed_work charging_check_work;
189 int waiting_for_topoff;
190 int waiting_for_veoc;
191 int vbatdet;
192 struct msm_hardware_charger hw_chg;
193 int current_charger_current;
194 int disabled;
195
196 struct msm_xo_voter *voter;
197 struct dentry *dent;
198
199 int inited;
200 int present;
201};
202
203static struct pm8058_charger pm8058_chg;
204static struct msm_hardware_charger usb_hw_chg;
205
206
207static int msm_battery_gauge_alarm_notify(struct notifier_block *nb,
208 unsigned long status, void *unused);
209
210static struct notifier_block alarm_notifier = {
211 .notifier_call = msm_battery_gauge_alarm_notify,
212};
213
214static int resume_mv = AUTO_CHARGING_RESUME_MV;
215static DEFINE_MUTEX(batt_alarm_lock);
216static int resume_mv_set(const char *val, struct kernel_param *kp);
217module_param_call(resume_mv, resume_mv_set, param_get_int,
218 &resume_mv, S_IRUGO | S_IWUSR);
219
220static int resume_mv_set(const char *val, struct kernel_param *kp)
221{
222 int rc;
223
224 mutex_lock(&batt_alarm_lock);
225
226 rc = param_set_int(val, kp);
227 if (rc)
228 goto out;
229
230 rc = pm8058_batt_alarm_threshold_set(resume_mv, 4300);
231
232out:
233 mutex_unlock(&batt_alarm_lock);
234 return rc;
235}
236
237static void pm8058_chg_enable_irq(int interrupt)
238{
239 if (!__test_and_set_bit(interrupt, pm8058_chg.enabled_irqs)) {
240 dev_dbg(pm8058_chg.dev, "%s %d\n", __func__,
241 pm8058_chg.pmic_chg_irq[interrupt]);
242 enable_irq(pm8058_chg.pmic_chg_irq[interrupt]);
243 }
244}
245
246static void pm8058_chg_disable_irq(int interrupt)
247{
248 if (__test_and_clear_bit(interrupt, pm8058_chg.enabled_irqs)) {
249 dev_dbg(pm8058_chg.dev, "%s %d\n", __func__,
250 pm8058_chg.pmic_chg_irq[interrupt]);
251 disable_irq_nosync(pm8058_chg.pmic_chg_irq[interrupt]);
252 }
253}
254
255static int pm_chg_get_rt_status(int irq)
256{
257 int count = 3;
258 int ret;
259
260 while ((ret =
261 pm8058_irq_get_rt_status(pm8058_chg.pm_chip, irq)) == -EAGAIN
262 && count--) {
263 dev_info(pm8058_chg.dev, "%s trycount=%d\n", __func__, count);
264 cpu_relax();
265 }
266 if (ret == -EAGAIN)
267 return 0;
268 else
269 return ret;
270}
271
272static int is_chg_plugged_in(void)
273{
274 return pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGVAL_IRQ]);
275}
276
277#ifdef DEBUG
278static void __dump_chg_regs(void)
279{
280 u8 temp;
281 int temp2;
282
283 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1);
284 dev_dbg(pm8058_chg.dev, "PM8058_CHG_CNTRL = 0x%x\n", temp);
285 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1);
286 dev_dbg(pm8058_chg.dev, "PM8058_CHG_CNTRL_2 = 0x%x\n", temp);
287 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_VMAX_SEL, &temp, 1);
288 dev_dbg(pm8058_chg.dev, "PM8058_CHG_VMAX_SEL = 0x%x\n", temp);
289 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_VBAT_DET, &temp, 1);
290 dev_dbg(pm8058_chg.dev, "PM8058_CHG_VBAT_DET = 0x%x\n", temp);
291 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_IMAX, &temp, 1);
292 dev_dbg(pm8058_chg.dev, "PM8058_CHG_IMAX = 0x%x\n", temp);
293 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TRICKLE, &temp, 1);
294 dev_dbg(pm8058_chg.dev, "PM8058_CHG_TRICKLE = 0x%x\n", temp);
295 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_ITERM, &temp, 1);
296 dev_dbg(pm8058_chg.dev, "PM8058_CHG_ITERM = 0x%x\n", temp);
297 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TTRKL_MAX, &temp, 1);
298 dev_dbg(pm8058_chg.dev, "PM8058_CHG_TTRKL_MAX = 0x%x\n", temp);
299 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TCHG_MAX, &temp, 1);
300 dev_dbg(pm8058_chg.dev, "PM8058_CHG_TCHG_MAX = 0x%x\n", temp);
301 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TEMP_THRESH, &temp, 1);
302 dev_dbg(pm8058_chg.dev, "PM8058_CHG_TEMP_THRESH = 0x%x\n", temp);
303 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TEMP_REG, &temp, 1);
304 dev_dbg(pm8058_chg.dev, "PM8058_CHG_TEMP_REG = 0x%x\n", temp);
305 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_PULSE, &temp, 1);
306 dev_dbg(pm8058_chg.dev, "PM8058_CHG_PULSE = 0x%x\n", temp);
307
308 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_STATUS_CLEAR_IRQ_1,
309 &temp, 1);
310 dev_dbg(pm8058_chg.dev, "PM8058_CHG_STATUS_CLEAR_IRQ_1 = 0x%x\n", temp);
311 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_STATUS_CLEAR_IRQ_3,
312 &temp, 1);
313 dev_dbg(pm8058_chg.dev, "PM8058_CHG_STATUS_CLEAR_IRQ_3 = 0x%x\n", temp);
314 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_STATUS_CLEAR_IRQ_10,
315 &temp, 1);
316 dev_dbg(pm8058_chg.dev, "PM8058_CHG_STATUS_CLEAR_IRQ_10 = 0x%x\n",
317 temp);
318 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_STATUS_CLEAR_IRQ_11,
319 &temp, 1);
320 dev_dbg(pm8058_chg.dev, "PM8058_CHG_STATUS_CLEAR_IRQ_11 = 0x%x\n",
321 temp);
322
323 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_MASK_IRQ_1, &temp, 1);
324 dev_dbg(pm8058_chg.dev, "PM8058_CHG_MASK_IRQ_1 = 0x%x\n", temp);
325 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_MASK_IRQ_3, &temp, 1);
326 dev_dbg(pm8058_chg.dev, "PM8058_CHG_MASK_IRQ_3 = 0x%x\n", temp);
327 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_MASK_IRQ_10, &temp, 1);
328 dev_dbg(pm8058_chg.dev, "PM8058_CHG_MASK_IRQ_10 = 0x%x\n", temp);
329 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_MASK_IRQ_11, &temp, 1);
330 dev_dbg(pm8058_chg.dev, "PM8058_CHG_MASK_IRQ_11 = 0x%x\n", temp);
331
332 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGVAL_IRQ]);
333 dev_dbg(pm8058_chg.dev, "CHGVAL_IRQ = %d\n", temp2);
334
335 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGINVAL_IRQ]);
336 dev_dbg(pm8058_chg.dev, "CHGINVAL_IRQ = %d\n", temp2);
337
338 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[VBATDET_LOW_IRQ]);
339 dev_dbg(pm8058_chg.dev, "VBATDET_LOW_IRQ= %d\n", temp2);
340
341 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[VCP_IRQ]);
342 dev_dbg(pm8058_chg.dev, "VCP_IRQ= %d\n", temp2);
343
344 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGILIM_IRQ]);
345 dev_dbg(pm8058_chg.dev, "CHGILIM_IRQ= %d\n", temp2);
346
347 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[ATC_DONE_IRQ]);
348 dev_dbg(pm8058_chg.dev, "ATC_DONE_IRQ= %d\n", temp2);
349
350 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[ATCFAIL_IRQ]);
351 dev_dbg(pm8058_chg.dev, "ATCFAIL_IRQ= %d\n", temp2);
352
353 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[AUTO_CHGDONE_IRQ]);
354 dev_dbg(pm8058_chg.dev, "AUTO_CHGDONE_IRQ= %d\n", temp2);
355
356 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[AUTO_CHGFAIL_IRQ]);
357 dev_dbg(pm8058_chg.dev, "AUTO_CHGFAIL_IRQ= %d\n", temp2);
358
359 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGSTATE_IRQ]);
360 dev_dbg(pm8058_chg.dev, "CHGSTATE_IRQ= %d\n", temp2);
361
362 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[FASTCHG_IRQ]);
363 dev_dbg(pm8058_chg.dev, "FASTCHG_IRQ= %d\n", temp2);
364
365 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHG_END_IRQ]);
366 dev_dbg(pm8058_chg.dev, "CHG_END_IRQ= %d\n", temp2);
367
368 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATTTEMP_IRQ]);
369 dev_dbg(pm8058_chg.dev, "BATTTEMP_IRQ= %d\n", temp2);
370
371 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGHOT_IRQ]);
372 dev_dbg(pm8058_chg.dev, "CHGHOT_IRQ= %d\n", temp2);
373
374 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHGTLIMIT_IRQ]);
375 dev_dbg(pm8058_chg.dev, "CHGTLIMIT_IRQ= %d\n", temp2);
376
377 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[CHG_GONE_IRQ]);
378 dev_dbg(pm8058_chg.dev, "CHG_GONE_IRQ= %d\n", temp2);
379
380 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[VCPMAJOR_IRQ]);
381 dev_dbg(pm8058_chg.dev, "VCPMAJOR_IRQ= %d\n", temp2);
382
383 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[VBATDET_IRQ]);
384 dev_dbg(pm8058_chg.dev, "VBATDET_IRQ= %d\n", temp2);
385
386 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATFET_IRQ]);
387 dev_dbg(pm8058_chg.dev, "BATFET_IRQ= %d\n", temp2);
388
389 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATT_REPLACE_IRQ]);
390 dev_dbg(pm8058_chg.dev, "BATT_REPLACE_IRQ= %d\n", temp2);
391
392 temp2 = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATTCONNECT_IRQ]);
393 dev_dbg(pm8058_chg.dev, "BATTCONNECT_IRQ= %d\n", temp2);
394}
395#else
396static inline void __dump_chg_regs(void)
397{
398}
399#endif
400
401/* SSBI register access helper functions */
402static int pm_chg_suspend(int value)
403{
404 u8 temp;
405 int ret;
406
407 ret = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1);
408 if (ret)
409 return ret;
410 if (value)
411 temp |= BIT(CHG_USB_SUSPEND);
412 else
413 temp &= ~BIT(CHG_USB_SUSPEND);
414
415 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1);
416}
417
418static int pm_chg_auto_disable(int value)
419{
420 u8 temp;
421 int ret;
422
423 ret = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1);
424 if (ret)
425 return ret;
426 if (value)
427 temp |= BIT(CHARGE_AUTO_DIS);
428 else
429 temp &= ~BIT(CHARGE_AUTO_DIS);
430
431 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1);
432}
433
434static int pm_chg_batt_temp_disable(int value)
435{
436 u8 temp;
437 int ret;
438
439 ret = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1);
440 if (ret)
441 return ret;
442 if (value)
443 temp |= BIT(CHG_BATT_TEMP_DIS);
444 else
445 temp &= ~BIT(CHG_BATT_TEMP_DIS);
446
447 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1);
448}
449
450static int pm_chg_vbatdet_set(int voltage)
451{
452 u8 temp;
453 int diff;
454
455 diff = (voltage - PM8058_CHG_V_MIN_MV);
456 if (diff < 0) {
457 dev_warn(pm8058_chg.dev, "%s bad mV=%d asked to set\n",
458 __func__, voltage);
459 return -EINVAL;
460 }
461
462 temp = diff / PM8058_CHG_V_STEP_MV;
463 dev_dbg(pm8058_chg.dev, "%s voltage=%d setting %02x\n", __func__,
464 voltage, temp);
465 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_VBAT_DET, &temp, 1);
466}
467
468static int pm_chg_imaxsel_set(int chg_current)
469{
470 u8 temp;
471 int diff;
472
473 diff = chg_current - PM8058_CHG_I_MIN_MA;
474 if (diff < 0) {
475 dev_warn(pm8058_chg.dev, "%s bad mA=%d asked to set\n",
476 __func__, chg_current);
477 return -EINVAL;
478 }
479 temp = diff / PM8058_CHG_I_STEP_MA;
480 /* make sure we arent writing more than 5 bits of data */
481 if (temp > 31) {
482 dev_warn(pm8058_chg.dev, "%s max mA=1500 requested mA=%d\n",
483 __func__, chg_current);
484 temp = 31;
485 }
486 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_IMAX, &temp, 1);
487}
488
489#define PM8058_CHG_VMAX_MIN 3300
490#define PM8058_CHG_VMAX_MAX 5500
491static int pm_chg_vmaxsel_set(int voltage)
492{
493 u8 temp;
494
495 if (voltage < PM8058_CHG_VMAX_MIN || voltage > PM8058_CHG_VMAX_MAX) {
496 dev_warn(pm8058_chg.dev, "%s bad mV=%d asked to set\n",
497 __func__, voltage);
498 return -EINVAL;
499 }
500 temp = (voltage - PM8058_CHG_V_MIN_MV) / PM8058_CHG_V_STEP_MV;
501 dev_dbg(pm8058_chg.dev, "%s mV=%d setting %02x\n", __func__, voltage,
502 temp);
503 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_VMAX_SEL, &temp, 1);
504}
505
506static int pm_chg_failed_clear(int value)
507{
508 u8 temp;
509 int ret;
510
511 ret = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1);
512 if (ret)
513 return ret;
514 if (value)
515 temp |= BIT(CHG_FAILED_CLEAR);
516 else
517 temp &= ~BIT(CHG_FAILED_CLEAR);
518 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1);
519}
520
521static int pm_chg_iterm_set(int chg_current)
522{
523 u8 temp;
524
525 temp = (chg_current / PM8058_CHG_I_TERM_STEP_MA) - 1;
526 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_ITERM, &temp, 1);
527}
528
529static int pm_chg_tchg_set(int minutes)
530{
531 u8 temp;
532
533 temp = (minutes >> PM8058_CHG_T_TCHG_SHIFT) - 1;
534 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TCHG_MAX, &temp, 1);
535}
536
537static int pm_chg_ttrkl_set(int minutes)
538{
539 u8 temp;
540
541 temp = minutes - 1;
542 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TTRKL_MAX, &temp, 1);
543}
544
545static int pm_chg_enum_done_enable(int value)
546{
547 u8 temp;
548 int ret;
549
550 ret = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1);
551 if (ret)
552 return ret;
553 if (value)
554 temp |= BIT(ENUM_DONE);
555 else
556 temp &= ~BIT(ENUM_DONE);
557
558 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL_2, &temp, 1);
559}
560
561static uint32_t get_fsm_state(void)
562{
563 u8 temp;
564
565 temp = 0x00;
566 pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST_3, &temp, 1);
567 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TEST_3, &temp, 1);
568 return (uint32_t)temp;
569}
570
571static int get_fsm_status(void *data, u64 * val)
572{
573 *val = get_fsm_state();
574 return 0;
575}
576
Abhijeet Dharmapurikar0758e1e2011-07-14 18:11:41 -0700577static int pm_chg_disable(int value)
578{
579 u8 temp;
580 int ret;
581
582 ret = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1);
583 if (ret)
584 return ret;
585 if (value)
586 temp |= BIT(CHG_CHARGE_DIS);
587 else
588 temp &= ~BIT(CHG_CHARGE_DIS);
589
590 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1);
591}
592
593static void pm8058_start_system_current(struct msm_hardware_charger *hw_chg,
594 int max_current)
595{
596 int ret = 0;
597
598 if (pm8058_chg.disabled)
599 return;
600
601 ret = pm_chg_imaxsel_set(max_current);
602 ret |= pm_chg_enum_done_enable(1);
603 ret |= pm_chg_disable(0);
604 if (ret)
605 pr_err("%s: failed to turn on system power err=%d",
606 __func__, ret);
607}
608
609static void pm8058_stop_system_current(struct msm_hardware_charger *hw_chg)
610{
611 int ret = 0;
612
613 ret = pm_chg_enum_done_enable(0);
614 ret |= pm_chg_disable(1);
615 if (ret)
616 pr_err("%s: failed to turn off system power err=%d",
617 __func__, ret);
618}
619
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700620static int __pm8058_start_charging(int chg_current, int termination_current,
621 int time)
622{
623 int ret = 0;
624
625 if (pm8058_chg.disabled)
626 goto out;
627
628 dev_info(pm8058_chg.dev, "%s %dmA %dmin\n",
629 __func__, chg_current, time);
630
631 ret = pm_chg_auto_disable(1);
632 if (ret)
633 goto out;
634
635 ret = pm_chg_suspend(0);
636 if (ret)
637 goto out;
638
639 ret = pm_chg_imaxsel_set(chg_current);
640 if (ret)
641 goto out;
642
643 ret = pm_chg_failed_clear(1);
644 if (ret)
645 goto out;
646
647 ret = pm_chg_iterm_set(termination_current);
648 if (ret)
649 goto out;
650
651 ret = pm_chg_tchg_set(time);
652 if (ret)
653 goto out;
654
655 ret = pm_chg_ttrkl_set(AUTO_CHARGING_TRICKLE_TIME_MINUTES);
656 if (ret)
657 goto out;
658
659 ret = pm_chg_batt_temp_disable(0);
660 if (ret)
661 goto out;
662
663 if (pm8058_chg.voter == NULL)
664 pm8058_chg.voter = msm_xo_get(MSM_XO_TCXO_D1, "pm8058_charger");
665 msm_xo_mode_vote(pm8058_chg.voter, MSM_XO_MODE_ON);
666
667 ret = pm_chg_enum_done_enable(1);
668 if (ret)
669 goto out;
670
671 wmb();
672
673 ret = pm_chg_auto_disable(0);
674 if (ret)
675 goto out;
676
677 /* wait for the enable to update interrupt status*/
678 msleep(20);
679
680 pm8058_chg_enable_irq(AUTO_CHGFAIL_IRQ);
681 pm8058_chg_enable_irq(CHGHOT_IRQ);
682 pm8058_chg_enable_irq(AUTO_CHGDONE_IRQ);
683 pm8058_chg_enable_irq(CHG_END_IRQ);
684 pm8058_chg_enable_irq(CHGSTATE_IRQ);
685
686out:
687 return ret;
688}
689
690static void chg_done_cleanup(void)
691{
692 dev_info(pm8058_chg.dev, "%s notify pm8058 charging completion"
693 "\n", __func__);
694
695 pm8058_chg_disable_irq(AUTO_CHGDONE_IRQ);
696 cancel_delayed_work_sync(&pm8058_chg.veoc_begin_work);
697 cancel_delayed_work_sync(&pm8058_chg.check_vbat_low_work);
698
699 pm8058_chg_disable_irq(CHG_END_IRQ);
700
701 pm8058_chg_disable_irq(VBATDET_LOW_IRQ);
702 pm8058_chg_disable_irq(VBATDET_IRQ);
703 pm8058_chg.waiting_for_veoc = 0;
704 pm8058_chg.waiting_for_topoff = 0;
705
706 pm_chg_auto_disable(1);
707
708 msm_charger_notify_event(&usb_hw_chg, CHG_DONE_EVENT);
709}
710
711static void chg_done_check_work(struct work_struct *work)
712{
713 chg_done_cleanup();
714}
715
716static void charging_check_work(struct work_struct *work)
717{
718 uint32_t fsm_state = get_fsm_state();
719 int rc;
720
721 switch (fsm_state) {
722 /* We're charging, so disarm alarm */
723 case 2:
724 case 7:
725 case 8:
726 rc = pm8058_batt_alarm_state_set(0, 0);
727 if (rc)
728 dev_err(pm8058_chg.dev,
729 "%s: unable to set alarm state\n", __func__);
730 break;
731 default:
732 /* Still not charging, so update driver state */
733 chg_done_cleanup();
734 break;
735 };
736}
737
738static int pm8058_start_charging(struct msm_hardware_charger *hw_chg,
739 int chg_voltage, int chg_current)
740{
741 int vbat_higher_than_vbatdet;
742 int ret = 0;
743
744 cancel_delayed_work_sync(&pm8058_chg.charging_check_work);
745
746 /*
747 * adjust the max current for PC USB connection - set the higher limit
748 * to 450 and make sure we never cross it
749 */
750 if (chg_current == 500)
751 chg_current = 450;
752 pm8058_chg.current_charger_current = chg_current;
753 pm8058_chg_enable_irq(FASTCHG_IRQ);
754
755 ret = pm_chg_vmaxsel_set(chg_voltage);
756 if (ret)
757 goto out;
758
759 /* set vbat to CC to CV threshold */
760 ret = pm_chg_vbatdet_set(AUTO_CHARGING_VBATDET);
761 if (ret)
762 goto out;
763
764 pm8058_chg.vbatdet = AUTO_CHARGING_VBATDET;
765 /*
766 * get the state of vbat and if it is higher than
767 * AUTO_CHARGING_VBATDET we start the veoc start timer
768 * else wait for the voltage to go to AUTO_CHARGING_VBATDET
769 * and then start the 90 min timer
770 */
771 vbat_higher_than_vbatdet =
772 pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[VBATDET_IRQ]);
773 if (vbat_higher_than_vbatdet) {
774 /*
775 * we are in constant voltage phase of charging
776 * IEOC should happen withing 90 mins of this instant
777 * else we enable VEOC
778 */
779 dev_info(pm8058_chg.dev, "%s begin veoc timer\n", __func__);
780 schedule_delayed_work(&pm8058_chg.veoc_begin_work,
781 round_jiffies_relative(msecs_to_jiffies
782 (AUTO_CHARGING_VEOC_BEGIN_TIME_MS)));
783 } else
784 pm8058_chg_enable_irq(VBATDET_IRQ);
785
786 ret = __pm8058_start_charging(chg_current, AUTO_CHARGING_IEOC_ITERM,
787 AUTO_CHARGING_FAST_TIME_MAX_MINUTES);
788 pm8058_chg.current_charger_current = chg_current;
789
790 /*
791 * We want to check the FSM state to verify we're charging. We must
792 * wait before doing this to allow the VBATDET to settle. The worst
793 * case for this is two seconds. The batt alarm does not have this
794 * delay.
795 */
796 schedule_delayed_work(&pm8058_chg.charging_check_work,
797 round_jiffies_relative(msecs_to_jiffies
798 (AUTO_CHARGING_VBATDET_DEBOUNCE_TIME_MS)));
799
800out:
801 return ret;
802}
803
804static void veoc_begin_work(struct work_struct *work)
805{
806 /* we have been doing CV for 90mins with no signs of IEOC
807 * start checking for VEOC in addition with 16min charges*/
808 dev_info(pm8058_chg.dev, "%s begin veoc detection\n", __func__);
809 pm8058_chg.waiting_for_veoc = 1;
810 /*
811 * disable VBATDET irq we dont need it unless we are at the end of
812 * charge cycle
813 */
814 pm8058_chg_disable_irq(VBATDET_IRQ);
815 __pm8058_start_charging(pm8058_chg.current_charger_current,
816 AUTO_CHARGING_VEOC_ITERM,
817 AUTO_CHARGING_VEOC_TCHG);
818}
819
820static void vbat_low_work(struct work_struct *work)
821{
822 /*
823 * It has been one minute and the battery still holds voltage
824 * start the final topoff - charging is almost done
825 */
826 dev_info(pm8058_chg.dev, "%s vbatt maintains for a minute"
827 "starting topoff\n", __func__);
828 pm8058_chg.waiting_for_veoc = 0;
829 pm8058_chg.waiting_for_topoff = 1;
830 pm8058_chg_disable_irq(VBATDET_LOW_IRQ);
831 pm8058_chg_disable_irq(VBATDET_IRQ);
832 __pm8058_start_charging(pm8058_chg.current_charger_current,
833 AUTO_CHARGING_VEOC_ITERM,
834 AUTO_CHARGING_VEOC_TCHG_FINAL_CYCLE);
835}
836
837
838static irqreturn_t pm8058_chg_chgval_handler(int irq, void *dev_id)
839{
840 u8 old, temp;
841 int ret;
842
843 if (is_chg_plugged_in()) { /* this debounces it */
844 if (!pm8058_chg.present) {
845 msm_charger_notify_event(&usb_hw_chg,
846 CHG_INSERTED_EVENT);
847 pm8058_chg.present = 1;
848 }
849 } else {
850 if (pm8058_chg.present) {
851 ret = pm8058_read(pm8058_chg.pm_chip,
852 PM8058_OVP_TEST_REG,
853 &old, 1);
854 temp = old | BIT(FORCE_OVP_OFF);
855 ret = pm8058_write(pm8058_chg.pm_chip,
856 PM8058_OVP_TEST_REG,
857 &temp, 1);
858 temp = 0xFC;
859 ret = pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST,
860 &temp, 1);
861 /* 10 ms sleep is for the VCHG to discharge */
862 msleep(10);
863 temp = 0xF0;
864 ret = pm8058_write(pm8058_chg.pm_chip,
865 PM8058_CHG_TEST,
866 &temp, 1);
867 ret = pm8058_write(pm8058_chg.pm_chip,
868 PM8058_OVP_TEST_REG,
869 &old, 1);
870
871 pm_chg_enum_done_enable(0);
872 pm_chg_auto_disable(1);
873 msm_charger_notify_event(&usb_hw_chg,
874 CHG_REMOVED_EVENT);
875 pm8058_chg.present = 0;
876 }
877 }
878
879 return IRQ_HANDLED;
880}
881
882static irqreturn_t pm8058_chg_chginval_handler(int irq, void *dev_id)
883{
884 u8 old, temp;
885 int ret;
886
887 if (pm8058_chg.present) {
888 pm8058_chg_disable_irq(CHGINVAL_IRQ);
889
890 pm_chg_enum_done_enable(0);
891 pm_chg_auto_disable(1);
892 ret = pm8058_read(pm8058_chg.pm_chip,
893 PM8058_OVP_TEST_REG, &old, 1);
894 temp = old | BIT(FORCE_OVP_OFF);
895 ret = pm8058_write(pm8058_chg.pm_chip,
896 PM8058_OVP_TEST_REG, &temp, 1);
897 temp = 0xFC;
898 ret = pm8058_write(pm8058_chg.pm_chip,
899 PM8058_CHG_TEST, &temp, 1);
900 /* 10 ms sleep is for the VCHG to discharge */
901 msleep(10);
902 temp = 0xF0;
903 ret = pm8058_write(pm8058_chg.pm_chip,
904 PM8058_CHG_TEST, &temp, 1);
905 ret = pm8058_write(pm8058_chg.pm_chip,
906 PM8058_OVP_TEST_REG, &old, 1);
907
908 if (!is_chg_plugged_in()) {
909 msm_charger_notify_event(&usb_hw_chg,
910 CHG_REMOVED_EVENT);
911 pm8058_chg.present = 0;
912 } else {
913 /* was a fake */
914 pm8058_chg_enable_irq(CHGINVAL_IRQ);
915 }
916 }
917
918 return IRQ_HANDLED;
919}
920
921static irqreturn_t pm8058_chg_auto_chgdone_handler(int irq, void *dev_id)
922{
923 dev_info(pm8058_chg.dev, "%s waiting a sec to confirm\n",
924 __func__);
925 pm8058_chg_disable_irq(VBATDET_IRQ);
926 if (!delayed_work_pending(&pm8058_chg.chg_done_check_work)) {
927 schedule_delayed_work(&pm8058_chg.chg_done_check_work,
928 round_jiffies_relative(msecs_to_jiffies
929 (AUTO_CHARGING_DONE_CHECK_TIME_MS)));
930 }
931 return IRQ_HANDLED;
932}
933
934/* can only happen with the pmic charger when it has been charing
935 * for either 16 mins wating for VEOC or 32 mins for topoff
936 * without a IEOC indication */
937static irqreturn_t pm8058_chg_auto_chgfail_handler(int irq, void *dev_id)
938{
939 pm8058_chg_disable_irq(AUTO_CHGFAIL_IRQ);
940
941 if (pm8058_chg.waiting_for_topoff == 1) {
942 dev_info(pm8058_chg.dev, "%s topoff done, charging done\n",
943 __func__);
944 pm8058_chg.waiting_for_topoff = 0;
945 /* notify we are done charging */
946 msm_charger_notify_event(&usb_hw_chg, CHG_DONE_EVENT);
947 } else {
948 /* start one minute timer and monitor VBATDET_LOW */
949 dev_info(pm8058_chg.dev, "%s monitoring vbat_low for a"
950 "minute\n", __func__);
951 schedule_delayed_work(&pm8058_chg.check_vbat_low_work,
952 round_jiffies_relative(msecs_to_jiffies
953 (AUTO_CHARGING_VEOC_VBAT_LOW_CHECK_TIME_MS)));
954
955 /* note we are waiting on veoc */
956 pm8058_chg.waiting_for_veoc = 1;
957
958 pm_chg_vbatdet_set(AUTO_CHARGING_VEOC_VBATDET);
959 pm8058_chg.vbatdet = AUTO_CHARGING_VEOC_VBATDET;
960 pm8058_chg_enable_irq(VBATDET_LOW_IRQ);
961 }
962 return IRQ_HANDLED;
963}
964
965static irqreturn_t pm8058_chg_chgstate_handler(int irq, void *dev_id)
966{
967 u8 temp;
968
969 temp = 0x00;
970 if (!pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST_3, &temp, 1)) {
971 pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TEST_3, &temp, 1);
972 dev_dbg(pm8058_chg.dev, "%s state=%d\n", __func__, temp);
973 }
974 return IRQ_HANDLED;
975}
976
977static irqreturn_t pm8058_chg_fastchg_handler(int irq, void *dev_id)
978{
979 pm8058_chg_disable_irq(FASTCHG_IRQ);
980
981 /* we have begun the fast charging state */
982 dev_info(pm8058_chg.dev, "%s begin fast charging"
983 , __func__);
984 msm_charger_notify_event(&usb_hw_chg, CHG_BATT_BEGIN_FAST_CHARGING);
985 return IRQ_HANDLED;
986}
987
988static irqreturn_t pm8058_chg_batttemp_handler(int irq, void *dev_id)
989{
990 int ret;
991
992 /* we could get temperature
993 * interrupt when the battery is plugged out
994 */
995 ret = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATTCONNECT_IRQ]);
996 if (ret) {
997 msm_charger_notify_event(&usb_hw_chg, CHG_BATT_REMOVED);
998 } else {
999 /* read status to determine we are inrange or outofrange */
1000 ret =
1001 pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATTTEMP_IRQ]);
1002 if (ret)
1003 msm_charger_notify_event(&usb_hw_chg,
1004 CHG_BATT_TEMP_OUTOFRANGE);
1005 else
1006 msm_charger_notify_event(&usb_hw_chg,
1007 CHG_BATT_TEMP_INRANGE);
1008 }
1009
1010 return IRQ_HANDLED;
1011}
1012
1013static irqreturn_t pm8058_chg_vbatdet_handler(int irq, void *dev_id)
1014{
1015 int ret;
1016
1017 /* settling time */
1018 msleep(20);
1019 ret = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[VBATDET_IRQ]);
1020
1021 if (ret) {
1022 if (pm8058_chg.vbatdet == AUTO_CHARGING_VBATDET
1023 && !delayed_work_pending(&pm8058_chg.veoc_begin_work)) {
1024 /*
1025 * we are in constant voltage phase of charging
1026 * IEOC should happen withing 90 mins of this instant
1027 * else we enable VEOC
1028 */
1029 dev_info(pm8058_chg.dev, "%s entered constant voltage"
1030 "begin veoc timer\n", __func__);
1031 schedule_delayed_work(&pm8058_chg.veoc_begin_work,
1032 round_jiffies_relative
1033 (msecs_to_jiffies
1034 (AUTO_CHARGING_VEOC_BEGIN_TIME_MS)));
1035 }
1036 } else {
1037 if (pm8058_chg.vbatdet == AUTO_CHARGING_VEOC_VBATDET) {
1038 cancel_delayed_work_sync(
1039 &pm8058_chg.check_vbat_low_work);
1040
1041 if (pm8058_chg.waiting_for_topoff
1042 || pm8058_chg.waiting_for_veoc) {
1043 /*
1044 * the battery dropped its voltage below 4100
1045 * around a minute charge the battery for 16
1046 * mins and check vbat again for a minute
1047 */
1048 dev_info(pm8058_chg.dev, "%s batt dropped vlt"
1049 "within a minute\n", __func__);
1050 pm8058_chg.waiting_for_topoff = 0;
1051 pm8058_chg.waiting_for_veoc = 1;
1052 pm8058_chg_disable_irq(VBATDET_IRQ);
1053 __pm8058_start_charging(pm8058_chg.
1054 current_charger_current,
1055 AUTO_CHARGING_VEOC_ITERM,
1056 AUTO_CHARGING_VEOC_TCHG);
1057 }
1058 }
1059 }
1060 return IRQ_HANDLED;
1061}
1062
1063static irqreturn_t pm8058_chg_batt_replace_handler(int irq, void *dev_id)
1064{
1065 int ret;
1066
1067 pm8058_chg_disable_irq(BATT_REPLACE_IRQ);
1068 ret = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATT_REPLACE_IRQ]);
1069 if (ret) {
1070 msm_charger_notify_event(&usb_hw_chg, CHG_BATT_INSERTED);
1071 /*
1072 * battery is present enable batt removal
1073 * and batt temperatture interrupt
1074 */
1075 pm8058_chg_enable_irq(BATTCONNECT_IRQ);
1076 }
1077 return IRQ_HANDLED;
1078}
1079
1080static irqreturn_t pm8058_chg_battconnect_handler(int irq, void *dev_id)
1081{
1082 int ret;
1083
1084 ret = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[BATTCONNECT_IRQ]);
1085 if (ret) {
1086 msm_charger_notify_event(&usb_hw_chg, CHG_BATT_REMOVED);
1087 } else {
1088 msm_charger_notify_event(&usb_hw_chg, CHG_BATT_INSERTED);
1089 pm8058_chg_enable_irq(BATTTEMP_IRQ);
1090 }
1091
1092 return IRQ_HANDLED;
1093}
1094
1095static int get_rt_status(void *data, u64 * val)
1096{
1097 int i = (int)data;
1098 int ret;
1099
1100 ret = pm_chg_get_rt_status(i);
1101 *val = ret;
1102 return 0;
1103}
1104
1105DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
1106DEFINE_SIMPLE_ATTRIBUTE(fsm_fops, get_fsm_status, NULL, "%llu\n");
1107
1108static void free_irqs(void)
1109{
1110 int i;
1111
1112 for (i = 0; i < PMIC_CHG_MAX_INTS; i++)
1113 if (pm8058_chg.pmic_chg_irq[i]) {
1114 free_irq(pm8058_chg.pmic_chg_irq[i], NULL);
1115 pm8058_chg.pmic_chg_irq[i] = 0;
1116 }
1117}
1118
1119static int __devinit request_irqs(struct platform_device *pdev)
1120{
1121 struct resource *res;
1122 int ret;
1123
1124 ret = 0;
1125 bitmap_fill(pm8058_chg.enabled_irqs, PMIC_CHG_MAX_INTS);
1126
1127 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "CHGVAL");
1128 if (res == NULL) {
1129 dev_err(pm8058_chg.dev,
1130 "%s:couldnt find resource CHGVAL\n", __func__);
1131 goto err_out;
1132 } else {
1133 ret = request_threaded_irq(res->start, NULL,
1134 pm8058_chg_chgval_handler,
1135 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1136 res->name, NULL);
1137 if (ret < 0) {
1138 dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n",
1139 __func__, res->start, ret);
1140 goto err_out;
1141 } else {
1142 pm8058_chg.pmic_chg_irq[CHGVAL_IRQ] = res->start;
1143 pm8058_chg_disable_irq(CHGVAL_IRQ);
1144 enable_irq_wake(pm8058_chg.pmic_chg_irq[CHGVAL_IRQ]);
1145 }
1146 }
1147
1148 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "CHGINVAL");
1149 if (res == NULL) {
1150 dev_err(pm8058_chg.dev,
1151 "%s:couldnt find resource CHGINVAL\n", __func__);
1152 goto err_out;
1153 } else {
1154 ret = request_threaded_irq(res->start, NULL,
1155 pm8058_chg_chginval_handler,
1156 IRQF_TRIGGER_RISING, res->name, NULL);
1157 if (ret < 0) {
1158 dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n",
1159 __func__, res->start, ret);
1160 goto err_out;
1161 } else {
1162 pm8058_chg.pmic_chg_irq[CHGINVAL_IRQ] = res->start;
1163 pm8058_chg_disable_irq(CHGINVAL_IRQ);
1164 }
1165 }
1166
1167 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1168 "AUTO_CHGDONE");
1169 if (res == NULL) {
1170 dev_err(pm8058_chg.dev,
1171 "%s:couldnt find resource AUTO_CHGDONE\n", __func__);
1172 goto err_out;
1173 } else {
1174 ret = request_threaded_irq(res->start, NULL,
1175 pm8058_chg_auto_chgdone_handler,
1176 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1177 res->name, NULL);
1178 if (ret < 0) {
1179 dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n",
1180 __func__, res->start, ret);
1181 goto err_out;
1182 } else {
1183 pm8058_chg.pmic_chg_irq[AUTO_CHGDONE_IRQ] = res->start;
1184 pm8058_chg_disable_irq(AUTO_CHGDONE_IRQ);
1185 }
1186 }
1187
1188 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1189 "AUTO_CHGFAIL");
1190 if (res == NULL) {
1191 dev_err(pm8058_chg.dev,
1192 "%s:couldnt find resource AUTO_CHGFAIL\n", __func__);
1193 goto err_out;
1194 } else {
1195 ret = request_threaded_irq(res->start, NULL,
1196 pm8058_chg_auto_chgfail_handler,
1197 IRQF_TRIGGER_RISING, res->name, NULL);
1198 if (ret < 0) {
1199 dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n",
1200 __func__, res->start, ret);
1201 goto err_out;
1202 } else {
1203 pm8058_chg.pmic_chg_irq[AUTO_CHGFAIL_IRQ] = res->start;
1204 pm8058_chg_disable_irq(AUTO_CHGFAIL_IRQ);
1205 }
1206 }
1207
1208 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "CHGSTATE");
1209 if (res == NULL) {
1210 dev_err(pm8058_chg.dev,
1211 "%s:couldnt find resource CHGSTATE\n", __func__);
1212 goto err_out;
1213 } else {
1214 ret = request_threaded_irq(res->start, NULL,
1215 pm8058_chg_chgstate_handler,
1216 IRQF_TRIGGER_RISING, res->name, NULL);
1217 if (ret < 0) {
1218 dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n",
1219 __func__, res->start, ret);
1220 goto err_out;
1221 } else {
1222 pm8058_chg.pmic_chg_irq[CHGSTATE_IRQ] = res->start;
1223 pm8058_chg_disable_irq(CHGSTATE_IRQ);
1224 }
1225 }
1226
1227 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "FASTCHG");
1228 if (res == NULL) {
1229 dev_err(pm8058_chg.dev,
1230 "%s:couldnt find resource FASTCHG\n", __func__);
1231 goto err_out;
1232 } else {
1233 ret = request_threaded_irq(res->start, NULL,
1234 pm8058_chg_fastchg_handler,
1235 IRQF_TRIGGER_RISING, res->name, NULL);
1236 if (ret < 0) {
1237 dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n",
1238 __func__, res->start, ret);
1239 goto err_out;
1240 } else {
1241 pm8058_chg.pmic_chg_irq[FASTCHG_IRQ] = res->start;
1242 pm8058_chg_disable_irq(FASTCHG_IRQ);
1243 }
1244 }
1245
1246 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "BATTTEMP");
1247 if (res == NULL) {
1248 dev_err(pm8058_chg.dev,
1249 "%s:couldnt find resource CHG_END\n", __func__);
1250 goto err_out;
1251 } else {
1252 ret = request_threaded_irq(res->start, NULL,
1253 pm8058_chg_batttemp_handler,
1254 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1255 res->name, NULL);
1256 if (ret < 0) {
1257 dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n",
1258 __func__, res->start, ret);
1259 goto err_out;
1260 } else {
1261 pm8058_chg.pmic_chg_irq[BATTTEMP_IRQ] = res->start;
1262 pm8058_chg_disable_irq(BATTTEMP_IRQ);
1263 }
1264 }
1265
1266 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
1267 "BATT_REPLACE");
1268 if (res == NULL) {
1269 dev_err(pm8058_chg.dev,
1270 "%s:couldnt find resource BATT_REPLACE\n", __func__);
1271 goto err_out;
1272 } else {
1273 ret = request_threaded_irq(res->start, NULL,
1274 pm8058_chg_batt_replace_handler,
1275 IRQF_TRIGGER_RISING, res->name, NULL);
1276 if (ret < 0) {
1277 dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n",
1278 __func__, res->start, ret);
1279 goto err_out;
1280 } else {
1281 pm8058_chg.pmic_chg_irq[BATT_REPLACE_IRQ] = res->start;
1282 pm8058_chg_disable_irq(BATT_REPLACE_IRQ);
1283 }
1284 }
1285
1286 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "BATTCONNECT");
1287 if (res == NULL) {
1288 dev_err(pm8058_chg.dev,
1289 "%s:couldnt find resource BATTCONNECT\n", __func__);
1290 goto err_out;
1291 } else {
1292 ret = request_threaded_irq(res->start, NULL,
1293 pm8058_chg_battconnect_handler,
1294 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1295 res->name, NULL);
1296 if (ret < 0) {
1297 dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n",
1298 __func__, res->start, ret);
1299 goto err_out;
1300 } else {
1301 pm8058_chg.pmic_chg_irq[BATTCONNECT_IRQ] = res->start;
1302 pm8058_chg_disable_irq(BATTCONNECT_IRQ);
1303 }
1304 }
1305
1306 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "VBATDET");
1307 if (res == NULL) {
1308 dev_err(pm8058_chg.dev,
1309 "%s:couldnt find resource VBATDET\n", __func__);
1310 goto err_out;
1311 } else {
1312 ret = request_threaded_irq(res->start, NULL,
1313 pm8058_chg_vbatdet_handler,
1314 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1315 res->name, NULL);
1316 if (ret < 0) {
1317 dev_err(pm8058_chg.dev, "%s:couldnt request %d %d\n",
1318 __func__, res->start, ret);
1319 goto err_out;
1320 } else {
1321 pm8058_chg.pmic_chg_irq[VBATDET_IRQ] = res->start;
1322 pm8058_chg_disable_irq(VBATDET_IRQ);
1323 }
1324 }
1325
1326 return 0;
1327
1328err_out:
1329 free_irqs();
1330 return -EINVAL;
1331}
1332
1333static int pm8058_get_charge_batt(void)
1334{
1335 u8 temp;
1336 int rc;
1337
1338 rc = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1);
1339 if (rc)
1340 return rc;
1341
1342 temp &= BIT(CHG_CHARGE_BAT);
1343 if (temp)
1344 temp = 1;
1345 return temp;
1346}
1347EXPORT_SYMBOL(pm8058_get_charge_batt);
1348
1349static int pm8058_set_charge_batt(int on)
1350{
1351 u8 temp;
1352 int rc;
1353
1354 rc = pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1);
1355 if (rc)
1356 return rc;
1357 if (on)
1358 temp |= BIT(CHG_CHARGE_BAT);
1359 else
1360 temp &= ~BIT(CHG_CHARGE_BAT);
1361
1362 return pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_CNTRL, &temp, 1);
1363
1364}
1365EXPORT_SYMBOL(pm8058_set_charge_batt);
1366
1367static int get_charge_batt(void *data, u64 * val)
1368{
1369 int ret;
1370
1371 ret = pm8058_get_charge_batt();
1372 if (ret < 0)
1373 return ret;
1374
1375 *val = ret;
1376 return 0;
1377}
1378
1379static int set_charge_batt(void *data, u64 val)
1380{
1381 return pm8058_set_charge_batt(val);
1382}
1383DEFINE_SIMPLE_ATTRIBUTE(fet_fops, get_charge_batt, set_charge_batt, "%llu\n");
1384
1385static void pm8058_chg_determine_initial_state(void)
1386{
1387 if (is_chg_plugged_in()) {
1388 pm8058_chg.present = 1;
1389 msm_charger_notify_event(&usb_hw_chg, CHG_INSERTED_EVENT);
1390 dev_info(pm8058_chg.dev, "%s charger present\n", __func__);
1391 } else {
1392 pm8058_chg.present = 0;
1393 dev_info(pm8058_chg.dev, "%s charger absent\n", __func__);
1394 }
1395 pm8058_chg_enable_irq(CHGVAL_IRQ);
1396}
1397
1398static int pm8058_stop_charging(struct msm_hardware_charger *hw_chg)
1399{
1400 int ret;
1401
1402 dev_info(pm8058_chg.dev, "%s stopping charging\n", __func__);
1403 cancel_delayed_work_sync(&pm8058_chg.veoc_begin_work);
1404 cancel_delayed_work_sync(&pm8058_chg.check_vbat_low_work);
1405 cancel_delayed_work_sync(&pm8058_chg.chg_done_check_work);
1406 cancel_delayed_work_sync(&pm8058_chg.charging_check_work);
1407
1408 ret = pm_chg_get_rt_status(pm8058_chg.pmic_chg_irq[FASTCHG_IRQ]);
1409 if (ret == 1)
1410 pm_chg_suspend(1);
1411 else
1412 dev_err(pm8058_chg.dev,
1413 "%s called when not fast-charging\n", __func__);
1414
1415 pm_chg_failed_clear(1);
1416
1417 pm8058_chg.waiting_for_veoc = 0;
1418 pm8058_chg.waiting_for_topoff = 0;
1419
1420 /* disable the irqs enabled while charging */
1421 pm8058_chg_disable_irq(AUTO_CHGFAIL_IRQ);
1422 pm8058_chg_disable_irq(CHGHOT_IRQ);
1423 pm8058_chg_disable_irq(AUTO_CHGDONE_IRQ);
1424 pm8058_chg_disable_irq(FASTCHG_IRQ);
1425 pm8058_chg_disable_irq(CHG_END_IRQ);
1426 pm8058_chg_disable_irq(VBATDET_IRQ);
1427 pm8058_chg_disable_irq(VBATDET_LOW_IRQ);
1428 if (pm8058_chg.voter)
1429 msm_xo_mode_vote(pm8058_chg.voter, MSM_XO_MODE_OFF);
1430
1431 return 0;
1432}
1433
1434static int get_status(void *data, u64 * val)
1435{
1436 *val = pm8058_chg.current_charger_current;
1437 return 0;
1438}
1439
1440static int set_status(void *data, u64 val)
1441{
1442
1443 pm8058_chg.current_charger_current = val;
1444 if (pm8058_chg.current_charger_current)
1445 pm8058_start_charging(NULL,
1446 AUTO_CHARGING_VMAXSEL,
1447 pm8058_chg.current_charger_current);
1448 else
1449 pm8058_stop_charging(NULL);
1450 return 0;
1451}
1452DEFINE_SIMPLE_ATTRIBUTE(chg_fops, get_status, set_status, "%llu\n");
1453
1454static int set_disable_status_param(const char *val, struct kernel_param *kp)
1455{
1456 int ret;
1457
1458 ret = param_set_int(val, kp);
1459 if (ret)
1460 return ret;
1461
1462 if (pm8058_chg.inited && pm8058_chg.disabled) {
1463 /*
1464 * stop_charging is called during usb suspend
1465 * act as the usb is removed by disabling auto and enum
1466 */
1467 pm_chg_enum_done_enable(0);
1468 pm_chg_auto_disable(1);
1469 pm8058_stop_charging(NULL);
1470 }
1471 return 0;
1472}
1473module_param_call(disabled, set_disable_status_param, param_get_uint,
1474 &(pm8058_chg.disabled), 0644);
1475
1476static int pm8058_charging_switched(struct msm_hardware_charger *hw_chg)
1477{
1478 u8 temp;
1479
1480 temp = 0xA3;
1481 pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST_2, &temp, 1);
1482 temp = 0x84;
1483 pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST_2, &temp, 1);
1484 msleep(2);
1485 temp = 0x80;
1486 pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST_2, &temp, 1);
1487 return 0;
1488}
1489
1490static int get_reg(void *data, u64 * val)
1491{
1492 int i = (int)data;
1493 int ret;
1494 u8 temp;
1495
1496 ret = pm8058_read(pm8058_chg.pm_chip, i, &temp, 1);
1497 if (ret)
1498 return -EAGAIN;
1499 *val = temp;
1500 return 0;
1501}
1502
1503static int set_reg(void *data, u64 val)
1504{
1505 int i = (int)data;
1506 int ret;
1507 u8 temp;
1508
1509 temp = (u8) val;
1510 ret = pm8058_write(pm8058_chg.pm_chip, i, &temp, 1);
1511 mb();
1512 if (ret)
1513 return -EAGAIN;
1514 return 0;
1515}
1516
1517DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "%llu\n");
1518
1519#ifdef CONFIG_DEBUG_FS
1520static void create_debugfs_entries(void)
1521{
1522 pm8058_chg.dent = debugfs_create_dir("pm8058_usb_chg", NULL);
1523
1524 if (IS_ERR(pm8058_chg.dent)) {
1525 pr_err("pmic charger couldnt create debugfs dir\n");
1526 return;
1527 }
1528
1529 debugfs_create_file("CHG_CNTRL", 0644, pm8058_chg.dent,
1530 (void *)PM8058_CHG_CNTRL, &reg_fops);
1531 debugfs_create_file("CHG_CNTRL_2", 0644, pm8058_chg.dent,
1532 (void *)PM8058_CHG_CNTRL_2, &reg_fops);
1533 debugfs_create_file("CHG_VMAX_SEL", 0644, pm8058_chg.dent,
1534 (void *)PM8058_CHG_VMAX_SEL, &reg_fops);
1535 debugfs_create_file("CHG_VBAT_DET", 0644, pm8058_chg.dent,
1536 (void *)PM8058_CHG_VBAT_DET, &reg_fops);
1537 debugfs_create_file("CHG_IMAX", 0644, pm8058_chg.dent,
1538 (void *)PM8058_CHG_IMAX, &reg_fops);
1539 debugfs_create_file("CHG_TRICKLE", 0644, pm8058_chg.dent,
1540 (void *)PM8058_CHG_TRICKLE, &reg_fops);
1541 debugfs_create_file("CHG_ITERM", 0644, pm8058_chg.dent,
1542 (void *)PM8058_CHG_ITERM, &reg_fops);
1543 debugfs_create_file("CHG_TTRKL_MAX", 0644, pm8058_chg.dent,
1544 (void *)PM8058_CHG_TTRKL_MAX, &reg_fops);
1545 debugfs_create_file("CHG_TCHG_MAX", 0644, pm8058_chg.dent,
1546 (void *)PM8058_CHG_TCHG_MAX, &reg_fops);
1547 debugfs_create_file("CHG_TEMP_THRESH", 0644, pm8058_chg.dent,
1548 (void *)PM8058_CHG_TEMP_THRESH, &reg_fops);
1549 debugfs_create_file("CHG_TEMP_REG", 0644, pm8058_chg.dent,
1550 (void *)PM8058_CHG_TEMP_REG, &reg_fops);
1551
1552 debugfs_create_file("FSM_STATE", 0644, pm8058_chg.dent, NULL,
1553 &fsm_fops);
1554
1555 debugfs_create_file("stop", 0644, pm8058_chg.dent, NULL,
1556 &chg_fops);
1557
1558 if (pm8058_chg.pmic_chg_irq[CHGVAL_IRQ])
1559 debugfs_create_file("CHGVAL", 0444, pm8058_chg.dent,
1560 (void *)pm8058_chg.pmic_chg_irq[CHGVAL_IRQ],
1561 &rt_fops);
1562
1563 if (pm8058_chg.pmic_chg_irq[CHGINVAL_IRQ])
1564 debugfs_create_file("CHGINVAL", 0444, pm8058_chg.dent, (void *)
1565 pm8058_chg.pmic_chg_irq[CHGINVAL_IRQ],
1566 &rt_fops);
1567 if (pm8058_chg.pmic_chg_irq[CHGILIM_IRQ])
1568 debugfs_create_file("CHGILIM", 0444, pm8058_chg.dent, (void *)
1569 pm8058_chg.pmic_chg_irq[CHGILIM_IRQ],
1570 &rt_fops);
1571 if (pm8058_chg.pmic_chg_irq[VCP_IRQ])
1572 debugfs_create_file("VCP", 0444, pm8058_chg.dent,
1573 (void *)pm8058_chg.pmic_chg_irq[VCP_IRQ],
1574 &rt_fops);
1575 if (pm8058_chg.pmic_chg_irq[ATC_DONE_IRQ])
1576 debugfs_create_file("ATC_DONE", 0444, pm8058_chg.dent, (void *)
1577 pm8058_chg.pmic_chg_irq[ATC_DONE_IRQ],
1578 &rt_fops);
1579 if (pm8058_chg.pmic_chg_irq[ATCFAIL_IRQ])
1580 debugfs_create_file("ATCFAIL", 0444, pm8058_chg.dent, (void *)
1581 pm8058_chg.pmic_chg_irq[ATCFAIL_IRQ],
1582 &rt_fops);
1583 if (pm8058_chg.pmic_chg_irq[AUTO_CHGDONE_IRQ])
1584 debugfs_create_file("AUTO_CHGDONE", 0444, pm8058_chg.dent,
1585 (void *)
1586 pm8058_chg.pmic_chg_irq[AUTO_CHGDONE_IRQ],
1587 &rt_fops);
1588 if (pm8058_chg.pmic_chg_irq[AUTO_CHGFAIL_IRQ])
1589 debugfs_create_file("AUTO_CHGFAIL", 0444, pm8058_chg.dent,
1590 (void *)
1591 pm8058_chg.pmic_chg_irq[AUTO_CHGFAIL_IRQ],
1592 &rt_fops);
1593 if (pm8058_chg.pmic_chg_irq[CHGSTATE_IRQ])
1594 debugfs_create_file("CHGSTATE", 0444, pm8058_chg.dent, (void *)
1595 pm8058_chg.pmic_chg_irq[CHGSTATE_IRQ],
1596 &rt_fops);
1597 if (pm8058_chg.pmic_chg_irq[FASTCHG_IRQ])
1598 debugfs_create_file("FASTCHG", 0444, pm8058_chg.dent, (void *)
1599 pm8058_chg.pmic_chg_irq[FASTCHG_IRQ],
1600 &rt_fops);
1601 if (pm8058_chg.pmic_chg_irq[CHG_END_IRQ])
1602 debugfs_create_file("CHG_END", 0444, pm8058_chg.dent, (void *)
1603 pm8058_chg.pmic_chg_irq[CHG_END_IRQ],
1604 &rt_fops);
1605 if (pm8058_chg.pmic_chg_irq[BATTTEMP_IRQ])
1606 debugfs_create_file("BATTTEMP", 0444, pm8058_chg.dent, (void *)
1607 pm8058_chg.pmic_chg_irq[BATTTEMP_IRQ],
1608 &rt_fops);
1609 if (pm8058_chg.pmic_chg_irq[CHGHOT_IRQ])
1610 debugfs_create_file("CHGHOT", 0444, pm8058_chg.dent,
1611 (void *)pm8058_chg.pmic_chg_irq[CHGHOT_IRQ],
1612 &rt_fops);
1613 if (pm8058_chg.pmic_chg_irq[CHGTLIMIT_IRQ])
1614 debugfs_create_file("CHGTLIMIT", 0444, pm8058_chg.dent, (void *)
1615 pm8058_chg.pmic_chg_irq[CHGTLIMIT_IRQ],
1616 &rt_fops);
1617 if (pm8058_chg.pmic_chg_irq[CHG_GONE_IRQ])
1618 debugfs_create_file("CHG_GONE", 0444, pm8058_chg.dent, (void *)
1619 pm8058_chg.pmic_chg_irq[CHG_GONE_IRQ],
1620 &rt_fops);
1621 if (pm8058_chg.pmic_chg_irq[VCPMAJOR_IRQ])
1622 debugfs_create_file("VCPMAJOR", 0444, pm8058_chg.dent, (void *)
1623 pm8058_chg.pmic_chg_irq[VCPMAJOR_IRQ],
1624 &rt_fops);
1625 if (pm8058_chg.pmic_chg_irq[BATFET_IRQ])
1626 debugfs_create_file("BATFET", 0444, pm8058_chg.dent,
1627 (void *)pm8058_chg.pmic_chg_irq[BATFET_IRQ],
1628 &rt_fops);
1629 if (pm8058_chg.pmic_chg_irq[BATT_REPLACE_IRQ])
1630 debugfs_create_file("BATT_REPLACE", 0444, pm8058_chg.dent,
1631 (void *)
1632 pm8058_chg.pmic_chg_irq[BATT_REPLACE_IRQ],
1633 &rt_fops);
1634 if (pm8058_chg.pmic_chg_irq[BATTCONNECT_IRQ])
1635 debugfs_create_file("BATTCONNECT", 0444, pm8058_chg.dent,
1636 (void *)
1637 pm8058_chg.pmic_chg_irq[BATTCONNECT_IRQ],
1638 &rt_fops);
1639 if (pm8058_chg.pmic_chg_irq[VBATDET_IRQ])
1640 debugfs_create_file("VBATDET", 0444, pm8058_chg.dent, (void *)
1641 pm8058_chg.pmic_chg_irq[VBATDET_IRQ],
1642 &rt_fops);
1643 if (pm8058_chg.pmic_chg_irq[VBATDET_LOW_IRQ])
1644 debugfs_create_file("VBATDET_LOW", 0444, pm8058_chg.dent,
1645 (void *)
1646 pm8058_chg.pmic_chg_irq[VBATDET_LOW_IRQ],
1647 &rt_fops);
1648 debugfs_create_file("CHARGE_BATT", 0444, pm8058_chg.dent,
1649 NULL,
1650 &fet_fops);
1651}
1652#else
1653static inline void create_debugfs_entries(void)
1654{
1655}
1656#endif
1657
1658static void remove_debugfs_entries(void)
1659{
1660 debugfs_remove_recursive(pm8058_chg.dent);
1661}
1662
1663static struct msm_hardware_charger usb_hw_chg = {
Abhijeet Dharmapurikar0758e1e2011-07-14 18:11:41 -07001664 .type = CHG_TYPE_USB,
1665 .rating = 1,
1666 .name = "pm8058-usb",
1667 .start_charging = pm8058_start_charging,
1668 .stop_charging = pm8058_stop_charging,
1669 .charging_switched = pm8058_charging_switched,
1670 .start_system_current = pm8058_start_system_current,
1671 .stop_system_current = pm8058_stop_system_current,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001672};
1673
1674static int batt_read_adc(int channel, int *mv_reading)
1675{
1676 int ret;
1677 void *h;
1678 struct adc_chan_result adc_chan_result;
1679 struct completion conv_complete_evt;
1680
1681 pr_debug("%s: called for %d\n", __func__, channel);
1682 ret = adc_channel_open(channel, &h);
1683 if (ret) {
1684 pr_err("%s: couldnt open channel %d ret=%d\n",
1685 __func__, channel, ret);
1686 goto out;
1687 }
1688 init_completion(&conv_complete_evt);
1689 ret = adc_channel_request_conv(h, &conv_complete_evt);
1690 if (ret) {
1691 pr_err("%s: couldnt request conv channel %d ret=%d\n",
1692 __func__, channel, ret);
1693 goto out;
1694 }
1695 wait_for_completion(&conv_complete_evt);
1696 ret = adc_channel_read_result(h, &adc_chan_result);
1697 if (ret) {
1698 pr_err("%s: couldnt read result channel %d ret=%d\n",
1699 __func__, channel, ret);
1700 goto out;
1701 }
1702 ret = adc_channel_close(h);
1703 if (ret) {
1704 pr_err("%s: couldnt close channel %d ret=%d\n",
1705 __func__, channel, ret);
1706 }
1707 if (mv_reading)
1708 *mv_reading = adc_chan_result.measurement;
1709
1710 pr_debug("%s: done for %d\n", __func__, channel);
1711 return adc_chan_result.physical;
1712out:
1713 pr_debug("%s: done for %d\n", __func__, channel);
1714 return -EINVAL;
1715
1716}
1717
1718#define BATT_THERM_OPEN_MV 2000
1719static int pm8058_is_battery_present(void)
1720{
1721 int mv_reading;
1722
1723 mv_reading = 0;
1724 batt_read_adc(CHANNEL_ADC_BATT_THERM, &mv_reading);
1725 pr_debug("%s: therm_raw is %d\n", __func__, mv_reading);
1726 if (mv_reading > 0 && mv_reading < BATT_THERM_OPEN_MV)
1727 return 1;
1728
1729 return 0;
1730}
1731
1732static int pm8058_get_battery_temperature(void)
1733{
1734 return batt_read_adc(CHANNEL_ADC_BATT_THERM, NULL);
1735}
1736
1737#define BATT_THERM_OPERATIONAL_MAX_CELCIUS 40
1738#define BATT_THERM_OPERATIONAL_MIN_CELCIUS 0
1739static int pm8058_is_battery_temp_within_range(void)
1740{
1741 int therm_celcius;
1742
1743 therm_celcius = pm8058_get_battery_temperature();
1744 pr_debug("%s: therm_celcius is %d\n", __func__, therm_celcius);
1745 if (therm_celcius > 0
1746 && therm_celcius > BATT_THERM_OPERATIONAL_MIN_CELCIUS
1747 && therm_celcius < BATT_THERM_OPERATIONAL_MAX_CELCIUS)
1748 return 1;
1749
1750 return 0;
1751}
1752
1753#define BATT_ID_MAX_MV 800
1754#define BATT_ID_MIN_MV 600
1755static int pm8058_is_battery_id_valid(void)
1756{
1757 int batt_id_mv;
1758
1759 batt_id_mv = batt_read_adc(CHANNEL_ADC_BATT_ID, NULL);
1760 pr_debug("%s: batt_id_mv is %d\n", __func__, batt_id_mv);
1761
1762 /*
1763 * The readings are not in range
1764 * assume battery is present for now
1765 */
1766 return 1;
1767
1768 if (batt_id_mv > 0
1769 && batt_id_mv > BATT_ID_MIN_MV
1770 && batt_id_mv < BATT_ID_MAX_MV)
1771 return 1;
1772
1773 return 0;
1774}
1775
1776/* returns voltage in mV */
1777static int pm8058_get_battery_mvolts(void)
1778{
1779 int vbatt_mv;
1780
1781 vbatt_mv = batt_read_adc(CHANNEL_ADC_VBATT, NULL);
1782 pr_debug("%s: vbatt_mv is %d\n", __func__, vbatt_mv);
1783 if (vbatt_mv > 0)
1784 return vbatt_mv;
1785 /*
1786 * return 0 to tell the upper layers
1787 * we couldnt read the battery voltage
1788 */
1789 return 0;
1790}
1791
1792static int msm_battery_gauge_alarm_notify(struct notifier_block *nb,
1793 unsigned long status, void *unused)
1794{
1795 int rc;
1796
1797 pr_info("%s: status: %lu\n", __func__, status);
1798
1799 switch (status) {
1800 case 0:
1801 dev_err(pm8058_chg.dev,
1802 "%s: spurious interrupt\n", __func__);
1803 break;
1804 /* expected case - trip of low threshold */
1805 case 1:
1806 rc = pm8058_batt_alarm_state_set(0, 0);
1807 if (rc)
1808 dev_err(pm8058_chg.dev,
1809 "%s: unable to set alarm state\n", __func__);
1810 msm_charger_notify_event(NULL, CHG_BATT_NEEDS_RECHARGING);
1811 break;
1812 case 2:
1813 dev_err(pm8058_chg.dev,
1814 "%s: trip of high threshold\n", __func__);
1815 break;
1816 default:
1817 dev_err(pm8058_chg.dev,
1818 "%s: error received\n", __func__);
1819 };
1820
1821 return 0;
1822}
1823
1824static int pm8058_monitor_for_recharging(void)
1825{
1826 /* enable low comparator */
1827 return pm8058_batt_alarm_state_set(1, 0);
1828}
1829
1830static struct msm_battery_gauge pm8058_batt_gauge = {
1831 .get_battery_mvolts = pm8058_get_battery_mvolts,
1832 .get_battery_temperature = pm8058_get_battery_temperature,
1833 .is_battery_present = pm8058_is_battery_present,
1834 .is_battery_temp_within_range = pm8058_is_battery_temp_within_range,
1835 .is_battery_id_valid = pm8058_is_battery_id_valid,
1836 .monitor_for_recharging = pm8058_monitor_for_recharging,
1837};
1838
1839static int pm8058_usb_voltage_lower_limit(void)
1840{
1841 u8 temp, old;
1842 int ret = 0;
1843
1844 temp = 0x10;
1845 ret |= pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST, &temp, 1);
1846 ret |= pm8058_read(pm8058_chg.pm_chip, PM8058_CHG_TEST, &old, 1);
1847 old = old & ~BIT(IGNORE_LL);
1848 temp = 0x90 | (0xF & old);
1849 ret |= pm8058_write(pm8058_chg.pm_chip, PM8058_CHG_TEST, &temp, 1);
1850
1851 return ret;
1852}
1853
1854static int __devinit pm8058_charger_probe(struct platform_device *pdev)
1855{
1856 struct pm8058_chip *pm_chip;
1857 int rc = 0;
1858
1859 pm_chip = dev_get_drvdata(pdev->dev.parent);
1860 if (pm_chip == NULL) {
1861 pr_err("%s:no parent data passed in.\n", __func__);
1862 return -EFAULT;
1863 }
1864
1865 pm8058_chg.pm_chip = pm_chip;
1866 pm8058_chg.pdata = pdev->dev.platform_data;
1867 pm8058_chg.dev = &pdev->dev;
1868
1869 rc = request_irqs(pdev);
1870 if (rc) {
1871 pr_err("%s: couldnt register interrupts\n", __func__);
1872 goto out;
1873 }
1874
1875 rc = pm8058_usb_voltage_lower_limit();
1876 if (rc) {
1877 pr_err("%s: couldnt set ignore lower limit bit to 0\n",
1878 __func__);
1879 goto free_irq;
1880 }
1881
1882 rc = msm_charger_register(&usb_hw_chg);
1883 if (rc) {
1884 pr_err("%s: msm_charger_register failed ret=%d\n",
1885 __func__, rc);
1886 goto free_irq;
1887 }
1888
1889 pm_chg_batt_temp_disable(0);
1890 msm_battery_gauge_register(&pm8058_batt_gauge);
1891 __dump_chg_regs();
1892
1893 create_debugfs_entries();
1894 INIT_DELAYED_WORK(&pm8058_chg.veoc_begin_work, veoc_begin_work);
1895 INIT_DELAYED_WORK(&pm8058_chg.check_vbat_low_work, vbat_low_work);
1896 INIT_DELAYED_WORK(&pm8058_chg.chg_done_check_work, chg_done_check_work);
1897 INIT_DELAYED_WORK(&pm8058_chg.charging_check_work, charging_check_work);
1898
1899 /* determine what state the charger is in */
1900 pm8058_chg_determine_initial_state();
1901
1902 pm8058_chg_enable_irq(BATTTEMP_IRQ);
1903 pm8058_chg_enable_irq(BATTCONNECT_IRQ);
1904
1905 rc = pm8058_batt_alarm_state_set(0, 0);
1906 if (rc) {
1907 pr_err("%s: unable to set batt alarm state\n", __func__);
1908 goto free_irq;
1909 }
1910
1911 /*
1912 * The batt-alarm driver requires sane values for both min / max,
1913 * regardless of whether they're both activated.
1914 */
1915 rc = pm8058_batt_alarm_threshold_set(resume_mv, 4300);
1916 if (rc) {
1917 pr_err("%s: unable to set batt alarm threshold\n", __func__);
1918 goto free_irq;
1919 }
1920
1921 rc = pm8058_batt_alarm_hold_time_set(PM8058_BATT_ALARM_HOLD_TIME_16_MS);
1922 if (rc) {
1923 pr_err("%s: unable to set batt alarm hold time\n", __func__);
1924 goto free_irq;
1925 }
1926
1927 /* PWM enabled at 2Hz */
1928 rc = pm8058_batt_alarm_pwm_rate_set(1, 7, 4);
1929 if (rc) {
1930 pr_err("%s: unable to set batt alarm pwm rate\n", __func__);
1931 goto free_irq;
1932 }
1933
1934 rc = pm8058_batt_alarm_register_notifier(&alarm_notifier);
1935 if (rc) {
1936 pr_err("%s: unable to register alarm notifier\n", __func__);
1937 goto free_irq;
1938 }
1939
1940 pm8058_chg.inited = 1;
1941
1942 return 0;
1943
1944free_irq:
1945 free_irqs();
1946out:
1947 return rc;
1948}
1949
1950static int __devexit pm8058_charger_remove(struct platform_device *pdev)
1951{
1952 struct pm8058_charger_chip *chip = platform_get_drvdata(pdev);
1953 int rc;
1954
1955 msm_charger_notify_event(&usb_hw_chg, CHG_REMOVED_EVENT);
1956 msm_charger_unregister(&usb_hw_chg);
1957 cancel_delayed_work_sync(&pm8058_chg.veoc_begin_work);
1958 cancel_delayed_work_sync(&pm8058_chg.check_vbat_low_work);
1959 cancel_delayed_work_sync(&pm8058_chg.charging_check_work);
1960 free_irqs();
1961 remove_debugfs_entries();
1962 kfree(chip);
1963
1964 rc = pm8058_batt_alarm_state_set(0, 0);
1965 if (rc)
1966 pr_err("%s: unable to set batt alarm state\n", __func__);
1967
1968 rc |= pm8058_batt_alarm_unregister_notifier(&alarm_notifier);
1969 if (rc)
1970 pr_err("%s: unable to register alarm notifier\n", __func__);
1971 return rc;
1972}
1973
1974static struct platform_driver pm8058_charger_driver = {
1975 .probe = pm8058_charger_probe,
1976 .remove = __devexit_p(pm8058_charger_remove),
1977 .driver = {
1978 .name = "pm8058-charger",
1979 .owner = THIS_MODULE,
1980 },
1981};
1982
1983static int __init pm8058_charger_init(void)
1984{
1985 return platform_driver_register(&pm8058_charger_driver);
1986}
1987
1988static void __exit pm8058_charger_exit(void)
1989{
1990 platform_driver_unregister(&pm8058_charger_driver);
1991}
1992
1993late_initcall(pm8058_charger_init);
1994module_exit(pm8058_charger_exit);
1995
1996MODULE_LICENSE("GPL v2");
1997MODULE_DESCRIPTION("PMIC8058 BATTERY driver");
1998MODULE_VERSION("1.0");
1999MODULE_ALIAS("platform:pm8058_charger");