blob: 552ed821efee8ff9fecb294e2be50420a933a73d [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>
Siddartha Mohanadoss77d106e2011-09-20 16:25:59 -070021#include <linux/mfd/pm8xxx/pm8921-adc.h>
Abhijeet Dharmapurikar4ffea2c2011-11-11 18:25:02 -080022#include <linux/mfd/pm8xxx/ccadc.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023#include <linux/mfd/pm8xxx/core.h>
24#include <linux/interrupt.h>
25#include <linux/power_supply.h>
26#include <linux/delay.h>
27#include <linux/bitops.h>
28#include <linux/workqueue.h>
29#include <linux/debugfs.h>
30#include <linux/slab.h>
31
32#include <mach/msm_xo.h>
33#include <mach/msm_hsusb.h>
34
35#define CHG_BUCK_CLOCK_CTRL 0x14
36
37#define PBL_ACCESS1 0x04
38#define PBL_ACCESS2 0x05
39#define SYS_CONFIG_1 0x06
40#define SYS_CONFIG_2 0x07
41#define CHG_CNTRL 0x204
42#define CHG_IBAT_MAX 0x205
43#define CHG_TEST 0x206
44#define CHG_BUCK_CTRL_TEST1 0x207
45#define CHG_BUCK_CTRL_TEST2 0x208
46#define CHG_BUCK_CTRL_TEST3 0x209
47#define COMPARATOR_OVERRIDE 0x20A
48#define PSI_TXRX_SAMPLE_DATA_0 0x20B
49#define PSI_TXRX_SAMPLE_DATA_1 0x20C
50#define PSI_TXRX_SAMPLE_DATA_2 0x20D
51#define PSI_TXRX_SAMPLE_DATA_3 0x20E
52#define PSI_CONFIG_STATUS 0x20F
53#define CHG_IBAT_SAFE 0x210
54#define CHG_ITRICKLE 0x211
55#define CHG_CNTRL_2 0x212
56#define CHG_VBAT_DET 0x213
57#define CHG_VTRICKLE 0x214
58#define CHG_ITERM 0x215
59#define CHG_CNTRL_3 0x216
60#define CHG_VIN_MIN 0x217
61#define CHG_TWDOG 0x218
62#define CHG_TTRKL_MAX 0x219
63#define CHG_TEMP_THRESH 0x21A
64#define CHG_TCHG_MAX 0x21B
65#define USB_OVP_CONTROL 0x21C
66#define DC_OVP_CONTROL 0x21D
67#define USB_OVP_TEST 0x21E
68#define DC_OVP_TEST 0x21F
69#define CHG_VDD_MAX 0x220
70#define CHG_VDD_SAFE 0x221
71#define CHG_VBAT_BOOT_THRESH 0x222
72#define USB_OVP_TRIM 0x355
73#define BUCK_CONTROL_TRIM1 0x356
74#define BUCK_CONTROL_TRIM2 0x357
75#define BUCK_CONTROL_TRIM3 0x358
76#define BUCK_CONTROL_TRIM4 0x359
77#define CHG_DEFAULTS_TRIM 0x35A
78#define CHG_ITRIM 0x35B
79#define CHG_TTRIM 0x35C
80#define CHG_COMP_OVR 0x20A
81
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -070082/* check EOC every 10 seconds */
83#define EOC_CHECK_PERIOD_MS 10000
84
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070085enum chg_fsm_state {
86 FSM_STATE_OFF_0 = 0,
87 FSM_STATE_BATFETDET_START_12 = 12,
88 FSM_STATE_BATFETDET_END_16 = 16,
89 FSM_STATE_ON_CHG_HIGHI_1 = 1,
90 FSM_STATE_ATC_2A = 2,
91 FSM_STATE_ATC_2B = 18,
92 FSM_STATE_ON_BAT_3 = 3,
93 FSM_STATE_ATC_FAIL_4 = 4 ,
94 FSM_STATE_DELAY_5 = 5,
95 FSM_STATE_ON_CHG_AND_BAT_6 = 6,
96 FSM_STATE_FAST_CHG_7 = 7,
97 FSM_STATE_TRKL_CHG_8 = 8,
98 FSM_STATE_CHG_FAIL_9 = 9,
99 FSM_STATE_EOC_10 = 10,
100 FSM_STATE_ON_CHG_VREGOK_11 = 11,
101 FSM_STATE_ATC_PAUSE_13 = 13,
102 FSM_STATE_FAST_CHG_PAUSE_14 = 14,
103 FSM_STATE_TRKL_CHG_PAUSE_15 = 15,
104 FSM_STATE_START_BOOT = 20,
105 FSM_STATE_FLCB_VREGOK = 21,
106 FSM_STATE_FLCB = 22,
107};
108
Abhijeet Dharmapurikar236aa5a2011-11-03 12:17:19 -0700109struct fsm_state_to_batt_status {
110 enum chg_fsm_state fsm_state;
111 int batt_state;
112};
113
114static struct fsm_state_to_batt_status map[] = {
115 {FSM_STATE_OFF_0, POWER_SUPPLY_STATUS_UNKNOWN},
116 {FSM_STATE_BATFETDET_START_12, POWER_SUPPLY_STATUS_UNKNOWN},
117 {FSM_STATE_BATFETDET_END_16, POWER_SUPPLY_STATUS_UNKNOWN},
118 /*
119 * for CHG_HIGHI_1 report NOT_CHARGING if battery missing,
120 * too hot/cold, charger too hot
121 */
122 {FSM_STATE_ON_CHG_HIGHI_1, POWER_SUPPLY_STATUS_FULL},
123 {FSM_STATE_ATC_2A, POWER_SUPPLY_STATUS_CHARGING},
124 {FSM_STATE_ATC_2B, POWER_SUPPLY_STATUS_CHARGING},
125 {FSM_STATE_ON_BAT_3, POWER_SUPPLY_STATUS_DISCHARGING},
126 {FSM_STATE_ATC_FAIL_4, POWER_SUPPLY_STATUS_DISCHARGING},
127 {FSM_STATE_DELAY_5, POWER_SUPPLY_STATUS_UNKNOWN },
128 {FSM_STATE_ON_CHG_AND_BAT_6, POWER_SUPPLY_STATUS_CHARGING},
129 {FSM_STATE_FAST_CHG_7, POWER_SUPPLY_STATUS_CHARGING},
130 {FSM_STATE_TRKL_CHG_8, POWER_SUPPLY_STATUS_CHARGING},
131 {FSM_STATE_CHG_FAIL_9, POWER_SUPPLY_STATUS_DISCHARGING},
132 {FSM_STATE_EOC_10, POWER_SUPPLY_STATUS_FULL},
133 {FSM_STATE_ON_CHG_VREGOK_11, POWER_SUPPLY_STATUS_NOT_CHARGING},
134 {FSM_STATE_ATC_PAUSE_13, POWER_SUPPLY_STATUS_NOT_CHARGING},
135 {FSM_STATE_FAST_CHG_PAUSE_14, POWER_SUPPLY_STATUS_NOT_CHARGING},
136 {FSM_STATE_TRKL_CHG_PAUSE_15, POWER_SUPPLY_STATUS_NOT_CHARGING},
137 {FSM_STATE_START_BOOT, POWER_SUPPLY_STATUS_NOT_CHARGING},
138 {FSM_STATE_FLCB_VREGOK, POWER_SUPPLY_STATUS_NOT_CHARGING},
139 {FSM_STATE_FLCB, POWER_SUPPLY_STATUS_NOT_CHARGING},
140};
141
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700142enum chg_regulation_loop {
143 VDD_LOOP = BIT(3),
144 BAT_CURRENT_LOOP = BIT(2),
145 INPUT_CURRENT_LOOP = BIT(1),
146 INPUT_VOLTAGE_LOOP = BIT(0),
147 CHG_ALL_LOOPS = VDD_LOOP | BAT_CURRENT_LOOP
148 | INPUT_CURRENT_LOOP | INPUT_VOLTAGE_LOOP,
149};
150
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151enum pmic_chg_interrupts {
152 USBIN_VALID_IRQ = 0,
153 USBIN_OV_IRQ,
154 BATT_INSERTED_IRQ,
155 VBATDET_LOW_IRQ,
156 USBIN_UV_IRQ,
157 VBAT_OV_IRQ,
158 CHGWDOG_IRQ,
159 VCP_IRQ,
160 ATCDONE_IRQ,
161 ATCFAIL_IRQ,
162 CHGDONE_IRQ,
163 CHGFAIL_IRQ,
164 CHGSTATE_IRQ,
165 LOOP_CHANGE_IRQ,
166 FASTCHG_IRQ,
167 TRKLCHG_IRQ,
168 BATT_REMOVED_IRQ,
169 BATTTEMP_HOT_IRQ,
170 CHGHOT_IRQ,
171 BATTTEMP_COLD_IRQ,
172 CHG_GONE_IRQ,
173 BAT_TEMP_OK_IRQ,
174 COARSE_DET_LOW_IRQ,
175 VDD_LOOP_IRQ,
176 VREG_OV_IRQ,
177 VBATDET_IRQ,
178 BATFET_IRQ,
179 PSI_IRQ,
180 DCIN_VALID_IRQ,
181 DCIN_OV_IRQ,
182 DCIN_UV_IRQ,
183 PM_CHG_MAX_INTS,
184};
185
186struct bms_notify {
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700187 int is_battery_full;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188 int is_charging;
189 struct work_struct work;
190};
191
192/**
193 * struct pm8921_chg_chip -device information
194 * @dev: device pointer to access the parent
195 * @is_usb_path_used: indicates whether USB charging is used at all
196 * @is_usb_path_used: indicates whether DC charging is used at all
197 * @usb_present: present status of usb
198 * @dc_present: present status of dc
199 * @usb_charger_current: usb current to charge the battery with used when
200 * the usb path is enabled or charging is resumed
201 * @safety_time: max time for which charging will happen
202 * @update_time: how frequently the userland needs to be updated
203 * @max_voltage: the max volts the batt should be charged up to
204 * @min_voltage: the min battery voltage before turning the FETon
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -0700205 * @resume_voltage_delta: the voltage delta from vdd max at which the
206 * battery should resume charging
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207 * @term_current: The charging based term current
208 *
209 */
210struct pm8921_chg_chip {
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700211 struct device *dev;
212 unsigned int usb_present;
213 unsigned int dc_present;
214 unsigned int usb_charger_current;
215 unsigned int max_bat_chg_current;
216 unsigned int pmic_chg_irq[PM_CHG_MAX_INTS];
217 unsigned int safety_time;
218 unsigned int ttrkl_time;
219 unsigned int update_time;
220 unsigned int max_voltage;
221 unsigned int min_voltage;
222 unsigned int cool_temp;
223 unsigned int warm_temp;
224 unsigned int temp_check_period;
225 unsigned int cool_bat_chg_current;
226 unsigned int warm_bat_chg_current;
227 unsigned int cool_bat_voltage;
228 unsigned int warm_bat_voltage;
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -0700229 unsigned int is_bat_cool;
230 unsigned int is_bat_warm;
231 unsigned int resume_voltage_delta;
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700232 unsigned int term_current;
233 unsigned int vbat_channel;
234 unsigned int batt_temp_channel;
235 unsigned int batt_id_channel;
236 struct power_supply usb_psy;
237 struct power_supply dc_psy;
238 struct power_supply batt_psy;
239 struct dentry *dent;
240 struct bms_notify bms_notify;
Amir Samuelovd31ef502011-10-26 14:41:36 +0200241 struct ext_chg_pm8921 *ext;
242 bool ext_charging;
243 bool ext_charge_done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244 DECLARE_BITMAP(enabled_irqs, PM_CHG_MAX_INTS);
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700245 struct work_struct battery_id_valid_work;
246 int64_t batt_id_min;
247 int64_t batt_id_max;
248 int trkl_voltage;
249 int weak_voltage;
250 int trkl_current;
251 int weak_current;
252 int vin_min;
253 int *thermal_mitigation;
254 int thermal_levels;
255 struct delayed_work update_heartbeat_work;
256 struct delayed_work eoc_work;
257 struct wake_lock eoc_wake_lock;
258 enum pm8921_chg_cold_thr cold_thr;
259 enum pm8921_chg_hot_thr hot_thr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260};
261
262static int charging_disabled;
Abhijeet Dharmapurikarad742362011-08-29 19:50:02 -0700263static int thermal_mitigation;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264
265static struct pm8921_chg_chip *the_chip;
266
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -0700267static struct pm8921_adc_arb_btm_param btm_config;
268
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269static int pm_chg_masked_write(struct pm8921_chg_chip *chip, u16 addr,
270 u8 mask, u8 val)
271{
272 int rc;
273 u8 reg;
274
275 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
276 if (rc) {
277 pr_err("pm8xxx_readb failed: addr=%03X, rc=%d\n", addr, rc);
278 return rc;
279 }
280 reg &= ~mask;
281 reg |= val & mask;
282 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
283 if (rc) {
284 pr_err("pm8xxx_writeb failed: addr=%03X, rc=%d\n", addr, rc);
285 return rc;
286 }
287 return 0;
288}
289
290#define CAPTURE_FSM_STATE_CMD 0xC2
291#define READ_BANK_7 0x70
292#define READ_BANK_4 0x40
293static int pm_chg_get_fsm_state(struct pm8921_chg_chip *chip)
294{
295 u8 temp;
296 int err, ret = 0;
297
298 temp = CAPTURE_FSM_STATE_CMD;
299 err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
300 if (err) {
301 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
302 return err;
303 }
304
305 temp = READ_BANK_7;
306 err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
307 if (err) {
308 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
309 return err;
310 }
311
312 err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp);
313 if (err) {
314 pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err);
315 return err;
316 }
317 /* get the lower 4 bits */
318 ret = temp & 0xF;
319
320 temp = READ_BANK_4;
321 err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
322 if (err) {
323 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
324 return err;
325 }
326
327 err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp);
328 if (err) {
329 pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err);
330 return err;
331 }
332 /* get the upper 1 bit */
333 ret |= (temp & 0x1) << 4;
334 return ret;
335}
336
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700337#define READ_BANK_6 0x60
338static int pm_chg_get_regulation_loop(struct pm8921_chg_chip *chip)
339{
340 u8 temp;
341 int err;
342
343 temp = READ_BANK_6;
344 err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
345 if (err) {
346 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
347 return err;
348 }
349
350 err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp);
351 if (err) {
352 pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err);
353 return err;
354 }
355
356 /* return the lower 4 bits */
357 return temp & CHG_ALL_LOOPS;
358}
359
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360#define CHG_USB_SUSPEND_BIT BIT(2)
361static int pm_chg_usb_suspend_enable(struct pm8921_chg_chip *chip, int enable)
362{
363 return pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_USB_SUSPEND_BIT,
364 enable ? CHG_USB_SUSPEND_BIT : 0);
365}
366
367#define CHG_EN_BIT BIT(7)
368static int pm_chg_auto_enable(struct pm8921_chg_chip *chip, int enable)
369{
370 return pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_EN_BIT,
371 enable ? CHG_EN_BIT : 0);
372}
373
David Keitel753ec8d2011-11-02 10:56:37 -0700374#define CHG_FAILED_CLEAR BIT(0)
375#define ATC_FAILED_CLEAR BIT(1)
376static int pm_chg_failed_clear(struct pm8921_chg_chip *chip, int clear)
377{
378 int rc;
379
380 rc = pm_chg_masked_write(chip, CHG_CNTRL_3, ATC_FAILED_CLEAR,
381 clear ? ATC_FAILED_CLEAR : 0);
382 rc |= pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_FAILED_CLEAR,
383 clear ? CHG_FAILED_CLEAR : 0);
384 return rc;
385}
386
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387#define CHG_CHARGE_DIS_BIT BIT(1)
388static int pm_chg_charge_dis(struct pm8921_chg_chip *chip, int disable)
389{
390 return pm_chg_masked_write(chip, CHG_CNTRL, CHG_CHARGE_DIS_BIT,
391 disable ? CHG_CHARGE_DIS_BIT : 0);
392}
393
394#define PM8921_CHG_V_MIN_MV 3240
395#define PM8921_CHG_V_STEP_MV 20
396#define PM8921_CHG_VDDMAX_MAX 4500
397#define PM8921_CHG_VDDMAX_MIN 3400
398#define PM8921_CHG_V_MASK 0x7F
399static int pm_chg_vddmax_set(struct pm8921_chg_chip *chip, int voltage)
400{
401 u8 temp;
402
403 if (voltage < PM8921_CHG_VDDMAX_MIN
404 || voltage > PM8921_CHG_VDDMAX_MAX) {
405 pr_err("bad mV=%d asked to set\n", voltage);
406 return -EINVAL;
407 }
408 temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV;
409 pr_debug("voltage=%d setting %02x\n", voltage, temp);
410 return pm_chg_masked_write(chip, CHG_VDD_MAX, PM8921_CHG_V_MASK, temp);
411}
412
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700413static int pm_chg_vddmax_get(struct pm8921_chg_chip *chip, int *voltage)
414{
415 u8 temp;
416 int rc;
417
418 rc = pm8xxx_readb(chip->dev->parent, CHG_VDD_MAX, &temp);
419 if (rc) {
420 pr_err("rc = %d while reading vdd max\n", rc);
Stephen Boyd81982232011-09-26 12:08:38 -0700421 *voltage = 0;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700422 return rc;
423 }
424 temp &= PM8921_CHG_V_MASK;
425 *voltage = (int)temp * PM8921_CHG_V_STEP_MV + PM8921_CHG_V_MIN_MV;
426 return 0;
427}
428
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429#define PM8921_CHG_VDDSAFE_MIN 3400
430#define PM8921_CHG_VDDSAFE_MAX 4500
431static int pm_chg_vddsafe_set(struct pm8921_chg_chip *chip, int voltage)
432{
433 u8 temp;
434
435 if (voltage < PM8921_CHG_VDDSAFE_MIN
436 || voltage > PM8921_CHG_VDDSAFE_MAX) {
437 pr_err("bad mV=%d asked to set\n", voltage);
438 return -EINVAL;
439 }
440 temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV;
441 pr_debug("voltage=%d setting %02x\n", voltage, temp);
442 return pm_chg_masked_write(chip, CHG_VDD_SAFE, PM8921_CHG_V_MASK, temp);
443}
444
445#define PM8921_CHG_VBATDET_MIN 3240
446#define PM8921_CHG_VBATDET_MAX 5780
447static int pm_chg_vbatdet_set(struct pm8921_chg_chip *chip, int voltage)
448{
449 u8 temp;
450
451 if (voltage < PM8921_CHG_VBATDET_MIN
452 || voltage > PM8921_CHG_VBATDET_MAX) {
453 pr_err("bad mV=%d asked to set\n", voltage);
454 return -EINVAL;
455 }
456 temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV;
457 pr_debug("voltage=%d setting %02x\n", voltage, temp);
458 return pm_chg_masked_write(chip, CHG_VBAT_DET, PM8921_CHG_V_MASK, temp);
459}
460
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -0700461#define PM8921_CHG_VINMIN_MIN_MV 3800
462#define PM8921_CHG_VINMIN_STEP_MV 100
463#define PM8921_CHG_VINMIN_USABLE_MAX 6500
464#define PM8921_CHG_VINMIN_USABLE_MIN 4300
465#define PM8921_CHG_VINMIN_MASK 0x1F
466static int pm_chg_vinmin_set(struct pm8921_chg_chip *chip, int voltage)
467{
468 u8 temp;
469
470 if (voltage < PM8921_CHG_VINMIN_USABLE_MIN
471 || voltage > PM8921_CHG_VINMIN_USABLE_MAX) {
472 pr_err("bad mV=%d asked to set\n", voltage);
473 return -EINVAL;
474 }
475 temp = (voltage - PM8921_CHG_VINMIN_MIN_MV) / PM8921_CHG_VINMIN_STEP_MV;
476 pr_debug("voltage=%d setting %02x\n", voltage, temp);
477 return pm_chg_masked_write(chip, CHG_VIN_MIN, PM8921_CHG_VINMIN_MASK,
478 temp);
479}
480
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481#define PM8921_CHG_IBATMAX_MIN 325
482#define PM8921_CHG_IBATMAX_MAX 2000
483#define PM8921_CHG_I_MIN_MA 225
484#define PM8921_CHG_I_STEP_MA 50
485#define PM8921_CHG_I_MASK 0x3F
486static int pm_chg_ibatmax_set(struct pm8921_chg_chip *chip, int chg_current)
487{
488 u8 temp;
489
490 if (chg_current < PM8921_CHG_IBATMAX_MIN
491 || chg_current > PM8921_CHG_IBATMAX_MAX) {
492 pr_err("bad mA=%d asked to set\n", chg_current);
493 return -EINVAL;
494 }
495 temp = (chg_current - PM8921_CHG_I_MIN_MA) / PM8921_CHG_I_STEP_MA;
496 return pm_chg_masked_write(chip, CHG_IBAT_MAX, PM8921_CHG_I_MASK, temp);
497}
498
499#define PM8921_CHG_IBATSAFE_MIN 225
500#define PM8921_CHG_IBATSAFE_MAX 3375
501static int pm_chg_ibatsafe_set(struct pm8921_chg_chip *chip, int chg_current)
502{
503 u8 temp;
504
505 if (chg_current < PM8921_CHG_IBATSAFE_MIN
506 || chg_current > PM8921_CHG_IBATSAFE_MAX) {
507 pr_err("bad mA=%d asked to set\n", chg_current);
508 return -EINVAL;
509 }
510 temp = (chg_current - PM8921_CHG_I_MIN_MA) / PM8921_CHG_I_STEP_MA;
511 return pm_chg_masked_write(chip, CHG_IBAT_SAFE,
512 PM8921_CHG_I_MASK, temp);
513}
514
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515#define PM8921_CHG_ITERM_MIN_MA 50
Abhijeet Dharmapurikar71816fd2011-08-12 17:14:10 -0700516#define PM8921_CHG_ITERM_MAX_MA 200
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517#define PM8921_CHG_ITERM_STEP_MA 10
518#define PM8921_CHG_ITERM_MASK 0xF
519static int pm_chg_iterm_set(struct pm8921_chg_chip *chip, int chg_current)
520{
521 u8 temp;
522
Abhijeet Dharmapurikar71816fd2011-08-12 17:14:10 -0700523 if (chg_current < PM8921_CHG_ITERM_MIN_MA
524 || chg_current > PM8921_CHG_ITERM_MAX_MA) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525 pr_err("bad mA=%d asked to set\n", chg_current);
526 return -EINVAL;
527 }
528
529 temp = (chg_current - PM8921_CHG_ITERM_MIN_MA)
530 / PM8921_CHG_ITERM_STEP_MA;
Abhijeet Dharmapurikar71816fd2011-08-12 17:14:10 -0700531 return pm_chg_masked_write(chip, CHG_ITERM, PM8921_CHG_ITERM_MASK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700532 temp);
533}
534
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700535static int pm_chg_iterm_get(struct pm8921_chg_chip *chip, int *chg_current)
536{
537 u8 temp;
538 int rc;
539
540 rc = pm8xxx_readb(chip->dev->parent, CHG_ITERM, &temp);
541 if (rc) {
542 pr_err("err=%d reading CHG_ITEM\n", rc);
Stephen Boyd81982232011-09-26 12:08:38 -0700543 *chg_current = 0;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700544 return rc;
545 }
546 temp &= PM8921_CHG_ITERM_MASK;
547 *chg_current = (int)temp * PM8921_CHG_ITERM_STEP_MA
548 + PM8921_CHG_ITERM_MIN_MA;
549 return 0;
550}
551
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552#define PM8921_CHG_IUSB_MASK 0x1C
553#define PM8921_CHG_IUSB_MAX 7
554#define PM8921_CHG_IUSB_MIN 0
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -0700555static int pm_chg_iusbmax_set(struct pm8921_chg_chip *chip, int reg_val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556{
557 u8 temp;
558
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -0700559 if (reg_val < PM8921_CHG_IUSB_MIN || reg_val > PM8921_CHG_IUSB_MAX) {
560 pr_err("bad mA=%d asked to set\n", reg_val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 return -EINVAL;
562 }
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -0700563 temp = reg_val << 2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 return pm_chg_masked_write(chip, PBL_ACCESS2, PM8921_CHG_IUSB_MASK,
565 temp);
566}
567
568#define PM8921_CHG_WD_MASK 0x1F
569static int pm_chg_disable_wd(struct pm8921_chg_chip *chip)
570{
571 /* writing 0 to the wd timer disables it */
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -0700572 return pm_chg_masked_write(chip, CHG_TWDOG, PM8921_CHG_WD_MASK, 0);
573}
574
Abhijeet Dharmapurikarc26cd902011-10-26 18:07:45 -0700575#define PM8921_CHG_TCHG_MASK 0x7F
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -0700576#define PM8921_CHG_TCHG_MIN 4
577#define PM8921_CHG_TCHG_MAX 512
578#define PM8921_CHG_TCHG_STEP 4
579static int pm_chg_tchg_max_set(struct pm8921_chg_chip *chip, int minutes)
580{
581 u8 temp;
582
583 if (minutes < PM8921_CHG_TCHG_MIN || minutes > PM8921_CHG_TCHG_MAX) {
584 pr_err("bad max minutes =%d asked to set\n", minutes);
585 return -EINVAL;
586 }
587
588 temp = (minutes - 1)/PM8921_CHG_TCHG_STEP;
589 return pm_chg_masked_write(chip, CHG_TCHG_MAX, PM8921_CHG_TCHG_MASK,
590 temp);
591}
592
593#define PM8921_CHG_TTRKL_MASK 0x1F
594#define PM8921_CHG_TTRKL_MIN 1
595#define PM8921_CHG_TTRKL_MAX 64
596static int pm_chg_ttrkl_max_set(struct pm8921_chg_chip *chip, int minutes)
597{
598 u8 temp;
599
600 if (minutes < PM8921_CHG_TTRKL_MIN || minutes > PM8921_CHG_TTRKL_MAX) {
601 pr_err("bad max minutes =%d asked to set\n", minutes);
602 return -EINVAL;
603 }
604
605 temp = minutes - 1;
606 return pm_chg_masked_write(chip, CHG_TTRKL_MAX, PM8921_CHG_TTRKL_MASK,
607 temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608}
609
Abhijeet Dharmapurikar26cef9c2011-08-25 19:16:42 -0700610#define PM8921_CHG_VTRKL_MIN_MV 2050
611#define PM8921_CHG_VTRKL_MAX_MV 2800
612#define PM8921_CHG_VTRKL_STEP_MV 50
613#define PM8921_CHG_VTRKL_SHIFT 4
614#define PM8921_CHG_VTRKL_MASK 0xF0
615static int pm_chg_vtrkl_low_set(struct pm8921_chg_chip *chip, int millivolts)
616{
617 u8 temp;
618
619 if (millivolts < PM8921_CHG_VTRKL_MIN_MV
620 || millivolts > PM8921_CHG_VTRKL_MAX_MV) {
621 pr_err("bad voltage = %dmV asked to set\n", millivolts);
622 return -EINVAL;
623 }
624
625 temp = (millivolts - PM8921_CHG_VTRKL_MIN_MV)/PM8921_CHG_VTRKL_STEP_MV;
626 temp = temp << PM8921_CHG_VTRKL_SHIFT;
627 return pm_chg_masked_write(chip, CHG_VTRICKLE, PM8921_CHG_VTRKL_MASK,
628 temp);
629}
630
631#define PM8921_CHG_VWEAK_MIN_MV 2100
632#define PM8921_CHG_VWEAK_MAX_MV 3600
633#define PM8921_CHG_VWEAK_STEP_MV 100
634#define PM8921_CHG_VWEAK_MASK 0x0F
635static int pm_chg_vweak_set(struct pm8921_chg_chip *chip, int millivolts)
636{
637 u8 temp;
638
639 if (millivolts < PM8921_CHG_VWEAK_MIN_MV
640 || millivolts > PM8921_CHG_VWEAK_MAX_MV) {
641 pr_err("bad voltage = %dmV asked to set\n", millivolts);
642 return -EINVAL;
643 }
644
645 temp = (millivolts - PM8921_CHG_VWEAK_MIN_MV)/PM8921_CHG_VWEAK_STEP_MV;
646 return pm_chg_masked_write(chip, CHG_VTRICKLE, PM8921_CHG_VWEAK_MASK,
647 temp);
648}
649
650#define PM8921_CHG_ITRKL_MIN_MA 50
651#define PM8921_CHG_ITRKL_MAX_MA 200
652#define PM8921_CHG_ITRKL_MASK 0x0F
653#define PM8921_CHG_ITRKL_STEP_MA 10
654static int pm_chg_itrkl_set(struct pm8921_chg_chip *chip, int milliamps)
655{
656 u8 temp;
657
658 if (milliamps < PM8921_CHG_ITRKL_MIN_MA
659 || milliamps > PM8921_CHG_ITRKL_MAX_MA) {
660 pr_err("bad current = %dmA asked to set\n", milliamps);
661 return -EINVAL;
662 }
663
664 temp = (milliamps - PM8921_CHG_ITRKL_MIN_MA)/PM8921_CHG_ITRKL_STEP_MA;
665
666 return pm_chg_masked_write(chip, CHG_ITRICKLE, PM8921_CHG_ITRKL_MASK,
667 temp);
668}
669
670#define PM8921_CHG_IWEAK_MIN_MA 325
671#define PM8921_CHG_IWEAK_MAX_MA 525
672#define PM8921_CHG_IWEAK_SHIFT 7
673#define PM8921_CHG_IWEAK_MASK 0x80
674static int pm_chg_iweak_set(struct pm8921_chg_chip *chip, int milliamps)
675{
676 u8 temp;
677
678 if (milliamps < PM8921_CHG_IWEAK_MIN_MA
679 || milliamps > PM8921_CHG_IWEAK_MAX_MA) {
680 pr_err("bad current = %dmA asked to set\n", milliamps);
681 return -EINVAL;
682 }
683
684 if (milliamps < PM8921_CHG_IWEAK_MAX_MA)
685 temp = 0;
686 else
687 temp = 1;
688
689 temp = temp << PM8921_CHG_IWEAK_SHIFT;
690 return pm_chg_masked_write(chip, CHG_ITRICKLE, PM8921_CHG_IWEAK_MASK,
691 temp);
692}
693
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700694#define PM8921_CHG_BATT_TEMP_THR_COLD BIT(1)
695#define PM8921_CHG_BATT_TEMP_THR_COLD_SHIFT 1
696static int pm_chg_batt_cold_temp_config(struct pm8921_chg_chip *chip,
697 enum pm8921_chg_cold_thr cold_thr)
698{
699 u8 temp;
700
701 temp = cold_thr << PM8921_CHG_BATT_TEMP_THR_COLD_SHIFT;
702 temp = temp & PM8921_CHG_BATT_TEMP_THR_COLD;
703 return pm_chg_masked_write(chip, CHG_CNTRL_2,
704 PM8921_CHG_BATT_TEMP_THR_COLD,
705 temp);
706}
707
708#define PM8921_CHG_BATT_TEMP_THR_HOT BIT(0)
709#define PM8921_CHG_BATT_TEMP_THR_HOT_SHIFT 0
710static int pm_chg_batt_hot_temp_config(struct pm8921_chg_chip *chip,
711 enum pm8921_chg_hot_thr hot_thr)
712{
713 u8 temp;
714
715 temp = hot_thr << PM8921_CHG_BATT_TEMP_THR_HOT_SHIFT;
716 temp = temp & PM8921_CHG_BATT_TEMP_THR_HOT;
717 return pm_chg_masked_write(chip, CHG_CNTRL_2,
718 PM8921_CHG_BATT_TEMP_THR_HOT,
719 temp);
720}
721
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -0700722static int64_t read_battery_id(struct pm8921_chg_chip *chip)
723{
724 int rc;
725 struct pm8921_adc_chan_result result;
726
727 rc = pm8921_adc_read(chip->batt_id_channel, &result);
728 if (rc) {
729 pr_err("error reading batt id channel = %d, rc = %d\n",
730 chip->vbat_channel, rc);
731 return rc;
732 }
733 pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical,
734 result.measurement);
735 return result.physical;
736}
737
738static int is_battery_valid(struct pm8921_chg_chip *chip)
739{
740 int64_t rc;
741
742 if (chip->batt_id_min == 0 && chip->batt_id_max == 0)
743 return 1;
744
745 rc = read_battery_id(chip);
746 if (rc < 0) {
747 pr_err("error reading batt id channel = %d, rc = %lld\n",
748 chip->vbat_channel, rc);
749 /* assume battery id is valid when adc error happens */
750 return 1;
751 }
752
753 if (rc < chip->batt_id_min || rc > chip->batt_id_max) {
754 pr_err("batt_id phy =%lld is not valid\n", rc);
755 return 0;
756 }
757 return 1;
758}
759
760static void check_battery_valid(struct pm8921_chg_chip *chip)
761{
762 if (is_battery_valid(chip) == 0) {
763 pr_err("batt_id not valid, disbling charging\n");
764 pm_chg_auto_enable(chip, 0);
765 } else {
766 pm_chg_auto_enable(chip, !charging_disabled);
767 }
768}
769
770static void battery_id_valid(struct work_struct *work)
771{
772 struct pm8921_chg_chip *chip = container_of(work,
773 struct pm8921_chg_chip, battery_id_valid_work);
774
775 check_battery_valid(chip);
776}
777
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778static void pm8921_chg_enable_irq(struct pm8921_chg_chip *chip, int interrupt)
779{
780 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
781 dev_dbg(chip->dev, "%d\n", chip->pmic_chg_irq[interrupt]);
782 enable_irq(chip->pmic_chg_irq[interrupt]);
783 }
784}
785
786static void pm8921_chg_disable_irq(struct pm8921_chg_chip *chip, int interrupt)
787{
788 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
789 dev_dbg(chip->dev, "%d\n", chip->pmic_chg_irq[interrupt]);
790 disable_irq_nosync(chip->pmic_chg_irq[interrupt]);
791 }
792}
793
794static int pm_chg_get_rt_status(struct pm8921_chg_chip *chip, int irq_id)
795{
796 return pm8xxx_read_irq_stat(chip->dev->parent,
797 chip->pmic_chg_irq[irq_id]);
798}
799
800/* Treat OverVoltage/UnderVoltage as source missing */
801static int is_usb_chg_plugged_in(struct pm8921_chg_chip *chip)
802{
Abhijeet Dharmapurikar956215b2011-10-19 15:30:14 -0700803 return pm_chg_get_rt_status(chip, USBIN_VALID_IRQ);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700804}
805
806/* Treat OverVoltage/UnderVoltage as source missing */
807static int is_dc_chg_plugged_in(struct pm8921_chg_chip *chip)
808{
Abhijeet Dharmapurikar956215b2011-10-19 15:30:14 -0700809 return pm_chg_get_rt_status(chip, DCIN_VALID_IRQ);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700810}
811
Amir Samuelovd31ef502011-10-26 14:41:36 +0200812static bool is_ext_charging(struct pm8921_chg_chip *chip)
813{
814 if (chip->ext == NULL)
815 return false;
816
817 if (chip->ext_charging)
818 return true;
819
820 return false;
821}
822
823static bool is_ext_trickle_charging(struct pm8921_chg_chip *chip)
824{
825 if (chip->ext == NULL)
826 return false;
827
828 if (chip->ext->is_trickle(chip->ext->ctx))
829 return true;
830
831 return false;
832}
833
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834static int is_battery_charging(int fsm_state)
835{
Amir Samuelovd31ef502011-10-26 14:41:36 +0200836 if (is_ext_charging(the_chip))
837 return 1;
838
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700839 switch (fsm_state) {
840 case FSM_STATE_ATC_2A:
841 case FSM_STATE_ATC_2B:
842 case FSM_STATE_ON_CHG_AND_BAT_6:
843 case FSM_STATE_FAST_CHG_7:
844 case FSM_STATE_TRKL_CHG_8:
845 return 1;
846 }
847 return 0;
848}
849
850static void bms_notify(struct work_struct *work)
851{
852 struct bms_notify *n = container_of(work, struct bms_notify, work);
853
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700854 if (n->is_charging) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 pm8921_bms_charging_began();
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700856 } else {
857 pm8921_bms_charging_end(n->is_battery_full);
858 n->is_battery_full = 0;
859 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860}
861
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -0700862static void bms_notify_check(struct pm8921_chg_chip *chip)
863{
864 int fsm_state, new_is_charging;
865
866 fsm_state = pm_chg_get_fsm_state(chip);
867 new_is_charging = is_battery_charging(fsm_state);
868
869 if (chip->bms_notify.is_charging ^ new_is_charging) {
870 chip->bms_notify.is_charging = new_is_charging;
871 schedule_work(&(chip->bms_notify.work));
872 }
873}
874
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875static enum power_supply_property pm_power_props[] = {
876 POWER_SUPPLY_PROP_PRESENT,
Willie Ruan9ad6d832011-08-19 11:58:42 -0700877 POWER_SUPPLY_PROP_ONLINE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878};
879
880static char *pm_power_supplied_to[] = {
881 "battery",
882};
883
884static int pm_power_get_property(struct power_supply *psy,
885 enum power_supply_property psp,
886 union power_supply_propval *val)
887{
888 struct pm8921_chg_chip *chip;
889
890 switch (psp) {
891 case POWER_SUPPLY_PROP_PRESENT:
Willie Ruan9ad6d832011-08-19 11:58:42 -0700892 case POWER_SUPPLY_PROP_ONLINE:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893 if (psy->type == POWER_SUPPLY_TYPE_MAINS) {
894 chip = container_of(psy, struct pm8921_chg_chip,
895 dc_psy);
896 val->intval = is_dc_chg_plugged_in(chip);
897 }
898 if (psy->type == POWER_SUPPLY_TYPE_USB) {
899 chip = container_of(psy, struct pm8921_chg_chip,
900 usb_psy);
901 val->intval = is_usb_chg_plugged_in(chip);
902 }
903 break;
904 default:
905 return -EINVAL;
906 }
907 return 0;
908}
909
910static enum power_supply_property msm_batt_power_props[] = {
911 POWER_SUPPLY_PROP_STATUS,
912 POWER_SUPPLY_PROP_CHARGE_TYPE,
913 POWER_SUPPLY_PROP_HEALTH,
914 POWER_SUPPLY_PROP_PRESENT,
915 POWER_SUPPLY_PROP_TECHNOLOGY,
916 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
917 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
918 POWER_SUPPLY_PROP_VOLTAGE_NOW,
919 POWER_SUPPLY_PROP_CAPACITY,
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -0700920 POWER_SUPPLY_PROP_CURRENT_NOW,
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -0700921 POWER_SUPPLY_PROP_TEMP,
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -0700922 POWER_SUPPLY_PROP_ENERGY_FULL,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700923};
924
925static int get_prop_battery_mvolts(struct pm8921_chg_chip *chip)
926{
927 int rc;
928 struct pm8921_adc_chan_result result;
929
930 rc = pm8921_adc_read(chip->vbat_channel, &result);
931 if (rc) {
932 pr_err("error reading adc channel = %d, rc = %d\n",
933 chip->vbat_channel, rc);
934 return rc;
935 }
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -0700936 pr_debug("mvolts phy = %lld meas = 0x%llx\n", result.physical,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700937 result.measurement);
938 return (int)result.physical;
939}
940
Abhijeet Dharmapurikar4ffea2c2011-11-11 18:25:02 -0800941static unsigned int voltage_based_capacity(struct pm8921_chg_chip *chip)
942{
943 unsigned int current_voltage = get_prop_battery_mvolts(chip);
944 unsigned int low_voltage = chip->min_voltage;
945 unsigned int high_voltage = chip->max_voltage;
946
947 if (current_voltage <= low_voltage)
948 return 0;
949 else if (current_voltage >= high_voltage)
950 return 100;
951 else
952 return (current_voltage - low_voltage) * 100
953 / (high_voltage - low_voltage);
954}
955
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956static int get_prop_batt_capacity(struct pm8921_chg_chip *chip)
957{
Abhijeet Dharmapurikar5a7df4e2011-07-27 13:16:24 -0700958 int percent_soc = pm8921_bms_get_percent_charge();
959
Abhijeet Dharmapurikar4ffea2c2011-11-11 18:25:02 -0800960 if (percent_soc == -ENXIO)
961 percent_soc = voltage_based_capacity(chip);
962
Abhijeet Dharmapurikar5a7df4e2011-07-27 13:16:24 -0700963 if (percent_soc <= 10)
964 pr_warn("low battery charge = %d%%\n", percent_soc);
965
966 return percent_soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967}
968
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -0700969static int get_prop_batt_current(struct pm8921_chg_chip *chip)
970{
971 int result_ma, rc;
972
973 rc = pm8921_bms_get_battery_current(&result_ma);
Abhijeet Dharmapurikar4ffea2c2011-11-11 18:25:02 -0800974 if (rc == -ENXIO) {
975 rc = pm8xxx_ccadc_get_battery_current(&result_ma);
976 }
977
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -0700978 if (rc) {
979 pr_err("unable to get batt current rc = %d\n", rc);
980 return rc;
981 } else {
982 return result_ma;
983 }
984}
985
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -0700986static int get_prop_batt_fcc(struct pm8921_chg_chip *chip)
987{
988 int rc;
989
990 rc = pm8921_bms_get_fcc();
991 if (rc < 0)
992 pr_err("unable to get batt fcc rc = %d\n", rc);
993 return rc;
994}
995
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700996static int get_prop_batt_health(struct pm8921_chg_chip *chip)
997{
998 int temp;
999
1000 temp = pm_chg_get_rt_status(chip, BATTTEMP_HOT_IRQ);
1001 if (temp)
1002 return POWER_SUPPLY_HEALTH_OVERHEAT;
1003
1004 temp = pm_chg_get_rt_status(chip, BATTTEMP_COLD_IRQ);
1005 if (temp)
1006 return POWER_SUPPLY_HEALTH_COLD;
1007
1008 return POWER_SUPPLY_HEALTH_GOOD;
1009}
1010
1011static int get_prop_batt_present(struct pm8921_chg_chip *chip)
1012{
1013 return pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ);
1014}
1015
1016static int get_prop_charge_type(struct pm8921_chg_chip *chip)
1017{
1018 int temp;
1019
Amir Samuelovd31ef502011-10-26 14:41:36 +02001020 if (!get_prop_batt_present(chip))
1021 return POWER_SUPPLY_CHARGE_TYPE_NONE;
1022
1023 if (is_ext_trickle_charging(chip))
1024 return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1025
1026 if (is_ext_charging(chip))
1027 return POWER_SUPPLY_CHARGE_TYPE_FAST;
1028
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001029 temp = pm_chg_get_rt_status(chip, TRKLCHG_IRQ);
1030 if (temp)
1031 return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1032
1033 temp = pm_chg_get_rt_status(chip, FASTCHG_IRQ);
1034 if (temp)
1035 return POWER_SUPPLY_CHARGE_TYPE_FAST;
1036
1037 return POWER_SUPPLY_CHARGE_TYPE_NONE;
1038}
1039
1040static int get_prop_batt_status(struct pm8921_chg_chip *chip)
1041{
Abhijeet Dharmapurikar236aa5a2011-11-03 12:17:19 -07001042 int batt_state = POWER_SUPPLY_STATUS_DISCHARGING;
1043 int fsm_state = pm_chg_get_fsm_state(chip);
1044 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045
David Keiteld4e59ef2011-11-09 17:38:31 -08001046 if (chip->ext) {
1047 if (chip->ext_charge_done)
1048 return POWER_SUPPLY_STATUS_FULL;
1049 if (chip->ext_charging)
1050 return POWER_SUPPLY_STATUS_CHARGING;
1051 }
1052
Abhijeet Dharmapurikar236aa5a2011-11-03 12:17:19 -07001053 for (i = 0; i < ARRAY_SIZE(map); i++)
1054 if (map[i].fsm_state == fsm_state)
1055 batt_state = map[i].batt_state;
Amir Samuelovd31ef502011-10-26 14:41:36 +02001056
Abhijeet Dharmapurikar236aa5a2011-11-03 12:17:19 -07001057 if (fsm_state == FSM_STATE_ON_CHG_HIGHI_1) {
1058 if (!pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ)
1059 || !pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ)
1060 || !pm_chg_get_rt_status(chip, CHGHOT_IRQ))
Amir Samuelovd31ef502011-10-26 14:41:36 +02001061
Abhijeet Dharmapurikar236aa5a2011-11-03 12:17:19 -07001062 batt_state = POWER_SUPPLY_STATUS_NOT_CHARGING;
Amir Samuelovd31ef502011-10-26 14:41:36 +02001063 }
Abhijeet Dharmapurikar236aa5a2011-11-03 12:17:19 -07001064 return batt_state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001065}
1066
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001067static int get_prop_batt_temp(struct pm8921_chg_chip *chip)
1068{
1069 int rc;
1070 struct pm8921_adc_chan_result result;
1071
1072 rc = pm8921_adc_read(chip->batt_temp_channel, &result);
1073 if (rc) {
1074 pr_err("error reading adc channel = %d, rc = %d\n",
1075 chip->vbat_channel, rc);
1076 return rc;
1077 }
1078 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1079 result.measurement);
1080 return (int)result.physical;
1081}
1082
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001083static int pm_batt_power_get_property(struct power_supply *psy,
1084 enum power_supply_property psp,
1085 union power_supply_propval *val)
1086{
1087 struct pm8921_chg_chip *chip = container_of(psy, struct pm8921_chg_chip,
1088 batt_psy);
1089
1090 switch (psp) {
1091 case POWER_SUPPLY_PROP_STATUS:
1092 val->intval = get_prop_batt_status(chip);
1093 break;
1094 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1095 val->intval = get_prop_charge_type(chip);
1096 break;
1097 case POWER_SUPPLY_PROP_HEALTH:
1098 val->intval = get_prop_batt_health(chip);
1099 break;
1100 case POWER_SUPPLY_PROP_PRESENT:
1101 val->intval = get_prop_batt_present(chip);
1102 break;
1103 case POWER_SUPPLY_PROP_TECHNOLOGY:
1104 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
1105 break;
1106 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
1107 val->intval = chip->max_voltage;
1108 break;
1109 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
1110 val->intval = chip->min_voltage;
1111 break;
1112 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1113 val->intval = get_prop_battery_mvolts(chip);
1114 break;
1115 case POWER_SUPPLY_PROP_CAPACITY:
1116 val->intval = get_prop_batt_capacity(chip);
1117 break;
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001118 case POWER_SUPPLY_PROP_CURRENT_NOW:
1119 val->intval = get_prop_batt_current(chip);
1120 break;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001121 case POWER_SUPPLY_PROP_TEMP:
1122 val->intval = get_prop_batt_temp(chip);
1123 break;
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001124 case POWER_SUPPLY_PROP_ENERGY_FULL:
1125 val->intval = get_prop_batt_fcc(chip);
1126 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127 default:
1128 return -EINVAL;
1129 }
1130
1131 return 0;
1132}
1133
1134static void (*notify_vbus_state_func_ptr)(int);
1135static int usb_chg_current;
1136static DEFINE_SPINLOCK(vbus_lock);
1137
1138int pm8921_charger_register_vbus_sn(void (*callback)(int))
1139{
1140 pr_debug("%p\n", callback);
1141 notify_vbus_state_func_ptr = callback;
1142 return 0;
1143}
1144EXPORT_SYMBOL_GPL(pm8921_charger_register_vbus_sn);
1145
1146/* this is passed to the hsusb via platform_data msm_otg_pdata */
1147void pm8921_charger_unregister_vbus_sn(void (*callback)(int))
1148{
1149 pr_debug("%p\n", callback);
1150 notify_vbus_state_func_ptr = NULL;
1151}
1152EXPORT_SYMBOL_GPL(pm8921_charger_unregister_vbus_sn);
1153
1154static void notify_usb_of_the_plugin_event(int plugin)
1155{
1156 plugin = !!plugin;
1157 if (notify_vbus_state_func_ptr) {
1158 pr_debug("notifying plugin\n");
1159 (*notify_vbus_state_func_ptr) (plugin);
1160 } else {
1161 pr_debug("unable to notify plugin\n");
1162 }
1163}
1164
1165struct usb_ma_limit_entry {
1166 int usb_ma;
1167 u8 chg_iusb_value;
1168};
1169
1170static struct usb_ma_limit_entry usb_ma_table[] = {
1171 {100, 0},
1172 {500, 1},
1173 {700, 2},
1174 {850, 3},
1175 {900, 4},
1176 {1100, 5},
1177 {1300, 6},
1178 {1500, 7},
1179};
1180
1181/* assumes vbus_lock is held */
1182static void __pm8921_charger_vbus_draw(unsigned int mA)
1183{
1184 int i, rc;
1185
1186 if (mA > 0 && mA <= 2) {
1187 usb_chg_current = 0;
1188 rc = pm_chg_iusbmax_set(the_chip,
1189 usb_ma_table[0].chg_iusb_value);
1190 if (rc) {
1191 pr_err("unable to set iusb to %d rc = %d\n",
1192 usb_ma_table[0].chg_iusb_value, rc);
1193 }
1194 rc = pm_chg_usb_suspend_enable(the_chip, 1);
1195 if (rc)
1196 pr_err("fail to set suspend bit rc=%d\n", rc);
1197 } else {
1198 rc = pm_chg_usb_suspend_enable(the_chip, 0);
1199 if (rc)
1200 pr_err("fail to reset suspend bit rc=%d\n", rc);
1201 for (i = ARRAY_SIZE(usb_ma_table) - 1; i >= 0; i--) {
1202 if (usb_ma_table[i].usb_ma <= mA)
1203 break;
1204 }
1205 if (i < 0)
1206 i = 0;
1207 rc = pm_chg_iusbmax_set(the_chip,
1208 usb_ma_table[i].chg_iusb_value);
1209 if (rc) {
1210 pr_err("unable to set iusb to %d rc = %d\n",
1211 usb_ma_table[i].chg_iusb_value, rc);
1212 }
1213 }
1214}
1215
1216/* USB calls these to tell us how much max usb current the system can draw */
1217void pm8921_charger_vbus_draw(unsigned int mA)
1218{
1219 unsigned long flags;
1220
1221 pr_debug("Enter charge=%d\n", mA);
1222 spin_lock_irqsave(&vbus_lock, flags);
1223 if (the_chip) {
1224 __pm8921_charger_vbus_draw(mA);
1225 } else {
1226 /*
1227 * called before pmic initialized,
1228 * save this value and use it at probe
1229 */
1230 usb_chg_current = mA;
1231 }
1232 spin_unlock_irqrestore(&vbus_lock, flags);
1233}
1234EXPORT_SYMBOL_GPL(pm8921_charger_vbus_draw);
1235
Abhijeet Dharmapurikar1f52c412011-08-12 17:11:45 -07001236int pm8921_charger_enable(bool enable)
1237{
1238 int rc;
1239
1240 if (!the_chip) {
1241 pr_err("called before init\n");
1242 return -EINVAL;
1243 }
1244 enable = !!enable;
1245 rc = pm_chg_auto_enable(the_chip, enable);
1246 if (rc)
1247 pr_err("Failed rc=%d\n", rc);
1248 return rc;
1249}
1250EXPORT_SYMBOL(pm8921_charger_enable);
1251
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001252int pm8921_is_usb_chg_plugged_in(void)
1253{
1254 if (!the_chip) {
1255 pr_err("called before init\n");
1256 return -EINVAL;
1257 }
1258 return is_usb_chg_plugged_in(the_chip);
1259}
1260EXPORT_SYMBOL(pm8921_is_usb_chg_plugged_in);
1261
1262int pm8921_is_dc_chg_plugged_in(void)
1263{
1264 if (!the_chip) {
1265 pr_err("called before init\n");
1266 return -EINVAL;
1267 }
1268 return is_dc_chg_plugged_in(the_chip);
1269}
1270EXPORT_SYMBOL(pm8921_is_dc_chg_plugged_in);
1271
1272int pm8921_is_battery_present(void)
1273{
1274 if (!the_chip) {
1275 pr_err("called before init\n");
1276 return -EINVAL;
1277 }
1278 return get_prop_batt_present(the_chip);
1279}
1280EXPORT_SYMBOL(pm8921_is_battery_present);
1281
1282int pm8921_set_max_battery_charge_current(int ma)
1283{
1284 if (!the_chip) {
1285 pr_err("called before init\n");
1286 return -EINVAL;
1287 }
1288 return pm_chg_ibatmax_set(the_chip, ma);
1289}
1290EXPORT_SYMBOL(pm8921_set_max_battery_charge_current);
1291
1292int pm8921_disable_source_current(bool disable)
1293{
1294 if (!the_chip) {
1295 pr_err("called before init\n");
1296 return -EINVAL;
1297 }
1298 if (disable)
1299 pr_warn("current drawn from chg=0, battery provides current\n");
1300 return pm_chg_charge_dis(the_chip, disable);
1301}
1302EXPORT_SYMBOL(pm8921_disable_source_current);
1303
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -07001304int pm8921_regulate_input_voltage(int voltage)
1305{
1306 if (!the_chip) {
1307 pr_err("called before init\n");
1308 return -EINVAL;
1309 }
1310 return pm_chg_vinmin_set(the_chip, voltage);
1311}
1312
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001313bool pm8921_is_battery_charging(int *source)
1314{
1315 int fsm_state, is_charging, dc_present, usb_present;
1316
1317 if (!the_chip) {
1318 pr_err("called before init\n");
1319 return -EINVAL;
1320 }
1321 fsm_state = pm_chg_get_fsm_state(the_chip);
1322 is_charging = is_battery_charging(fsm_state);
1323 if (is_charging == 0) {
1324 *source = PM8921_CHG_SRC_NONE;
1325 return is_charging;
1326 }
1327
1328 if (source == NULL)
1329 return is_charging;
1330
1331 /* the battery is charging, the source is requested, find it */
1332 dc_present = is_dc_chg_plugged_in(the_chip);
1333 usb_present = is_usb_chg_plugged_in(the_chip);
1334
1335 if (dc_present && !usb_present)
1336 *source = PM8921_CHG_SRC_DC;
1337
1338 if (usb_present && !dc_present)
1339 *source = PM8921_CHG_SRC_USB;
1340
1341 if (usb_present && dc_present)
1342 /*
1343 * The system always chooses dc for charging since it has
1344 * higher priority.
1345 */
1346 *source = PM8921_CHG_SRC_DC;
1347
1348 return is_charging;
1349}
1350EXPORT_SYMBOL(pm8921_is_battery_charging);
1351
1352int pm8921_batt_temperature(void)
1353{
1354 if (!the_chip) {
1355 pr_err("called before init\n");
1356 return -EINVAL;
1357 }
1358 return get_prop_batt_temp(the_chip);
1359}
1360
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001361static void handle_usb_insertion_removal(struct pm8921_chg_chip *chip)
1362{
1363 int usb_present;
1364
1365 usb_present = is_usb_chg_plugged_in(chip);
1366 if (chip->usb_present ^ usb_present) {
1367 notify_usb_of_the_plugin_event(usb_present);
1368 chip->usb_present = usb_present;
1369 power_supply_changed(&chip->usb_psy);
Abhijeet Dharmapurikar0c4bc152011-10-27 10:22:24 -07001370 power_supply_changed(&chip->batt_psy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001371 }
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07001372 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001373}
1374
Amir Samuelovd31ef502011-10-26 14:41:36 +02001375static void handle_stop_ext_chg(struct pm8921_chg_chip *chip)
1376{
1377 if (chip->ext == NULL) {
1378 pr_debug("external charger not registered.\n");
1379 return;
1380 }
1381
1382 if (!chip->ext_charging) {
1383 pr_debug("already not charging.\n");
1384 return;
1385 }
1386
1387 chip->ext->stop_charging(chip->ext->ctx);
1388 chip->ext_charging = false;
David Keiteld4e59ef2011-11-09 17:38:31 -08001389 chip->ext_charge_done = false;
Amir Samuelovd31ef502011-10-26 14:41:36 +02001390}
1391
1392static void handle_start_ext_chg(struct pm8921_chg_chip *chip)
1393{
1394 int dc_present;
1395 int batt_present;
1396 int batt_temp_ok;
1397 int vbat_ov;
1398 int batfet;
1399 unsigned long delay =
1400 round_jiffies_relative(msecs_to_jiffies(EOC_CHECK_PERIOD_MS));
1401
1402 if (chip->ext == NULL) {
1403 pr_debug("external charger not registered.\n");
1404 return;
1405 }
1406
1407 if (chip->ext_charging) {
1408 pr_debug("already charging.\n");
1409 return;
1410 }
1411
1412 dc_present = is_dc_chg_plugged_in(chip);
1413 batt_present = pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ);
1414 batt_temp_ok = pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ);
1415 vbat_ov = pm_chg_get_rt_status(chip, VBAT_OV_IRQ);
1416 batfet = pm_chg_get_rt_status(chip, BATFET_IRQ);
1417
1418 if (!dc_present) {
1419 pr_warn("%s. dc not present.\n", __func__);
1420 return;
1421 }
1422 if (!batt_present) {
1423 pr_warn("%s. battery not present.\n", __func__);
1424 return;
1425 }
1426 if (!batt_temp_ok) {
1427 pr_warn("%s. battery temperature not ok.\n", __func__);
1428 return;
1429 }
1430 if (vbat_ov) {
1431 pr_warn("%s. battery over voltage.\n", __func__);
1432 return;
1433 }
1434 if (!batfet) {
1435 pr_warn("%s. battery FET not closed.\n", __func__);
1436 return;
1437 }
1438
1439 chip->ext->start_charging(chip->ext->ctx);
1440 chip->ext_charging = true;
1441 chip->ext_charge_done = false;
1442 /* Start BMS */
1443 schedule_delayed_work(&chip->eoc_work, delay);
1444 wake_lock(&chip->eoc_wake_lock);
1445}
1446
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001447static void handle_dc_removal_insertion(struct pm8921_chg_chip *chip)
1448{
1449 int dc_present;
1450
1451 dc_present = is_dc_chg_plugged_in(chip);
1452 if (chip->dc_present ^ dc_present) {
1453 chip->dc_present = dc_present;
1454 power_supply_changed(&chip->dc_psy);
Abhijeet Dharmapurikar0c4bc152011-10-27 10:22:24 -07001455 power_supply_changed(&chip->batt_psy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001456 }
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07001457 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001458}
1459
1460static irqreturn_t usbin_valid_irq_handler(int irq, void *data)
1461{
1462 handle_usb_insertion_removal(data);
1463 return IRQ_HANDLED;
1464}
1465
1466static irqreturn_t usbin_ov_irq_handler(int irq, void *data)
1467{
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -07001468 pr_err("USB OverVoltage\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001469 return IRQ_HANDLED;
1470}
1471
1472static irqreturn_t batt_inserted_irq_handler(int irq, void *data)
1473{
1474 struct pm8921_chg_chip *chip = data;
1475 int status;
1476
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07001477 status = pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ);
1478 schedule_work(&chip->battery_id_valid_work);
Amir Samuelovd31ef502011-10-26 14:41:36 +02001479 handle_start_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001480 pr_debug("battery present=%d", status);
1481 power_supply_changed(&chip->batt_psy);
1482 return IRQ_HANDLED;
1483}
Amir Samuelovd31ef502011-10-26 14:41:36 +02001484
1485/*
1486 * this interrupt used to restart charging a battery.
1487 *
1488 * Note: When DC-inserted the VBAT can't go low.
1489 * VPH_PWR is provided by the ext-charger.
1490 * After End-Of-Charging from DC, charging can be resumed only
1491 * if DC is removed and then inserted after the battery was in use.
1492 * Therefore the handle_start_ext_chg() is not called.
1493 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001494static irqreturn_t vbatdet_low_irq_handler(int irq, void *data)
1495{
1496 struct pm8921_chg_chip *chip = data;
1497
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001498 pm8921_chg_disable_irq(chip, VBATDET_LOW_IRQ);
Abhijeet Dharmapurikar07e48bf2011-09-26 19:46:40 -07001499
1500 /* enable auto charging */
1501 pm_chg_auto_enable(chip, !charging_disabled);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001502 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001503
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001504 power_supply_changed(&chip->batt_psy);
1505 power_supply_changed(&chip->usb_psy);
1506 power_supply_changed(&chip->dc_psy);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001507
1508 pm8921_chg_enable_irq(chip, FASTCHG_IRQ);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001509 return IRQ_HANDLED;
1510}
1511
1512static irqreturn_t usbin_uv_irq_handler(int irq, void *data)
1513{
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -07001514 pr_err("USB UnderVoltage\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001515 return IRQ_HANDLED;
1516}
1517
1518static irqreturn_t vbat_ov_irq_handler(int irq, void *data)
1519{
1520 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1521 return IRQ_HANDLED;
1522}
1523
1524static irqreturn_t chgwdog_irq_handler(int irq, void *data)
1525{
1526 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1527 return IRQ_HANDLED;
1528}
1529
1530static irqreturn_t vcp_irq_handler(int irq, void *data)
1531{
1532 pr_warning("VCP triggered BATDET forced on\n");
1533 pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
1534 return IRQ_HANDLED;
1535}
1536
1537static irqreturn_t atcdone_irq_handler(int irq, void *data)
1538{
1539 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1540 return IRQ_HANDLED;
1541}
1542
1543static irqreturn_t atcfail_irq_handler(int irq, void *data)
1544{
1545 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1546 return IRQ_HANDLED;
1547}
1548
1549static irqreturn_t chgdone_irq_handler(int irq, void *data)
1550{
1551 struct pm8921_chg_chip *chip = data;
1552
1553 pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
Amir Samuelovd31ef502011-10-26 14:41:36 +02001554
1555 handle_stop_ext_chg(chip);
1556
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001557 power_supply_changed(&chip->batt_psy);
1558 power_supply_changed(&chip->usb_psy);
1559 power_supply_changed(&chip->dc_psy);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001560
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001561 chip->bms_notify.is_battery_full = 1;
1562 bms_notify_check(chip);
1563
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001564 /*
1565 * since charging is now done, start monitoring for
1566 * battery voltage below resume voltage
1567 */
1568 pm8921_chg_enable_irq(chip, VBATDET_LOW_IRQ);
1569
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001570 return IRQ_HANDLED;
1571}
1572
1573static irqreturn_t chgfail_irq_handler(int irq, void *data)
1574{
1575 struct pm8921_chg_chip *chip = data;
David Keitel753ec8d2011-11-02 10:56:37 -07001576 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001577
David Keitel753ec8d2011-11-02 10:56:37 -07001578 ret = pm_chg_failed_clear(chip, 1);
1579 if (ret)
1580 pr_err("Failed to write CHG_FAILED_CLEAR bit\n");
1581
1582 pr_err("batt_present = %d, batt_temp_ok = %d, state_changed_to=%d\n",
1583 get_prop_batt_present(chip),
1584 pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ),
1585 pm_chg_get_fsm_state(data));
1586
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001587 power_supply_changed(&chip->batt_psy);
1588 power_supply_changed(&chip->usb_psy);
1589 power_supply_changed(&chip->dc_psy);
1590 return IRQ_HANDLED;
1591}
1592
1593static irqreturn_t chgstate_irq_handler(int irq, void *data)
1594{
1595 struct pm8921_chg_chip *chip = data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001596
1597 pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
1598 power_supply_changed(&chip->batt_psy);
1599 power_supply_changed(&chip->usb_psy);
1600 power_supply_changed(&chip->dc_psy);
1601
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07001602 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001603
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001604 return IRQ_HANDLED;
1605}
1606
1607static irqreturn_t loop_change_irq_handler(int irq, void *data)
1608{
1609 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1610 return IRQ_HANDLED;
1611}
1612
1613static irqreturn_t fastchg_irq_handler(int irq, void *data)
1614{
1615 struct pm8921_chg_chip *chip = data;
1616
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001617 /* disable this irq now, reenable it when resuming charging */
1618 pm8921_chg_disable_irq(chip, FASTCHG_IRQ);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001619 power_supply_changed(&chip->batt_psy);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001620 wake_lock(&chip->eoc_wake_lock);
1621 schedule_delayed_work(&chip->eoc_work,
1622 round_jiffies_relative(msecs_to_jiffies
1623 (EOC_CHECK_PERIOD_MS)));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001624 return IRQ_HANDLED;
1625}
1626
1627static irqreturn_t trklchg_irq_handler(int irq, void *data)
1628{
1629 struct pm8921_chg_chip *chip = data;
1630
1631 power_supply_changed(&chip->batt_psy);
1632 return IRQ_HANDLED;
1633}
1634
1635static irqreturn_t batt_removed_irq_handler(int irq, void *data)
1636{
1637 struct pm8921_chg_chip *chip = data;
1638 int status;
1639
1640 status = pm_chg_get_rt_status(chip, BATT_REMOVED_IRQ);
1641 pr_debug("battery present=%d state=%d", !status,
1642 pm_chg_get_fsm_state(data));
Amir Samuelovd31ef502011-10-26 14:41:36 +02001643 handle_stop_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001644 power_supply_changed(&chip->batt_psy);
1645 return IRQ_HANDLED;
1646}
1647
1648static irqreturn_t batttemp_hot_irq_handler(int irq, void *data)
1649{
1650 struct pm8921_chg_chip *chip = data;
1651
Amir Samuelovd31ef502011-10-26 14:41:36 +02001652 handle_stop_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001653 power_supply_changed(&chip->batt_psy);
1654 return IRQ_HANDLED;
1655}
1656
1657static irqreturn_t chghot_irq_handler(int irq, void *data)
1658{
1659 struct pm8921_chg_chip *chip = data;
1660
1661 pr_debug("Chg hot fsm_state=%d\n", pm_chg_get_fsm_state(data));
1662 power_supply_changed(&chip->batt_psy);
1663 power_supply_changed(&chip->usb_psy);
1664 power_supply_changed(&chip->dc_psy);
1665 return IRQ_HANDLED;
1666}
1667
1668static irqreturn_t batttemp_cold_irq_handler(int irq, void *data)
1669{
1670 struct pm8921_chg_chip *chip = data;
1671
1672 pr_debug("Batt cold fsm_state=%d\n", pm_chg_get_fsm_state(data));
Amir Samuelovd31ef502011-10-26 14:41:36 +02001673 handle_stop_ext_chg(chip);
1674
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675 power_supply_changed(&chip->batt_psy);
1676 power_supply_changed(&chip->usb_psy);
1677 power_supply_changed(&chip->dc_psy);
1678 return IRQ_HANDLED;
1679}
1680
1681static irqreturn_t chg_gone_irq_handler(int irq, void *data)
1682{
1683 struct pm8921_chg_chip *chip = data;
1684
1685 pr_debug("Chg gone fsm_state=%d\n", pm_chg_get_fsm_state(data));
1686 power_supply_changed(&chip->batt_psy);
1687 power_supply_changed(&chip->usb_psy);
1688 power_supply_changed(&chip->dc_psy);
1689 return IRQ_HANDLED;
1690}
1691
1692static irqreturn_t bat_temp_ok_irq_handler(int irq, void *data)
1693{
1694 struct pm8921_chg_chip *chip = data;
1695
1696 pr_debug("batt temp ok fsm_state=%d\n", pm_chg_get_fsm_state(data));
Amir Samuelovd31ef502011-10-26 14:41:36 +02001697 handle_start_ext_chg(chip);
1698
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001699 power_supply_changed(&chip->batt_psy);
1700 power_supply_changed(&chip->usb_psy);
1701 power_supply_changed(&chip->dc_psy);
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07001702 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001703 return IRQ_HANDLED;
1704}
1705
1706static irqreturn_t coarse_det_low_irq_handler(int irq, void *data)
1707{
1708 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1709 return IRQ_HANDLED;
1710}
1711
1712static irqreturn_t vdd_loop_irq_handler(int irq, void *data)
1713{
1714 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1715 return IRQ_HANDLED;
1716}
1717
1718static irqreturn_t vreg_ov_irq_handler(int irq, void *data)
1719{
1720 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1721 return IRQ_HANDLED;
1722}
1723
1724static irqreturn_t vbatdet_irq_handler(int irq, void *data)
1725{
1726 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1727 return IRQ_HANDLED;
1728}
1729
1730static irqreturn_t batfet_irq_handler(int irq, void *data)
1731{
1732 struct pm8921_chg_chip *chip = data;
1733
1734 pr_debug("vreg ov\n");
1735 power_supply_changed(&chip->batt_psy);
1736 return IRQ_HANDLED;
1737}
1738
1739static irqreturn_t dcin_valid_irq_handler(int irq, void *data)
1740{
Amir Samuelovd31ef502011-10-26 14:41:36 +02001741 struct pm8921_chg_chip *chip = data;
1742
1743 pm8921_disable_source_current(true); /* Force BATFET=ON */
1744
1745 handle_dc_removal_insertion(chip);
1746 handle_start_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001747 return IRQ_HANDLED;
1748}
1749
1750static irqreturn_t dcin_ov_irq_handler(int irq, void *data)
1751{
Amir Samuelovd31ef502011-10-26 14:41:36 +02001752 struct pm8921_chg_chip *chip = data;
1753
1754 pm8921_disable_source_current(false); /* release BATFET */
1755
1756 handle_dc_removal_insertion(chip);
1757 handle_stop_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001758 return IRQ_HANDLED;
1759}
1760
1761static irqreturn_t dcin_uv_irq_handler(int irq, void *data)
1762{
Amir Samuelovd31ef502011-10-26 14:41:36 +02001763 struct pm8921_chg_chip *chip = data;
1764
1765 pm8921_disable_source_current(false); /* release BATFET */
1766 handle_stop_ext_chg(chip);
1767
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001768 return IRQ_HANDLED;
1769}
1770
Abhijeet Dharmapurikar33fe6fb2011-09-14 16:03:11 -07001771/**
1772 * update_heartbeat - internal function to update userspace
1773 * per update_time minutes
1774 *
1775 */
1776static void update_heartbeat(struct work_struct *work)
1777{
1778 struct delayed_work *dwork = to_delayed_work(work);
1779 struct pm8921_chg_chip *chip = container_of(dwork,
1780 struct pm8921_chg_chip, update_heartbeat_work);
1781
1782 power_supply_changed(&chip->batt_psy);
1783 schedule_delayed_work(&chip->update_heartbeat_work,
1784 round_jiffies_relative(msecs_to_jiffies
1785 (chip->update_time)));
1786}
1787
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001788/**
Amir Samuelovd31ef502011-10-26 14:41:36 +02001789 * eoc_worker - internal function to check if battery EOC
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001790 * has happened
1791 *
1792 * If all conditions favouring, if the charge current is
1793 * less than the term current for three consecutive times
1794 * an EOC has happened.
1795 * The wakelock is released if there is no need to reshedule
1796 * - this happens when the battery is removed or EOC has
1797 * happened
1798 */
1799#define CONSECUTIVE_COUNT 3
1800#define VBAT_TOLERANCE_MV 70
1801#define CHG_DISABLE_MSLEEP 100
Amir Samuelovd31ef502011-10-26 14:41:36 +02001802static void eoc_worker(struct work_struct *work)
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001803{
1804 struct delayed_work *dwork = to_delayed_work(work);
1805 struct pm8921_chg_chip *chip = container_of(dwork,
1806 struct pm8921_chg_chip, eoc_work);
1807 int vbat_meas, vbat_programmed;
1808 int ichg_meas, iterm_programmed;
1809 int regulation_loop, fast_chg, vcp;
1810 int rc;
1811 static int count;
1812
Amir Samuelovd31ef502011-10-26 14:41:36 +02001813 if (!is_ext_charging(chip)) {
1814 /* return if the battery is not being fastcharged */
1815 fast_chg = pm_chg_get_rt_status(chip, FASTCHG_IRQ);
1816 pr_debug("fast_chg = %d\n", fast_chg);
1817 if (fast_chg == 0) {
1818 /* enable fastchg irq */
1819 pm8921_chg_enable_irq(chip, FASTCHG_IRQ);
1820 count = 0;
1821 wake_unlock(&chip->eoc_wake_lock);
1822 return;
1823 }
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001824
Amir Samuelovd31ef502011-10-26 14:41:36 +02001825 vcp = pm_chg_get_rt_status(chip, VCP_IRQ);
1826 pr_debug("vcp = %d\n", vcp);
1827 if (vcp == 1)
1828 goto reset_and_reschedule;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001829
Amir Samuelovd31ef502011-10-26 14:41:36 +02001830 /* reset count if battery is hot/cold */
1831 rc = pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ);
1832 pr_debug("batt_temp_ok = %d\n", rc);
1833 if (rc == 0)
1834 goto reset_and_reschedule;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001835
Amir Samuelovd31ef502011-10-26 14:41:36 +02001836 /* reset count if battery voltage is less than vddmax */
1837 vbat_meas = get_prop_battery_mvolts(chip);
1838 if (vbat_meas < 0)
1839 goto reset_and_reschedule;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001840
Amir Samuelovd31ef502011-10-26 14:41:36 +02001841 rc = pm_chg_vddmax_get(chip, &vbat_programmed);
1842 if (rc) {
1843 pr_err("couldnt read vddmax rc = %d\n", rc);
1844 goto reset_and_reschedule;
1845 }
1846 pr_debug("vddmax = %d vbat_meas=%d\n",
1847 vbat_programmed, vbat_meas);
1848 if (vbat_meas < vbat_programmed - VBAT_TOLERANCE_MV)
1849 goto reset_and_reschedule;
1850 } /* !is_ext_charging */
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001851
1852 /* reset count if battery chg current is more than iterm */
1853 rc = pm_chg_iterm_get(chip, &iterm_programmed);
1854 if (rc) {
1855 pr_err("couldnt read iterm rc = %d\n", rc);
1856 goto reset_and_reschedule;
1857 }
1858
1859 ichg_meas = get_prop_batt_current(chip);
1860 pr_debug("iterm_programmed = %d ichg_meas=%d\n",
1861 iterm_programmed, ichg_meas);
1862 /*
1863 * ichg_meas < 0 means battery is drawing current
1864 * ichg_meas > 0 means battery is providing current
1865 */
1866 if (ichg_meas > 0)
1867 goto reset_and_reschedule;
1868
1869 if (ichg_meas * -1 > iterm_programmed)
1870 goto reset_and_reschedule;
1871
Amir Samuelovd31ef502011-10-26 14:41:36 +02001872 if (!is_ext_charging(chip)) {
1873 /*
1874 * TODO if charging from an external charger
1875 * check SOC instead of regulation loop
1876 */
1877 regulation_loop = pm_chg_get_regulation_loop(chip);
1878 if (regulation_loop < 0) {
1879 pr_err("couldnt read the regulation loop err=%d\n",
1880 regulation_loop);
1881 goto reset_and_reschedule;
1882 }
1883 pr_debug("regulation_loop=%d\n", regulation_loop);
Abhijeet Dharmapurikar07e48bf2011-09-26 19:46:40 -07001884
Amir Samuelovd31ef502011-10-26 14:41:36 +02001885 if (regulation_loop != 0 && regulation_loop != VDD_LOOP)
1886 goto reset_and_reschedule;
1887 } /* !is_ext_charging */
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001888
1889 count++;
1890 if (count == CONSECUTIVE_COUNT) {
1891 count = 0;
1892 pr_info("End of Charging\n");
1893
1894 pm_chg_auto_enable(chip, 0);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001895
Amir Samuelovd31ef502011-10-26 14:41:36 +02001896 if (is_ext_charging(chip))
1897 chip->ext_charge_done = true;
1898
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001899 /* declare end of charging by invoking chgdone interrupt */
1900 chgdone_irq_handler(chip->pmic_chg_irq[CHGDONE_IRQ], chip);
1901 wake_unlock(&chip->eoc_wake_lock);
1902 return;
1903 } else {
1904 pr_debug("EOC count = %d\n", count);
1905 goto reschedule;
1906 }
1907
1908reset_and_reschedule:
1909 count = 0;
1910reschedule:
1911 schedule_delayed_work(&chip->eoc_work,
1912 round_jiffies_relative(msecs_to_jiffies
1913 (EOC_CHECK_PERIOD_MS)));
1914}
1915
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001916static void btm_configure_work(struct work_struct *work)
1917{
1918 int rc;
1919
1920 rc = pm8921_adc_btm_configure(&btm_config);
1921 if (rc)
1922 pr_err("failed to configure btm rc=%d", rc);
1923}
1924
1925DECLARE_WORK(btm_config_work, btm_configure_work);
1926
1927#define TEMP_HYSTERISIS_DEGC 2
1928static void battery_cool(bool enter)
1929{
1930 pr_debug("enter = %d\n", enter);
1931 if (enter) {
1932 btm_config.low_thr_temp =
1933 the_chip->cool_temp + TEMP_HYSTERISIS_DEGC;
1934 pm_chg_ibatmax_set(the_chip, the_chip->cool_bat_chg_current);
1935 pm_chg_vddmax_set(the_chip, the_chip->cool_bat_voltage);
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07001936 pm_chg_vbatdet_set(the_chip,
1937 the_chip->cool_bat_voltage
1938 - the_chip->resume_voltage_delta);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001939 } else {
1940 btm_config.low_thr_temp = the_chip->cool_temp;
1941 pm_chg_ibatmax_set(the_chip, the_chip->max_bat_chg_current);
1942 pm_chg_vddmax_set(the_chip, the_chip->max_voltage);
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07001943 pm_chg_vbatdet_set(the_chip,
1944 the_chip->max_voltage - the_chip->resume_voltage_delta);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001945 }
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07001946 the_chip->is_bat_cool = enter;
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001947 schedule_work(&btm_config_work);
1948}
1949
1950static void battery_warm(bool enter)
1951{
1952 pr_debug("enter = %d\n", enter);
1953 if (enter) {
1954 btm_config.high_thr_temp =
1955 the_chip->warm_temp - TEMP_HYSTERISIS_DEGC;
1956 pm_chg_ibatmax_set(the_chip, the_chip->warm_bat_chg_current);
1957 pm_chg_vddmax_set(the_chip, the_chip->warm_bat_voltage);
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07001958 pm_chg_vbatdet_set(the_chip,
1959 the_chip->warm_bat_voltage
1960 - the_chip->resume_voltage_delta);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001961 } else {
1962 btm_config.high_thr_temp = the_chip->warm_temp;
1963 pm_chg_ibatmax_set(the_chip, the_chip->max_bat_chg_current);
1964 pm_chg_vddmax_set(the_chip, the_chip->max_voltage);
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07001965 pm_chg_vbatdet_set(the_chip,
1966 the_chip->max_voltage - the_chip->resume_voltage_delta);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001967 }
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07001968 the_chip->is_bat_warm = enter;
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001969 schedule_work(&btm_config_work);
1970}
1971
1972static int configure_btm(struct pm8921_chg_chip *chip)
1973{
1974 int rc;
1975
1976 btm_config.btm_warm_fn = battery_warm;
1977 btm_config.btm_cool_fn = battery_cool;
1978 btm_config.low_thr_temp = chip->cool_temp;
1979 btm_config.high_thr_temp = chip->warm_temp;
1980 btm_config.interval = chip->temp_check_period;
1981 rc = pm8921_adc_btm_configure(&btm_config);
1982 if (rc)
1983 pr_err("failed to configure btm rc = %d\n", rc);
1984 rc = pm8921_adc_btm_start();
1985 if (rc)
1986 pr_err("failed to start btm rc = %d\n", rc);
1987
1988 return rc;
1989}
1990
Amir Samuelovd31ef502011-10-26 14:41:36 +02001991int register_external_dc_charger(struct ext_chg_pm8921 *ext)
1992{
1993 if (the_chip == NULL) {
1994 pr_err("called too early\n");
1995 return -EINVAL;
1996 }
1997 /* TODO check function pointers */
1998 the_chip->ext = ext;
1999 the_chip->ext_charging = false;
2000
2001 if (is_dc_chg_plugged_in(the_chip))
2002 pm8921_disable_source_current(true); /* Force BATFET=ON */
2003
2004 handle_start_ext_chg(the_chip);
2005
2006 return 0;
2007}
2008EXPORT_SYMBOL(register_external_dc_charger);
2009
2010void unregister_external_dc_charger(struct ext_chg_pm8921 *ext)
2011{
2012 if (the_chip == NULL) {
2013 pr_err("called too early\n");
2014 return;
2015 }
2016 handle_stop_ext_chg(the_chip);
2017 the_chip->ext = NULL;
2018}
2019EXPORT_SYMBOL(unregister_external_dc_charger);
2020
Abhijeet Dharmapurikar1f52c412011-08-12 17:11:45 -07002021/**
2022 * set_disable_status_param -
2023 *
2024 * Internal function to disable battery charging and also disable drawing
2025 * any current from the source. The device is forced to run on a battery
2026 * after this.
2027 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002028static int set_disable_status_param(const char *val, struct kernel_param *kp)
2029{
2030 int ret;
2031 struct pm8921_chg_chip *chip = the_chip;
2032
2033 ret = param_set_int(val, kp);
2034 if (ret) {
2035 pr_err("error setting value %d\n", ret);
2036 return ret;
2037 }
2038 pr_info("factory set disable param to %d\n", charging_disabled);
2039 if (chip) {
2040 pm_chg_auto_enable(chip, !charging_disabled);
2041 pm_chg_charge_dis(chip, charging_disabled);
2042 }
2043 return 0;
2044}
2045module_param_call(disabled, set_disable_status_param, param_get_uint,
2046 &charging_disabled, 0644);
2047
Abhijeet Dharmapurikarad742362011-08-29 19:50:02 -07002048/**
2049 * set_thermal_mitigation_level -
2050 *
2051 * Internal function to control battery charging current to reduce
2052 * temperature
2053 */
2054static int set_therm_mitigation_level(const char *val, struct kernel_param *kp)
2055{
2056 int ret;
2057 struct pm8921_chg_chip *chip = the_chip;
2058
2059 ret = param_set_int(val, kp);
2060 if (ret) {
2061 pr_err("error setting value %d\n", ret);
2062 return ret;
2063 }
2064
2065 if (!chip) {
2066 pr_err("called before init\n");
2067 return -EINVAL;
2068 }
2069
2070 if (!chip->thermal_mitigation) {
2071 pr_err("no thermal mitigation\n");
2072 return -EINVAL;
2073 }
2074
2075 if (thermal_mitigation < 0
2076 || thermal_mitigation >= chip->thermal_levels) {
2077 pr_err("out of bound level selected\n");
2078 return -EINVAL;
2079 }
2080
2081 ret = pm_chg_ibatmax_set(chip,
2082 chip->thermal_mitigation[thermal_mitigation]);
2083 return ret;
2084}
2085module_param_call(thermal_mitigation, set_therm_mitigation_level,
2086 param_get_uint,
2087 &thermal_mitigation, 0644);
2088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002089static void free_irqs(struct pm8921_chg_chip *chip)
2090{
2091 int i;
2092
2093 for (i = 0; i < PM_CHG_MAX_INTS; i++)
2094 if (chip->pmic_chg_irq[i]) {
2095 free_irq(chip->pmic_chg_irq[i], chip);
2096 chip->pmic_chg_irq[i] = 0;
2097 }
2098}
2099
2100/* determines the initial present states and notifies msm_charger */
2101static void __devinit determine_initial_state(struct pm8921_chg_chip *chip)
2102{
2103 unsigned long flags;
2104 int fsm_state;
2105
2106 chip->dc_present = !!is_dc_chg_plugged_in(chip);
2107 chip->usb_present = !!is_usb_chg_plugged_in(chip);
2108
2109 notify_usb_of_the_plugin_event(chip->usb_present);
2110
2111 pm8921_chg_enable_irq(chip, DCIN_VALID_IRQ);
2112 pm8921_chg_enable_irq(chip, USBIN_VALID_IRQ);
2113 pm8921_chg_enable_irq(chip, BATT_REMOVED_IRQ);
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002114 pm8921_chg_enable_irq(chip, BATT_INSERTED_IRQ);
2115 pm8921_chg_enable_irq(chip, USBIN_OV_IRQ);
2116 pm8921_chg_enable_irq(chip, USBIN_UV_IRQ);
2117 pm8921_chg_enable_irq(chip, DCIN_OV_IRQ);
2118 pm8921_chg_enable_irq(chip, DCIN_UV_IRQ);
David Keitel753ec8d2011-11-02 10:56:37 -07002119 pm8921_chg_enable_irq(chip, CHGFAIL_IRQ);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002120 pm8921_chg_enable_irq(chip, FASTCHG_IRQ);
2121 pm8921_chg_enable_irq(chip, VBATDET_LOW_IRQ);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002122
2123 spin_lock_irqsave(&vbus_lock, flags);
2124 if (usb_chg_current) {
2125 /* reissue a vbus draw call */
2126 __pm8921_charger_vbus_draw(usb_chg_current);
2127 }
2128 spin_unlock_irqrestore(&vbus_lock, flags);
2129
2130 fsm_state = pm_chg_get_fsm_state(chip);
2131 if (is_battery_charging(fsm_state)) {
2132 chip->bms_notify.is_charging = 1;
2133 pm8921_bms_charging_began();
2134 }
2135
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07002136 check_battery_valid(chip);
2137
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002138 pr_debug("usb = %d, dc = %d batt = %d state=%d\n",
2139 chip->usb_present,
2140 chip->dc_present,
2141 get_prop_batt_present(chip),
2142 fsm_state);
2143}
2144
2145struct pm_chg_irq_init_data {
2146 unsigned int irq_id;
2147 char *name;
2148 unsigned long flags;
2149 irqreturn_t (*handler)(int, void *);
2150};
2151
2152#define CHG_IRQ(_id, _flags, _handler) \
2153{ \
2154 .irq_id = _id, \
2155 .name = #_id, \
2156 .flags = _flags, \
2157 .handler = _handler, \
2158}
2159struct pm_chg_irq_init_data chg_irq_data[] = {
2160 CHG_IRQ(USBIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2161 usbin_valid_irq_handler),
Abhijeet Dharmapurikar956215b2011-10-19 15:30:14 -07002162 CHG_IRQ(USBIN_OV_IRQ, IRQF_TRIGGER_RISING, usbin_ov_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002163 CHG_IRQ(BATT_INSERTED_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2164 batt_inserted_irq_handler),
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002165 CHG_IRQ(VBATDET_LOW_IRQ, IRQF_TRIGGER_HIGH, vbatdet_low_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002166 CHG_IRQ(USBIN_UV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2167 usbin_uv_irq_handler),
2168 CHG_IRQ(VBAT_OV_IRQ, IRQF_TRIGGER_RISING, vbat_ov_irq_handler),
2169 CHG_IRQ(CHGWDOG_IRQ, IRQF_TRIGGER_RISING, chgwdog_irq_handler),
2170 CHG_IRQ(VCP_IRQ, IRQF_TRIGGER_RISING, vcp_irq_handler),
2171 CHG_IRQ(ATCDONE_IRQ, IRQF_TRIGGER_RISING, atcdone_irq_handler),
2172 CHG_IRQ(ATCFAIL_IRQ, IRQF_TRIGGER_RISING, atcfail_irq_handler),
2173 CHG_IRQ(CHGDONE_IRQ, IRQF_TRIGGER_RISING, chgdone_irq_handler),
2174 CHG_IRQ(CHGFAIL_IRQ, IRQF_TRIGGER_RISING, chgfail_irq_handler),
2175 CHG_IRQ(CHGSTATE_IRQ, IRQF_TRIGGER_RISING, chgstate_irq_handler),
2176 CHG_IRQ(LOOP_CHANGE_IRQ, IRQF_TRIGGER_RISING, loop_change_irq_handler),
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002177 CHG_IRQ(FASTCHG_IRQ, IRQF_TRIGGER_HIGH, fastchg_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002178 CHG_IRQ(TRKLCHG_IRQ, IRQF_TRIGGER_RISING, trklchg_irq_handler),
2179 CHG_IRQ(BATT_REMOVED_IRQ, IRQF_TRIGGER_RISING,
2180 batt_removed_irq_handler),
2181 CHG_IRQ(BATTTEMP_HOT_IRQ, IRQF_TRIGGER_RISING,
2182 batttemp_hot_irq_handler),
2183 CHG_IRQ(CHGHOT_IRQ, IRQF_TRIGGER_RISING, chghot_irq_handler),
2184 CHG_IRQ(BATTTEMP_COLD_IRQ, IRQF_TRIGGER_RISING,
2185 batttemp_cold_irq_handler),
2186 CHG_IRQ(CHG_GONE_IRQ, IRQF_TRIGGER_RISING, chg_gone_irq_handler),
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07002187 CHG_IRQ(BAT_TEMP_OK_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2188 bat_temp_ok_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002189 CHG_IRQ(COARSE_DET_LOW_IRQ, IRQF_TRIGGER_RISING,
2190 coarse_det_low_irq_handler),
2191 CHG_IRQ(VDD_LOOP_IRQ, IRQF_TRIGGER_RISING, vdd_loop_irq_handler),
2192 CHG_IRQ(VREG_OV_IRQ, IRQF_TRIGGER_RISING, vreg_ov_irq_handler),
2193 CHG_IRQ(VBATDET_IRQ, IRQF_TRIGGER_RISING, vbatdet_irq_handler),
2194 CHG_IRQ(BATFET_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2195 batfet_irq_handler),
2196 CHG_IRQ(DCIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2197 dcin_valid_irq_handler),
2198 CHG_IRQ(DCIN_OV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2199 dcin_ov_irq_handler),
Abhijeet Dharmapurikar956215b2011-10-19 15:30:14 -07002200 CHG_IRQ(DCIN_UV_IRQ, IRQF_TRIGGER_RISING, dcin_uv_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002201};
2202
2203static int __devinit request_irqs(struct pm8921_chg_chip *chip,
2204 struct platform_device *pdev)
2205{
2206 struct resource *res;
2207 int ret, i;
2208
2209 ret = 0;
2210 bitmap_fill(chip->enabled_irqs, PM_CHG_MAX_INTS);
2211
2212 for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) {
2213 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
2214 chg_irq_data[i].name);
2215 if (res == NULL) {
2216 pr_err("couldn't find %s\n", chg_irq_data[i].name);
2217 goto err_out;
2218 }
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002219 chip->pmic_chg_irq[chg_irq_data[i].irq_id] = res->start;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002220 ret = request_irq(res->start, chg_irq_data[i].handler,
2221 chg_irq_data[i].flags,
2222 chg_irq_data[i].name, chip);
2223 if (ret < 0) {
2224 pr_err("couldn't request %d (%s) %d\n", res->start,
2225 chg_irq_data[i].name, ret);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002226 chip->pmic_chg_irq[chg_irq_data[i].irq_id] = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002227 goto err_out;
2228 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002229 pm8921_chg_disable_irq(chip, chg_irq_data[i].irq_id);
2230 }
2231 return 0;
2232
2233err_out:
2234 free_irqs(chip);
2235 return -EINVAL;
2236}
2237
2238#define ENUM_TIMER_STOP_BIT BIT(1)
2239#define BOOT_DONE_BIT BIT(6)
2240#define CHG_BATFET_ON_BIT BIT(3)
2241#define CHG_VCP_EN BIT(0)
2242#define CHG_BAT_TEMP_DIS_BIT BIT(2)
2243#define SAFE_CURRENT_MA 1500
2244static int __devinit pm8921_chg_hw_init(struct pm8921_chg_chip *chip)
2245{
2246 int rc;
2247
2248 rc = pm_chg_masked_write(chip, SYS_CONFIG_2,
2249 BOOT_DONE_BIT, BOOT_DONE_BIT);
2250 if (rc) {
2251 pr_err("Failed to set BOOT_DONE_BIT rc=%d\n", rc);
2252 return rc;
2253 }
2254
2255 rc = pm_chg_vddsafe_set(chip, chip->max_voltage);
2256 if (rc) {
2257 pr_err("Failed to set safe voltage to %d rc=%d\n",
2258 chip->max_voltage, rc);
2259 return rc;
2260 }
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002261 rc = pm_chg_vbatdet_set(chip,
2262 chip->max_voltage - chip->resume_voltage_delta);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002263 if (rc) {
2264 pr_err("Failed to set vbatdet comprator voltage to %d rc=%d\n",
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002265 chip->max_voltage - chip->resume_voltage_delta, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002266 return rc;
2267 }
2268
2269 rc = pm_chg_vddmax_set(chip, chip->max_voltage);
2270 if (rc) {
2271 pr_err("Failed to set max voltage to %d rc=%d\n",
2272 chip->max_voltage, rc);
2273 return rc;
2274 }
2275 rc = pm_chg_ibatsafe_set(chip, SAFE_CURRENT_MA);
2276 if (rc) {
2277 pr_err("Failed to set max voltage to %d rc=%d\n",
2278 SAFE_CURRENT_MA, rc);
2279 return rc;
2280 }
2281
2282 /* TODO needs to be changed as per the temeperature of the battery */
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002283 rc = pm_chg_ibatmax_set(chip, chip->max_bat_chg_current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002284 if (rc) {
2285 pr_err("Failed to set max current to 400 rc=%d\n", rc);
2286 return rc;
2287 }
2288
2289 rc = pm_chg_iterm_set(chip, chip->term_current);
2290 if (rc) {
2291 pr_err("Failed to set term current to %d rc=%d\n",
2292 chip->term_current, rc);
2293 return rc;
2294 }
2295
2296 /* Disable the ENUM TIMER */
2297 rc = pm_chg_masked_write(chip, PBL_ACCESS2, ENUM_TIMER_STOP_BIT,
2298 ENUM_TIMER_STOP_BIT);
2299 if (rc) {
2300 pr_err("Failed to set enum timer stop rc=%d\n", rc);
2301 return rc;
2302 }
2303
2304 /* init with the lowest USB current */
2305 rc = pm_chg_iusbmax_set(chip, usb_ma_table[0].chg_iusb_value);
2306 if (rc) {
2307 pr_err("Failed to set usb max to %d rc=%d\n",
2308 usb_ma_table[0].chg_iusb_value, rc);
2309 return rc;
2310 }
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002311
2312 if (chip->safety_time != 0) {
2313 rc = pm_chg_tchg_max_set(chip, chip->safety_time);
2314 if (rc) {
2315 pr_err("Failed to set max time to %d minutes rc=%d\n",
2316 chip->safety_time, rc);
2317 return rc;
2318 }
2319 }
2320
2321 if (chip->ttrkl_time != 0) {
Abhijeet Dharmapurikare45c3d32011-10-19 19:46:48 -07002322 rc = pm_chg_ttrkl_max_set(chip, chip->ttrkl_time);
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002323 if (rc) {
2324 pr_err("Failed to set trkl time to %d minutes rc=%d\n",
2325 chip->safety_time, rc);
2326 return rc;
2327 }
2328 }
2329
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -07002330 if (chip->vin_min != 0) {
2331 rc = pm_chg_vinmin_set(chip, chip->vin_min);
2332 if (rc) {
2333 pr_err("Failed to set vin min to %d mV rc=%d\n",
2334 chip->vin_min, rc);
2335 return rc;
2336 }
2337 }
2338
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002339 rc = pm_chg_disable_wd(chip);
2340 if (rc) {
2341 pr_err("Failed to disable wd rc=%d\n", rc);
2342 return rc;
2343 }
2344
2345 rc = pm_chg_masked_write(chip, CHG_CNTRL_2,
2346 CHG_BAT_TEMP_DIS_BIT, 0);
2347 if (rc) {
2348 pr_err("Failed to enable temp control chg rc=%d\n", rc);
2349 return rc;
2350 }
2351 /* switch to a 3.2Mhz for the buck */
2352 rc = pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CLOCK_CTRL, 0x15);
2353 if (rc) {
2354 pr_err("Failed to switch buck clk rc=%d\n", rc);
2355 return rc;
2356 }
2357
Abhijeet Dharmapurikar26cef9c2011-08-25 19:16:42 -07002358 if (chip->trkl_voltage != 0) {
2359 rc = pm_chg_vtrkl_low_set(chip, chip->trkl_voltage);
2360 if (rc) {
2361 pr_err("Failed to set trkl voltage to %dmv rc=%d\n",
2362 chip->trkl_voltage, rc);
2363 return rc;
2364 }
2365 }
2366
2367 if (chip->weak_voltage != 0) {
2368 rc = pm_chg_vweak_set(chip, chip->weak_voltage);
2369 if (rc) {
2370 pr_err("Failed to set weak voltage to %dmv rc=%d\n",
2371 chip->weak_voltage, rc);
2372 return rc;
2373 }
2374 }
2375
2376 if (chip->trkl_current != 0) {
2377 rc = pm_chg_itrkl_set(chip, chip->trkl_current);
2378 if (rc) {
2379 pr_err("Failed to set trkl current to %dmA rc=%d\n",
2380 chip->trkl_voltage, rc);
2381 return rc;
2382 }
2383 }
2384
2385 if (chip->weak_current != 0) {
2386 rc = pm_chg_iweak_set(chip, chip->weak_current);
2387 if (rc) {
2388 pr_err("Failed to set weak current to %dmA rc=%d\n",
2389 chip->weak_current, rc);
2390 return rc;
2391 }
2392 }
2393
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -07002394 rc = pm_chg_batt_cold_temp_config(chip, chip->cold_thr);
2395 if (rc) {
2396 pr_err("Failed to set cold config %d rc=%d\n",
2397 chip->cold_thr, rc);
2398 }
2399
2400 rc = pm_chg_batt_hot_temp_config(chip, chip->hot_thr);
2401 if (rc) {
2402 pr_err("Failed to set hot config %d rc=%d\n",
2403 chip->hot_thr, rc);
2404 }
2405
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002406 /* Workarounds for die 1.1 and 1.0 */
2407 if (pm8xxx_get_revision(chip->dev->parent) < PM8XXX_REVISION_8921_2p0) {
2408 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST2, 0xF1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002409 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xCE);
2410 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xD8);
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07002411
2412 /* software workaround for correct battery_id detection */
2413 pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_0, 0xFF);
2414 pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_1, 0xFF);
2415 pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_2, 0xFF);
2416 pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_3, 0xFF);
2417 pm8xxx_writeb(chip->dev->parent, PSI_CONFIG_STATUS, 0x0D);
2418 udelay(100);
2419 pm8xxx_writeb(chip->dev->parent, PSI_CONFIG_STATUS, 0x0C);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002420 }
2421
David Keitela3c0d822011-11-03 14:18:52 -07002422 /* Disable EOC FSM processing */
2423 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0x91);
2424
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002425 rc = pm_chg_charge_dis(chip, charging_disabled);
2426 if (rc) {
2427 pr_err("Failed to disable CHG_CHARGE_DIS bit rc=%d\n", rc);
2428 return rc;
2429 }
2430
2431 rc = pm_chg_auto_enable(chip, !charging_disabled);
2432 if (rc) {
2433 pr_err("Failed to enable charging rc=%d\n", rc);
2434 return rc;
2435 }
2436
2437 return 0;
2438}
2439
2440static int get_rt_status(void *data, u64 * val)
2441{
2442 int i = (int)data;
2443 int ret;
2444
2445 /* global irq number is passed in via data */
2446 ret = pm_chg_get_rt_status(the_chip, i);
2447 *val = ret;
2448 return 0;
2449}
2450DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
2451
2452static int get_fsm_status(void *data, u64 * val)
2453{
2454 u8 temp;
2455
2456 temp = pm_chg_get_fsm_state(the_chip);
2457 *val = temp;
2458 return 0;
2459}
2460DEFINE_SIMPLE_ATTRIBUTE(fsm_fops, get_fsm_status, NULL, "%llu\n");
2461
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002462static int get_reg_loop(void *data, u64 * val)
2463{
2464 u8 temp;
2465
2466 if (!the_chip) {
2467 pr_err("%s called before init\n", __func__);
2468 return -EINVAL;
2469 }
2470 temp = pm_chg_get_regulation_loop(the_chip);
2471 *val = temp;
2472 return 0;
2473}
2474DEFINE_SIMPLE_ATTRIBUTE(reg_loop_fops, get_reg_loop, NULL, "0x%02llx\n");
2475
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002476static int get_reg(void *data, u64 * val)
2477{
2478 int addr = (int)data;
2479 int ret;
2480 u8 temp;
2481
2482 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
2483 if (ret) {
2484 pr_err("pm8xxx_readb to %x value =%d errored = %d\n",
2485 addr, temp, ret);
2486 return -EAGAIN;
2487 }
2488 *val = temp;
2489 return 0;
2490}
2491
2492static int set_reg(void *data, u64 val)
2493{
2494 int addr = (int)data;
2495 int ret;
2496 u8 temp;
2497
2498 temp = (u8) val;
2499 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
2500 if (ret) {
2501 pr_err("pm8xxx_writeb to %x value =%d errored = %d\n",
2502 addr, temp, ret);
2503 return -EAGAIN;
2504 }
2505 return 0;
2506}
2507DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
2508
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002509enum {
2510 BAT_WARM_ZONE,
2511 BAT_COOL_ZONE,
2512};
2513static int get_warm_cool(void *data, u64 * val)
2514{
2515 if (!the_chip) {
2516 pr_err("%s called before init\n", __func__);
2517 return -EINVAL;
2518 }
2519 if ((int)data == BAT_WARM_ZONE)
2520 *val = the_chip->is_bat_warm;
2521 if ((int)data == BAT_COOL_ZONE)
2522 *val = the_chip->is_bat_cool;
2523 return 0;
2524}
2525DEFINE_SIMPLE_ATTRIBUTE(warm_cool_fops, get_warm_cool, NULL, "0x%lld\n");
2526
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002527static void create_debugfs_entries(struct pm8921_chg_chip *chip)
2528{
2529 int i;
2530
2531 chip->dent = debugfs_create_dir("pm8921_chg", NULL);
2532
2533 if (IS_ERR(chip->dent)) {
2534 pr_err("pmic charger couldnt create debugfs dir\n");
2535 return;
2536 }
2537
2538 debugfs_create_file("CHG_CNTRL", 0644, chip->dent,
2539 (void *)CHG_CNTRL, &reg_fops);
2540 debugfs_create_file("CHG_CNTRL_2", 0644, chip->dent,
2541 (void *)CHG_CNTRL_2, &reg_fops);
2542 debugfs_create_file("CHG_CNTRL_3", 0644, chip->dent,
2543 (void *)CHG_CNTRL_3, &reg_fops);
2544 debugfs_create_file("PBL_ACCESS1", 0644, chip->dent,
2545 (void *)PBL_ACCESS1, &reg_fops);
2546 debugfs_create_file("PBL_ACCESS2", 0644, chip->dent,
2547 (void *)PBL_ACCESS2, &reg_fops);
2548 debugfs_create_file("SYS_CONFIG_1", 0644, chip->dent,
2549 (void *)SYS_CONFIG_1, &reg_fops);
2550 debugfs_create_file("SYS_CONFIG_2", 0644, chip->dent,
2551 (void *)SYS_CONFIG_2, &reg_fops);
2552 debugfs_create_file("CHG_VDD_MAX", 0644, chip->dent,
2553 (void *)CHG_VDD_MAX, &reg_fops);
2554 debugfs_create_file("CHG_VDD_SAFE", 0644, chip->dent,
2555 (void *)CHG_VDD_SAFE, &reg_fops);
2556 debugfs_create_file("CHG_VBAT_DET", 0644, chip->dent,
2557 (void *)CHG_VBAT_DET, &reg_fops);
2558 debugfs_create_file("CHG_IBAT_MAX", 0644, chip->dent,
2559 (void *)CHG_IBAT_MAX, &reg_fops);
2560 debugfs_create_file("CHG_IBAT_SAFE", 0644, chip->dent,
2561 (void *)CHG_IBAT_SAFE, &reg_fops);
2562 debugfs_create_file("CHG_VIN_MIN", 0644, chip->dent,
2563 (void *)CHG_VIN_MIN, &reg_fops);
2564 debugfs_create_file("CHG_VTRICKLE", 0644, chip->dent,
2565 (void *)CHG_VTRICKLE, &reg_fops);
2566 debugfs_create_file("CHG_ITRICKLE", 0644, chip->dent,
2567 (void *)CHG_ITRICKLE, &reg_fops);
2568 debugfs_create_file("CHG_ITERM", 0644, chip->dent,
2569 (void *)CHG_ITERM, &reg_fops);
2570 debugfs_create_file("CHG_TCHG_MAX", 0644, chip->dent,
2571 (void *)CHG_TCHG_MAX, &reg_fops);
2572 debugfs_create_file("CHG_TWDOG", 0644, chip->dent,
2573 (void *)CHG_TWDOG, &reg_fops);
2574 debugfs_create_file("CHG_TEMP_THRESH", 0644, chip->dent,
2575 (void *)CHG_TEMP_THRESH, &reg_fops);
2576 debugfs_create_file("CHG_COMP_OVR", 0644, chip->dent,
2577 (void *)CHG_COMP_OVR, &reg_fops);
2578 debugfs_create_file("CHG_BUCK_CTRL_TEST1", 0644, chip->dent,
2579 (void *)CHG_BUCK_CTRL_TEST1, &reg_fops);
2580 debugfs_create_file("CHG_BUCK_CTRL_TEST2", 0644, chip->dent,
2581 (void *)CHG_BUCK_CTRL_TEST2, &reg_fops);
2582 debugfs_create_file("CHG_BUCK_CTRL_TEST3", 0644, chip->dent,
2583 (void *)CHG_BUCK_CTRL_TEST3, &reg_fops);
2584 debugfs_create_file("CHG_TEST", 0644, chip->dent,
2585 (void *)CHG_TEST, &reg_fops);
2586
2587 debugfs_create_file("FSM_STATE", 0644, chip->dent, NULL,
2588 &fsm_fops);
2589
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002590 debugfs_create_file("REGULATION_LOOP_CONTROL", 0644, chip->dent, NULL,
2591 &reg_loop_fops);
2592
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002593 debugfs_create_file("BAT_WARM_ZONE", 0644, chip->dent,
2594 (void *)BAT_WARM_ZONE, &warm_cool_fops);
2595 debugfs_create_file("BAT_COOL_ZONE", 0644, chip->dent,
2596 (void *)BAT_COOL_ZONE, &warm_cool_fops);
2597
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002598 for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) {
2599 if (chip->pmic_chg_irq[chg_irq_data[i].irq_id])
2600 debugfs_create_file(chg_irq_data[i].name, 0444,
2601 chip->dent,
2602 (void *)chg_irq_data[i].irq_id,
2603 &rt_fops);
2604 }
2605}
2606
2607static int __devinit pm8921_charger_probe(struct platform_device *pdev)
2608{
2609 int rc = 0;
2610 struct pm8921_chg_chip *chip;
2611 const struct pm8921_charger_platform_data *pdata
2612 = pdev->dev.platform_data;
2613
2614 if (!pdata) {
2615 pr_err("missing platform data\n");
2616 return -EINVAL;
2617 }
2618
2619 chip = kzalloc(sizeof(struct pm8921_chg_chip),
2620 GFP_KERNEL);
2621 if (!chip) {
2622 pr_err("Cannot allocate pm_chg_chip\n");
2623 return -ENOMEM;
2624 }
2625
2626 chip->dev = &pdev->dev;
2627 chip->safety_time = pdata->safety_time;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002628 chip->ttrkl_time = pdata->ttrkl_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002629 chip->update_time = pdata->update_time;
2630 chip->max_voltage = pdata->max_voltage;
2631 chip->min_voltage = pdata->min_voltage;
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002632 chip->resume_voltage_delta = pdata->resume_voltage_delta;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002633 chip->term_current = pdata->term_current;
2634 chip->vbat_channel = pdata->charger_cdata.vbat_channel;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002635 chip->batt_temp_channel = pdata->charger_cdata.batt_temp_channel;
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07002636 chip->batt_id_channel = pdata->charger_cdata.batt_id_channel;
2637 chip->batt_id_min = pdata->batt_id_min;
2638 chip->batt_id_max = pdata->batt_id_max;
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002639 chip->cool_temp = pdata->cool_temp;
2640 chip->warm_temp = pdata->warm_temp;
2641 chip->temp_check_period = pdata->temp_check_period;
2642 chip->max_bat_chg_current = pdata->max_bat_chg_current;
2643 chip->cool_bat_chg_current = pdata->cool_bat_chg_current;
2644 chip->warm_bat_chg_current = pdata->warm_bat_chg_current;
2645 chip->cool_bat_voltage = pdata->cool_bat_voltage;
2646 chip->warm_bat_voltage = pdata->warm_bat_voltage;
Abhijeet Dharmapurikar26cef9c2011-08-25 19:16:42 -07002647 chip->trkl_voltage = pdata->trkl_voltage;
2648 chip->weak_voltage = pdata->weak_voltage;
2649 chip->trkl_current = pdata->trkl_current;
2650 chip->weak_current = pdata->weak_current;
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -07002651 chip->vin_min = pdata->vin_min;
Abhijeet Dharmapurikarad742362011-08-29 19:50:02 -07002652 chip->thermal_mitigation = pdata->thermal_mitigation;
2653 chip->thermal_levels = pdata->thermal_levels;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002654
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -07002655 chip->cold_thr = pdata->cold_thr;
2656 chip->hot_thr = pdata->hot_thr;
2657
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002658 rc = pm8921_chg_hw_init(chip);
2659 if (rc) {
2660 pr_err("couldn't init hardware rc=%d\n", rc);
2661 goto free_chip;
2662 }
2663
2664 chip->usb_psy.name = "usb",
2665 chip->usb_psy.type = POWER_SUPPLY_TYPE_USB,
2666 chip->usb_psy.supplied_to = pm_power_supplied_to,
2667 chip->usb_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to),
2668 chip->usb_psy.properties = pm_power_props,
2669 chip->usb_psy.num_properties = ARRAY_SIZE(pm_power_props),
2670 chip->usb_psy.get_property = pm_power_get_property,
2671
2672 chip->dc_psy.name = "ac",
2673 chip->dc_psy.type = POWER_SUPPLY_TYPE_MAINS,
2674 chip->dc_psy.supplied_to = pm_power_supplied_to,
2675 chip->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to),
2676 chip->dc_psy.properties = pm_power_props,
2677 chip->dc_psy.num_properties = ARRAY_SIZE(pm_power_props),
2678 chip->dc_psy.get_property = pm_power_get_property,
2679
2680 chip->batt_psy.name = "battery",
2681 chip->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY,
2682 chip->batt_psy.properties = msm_batt_power_props,
2683 chip->batt_psy.num_properties = ARRAY_SIZE(msm_batt_power_props),
2684 chip->batt_psy.get_property = pm_batt_power_get_property,
2685
2686 rc = power_supply_register(chip->dev, &chip->usb_psy);
2687 if (rc < 0) {
2688 pr_err("power_supply_register usb failed rc = %d\n", rc);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002689 goto free_chip;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002690 }
2691
2692 rc = power_supply_register(chip->dev, &chip->dc_psy);
2693 if (rc < 0) {
2694 pr_err("power_supply_register dc failed rc = %d\n", rc);
2695 goto unregister_usb;
2696 }
2697
2698 rc = power_supply_register(chip->dev, &chip->batt_psy);
2699 if (rc < 0) {
2700 pr_err("power_supply_register batt failed rc = %d\n", rc);
2701 goto unregister_dc;
2702 }
2703
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002704 platform_set_drvdata(pdev, chip);
2705 the_chip = chip;
2706
2707 wake_lock_init(&chip->eoc_wake_lock, WAKE_LOCK_SUSPEND, "pm8921_eoc");
Amir Samuelovd31ef502011-10-26 14:41:36 +02002708 INIT_DELAYED_WORK(&chip->eoc_work, eoc_worker);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002709
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002710 rc = request_irqs(chip, pdev);
2711 if (rc) {
2712 pr_err("couldn't register interrupts rc=%d\n", rc);
2713 goto unregister_batt;
2714 }
2715
Abhijeet Dharmapurikare27d8092011-08-12 17:16:29 -07002716 enable_irq_wake(chip->pmic_chg_irq[USBIN_VALID_IRQ]);
2717 enable_irq_wake(chip->pmic_chg_irq[USBIN_OV_IRQ]);
2718 enable_irq_wake(chip->pmic_chg_irq[USBIN_UV_IRQ]);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002719 /*
2720 * if both the cool_temp and warm_temp are zero the device doesnt
2721 * care for jeita compliance
2722 */
2723 if (!(chip->cool_temp == 0 && chip->warm_temp == 0)) {
2724 rc = configure_btm(chip);
2725 if (rc) {
2726 pr_err("couldn't register with btm rc=%d\n", rc);
2727 goto free_irq;
2728 }
2729 }
Abhijeet Dharmapurikare27d8092011-08-12 17:16:29 -07002730
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002731 create_debugfs_entries(chip);
2732
2733 INIT_WORK(&chip->bms_notify.work, bms_notify);
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07002734 INIT_WORK(&chip->battery_id_valid_work, battery_id_valid);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002735
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002736 /* determine what state the charger is in */
2737 determine_initial_state(chip);
2738
Abhijeet Dharmapurikar33fe6fb2011-09-14 16:03:11 -07002739 if (chip->update_time) {
2740 INIT_DELAYED_WORK(&chip->update_heartbeat_work,
2741 update_heartbeat);
2742 schedule_delayed_work(&chip->update_heartbeat_work,
2743 round_jiffies_relative(msecs_to_jiffies
2744 (chip->update_time)));
2745 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002746 return 0;
2747
2748free_irq:
2749 free_irqs(chip);
2750unregister_batt:
2751 power_supply_unregister(&chip->batt_psy);
2752unregister_dc:
2753 power_supply_unregister(&chip->dc_psy);
2754unregister_usb:
2755 power_supply_unregister(&chip->usb_psy);
2756free_chip:
2757 kfree(chip);
2758 return rc;
2759}
2760
2761static int __devexit pm8921_charger_remove(struct platform_device *pdev)
2762{
2763 struct pm8921_chg_chip *chip = platform_get_drvdata(pdev);
2764
2765 free_irqs(chip);
2766 platform_set_drvdata(pdev, NULL);
2767 the_chip = NULL;
2768 kfree(chip);
2769 return 0;
2770}
2771
2772static struct platform_driver pm8921_charger_driver = {
2773 .probe = pm8921_charger_probe,
2774 .remove = __devexit_p(pm8921_charger_remove),
2775 .driver = {
2776 .name = PM8921_CHARGER_DEV_NAME,
2777 .owner = THIS_MODULE,
2778 },
2779};
2780
2781static int __init pm8921_charger_init(void)
2782{
2783 return platform_driver_register(&pm8921_charger_driver);
2784}
2785
2786static void __exit pm8921_charger_exit(void)
2787{
2788 platform_driver_unregister(&pm8921_charger_driver);
2789}
2790
2791late_initcall(pm8921_charger_init);
2792module_exit(pm8921_charger_exit);
2793
2794MODULE_LICENSE("GPL v2");
2795MODULE_DESCRIPTION("PMIC8921 charger/battery driver");
2796MODULE_VERSION("1.0");
2797MODULE_ALIAS("platform:" PM8921_CHARGER_DEV_NAME);