19 #include <linux/module.h>
22 #include <linux/slab.h>
26 #define LP8788_CHG_STATUS 0x07
27 #define LP8788_CHG_IDCIN 0x13
28 #define LP8788_CHG_IBATT 0x14
29 #define LP8788_CHG_VTERM 0x15
30 #define LP8788_CHG_EOC 0x16
33 #define LP8788_CHG_INPUT_STATE_M 0x03
34 #define LP8788_CHG_STATE_M 0x3C
35 #define LP8788_CHG_STATE_S 2
36 #define LP8788_NO_BATT_M BIT(6)
37 #define LP8788_BAD_BATT_M BIT(7)
38 #define LP8788_CHG_IBATT_M 0x1F
39 #define LP8788_CHG_VTERM_M 0x0F
40 #define LP8788_CHG_EOC_LEVEL_M 0x30
41 #define LP8788_CHG_EOC_LEVEL_S 4
42 #define LP8788_CHG_EOC_TIME_M 0x0E
43 #define LP8788_CHG_EOC_TIME_S 1
44 #define LP8788_CHG_EOC_MODE_M BIT(0)
46 #define LP8788_CHARGER_NAME "charger"
47 #define LP8788_BATTERY_NAME "main_batt"
49 #define LP8788_CHG_START 0x11
50 #define LP8788_CHG_END 0x1C
52 #define LP8788_BUF_SIZE 40
53 #define LP8788_ISEL_MAX 23
54 #define LP8788_ISEL_STEP 50
55 #define LP8788_VTERM_MIN 4100
56 #define LP8788_VTERM_STEP 25
57 #define LP8788_MAX_BATT_CAPACITY 100
58 #define LP8788_MAX_CHG_IRQS 11
118 static char *battery_supplied_to[] = {
138 static bool lp8788_is_charger_detected(
struct lp8788_charger *pchg)
148 static int lp8788_charger_get_property(
struct power_supply *psy,
157 val->
intval = lp8788_is_charger_detected(pchg);
224 static int lp8788_get_battery_present(
struct lp8788_charger *pchg,
254 *result = (scaleint + scalepart * 1000000) / 1000;
259 static int lp8788_get_battery_voltage(
struct lp8788_charger *pchg,
262 return lp8788_get_vbatt_adc(pchg, &val->
intval);
265 static int lp8788_get_battery_capacity(
struct lp8788_charger *pchg,
270 unsigned int max_vbatt;
292 ret = lp8788_get_vbatt_adc(pchg, &vbatt);
303 static int lp8788_get_battery_temperature(
struct lp8788_charger *pchg,
319 val->
intval = (scaleint + scalepart * 1000000) / 100;
324 static int lp8788_get_battery_charging_current(
struct lp8788_charger *pchg,
337 static int lp8788_get_charging_termination_voltage(
struct lp8788_charger *pchg,
349 static int lp8788_battery_get_property(
struct power_supply *psy,
357 return lp8788_get_battery_status(pchg, val);
359 return lp8788_get_battery_health(pchg, val);
361 return lp8788_get_battery_present(pchg, val);
363 return lp8788_get_battery_voltage(pchg, val);
365 return lp8788_get_battery_capacity(pchg, val);
367 return lp8788_get_battery_temperature(pchg, val);
369 return lp8788_get_battery_charging_current(pchg, val);
371 return lp8788_get_charging_termination_voltage(pchg, val);
377 static inline bool lp8788_is_valid_charger_register(
u8 addr)
382 static int lp8788_update_charger_params(
struct lp8788_charger *pchg)
391 dev_info(lp->
dev,
"skip updating charger parameters\n");
402 if (lp8788_is_valid_charger_register(param->
addr)) {
417 pchg->
charger.properties = lp8788_charger_prop;
419 pchg->
charger.get_property = lp8788_charger_get_property;
420 pchg->
charger.supplied_to = battery_supplied_to;
428 pchg->
battery.properties = lp8788_battery_prop;
430 pchg->
battery.get_property = lp8788_battery_get_property;
454 static bool lp8788_find_irq_id(
struct lp8788_charger *pchg,
int virq,
int *
id)
459 for (i = 0; i < pchg->
num_irqs; i++) {
460 if (pchg->
irqs[i].virq == virq) {
461 *
id = pchg->
irqs[
i].which;
476 if (!lp8788_find_irq_id(pchg, virq, &
id))
520 irq_start = r->
start;
523 for (i = irq_start; i <= irq_end; i++) {
527 pchg->
irqs[nr_irq].virq = virq;
528 pchg->
irqs[nr_irq].which =
i;
532 lp8788_charger_irq_thread,
544 for (i = 0; i < pchg->
num_irqs; i++)
553 const char *name[] = {
563 ret = lp8788_set_irqs(pdev, pchg, name[i]);
565 dev_warn(lp->
dev,
"irq setup failed: %s\n", name[i]);
571 dev_err(lp->
dev,
"invalid total number of irqs: %d\n",
586 for (i = 0; i < pchg->
num_irqs; i++) {
587 irq = pchg->
irqs[
i].virq;
623 dev_err(dev,
"invalid ADC id for VBATT: %d\n",
id);
638 dev_err(dev,
"invalid ADC id for BATT_TEMP : %d\n",
id);
644 static void lp8788_release_adc_channel(
struct lp8788_charger *pchg)
657 static ssize_t lp8788_show_charger_status(
struct device *dev,
686 char *
stime[] = {
"400ms",
"5min",
"10min",
"15min",
687 "20min",
"25min",
"30min" "No timeout" };
701 char *abs_level[] = {
"25mA",
"49mA",
"75mA",
"98mA" };
702 char *relative_level[] = {
"5%",
"10%",
"15%",
"20%" };
711 level = mode ? abs_level[
val] : relative_level[
val];
721 static struct attribute *lp8788_charger_attr[] = {
722 &dev_attr_charger_status.attr,
723 &dev_attr_eoc_time.attr,
724 &dev_attr_eoc_level.attr,
729 .attrs = lp8788_charger_attr,
744 platform_set_drvdata(pdev, pchg);
746 ret = lp8788_update_charger_params(pchg);
750 lp8788_setup_adc_channel(pchg);
752 ret = lp8788_psy_register(pdev, pchg);
758 lp8788_psy_unregister(pchg);
762 ret = lp8788_irq_register(pdev, pchg);
764 dev_warn(lp->
dev,
"failed to register charger irq: %d\n", ret);
774 lp8788_irq_unregister(pdev, pchg);
776 lp8788_psy_unregister(pchg);
777 lp8788_release_adc_channel(pchg);
783 .probe = lp8788_charger_probe,