2 * Power supply driver for the Active-semi ACT8945A PMIC
4 * Copyright (C) 2015 Atmel Corporation
6 * Author: Wenyou Yang <wenyou.yang@atmel.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
16 #include <linux/of_irq.h>
17 #include <linux/platform_device.h>
18 #include <linux/power_supply.h>
19 #include <linux/regmap.h>
20 #include <linux/gpio/consumer.h>
22 static const char *act8945a_charger_model
= "ACT8945A";
23 static const char *act8945a_charger_manufacturer
= "Active-semi";
26 * ACT8945A Charger Register Map
30 #define ACT8945A_APCH_CFG 0x71
31 #define ACT8945A_APCH_STATUS 0x78
32 #define ACT8945A_APCH_CTRL 0x79
33 #define ACT8945A_APCH_STATE 0x7A
35 /* ACT8945A_APCH_CFG */
36 #define APCH_CFG_OVPSET (0x3 << 0)
37 #define APCH_CFG_OVPSET_6V6 (0x0 << 0)
38 #define APCH_CFG_OVPSET_7V (0x1 << 0)
39 #define APCH_CFG_OVPSET_7V5 (0x2 << 0)
40 #define APCH_CFG_OVPSET_8V (0x3 << 0)
41 #define APCH_CFG_PRETIMO (0x3 << 2)
42 #define APCH_CFG_PRETIMO_40_MIN (0x0 << 2)
43 #define APCH_CFG_PRETIMO_60_MIN (0x1 << 2)
44 #define APCH_CFG_PRETIMO_80_MIN (0x2 << 2)
45 #define APCH_CFG_PRETIMO_DISABLED (0x3 << 2)
46 #define APCH_CFG_TOTTIMO (0x3 << 4)
47 #define APCH_CFG_TOTTIMO_3_HOUR (0x0 << 4)
48 #define APCH_CFG_TOTTIMO_4_HOUR (0x1 << 4)
49 #define APCH_CFG_TOTTIMO_5_HOUR (0x2 << 4)
50 #define APCH_CFG_TOTTIMO_DISABLED (0x3 << 4)
51 #define APCH_CFG_SUSCHG (0x1 << 7)
53 #define APCH_STATUS_CHGDAT BIT(0)
54 #define APCH_STATUS_INDAT BIT(1)
55 #define APCH_STATUS_TEMPDAT BIT(2)
56 #define APCH_STATUS_TIMRDAT BIT(3)
57 #define APCH_STATUS_CHGSTAT BIT(4)
58 #define APCH_STATUS_INSTAT BIT(5)
59 #define APCH_STATUS_TEMPSTAT BIT(6)
60 #define APCH_STATUS_TIMRSTAT BIT(7)
62 #define APCH_CTRL_CHGEOCOUT BIT(0)
63 #define APCH_CTRL_INDIS BIT(1)
64 #define APCH_CTRL_TEMPOUT BIT(2)
65 #define APCH_CTRL_TIMRPRE BIT(3)
66 #define APCH_CTRL_CHGEOCIN BIT(4)
67 #define APCH_CTRL_INCON BIT(5)
68 #define APCH_CTRL_TEMPIN BIT(6)
69 #define APCH_CTRL_TIMRTOT BIT(7)
71 #define APCH_STATE_ACINSTAT (0x1 << 1)
72 #define APCH_STATE_CSTATE (0x3 << 4)
73 #define APCH_STATE_CSTATE_SHIFT 4
74 #define APCH_STATE_CSTATE_DISABLED 0x00
75 #define APCH_STATE_CSTATE_EOC 0x01
76 #define APCH_STATE_CSTATE_FAST 0x02
77 #define APCH_STATE_CSTATE_PRE 0x03
79 struct act8945a_charger
{
80 struct power_supply
*psy
;
81 struct power_supply_desc desc
;
82 struct regmap
*regmap
;
83 struct work_struct work
;
86 struct gpio_desc
*lbo_gpio
;
87 struct gpio_desc
*chglev_gpio
;
90 static int act8945a_get_charger_state(struct regmap
*regmap
, int *val
)
93 unsigned int status
, state
;
95 ret
= regmap_read(regmap
, ACT8945A_APCH_STATUS
, &status
);
99 ret
= regmap_read(regmap
, ACT8945A_APCH_STATE
, &state
);
103 state
&= APCH_STATE_CSTATE
;
104 state
>>= APCH_STATE_CSTATE_SHIFT
;
107 case APCH_STATE_CSTATE_PRE
:
108 case APCH_STATE_CSTATE_FAST
:
109 *val
= POWER_SUPPLY_STATUS_CHARGING
;
111 case APCH_STATE_CSTATE_EOC
:
112 if (status
& APCH_STATUS_CHGDAT
)
113 *val
= POWER_SUPPLY_STATUS_FULL
;
115 *val
= POWER_SUPPLY_STATUS_CHARGING
;
117 case APCH_STATE_CSTATE_DISABLED
:
119 if (!(status
& APCH_STATUS_INDAT
))
120 *val
= POWER_SUPPLY_STATUS_DISCHARGING
;
122 *val
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
129 static int act8945a_get_charge_type(struct regmap
*regmap
, int *val
)
132 unsigned int status
, state
;
134 ret
= regmap_read(regmap
, ACT8945A_APCH_STATUS
, &status
);
138 ret
= regmap_read(regmap
, ACT8945A_APCH_STATE
, &state
);
142 state
&= APCH_STATE_CSTATE
;
143 state
>>= APCH_STATE_CSTATE_SHIFT
;
146 case APCH_STATE_CSTATE_PRE
:
147 *val
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
149 case APCH_STATE_CSTATE_FAST
:
150 *val
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
152 case APCH_STATE_CSTATE_EOC
:
153 *val
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
155 case APCH_STATE_CSTATE_DISABLED
:
157 if (!(status
& APCH_STATUS_INDAT
))
158 *val
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
160 *val
= POWER_SUPPLY_CHARGE_TYPE_UNKNOWN
;
167 static int act8945a_get_battery_health(struct regmap
*regmap
, int *val
)
170 unsigned int status
, state
, config
;
172 ret
= regmap_read(regmap
, ACT8945A_APCH_STATUS
, &status
);
176 ret
= regmap_read(regmap
, ACT8945A_APCH_CFG
, &config
);
180 ret
= regmap_read(regmap
, ACT8945A_APCH_STATE
, &state
);
184 state
&= APCH_STATE_CSTATE
;
185 state
>>= APCH_STATE_CSTATE_SHIFT
;
188 case APCH_STATE_CSTATE_DISABLED
:
189 if (config
& APCH_CFG_SUSCHG
) {
190 *val
= POWER_SUPPLY_HEALTH_UNKNOWN
;
191 } else if (status
& APCH_STATUS_INDAT
) {
192 if (!(status
& APCH_STATUS_TEMPDAT
))
193 *val
= POWER_SUPPLY_HEALTH_OVERHEAT
;
194 else if (status
& APCH_STATUS_TIMRDAT
)
195 *val
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
197 *val
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
199 *val
= POWER_SUPPLY_HEALTH_GOOD
;
202 case APCH_STATE_CSTATE_PRE
:
203 case APCH_STATE_CSTATE_FAST
:
204 case APCH_STATE_CSTATE_EOC
:
206 *val
= POWER_SUPPLY_HEALTH_GOOD
;
213 static int act8945a_get_capacity_level(struct act8945a_charger
*charger
,
214 struct regmap
*regmap
, int *val
)
217 unsigned int status
, state
, config
;
218 int lbo_level
= gpiod_get_value(charger
->lbo_gpio
);
220 ret
= regmap_read(regmap
, ACT8945A_APCH_STATUS
, &status
);
224 ret
= regmap_read(regmap
, ACT8945A_APCH_CFG
, &config
);
228 ret
= regmap_read(regmap
, ACT8945A_APCH_STATE
, &state
);
232 state
&= APCH_STATE_CSTATE
;
233 state
>>= APCH_STATE_CSTATE_SHIFT
;
236 case APCH_STATE_CSTATE_PRE
:
237 *val
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
239 case APCH_STATE_CSTATE_FAST
:
241 *val
= POWER_SUPPLY_CAPACITY_LEVEL_HIGH
;
243 *val
= POWER_SUPPLY_CAPACITY_LEVEL_LOW
;
245 case APCH_STATE_CSTATE_EOC
:
246 if (status
& APCH_STATUS_CHGDAT
)
247 *val
= POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
249 *val
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
251 case APCH_STATE_CSTATE_DISABLED
:
253 if (config
& APCH_CFG_SUSCHG
) {
254 *val
= POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
256 *val
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
257 if (!(status
& APCH_STATUS_INDAT
)) {
259 *val
= POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
268 #define MAX_CURRENT_USB_HIGH 450000
269 #define MAX_CURRENT_USB_LOW 90000
270 #define MAX_CURRENT_USB_PRE 45000
272 * Riset(K) = 2336 * (1V/Ichg(mA)) - 0.205
275 #define MAX_CURRENT_AC_HIGH 886527
276 #define MAX_CURRENT_AC_LOW 117305
277 #define MAX_CURRENT_AC_HIGH_PRE 88653
278 #define MAX_CURRENT_AC_LOW_PRE 11731
280 static int act8945a_get_current_max(struct act8945a_charger
*charger
,
281 struct regmap
*regmap
, int *val
)
284 unsigned int status
, state
;
285 unsigned int acin_state
;
286 int chgin_level
= gpiod_get_value(charger
->chglev_gpio
);
288 ret
= regmap_read(regmap
, ACT8945A_APCH_STATUS
, &status
);
292 ret
= regmap_read(regmap
, ACT8945A_APCH_STATE
, &state
);
296 acin_state
= (state
& APCH_STATE_ACINSTAT
) >> 1;
298 state
&= APCH_STATE_CSTATE
;
299 state
>>= APCH_STATE_CSTATE_SHIFT
;
302 case APCH_STATE_CSTATE_PRE
:
305 *val
= MAX_CURRENT_AC_HIGH_PRE
;
307 *val
= MAX_CURRENT_AC_LOW_PRE
;
309 *val
= MAX_CURRENT_USB_PRE
;
312 case APCH_STATE_CSTATE_FAST
:
315 *val
= MAX_CURRENT_AC_HIGH
;
317 *val
= MAX_CURRENT_AC_LOW
;
320 *val
= MAX_CURRENT_USB_HIGH
;
322 *val
= MAX_CURRENT_USB_LOW
;
325 case APCH_STATE_CSTATE_EOC
:
326 case APCH_STATE_CSTATE_DISABLED
:
335 static enum power_supply_property act8945a_charger_props
[] = {
336 POWER_SUPPLY_PROP_STATUS
,
337 POWER_SUPPLY_PROP_CHARGE_TYPE
,
338 POWER_SUPPLY_PROP_TECHNOLOGY
,
339 POWER_SUPPLY_PROP_HEALTH
,
340 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
341 POWER_SUPPLY_PROP_CURRENT_MAX
,
342 POWER_SUPPLY_PROP_MODEL_NAME
,
343 POWER_SUPPLY_PROP_MANUFACTURER
346 static int act8945a_charger_get_property(struct power_supply
*psy
,
347 enum power_supply_property prop
,
348 union power_supply_propval
*val
)
350 struct act8945a_charger
*charger
= power_supply_get_drvdata(psy
);
351 struct regmap
*regmap
= charger
->regmap
;
355 case POWER_SUPPLY_PROP_STATUS
:
356 ret
= act8945a_get_charger_state(regmap
, &val
->intval
);
358 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
359 ret
= act8945a_get_charge_type(regmap
, &val
->intval
);
361 case POWER_SUPPLY_PROP_TECHNOLOGY
:
362 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
364 case POWER_SUPPLY_PROP_HEALTH
:
365 ret
= act8945a_get_battery_health(regmap
, &val
->intval
);
367 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
368 ret
= act8945a_get_capacity_level(charger
,
369 regmap
, &val
->intval
);
371 case POWER_SUPPLY_PROP_CURRENT_MAX
:
372 ret
= act8945a_get_current_max(charger
,
373 regmap
, &val
->intval
);
375 case POWER_SUPPLY_PROP_MODEL_NAME
:
376 val
->strval
= act8945a_charger_model
;
378 case POWER_SUPPLY_PROP_MANUFACTURER
:
379 val
->strval
= act8945a_charger_manufacturer
;
388 static int act8945a_enable_interrupt(struct act8945a_charger
*charger
)
390 struct regmap
*regmap
= charger
->regmap
;
394 ctrl
= APCH_CTRL_CHGEOCOUT
| APCH_CTRL_CHGEOCIN
|
395 APCH_CTRL_INDIS
| APCH_CTRL_INCON
|
396 APCH_CTRL_TEMPOUT
| APCH_CTRL_TEMPIN
|
397 APCH_CTRL_TIMRPRE
| APCH_CTRL_TIMRTOT
;
398 ret
= regmap_write(regmap
, ACT8945A_APCH_CTRL
, ctrl
);
402 ctrl
= APCH_STATUS_CHGSTAT
| APCH_STATUS_INSTAT
|
403 APCH_STATUS_TEMPSTAT
| APCH_STATUS_TIMRSTAT
;
404 ret
= regmap_write(regmap
, ACT8945A_APCH_STATUS
, ctrl
);
411 static unsigned int act8945a_set_supply_type(struct act8945a_charger
*charger
,
414 unsigned int status
, state
;
417 ret
= regmap_read(charger
->regmap
, ACT8945A_APCH_STATUS
, &status
);
421 ret
= regmap_read(charger
->regmap
, ACT8945A_APCH_STATE
, &state
);
425 if (status
& APCH_STATUS_INDAT
) {
426 if (state
& APCH_STATE_ACINSTAT
)
427 *type
= POWER_SUPPLY_TYPE_MAINS
;
429 *type
= POWER_SUPPLY_TYPE_USB
;
431 *type
= POWER_SUPPLY_TYPE_BATTERY
;
437 static void act8945a_work(struct work_struct
*work
)
439 struct act8945a_charger
*charger
=
440 container_of(work
, struct act8945a_charger
, work
);
442 act8945a_set_supply_type(charger
, &charger
->desc
.type
);
444 power_supply_changed(charger
->psy
);
447 static irqreturn_t
act8945a_status_changed(int irq
, void *dev_id
)
449 struct act8945a_charger
*charger
= dev_id
;
451 if (charger
->init_done
)
452 schedule_work(&charger
->work
);
457 #define DEFAULT_TOTAL_TIME_OUT 3
458 #define DEFAULT_PRE_TIME_OUT 40
459 #define DEFAULT_INPUT_OVP_THRESHOLD 6600
461 static int act8945a_charger_config(struct device
*dev
,
462 struct act8945a_charger
*charger
)
464 struct device_node
*np
= dev
->of_node
;
465 struct regmap
*regmap
= charger
->regmap
;
469 u32 input_voltage_threshold
;
473 unsigned int value
= 0;
476 dev_err(dev
, "no charger of node\n");
480 ret
= regmap_read(regmap
, ACT8945A_APCH_CFG
, &tmp
);
484 if (tmp
& APCH_CFG_SUSCHG
) {
485 value
|= APCH_CFG_SUSCHG
;
486 dev_info(dev
, "have been suspended\n");
489 charger
->lbo_gpio
= devm_gpiod_get_optional(dev
, "active-semi,lbo",
491 if (IS_ERR(charger
->lbo_gpio
)) {
492 err
= PTR_ERR(charger
->lbo_gpio
);
493 dev_err(dev
, "unable to claim gpio \"lbo\": %d\n", err
);
497 ret
= devm_request_irq(dev
, gpiod_to_irq(charger
->lbo_gpio
),
498 act8945a_status_changed
,
499 (IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
),
500 "act8945a_lbo_detect", charger
);
502 dev_info(dev
, "failed to request gpio \"lbo\" IRQ\n");
504 charger
->chglev_gpio
= devm_gpiod_get_optional(dev
,
505 "active-semi,chglev",
507 if (IS_ERR(charger
->chglev_gpio
)) {
508 err
= PTR_ERR(charger
->chglev_gpio
);
509 dev_err(dev
, "unable to claim gpio \"chglev\": %d\n", err
);
513 if (of_property_read_u32(np
,
514 "active-semi,input-voltage-threshold-microvolt",
515 &input_voltage_threshold
))
516 input_voltage_threshold
= DEFAULT_INPUT_OVP_THRESHOLD
;
518 if (of_property_read_u32(np
,
519 "active-semi,precondition-timeout",
521 pre_time_out
= DEFAULT_PRE_TIME_OUT
;
523 if (of_property_read_u32(np
, "active-semi,total-timeout",
525 total_time_out
= DEFAULT_TOTAL_TIME_OUT
;
527 switch (input_voltage_threshold
) {
529 value
|= APCH_CFG_OVPSET_8V
;
532 value
|= APCH_CFG_OVPSET_7V5
;
535 value
|= APCH_CFG_OVPSET_7V
;
539 value
|= APCH_CFG_OVPSET_6V6
;
543 switch (pre_time_out
) {
545 value
|= APCH_CFG_PRETIMO_60_MIN
;
548 value
|= APCH_CFG_PRETIMO_80_MIN
;
551 value
|= APCH_CFG_PRETIMO_DISABLED
;
555 value
|= APCH_CFG_PRETIMO_40_MIN
;
559 switch (total_time_out
) {
561 value
|= APCH_CFG_TOTTIMO_4_HOUR
;
564 value
|= APCH_CFG_TOTTIMO_5_HOUR
;
567 value
|= APCH_CFG_TOTTIMO_DISABLED
;
571 value
|= APCH_CFG_TOTTIMO_3_HOUR
;
575 return regmap_write(regmap
, ACT8945A_APCH_CFG
, value
);
578 static int act8945a_charger_probe(struct platform_device
*pdev
)
580 struct act8945a_charger
*charger
;
581 struct power_supply_config psy_cfg
= {};
584 charger
= devm_kzalloc(&pdev
->dev
, sizeof(*charger
), GFP_KERNEL
);
588 charger
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
589 if (!charger
->regmap
) {
590 dev_err(&pdev
->dev
, "Parent did not provide regmap\n");
594 ret
= act8945a_charger_config(&pdev
->dev
, charger
);
598 irq
= of_irq_get(pdev
->dev
.of_node
, 0);
600 dev_err(&pdev
->dev
, "failed to find IRQ number\n");
601 return irq
?: -ENXIO
;
604 ret
= devm_request_irq(&pdev
->dev
, irq
, act8945a_status_changed
,
605 IRQF_TRIGGER_FALLING
, "act8945a_interrupt",
608 dev_err(&pdev
->dev
, "failed to request nIRQ pin IRQ\n");
612 charger
->desc
.name
= "act8945a-charger";
613 charger
->desc
.get_property
= act8945a_charger_get_property
;
614 charger
->desc
.properties
= act8945a_charger_props
;
615 charger
->desc
.num_properties
= ARRAY_SIZE(act8945a_charger_props
);
617 ret
= act8945a_set_supply_type(charger
, &charger
->desc
.type
);
621 psy_cfg
.of_node
= pdev
->dev
.of_node
;
622 psy_cfg
.drv_data
= charger
;
624 charger
->psy
= devm_power_supply_register(&pdev
->dev
,
627 if (IS_ERR(charger
->psy
)) {
628 dev_err(&pdev
->dev
, "failed to register power supply\n");
629 return PTR_ERR(charger
->psy
);
632 platform_set_drvdata(pdev
, charger
);
634 INIT_WORK(&charger
->work
, act8945a_work
);
636 ret
= act8945a_enable_interrupt(charger
);
640 charger
->init_done
= true;
645 static int act8945a_charger_remove(struct platform_device
*pdev
)
647 struct act8945a_charger
*charger
= platform_get_drvdata(pdev
);
649 charger
->init_done
= false;
650 cancel_work_sync(&charger
->work
);
655 static struct platform_driver act8945a_charger_driver
= {
657 .name
= "act8945a-charger",
659 .probe
= act8945a_charger_probe
,
660 .remove
= act8945a_charger_remove
,
662 module_platform_driver(act8945a_charger_driver
);
664 MODULE_DESCRIPTION("Active-semi ACT8945A ActivePath charger driver");
665 MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
666 MODULE_LICENSE("GPL");