WIP FPC-III support
[linux/fpc-iii.git] / drivers / power / supply / pm2301_charger.c
blobac06ecf7fc9ca04004d4abd123f629f2ced4655b
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 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)
123 int ret;
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,
129 1, val);
130 if (ret < 0)
131 dev_err(pm2->dev, "Error reading register at 0x%x\n", reg);
132 else
133 ret = 0;
135 pm_runtime_put_sync(pm2->dev);
137 return ret;
140 static int pm2xxx_reg_write(struct pm2xxx_charger *pm2, int reg, u8 val)
142 int ret;
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,
148 1, &val);
149 if (ret < 0)
150 dev_err(pm2->dev, "Error writing register at 0x%x\n", reg);
151 else
152 ret = 0;
154 pm_runtime_put_sync(pm2->dev);
156 return ret;
159 static int pm2xxx_charging_enable_mngt(struct pm2xxx_charger *pm2)
161 int ret;
163 /* Enable charging */
164 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2,
165 (PM2XXX_CH_AUTO_RESUME_EN | PM2XXX_CHARGER_ENA));
167 return ret;
170 static int pm2xxx_charging_disable_mngt(struct pm2xxx_charger *pm2)
172 int ret;
174 /* Disable SW EOC ctrl */
175 ret = pm2xxx_reg_write(pm2, PM2XXX_SW_CTRL_REG, PM2XXX_SWCTRL_HW);
176 if (ret < 0) {
177 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
178 return ret;
181 /* Disable charging */
182 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2,
183 (PM2XXX_CH_AUTO_RESUME_DIS | PM2XXX_CHARGER_DIS));
184 if (ret < 0) {
185 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
186 return ret;
189 return 0;
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);
196 return 0;
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);
204 return 0;
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);
216 return 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);
226 return 0;
229 static int pm2xxx_charger_vbat_lsig_mngt(struct pm2xxx_charger *pm2, int val)
231 int ret;
233 switch (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,
238 PM2XXX_SWCTRL_SW);
239 if (ret < 0) {
240 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
241 return ret;
243 break;
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,
249 PM2XXX_SWCTRL_HW);
250 if (ret < 0) {
251 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
252 return ret;
254 break;
256 default:
257 dev_err(pm2->dev, "Unknown VBAT level\n");
260 return 0;
263 static int pm2xxx_charger_bat_disc_mngt(struct pm2xxx_charger *pm2, int val)
265 dev_dbg(pm2->dev, "battery disconnected\n");
267 return 0;
270 static int pm2xxx_charger_detection(struct pm2xxx_charger *pm2, u8 *val)
272 int ret;
274 ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT2, val);
276 if (ret < 0) {
277 dev_err(pm2->dev, "Charger detection failed\n");
278 goto out;
281 *val &= (PM2XXX_INT2_S_ITVPWR1PLUG | PM2XXX_INT2_S_ITVPWR2PLUG);
283 out:
284 return ret;
287 static int pm2xxx_charger_itv_pwr_plug_mngt(struct pm2xxx_charger *pm2, int val)
290 int ret;
291 u8 read_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;
302 pm2->ac_conn = true;
303 queue_work(pm2->charger_wq, &pm2->ac_work);
307 return ret;
310 static int pm2xxx_charger_itv_pwr_unplug_mngt(struct pm2xxx_charger *pm2,
311 int val)
313 pm2->ac.charger_connected = 0;
314 queue_work(pm2->charger_wq, &pm2->ac_work);
316 return 0;
319 static int pm2_int_reg0(void *pm2_data, int val)
321 struct pm2xxx_charger *pm2 = pm2_data;
322 int ret = 0;
324 if (val & PM2XXX_INT1_ITVBATLOWR) {
325 ret = pm2xxx_charger_vbat_lsig_mngt(pm2,
326 PM2XXX_INT1_ITVBATLOWR);
327 if (ret < 0)
328 goto out;
331 if (val & PM2XXX_INT1_ITVBATLOWF) {
332 ret = pm2xxx_charger_vbat_lsig_mngt(pm2,
333 PM2XXX_INT1_ITVBATLOWF);
334 if (ret < 0)
335 goto out;
338 if (val & PM2XXX_INT1_ITVBATDISCONNECT) {
339 ret = pm2xxx_charger_bat_disc_mngt(pm2,
340 PM2XXX_INT1_ITVBATDISCONNECT);
341 if (ret < 0)
342 goto out;
344 out:
345 return ret;
348 static int pm2_int_reg1(void *pm2_data, int val)
350 struct pm2xxx_charger *pm2 = pm2_data;
351 int ret = 0;
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));
359 if (val &
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));
367 return ret;
370 static int pm2_int_reg2(void *pm2_data, int val)
372 struct pm2xxx_charger *pm2 = pm2_data;
373 int ret = 0;
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)) {
380 dev_dbg(pm2->dev,
381 "Watchdog occurred for precharge, CC and CV charge\n");
384 return ret;
387 static int pm2_int_reg3(void *pm2_data, int val)
389 struct pm2xxx_charger *pm2 = pm2_data;
390 int ret = 0;
392 if (val & (PM2XXX_INT4_ITCHARGINGON)) {
393 dev_dbg(pm2->dev ,
394 "charging operation has started\n");
397 if (val & (PM2XXX_INT4_ITVRESUME)) {
398 dev_dbg(pm2->dev,
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");
425 return ret;
428 static int pm2_int_reg4(void *pm2_data, int val)
430 struct pm2xxx_charger *pm2 = pm2_data;
431 int ret = 0;
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));
452 return ret;
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");
470 return 0;
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;
477 int i;
479 /* wake up the device */
480 pm_runtime_get_sync(pm2->dev);
482 do {
483 for (i = 0; i < PM2XXX_NUM_INT_REG; i++) {
484 pm2xxx_reg_read(pm2,
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);
496 return IRQ_HANDLED;
499 static int pm2xxx_charger_get_ac_cv(struct pm2xxx_charger *pm2)
501 int ret = 0;
502 u8 val;
504 if (pm2->ac.charger_connected && pm2->ac.charger_online) {
506 ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT4, &val);
507 if (ret < 0) {
508 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
509 goto out;
512 if (val & PM2XXX_INT4_S_ITCVPHASE)
513 ret = PM2XXX_CONST_VOLT;
514 else
515 ret = PM2XXX_CONST_CURR;
517 out:
518 return ret;
521 static int pm2xxx_current_to_regval(int curr)
523 int i;
525 if (curr < pm2xxx_charger_current_map[0])
526 return 0;
528 for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_current_map); i++) {
529 if (curr < pm2xxx_charger_current_map[i])
530 return (i - 1);
533 i = ARRAY_SIZE(pm2xxx_charger_current_map) - 1;
534 if (curr == pm2xxx_charger_current_map[i])
535 return i;
536 else
537 return -EINVAL;
540 static int pm2xxx_voltage_to_regval(int curr)
542 int i;
544 if (curr < pm2xxx_charger_voltage_map[0])
545 return 0;
547 for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_voltage_map); i++) {
548 if (curr < pm2xxx_charger_voltage_map[i])
549 return i - 1;
552 i = ARRAY_SIZE(pm2xxx_charger_voltage_map) - 1;
553 if (curr == pm2xxx_charger_voltage_map[i])
554 return i;
555 else
556 return -EINVAL;
559 static int pm2xxx_charger_update_charger_current(struct ux500_charger *charger,
560 int ich_out)
562 int ret;
563 int curr_index;
564 struct pm2xxx_charger *pm2;
565 u8 val;
567 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
568 pm2 = to_pm2xxx_charger_ac_device_info(charger);
569 else
570 return -ENXIO;
572 curr_index = pm2xxx_current_to_regval(ich_out);
573 if (curr_index < 0) {
574 dev_err(pm2->dev,
575 "Charger current too high, charging not started\n");
576 return -ENXIO;
579 ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val);
580 if (ret >= 0) {
581 val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK;
582 val |= curr_index;
583 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val);
584 if (ret < 0) {
585 dev_err(pm2->dev,
586 "%s write failed\n", __func__);
589 else
590 dev_err(pm2->dev, "%s read failed\n", __func__);
592 return ret;
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));
603 switch (psp) {
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;
613 else
614 val->intval = POWER_SUPPLY_HEALTH_GOOD;
615 break;
616 case POWER_SUPPLY_PROP_ONLINE:
617 val->intval = pm2->ac.charger_online;
618 break;
619 case POWER_SUPPLY_PROP_PRESENT:
620 val->intval = pm2->ac.charger_connected;
621 break;
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;
625 break;
626 default:
627 return -EINVAL;
629 return 0;
632 static int pm2xxx_charging_init(struct pm2xxx_charger *pm2)
634 int ret = 0;
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));
639 if( ret < 0)
640 return ret;
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,
669 PM2XXX_CH_VOLT_4_2);
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);
695 return ret;
698 static int pm2xxx_charger_ac_en(struct ux500_charger *charger,
699 int enable, int vset, int iset)
701 int ret;
702 int volt_index;
703 int curr_index;
704 u8 val;
706 struct pm2xxx_charger *pm2 = to_pm2xxx_charger_ac_device_info(charger);
708 if (enable) {
709 if (!pm2->ac.charger_connected) {
710 dev_dbg(pm2->dev, "AC charger not connected\n");
711 return -ENXIO;
714 dev_dbg(pm2->dev, "Enable AC: %dmV %dmA\n", vset, iset);
715 if (!pm2->vddadc_en_ac) {
716 ret = regulator_enable(pm2->regu);
717 if (ret)
718 dev_warn(pm2->dev,
719 "Failed to enable vddadc regulator\n");
720 else
721 pm2->vddadc_en_ac = true;
724 ret = pm2xxx_charging_init(pm2);
725 if (ret < 0) {
726 dev_err(pm2->dev, "%s charging init failed\n",
727 __func__);
728 goto error_occured;
731 volt_index = pm2xxx_voltage_to_regval(vset);
732 curr_index = pm2xxx_current_to_regval(iset);
734 if (volt_index < 0 || curr_index < 0) {
735 dev_err(pm2->dev,
736 "Charger voltage or current too high, "
737 "charging not started\n");
738 return -ENXIO;
741 ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG8, &val);
742 if (ret < 0) {
743 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
744 goto error_occured;
746 val &= ~PM2XXX_CH_VOLT_MASK;
747 val |= volt_index;
748 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG8, val);
749 if (ret < 0) {
750 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
751 goto error_occured;
754 ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val);
755 if (ret < 0) {
756 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
757 goto error_occured;
759 val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK;
760 val |= curr_index;
761 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val);
762 if (ret < 0) {
763 dev_err(pm2->dev, "%s pm2xxx write failed\n", __func__);
764 goto error_occured;
767 if (!pm2->bat->enable_overshoot) {
768 ret = pm2xxx_reg_read(pm2, PM2XXX_LED_CTRL_REG, &val);
769 if (ret < 0) {
770 dev_err(pm2->dev, "%s pm2xxx read failed\n",
771 __func__);
772 goto error_occured;
774 val |= PM2XXX_ANTI_OVERSHOOT_EN;
775 ret = pm2xxx_reg_write(pm2, PM2XXX_LED_CTRL_REG, val);
776 if (ret < 0) {
777 dev_err(pm2->dev, "%s pm2xxx write failed\n",
778 __func__);
779 goto error_occured;
783 ret = pm2xxx_charging_enable_mngt(pm2);
784 if (ret < 0) {
785 dev_err(pm2->dev, "Failed to enable"
786 "pm2xxx ac charger\n");
787 goto error_occured;
790 pm2->ac.charger_online = 1;
791 } else {
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);
802 if (ret < 0) {
803 dev_err(pm2->dev, "failed to disable"
804 "pm2xxx ac charger\n");
805 goto error_occured;
808 dev_dbg(pm2->dev, "PM2301: " "Disabled AC charging\n");
810 power_supply_changed(pm2->ac_chg.psy);
812 error_occured:
813 return ret;
816 static int pm2xxx_charger_watchdog_kick(struct ux500_charger *charger)
818 int ret;
819 struct pm2xxx_charger *pm2;
821 if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
822 pm2 = to_pm2xxx_charger_ac_device_info(charger);
823 else
824 return -ENXIO;
826 ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_WD_KICK, WD_TIMER);
827 if (ret)
828 dev_err(pm2->dev, "Failed to kick WD!\n");
830 return ret;
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)
845 u8 reg_value;
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, &reg_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)
870 int ret;
871 u8 val;
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);
878 if (ret < 0) {
879 dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__);
880 return;
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);
911 set_lpn_pin(pm2);
913 /* If we still have a HW failure, schedule a new check */
914 if (pm2->flags.ovv)
915 queue_delayed_work(pm2->charger_wq,
916 &pm2->check_hw_failure_work, 0);
918 return 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);
927 clear_lpn_pin(pm2);
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);
936 return 0;
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);
945 clear_lpn_pin(pm2);
947 return 0;
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)
958 set_lpn_pin(pm2);
960 return 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;
975 int ret = 0;
976 u8 val;
977 int i;
979 if (!pl_data) {
980 dev_err(&i2c_client->dev, "No platform data supplied\n");
981 return -EINVAL;
984 pm2 = kzalloc(sizeof(struct pm2xxx_charger), GFP_KERNEL);
985 if (!pm2) {
986 dev_err(&i2c_client->dev, "pm2xxx_charger allocation failed\n");
987 return -ENOMEM;
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");
998 ret = -EINVAL;
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");
1007 ret = -EINVAL;
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)) {
1016 ret = -ENODEV;
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);
1025 /* AC supply */
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",
1049 WQ_MEM_RECLAIM);
1050 if (pm2->charger_wq == NULL) {
1051 ret = -ENOMEM;
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,
1081 &psy_cfg);
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),
1090 NULL,
1091 pm2xxx_charger_irq[0].isr,
1092 pm2->pdata->irq_type,
1093 pm2xxx_charger_irq[0].name, pm2);
1095 if (ret != 0) {
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);
1103 if (ret)
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));
1113 if (ret) {
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");
1131 if (ret < 0) {
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);
1136 if (ret < 0) {
1137 dev_err(pm2->dev, "pm2301_lpm_gpio direction failed\n");
1138 goto free_gpio;
1140 set_lpn_pin(pm2);
1143 /* read interrupt registers */
1144 for (i = 0; i < PM2XXX_NUM_INT_REG; i++)
1145 pm2xxx_reg_read(pm2,
1146 pm2xxx_interrupt_registers[i],
1147 &val);
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");
1160 return 0;
1162 free_gpio:
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);
1173 free_regulator:
1174 /* disable the regulator */
1175 regulator_put(pm2->regu);
1176 free_charger_wq:
1177 destroy_workqueue(pm2->charger_wq);
1178 free_device_info:
1179 kfree(pm2);
1181 return ret;
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);
1212 kfree(pm2);
1214 return 0;
1217 static const struct i2c_device_id pm2xxx_id[] = {
1218 { "pm2301", 0 },
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,
1227 .driver = {
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");