1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2012 ST Ericsson.
5 * Power supply driver for ST Ericsson pm2xxx_charger charger
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/device.h>
11 #include <linux/interrupt.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/platform_device.h>
15 #include <linux/power_supply.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/err.h>
18 #include <linux/i2c.h>
19 #include <linux/workqueue.h>
20 #include <linux/mfd/abx500/ab8500.h>
21 #include <linux/mfd/abx500/ab8500-bm.h>
22 #include <linux/mfd/abx500/ux500_chargalg.h>
23 #include <linux/pm2301_charger.h>
24 #include <linux/gpio.h>
25 #include <linux/pm_runtime.h>
28 #include "pm2301_charger.h"
30 #define to_pm2xxx_charger_ac_device_info(x) container_of((x), \
31 struct pm2xxx_charger, ac_chg)
34 #define PM2XXX_AUTOSUSPEND_DELAY 500
36 static int pm2xxx_interrupt_registers
[] = {
45 static enum power_supply_property pm2xxx_charger_ac_props
[] = {
46 POWER_SUPPLY_PROP_HEALTH
,
47 POWER_SUPPLY_PROP_PRESENT
,
48 POWER_SUPPLY_PROP_ONLINE
,
49 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
52 static int pm2xxx_charger_voltage_map
[] = {
88 static int pm2xxx_charger_current_map
[] = {
107 static const struct i2c_device_id pm2xxx_ident
[] = {
112 static void set_lpn_pin(struct pm2xxx_charger
*pm2
)
114 if (!pm2
->ac
.charger_connected
&& gpio_is_valid(pm2
->lpn_pin
)) {
115 gpio_set_value(pm2
->lpn_pin
, 1);
116 usleep_range(SLEEP_MIN
, SLEEP_MAX
);
120 static void clear_lpn_pin(struct pm2xxx_charger
*pm2
)
122 if (!pm2
->ac
.charger_connected
&& gpio_is_valid(pm2
->lpn_pin
))
123 gpio_set_value(pm2
->lpn_pin
, 0);
126 static int pm2xxx_reg_read(struct pm2xxx_charger
*pm2
, int reg
, u8
*val
)
130 /* wake up the device */
131 pm_runtime_get_sync(pm2
->dev
);
133 ret
= i2c_smbus_read_i2c_block_data(pm2
->config
.pm2xxx_i2c
, reg
,
136 dev_err(pm2
->dev
, "Error reading register at 0x%x\n", reg
);
140 pm_runtime_put_sync(pm2
->dev
);
145 static int pm2xxx_reg_write(struct pm2xxx_charger
*pm2
, int reg
, u8 val
)
149 /* wake up the device */
150 pm_runtime_get_sync(pm2
->dev
);
152 ret
= i2c_smbus_write_i2c_block_data(pm2
->config
.pm2xxx_i2c
, reg
,
155 dev_err(pm2
->dev
, "Error writing register at 0x%x\n", reg
);
159 pm_runtime_put_sync(pm2
->dev
);
164 static int pm2xxx_charging_enable_mngt(struct pm2xxx_charger
*pm2
)
168 /* Enable charging */
169 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG2
,
170 (PM2XXX_CH_AUTO_RESUME_EN
| PM2XXX_CHARGER_ENA
));
175 static int pm2xxx_charging_disable_mngt(struct pm2xxx_charger
*pm2
)
179 /* Disable SW EOC ctrl */
180 ret
= pm2xxx_reg_write(pm2
, PM2XXX_SW_CTRL_REG
, PM2XXX_SWCTRL_HW
);
182 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
186 /* Disable charging */
187 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG2
,
188 (PM2XXX_CH_AUTO_RESUME_DIS
| PM2XXX_CHARGER_DIS
));
190 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
197 static int pm2xxx_charger_batt_therm_mngt(struct pm2xxx_charger
*pm2
, int val
)
199 queue_work(pm2
->charger_wq
, &pm2
->check_main_thermal_prot_work
);
205 static int pm2xxx_charger_die_therm_mngt(struct pm2xxx_charger
*pm2
, int val
)
207 queue_work(pm2
->charger_wq
, &pm2
->check_main_thermal_prot_work
);
212 static int pm2xxx_charger_ovv_mngt(struct pm2xxx_charger
*pm2
, int val
)
214 dev_err(pm2
->dev
, "Overvoltage detected\n");
215 pm2
->flags
.ovv
= true;
216 power_supply_changed(pm2
->ac_chg
.psy
);
218 /* Schedule a new HW failure check */
219 queue_delayed_work(pm2
->charger_wq
, &pm2
->check_hw_failure_work
, 0);
224 static int pm2xxx_charger_wd_exp_mngt(struct pm2xxx_charger
*pm2
, int val
)
226 dev_dbg(pm2
->dev
, "20 minutes watchdog expired\n");
228 pm2
->ac
.wd_expired
= true;
229 power_supply_changed(pm2
->ac_chg
.psy
);
234 static int pm2xxx_charger_vbat_lsig_mngt(struct pm2xxx_charger
*pm2
, int val
)
239 case PM2XXX_INT1_ITVBATLOWR
:
240 dev_dbg(pm2
->dev
, "VBAT grows above VBAT_LOW level\n");
241 /* Enable SW EOC ctrl */
242 ret
= pm2xxx_reg_write(pm2
, PM2XXX_SW_CTRL_REG
,
245 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
250 case PM2XXX_INT1_ITVBATLOWF
:
251 dev_dbg(pm2
->dev
, "VBAT drops below VBAT_LOW level\n");
252 /* Disable SW EOC ctrl */
253 ret
= pm2xxx_reg_write(pm2
, PM2XXX_SW_CTRL_REG
,
256 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
262 dev_err(pm2
->dev
, "Unknown VBAT level\n");
268 static int pm2xxx_charger_bat_disc_mngt(struct pm2xxx_charger
*pm2
, int val
)
270 dev_dbg(pm2
->dev
, "battery disconnected\n");
275 static int pm2xxx_charger_detection(struct pm2xxx_charger
*pm2
, u8
*val
)
279 ret
= pm2xxx_reg_read(pm2
, PM2XXX_SRCE_REG_INT2
, val
);
282 dev_err(pm2
->dev
, "Charger detection failed\n");
286 *val
&= (PM2XXX_INT2_S_ITVPWR1PLUG
| PM2XXX_INT2_S_ITVPWR2PLUG
);
292 static int pm2xxx_charger_itv_pwr_plug_mngt(struct pm2xxx_charger
*pm2
, int val
)
299 * Since we can't be sure that the events are received
300 * synchronously, we have the check if the main charger is
301 * connected by reading the interrupt source register.
303 ret
= pm2xxx_charger_detection(pm2
, &read_val
);
305 if ((ret
== 0) && read_val
) {
306 pm2
->ac
.charger_connected
= 1;
308 queue_work(pm2
->charger_wq
, &pm2
->ac_work
);
315 static int pm2xxx_charger_itv_pwr_unplug_mngt(struct pm2xxx_charger
*pm2
,
318 pm2
->ac
.charger_connected
= 0;
319 queue_work(pm2
->charger_wq
, &pm2
->ac_work
);
324 static int pm2_int_reg0(void *pm2_data
, int val
)
326 struct pm2xxx_charger
*pm2
= pm2_data
;
329 if (val
& PM2XXX_INT1_ITVBATLOWR
) {
330 ret
= pm2xxx_charger_vbat_lsig_mngt(pm2
,
331 PM2XXX_INT1_ITVBATLOWR
);
336 if (val
& PM2XXX_INT1_ITVBATLOWF
) {
337 ret
= pm2xxx_charger_vbat_lsig_mngt(pm2
,
338 PM2XXX_INT1_ITVBATLOWF
);
343 if (val
& PM2XXX_INT1_ITVBATDISCONNECT
) {
344 ret
= pm2xxx_charger_bat_disc_mngt(pm2
,
345 PM2XXX_INT1_ITVBATDISCONNECT
);
353 static int pm2_int_reg1(void *pm2_data
, int val
)
355 struct pm2xxx_charger
*pm2
= pm2_data
;
358 if (val
& (PM2XXX_INT2_ITVPWR1PLUG
| PM2XXX_INT2_ITVPWR2PLUG
)) {
359 dev_dbg(pm2
->dev
, "Main charger plugged\n");
360 ret
= pm2xxx_charger_itv_pwr_plug_mngt(pm2
, val
&
361 (PM2XXX_INT2_ITVPWR1PLUG
| PM2XXX_INT2_ITVPWR2PLUG
));
365 (PM2XXX_INT2_ITVPWR1UNPLUG
| PM2XXX_INT2_ITVPWR2UNPLUG
)) {
366 dev_dbg(pm2
->dev
, "Main charger unplugged\n");
367 ret
= pm2xxx_charger_itv_pwr_unplug_mngt(pm2
, val
&
368 (PM2XXX_INT2_ITVPWR1UNPLUG
|
369 PM2XXX_INT2_ITVPWR2UNPLUG
));
375 static int pm2_int_reg2(void *pm2_data
, int val
)
377 struct pm2xxx_charger
*pm2
= pm2_data
;
380 if (val
& PM2XXX_INT3_ITAUTOTIMEOUTWD
)
381 ret
= pm2xxx_charger_wd_exp_mngt(pm2
, val
);
383 if (val
& (PM2XXX_INT3_ITCHPRECHARGEWD
|
384 PM2XXX_INT3_ITCHCCWD
| PM2XXX_INT3_ITCHCVWD
)) {
386 "Watchdog occurred for precharge, CC and CV charge\n");
392 static int pm2_int_reg3(void *pm2_data
, int val
)
394 struct pm2xxx_charger
*pm2
= pm2_data
;
397 if (val
& (PM2XXX_INT4_ITCHARGINGON
)) {
399 "chargind operation has started\n");
402 if (val
& (PM2XXX_INT4_ITVRESUME
)) {
404 "battery discharged down to VResume threshold\n");
407 if (val
& (PM2XXX_INT4_ITBATTFULL
)) {
408 dev_dbg(pm2
->dev
, "battery fully detected\n");
411 if (val
& (PM2XXX_INT4_ITCVPHASE
)) {
412 dev_dbg(pm2
->dev
, "CV phase enter with 0.5C charging\n");
415 if (val
& (PM2XXX_INT4_ITVPWR2OVV
| PM2XXX_INT4_ITVPWR1OVV
)) {
416 pm2
->failure_case
= VPWR_OVV
;
417 ret
= pm2xxx_charger_ovv_mngt(pm2
, val
&
418 (PM2XXX_INT4_ITVPWR2OVV
| PM2XXX_INT4_ITVPWR1OVV
));
419 dev_dbg(pm2
->dev
, "VPWR/VSYSTEM overvoltage detected\n");
422 if (val
& (PM2XXX_INT4_S_ITBATTEMPCOLD
|
423 PM2XXX_INT4_S_ITBATTEMPHOT
)) {
424 ret
= pm2xxx_charger_batt_therm_mngt(pm2
, val
&
425 (PM2XXX_INT4_S_ITBATTEMPCOLD
|
426 PM2XXX_INT4_S_ITBATTEMPHOT
));
427 dev_dbg(pm2
->dev
, "BTEMP is too Low/High\n");
433 static int pm2_int_reg4(void *pm2_data
, int val
)
435 struct pm2xxx_charger
*pm2
= pm2_data
;
438 if (val
& PM2XXX_INT5_ITVSYSTEMOVV
) {
439 pm2
->failure_case
= VSYSTEM_OVV
;
440 ret
= pm2xxx_charger_ovv_mngt(pm2
, val
&
441 PM2XXX_INT5_ITVSYSTEMOVV
);
442 dev_dbg(pm2
->dev
, "VSYSTEM overvoltage detected\n");
445 if (val
& (PM2XXX_INT5_ITTHERMALWARNINGFALL
|
446 PM2XXX_INT5_ITTHERMALWARNINGRISE
|
447 PM2XXX_INT5_ITTHERMALSHUTDOWNFALL
|
448 PM2XXX_INT5_ITTHERMALSHUTDOWNRISE
)) {
449 dev_dbg(pm2
->dev
, "BTEMP die temperature is too Low/High\n");
450 ret
= pm2xxx_charger_die_therm_mngt(pm2
, val
&
451 (PM2XXX_INT5_ITTHERMALWARNINGFALL
|
452 PM2XXX_INT5_ITTHERMALWARNINGRISE
|
453 PM2XXX_INT5_ITTHERMALSHUTDOWNFALL
|
454 PM2XXX_INT5_ITTHERMALSHUTDOWNRISE
));
460 static int pm2_int_reg5(void *pm2_data
, int val
)
462 struct pm2xxx_charger
*pm2
= pm2_data
;
465 if (val
& (PM2XXX_INT6_ITVPWR2DROP
| PM2XXX_INT6_ITVPWR1DROP
)) {
466 dev_dbg(pm2
->dev
, "VMPWR drop to VBAT level\n");
469 if (val
& (PM2XXX_INT6_ITVPWR2VALIDRISE
|
470 PM2XXX_INT6_ITVPWR1VALIDRISE
|
471 PM2XXX_INT6_ITVPWR2VALIDFALL
|
472 PM2XXX_INT6_ITVPWR1VALIDFALL
)) {
473 dev_dbg(pm2
->dev
, "Falling/Rising edge on WPWR1/2\n");
479 static irqreturn_t
pm2xxx_irq_int(int irq
, void *data
)
481 struct pm2xxx_charger
*pm2
= data
;
482 struct pm2xxx_interrupts
*interrupt
= pm2
->pm2_int
;
485 /* wake up the device */
486 pm_runtime_get_sync(pm2
->dev
);
489 for (i
= 0; i
< PM2XXX_NUM_INT_REG
; i
++) {
491 pm2xxx_interrupt_registers
[i
],
492 &(interrupt
->reg
[i
]));
494 if (interrupt
->reg
[i
] > 0)
495 interrupt
->handler
[i
](pm2
, interrupt
->reg
[i
]);
497 } while (gpio_get_value(pm2
->pdata
->gpio_irq_number
) == 0);
499 pm_runtime_mark_last_busy(pm2
->dev
);
500 pm_runtime_put_autosuspend(pm2
->dev
);
505 static int pm2xxx_charger_get_ac_cv(struct pm2xxx_charger
*pm2
)
510 if (pm2
->ac
.charger_connected
&& pm2
->ac
.charger_online
) {
512 ret
= pm2xxx_reg_read(pm2
, PM2XXX_SRCE_REG_INT4
, &val
);
514 dev_err(pm2
->dev
, "%s pm2xxx read failed\n", __func__
);
518 if (val
& PM2XXX_INT4_S_ITCVPHASE
)
519 ret
= PM2XXX_CONST_VOLT
;
521 ret
= PM2XXX_CONST_CURR
;
527 static int pm2xxx_current_to_regval(int curr
)
531 if (curr
< pm2xxx_charger_current_map
[0])
534 for (i
= 1; i
< ARRAY_SIZE(pm2xxx_charger_current_map
); i
++) {
535 if (curr
< pm2xxx_charger_current_map
[i
])
539 i
= ARRAY_SIZE(pm2xxx_charger_current_map
) - 1;
540 if (curr
== pm2xxx_charger_current_map
[i
])
546 static int pm2xxx_voltage_to_regval(int curr
)
550 if (curr
< pm2xxx_charger_voltage_map
[0])
553 for (i
= 1; i
< ARRAY_SIZE(pm2xxx_charger_voltage_map
); i
++) {
554 if (curr
< pm2xxx_charger_voltage_map
[i
])
558 i
= ARRAY_SIZE(pm2xxx_charger_voltage_map
) - 1;
559 if (curr
== pm2xxx_charger_voltage_map
[i
])
565 static int pm2xxx_charger_update_charger_current(struct ux500_charger
*charger
,
570 struct pm2xxx_charger
*pm2
;
573 if (charger
->psy
->desc
->type
== POWER_SUPPLY_TYPE_MAINS
)
574 pm2
= to_pm2xxx_charger_ac_device_info(charger
);
578 curr_index
= pm2xxx_current_to_regval(ich_out
);
579 if (curr_index
< 0) {
581 "Charger current too high, charging not started\n");
585 ret
= pm2xxx_reg_read(pm2
, PM2XXX_BATT_CTRL_REG6
, &val
);
587 val
&= ~PM2XXX_DIR_CH_CC_CURRENT_MASK
;
589 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG6
, val
);
592 "%s write failed\n", __func__
);
596 dev_err(pm2
->dev
, "%s read failed\n", __func__
);
601 static int pm2xxx_charger_ac_get_property(struct power_supply
*psy
,
602 enum power_supply_property psp
,
603 union power_supply_propval
*val
)
605 struct pm2xxx_charger
*pm2
;
607 pm2
= to_pm2xxx_charger_ac_device_info(psy_to_ux500_charger(psy
));
610 case POWER_SUPPLY_PROP_HEALTH
:
611 if (pm2
->flags
.mainextchnotok
)
612 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
613 else if (pm2
->ac
.wd_expired
)
614 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
615 else if (pm2
->flags
.main_thermal_prot
)
616 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
617 else if (pm2
->flags
.ovv
)
618 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
620 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
622 case POWER_SUPPLY_PROP_ONLINE
:
623 val
->intval
= pm2
->ac
.charger_online
;
625 case POWER_SUPPLY_PROP_PRESENT
:
626 val
->intval
= pm2
->ac
.charger_connected
;
628 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
629 pm2
->ac
.cv_active
= pm2xxx_charger_get_ac_cv(pm2
);
630 val
->intval
= pm2
->ac
.cv_active
;
638 static int pm2xxx_charging_init(struct pm2xxx_charger
*pm2
)
642 /* enable CC and CV watchdog */
643 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG3
,
644 (PM2XXX_CH_WD_CV_PHASE_60MIN
| PM2XXX_CH_WD_CC_PHASE_60MIN
));
648 /* enable precharge watchdog */
649 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG4
,
650 PM2XXX_CH_WD_PRECH_PHASE_60MIN
);
652 /* Disable auto timeout */
653 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG5
,
654 PM2XXX_CH_WD_AUTO_TIMEOUT_20MIN
);
657 * EOC current level = 100mA
658 * Precharge current level = 100mA
659 * CC current level = 1000mA
661 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG6
,
662 (PM2XXX_DIR_CH_CC_CURRENT_1000MA
|
663 PM2XXX_CH_PRECH_CURRENT_100MA
|
664 PM2XXX_CH_EOC_CURRENT_100MA
));
667 * recharge threshold = 3.8V
668 * Precharge to CC threshold = 2.9V
670 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG7
,
671 (PM2XXX_CH_PRECH_VOL_2_9
| PM2XXX_CH_VRESUME_VOL_3_8
));
673 /* float voltage charger level = 4.2V */
674 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG8
,
677 /* Voltage drop between VBAT and VSYS in HW charging = 300mV */
678 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG9
,
679 (PM2XXX_CH_150MV_DROP_300MV
| PM2XXX_CHARCHING_INFO_DIS
|
680 PM2XXX_CH_CC_REDUCED_CURRENT_IDENT
|
681 PM2XXX_CH_CC_MODEDROP_DIS
));
683 /* Input charger level of over voltage = 10V */
684 ret
= pm2xxx_reg_write(pm2
, PM2XXX_INP_VOLT_VPWR2
,
685 PM2XXX_VPWR2_OVV_10
);
686 ret
= pm2xxx_reg_write(pm2
, PM2XXX_INP_VOLT_VPWR1
,
687 PM2XXX_VPWR1_OVV_10
);
689 /* Input charger drop */
690 ret
= pm2xxx_reg_write(pm2
, PM2XXX_INP_DROP_VPWR2
,
691 (PM2XXX_VPWR2_HW_OPT_DIS
| PM2XXX_VPWR2_VALID_DIS
|
692 PM2XXX_VPWR2_DROP_DIS
));
693 ret
= pm2xxx_reg_write(pm2
, PM2XXX_INP_DROP_VPWR1
,
694 (PM2XXX_VPWR1_HW_OPT_DIS
| PM2XXX_VPWR1_VALID_DIS
|
695 PM2XXX_VPWR1_DROP_DIS
));
697 /* Disable battery low monitoring */
698 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_LOW_LEV_COMP_REG
,
699 PM2XXX_VBAT_LOW_MONITORING_ENA
);
704 static int pm2xxx_charger_ac_en(struct ux500_charger
*charger
,
705 int enable
, int vset
, int iset
)
712 struct pm2xxx_charger
*pm2
= to_pm2xxx_charger_ac_device_info(charger
);
715 if (!pm2
->ac
.charger_connected
) {
716 dev_dbg(pm2
->dev
, "AC charger not connected\n");
720 dev_dbg(pm2
->dev
, "Enable AC: %dmV %dmA\n", vset
, iset
);
721 if (!pm2
->vddadc_en_ac
) {
722 ret
= regulator_enable(pm2
->regu
);
725 "Failed to enable vddadc regulator\n");
727 pm2
->vddadc_en_ac
= true;
730 ret
= pm2xxx_charging_init(pm2
);
732 dev_err(pm2
->dev
, "%s charging init failed\n",
737 volt_index
= pm2xxx_voltage_to_regval(vset
);
738 curr_index
= pm2xxx_current_to_regval(iset
);
740 if (volt_index
< 0 || curr_index
< 0) {
742 "Charger voltage or current too high, "
743 "charging not started\n");
747 ret
= pm2xxx_reg_read(pm2
, PM2XXX_BATT_CTRL_REG8
, &val
);
749 dev_err(pm2
->dev
, "%s pm2xxx read failed\n", __func__
);
752 val
&= ~PM2XXX_CH_VOLT_MASK
;
754 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG8
, val
);
756 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
760 ret
= pm2xxx_reg_read(pm2
, PM2XXX_BATT_CTRL_REG6
, &val
);
762 dev_err(pm2
->dev
, "%s pm2xxx read failed\n", __func__
);
765 val
&= ~PM2XXX_DIR_CH_CC_CURRENT_MASK
;
767 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG6
, val
);
769 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
773 if (!pm2
->bat
->enable_overshoot
) {
774 ret
= pm2xxx_reg_read(pm2
, PM2XXX_LED_CTRL_REG
, &val
);
776 dev_err(pm2
->dev
, "%s pm2xxx read failed\n",
780 val
|= PM2XXX_ANTI_OVERSHOOT_EN
;
781 ret
= pm2xxx_reg_write(pm2
, PM2XXX_LED_CTRL_REG
, val
);
783 dev_err(pm2
->dev
, "%s pm2xxx write failed\n",
789 ret
= pm2xxx_charging_enable_mngt(pm2
);
791 dev_err(pm2
->dev
, "Failed to enable"
792 "pm2xxx ac charger\n");
796 pm2
->ac
.charger_online
= 1;
798 pm2
->ac
.charger_online
= 0;
799 pm2
->ac
.wd_expired
= false;
801 /* Disable regulator if enabled */
802 if (pm2
->vddadc_en_ac
) {
803 regulator_disable(pm2
->regu
);
804 pm2
->vddadc_en_ac
= false;
807 ret
= pm2xxx_charging_disable_mngt(pm2
);
809 dev_err(pm2
->dev
, "failed to disable"
810 "pm2xxx ac charger\n");
814 dev_dbg(pm2
->dev
, "PM2301: " "Disabled AC charging\n");
816 power_supply_changed(pm2
->ac_chg
.psy
);
822 static int pm2xxx_charger_watchdog_kick(struct ux500_charger
*charger
)
825 struct pm2xxx_charger
*pm2
;
827 if (charger
->psy
->desc
->type
== POWER_SUPPLY_TYPE_MAINS
)
828 pm2
= to_pm2xxx_charger_ac_device_info(charger
);
832 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_WD_KICK
, WD_TIMER
);
834 dev_err(pm2
->dev
, "Failed to kick WD!\n");
839 static void pm2xxx_charger_ac_work(struct work_struct
*work
)
841 struct pm2xxx_charger
*pm2
= container_of(work
,
842 struct pm2xxx_charger
, ac_work
);
845 power_supply_changed(pm2
->ac_chg
.psy
);
846 sysfs_notify(&pm2
->ac_chg
.psy
->dev
.kobj
, NULL
, "present");
849 static void pm2xxx_charger_check_hw_failure_work(struct work_struct
*work
)
853 struct pm2xxx_charger
*pm2
= container_of(work
,
854 struct pm2xxx_charger
, check_hw_failure_work
.work
);
856 if (pm2
->flags
.ovv
) {
857 pm2xxx_reg_read(pm2
, PM2XXX_SRCE_REG_INT4
, ®_value
);
859 if (!(reg_value
& (PM2XXX_INT4_S_ITVPWR1OVV
|
860 PM2XXX_INT4_S_ITVPWR2OVV
))) {
861 pm2
->flags
.ovv
= false;
862 power_supply_changed(pm2
->ac_chg
.psy
);
866 /* If we still have a failure, schedule a new check */
867 if (pm2
->flags
.ovv
) {
868 queue_delayed_work(pm2
->charger_wq
,
869 &pm2
->check_hw_failure_work
, round_jiffies(HZ
));
873 static void pm2xxx_charger_check_main_thermal_prot_work(
874 struct work_struct
*work
)
879 struct pm2xxx_charger
*pm2
= container_of(work
, struct pm2xxx_charger
,
880 check_main_thermal_prot_work
);
882 /* Check if die temp warning is still active */
883 ret
= pm2xxx_reg_read(pm2
, PM2XXX_SRCE_REG_INT5
, &val
);
885 dev_err(pm2
->dev
, "%s pm2xxx read failed\n", __func__
);
888 if (val
& (PM2XXX_INT5_S_ITTHERMALWARNINGRISE
889 | PM2XXX_INT5_S_ITTHERMALSHUTDOWNRISE
))
890 pm2
->flags
.main_thermal_prot
= true;
891 else if (val
& (PM2XXX_INT5_S_ITTHERMALWARNINGFALL
892 | PM2XXX_INT5_S_ITTHERMALSHUTDOWNFALL
))
893 pm2
->flags
.main_thermal_prot
= false;
895 power_supply_changed(pm2
->ac_chg
.psy
);
898 static struct pm2xxx_interrupts pm2xxx_int
= {
899 .handler
[0] = pm2_int_reg0
,
900 .handler
[1] = pm2_int_reg1
,
901 .handler
[2] = pm2_int_reg2
,
902 .handler
[3] = pm2_int_reg3
,
903 .handler
[4] = pm2_int_reg4
,
904 .handler
[5] = pm2_int_reg5
,
907 static struct pm2xxx_irq pm2xxx_charger_irq
[] = {
908 {"PM2XXX_IRQ_INT", pm2xxx_irq_int
},
911 static int __maybe_unused
pm2xxx_wall_charger_resume(struct device
*dev
)
913 struct i2c_client
*i2c_client
= to_i2c_client(dev
);
914 struct pm2xxx_charger
*pm2
;
916 pm2
= (struct pm2xxx_charger
*)i2c_get_clientdata(i2c_client
);
919 /* If we still have a HW failure, schedule a new check */
921 queue_delayed_work(pm2
->charger_wq
,
922 &pm2
->check_hw_failure_work
, 0);
927 static int __maybe_unused
pm2xxx_wall_charger_suspend(struct device
*dev
)
929 struct i2c_client
*i2c_client
= to_i2c_client(dev
);
930 struct pm2xxx_charger
*pm2
;
932 pm2
= (struct pm2xxx_charger
*)i2c_get_clientdata(i2c_client
);
935 /* Cancel any pending HW failure check */
936 if (delayed_work_pending(&pm2
->check_hw_failure_work
))
937 cancel_delayed_work(&pm2
->check_hw_failure_work
);
939 flush_work(&pm2
->ac_work
);
940 flush_work(&pm2
->check_main_thermal_prot_work
);
945 static int __maybe_unused
pm2xxx_runtime_suspend(struct device
*dev
)
947 struct i2c_client
*pm2xxx_i2c_client
= to_i2c_client(dev
);
948 struct pm2xxx_charger
*pm2
;
950 pm2
= (struct pm2xxx_charger
*)i2c_get_clientdata(pm2xxx_i2c_client
);
956 static int __maybe_unused
pm2xxx_runtime_resume(struct device
*dev
)
958 struct i2c_client
*pm2xxx_i2c_client
= to_i2c_client(dev
);
959 struct pm2xxx_charger
*pm2
;
961 pm2
= (struct pm2xxx_charger
*)i2c_get_clientdata(pm2xxx_i2c_client
);
963 if (gpio_is_valid(pm2
->lpn_pin
) && gpio_get_value(pm2
->lpn_pin
) == 0)
969 static const struct dev_pm_ops pm2xxx_pm_ops __maybe_unused
= {
970 SET_SYSTEM_SLEEP_PM_OPS(pm2xxx_wall_charger_suspend
,
971 pm2xxx_wall_charger_resume
)
972 SET_RUNTIME_PM_OPS(pm2xxx_runtime_suspend
, pm2xxx_runtime_resume
, NULL
)
975 static int pm2xxx_wall_charger_probe(struct i2c_client
*i2c_client
,
976 const struct i2c_device_id
*id
)
978 struct pm2xxx_platform_data
*pl_data
= i2c_client
->dev
.platform_data
;
979 struct power_supply_config psy_cfg
= {};
980 struct pm2xxx_charger
*pm2
;
986 dev_err(&i2c_client
->dev
, "No platform data supplied\n");
990 pm2
= kzalloc(sizeof(struct pm2xxx_charger
), GFP_KERNEL
);
992 dev_err(&i2c_client
->dev
, "pm2xxx_charger allocation failed\n");
996 /* get parent data */
997 pm2
->dev
= &i2c_client
->dev
;
999 pm2
->pm2_int
= &pm2xxx_int
;
1001 /* get charger spcific platform data */
1002 if (!pl_data
->wall_charger
) {
1003 dev_err(pm2
->dev
, "no charger platform data supplied\n");
1005 goto free_device_info
;
1008 pm2
->pdata
= pl_data
->wall_charger
;
1010 /* get battery specific platform data */
1011 if (!pl_data
->battery
) {
1012 dev_err(pm2
->dev
, "no battery platform data supplied\n");
1014 goto free_device_info
;
1017 pm2
->bat
= pl_data
->battery
;
1019 if (!i2c_check_functionality(i2c_client
->adapter
,
1020 I2C_FUNC_SMBUS_BYTE_DATA
|
1021 I2C_FUNC_SMBUS_READ_WORD_DATA
)) {
1023 dev_info(pm2
->dev
, "pm2301 i2c_check_functionality failed\n");
1024 goto free_device_info
;
1027 pm2
->config
.pm2xxx_i2c
= i2c_client
;
1028 pm2
->config
.pm2xxx_id
= (struct i2c_device_id
*) id
;
1029 i2c_set_clientdata(i2c_client
, pm2
);
1032 /* power_supply base class */
1033 pm2
->ac_chg_desc
.name
= pm2
->pdata
->label
;
1034 pm2
->ac_chg_desc
.type
= POWER_SUPPLY_TYPE_MAINS
;
1035 pm2
->ac_chg_desc
.properties
= pm2xxx_charger_ac_props
;
1036 pm2
->ac_chg_desc
.num_properties
= ARRAY_SIZE(pm2xxx_charger_ac_props
);
1037 pm2
->ac_chg_desc
.get_property
= pm2xxx_charger_ac_get_property
;
1039 psy_cfg
.supplied_to
= pm2
->pdata
->supplied_to
;
1040 psy_cfg
.num_supplicants
= pm2
->pdata
->num_supplicants
;
1041 /* pm2xxx_charger sub-class */
1042 pm2
->ac_chg
.ops
.enable
= &pm2xxx_charger_ac_en
;
1043 pm2
->ac_chg
.ops
.kick_wd
= &pm2xxx_charger_watchdog_kick
;
1044 pm2
->ac_chg
.ops
.update_curr
= &pm2xxx_charger_update_charger_current
;
1045 pm2
->ac_chg
.max_out_volt
= pm2xxx_charger_voltage_map
[
1046 ARRAY_SIZE(pm2xxx_charger_voltage_map
) - 1];
1047 pm2
->ac_chg
.max_out_curr
= pm2xxx_charger_current_map
[
1048 ARRAY_SIZE(pm2xxx_charger_current_map
) - 1];
1049 pm2
->ac_chg
.wdt_refresh
= WD_KICK_INTERVAL
;
1050 pm2
->ac_chg
.enabled
= true;
1051 pm2
->ac_chg
.external
= true;
1053 /* Create a work queue for the charger */
1054 pm2
->charger_wq
= alloc_ordered_workqueue("pm2xxx_charger_wq",
1056 if (pm2
->charger_wq
== NULL
) {
1058 dev_err(pm2
->dev
, "failed to create work queue\n");
1059 goto free_device_info
;
1062 /* Init work for charger detection */
1063 INIT_WORK(&pm2
->ac_work
, pm2xxx_charger_ac_work
);
1065 /* Init work for checking HW status */
1066 INIT_WORK(&pm2
->check_main_thermal_prot_work
,
1067 pm2xxx_charger_check_main_thermal_prot_work
);
1069 /* Init work for HW failure check */
1070 INIT_DEFERRABLE_WORK(&pm2
->check_hw_failure_work
,
1071 pm2xxx_charger_check_hw_failure_work
);
1074 * VDD ADC supply needs to be enabled from this driver when there
1075 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
1076 * interrupts during charging
1078 pm2
->regu
= regulator_get(pm2
->dev
, "vddadc");
1079 if (IS_ERR(pm2
->regu
)) {
1080 ret
= PTR_ERR(pm2
->regu
);
1081 dev_err(pm2
->dev
, "failed to get vddadc regulator\n");
1082 goto free_charger_wq
;
1085 /* Register AC charger class */
1086 pm2
->ac_chg
.psy
= power_supply_register(pm2
->dev
, &pm2
->ac_chg_desc
,
1088 if (IS_ERR(pm2
->ac_chg
.psy
)) {
1089 dev_err(pm2
->dev
, "failed to register AC charger\n");
1090 ret
= PTR_ERR(pm2
->ac_chg
.psy
);
1091 goto free_regulator
;
1094 /* Register interrupts */
1095 ret
= request_threaded_irq(gpio_to_irq(pm2
->pdata
->gpio_irq_number
),
1097 pm2xxx_charger_irq
[0].isr
,
1098 pm2
->pdata
->irq_type
,
1099 pm2xxx_charger_irq
[0].name
, pm2
);
1102 dev_err(pm2
->dev
, "failed to request %s IRQ %d: %d\n",
1103 pm2xxx_charger_irq
[0].name
,
1104 gpio_to_irq(pm2
->pdata
->gpio_irq_number
), ret
);
1105 goto unregister_pm2xxx_charger
;
1108 ret
= pm_runtime_set_active(pm2
->dev
);
1110 dev_err(pm2
->dev
, "set active Error\n");
1112 pm_runtime_enable(pm2
->dev
);
1113 pm_runtime_set_autosuspend_delay(pm2
->dev
, PM2XXX_AUTOSUSPEND_DELAY
);
1114 pm_runtime_use_autosuspend(pm2
->dev
);
1115 pm_runtime_resume(pm2
->dev
);
1117 /* pm interrupt can wake up system */
1118 ret
= enable_irq_wake(gpio_to_irq(pm2
->pdata
->gpio_irq_number
));
1120 dev_err(pm2
->dev
, "failed to set irq wake\n");
1121 goto unregister_pm2xxx_interrupt
;
1124 mutex_init(&pm2
->lock
);
1126 if (gpio_is_valid(pm2
->pdata
->lpn_gpio
)) {
1127 /* get lpn GPIO from platform data */
1128 pm2
->lpn_pin
= pm2
->pdata
->lpn_gpio
;
1131 * Charger detection mechanism requires pulling up the LPN pin
1132 * while i2c communication if Charger is not connected
1133 * LPN pin of PM2301 is GPIO60 of AB9540
1135 ret
= gpio_request(pm2
->lpn_pin
, "pm2301_lpm_gpio");
1138 dev_err(pm2
->dev
, "pm2301_lpm_gpio request failed\n");
1139 goto disable_pm2_irq_wake
;
1141 ret
= gpio_direction_output(pm2
->lpn_pin
, 0);
1143 dev_err(pm2
->dev
, "pm2301_lpm_gpio direction failed\n");
1149 /* read interrupt registers */
1150 for (i
= 0; i
< PM2XXX_NUM_INT_REG
; i
++)
1151 pm2xxx_reg_read(pm2
,
1152 pm2xxx_interrupt_registers
[i
],
1155 ret
= pm2xxx_charger_detection(pm2
, &val
);
1157 if ((ret
== 0) && val
) {
1158 pm2
->ac
.charger_connected
= 1;
1159 ab8500_override_turn_on_stat(~AB8500_POW_KEY_1_ON
,
1160 AB8500_MAIN_CH_DET
);
1161 pm2
->ac_conn
= true;
1162 power_supply_changed(pm2
->ac_chg
.psy
);
1163 sysfs_notify(&pm2
->ac_chg
.psy
->dev
.kobj
, NULL
, "present");
1169 if (gpio_is_valid(pm2
->lpn_pin
))
1170 gpio_free(pm2
->lpn_pin
);
1171 disable_pm2_irq_wake
:
1172 disable_irq_wake(gpio_to_irq(pm2
->pdata
->gpio_irq_number
));
1173 unregister_pm2xxx_interrupt
:
1174 /* disable interrupt */
1175 free_irq(gpio_to_irq(pm2
->pdata
->gpio_irq_number
), pm2
);
1176 unregister_pm2xxx_charger
:
1177 /* unregister power supply */
1178 power_supply_unregister(pm2
->ac_chg
.psy
);
1180 /* disable the regulator */
1181 regulator_put(pm2
->regu
);
1183 destroy_workqueue(pm2
->charger_wq
);
1190 static int pm2xxx_wall_charger_remove(struct i2c_client
*i2c_client
)
1192 struct pm2xxx_charger
*pm2
= i2c_get_clientdata(i2c_client
);
1194 /* Disable pm_runtime */
1195 pm_runtime_disable(pm2
->dev
);
1196 /* Disable AC charging */
1197 pm2xxx_charger_ac_en(&pm2
->ac_chg
, false, 0, 0);
1199 /* Disable wake by pm interrupt */
1200 disable_irq_wake(gpio_to_irq(pm2
->pdata
->gpio_irq_number
));
1202 /* Disable interrupts */
1203 free_irq(gpio_to_irq(pm2
->pdata
->gpio_irq_number
), pm2
);
1205 /* Delete the work queue */
1206 destroy_workqueue(pm2
->charger_wq
);
1208 flush_scheduled_work();
1210 /* disable the regulator */
1211 regulator_put(pm2
->regu
);
1213 power_supply_unregister(pm2
->ac_chg
.psy
);
1215 if (gpio_is_valid(pm2
->lpn_pin
))
1216 gpio_free(pm2
->lpn_pin
);
1223 static const struct i2c_device_id pm2xxx_id
[] = {
1228 MODULE_DEVICE_TABLE(i2c
, pm2xxx_id
);
1230 static struct i2c_driver pm2xxx_charger_driver
= {
1231 .probe
= pm2xxx_wall_charger_probe
,
1232 .remove
= pm2xxx_wall_charger_remove
,
1234 .name
= "pm2xxx-wall_charger",
1235 .pm
= IS_ENABLED(CONFIG_PM
) ? &pm2xxx_pm_ops
: NULL
,
1237 .id_table
= pm2xxx_id
,
1240 static int __init
pm2xxx_charger_init(void)
1242 return i2c_add_driver(&pm2xxx_charger_driver
);
1245 static void __exit
pm2xxx_charger_exit(void)
1247 i2c_del_driver(&pm2xxx_charger_driver
);
1250 device_initcall_sync(pm2xxx_charger_init
);
1251 module_exit(pm2xxx_charger_exit
);
1253 MODULE_LICENSE("GPL v2");
1254 MODULE_AUTHOR("Rajkumar kasirajan, Olivier Launay");
1255 MODULE_DESCRIPTION("PM2xxx charger management driver");