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 void set_lpn_pin(struct pm2xxx_charger
*pm2
)
109 if (!pm2
->ac
.charger_connected
&& gpio_is_valid(pm2
->lpn_pin
)) {
110 gpio_set_value(pm2
->lpn_pin
, 1);
111 usleep_range(SLEEP_MIN
, SLEEP_MAX
);
115 static void clear_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
, 0);
121 static int pm2xxx_reg_read(struct pm2xxx_charger
*pm2
, int reg
, u8
*val
)
125 /* wake up the device */
126 pm_runtime_get_sync(pm2
->dev
);
128 ret
= i2c_smbus_read_i2c_block_data(pm2
->config
.pm2xxx_i2c
, reg
,
131 dev_err(pm2
->dev
, "Error reading register at 0x%x\n", reg
);
135 pm_runtime_put_sync(pm2
->dev
);
140 static int pm2xxx_reg_write(struct pm2xxx_charger
*pm2
, int reg
, u8 val
)
144 /* wake up the device */
145 pm_runtime_get_sync(pm2
->dev
);
147 ret
= i2c_smbus_write_i2c_block_data(pm2
->config
.pm2xxx_i2c
, reg
,
150 dev_err(pm2
->dev
, "Error writing register at 0x%x\n", reg
);
154 pm_runtime_put_sync(pm2
->dev
);
159 static int pm2xxx_charging_enable_mngt(struct pm2xxx_charger
*pm2
)
163 /* Enable charging */
164 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG2
,
165 (PM2XXX_CH_AUTO_RESUME_EN
| PM2XXX_CHARGER_ENA
));
170 static int pm2xxx_charging_disable_mngt(struct pm2xxx_charger
*pm2
)
174 /* Disable SW EOC ctrl */
175 ret
= pm2xxx_reg_write(pm2
, PM2XXX_SW_CTRL_REG
, PM2XXX_SWCTRL_HW
);
177 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
181 /* Disable charging */
182 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG2
,
183 (PM2XXX_CH_AUTO_RESUME_DIS
| PM2XXX_CHARGER_DIS
));
185 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
192 static int pm2xxx_charger_batt_therm_mngt(struct pm2xxx_charger
*pm2
, int val
)
194 queue_work(pm2
->charger_wq
, &pm2
->check_main_thermal_prot_work
);
200 static int pm2xxx_charger_die_therm_mngt(struct pm2xxx_charger
*pm2
, int val
)
202 queue_work(pm2
->charger_wq
, &pm2
->check_main_thermal_prot_work
);
207 static int pm2xxx_charger_ovv_mngt(struct pm2xxx_charger
*pm2
, int val
)
209 dev_err(pm2
->dev
, "Overvoltage detected\n");
210 pm2
->flags
.ovv
= true;
211 power_supply_changed(pm2
->ac_chg
.psy
);
213 /* Schedule a new HW failure check */
214 queue_delayed_work(pm2
->charger_wq
, &pm2
->check_hw_failure_work
, 0);
219 static int pm2xxx_charger_wd_exp_mngt(struct pm2xxx_charger
*pm2
, int val
)
221 dev_dbg(pm2
->dev
, "20 minutes watchdog expired\n");
223 pm2
->ac
.wd_expired
= true;
224 power_supply_changed(pm2
->ac_chg
.psy
);
229 static int pm2xxx_charger_vbat_lsig_mngt(struct pm2xxx_charger
*pm2
, int val
)
234 case PM2XXX_INT1_ITVBATLOWR
:
235 dev_dbg(pm2
->dev
, "VBAT grows above VBAT_LOW level\n");
236 /* Enable SW EOC ctrl */
237 ret
= pm2xxx_reg_write(pm2
, PM2XXX_SW_CTRL_REG
,
240 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
245 case PM2XXX_INT1_ITVBATLOWF
:
246 dev_dbg(pm2
->dev
, "VBAT drops below VBAT_LOW level\n");
247 /* Disable SW EOC ctrl */
248 ret
= pm2xxx_reg_write(pm2
, PM2XXX_SW_CTRL_REG
,
251 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
257 dev_err(pm2
->dev
, "Unknown VBAT level\n");
263 static int pm2xxx_charger_bat_disc_mngt(struct pm2xxx_charger
*pm2
, int val
)
265 dev_dbg(pm2
->dev
, "battery disconnected\n");
270 static int pm2xxx_charger_detection(struct pm2xxx_charger
*pm2
, u8
*val
)
274 ret
= pm2xxx_reg_read(pm2
, PM2XXX_SRCE_REG_INT2
, val
);
277 dev_err(pm2
->dev
, "Charger detection failed\n");
281 *val
&= (PM2XXX_INT2_S_ITVPWR1PLUG
| PM2XXX_INT2_S_ITVPWR2PLUG
);
287 static int pm2xxx_charger_itv_pwr_plug_mngt(struct pm2xxx_charger
*pm2
, int val
)
294 * Since we can't be sure that the events are received
295 * synchronously, we have the check if the main charger is
296 * connected by reading the interrupt source register.
298 ret
= pm2xxx_charger_detection(pm2
, &read_val
);
300 if ((ret
== 0) && read_val
) {
301 pm2
->ac
.charger_connected
= 1;
303 queue_work(pm2
->charger_wq
, &pm2
->ac_work
);
310 static int pm2xxx_charger_itv_pwr_unplug_mngt(struct pm2xxx_charger
*pm2
,
313 pm2
->ac
.charger_connected
= 0;
314 queue_work(pm2
->charger_wq
, &pm2
->ac_work
);
319 static int pm2_int_reg0(void *pm2_data
, int val
)
321 struct pm2xxx_charger
*pm2
= pm2_data
;
324 if (val
& PM2XXX_INT1_ITVBATLOWR
) {
325 ret
= pm2xxx_charger_vbat_lsig_mngt(pm2
,
326 PM2XXX_INT1_ITVBATLOWR
);
331 if (val
& PM2XXX_INT1_ITVBATLOWF
) {
332 ret
= pm2xxx_charger_vbat_lsig_mngt(pm2
,
333 PM2XXX_INT1_ITVBATLOWF
);
338 if (val
& PM2XXX_INT1_ITVBATDISCONNECT
) {
339 ret
= pm2xxx_charger_bat_disc_mngt(pm2
,
340 PM2XXX_INT1_ITVBATDISCONNECT
);
348 static int pm2_int_reg1(void *pm2_data
, int val
)
350 struct pm2xxx_charger
*pm2
= pm2_data
;
353 if (val
& (PM2XXX_INT2_ITVPWR1PLUG
| PM2XXX_INT2_ITVPWR2PLUG
)) {
354 dev_dbg(pm2
->dev
, "Main charger plugged\n");
355 ret
= pm2xxx_charger_itv_pwr_plug_mngt(pm2
, val
&
356 (PM2XXX_INT2_ITVPWR1PLUG
| PM2XXX_INT2_ITVPWR2PLUG
));
360 (PM2XXX_INT2_ITVPWR1UNPLUG
| PM2XXX_INT2_ITVPWR2UNPLUG
)) {
361 dev_dbg(pm2
->dev
, "Main charger unplugged\n");
362 ret
= pm2xxx_charger_itv_pwr_unplug_mngt(pm2
, val
&
363 (PM2XXX_INT2_ITVPWR1UNPLUG
|
364 PM2XXX_INT2_ITVPWR2UNPLUG
));
370 static int pm2_int_reg2(void *pm2_data
, int val
)
372 struct pm2xxx_charger
*pm2
= pm2_data
;
375 if (val
& PM2XXX_INT3_ITAUTOTIMEOUTWD
)
376 ret
= pm2xxx_charger_wd_exp_mngt(pm2
, val
);
378 if (val
& (PM2XXX_INT3_ITCHPRECHARGEWD
|
379 PM2XXX_INT3_ITCHCCWD
| PM2XXX_INT3_ITCHCVWD
)) {
381 "Watchdog occurred for precharge, CC and CV charge\n");
387 static int pm2_int_reg3(void *pm2_data
, int val
)
389 struct pm2xxx_charger
*pm2
= pm2_data
;
392 if (val
& (PM2XXX_INT4_ITCHARGINGON
)) {
394 "charging operation has started\n");
397 if (val
& (PM2XXX_INT4_ITVRESUME
)) {
399 "battery discharged down to VResume threshold\n");
402 if (val
& (PM2XXX_INT4_ITBATTFULL
)) {
403 dev_dbg(pm2
->dev
, "battery fully detected\n");
406 if (val
& (PM2XXX_INT4_ITCVPHASE
)) {
407 dev_dbg(pm2
->dev
, "CV phase enter with 0.5C charging\n");
410 if (val
& (PM2XXX_INT4_ITVPWR2OVV
| PM2XXX_INT4_ITVPWR1OVV
)) {
411 pm2
->failure_case
= VPWR_OVV
;
412 ret
= pm2xxx_charger_ovv_mngt(pm2
, val
&
413 (PM2XXX_INT4_ITVPWR2OVV
| PM2XXX_INT4_ITVPWR1OVV
));
414 dev_dbg(pm2
->dev
, "VPWR/VSYSTEM overvoltage detected\n");
417 if (val
& (PM2XXX_INT4_S_ITBATTEMPCOLD
|
418 PM2XXX_INT4_S_ITBATTEMPHOT
)) {
419 ret
= pm2xxx_charger_batt_therm_mngt(pm2
, val
&
420 (PM2XXX_INT4_S_ITBATTEMPCOLD
|
421 PM2XXX_INT4_S_ITBATTEMPHOT
));
422 dev_dbg(pm2
->dev
, "BTEMP is too Low/High\n");
428 static int pm2_int_reg4(void *pm2_data
, int val
)
430 struct pm2xxx_charger
*pm2
= pm2_data
;
433 if (val
& PM2XXX_INT5_ITVSYSTEMOVV
) {
434 pm2
->failure_case
= VSYSTEM_OVV
;
435 ret
= pm2xxx_charger_ovv_mngt(pm2
, val
&
436 PM2XXX_INT5_ITVSYSTEMOVV
);
437 dev_dbg(pm2
->dev
, "VSYSTEM overvoltage detected\n");
440 if (val
& (PM2XXX_INT5_ITTHERMALWARNINGFALL
|
441 PM2XXX_INT5_ITTHERMALWARNINGRISE
|
442 PM2XXX_INT5_ITTHERMALSHUTDOWNFALL
|
443 PM2XXX_INT5_ITTHERMALSHUTDOWNRISE
)) {
444 dev_dbg(pm2
->dev
, "BTEMP die temperature is too Low/High\n");
445 ret
= pm2xxx_charger_die_therm_mngt(pm2
, val
&
446 (PM2XXX_INT5_ITTHERMALWARNINGFALL
|
447 PM2XXX_INT5_ITTHERMALWARNINGRISE
|
448 PM2XXX_INT5_ITTHERMALSHUTDOWNFALL
|
449 PM2XXX_INT5_ITTHERMALSHUTDOWNRISE
));
455 static int pm2_int_reg5(void *pm2_data
, int val
)
457 struct pm2xxx_charger
*pm2
= pm2_data
;
459 if (val
& (PM2XXX_INT6_ITVPWR2DROP
| PM2XXX_INT6_ITVPWR1DROP
)) {
460 dev_dbg(pm2
->dev
, "VMPWR drop to VBAT level\n");
463 if (val
& (PM2XXX_INT6_ITVPWR2VALIDRISE
|
464 PM2XXX_INT6_ITVPWR1VALIDRISE
|
465 PM2XXX_INT6_ITVPWR2VALIDFALL
|
466 PM2XXX_INT6_ITVPWR1VALIDFALL
)) {
467 dev_dbg(pm2
->dev
, "Falling/Rising edge on WPWR1/2\n");
473 static irqreturn_t
pm2xxx_irq_int(int irq
, void *data
)
475 struct pm2xxx_charger
*pm2
= data
;
476 struct pm2xxx_interrupts
*interrupt
= pm2
->pm2_int
;
479 /* wake up the device */
480 pm_runtime_get_sync(pm2
->dev
);
483 for (i
= 0; i
< PM2XXX_NUM_INT_REG
; i
++) {
485 pm2xxx_interrupt_registers
[i
],
486 &(interrupt
->reg
[i
]));
488 if (interrupt
->reg
[i
] > 0)
489 interrupt
->handler
[i
](pm2
, interrupt
->reg
[i
]);
491 } while (gpio_get_value(pm2
->pdata
->gpio_irq_number
) == 0);
493 pm_runtime_mark_last_busy(pm2
->dev
);
494 pm_runtime_put_autosuspend(pm2
->dev
);
499 static int pm2xxx_charger_get_ac_cv(struct pm2xxx_charger
*pm2
)
504 if (pm2
->ac
.charger_connected
&& pm2
->ac
.charger_online
) {
506 ret
= pm2xxx_reg_read(pm2
, PM2XXX_SRCE_REG_INT4
, &val
);
508 dev_err(pm2
->dev
, "%s pm2xxx read failed\n", __func__
);
512 if (val
& PM2XXX_INT4_S_ITCVPHASE
)
513 ret
= PM2XXX_CONST_VOLT
;
515 ret
= PM2XXX_CONST_CURR
;
521 static int pm2xxx_current_to_regval(int curr
)
525 if (curr
< pm2xxx_charger_current_map
[0])
528 for (i
= 1; i
< ARRAY_SIZE(pm2xxx_charger_current_map
); i
++) {
529 if (curr
< pm2xxx_charger_current_map
[i
])
533 i
= ARRAY_SIZE(pm2xxx_charger_current_map
) - 1;
534 if (curr
== pm2xxx_charger_current_map
[i
])
540 static int pm2xxx_voltage_to_regval(int curr
)
544 if (curr
< pm2xxx_charger_voltage_map
[0])
547 for (i
= 1; i
< ARRAY_SIZE(pm2xxx_charger_voltage_map
); i
++) {
548 if (curr
< pm2xxx_charger_voltage_map
[i
])
552 i
= ARRAY_SIZE(pm2xxx_charger_voltage_map
) - 1;
553 if (curr
== pm2xxx_charger_voltage_map
[i
])
559 static int pm2xxx_charger_update_charger_current(struct ux500_charger
*charger
,
564 struct pm2xxx_charger
*pm2
;
567 if (charger
->psy
->desc
->type
== POWER_SUPPLY_TYPE_MAINS
)
568 pm2
= to_pm2xxx_charger_ac_device_info(charger
);
572 curr_index
= pm2xxx_current_to_regval(ich_out
);
573 if (curr_index
< 0) {
575 "Charger current too high, charging not started\n");
579 ret
= pm2xxx_reg_read(pm2
, PM2XXX_BATT_CTRL_REG6
, &val
);
581 val
&= ~PM2XXX_DIR_CH_CC_CURRENT_MASK
;
583 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG6
, val
);
586 "%s write failed\n", __func__
);
590 dev_err(pm2
->dev
, "%s read failed\n", __func__
);
595 static int pm2xxx_charger_ac_get_property(struct power_supply
*psy
,
596 enum power_supply_property psp
,
597 union power_supply_propval
*val
)
599 struct pm2xxx_charger
*pm2
;
601 pm2
= to_pm2xxx_charger_ac_device_info(psy_to_ux500_charger(psy
));
604 case POWER_SUPPLY_PROP_HEALTH
:
605 if (pm2
->flags
.mainextchnotok
)
606 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
607 else if (pm2
->ac
.wd_expired
)
608 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
609 else if (pm2
->flags
.main_thermal_prot
)
610 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
611 else if (pm2
->flags
.ovv
)
612 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
614 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
616 case POWER_SUPPLY_PROP_ONLINE
:
617 val
->intval
= pm2
->ac
.charger_online
;
619 case POWER_SUPPLY_PROP_PRESENT
:
620 val
->intval
= pm2
->ac
.charger_connected
;
622 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
623 pm2
->ac
.cv_active
= pm2xxx_charger_get_ac_cv(pm2
);
624 val
->intval
= pm2
->ac
.cv_active
;
632 static int pm2xxx_charging_init(struct pm2xxx_charger
*pm2
)
636 /* enable CC and CV watchdog */
637 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG3
,
638 (PM2XXX_CH_WD_CV_PHASE_60MIN
| PM2XXX_CH_WD_CC_PHASE_60MIN
));
642 /* enable precharge watchdog */
643 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG4
,
644 PM2XXX_CH_WD_PRECH_PHASE_60MIN
);
646 /* Disable auto timeout */
647 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG5
,
648 PM2XXX_CH_WD_AUTO_TIMEOUT_20MIN
);
651 * EOC current level = 100mA
652 * Precharge current level = 100mA
653 * CC current level = 1000mA
655 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG6
,
656 (PM2XXX_DIR_CH_CC_CURRENT_1000MA
|
657 PM2XXX_CH_PRECH_CURRENT_100MA
|
658 PM2XXX_CH_EOC_CURRENT_100MA
));
661 * recharge threshold = 3.8V
662 * Precharge to CC threshold = 2.9V
664 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG7
,
665 (PM2XXX_CH_PRECH_VOL_2_9
| PM2XXX_CH_VRESUME_VOL_3_8
));
667 /* float voltage charger level = 4.2V */
668 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG8
,
671 /* Voltage drop between VBAT and VSYS in HW charging = 300mV */
672 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG9
,
673 (PM2XXX_CH_150MV_DROP_300MV
| PM2XXX_CHARCHING_INFO_DIS
|
674 PM2XXX_CH_CC_REDUCED_CURRENT_IDENT
|
675 PM2XXX_CH_CC_MODEDROP_DIS
));
677 /* Input charger level of over voltage = 10V */
678 ret
= pm2xxx_reg_write(pm2
, PM2XXX_INP_VOLT_VPWR2
,
679 PM2XXX_VPWR2_OVV_10
);
680 ret
= pm2xxx_reg_write(pm2
, PM2XXX_INP_VOLT_VPWR1
,
681 PM2XXX_VPWR1_OVV_10
);
683 /* Input charger drop */
684 ret
= pm2xxx_reg_write(pm2
, PM2XXX_INP_DROP_VPWR2
,
685 (PM2XXX_VPWR2_HW_OPT_DIS
| PM2XXX_VPWR2_VALID_DIS
|
686 PM2XXX_VPWR2_DROP_DIS
));
687 ret
= pm2xxx_reg_write(pm2
, PM2XXX_INP_DROP_VPWR1
,
688 (PM2XXX_VPWR1_HW_OPT_DIS
| PM2XXX_VPWR1_VALID_DIS
|
689 PM2XXX_VPWR1_DROP_DIS
));
691 /* Disable battery low monitoring */
692 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_LOW_LEV_COMP_REG
,
693 PM2XXX_VBAT_LOW_MONITORING_ENA
);
698 static int pm2xxx_charger_ac_en(struct ux500_charger
*charger
,
699 int enable
, int vset
, int iset
)
706 struct pm2xxx_charger
*pm2
= to_pm2xxx_charger_ac_device_info(charger
);
709 if (!pm2
->ac
.charger_connected
) {
710 dev_dbg(pm2
->dev
, "AC charger not connected\n");
714 dev_dbg(pm2
->dev
, "Enable AC: %dmV %dmA\n", vset
, iset
);
715 if (!pm2
->vddadc_en_ac
) {
716 ret
= regulator_enable(pm2
->regu
);
719 "Failed to enable vddadc regulator\n");
721 pm2
->vddadc_en_ac
= true;
724 ret
= pm2xxx_charging_init(pm2
);
726 dev_err(pm2
->dev
, "%s charging init failed\n",
731 volt_index
= pm2xxx_voltage_to_regval(vset
);
732 curr_index
= pm2xxx_current_to_regval(iset
);
734 if (volt_index
< 0 || curr_index
< 0) {
736 "Charger voltage or current too high, "
737 "charging not started\n");
741 ret
= pm2xxx_reg_read(pm2
, PM2XXX_BATT_CTRL_REG8
, &val
);
743 dev_err(pm2
->dev
, "%s pm2xxx read failed\n", __func__
);
746 val
&= ~PM2XXX_CH_VOLT_MASK
;
748 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG8
, val
);
750 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
754 ret
= pm2xxx_reg_read(pm2
, PM2XXX_BATT_CTRL_REG6
, &val
);
756 dev_err(pm2
->dev
, "%s pm2xxx read failed\n", __func__
);
759 val
&= ~PM2XXX_DIR_CH_CC_CURRENT_MASK
;
761 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_CTRL_REG6
, val
);
763 dev_err(pm2
->dev
, "%s pm2xxx write failed\n", __func__
);
767 if (!pm2
->bat
->enable_overshoot
) {
768 ret
= pm2xxx_reg_read(pm2
, PM2XXX_LED_CTRL_REG
, &val
);
770 dev_err(pm2
->dev
, "%s pm2xxx read failed\n",
774 val
|= PM2XXX_ANTI_OVERSHOOT_EN
;
775 ret
= pm2xxx_reg_write(pm2
, PM2XXX_LED_CTRL_REG
, val
);
777 dev_err(pm2
->dev
, "%s pm2xxx write failed\n",
783 ret
= pm2xxx_charging_enable_mngt(pm2
);
785 dev_err(pm2
->dev
, "Failed to enable"
786 "pm2xxx ac charger\n");
790 pm2
->ac
.charger_online
= 1;
792 pm2
->ac
.charger_online
= 0;
793 pm2
->ac
.wd_expired
= false;
795 /* Disable regulator if enabled */
796 if (pm2
->vddadc_en_ac
) {
797 regulator_disable(pm2
->regu
);
798 pm2
->vddadc_en_ac
= false;
801 ret
= pm2xxx_charging_disable_mngt(pm2
);
803 dev_err(pm2
->dev
, "failed to disable"
804 "pm2xxx ac charger\n");
808 dev_dbg(pm2
->dev
, "PM2301: " "Disabled AC charging\n");
810 power_supply_changed(pm2
->ac_chg
.psy
);
816 static int pm2xxx_charger_watchdog_kick(struct ux500_charger
*charger
)
819 struct pm2xxx_charger
*pm2
;
821 if (charger
->psy
->desc
->type
== POWER_SUPPLY_TYPE_MAINS
)
822 pm2
= to_pm2xxx_charger_ac_device_info(charger
);
826 ret
= pm2xxx_reg_write(pm2
, PM2XXX_BATT_WD_KICK
, WD_TIMER
);
828 dev_err(pm2
->dev
, "Failed to kick WD!\n");
833 static void pm2xxx_charger_ac_work(struct work_struct
*work
)
835 struct pm2xxx_charger
*pm2
= container_of(work
,
836 struct pm2xxx_charger
, ac_work
);
839 power_supply_changed(pm2
->ac_chg
.psy
);
840 sysfs_notify(&pm2
->ac_chg
.psy
->dev
.kobj
, NULL
, "present");
843 static void pm2xxx_charger_check_hw_failure_work(struct work_struct
*work
)
847 struct pm2xxx_charger
*pm2
= container_of(work
,
848 struct pm2xxx_charger
, check_hw_failure_work
.work
);
850 if (pm2
->flags
.ovv
) {
851 pm2xxx_reg_read(pm2
, PM2XXX_SRCE_REG_INT4
, ®_value
);
853 if (!(reg_value
& (PM2XXX_INT4_S_ITVPWR1OVV
|
854 PM2XXX_INT4_S_ITVPWR2OVV
))) {
855 pm2
->flags
.ovv
= false;
856 power_supply_changed(pm2
->ac_chg
.psy
);
860 /* If we still have a failure, schedule a new check */
861 if (pm2
->flags
.ovv
) {
862 queue_delayed_work(pm2
->charger_wq
,
863 &pm2
->check_hw_failure_work
, round_jiffies(HZ
));
867 static void pm2xxx_charger_check_main_thermal_prot_work(
868 struct work_struct
*work
)
873 struct pm2xxx_charger
*pm2
= container_of(work
, struct pm2xxx_charger
,
874 check_main_thermal_prot_work
);
876 /* Check if die temp warning is still active */
877 ret
= pm2xxx_reg_read(pm2
, PM2XXX_SRCE_REG_INT5
, &val
);
879 dev_err(pm2
->dev
, "%s pm2xxx read failed\n", __func__
);
882 if (val
& (PM2XXX_INT5_S_ITTHERMALWARNINGRISE
883 | PM2XXX_INT5_S_ITTHERMALSHUTDOWNRISE
))
884 pm2
->flags
.main_thermal_prot
= true;
885 else if (val
& (PM2XXX_INT5_S_ITTHERMALWARNINGFALL
886 | PM2XXX_INT5_S_ITTHERMALSHUTDOWNFALL
))
887 pm2
->flags
.main_thermal_prot
= false;
889 power_supply_changed(pm2
->ac_chg
.psy
);
892 static struct pm2xxx_interrupts pm2xxx_int
= {
893 .handler
[0] = pm2_int_reg0
,
894 .handler
[1] = pm2_int_reg1
,
895 .handler
[2] = pm2_int_reg2
,
896 .handler
[3] = pm2_int_reg3
,
897 .handler
[4] = pm2_int_reg4
,
898 .handler
[5] = pm2_int_reg5
,
901 static struct pm2xxx_irq pm2xxx_charger_irq
[] = {
902 {"PM2XXX_IRQ_INT", pm2xxx_irq_int
},
905 static int __maybe_unused
pm2xxx_wall_charger_resume(struct device
*dev
)
907 struct i2c_client
*i2c_client
= to_i2c_client(dev
);
908 struct pm2xxx_charger
*pm2
;
910 pm2
= (struct pm2xxx_charger
*)i2c_get_clientdata(i2c_client
);
913 /* If we still have a HW failure, schedule a new check */
915 queue_delayed_work(pm2
->charger_wq
,
916 &pm2
->check_hw_failure_work
, 0);
921 static int __maybe_unused
pm2xxx_wall_charger_suspend(struct device
*dev
)
923 struct i2c_client
*i2c_client
= to_i2c_client(dev
);
924 struct pm2xxx_charger
*pm2
;
926 pm2
= (struct pm2xxx_charger
*)i2c_get_clientdata(i2c_client
);
929 /* Cancel any pending HW failure check */
930 if (delayed_work_pending(&pm2
->check_hw_failure_work
))
931 cancel_delayed_work(&pm2
->check_hw_failure_work
);
933 flush_work(&pm2
->ac_work
);
934 flush_work(&pm2
->check_main_thermal_prot_work
);
939 static int __maybe_unused
pm2xxx_runtime_suspend(struct device
*dev
)
941 struct i2c_client
*pm2xxx_i2c_client
= to_i2c_client(dev
);
942 struct pm2xxx_charger
*pm2
;
944 pm2
= (struct pm2xxx_charger
*)i2c_get_clientdata(pm2xxx_i2c_client
);
950 static int __maybe_unused
pm2xxx_runtime_resume(struct device
*dev
)
952 struct i2c_client
*pm2xxx_i2c_client
= to_i2c_client(dev
);
953 struct pm2xxx_charger
*pm2
;
955 pm2
= (struct pm2xxx_charger
*)i2c_get_clientdata(pm2xxx_i2c_client
);
957 if (gpio_is_valid(pm2
->lpn_pin
) && gpio_get_value(pm2
->lpn_pin
) == 0)
963 static const struct dev_pm_ops pm2xxx_pm_ops __maybe_unused
= {
964 SET_SYSTEM_SLEEP_PM_OPS(pm2xxx_wall_charger_suspend
,
965 pm2xxx_wall_charger_resume
)
966 SET_RUNTIME_PM_OPS(pm2xxx_runtime_suspend
, pm2xxx_runtime_resume
, NULL
)
969 static int pm2xxx_wall_charger_probe(struct i2c_client
*i2c_client
,
970 const struct i2c_device_id
*id
)
972 struct pm2xxx_platform_data
*pl_data
= i2c_client
->dev
.platform_data
;
973 struct power_supply_config psy_cfg
= {};
974 struct pm2xxx_charger
*pm2
;
980 dev_err(&i2c_client
->dev
, "No platform data supplied\n");
984 pm2
= kzalloc(sizeof(struct pm2xxx_charger
), GFP_KERNEL
);
986 dev_err(&i2c_client
->dev
, "pm2xxx_charger allocation failed\n");
990 /* get parent data */
991 pm2
->dev
= &i2c_client
->dev
;
993 pm2
->pm2_int
= &pm2xxx_int
;
995 /* get charger spcific platform data */
996 if (!pl_data
->wall_charger
) {
997 dev_err(pm2
->dev
, "no charger platform data supplied\n");
999 goto free_device_info
;
1002 pm2
->pdata
= pl_data
->wall_charger
;
1004 /* get battery specific platform data */
1005 if (!pl_data
->battery
) {
1006 dev_err(pm2
->dev
, "no battery platform data supplied\n");
1008 goto free_device_info
;
1011 pm2
->bat
= pl_data
->battery
;
1013 if (!i2c_check_functionality(i2c_client
->adapter
,
1014 I2C_FUNC_SMBUS_BYTE_DATA
|
1015 I2C_FUNC_SMBUS_READ_WORD_DATA
)) {
1017 dev_info(pm2
->dev
, "pm2301 i2c_check_functionality failed\n");
1018 goto free_device_info
;
1021 pm2
->config
.pm2xxx_i2c
= i2c_client
;
1022 pm2
->config
.pm2xxx_id
= (struct i2c_device_id
*) id
;
1023 i2c_set_clientdata(i2c_client
, pm2
);
1026 /* power_supply base class */
1027 pm2
->ac_chg_desc
.name
= pm2
->pdata
->label
;
1028 pm2
->ac_chg_desc
.type
= POWER_SUPPLY_TYPE_MAINS
;
1029 pm2
->ac_chg_desc
.properties
= pm2xxx_charger_ac_props
;
1030 pm2
->ac_chg_desc
.num_properties
= ARRAY_SIZE(pm2xxx_charger_ac_props
);
1031 pm2
->ac_chg_desc
.get_property
= pm2xxx_charger_ac_get_property
;
1033 psy_cfg
.supplied_to
= pm2
->pdata
->supplied_to
;
1034 psy_cfg
.num_supplicants
= pm2
->pdata
->num_supplicants
;
1035 /* pm2xxx_charger sub-class */
1036 pm2
->ac_chg
.ops
.enable
= &pm2xxx_charger_ac_en
;
1037 pm2
->ac_chg
.ops
.kick_wd
= &pm2xxx_charger_watchdog_kick
;
1038 pm2
->ac_chg
.ops
.update_curr
= &pm2xxx_charger_update_charger_current
;
1039 pm2
->ac_chg
.max_out_volt
= pm2xxx_charger_voltage_map
[
1040 ARRAY_SIZE(pm2xxx_charger_voltage_map
) - 1];
1041 pm2
->ac_chg
.max_out_curr
= pm2xxx_charger_current_map
[
1042 ARRAY_SIZE(pm2xxx_charger_current_map
) - 1];
1043 pm2
->ac_chg
.wdt_refresh
= WD_KICK_INTERVAL
;
1044 pm2
->ac_chg
.enabled
= true;
1045 pm2
->ac_chg
.external
= true;
1047 /* Create a work queue for the charger */
1048 pm2
->charger_wq
= alloc_ordered_workqueue("pm2xxx_charger_wq",
1050 if (pm2
->charger_wq
== NULL
) {
1052 dev_err(pm2
->dev
, "failed to create work queue\n");
1053 goto free_device_info
;
1056 /* Init work for charger detection */
1057 INIT_WORK(&pm2
->ac_work
, pm2xxx_charger_ac_work
);
1059 /* Init work for checking HW status */
1060 INIT_WORK(&pm2
->check_main_thermal_prot_work
,
1061 pm2xxx_charger_check_main_thermal_prot_work
);
1063 /* Init work for HW failure check */
1064 INIT_DEFERRABLE_WORK(&pm2
->check_hw_failure_work
,
1065 pm2xxx_charger_check_hw_failure_work
);
1068 * VDD ADC supply needs to be enabled from this driver when there
1069 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
1070 * interrupts during charging
1072 pm2
->regu
= regulator_get(pm2
->dev
, "vddadc");
1073 if (IS_ERR(pm2
->regu
)) {
1074 ret
= PTR_ERR(pm2
->regu
);
1075 dev_err(pm2
->dev
, "failed to get vddadc regulator\n");
1076 goto free_charger_wq
;
1079 /* Register AC charger class */
1080 pm2
->ac_chg
.psy
= power_supply_register(pm2
->dev
, &pm2
->ac_chg_desc
,
1082 if (IS_ERR(pm2
->ac_chg
.psy
)) {
1083 dev_err(pm2
->dev
, "failed to register AC charger\n");
1084 ret
= PTR_ERR(pm2
->ac_chg
.psy
);
1085 goto free_regulator
;
1088 /* Register interrupts */
1089 ret
= request_threaded_irq(gpio_to_irq(pm2
->pdata
->gpio_irq_number
),
1091 pm2xxx_charger_irq
[0].isr
,
1092 pm2
->pdata
->irq_type
,
1093 pm2xxx_charger_irq
[0].name
, pm2
);
1096 dev_err(pm2
->dev
, "failed to request %s IRQ %d: %d\n",
1097 pm2xxx_charger_irq
[0].name
,
1098 gpio_to_irq(pm2
->pdata
->gpio_irq_number
), ret
);
1099 goto unregister_pm2xxx_charger
;
1102 ret
= pm_runtime_set_active(pm2
->dev
);
1104 dev_err(pm2
->dev
, "set active Error\n");
1106 pm_runtime_enable(pm2
->dev
);
1107 pm_runtime_set_autosuspend_delay(pm2
->dev
, PM2XXX_AUTOSUSPEND_DELAY
);
1108 pm_runtime_use_autosuspend(pm2
->dev
);
1109 pm_runtime_resume(pm2
->dev
);
1111 /* pm interrupt can wake up system */
1112 ret
= enable_irq_wake(gpio_to_irq(pm2
->pdata
->gpio_irq_number
));
1114 dev_err(pm2
->dev
, "failed to set irq wake\n");
1115 goto unregister_pm2xxx_interrupt
;
1118 mutex_init(&pm2
->lock
);
1120 if (gpio_is_valid(pm2
->pdata
->lpn_gpio
)) {
1121 /* get lpn GPIO from platform data */
1122 pm2
->lpn_pin
= pm2
->pdata
->lpn_gpio
;
1125 * Charger detection mechanism requires pulling up the LPN pin
1126 * while i2c communication if Charger is not connected
1127 * LPN pin of PM2301 is GPIO60 of AB9540
1129 ret
= gpio_request(pm2
->lpn_pin
, "pm2301_lpm_gpio");
1132 dev_err(pm2
->dev
, "pm2301_lpm_gpio request failed\n");
1133 goto disable_pm2_irq_wake
;
1135 ret
= gpio_direction_output(pm2
->lpn_pin
, 0);
1137 dev_err(pm2
->dev
, "pm2301_lpm_gpio direction failed\n");
1143 /* read interrupt registers */
1144 for (i
= 0; i
< PM2XXX_NUM_INT_REG
; i
++)
1145 pm2xxx_reg_read(pm2
,
1146 pm2xxx_interrupt_registers
[i
],
1149 ret
= pm2xxx_charger_detection(pm2
, &val
);
1151 if ((ret
== 0) && val
) {
1152 pm2
->ac
.charger_connected
= 1;
1153 ab8500_override_turn_on_stat(~AB8500_POW_KEY_1_ON
,
1154 AB8500_MAIN_CH_DET
);
1155 pm2
->ac_conn
= true;
1156 power_supply_changed(pm2
->ac_chg
.psy
);
1157 sysfs_notify(&pm2
->ac_chg
.psy
->dev
.kobj
, NULL
, "present");
1163 if (gpio_is_valid(pm2
->lpn_pin
))
1164 gpio_free(pm2
->lpn_pin
);
1165 disable_pm2_irq_wake
:
1166 disable_irq_wake(gpio_to_irq(pm2
->pdata
->gpio_irq_number
));
1167 unregister_pm2xxx_interrupt
:
1168 /* disable interrupt */
1169 free_irq(gpio_to_irq(pm2
->pdata
->gpio_irq_number
), pm2
);
1170 unregister_pm2xxx_charger
:
1171 /* unregister power supply */
1172 power_supply_unregister(pm2
->ac_chg
.psy
);
1174 /* disable the regulator */
1175 regulator_put(pm2
->regu
);
1177 destroy_workqueue(pm2
->charger_wq
);
1184 static int pm2xxx_wall_charger_remove(struct i2c_client
*i2c_client
)
1186 struct pm2xxx_charger
*pm2
= i2c_get_clientdata(i2c_client
);
1188 /* Disable pm_runtime */
1189 pm_runtime_disable(pm2
->dev
);
1190 /* Disable AC charging */
1191 pm2xxx_charger_ac_en(&pm2
->ac_chg
, false, 0, 0);
1193 /* Disable wake by pm interrupt */
1194 disable_irq_wake(gpio_to_irq(pm2
->pdata
->gpio_irq_number
));
1196 /* Disable interrupts */
1197 free_irq(gpio_to_irq(pm2
->pdata
->gpio_irq_number
), pm2
);
1199 /* Delete the work queue */
1200 destroy_workqueue(pm2
->charger_wq
);
1202 flush_scheduled_work();
1204 /* disable the regulator */
1205 regulator_put(pm2
->regu
);
1207 power_supply_unregister(pm2
->ac_chg
.psy
);
1209 if (gpio_is_valid(pm2
->lpn_pin
))
1210 gpio_free(pm2
->lpn_pin
);
1217 static const struct i2c_device_id pm2xxx_id
[] = {
1222 MODULE_DEVICE_TABLE(i2c
, pm2xxx_id
);
1224 static struct i2c_driver pm2xxx_charger_driver
= {
1225 .probe
= pm2xxx_wall_charger_probe
,
1226 .remove
= pm2xxx_wall_charger_remove
,
1228 .name
= "pm2xxx-wall_charger",
1229 .pm
= IS_ENABLED(CONFIG_PM
) ? &pm2xxx_pm_ops
: NULL
,
1231 .id_table
= pm2xxx_id
,
1234 static int __init
pm2xxx_charger_init(void)
1236 return i2c_add_driver(&pm2xxx_charger_driver
);
1239 static void __exit
pm2xxx_charger_exit(void)
1241 i2c_del_driver(&pm2xxx_charger_driver
);
1244 device_initcall_sync(pm2xxx_charger_init
);
1245 module_exit(pm2xxx_charger_exit
);
1247 MODULE_LICENSE("GPL v2");
1248 MODULE_AUTHOR("Rajkumar kasirajan, Olivier Launay");
1249 MODULE_DESCRIPTION("PM2xxx charger management driver");