treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / power / supply / pm2301_charger.c
blob17749fc90e16514237d107f69c93c828a4864005
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2012 ST Ericsson.
5 * Power supply driver for ST Ericsson pm2xxx_charger charger
6 */
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>
26 #include <linux/pm.h>
28 #include "pm2301_charger.h"
30 #define to_pm2xxx_charger_ac_device_info(x) container_of((x), \
31 struct pm2xxx_charger, ac_chg)
32 #define SLEEP_MIN 50
33 #define SLEEP_MAX 100
34 #define PM2XXX_AUTOSUSPEND_DELAY 500
36 static int pm2xxx_interrupt_registers[] = {
37 PM2XXX_REG_INT1,
38 PM2XXX_REG_INT2,
39 PM2XXX_REG_INT3,
40 PM2XXX_REG_INT4,
41 PM2XXX_REG_INT5,
42 PM2XXX_REG_INT6,
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[] = {
53 3500,
54 3525,
55 3550,
56 3575,
57 3600,
58 3625,
59 3650,
60 3675,
61 3700,
62 3725,
63 3750,
64 3775,
65 3800,
66 3825,
67 3850,
68 3875,
69 3900,
70 3925,
71 3950,
72 3975,
73 4000,
74 4025,
75 4050,
76 4075,
77 4100,
78 4125,
79 4150,
80 4175,
81 4200,
82 4225,
83 4250,
84 4275,
85 4300,
88 static int pm2xxx_charger_current_map[] = {
89 200,
90 200,
91 400,
92 600,
93 800,
94 1000,
95 1200,
96 1400,
97 1600,
98 1800,
99 2000,
100 2200,
101 2400,
102 2600,
103 2800,
104 3000,
107 static const struct i2c_device_id pm2xxx_ident[] = {
108 { "pm2301", 0 },
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)
128 int ret;
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,
134 1, val);
135 if (ret < 0)
136 dev_err(pm2->dev, "Error reading register at 0x%x\n", reg);
137 else
138 ret = 0;
140 pm_runtime_put_sync(pm2->dev);
142 return ret;
145 static int pm2xxx_reg_write(struct pm2xxx_charger *pm2, int reg, u8 val)
147 int ret;
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,
153 1, &val);
154 if (ret < 0)
155 dev_err(pm2->dev, "Error writing register at 0x%x\n", reg);
156 else
157 ret = 0;
159 pm_runtime_put_sync(pm2->dev);
161 return ret;
164 static int pm2xxx_charging_enable_mngt(struct pm2xxx_charger *pm2)
166 int ret;
168 /* Enable charging */
169 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2,
170 (PM2XXX_CH_AUTO_RESUME_EN | PM2XXX_CHARGER_ENA));
172 return ret;
175 static int pm2xxx_charging_disable_mngt(struct pm2xxx_charger *pm2)
177 int ret;
179 /* Disable SW EOC ctrl */
180 ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG, PM2XXX_SWCTRL_HW);
181 if (ret < 0) {
182 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
183 return ret;
186 /* Disable charging */
187 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2,
188 (PM2XXX_CH_AUTO_RESUME_DIS | PM2XXX_CHARGER_DIS));
189 if (ret < 0) {
190 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
191 return ret;
194 return 0;
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);
201 return 0;
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);
209 return 0;
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);
221 return 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);
231 return 0;
234 static int pm2xxx_charger_vbat_lsig_mngt(struct pm2xxx_charger *pm2, int val)
236 int ret;
238 switch (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,
243 PM2XXX_SWCTRL_SW);
244 if (ret < 0) {
245 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
246 return ret;
248 break;
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,
254 PM2XXX_SWCTRL_HW);
255 if (ret < 0) {
256 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
257 return ret;
259 break;
261 default:
262 dev_err(pm2->dev, "Unknown VBAT level\n");
265 return 0;
268 static int pm2xxx_charger_bat_disc_mngt(struct pm2xxx_charger *pm2, int val)
270 dev_dbg(pm2->dev, "battery disconnected\n");
272 return 0;
275 static int pm2xxx_charger_detection(struct pm2xxx_charger *pm2, u8 *val)
277 int ret;
279 ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT2, val);
281 if (ret < 0) {
282 dev_err(pm2->dev, "Charger detection failed\n");
283 goto out;
286 *val &= (PM2XXX_INT2_S_ITVPWR1PLUG | PM2XXX_INT2_S_ITVPWR2PLUG);
288 out:
289 return ret;
292 static int pm2xxx_charger_itv_pwr_plug_mngt(struct pm2xxx_charger *pm2, int val)
295 int ret;
296 u8 read_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;
307 pm2->ac_conn = true;
308 queue_work(pm2->charger_wq, &pm2->ac_work);
312 return ret;
315 static int pm2xxx_charger_itv_pwr_unplug_mngt(struct pm2xxx_charger *pm2,
316 int val)
318 pm2->ac.charger_connected = 0;
319 queue_work(pm2->charger_wq, &pm2->ac_work);
321 return 0;
324 static int pm2_int_reg0(void *pm2_data, int val)
326 struct pm2xxx_charger *pm2 = pm2_data;
327 int ret = 0;
329 if (val & PM2XXX_INT1_ITVBATLOWR) {
330 ret = pm2xxx_charger_vbat_lsig_mngt(pm2,
331 PM2XXX_INT1_ITVBATLOWR);
332 if (ret < 0)
333 goto out;
336 if (val & PM2XXX_INT1_ITVBATLOWF) {
337 ret = pm2xxx_charger_vbat_lsig_mngt(pm2,
338 PM2XXX_INT1_ITVBATLOWF);
339 if (ret < 0)
340 goto out;
343 if (val & PM2XXX_INT1_ITVBATDISCONNECT) {
344 ret = pm2xxx_charger_bat_disc_mngt(pm2,
345 PM2XXX_INT1_ITVBATDISCONNECT);
346 if (ret < 0)
347 goto out;
349 out:
350 return ret;
353 static int pm2_int_reg1(void *pm2_data, int val)
355 struct pm2xxx_charger *pm2 = pm2_data;
356 int ret = 0;
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));
364 if (val &
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));
372 return ret;
375 static int pm2_int_reg2(void *pm2_data, int val)
377 struct pm2xxx_charger *pm2 = pm2_data;
378 int ret = 0;
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)) {
385 dev_dbg(pm2->dev,
386 "Watchdog occurred for precharge, CC and CV charge\n");
389 return ret;
392 static int pm2_int_reg3(void *pm2_data, int val)
394 struct pm2xxx_charger *pm2 = pm2_data;
395 int ret = 0;
397 if (val & (PM2XXX_INT4_ITCHARGINGON)) {
398 dev_dbg(pm2->dev ,
399 "chargind operation has started\n");
402 if (val & (PM2XXX_INT4_ITVRESUME)) {
403 dev_dbg(pm2->dev,
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");
430 return ret;
433 static int pm2_int_reg4(void *pm2_data, int val)
435 struct pm2xxx_charger *pm2 = pm2_data;
436 int ret = 0;
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));
457 return ret;
460 static int pm2_int_reg5(void *pm2_data, int val)
462 struct pm2xxx_charger *pm2 = pm2_data;
463 int ret = 0;
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");
476 return ret;
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;
483 int i;
485 /* wake up the device */
486 pm_runtime_get_sync(pm2->dev);
488 do {
489 for (i = 0; i < PM2XXX_NUM_INT_REG; i++) {
490 pm2xxx_reg_read(pm2,
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);
502 return IRQ_HANDLED;
505 static int pm2xxx_charger_get_ac_cv(struct pm2xxx_charger *pm2)
507 int ret = 0;
508 u8 val;
510 if (pm2->ac.charger_connected && pm2->ac.charger_online) {
512 ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT4, &val);
513 if (ret < 0) {
514 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
515 goto out;
518 if (val & PM2XXX_INT4_S_ITCVPHASE)
519 ret = PM2XXX_CONST_VOLT;
520 else
521 ret = PM2XXX_CONST_CURR;
523 out:
524 return ret;
527 static int pm2xxx_current_to_regval(int curr)
529 int i;
531 if (curr < pm2xxx_charger_current_map[0])
532 return 0;
534 for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_current_map); i++) {
535 if (curr < pm2xxx_charger_current_map[i])
536 return (i - 1);
539 i = ARRAY_SIZE(pm2xxx_charger_current_map) - 1;
540 if (curr == pm2xxx_charger_current_map[i])
541 return i;
542 else
543 return -EINVAL;
546 static int pm2xxx_voltage_to_regval(int curr)
548 int i;
550 if (curr < pm2xxx_charger_voltage_map[0])
551 return 0;
553 for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_voltage_map); i++) {
554 if (curr < pm2xxx_charger_voltage_map[i])
555 return i - 1;
558 i = ARRAY_SIZE(pm2xxx_charger_voltage_map) - 1;
559 if (curr == pm2xxx_charger_voltage_map[i])
560 return i;
561 else
562 return -EINVAL;
565 static int pm2xxx_charger_update_charger_current(struct ux500_charger *charger,
566 int ich_out)
568 int ret;
569 int curr_index;
570 struct pm2xxx_charger *pm2;
571 u8 val;
573 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
574 pm2 = to_pm2xxx_charger_ac_device_info(charger);
575 else
576 return -ENXIO;
578 curr_index = pm2xxx_current_to_regval(ich_out);
579 if (curr_index < 0) {
580 dev_err(pm2->dev,
581 "Charger current too high, charging not started\n");
582 return -ENXIO;
585 ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val);
586 if (ret >= 0) {
587 val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK;
588 val |= curr_index;
589 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val);
590 if (ret < 0) {
591 dev_err(pm2->dev,
592 "%s write failed\n", __func__);
595 else
596 dev_err(pm2->dev, "%s read failed\n", __func__);
598 return ret;
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));
609 switch (psp) {
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;
619 else
620 val->intval = POWER_SUPPLY_HEALTH_GOOD;
621 break;
622 case POWER_SUPPLY_PROP_ONLINE:
623 val->intval = pm2->ac.charger_online;
624 break;
625 case POWER_SUPPLY_PROP_PRESENT:
626 val->intval = pm2->ac.charger_connected;
627 break;
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;
631 break;
632 default:
633 return -EINVAL;
635 return 0;
638 static int pm2xxx_charging_init(struct pm2xxx_charger *pm2)
640 int ret = 0;
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));
645 if( ret < 0)
646 return ret;
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,
675 PM2XXX_CH_VOLT_4_2);
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);
701 return ret;
704 static int pm2xxx_charger_ac_en(struct ux500_charger *charger,
705 int enable, int vset, int iset)
707 int ret;
708 int volt_index;
709 int curr_index;
710 u8 val;
712 struct pm2xxx_charger *pm2 = to_pm2xxx_charger_ac_device_info(charger);
714 if (enable) {
715 if (!pm2->ac.charger_connected) {
716 dev_dbg(pm2->dev, "AC charger not connected\n");
717 return -ENXIO;
720 dev_dbg(pm2->dev, "Enable AC: %dmV %dmA\n", vset, iset);
721 if (!pm2->vddadc_en_ac) {
722 ret = regulator_enable(pm2->regu);
723 if (ret)
724 dev_warn(pm2->dev,
725 "Failed to enable vddadc regulator\n");
726 else
727 pm2->vddadc_en_ac = true;
730 ret = pm2xxx_charging_init(pm2);
731 if (ret < 0) {
732 dev_err(pm2->dev, "%s charging init failed\n",
733 __func__);
734 goto error_occured;
737 volt_index = pm2xxx_voltage_to_regval(vset);
738 curr_index = pm2xxx_current_to_regval(iset);
740 if (volt_index < 0 || curr_index < 0) {
741 dev_err(pm2->dev,
742 "Charger voltage or current too high, "
743 "charging not started\n");
744 return -ENXIO;
747 ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG8, &val);
748 if (ret < 0) {
749 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
750 goto error_occured;
752 val &= ~PM2XXX_CH_VOLT_MASK;
753 val |= volt_index;
754 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG8, val);
755 if (ret < 0) {
756 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
757 goto error_occured;
760 ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val);
761 if (ret < 0) {
762 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
763 goto error_occured;
765 val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK;
766 val |= curr_index;
767 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val);
768 if (ret < 0) {
769 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
770 goto error_occured;
773 if (!pm2->bat->enable_overshoot) {
774 ret = pm2xxx_reg_read(pm2, PM2XXX_LED_CTRL_REG, &val);
775 if (ret < 0) {
776 dev_err(pm2->dev, "%s pm2xxx read failed\n",
777 __func__);
778 goto error_occured;
780 val |= PM2XXX_ANTI_OVERSHOOT_EN;
781 ret = pm2xxx_reg_write(pm2, PM2XXX_LED_CTRL_REG, val);
782 if (ret < 0) {
783 dev_err(pm2->dev, "%s pm2xxx write failed\n",
784 __func__);
785 goto error_occured;
789 ret = pm2xxx_charging_enable_mngt(pm2);
790 if (ret < 0) {
791 dev_err(pm2->dev, "Failed to enable"
792 "pm2xxx ac charger\n");
793 goto error_occured;
796 pm2->ac.charger_online = 1;
797 } else {
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);
808 if (ret < 0) {
809 dev_err(pm2->dev, "failed to disable"
810 "pm2xxx ac charger\n");
811 goto error_occured;
814 dev_dbg(pm2->dev, "PM2301: " "Disabled AC charging\n");
816 power_supply_changed(pm2->ac_chg.psy);
818 error_occured:
819 return ret;
822 static int pm2xxx_charger_watchdog_kick(struct ux500_charger *charger)
824 int ret;
825 struct pm2xxx_charger *pm2;
827 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
828 pm2 = to_pm2xxx_charger_ac_device_info(charger);
829 else
830 return -ENXIO;
832 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_WD_KICK, WD_TIMER);
833 if (ret)
834 dev_err(pm2->dev, "Failed to kick WD!\n");
836 return ret;
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)
851 u8 reg_value;
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, &reg_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)
876 int ret;
877 u8 val;
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);
884 if (ret < 0) {
885 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
886 return;
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);
917 set_lpn_pin(pm2);
919 /* If we still have a HW failure, schedule a new check */
920 if (pm2->flags.ovv)
921 queue_delayed_work(pm2->charger_wq,
922 &pm2->check_hw_failure_work, 0);
924 return 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);
933 clear_lpn_pin(pm2);
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);
942 return 0;
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);
951 clear_lpn_pin(pm2);
953 return 0;
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)
964 set_lpn_pin(pm2);
966 return 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;
981 int ret = 0;
982 u8 val;
983 int i;
985 if (!pl_data) {
986 dev_err(&i2c_client->dev, "No platform data supplied\n");
987 return -EINVAL;
990 pm2 = kzalloc(sizeof(struct pm2xxx_charger), GFP_KERNEL);
991 if (!pm2) {
992 dev_err(&i2c_client->dev, "pm2xxx_charger allocation failed\n");
993 return -ENOMEM;
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");
1004 ret = -EINVAL;
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");
1013 ret = -EINVAL;
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)) {
1022 ret = -ENODEV;
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);
1031 /* AC supply */
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",
1055 WQ_MEM_RECLAIM);
1056 if (pm2->charger_wq == NULL) {
1057 ret = -ENOMEM;
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,
1087 &psy_cfg);
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),
1096 NULL,
1097 pm2xxx_charger_irq[0].isr,
1098 pm2->pdata->irq_type,
1099 pm2xxx_charger_irq[0].name, pm2);
1101 if (ret != 0) {
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);
1109 if (ret)
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));
1119 if (ret) {
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");
1137 if (ret < 0) {
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);
1142 if (ret < 0) {
1143 dev_err(pm2->dev, "pm2301_lpm_gpio direction failed\n");
1144 goto free_gpio;
1146 set_lpn_pin(pm2);
1149 /* read interrupt registers */
1150 for (i = 0; i < PM2XXX_NUM_INT_REG; i++)
1151 pm2xxx_reg_read(pm2,
1152 pm2xxx_interrupt_registers[i],
1153 &val);
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");
1166 return 0;
1168 free_gpio:
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);
1179 free_regulator:
1180 /* disable the regulator */
1181 regulator_put(pm2->regu);
1182 free_charger_wq:
1183 destroy_workqueue(pm2->charger_wq);
1184 free_device_info:
1185 kfree(pm2);
1187 return ret;
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);
1218 kfree(pm2);
1220 return 0;
1223 static const struct i2c_device_id pm2xxx_id[] = {
1224 { "pm2301", 0 },
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,
1233 .driver = {
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");