blob: 03b3e0d8817ef9853f79c572aa0c8cd21c4c264e [file] [log] [blame]
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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>
David Keitel663abad2012-09-04 21:00:27 -070024#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025#include <linux/interrupt.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026#include <linux/delay.h>
27#include <linux/bitops.h>
28#include <linux/workqueue.h>
29#include <linux/debugfs.h>
30#include <linux/slab.h>
David Keitel8b079fb2012-06-28 19:14:30 -070031#include <linux/mfd/pm8xxx/batt-alarm.h>
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -070032#include <linux/ratelimit.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
34#include <mach/msm_xo.h>
35#include <mach/msm_hsusb.h>
36
37#define CHG_BUCK_CLOCK_CTRL 0x14
David Keitel3567bec2012-06-26 16:35:53 -070038#define CHG_BUCK_CLOCK_CTRL_8038 0xD
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039
40#define PBL_ACCESS1 0x04
41#define PBL_ACCESS2 0x05
42#define SYS_CONFIG_1 0x06
43#define SYS_CONFIG_2 0x07
44#define CHG_CNTRL 0x204
45#define CHG_IBAT_MAX 0x205
46#define CHG_TEST 0x206
47#define CHG_BUCK_CTRL_TEST1 0x207
48#define CHG_BUCK_CTRL_TEST2 0x208
49#define CHG_BUCK_CTRL_TEST3 0x209
50#define COMPARATOR_OVERRIDE 0x20A
51#define PSI_TXRX_SAMPLE_DATA_0 0x20B
52#define PSI_TXRX_SAMPLE_DATA_1 0x20C
53#define PSI_TXRX_SAMPLE_DATA_2 0x20D
54#define PSI_TXRX_SAMPLE_DATA_3 0x20E
55#define PSI_CONFIG_STATUS 0x20F
56#define CHG_IBAT_SAFE 0x210
57#define CHG_ITRICKLE 0x211
58#define CHG_CNTRL_2 0x212
59#define CHG_VBAT_DET 0x213
60#define CHG_VTRICKLE 0x214
61#define CHG_ITERM 0x215
62#define CHG_CNTRL_3 0x216
63#define CHG_VIN_MIN 0x217
64#define CHG_TWDOG 0x218
65#define CHG_TTRKL_MAX 0x219
66#define CHG_TEMP_THRESH 0x21A
67#define CHG_TCHG_MAX 0x21B
68#define USB_OVP_CONTROL 0x21C
69#define DC_OVP_CONTROL 0x21D
70#define USB_OVP_TEST 0x21E
71#define DC_OVP_TEST 0x21F
72#define CHG_VDD_MAX 0x220
73#define CHG_VDD_SAFE 0x221
74#define CHG_VBAT_BOOT_THRESH 0x222
75#define USB_OVP_TRIM 0x355
76#define BUCK_CONTROL_TRIM1 0x356
77#define BUCK_CONTROL_TRIM2 0x357
78#define BUCK_CONTROL_TRIM3 0x358
79#define BUCK_CONTROL_TRIM4 0x359
80#define CHG_DEFAULTS_TRIM 0x35A
81#define CHG_ITRIM 0x35B
82#define CHG_TTRIM 0x35C
83#define CHG_COMP_OVR 0x20A
David Keitel38bdd4f2012-04-19 15:39:13 -070084#define IUSB_FINE_RES 0x2B6
David Keitel0789fc62012-06-07 17:43:27 -070085#define OVP_USB_UVD 0x2B7
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070086
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -070087/* check EOC every 10 seconds */
88#define EOC_CHECK_PERIOD_MS 10000
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -080089/* check for USB unplug every 200 msecs */
90#define UNPLUG_CHECK_WAIT_PERIOD_MS 200
Abhijeet Dharmapurikarb73001a2012-11-25 22:56:52 -080091#define UNPLUG_CHECK_RAMP_MS 25
David Keitel1e3a82f2012-06-26 16:27:15 -070092#define USB_TRIM_ENTRIES 16
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -070093
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094enum chg_fsm_state {
95 FSM_STATE_OFF_0 = 0,
96 FSM_STATE_BATFETDET_START_12 = 12,
97 FSM_STATE_BATFETDET_END_16 = 16,
98 FSM_STATE_ON_CHG_HIGHI_1 = 1,
99 FSM_STATE_ATC_2A = 2,
100 FSM_STATE_ATC_2B = 18,
101 FSM_STATE_ON_BAT_3 = 3,
102 FSM_STATE_ATC_FAIL_4 = 4 ,
103 FSM_STATE_DELAY_5 = 5,
104 FSM_STATE_ON_CHG_AND_BAT_6 = 6,
105 FSM_STATE_FAST_CHG_7 = 7,
106 FSM_STATE_TRKL_CHG_8 = 8,
107 FSM_STATE_CHG_FAIL_9 = 9,
108 FSM_STATE_EOC_10 = 10,
109 FSM_STATE_ON_CHG_VREGOK_11 = 11,
110 FSM_STATE_ATC_PAUSE_13 = 13,
111 FSM_STATE_FAST_CHG_PAUSE_14 = 14,
112 FSM_STATE_TRKL_CHG_PAUSE_15 = 15,
113 FSM_STATE_START_BOOT = 20,
114 FSM_STATE_FLCB_VREGOK = 21,
115 FSM_STATE_FLCB = 22,
116};
117
Abhijeet Dharmapurikar236aa5a2011-11-03 12:17:19 -0700118struct fsm_state_to_batt_status {
119 enum chg_fsm_state fsm_state;
120 int batt_state;
121};
122
123static struct fsm_state_to_batt_status map[] = {
124 {FSM_STATE_OFF_0, POWER_SUPPLY_STATUS_UNKNOWN},
125 {FSM_STATE_BATFETDET_START_12, POWER_SUPPLY_STATUS_UNKNOWN},
126 {FSM_STATE_BATFETDET_END_16, POWER_SUPPLY_STATUS_UNKNOWN},
127 /*
128 * for CHG_HIGHI_1 report NOT_CHARGING if battery missing,
129 * too hot/cold, charger too hot
130 */
131 {FSM_STATE_ON_CHG_HIGHI_1, POWER_SUPPLY_STATUS_FULL},
132 {FSM_STATE_ATC_2A, POWER_SUPPLY_STATUS_CHARGING},
133 {FSM_STATE_ATC_2B, POWER_SUPPLY_STATUS_CHARGING},
134 {FSM_STATE_ON_BAT_3, POWER_SUPPLY_STATUS_DISCHARGING},
135 {FSM_STATE_ATC_FAIL_4, POWER_SUPPLY_STATUS_DISCHARGING},
136 {FSM_STATE_DELAY_5, POWER_SUPPLY_STATUS_UNKNOWN },
137 {FSM_STATE_ON_CHG_AND_BAT_6, POWER_SUPPLY_STATUS_CHARGING},
138 {FSM_STATE_FAST_CHG_7, POWER_SUPPLY_STATUS_CHARGING},
139 {FSM_STATE_TRKL_CHG_8, POWER_SUPPLY_STATUS_CHARGING},
140 {FSM_STATE_CHG_FAIL_9, POWER_SUPPLY_STATUS_DISCHARGING},
141 {FSM_STATE_EOC_10, POWER_SUPPLY_STATUS_FULL},
142 {FSM_STATE_ON_CHG_VREGOK_11, POWER_SUPPLY_STATUS_NOT_CHARGING},
143 {FSM_STATE_ATC_PAUSE_13, POWER_SUPPLY_STATUS_NOT_CHARGING},
144 {FSM_STATE_FAST_CHG_PAUSE_14, POWER_SUPPLY_STATUS_NOT_CHARGING},
145 {FSM_STATE_TRKL_CHG_PAUSE_15, POWER_SUPPLY_STATUS_NOT_CHARGING},
146 {FSM_STATE_START_BOOT, POWER_SUPPLY_STATUS_NOT_CHARGING},
147 {FSM_STATE_FLCB_VREGOK, POWER_SUPPLY_STATUS_NOT_CHARGING},
148 {FSM_STATE_FLCB, POWER_SUPPLY_STATUS_NOT_CHARGING},
149};
150
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700151enum chg_regulation_loop {
152 VDD_LOOP = BIT(3),
153 BAT_CURRENT_LOOP = BIT(2),
154 INPUT_CURRENT_LOOP = BIT(1),
155 INPUT_VOLTAGE_LOOP = BIT(0),
156 CHG_ALL_LOOPS = VDD_LOOP | BAT_CURRENT_LOOP
157 | INPUT_CURRENT_LOOP | INPUT_VOLTAGE_LOOP,
158};
159
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160enum pmic_chg_interrupts {
161 USBIN_VALID_IRQ = 0,
162 USBIN_OV_IRQ,
163 BATT_INSERTED_IRQ,
164 VBATDET_LOW_IRQ,
165 USBIN_UV_IRQ,
166 VBAT_OV_IRQ,
167 CHGWDOG_IRQ,
168 VCP_IRQ,
169 ATCDONE_IRQ,
170 ATCFAIL_IRQ,
171 CHGDONE_IRQ,
172 CHGFAIL_IRQ,
173 CHGSTATE_IRQ,
174 LOOP_CHANGE_IRQ,
175 FASTCHG_IRQ,
176 TRKLCHG_IRQ,
177 BATT_REMOVED_IRQ,
178 BATTTEMP_HOT_IRQ,
179 CHGHOT_IRQ,
180 BATTTEMP_COLD_IRQ,
181 CHG_GONE_IRQ,
182 BAT_TEMP_OK_IRQ,
183 COARSE_DET_LOW_IRQ,
184 VDD_LOOP_IRQ,
185 VREG_OV_IRQ,
186 VBATDET_IRQ,
187 BATFET_IRQ,
188 PSI_IRQ,
189 DCIN_VALID_IRQ,
190 DCIN_OV_IRQ,
191 DCIN_UV_IRQ,
192 PM_CHG_MAX_INTS,
193};
194
195struct bms_notify {
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -0700196 int is_battery_full;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197 int is_charging;
198 struct work_struct work;
199};
200
201/**
202 * struct pm8921_chg_chip -device information
203 * @dev: device pointer to access the parent
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 * @usb_present: present status of usb
205 * @dc_present: present status of dc
206 * @usb_charger_current: usb current to charge the battery with used when
207 * the usb path is enabled or charging is resumed
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 * @update_time: how frequently the userland needs to be updated
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -0800209 * @max_voltage_mv: the max volts the batt should be charged up to
210 * @min_voltage_mv: the min battery voltage before turning the FETon
David Keitel0789fc62012-06-07 17:43:27 -0700211 * @uvd_voltage_mv: (PM8917 only) the falling UVD threshold voltage
David Keitel8b079fb2012-06-28 19:14:30 -0700212 * @alarm_low_mv: the battery alarm voltage low
213 * @alarm_high_mv: the battery alarm voltage high
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -0800214 * @cool_temp_dc: the cool temp threshold in deciCelcius
215 * @warm_temp_dc: the warm temp threshold in deciCelcius
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -0700216 * @resume_voltage_delta: the voltage delta from vdd max at which the
217 * battery should resume charging
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 * @term_current: The charging based term current
219 *
220 */
221struct pm8921_chg_chip {
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700222 struct device *dev;
223 unsigned int usb_present;
224 unsigned int dc_present;
225 unsigned int usb_charger_current;
226 unsigned int max_bat_chg_current;
227 unsigned int pmic_chg_irq[PM_CHG_MAX_INTS];
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700228 unsigned int ttrkl_time;
229 unsigned int update_time;
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -0800230 unsigned int max_voltage_mv;
231 unsigned int min_voltage_mv;
David Keitel0789fc62012-06-07 17:43:27 -0700232 unsigned int uvd_voltage_mv;
Abhijeet Dharmapurikara8f35422012-10-19 10:50:36 -0700233 unsigned int safe_current_ma;
David Keitel8b079fb2012-06-28 19:14:30 -0700234 unsigned int alarm_low_mv;
235 unsigned int alarm_high_mv;
Abhijeet Dharmapurikarbc38f632012-01-10 09:30:11 -0800236 int cool_temp_dc;
237 int warm_temp_dc;
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700238 unsigned int temp_check_period;
239 unsigned int cool_bat_chg_current;
240 unsigned int warm_bat_chg_current;
241 unsigned int cool_bat_voltage;
242 unsigned int warm_bat_voltage;
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -0700243 unsigned int is_bat_cool;
244 unsigned int is_bat_warm;
245 unsigned int resume_voltage_delta;
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -0700246 int resume_charge_percent;
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700247 unsigned int term_current;
248 unsigned int vbat_channel;
249 unsigned int batt_temp_channel;
250 unsigned int batt_id_channel;
251 struct power_supply usb_psy;
David Keitel6ed71a52012-01-30 12:42:18 -0800252 struct power_supply dc_psy;
David Keitel88e1b572012-01-11 11:57:14 -0800253 struct power_supply *ext_psy;
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700254 struct power_supply batt_psy;
255 struct dentry *dent;
256 struct bms_notify bms_notify;
David Keitel1e3a82f2012-06-26 16:27:15 -0700257 int *usb_trim_table;
Amir Samuelovd31ef502011-10-26 14:41:36 +0200258 bool ext_charging;
259 bool ext_charge_done;
David Keitel38bdd4f2012-04-19 15:39:13 -0700260 bool iusb_fine_res;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 DECLARE_BITMAP(enabled_irqs, PM_CHG_MAX_INTS);
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700262 struct work_struct battery_id_valid_work;
263 int64_t batt_id_min;
264 int64_t batt_id_max;
265 int trkl_voltage;
266 int weak_voltage;
267 int trkl_current;
268 int weak_current;
269 int vin_min;
Abhijeet Dharmapurikard585ff22011-11-10 14:27:25 -0800270 unsigned int *thermal_mitigation;
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700271 int thermal_levels;
272 struct delayed_work update_heartbeat_work;
273 struct delayed_work eoc_work;
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -0800274 struct delayed_work unplug_check_work;
David Keitelacf57c82012-03-07 18:48:50 -0800275 struct delayed_work vin_collapse_check_work;
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -0800276 struct delayed_work btc_override_work;
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -0700277 struct wake_lock eoc_wake_lock;
278 enum pm8921_chg_cold_thr cold_thr;
279 enum pm8921_chg_hot_thr hot_thr;
Abhijeet Dharmapurikar40491ce2011-11-07 11:38:54 -0800280 int rconn_mohm;
Jay Chokshid674a512012-03-15 14:06:04 -0700281 enum pm8921_chg_led_src_config led_src_config;
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -0700282 bool host_mode;
David Keitela3cf9432012-08-24 15:11:23 -0700283 bool has_dc_supply;
David Keitel6ccbf132012-05-30 14:21:24 -0700284 u8 active_path;
Abhijeet Dharmapurikarde9c7e72012-07-15 16:29:51 -0700285 int recent_reported_soc;
Abhijeet Dharmapurikarbbaef592012-10-10 10:07:51 -0700286 int battery_less_hardware;
Abhijeet Dharmapurikar3ee5b072012-10-15 17:47:29 -0700287 int ibatmax_max_adj_ma;
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -0800288 int btc_override;
289 int btc_override_cold_decidegc;
290 int btc_override_hot_decidegc;
291 int btc_delay_ms;
292 bool btc_panic_if_cant_stop_chg;
Abhijeet Dharmapurikarf8a66e92012-12-13 15:46:01 -0800293 int stop_chg_upon_expiry;
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -0800294 bool disable_aicl;
Anirudh Ghayal1e5bb372013-02-20 09:48:42 +0530295 int usb_type;
Anirudh Ghayal80c729b2013-02-22 11:17:19 +0530296 bool disable_chg_rmvl_wrkarnd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297};
298
David Keitelacf57c82012-03-07 18:48:50 -0800299/* user space parameter to limit usb current */
300static unsigned int usb_max_current;
301/*
302 * usb_target_ma is used for wall charger
303 * adaptive input current limiting only. Use
304 * pm_iusbmax_get() to get current maximum usb current setting.
305 */
306static int usb_target_ma;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307static int charging_disabled;
Abhijeet Dharmapurikarad742362011-08-29 19:50:02 -0700308static int thermal_mitigation;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700309
310static struct pm8921_chg_chip *the_chip;
311
David Keitel663abad2012-09-04 21:00:27 -0700312#define LPM_ENABLE_BIT BIT(2)
313static int pm8921_chg_set_lpm(struct pm8921_chg_chip *chip, int enable)
314{
315 int rc;
316 u8 reg;
317
318 rc = pm8xxx_readb(chip->dev->parent, CHG_CNTRL, &reg);
319 if (rc) {
320 pr_err("pm8xxx_readb failed: addr=%03X, rc=%d\n",
321 CHG_CNTRL, rc);
322 return rc;
323 }
324 reg &= ~LPM_ENABLE_BIT;
325 reg |= (enable ? LPM_ENABLE_BIT : 0);
326
327 rc = pm8xxx_writeb(chip->dev->parent, CHG_CNTRL, reg);
328 if (rc) {
329 pr_err("pm_chg_write failed: addr=%03X, rc=%d\n",
330 CHG_CNTRL, rc);
331 return rc;
332 }
333
334 return rc;
335}
336
David Keiteladf790e2012-11-28 12:56:55 -0800337static int pm_chg_write(struct pm8921_chg_chip *chip, u16 addr, u8 reg)
338{
339 int rc;
David Keitel663abad2012-09-04 21:00:27 -0700340
Abhijeet Dharmapurikarbba40912013-03-01 18:25:05 -0800341 rc = pm8xxx_writeb(chip->dev->parent, addr, reg);
342 if (rc)
343 pr_err("failed: addr=%03X, rc=%d\n", addr, rc);
David Keitel663abad2012-09-04 21:00:27 -0700344
David Keiteladf790e2012-11-28 12:56:55 -0800345 return rc;
346}
347
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348static int pm_chg_masked_write(struct pm8921_chg_chip *chip, u16 addr,
349 u8 mask, u8 val)
350{
351 int rc;
352 u8 reg;
353
354 rc = pm8xxx_readb(chip->dev->parent, addr, &reg);
355 if (rc) {
356 pr_err("pm8xxx_readb failed: addr=%03X, rc=%d\n", addr, rc);
357 return rc;
358 }
359 reg &= ~mask;
360 reg |= val & mask;
David Keiteladf790e2012-11-28 12:56:55 -0800361 rc = pm_chg_write(chip, addr, reg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362 if (rc) {
David Keiteladf790e2012-11-28 12:56:55 -0800363 pr_err("pm_chg_write failed: addr=%03X, rc=%d\n", addr, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364 return rc;
365 }
366 return 0;
367}
368
David Keitelcf867232012-01-27 18:40:12 -0800369static int pm_chg_get_rt_status(struct pm8921_chg_chip *chip, int irq_id)
370{
371 return pm8xxx_read_irq_stat(chip->dev->parent,
372 chip->pmic_chg_irq[irq_id]);
373}
374
375/* Treat OverVoltage/UnderVoltage as source missing */
376static int is_usb_chg_plugged_in(struct pm8921_chg_chip *chip)
377{
378 return pm_chg_get_rt_status(chip, USBIN_VALID_IRQ);
379}
380
381/* Treat OverVoltage/UnderVoltage as source missing */
382static int is_dc_chg_plugged_in(struct pm8921_chg_chip *chip)
383{
384 return pm_chg_get_rt_status(chip, DCIN_VALID_IRQ);
385}
386
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -0700387static int is_batfet_closed(struct pm8921_chg_chip *chip)
388{
389 return pm_chg_get_rt_status(chip, BATFET_IRQ);
390}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391#define CAPTURE_FSM_STATE_CMD 0xC2
392#define READ_BANK_7 0x70
393#define READ_BANK_4 0x40
394static int pm_chg_get_fsm_state(struct pm8921_chg_chip *chip)
395{
396 u8 temp;
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800397 int err = 0, ret = 0;
398
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 temp = CAPTURE_FSM_STATE_CMD;
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800400 err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700401 if (err) {
402 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800403 goto err_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404 }
405
406 temp = READ_BANK_7;
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800407 err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 if (err) {
409 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800410 goto err_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700411 }
412
413 err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp);
414 if (err) {
415 pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err);
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800416 goto err_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417 }
418 /* get the lower 4 bits */
419 ret = temp & 0xF;
420
421 temp = READ_BANK_4;
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800422 err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 if (err) {
424 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800425 goto err_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 }
427
428 err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &temp);
429 if (err) {
430 pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err);
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800431 goto err_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432 }
433 /* get the upper 1 bit */
434 ret |= (temp & 0x1) << 4;
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800435
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800436err_out:
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800437 if (err)
438 return err;
439
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440 return ret;
441}
442
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700443#define READ_BANK_6 0x60
444static int pm_chg_get_regulation_loop(struct pm8921_chg_chip *chip)
445{
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800446 u8 temp, data;
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800447 int err = 0;
448
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700449 temp = READ_BANK_6;
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800450 err = pm8xxx_writeb(chip->dev->parent, CHG_TEST, temp);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700451 if (err) {
452 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800453 goto err_out;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700454 }
455
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800456 err = pm8xxx_readb(chip->dev->parent, CHG_TEST, &data);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700457 if (err) {
458 pr_err("pm8xxx_readb fail: addr=%03X, rc=%d\n", CHG_TEST, err);
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800459 goto err_out;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700460 }
461
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800462err_out:
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800463 if (err)
464 return err;
465
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700466 /* return the lower 4 bits */
Abhijeet Dharmapurikar63941cc2013-01-07 18:17:46 -0800467 return data & CHG_ALL_LOOPS;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700468}
469
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700470#define CHG_USB_SUSPEND_BIT BIT(2)
471static int pm_chg_usb_suspend_enable(struct pm8921_chg_chip *chip, int enable)
472{
473 return pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_USB_SUSPEND_BIT,
474 enable ? CHG_USB_SUSPEND_BIT : 0);
475}
476
477#define CHG_EN_BIT BIT(7)
478static int pm_chg_auto_enable(struct pm8921_chg_chip *chip, int enable)
479{
480 return pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_EN_BIT,
481 enable ? CHG_EN_BIT : 0);
482}
483
David Keitel753ec8d2011-11-02 10:56:37 -0700484#define CHG_FAILED_CLEAR BIT(0)
485#define ATC_FAILED_CLEAR BIT(1)
486static int pm_chg_failed_clear(struct pm8921_chg_chip *chip, int clear)
487{
488 int rc;
489
490 rc = pm_chg_masked_write(chip, CHG_CNTRL_3, ATC_FAILED_CLEAR,
491 clear ? ATC_FAILED_CLEAR : 0);
492 rc |= pm_chg_masked_write(chip, CHG_CNTRL_3, CHG_FAILED_CLEAR,
493 clear ? CHG_FAILED_CLEAR : 0);
494 return rc;
495}
496
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497#define CHG_CHARGE_DIS_BIT BIT(1)
498static int pm_chg_charge_dis(struct pm8921_chg_chip *chip, int disable)
499{
500 return pm_chg_masked_write(chip, CHG_CNTRL, CHG_CHARGE_DIS_BIT,
501 disable ? CHG_CHARGE_DIS_BIT : 0);
502}
503
Abhijeet Dharmapurikar9da392d2012-02-22 18:59:57 -0800504static int pm_is_chg_charge_dis(struct pm8921_chg_chip *chip)
505{
506 u8 temp;
507
508 pm8xxx_readb(chip->dev->parent, CHG_CNTRL, &temp);
509 return temp & CHG_CHARGE_DIS_BIT;
510}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511#define PM8921_CHG_V_MIN_MV 3240
512#define PM8921_CHG_V_STEP_MV 20
Abhijeet Dharmapurikar08e7e0a2012-02-22 19:34:20 -0800513#define PM8921_CHG_V_STEP_10MV_OFFSET_BIT BIT(7)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514#define PM8921_CHG_VDDMAX_MAX 4500
515#define PM8921_CHG_VDDMAX_MIN 3400
516#define PM8921_CHG_V_MASK 0x7F
David Keitelcf867232012-01-27 18:40:12 -0800517static int __pm_chg_vddmax_set(struct pm8921_chg_chip *chip, int voltage)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518{
Abhijeet Dharmapurikar08e7e0a2012-02-22 19:34:20 -0800519 int remainder;
David Keitelcf867232012-01-27 18:40:12 -0800520 u8 temp = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521
Abhijeet Dharmapurikar08e7e0a2012-02-22 19:34:20 -0800522 if (voltage < PM8921_CHG_VDDMAX_MIN
523 || voltage > PM8921_CHG_VDDMAX_MAX) {
524 pr_err("bad mV=%d asked to set\n", voltage);
525 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 }
David Keitelcf867232012-01-27 18:40:12 -0800527
Abhijeet Dharmapurikar08e7e0a2012-02-22 19:34:20 -0800528 temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV;
529
530 remainder = voltage % 20;
531 if (remainder >= 10) {
532 temp |= PM8921_CHG_V_STEP_10MV_OFFSET_BIT;
533 }
534
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535 pr_debug("voltage=%d setting %02x\n", voltage, temp);
David Keiteladf790e2012-11-28 12:56:55 -0800536 return pm_chg_write(chip, CHG_VDD_MAX, temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537}
538
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700539static int pm_chg_vddmax_get(struct pm8921_chg_chip *chip, int *voltage)
540{
541 u8 temp;
542 int rc;
543
544 rc = pm8xxx_readb(chip->dev->parent, CHG_VDD_MAX, &temp);
545 if (rc) {
546 pr_err("rc = %d while reading vdd max\n", rc);
Stephen Boyd81982232011-09-26 12:08:38 -0700547 *voltage = 0;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700548 return rc;
549 }
Abhijeet Dharmapurikar08e7e0a2012-02-22 19:34:20 -0800550 *voltage = (int)(temp & PM8921_CHG_V_MASK) * PM8921_CHG_V_STEP_MV
551 + PM8921_CHG_V_MIN_MV;
552 if (temp & PM8921_CHG_V_STEP_10MV_OFFSET_BIT)
553 *voltage = *voltage + 10;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700554 return 0;
555}
556
David Keitelcf867232012-01-27 18:40:12 -0800557static int pm_chg_vddmax_set(struct pm8921_chg_chip *chip, int voltage)
558{
559 int current_mv, ret, steps, i;
560 bool increase;
561
562 ret = 0;
563
564 if (voltage < PM8921_CHG_VDDMAX_MIN
565 || voltage > PM8921_CHG_VDDMAX_MAX) {
566 pr_err("bad mV=%d asked to set\n", voltage);
567 return -EINVAL;
568 }
569
570 ret = pm_chg_vddmax_get(chip, &current_mv);
571 if (ret) {
572 pr_err("Failed to read vddmax rc=%d\n", ret);
573 return -EINVAL;
574 }
575 if (current_mv == voltage)
576 return 0;
577
578 /* Only change in increments when USB is present */
579 if (is_usb_chg_plugged_in(chip)) {
580 if (current_mv < voltage) {
581 steps = (voltage - current_mv) / PM8921_CHG_V_STEP_MV;
582 increase = true;
583 } else {
584 steps = (current_mv - voltage) / PM8921_CHG_V_STEP_MV;
585 increase = false;
586 }
587 for (i = 0; i < steps; i++) {
588 if (increase)
589 current_mv += PM8921_CHG_V_STEP_MV;
590 else
591 current_mv -= PM8921_CHG_V_STEP_MV;
592 ret |= __pm_chg_vddmax_set(chip, current_mv);
593 }
594 }
595 ret |= __pm_chg_vddmax_set(chip, voltage);
596 return ret;
597}
598
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599#define PM8921_CHG_VDDSAFE_MIN 3400
600#define PM8921_CHG_VDDSAFE_MAX 4500
601static int pm_chg_vddsafe_set(struct pm8921_chg_chip *chip, int voltage)
602{
603 u8 temp;
604
605 if (voltage < PM8921_CHG_VDDSAFE_MIN
606 || voltage > PM8921_CHG_VDDSAFE_MAX) {
607 pr_err("bad mV=%d asked to set\n", voltage);
608 return -EINVAL;
609 }
610 temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV;
611 pr_debug("voltage=%d setting %02x\n", voltage, temp);
612 return pm_chg_masked_write(chip, CHG_VDD_SAFE, PM8921_CHG_V_MASK, temp);
613}
614
615#define PM8921_CHG_VBATDET_MIN 3240
616#define PM8921_CHG_VBATDET_MAX 5780
617static int pm_chg_vbatdet_set(struct pm8921_chg_chip *chip, int voltage)
618{
619 u8 temp;
620
621 if (voltage < PM8921_CHG_VBATDET_MIN
622 || voltage > PM8921_CHG_VBATDET_MAX) {
623 pr_err("bad mV=%d asked to set\n", voltage);
624 return -EINVAL;
625 }
626 temp = (voltage - PM8921_CHG_V_MIN_MV) / PM8921_CHG_V_STEP_MV;
627 pr_debug("voltage=%d setting %02x\n", voltage, temp);
628 return pm_chg_masked_write(chip, CHG_VBAT_DET, PM8921_CHG_V_MASK, temp);
629}
630
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -0700631#define PM8921_CHG_VINMIN_MIN_MV 3800
632#define PM8921_CHG_VINMIN_STEP_MV 100
633#define PM8921_CHG_VINMIN_USABLE_MAX 6500
634#define PM8921_CHG_VINMIN_USABLE_MIN 4300
635#define PM8921_CHG_VINMIN_MASK 0x1F
636static int pm_chg_vinmin_set(struct pm8921_chg_chip *chip, int voltage)
637{
638 u8 temp;
639
640 if (voltage < PM8921_CHG_VINMIN_USABLE_MIN
641 || voltage > PM8921_CHG_VINMIN_USABLE_MAX) {
642 pr_err("bad mV=%d asked to set\n", voltage);
643 return -EINVAL;
644 }
645 temp = (voltage - PM8921_CHG_VINMIN_MIN_MV) / PM8921_CHG_VINMIN_STEP_MV;
646 pr_debug("voltage=%d setting %02x\n", voltage, temp);
647 return pm_chg_masked_write(chip, CHG_VIN_MIN, PM8921_CHG_VINMIN_MASK,
648 temp);
649}
650
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -0800651static int pm_chg_vinmin_get(struct pm8921_chg_chip *chip)
652{
653 u8 temp;
654 int rc, voltage_mv;
655
656 rc = pm8xxx_readb(chip->dev->parent, CHG_VIN_MIN, &temp);
657 temp &= PM8921_CHG_VINMIN_MASK;
658
659 voltage_mv = PM8921_CHG_VINMIN_MIN_MV +
660 (int)temp * PM8921_CHG_VINMIN_STEP_MV;
661
662 return voltage_mv;
663}
664
David Keitel0789fc62012-06-07 17:43:27 -0700665#define PM8917_USB_UVD_MIN_MV 3850
666#define PM8917_USB_UVD_MAX_MV 4350
667#define PM8917_USB_UVD_STEP_MV 100
668#define PM8917_USB_UVD_MASK 0x7
669static int pm_chg_uvd_threshold_set(struct pm8921_chg_chip *chip, int thresh_mv)
670{
671 u8 temp;
672
673 if (thresh_mv < PM8917_USB_UVD_MIN_MV
674 || thresh_mv > PM8917_USB_UVD_MAX_MV) {
675 pr_err("bad mV=%d asked to set\n", thresh_mv);
676 return -EINVAL;
677 }
678 temp = (thresh_mv - PM8917_USB_UVD_MIN_MV) / PM8917_USB_UVD_STEP_MV;
679 return pm_chg_masked_write(chip, OVP_USB_UVD,
680 PM8917_USB_UVD_MASK, temp);
681}
682
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700683#define PM8921_CHG_IBATMAX_MIN 325
Abhijeet Dharmapurikar3ee5b072012-10-15 17:47:29 -0700684#define PM8921_CHG_IBATMAX_MAX 3025
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685#define PM8921_CHG_I_MIN_MA 225
686#define PM8921_CHG_I_STEP_MA 50
687#define PM8921_CHG_I_MASK 0x3F
Abhijeet Dharmapurikar3ee5b072012-10-15 17:47:29 -0700688static int pm_chg_ibatmax_get(struct pm8921_chg_chip *chip, int *ibat_ma)
689{
690 u8 temp;
691 int rc;
692
693 rc = pm8xxx_readb(chip->dev->parent, CHG_IBAT_MAX, &temp);
694 if (rc) {
695 pr_err("rc = %d while reading ibat max\n", rc);
696 *ibat_ma = 0;
697 return rc;
698 }
699 *ibat_ma = (int)(temp & PM8921_CHG_I_MASK) * PM8921_CHG_I_STEP_MA
700 + PM8921_CHG_I_MIN_MA;
701 return 0;
702}
703
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700704static int pm_chg_ibatmax_set(struct pm8921_chg_chip *chip, int chg_current)
705{
706 u8 temp;
707
708 if (chg_current < PM8921_CHG_IBATMAX_MIN
709 || chg_current > PM8921_CHG_IBATMAX_MAX) {
710 pr_err("bad mA=%d asked to set\n", chg_current);
711 return -EINVAL;
712 }
713 temp = (chg_current - PM8921_CHG_I_MIN_MA) / PM8921_CHG_I_STEP_MA;
714 return pm_chg_masked_write(chip, CHG_IBAT_MAX, PM8921_CHG_I_MASK, temp);
715}
716
717#define PM8921_CHG_IBATSAFE_MIN 225
718#define PM8921_CHG_IBATSAFE_MAX 3375
719static int pm_chg_ibatsafe_set(struct pm8921_chg_chip *chip, int chg_current)
720{
721 u8 temp;
722
723 if (chg_current < PM8921_CHG_IBATSAFE_MIN
724 || chg_current > PM8921_CHG_IBATSAFE_MAX) {
725 pr_err("bad mA=%d asked to set\n", chg_current);
726 return -EINVAL;
727 }
728 temp = (chg_current - PM8921_CHG_I_MIN_MA) / PM8921_CHG_I_STEP_MA;
729 return pm_chg_masked_write(chip, CHG_IBAT_SAFE,
730 PM8921_CHG_I_MASK, temp);
731}
732
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733#define PM8921_CHG_ITERM_MIN_MA 50
Abhijeet Dharmapurikar71816fd2011-08-12 17:14:10 -0700734#define PM8921_CHG_ITERM_MAX_MA 200
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735#define PM8921_CHG_ITERM_STEP_MA 10
736#define PM8921_CHG_ITERM_MASK 0xF
737static int pm_chg_iterm_set(struct pm8921_chg_chip *chip, int chg_current)
738{
739 u8 temp;
740
Abhijeet Dharmapurikar71816fd2011-08-12 17:14:10 -0700741 if (chg_current < PM8921_CHG_ITERM_MIN_MA
742 || chg_current > PM8921_CHG_ITERM_MAX_MA) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743 pr_err("bad mA=%d asked to set\n", chg_current);
744 return -EINVAL;
745 }
746
747 temp = (chg_current - PM8921_CHG_ITERM_MIN_MA)
748 / PM8921_CHG_ITERM_STEP_MA;
Abhijeet Dharmapurikar71816fd2011-08-12 17:14:10 -0700749 return pm_chg_masked_write(chip, CHG_ITERM, PM8921_CHG_ITERM_MASK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750 temp);
751}
752
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700753static int pm_chg_iterm_get(struct pm8921_chg_chip *chip, int *chg_current)
754{
755 u8 temp;
756 int rc;
757
758 rc = pm8xxx_readb(chip->dev->parent, CHG_ITERM, &temp);
759 if (rc) {
760 pr_err("err=%d reading CHG_ITEM\n", rc);
Stephen Boyd81982232011-09-26 12:08:38 -0700761 *chg_current = 0;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -0700762 return rc;
763 }
764 temp &= PM8921_CHG_ITERM_MASK;
765 *chg_current = (int)temp * PM8921_CHG_ITERM_STEP_MA
766 + PM8921_CHG_ITERM_MIN_MA;
767 return 0;
768}
769
Abhijeet Dharmapurikar28fd0c62012-01-27 13:28:29 -0800770struct usb_ma_limit_entry {
David Keitel38bdd4f2012-04-19 15:39:13 -0700771 int usb_ma;
772 u8 value;
Abhijeet Dharmapurikar28fd0c62012-01-27 13:28:29 -0800773};
774
David Keitel1e3a82f2012-06-26 16:27:15 -0700775/* USB Trim tables */
776static int usb_trim_8038_table[USB_TRIM_ENTRIES] = {
777 0x0,
778 0x0,
779 -0x9,
780 0x0,
781 -0xD,
782 0x0,
783 -0x10,
784 -0x11,
785 0x0,
786 0x0,
787 -0x25,
788 0x0,
789 -0x28,
790 0x0,
791 -0x32,
792 0x0
793};
794
795static int usb_trim_8917_table[USB_TRIM_ENTRIES] = {
796 0x0,
797 0x0,
798 0xA,
799 0xC,
800 0x10,
801 0x10,
802 0x13,
803 0x14,
804 0x13,
David Keitelb923d2e2012-11-08 11:28:11 -0800805 0x3,
David Keitel1e3a82f2012-06-26 16:27:15 -0700806 0x1A,
807 0x1D,
808 0x1D,
809 0x21,
810 0x24,
811 0x26
812};
813
814/* Maximum USB setting table */
Abhijeet Dharmapurikar28fd0c62012-01-27 13:28:29 -0800815static struct usb_ma_limit_entry usb_ma_table[] = {
David Keitel38bdd4f2012-04-19 15:39:13 -0700816 {100, 0x0},
817 {200, 0x1},
818 {500, 0x2},
819 {600, 0x3},
820 {700, 0x4},
821 {800, 0x5},
822 {850, 0x6},
823 {900, 0x8},
824 {950, 0x7},
825 {1000, 0x9},
826 {1100, 0xA},
827 {1200, 0xB},
828 {1300, 0xC},
829 {1400, 0xD},
830 {1500, 0xE},
831 {1600, 0xF},
Abhijeet Dharmapurikar28fd0c62012-01-27 13:28:29 -0800832};
833
David Keitel1bc3e252012-12-05 15:54:13 -0800834#define REG_SBI_CONFIG 0x04F
835#define PAGE3_ENABLE_MASK 0x6
836#define USB_OVP_TRIM_MASK 0x3F
837#define USB_OVP_TRIM_PM8917_MASK 0x7F
838#define USB_OVP_TRIM_MIN 0x00
David Keitel1e3a82f2012-06-26 16:27:15 -0700839#define REG_USB_OVP_TRIM_ORIG_LSB 0x10A
840#define REG_USB_OVP_TRIM_ORIG_MSB 0x09C
David Keitel1bc3e252012-12-05 15:54:13 -0800841#define REG_USB_OVP_TRIM_PM8917 0x2B5
842#define REG_USB_OVP_TRIM_PM8917_BIT BIT(0)
David Keitel1e3a82f2012-06-26 16:27:15 -0700843static int pm_chg_usb_trim(struct pm8921_chg_chip *chip, int index)
844{
David Keitel1bc3e252012-12-05 15:54:13 -0800845 u8 temp, sbi_config, msb, lsb, mask;
David Keitel1e3a82f2012-06-26 16:27:15 -0700846 s8 trim;
847 int rc = 0;
848 static u8 usb_trim_reg_orig = 0xFF;
849
850 /* No trim data for PM8921 */
851 if (!chip->usb_trim_table)
852 return 0;
853
854 if (usb_trim_reg_orig == 0xFF) {
855 rc = pm8xxx_readb(chip->dev->parent,
856 REG_USB_OVP_TRIM_ORIG_MSB, &msb);
857 if (rc) {
858 pr_err("error = %d reading sbi config reg\n", rc);
859 return rc;
860 }
861
862 rc = pm8xxx_readb(chip->dev->parent,
863 REG_USB_OVP_TRIM_ORIG_LSB, &lsb);
864 if (rc) {
865 pr_err("error = %d reading sbi config reg\n", rc);
866 return rc;
867 }
868
869 msb = msb >> 5;
870 lsb = lsb >> 5;
871 usb_trim_reg_orig = msb << 3 | lsb;
David Keitel1bc3e252012-12-05 15:54:13 -0800872
873 if (pm8xxx_get_version(chip->dev->parent)
874 == PM8XXX_VERSION_8917) {
875 rc = pm8xxx_readb(chip->dev->parent,
876 REG_USB_OVP_TRIM_PM8917, &msb);
877 if (rc) {
878 pr_err("error = %d reading config reg\n", rc);
879 return rc;
880 }
881
882 msb = msb & REG_USB_OVP_TRIM_PM8917_BIT;
883 usb_trim_reg_orig |= msb << 6;
884 }
David Keitel1e3a82f2012-06-26 16:27:15 -0700885 }
886
887 /* use the original trim value */
888 trim = usb_trim_reg_orig;
889
890 trim += chip->usb_trim_table[index];
891 if (trim < 0)
892 trim = 0;
893
Abhijeet Dharmapurikar7016f122013-01-11 19:00:05 -0800894 pr_debug("trim_orig %d write 0x%x index=%d value 0x%x to USB_OVP_TRIM\n",
David Keitel1e3a82f2012-06-26 16:27:15 -0700895 usb_trim_reg_orig, trim, index, chip->usb_trim_table[index]);
896
897 rc = pm8xxx_readb(chip->dev->parent, REG_SBI_CONFIG, &sbi_config);
898 if (rc) {
899 pr_err("error = %d reading sbi config reg\n", rc);
900 return rc;
901 }
902
903 temp = sbi_config | PAGE3_ENABLE_MASK;
David Keiteladf790e2012-11-28 12:56:55 -0800904 rc = pm_chg_write(chip, REG_SBI_CONFIG, temp);
David Keitel1e3a82f2012-06-26 16:27:15 -0700905 if (rc) {
906 pr_err("error = %d writing sbi config reg\n", rc);
907 return rc;
908 }
909
David Keitel1bc3e252012-12-05 15:54:13 -0800910 mask = USB_OVP_TRIM_MASK;
911
912 if (pm8xxx_get_version(chip->dev->parent) == PM8XXX_VERSION_8917)
913 mask = USB_OVP_TRIM_PM8917_MASK;
914
915 rc = pm_chg_masked_write(chip, USB_OVP_TRIM, mask, trim);
David Keitel1e3a82f2012-06-26 16:27:15 -0700916 if (rc) {
917 pr_err("error = %d writing USB_OVP_TRIM\n", rc);
918 return rc;
919 }
920
David Keiteladf790e2012-11-28 12:56:55 -0800921 rc = pm_chg_write(chip, REG_SBI_CONFIG, sbi_config);
David Keitel1e3a82f2012-06-26 16:27:15 -0700922 if (rc) {
923 pr_err("error = %d writing sbi config reg\n", rc);
924 return rc;
925 }
926 return rc;
927}
928
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700929#define PM8921_CHG_IUSB_MASK 0x1C
David Keitel38bdd4f2012-04-19 15:39:13 -0700930#define PM8921_CHG_IUSB_SHIFT 2
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700931#define PM8921_CHG_IUSB_MAX 7
932#define PM8921_CHG_IUSB_MIN 0
David Keitel38bdd4f2012-04-19 15:39:13 -0700933#define PM8917_IUSB_FINE_RES BIT(0)
David Keitel1e3a82f2012-06-26 16:27:15 -0700934static int pm_chg_iusbmax_set(struct pm8921_chg_chip *chip, int index)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935{
David Keitel1e3a82f2012-06-26 16:27:15 -0700936 u8 temp, fineres, reg_val;
David Keitel38bdd4f2012-04-19 15:39:13 -0700937 int rc;
938
David Keitel1e3a82f2012-06-26 16:27:15 -0700939 reg_val = usb_ma_table[index].value >> 1;
940 fineres = PM8917_IUSB_FINE_RES & usb_ma_table[index].value;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941
Abhijeet Dharmapurikar485a0532011-08-12 17:18:10 -0700942 if (reg_val < PM8921_CHG_IUSB_MIN || reg_val > PM8921_CHG_IUSB_MAX) {
943 pr_err("bad mA=%d asked to set\n", reg_val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944 return -EINVAL;
945 }
David Keitel38bdd4f2012-04-19 15:39:13 -0700946 temp = reg_val << PM8921_CHG_IUSB_SHIFT;
947
948 /* IUSB_FINE_RES */
949 if (chip->iusb_fine_res) {
950 /* Clear IUSB_FINE_RES bit to avoid overshoot */
951 rc = pm_chg_masked_write(chip, IUSB_FINE_RES,
952 PM8917_IUSB_FINE_RES, 0);
953
954 rc |= pm_chg_masked_write(chip, PBL_ACCESS2,
955 PM8921_CHG_IUSB_MASK, temp);
956
957 if (rc) {
958 pr_err("Failed to write PBL_ACCESS2 rc=%d\n", rc);
959 return rc;
960 }
961
962 if (fineres) {
963 rc = pm_chg_masked_write(chip, IUSB_FINE_RES,
964 PM8917_IUSB_FINE_RES, fineres);
David Keitel1e3a82f2012-06-26 16:27:15 -0700965 if (rc) {
David Keitel38bdd4f2012-04-19 15:39:13 -0700966 pr_err("Failed to write ISUB_FINE_RES rc=%d\n",
967 rc);
David Keitel1e3a82f2012-06-26 16:27:15 -0700968 return rc;
969 }
David Keitel38bdd4f2012-04-19 15:39:13 -0700970 }
971 } else {
972 rc = pm_chg_masked_write(chip, PBL_ACCESS2,
973 PM8921_CHG_IUSB_MASK, temp);
David Keitel1e3a82f2012-06-26 16:27:15 -0700974 if (rc) {
David Keitel38bdd4f2012-04-19 15:39:13 -0700975 pr_err("Failed to write PBL_ACCESS2 rc=%d\n", rc);
David Keitel1e3a82f2012-06-26 16:27:15 -0700976 return rc;
977 }
David Keitel38bdd4f2012-04-19 15:39:13 -0700978 }
979
David Keitel1e3a82f2012-06-26 16:27:15 -0700980 rc = pm_chg_usb_trim(chip, index);
981 if (rc)
982 pr_err("unable to set usb trim rc = %d\n", rc);
983
David Keitel38bdd4f2012-04-19 15:39:13 -0700984 return rc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985}
986
Abhijeet Dharmapurikar28fd0c62012-01-27 13:28:29 -0800987static int pm_chg_iusbmax_get(struct pm8921_chg_chip *chip, int *mA)
988{
David Keitel38bdd4f2012-04-19 15:39:13 -0700989 u8 temp, fineres;
990 int rc, i;
Abhijeet Dharmapurikar28fd0c62012-01-27 13:28:29 -0800991
David Keitel38bdd4f2012-04-19 15:39:13 -0700992 fineres = 0;
Abhijeet Dharmapurikar28fd0c62012-01-27 13:28:29 -0800993 *mA = 0;
994 rc = pm8xxx_readb(chip->dev->parent, PBL_ACCESS2, &temp);
995 if (rc) {
996 pr_err("err=%d reading PBL_ACCESS2\n", rc);
997 return rc;
998 }
David Keitel38bdd4f2012-04-19 15:39:13 -0700999
1000 if (chip->iusb_fine_res) {
1001 rc = pm8xxx_readb(chip->dev->parent, IUSB_FINE_RES, &fineres);
1002 if (rc) {
1003 pr_err("err=%d reading IUSB_FINE_RES\n", rc);
1004 return rc;
1005 }
1006 }
Abhijeet Dharmapurikar28fd0c62012-01-27 13:28:29 -08001007 temp &= PM8921_CHG_IUSB_MASK;
David Keitel38bdd4f2012-04-19 15:39:13 -07001008 temp = temp >> PM8921_CHG_IUSB_SHIFT;
1009
1010 temp = (temp << 1) | (fineres & PM8917_IUSB_FINE_RES);
1011 for (i = ARRAY_SIZE(usb_ma_table) - 1; i >= 0; i--) {
1012 if (usb_ma_table[i].value == temp)
1013 break;
1014 }
1015
Willie Ruan69566002012-11-15 11:56:36 -08001016 if (i < 0) {
1017 pr_err("can't find %d in usb_ma_table. Use min.\n", temp);
1018 i = 0;
1019 }
1020
David Keitel38bdd4f2012-04-19 15:39:13 -07001021 *mA = usb_ma_table[i].usb_ma;
1022
Abhijeet Dharmapurikar28fd0c62012-01-27 13:28:29 -08001023 return rc;
1024}
1025
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026#define PM8921_CHG_WD_MASK 0x1F
1027static int pm_chg_disable_wd(struct pm8921_chg_chip *chip)
1028{
1029 /* writing 0 to the wd timer disables it */
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001030 return pm_chg_masked_write(chip, CHG_TWDOG, PM8921_CHG_WD_MASK, 0);
1031}
1032
Abhijeet Dharmapurikarc26cd902011-10-26 18:07:45 -07001033#define PM8921_CHG_TCHG_MASK 0x7F
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001034#define PM8921_CHG_TCHG_MIN 4
1035#define PM8921_CHG_TCHG_MAX 512
1036#define PM8921_CHG_TCHG_STEP 4
1037static int pm_chg_tchg_max_set(struct pm8921_chg_chip *chip, int minutes)
1038{
1039 u8 temp;
1040
1041 if (minutes < PM8921_CHG_TCHG_MIN || minutes > PM8921_CHG_TCHG_MAX) {
1042 pr_err("bad max minutes =%d asked to set\n", minutes);
1043 return -EINVAL;
1044 }
1045
1046 temp = (minutes - 1)/PM8921_CHG_TCHG_STEP;
1047 return pm_chg_masked_write(chip, CHG_TCHG_MAX, PM8921_CHG_TCHG_MASK,
1048 temp);
1049}
1050
David Keitelfb81b0a2012-08-31 20:00:53 -07001051#define PM8921_CHG_TTRKL_MASK 0x3F
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001052#define PM8921_CHG_TTRKL_MIN 1
1053#define PM8921_CHG_TTRKL_MAX 64
1054static int pm_chg_ttrkl_max_set(struct pm8921_chg_chip *chip, int minutes)
1055{
1056 u8 temp;
1057
1058 if (minutes < PM8921_CHG_TTRKL_MIN || minutes > PM8921_CHG_TTRKL_MAX) {
1059 pr_err("bad max minutes =%d asked to set\n", minutes);
1060 return -EINVAL;
1061 }
1062
1063 temp = minutes - 1;
1064 return pm_chg_masked_write(chip, CHG_TTRKL_MAX, PM8921_CHG_TTRKL_MASK,
1065 temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001066}
1067
Abhijeet Dharmapurikar26cef9c2011-08-25 19:16:42 -07001068#define PM8921_CHG_VTRKL_MIN_MV 2050
1069#define PM8921_CHG_VTRKL_MAX_MV 2800
1070#define PM8921_CHG_VTRKL_STEP_MV 50
1071#define PM8921_CHG_VTRKL_SHIFT 4
1072#define PM8921_CHG_VTRKL_MASK 0xF0
1073static int pm_chg_vtrkl_low_set(struct pm8921_chg_chip *chip, int millivolts)
1074{
1075 u8 temp;
1076
1077 if (millivolts < PM8921_CHG_VTRKL_MIN_MV
1078 || millivolts > PM8921_CHG_VTRKL_MAX_MV) {
1079 pr_err("bad voltage = %dmV asked to set\n", millivolts);
1080 return -EINVAL;
1081 }
1082
1083 temp = (millivolts - PM8921_CHG_VTRKL_MIN_MV)/PM8921_CHG_VTRKL_STEP_MV;
1084 temp = temp << PM8921_CHG_VTRKL_SHIFT;
1085 return pm_chg_masked_write(chip, CHG_VTRICKLE, PM8921_CHG_VTRKL_MASK,
1086 temp);
1087}
1088
1089#define PM8921_CHG_VWEAK_MIN_MV 2100
1090#define PM8921_CHG_VWEAK_MAX_MV 3600
1091#define PM8921_CHG_VWEAK_STEP_MV 100
1092#define PM8921_CHG_VWEAK_MASK 0x0F
1093static int pm_chg_vweak_set(struct pm8921_chg_chip *chip, int millivolts)
1094{
1095 u8 temp;
1096
1097 if (millivolts < PM8921_CHG_VWEAK_MIN_MV
1098 || millivolts > PM8921_CHG_VWEAK_MAX_MV) {
1099 pr_err("bad voltage = %dmV asked to set\n", millivolts);
1100 return -EINVAL;
1101 }
1102
1103 temp = (millivolts - PM8921_CHG_VWEAK_MIN_MV)/PM8921_CHG_VWEAK_STEP_MV;
1104 return pm_chg_masked_write(chip, CHG_VTRICKLE, PM8921_CHG_VWEAK_MASK,
1105 temp);
1106}
1107
1108#define PM8921_CHG_ITRKL_MIN_MA 50
1109#define PM8921_CHG_ITRKL_MAX_MA 200
1110#define PM8921_CHG_ITRKL_MASK 0x0F
1111#define PM8921_CHG_ITRKL_STEP_MA 10
1112static int pm_chg_itrkl_set(struct pm8921_chg_chip *chip, int milliamps)
1113{
1114 u8 temp;
1115
1116 if (milliamps < PM8921_CHG_ITRKL_MIN_MA
1117 || milliamps > PM8921_CHG_ITRKL_MAX_MA) {
1118 pr_err("bad current = %dmA asked to set\n", milliamps);
1119 return -EINVAL;
1120 }
1121
1122 temp = (milliamps - PM8921_CHG_ITRKL_MIN_MA)/PM8921_CHG_ITRKL_STEP_MA;
1123
1124 return pm_chg_masked_write(chip, CHG_ITRICKLE, PM8921_CHG_ITRKL_MASK,
1125 temp);
1126}
1127
1128#define PM8921_CHG_IWEAK_MIN_MA 325
1129#define PM8921_CHG_IWEAK_MAX_MA 525
1130#define PM8921_CHG_IWEAK_SHIFT 7
1131#define PM8921_CHG_IWEAK_MASK 0x80
1132static int pm_chg_iweak_set(struct pm8921_chg_chip *chip, int milliamps)
1133{
1134 u8 temp;
1135
1136 if (milliamps < PM8921_CHG_IWEAK_MIN_MA
1137 || milliamps > PM8921_CHG_IWEAK_MAX_MA) {
1138 pr_err("bad current = %dmA asked to set\n", milliamps);
1139 return -EINVAL;
1140 }
1141
1142 if (milliamps < PM8921_CHG_IWEAK_MAX_MA)
1143 temp = 0;
1144 else
1145 temp = 1;
1146
1147 temp = temp << PM8921_CHG_IWEAK_SHIFT;
1148 return pm_chg_masked_write(chip, CHG_ITRICKLE, PM8921_CHG_IWEAK_MASK,
1149 temp);
1150}
1151
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -07001152#define PM8921_CHG_BATT_TEMP_THR_COLD BIT(1)
1153#define PM8921_CHG_BATT_TEMP_THR_COLD_SHIFT 1
1154static int pm_chg_batt_cold_temp_config(struct pm8921_chg_chip *chip,
1155 enum pm8921_chg_cold_thr cold_thr)
1156{
1157 u8 temp;
1158
1159 temp = cold_thr << PM8921_CHG_BATT_TEMP_THR_COLD_SHIFT;
1160 temp = temp & PM8921_CHG_BATT_TEMP_THR_COLD;
1161 return pm_chg_masked_write(chip, CHG_CNTRL_2,
1162 PM8921_CHG_BATT_TEMP_THR_COLD,
1163 temp);
1164}
1165
1166#define PM8921_CHG_BATT_TEMP_THR_HOT BIT(0)
1167#define PM8921_CHG_BATT_TEMP_THR_HOT_SHIFT 0
1168static int pm_chg_batt_hot_temp_config(struct pm8921_chg_chip *chip,
1169 enum pm8921_chg_hot_thr hot_thr)
1170{
1171 u8 temp;
1172
1173 temp = hot_thr << PM8921_CHG_BATT_TEMP_THR_HOT_SHIFT;
1174 temp = temp & PM8921_CHG_BATT_TEMP_THR_HOT;
1175 return pm_chg_masked_write(chip, CHG_CNTRL_2,
1176 PM8921_CHG_BATT_TEMP_THR_HOT,
1177 temp);
1178}
1179
Jay Chokshid674a512012-03-15 14:06:04 -07001180#define PM8921_CHG_LED_SRC_CONFIG_SHIFT 4
1181#define PM8921_CHG_LED_SRC_CONFIG_MASK 0x30
1182static int pm_chg_led_src_config(struct pm8921_chg_chip *chip,
1183 enum pm8921_chg_led_src_config led_src_config)
1184{
1185 u8 temp;
1186
1187 if (led_src_config < LED_SRC_GND ||
1188 led_src_config > LED_SRC_BYPASS)
1189 return -EINVAL;
1190
1191 if (led_src_config == LED_SRC_BYPASS)
1192 return 0;
1193
1194 temp = led_src_config << PM8921_CHG_LED_SRC_CONFIG_SHIFT;
1195
1196 return pm_chg_masked_write(chip, CHG_CNTRL_3,
1197 PM8921_CHG_LED_SRC_CONFIG_MASK, temp);
1198}
1199
David Keitel8c5201a2012-02-24 16:08:54 -08001200
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07001201static int64_t read_battery_id(struct pm8921_chg_chip *chip)
1202{
1203 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001204 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07001205
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001206 rc = pm8xxx_adc_read(chip->batt_id_channel, &result);
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07001207 if (rc) {
1208 pr_err("error reading batt id channel = %d, rc = %d\n",
1209 chip->vbat_channel, rc);
1210 return rc;
1211 }
1212 pr_debug("batt_id phy = %lld meas = 0x%llx\n", result.physical,
1213 result.measurement);
1214 return result.physical;
1215}
1216
1217static int is_battery_valid(struct pm8921_chg_chip *chip)
1218{
1219 int64_t rc;
1220
1221 if (chip->batt_id_min == 0 && chip->batt_id_max == 0)
1222 return 1;
1223
1224 rc = read_battery_id(chip);
1225 if (rc < 0) {
1226 pr_err("error reading batt id channel = %d, rc = %lld\n",
1227 chip->vbat_channel, rc);
1228 /* assume battery id is valid when adc error happens */
1229 return 1;
1230 }
1231
1232 if (rc < chip->batt_id_min || rc > chip->batt_id_max) {
1233 pr_err("batt_id phy =%lld is not valid\n", rc);
1234 return 0;
1235 }
1236 return 1;
1237}
1238
1239static void check_battery_valid(struct pm8921_chg_chip *chip)
1240{
1241 if (is_battery_valid(chip) == 0) {
1242 pr_err("batt_id not valid, disbling charging\n");
1243 pm_chg_auto_enable(chip, 0);
1244 } else {
1245 pm_chg_auto_enable(chip, !charging_disabled);
1246 }
1247}
1248
1249static void battery_id_valid(struct work_struct *work)
1250{
1251 struct pm8921_chg_chip *chip = container_of(work,
1252 struct pm8921_chg_chip, battery_id_valid_work);
1253
1254 check_battery_valid(chip);
1255}
1256
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001257static void pm8921_chg_enable_irq(struct pm8921_chg_chip *chip, int interrupt)
1258{
1259 if (!__test_and_set_bit(interrupt, chip->enabled_irqs)) {
1260 dev_dbg(chip->dev, "%d\n", chip->pmic_chg_irq[interrupt]);
1261 enable_irq(chip->pmic_chg_irq[interrupt]);
1262 }
1263}
1264
1265static void pm8921_chg_disable_irq(struct pm8921_chg_chip *chip, int interrupt)
1266{
1267 if (__test_and_clear_bit(interrupt, chip->enabled_irqs)) {
1268 dev_dbg(chip->dev, "%d\n", chip->pmic_chg_irq[interrupt]);
1269 disable_irq_nosync(chip->pmic_chg_irq[interrupt]);
1270 }
1271}
1272
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08001273static int pm8921_chg_is_enabled(struct pm8921_chg_chip *chip, int interrupt)
1274{
1275 return test_bit(interrupt, chip->enabled_irqs);
1276}
1277
Amir Samuelovd31ef502011-10-26 14:41:36 +02001278static bool is_ext_charging(struct pm8921_chg_chip *chip)
1279{
David Keitel88e1b572012-01-11 11:57:14 -08001280 union power_supply_propval ret = {0,};
Amir Samuelovd31ef502011-10-26 14:41:36 +02001281
David Keitel88e1b572012-01-11 11:57:14 -08001282 if (!chip->ext_psy)
1283 return false;
1284 if (chip->ext_psy->get_property(chip->ext_psy,
1285 POWER_SUPPLY_PROP_CHARGE_TYPE, &ret))
1286 return false;
1287 if (ret.intval > POWER_SUPPLY_CHARGE_TYPE_NONE)
1288 return ret.intval;
Amir Samuelovd31ef502011-10-26 14:41:36 +02001289
1290 return false;
1291}
1292
1293static bool is_ext_trickle_charging(struct pm8921_chg_chip *chip)
1294{
David Keitel88e1b572012-01-11 11:57:14 -08001295 union power_supply_propval ret = {0,};
Amir Samuelovd31ef502011-10-26 14:41:36 +02001296
David Keitel88e1b572012-01-11 11:57:14 -08001297 if (!chip->ext_psy)
1298 return false;
1299 if (chip->ext_psy->get_property(chip->ext_psy,
1300 POWER_SUPPLY_PROP_CHARGE_TYPE, &ret))
1301 return false;
1302 if (ret.intval == POWER_SUPPLY_CHARGE_TYPE_TRICKLE)
Amir Samuelovd31ef502011-10-26 14:41:36 +02001303 return true;
1304
1305 return false;
1306}
1307
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001308static int is_battery_charging(int fsm_state)
1309{
Amir Samuelovd31ef502011-10-26 14:41:36 +02001310 if (is_ext_charging(the_chip))
1311 return 1;
1312
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001313 switch (fsm_state) {
1314 case FSM_STATE_ATC_2A:
1315 case FSM_STATE_ATC_2B:
1316 case FSM_STATE_ON_CHG_AND_BAT_6:
1317 case FSM_STATE_FAST_CHG_7:
1318 case FSM_STATE_TRKL_CHG_8:
1319 return 1;
1320 }
1321 return 0;
1322}
1323
1324static void bms_notify(struct work_struct *work)
1325{
1326 struct bms_notify *n = container_of(work, struct bms_notify, work);
1327
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001328 if (n->is_charging) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329 pm8921_bms_charging_began();
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07001330 } else {
1331 pm8921_bms_charging_end(n->is_battery_full);
1332 n->is_battery_full = 0;
1333 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001334}
1335
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07001336static void bms_notify_check(struct pm8921_chg_chip *chip)
1337{
1338 int fsm_state, new_is_charging;
1339
1340 fsm_state = pm_chg_get_fsm_state(chip);
1341 new_is_charging = is_battery_charging(fsm_state);
1342
1343 if (chip->bms_notify.is_charging ^ new_is_charging) {
1344 chip->bms_notify.is_charging = new_is_charging;
1345 schedule_work(&(chip->bms_notify.work));
1346 }
1347}
1348
Abhijeet Dharmapurikar9da392d2012-02-22 18:59:57 -08001349static enum power_supply_property pm_power_props_usb[] = {
1350 POWER_SUPPLY_PROP_PRESENT,
1351 POWER_SUPPLY_PROP_ONLINE,
1352 POWER_SUPPLY_PROP_CURRENT_MAX,
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001353 POWER_SUPPLY_PROP_SCOPE,
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08001354 POWER_SUPPLY_PROP_HEALTH,
Abhijeet Dharmapurikar9da392d2012-02-22 18:59:57 -08001355};
1356
1357static enum power_supply_property pm_power_props_mains[] = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001358 POWER_SUPPLY_PROP_PRESENT,
Willie Ruan9ad6d832011-08-19 11:58:42 -07001359 POWER_SUPPLY_PROP_ONLINE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001360};
1361
1362static char *pm_power_supplied_to[] = {
1363 "battery",
1364};
1365
David Keitel6ed71a52012-01-30 12:42:18 -08001366#define USB_WALL_THRESHOLD_MA 500
Abhijeet Dharmapurikar9da392d2012-02-22 18:59:57 -08001367static int pm_power_get_property_mains(struct power_supply *psy,
1368 enum power_supply_property psp,
1369 union power_supply_propval *val)
1370{
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05301371 int type;
1372
Abhijeet Dharmapurikar9da392d2012-02-22 18:59:57 -08001373 /* Check if called before init */
1374 if (!the_chip)
1375 return -EINVAL;
1376
1377 switch (psp) {
1378 case POWER_SUPPLY_PROP_PRESENT:
1379 case POWER_SUPPLY_PROP_ONLINE:
1380 val->intval = 0;
David Keitela3cf9432012-08-24 15:11:23 -07001381
1382 if (the_chip->has_dc_supply) {
1383 val->intval = 1;
1384 return 0;
1385 }
1386
Abhijeet Dharmapurikar9da392d2012-02-22 18:59:57 -08001387 if (the_chip->dc_present) {
1388 val->intval = 1;
1389 return 0;
1390 }
1391
Anirudh Ghayal1e5bb372013-02-20 09:48:42 +05301392 type = the_chip->usb_type;
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05301393 if (type == POWER_SUPPLY_TYPE_USB_DCP ||
1394 type == POWER_SUPPLY_TYPE_USB_ACA ||
1395 type == POWER_SUPPLY_TYPE_USB_CDP)
Abhijeet Dharmapurikar9da392d2012-02-22 18:59:57 -08001396 val->intval = is_usb_chg_plugged_in(the_chip);
Abhijeet Dharmapurikar9da392d2012-02-22 18:59:57 -08001397
1398 break;
1399 default:
1400 return -EINVAL;
1401 }
1402 return 0;
1403}
1404
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08001405static int disable_aicl(int disable)
1406{
1407 if (disable != POWER_SUPPLY_HEALTH_UNKNOWN
1408 && disable != POWER_SUPPLY_HEALTH_GOOD) {
1409 pr_err("called with invalid param :%d\n", disable);
1410 return -EINVAL;
1411 }
1412
1413 if (!the_chip) {
1414 pr_err("%s called before init\n", __func__);
1415 return -EINVAL;
1416 }
1417
1418 pr_debug("Disable AICL = %d\n", disable);
1419 the_chip->disable_aicl = disable;
1420 return 0;
1421}
1422
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001423static int switch_usb_to_charge_mode(struct pm8921_chg_chip *chip)
1424{
1425 int rc;
1426
1427 if (!chip->host_mode)
1428 return 0;
1429
1430 /* enable usbin valid comparator and remove force usb ovp fet off */
David Keiteladf790e2012-11-28 12:56:55 -08001431 rc = pm_chg_write(chip, USB_OVP_TEST, 0xB2);
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001432 if (rc < 0) {
1433 pr_err("Failed to write 0xB2 to USB_OVP_TEST rc = %d\n", rc);
1434 return rc;
1435 }
1436
1437 chip->host_mode = 0;
1438
1439 return 0;
1440}
1441
1442static int switch_usb_to_host_mode(struct pm8921_chg_chip *chip)
1443{
1444 int rc;
1445
1446 if (chip->host_mode)
1447 return 0;
1448
1449 /* disable usbin valid comparator and force usb ovp fet off */
David Keiteladf790e2012-11-28 12:56:55 -08001450 rc = pm_chg_write(chip, USB_OVP_TEST, 0xB3);
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001451 if (rc < 0) {
1452 pr_err("Failed to write 0xB3 to USB_OVP_TEST rc = %d\n", rc);
1453 return rc;
1454 }
1455
1456 chip->host_mode = 1;
1457
1458 return 0;
1459}
1460
1461static int pm_power_set_property_usb(struct power_supply *psy,
1462 enum power_supply_property psp,
1463 const union power_supply_propval *val)
1464{
1465 /* Check if called before init */
1466 if (!the_chip)
1467 return -EINVAL;
1468
1469 switch (psp) {
1470 case POWER_SUPPLY_PROP_SCOPE:
1471 if (val->intval == POWER_SUPPLY_SCOPE_SYSTEM)
1472 return switch_usb_to_host_mode(the_chip);
1473 if (val->intval == POWER_SUPPLY_SCOPE_DEVICE)
1474 return switch_usb_to_charge_mode(the_chip);
1475 else
1476 return -EINVAL;
1477 break;
David Keitel0e651472012-06-06 14:44:38 -07001478 case POWER_SUPPLY_PROP_TYPE:
1479 return pm8921_set_usb_power_supply_type(val->intval);
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08001480 case POWER_SUPPLY_PROP_HEALTH:
1481 /* UNKNOWN(0) means enable aicl, GOOD(1) means disable aicl */
1482 return disable_aicl(val->intval);
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001483 default:
1484 return -EINVAL;
1485 }
1486 return 0;
1487}
1488
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08001489static int usb_property_is_writeable(struct power_supply *psy,
1490 enum power_supply_property psp)
1491{
1492 switch (psp) {
1493 case POWER_SUPPLY_PROP_HEALTH:
1494 return 1;
1495 default:
1496 break;
1497 }
1498
1499 return 0;
1500}
1501
Abhijeet Dharmapurikar9da392d2012-02-22 18:59:57 -08001502static int pm_power_get_property_usb(struct power_supply *psy,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001503 enum power_supply_property psp,
1504 union power_supply_propval *val)
1505{
David Keitel6ed71a52012-01-30 12:42:18 -08001506 int current_max;
1507
1508 /* Check if called before init */
1509 if (!the_chip)
1510 return -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001511
1512 switch (psp) {
David Keitel6ed71a52012-01-30 12:42:18 -08001513 case POWER_SUPPLY_PROP_CURRENT_MAX:
Abhijeet Dharmapurikar9da392d2012-02-22 18:59:57 -08001514 if (pm_is_chg_charge_dis(the_chip)) {
1515 val->intval = 0;
1516 } else {
1517 pm_chg_iusbmax_get(the_chip, &current_max);
1518 val->intval = current_max;
1519 }
David Keitel6ed71a52012-01-30 12:42:18 -08001520 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001521 case POWER_SUPPLY_PROP_PRESENT:
Willie Ruan9ad6d832011-08-19 11:58:42 -07001522 case POWER_SUPPLY_PROP_ONLINE:
David Keitel63f71662012-02-08 20:30:00 -08001523 val->intval = 0;
Abhijeet Dharmapurikar9da392d2012-02-22 18:59:57 -08001524
Anirudh Ghayal1e5bb372013-02-20 09:48:42 +05301525 if (the_chip->usb_type == POWER_SUPPLY_TYPE_USB)
David Keitel86034022012-04-18 12:33:58 -07001526 val->intval = is_usb_chg_plugged_in(the_chip);
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05301527
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001528 break;
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001529
1530 case POWER_SUPPLY_PROP_SCOPE:
1531 if (the_chip->host_mode)
1532 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
1533 else
1534 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1535 break;
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08001536 case POWER_SUPPLY_PROP_HEALTH:
1537 /* UNKNOWN(0) means enable aicl, GOOD(1) means disable aicl */
1538 val->intval = the_chip->disable_aicl;
1539 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001540 default:
1541 return -EINVAL;
1542 }
1543 return 0;
1544}
1545
1546static enum power_supply_property msm_batt_power_props[] = {
1547 POWER_SUPPLY_PROP_STATUS,
1548 POWER_SUPPLY_PROP_CHARGE_TYPE,
1549 POWER_SUPPLY_PROP_HEALTH,
1550 POWER_SUPPLY_PROP_PRESENT,
1551 POWER_SUPPLY_PROP_TECHNOLOGY,
1552 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
1553 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
1554 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1555 POWER_SUPPLY_PROP_CAPACITY,
Binqiang Qiu2e1819e2012-08-15 17:37:37 -07001556 POWER_SUPPLY_PROP_CURRENT_MAX,
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001557 POWER_SUPPLY_PROP_CURRENT_NOW,
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001558 POWER_SUPPLY_PROP_TEMP,
David Keitel5e0d3a62012-11-26 10:55:11 -08001559 POWER_SUPPLY_PROP_CHARGE_FULL,
Abhijeet Dharmapurikar4c78f462012-10-16 15:43:55 -07001560 POWER_SUPPLY_PROP_CHARGE_NOW,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001561};
1562
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001563static int get_prop_battery_uvolts(struct pm8921_chg_chip *chip)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001564{
1565 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001566 struct pm8xxx_adc_chan_result result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001567
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001568 rc = pm8xxx_adc_read(chip->vbat_channel, &result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001569 if (rc) {
1570 pr_err("error reading adc channel = %d, rc = %d\n",
1571 chip->vbat_channel, rc);
1572 return rc;
1573 }
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07001574 pr_debug("mvolts phy = %lld meas = 0x%llx\n", result.physical,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001575 result.measurement);
1576 return (int)result.physical;
1577}
1578
Abhijeet Dharmapurikar4ffea2c2011-11-11 18:25:02 -08001579static unsigned int voltage_based_capacity(struct pm8921_chg_chip *chip)
1580{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001581 unsigned int current_voltage_uv = get_prop_battery_uvolts(chip);
1582 unsigned int current_voltage_mv = current_voltage_uv / 1000;
1583 unsigned int low_voltage = chip->min_voltage_mv;
1584 unsigned int high_voltage = chip->max_voltage_mv;
Abhijeet Dharmapurikar4ffea2c2011-11-11 18:25:02 -08001585
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001586 if (current_voltage_mv <= low_voltage)
Abhijeet Dharmapurikar4ffea2c2011-11-11 18:25:02 -08001587 return 0;
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001588 else if (current_voltage_mv >= high_voltage)
Abhijeet Dharmapurikar4ffea2c2011-11-11 18:25:02 -08001589 return 100;
1590 else
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001591 return (current_voltage_mv - low_voltage) * 100
Abhijeet Dharmapurikar4ffea2c2011-11-11 18:25:02 -08001592 / (high_voltage - low_voltage);
1593}
1594
David Keitel4f9397b2012-04-16 11:46:16 -07001595static int get_prop_batt_present(struct pm8921_chg_chip *chip)
1596{
1597 return pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ);
1598}
1599
Abhijeet Dharmapurikar035a0582012-10-26 16:54:12 -07001600static int get_prop_batt_status(struct pm8921_chg_chip *chip)
1601{
1602 int batt_state = POWER_SUPPLY_STATUS_DISCHARGING;
1603 int fsm_state = pm_chg_get_fsm_state(chip);
1604 int i;
1605
1606 if (chip->ext_psy) {
1607 if (chip->ext_charge_done)
1608 return POWER_SUPPLY_STATUS_FULL;
1609 if (chip->ext_charging)
1610 return POWER_SUPPLY_STATUS_CHARGING;
1611 }
1612
1613 for (i = 0; i < ARRAY_SIZE(map); i++)
1614 if (map[i].fsm_state == fsm_state)
1615 batt_state = map[i].batt_state;
1616
1617 if (fsm_state == FSM_STATE_ON_CHG_HIGHI_1) {
1618 if (!pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ)
1619 || !pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ)
1620 || pm_chg_get_rt_status(chip, CHGHOT_IRQ)
1621 || pm_chg_get_rt_status(chip, VBATDET_LOW_IRQ))
1622
1623 batt_state = POWER_SUPPLY_STATUS_NOT_CHARGING;
1624 }
1625 return batt_state;
1626}
1627
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001628static int get_prop_batt_capacity(struct pm8921_chg_chip *chip)
1629{
David Keitel4f9397b2012-04-16 11:46:16 -07001630 int percent_soc;
1631
Abhijeet Dharmapurikarbbaef592012-10-10 10:07:51 -07001632 if (chip->battery_less_hardware)
1633 return 100;
1634
David Keitel4f9397b2012-04-16 11:46:16 -07001635 if (!get_prop_batt_present(chip))
1636 percent_soc = voltage_based_capacity(chip);
1637 else
1638 percent_soc = pm8921_bms_get_percent_charge();
Abhijeet Dharmapurikar5a7df4e2011-07-27 13:16:24 -07001639
Abhijeet Dharmapurikar4ffea2c2011-11-11 18:25:02 -08001640 if (percent_soc == -ENXIO)
1641 percent_soc = voltage_based_capacity(chip);
1642
Abhijeet Dharmapurikar5a7df4e2011-07-27 13:16:24 -07001643 if (percent_soc <= 10)
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -07001644 pr_warn_ratelimited("low battery charge = %d%%\n",
1645 percent_soc);
1646
Abhijeet Dharmapurikar035a0582012-10-26 16:54:12 -07001647 if (percent_soc <= chip->resume_charge_percent
1648 && get_prop_batt_status(chip) == POWER_SUPPLY_STATUS_FULL) {
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -07001649 pr_debug("soc fell below %d. charging enabled.\n",
1650 chip->resume_charge_percent);
1651 if (chip->is_bat_warm)
1652 pr_warn_ratelimited("battery is warm = %d, do not resume charging at %d%%.\n",
1653 chip->is_bat_warm,
1654 chip->resume_charge_percent);
1655 else if (chip->is_bat_cool)
1656 pr_warn_ratelimited("battery is cool = %d, do not resume charging at %d%%.\n",
1657 chip->is_bat_cool,
1658 chip->resume_charge_percent);
1659 else
1660 pm_chg_vbatdet_set(the_chip, PM8921_CHG_VBATDET_MAX);
1661 }
Abhijeet Dharmapurikar5a7df4e2011-07-27 13:16:24 -07001662
Abhijeet Dharmapurikarde9c7e72012-07-15 16:29:51 -07001663 chip->recent_reported_soc = percent_soc;
Abhijeet Dharmapurikar5a7df4e2011-07-27 13:16:24 -07001664 return percent_soc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001665}
1666
Binqiang Qiu2e1819e2012-08-15 17:37:37 -07001667static int get_prop_batt_current_max(struct pm8921_chg_chip *chip)
1668{
Abhijeet Dharmapurikareca7b3e2012-12-26 21:30:29 -08001669 return pm8921_bms_get_current_max();
Binqiang Qiu2e1819e2012-08-15 17:37:37 -07001670}
1671
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001672static int get_prop_batt_current(struct pm8921_chg_chip *chip)
1673{
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001674 int result_ua, rc;
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001675
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001676 rc = pm8921_bms_get_battery_current(&result_ua);
Abhijeet Dharmapurikar4ffea2c2011-11-11 18:25:02 -08001677 if (rc == -ENXIO) {
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001678 rc = pm8xxx_ccadc_get_battery_current(&result_ua);
Abhijeet Dharmapurikar4ffea2c2011-11-11 18:25:02 -08001679 }
1680
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001681 if (rc) {
1682 pr_err("unable to get batt current rc = %d\n", rc);
1683 return rc;
1684 } else {
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001685 return result_ua;
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001686 }
1687}
1688
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001689static int get_prop_batt_fcc(struct pm8921_chg_chip *chip)
1690{
1691 int rc;
1692
1693 rc = pm8921_bms_get_fcc();
1694 if (rc < 0)
1695 pr_err("unable to get batt fcc rc = %d\n", rc);
1696 return rc;
1697}
1698
Abhijeet Dharmapurikar4c78f462012-10-16 15:43:55 -07001699static int get_prop_batt_charge_now(struct pm8921_chg_chip *chip)
1700{
1701 int rc;
1702 int cc_uah;
1703
1704 rc = pm8921_bms_cc_uah(&cc_uah);
1705
1706 if (rc == 0)
1707 return cc_uah;
1708
1709 pr_err("unable to get batt fcc rc = %d\n", rc);
1710 return rc;
1711}
1712
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713static int get_prop_batt_health(struct pm8921_chg_chip *chip)
1714{
1715 int temp;
1716
1717 temp = pm_chg_get_rt_status(chip, BATTTEMP_HOT_IRQ);
1718 if (temp)
1719 return POWER_SUPPLY_HEALTH_OVERHEAT;
1720
1721 temp = pm_chg_get_rt_status(chip, BATTTEMP_COLD_IRQ);
1722 if (temp)
1723 return POWER_SUPPLY_HEALTH_COLD;
1724
1725 return POWER_SUPPLY_HEALTH_GOOD;
1726}
1727
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001728static int get_prop_charge_type(struct pm8921_chg_chip *chip)
1729{
1730 int temp;
1731
Amir Samuelovd31ef502011-10-26 14:41:36 +02001732 if (!get_prop_batt_present(chip))
1733 return POWER_SUPPLY_CHARGE_TYPE_NONE;
1734
1735 if (is_ext_trickle_charging(chip))
1736 return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1737
1738 if (is_ext_charging(chip))
1739 return POWER_SUPPLY_CHARGE_TYPE_FAST;
1740
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001741 temp = pm_chg_get_rt_status(chip, TRKLCHG_IRQ);
1742 if (temp)
1743 return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1744
1745 temp = pm_chg_get_rt_status(chip, FASTCHG_IRQ);
1746 if (temp)
1747 return POWER_SUPPLY_CHARGE_TYPE_FAST;
1748
1749 return POWER_SUPPLY_CHARGE_TYPE_NONE;
1750}
1751
Abhijeet Dharmapurikar728bc4d2012-01-03 11:08:33 -08001752#define MAX_TOLERABLE_BATT_TEMP_DDC 680
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001753static int get_prop_batt_temp(struct pm8921_chg_chip *chip)
1754{
1755 int rc;
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001756 struct pm8xxx_adc_chan_result result;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001757
Abhijeet Dharmapurikarbbaef592012-10-10 10:07:51 -07001758 if (chip->battery_less_hardware)
1759 return 300;
1760
Siddartha Mohanadossaf91d902011-10-20 10:23:34 -07001761 rc = pm8xxx_adc_read(chip->batt_temp_channel, &result);
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001762 if (rc) {
1763 pr_err("error reading adc channel = %d, rc = %d\n",
1764 chip->vbat_channel, rc);
1765 return rc;
1766 }
1767 pr_debug("batt_temp phy = %lld meas = 0x%llx\n", result.physical,
1768 result.measurement);
Abhijeet Dharmapurikar728bc4d2012-01-03 11:08:33 -08001769 if (result.physical > MAX_TOLERABLE_BATT_TEMP_DDC)
1770 pr_err("BATT_TEMP= %d > 68degC, device will be shutdown\n",
1771 (int) result.physical);
1772
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001773 return (int)result.physical;
1774}
1775
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776static int pm_batt_power_get_property(struct power_supply *psy,
1777 enum power_supply_property psp,
1778 union power_supply_propval *val)
1779{
1780 struct pm8921_chg_chip *chip = container_of(psy, struct pm8921_chg_chip,
1781 batt_psy);
1782
1783 switch (psp) {
1784 case POWER_SUPPLY_PROP_STATUS:
1785 val->intval = get_prop_batt_status(chip);
1786 break;
1787 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1788 val->intval = get_prop_charge_type(chip);
1789 break;
1790 case POWER_SUPPLY_PROP_HEALTH:
1791 val->intval = get_prop_batt_health(chip);
1792 break;
1793 case POWER_SUPPLY_PROP_PRESENT:
1794 val->intval = get_prop_batt_present(chip);
1795 break;
1796 case POWER_SUPPLY_PROP_TECHNOLOGY:
1797 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
1798 break;
1799 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001800 val->intval = chip->max_voltage_mv * 1000;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001801 break;
1802 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001803 val->intval = chip->min_voltage_mv * 1000;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001804 break;
1805 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08001806 val->intval = get_prop_battery_uvolts(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001807 break;
1808 case POWER_SUPPLY_PROP_CAPACITY:
1809 val->intval = get_prop_batt_capacity(chip);
1810 break;
Abhijeet Dharmapurikare0c3e5f2011-07-11 21:17:23 -07001811 case POWER_SUPPLY_PROP_CURRENT_NOW:
1812 val->intval = get_prop_batt_current(chip);
1813 break;
Binqiang Qiu2e1819e2012-08-15 17:37:37 -07001814 case POWER_SUPPLY_PROP_CURRENT_MAX:
1815 val->intval = get_prop_batt_current_max(chip);
1816 break;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001817 case POWER_SUPPLY_PROP_TEMP:
1818 val->intval = get_prop_batt_temp(chip);
1819 break;
David Keitel5e0d3a62012-11-26 10:55:11 -08001820 case POWER_SUPPLY_PROP_CHARGE_FULL:
1821 val->intval = get_prop_batt_fcc(chip);
Abhijeet Dharmapurikar325bdc92011-09-07 20:45:49 -07001822 break;
Abhijeet Dharmapurikar4c78f462012-10-16 15:43:55 -07001823 case POWER_SUPPLY_PROP_CHARGE_NOW:
1824 val->intval = get_prop_batt_charge_now(chip);
1825 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001826 default:
1827 return -EINVAL;
1828 }
1829
1830 return 0;
1831}
1832
1833static void (*notify_vbus_state_func_ptr)(int);
1834static int usb_chg_current;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001835
1836int pm8921_charger_register_vbus_sn(void (*callback)(int))
1837{
1838 pr_debug("%p\n", callback);
1839 notify_vbus_state_func_ptr = callback;
1840 return 0;
1841}
1842EXPORT_SYMBOL_GPL(pm8921_charger_register_vbus_sn);
1843
1844/* this is passed to the hsusb via platform_data msm_otg_pdata */
1845void pm8921_charger_unregister_vbus_sn(void (*callback)(int))
1846{
1847 pr_debug("%p\n", callback);
1848 notify_vbus_state_func_ptr = NULL;
1849}
1850EXPORT_SYMBOL_GPL(pm8921_charger_unregister_vbus_sn);
1851
1852static void notify_usb_of_the_plugin_event(int plugin)
1853{
1854 plugin = !!plugin;
1855 if (notify_vbus_state_func_ptr) {
1856 pr_debug("notifying plugin\n");
1857 (*notify_vbus_state_func_ptr) (plugin);
1858 } else {
1859 pr_debug("unable to notify plugin\n");
1860 }
1861}
1862
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001863static void __pm8921_charger_vbus_draw(unsigned int mA)
1864{
1865 int i, rc;
David Keitel38bdd4f2012-04-19 15:39:13 -07001866 if (!the_chip) {
1867 pr_err("called before init\n");
1868 return;
1869 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001870
David Keitel8e672752012-11-02 16:00:41 -07001871 if (usb_max_current && mA > usb_max_current) {
1872 pr_debug("restricting usb current to %d instead of %d\n",
1873 usb_max_current, mA);
1874 mA = usb_max_current;
1875 }
1876
Willie Ruan0db41d42012-11-15 13:49:22 -08001877 if (mA <= 2) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001878 usb_chg_current = 0;
David Keitelacf57c82012-03-07 18:48:50 -08001879 rc = pm_chg_iusbmax_set(the_chip, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001880 if (rc) {
David Keitelacf57c82012-03-07 18:48:50 -08001881 pr_err("unable to set iusb to %d rc = %d\n", 0, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001882 }
1883 rc = pm_chg_usb_suspend_enable(the_chip, 1);
1884 if (rc)
1885 pr_err("fail to set suspend bit rc=%d\n", rc);
1886 } else {
1887 rc = pm_chg_usb_suspend_enable(the_chip, 0);
1888 if (rc)
1889 pr_err("fail to reset suspend bit rc=%d\n", rc);
1890 for (i = ARRAY_SIZE(usb_ma_table) - 1; i >= 0; i--) {
1891 if (usb_ma_table[i].usb_ma <= mA)
1892 break;
1893 }
David Keitel38bdd4f2012-04-19 15:39:13 -07001894
Willie Ruan69566002012-11-15 11:56:36 -08001895 if (i < 0) {
1896 pr_err("can't find %dmA in usb_ma_table. Use min.\n",
1897 mA);
1898 i = 0;
1899 }
1900
David Keitel38bdd4f2012-04-19 15:39:13 -07001901 /* Check if IUSB_FINE_RES is available */
David Keitel67ebde02012-10-01 11:12:59 -07001902 while ((usb_ma_table[i].value & PM8917_IUSB_FINE_RES)
David Keitel38bdd4f2012-04-19 15:39:13 -07001903 && !the_chip->iusb_fine_res)
1904 i--;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001905 if (i < 0)
1906 i = 0;
David Keitelacf57c82012-03-07 18:48:50 -08001907 rc = pm_chg_iusbmax_set(the_chip, i);
David Keitel1e3a82f2012-06-26 16:27:15 -07001908 if (rc)
David Keitelacf57c82012-03-07 18:48:50 -08001909 pr_err("unable to set iusb to %d rc = %d\n", i, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001910 }
1911}
1912
1913/* USB calls these to tell us how much max usb current the system can draw */
1914void pm8921_charger_vbus_draw(unsigned int mA)
1915{
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08001916 int set_usb_now_ma;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001917
1918 pr_debug("Enter charge=%d\n", mA);
David Keitelacf57c82012-03-07 18:48:50 -08001919
David Keitel62c6a4b2012-05-17 12:54:59 -07001920 /*
1921 * Reject VBUS requests if USB connection is the only available
1922 * power source. This makes sure that if booting without
1923 * battery the iusb_max value is not decreased avoiding potential
1924 * brown_outs.
1925 *
1926 * This would also apply when the battery has been
1927 * removed from the running system.
1928 */
Abhijeet Dharmapurikar76016a92013-02-20 18:40:59 -08001929 if (mA == 0 && the_chip && !get_prop_batt_present(the_chip)
David Keitel62c6a4b2012-05-17 12:54:59 -07001930 && !is_dc_chg_plugged_in(the_chip)) {
David Keitela3cf9432012-08-24 15:11:23 -07001931 if (!the_chip->has_dc_supply) {
Abhijeet Dharmapurikar76016a92013-02-20 18:40:59 -08001932 pr_err("rejected: no other power source mA = %d\n", mA);
David Keitelacb258c2012-07-13 17:12:04 -07001933 return;
1934 }
David Keitel62c6a4b2012-05-17 12:54:59 -07001935 }
1936
David Keitelacf57c82012-03-07 18:48:50 -08001937 if (usb_max_current && mA > usb_max_current) {
1938 pr_warn("restricting usb current to %d instead of %d\n",
1939 usb_max_current, mA);
1940 mA = usb_max_current;
1941 }
1942 if (usb_target_ma == 0 && mA > USB_WALL_THRESHOLD_MA)
1943 usb_target_ma = mA;
1944
Anirudh Ghayal037cfc82013-01-23 16:43:31 +05301945 if (usb_target_ma)
1946 usb_target_ma = mA;
1947
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08001948
1949 if (mA > USB_WALL_THRESHOLD_MA)
1950 set_usb_now_ma = USB_WALL_THRESHOLD_MA;
1951 else
1952 set_usb_now_ma = mA;
1953
1954 if (the_chip && the_chip->disable_aicl)
1955 set_usb_now_ma = mA;
1956
1957 if (the_chip)
1958 __pm8921_charger_vbus_draw(set_usb_now_ma);
1959 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001960 /*
1961 * called before pmic initialized,
1962 * save this value and use it at probe
1963 */
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08001964 usb_chg_current = set_usb_now_ma;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001965}
1966EXPORT_SYMBOL_GPL(pm8921_charger_vbus_draw);
1967
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07001968int pm8921_is_usb_chg_plugged_in(void)
1969{
1970 if (!the_chip) {
1971 pr_err("called before init\n");
1972 return -EINVAL;
1973 }
1974 return is_usb_chg_plugged_in(the_chip);
1975}
1976EXPORT_SYMBOL(pm8921_is_usb_chg_plugged_in);
1977
1978int pm8921_is_dc_chg_plugged_in(void)
1979{
1980 if (!the_chip) {
1981 pr_err("called before init\n");
1982 return -EINVAL;
1983 }
1984 return is_dc_chg_plugged_in(the_chip);
1985}
1986EXPORT_SYMBOL(pm8921_is_dc_chg_plugged_in);
1987
1988int pm8921_is_battery_present(void)
1989{
1990 if (!the_chip) {
1991 pr_err("called before init\n");
1992 return -EINVAL;
1993 }
1994 return get_prop_batt_present(the_chip);
1995}
1996EXPORT_SYMBOL(pm8921_is_battery_present);
1997
Abhijeet Dharmapurikarb2680132012-05-15 16:32:51 -07001998int pm8921_is_batfet_closed(void)
1999{
2000 if (!the_chip) {
2001 pr_err("called before init\n");
2002 return -EINVAL;
2003 }
2004 return is_batfet_closed(the_chip);
2005}
2006EXPORT_SYMBOL(pm8921_is_batfet_closed);
David Keitel012deef2011-12-02 11:49:33 -08002007/*
2008 * Disabling the charge current limit causes current
2009 * current limits to have no monitoring. An adequate charger
2010 * capable of supplying high current while sustaining VIN_MIN
2011 * is required if the limiting is disabled.
2012 */
2013int pm8921_disable_input_current_limit(bool disable)
2014{
2015 if (!the_chip) {
2016 pr_err("called before init\n");
2017 return -EINVAL;
2018 }
2019 if (disable) {
2020 pr_warn("Disabling input current limit!\n");
2021
David Keitel663abad2012-09-04 21:00:27 -07002022 return pm_chg_write(the_chip, CHG_BUCK_CTRL_TEST3, 0xF2);
David Keitel012deef2011-12-02 11:49:33 -08002023 }
2024 return 0;
2025}
2026EXPORT_SYMBOL(pm8921_disable_input_current_limit);
2027
David Keitel0789fc62012-06-07 17:43:27 -07002028int pm8917_set_under_voltage_detection_threshold(int mv)
2029{
2030 if (!the_chip) {
2031 pr_err("called before init\n");
2032 return -EINVAL;
2033 }
2034 return pm_chg_uvd_threshold_set(the_chip, mv);
2035}
2036EXPORT_SYMBOL(pm8917_set_under_voltage_detection_threshold);
2037
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002038int pm8921_set_max_battery_charge_current(int ma)
2039{
2040 if (!the_chip) {
2041 pr_err("called before init\n");
2042 return -EINVAL;
2043 }
2044 return pm_chg_ibatmax_set(the_chip, ma);
2045}
2046EXPORT_SYMBOL(pm8921_set_max_battery_charge_current);
2047
2048int pm8921_disable_source_current(bool disable)
2049{
2050 if (!the_chip) {
2051 pr_err("called before init\n");
2052 return -EINVAL;
2053 }
2054 if (disable)
2055 pr_warn("current drawn from chg=0, battery provides current\n");
David Keiteld8878592012-09-04 09:33:28 -07002056
2057 pm_chg_usb_suspend_enable(the_chip, disable);
2058
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002059 return pm_chg_charge_dis(the_chip, disable);
2060}
2061EXPORT_SYMBOL(pm8921_disable_source_current);
2062
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -07002063int pm8921_regulate_input_voltage(int voltage)
2064{
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002065 int rc;
2066
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -07002067 if (!the_chip) {
2068 pr_err("called before init\n");
2069 return -EINVAL;
2070 }
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002071 rc = pm_chg_vinmin_set(the_chip, voltage);
2072
2073 if (rc == 0)
2074 the_chip->vin_min = voltage;
2075
2076 return rc;
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -07002077}
2078
Abhijeet Dharmapurikar28669302011-12-21 08:31:28 -08002079#define USB_OV_THRESHOLD_MASK 0x60
2080#define USB_OV_THRESHOLD_SHIFT 5
2081int pm8921_usb_ovp_set_threshold(enum pm8921_usb_ov_threshold ov)
2082{
2083 u8 temp;
2084
2085 if (!the_chip) {
2086 pr_err("called before init\n");
2087 return -EINVAL;
2088 }
2089
2090 if (ov > PM_USB_OV_7V) {
2091 pr_err("limiting to over voltage threshold to 7volts\n");
2092 ov = PM_USB_OV_7V;
2093 }
2094
2095 temp = USB_OV_THRESHOLD_MASK & (ov << USB_OV_THRESHOLD_SHIFT);
2096
2097 return pm_chg_masked_write(the_chip, USB_OVP_CONTROL,
2098 USB_OV_THRESHOLD_MASK, temp);
2099}
2100EXPORT_SYMBOL(pm8921_usb_ovp_set_threshold);
2101
2102#define USB_DEBOUNCE_TIME_MASK 0x06
2103#define USB_DEBOUNCE_TIME_SHIFT 1
2104int pm8921_usb_ovp_set_hystersis(enum pm8921_usb_debounce_time ms)
2105{
2106 u8 temp;
2107
2108 if (!the_chip) {
2109 pr_err("called before init\n");
2110 return -EINVAL;
2111 }
2112
2113 if (ms > PM_USB_DEBOUNCE_80P5MS) {
2114 pr_err("limiting debounce to 80.5ms\n");
2115 ms = PM_USB_DEBOUNCE_80P5MS;
2116 }
2117
2118 temp = USB_DEBOUNCE_TIME_MASK & (ms << USB_DEBOUNCE_TIME_SHIFT);
2119
2120 return pm_chg_masked_write(the_chip, USB_OVP_CONTROL,
2121 USB_DEBOUNCE_TIME_MASK, temp);
2122}
2123EXPORT_SYMBOL(pm8921_usb_ovp_set_hystersis);
2124
2125#define USB_OVP_DISABLE_MASK 0x80
2126int pm8921_usb_ovp_disable(int disable)
2127{
2128 u8 temp = 0;
2129
2130 if (!the_chip) {
2131 pr_err("called before init\n");
2132 return -EINVAL;
2133 }
2134
2135 if (disable)
2136 temp = USB_OVP_DISABLE_MASK;
2137
2138 return pm_chg_masked_write(the_chip, USB_OVP_CONTROL,
2139 USB_OVP_DISABLE_MASK, temp);
2140}
2141
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002142bool pm8921_is_battery_charging(int *source)
2143{
2144 int fsm_state, is_charging, dc_present, usb_present;
2145
2146 if (!the_chip) {
2147 pr_err("called before init\n");
2148 return -EINVAL;
2149 }
2150 fsm_state = pm_chg_get_fsm_state(the_chip);
2151 is_charging = is_battery_charging(fsm_state);
2152 if (is_charging == 0) {
2153 *source = PM8921_CHG_SRC_NONE;
2154 return is_charging;
2155 }
2156
2157 if (source == NULL)
2158 return is_charging;
2159
2160 /* the battery is charging, the source is requested, find it */
2161 dc_present = is_dc_chg_plugged_in(the_chip);
2162 usb_present = is_usb_chg_plugged_in(the_chip);
2163
2164 if (dc_present && !usb_present)
2165 *source = PM8921_CHG_SRC_DC;
2166
2167 if (usb_present && !dc_present)
2168 *source = PM8921_CHG_SRC_USB;
2169
2170 if (usb_present && dc_present)
2171 /*
2172 * The system always chooses dc for charging since it has
2173 * higher priority.
2174 */
2175 *source = PM8921_CHG_SRC_DC;
2176
2177 return is_charging;
2178}
2179EXPORT_SYMBOL(pm8921_is_battery_charging);
2180
David Keitel86034022012-04-18 12:33:58 -07002181int pm8921_set_usb_power_supply_type(enum power_supply_type type)
2182{
2183 if (!the_chip) {
2184 pr_err("called before init\n");
2185 return -EINVAL;
2186 }
2187
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05302188 if (type < POWER_SUPPLY_TYPE_USB && type > POWER_SUPPLY_TYPE_BATTERY)
David Keitel86034022012-04-18 12:33:58 -07002189 return -EINVAL;
2190
Anirudh Ghayal1e5bb372013-02-20 09:48:42 +05302191 the_chip->usb_type = type;
David Keitel86034022012-04-18 12:33:58 -07002192 power_supply_changed(&the_chip->usb_psy);
2193 power_supply_changed(&the_chip->dc_psy);
2194 return 0;
2195}
2196EXPORT_SYMBOL_GPL(pm8921_set_usb_power_supply_type);
2197
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07002198int pm8921_batt_temperature(void)
2199{
2200 if (!the_chip) {
2201 pr_err("called before init\n");
2202 return -EINVAL;
2203 }
2204 return get_prop_batt_temp(the_chip);
2205}
2206
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002207static void handle_usb_insertion_removal(struct pm8921_chg_chip *chip)
2208{
Abhijeet Dharmapurikarbba40912013-03-01 18:25:05 -08002209 int usb_present;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002210
Abhijeet Dharmapurikare451f342012-02-23 16:39:42 -08002211 pm_chg_failed_clear(chip, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002212 usb_present = is_usb_chg_plugged_in(chip);
2213 if (chip->usb_present ^ usb_present) {
2214 notify_usb_of_the_plugin_event(usb_present);
2215 chip->usb_present = usb_present;
2216 power_supply_changed(&chip->usb_psy);
Abhijeet Dharmapurikar0c4bc152011-10-27 10:22:24 -07002217 power_supply_changed(&chip->batt_psy);
Abhijeet Dharmapurikarc9ba2712012-02-23 12:42:24 -08002218 pm8921_bms_calibrate_hkadc();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002219 }
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002220 if (usb_present) {
2221 schedule_delayed_work(&chip->unplug_check_work,
Abhijeet Dharmapurikarb73001a2012-11-25 22:56:52 -08002222 msecs_to_jiffies(UNPLUG_CHECK_RAMP_MS));
David Keitel8c5201a2012-02-24 16:08:54 -08002223 pm8921_chg_enable_irq(chip, CHG_GONE_IRQ);
2224 } else {
David Keitelacf57c82012-03-07 18:48:50 -08002225 /* USB unplugged reset target current */
2226 usb_target_ma = 0;
David Keitel8c5201a2012-02-24 16:08:54 -08002227 pm8921_chg_disable_irq(chip, CHG_GONE_IRQ);
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002228 }
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07002229 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002230}
2231
Amir Samuelovd31ef502011-10-26 14:41:36 +02002232static void handle_stop_ext_chg(struct pm8921_chg_chip *chip)
2233{
David Keitel88e1b572012-01-11 11:57:14 -08002234 if (!chip->ext_psy) {
Amir Samuelovd31ef502011-10-26 14:41:36 +02002235 pr_debug("external charger not registered.\n");
2236 return;
2237 }
2238
2239 if (!chip->ext_charging) {
2240 pr_debug("already not charging.\n");
2241 return;
2242 }
2243
David Keitel88e1b572012-01-11 11:57:14 -08002244 power_supply_set_charge_type(chip->ext_psy,
2245 POWER_SUPPLY_CHARGE_TYPE_NONE);
2246 pm8921_disable_source_current(false); /* release BATFET */
David Keitel63f71662012-02-08 20:30:00 -08002247 power_supply_changed(&chip->dc_psy);
Amir Samuelovd31ef502011-10-26 14:41:36 +02002248 chip->ext_charging = false;
David Keiteld4e59ef2011-11-09 17:38:31 -08002249 chip->ext_charge_done = false;
David Keitel88e1b572012-01-11 11:57:14 -08002250 bms_notify_check(chip);
Amir Samuelov4ffc8ec2012-05-16 09:38:43 +03002251 /* Update battery charging LEDs and user space battery info */
2252 power_supply_changed(&chip->batt_psy);
Amir Samuelovd31ef502011-10-26 14:41:36 +02002253}
2254
2255static void handle_start_ext_chg(struct pm8921_chg_chip *chip)
2256{
2257 int dc_present;
2258 int batt_present;
2259 int batt_temp_ok;
Amir Samuelovd31ef502011-10-26 14:41:36 +02002260 unsigned long delay =
2261 round_jiffies_relative(msecs_to_jiffies(EOC_CHECK_PERIOD_MS));
2262
David Keitel88e1b572012-01-11 11:57:14 -08002263 if (!chip->ext_psy) {
Amir Samuelovd31ef502011-10-26 14:41:36 +02002264 pr_debug("external charger not registered.\n");
2265 return;
2266 }
2267
2268 if (chip->ext_charging) {
2269 pr_debug("already charging.\n");
2270 return;
2271 }
2272
David Keitel663abad2012-09-04 21:00:27 -07002273 dc_present = is_dc_chg_plugged_in(chip);
Amir Samuelovd31ef502011-10-26 14:41:36 +02002274 batt_present = pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ);
2275 batt_temp_ok = pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ);
Amir Samuelovd31ef502011-10-26 14:41:36 +02002276
2277 if (!dc_present) {
2278 pr_warn("%s. dc not present.\n", __func__);
2279 return;
2280 }
2281 if (!batt_present) {
2282 pr_warn("%s. battery not present.\n", __func__);
2283 return;
2284 }
2285 if (!batt_temp_ok) {
2286 pr_warn("%s. battery temperature not ok.\n", __func__);
2287 return;
2288 }
Abhijeet Dharmapurikar70ef3822012-10-29 15:55:34 -07002289
2290 /* Force BATFET=ON */
2291 pm8921_disable_source_current(true);
2292
David Keitel6ccbf132012-05-30 14:21:24 -07002293 schedule_delayed_work(&chip->unplug_check_work,
Abhijeet Dharmapurikarb73001a2012-11-25 22:56:52 -08002294 msecs_to_jiffies(UNPLUG_CHECK_RAMP_MS));
David Keitel6ccbf132012-05-30 14:21:24 -07002295
David Keitel63f71662012-02-08 20:30:00 -08002296 power_supply_set_online(chip->ext_psy, dc_present);
David Keitel88e1b572012-01-11 11:57:14 -08002297 power_supply_set_charge_type(chip->ext_psy,
2298 POWER_SUPPLY_CHARGE_TYPE_FAST);
Amir Samuelovd31ef502011-10-26 14:41:36 +02002299 chip->ext_charging = true;
2300 chip->ext_charge_done = false;
David Keitel88e1b572012-01-11 11:57:14 -08002301 bms_notify_check(chip);
Abhijeet Dharmapurikar70ef3822012-10-29 15:55:34 -07002302 /*
2303 * since we wont get a fastchg irq from external charger
2304 * use eoc worker to detect end of charging
2305 */
Amir Samuelovd31ef502011-10-26 14:41:36 +02002306 schedule_delayed_work(&chip->eoc_work, delay);
2307 wake_lock(&chip->eoc_wake_lock);
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -08002308 if (chip->btc_override)
2309 schedule_delayed_work(&chip->btc_override_work,
2310 round_jiffies_relative(msecs_to_jiffies
2311 (chip->btc_delay_ms)));
Amir Samuelov4ffc8ec2012-05-16 09:38:43 +03002312 /* Update battery charging LEDs and user space battery info */
2313 power_supply_changed(&chip->batt_psy);
Amir Samuelovd31ef502011-10-26 14:41:36 +02002314}
2315
David Keitel6ccbf132012-05-30 14:21:24 -07002316static void turn_off_ovp_fet(struct pm8921_chg_chip *chip, u16 ovptestreg)
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002317{
2318 u8 temp;
2319 int rc;
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002320
David Keiteladf790e2012-11-28 12:56:55 -08002321 rc = pm_chg_write(chip, ovptestreg, 0x30);
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002322 if (rc) {
David Keitel663abad2012-09-04 21:00:27 -07002323 pr_err("Failed to write 0x30 to ovptestreg rc = %d\n", rc);
David Keitel8c5201a2012-02-24 16:08:54 -08002324 return;
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002325 }
David Keitel6ccbf132012-05-30 14:21:24 -07002326 rc = pm8xxx_readb(chip->dev->parent, ovptestreg, &temp);
David Keitel8c5201a2012-02-24 16:08:54 -08002327 if (rc) {
David Keitel663abad2012-09-04 21:00:27 -07002328 pr_err("Failed to read from ovptestreg rc = %d\n", rc);
David Keitel8c5201a2012-02-24 16:08:54 -08002329 return;
2330 }
2331 /* set ovp fet disable bit and the write bit */
2332 temp |= 0x81;
David Keiteladf790e2012-11-28 12:56:55 -08002333 rc = pm_chg_write(chip, ovptestreg, temp);
David Keitel8c5201a2012-02-24 16:08:54 -08002334 if (rc) {
David Keitel663abad2012-09-04 21:00:27 -07002335 pr_err("Failed to write 0x%x ovptestreg rc=%d\n", temp, rc);
David Keitel8c5201a2012-02-24 16:08:54 -08002336 return;
2337 }
2338}
2339
David Keitel6ccbf132012-05-30 14:21:24 -07002340static void turn_on_ovp_fet(struct pm8921_chg_chip *chip, u16 ovptestreg)
David Keitel8c5201a2012-02-24 16:08:54 -08002341{
2342 u8 temp;
2343 int rc;
2344
David Keiteladf790e2012-11-28 12:56:55 -08002345 rc = pm_chg_write(chip, ovptestreg, 0x30);
David Keitel8c5201a2012-02-24 16:08:54 -08002346 if (rc) {
David Keitel6ccbf132012-05-30 14:21:24 -07002347 pr_err("Failed to write 0x30 to OVP_TEST rc = %d\n", rc);
David Keitel8c5201a2012-02-24 16:08:54 -08002348 return;
2349 }
David Keitel6ccbf132012-05-30 14:21:24 -07002350 rc = pm8xxx_readb(chip->dev->parent, ovptestreg, &temp);
David Keitel8c5201a2012-02-24 16:08:54 -08002351 if (rc) {
David Keitel6ccbf132012-05-30 14:21:24 -07002352 pr_err("Failed to read from OVP_TEST rc = %d\n", rc);
David Keitel8c5201a2012-02-24 16:08:54 -08002353 return;
2354 }
2355 /* unset ovp fet disable bit and set the write bit */
2356 temp &= 0xFE;
2357 temp |= 0x80;
David Keiteladf790e2012-11-28 12:56:55 -08002358 rc = pm_chg_write(chip, ovptestreg, temp);
David Keitel8c5201a2012-02-24 16:08:54 -08002359 if (rc) {
David Keitel6ccbf132012-05-30 14:21:24 -07002360 pr_err("Failed to write 0x%x to OVP_TEST rc = %d\n",
David Keitel8c5201a2012-02-24 16:08:54 -08002361 temp, rc);
2362 return;
2363 }
2364}
2365
2366static int param_open_ovp_counter = 10;
2367module_param(param_open_ovp_counter, int, 0644);
2368
David Keitel6ccbf132012-05-30 14:21:24 -07002369#define USB_ACTIVE_BIT BIT(5)
2370#define DC_ACTIVE_BIT BIT(6)
2371static int is_active_chg_plugged_in(struct pm8921_chg_chip *chip,
2372 u8 active_chg_mask)
2373{
2374 if (active_chg_mask & USB_ACTIVE_BIT)
2375 return pm_chg_get_rt_status(chip, USBIN_VALID_IRQ);
2376 else if (active_chg_mask & DC_ACTIVE_BIT)
2377 return pm_chg_get_rt_status(chip, DCIN_VALID_IRQ);
2378 else
2379 return 0;
2380}
2381
David Keitel8c5201a2012-02-24 16:08:54 -08002382#define WRITE_BANK_4 0xC0
David Keitel6ccbf132012-05-30 14:21:24 -07002383#define OVP_DEBOUNCE_TIME 0x06
David Keitelc3e15bd2012-05-25 17:07:25 -07002384static void unplug_ovp_fet_open(struct pm8921_chg_chip *chip)
David Keitel8c5201a2012-02-24 16:08:54 -08002385{
David Keitel6ccbf132012-05-30 14:21:24 -07002386 int chg_gone = 0, active_chg_plugged_in = 0;
David Keitel8c5201a2012-02-24 16:08:54 -08002387 int count = 0;
David Keitel6ccbf132012-05-30 14:21:24 -07002388 u8 active_mask = 0;
2389 u16 ovpreg, ovptestreg;
2390
2391 if (is_usb_chg_plugged_in(chip) &&
2392 (chip->active_path & USB_ACTIVE_BIT)) {
2393 ovpreg = USB_OVP_CONTROL;
2394 ovptestreg = USB_OVP_TEST;
2395 active_mask = USB_ACTIVE_BIT;
2396 } else if (is_dc_chg_plugged_in(chip) &&
2397 (chip->active_path & DC_ACTIVE_BIT)) {
2398 ovpreg = DC_OVP_CONTROL;
2399 ovptestreg = DC_OVP_TEST;
2400 active_mask = DC_ACTIVE_BIT;
2401 } else {
2402 return;
2403 }
David Keitel8c5201a2012-02-24 16:08:54 -08002404
2405 while (count++ < param_open_ovp_counter) {
David Keitel6ccbf132012-05-30 14:21:24 -07002406 pm_chg_masked_write(chip, ovpreg, OVP_DEBOUNCE_TIME, 0x0);
David Keitel8c5201a2012-02-24 16:08:54 -08002407 usleep(10);
David Keitel6ccbf132012-05-30 14:21:24 -07002408 active_chg_plugged_in
2409 = is_active_chg_plugged_in(chip, active_mask);
David Keitel8c5201a2012-02-24 16:08:54 -08002410 chg_gone = pm_chg_get_rt_status(chip, CHG_GONE_IRQ);
David Keitel6ccbf132012-05-30 14:21:24 -07002411 pr_debug("OVP FET count = %d chg_gone=%d, active_valid = %d\n",
2412 count, chg_gone, active_chg_plugged_in);
David Keitel8c5201a2012-02-24 16:08:54 -08002413
2414 /* note usb_chg_plugged_in=0 => chg_gone=1 */
David Keitel6ccbf132012-05-30 14:21:24 -07002415 if (chg_gone == 1 && active_chg_plugged_in == 1) {
David Keitel8c5201a2012-02-24 16:08:54 -08002416 pr_debug("since chg_gone = 1 dis ovp_fet for 20msec\n");
David Keitel6ccbf132012-05-30 14:21:24 -07002417 turn_off_ovp_fet(chip, ovptestreg);
David Keitel8c5201a2012-02-24 16:08:54 -08002418
2419 msleep(20);
2420
David Keitel6ccbf132012-05-30 14:21:24 -07002421 turn_on_ovp_fet(chip, ovptestreg);
David Keitel8c5201a2012-02-24 16:08:54 -08002422 } else {
David Keitel712782e2012-03-29 14:11:47 -07002423 break;
David Keitel8c5201a2012-02-24 16:08:54 -08002424 }
2425 }
David Keitel6e86ba92012-12-20 17:50:31 -08002426 if (pm8xxx_get_version(chip->dev->parent) == PM8XXX_VERSION_8917)
2427 pm_chg_masked_write(chip, ovpreg, OVP_DEBOUNCE_TIME, 0x6);
2428 else
2429 pm_chg_masked_write(chip, ovpreg, OVP_DEBOUNCE_TIME, 0x2);
2430
David Keitel6ccbf132012-05-30 14:21:24 -07002431 pr_debug("Exit count=%d chg_gone=%d, active_valid=%d\n",
2432 count, chg_gone, active_chg_plugged_in);
David Keitel712782e2012-03-29 14:11:47 -07002433 return;
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002434}
David Keitelacf57c82012-03-07 18:48:50 -08002435
2436static int find_usb_ma_value(int value)
2437{
2438 int i;
2439
2440 for (i = ARRAY_SIZE(usb_ma_table) - 1; i >= 0; i--) {
2441 if (value >= usb_ma_table[i].usb_ma)
2442 break;
2443 }
2444
2445 return i;
2446}
2447
2448static void decrease_usb_ma_value(int *value)
2449{
2450 int i;
2451
2452 if (value) {
2453 i = find_usb_ma_value(*value);
2454 if (i > 0)
2455 i--;
David Keitelc7bba2f2012-06-15 17:05:52 -07002456 while (!the_chip->iusb_fine_res && i > 0
2457 && (usb_ma_table[i].value & PM8917_IUSB_FINE_RES))
2458 i--;
Willie Ruan69566002012-11-15 11:56:36 -08002459
2460 if (i < 0) {
2461 pr_err("can't find %dmA in usb_ma_table. Use min.\n",
2462 *value);
2463 i = 0;
2464 }
2465
David Keitelacf57c82012-03-07 18:48:50 -08002466 *value = usb_ma_table[i].usb_ma;
2467 }
2468}
2469
2470static void increase_usb_ma_value(int *value)
2471{
2472 int i;
2473
2474 if (value) {
2475 i = find_usb_ma_value(*value);
2476
2477 if (i < (ARRAY_SIZE(usb_ma_table) - 1))
2478 i++;
David Keitel38bdd4f2012-04-19 15:39:13 -07002479 /* Get next correct entry if IUSB_FINE_RES is not available */
2480 while (!the_chip->iusb_fine_res
2481 && (usb_ma_table[i].value & PM8917_IUSB_FINE_RES)
2482 && i < (ARRAY_SIZE(usb_ma_table) - 1))
2483 i++;
2484
David Keitelacf57c82012-03-07 18:48:50 -08002485 *value = usb_ma_table[i].usb_ma;
2486 }
2487}
2488
2489static void vin_collapse_check_worker(struct work_struct *work)
2490{
2491 struct delayed_work *dwork = to_delayed_work(work);
2492 struct pm8921_chg_chip *chip = container_of(dwork,
2493 struct pm8921_chg_chip, vin_collapse_check_work);
2494
Abhijeet Dharmapurikar6bf4d142012-10-31 19:07:16 -07002495 /*
2496 * AICL only for wall-chargers. If the charger appears to be plugged
2497 * back in now, the corresponding unplug must have been because of we
2498 * were trying to draw more current than the charger can support. In
2499 * such a case reset usb current to 500mA and decrease the target.
2500 * The AICL algorithm will step up the current from 500mA to target
2501 */
2502 if (is_usb_chg_plugged_in(chip)
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08002503 && usb_target_ma > USB_WALL_THRESHOLD_MA
2504 && !chip->disable_aicl) {
David Keitelacf57c82012-03-07 18:48:50 -08002505 /* decrease usb_target_ma */
2506 decrease_usb_ma_value(&usb_target_ma);
2507 /* reset here, increase in unplug_check_worker */
2508 __pm8921_charger_vbus_draw(USB_WALL_THRESHOLD_MA);
2509 pr_debug("usb_now=%d, usb_target = %d\n",
2510 USB_WALL_THRESHOLD_MA, usb_target_ma);
Abhijeet Dharmapurikar6bf4d142012-10-31 19:07:16 -07002511 if (!delayed_work_pending(&chip->unplug_check_work))
2512 schedule_delayed_work(&chip->unplug_check_work,
Abhijeet Dharmapurikarb73001a2012-11-25 22:56:52 -08002513 msecs_to_jiffies
2514 (UNPLUG_CHECK_WAIT_PERIOD_MS));
David Keitelacf57c82012-03-07 18:48:50 -08002515 } else {
2516 handle_usb_insertion_removal(chip);
2517 }
2518}
2519
2520#define VIN_MIN_COLLAPSE_CHECK_MS 50
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002521static irqreturn_t usbin_valid_irq_handler(int irq, void *data)
2522{
David Keitelacf57c82012-03-07 18:48:50 -08002523 if (usb_target_ma)
2524 schedule_delayed_work(&the_chip->vin_collapse_check_work,
2525 round_jiffies_relative(msecs_to_jiffies
2526 (VIN_MIN_COLLAPSE_CHECK_MS)));
2527 else
2528 handle_usb_insertion_removal(data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002529 return IRQ_HANDLED;
2530}
2531
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002532static irqreturn_t batt_inserted_irq_handler(int irq, void *data)
2533{
2534 struct pm8921_chg_chip *chip = data;
2535 int status;
2536
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07002537 status = pm_chg_get_rt_status(chip, BATT_INSERTED_IRQ);
2538 schedule_work(&chip->battery_id_valid_work);
Amir Samuelovd31ef502011-10-26 14:41:36 +02002539 handle_start_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002540 pr_debug("battery present=%d", status);
2541 power_supply_changed(&chip->batt_psy);
2542 return IRQ_HANDLED;
2543}
Amir Samuelovd31ef502011-10-26 14:41:36 +02002544
2545/*
2546 * this interrupt used to restart charging a battery.
2547 *
2548 * Note: When DC-inserted the VBAT can't go low.
2549 * VPH_PWR is provided by the ext-charger.
2550 * After End-Of-Charging from DC, charging can be resumed only
2551 * if DC is removed and then inserted after the battery was in use.
2552 * Therefore the handle_start_ext_chg() is not called.
2553 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002554static irqreturn_t vbatdet_low_irq_handler(int irq, void *data)
2555{
2556 struct pm8921_chg_chip *chip = data;
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07002557 int high_transition;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002558
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07002559 high_transition = pm_chg_get_rt_status(chip, VBATDET_LOW_IRQ);
Abhijeet Dharmapurikar07e48bf2011-09-26 19:46:40 -07002560
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07002561 if (high_transition) {
2562 /* enable auto charging */
2563 pm_chg_auto_enable(chip, !charging_disabled);
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08002564 pr_info("batt fell below resume voltage %s\n",
2565 charging_disabled ? "" : "charger enabled");
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07002566 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002567 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002568
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002569 power_supply_changed(&chip->batt_psy);
2570 power_supply_changed(&chip->usb_psy);
David Keitel63f71662012-02-08 20:30:00 -08002571 power_supply_changed(&chip->dc_psy);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002572
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002573 return IRQ_HANDLED;
2574}
2575
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002576static irqreturn_t chgwdog_irq_handler(int irq, void *data)
2577{
2578 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
2579 return IRQ_HANDLED;
2580}
2581
2582static irqreturn_t vcp_irq_handler(int irq, void *data)
2583{
David Keitel8c5201a2012-02-24 16:08:54 -08002584 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002585 return IRQ_HANDLED;
2586}
2587
2588static irqreturn_t atcdone_irq_handler(int irq, void *data)
2589{
2590 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
2591 return IRQ_HANDLED;
2592}
2593
2594static irqreturn_t atcfail_irq_handler(int irq, void *data)
2595{
2596 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
2597 return IRQ_HANDLED;
2598}
2599
2600static irqreturn_t chgdone_irq_handler(int irq, void *data)
2601{
2602 struct pm8921_chg_chip *chip = data;
2603
2604 pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
Amir Samuelovd31ef502011-10-26 14:41:36 +02002605
2606 handle_stop_ext_chg(chip);
2607
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002608 power_supply_changed(&chip->batt_psy);
2609 power_supply_changed(&chip->usb_psy);
David Keitel63f71662012-02-08 20:30:00 -08002610 power_supply_changed(&chip->dc_psy);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002611
Abhijeet Dharmapurikarfad72352011-09-25 23:06:05 -07002612 bms_notify_check(chip);
2613
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002614 return IRQ_HANDLED;
2615}
2616
2617static irqreturn_t chgfail_irq_handler(int irq, void *data)
2618{
2619 struct pm8921_chg_chip *chip = data;
David Keitel753ec8d2011-11-02 10:56:37 -07002620 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002621
Abhijeet Dharmapurikarf8a66e92012-12-13 15:46:01 -08002622 if (!chip->stop_chg_upon_expiry) {
2623 ret = pm_chg_failed_clear(chip, 1);
2624 if (ret)
2625 pr_err("Failed to write CHG_FAILED_CLEAR bit\n");
2626 }
David Keitel753ec8d2011-11-02 10:56:37 -07002627
2628 pr_err("batt_present = %d, batt_temp_ok = %d, state_changed_to=%d\n",
2629 get_prop_batt_present(chip),
2630 pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ),
2631 pm_chg_get_fsm_state(data));
2632
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002633 power_supply_changed(&chip->batt_psy);
2634 power_supply_changed(&chip->usb_psy);
David Keitel63f71662012-02-08 20:30:00 -08002635 power_supply_changed(&chip->dc_psy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002636 return IRQ_HANDLED;
2637}
2638
2639static irqreturn_t chgstate_irq_handler(int irq, void *data)
2640{
2641 struct pm8921_chg_chip *chip = data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002642
2643 pr_debug("state_changed_to=%d\n", pm_chg_get_fsm_state(data));
2644 power_supply_changed(&chip->batt_psy);
2645 power_supply_changed(&chip->usb_psy);
David Keitel63f71662012-02-08 20:30:00 -08002646 power_supply_changed(&chip->dc_psy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002647
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07002648 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002649
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002650 return IRQ_HANDLED;
2651}
2652
Abhijeet Dharmapurikar5b675ff2012-10-24 13:12:57 -07002653enum {
2654 PON_TIME_25NS = 0x04,
2655 PON_TIME_50NS = 0x08,
2656 PON_TIME_100NS = 0x0C,
2657};
David Keitel8c5201a2012-02-24 16:08:54 -08002658
Abhijeet Dharmapurikar5b675ff2012-10-24 13:12:57 -07002659static void set_min_pon_time(struct pm8921_chg_chip *chip, int pon_time_ns)
David Keitel8c5201a2012-02-24 16:08:54 -08002660{
Abhijeet Dharmapurikar5b675ff2012-10-24 13:12:57 -07002661 u8 temp;
2662 int rc;
David Keitel8c5201a2012-02-24 16:08:54 -08002663
David Keiteladf790e2012-11-28 12:56:55 -08002664 rc = pm_chg_write(chip, CHG_BUCK_CTRL_TEST3, 0x40);
Abhijeet Dharmapurikar5b675ff2012-10-24 13:12:57 -07002665 if (rc) {
2666 pr_err("Failed to write 0x70 to CTRL_TEST3 rc = %d\n", rc);
2667 return;
2668 }
2669 rc = pm8xxx_readb(chip->dev->parent, CHG_BUCK_CTRL_TEST3, &temp);
2670 if (rc) {
2671 pr_err("Failed to read CTRL_TEST3 rc = %d\n", rc);
2672 return;
2673 }
2674 /* clear the min pon time select bit */
2675 temp &= 0xF3;
2676 /* set the pon time */
2677 temp |= (u8)pon_time_ns;
2678 /* write enable bank 4 */
2679 temp |= 0x80;
David Keiteladf790e2012-11-28 12:56:55 -08002680 rc = pm_chg_write(chip, CHG_BUCK_CTRL_TEST3, temp);
Abhijeet Dharmapurikar5b675ff2012-10-24 13:12:57 -07002681 if (rc) {
2682 pr_err("Failed to write 0x%x to CTRL_TEST3 rc=%d\n", temp, rc);
2683 return;
2684 }
2685}
David Keitel8c5201a2012-02-24 16:08:54 -08002686
Abhijeet Dharmapurikar5b675ff2012-10-24 13:12:57 -07002687static void attempt_reverse_boost_fix(struct pm8921_chg_chip *chip)
2688{
2689 pr_debug("Start\n");
2690 set_min_pon_time(chip, PON_TIME_100NS);
2691 pm_chg_vinmin_set(chip, chip->vin_min + 200);
2692 msleep(250);
2693 pm_chg_vinmin_set(chip, chip->vin_min);
2694 set_min_pon_time(chip, PON_TIME_25NS);
2695 pr_debug("End\n");
David Keitel8c5201a2012-02-24 16:08:54 -08002696}
2697
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002698#define VIN_ACTIVE_BIT BIT(0)
David Keitel6ccbf132012-05-30 14:21:24 -07002699#define UNPLUG_WRKARND_RESTORE_WAIT_PERIOD_US 200
2700#define VIN_MIN_INCREASE_MV 100
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002701static void unplug_check_worker(struct work_struct *work)
2702{
2703 struct delayed_work *dwork = to_delayed_work(work);
2704 struct pm8921_chg_chip *chip = container_of(dwork,
2705 struct pm8921_chg_chip, unplug_check_work);
David Keitel663abad2012-09-04 21:00:27 -07002706 u8 reg_loop = 0, active_path;
David Keitel6ccbf132012-05-30 14:21:24 -07002707 int rc, ibat, active_chg_plugged_in, usb_ma;
David Keitel8c5201a2012-02-24 16:08:54 -08002708 int chg_gone = 0;
Abhijeet Dharmapurikarb73001a2012-11-25 22:56:52 -08002709 bool ramp = false;
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002710
David Keitel6ccbf132012-05-30 14:21:24 -07002711 rc = pm8xxx_readb(chip->dev->parent, PBL_ACCESS1, &active_path);
2712 if (rc) {
2713 pr_err("Failed to read PBL_ACCESS1 rc=%d\n", rc);
2714 return;
2715 }
David Keitel6ccbf132012-05-30 14:21:24 -07002716
David Keitel663abad2012-09-04 21:00:27 -07002717 chip->active_path = active_path;
David Keitel6ccbf132012-05-30 14:21:24 -07002718 active_chg_plugged_in = is_active_chg_plugged_in(chip, active_path);
2719 pr_debug("active_path = 0x%x, active_chg_plugged_in = %d\n",
2720 active_path, active_chg_plugged_in);
2721 if (active_path & USB_ACTIVE_BIT) {
2722 pr_debug("USB charger active\n");
2723
2724 pm_chg_iusbmax_get(chip, &usb_ma);
David Keitel6ccbf132012-05-30 14:21:24 -07002725
2726 if (usb_ma <= 100) {
2727 pr_debug(
2728 "Unenumerated or suspended usb_ma = %d skip\n",
2729 usb_ma);
2730 goto check_again_later;
2731 }
2732 } else if (active_path & DC_ACTIVE_BIT) {
2733 pr_debug("DC charger active\n");
2734 } else {
2735 /* No charger active */
2736 if (!(is_usb_chg_plugged_in(chip)
2737 && !(is_dc_chg_plugged_in(chip)))) {
2738 pr_debug(
2739 "Stop: chg removed reg_loop = %d, fsm = %d ibat = %d\n",
2740 pm_chg_get_regulation_loop(chip),
2741 pm_chg_get_fsm_state(chip),
2742 get_prop_batt_current(chip)
2743 );
Abhijeet Dharmapurikar76d25482012-08-28 19:33:49 -07002744 return;
2745 } else {
2746 goto check_again_later;
David Keitel6ccbf132012-05-30 14:21:24 -07002747 }
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002748 }
David Keitel663abad2012-09-04 21:00:27 -07002749
Abhijeet Dharmapurikarcda28f92012-10-29 11:55:48 -07002750 /* AICL only for usb wall charger */
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08002751 if ((active_path & USB_ACTIVE_BIT) && usb_target_ma > 0 &&
2752 !chip->disable_aicl) {
David Keitel6ccbf132012-05-30 14:21:24 -07002753 reg_loop = pm_chg_get_regulation_loop(chip);
2754 pr_debug("reg_loop=0x%x usb_ma = %d\n", reg_loop, usb_ma);
2755 if ((reg_loop & VIN_ACTIVE_BIT) &&
David Keitel5c3ab6d2012-07-30 16:42:42 -07002756 (usb_ma > USB_WALL_THRESHOLD_MA)
2757 && !charging_disabled) {
David Keitel6ccbf132012-05-30 14:21:24 -07002758 decrease_usb_ma_value(&usb_ma);
2759 usb_target_ma = usb_ma;
2760 /* end AICL here */
2761 __pm8921_charger_vbus_draw(usb_ma);
2762 pr_debug("usb_now=%d, usb_target = %d\n",
2763 usb_ma, usb_target_ma);
2764 }
David Keitelacf57c82012-03-07 18:48:50 -08002765 }
2766
2767 reg_loop = pm_chg_get_regulation_loop(chip);
2768 pr_debug("reg_loop=0x%x usb_ma = %d\n", reg_loop, usb_ma);
2769
David Keitel6ccbf132012-05-30 14:21:24 -07002770 ibat = get_prop_batt_current(chip);
Anirudh Ghayal80c729b2013-02-22 11:17:19 +05302771 if ((reg_loop & VIN_ACTIVE_BIT) && !chip->disable_chg_rmvl_wrkarnd) {
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002772 if (ibat > 0) {
Abhijeet Dharmapurikar5b675ff2012-10-24 13:12:57 -07002773 pr_debug("revboost ibat = %d fsm = %d loop = 0x%x\n",
2774 ibat, pm_chg_get_fsm_state(chip), reg_loop);
2775 attempt_reverse_boost_fix(chip);
2776 /* after reverse boost fix check if the active
2777 * charger was detected as removed */
2778 active_chg_plugged_in
2779 = is_active_chg_plugged_in(chip,
2780 active_path);
2781 pr_debug("revboost post: active_chg_plugged_in = %d\n",
2782 active_chg_plugged_in);
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002783 }
2784 }
2785
David Keitel6ccbf132012-05-30 14:21:24 -07002786 active_chg_plugged_in = is_active_chg_plugged_in(chip, active_path);
2787 pr_debug("active_path = 0x%x, active_chg = %d\n",
2788 active_path, active_chg_plugged_in);
David Keitel8c5201a2012-02-24 16:08:54 -08002789 chg_gone = pm_chg_get_rt_status(chip, CHG_GONE_IRQ);
2790
Anirudh Ghayal80c729b2013-02-22 11:17:19 +05302791 if (chg_gone == 1 && active_chg_plugged_in == 1 &&
2792 !chip->disable_chg_rmvl_wrkarnd) {
David Keitel6ccbf132012-05-30 14:21:24 -07002793 pr_debug("chg_gone=%d, active_chg_plugged_in = %d\n",
2794 chg_gone, active_chg_plugged_in);
David Keitelc3e15bd2012-05-25 17:07:25 -07002795 unplug_ovp_fet_open(chip);
David Keitel8c5201a2012-02-24 16:08:54 -08002796 }
2797
Abhijeet Dharmapurikarcda28f92012-10-29 11:55:48 -07002798 /* AICL only for usb wall charger */
David Keitel5c3ab6d2012-07-30 16:42:42 -07002799 if (!(reg_loop & VIN_ACTIVE_BIT) && (active_path & USB_ACTIVE_BIT)
Abhijeet Dharmapurikarcda28f92012-10-29 11:55:48 -07002800 && usb_target_ma > 0
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08002801 && !charging_disabled
2802 && !chip->disable_aicl) {
David Keitelacf57c82012-03-07 18:48:50 -08002803 /* only increase iusb_max if vin loop not active */
2804 if (usb_ma < usb_target_ma) {
2805 increase_usb_ma_value(&usb_ma);
Anirudh Ghayal037cfc82013-01-23 16:43:31 +05302806 if (usb_ma > usb_target_ma)
2807 usb_ma = usb_target_ma;
David Keitelacf57c82012-03-07 18:48:50 -08002808 __pm8921_charger_vbus_draw(usb_ma);
2809 pr_debug("usb_now=%d, usb_target = %d\n",
2810 usb_ma, usb_target_ma);
Abhijeet Dharmapurikarb73001a2012-11-25 22:56:52 -08002811 ramp = true;
David Keitelacf57c82012-03-07 18:48:50 -08002812 } else {
2813 usb_target_ma = usb_ma;
2814 }
2815 }
David Keitel8c5201a2012-02-24 16:08:54 -08002816check_again_later:
Abhijeet Dharmapurikarb73001a2012-11-25 22:56:52 -08002817 pr_debug("ramp: %d\n", ramp);
David Keitelacf57c82012-03-07 18:48:50 -08002818 /* schedule to check again later */
Abhijeet Dharmapurikarb73001a2012-11-25 22:56:52 -08002819 if (ramp)
2820 schedule_delayed_work(&chip->unplug_check_work,
2821 msecs_to_jiffies(UNPLUG_CHECK_RAMP_MS));
2822 else
2823 schedule_delayed_work(&chip->unplug_check_work,
2824 msecs_to_jiffies(UNPLUG_CHECK_WAIT_PERIOD_MS));
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002825}
2826
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002827static irqreturn_t loop_change_irq_handler(int irq, void *data)
2828{
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08002829 struct pm8921_chg_chip *chip = data;
2830
2831 pr_debug("fsm_state=%d reg_loop=0x%x\n",
2832 pm_chg_get_fsm_state(data),
2833 pm_chg_get_regulation_loop(data));
David Keitelb57db9e2012-03-28 16:08:43 -07002834 schedule_work(&chip->unplug_check_work.work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002835 return IRQ_HANDLED;
2836}
2837
Abhijeet Dharmapurikar3ee5b072012-10-15 17:47:29 -07002838struct ibatmax_max_adj_entry {
2839 int ibat_max_ma;
2840 int max_adj_ma;
2841};
2842
2843static struct ibatmax_max_adj_entry ibatmax_adj_table[] = {
2844 {975, 300},
2845 {1475, 150},
2846 {1975, 200},
2847 {2475, 250},
2848};
2849
2850static int find_ibat_max_adj_ma(int ibat_target_ma)
2851{
2852 int i = 0;
2853
Abhijeet Dharmapurikar7781e2f2013-02-12 14:44:04 -08002854 for (i = ARRAY_SIZE(ibatmax_adj_table); i > 0; i--) {
2855 if (ibat_target_ma >= ibatmax_adj_table[i - 1].ibat_max_ma)
Abhijeet Dharmapurikar3ee5b072012-10-15 17:47:29 -07002856 break;
2857 }
2858
2859 return ibatmax_adj_table[i].max_adj_ma;
2860}
2861
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002862static irqreturn_t fastchg_irq_handler(int irq, void *data)
2863{
2864 struct pm8921_chg_chip *chip = data;
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07002865 int high_transition;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002866
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07002867 high_transition = pm_chg_get_rt_status(chip, FASTCHG_IRQ);
2868 if (high_transition && !delayed_work_pending(&chip->eoc_work)) {
2869 wake_lock(&chip->eoc_wake_lock);
2870 schedule_delayed_work(&chip->eoc_work,
2871 round_jiffies_relative(msecs_to_jiffies
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07002872 (EOC_CHECK_PERIOD_MS)));
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07002873 }
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -08002874 if (high_transition
2875 && chip->btc_override
2876 && !delayed_work_pending(&chip->btc_override_work)) {
2877 schedule_delayed_work(&chip->btc_override_work,
2878 round_jiffies_relative(msecs_to_jiffies
2879 (chip->btc_delay_ms)));
2880 }
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07002881 power_supply_changed(&chip->batt_psy);
2882 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002883 return IRQ_HANDLED;
2884}
2885
2886static irqreturn_t trklchg_irq_handler(int irq, void *data)
2887{
2888 struct pm8921_chg_chip *chip = data;
2889
2890 power_supply_changed(&chip->batt_psy);
2891 return IRQ_HANDLED;
2892}
2893
2894static irqreturn_t batt_removed_irq_handler(int irq, void *data)
2895{
2896 struct pm8921_chg_chip *chip = data;
2897 int status;
2898
2899 status = pm_chg_get_rt_status(chip, BATT_REMOVED_IRQ);
2900 pr_debug("battery present=%d state=%d", !status,
2901 pm_chg_get_fsm_state(data));
Amir Samuelovd31ef502011-10-26 14:41:36 +02002902 handle_stop_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002903 power_supply_changed(&chip->batt_psy);
2904 return IRQ_HANDLED;
2905}
2906
2907static irqreturn_t batttemp_hot_irq_handler(int irq, void *data)
2908{
2909 struct pm8921_chg_chip *chip = data;
2910
Amir Samuelovd31ef502011-10-26 14:41:36 +02002911 handle_stop_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002912 power_supply_changed(&chip->batt_psy);
2913 return IRQ_HANDLED;
2914}
2915
2916static irqreturn_t chghot_irq_handler(int irq, void *data)
2917{
2918 struct pm8921_chg_chip *chip = data;
2919
2920 pr_debug("Chg hot fsm_state=%d\n", pm_chg_get_fsm_state(data));
2921 power_supply_changed(&chip->batt_psy);
2922 power_supply_changed(&chip->usb_psy);
David Keitel88e1b572012-01-11 11:57:14 -08002923 handle_stop_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002924 return IRQ_HANDLED;
2925}
2926
2927static irqreturn_t batttemp_cold_irq_handler(int irq, void *data)
2928{
2929 struct pm8921_chg_chip *chip = data;
2930
2931 pr_debug("Batt cold fsm_state=%d\n", pm_chg_get_fsm_state(data));
Amir Samuelovd31ef502011-10-26 14:41:36 +02002932 handle_stop_ext_chg(chip);
2933
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002934 power_supply_changed(&chip->batt_psy);
2935 power_supply_changed(&chip->usb_psy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002936 return IRQ_HANDLED;
2937}
2938
2939static irqreturn_t chg_gone_irq_handler(int irq, void *data)
2940{
2941 struct pm8921_chg_chip *chip = data;
David Keitelc3e15bd2012-05-25 17:07:25 -07002942 int chg_gone, usb_chg_plugged_in;
David Keitel8c5201a2012-02-24 16:08:54 -08002943
2944 usb_chg_plugged_in = is_usb_chg_plugged_in(chip);
2945 chg_gone = pm_chg_get_rt_status(chip, CHG_GONE_IRQ);
2946
2947 pr_debug("chg_gone=%d, usb_valid = %d\n", chg_gone, usb_chg_plugged_in);
David Keitel0873d0f2012-03-29 11:44:49 -07002948 pr_debug("Chg gone fsm_state=%d\n", pm_chg_get_fsm_state(data));
2949
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002950 power_supply_changed(&chip->batt_psy);
2951 power_supply_changed(&chip->usb_psy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002952 return IRQ_HANDLED;
2953}
David Keitel52fda532011-11-10 10:43:44 -08002954/*
2955 *
2956 * bat_temp_ok_irq_handler - is edge triggered, hence it will
2957 * fire for two cases:
2958 *
2959 * If the interrupt line switches to high temperature is okay
2960 * and thus charging begins.
2961 * If bat_temp_ok is low this means the temperature is now
2962 * too hot or cold, so charging is stopped.
2963 *
2964 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002965static irqreturn_t bat_temp_ok_irq_handler(int irq, void *data)
2966{
David Keitel52fda532011-11-10 10:43:44 -08002967 int bat_temp_ok;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002968 struct pm8921_chg_chip *chip = data;
2969
David Keitel52fda532011-11-10 10:43:44 -08002970 bat_temp_ok = pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ);
2971
2972 pr_debug("batt_temp_ok = %d fsm_state%d\n",
2973 bat_temp_ok, pm_chg_get_fsm_state(data));
2974
2975 if (bat_temp_ok)
2976 handle_start_ext_chg(chip);
2977 else
2978 handle_stop_ext_chg(chip);
Amir Samuelovd31ef502011-10-26 14:41:36 +02002979
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002980 power_supply_changed(&chip->batt_psy);
2981 power_supply_changed(&chip->usb_psy);
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07002982 bms_notify_check(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002983 return IRQ_HANDLED;
2984}
2985
2986static irqreturn_t coarse_det_low_irq_handler(int irq, void *data)
2987{
2988 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
2989 return IRQ_HANDLED;
2990}
2991
2992static irqreturn_t vdd_loop_irq_handler(int irq, void *data)
2993{
2994 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
2995 return IRQ_HANDLED;
2996}
2997
2998static irqreturn_t vreg_ov_irq_handler(int irq, void *data)
2999{
3000 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
3001 return IRQ_HANDLED;
3002}
3003
3004static irqreturn_t vbatdet_irq_handler(int irq, void *data)
3005{
3006 pr_debug("fsm_state=%d\n", pm_chg_get_fsm_state(data));
3007 return IRQ_HANDLED;
3008}
3009
3010static irqreturn_t batfet_irq_handler(int irq, void *data)
3011{
3012 struct pm8921_chg_chip *chip = data;
3013
3014 pr_debug("vreg ov\n");
3015 power_supply_changed(&chip->batt_psy);
3016 return IRQ_HANDLED;
3017}
3018
3019static irqreturn_t dcin_valid_irq_handler(int irq, void *data)
3020{
Amir Samuelovd31ef502011-10-26 14:41:36 +02003021 struct pm8921_chg_chip *chip = data;
David Keitel88e1b572012-01-11 11:57:14 -08003022 int dc_present;
Amir Samuelovd31ef502011-10-26 14:41:36 +02003023
Abhijeet Dharmapurikar70ef3822012-10-29 15:55:34 -07003024 pm_chg_failed_clear(chip, 1);
David Keitel88e1b572012-01-11 11:57:14 -08003025 dc_present = pm_chg_get_rt_status(chip, DCIN_VALID_IRQ);
David Keitele7a05ca2012-10-16 16:37:28 -07003026
Abhijeet Dharmapurikar70ef3822012-10-29 15:55:34 -07003027 if (chip->dc_present ^ dc_present)
3028 pm8921_bms_calibrate_hkadc();
3029
3030 if (dc_present)
3031 pm8921_chg_enable_irq(chip, CHG_GONE_IRQ);
3032 else
3033 pm8921_chg_disable_irq(chip, CHG_GONE_IRQ);
3034
3035 chip->dc_present = dc_present;
3036
3037 if (chip->ext_psy) {
3038 if (dc_present)
3039 handle_start_ext_chg(chip);
3040 else
3041 handle_stop_ext_chg(chip);
3042 } else {
3043 if (dc_present)
3044 schedule_delayed_work(&chip->unplug_check_work,
Abhijeet Dharmapurikarb73001a2012-11-25 22:56:52 -08003045 msecs_to_jiffies(UNPLUG_CHECK_WAIT_PERIOD_MS));
David Keitele7a05ca2012-10-16 16:37:28 -07003046 power_supply_changed(&chip->dc_psy);
David Keitele7a05ca2012-10-16 16:37:28 -07003047 }
3048
Abhijeet Dharmapurikar70ef3822012-10-29 15:55:34 -07003049 power_supply_changed(&chip->batt_psy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003050 return IRQ_HANDLED;
3051}
3052
3053static irqreturn_t dcin_ov_irq_handler(int irq, void *data)
3054{
Amir Samuelovd31ef502011-10-26 14:41:36 +02003055 struct pm8921_chg_chip *chip = data;
3056
Amir Samuelovd31ef502011-10-26 14:41:36 +02003057 handle_stop_ext_chg(chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003058 return IRQ_HANDLED;
3059}
3060
3061static irqreturn_t dcin_uv_irq_handler(int irq, void *data)
3062{
Amir Samuelovd31ef502011-10-26 14:41:36 +02003063 struct pm8921_chg_chip *chip = data;
3064
Amir Samuelovd31ef502011-10-26 14:41:36 +02003065 handle_stop_ext_chg(chip);
3066
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003067 return IRQ_HANDLED;
3068}
3069
David Keitel88e1b572012-01-11 11:57:14 -08003070static int __pm_batt_external_power_changed_work(struct device *dev, void *data)
3071{
3072 struct power_supply *psy = &the_chip->batt_psy;
3073 struct power_supply *epsy = dev_get_drvdata(dev);
3074 int i, dcin_irq;
3075
3076 /* Only search for external supply if none is registered */
3077 if (!the_chip->ext_psy) {
3078 dcin_irq = the_chip->pmic_chg_irq[DCIN_VALID_IRQ];
3079 for (i = 0; i < epsy->num_supplicants; i++) {
3080 if (!strncmp(epsy->supplied_to[i], psy->name, 7)) {
3081 if (!strncmp(epsy->name, "dc", 2)) {
3082 the_chip->ext_psy = epsy;
3083 dcin_valid_irq_handler(dcin_irq,
3084 the_chip);
3085 }
3086 }
3087 }
3088 }
3089 return 0;
3090}
3091
3092static void pm_batt_external_power_changed(struct power_supply *psy)
3093{
3094 /* Only look for an external supply if it hasn't been registered */
3095 if (!the_chip->ext_psy)
3096 class_for_each_device(power_supply_class, NULL, psy,
3097 __pm_batt_external_power_changed_work);
3098}
3099
Abhijeet Dharmapurikar33fe6fb2011-09-14 16:03:11 -07003100/**
3101 * update_heartbeat - internal function to update userspace
3102 * per update_time minutes
3103 *
3104 */
Abhijeet Dharmapurikarde9c7e72012-07-15 16:29:51 -07003105#define LOW_SOC_HEARTBEAT_MS 20000
Abhijeet Dharmapurikar33fe6fb2011-09-14 16:03:11 -07003106static void update_heartbeat(struct work_struct *work)
3107{
3108 struct delayed_work *dwork = to_delayed_work(work);
3109 struct pm8921_chg_chip *chip = container_of(dwork,
3110 struct pm8921_chg_chip, update_heartbeat_work);
3111
3112 power_supply_changed(&chip->batt_psy);
Abhijeet Dharmapurikarde9c7e72012-07-15 16:29:51 -07003113 if (chip->recent_reported_soc <= 20)
3114 schedule_delayed_work(&chip->update_heartbeat_work,
3115 round_jiffies_relative(msecs_to_jiffies
3116 (LOW_SOC_HEARTBEAT_MS)));
3117 else
3118 schedule_delayed_work(&chip->update_heartbeat_work,
Abhijeet Dharmapurikar33fe6fb2011-09-14 16:03:11 -07003119 round_jiffies_relative(msecs_to_jiffies
3120 (chip->update_time)));
3121}
Abhijeet Dharmapurikar40491ce2011-11-07 11:38:54 -08003122#define VDD_LOOP_ACTIVE_BIT BIT(3)
3123#define VDD_MAX_INCREASE_MV 400
3124static int vdd_max_increase_mv = VDD_MAX_INCREASE_MV;
3125module_param(vdd_max_increase_mv, int, 0644);
3126
3127static int ichg_threshold_ua = -400000;
3128module_param(ichg_threshold_ua, int, 0644);
Abhijeet Dharmapurikardc3323a2012-08-10 15:47:31 -07003129
3130#define PM8921_CHG_VDDMAX_RES_MV 10
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -07003131static void adjust_vdd_max_for_fastchg(struct pm8921_chg_chip *chip,
3132 int vbat_batt_terminal_uv)
Abhijeet Dharmapurikar40491ce2011-11-07 11:38:54 -08003133{
Abhijeet Dharmapurikar40491ce2011-11-07 11:38:54 -08003134 int adj_vdd_max_mv, programmed_vdd_max;
Abhijeet Dharmapurikar40491ce2011-11-07 11:38:54 -08003135 int vbat_batt_terminal_mv;
3136 int reg_loop;
3137 int delta_mv = 0;
3138
3139 if (chip->rconn_mohm == 0) {
3140 pr_debug("Exiting as rconn_mohm is 0\n");
3141 return;
3142 }
3143 /* adjust vdd_max only in normal temperature zone */
3144 if (chip->is_bat_cool || chip->is_bat_warm) {
3145 pr_debug("Exiting is_bat_cool = %d is_batt_warm = %d\n",
3146 chip->is_bat_cool, chip->is_bat_warm);
3147 return;
3148 }
3149
3150 reg_loop = pm_chg_get_regulation_loop(chip);
3151 if (!(reg_loop & VDD_LOOP_ACTIVE_BIT)) {
3152 pr_debug("Exiting Vdd loop is not active reg loop=0x%x\n",
3153 reg_loop);
3154 return;
3155 }
Abhijeet Dharmapurikar40491ce2011-11-07 11:38:54 -08003156 vbat_batt_terminal_mv = vbat_batt_terminal_uv/1000;
3157 pm_chg_vddmax_get(the_chip, &programmed_vdd_max);
3158
3159 delta_mv = chip->max_voltage_mv - vbat_batt_terminal_mv;
3160
3161 adj_vdd_max_mv = programmed_vdd_max + delta_mv;
3162 pr_debug("vdd_max needs to be changed by %d mv from %d to %d\n",
3163 delta_mv,
3164 programmed_vdd_max,
3165 adj_vdd_max_mv);
3166
3167 if (adj_vdd_max_mv < chip->max_voltage_mv) {
3168 pr_debug("adj vdd_max lower than default max voltage\n");
3169 return;
3170 }
3171
Abhijeet Dharmapurikardc3323a2012-08-10 15:47:31 -07003172 adj_vdd_max_mv = DIV_ROUND_UP(adj_vdd_max_mv, PM8921_CHG_VDDMAX_RES_MV)
3173 * PM8921_CHG_VDDMAX_RES_MV;
3174
Abhijeet Dharmapurikar40491ce2011-11-07 11:38:54 -08003175 if (adj_vdd_max_mv > (chip->max_voltage_mv + vdd_max_increase_mv))
3176 adj_vdd_max_mv = chip->max_voltage_mv + vdd_max_increase_mv;
Abhijeet Dharmapurikar40491ce2011-11-07 11:38:54 -08003177 pr_debug("adjusting vdd_max_mv to %d to make "
3178 "vbat_batt_termial_uv = %d to %d\n",
3179 adj_vdd_max_mv, vbat_batt_terminal_uv, chip->max_voltage_mv);
3180 pm_chg_vddmax_set(chip, adj_vdd_max_mv);
3181}
Abhijeet Dharmapurikar33fe6fb2011-09-14 16:03:11 -07003182
Abhijeet Dharmapurikar035a0582012-10-26 16:54:12 -07003183static void set_appropriate_vbatdet(struct pm8921_chg_chip *chip)
3184{
3185 if (chip->is_bat_cool)
3186 pm_chg_vbatdet_set(the_chip,
3187 the_chip->cool_bat_voltage
3188 - the_chip->resume_voltage_delta);
3189 else if (chip->is_bat_warm)
3190 pm_chg_vbatdet_set(the_chip,
3191 the_chip->warm_bat_voltage
3192 - the_chip->resume_voltage_delta);
3193 else
3194 pm_chg_vbatdet_set(the_chip,
3195 the_chip->max_voltage_mv
3196 - the_chip->resume_voltage_delta);
3197}
3198
Abhijeet Dharmapurikar16adb082012-11-07 10:36:50 -08003199static void set_appropriate_battery_current(struct pm8921_chg_chip *chip)
3200{
3201 unsigned int chg_current = chip->max_bat_chg_current;
3202
3203 if (chip->is_bat_cool)
3204 chg_current = min(chg_current, chip->cool_bat_chg_current);
3205
3206 if (chip->is_bat_warm)
3207 chg_current = min(chg_current, chip->warm_bat_chg_current);
3208
3209 if (thermal_mitigation != 0 && chip->thermal_mitigation)
3210 chg_current = min(chg_current,
3211 chip->thermal_mitigation[thermal_mitigation]);
3212
3213 pm_chg_ibatmax_set(the_chip, chg_current);
3214}
3215
3216#define TEMP_HYSTERISIS_DECIDEGC 20
3217static void battery_cool(bool enter)
3218{
3219 pr_debug("enter = %d\n", enter);
3220 if (enter == the_chip->is_bat_cool)
3221 return;
3222 the_chip->is_bat_cool = enter;
3223 if (enter)
3224 pm_chg_vddmax_set(the_chip, the_chip->cool_bat_voltage);
3225 else
3226 pm_chg_vddmax_set(the_chip, the_chip->max_voltage_mv);
3227 set_appropriate_battery_current(the_chip);
3228 set_appropriate_vbatdet(the_chip);
3229}
3230
3231static void battery_warm(bool enter)
3232{
3233 pr_debug("enter = %d\n", enter);
3234 if (enter == the_chip->is_bat_warm)
3235 return;
3236 the_chip->is_bat_warm = enter;
3237 if (enter)
3238 pm_chg_vddmax_set(the_chip, the_chip->warm_bat_voltage);
3239 else
3240 pm_chg_vddmax_set(the_chip, the_chip->max_voltage_mv);
3241
3242 set_appropriate_battery_current(the_chip);
3243 set_appropriate_vbatdet(the_chip);
3244}
3245
3246static void check_temp_thresholds(struct pm8921_chg_chip *chip)
3247{
3248 int temp = 0;
3249
3250 temp = get_prop_batt_temp(chip);
3251 pr_debug("temp = %d, warm_thr_temp = %d, cool_thr_temp = %d\n",
3252 temp, chip->warm_temp_dc,
3253 chip->cool_temp_dc);
3254
3255 if (chip->warm_temp_dc != INT_MIN) {
3256 if (chip->is_bat_warm
3257 && temp < chip->warm_temp_dc - TEMP_HYSTERISIS_DECIDEGC)
3258 battery_warm(false);
3259 else if (!chip->is_bat_warm && temp >= chip->warm_temp_dc)
3260 battery_warm(true);
3261 }
3262
3263 if (chip->cool_temp_dc != INT_MIN) {
3264 if (chip->is_bat_cool
3265 && temp > chip->cool_temp_dc + TEMP_HYSTERISIS_DECIDEGC)
3266 battery_cool(false);
3267 else if (!chip->is_bat_cool && temp <= chip->cool_temp_dc)
3268 battery_cool(true);
3269 }
3270}
3271
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08003272enum {
3273 CHG_IN_PROGRESS,
3274 CHG_NOT_IN_PROGRESS,
3275 CHG_FINISHED,
3276};
3277
3278#define VBAT_TOLERANCE_MV 70
3279#define CHG_DISABLE_MSLEEP 100
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -07003280static int is_charging_finished(struct pm8921_chg_chip *chip,
3281 int vbat_batt_terminal_uv, int ichg_meas_ma)
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08003282{
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -07003283 int vbat_programmed, iterm_programmed, vbat_intended;
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08003284 int regulation_loop, fast_chg, vcp;
3285 int rc;
3286 static int last_vbat_programmed = -EINVAL;
3287
3288 if (!is_ext_charging(chip)) {
3289 /* return if the battery is not being fastcharged */
3290 fast_chg = pm_chg_get_rt_status(chip, FASTCHG_IRQ);
3291 pr_debug("fast_chg = %d\n", fast_chg);
3292 if (fast_chg == 0)
3293 return CHG_NOT_IN_PROGRESS;
3294
3295 vcp = pm_chg_get_rt_status(chip, VCP_IRQ);
3296 pr_debug("vcp = %d\n", vcp);
3297 if (vcp == 1)
3298 return CHG_IN_PROGRESS;
3299
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08003300 /* reset count if battery is hot/cold */
3301 rc = pm_chg_get_rt_status(chip, BAT_TEMP_OK_IRQ);
3302 pr_debug("batt_temp_ok = %d\n", rc);
3303 if (rc == 0)
3304 return CHG_IN_PROGRESS;
3305
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08003306 rc = pm_chg_vddmax_get(chip, &vbat_programmed);
3307 if (rc) {
3308 pr_err("couldnt read vddmax rc = %d\n", rc);
3309 return CHG_IN_PROGRESS;
3310 }
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -07003311 pr_debug("vddmax = %d vbat_batt_terminal_uv=%d\n",
3312 vbat_programmed, vbat_batt_terminal_uv);
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08003313
3314 if (last_vbat_programmed == -EINVAL)
3315 last_vbat_programmed = vbat_programmed;
3316 if (last_vbat_programmed != vbat_programmed) {
3317 /* vddmax changed, reset and check again */
3318 pr_debug("vddmax = %d last_vdd_max=%d\n",
3319 vbat_programmed, last_vbat_programmed);
3320 last_vbat_programmed = vbat_programmed;
3321 return CHG_IN_PROGRESS;
3322 }
3323
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -07003324 if (chip->is_bat_cool)
3325 vbat_intended = chip->cool_bat_voltage;
3326 else if (chip->is_bat_warm)
3327 vbat_intended = chip->warm_bat_voltage;
3328 else
3329 vbat_intended = chip->max_voltage_mv;
3330
3331 if (vbat_batt_terminal_uv / 1000 < vbat_intended) {
3332 pr_debug("terminal_uv:%d < vbat_intended:%d.\n",
3333 vbat_batt_terminal_uv,
3334 vbat_intended);
3335 return CHG_IN_PROGRESS;
3336 }
3337
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08003338 regulation_loop = pm_chg_get_regulation_loop(chip);
3339 if (regulation_loop < 0) {
3340 pr_err("couldnt read the regulation loop err=%d\n",
3341 regulation_loop);
3342 return CHG_IN_PROGRESS;
3343 }
3344 pr_debug("regulation_loop=%d\n", regulation_loop);
3345
3346 if (regulation_loop != 0 && regulation_loop != VDD_LOOP)
3347 return CHG_IN_PROGRESS;
3348 } /* !is_ext_charging */
3349
3350 /* reset count if battery chg current is more than iterm */
3351 rc = pm_chg_iterm_get(chip, &iterm_programmed);
3352 if (rc) {
3353 pr_err("couldnt read iterm rc = %d\n", rc);
3354 return CHG_IN_PROGRESS;
3355 }
3356
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08003357 pr_debug("iterm_programmed = %d ichg_meas_ma=%d\n",
3358 iterm_programmed, ichg_meas_ma);
3359 /*
3360 * ichg_meas_ma < 0 means battery is drawing current
3361 * ichg_meas_ma > 0 means battery is providing current
3362 */
3363 if (ichg_meas_ma > 0)
3364 return CHG_IN_PROGRESS;
3365
3366 if (ichg_meas_ma * -1 > iterm_programmed)
3367 return CHG_IN_PROGRESS;
3368
3369 return CHG_FINISHED;
3370}
3371
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -08003372#define COMP_OVERRIDE_HOT_BANK 6
3373#define COMP_OVERRIDE_COLD_BANK 7
3374#define COMP_OVERRIDE_BIT BIT(1)
3375static int pm_chg_override_cold(struct pm8921_chg_chip *chip, int flag)
3376{
3377 u8 val;
3378 int rc = 0;
3379
3380 val = 0x80 | COMP_OVERRIDE_COLD_BANK << 2 | COMP_OVERRIDE_BIT;
3381
3382 if (flag)
3383 val |= 0x01;
3384
David Keiteladf790e2012-11-28 12:56:55 -08003385 rc = pm_chg_write(chip, COMPARATOR_OVERRIDE, val);
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -08003386 if (rc < 0)
3387 pr_err("Could not write 0x%x to override rc = %d\n", val, rc);
3388
3389 pr_debug("btc cold = %d val = 0x%x\n", flag, val);
3390 return rc;
3391}
3392
3393static int pm_chg_override_hot(struct pm8921_chg_chip *chip, int flag)
3394{
3395 u8 val;
3396 int rc = 0;
3397
3398 val = 0x80 | COMP_OVERRIDE_HOT_BANK << 2 | COMP_OVERRIDE_BIT;
3399
3400 if (flag)
3401 val |= 0x01;
3402
David Keiteladf790e2012-11-28 12:56:55 -08003403 rc = pm_chg_write(chip, COMPARATOR_OVERRIDE, val);
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -08003404 if (rc < 0)
3405 pr_err("Could not write 0x%x to override rc = %d\n", val, rc);
3406
3407 pr_debug("btc hot = %d val = 0x%x\n", flag, val);
3408 return rc;
3409}
3410
3411static void __devinit pm8921_chg_btc_override_init(struct pm8921_chg_chip *chip)
3412{
3413 int rc = 0;
3414 u8 reg;
3415 u8 val;
3416
3417 val = COMP_OVERRIDE_HOT_BANK << 2;
David Keiteladf790e2012-11-28 12:56:55 -08003418 rc = pm_chg_write(chip, COMPARATOR_OVERRIDE, val);
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -08003419 if (rc < 0) {
3420 pr_err("Could not write 0x%x to override rc = %d\n", val, rc);
3421 goto cold_init;
3422 }
3423 rc = pm8xxx_readb(chip->dev->parent, COMPARATOR_OVERRIDE, &reg);
3424 if (rc < 0) {
3425 pr_err("Could not read bank %d of override rc = %d\n",
3426 COMP_OVERRIDE_HOT_BANK, rc);
3427 goto cold_init;
3428 }
3429 if ((reg & COMP_OVERRIDE_BIT) != COMP_OVERRIDE_BIT) {
3430 /* for now override it as not hot */
3431 rc = pm_chg_override_hot(chip, 0);
3432 if (rc < 0)
3433 pr_err("Could not override hot rc = %d\n", rc);
3434 }
3435
3436cold_init:
3437 val = COMP_OVERRIDE_COLD_BANK << 2;
David Keiteladf790e2012-11-28 12:56:55 -08003438 rc = pm_chg_write(chip, COMPARATOR_OVERRIDE, val);
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -08003439 if (rc < 0) {
3440 pr_err("Could not write 0x%x to override rc = %d\n", val, rc);
3441 return;
3442 }
3443 rc = pm8xxx_readb(chip->dev->parent, COMPARATOR_OVERRIDE, &reg);
3444 if (rc < 0) {
3445 pr_err("Could not read bank %d of override rc = %d\n",
3446 COMP_OVERRIDE_COLD_BANK, rc);
3447 return;
3448 }
3449 if ((reg & COMP_OVERRIDE_BIT) != COMP_OVERRIDE_BIT) {
3450 /* for now override it as not cold */
3451 rc = pm_chg_override_cold(chip, 0);
3452 if (rc < 0)
3453 pr_err("Could not override cold rc = %d\n", rc);
3454 }
3455}
3456
3457static void btc_override_worker(struct work_struct *work)
3458{
3459 int decidegc;
3460 int temp;
3461 int rc = 0;
3462 struct delayed_work *dwork = to_delayed_work(work);
3463 struct pm8921_chg_chip *chip = container_of(dwork,
3464 struct pm8921_chg_chip, btc_override_work);
3465
3466 if (!chip->btc_override) {
3467 pr_err("called when not enabled\n");
3468 return;
3469 }
3470
3471 decidegc = get_prop_batt_temp(chip);
3472
3473 pr_debug("temp=%d\n", decidegc);
3474
3475 temp = pm_chg_get_rt_status(chip, BATTTEMP_HOT_IRQ);
3476 if (temp) {
3477 if (decidegc < chip->btc_override_hot_decidegc)
3478 /* stop forcing batt hot */
3479 rc = pm_chg_override_hot(chip, 0);
3480 if (rc)
3481 pr_err("Couldnt write 0 to hot comp\n");
3482 } else {
3483 if (decidegc >= chip->btc_override_hot_decidegc)
3484 /* start forcing batt hot */
3485 rc = pm_chg_override_hot(chip, 1);
3486 if (rc && chip->btc_panic_if_cant_stop_chg)
3487 panic("Couldnt override comps to stop chg\n");
3488 }
3489
3490 temp = pm_chg_get_rt_status(chip, BATTTEMP_COLD_IRQ);
3491 if (temp) {
3492 if (decidegc > chip->btc_override_cold_decidegc)
3493 /* stop forcing batt cold */
3494 rc = pm_chg_override_cold(chip, 0);
3495 if (rc)
3496 pr_err("Couldnt write 0 to cold comp\n");
3497 } else {
3498 if (decidegc <= chip->btc_override_cold_decidegc)
3499 /* start forcing batt cold */
3500 rc = pm_chg_override_cold(chip, 1);
3501 if (rc && chip->btc_panic_if_cant_stop_chg)
3502 panic("Couldnt override comps to stop chg\n");
3503 }
3504
3505 if ((is_dc_chg_plugged_in(the_chip) || is_usb_chg_plugged_in(the_chip))
3506 && get_prop_batt_status(chip) != POWER_SUPPLY_STATUS_FULL) {
3507 schedule_delayed_work(&chip->btc_override_work,
3508 round_jiffies_relative(msecs_to_jiffies
3509 (chip->btc_delay_ms)));
3510 return;
3511 }
3512
3513 rc = pm_chg_override_hot(chip, 0);
3514 if (rc)
3515 pr_err("Couldnt write 0 to hot comp\n");
3516 rc = pm_chg_override_cold(chip, 0);
3517 if (rc)
3518 pr_err("Couldnt write 0 to cold comp\n");
3519}
3520
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003521/**
Amir Samuelovd31ef502011-10-26 14:41:36 +02003522 * eoc_worker - internal function to check if battery EOC
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003523 * has happened
3524 *
3525 * If all conditions favouring, if the charge current is
3526 * less than the term current for three consecutive times
3527 * an EOC has happened.
3528 * The wakelock is released if there is no need to reshedule
3529 * - this happens when the battery is removed or EOC has
3530 * happened
3531 */
3532#define CONSECUTIVE_COUNT 3
Amir Samuelovd31ef502011-10-26 14:41:36 +02003533static void eoc_worker(struct work_struct *work)
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003534{
3535 struct delayed_work *dwork = to_delayed_work(work);
3536 struct pm8921_chg_chip *chip = container_of(dwork,
3537 struct pm8921_chg_chip, eoc_work);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003538 static int count;
Abhijeet Dharmapurikare451f342012-02-23 16:39:42 -08003539 int end;
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -07003540 int vbat_meas_uv, vbat_meas_mv;
3541 int ichg_meas_ua, ichg_meas_ma;
3542 int vbat_batt_terminal_uv;
Abhijeet Dharmapurikare451f342012-02-23 16:39:42 -08003543
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -07003544 pm8921_bms_get_simultaneous_battery_voltage_and_current(
3545 &ichg_meas_ua, &vbat_meas_uv);
3546 vbat_meas_mv = vbat_meas_uv / 1000;
3547 /* rconn_mohm is in milliOhms */
3548 ichg_meas_ma = ichg_meas_ua / 1000;
3549 vbat_batt_terminal_uv = vbat_meas_uv
3550 + ichg_meas_ma
3551 * the_chip->rconn_mohm;
3552
3553 end = is_charging_finished(chip, vbat_batt_terminal_uv, ichg_meas_ma);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003554
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08003555 if (end == CHG_NOT_IN_PROGRESS) {
Abhijeet Dharmapurikar40491ce2011-11-07 11:38:54 -08003556 count = 0;
Abhijeet Dharmapurikar035a0582012-10-26 16:54:12 -07003557 goto eoc_worker_stop;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003558 }
3559
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08003560 if (end == CHG_FINISHED) {
3561 count++;
3562 } else {
3563 count = 0;
3564 }
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003565
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003566 if (count == CONSECUTIVE_COUNT) {
3567 count = 0;
3568 pr_info("End of Charging\n");
3569
3570 pm_chg_auto_enable(chip, 0);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003571
Amir Samuelovd31ef502011-10-26 14:41:36 +02003572 if (is_ext_charging(chip))
3573 chip->ext_charge_done = true;
3574
Abhijeet Dharmapurikareb882f52011-12-22 09:02:34 -08003575 if (chip->is_bat_warm || chip->is_bat_cool)
3576 chip->bms_notify.is_battery_full = 0;
3577 else
3578 chip->bms_notify.is_battery_full = 1;
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003579 /* declare end of charging by invoking chgdone interrupt */
3580 chgdone_irq_handler(chip->pmic_chg_irq[CHGDONE_IRQ], chip);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003581 } else {
Abhijeet Dharmapurikar16adb082012-11-07 10:36:50 -08003582 check_temp_thresholds(chip);
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -07003583 adjust_vdd_max_for_fastchg(chip, vbat_batt_terminal_uv);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003584 pr_debug("EOC count = %d\n", count);
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08003585 schedule_delayed_work(&chip->eoc_work,
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003586 round_jiffies_relative(msecs_to_jiffies
3587 (EOC_CHECK_PERIOD_MS)));
Abhijeet Dharmapurikar035a0582012-10-26 16:54:12 -07003588 return;
Abhijeet Dharmapurikar75c03502012-01-09 12:45:57 -08003589 }
Abhijeet Dharmapurikar035a0582012-10-26 16:54:12 -07003590
3591eoc_worker_stop:
3592 wake_unlock(&chip->eoc_wake_lock);
3593 /* set the vbatdet back, in case it was changed to trigger charging */
3594 set_appropriate_vbatdet(chip);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003595}
3596
Abhijeet Dharmapurikar1f52c412011-08-12 17:11:45 -07003597/**
3598 * set_disable_status_param -
3599 *
3600 * Internal function to disable battery charging and also disable drawing
3601 * any current from the source. The device is forced to run on a battery
3602 * after this.
3603 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003604static int set_disable_status_param(const char *val, struct kernel_param *kp)
3605{
3606 int ret;
3607 struct pm8921_chg_chip *chip = the_chip;
3608
3609 ret = param_set_int(val, kp);
3610 if (ret) {
3611 pr_err("error setting value %d\n", ret);
3612 return ret;
3613 }
3614 pr_info("factory set disable param to %d\n", charging_disabled);
3615 if (chip) {
3616 pm_chg_auto_enable(chip, !charging_disabled);
3617 pm_chg_charge_dis(chip, charging_disabled);
3618 }
3619 return 0;
3620}
3621module_param_call(disabled, set_disable_status_param, param_get_uint,
3622 &charging_disabled, 0644);
3623
Abhijeet Dharmapurikar40491ce2011-11-07 11:38:54 -08003624static int rconn_mohm;
3625static int set_rconn_mohm(const char *val, struct kernel_param *kp)
3626{
3627 int ret;
3628 struct pm8921_chg_chip *chip = the_chip;
3629
3630 ret = param_set_int(val, kp);
3631 if (ret) {
3632 pr_err("error setting value %d\n", ret);
3633 return ret;
3634 }
3635 if (chip)
3636 chip->rconn_mohm = rconn_mohm;
3637 return 0;
3638}
3639module_param_call(rconn_mohm, set_rconn_mohm, param_get_uint,
3640 &rconn_mohm, 0644);
Abhijeet Dharmapurikarad742362011-08-29 19:50:02 -07003641/**
3642 * set_thermal_mitigation_level -
3643 *
3644 * Internal function to control battery charging current to reduce
3645 * temperature
3646 */
3647static int set_therm_mitigation_level(const char *val, struct kernel_param *kp)
3648{
3649 int ret;
3650 struct pm8921_chg_chip *chip = the_chip;
3651
3652 ret = param_set_int(val, kp);
3653 if (ret) {
3654 pr_err("error setting value %d\n", ret);
3655 return ret;
3656 }
3657
3658 if (!chip) {
3659 pr_err("called before init\n");
3660 return -EINVAL;
3661 }
3662
3663 if (!chip->thermal_mitigation) {
3664 pr_err("no thermal mitigation\n");
3665 return -EINVAL;
3666 }
3667
3668 if (thermal_mitigation < 0
3669 || thermal_mitigation >= chip->thermal_levels) {
3670 pr_err("out of bound level selected\n");
3671 return -EINVAL;
3672 }
3673
Abhijeet Dharmapurikard585ff22011-11-10 14:27:25 -08003674 set_appropriate_battery_current(chip);
Abhijeet Dharmapurikarad742362011-08-29 19:50:02 -07003675 return ret;
3676}
3677module_param_call(thermal_mitigation, set_therm_mitigation_level,
3678 param_get_uint,
3679 &thermal_mitigation, 0644);
3680
Abhijeet Dharmapurikar28fd0c62012-01-27 13:28:29 -08003681static int set_usb_max_current(const char *val, struct kernel_param *kp)
3682{
3683 int ret, mA;
3684 struct pm8921_chg_chip *chip = the_chip;
3685
3686 ret = param_set_int(val, kp);
3687 if (ret) {
3688 pr_err("error setting value %d\n", ret);
3689 return ret;
3690 }
3691 if (chip) {
3692 pr_warn("setting current max to %d\n", usb_max_current);
3693 pm_chg_iusbmax_get(chip, &mA);
3694 if (mA > usb_max_current)
3695 pm8921_charger_vbus_draw(usb_max_current);
3696 return 0;
3697 }
3698 return -EINVAL;
3699}
David Keitelacf57c82012-03-07 18:48:50 -08003700module_param_call(usb_max_current, set_usb_max_current,
3701 param_get_uint, &usb_max_current, 0644);
Abhijeet Dharmapurikar28fd0c62012-01-27 13:28:29 -08003702
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003703static void free_irqs(struct pm8921_chg_chip *chip)
3704{
3705 int i;
3706
3707 for (i = 0; i < PM_CHG_MAX_INTS; i++)
3708 if (chip->pmic_chg_irq[i]) {
3709 free_irq(chip->pmic_chg_irq[i], chip);
3710 chip->pmic_chg_irq[i] = 0;
3711 }
3712}
3713
David Keitel88e1b572012-01-11 11:57:14 -08003714/* determines the initial present states */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003715static void __devinit determine_initial_state(struct pm8921_chg_chip *chip)
3716{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003717 int fsm_state;
Abhijeet Dharmapurikar7f1d97f2012-09-17 16:59:36 -07003718 int is_fast_chg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003719
3720 chip->dc_present = !!is_dc_chg_plugged_in(chip);
3721 chip->usb_present = !!is_usb_chg_plugged_in(chip);
3722
3723 notify_usb_of_the_plugin_event(chip->usb_present);
Abhijeet Dharmapurikar8c0c8442012-09-17 16:54:07 -07003724 if (chip->usb_present || chip->dc_present) {
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08003725 schedule_delayed_work(&chip->unplug_check_work,
Abhijeet Dharmapurikarb73001a2012-11-25 22:56:52 -08003726 msecs_to_jiffies(UNPLUG_CHECK_WAIT_PERIOD_MS));
David Keitel8c5201a2012-02-24 16:08:54 -08003727 pm8921_chg_enable_irq(chip, CHG_GONE_IRQ);
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08003728 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003729
3730 pm8921_chg_enable_irq(chip, DCIN_VALID_IRQ);
3731 pm8921_chg_enable_irq(chip, USBIN_VALID_IRQ);
3732 pm8921_chg_enable_irq(chip, BATT_REMOVED_IRQ);
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07003733 pm8921_chg_enable_irq(chip, BATT_INSERTED_IRQ);
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07003734 pm8921_chg_enable_irq(chip, DCIN_OV_IRQ);
3735 pm8921_chg_enable_irq(chip, DCIN_UV_IRQ);
David Keitel753ec8d2011-11-02 10:56:37 -07003736 pm8921_chg_enable_irq(chip, CHGFAIL_IRQ);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003737 pm8921_chg_enable_irq(chip, FASTCHG_IRQ);
3738 pm8921_chg_enable_irq(chip, VBATDET_LOW_IRQ);
David Keitel52fda532011-11-10 10:43:44 -08003739 pm8921_chg_enable_irq(chip, BAT_TEMP_OK_IRQ);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003740
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08003741 if (get_prop_batt_present(the_chip) || is_dc_chg_plugged_in(the_chip))
3742 if (usb_chg_current)
3743 /*
3744 * Reissue a vbus draw call only if a battery
3745 * or DC is present. We don't want to brown out the
3746 * device if usb is its only source
3747 */
3748 __pm8921_charger_vbus_draw(usb_chg_current);
3749 usb_chg_current = 0;
3750
Abhijeet Dharmapurikar7f1d97f2012-09-17 16:59:36 -07003751 /*
3752 * The bootloader could have started charging, a fastchg interrupt
3753 * might not happen. Check the real time status and if it is fast
3754 * charging invoke the handler so that the eoc worker could be
3755 * started
3756 */
3757 is_fast_chg = pm_chg_get_rt_status(chip, FASTCHG_IRQ);
3758 if (is_fast_chg)
3759 fastchg_irq_handler(chip->pmic_chg_irq[FASTCHG_IRQ], chip);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003760
3761 fsm_state = pm_chg_get_fsm_state(chip);
3762 if (is_battery_charging(fsm_state)) {
3763 chip->bms_notify.is_charging = 1;
3764 pm8921_bms_charging_began();
3765 }
3766
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07003767 check_battery_valid(chip);
3768
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003769 pr_debug("usb = %d, dc = %d batt = %d state=%d\n",
3770 chip->usb_present,
3771 chip->dc_present,
3772 get_prop_batt_present(chip),
3773 fsm_state);
David Keitel1e3a82f2012-06-26 16:27:15 -07003774
3775 /* Determine which USB trim column to use */
3776 if (pm8xxx_get_version(chip->dev->parent) == PM8XXX_VERSION_8917)
3777 chip->usb_trim_table = usb_trim_8917_table;
3778 else if (pm8xxx_get_version(chip->dev->parent) == PM8XXX_VERSION_8038)
3779 chip->usb_trim_table = usb_trim_8038_table;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003780}
3781
3782struct pm_chg_irq_init_data {
3783 unsigned int irq_id;
3784 char *name;
3785 unsigned long flags;
3786 irqreturn_t (*handler)(int, void *);
3787};
3788
3789#define CHG_IRQ(_id, _flags, _handler) \
3790{ \
3791 .irq_id = _id, \
3792 .name = #_id, \
3793 .flags = _flags, \
3794 .handler = _handler, \
3795}
3796struct pm_chg_irq_init_data chg_irq_data[] = {
3797 CHG_IRQ(USBIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3798 usbin_valid_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003799 CHG_IRQ(BATT_INSERTED_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3800 batt_inserted_irq_handler),
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07003801 CHG_IRQ(VBATDET_LOW_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3802 vbatdet_low_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003803 CHG_IRQ(CHGWDOG_IRQ, IRQF_TRIGGER_RISING, chgwdog_irq_handler),
3804 CHG_IRQ(VCP_IRQ, IRQF_TRIGGER_RISING, vcp_irq_handler),
3805 CHG_IRQ(ATCDONE_IRQ, IRQF_TRIGGER_RISING, atcdone_irq_handler),
3806 CHG_IRQ(ATCFAIL_IRQ, IRQF_TRIGGER_RISING, atcfail_irq_handler),
3807 CHG_IRQ(CHGDONE_IRQ, IRQF_TRIGGER_RISING, chgdone_irq_handler),
3808 CHG_IRQ(CHGFAIL_IRQ, IRQF_TRIGGER_RISING, chgfail_irq_handler),
3809 CHG_IRQ(CHGSTATE_IRQ, IRQF_TRIGGER_RISING, chgstate_irq_handler),
3810 CHG_IRQ(LOOP_CHANGE_IRQ, IRQF_TRIGGER_RISING, loop_change_irq_handler),
Abhijeet Dharmapurikar19e38132011-11-03 12:03:36 -07003811 CHG_IRQ(FASTCHG_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3812 fastchg_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003813 CHG_IRQ(TRKLCHG_IRQ, IRQF_TRIGGER_RISING, trklchg_irq_handler),
3814 CHG_IRQ(BATT_REMOVED_IRQ, IRQF_TRIGGER_RISING,
3815 batt_removed_irq_handler),
3816 CHG_IRQ(BATTTEMP_HOT_IRQ, IRQF_TRIGGER_RISING,
3817 batttemp_hot_irq_handler),
3818 CHG_IRQ(CHGHOT_IRQ, IRQF_TRIGGER_RISING, chghot_irq_handler),
3819 CHG_IRQ(BATTTEMP_COLD_IRQ, IRQF_TRIGGER_RISING,
3820 batttemp_cold_irq_handler),
David Keitel8c5201a2012-02-24 16:08:54 -08003821 CHG_IRQ(CHG_GONE_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3822 chg_gone_irq_handler),
Abhijeet Dharmapurikarf2471052011-09-25 22:18:36 -07003823 CHG_IRQ(BAT_TEMP_OK_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3824 bat_temp_ok_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003825 CHG_IRQ(COARSE_DET_LOW_IRQ, IRQF_TRIGGER_RISING,
3826 coarse_det_low_irq_handler),
3827 CHG_IRQ(VDD_LOOP_IRQ, IRQF_TRIGGER_RISING, vdd_loop_irq_handler),
3828 CHG_IRQ(VREG_OV_IRQ, IRQF_TRIGGER_RISING, vreg_ov_irq_handler),
3829 CHG_IRQ(VBATDET_IRQ, IRQF_TRIGGER_RISING, vbatdet_irq_handler),
3830 CHG_IRQ(BATFET_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3831 batfet_irq_handler),
3832 CHG_IRQ(DCIN_VALID_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3833 dcin_valid_irq_handler),
3834 CHG_IRQ(DCIN_OV_IRQ, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
3835 dcin_ov_irq_handler),
Abhijeet Dharmapurikar956215b2011-10-19 15:30:14 -07003836 CHG_IRQ(DCIN_UV_IRQ, IRQF_TRIGGER_RISING, dcin_uv_irq_handler),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003837};
3838
3839static int __devinit request_irqs(struct pm8921_chg_chip *chip,
3840 struct platform_device *pdev)
3841{
3842 struct resource *res;
3843 int ret, i;
3844
3845 ret = 0;
3846 bitmap_fill(chip->enabled_irqs, PM_CHG_MAX_INTS);
3847
3848 for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) {
3849 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
3850 chg_irq_data[i].name);
3851 if (res == NULL) {
3852 pr_err("couldn't find %s\n", chg_irq_data[i].name);
3853 goto err_out;
3854 }
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003855 chip->pmic_chg_irq[chg_irq_data[i].irq_id] = res->start;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003856 ret = request_irq(res->start, chg_irq_data[i].handler,
3857 chg_irq_data[i].flags,
3858 chg_irq_data[i].name, chip);
3859 if (ret < 0) {
3860 pr_err("couldn't request %d (%s) %d\n", res->start,
3861 chg_irq_data[i].name, ret);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07003862 chip->pmic_chg_irq[chg_irq_data[i].irq_id] = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003863 goto err_out;
3864 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003865 pm8921_chg_disable_irq(chip, chg_irq_data[i].irq_id);
3866 }
3867 return 0;
3868
3869err_out:
3870 free_irqs(chip);
3871 return -EINVAL;
3872}
3873
Abhijeet Dharmapurikarbba40912013-03-01 18:25:05 -08003874static void pm8921_chg_force_19p2mhz_clk(struct pm8921_chg_chip *chip)
3875{
3876 int err;
3877 u8 temp;
3878
3879 temp = 0xD1;
3880 err = pm_chg_write(chip, CHG_TEST, temp);
3881 if (err) {
3882 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
3883 return;
3884 }
3885
3886 temp = 0xD3;
3887 err = pm_chg_write(chip, CHG_TEST, temp);
3888 if (err) {
3889 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
3890 return;
3891 }
3892
3893 temp = 0xD1;
3894 err = pm_chg_write(chip, CHG_TEST, temp);
3895 if (err) {
3896 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
3897 return;
3898 }
3899
3900 temp = 0xD5;
3901 err = pm_chg_write(chip, CHG_TEST, temp);
3902 if (err) {
3903 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
3904 return;
3905 }
3906
3907 udelay(183);
3908
3909 temp = 0xD1;
3910 err = pm_chg_write(chip, CHG_TEST, temp);
3911 if (err) {
3912 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
3913 return;
3914 }
3915
3916 temp = 0xD0;
3917 err = pm_chg_write(chip, CHG_TEST, temp);
3918 if (err) {
3919 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
3920 return;
3921 }
3922 udelay(32);
3923
3924 temp = 0xD1;
3925 err = pm_chg_write(chip, CHG_TEST, temp);
3926 if (err) {
3927 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
3928 return;
3929 }
3930
3931 temp = 0xD3;
3932 err = pm_chg_write(chip, CHG_TEST, temp);
3933 if (err) {
3934 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
3935 return;
3936 }
3937}
3938
3939static void pm8921_chg_set_hw_clk_switching(struct pm8921_chg_chip *chip)
3940{
3941 int err;
3942 u8 temp;
3943
3944 temp = 0xD1;
3945 err = pm_chg_write(chip, CHG_TEST, temp);
3946 if (err) {
3947 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
3948 return;
3949 }
3950
3951 temp = 0xD0;
3952 err = pm_chg_write(chip, CHG_TEST, temp);
3953 if (err) {
3954 pr_err("Error %d writing %d to addr %d\n", err, temp, CHG_TEST);
3955 return;
3956 }
3957}
3958
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07003959#define VREF_BATT_THERM_FORCE_ON BIT(7)
3960static void detect_battery_removal(struct pm8921_chg_chip *chip)
3961{
3962 u8 temp;
3963
3964 pm8xxx_readb(chip->dev->parent, CHG_CNTRL, &temp);
3965 pr_debug("upon restart CHG_CNTRL = 0x%x\n", temp);
3966
3967 if (!(temp & VREF_BATT_THERM_FORCE_ON))
3968 /*
3969 * batt therm force on bit is battery backed and is default 0
3970 * The charger sets this bit at init time. If this bit is found
3971 * 0 that means the battery was removed. Tell the bms about it
3972 */
3973 pm8921_bms_invalidate_shutdown_soc();
3974}
3975
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003976#define ENUM_TIMER_STOP_BIT BIT(1)
3977#define BOOT_DONE_BIT BIT(6)
3978#define CHG_BATFET_ON_BIT BIT(3)
3979#define CHG_VCP_EN BIT(0)
3980#define CHG_BAT_TEMP_DIS_BIT BIT(2)
3981#define SAFE_CURRENT_MA 1500
David Keitel913c55e2012-10-01 11:35:33 -07003982#define PM_SUB_REV 0x001
Abhijeet Dharmapurikar77b1f6b2012-10-26 11:01:27 -07003983#define MIN_CHARGE_CURRENT_MA 350
3984#define DEFAULT_SAFETY_MINUTES 500
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003985static int __devinit pm8921_chg_hw_init(struct pm8921_chg_chip *chip)
3986{
David Keitel913c55e2012-10-01 11:35:33 -07003987 u8 subrev;
David Keitel663abad2012-09-04 21:00:27 -07003988 int rc, vdd_safe, fcc_uah, safety_time = DEFAULT_SAFETY_MINUTES;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003989
Abhijeet Dharmapurikarbba40912013-03-01 18:25:05 -08003990 /* forcing 19p2mhz before accessing any charger registers */
3991 pm8921_chg_force_19p2mhz_clk(chip);
Abhijeet Dharmapurikarc3695de2012-06-22 14:50:23 -07003992
Abhijeet Dharmapurikared2d87e2012-06-26 20:15:36 -07003993 detect_battery_removal(chip);
3994
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003995 rc = pm_chg_masked_write(chip, SYS_CONFIG_2,
Anirudh Ghayal953e63d2013-01-16 16:22:02 +05303996 BOOT_DONE_BIT, BOOT_DONE_BIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003997 if (rc) {
3998 pr_err("Failed to set BOOT_DONE_BIT rc=%d\n", rc);
3999 return rc;
4000 }
4001
Abhijeet Dharmapurikar40491ce2011-11-07 11:38:54 -08004002 vdd_safe = chip->max_voltage_mv + VDD_MAX_INCREASE_MV;
4003
4004 if (vdd_safe > PM8921_CHG_VDDSAFE_MAX)
4005 vdd_safe = PM8921_CHG_VDDSAFE_MAX;
4006
4007 rc = pm_chg_vddsafe_set(chip, vdd_safe);
4008
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004009 if (rc) {
4010 pr_err("Failed to set safe voltage to %d rc=%d\n",
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08004011 chip->max_voltage_mv, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004012 return rc;
4013 }
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07004014 rc = pm_chg_vbatdet_set(chip,
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08004015 chip->max_voltage_mv
4016 - chip->resume_voltage_delta);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004017 if (rc) {
4018 pr_err("Failed to set vbatdet comprator voltage to %d rc=%d\n",
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08004019 chip->max_voltage_mv - chip->resume_voltage_delta, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004020 return rc;
4021 }
4022
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08004023 rc = pm_chg_vddmax_set(chip, chip->max_voltage_mv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004024 if (rc) {
4025 pr_err("Failed to set max voltage to %d rc=%d\n",
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08004026 chip->max_voltage_mv, rc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004027 return rc;
4028 }
Abhijeet Dharmapurikara8f35422012-10-19 10:50:36 -07004029
4030 if (chip->safe_current_ma == 0)
4031 chip->safe_current_ma = SAFE_CURRENT_MA;
4032
4033 rc = pm_chg_ibatsafe_set(chip, chip->safe_current_ma);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004034 if (rc) {
4035 pr_err("Failed to set max voltage to %d rc=%d\n",
4036 SAFE_CURRENT_MA, rc);
4037 return rc;
4038 }
4039
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07004040 rc = pm_chg_ibatmax_set(chip, chip->max_bat_chg_current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004041 if (rc) {
4042 pr_err("Failed to set max current to 400 rc=%d\n", rc);
4043 return rc;
4044 }
4045
4046 rc = pm_chg_iterm_set(chip, chip->term_current);
4047 if (rc) {
4048 pr_err("Failed to set term current to %d rc=%d\n",
4049 chip->term_current, rc);
4050 return rc;
4051 }
4052
4053 /* Disable the ENUM TIMER */
4054 rc = pm_chg_masked_write(chip, PBL_ACCESS2, ENUM_TIMER_STOP_BIT,
4055 ENUM_TIMER_STOP_BIT);
4056 if (rc) {
4057 pr_err("Failed to set enum timer stop rc=%d\n", rc);
4058 return rc;
4059 }
4060
Abhijeet Dharmapurikar77b1f6b2012-10-26 11:01:27 -07004061 fcc_uah = pm8921_bms_get_fcc();
4062 if (fcc_uah > 0) {
4063 safety_time = div_s64((s64)fcc_uah * 60,
4064 1000 * MIN_CHARGE_CURRENT_MA);
4065 /* add 20 minutes of buffer time */
4066 safety_time += 20;
David Keitel2083d592012-12-07 15:13:17 -08004067
4068 /* make sure we do not exceed the maximum programmable time */
4069 if (safety_time > PM8921_CHG_TCHG_MAX)
4070 safety_time = PM8921_CHG_TCHG_MAX;
Abhijeet Dharmapurikar77b1f6b2012-10-26 11:01:27 -07004071 }
4072
4073 rc = pm_chg_tchg_max_set(chip, safety_time);
4074 if (rc) {
4075 pr_err("Failed to set max time to %d minutes rc=%d\n",
4076 safety_time, rc);
4077 return rc;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07004078 }
4079
4080 if (chip->ttrkl_time != 0) {
Abhijeet Dharmapurikare45c3d32011-10-19 19:46:48 -07004081 rc = pm_chg_ttrkl_max_set(chip, chip->ttrkl_time);
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07004082 if (rc) {
4083 pr_err("Failed to set trkl time to %d minutes rc=%d\n",
Abhijeet Dharmapurikar77b1f6b2012-10-26 11:01:27 -07004084 chip->ttrkl_time, rc);
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07004085 return rc;
4086 }
4087 }
4088
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -07004089 if (chip->vin_min != 0) {
4090 rc = pm_chg_vinmin_set(chip, chip->vin_min);
4091 if (rc) {
4092 pr_err("Failed to set vin min to %d mV rc=%d\n",
4093 chip->vin_min, rc);
4094 return rc;
4095 }
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08004096 } else {
4097 chip->vin_min = pm_chg_vinmin_get(chip);
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -07004098 }
4099
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004100 rc = pm_chg_disable_wd(chip);
4101 if (rc) {
4102 pr_err("Failed to disable wd rc=%d\n", rc);
4103 return rc;
4104 }
4105
4106 rc = pm_chg_masked_write(chip, CHG_CNTRL_2,
4107 CHG_BAT_TEMP_DIS_BIT, 0);
4108 if (rc) {
4109 pr_err("Failed to enable temp control chg rc=%d\n", rc);
4110 return rc;
4111 }
4112 /* switch to a 3.2Mhz for the buck */
David Keitel3567bec2012-06-26 16:35:53 -07004113 if (pm8xxx_get_revision(chip->dev->parent) >= PM8XXX_REVISION_8038_1p0)
David Keiteladf790e2012-11-28 12:56:55 -08004114 rc = pm_chg_write(chip,
David Keitel3567bec2012-06-26 16:35:53 -07004115 CHG_BUCK_CLOCK_CTRL_8038, 0x15);
4116 else
David Keiteladf790e2012-11-28 12:56:55 -08004117 rc = pm_chg_write(chip,
David Keitel3567bec2012-06-26 16:35:53 -07004118 CHG_BUCK_CLOCK_CTRL, 0x15);
4119
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004120 if (rc) {
4121 pr_err("Failed to switch buck clk rc=%d\n", rc);
4122 return rc;
4123 }
4124
Abhijeet Dharmapurikar26cef9c2011-08-25 19:16:42 -07004125 if (chip->trkl_voltage != 0) {
4126 rc = pm_chg_vtrkl_low_set(chip, chip->trkl_voltage);
4127 if (rc) {
4128 pr_err("Failed to set trkl voltage to %dmv rc=%d\n",
4129 chip->trkl_voltage, rc);
4130 return rc;
4131 }
4132 }
4133
4134 if (chip->weak_voltage != 0) {
4135 rc = pm_chg_vweak_set(chip, chip->weak_voltage);
4136 if (rc) {
4137 pr_err("Failed to set weak voltage to %dmv rc=%d\n",
4138 chip->weak_voltage, rc);
4139 return rc;
4140 }
4141 }
4142
4143 if (chip->trkl_current != 0) {
4144 rc = pm_chg_itrkl_set(chip, chip->trkl_current);
4145 if (rc) {
4146 pr_err("Failed to set trkl current to %dmA rc=%d\n",
4147 chip->trkl_voltage, rc);
4148 return rc;
4149 }
4150 }
4151
4152 if (chip->weak_current != 0) {
4153 rc = pm_chg_iweak_set(chip, chip->weak_current);
4154 if (rc) {
4155 pr_err("Failed to set weak current to %dmA rc=%d\n",
4156 chip->weak_current, rc);
4157 return rc;
4158 }
4159 }
4160
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -07004161 rc = pm_chg_batt_cold_temp_config(chip, chip->cold_thr);
4162 if (rc) {
4163 pr_err("Failed to set cold config %d rc=%d\n",
4164 chip->cold_thr, rc);
4165 }
4166
4167 rc = pm_chg_batt_hot_temp_config(chip, chip->hot_thr);
4168 if (rc) {
4169 pr_err("Failed to set hot config %d rc=%d\n",
4170 chip->hot_thr, rc);
4171 }
4172
Jay Chokshid674a512012-03-15 14:06:04 -07004173 rc = pm_chg_led_src_config(chip, chip->led_src_config);
4174 if (rc) {
4175 pr_err("Failed to set charger LED src config %d rc=%d\n",
4176 chip->led_src_config, rc);
4177 }
4178
David Keitelb51995e2011-11-18 17:03:31 -08004179 /* Workarounds for die 3.0 */
David Keitel0a6ff482012-08-10 17:14:50 -07004180 if (pm8xxx_get_revision(chip->dev->parent) == PM8XXX_REVISION_8921_3p0
David Keitel913c55e2012-10-01 11:35:33 -07004181 && pm8xxx_get_version(chip->dev->parent) == PM8XXX_VERSION_8921) {
4182 rc = pm8xxx_readb(chip->dev->parent, PM_SUB_REV, &subrev);
4183 if (rc) {
4184 pr_err("read failed: addr=%03X, rc=%d\n",
4185 PM_SUB_REV, rc);
4186 return rc;
4187 }
4188 /* Check if die 3.0.1 is present */
David Keitelfc8a7422012-10-31 17:52:32 -07004189 if (subrev & 0x1)
David Keitel663abad2012-09-04 21:00:27 -07004190 pm_chg_write(chip, CHG_BUCK_CTRL_TEST3, 0xA4);
David Keitel913c55e2012-10-01 11:35:33 -07004191 else
David Keitel663abad2012-09-04 21:00:27 -07004192 pm_chg_write(chip, CHG_BUCK_CTRL_TEST3, 0xAC);
David Keitel913c55e2012-10-01 11:35:33 -07004193 }
David Keitelb51995e2011-11-18 17:03:31 -08004194
David Keitel0789fc62012-06-07 17:43:27 -07004195 if (pm8xxx_get_version(chip->dev->parent) == PM8XXX_VERSION_8917) {
David Keitel6e86ba92012-12-20 17:50:31 -08004196 /* Set PM8917 USB_OVP debounce time to 15 ms */
4197 rc = pm_chg_masked_write(chip, USB_OVP_CONTROL,
4198 OVP_DEBOUNCE_TIME, 0x6);
4199 if (rc) {
4200 pr_err("Failed to set USB OVP db rc=%d\n", rc);
4201 return rc;
4202 }
4203
4204 /* Enable isub_fine resolution AICL for PM8917 */
David Keitel38bdd4f2012-04-19 15:39:13 -07004205 chip->iusb_fine_res = true;
David Keitel663abad2012-09-04 21:00:27 -07004206 if (chip->uvd_voltage_mv) {
David Keitel0789fc62012-06-07 17:43:27 -07004207 rc = pm_chg_uvd_threshold_set(chip,
4208 chip->uvd_voltage_mv);
4209 if (rc) {
4210 pr_err("Failed to set UVD threshold %drc=%d\n",
4211 chip->uvd_voltage_mv, rc);
David Keitel663abad2012-09-04 21:00:27 -07004212 return rc;
4213 }
David Keitel0789fc62012-06-07 17:43:27 -07004214 }
4215 }
David Keitel38bdd4f2012-04-19 15:39:13 -07004216
David Keiteladf790e2012-11-28 12:56:55 -08004217 pm_chg_write(chip, CHG_BUCK_CTRL_TEST3, 0xD9);
David Keitelb51995e2011-11-18 17:03:31 -08004218
David Keitela3c0d822011-11-03 14:18:52 -07004219 /* Disable EOC FSM processing */
David Keiteladf790e2012-11-28 12:56:55 -08004220 pm_chg_write(chip, CHG_BUCK_CTRL_TEST3, 0x91);
David Keitela3c0d822011-11-03 14:18:52 -07004221
Abhijeet Dharmapurikarccac6952012-01-18 16:35:34 -08004222 rc = pm_chg_masked_write(chip, CHG_CNTRL, VREF_BATT_THERM_FORCE_ON,
4223 VREF_BATT_THERM_FORCE_ON);
4224 if (rc)
4225 pr_err("Failed to Force Vref therm rc=%d\n", rc);
4226
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004227 rc = pm_chg_charge_dis(chip, charging_disabled);
4228 if (rc) {
4229 pr_err("Failed to disable CHG_CHARGE_DIS bit rc=%d\n", rc);
4230 return rc;
4231 }
4232
4233 rc = pm_chg_auto_enable(chip, !charging_disabled);
4234 if (rc) {
4235 pr_err("Failed to enable charging rc=%d\n", rc);
4236 return rc;
4237 }
4238
4239 return 0;
4240}
4241
4242static int get_rt_status(void *data, u64 * val)
4243{
4244 int i = (int)data;
4245 int ret;
4246
4247 /* global irq number is passed in via data */
4248 ret = pm_chg_get_rt_status(the_chip, i);
4249 *val = ret;
4250 return 0;
4251}
4252DEFINE_SIMPLE_ATTRIBUTE(rt_fops, get_rt_status, NULL, "%llu\n");
4253
4254static int get_fsm_status(void *data, u64 * val)
4255{
4256 u8 temp;
4257
4258 temp = pm_chg_get_fsm_state(the_chip);
4259 *val = temp;
4260 return 0;
4261}
4262DEFINE_SIMPLE_ATTRIBUTE(fsm_fops, get_fsm_status, NULL, "%llu\n");
4263
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07004264static int get_reg_loop(void *data, u64 * val)
4265{
4266 u8 temp;
4267
4268 if (!the_chip) {
4269 pr_err("%s called before init\n", __func__);
4270 return -EINVAL;
4271 }
4272 temp = pm_chg_get_regulation_loop(the_chip);
4273 *val = temp;
4274 return 0;
4275}
4276DEFINE_SIMPLE_ATTRIBUTE(reg_loop_fops, get_reg_loop, NULL, "0x%02llx\n");
4277
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004278static int get_reg(void *data, u64 * val)
4279{
4280 int addr = (int)data;
4281 int ret;
4282 u8 temp;
4283
4284 ret = pm8xxx_readb(the_chip->dev->parent, addr, &temp);
4285 if (ret) {
4286 pr_err("pm8xxx_readb to %x value =%d errored = %d\n",
4287 addr, temp, ret);
4288 return -EAGAIN;
4289 }
4290 *val = temp;
4291 return 0;
4292}
4293
4294static int set_reg(void *data, u64 val)
4295{
4296 int addr = (int)data;
4297 int ret;
4298 u8 temp;
4299
4300 temp = (u8) val;
David Keiteladf790e2012-11-28 12:56:55 -08004301 ret = pm_chg_write(the_chip, addr, temp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004302 if (ret) {
David Keiteladf790e2012-11-28 12:56:55 -08004303 pr_err("pm_chg_write to %x value =%d errored = %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004304 addr, temp, ret);
4305 return -EAGAIN;
4306 }
4307 return 0;
4308}
4309DEFINE_SIMPLE_ATTRIBUTE(reg_fops, get_reg, set_reg, "0x%02llx\n");
4310
Abhijeet Dharmapurikar3ee5b072012-10-15 17:47:29 -07004311static int reg_loop;
4312#define MAX_REG_LOOP_CHAR 10
4313static int get_reg_loop_param(char *buf, struct kernel_param *kp)
4314{
4315 u8 temp;
4316
4317 if (!the_chip) {
4318 pr_err("called before init\n");
4319 return -EINVAL;
4320 }
4321 temp = pm_chg_get_regulation_loop(the_chip);
4322 return snprintf(buf, MAX_REG_LOOP_CHAR, "%d", temp);
4323}
4324module_param_call(reg_loop, NULL, get_reg_loop_param,
4325 &reg_loop, 0644);
4326
4327static int max_chg_ma;
4328#define MAX_MA_CHAR 10
4329static int get_max_chg_ma_param(char *buf, struct kernel_param *kp)
4330{
4331 if (!the_chip) {
4332 pr_err("called before init\n");
4333 return -EINVAL;
4334 }
4335 return snprintf(buf, MAX_MA_CHAR, "%d", the_chip->max_bat_chg_current);
4336}
4337module_param_call(max_chg_ma, NULL, get_max_chg_ma_param,
4338 &max_chg_ma, 0644);
4339static int ibatmax_ma;
4340static int set_ibat_max(const char *val, struct kernel_param *kp)
4341{
4342 int rc;
4343
4344 if (!the_chip) {
4345 pr_err("called before init\n");
4346 return -EINVAL;
4347 }
4348
4349 rc = param_set_int(val, kp);
4350 if (rc) {
4351 pr_err("error setting value %d\n", rc);
4352 return rc;
4353 }
4354
4355 if (abs(ibatmax_ma - the_chip->max_bat_chg_current)
4356 <= the_chip->ibatmax_max_adj_ma) {
4357 rc = pm_chg_ibatmax_set(the_chip, ibatmax_ma);
4358 if (rc) {
4359 pr_err("Failed to set ibatmax rc = %d\n", rc);
4360 return rc;
4361 }
4362 }
4363
4364 return 0;
4365}
4366static int get_ibat_max(char *buf, struct kernel_param *kp)
4367{
4368 int ibat_ma;
4369 int rc;
4370
4371 if (!the_chip) {
4372 pr_err("called before init\n");
4373 return -EINVAL;
4374 }
4375
4376 rc = pm_chg_ibatmax_get(the_chip, &ibat_ma);
4377 if (rc) {
4378 pr_err("ibatmax_get error = %d\n", rc);
4379 return rc;
4380 }
4381
4382 return snprintf(buf, MAX_MA_CHAR, "%d", ibat_ma);
4383}
4384module_param_call(ibatmax_ma, set_ibat_max, get_ibat_max,
4385 &ibatmax_ma, 0644);
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07004386enum {
4387 BAT_WARM_ZONE,
4388 BAT_COOL_ZONE,
4389};
4390static int get_warm_cool(void *data, u64 * val)
4391{
4392 if (!the_chip) {
4393 pr_err("%s called before init\n", __func__);
4394 return -EINVAL;
4395 }
4396 if ((int)data == BAT_WARM_ZONE)
4397 *val = the_chip->is_bat_warm;
4398 if ((int)data == BAT_COOL_ZONE)
4399 *val = the_chip->is_bat_cool;
4400 return 0;
4401}
4402DEFINE_SIMPLE_ATTRIBUTE(warm_cool_fops, get_warm_cool, NULL, "0x%lld\n");
4403
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004404static void create_debugfs_entries(struct pm8921_chg_chip *chip)
4405{
4406 int i;
4407
4408 chip->dent = debugfs_create_dir("pm8921_chg", NULL);
4409
4410 if (IS_ERR(chip->dent)) {
4411 pr_err("pmic charger couldnt create debugfs dir\n");
4412 return;
4413 }
4414
4415 debugfs_create_file("CHG_CNTRL", 0644, chip->dent,
4416 (void *)CHG_CNTRL, &reg_fops);
4417 debugfs_create_file("CHG_CNTRL_2", 0644, chip->dent,
4418 (void *)CHG_CNTRL_2, &reg_fops);
4419 debugfs_create_file("CHG_CNTRL_3", 0644, chip->dent,
4420 (void *)CHG_CNTRL_3, &reg_fops);
4421 debugfs_create_file("PBL_ACCESS1", 0644, chip->dent,
4422 (void *)PBL_ACCESS1, &reg_fops);
4423 debugfs_create_file("PBL_ACCESS2", 0644, chip->dent,
4424 (void *)PBL_ACCESS2, &reg_fops);
4425 debugfs_create_file("SYS_CONFIG_1", 0644, chip->dent,
4426 (void *)SYS_CONFIG_1, &reg_fops);
4427 debugfs_create_file("SYS_CONFIG_2", 0644, chip->dent,
4428 (void *)SYS_CONFIG_2, &reg_fops);
4429 debugfs_create_file("CHG_VDD_MAX", 0644, chip->dent,
4430 (void *)CHG_VDD_MAX, &reg_fops);
4431 debugfs_create_file("CHG_VDD_SAFE", 0644, chip->dent,
4432 (void *)CHG_VDD_SAFE, &reg_fops);
4433 debugfs_create_file("CHG_VBAT_DET", 0644, chip->dent,
4434 (void *)CHG_VBAT_DET, &reg_fops);
4435 debugfs_create_file("CHG_IBAT_MAX", 0644, chip->dent,
4436 (void *)CHG_IBAT_MAX, &reg_fops);
4437 debugfs_create_file("CHG_IBAT_SAFE", 0644, chip->dent,
4438 (void *)CHG_IBAT_SAFE, &reg_fops);
4439 debugfs_create_file("CHG_VIN_MIN", 0644, chip->dent,
4440 (void *)CHG_VIN_MIN, &reg_fops);
4441 debugfs_create_file("CHG_VTRICKLE", 0644, chip->dent,
4442 (void *)CHG_VTRICKLE, &reg_fops);
4443 debugfs_create_file("CHG_ITRICKLE", 0644, chip->dent,
4444 (void *)CHG_ITRICKLE, &reg_fops);
4445 debugfs_create_file("CHG_ITERM", 0644, chip->dent,
4446 (void *)CHG_ITERM, &reg_fops);
4447 debugfs_create_file("CHG_TCHG_MAX", 0644, chip->dent,
4448 (void *)CHG_TCHG_MAX, &reg_fops);
4449 debugfs_create_file("CHG_TWDOG", 0644, chip->dent,
4450 (void *)CHG_TWDOG, &reg_fops);
4451 debugfs_create_file("CHG_TEMP_THRESH", 0644, chip->dent,
4452 (void *)CHG_TEMP_THRESH, &reg_fops);
4453 debugfs_create_file("CHG_COMP_OVR", 0644, chip->dent,
4454 (void *)CHG_COMP_OVR, &reg_fops);
4455 debugfs_create_file("CHG_BUCK_CTRL_TEST1", 0644, chip->dent,
4456 (void *)CHG_BUCK_CTRL_TEST1, &reg_fops);
4457 debugfs_create_file("CHG_BUCK_CTRL_TEST2", 0644, chip->dent,
4458 (void *)CHG_BUCK_CTRL_TEST2, &reg_fops);
4459 debugfs_create_file("CHG_BUCK_CTRL_TEST3", 0644, chip->dent,
4460 (void *)CHG_BUCK_CTRL_TEST3, &reg_fops);
4461 debugfs_create_file("CHG_TEST", 0644, chip->dent,
4462 (void *)CHG_TEST, &reg_fops);
4463
4464 debugfs_create_file("FSM_STATE", 0644, chip->dent, NULL,
4465 &fsm_fops);
4466
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07004467 debugfs_create_file("REGULATION_LOOP_CONTROL", 0644, chip->dent, NULL,
4468 &reg_loop_fops);
4469
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07004470 debugfs_create_file("BAT_WARM_ZONE", 0644, chip->dent,
4471 (void *)BAT_WARM_ZONE, &warm_cool_fops);
4472 debugfs_create_file("BAT_COOL_ZONE", 0644, chip->dent,
4473 (void *)BAT_COOL_ZONE, &warm_cool_fops);
4474
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004475 for (i = 0; i < ARRAY_SIZE(chg_irq_data); i++) {
4476 if (chip->pmic_chg_irq[chg_irq_data[i].irq_id])
4477 debugfs_create_file(chg_irq_data[i].name, 0444,
4478 chip->dent,
4479 (void *)chg_irq_data[i].irq_id,
4480 &rt_fops);
4481 }
4482}
4483
Abhijeet Dharmapurikarccac6952012-01-18 16:35:34 -08004484static int pm8921_charger_suspend_noirq(struct device *dev)
4485{
4486 int rc;
4487 struct pm8921_chg_chip *chip = dev_get_drvdata(dev);
4488
4489 rc = pm_chg_masked_write(chip, CHG_CNTRL, VREF_BATT_THERM_FORCE_ON, 0);
4490 if (rc)
4491 pr_err("Failed to Force Vref therm off rc=%d\n", rc);
Abhijeet Dharmapurikarbba40912013-03-01 18:25:05 -08004492
4493 rc = pm8921_chg_set_lpm(chip, 1);
4494 if (rc)
4495 pr_err("Failed to set lpm rc=%d\n", rc);
4496
4497 pm8921_chg_set_hw_clk_switching(chip);
4498
Abhijeet Dharmapurikarccac6952012-01-18 16:35:34 -08004499 return 0;
4500}
4501
4502static int pm8921_charger_resume_noirq(struct device *dev)
4503{
4504 int rc;
4505 struct pm8921_chg_chip *chip = dev_get_drvdata(dev);
4506
Abhijeet Dharmapurikarbba40912013-03-01 18:25:05 -08004507 pm8921_chg_force_19p2mhz_clk(chip);
David Keitel663abad2012-09-04 21:00:27 -07004508
Abhijeet Dharmapurikarbba40912013-03-01 18:25:05 -08004509 rc = pm8921_chg_set_lpm(chip, 0);
4510 if (rc)
4511 pr_err("Failed to set lpm rc=%d\n", rc);
Abhijeet Dharmapurikarccac6952012-01-18 16:35:34 -08004512
4513 rc = pm_chg_masked_write(chip, CHG_CNTRL, VREF_BATT_THERM_FORCE_ON,
4514 VREF_BATT_THERM_FORCE_ON);
4515 if (rc)
4516 pr_err("Failed to Force Vref therm on rc=%d\n", rc);
4517 return 0;
4518}
4519
David Keitelf2226022011-12-13 15:55:50 -08004520static int pm8921_charger_resume(struct device *dev)
4521{
David Keitelf2226022011-12-13 15:55:50 -08004522 struct pm8921_chg_chip *chip = dev_get_drvdata(dev);
4523
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08004524 if (pm8921_chg_is_enabled(chip, LOOP_CHANGE_IRQ)) {
4525 disable_irq_wake(chip->pmic_chg_irq[LOOP_CHANGE_IRQ]);
4526 pm8921_chg_disable_irq(chip, LOOP_CHANGE_IRQ);
4527 }
Anirudh Ghayalc5d11862012-12-19 16:55:29 +05304528
4529 if (chip->btc_override && (is_dc_chg_plugged_in(the_chip) ||
4530 is_usb_chg_plugged_in(the_chip)))
4531 schedule_delayed_work(&chip->btc_override_work, 0);
4532
David Keitelf2226022011-12-13 15:55:50 -08004533 return 0;
4534}
4535
4536static int pm8921_charger_suspend(struct device *dev)
4537{
David Keitelf2226022011-12-13 15:55:50 -08004538 struct pm8921_chg_chip *chip = dev_get_drvdata(dev);
4539
Anirudh Ghayalc5d11862012-12-19 16:55:29 +05304540 if (chip->btc_override)
4541 cancel_delayed_work_sync(&chip->btc_override_work);
4542
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08004543 if (is_usb_chg_plugged_in(chip)) {
4544 pm8921_chg_enable_irq(chip, LOOP_CHANGE_IRQ);
4545 enable_irq_wake(chip->pmic_chg_irq[LOOP_CHANGE_IRQ]);
4546 }
Abhijeet Dharmapurikarccac6952012-01-18 16:35:34 -08004547
David Keitelf2226022011-12-13 15:55:50 -08004548 return 0;
4549}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004550static int __devinit pm8921_charger_probe(struct platform_device *pdev)
4551{
4552 int rc = 0;
4553 struct pm8921_chg_chip *chip;
4554 const struct pm8921_charger_platform_data *pdata
4555 = pdev->dev.platform_data;
4556
4557 if (!pdata) {
4558 pr_err("missing platform data\n");
4559 return -EINVAL;
4560 }
4561
4562 chip = kzalloc(sizeof(struct pm8921_chg_chip),
4563 GFP_KERNEL);
4564 if (!chip) {
4565 pr_err("Cannot allocate pm_chg_chip\n");
4566 return -ENOMEM;
4567 }
4568
4569 chip->dev = &pdev->dev;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07004570 chip->ttrkl_time = pdata->ttrkl_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004571 chip->update_time = pdata->update_time;
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08004572 chip->max_voltage_mv = pdata->max_voltage;
David Keitel8b079fb2012-06-28 19:14:30 -07004573 chip->alarm_low_mv = pdata->alarm_low_mv;
4574 chip->alarm_high_mv = pdata->alarm_high_mv;
Siddartha Mohanadoss37e6fc02011-11-16 16:57:03 -08004575 chip->min_voltage_mv = pdata->min_voltage;
Abhijeet Dharmapurikara8f35422012-10-19 10:50:36 -07004576 chip->safe_current_ma = pdata->safe_current_ma;
David Keitel0789fc62012-06-07 17:43:27 -07004577 chip->uvd_voltage_mv = pdata->uvd_thresh_voltage;
Abhijeet Dharmapurikard55878e2011-10-27 10:22:24 -07004578 chip->resume_voltage_delta = pdata->resume_voltage_delta;
Xiaozhe Shi14e8fa12012-10-11 15:49:58 -07004579 chip->resume_charge_percent = pdata->resume_charge_percent;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004580 chip->term_current = pdata->term_current;
4581 chip->vbat_channel = pdata->charger_cdata.vbat_channel;
Abhijeet Dharmapurikarb24e2c32011-08-17 17:13:09 -07004582 chip->batt_temp_channel = pdata->charger_cdata.batt_temp_channel;
Abhijeet Dharmapurikarbe6bd8c2011-08-19 12:15:06 -07004583 chip->batt_id_channel = pdata->charger_cdata.batt_id_channel;
4584 chip->batt_id_min = pdata->batt_id_min;
4585 chip->batt_id_max = pdata->batt_id_max;
Abhijeet Dharmapurikarbc38f632012-01-10 09:30:11 -08004586 if (pdata->cool_temp != INT_MIN)
4587 chip->cool_temp_dc = pdata->cool_temp * 10;
4588 else
4589 chip->cool_temp_dc = INT_MIN;
4590
4591 if (pdata->warm_temp != INT_MIN)
4592 chip->warm_temp_dc = pdata->warm_temp * 10;
4593 else
4594 chip->warm_temp_dc = INT_MIN;
4595
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07004596 chip->temp_check_period = pdata->temp_check_period;
4597 chip->max_bat_chg_current = pdata->max_bat_chg_current;
David Keitel8e672752012-11-02 16:00:41 -07004598 /* Assign to corresponding module parameter */
4599 usb_max_current = pdata->usb_max_current;
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07004600 chip->cool_bat_chg_current = pdata->cool_bat_chg_current;
4601 chip->warm_bat_chg_current = pdata->warm_bat_chg_current;
4602 chip->cool_bat_voltage = pdata->cool_bat_voltage;
4603 chip->warm_bat_voltage = pdata->warm_bat_voltage;
Abhijeet Dharmapurikar26cef9c2011-08-25 19:16:42 -07004604 chip->trkl_voltage = pdata->trkl_voltage;
4605 chip->weak_voltage = pdata->weak_voltage;
4606 chip->trkl_current = pdata->trkl_current;
4607 chip->weak_current = pdata->weak_current;
Abhijeet Dharmapurikar6fe50a82011-08-25 21:33:14 -07004608 chip->vin_min = pdata->vin_min;
Abhijeet Dharmapurikarad742362011-08-29 19:50:02 -07004609 chip->thermal_mitigation = pdata->thermal_mitigation;
4610 chip->thermal_levels = pdata->thermal_levels;
Anirudh Ghayal80c729b2013-02-22 11:17:19 +05304611 chip->disable_chg_rmvl_wrkarnd = pdata->disable_chg_rmvl_wrkarnd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004612
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -07004613 chip->cold_thr = pdata->cold_thr;
4614 chip->hot_thr = pdata->hot_thr;
Abhijeet Dharmapurikar40491ce2011-11-07 11:38:54 -08004615 chip->rconn_mohm = pdata->rconn_mohm;
Jay Chokshid674a512012-03-15 14:06:04 -07004616 chip->led_src_config = pdata->led_src_config;
David Keitela3cf9432012-08-24 15:11:23 -07004617 chip->has_dc_supply = pdata->has_dc_supply;
Abhijeet Dharmapurikarbbaef592012-10-10 10:07:51 -07004618 chip->battery_less_hardware = pdata->battery_less_hardware;
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -08004619 chip->btc_override = pdata->btc_override;
4620 if (chip->btc_override) {
4621 chip->btc_delay_ms = pdata->btc_delay_ms;
4622 chip->btc_override_cold_decidegc
4623 = pdata->btc_override_cold_degc * 10;
4624 chip->btc_override_hot_decidegc
4625 = pdata->btc_override_hot_degc * 10;
4626 chip->btc_panic_if_cant_stop_chg
4627 = pdata->btc_panic_if_cant_stop_chg;
4628 }
Abhijeet Dharmapurikarbbaef592012-10-10 10:07:51 -07004629
4630 if (chip->battery_less_hardware)
4631 charging_disabled = 1;
Abhijeet Dharmapurikar0f024232011-10-03 12:00:15 -07004632
Abhijeet Dharmapurikar3ee5b072012-10-15 17:47:29 -07004633 chip->ibatmax_max_adj_ma = find_ibat_max_adj_ma(
4634 chip->max_bat_chg_current);
4635
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004636 rc = pm8921_chg_hw_init(chip);
4637 if (rc) {
4638 pr_err("couldn't init hardware rc=%d\n", rc);
4639 goto free_chip;
4640 }
4641
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -08004642 if (chip->btc_override)
4643 pm8921_chg_btc_override_init(chip);
4644
Abhijeet Dharmapurikarf8a66e92012-12-13 15:46:01 -08004645 chip->stop_chg_upon_expiry = pdata->stop_chg_upon_expiry;
Anirudh Ghayal1e5bb372013-02-20 09:48:42 +05304646 chip->usb_type = POWER_SUPPLY_TYPE_UNKNOWN;
Abhijeet Dharmapurikarf8a66e92012-12-13 15:46:01 -08004647
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08004648 chip->usb_psy.name = "usb";
4649 chip->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4650 chip->usb_psy.supplied_to = pm_power_supplied_to;
4651 chip->usb_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to);
4652 chip->usb_psy.properties = pm_power_props_usb;
4653 chip->usb_psy.num_properties = ARRAY_SIZE(pm_power_props_usb);
4654 chip->usb_psy.get_property = pm_power_get_property_usb;
4655 chip->usb_psy.set_property = pm_power_set_property_usb;
4656 chip->usb_psy.property_is_writeable = usb_property_is_writeable;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004657
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08004658 chip->dc_psy.name = "pm8921-dc";
4659 chip->dc_psy.type = POWER_SUPPLY_TYPE_MAINS;
4660 chip->dc_psy.supplied_to = pm_power_supplied_to;
4661 chip->dc_psy.num_supplicants = ARRAY_SIZE(pm_power_supplied_to);
4662 chip->dc_psy.properties = pm_power_props_mains;
4663 chip->dc_psy.num_properties = ARRAY_SIZE(pm_power_props_mains);
4664 chip->dc_psy.get_property = pm_power_get_property_mains;
David Keitel6ed71a52012-01-30 12:42:18 -08004665
Abhijeet Dharmapurikard24aee82013-01-14 20:48:25 -08004666 chip->batt_psy.name = "battery";
4667 chip->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY;
4668 chip->batt_psy.properties = msm_batt_power_props;
4669 chip->batt_psy.num_properties = ARRAY_SIZE(msm_batt_power_props);
4670 chip->batt_psy.get_property = pm_batt_power_get_property;
4671 chip->batt_psy.external_power_changed = pm_batt_external_power_changed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004672 rc = power_supply_register(chip->dev, &chip->usb_psy);
4673 if (rc < 0) {
4674 pr_err("power_supply_register usb failed rc = %d\n", rc);
Abhijeet Dharmapurikar86eea302011-08-15 13:55:18 -07004675 goto free_chip;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004676 }
4677
David Keitel6ed71a52012-01-30 12:42:18 -08004678 rc = power_supply_register(chip->dev, &chip->dc_psy);
4679 if (rc < 0) {
4680 pr_err("power_supply_register usb failed rc = %d\n", rc);
4681 goto unregister_usb;
4682 }
4683
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004684 rc = power_supply_register(chip->dev, &chip->batt_psy);
4685 if (rc < 0) {
4686 pr_err("power_supply_register batt failed rc = %d\n", rc);
David Keitel6ed71a52012-01-30 12:42:18 -08004687 goto unregister_dc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004688 }
4689
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07004690 platform_set_drvdata(pdev, chip);
4691 the_chip = chip;
4692
4693 wake_lock_init(&chip->eoc_wake_lock, WAKE_LOCK_SUSPEND, "pm8921_eoc");
Amir Samuelovd31ef502011-10-26 14:41:36 +02004694 INIT_DELAYED_WORK(&chip->eoc_work, eoc_worker);
David Keitelacf57c82012-03-07 18:48:50 -08004695 INIT_DELAYED_WORK(&chip->vin_collapse_check_work,
4696 vin_collapse_check_worker);
Abhijeet Dharmapurikarc4599552011-12-22 17:27:56 -08004697 INIT_DELAYED_WORK(&chip->unplug_check_work, unplug_check_worker);
Abhijeet Dharmapurikar98738c52011-09-20 17:04:48 -07004698
Abhijeet Dharmapurikarcf5bb9f2012-10-15 13:57:59 -07004699 INIT_WORK(&chip->bms_notify.work, bms_notify);
4700 INIT_WORK(&chip->battery_id_valid_work, battery_id_valid);
4701
4702 INIT_DELAYED_WORK(&chip->update_heartbeat_work, update_heartbeat);
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -08004703 INIT_DELAYED_WORK(&chip->btc_override_work, btc_override_worker);
Abhijeet Dharmapurikarcf5bb9f2012-10-15 13:57:59 -07004704
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004705 rc = request_irqs(chip, pdev);
4706 if (rc) {
4707 pr_err("couldn't register interrupts rc=%d\n", rc);
4708 goto unregister_batt;
4709 }
4710
Abhijeet Dharmapurikare27d8092011-08-12 17:16:29 -07004711 enable_irq_wake(chip->pmic_chg_irq[USBIN_VALID_IRQ]);
David Keitel847ec162012-09-04 09:50:36 -07004712 enable_irq_wake(chip->pmic_chg_irq[DCIN_VALID_IRQ]);
David Keitel64dd3f52011-11-10 14:19:38 -08004713 enable_irq_wake(chip->pmic_chg_irq[VBATDET_LOW_IRQ]);
Abhijeet Dharmapurikar28fd0c62012-01-27 13:28:29 -08004714 enable_irq_wake(chip->pmic_chg_irq[FASTCHG_IRQ]);
Abhijeet Dharmapurikare27d8092011-08-12 17:16:29 -07004715
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004716 create_debugfs_entries(chip);
4717
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004718 /* determine what state the charger is in */
4719 determine_initial_state(chip);
4720
Abhijeet Dharmapurikarcf5bb9f2012-10-15 13:57:59 -07004721 if (chip->update_time)
Abhijeet Dharmapurikar33fe6fb2011-09-14 16:03:11 -07004722 schedule_delayed_work(&chip->update_heartbeat_work,
4723 round_jiffies_relative(msecs_to_jiffies
4724 (chip->update_time)));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004725 return 0;
4726
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004727unregister_batt:
Abhijeet Dharmapurikar2f45e2c2012-11-19 16:33:56 -08004728 wake_lock_destroy(&chip->eoc_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004729 power_supply_unregister(&chip->batt_psy);
David Keitel6ed71a52012-01-30 12:42:18 -08004730unregister_dc:
4731 power_supply_unregister(&chip->dc_psy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004732unregister_usb:
4733 power_supply_unregister(&chip->usb_psy);
4734free_chip:
4735 kfree(chip);
4736 return rc;
4737}
4738
4739static int __devexit pm8921_charger_remove(struct platform_device *pdev)
4740{
4741 struct pm8921_chg_chip *chip = platform_get_drvdata(pdev);
4742
4743 free_irqs(chip);
4744 platform_set_drvdata(pdev, NULL);
4745 the_chip = NULL;
4746 kfree(chip);
4747 return 0;
4748}
David Keitelf2226022011-12-13 15:55:50 -08004749static const struct dev_pm_ops pm8921_pm_ops = {
4750 .suspend = pm8921_charger_suspend,
Abhijeet Dharmapurikarccac6952012-01-18 16:35:34 -08004751 .suspend_noirq = pm8921_charger_suspend_noirq,
4752 .resume_noirq = pm8921_charger_resume_noirq,
David Keitelf2226022011-12-13 15:55:50 -08004753 .resume = pm8921_charger_resume,
4754};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004755static struct platform_driver pm8921_charger_driver = {
David Keitelf2226022011-12-13 15:55:50 -08004756 .probe = pm8921_charger_probe,
4757 .remove = __devexit_p(pm8921_charger_remove),
4758 .driver = {
4759 .name = PM8921_CHARGER_DEV_NAME,
4760 .owner = THIS_MODULE,
4761 .pm = &pm8921_pm_ops,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004762 },
4763};
4764
4765static int __init pm8921_charger_init(void)
4766{
4767 return platform_driver_register(&pm8921_charger_driver);
4768}
4769
4770static void __exit pm8921_charger_exit(void)
4771{
4772 platform_driver_unregister(&pm8921_charger_driver);
4773}
4774
4775late_initcall(pm8921_charger_init);
4776module_exit(pm8921_charger_exit);
4777
4778MODULE_LICENSE("GPL v2");
4779MODULE_DESCRIPTION("PMIC8921 charger/battery driver");
4780MODULE_VERSION("1.0");
4781MODULE_ALIAS("platform:" PM8921_CHARGER_DEV_NAME);