blob: 12aeed34bd4fa991b25de6aa85e46ff28538a36f [file] [log] [blame]
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001/*
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02002 * Copyright 2012 ST Ericsson.
3 *
Lee Jones2fa5b0f2013-01-23 14:33:47 +00004 * Power supply driver for ST Ericsson pm2xxx_charger charger
5 *
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/device.h>
14#include <linux/interrupt.h>
15#include <linux/delay.h>
16#include <linux/slab.h>
17#include <linux/platform_device.h>
18#include <linux/power_supply.h>
Michel JAOUEN01ec8c52012-04-26 10:00:04 +020019#include <linux/regulator/consumer.h>
20#include <linux/err.h>
21#include <linux/i2c.h>
22#include <linux/workqueue.h>
Michel JAOUEN01ec8c52012-04-26 10:00:04 +020023#include <linux/mfd/abx500/ab8500.h>
24#include <linux/mfd/abx500/ab8500-bm.h>
Michel JAOUEN01ec8c52012-04-26 10:00:04 +020025#include <linux/mfd/abx500/ux500_chargalg.h>
26#include <linux/pm2301_charger.h>
Lee Jones39880432013-01-23 14:38:15 +000027#include <linux/gpio.h>
Lee Jonesaee2b842013-02-25 14:28:59 +000028#include <linux/pm_runtime.h>
Michel JAOUEN01ec8c52012-04-26 10:00:04 +020029
Lee Jones2fa5b0f2013-01-23 14:33:47 +000030#include "pm2301_charger.h"
Michel JAOUEN01ec8c52012-04-26 10:00:04 +020031
32#define to_pm2xxx_charger_ac_device_info(x) container_of((x), \
33 struct pm2xxx_charger, ac_chg)
Mustapha Ben Zoubeir49fddee2012-06-27 15:40:58 +020034#define SLEEP_MIN 50
35#define SLEEP_MAX 100
Lee Jonesaee2b842013-02-25 14:28:59 +000036#define PM2XXX_AUTOSUSPEND_DELAY 500
Michel JAOUEN01ec8c52012-04-26 10:00:04 +020037
38static int pm2xxx_interrupt_registers[] = {
39 PM2XXX_REG_INT1,
40 PM2XXX_REG_INT2,
41 PM2XXX_REG_INT3,
42 PM2XXX_REG_INT4,
43 PM2XXX_REG_INT5,
44 PM2XXX_REG_INT6,
45};
46
Michel JAOUEN01ec8c52012-04-26 10:00:04 +020047static enum power_supply_property pm2xxx_charger_ac_props[] = {
48 POWER_SUPPLY_PROP_HEALTH,
49 POWER_SUPPLY_PROP_PRESENT,
50 POWER_SUPPLY_PROP_ONLINE,
Michel JAOUEN01ec8c52012-04-26 10:00:04 +020051 POWER_SUPPLY_PROP_VOLTAGE_AVG,
Michel JAOUEN01ec8c52012-04-26 10:00:04 +020052};
53
54static int pm2xxx_charger_voltage_map[] = {
55 3500,
56 3525,
57 3550,
58 3575,
59 3600,
60 3625,
61 3650,
62 3675,
63 3700,
64 3725,
65 3750,
66 3775,
67 3800,
68 3825,
69 3850,
70 3875,
71 3900,
72 3925,
73 3950,
74 3975,
75 4000,
76 4025,
77 4050,
78 4075,
79 4100,
80 4125,
81 4150,
82 4175,
83 4200,
84 4225,
85 4250,
86 4275,
87 4300,
88};
89
90static int pm2xxx_charger_current_map[] = {
91 200,
92 200,
93 400,
94 600,
95 800,
96 1000,
97 1200,
98 1400,
99 1600,
100 1800,
101 2000,
102 2200,
103 2400,
104 2600,
105 2800,
106 3000,
107};
108
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200109static const struct i2c_device_id pm2xxx_ident[] = {
110 { "pm2301", 0 },
111 { }
112};
113
Lee Jones39880432013-01-23 14:38:15 +0000114static void set_lpn_pin(struct pm2xxx_charger *pm2)
115{
lme00437a21e22f2012-11-13 14:43:48 +0100116 if (!pm2->ac.charger_connected && gpio_is_valid(pm2->lpn_pin)) {
117 gpio_set_value(pm2->lpn_pin, 1);
118 usleep_range(SLEEP_MIN, SLEEP_MAX);
119 }
Lee Jones39880432013-01-23 14:38:15 +0000120}
121
122static void clear_lpn_pin(struct pm2xxx_charger *pm2)
123{
lme00437a21e22f2012-11-13 14:43:48 +0100124 if (!pm2->ac.charger_connected && gpio_is_valid(pm2->lpn_pin))
125 gpio_set_value(pm2->lpn_pin, 0);
Lee Jones39880432013-01-23 14:38:15 +0000126}
127
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200128static int pm2xxx_reg_read(struct pm2xxx_charger *pm2, int reg, u8 *val)
129{
130 int ret;
Lee Jones39880432013-01-23 14:38:15 +0000131 /*
132 * When AC adaptor is unplugged, the host
133 * must put LPN high to be able to
134 * communicate by I2C with PM2301
135 * and receive I2C "acknowledge" from PM2301.
136 */
137 mutex_lock(&pm2->lock);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200138
139 ret = i2c_smbus_read_i2c_block_data(pm2->config.pm2xxx_i2c, reg,
140 1, val);
141 if (ret < 0)
142 dev_err(pm2->dev, "Error reading register at 0x%x\n", reg);
Rajkumar Kasirajane41f39e2012-05-28 15:57:33 +0530143 else
144 ret = 0;
Lee Jones39880432013-01-23 14:38:15 +0000145 mutex_unlock(&pm2->lock);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200146
147 return ret;
148}
149
150static int pm2xxx_reg_write(struct pm2xxx_charger *pm2, int reg, u8 val)
151{
152 int ret;
Lee Jones39880432013-01-23 14:38:15 +0000153 /*
154 * When AC adaptor is unplugged, the host
155 * must put LPN high to be able to
156 * communicate by I2C with PM2301
157 * and receive I2C "acknowledge" from PM2301.
158 */
159 mutex_lock(&pm2->lock);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200160
161 ret = i2c_smbus_write_i2c_block_data(pm2->config.pm2xxx_i2c, reg,
162 1, &val);
163 if (ret < 0)
164 dev_err(pm2->dev, "Error writing register at 0x%x\n", reg);
Rajkumar Kasirajane41f39e2012-05-28 15:57:33 +0530165 else
166 ret = 0;
Lee Jones39880432013-01-23 14:38:15 +0000167 mutex_unlock(&pm2->lock);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200168
169 return ret;
170}
171
172static int pm2xxx_charging_enable_mngt(struct pm2xxx_charger *pm2)
173{
174 int ret;
175
176 /* Enable charging */
177 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2,
178 (PM2XXX_CH_AUTO_RESUME_EN | PM2XXX_CHARGER_ENA));
179
180 return ret;
181}
182
183static int pm2xxx_charging_disable_mngt(struct pm2xxx_charger *pm2)
184{
185 int ret;
186
Rajkumar Kasirajan584f9702012-06-05 12:31:25 +0530187 /* Disable SW EOC ctrl */
188 ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG, PM2XXX_SWCTRL_HW);
189 if (ret < 0) {
190 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
191 return ret;
192 }
193
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200194 /* Disable charging */
195 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2,
196 (PM2XXX_CH_AUTO_RESUME_DIS | PM2XXX_CHARGER_DIS));
Rajkumar Kasirajan584f9702012-06-05 12:31:25 +0530197 if (ret < 0) {
198 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
199 return ret;
200 }
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200201
Rajkumar Kasirajan584f9702012-06-05 12:31:25 +0530202 return 0;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200203}
204
205static int pm2xxx_charger_batt_therm_mngt(struct pm2xxx_charger *pm2, int val)
206{
207 queue_work(pm2->charger_wq, &pm2->check_main_thermal_prot_work);
208
209 return 0;
210}
211
212
213int pm2xxx_charger_die_therm_mngt(struct pm2xxx_charger *pm2, int val)
214{
215 queue_work(pm2->charger_wq, &pm2->check_main_thermal_prot_work);
216
217 return 0;
218}
219
220static int pm2xxx_charger_ovv_mngt(struct pm2xxx_charger *pm2, int val)
221{
Rupesh Kumar789ca7b2012-06-19 10:21:24 +0530222 dev_err(pm2->dev, "Overvoltage detected\n");
223 pm2->flags.ovv = true;
224 power_supply_changed(&pm2->ac_chg.psy);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200225
Rupesh Kumar789ca7b2012-06-19 10:21:24 +0530226 /* Schedule a new HW failure check */
227 queue_delayed_work(pm2->charger_wq, &pm2->check_hw_failure_work, 0);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200228
Rupesh Kumar789ca7b2012-06-19 10:21:24 +0530229 return 0;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200230}
231
232static int pm2xxx_charger_wd_exp_mngt(struct pm2xxx_charger *pm2, int val)
233{
234 dev_dbg(pm2->dev , "20 minutes watchdog occured\n");
235
236 pm2->ac.wd_expired = true;
237 power_supply_changed(&pm2->ac_chg.psy);
238
239 return 0;
240}
241
242static int pm2xxx_charger_vbat_lsig_mngt(struct pm2xxx_charger *pm2, int val)
243{
Rajkumar Kasirajan584f9702012-06-05 12:31:25 +0530244 int ret;
245
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200246 switch (val) {
247 case PM2XXX_INT1_ITVBATLOWR:
248 dev_dbg(pm2->dev, "VBAT grows above VBAT_LOW level\n");
Rajkumar Kasirajan584f9702012-06-05 12:31:25 +0530249 /* Enable SW EOC ctrl */
250 ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG,
251 PM2XXX_SWCTRL_SW);
252 if (ret < 0) {
253 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
254 return ret;
255 }
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200256 break;
257
258 case PM2XXX_INT1_ITVBATLOWF:
259 dev_dbg(pm2->dev, "VBAT drops below VBAT_LOW level\n");
Rajkumar Kasirajan584f9702012-06-05 12:31:25 +0530260 /* Disable SW EOC ctrl */
261 ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG,
262 PM2XXX_SWCTRL_HW);
263 if (ret < 0) {
264 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
265 return ret;
266 }
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200267 break;
268
269 default:
270 dev_err(pm2->dev, "Unknown VBAT level\n");
271 }
272
273 return 0;
274}
275
276static int pm2xxx_charger_bat_disc_mngt(struct pm2xxx_charger *pm2, int val)
277{
278 dev_dbg(pm2->dev, "battery disconnected\n");
279
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200280 return 0;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200281}
282
283static int pm2xxx_charger_detection(struct pm2xxx_charger *pm2, u8 *val)
284{
Rajkumar Kasirajane41f39e2012-05-28 15:57:33 +0530285 int ret;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200286
287 ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT2, val);
288
289 if (ret < 0) {
290 dev_err(pm2->dev, "Charger detection failed\n");
291 goto out;
292 }
293
294 *val &= (PM2XXX_INT2_S_ITVPWR1PLUG | PM2XXX_INT2_S_ITVPWR2PLUG);
Rajkumar Kasirajane41f39e2012-05-28 15:57:33 +0530295
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200296out:
297 return ret;
298}
299
300static int pm2xxx_charger_itv_pwr_plug_mngt(struct pm2xxx_charger *pm2, int val)
301{
302
303 int ret;
304 u8 read_val;
305
306 /*
307 * Since we can't be sure that the events are received
308 * synchronously, we have the check if the main charger is
309 * connected by reading the interrupt source register.
310 */
311 ret = pm2xxx_charger_detection(pm2, &read_val);
312
313 if ((ret == 0) && read_val) {
314 pm2->ac.charger_connected = 1;
315 pm2->ac_conn = true;
316 queue_work(pm2->charger_wq, &pm2->ac_work);
317 }
318
319
320 return ret;
321}
322
323static int pm2xxx_charger_itv_pwr_unplug_mngt(struct pm2xxx_charger *pm2,
324 int val)
325{
326 pm2->ac.charger_connected = 0;
327 queue_work(pm2->charger_wq, &pm2->ac_work);
328
329 return 0;
330}
331
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200332static int pm2_int_reg0(void *pm2_data, int val)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200333{
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200334 struct pm2xxx_charger *pm2 = pm2_data;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200335 int ret = 0;
336
Rajkumar Kasirajan584f9702012-06-05 12:31:25 +0530337 if (val & PM2XXX_INT1_ITVBATLOWR) {
338 ret = pm2xxx_charger_vbat_lsig_mngt(pm2,
339 PM2XXX_INT1_ITVBATLOWR);
340 if (ret < 0)
341 goto out;
342 }
343
344 if (val & PM2XXX_INT1_ITVBATLOWF) {
345 ret = pm2xxx_charger_vbat_lsig_mngt(pm2,
346 PM2XXX_INT1_ITVBATLOWF);
347 if (ret < 0)
348 goto out;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200349 }
350
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200351 if (val & PM2XXX_INT1_ITVBATDISCONNECT) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200352 ret = pm2xxx_charger_bat_disc_mngt(pm2,
353 PM2XXX_INT1_ITVBATDISCONNECT);
Rajkumar Kasirajan584f9702012-06-05 12:31:25 +0530354 if (ret < 0)
355 goto out;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200356 }
Rajkumar Kasirajan584f9702012-06-05 12:31:25 +0530357out:
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200358 return ret;
359}
360
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200361static int pm2_int_reg1(void *pm2_data, int val)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200362{
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200363 struct pm2xxx_charger *pm2 = pm2_data;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200364 int ret = 0;
365
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200366 if (val & (PM2XXX_INT2_ITVPWR1PLUG | PM2XXX_INT2_ITVPWR2PLUG)) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200367 dev_dbg(pm2->dev , "Main charger plugged\n");
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200368 ret = pm2xxx_charger_itv_pwr_plug_mngt(pm2, val &
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200369 (PM2XXX_INT2_ITVPWR1PLUG | PM2XXX_INT2_ITVPWR2PLUG));
370 }
371
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200372 if (val &
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200373 (PM2XXX_INT2_ITVPWR1UNPLUG | PM2XXX_INT2_ITVPWR2UNPLUG)) {
374 dev_dbg(pm2->dev , "Main charger unplugged\n");
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200375 ret = pm2xxx_charger_itv_pwr_unplug_mngt(pm2, val &
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200376 (PM2XXX_INT2_ITVPWR1UNPLUG |
377 PM2XXX_INT2_ITVPWR2UNPLUG));
378 }
379
380 return ret;
381}
382
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200383static int pm2_int_reg2(void *pm2_data, int val)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200384{
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200385 struct pm2xxx_charger *pm2 = pm2_data;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200386 int ret = 0;
387
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200388 if (val & PM2XXX_INT3_ITAUTOTIMEOUTWD)
389 ret = pm2xxx_charger_wd_exp_mngt(pm2, val);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200390
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200391 if (val & (PM2XXX_INT3_ITCHPRECHARGEWD |
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200392 PM2XXX_INT3_ITCHCCWD | PM2XXX_INT3_ITCHCVWD)) {
393 dev_dbg(pm2->dev,
394 "Watchdog occured for precharge, CC and CV charge\n");
395 }
396
397 return ret;
398}
399
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200400static int pm2_int_reg3(void *pm2_data, int val)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200401{
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200402 struct pm2xxx_charger *pm2 = pm2_data;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200403 int ret = 0;
404
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200405 if (val & (PM2XXX_INT4_ITCHARGINGON)) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200406 dev_dbg(pm2->dev ,
407 "chargind operation has started\n");
408 }
409
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200410 if (val & (PM2XXX_INT4_ITVRESUME)) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200411 dev_dbg(pm2->dev,
412 "battery discharged down to VResume threshold\n");
413 }
414
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200415 if (val & (PM2XXX_INT4_ITBATTFULL)) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200416 dev_dbg(pm2->dev , "battery fully detected\n");
417 }
418
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200419 if (val & (PM2XXX_INT4_ITCVPHASE)) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200420 dev_dbg(pm2->dev, "CV phase enter with 0.5C charging\n");
421 }
422
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200423 if (val & (PM2XXX_INT4_ITVPWR2OVV | PM2XXX_INT4_ITVPWR1OVV)) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200424 pm2->failure_case = VPWR_OVV;
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200425 ret = pm2xxx_charger_ovv_mngt(pm2, val &
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200426 (PM2XXX_INT4_ITVPWR2OVV | PM2XXX_INT4_ITVPWR1OVV));
427 dev_dbg(pm2->dev, "VPWR/VSYSTEM overvoltage detected\n");
428 }
429
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200430 if (val & (PM2XXX_INT4_S_ITBATTEMPCOLD |
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200431 PM2XXX_INT4_S_ITBATTEMPHOT)) {
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200432 ret = pm2xxx_charger_batt_therm_mngt(pm2, val &
433 (PM2XXX_INT4_S_ITBATTEMPCOLD |
434 PM2XXX_INT4_S_ITBATTEMPHOT));
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200435 dev_dbg(pm2->dev, "BTEMP is too Low/High\n");
436 }
437
438 return ret;
439}
440
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200441static int pm2_int_reg4(void *pm2_data, int val)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200442{
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200443 struct pm2xxx_charger *pm2 = pm2_data;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200444 int ret = 0;
445
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200446 if (val & PM2XXX_INT5_ITVSYSTEMOVV) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200447 pm2->failure_case = VSYSTEM_OVV;
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200448 ret = pm2xxx_charger_ovv_mngt(pm2, val &
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200449 PM2XXX_INT5_ITVSYSTEMOVV);
450 dev_dbg(pm2->dev, "VSYSTEM overvoltage detected\n");
451 }
452
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200453 if (val & (PM2XXX_INT5_ITTHERMALWARNINGFALL |
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200454 PM2XXX_INT5_ITTHERMALWARNINGRISE |
455 PM2XXX_INT5_ITTHERMALSHUTDOWNFALL |
456 PM2XXX_INT5_ITTHERMALSHUTDOWNRISE)) {
457 dev_dbg(pm2->dev, "BTEMP die temperature is too Low/High\n");
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200458 ret = pm2xxx_charger_die_therm_mngt(pm2, val &
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200459 (PM2XXX_INT5_ITTHERMALWARNINGFALL |
460 PM2XXX_INT5_ITTHERMALWARNINGRISE |
461 PM2XXX_INT5_ITTHERMALSHUTDOWNFALL |
462 PM2XXX_INT5_ITTHERMALSHUTDOWNRISE));
463 }
464
465 return ret;
466}
467
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200468static int pm2_int_reg5(void *pm2_data, int val)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200469{
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200470 struct pm2xxx_charger *pm2 = pm2_data;
471 int ret = 0;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200472
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200473 if (val & (PM2XXX_INT6_ITVPWR2DROP | PM2XXX_INT6_ITVPWR1DROP)) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200474 dev_dbg(pm2->dev, "VMPWR drop to VBAT level\n");
475 }
476
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200477 if (val & (PM2XXX_INT6_ITVPWR2VALIDRISE |
478 PM2XXX_INT6_ITVPWR1VALIDRISE |
479 PM2XXX_INT6_ITVPWR2VALIDFALL |
480 PM2XXX_INT6_ITVPWR1VALIDFALL)) {
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200481 dev_dbg(pm2->dev, "Falling/Rising edge on WPWR1/2\n");
482 }
483
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200484 return ret;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200485}
486
487static irqreturn_t pm2xxx_irq_int(int irq, void *data)
488{
489 struct pm2xxx_charger *pm2 = data;
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200490 struct pm2xxx_interrupts *interrupt = pm2->pm2_int;
491 int i;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200492
Lee Jonesaee2b842013-02-25 14:28:59 +0000493 /* wake up the device */
494 pm_runtime_get_sync(pm2->dev);
495
M BenZoubeirf4095a02012-09-13 10:34:18 +0200496 do {
497 for (i = 0; i < PM2XXX_NUM_INT_REG; i++) {
498 pm2xxx_reg_read(pm2,
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200499 pm2xxx_interrupt_registers[i],
500 &(interrupt->reg[i]));
501
M BenZoubeirf4095a02012-09-13 10:34:18 +0200502 if (interrupt->reg[i] > 0)
503 interrupt->handler[i](pm2, interrupt->reg[i]);
504 }
505 } while (gpio_get_value(pm2->pdata->gpio_irq_number) == 0);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200506
Lee Jonesaee2b842013-02-25 14:28:59 +0000507 pm_runtime_mark_last_busy(pm2->dev);
508 pm_runtime_put_autosuspend(pm2->dev);
509
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200510 return IRQ_HANDLED;
511}
512
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200513static int pm2xxx_charger_get_ac_cv(struct pm2xxx_charger *pm2)
514{
515 int ret = 0;
516 u8 val;
517
518 if (pm2->ac.charger_connected && pm2->ac.charger_online) {
519
520 ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT4, &val);
521 if (ret < 0) {
522 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
523 goto out;
524 }
525
526 if (val & PM2XXX_INT4_S_ITCVPHASE)
527 ret = PM2XXX_CONST_VOLT;
528 else
529 ret = PM2XXX_CONST_CURR;
530 }
531out:
532 return ret;
533}
534
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200535static int pm2xxx_current_to_regval(int curr)
536{
537 int i;
538
539 if (curr < pm2xxx_charger_current_map[0])
540 return 0;
541
542 for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_current_map); i++) {
543 if (curr < pm2xxx_charger_current_map[i])
544 return (i - 1);
545 }
546
547 i = ARRAY_SIZE(pm2xxx_charger_current_map) - 1;
548 if (curr == pm2xxx_charger_current_map[i])
549 return i;
550 else
551 return -EINVAL;
552}
553
554static int pm2xxx_voltage_to_regval(int curr)
555{
556 int i;
557
558 if (curr < pm2xxx_charger_voltage_map[0])
559 return 0;
560
561 for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_voltage_map); i++) {
562 if (curr < pm2xxx_charger_voltage_map[i])
563 return i - 1;
564 }
565
566 i = ARRAY_SIZE(pm2xxx_charger_voltage_map) - 1;
567 if (curr == pm2xxx_charger_voltage_map[i])
568 return i;
569 else
570 return -EINVAL;
571}
572
573static int pm2xxx_charger_update_charger_current(struct ux500_charger *charger,
574 int ich_out)
575{
576 int ret;
577 int curr_index;
578 struct pm2xxx_charger *pm2;
579 u8 val;
580
581 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
582 pm2 = to_pm2xxx_charger_ac_device_info(charger);
583 else
584 return -ENXIO;
585
586 curr_index = pm2xxx_current_to_regval(ich_out);
587 if (curr_index < 0) {
588 dev_err(pm2->dev,
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200589 "Charger current too high, charging not started\n");
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200590 return -ENXIO;
591 }
592
593 ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val);
594 if (ret >= 0) {
595 val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK;
596 val |= curr_index;
597 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val);
598 if (ret < 0) {
599 dev_err(pm2->dev,
600 "%s write failed\n", __func__);
601 }
602 }
603 else
604 dev_err(pm2->dev, "%s read failed\n", __func__);
605
606 return ret;
607}
608
609static int pm2xxx_charger_ac_get_property(struct power_supply *psy,
610 enum power_supply_property psp,
611 union power_supply_propval *val)
612{
613 struct pm2xxx_charger *pm2;
614
615 pm2 = to_pm2xxx_charger_ac_device_info(psy_to_ux500_charger(psy));
616
617 switch (psp) {
618 case POWER_SUPPLY_PROP_HEALTH:
619 if (pm2->flags.mainextchnotok)
620 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
621 else if (pm2->ac.wd_expired)
622 val->intval = POWER_SUPPLY_HEALTH_DEAD;
623 else if (pm2->flags.main_thermal_prot)
624 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
Rupesh Kumar789ca7b2012-06-19 10:21:24 +0530625 else if (pm2->flags.ovv)
626 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200627 else
628 val->intval = POWER_SUPPLY_HEALTH_GOOD;
629 break;
630 case POWER_SUPPLY_PROP_ONLINE:
631 val->intval = pm2->ac.charger_online;
632 break;
633 case POWER_SUPPLY_PROP_PRESENT:
634 val->intval = pm2->ac.charger_connected;
635 break;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200636 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
637 pm2->ac.cv_active = pm2xxx_charger_get_ac_cv(pm2);
638 val->intval = pm2->ac.cv_active;
639 break;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200640 default:
641 return -EINVAL;
642 }
643 return 0;
644}
645
646static int pm2xxx_charging_init(struct pm2xxx_charger *pm2)
647{
648 int ret = 0;
649
650 /* enable CC and CV watchdog */
651 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG3,
652 (PM2XXX_CH_WD_CV_PHASE_60MIN | PM2XXX_CH_WD_CC_PHASE_60MIN));
653 if( ret < 0)
654 return ret;
655
656 /* enable precharge watchdog */
657 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG4,
658 PM2XXX_CH_WD_PRECH_PHASE_60MIN);
659
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200660 /* Disable auto timeout */
661 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG5,
662 PM2XXX_CH_WD_AUTO_TIMEOUT_20MIN);
663
664 /*
665 * EOC current level = 100mA
666 * Precharge current level = 100mA
667 * CC current level = 1000mA
668 */
669 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6,
670 (PM2XXX_DIR_CH_CC_CURRENT_1000MA |
671 PM2XXX_CH_PRECH_CURRENT_100MA |
672 PM2XXX_CH_EOC_CURRENT_100MA));
673
674 /*
675 * recharge threshold = 3.8V
676 * Precharge to CC threshold = 2.9V
677 */
678 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG7,
679 (PM2XXX_CH_PRECH_VOL_2_9 | PM2XXX_CH_VRESUME_VOL_3_8));
680
681 /* float voltage charger level = 4.2V */
682 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG8,
683 PM2XXX_CH_VOLT_4_2);
684
685 /* Voltage drop between VBAT and VSYS in HW charging = 300mV */
686 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG9,
687 (PM2XXX_CH_150MV_DROP_300MV | PM2XXX_CHARCHING_INFO_DIS |
688 PM2XXX_CH_CC_REDUCED_CURRENT_IDENT |
689 PM2XXX_CH_CC_MODEDROP_DIS));
690
691 /* Input charger level of over voltage = 10V */
692 ret = pm2xxx_reg_write(pm2, PM2XXX_INP_VOLT_VPWR2,
693 PM2XXX_VPWR2_OVV_10);
694 ret = pm2xxx_reg_write(pm2, PM2XXX_INP_VOLT_VPWR1,
695 PM2XXX_VPWR1_OVV_10);
696
697 /* Input charger drop */
698 ret = pm2xxx_reg_write(pm2, PM2XXX_INP_DROP_VPWR2,
699 (PM2XXX_VPWR2_HW_OPT_DIS | PM2XXX_VPWR2_VALID_DIS |
700 PM2XXX_VPWR2_DROP_DIS));
701 ret = pm2xxx_reg_write(pm2, PM2XXX_INP_DROP_VPWR1,
702 (PM2XXX_VPWR1_HW_OPT_DIS | PM2XXX_VPWR1_VALID_DIS |
703 PM2XXX_VPWR1_DROP_DIS));
704
705 /* Disable battery low monitoring */
706 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_LOW_LEV_COMP_REG,
Rajkumar Kasirajane41f39e2012-05-28 15:57:33 +0530707 PM2XXX_VBAT_LOW_MONITORING_ENA);
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200708
709 /* Disable LED */
710 ret = pm2xxx_reg_write(pm2, PM2XXX_LED_CTRL_REG,
711 PM2XXX_LED_SELECT_DIS);
712
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200713 return ret;
714}
715
716static int pm2xxx_charger_ac_en(struct ux500_charger *charger,
717 int enable, int vset, int iset)
718{
719 int ret;
720 int volt_index;
721 int curr_index;
722 u8 val;
723
724 struct pm2xxx_charger *pm2 = to_pm2xxx_charger_ac_device_info(charger);
725
726 if (enable) {
727 if (!pm2->ac.charger_connected) {
728 dev_dbg(pm2->dev, "AC charger not connected\n");
729 return -ENXIO;
730 }
731
732 dev_dbg(pm2->dev, "Enable AC: %dmV %dmA\n", vset, iset);
733 if (!pm2->vddadc_en_ac) {
734 regulator_enable(pm2->regu);
735 pm2->vddadc_en_ac = true;
736 }
737
738 ret = pm2xxx_charging_init(pm2);
739 if (ret < 0) {
740 dev_err(pm2->dev, "%s charging init failed\n",
741 __func__);
742 goto error_occured;
743 }
744
745 volt_index = pm2xxx_voltage_to_regval(vset);
746 curr_index = pm2xxx_current_to_regval(iset);
747
748 if (volt_index < 0 || curr_index < 0) {
749 dev_err(pm2->dev,
750 "Charger voltage or current too high, "
751 "charging not started\n");
752 return -ENXIO;
753 }
754
755 ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG8, &val);
Rajkumar Kasirajane41f39e2012-05-28 15:57:33 +0530756 if (ret < 0) {
757 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
758 goto error_occured;
759 }
760 val &= ~PM2XXX_CH_VOLT_MASK;
761 val |= volt_index;
762 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG8, val);
763 if (ret < 0) {
764 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
765 goto error_occured;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200766 }
767
768 ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val);
Rajkumar Kasirajane41f39e2012-05-28 15:57:33 +0530769 if (ret < 0) {
770 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
771 goto error_occured;
772 }
773 val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK;
774 val |= curr_index;
775 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val);
776 if (ret < 0) {
777 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
778 goto error_occured;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200779 }
780
781 if (!pm2->bat->enable_overshoot) {
782 ret = pm2xxx_reg_read(pm2, PM2XXX_LED_CTRL_REG, &val);
Rajkumar Kasirajane41f39e2012-05-28 15:57:33 +0530783 if (ret < 0) {
784 dev_err(pm2->dev, "%s pm2xxx read failed\n",
785 __func__);
786 goto error_occured;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200787 }
Rajkumar Kasirajane41f39e2012-05-28 15:57:33 +0530788 val |= PM2XXX_ANTI_OVERSHOOT_EN;
789 ret = pm2xxx_reg_write(pm2, PM2XXX_LED_CTRL_REG, val);
790 if (ret < 0) {
791 dev_err(pm2->dev, "%s pm2xxx write failed\n",
792 __func__);
793 goto error_occured;
794 }
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200795 }
796
797 ret = pm2xxx_charging_enable_mngt(pm2);
Rajkumar Kasirajane41f39e2012-05-28 15:57:33 +0530798 if (ret < 0) {
799 dev_err(pm2->dev, "Failed to enable"
800 "pm2xxx ac charger\n");
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200801 goto error_occured;
802 }
803
804 pm2->ac.charger_online = 1;
805 } else {
806 pm2->ac.charger_online = 0;
807 pm2->ac.wd_expired = false;
808
809 /* Disable regulator if enabled */
810 if (pm2->vddadc_en_ac) {
811 regulator_disable(pm2->regu);
812 pm2->vddadc_en_ac = false;
813 }
814
815 ret = pm2xxx_charging_disable_mngt(pm2);
Rajkumar Kasirajane41f39e2012-05-28 15:57:33 +0530816 if (ret < 0) {
817 dev_err(pm2->dev, "failed to disable"
818 "pm2xxx ac charger\n");
819 goto error_occured;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200820 }
821
822 dev_dbg(pm2->dev, "PM2301: " "Disabled AC charging\n");
823 }
824 power_supply_changed(&pm2->ac_chg.psy);
825
826error_occured:
827 return ret;
828}
829
830static int pm2xxx_charger_watchdog_kick(struct ux500_charger *charger)
831{
832 int ret;
833 struct pm2xxx_charger *pm2;
834
835 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
836 pm2 = to_pm2xxx_charger_ac_device_info(charger);
837 else
838 return -ENXIO;
839
840 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_WD_KICK, WD_TIMER);
841 if (ret)
842 dev_err(pm2->dev, "Failed to kick WD!\n");
843
844 return ret;
845}
846
847static void pm2xxx_charger_ac_work(struct work_struct *work)
848{
849 struct pm2xxx_charger *pm2 = container_of(work,
850 struct pm2xxx_charger, ac_work);
851
852
853 power_supply_changed(&pm2->ac_chg.psy);
854 sysfs_notify(&pm2->ac_chg.psy.dev->kobj, NULL, "present");
855};
856
Rupesh Kumar789ca7b2012-06-19 10:21:24 +0530857static void pm2xxx_charger_check_hw_failure_work(struct work_struct *work)
858{
859 u8 reg_value;
860
861 struct pm2xxx_charger *pm2 = container_of(work,
862 struct pm2xxx_charger, check_hw_failure_work.work);
863
864 if (pm2->flags.ovv) {
865 pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT4, &reg_value);
866
867 if (!(reg_value & (PM2XXX_INT4_S_ITVPWR1OVV |
868 PM2XXX_INT4_S_ITVPWR2OVV))) {
869 pm2->flags.ovv = false;
870 power_supply_changed(&pm2->ac_chg.psy);
871 }
872 }
873
874 /* If we still have a failure, schedule a new check */
875 if (pm2->flags.ovv) {
876 queue_delayed_work(pm2->charger_wq,
877 &pm2->check_hw_failure_work, round_jiffies(HZ));
878 }
879}
880
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200881static void pm2xxx_charger_check_main_thermal_prot_work(
882 struct work_struct *work)
883{
Rupesh Kumarda9e83d2012-07-16 12:45:19 +0530884 int ret;
885 u8 val;
886
887 struct pm2xxx_charger *pm2 = container_of(work, struct pm2xxx_charger,
888 check_main_thermal_prot_work);
889
890 /* Check if die temp warning is still active */
891 ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT5, &val);
892 if (ret < 0) {
893 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
894 return;
895 }
896 if (val & (PM2XXX_INT5_S_ITTHERMALWARNINGRISE
897 | PM2XXX_INT5_S_ITTHERMALSHUTDOWNRISE))
898 pm2->flags.main_thermal_prot = true;
899 else if (val & (PM2XXX_INT5_S_ITTHERMALWARNINGFALL
900 | PM2XXX_INT5_S_ITTHERMALSHUTDOWNFALL))
901 pm2->flags.main_thermal_prot = false;
902
903 power_supply_changed(&pm2->ac_chg.psy);
904}
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200905
Olivier Clergeaud006f82d2012-05-14 16:50:29 +0200906static struct pm2xxx_interrupts pm2xxx_int = {
907 .handler[0] = pm2_int_reg0,
908 .handler[1] = pm2_int_reg1,
909 .handler[2] = pm2_int_reg2,
910 .handler[3] = pm2_int_reg3,
911 .handler[4] = pm2_int_reg4,
912 .handler[5] = pm2_int_reg5,
913};
914
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200915static struct pm2xxx_irq pm2xxx_charger_irq[] = {
916 {"PM2XXX_IRQ_INT", pm2xxx_irq_int},
917};
918
919static int pm2xxx_wall_charger_resume(struct i2c_client *i2c_client)
920{
Mustapha Ben Zoubeir49fddee2012-06-27 15:40:58 +0200921 struct pm2xxx_charger *pm2;
922
923 pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(i2c_client);
924 set_lpn_pin(pm2);
925
926 /* If we still have a HW failure, schedule a new check */
927 if (pm2->flags.ovv)
928 queue_delayed_work(pm2->charger_wq,
929 &pm2->check_hw_failure_work, 0);
930
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200931 return 0;
932}
933
934static int pm2xxx_wall_charger_suspend(struct i2c_client *i2c_client,
935 pm_message_t state)
936{
Mustapha Ben Zoubeir49fddee2012-06-27 15:40:58 +0200937 struct pm2xxx_charger *pm2;
938
939 pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(i2c_client);
940 clear_lpn_pin(pm2);
941
942 /* Cancel any pending HW failure check */
943 if (delayed_work_pending(&pm2->check_hw_failure_work))
944 cancel_delayed_work(&pm2->check_hw_failure_work);
945
946 flush_work(&pm2->ac_work);
947 flush_work(&pm2->check_main_thermal_prot_work);
948
Michel JAOUEN01ec8c52012-04-26 10:00:04 +0200949 return 0;
950}
951
Lee Jonesaee2b842013-02-25 14:28:59 +0000952#ifdef CONFIG_PM
953static int pm2xxx_runtime_suspend(struct device *dev)
954{
955 struct i2c_client *pm2xxx_i2c_client = to_i2c_client(dev);
956 struct pm2xxx_charger *pm2;
957 int ret = 0;
958
959 pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(pm2xxx_i2c_client);
960 if (!pm2) {
961 dev_err(pm2->dev, "no pm2xxx_charger data supplied\n");
962 ret = -EINVAL;
963 return ret;
964 }
965
966 clear_lpn_pin(pm2);
967
968 return ret;
969}
970
971static int pm2xxx_runtime_resume(struct device *dev)
972{
973 struct i2c_client *pm2xxx_i2c_client = to_i2c_client(dev);
974 struct pm2xxx_charger *pm2;
975 int ret = 0;
976
977 pm2 = (struct pm2xxx_charger *)i2c_get_clientdata(pm2xxx_i2c_client);
978 if (!pm2) {
979 dev_err(pm2->dev, "no pm2xxx_charger data supplied\n");
980 ret = -EINVAL;
981 return ret;
982 }
983
984 if (gpio_is_valid(pm2->lpn_pin) && gpio_get_value(pm2->lpn_pin) == 0)
985 set_lpn_pin(pm2);
986
987 return ret;
988}
989
990static const struct dev_pm_ops pm2xxx_pm_ops = {
991 .runtime_suspend = pm2xxx_runtime_suspend,
992 .runtime_resume = pm2xxx_runtime_resume,
993};
994#define PM2XXX_PM_OPS (&pm2xxx_pm_ops)
995#else
996#define PM2XXX_PM_OPS NULL
997#endif
998
Lee Jones116c3262013-02-25 14:26:52 +0000999static int pm2xxx_wall_charger_probe(struct i2c_client *i2c_client,
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001000 const struct i2c_device_id *id)
1001{
1002 struct pm2xxx_platform_data *pl_data = i2c_client->dev.platform_data;
1003 struct pm2xxx_charger *pm2;
1004 int ret = 0;
1005 u8 val;
M BenZoubeirf4095a02012-09-13 10:34:18 +02001006 int i;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001007
1008 pm2 = kzalloc(sizeof(struct pm2xxx_charger), GFP_KERNEL);
1009 if (!pm2) {
1010 dev_err(pm2->dev, "pm2xxx_charger allocation failed\n");
1011 return -ENOMEM;
1012 }
1013
1014 /* get parent data */
1015 pm2->dev = &i2c_client->dev;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001016
Olivier Clergeaud006f82d2012-05-14 16:50:29 +02001017 pm2->pm2_int = &pm2xxx_int;
1018
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001019 /* get charger spcific platform data */
1020 if (!pl_data->wall_charger) {
1021 dev_err(pm2->dev, "no charger platform data supplied\n");
1022 ret = -EINVAL;
1023 goto free_device_info;
1024 }
1025
1026 pm2->pdata = pl_data->wall_charger;
1027
1028 /* get battery specific platform data */
1029 if (!pl_data->battery) {
1030 dev_err(pm2->dev, "no battery platform data supplied\n");
1031 ret = -EINVAL;
1032 goto free_device_info;
1033 }
1034
1035 pm2->bat = pl_data->battery;
1036
1037 if (!i2c_check_functionality(i2c_client->adapter,
1038 I2C_FUNC_SMBUS_BYTE_DATA |
1039 I2C_FUNC_SMBUS_READ_WORD_DATA)) {
1040 ret = -ENODEV;
1041 dev_info(pm2->dev, "pm2301 i2c_check_functionality failed\n");
1042 goto free_device_info;
1043 }
1044
1045 pm2->config.pm2xxx_i2c = i2c_client;
1046 pm2->config.pm2xxx_id = (struct i2c_device_id *) id;
1047 i2c_set_clientdata(i2c_client, pm2);
1048
1049 /* AC supply */
1050 /* power_supply base class */
1051 pm2->ac_chg.psy.name = pm2->pdata->label;
1052 pm2->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
1053 pm2->ac_chg.psy.properties = pm2xxx_charger_ac_props;
1054 pm2->ac_chg.psy.num_properties = ARRAY_SIZE(pm2xxx_charger_ac_props);
1055 pm2->ac_chg.psy.get_property = pm2xxx_charger_ac_get_property;
1056 pm2->ac_chg.psy.supplied_to = pm2->pdata->supplied_to;
1057 pm2->ac_chg.psy.num_supplicants = pm2->pdata->num_supplicants;
1058 /* pm2xxx_charger sub-class */
1059 pm2->ac_chg.ops.enable = &pm2xxx_charger_ac_en;
1060 pm2->ac_chg.ops.kick_wd = &pm2xxx_charger_watchdog_kick;
1061 pm2->ac_chg.ops.update_curr = &pm2xxx_charger_update_charger_current;
1062 pm2->ac_chg.max_out_volt = pm2xxx_charger_voltage_map[
1063 ARRAY_SIZE(pm2xxx_charger_voltage_map) - 1];
1064 pm2->ac_chg.max_out_curr = pm2xxx_charger_current_map[
1065 ARRAY_SIZE(pm2xxx_charger_current_map) - 1];
Loic Pallardye07a5642012-05-10 15:33:56 +02001066 pm2->ac_chg.wdt_refresh = WD_KICK_INTERVAL;
Olivier Clergeaud006f82d2012-05-14 16:50:29 +02001067 pm2->ac_chg.enabled = true;
Loic Pallardye07a5642012-05-10 15:33:56 +02001068 pm2->ac_chg.external = true;
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001069
1070 /* Create a work queue for the charger */
1071 pm2->charger_wq =
1072 create_singlethread_workqueue("pm2xxx_charger_wq");
1073 if (pm2->charger_wq == NULL) {
1074 dev_err(pm2->dev, "failed to create work queue\n");
1075 goto free_device_info;
1076 }
1077
1078 /* Init work for charger detection */
1079 INIT_WORK(&pm2->ac_work, pm2xxx_charger_ac_work);
1080
1081 /* Init work for checking HW status */
1082 INIT_WORK(&pm2->check_main_thermal_prot_work,
1083 pm2xxx_charger_check_main_thermal_prot_work);
1084
Rupesh Kumar789ca7b2012-06-19 10:21:24 +05301085 /* Init work for HW failure check */
1086 INIT_DEFERRABLE_WORK(&pm2->check_hw_failure_work,
1087 pm2xxx_charger_check_hw_failure_work);
1088
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001089 /*
1090 * VDD ADC supply needs to be enabled from this driver when there
1091 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
1092 * interrupts during charging
1093 */
1094 pm2->regu = regulator_get(pm2->dev, "vddadc");
1095 if (IS_ERR(pm2->regu)) {
1096 ret = PTR_ERR(pm2->regu);
1097 dev_err(pm2->dev, "failed to get vddadc regulator\n");
1098 goto free_charger_wq;
1099 }
1100
1101 /* Register AC charger class */
1102 ret = power_supply_register(pm2->dev, &pm2->ac_chg.psy);
1103 if (ret) {
1104 dev_err(pm2->dev, "failed to register AC charger\n");
1105 goto free_regulator;
1106 }
1107
1108 /* Register interrupts */
M BenZoubeirf4095a02012-09-13 10:34:18 +02001109 ret = request_threaded_irq(gpio_to_irq(pm2->pdata->gpio_irq_number),
1110 NULL,
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001111 pm2xxx_charger_irq[0].isr,
1112 pm2->pdata->irq_type,
1113 pm2xxx_charger_irq[0].name, pm2);
1114
1115 if (ret != 0) {
1116 dev_err(pm2->dev, "failed to request %s IRQ %d: %d\n",
M BenZoubeirf4095a02012-09-13 10:34:18 +02001117 pm2xxx_charger_irq[0].name,
1118 gpio_to_irq(pm2->pdata->gpio_irq_number), ret);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001119 goto unregister_pm2xxx_charger;
1120 }
Lee Jonesaee2b842013-02-25 14:28:59 +00001121
1122 ret = pm_runtime_set_active(pm2->dev);
1123 if (ret)
1124 dev_err(pm2->dev, "set active Error\n");
1125
1126 pm_runtime_enable(pm2->dev);
1127 pm_runtime_set_autosuspend_delay(pm2->dev, PM2XXX_AUTOSUSPEND_DELAY);
1128 pm_runtime_use_autosuspend(pm2->dev);
1129 pm_runtime_resume(pm2->dev);
1130
Rupesh Kumard4f510f2012-06-28 18:31:19 +05301131 /* pm interrupt can wake up system */
M BenZoubeirf4095a02012-09-13 10:34:18 +02001132 ret = enable_irq_wake(gpio_to_irq(pm2->pdata->gpio_irq_number));
Rupesh Kumard4f510f2012-06-28 18:31:19 +05301133 if (ret) {
1134 dev_err(pm2->dev, "failed to set irq wake\n");
1135 goto unregister_pm2xxx_interrupt;
1136 }
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001137
Lee Jones39880432013-01-23 14:38:15 +00001138 mutex_init(&pm2->lock);
1139
lme00437a21e22f2012-11-13 14:43:48 +01001140 if (gpio_is_valid(pm2->pdata->lpn_gpio)) {
1141 /* get lpn GPIO from platform data */
1142 pm2->lpn_pin = pm2->pdata->lpn_gpio;
Lee Jones39880432013-01-23 14:38:15 +00001143
lme00437a21e22f2012-11-13 14:43:48 +01001144 /*
1145 * Charger detection mechanism requires pulling up the LPN pin
1146 * while i2c communication if Charger is not connected
1147 * LPN pin of PM2301 is GPIO60 of AB9540
1148 */
1149 ret = gpio_request(pm2->lpn_pin, "pm2301_lpm_gpio");
1150
1151 if (ret < 0) {
1152 dev_err(pm2->dev, "pm2301_lpm_gpio request failed\n");
1153 goto disable_pm2_irq_wake;
1154 }
1155 ret = gpio_direction_output(pm2->lpn_pin, 0);
1156 if (ret < 0) {
1157 dev_err(pm2->dev, "pm2301_lpm_gpio direction failed\n");
1158 goto free_gpio;
1159 }
1160 set_lpn_pin(pm2);
1161 }
M BenZoubeirf4095a02012-09-13 10:34:18 +02001162
1163 /* read interrupt registers */
1164 for (i = 0; i < PM2XXX_NUM_INT_REG; i++)
1165 pm2xxx_reg_read(pm2,
1166 pm2xxx_interrupt_registers[i],
1167 &val);
1168
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001169 ret = pm2xxx_charger_detection(pm2, &val);
1170
1171 if ((ret == 0) && val) {
1172 pm2->ac.charger_connected = 1;
Per Forlin54fbbb62012-07-05 16:52:55 +02001173 ab8500_override_turn_on_stat(~AB8500_POW_KEY_1_ON,
1174 AB8500_MAIN_CH_DET);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001175 pm2->ac_conn = true;
1176 power_supply_changed(&pm2->ac_chg.psy);
1177 sysfs_notify(&pm2->ac_chg.psy.dev->kobj, NULL, "present");
1178 }
1179
1180 return 0;
1181
Lee Jones39880432013-01-23 14:38:15 +00001182free_gpio:
lme00437a21e22f2012-11-13 14:43:48 +01001183 if (gpio_is_valid(pm2->lpn_pin))
1184 gpio_free(pm2->lpn_pin);
Rupesh Kumard4f510f2012-06-28 18:31:19 +05301185disable_pm2_irq_wake:
M BenZoubeirf4095a02012-09-13 10:34:18 +02001186 disable_irq_wake(gpio_to_irq(pm2->pdata->gpio_irq_number));
Rupesh Kumard4f510f2012-06-28 18:31:19 +05301187unregister_pm2xxx_interrupt:
1188 /* disable interrupt */
M BenZoubeirf4095a02012-09-13 10:34:18 +02001189 free_irq(gpio_to_irq(pm2->pdata->gpio_irq_number), pm2);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001190unregister_pm2xxx_charger:
1191 /* unregister power supply */
1192 power_supply_unregister(&pm2->ac_chg.psy);
1193free_regulator:
1194 /* disable the regulator */
1195 regulator_put(pm2->regu);
1196free_charger_wq:
1197 destroy_workqueue(pm2->charger_wq);
1198free_device_info:
1199 kfree(pm2);
Rupesh Kumard4f510f2012-06-28 18:31:19 +05301200
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001201 return ret;
1202}
1203
Lee Jones116c3262013-02-25 14:26:52 +00001204static int pm2xxx_wall_charger_remove(struct i2c_client *i2c_client)
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001205{
1206 struct pm2xxx_charger *pm2 = i2c_get_clientdata(i2c_client);
1207
Lee Jonesaee2b842013-02-25 14:28:59 +00001208 /* Disable pm_runtime */
1209 pm_runtime_disable(pm2->dev);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001210 /* Disable AC charging */
1211 pm2xxx_charger_ac_en(&pm2->ac_chg, false, 0, 0);
1212
Rupesh Kumard4f510f2012-06-28 18:31:19 +05301213 /* Disable wake by pm interrupt */
M BenZoubeirf4095a02012-09-13 10:34:18 +02001214 disable_irq_wake(gpio_to_irq(pm2->pdata->gpio_irq_number));
Rupesh Kumard4f510f2012-06-28 18:31:19 +05301215
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001216 /* Disable interrupts */
M BenZoubeirf4095a02012-09-13 10:34:18 +02001217 free_irq(gpio_to_irq(pm2->pdata->gpio_irq_number), pm2);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001218
1219 /* Delete the work queue */
1220 destroy_workqueue(pm2->charger_wq);
1221
1222 flush_scheduled_work();
1223
1224 /* disable the regulator */
1225 regulator_put(pm2->regu);
1226
1227 power_supply_unregister(&pm2->ac_chg.psy);
1228
lme00437a21e22f2012-11-13 14:43:48 +01001229 if (gpio_is_valid(pm2->lpn_pin))
1230 gpio_free(pm2->lpn_pin);
Lee Jones39880432013-01-23 14:38:15 +00001231
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001232 kfree(pm2);
1233
1234 return 0;
1235}
1236
1237static const struct i2c_device_id pm2xxx_id[] = {
1238 { "pm2301", 0 },
1239 { }
1240};
1241
1242MODULE_DEVICE_TABLE(i2c, pm2xxx_id);
1243
1244static struct i2c_driver pm2xxx_charger_driver = {
1245 .probe = pm2xxx_wall_charger_probe,
Lee Jones116c3262013-02-25 14:26:52 +00001246 .remove = pm2xxx_wall_charger_remove,
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001247 .suspend = pm2xxx_wall_charger_suspend,
1248 .resume = pm2xxx_wall_charger_resume,
1249 .driver = {
1250 .name = "pm2xxx-wall_charger",
1251 .owner = THIS_MODULE,
Lee Jonesaee2b842013-02-25 14:28:59 +00001252 .pm = PM2XXX_PM_OPS,
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001253 },
1254 .id_table = pm2xxx_id,
1255};
1256
1257static int __init pm2xxx_charger_init(void)
1258{
1259 return i2c_add_driver(&pm2xxx_charger_driver);
1260}
1261
1262static void __exit pm2xxx_charger_exit(void)
1263{
1264 i2c_del_driver(&pm2xxx_charger_driver);
1265}
1266
lme00437a21e22f2012-11-13 14:43:48 +01001267device_initcall_sync(pm2xxx_charger_init);
Michel JAOUEN01ec8c52012-04-26 10:00:04 +02001268module_exit(pm2xxx_charger_exit);
1269
1270MODULE_LICENSE("GPL v2");
1271MODULE_AUTHOR("Rajkumar kasirajan, Olivier Launay");
1272MODULE_ALIAS("platform:pm2xxx-charger");
1273MODULE_DESCRIPTION("PM2xxx charger management driver");