blob: 6b05643fa1371c922113be9086177fd5f98eba33 [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 Mohanadossaf91d902011-10-20 10:23:34 -070021#include <linux/mfd/pm8xxx/pm8xxx-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;
Abhijeet Dharmapurikard585ff22011-11-10 14:27:25 -0800253 unsigned int *thermal_mitigation;
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700254 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
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -0700267static struct pm8xxx_adc_arb_btm_param btm_config;
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -0700268
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;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -0700725 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -0700726
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -0700727 rc = pm8xxx_adc_read(chip->batt_id_channel, &result);
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -0700728 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;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -0700928 struct pm8xxx_adc_chan_result result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700929
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -0700930 rc = pm8xxx_adc_read(chip->vbat_channel, &result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700931 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;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001070 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001071
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001072 rc = pm8xxx_adc_read(chip->batt_temp_channel, &result);
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001073 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
David Keitel012deef2011-12-02 11:49:33 -08001282/*
1283 * Disabling the charge current limit causes current
1284 * current limits to have no monitoring. An adequate charger
1285 * capable of supplying high current while sustaining VIN_MIN
1286 * is required if the limiting is disabled.
1287 */
1288int pm8921_disable_input_current_limit(bool disable)
1289{
1290 if (!the_chip) {
1291 pr_err("called before init\n");
1292 return -EINVAL;
1293 }
1294 if (disable) {
1295 pr_warn("Disabling input current limit!\n");
1296
1297 return pm8xxx_writeb(the_chip->dev->parent,
1298 CHG_BUCK_CTRL_TEST3, 0xF2);
1299 }
1300 return 0;
1301}
1302EXPORT_SYMBOL(pm8921_disable_input_current_limit);
1303
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001304int pm8921_set_max_battery_charge_current(int ma)
1305{
1306 if (!the_chip) {
1307 pr_err("called before init\n");
1308 return -EINVAL;
1309 }
1310 return pm_chg_ibatmax_set(the_chip, ma);
1311}
1312EXPORT_SYMBOL(pm8921_set_max_battery_charge_current);
1313
1314int pm8921_disable_source_current(bool disable)
1315{
1316 if (!the_chip) {
1317 pr_err("called before init\n");
1318 return -EINVAL;
1319 }
1320 if (disable)
1321 pr_warn("current drawn from chg=0, battery provides current\n");
1322 return pm_chg_charge_dis(the_chip, disable);
1323}
1324EXPORT_SYMBOL(pm8921_disable_source_current);
1325
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -07001326int pm8921_regulate_input_voltage(int voltage)
1327{
1328 if (!the_chip) {
1329 pr_err("called before init\n");
1330 return -EINVAL;
1331 }
1332 return pm_chg_vinmin_set(the_chip, voltage);
1333}
1334
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001335bool pm8921_is_battery_charging(int *source)
1336{
1337 int fsm_state, is_charging, dc_present, usb_present;
1338
1339 if (!the_chip) {
1340 pr_err("called before init\n");
1341 return -EINVAL;
1342 }
1343 fsm_state = pm_chg_get_fsm_state(the_chip);
1344 is_charging = is_battery_charging(fsm_state);
1345 if (is_charging == 0) {
1346 *source = PM8921_CHG_SRC_NONE;
1347 return is_charging;
1348 }
1349
1350 if (source == NULL)
1351 return is_charging;
1352
1353 /* the battery is charging, the source is requested, find it */
1354 dc_present = is_dc_chg_plugged_in(the_chip);
1355 usb_present = is_usb_chg_plugged_in(the_chip);
1356
1357 if (dc_present && !usb_present)
1358 *source = PM8921_CHG_SRC_DC;
1359
1360 if (usb_present && !dc_present)
1361 *source = PM8921_CHG_SRC_USB;
1362
1363 if (usb_present && dc_present)
1364 /*
1365 * The system always chooses dc for charging since it has
1366 * higher priority.
1367 */
1368 *source = PM8921_CHG_SRC_DC;
1369
1370 return is_charging;
1371}
1372EXPORT_SYMBOL(pm8921_is_battery_charging);
1373
1374int pm8921_batt_temperature(void)
1375{
1376 if (!the_chip) {
1377 pr_err("called before init\n");
1378 return -EINVAL;
1379 }
1380 return get_prop_batt_temp(the_chip);
1381}
1382
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001383static void handle_usb_insertion_removal(struct pm8921_chg_chip *chip)
1384{
1385 int usb_present;
1386
1387 usb_present = is_usb_chg_plugged_in(chip);
1388 if (chip->usb_present ^ usb_present) {
1389 notify_usb_of_the_plugin_event(usb_present);
1390 chip->usb_present = usb_present;
1391 power_supply_changed(&chip->usb_psy);
Abhijeet Dharmapurikar0c4bc152011-10-27 10:22:24 -07001392 power_supply_changed(&chip->batt_psy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001393 }
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07001394 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001395}
1396
Amir Samuelovd31ef502011-10-26 14:41:36 +02001397static void handle_stop_ext_chg(struct pm8921_chg_chip *chip)
1398{
1399 if (chip->ext == NULL) {
1400 pr_debug("external charger not registered.\n");
1401 return;
1402 }
1403
1404 if (!chip->ext_charging) {
1405 pr_debug("already not charging.\n");
1406 return;
1407 }
1408
1409 chip->ext->stop_charging(chip->ext->ctx);
1410 chip->ext_charging = false;
David Keiteld4e59ef2011-11-09 17:38:31 -08001411 chip->ext_charge_done = false;
Amir Samuelovd31ef502011-10-26 14:41:36 +02001412}
1413
1414static void handle_start_ext_chg(struct pm8921_chg_chip *chip)
1415{
1416 int dc_present;
1417 int batt_present;
1418 int batt_temp_ok;
1419 int vbat_ov;
1420 int batfet;
1421 unsigned long delay =
1422 round_jiffies_relative(msecs_to_jiffies(EOC_CHECK_PERIOD_MS));
1423
1424 if (chip->ext == NULL) {
1425 pr_debug("external charger not registered.\n");
1426 return;
1427 }
1428
1429 if (chip->ext_charging) {
1430 pr_debug("already charging.\n");
1431 return;
1432 }
1433
1434 dc_present = is_dc_chg_plugged_in(chip);
1435 batt_present = pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ);
1436 batt_temp_ok = pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ);
1437 vbat_ov = pm_chg_get_rt_status(chip, VBAT_OV_IRQ);
1438 batfet = pm_chg_get_rt_status(chip, BATFET_IRQ);
1439
1440 if (!dc_present) {
1441 pr_warn("%s. dc not present.\n", __func__);
1442 return;
1443 }
1444 if (!batt_present) {
1445 pr_warn("%s. battery not present.\n", __func__);
1446 return;
1447 }
1448 if (!batt_temp_ok) {
1449 pr_warn("%s. battery temperature not ok.\n", __func__);
1450 return;
1451 }
1452 if (vbat_ov) {
1453 pr_warn("%s. battery over voltage.\n", __func__);
1454 return;
1455 }
1456 if (!batfet) {
1457 pr_warn("%s. battery FET not closed.\n", __func__);
1458 return;
1459 }
1460
1461 chip->ext->start_charging(chip->ext->ctx);
1462 chip->ext_charging = true;
1463 chip->ext_charge_done = false;
1464 /* Start BMS */
1465 schedule_delayed_work(&chip->eoc_work, delay);
1466 wake_lock(&chip->eoc_wake_lock);
1467}
1468
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001469static void handle_dc_removal_insertion(struct pm8921_chg_chip *chip)
1470{
1471 int dc_present;
1472
1473 dc_present = is_dc_chg_plugged_in(chip);
1474 if (chip->dc_present ^ dc_present) {
1475 chip->dc_present = dc_present;
1476 power_supply_changed(&chip->dc_psy);
Abhijeet Dharmapurikar0c4bc152011-10-27 10:22:24 -07001477 power_supply_changed(&chip->batt_psy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001478 }
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07001479 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001480}
1481
1482static irqreturn_t usbin_valid_irq_handler(int irq, void *data)
1483{
1484 handle_usb_insertion_removal(data);
1485 return IRQ_HANDLED;
1486}
1487
1488static irqreturn_t usbin_ov_irq_handler(int irq, void *data)
1489{
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -07001490 pr_err("USB OverVoltage\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001491 return IRQ_HANDLED;
1492}
1493
1494static irqreturn_t batt_inserted_irq_handler(int irq, void *data)
1495{
1496 struct pm8921_chg_chip *chip = data;
1497 int status;
1498
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07001499 status = pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ);
1500 schedule_work(&chip->battery_id_valid_work);
Amir Samuelovd31ef502011-10-26 14:41:36 +02001501 handle_start_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001502 pr_debug("battery present=%d", status);
1503 power_supply_changed(&chip->batt_psy);
1504 return IRQ_HANDLED;
1505}
Amir Samuelovd31ef502011-10-26 14:41:36 +02001506
1507/*
1508 * this interrupt used to restart charging a battery.
1509 *
1510 * Note: When DC-inserted the VBAT can't go low.
1511 * VPH_PWR is provided by the ext-charger.
1512 * After End-Of-Charging from DC, charging can be resumed only
1513 * if DC is removed and then inserted after the battery was in use.
1514 * Therefore the handle_start_ext_chg() is not called.
1515 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001516static irqreturn_t vbatdet_low_irq_handler(int irq, void *data)
1517{
1518 struct pm8921_chg_chip *chip = data;
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07001519 int high_transition;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001520
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07001521 high_transition = pm_chg_get_rt_status(chip, VBATDET_LOW_IRQ);
Abhijeet Dharmapurikar07e48bf2011-09-26 19:46:40 -07001522
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07001523 if (high_transition) {
1524 /* enable auto charging */
1525 pm_chg_auto_enable(chip, !charging_disabled);
1526 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001527 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001528
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001529 power_supply_changed(&chip->batt_psy);
1530 power_supply_changed(&chip->usb_psy);
1531 power_supply_changed(&chip->dc_psy);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001532
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001533 return IRQ_HANDLED;
1534}
1535
1536static irqreturn_t usbin_uv_irq_handler(int irq, void *data)
1537{
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -07001538 pr_err("USB UnderVoltage\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001539 return IRQ_HANDLED;
1540}
1541
1542static irqreturn_t vbat_ov_irq_handler(int irq, void *data)
1543{
1544 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1545 return IRQ_HANDLED;
1546}
1547
1548static irqreturn_t chgwdog_irq_handler(int irq, void *data)
1549{
1550 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1551 return IRQ_HANDLED;
1552}
1553
1554static irqreturn_t vcp_irq_handler(int irq, void *data)
1555{
1556 pr_warning("VCP triggered BATDET forced on\n");
1557 pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
1558 return IRQ_HANDLED;
1559}
1560
1561static irqreturn_t atcdone_irq_handler(int irq, void *data)
1562{
1563 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1564 return IRQ_HANDLED;
1565}
1566
1567static irqreturn_t atcfail_irq_handler(int irq, void *data)
1568{
1569 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1570 return IRQ_HANDLED;
1571}
1572
1573static irqreturn_t chgdone_irq_handler(int irq, void *data)
1574{
1575 struct pm8921_chg_chip *chip = data;
1576
1577 pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
Amir Samuelovd31ef502011-10-26 14:41:36 +02001578
1579 handle_stop_ext_chg(chip);
1580
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001581 power_supply_changed(&chip->batt_psy);
1582 power_supply_changed(&chip->usb_psy);
1583 power_supply_changed(&chip->dc_psy);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001584
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001585 chip->bms_notify.is_battery_full = 1;
1586 bms_notify_check(chip);
1587
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001588 return IRQ_HANDLED;
1589}
1590
1591static irqreturn_t chgfail_irq_handler(int irq, void *data)
1592{
1593 struct pm8921_chg_chip *chip = data;
David Keitel753ec8d2011-11-02 10:56:37 -07001594 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001595
David Keitel753ec8d2011-11-02 10:56:37 -07001596 ret = pm_chg_failed_clear(chip, 1);
1597 if (ret)
1598 pr_err("Failed to write CHG_FAILED_CLEAR bit\n");
1599
1600 pr_err("batt_present = %d, batt_temp_ok = %d, state_changed_to=%d\n",
1601 get_prop_batt_present(chip),
1602 pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ),
1603 pm_chg_get_fsm_state(data));
1604
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001605 power_supply_changed(&chip->batt_psy);
1606 power_supply_changed(&chip->usb_psy);
1607 power_supply_changed(&chip->dc_psy);
1608 return IRQ_HANDLED;
1609}
1610
1611static irqreturn_t chgstate_irq_handler(int irq, void *data)
1612{
1613 struct pm8921_chg_chip *chip = data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001614
1615 pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
1616 power_supply_changed(&chip->batt_psy);
1617 power_supply_changed(&chip->usb_psy);
1618 power_supply_changed(&chip->dc_psy);
1619
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07001620 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001621
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001622 return IRQ_HANDLED;
1623}
1624
1625static irqreturn_t loop_change_irq_handler(int irq, void *data)
1626{
1627 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1628 return IRQ_HANDLED;
1629}
1630
1631static irqreturn_t fastchg_irq_handler(int irq, void *data)
1632{
1633 struct pm8921_chg_chip *chip = data;
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07001634 int high_transition;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001635
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07001636 high_transition = pm_chg_get_rt_status(chip, FASTCHG_IRQ);
1637 if (high_transition && !delayed_work_pending(&chip->eoc_work)) {
1638 wake_lock(&chip->eoc_wake_lock);
1639 schedule_delayed_work(&chip->eoc_work,
1640 round_jiffies_relative(msecs_to_jiffies
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001641 (EOC_CHECK_PERIOD_MS)));
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07001642 }
1643 power_supply_changed(&chip->batt_psy);
1644 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001645 return IRQ_HANDLED;
1646}
1647
1648static irqreturn_t trklchg_irq_handler(int irq, void *data)
1649{
1650 struct pm8921_chg_chip *chip = data;
1651
1652 power_supply_changed(&chip->batt_psy);
1653 return IRQ_HANDLED;
1654}
1655
1656static irqreturn_t batt_removed_irq_handler(int irq, void *data)
1657{
1658 struct pm8921_chg_chip *chip = data;
1659 int status;
1660
1661 status = pm_chg_get_rt_status(chip, BATT_REMOVED_IRQ);
1662 pr_debug("battery present=%d state=%d", !status,
1663 pm_chg_get_fsm_state(data));
Amir Samuelovd31ef502011-10-26 14:41:36 +02001664 handle_stop_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001665 power_supply_changed(&chip->batt_psy);
1666 return IRQ_HANDLED;
1667}
1668
1669static irqreturn_t batttemp_hot_irq_handler(int irq, void *data)
1670{
1671 struct pm8921_chg_chip *chip = data;
1672
Amir Samuelovd31ef502011-10-26 14:41:36 +02001673 handle_stop_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001674 power_supply_changed(&chip->batt_psy);
1675 return IRQ_HANDLED;
1676}
1677
1678static irqreturn_t chghot_irq_handler(int irq, void *data)
1679{
1680 struct pm8921_chg_chip *chip = data;
1681
1682 pr_debug("Chg hot fsm_state=%d\n", pm_chg_get_fsm_state(data));
1683 power_supply_changed(&chip->batt_psy);
1684 power_supply_changed(&chip->usb_psy);
1685 power_supply_changed(&chip->dc_psy);
1686 return IRQ_HANDLED;
1687}
1688
1689static irqreturn_t batttemp_cold_irq_handler(int irq, void *data)
1690{
1691 struct pm8921_chg_chip *chip = data;
1692
1693 pr_debug("Batt cold fsm_state=%d\n", pm_chg_get_fsm_state(data));
Amir Samuelovd31ef502011-10-26 14:41:36 +02001694 handle_stop_ext_chg(chip);
1695
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001696 power_supply_changed(&chip->batt_psy);
1697 power_supply_changed(&chip->usb_psy);
1698 power_supply_changed(&chip->dc_psy);
1699 return IRQ_HANDLED;
1700}
1701
1702static irqreturn_t chg_gone_irq_handler(int irq, void *data)
1703{
1704 struct pm8921_chg_chip *chip = data;
1705
1706 pr_debug("Chg gone fsm_state=%d\n", pm_chg_get_fsm_state(data));
1707 power_supply_changed(&chip->batt_psy);
1708 power_supply_changed(&chip->usb_psy);
1709 power_supply_changed(&chip->dc_psy);
1710 return IRQ_HANDLED;
1711}
David Keitel52fda532011-11-10 10:43:44 -08001712/*
1713 *
1714 * bat_temp_ok_irq_handler - is edge triggered, hence it will
1715 * fire for two cases:
1716 *
1717 * If the interrupt line switches to high temperature is okay
1718 * and thus charging begins.
1719 * If bat_temp_ok is low this means the temperature is now
1720 * too hot or cold, so charging is stopped.
1721 *
1722 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001723static irqreturn_t bat_temp_ok_irq_handler(int irq, void *data)
1724{
David Keitel52fda532011-11-10 10:43:44 -08001725 int bat_temp_ok;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001726 struct pm8921_chg_chip *chip = data;
1727
David Keitel52fda532011-11-10 10:43:44 -08001728 bat_temp_ok = pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ);
1729
1730 pr_debug("batt_temp_ok = %d fsm_state%d\n",
1731 bat_temp_ok, pm_chg_get_fsm_state(data));
1732
1733 if (bat_temp_ok)
1734 handle_start_ext_chg(chip);
1735 else
1736 handle_stop_ext_chg(chip);
Amir Samuelovd31ef502011-10-26 14:41:36 +02001737
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001738 power_supply_changed(&chip->batt_psy);
1739 power_supply_changed(&chip->usb_psy);
1740 power_supply_changed(&chip->dc_psy);
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07001741 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001742 return IRQ_HANDLED;
1743}
1744
1745static irqreturn_t coarse_det_low_irq_handler(int irq, void *data)
1746{
1747 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1748 return IRQ_HANDLED;
1749}
1750
1751static irqreturn_t vdd_loop_irq_handler(int irq, void *data)
1752{
1753 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1754 return IRQ_HANDLED;
1755}
1756
1757static irqreturn_t vreg_ov_irq_handler(int irq, void *data)
1758{
1759 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1760 return IRQ_HANDLED;
1761}
1762
1763static irqreturn_t vbatdet_irq_handler(int irq, void *data)
1764{
1765 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
1766 return IRQ_HANDLED;
1767}
1768
1769static irqreturn_t batfet_irq_handler(int irq, void *data)
1770{
1771 struct pm8921_chg_chip *chip = data;
1772
1773 pr_debug("vreg ov\n");
1774 power_supply_changed(&chip->batt_psy);
1775 return IRQ_HANDLED;
1776}
1777
1778static irqreturn_t dcin_valid_irq_handler(int irq, void *data)
1779{
Amir Samuelovd31ef502011-10-26 14:41:36 +02001780 struct pm8921_chg_chip *chip = data;
1781
1782 pm8921_disable_source_current(true); /* Force BATFET=ON */
1783
1784 handle_dc_removal_insertion(chip);
1785 handle_start_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001786 return IRQ_HANDLED;
1787}
1788
1789static irqreturn_t dcin_ov_irq_handler(int irq, void *data)
1790{
Amir Samuelovd31ef502011-10-26 14:41:36 +02001791 struct pm8921_chg_chip *chip = data;
1792
1793 pm8921_disable_source_current(false); /* release BATFET */
1794
1795 handle_dc_removal_insertion(chip);
1796 handle_stop_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001797 return IRQ_HANDLED;
1798}
1799
1800static irqreturn_t dcin_uv_irq_handler(int irq, void *data)
1801{
Amir Samuelovd31ef502011-10-26 14:41:36 +02001802 struct pm8921_chg_chip *chip = data;
1803
1804 pm8921_disable_source_current(false); /* release BATFET */
1805 handle_stop_ext_chg(chip);
1806
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001807 return IRQ_HANDLED;
1808}
1809
Abhijeet Dharmapurikar33fe6fb2011-09-14 16:03:11 -07001810/**
1811 * update_heartbeat - internal function to update userspace
1812 * per update_time minutes
1813 *
1814 */
1815static void update_heartbeat(struct work_struct *work)
1816{
1817 struct delayed_work *dwork = to_delayed_work(work);
1818 struct pm8921_chg_chip *chip = container_of(dwork,
1819 struct pm8921_chg_chip, update_heartbeat_work);
1820
1821 power_supply_changed(&chip->batt_psy);
1822 schedule_delayed_work(&chip->update_heartbeat_work,
1823 round_jiffies_relative(msecs_to_jiffies
1824 (chip->update_time)));
1825}
1826
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001827/**
Amir Samuelovd31ef502011-10-26 14:41:36 +02001828 * eoc_worker - internal function to check if battery EOC
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001829 * has happened
1830 *
1831 * If all conditions favouring, if the charge current is
1832 * less than the term current for three consecutive times
1833 * an EOC has happened.
1834 * The wakelock is released if there is no need to reshedule
1835 * - this happens when the battery is removed or EOC has
1836 * happened
1837 */
1838#define CONSECUTIVE_COUNT 3
1839#define VBAT_TOLERANCE_MV 70
1840#define CHG_DISABLE_MSLEEP 100
Amir Samuelovd31ef502011-10-26 14:41:36 +02001841static void eoc_worker(struct work_struct *work)
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001842{
1843 struct delayed_work *dwork = to_delayed_work(work);
1844 struct pm8921_chg_chip *chip = container_of(dwork,
1845 struct pm8921_chg_chip, eoc_work);
1846 int vbat_meas, vbat_programmed;
1847 int ichg_meas, iterm_programmed;
1848 int regulation_loop, fast_chg, vcp;
1849 int rc;
1850 static int count;
1851
Amir Samuelovd31ef502011-10-26 14:41:36 +02001852 if (!is_ext_charging(chip)) {
1853 /* return if the battery is not being fastcharged */
1854 fast_chg = pm_chg_get_rt_status(chip, FASTCHG_IRQ);
1855 pr_debug("fast_chg = %d\n", fast_chg);
1856 if (fast_chg == 0) {
1857 /* enable fastchg irq */
Amir Samuelovd31ef502011-10-26 14:41:36 +02001858 count = 0;
1859 wake_unlock(&chip->eoc_wake_lock);
1860 return;
1861 }
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001862
Amir Samuelovd31ef502011-10-26 14:41:36 +02001863 vcp = pm_chg_get_rt_status(chip, VCP_IRQ);
1864 pr_debug("vcp = %d\n", vcp);
1865 if (vcp == 1)
1866 goto reset_and_reschedule;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001867
Amir Samuelovd31ef502011-10-26 14:41:36 +02001868 /* reset count if battery is hot/cold */
1869 rc = pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ);
1870 pr_debug("batt_temp_ok = %d\n", rc);
1871 if (rc == 0)
1872 goto reset_and_reschedule;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001873
Amir Samuelovd31ef502011-10-26 14:41:36 +02001874 /* reset count if battery voltage is less than vddmax */
1875 vbat_meas = get_prop_battery_mvolts(chip);
1876 if (vbat_meas < 0)
1877 goto reset_and_reschedule;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001878
Amir Samuelovd31ef502011-10-26 14:41:36 +02001879 rc = pm_chg_vddmax_get(chip, &vbat_programmed);
1880 if (rc) {
1881 pr_err("couldnt read vddmax rc = %d\n", rc);
1882 goto reset_and_reschedule;
1883 }
1884 pr_debug("vddmax = %d vbat_meas=%d\n",
1885 vbat_programmed, vbat_meas);
1886 if (vbat_meas < vbat_programmed - VBAT_TOLERANCE_MV)
1887 goto reset_and_reschedule;
1888 } /* !is_ext_charging */
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001889
1890 /* reset count if battery chg current is more than iterm */
1891 rc = pm_chg_iterm_get(chip, &iterm_programmed);
1892 if (rc) {
1893 pr_err("couldnt read iterm rc = %d\n", rc);
1894 goto reset_and_reschedule;
1895 }
1896
1897 ichg_meas = get_prop_batt_current(chip);
1898 pr_debug("iterm_programmed = %d ichg_meas=%d\n",
1899 iterm_programmed, ichg_meas);
1900 /*
1901 * ichg_meas < 0 means battery is drawing current
1902 * ichg_meas > 0 means battery is providing current
1903 */
1904 if (ichg_meas > 0)
1905 goto reset_and_reschedule;
1906
1907 if (ichg_meas * -1 > iterm_programmed)
1908 goto reset_and_reschedule;
1909
Amir Samuelovd31ef502011-10-26 14:41:36 +02001910 if (!is_ext_charging(chip)) {
1911 /*
1912 * TODO if charging from an external charger
1913 * check SOC instead of regulation loop
1914 */
1915 regulation_loop = pm_chg_get_regulation_loop(chip);
1916 if (regulation_loop < 0) {
1917 pr_err("couldnt read the regulation loop err=%d\n",
1918 regulation_loop);
1919 goto reset_and_reschedule;
1920 }
1921 pr_debug("regulation_loop=%d\n", regulation_loop);
Abhijeet Dharmapurikar07e48bf2011-09-26 19:46:40 -07001922
Amir Samuelovd31ef502011-10-26 14:41:36 +02001923 if (regulation_loop != 0 && regulation_loop != VDD_LOOP)
1924 goto reset_and_reschedule;
1925 } /* !is_ext_charging */
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001926
1927 count++;
1928 if (count == CONSECUTIVE_COUNT) {
1929 count = 0;
1930 pr_info("End of Charging\n");
1931
1932 pm_chg_auto_enable(chip, 0);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001933
Amir Samuelovd31ef502011-10-26 14:41:36 +02001934 if (is_ext_charging(chip))
1935 chip->ext_charge_done = true;
1936
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07001937 /* declare end of charging by invoking chgdone interrupt */
1938 chgdone_irq_handler(chip->pmic_chg_irq[CHGDONE_IRQ], chip);
1939 wake_unlock(&chip->eoc_wake_lock);
1940 return;
1941 } else {
1942 pr_debug("EOC count = %d\n", count);
1943 goto reschedule;
1944 }
1945
1946reset_and_reschedule:
1947 count = 0;
1948reschedule:
1949 schedule_delayed_work(&chip->eoc_work,
1950 round_jiffies_relative(msecs_to_jiffies
1951 (EOC_CHECK_PERIOD_MS)));
1952}
1953
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001954static void btm_configure_work(struct work_struct *work)
1955{
1956 int rc;
1957
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001958 rc = pm8xxx_adc_btm_configure(&btm_config);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001959 if (rc)
1960 pr_err("failed to configure btm rc=%d", rc);
1961}
1962
1963DECLARE_WORK(btm_config_work, btm_configure_work);
1964
Abhijeet Dharmapurikard585ff22011-11-10 14:27:25 -08001965static void set_appropriate_battery_current(struct pm8921_chg_chip *chip)
1966{
1967 unsigned int chg_current = chip->max_bat_chg_current;
1968
1969 if (chip->is_bat_cool)
1970 chg_current = min(chg_current, chip->cool_bat_chg_current);
1971
1972 if (chip->is_bat_warm)
1973 chg_current = min(chg_current, chip->warm_bat_chg_current);
1974
1975 if (thermal_mitigation != 0 && !chip->thermal_mitigation)
1976 chg_current = min(chg_current,
1977 chip->thermal_mitigation[thermal_mitigation]);
1978
1979 pm_chg_ibatmax_set(the_chip, chg_current);
1980}
1981
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001982#define TEMP_HYSTERISIS_DEGC 2
1983static void battery_cool(bool enter)
1984{
1985 pr_debug("enter = %d\n", enter);
Abhijeet Dharmapurikard585ff22011-11-10 14:27:25 -08001986 if (enter == the_chip->is_bat_cool)
1987 return;
1988 the_chip->is_bat_cool = enter;
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001989 if (enter) {
1990 btm_config.low_thr_temp =
1991 the_chip->cool_temp + TEMP_HYSTERISIS_DEGC;
Abhijeet Dharmapurikard585ff22011-11-10 14:27:25 -08001992 set_appropriate_battery_current(the_chip);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001993 pm_chg_vddmax_set(the_chip, the_chip->cool_bat_voltage);
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07001994 pm_chg_vbatdet_set(the_chip,
1995 the_chip->cool_bat_voltage
1996 - the_chip->resume_voltage_delta);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07001997 } else {
1998 btm_config.low_thr_temp = the_chip->cool_temp;
Abhijeet Dharmapurikard585ff22011-11-10 14:27:25 -08001999 set_appropriate_battery_current(the_chip);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002000 pm_chg_vddmax_set(the_chip, the_chip->max_voltage);
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002001 pm_chg_vbatdet_set(the_chip,
2002 the_chip->max_voltage - the_chip->resume_voltage_delta);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002003 }
2004 schedule_work(&btm_config_work);
2005}
2006
2007static void battery_warm(bool enter)
2008{
2009 pr_debug("enter = %d\n", enter);
Abhijeet Dharmapurikard585ff22011-11-10 14:27:25 -08002010 if (enter == the_chip->is_bat_warm)
2011 return;
2012 the_chip->is_bat_warm = enter;
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002013 if (enter) {
2014 btm_config.high_thr_temp =
2015 the_chip->warm_temp - TEMP_HYSTERISIS_DEGC;
Abhijeet Dharmapurikard585ff22011-11-10 14:27:25 -08002016 set_appropriate_battery_current(the_chip);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002017 pm_chg_vddmax_set(the_chip, the_chip->warm_bat_voltage);
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002018 pm_chg_vbatdet_set(the_chip,
2019 the_chip->warm_bat_voltage
2020 - the_chip->resume_voltage_delta);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002021 } else {
2022 btm_config.high_thr_temp = the_chip->warm_temp;
Abhijeet Dharmapurikard585ff22011-11-10 14:27:25 -08002023 set_appropriate_battery_current(the_chip);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002024 pm_chg_vddmax_set(the_chip, the_chip->max_voltage);
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002025 pm_chg_vbatdet_set(the_chip,
2026 the_chip->max_voltage - the_chip->resume_voltage_delta);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002027 }
2028 schedule_work(&btm_config_work);
2029}
2030
2031static int configure_btm(struct pm8921_chg_chip *chip)
2032{
2033 int rc;
2034
2035 btm_config.btm_warm_fn = battery_warm;
2036 btm_config.btm_cool_fn = battery_cool;
2037 btm_config.low_thr_temp = chip->cool_temp;
2038 btm_config.high_thr_temp = chip->warm_temp;
2039 btm_config.interval = chip->temp_check_period;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002040 rc = pm8xxx_adc_btm_configure(&btm_config);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002041 if (rc)
2042 pr_err("failed to configure btm rc = %d\n", rc);
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07002043 rc = pm8xxx_adc_btm_start();
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002044 if (rc)
2045 pr_err("failed to start btm rc = %d\n", rc);
2046
2047 return rc;
2048}
2049
Amir Samuelovd31ef502011-10-26 14:41:36 +02002050int register_external_dc_charger(struct ext_chg_pm8921 *ext)
2051{
2052 if (the_chip == NULL) {
2053 pr_err("called too early\n");
2054 return -EINVAL;
2055 }
2056 /* TODO check function pointers */
2057 the_chip->ext = ext;
2058 the_chip->ext_charging = false;
2059
2060 if (is_dc_chg_plugged_in(the_chip))
2061 pm8921_disable_source_current(true); /* Force BATFET=ON */
2062
2063 handle_start_ext_chg(the_chip);
2064
2065 return 0;
2066}
2067EXPORT_SYMBOL(register_external_dc_charger);
2068
2069void unregister_external_dc_charger(struct ext_chg_pm8921 *ext)
2070{
2071 if (the_chip == NULL) {
2072 pr_err("called too early\n");
2073 return;
2074 }
2075 handle_stop_ext_chg(the_chip);
2076 the_chip->ext = NULL;
2077}
2078EXPORT_SYMBOL(unregister_external_dc_charger);
2079
Abhijeet Dharmapurikar1f52c412011-08-12 17:11:45 -07002080/**
2081 * set_disable_status_param -
2082 *
2083 * Internal function to disable battery charging and also disable drawing
2084 * any current from the source. The device is forced to run on a battery
2085 * after this.
2086 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002087static int set_disable_status_param(const char *val, struct kernel_param *kp)
2088{
2089 int ret;
2090 struct pm8921_chg_chip *chip = the_chip;
2091
2092 ret = param_set_int(val, kp);
2093 if (ret) {
2094 pr_err("error setting value %d\n", ret);
2095 return ret;
2096 }
2097 pr_info("factory set disable param to %d\n", charging_disabled);
2098 if (chip) {
2099 pm_chg_auto_enable(chip, !charging_disabled);
2100 pm_chg_charge_dis(chip, charging_disabled);
2101 }
2102 return 0;
2103}
2104module_param_call(disabled, set_disable_status_param, param_get_uint,
2105 &charging_disabled, 0644);
2106
Abhijeet Dharmapurikarad742362011-08-29 19:50:02 -07002107/**
2108 * set_thermal_mitigation_level -
2109 *
2110 * Internal function to control battery charging current to reduce
2111 * temperature
2112 */
2113static int set_therm_mitigation_level(const char *val, struct kernel_param *kp)
2114{
2115 int ret;
2116 struct pm8921_chg_chip *chip = the_chip;
2117
2118 ret = param_set_int(val, kp);
2119 if (ret) {
2120 pr_err("error setting value %d\n", ret);
2121 return ret;
2122 }
2123
2124 if (!chip) {
2125 pr_err("called before init\n");
2126 return -EINVAL;
2127 }
2128
2129 if (!chip->thermal_mitigation) {
2130 pr_err("no thermal mitigation\n");
2131 return -EINVAL;
2132 }
2133
2134 if (thermal_mitigation < 0
2135 || thermal_mitigation >= chip->thermal_levels) {
2136 pr_err("out of bound level selected\n");
2137 return -EINVAL;
2138 }
2139
Abhijeet Dharmapurikard585ff22011-11-10 14:27:25 -08002140 set_appropriate_battery_current(chip);
Abhijeet Dharmapurikarad742362011-08-29 19:50:02 -07002141 return ret;
2142}
2143module_param_call(thermal_mitigation, set_therm_mitigation_level,
2144 param_get_uint,
2145 &thermal_mitigation, 0644);
2146
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002147static void free_irqs(struct pm8921_chg_chip *chip)
2148{
2149 int i;
2150
2151 for (i = 0; i < PM_CHG_MAX_INTS; i++)
2152 if (chip->pmic_chg_irq[i]) {
2153 free_irq(chip->pmic_chg_irq[i], chip);
2154 chip->pmic_chg_irq[i] = 0;
2155 }
2156}
2157
2158/* determines the initial present states and notifies msm_charger */
2159static void __devinit determine_initial_state(struct pm8921_chg_chip *chip)
2160{
2161 unsigned long flags;
2162 int fsm_state;
2163
2164 chip->dc_present = !!is_dc_chg_plugged_in(chip);
2165 chip->usb_present = !!is_usb_chg_plugged_in(chip);
2166
2167 notify_usb_of_the_plugin_event(chip->usb_present);
2168
2169 pm8921_chg_enable_irq(chip, DCIN_VALID_IRQ);
2170 pm8921_chg_enable_irq(chip, USBIN_VALID_IRQ);
2171 pm8921_chg_enable_irq(chip, BATT_REMOVED_IRQ);
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002172 pm8921_chg_enable_irq(chip, BATT_INSERTED_IRQ);
2173 pm8921_chg_enable_irq(chip, USBIN_OV_IRQ);
2174 pm8921_chg_enable_irq(chip, USBIN_UV_IRQ);
2175 pm8921_chg_enable_irq(chip, DCIN_OV_IRQ);
2176 pm8921_chg_enable_irq(chip, DCIN_UV_IRQ);
David Keitel753ec8d2011-11-02 10:56:37 -07002177 pm8921_chg_enable_irq(chip, CHGFAIL_IRQ);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002178 pm8921_chg_enable_irq(chip, FASTCHG_IRQ);
2179 pm8921_chg_enable_irq(chip, VBATDET_LOW_IRQ);
David Keitel52fda532011-11-10 10:43:44 -08002180 pm8921_chg_enable_irq(chip, BAT_TEMP_OK_IRQ);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002181
2182 spin_lock_irqsave(&vbus_lock, flags);
2183 if (usb_chg_current) {
2184 /* reissue a vbus draw call */
2185 __pm8921_charger_vbus_draw(usb_chg_current);
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07002186 fastchg_irq_handler(chip->pmic_chg_irq[FASTCHG_IRQ], chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002187 }
2188 spin_unlock_irqrestore(&vbus_lock, flags);
2189
2190 fsm_state = pm_chg_get_fsm_state(chip);
2191 if (is_battery_charging(fsm_state)) {
2192 chip->bms_notify.is_charging = 1;
2193 pm8921_bms_charging_began();
2194 }
2195
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07002196 check_battery_valid(chip);
2197
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002198 pr_debug("usb = %d, dc = %d batt = %d state=%d\n",
2199 chip->usb_present,
2200 chip->dc_present,
2201 get_prop_batt_present(chip),
2202 fsm_state);
2203}
2204
2205struct pm_chg_irq_init_data {
2206 unsigned int irq_id;
2207 char *name;
2208 unsigned long flags;
2209 irqreturn_t (*handler)(int, void *);
2210};
2211
2212#define CHG_IRQ(_id, _flags, _handler) \
2213{ \
2214 .irq_id = _id, \
2215 .name = #_id, \
2216 .flags = _flags, \
2217 .handler = _handler, \
2218}
2219struct pm_chg_irq_init_data chg_irq_data[] = {
2220 CHG_IRQ(USBIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2221 usbin_valid_irq_handler),
Abhijeet Dharmapurikar956215b2011-10-19 15:30:14 -07002222 CHG_IRQ(USBIN_OV_IRQ, IRQF_TRIGGER_RISING, usbin_ov_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002223 CHG_IRQ(BATT_INSERTED_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2224 batt_inserted_irq_handler),
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07002225 CHG_IRQ(VBATDET_LOW_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2226 vbatdet_low_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002227 CHG_IRQ(USBIN_UV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2228 usbin_uv_irq_handler),
2229 CHG_IRQ(VBAT_OV_IRQ, IRQF_TRIGGER_RISING, vbat_ov_irq_handler),
2230 CHG_IRQ(CHGWDOG_IRQ, IRQF_TRIGGER_RISING, chgwdog_irq_handler),
2231 CHG_IRQ(VCP_IRQ, IRQF_TRIGGER_RISING, vcp_irq_handler),
2232 CHG_IRQ(ATCDONE_IRQ, IRQF_TRIGGER_RISING, atcdone_irq_handler),
2233 CHG_IRQ(ATCFAIL_IRQ, IRQF_TRIGGER_RISING, atcfail_irq_handler),
2234 CHG_IRQ(CHGDONE_IRQ, IRQF_TRIGGER_RISING, chgdone_irq_handler),
2235 CHG_IRQ(CHGFAIL_IRQ, IRQF_TRIGGER_RISING, chgfail_irq_handler),
2236 CHG_IRQ(CHGSTATE_IRQ, IRQF_TRIGGER_RISING, chgstate_irq_handler),
2237 CHG_IRQ(LOOP_CHANGE_IRQ, IRQF_TRIGGER_RISING, loop_change_irq_handler),
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07002238 CHG_IRQ(FASTCHG_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2239 fastchg_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002240 CHG_IRQ(TRKLCHG_IRQ, IRQF_TRIGGER_RISING, trklchg_irq_handler),
2241 CHG_IRQ(BATT_REMOVED_IRQ, IRQF_TRIGGER_RISING,
2242 batt_removed_irq_handler),
2243 CHG_IRQ(BATTTEMP_HOT_IRQ, IRQF_TRIGGER_RISING,
2244 batttemp_hot_irq_handler),
2245 CHG_IRQ(CHGHOT_IRQ, IRQF_TRIGGER_RISING, chghot_irq_handler),
2246 CHG_IRQ(BATTTEMP_COLD_IRQ, IRQF_TRIGGER_RISING,
2247 batttemp_cold_irq_handler),
2248 CHG_IRQ(CHG_GONE_IRQ, IRQF_TRIGGER_RISING, chg_gone_irq_handler),
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07002249 CHG_IRQ(BAT_TEMP_OK_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2250 bat_temp_ok_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002251 CHG_IRQ(COARSE_DET_LOW_IRQ, IRQF_TRIGGER_RISING,
2252 coarse_det_low_irq_handler),
2253 CHG_IRQ(VDD_LOOP_IRQ, IRQF_TRIGGER_RISING, vdd_loop_irq_handler),
2254 CHG_IRQ(VREG_OV_IRQ, IRQF_TRIGGER_RISING, vreg_ov_irq_handler),
2255 CHG_IRQ(VBATDET_IRQ, IRQF_TRIGGER_RISING, vbatdet_irq_handler),
2256 CHG_IRQ(BATFET_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2257 batfet_irq_handler),
2258 CHG_IRQ(DCIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2259 dcin_valid_irq_handler),
2260 CHG_IRQ(DCIN_OV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2261 dcin_ov_irq_handler),
Abhijeet Dharmapurikar956215b2011-10-19 15:30:14 -07002262 CHG_IRQ(DCIN_UV_IRQ, IRQF_TRIGGER_RISING, dcin_uv_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002263};
2264
2265static int __devinit request_irqs(struct pm8921_chg_chip *chip,
2266 struct platform_device *pdev)
2267{
2268 struct resource *res;
2269 int ret, i;
2270
2271 ret = 0;
2272 bitmap_fill(chip->enabled_irqs, PM_CHG_MAX_INTS);
2273
2274 for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) {
2275 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
2276 chg_irq_data[i].name);
2277 if (res == NULL) {
2278 pr_err("couldn't find %s\n", chg_irq_data[i].name);
2279 goto err_out;
2280 }
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002281 chip->pmic_chg_irq[chg_irq_data[i].irq_id] = res->start;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002282 ret = request_irq(res->start, chg_irq_data[i].handler,
2283 chg_irq_data[i].flags,
2284 chg_irq_data[i].name, chip);
2285 if (ret < 0) {
2286 pr_err("couldn't request %d (%s) %d\n", res->start,
2287 chg_irq_data[i].name, ret);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002288 chip->pmic_chg_irq[chg_irq_data[i].irq_id] = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002289 goto err_out;
2290 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002291 pm8921_chg_disable_irq(chip, chg_irq_data[i].irq_id);
2292 }
2293 return 0;
2294
2295err_out:
2296 free_irqs(chip);
2297 return -EINVAL;
2298}
2299
2300#define ENUM_TIMER_STOP_BIT BIT(1)
2301#define BOOT_DONE_BIT BIT(6)
2302#define CHG_BATFET_ON_BIT BIT(3)
2303#define CHG_VCP_EN BIT(0)
2304#define CHG_BAT_TEMP_DIS_BIT BIT(2)
2305#define SAFE_CURRENT_MA 1500
2306static int __devinit pm8921_chg_hw_init(struct pm8921_chg_chip *chip)
2307{
2308 int rc;
2309
2310 rc = pm_chg_masked_write(chip, SYS_CONFIG_2,
2311 BOOT_DONE_BIT, BOOT_DONE_BIT);
2312 if (rc) {
2313 pr_err("Failed to set BOOT_DONE_BIT rc=%d\n", rc);
2314 return rc;
2315 }
2316
2317 rc = pm_chg_vddsafe_set(chip, chip->max_voltage);
2318 if (rc) {
2319 pr_err("Failed to set safe voltage to %d rc=%d\n",
2320 chip->max_voltage, rc);
2321 return rc;
2322 }
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002323 rc = pm_chg_vbatdet_set(chip,
2324 chip->max_voltage - chip->resume_voltage_delta);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002325 if (rc) {
2326 pr_err("Failed to set vbatdet comprator voltage to %d rc=%d\n",
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002327 chip->max_voltage - chip->resume_voltage_delta, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002328 return rc;
2329 }
2330
2331 rc = pm_chg_vddmax_set(chip, chip->max_voltage);
2332 if (rc) {
2333 pr_err("Failed to set max voltage to %d rc=%d\n",
2334 chip->max_voltage, rc);
2335 return rc;
2336 }
2337 rc = pm_chg_ibatsafe_set(chip, SAFE_CURRENT_MA);
2338 if (rc) {
2339 pr_err("Failed to set max voltage to %d rc=%d\n",
2340 SAFE_CURRENT_MA, rc);
2341 return rc;
2342 }
2343
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002344 rc = pm_chg_ibatmax_set(chip, chip->max_bat_chg_current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002345 if (rc) {
2346 pr_err("Failed to set max current to 400 rc=%d\n", rc);
2347 return rc;
2348 }
2349
2350 rc = pm_chg_iterm_set(chip, chip->term_current);
2351 if (rc) {
2352 pr_err("Failed to set term current to %d rc=%d\n",
2353 chip->term_current, rc);
2354 return rc;
2355 }
2356
2357 /* Disable the ENUM TIMER */
2358 rc = pm_chg_masked_write(chip, PBL_ACCESS2, ENUM_TIMER_STOP_BIT,
2359 ENUM_TIMER_STOP_BIT);
2360 if (rc) {
2361 pr_err("Failed to set enum timer stop rc=%d\n", rc);
2362 return rc;
2363 }
2364
2365 /* init with the lowest USB current */
2366 rc = pm_chg_iusbmax_set(chip, usb_ma_table[0].chg_iusb_value);
2367 if (rc) {
2368 pr_err("Failed to set usb max to %d rc=%d\n",
2369 usb_ma_table[0].chg_iusb_value, rc);
2370 return rc;
2371 }
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002372
2373 if (chip->safety_time != 0) {
2374 rc = pm_chg_tchg_max_set(chip, chip->safety_time);
2375 if (rc) {
2376 pr_err("Failed to set max time to %d minutes rc=%d\n",
2377 chip->safety_time, rc);
2378 return rc;
2379 }
2380 }
2381
2382 if (chip->ttrkl_time != 0) {
Abhijeet Dharmapurikare45c3d32011-10-19 19:46:48 -07002383 rc = pm_chg_ttrkl_max_set(chip, chip->ttrkl_time);
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002384 if (rc) {
2385 pr_err("Failed to set trkl time to %d minutes rc=%d\n",
2386 chip->safety_time, rc);
2387 return rc;
2388 }
2389 }
2390
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -07002391 if (chip->vin_min != 0) {
2392 rc = pm_chg_vinmin_set(chip, chip->vin_min);
2393 if (rc) {
2394 pr_err("Failed to set vin min to %d mV rc=%d\n",
2395 chip->vin_min, rc);
2396 return rc;
2397 }
2398 }
2399
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002400 rc = pm_chg_disable_wd(chip);
2401 if (rc) {
2402 pr_err("Failed to disable wd rc=%d\n", rc);
2403 return rc;
2404 }
2405
2406 rc = pm_chg_masked_write(chip, CHG_CNTRL_2,
2407 CHG_BAT_TEMP_DIS_BIT, 0);
2408 if (rc) {
2409 pr_err("Failed to enable temp control chg rc=%d\n", rc);
2410 return rc;
2411 }
2412 /* switch to a 3.2Mhz for the buck */
2413 rc = pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CLOCK_CTRL, 0x15);
2414 if (rc) {
2415 pr_err("Failed to switch buck clk rc=%d\n", rc);
2416 return rc;
2417 }
2418
Abhijeet Dharmapurikar26cef9c2011-08-25 19:16:42 -07002419 if (chip->trkl_voltage != 0) {
2420 rc = pm_chg_vtrkl_low_set(chip, chip->trkl_voltage);
2421 if (rc) {
2422 pr_err("Failed to set trkl voltage to %dmv rc=%d\n",
2423 chip->trkl_voltage, rc);
2424 return rc;
2425 }
2426 }
2427
2428 if (chip->weak_voltage != 0) {
2429 rc = pm_chg_vweak_set(chip, chip->weak_voltage);
2430 if (rc) {
2431 pr_err("Failed to set weak voltage to %dmv rc=%d\n",
2432 chip->weak_voltage, rc);
2433 return rc;
2434 }
2435 }
2436
2437 if (chip->trkl_current != 0) {
2438 rc = pm_chg_itrkl_set(chip, chip->trkl_current);
2439 if (rc) {
2440 pr_err("Failed to set trkl current to %dmA rc=%d\n",
2441 chip->trkl_voltage, rc);
2442 return rc;
2443 }
2444 }
2445
2446 if (chip->weak_current != 0) {
2447 rc = pm_chg_iweak_set(chip, chip->weak_current);
2448 if (rc) {
2449 pr_err("Failed to set weak current to %dmA rc=%d\n",
2450 chip->weak_current, rc);
2451 return rc;
2452 }
2453 }
2454
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -07002455 rc = pm_chg_batt_cold_temp_config(chip, chip->cold_thr);
2456 if (rc) {
2457 pr_err("Failed to set cold config %d rc=%d\n",
2458 chip->cold_thr, rc);
2459 }
2460
2461 rc = pm_chg_batt_hot_temp_config(chip, chip->hot_thr);
2462 if (rc) {
2463 pr_err("Failed to set hot config %d rc=%d\n",
2464 chip->hot_thr, rc);
2465 }
2466
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002467 /* Workarounds for die 1.1 and 1.0 */
2468 if (pm8xxx_get_revision(chip->dev->parent) < PM8XXX_REVISION_8921_2p0) {
2469 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST2, 0xF1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002470 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xCE);
2471 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xD8);
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07002472
2473 /* software workaround for correct battery_id detection */
2474 pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_0, 0xFF);
2475 pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_1, 0xFF);
2476 pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_2, 0xFF);
2477 pm8xxx_writeb(chip->dev->parent, PSI_TXRX_SAMPLE_DATA_3, 0xFF);
2478 pm8xxx_writeb(chip->dev->parent, PSI_CONFIG_STATUS, 0x0D);
2479 udelay(100);
2480 pm8xxx_writeb(chip->dev->parent, PSI_CONFIG_STATUS, 0x0C);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002481 }
2482
David Keitelb51995e2011-11-18 17:03:31 -08002483 /* Workarounds for die 3.0 */
2484 if (pm8xxx_get_revision(chip->dev->parent) == PM8XXX_REVISION_8921_3p0)
2485 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xAC);
2486
2487 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0xD9);
2488
David Keitela3c0d822011-11-03 14:18:52 -07002489 /* Disable EOC FSM processing */
2490 pm8xxx_writeb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, 0x91);
2491
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002492 rc = pm_chg_charge_dis(chip, charging_disabled);
2493 if (rc) {
2494 pr_err("Failed to disable CHG_CHARGE_DIS bit rc=%d\n", rc);
2495 return rc;
2496 }
2497
2498 rc = pm_chg_auto_enable(chip, !charging_disabled);
2499 if (rc) {
2500 pr_err("Failed to enable charging rc=%d\n", rc);
2501 return rc;
2502 }
2503
2504 return 0;
2505}
2506
2507static int get_rt_status(void *data, u64 * val)
2508{
2509 int i = (int)data;
2510 int ret;
2511
2512 /* global irq number is passed in via data */
2513 ret = pm_chg_get_rt_status(the_chip, i);
2514 *val = ret;
2515 return 0;
2516}
2517DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
2518
2519static int get_fsm_status(void *data, u64 * val)
2520{
2521 u8 temp;
2522
2523 temp = pm_chg_get_fsm_state(the_chip);
2524 *val = temp;
2525 return 0;
2526}
2527DEFINE_SIMPLE_ATTRIBUTE(fsm_fops, get_fsm_status, NULL, "%llu\n");
2528
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002529static int get_reg_loop(void *data, u64 * val)
2530{
2531 u8 temp;
2532
2533 if (!the_chip) {
2534 pr_err("%s called before init\n", __func__);
2535 return -EINVAL;
2536 }
2537 temp = pm_chg_get_regulation_loop(the_chip);
2538 *val = temp;
2539 return 0;
2540}
2541DEFINE_SIMPLE_ATTRIBUTE(reg_loop_fops, get_reg_loop, NULL, "0x%02llx\n");
2542
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002543static int get_reg(void *data, u64 * val)
2544{
2545 int addr = (int)data;
2546 int ret;
2547 u8 temp;
2548
2549 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
2550 if (ret) {
2551 pr_err("pm8xxx_readb to %x value =%d errored = %d\n",
2552 addr, temp, ret);
2553 return -EAGAIN;
2554 }
2555 *val = temp;
2556 return 0;
2557}
2558
2559static int set_reg(void *data, u64 val)
2560{
2561 int addr = (int)data;
2562 int ret;
2563 u8 temp;
2564
2565 temp = (u8) val;
2566 ret = pm8xxx_writeb(the_chip->dev->parent, addr, temp);
2567 if (ret) {
2568 pr_err("pm8xxx_writeb to %x value =%d errored = %d\n",
2569 addr, temp, ret);
2570 return -EAGAIN;
2571 }
2572 return 0;
2573}
2574DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
2575
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002576enum {
2577 BAT_WARM_ZONE,
2578 BAT_COOL_ZONE,
2579};
2580static int get_warm_cool(void *data, u64 * val)
2581{
2582 if (!the_chip) {
2583 pr_err("%s called before init\n", __func__);
2584 return -EINVAL;
2585 }
2586 if ((int)data == BAT_WARM_ZONE)
2587 *val = the_chip->is_bat_warm;
2588 if ((int)data == BAT_COOL_ZONE)
2589 *val = the_chip->is_bat_cool;
2590 return 0;
2591}
2592DEFINE_SIMPLE_ATTRIBUTE(warm_cool_fops, get_warm_cool, NULL, "0x%lld\n");
2593
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002594static void create_debugfs_entries(struct pm8921_chg_chip *chip)
2595{
2596 int i;
2597
2598 chip->dent = debugfs_create_dir("pm8921_chg", NULL);
2599
2600 if (IS_ERR(chip->dent)) {
2601 pr_err("pmic charger couldnt create debugfs dir\n");
2602 return;
2603 }
2604
2605 debugfs_create_file("CHG_CNTRL", 0644, chip->dent,
2606 (void *)CHG_CNTRL, &reg_fops);
2607 debugfs_create_file("CHG_CNTRL_2", 0644, chip->dent,
2608 (void *)CHG_CNTRL_2, &reg_fops);
2609 debugfs_create_file("CHG_CNTRL_3", 0644, chip->dent,
2610 (void *)CHG_CNTRL_3, &reg_fops);
2611 debugfs_create_file("PBL_ACCESS1", 0644, chip->dent,
2612 (void *)PBL_ACCESS1, &reg_fops);
2613 debugfs_create_file("PBL_ACCESS2", 0644, chip->dent,
2614 (void *)PBL_ACCESS2, &reg_fops);
2615 debugfs_create_file("SYS_CONFIG_1", 0644, chip->dent,
2616 (void *)SYS_CONFIG_1, &reg_fops);
2617 debugfs_create_file("SYS_CONFIG_2", 0644, chip->dent,
2618 (void *)SYS_CONFIG_2, &reg_fops);
2619 debugfs_create_file("CHG_VDD_MAX", 0644, chip->dent,
2620 (void *)CHG_VDD_MAX, &reg_fops);
2621 debugfs_create_file("CHG_VDD_SAFE", 0644, chip->dent,
2622 (void *)CHG_VDD_SAFE, &reg_fops);
2623 debugfs_create_file("CHG_VBAT_DET", 0644, chip->dent,
2624 (void *)CHG_VBAT_DET, &reg_fops);
2625 debugfs_create_file("CHG_IBAT_MAX", 0644, chip->dent,
2626 (void *)CHG_IBAT_MAX, &reg_fops);
2627 debugfs_create_file("CHG_IBAT_SAFE", 0644, chip->dent,
2628 (void *)CHG_IBAT_SAFE, &reg_fops);
2629 debugfs_create_file("CHG_VIN_MIN", 0644, chip->dent,
2630 (void *)CHG_VIN_MIN, &reg_fops);
2631 debugfs_create_file("CHG_VTRICKLE", 0644, chip->dent,
2632 (void *)CHG_VTRICKLE, &reg_fops);
2633 debugfs_create_file("CHG_ITRICKLE", 0644, chip->dent,
2634 (void *)CHG_ITRICKLE, &reg_fops);
2635 debugfs_create_file("CHG_ITERM", 0644, chip->dent,
2636 (void *)CHG_ITERM, &reg_fops);
2637 debugfs_create_file("CHG_TCHG_MAX", 0644, chip->dent,
2638 (void *)CHG_TCHG_MAX, &reg_fops);
2639 debugfs_create_file("CHG_TWDOG", 0644, chip->dent,
2640 (void *)CHG_TWDOG, &reg_fops);
2641 debugfs_create_file("CHG_TEMP_THRESH", 0644, chip->dent,
2642 (void *)CHG_TEMP_THRESH, &reg_fops);
2643 debugfs_create_file("CHG_COMP_OVR", 0644, chip->dent,
2644 (void *)CHG_COMP_OVR, &reg_fops);
2645 debugfs_create_file("CHG_BUCK_CTRL_TEST1", 0644, chip->dent,
2646 (void *)CHG_BUCK_CTRL_TEST1, &reg_fops);
2647 debugfs_create_file("CHG_BUCK_CTRL_TEST2", 0644, chip->dent,
2648 (void *)CHG_BUCK_CTRL_TEST2, &reg_fops);
2649 debugfs_create_file("CHG_BUCK_CTRL_TEST3", 0644, chip->dent,
2650 (void *)CHG_BUCK_CTRL_TEST3, &reg_fops);
2651 debugfs_create_file("CHG_TEST", 0644, chip->dent,
2652 (void *)CHG_TEST, &reg_fops);
2653
2654 debugfs_create_file("FSM_STATE", 0644, chip->dent, NULL,
2655 &fsm_fops);
2656
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002657 debugfs_create_file("REGULATION_LOOP_CONTROL", 0644, chip->dent, NULL,
2658 &reg_loop_fops);
2659
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002660 debugfs_create_file("BAT_WARM_ZONE", 0644, chip->dent,
2661 (void *)BAT_WARM_ZONE, &warm_cool_fops);
2662 debugfs_create_file("BAT_COOL_ZONE", 0644, chip->dent,
2663 (void *)BAT_COOL_ZONE, &warm_cool_fops);
2664
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002665 for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) {
2666 if (chip->pmic_chg_irq[chg_irq_data[i].irq_id])
2667 debugfs_create_file(chg_irq_data[i].name, 0444,
2668 chip->dent,
2669 (void *)chg_irq_data[i].irq_id,
2670 &rt_fops);
2671 }
2672}
2673
2674static int __devinit pm8921_charger_probe(struct platform_device *pdev)
2675{
2676 int rc = 0;
2677 struct pm8921_chg_chip *chip;
2678 const struct pm8921_charger_platform_data *pdata
2679 = pdev->dev.platform_data;
2680
2681 if (!pdata) {
2682 pr_err("missing platform data\n");
2683 return -EINVAL;
2684 }
2685
2686 chip = kzalloc(sizeof(struct pm8921_chg_chip),
2687 GFP_KERNEL);
2688 if (!chip) {
2689 pr_err("Cannot allocate pm_chg_chip\n");
2690 return -ENOMEM;
2691 }
2692
2693 chip->dev = &pdev->dev;
2694 chip->safety_time = pdata->safety_time;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002695 chip->ttrkl_time = pdata->ttrkl_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002696 chip->update_time = pdata->update_time;
2697 chip->max_voltage = pdata->max_voltage;
2698 chip->min_voltage = pdata->min_voltage;
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07002699 chip->resume_voltage_delta = pdata->resume_voltage_delta;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002700 chip->term_current = pdata->term_current;
2701 chip->vbat_channel = pdata->charger_cdata.vbat_channel;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002702 chip->batt_temp_channel = pdata->charger_cdata.batt_temp_channel;
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07002703 chip->batt_id_channel = pdata->charger_cdata.batt_id_channel;
2704 chip->batt_id_min = pdata->batt_id_min;
2705 chip->batt_id_max = pdata->batt_id_max;
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002706 chip->cool_temp = pdata->cool_temp;
2707 chip->warm_temp = pdata->warm_temp;
2708 chip->temp_check_period = pdata->temp_check_period;
2709 chip->max_bat_chg_current = pdata->max_bat_chg_current;
2710 chip->cool_bat_chg_current = pdata->cool_bat_chg_current;
2711 chip->warm_bat_chg_current = pdata->warm_bat_chg_current;
2712 chip->cool_bat_voltage = pdata->cool_bat_voltage;
2713 chip->warm_bat_voltage = pdata->warm_bat_voltage;
Abhijeet Dharmapurikar26cef9c2011-08-25 19:16:42 -07002714 chip->trkl_voltage = pdata->trkl_voltage;
2715 chip->weak_voltage = pdata->weak_voltage;
2716 chip->trkl_current = pdata->trkl_current;
2717 chip->weak_current = pdata->weak_current;
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -07002718 chip->vin_min = pdata->vin_min;
Abhijeet Dharmapurikarad742362011-08-29 19:50:02 -07002719 chip->thermal_mitigation = pdata->thermal_mitigation;
2720 chip->thermal_levels = pdata->thermal_levels;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002721
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -07002722 chip->cold_thr = pdata->cold_thr;
2723 chip->hot_thr = pdata->hot_thr;
2724
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002725 rc = pm8921_chg_hw_init(chip);
2726 if (rc) {
2727 pr_err("couldn't init hardware rc=%d\n", rc);
2728 goto free_chip;
2729 }
2730
2731 chip->usb_psy.name = "usb",
2732 chip->usb_psy.type = POWER_SUPPLY_TYPE_USB,
2733 chip->usb_psy.supplied_to = pm_power_supplied_to,
2734 chip->usb_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to),
2735 chip->usb_psy.properties = pm_power_props,
2736 chip->usb_psy.num_properties = ARRAY_SIZE(pm_power_props),
2737 chip->usb_psy.get_property = pm_power_get_property,
2738
2739 chip->dc_psy.name = "ac",
2740 chip->dc_psy.type = POWER_SUPPLY_TYPE_MAINS,
2741 chip->dc_psy.supplied_to = pm_power_supplied_to,
2742 chip->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to),
2743 chip->dc_psy.properties = pm_power_props,
2744 chip->dc_psy.num_properties = ARRAY_SIZE(pm_power_props),
2745 chip->dc_psy.get_property = pm_power_get_property,
2746
2747 chip->batt_psy.name = "battery",
2748 chip->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY,
2749 chip->batt_psy.properties = msm_batt_power_props,
2750 chip->batt_psy.num_properties = ARRAY_SIZE(msm_batt_power_props),
2751 chip->batt_psy.get_property = pm_batt_power_get_property,
2752
2753 rc = power_supply_register(chip->dev, &chip->usb_psy);
2754 if (rc < 0) {
2755 pr_err("power_supply_register usb failed rc = %d\n", rc);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002756 goto free_chip;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002757 }
2758
2759 rc = power_supply_register(chip->dev, &chip->dc_psy);
2760 if (rc < 0) {
2761 pr_err("power_supply_register dc failed rc = %d\n", rc);
2762 goto unregister_usb;
2763 }
2764
2765 rc = power_supply_register(chip->dev, &chip->batt_psy);
2766 if (rc < 0) {
2767 pr_err("power_supply_register batt failed rc = %d\n", rc);
2768 goto unregister_dc;
2769 }
2770
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002771 platform_set_drvdata(pdev, chip);
2772 the_chip = chip;
2773
2774 wake_lock_init(&chip->eoc_wake_lock, WAKE_LOCK_SUSPEND, "pm8921_eoc");
Amir Samuelovd31ef502011-10-26 14:41:36 +02002775 INIT_DELAYED_WORK(&chip->eoc_work, eoc_worker);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002776
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002777 rc = request_irqs(chip, pdev);
2778 if (rc) {
2779 pr_err("couldn't register interrupts rc=%d\n", rc);
2780 goto unregister_batt;
2781 }
2782
Abhijeet Dharmapurikare27d8092011-08-12 17:16:29 -07002783 enable_irq_wake(chip->pmic_chg_irq[USBIN_VALID_IRQ]);
2784 enable_irq_wake(chip->pmic_chg_irq[USBIN_OV_IRQ]);
2785 enable_irq_wake(chip->pmic_chg_irq[USBIN_UV_IRQ]);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07002786 /*
2787 * if both the cool_temp and warm_temp are zero the device doesnt
2788 * care for jeita compliance
2789 */
2790 if (!(chip->cool_temp == 0 && chip->warm_temp == 0)) {
2791 rc = configure_btm(chip);
2792 if (rc) {
2793 pr_err("couldn't register with btm rc=%d\n", rc);
2794 goto free_irq;
2795 }
2796 }
Abhijeet Dharmapurikare27d8092011-08-12 17:16:29 -07002797
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002798 create_debugfs_entries(chip);
2799
2800 INIT_WORK(&chip->bms_notify.work, bms_notify);
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07002801 INIT_WORK(&chip->battery_id_valid_work, battery_id_valid);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002802
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002803 /* determine what state the charger is in */
2804 determine_initial_state(chip);
2805
Abhijeet Dharmapurikar33fe6fb2011-09-14 16:03:11 -07002806 if (chip->update_time) {
2807 INIT_DELAYED_WORK(&chip->update_heartbeat_work,
2808 update_heartbeat);
2809 schedule_delayed_work(&chip->update_heartbeat_work,
2810 round_jiffies_relative(msecs_to_jiffies
2811 (chip->update_time)));
2812 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002813 return 0;
2814
2815free_irq:
2816 free_irqs(chip);
2817unregister_batt:
2818 power_supply_unregister(&chip->batt_psy);
2819unregister_dc:
2820 power_supply_unregister(&chip->dc_psy);
2821unregister_usb:
2822 power_supply_unregister(&chip->usb_psy);
2823free_chip:
2824 kfree(chip);
2825 return rc;
2826}
2827
2828static int __devexit pm8921_charger_remove(struct platform_device *pdev)
2829{
2830 struct pm8921_chg_chip *chip = platform_get_drvdata(pdev);
2831
2832 free_irqs(chip);
2833 platform_set_drvdata(pdev, NULL);
2834 the_chip = NULL;
2835 kfree(chip);
2836 return 0;
2837}
2838
2839static struct platform_driver pm8921_charger_driver = {
2840 .probe = pm8921_charger_probe,
2841 .remove = __devexit_p(pm8921_charger_remove),
2842 .driver = {
2843 .name = PM8921_CHARGER_DEV_NAME,
2844 .owner = THIS_MODULE,
2845 },
2846};
2847
2848static int __init pm8921_charger_init(void)
2849{
2850 return platform_driver_register(&pm8921_charger_driver);
2851}
2852
2853static void __exit pm8921_charger_exit(void)
2854{
2855 platform_driver_unregister(&pm8921_charger_driver);
2856}
2857
2858late_initcall(pm8921_charger_init);
2859module_exit(pm8921_charger_exit);
2860
2861MODULE_LICENSE("GPL v2");
2862MODULE_DESCRIPTION("PMIC8921 charger/battery driver");
2863MODULE_VERSION("1.0");
2864MODULE_ALIAS("platform:" PM8921_CHARGER_DEV_NAME);