2 * Copyright 2012 ST Ericsson.
4 * Power supply driver for ST Ericsson pm2xxx_charger charger
6 * 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.
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>
19 #include <linux/regulator/consumer.h>
20 #include <linux/err.h>
21 #include <linux/i2c.h>
22 #include <linux/workqueue.h>
23 #include <linux/mfd/abx500/ab8500.h>
24 #include <linux/mfd/abx500/ab8500-bm.h>
25 #include <linux/mfd/abx500/ux500_chargalg.h>
26 #include <linux/pm2301_charger.h>
27 #include <linux/gpio.h>
28 #include <linux/pm_runtime.h>
31 #include "pm2301_charger.h"
33 #define to_pm2xxx_charger_ac_device_info(x) container_of((x), \
34 struct pm2xxx_charger, ac_chg)
37 #define PM2XXX_AUTOSUSPEND_DELAY 500
39 static int pm2xxx_interrupt_registers
[] = {
48 static enum power_supply_property pm2xxx_charger_ac_props
[] = {
49 POWER_SUPPLY_PROP_HEALTH
,
50 POWER_SUPPLY_PROP_PRESENT
,
51 POWER_SUPPLY_PROP_ONLINE
,
52 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
55 static int pm2xxx_charger_voltage_map
[] = {
91 static int pm2xxx_charger_current_map
[] = {
110 static const struct i2c_device_id pm2xxx_ident
[] = {
115 static void set_lpn_pin(struct pm2xxx_charger
*pm2
)
117 if (!pm2
->ac
.charger_connected
&& gpio_is_valid(pm2
->lpn_pin
)) {
118 gpio_set_value(pm2
->lpn_pin
, 1);
119 usleep_range(SLEEP_MIN
, SLEEP_MAX
);
123 static void clear_lpn_pin(struct pm2xxx_charger
*pm2
)
125 if (!pm2
->ac
.charger_connected
&& gpio_is_valid(pm2
->lpn_pin
))
126 gpio_set_value(pm2
->lpn_pin
, 0);
129 static int pm2xxx_reg_read(struct pm2xxx_charger
*pm2
, int reg
, u8
*val
)
133 /* wake up the device */
134 pm_runtime_get_sync(pm2
->dev
);
136 ret
= i2c_smbus_read_i2c_block_data(pm2
->config
.pm2xxx_i2c
, reg
,
139 dev_err(pm2
->dev
, "Error reading register at 0x%x\n", reg
);
143 pm_runtime_put_sync(pm2
->dev
);
148 static int pm2xxx_reg_write(struct pm2xxx_charger
*pm2
, int reg
, u8 val
)
152 /* wake up the device */
153 pm_runtime_get_sync(pm2
->dev
);
155 ret
= i2c_smbus_write_i2c_block_data(pm2
->config
.pm2xxx_i2c
, reg
,
158 dev_err(pm2
->dev
, "Error writing register at 0x%x\n", reg
);
162 pm_runtime_put_sync(pm2
->dev
);
167 static int pm2xxx_charging_enable_mngt(struct pm2xxx_charger
*pm2
)
171 /* Enable charging */
172 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG2
,
173 (PM2XXX_CH_AUTO_RESUME_EN
| PM2XXX_CHARGER_ENA
));
178 static int pm2xxx_charging_disable_mngt(struct pm2xxx_charger
*pm2
)
182 /* Disable SW EOC ctrl */
183 ret
= pm2xxx_reg_write(pm2
, PM2XXX_SW_CTRL_REG
, PM2XXX_SWCTRL_HW
);
185 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
189 /* Disable charging */
190 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG2
,
191 (PM2XXX_CH_AUTO_RESUME_DIS
| PM2XXX_CHARGER_DIS
));
193 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
200 static int pm2xxx_charger_batt_therm_mngt(struct pm2xxx_charger
*pm2
, int val
)
202 queue_work(pm2
->charger_wq
, &pm2
->check_main_thermal_prot_work
);
208 static int pm2xxx_charger_die_therm_mngt(struct pm2xxx_charger
*pm2
, int val
)
210 queue_work(pm2
->charger_wq
, &pm2
->check_main_thermal_prot_work
);
215 static int pm2xxx_charger_ovv_mngt(struct pm2xxx_charger
*pm2
, int val
)
217 dev_err(pm2
->dev
, "Overvoltage detected\n");
218 pm2
->flags
.ovv
= true;
219 power_supply_changed(&pm2
->ac_chg
.psy
);
221 /* Schedule a new HW failure check */
222 queue_delayed_work(pm2
->charger_wq
, &pm2
->check_hw_failure_work
, 0);
227 static int pm2xxx_charger_wd_exp_mngt(struct pm2xxx_charger
*pm2
, int val
)
229 dev_dbg(pm2
->dev
, "20 minutes watchdog expired\n");
231 pm2
->ac
.wd_expired
= true;
232 power_supply_changed(&pm2
->ac_chg
.psy
);
237 static int pm2xxx_charger_vbat_lsig_mngt(struct pm2xxx_charger
*pm2
, int val
)
242 case PM2XXX_INT1_ITVBATLOWR
:
243 dev_dbg(pm2
->dev
, "VBAT grows above VBAT_LOW level\n");
244 /* Enable SW EOC ctrl */
245 ret
= pm2xxx_reg_write(pm2
, PM2XXX_SW_CTRL_REG
,
248 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
253 case PM2XXX_INT1_ITVBATLOWF
:
254 dev_dbg(pm2
->dev
, "VBAT drops below VBAT_LOW level\n");
255 /* Disable SW EOC ctrl */
256 ret
= pm2xxx_reg_write(pm2
, PM2XXX_SW_CTRL_REG
,
259 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
265 dev_err(pm2
->dev
, "Unknown VBAT level\n");
271 static int pm2xxx_charger_bat_disc_mngt(struct pm2xxx_charger
*pm2
, int val
)
273 dev_dbg(pm2
->dev
, "battery disconnected\n");
278 static int pm2xxx_charger_detection(struct pm2xxx_charger
*pm2
, u8
*val
)
282 ret
= pm2xxx_reg_read(pm2
, PM2XXX_SRCE_REG_INT2
, val
);
285 dev_err(pm2
->dev
, "Charger detection failed\n");
289 *val
&= (PM2XXX_INT2_S_ITVPWR1PLUG
| PM2XXX_INT2_S_ITVPWR2PLUG
);
295 static int pm2xxx_charger_itv_pwr_plug_mngt(struct pm2xxx_charger
*pm2
, int val
)
302 * Since we can't be sure that the events are received
303 * synchronously, we have the check if the main charger is
304 * connected by reading the interrupt source register.
306 ret
= pm2xxx_charger_detection(pm2
, &read_val
);
308 if ((ret
== 0) && read_val
) {
309 pm2
->ac
.charger_connected
= 1;
311 queue_work(pm2
->charger_wq
, &pm2
->ac_work
);
318 static int pm2xxx_charger_itv_pwr_unplug_mngt(struct pm2xxx_charger
*pm2
,
321 pm2
->ac
.charger_connected
= 0;
322 queue_work(pm2
->charger_wq
, &pm2
->ac_work
);
327 static int pm2_int_reg0(void *pm2_data
, int val
)
329 struct pm2xxx_charger
*pm2
= pm2_data
;
332 if (val
& PM2XXX_INT1_ITVBATLOWR
) {
333 ret
= pm2xxx_charger_vbat_lsig_mngt(pm2
,
334 PM2XXX_INT1_ITVBATLOWR
);
339 if (val
& PM2XXX_INT1_ITVBATLOWF
) {
340 ret
= pm2xxx_charger_vbat_lsig_mngt(pm2
,
341 PM2XXX_INT1_ITVBATLOWF
);
346 if (val
& PM2XXX_INT1_ITVBATDISCONNECT
) {
347 ret
= pm2xxx_charger_bat_disc_mngt(pm2
,
348 PM2XXX_INT1_ITVBATDISCONNECT
);
356 static int pm2_int_reg1(void *pm2_data
, int val
)
358 struct pm2xxx_charger
*pm2
= pm2_data
;
361 if (val
& (PM2XXX_INT2_ITVPWR1PLUG
| PM2XXX_INT2_ITVPWR2PLUG
)) {
362 dev_dbg(pm2
->dev
, "Main charger plugged\n");
363 ret
= pm2xxx_charger_itv_pwr_plug_mngt(pm2
, val
&
364 (PM2XXX_INT2_ITVPWR1PLUG
| PM2XXX_INT2_ITVPWR2PLUG
));
368 (PM2XXX_INT2_ITVPWR1UNPLUG
| PM2XXX_INT2_ITVPWR2UNPLUG
)) {
369 dev_dbg(pm2
->dev
, "Main charger unplugged\n");
370 ret
= pm2xxx_charger_itv_pwr_unplug_mngt(pm2
, val
&
371 (PM2XXX_INT2_ITVPWR1UNPLUG
|
372 PM2XXX_INT2_ITVPWR2UNPLUG
));
378 static int pm2_int_reg2(void *pm2_data
, int val
)
380 struct pm2xxx_charger
*pm2
= pm2_data
;
383 if (val
& PM2XXX_INT3_ITAUTOTIMEOUTWD
)
384 ret
= pm2xxx_charger_wd_exp_mngt(pm2
, val
);
386 if (val
& (PM2XXX_INT3_ITCHPRECHARGEWD
|
387 PM2XXX_INT3_ITCHCCWD
| PM2XXX_INT3_ITCHCVWD
)) {
389 "Watchdog occurred for precharge, CC and CV charge\n");
395 static int pm2_int_reg3(void *pm2_data
, int val
)
397 struct pm2xxx_charger
*pm2
= pm2_data
;
400 if (val
& (PM2XXX_INT4_ITCHARGINGON
)) {
402 "chargind operation has started\n");
405 if (val
& (PM2XXX_INT4_ITVRESUME
)) {
407 "battery discharged down to VResume threshold\n");
410 if (val
& (PM2XXX_INT4_ITBATTFULL
)) {
411 dev_dbg(pm2
->dev
, "battery fully detected\n");
414 if (val
& (PM2XXX_INT4_ITCVPHASE
)) {
415 dev_dbg(pm2
->dev
, "CV phase enter with 0.5C charging\n");
418 if (val
& (PM2XXX_INT4_ITVPWR2OVV
| PM2XXX_INT4_ITVPWR1OVV
)) {
419 pm2
->failure_case
= VPWR_OVV
;
420 ret
= pm2xxx_charger_ovv_mngt(pm2
, val
&
421 (PM2XXX_INT4_ITVPWR2OVV
| PM2XXX_INT4_ITVPWR1OVV
));
422 dev_dbg(pm2
->dev
, "VPWR/VSYSTEM overvoltage detected\n");
425 if (val
& (PM2XXX_INT4_S_ITBATTEMPCOLD
|
426 PM2XXX_INT4_S_ITBATTEMPHOT
)) {
427 ret
= pm2xxx_charger_batt_therm_mngt(pm2
, val
&
428 (PM2XXX_INT4_S_ITBATTEMPCOLD
|
429 PM2XXX_INT4_S_ITBATTEMPHOT
));
430 dev_dbg(pm2
->dev
, "BTEMP is too Low/High\n");
436 static int pm2_int_reg4(void *pm2_data
, int val
)
438 struct pm2xxx_charger
*pm2
= pm2_data
;
441 if (val
& PM2XXX_INT5_ITVSYSTEMOVV
) {
442 pm2
->failure_case
= VSYSTEM_OVV
;
443 ret
= pm2xxx_charger_ovv_mngt(pm2
, val
&
444 PM2XXX_INT5_ITVSYSTEMOVV
);
445 dev_dbg(pm2
->dev
, "VSYSTEM overvoltage detected\n");
448 if (val
& (PM2XXX_INT5_ITTHERMALWARNINGFALL
|
449 PM2XXX_INT5_ITTHERMALWARNINGRISE
|
450 PM2XXX_INT5_ITTHERMALSHUTDOWNFALL
|
451 PM2XXX_INT5_ITTHERMALSHUTDOWNRISE
)) {
452 dev_dbg(pm2
->dev
, "BTEMP die temperature is too Low/High\n");
453 ret
= pm2xxx_charger_die_therm_mngt(pm2
, val
&
454 (PM2XXX_INT5_ITTHERMALWARNINGFALL
|
455 PM2XXX_INT5_ITTHERMALWARNINGRISE
|
456 PM2XXX_INT5_ITTHERMALSHUTDOWNFALL
|
457 PM2XXX_INT5_ITTHERMALSHUTDOWNRISE
));
463 static int pm2_int_reg5(void *pm2_data
, int val
)
465 struct pm2xxx_charger
*pm2
= pm2_data
;
468 if (val
& (PM2XXX_INT6_ITVPWR2DROP
| PM2XXX_INT6_ITVPWR1DROP
)) {
469 dev_dbg(pm2
->dev
, "VMPWR drop to VBAT level\n");
472 if (val
& (PM2XXX_INT6_ITVPWR2VALIDRISE
|
473 PM2XXX_INT6_ITVPWR1VALIDRISE
|
474 PM2XXX_INT6_ITVPWR2VALIDFALL
|
475 PM2XXX_INT6_ITVPWR1VALIDFALL
)) {
476 dev_dbg(pm2
->dev
, "Falling/Rising edge on WPWR1/2\n");
482 static irqreturn_t
pm2xxx_irq_int(int irq
, void *data
)
484 struct pm2xxx_charger
*pm2
= data
;
485 struct pm2xxx_interrupts
*interrupt
= pm2
->pm2_int
;
488 /* wake up the device */
489 pm_runtime_get_sync(pm2
->dev
);
492 for (i
= 0; i
< PM2XXX_NUM_INT_REG
; i
++) {
494 pm2xxx_interrupt_registers
[i
],
495 &(interrupt
->reg
[i
]));
497 if (interrupt
->reg
[i
] > 0)
498 interrupt
->handler
[i
](pm2
, interrupt
->reg
[i
]);
500 } while (gpio_get_value(pm2
->pdata
->gpio_irq_number
) == 0);
502 pm_runtime_mark_last_busy(pm2
->dev
);
503 pm_runtime_put_autosuspend(pm2
->dev
);
508 static int pm2xxx_charger_get_ac_cv(struct pm2xxx_charger
*pm2
)
513 if (pm2
->ac
.charger_connected
&& pm2
->ac
.charger_online
) {
515 ret
= pm2xxx_reg_read(pm2
, PM2XXX_SRCE_REG_INT4
, &val
);
517 dev_err(pm2
->dev
, "%s pm2xxx read failed\n", __func__
);
521 if (val
& PM2XXX_INT4_S_ITCVPHASE
)
522 ret
= PM2XXX_CONST_VOLT
;
524 ret
= PM2XXX_CONST_CURR
;
530 static int pm2xxx_current_to_regval(int curr
)
534 if (curr
< pm2xxx_charger_current_map
[0])
537 for (i
= 1; i
< ARRAY_SIZE(pm2xxx_charger_current_map
); i
++) {
538 if (curr
< pm2xxx_charger_current_map
[i
])
542 i
= ARRAY_SIZE(pm2xxx_charger_current_map
) - 1;
543 if (curr
== pm2xxx_charger_current_map
[i
])
549 static int pm2xxx_voltage_to_regval(int curr
)
553 if (curr
< pm2xxx_charger_voltage_map
[0])
556 for (i
= 1; i
< ARRAY_SIZE(pm2xxx_charger_voltage_map
); i
++) {
557 if (curr
< pm2xxx_charger_voltage_map
[i
])
561 i
= ARRAY_SIZE(pm2xxx_charger_voltage_map
) - 1;
562 if (curr
== pm2xxx_charger_voltage_map
[i
])
568 static int pm2xxx_charger_update_charger_current(struct ux500_charger
*charger
,
573 struct pm2xxx_charger
*pm2
;
576 if (charger
->psy
.type
== POWER_SUPPLY_TYPE_MAINS
)
577 pm2
= to_pm2xxx_charger_ac_device_info(charger
);
581 curr_index
= pm2xxx_current_to_regval(ich_out
);
582 if (curr_index
< 0) {
584 "Charger current too high, charging not started\n");
588 ret
= pm2xxx_reg_read(pm2
, PM2XXX_BATT_CTRL_REG6
, &val
);
590 val
&= ~PM2XXX_DIR_CH_CC_CURRENT_MASK
;
592 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG6
, val
);
595 "%s write failed\n", __func__
);
599 dev_err(pm2
->dev
, "%s read failed\n", __func__
);
604 static int pm2xxx_charger_ac_get_property(struct power_supply
*psy
,
605 enum power_supply_property psp
,
606 union power_supply_propval
*val
)
608 struct pm2xxx_charger
*pm2
;
610 pm2
= to_pm2xxx_charger_ac_device_info(psy_to_ux500_charger(psy
));
613 case POWER_SUPPLY_PROP_HEALTH
:
614 if (pm2
->flags
.mainextchnotok
)
615 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
616 else if (pm2
->ac
.wd_expired
)
617 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
618 else if (pm2
->flags
.main_thermal_prot
)
619 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
620 else if (pm2
->flags
.ovv
)
621 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
623 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
625 case POWER_SUPPLY_PROP_ONLINE
:
626 val
->intval
= pm2
->ac
.charger_online
;
628 case POWER_SUPPLY_PROP_PRESENT
:
629 val
->intval
= pm2
->ac
.charger_connected
;
631 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
632 pm2
->ac
.cv_active
= pm2xxx_charger_get_ac_cv(pm2
);
633 val
->intval
= pm2
->ac
.cv_active
;
641 static int pm2xxx_charging_init(struct pm2xxx_charger
*pm2
)
645 /* enable CC and CV watchdog */
646 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG3
,
647 (PM2XXX_CH_WD_CV_PHASE_60MIN
| PM2XXX_CH_WD_CC_PHASE_60MIN
));
651 /* enable precharge watchdog */
652 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG4
,
653 PM2XXX_CH_WD_PRECH_PHASE_60MIN
);
655 /* Disable auto timeout */
656 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG5
,
657 PM2XXX_CH_WD_AUTO_TIMEOUT_20MIN
);
660 * EOC current level = 100mA
661 * Precharge current level = 100mA
662 * CC current level = 1000mA
664 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG6
,
665 (PM2XXX_DIR_CH_CC_CURRENT_1000MA
|
666 PM2XXX_CH_PRECH_CURRENT_100MA
|
667 PM2XXX_CH_EOC_CURRENT_100MA
));
670 * recharge threshold = 3.8V
671 * Precharge to CC threshold = 2.9V
673 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG7
,
674 (PM2XXX_CH_PRECH_VOL_2_9
| PM2XXX_CH_VRESUME_VOL_3_8
));
676 /* float voltage charger level = 4.2V */
677 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG8
,
680 /* Voltage drop between VBAT and VSYS in HW charging = 300mV */
681 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG9
,
682 (PM2XXX_CH_150MV_DROP_300MV
| PM2XXX_CHARCHING_INFO_DIS
|
683 PM2XXX_CH_CC_REDUCED_CURRENT_IDENT
|
684 PM2XXX_CH_CC_MODEDROP_DIS
));
686 /* Input charger level of over voltage = 10V */
687 ret
= pm2xxx_reg_write(pm2
, PM2XXX_INP_VOLT_VPWR2
,
688 PM2XXX_VPWR2_OVV_10
);
689 ret
= pm2xxx_reg_write(pm2
, PM2XXX_INP_VOLT_VPWR1
,
690 PM2XXX_VPWR1_OVV_10
);
692 /* Input charger drop */
693 ret
= pm2xxx_reg_write(pm2
, PM2XXX_INP_DROP_VPWR2
,
694 (PM2XXX_VPWR2_HW_OPT_DIS
| PM2XXX_VPWR2_VALID_DIS
|
695 PM2XXX_VPWR2_DROP_DIS
));
696 ret
= pm2xxx_reg_write(pm2
, PM2XXX_INP_DROP_VPWR1
,
697 (PM2XXX_VPWR1_HW_OPT_DIS
| PM2XXX_VPWR1_VALID_DIS
|
698 PM2XXX_VPWR1_DROP_DIS
));
700 /* Disable battery low monitoring */
701 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_LOW_LEV_COMP_REG
,
702 PM2XXX_VBAT_LOW_MONITORING_ENA
);
707 static int pm2xxx_charger_ac_en(struct ux500_charger
*charger
,
708 int enable
, int vset
, int iset
)
715 struct pm2xxx_charger
*pm2
= to_pm2xxx_charger_ac_device_info(charger
);
718 if (!pm2
->ac
.charger_connected
) {
719 dev_dbg(pm2
->dev
, "AC charger not connected\n");
723 dev_dbg(pm2
->dev
, "Enable AC: %dmV %dmA\n", vset
, iset
);
724 if (!pm2
->vddadc_en_ac
) {
725 ret
= regulator_enable(pm2
->regu
);
728 "Failed to enable vddadc regulator\n");
730 pm2
->vddadc_en_ac
= true;
733 ret
= pm2xxx_charging_init(pm2
);
735 dev_err(pm2
->dev
, "%s charging init failed\n",
740 volt_index
= pm2xxx_voltage_to_regval(vset
);
741 curr_index
= pm2xxx_current_to_regval(iset
);
743 if (volt_index
< 0 || curr_index
< 0) {
745 "Charger voltage or current too high, "
746 "charging not started\n");
750 ret
= pm2xxx_reg_read(pm2
, PM2XXX_BATT_CTRL_REG8
, &val
);
752 dev_err(pm2
->dev
, "%s pm2xxx read failed\n", __func__
);
755 val
&= ~PM2XXX_CH_VOLT_MASK
;
757 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG8
, val
);
759 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
763 ret
= pm2xxx_reg_read(pm2
, PM2XXX_BATT_CTRL_REG6
, &val
);
765 dev_err(pm2
->dev
, "%s pm2xxx read failed\n", __func__
);
768 val
&= ~PM2XXX_DIR_CH_CC_CURRENT_MASK
;
770 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG6
, val
);
772 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
776 if (!pm2
->bat
->enable_overshoot
) {
777 ret
= pm2xxx_reg_read(pm2
, PM2XXX_LED_CTRL_REG
, &val
);
779 dev_err(pm2
->dev
, "%s pm2xxx read failed\n",
783 val
|= PM2XXX_ANTI_OVERSHOOT_EN
;
784 ret
= pm2xxx_reg_write(pm2
, PM2XXX_LED_CTRL_REG
, val
);
786 dev_err(pm2
->dev
, "%s pm2xxx write failed\n",
792 ret
= pm2xxx_charging_enable_mngt(pm2
);
794 dev_err(pm2
->dev
, "Failed to enable"
795 "pm2xxx ac charger\n");
799 pm2
->ac
.charger_online
= 1;
801 pm2
->ac
.charger_online
= 0;
802 pm2
->ac
.wd_expired
= false;
804 /* Disable regulator if enabled */
805 if (pm2
->vddadc_en_ac
) {
806 regulator_disable(pm2
->regu
);
807 pm2
->vddadc_en_ac
= false;
810 ret
= pm2xxx_charging_disable_mngt(pm2
);
812 dev_err(pm2
->dev
, "failed to disable"
813 "pm2xxx ac charger\n");
817 dev_dbg(pm2
->dev
, "PM2301: " "Disabled AC charging\n");
819 power_supply_changed(&pm2
->ac_chg
.psy
);
825 static int pm2xxx_charger_watchdog_kick(struct ux500_charger
*charger
)
828 struct pm2xxx_charger
*pm2
;
830 if (charger
->psy
.type
== POWER_SUPPLY_TYPE_MAINS
)
831 pm2
= to_pm2xxx_charger_ac_device_info(charger
);
835 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_WD_KICK
, WD_TIMER
);
837 dev_err(pm2
->dev
, "Failed to kick WD!\n");
842 static void pm2xxx_charger_ac_work(struct work_struct
*work
)
844 struct pm2xxx_charger
*pm2
= container_of(work
,
845 struct pm2xxx_charger
, ac_work
);
848 power_supply_changed(&pm2
->ac_chg
.psy
);
849 sysfs_notify(&pm2
->ac_chg
.psy
.dev
->kobj
, NULL
, "present");
852 static void pm2xxx_charger_check_hw_failure_work(struct work_struct
*work
)
856 struct pm2xxx_charger
*pm2
= container_of(work
,
857 struct pm2xxx_charger
, check_hw_failure_work
.work
);
859 if (pm2
->flags
.ovv
) {
860 pm2xxx_reg_read(pm2
, PM2XXX_SRCE_REG_INT4
, ®_value
);
862 if (!(reg_value
& (PM2XXX_INT4_S_ITVPWR1OVV
|
863 PM2XXX_INT4_S_ITVPWR2OVV
))) {
864 pm2
->flags
.ovv
= false;
865 power_supply_changed(&pm2
->ac_chg
.psy
);
869 /* If we still have a failure, schedule a new check */
870 if (pm2
->flags
.ovv
) {
871 queue_delayed_work(pm2
->charger_wq
,
872 &pm2
->check_hw_failure_work
, round_jiffies(HZ
));
876 static void pm2xxx_charger_check_main_thermal_prot_work(
877 struct work_struct
*work
)
882 struct pm2xxx_charger
*pm2
= container_of(work
, struct pm2xxx_charger
,
883 check_main_thermal_prot_work
);
885 /* Check if die temp warning is still active */
886 ret
= pm2xxx_reg_read(pm2
, PM2XXX_SRCE_REG_INT5
, &val
);
888 dev_err(pm2
->dev
, "%s pm2xxx read failed\n", __func__
);
891 if (val
& (PM2XXX_INT5_S_ITTHERMALWARNINGRISE
892 | PM2XXX_INT5_S_ITTHERMALSHUTDOWNRISE
))
893 pm2
->flags
.main_thermal_prot
= true;
894 else if (val
& (PM2XXX_INT5_S_ITTHERMALWARNINGFALL
895 | PM2XXX_INT5_S_ITTHERMALSHUTDOWNFALL
))
896 pm2
->flags
.main_thermal_prot
= false;
898 power_supply_changed(&pm2
->ac_chg
.psy
);
901 static struct pm2xxx_interrupts pm2xxx_int
= {
902 .handler
[0] = pm2_int_reg0
,
903 .handler
[1] = pm2_int_reg1
,
904 .handler
[2] = pm2_int_reg2
,
905 .handler
[3] = pm2_int_reg3
,
906 .handler
[4] = pm2_int_reg4
,
907 .handler
[5] = pm2_int_reg5
,
910 static struct pm2xxx_irq pm2xxx_charger_irq
[] = {
911 {"PM2XXX_IRQ_INT", pm2xxx_irq_int
},
916 #ifdef CONFIG_PM_SLEEP
918 static int pm2xxx_wall_charger_resume(struct device
*dev
)
920 struct i2c_client
*i2c_client
= to_i2c_client(dev
);
921 struct pm2xxx_charger
*pm2
;
923 pm2
= (struct pm2xxx_charger
*)i2c_get_clientdata(i2c_client
);
926 /* If we still have a HW failure, schedule a new check */
928 queue_delayed_work(pm2
->charger_wq
,
929 &pm2
->check_hw_failure_work
, 0);
934 static int pm2xxx_wall_charger_suspend(struct device
*dev
)
936 struct i2c_client
*i2c_client
= to_i2c_client(dev
);
937 struct pm2xxx_charger
*pm2
;
939 pm2
= (struct pm2xxx_charger
*)i2c_get_clientdata(i2c_client
);
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
);
946 flush_work(&pm2
->ac_work
);
947 flush_work(&pm2
->check_main_thermal_prot_work
);
954 static int pm2xxx_runtime_suspend(struct device
*dev
)
956 struct i2c_client
*pm2xxx_i2c_client
= to_i2c_client(dev
);
957 struct pm2xxx_charger
*pm2
;
959 pm2
= (struct pm2xxx_charger
*)i2c_get_clientdata(pm2xxx_i2c_client
);
965 static int pm2xxx_runtime_resume(struct device
*dev
)
967 struct i2c_client
*pm2xxx_i2c_client
= to_i2c_client(dev
);
968 struct pm2xxx_charger
*pm2
;
970 pm2
= (struct pm2xxx_charger
*)i2c_get_clientdata(pm2xxx_i2c_client
);
972 if (gpio_is_valid(pm2
->lpn_pin
) && gpio_get_value(pm2
->lpn_pin
) == 0)
978 static const struct dev_pm_ops pm2xxx_pm_ops
= {
979 SET_SYSTEM_SLEEP_PM_OPS(pm2xxx_wall_charger_suspend
,
980 pm2xxx_wall_charger_resume
)
981 SET_RUNTIME_PM_OPS(pm2xxx_runtime_suspend
, pm2xxx_runtime_resume
, NULL
)
983 #define PM2XXX_PM_OPS (&pm2xxx_pm_ops)
985 #define PM2XXX_PM_OPS NULL
988 static int pm2xxx_wall_charger_probe(struct i2c_client
*i2c_client
,
989 const struct i2c_device_id
*id
)
991 struct pm2xxx_platform_data
*pl_data
= i2c_client
->dev
.platform_data
;
992 struct pm2xxx_charger
*pm2
;
998 dev_err(&i2c_client
->dev
, "No platform data supplied\n");
1002 pm2
= kzalloc(sizeof(struct pm2xxx_charger
), GFP_KERNEL
);
1004 dev_err(&i2c_client
->dev
, "pm2xxx_charger allocation failed\n");
1008 /* get parent data */
1009 pm2
->dev
= &i2c_client
->dev
;
1011 pm2
->pm2_int
= &pm2xxx_int
;
1013 /* get charger spcific platform data */
1014 if (!pl_data
->wall_charger
) {
1015 dev_err(pm2
->dev
, "no charger platform data supplied\n");
1017 goto free_device_info
;
1020 pm2
->pdata
= pl_data
->wall_charger
;
1022 /* get battery specific platform data */
1023 if (!pl_data
->battery
) {
1024 dev_err(pm2
->dev
, "no battery platform data supplied\n");
1026 goto free_device_info
;
1029 pm2
->bat
= pl_data
->battery
;
1031 if (!i2c_check_functionality(i2c_client
->adapter
,
1032 I2C_FUNC_SMBUS_BYTE_DATA
|
1033 I2C_FUNC_SMBUS_READ_WORD_DATA
)) {
1035 dev_info(pm2
->dev
, "pm2301 i2c_check_functionality failed\n");
1036 goto free_device_info
;
1039 pm2
->config
.pm2xxx_i2c
= i2c_client
;
1040 pm2
->config
.pm2xxx_id
= (struct i2c_device_id
*) id
;
1041 i2c_set_clientdata(i2c_client
, pm2
);
1044 /* power_supply base class */
1045 pm2
->ac_chg
.psy
.name
= pm2
->pdata
->label
;
1046 pm2
->ac_chg
.psy
.type
= POWER_SUPPLY_TYPE_MAINS
;
1047 pm2
->ac_chg
.psy
.properties
= pm2xxx_charger_ac_props
;
1048 pm2
->ac_chg
.psy
.num_properties
= ARRAY_SIZE(pm2xxx_charger_ac_props
);
1049 pm2
->ac_chg
.psy
.get_property
= pm2xxx_charger_ac_get_property
;
1050 pm2
->ac_chg
.psy
.supplied_to
= pm2
->pdata
->supplied_to
;
1051 pm2
->ac_chg
.psy
.num_supplicants
= pm2
->pdata
->num_supplicants
;
1052 /* pm2xxx_charger sub-class */
1053 pm2
->ac_chg
.ops
.enable
= &pm2xxx_charger_ac_en
;
1054 pm2
->ac_chg
.ops
.kick_wd
= &pm2xxx_charger_watchdog_kick
;
1055 pm2
->ac_chg
.ops
.update_curr
= &pm2xxx_charger_update_charger_current
;
1056 pm2
->ac_chg
.max_out_volt
= pm2xxx_charger_voltage_map
[
1057 ARRAY_SIZE(pm2xxx_charger_voltage_map
) - 1];
1058 pm2
->ac_chg
.max_out_curr
= pm2xxx_charger_current_map
[
1059 ARRAY_SIZE(pm2xxx_charger_current_map
) - 1];
1060 pm2
->ac_chg
.wdt_refresh
= WD_KICK_INTERVAL
;
1061 pm2
->ac_chg
.enabled
= true;
1062 pm2
->ac_chg
.external
= true;
1064 /* Create a work queue for the charger */
1065 pm2
->charger_wq
= create_singlethread_workqueue("pm2xxx_charger_wq");
1066 if (pm2
->charger_wq
== NULL
) {
1068 dev_err(pm2
->dev
, "failed to create work queue\n");
1069 goto free_device_info
;
1072 /* Init work for charger detection */
1073 INIT_WORK(&pm2
->ac_work
, pm2xxx_charger_ac_work
);
1075 /* Init work for checking HW status */
1076 INIT_WORK(&pm2
->check_main_thermal_prot_work
,
1077 pm2xxx_charger_check_main_thermal_prot_work
);
1079 /* Init work for HW failure check */
1080 INIT_DEFERRABLE_WORK(&pm2
->check_hw_failure_work
,
1081 pm2xxx_charger_check_hw_failure_work
);
1084 * VDD ADC supply needs to be enabled from this driver when there
1085 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
1086 * interrupts during charging
1088 pm2
->regu
= regulator_get(pm2
->dev
, "vddadc");
1089 if (IS_ERR(pm2
->regu
)) {
1090 ret
= PTR_ERR(pm2
->regu
);
1091 dev_err(pm2
->dev
, "failed to get vddadc regulator\n");
1092 goto free_charger_wq
;
1095 /* Register AC charger class */
1096 ret
= power_supply_register(pm2
->dev
, &pm2
->ac_chg
.psy
);
1098 dev_err(pm2
->dev
, "failed to register AC charger\n");
1099 goto free_regulator
;
1102 /* Register interrupts */
1103 ret
= request_threaded_irq(gpio_to_irq(pm2
->pdata
->gpio_irq_number
),
1105 pm2xxx_charger_irq
[0].isr
,
1106 pm2
->pdata
->irq_type
,
1107 pm2xxx_charger_irq
[0].name
, pm2
);
1110 dev_err(pm2
->dev
, "failed to request %s IRQ %d: %d\n",
1111 pm2xxx_charger_irq
[0].name
,
1112 gpio_to_irq(pm2
->pdata
->gpio_irq_number
), ret
);
1113 goto unregister_pm2xxx_charger
;
1116 ret
= pm_runtime_set_active(pm2
->dev
);
1118 dev_err(pm2
->dev
, "set active Error\n");
1120 pm_runtime_enable(pm2
->dev
);
1121 pm_runtime_set_autosuspend_delay(pm2
->dev
, PM2XXX_AUTOSUSPEND_DELAY
);
1122 pm_runtime_use_autosuspend(pm2
->dev
);
1123 pm_runtime_resume(pm2
->dev
);
1125 /* pm interrupt can wake up system */
1126 ret
= enable_irq_wake(gpio_to_irq(pm2
->pdata
->gpio_irq_number
));
1128 dev_err(pm2
->dev
, "failed to set irq wake\n");
1129 goto unregister_pm2xxx_interrupt
;
1132 mutex_init(&pm2
->lock
);
1134 if (gpio_is_valid(pm2
->pdata
->lpn_gpio
)) {
1135 /* get lpn GPIO from platform data */
1136 pm2
->lpn_pin
= pm2
->pdata
->lpn_gpio
;
1139 * Charger detection mechanism requires pulling up the LPN pin
1140 * while i2c communication if Charger is not connected
1141 * LPN pin of PM2301 is GPIO60 of AB9540
1143 ret
= gpio_request(pm2
->lpn_pin
, "pm2301_lpm_gpio");
1146 dev_err(pm2
->dev
, "pm2301_lpm_gpio request failed\n");
1147 goto disable_pm2_irq_wake
;
1149 ret
= gpio_direction_output(pm2
->lpn_pin
, 0);
1151 dev_err(pm2
->dev
, "pm2301_lpm_gpio direction failed\n");
1157 /* read interrupt registers */
1158 for (i
= 0; i
< PM2XXX_NUM_INT_REG
; i
++)
1159 pm2xxx_reg_read(pm2
,
1160 pm2xxx_interrupt_registers
[i
],
1163 ret
= pm2xxx_charger_detection(pm2
, &val
);
1165 if ((ret
== 0) && val
) {
1166 pm2
->ac
.charger_connected
= 1;
1167 ab8500_override_turn_on_stat(~AB8500_POW_KEY_1_ON
,
1168 AB8500_MAIN_CH_DET
);
1169 pm2
->ac_conn
= true;
1170 power_supply_changed(&pm2
->ac_chg
.psy
);
1171 sysfs_notify(&pm2
->ac_chg
.psy
.dev
->kobj
, NULL
, "present");
1177 if (gpio_is_valid(pm2
->lpn_pin
))
1178 gpio_free(pm2
->lpn_pin
);
1179 disable_pm2_irq_wake
:
1180 disable_irq_wake(gpio_to_irq(pm2
->pdata
->gpio_irq_number
));
1181 unregister_pm2xxx_interrupt
:
1182 /* disable interrupt */
1183 free_irq(gpio_to_irq(pm2
->pdata
->gpio_irq_number
), pm2
);
1184 unregister_pm2xxx_charger
:
1185 /* unregister power supply */
1186 power_supply_unregister(&pm2
->ac_chg
.psy
);
1188 /* disable the regulator */
1189 regulator_put(pm2
->regu
);
1191 destroy_workqueue(pm2
->charger_wq
);
1198 static int pm2xxx_wall_charger_remove(struct i2c_client
*i2c_client
)
1200 struct pm2xxx_charger
*pm2
= i2c_get_clientdata(i2c_client
);
1202 /* Disable pm_runtime */
1203 pm_runtime_disable(pm2
->dev
);
1204 /* Disable AC charging */
1205 pm2xxx_charger_ac_en(&pm2
->ac_chg
, false, 0, 0);
1207 /* Disable wake by pm interrupt */
1208 disable_irq_wake(gpio_to_irq(pm2
->pdata
->gpio_irq_number
));
1210 /* Disable interrupts */
1211 free_irq(gpio_to_irq(pm2
->pdata
->gpio_irq_number
), pm2
);
1213 /* Delete the work queue */
1214 destroy_workqueue(pm2
->charger_wq
);
1216 flush_scheduled_work();
1218 /* disable the regulator */
1219 regulator_put(pm2
->regu
);
1221 power_supply_unregister(&pm2
->ac_chg
.psy
);
1223 if (gpio_is_valid(pm2
->lpn_pin
))
1224 gpio_free(pm2
->lpn_pin
);
1231 static const struct i2c_device_id pm2xxx_id
[] = {
1236 MODULE_DEVICE_TABLE(i2c
, pm2xxx_id
);
1238 static struct i2c_driver pm2xxx_charger_driver
= {
1239 .probe
= pm2xxx_wall_charger_probe
,
1240 .remove
= pm2xxx_wall_charger_remove
,
1242 .name
= "pm2xxx-wall_charger",
1243 .owner
= THIS_MODULE
,
1244 .pm
= PM2XXX_PM_OPS
,
1246 .id_table
= pm2xxx_id
,
1249 static int __init
pm2xxx_charger_init(void)
1251 return i2c_add_driver(&pm2xxx_charger_driver
);
1254 static void __exit
pm2xxx_charger_exit(void)
1256 i2c_del_driver(&pm2xxx_charger_driver
);
1259 device_initcall_sync(pm2xxx_charger_init
);
1260 module_exit(pm2xxx_charger_exit
);
1262 MODULE_LICENSE("GPL v2");
1263 MODULE_AUTHOR("Rajkumar kasirajan, Olivier Launay");
1264 MODULE_ALIAS("i2c:pm2xxx-charger");
1265 MODULE_DESCRIPTION("PM2xxx charger management driver");