1 /* Copyright (c) 2014, Sony Mobile Communications Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * This driver is for the multi-block Switch-Mode Battery Charger and Boost
13 * (SMBB) hardware, found in Qualcomm PM8941 PMICs. The charger is an
14 * integrated, single-cell lithium-ion battery charger.
22 * - Boost (not implemented)
27 #include <linux/errno.h>
28 #include <linux/interrupt.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
33 #include <linux/platform_device.h>
34 #include <linux/power_supply.h>
35 #include <linux/regmap.h>
36 #include <linux/slab.h>
37 #include <linux/extcon.h>
39 #define SMBB_CHG_VMAX 0x040
40 #define SMBB_CHG_VSAFE 0x041
41 #define SMBB_CHG_CFG 0x043
42 #define SMBB_CHG_IMAX 0x044
43 #define SMBB_CHG_ISAFE 0x045
44 #define SMBB_CHG_VIN_MIN 0x047
45 #define SMBB_CHG_CTRL 0x049
46 #define CTRL_EN BIT(7)
47 #define SMBB_CHG_VBAT_WEAK 0x052
48 #define SMBB_CHG_IBAT_TERM_CHG 0x05b
49 #define IBAT_TERM_CHG_IEOC BIT(7)
50 #define IBAT_TERM_CHG_IEOC_BMS BIT(7)
51 #define IBAT_TERM_CHG_IEOC_CHG 0
52 #define SMBB_CHG_VBAT_DET 0x05d
53 #define SMBB_CHG_TCHG_MAX_EN 0x060
54 #define TCHG_MAX_EN BIT(7)
55 #define SMBB_CHG_WDOG_TIME 0x062
56 #define SMBB_CHG_WDOG_EN 0x065
57 #define WDOG_EN BIT(7)
59 #define SMBB_BUCK_REG_MODE 0x174
60 #define BUCK_REG_MODE BIT(0)
61 #define BUCK_REG_MODE_VBAT BIT(0)
62 #define BUCK_REG_MODE_VSYS 0
64 #define SMBB_BAT_PRES_STATUS 0x208
65 #define PRES_STATUS_BAT_PRES BIT(7)
66 #define SMBB_BAT_TEMP_STATUS 0x209
67 #define TEMP_STATUS_OK BIT(7)
68 #define TEMP_STATUS_HOT BIT(6)
69 #define SMBB_BAT_BTC_CTRL 0x249
70 #define BTC_CTRL_COMP_EN BIT(7)
71 #define BTC_CTRL_COLD_EXT BIT(1)
72 #define BTC_CTRL_HOT_EXT_N BIT(0)
74 #define SMBB_USB_IMAX 0x344
75 #define SMBB_USB_ENUM_TIMER_STOP 0x34e
76 #define ENUM_TIMER_STOP BIT(0)
77 #define SMBB_USB_SEC_ACCESS 0x3d0
78 #define SEC_ACCESS_MAGIC 0xa5
79 #define SMBB_USB_REV_BST 0x3ed
80 #define REV_BST_CHG_GONE BIT(7)
82 #define SMBB_DC_IMAX 0x444
84 #define SMBB_MISC_REV2 0x601
85 #define SMBB_MISC_BOOT_DONE 0x642
86 #define BOOT_DONE BIT(7)
88 #define STATUS_USBIN_VALID BIT(0) /* USB connection is valid */
89 #define STATUS_DCIN_VALID BIT(1) /* DC connection is valid */
90 #define STATUS_BAT_HOT BIT(2) /* Battery temp 1=Hot, 0=Cold */
91 #define STATUS_BAT_OK BIT(3) /* Battery temp OK */
92 #define STATUS_BAT_PRESENT BIT(4) /* Battery is present */
93 #define STATUS_CHG_DONE BIT(5) /* Charge cycle is complete */
94 #define STATUS_CHG_TRKL BIT(6) /* Trickle charging */
95 #define STATUS_CHG_FAST BIT(7) /* Fast charging */
96 #define STATUS_CHG_GONE BIT(8) /* No charger is connected */
111 struct smbb_charger
{
112 unsigned int revision
;
115 struct extcon_dev
*edev
;
119 unsigned long status
;
120 struct mutex statlock
;
122 unsigned int attr
[_ATTR_CNT
];
124 struct power_supply
*usb_psy
;
125 struct power_supply
*dc_psy
;
126 struct power_supply
*bat_psy
;
127 struct regmap
*regmap
;
130 static const unsigned int smbb_usb_extcon_cable
[] = {
135 static int smbb_vbat_weak_fn(unsigned int index
)
137 return 2100000 + index
* 100000;
140 static int smbb_vin_fn(unsigned int index
)
143 return 5600000 + (index
- 43) * 200000;
144 return 3400000 + index
* 50000;
147 static int smbb_vmax_fn(unsigned int index
)
149 return 3240000 + index
* 10000;
152 static int smbb_vbat_det_fn(unsigned int index
)
154 return 3240000 + index
* 20000;
157 static int smbb_imax_fn(unsigned int index
)
160 return 100000 + index
* 50000;
161 return index
* 100000;
164 static int smbb_bat_imax_fn(unsigned int index
)
166 return index
* 50000;
169 static unsigned int smbb_hw_lookup(unsigned int val
, int (*fn
)(unsigned int))
174 for (widx
= sel
= 0; (*fn
)(widx
) <= val
; ++widx
)
180 static const struct smbb_charger_attr
{
183 unsigned int safe_reg
;
186 unsigned int fail_ok
;
187 int (*hw_fn
)(unsigned int);
188 } smbb_charger_attrs
[] = {
190 .name
= "qcom,fast-charge-safe-current",
191 .reg
= SMBB_CHG_ISAFE
,
194 .hw_fn
= smbb_bat_imax_fn
,
198 .name
= "qcom,fast-charge-current-limit",
199 .reg
= SMBB_CHG_IMAX
,
200 .safe_reg
= SMBB_CHG_ISAFE
,
203 .hw_fn
= smbb_bat_imax_fn
,
206 .name
= "qcom,dc-current-limit",
210 .hw_fn
= smbb_imax_fn
,
213 .name
= "qcom,fast-charge-safe-voltage",
214 .reg
= SMBB_CHG_VSAFE
,
217 .hw_fn
= smbb_vmax_fn
,
221 .name
= "qcom,fast-charge-high-threshold-voltage",
222 .reg
= SMBB_CHG_VMAX
,
223 .safe_reg
= SMBB_CHG_VSAFE
,
226 .hw_fn
= smbb_vmax_fn
,
229 .name
= "qcom,fast-charge-low-threshold-voltage",
230 .reg
= SMBB_CHG_VBAT_WEAK
,
233 .hw_fn
= smbb_vbat_weak_fn
,
236 .name
= "qcom,auto-recharge-threshold-voltage",
237 .reg
= SMBB_CHG_VBAT_DET
,
240 .hw_fn
= smbb_vbat_det_fn
,
243 .name
= "qcom,minimum-input-voltage",
244 .reg
= SMBB_CHG_VIN_MIN
,
247 .hw_fn
= smbb_vin_fn
,
249 [ATTR_USBIN_IMAX
] = {
250 .name
= "usb-charge-current-limit",
251 .reg
= SMBB_USB_IMAX
,
254 .hw_fn
= smbb_imax_fn
,
258 static int smbb_charger_attr_write(struct smbb_charger
*chg
,
259 enum smbb_attr which
, unsigned int val
)
261 const struct smbb_charger_attr
*prop
;
266 prop
= &smbb_charger_attrs
[which
];
268 if (val
> prop
->max
|| val
< prop
->min
) {
269 dev_err(chg
->dev
, "value out of range for %s [%u:%u]\n",
270 prop
->name
, prop
->min
, prop
->max
);
274 if (prop
->safe_reg
) {
275 rc
= regmap_read(chg
->regmap
,
276 chg
->addr
+ prop
->safe_reg
, &wval
);
279 "unable to read safe value for '%s'\n",
284 wval
= prop
->hw_fn(wval
);
288 "%s above safe value, clamping at %u\n",
294 wval
= smbb_hw_lookup(val
, prop
->hw_fn
);
296 rc
= regmap_write(chg
->regmap
, chg
->addr
+ prop
->reg
, wval
);
298 dev_err(chg
->dev
, "unable to update %s", prop
->name
);
301 out
= prop
->hw_fn(wval
);
304 "%s inaccurate, rounded to %u\n",
308 dev_dbg(chg
->dev
, "%s <= %d\n", prop
->name
, out
);
310 chg
->attr
[which
] = out
;
315 static int smbb_charger_attr_read(struct smbb_charger
*chg
,
316 enum smbb_attr which
)
318 const struct smbb_charger_attr
*prop
;
322 prop
= &smbb_charger_attrs
[which
];
324 rc
= regmap_read(chg
->regmap
, chg
->addr
+ prop
->reg
, &val
);
326 dev_err(chg
->dev
, "failed to read %s\n", prop
->name
);
329 val
= prop
->hw_fn(val
);
330 dev_dbg(chg
->dev
, "%s => %d\n", prop
->name
, val
);
332 chg
->attr
[which
] = val
;
337 static int smbb_charger_attr_parse(struct smbb_charger
*chg
,
338 enum smbb_attr which
)
340 const struct smbb_charger_attr
*prop
;
344 prop
= &smbb_charger_attrs
[which
];
346 rc
= of_property_read_u32(chg
->dev
->of_node
, prop
->name
, &val
);
348 rc
= smbb_charger_attr_write(chg
, which
, val
);
349 if (!rc
|| !prop
->fail_ok
)
352 return smbb_charger_attr_read(chg
, which
);
355 static void smbb_set_line_flag(struct smbb_charger
*chg
, int irq
, int flag
)
360 ret
= irq_get_irqchip_state(irq
, IRQCHIP_STATE_LINE_LEVEL
, &state
);
362 dev_err(chg
->dev
, "failed to read irq line\n");
366 mutex_lock(&chg
->statlock
);
370 chg
->status
&= ~flag
;
371 mutex_unlock(&chg
->statlock
);
373 dev_dbg(chg
->dev
, "status = %03lx\n", chg
->status
);
376 static irqreturn_t
smbb_usb_valid_handler(int irq
, void *_data
)
378 struct smbb_charger
*chg
= _data
;
380 smbb_set_line_flag(chg
, irq
, STATUS_USBIN_VALID
);
381 extcon_set_cable_state_(chg
->edev
, EXTCON_USB
,
382 chg
->status
& STATUS_USBIN_VALID
);
383 power_supply_changed(chg
->usb_psy
);
388 static irqreturn_t
smbb_dc_valid_handler(int irq
, void *_data
)
390 struct smbb_charger
*chg
= _data
;
392 smbb_set_line_flag(chg
, irq
, STATUS_DCIN_VALID
);
393 if (!chg
->dc_disabled
)
394 power_supply_changed(chg
->dc_psy
);
399 static irqreturn_t
smbb_bat_temp_handler(int irq
, void *_data
)
401 struct smbb_charger
*chg
= _data
;
405 rc
= regmap_read(chg
->regmap
, chg
->addr
+ SMBB_BAT_TEMP_STATUS
, &val
);
409 mutex_lock(&chg
->statlock
);
410 if (val
& TEMP_STATUS_OK
) {
411 chg
->status
|= STATUS_BAT_OK
;
413 chg
->status
&= ~STATUS_BAT_OK
;
414 if (val
& TEMP_STATUS_HOT
)
415 chg
->status
|= STATUS_BAT_HOT
;
417 mutex_unlock(&chg
->statlock
);
419 power_supply_changed(chg
->bat_psy
);
423 static irqreturn_t
smbb_bat_present_handler(int irq
, void *_data
)
425 struct smbb_charger
*chg
= _data
;
427 smbb_set_line_flag(chg
, irq
, STATUS_BAT_PRESENT
);
428 power_supply_changed(chg
->bat_psy
);
433 static irqreturn_t
smbb_chg_done_handler(int irq
, void *_data
)
435 struct smbb_charger
*chg
= _data
;
437 smbb_set_line_flag(chg
, irq
, STATUS_CHG_DONE
);
438 power_supply_changed(chg
->bat_psy
);
443 static irqreturn_t
smbb_chg_gone_handler(int irq
, void *_data
)
445 struct smbb_charger
*chg
= _data
;
447 smbb_set_line_flag(chg
, irq
, STATUS_CHG_GONE
);
448 power_supply_changed(chg
->bat_psy
);
449 power_supply_changed(chg
->usb_psy
);
450 if (!chg
->dc_disabled
)
451 power_supply_changed(chg
->dc_psy
);
456 static irqreturn_t
smbb_chg_fast_handler(int irq
, void *_data
)
458 struct smbb_charger
*chg
= _data
;
460 smbb_set_line_flag(chg
, irq
, STATUS_CHG_FAST
);
461 power_supply_changed(chg
->bat_psy
);
466 static irqreturn_t
smbb_chg_trkl_handler(int irq
, void *_data
)
468 struct smbb_charger
*chg
= _data
;
470 smbb_set_line_flag(chg
, irq
, STATUS_CHG_TRKL
);
471 power_supply_changed(chg
->bat_psy
);
476 static const struct smbb_irq
{
478 irqreturn_t (*handler
)(int, void *);
479 } smbb_charger_irqs
[] = {
480 { "chg-done", smbb_chg_done_handler
},
481 { "chg-fast", smbb_chg_fast_handler
},
482 { "chg-trkl", smbb_chg_trkl_handler
},
483 { "bat-temp-ok", smbb_bat_temp_handler
},
484 { "bat-present", smbb_bat_present_handler
},
485 { "chg-gone", smbb_chg_gone_handler
},
486 { "usb-valid", smbb_usb_valid_handler
},
487 { "dc-valid", smbb_dc_valid_handler
},
490 static int smbb_usbin_get_property(struct power_supply
*psy
,
491 enum power_supply_property psp
,
492 union power_supply_propval
*val
)
494 struct smbb_charger
*chg
= power_supply_get_drvdata(psy
);
498 case POWER_SUPPLY_PROP_ONLINE
:
499 mutex_lock(&chg
->statlock
);
500 val
->intval
= !(chg
->status
& STATUS_CHG_GONE
) &&
501 (chg
->status
& STATUS_USBIN_VALID
);
502 mutex_unlock(&chg
->statlock
);
504 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
:
505 val
->intval
= chg
->attr
[ATTR_USBIN_IMAX
];
507 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX
:
508 val
->intval
= 2500000;
518 static int smbb_usbin_set_property(struct power_supply
*psy
,
519 enum power_supply_property psp
,
520 const union power_supply_propval
*val
)
522 struct smbb_charger
*chg
= power_supply_get_drvdata(psy
);
526 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
:
527 rc
= smbb_charger_attr_write(chg
, ATTR_USBIN_IMAX
,
538 static int smbb_dcin_get_property(struct power_supply
*psy
,
539 enum power_supply_property psp
,
540 union power_supply_propval
*val
)
542 struct smbb_charger
*chg
= power_supply_get_drvdata(psy
);
546 case POWER_SUPPLY_PROP_ONLINE
:
547 mutex_lock(&chg
->statlock
);
548 val
->intval
= !(chg
->status
& STATUS_CHG_GONE
) &&
549 (chg
->status
& STATUS_DCIN_VALID
);
550 mutex_unlock(&chg
->statlock
);
552 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
:
553 val
->intval
= chg
->attr
[ATTR_DCIN_IMAX
];
555 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX
:
556 val
->intval
= 2500000;
566 static int smbb_dcin_set_property(struct power_supply
*psy
,
567 enum power_supply_property psp
,
568 const union power_supply_propval
*val
)
570 struct smbb_charger
*chg
= power_supply_get_drvdata(psy
);
574 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
:
575 rc
= smbb_charger_attr_write(chg
, ATTR_DCIN_IMAX
,
586 static int smbb_charger_writable_property(struct power_supply
*psy
,
587 enum power_supply_property psp
)
589 return psp
== POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
;
592 static int smbb_battery_get_property(struct power_supply
*psy
,
593 enum power_supply_property psp
,
594 union power_supply_propval
*val
)
596 struct smbb_charger
*chg
= power_supply_get_drvdata(psy
);
597 unsigned long status
;
600 mutex_lock(&chg
->statlock
);
601 status
= chg
->status
;
602 mutex_unlock(&chg
->statlock
);
605 case POWER_SUPPLY_PROP_STATUS
:
606 if (status
& STATUS_CHG_GONE
)
607 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
608 else if (!(status
& (STATUS_DCIN_VALID
| STATUS_USBIN_VALID
)))
609 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
610 else if (status
& STATUS_CHG_DONE
)
611 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
612 else if (!(status
& STATUS_BAT_OK
))
613 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
614 else if (status
& (STATUS_CHG_FAST
| STATUS_CHG_TRKL
))
615 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
616 else /* everything is ok for charging, but we are not... */
617 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
619 case POWER_SUPPLY_PROP_HEALTH
:
620 if (status
& STATUS_BAT_OK
)
621 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
622 else if (status
& STATUS_BAT_HOT
)
623 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
625 val
->intval
= POWER_SUPPLY_HEALTH_COLD
;
627 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
628 if (status
& STATUS_CHG_FAST
)
629 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
630 else if (status
& STATUS_CHG_TRKL
)
631 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_TRICKLE
;
633 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
635 case POWER_SUPPLY_PROP_PRESENT
:
636 val
->intval
= !!(status
& STATUS_BAT_PRESENT
);
638 case POWER_SUPPLY_PROP_CURRENT_MAX
:
639 val
->intval
= chg
->attr
[ATTR_BAT_IMAX
];
641 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
642 val
->intval
= chg
->attr
[ATTR_BAT_VMAX
];
644 case POWER_SUPPLY_PROP_TECHNOLOGY
:
645 /* this charger is a single-cell lithium-ion battery charger
646 * only. If you hook up some other technology, there will be
649 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
651 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
652 val
->intval
= 3000000; /* single-cell li-ion low end */
662 static int smbb_battery_set_property(struct power_supply
*psy
,
663 enum power_supply_property psp
,
664 const union power_supply_propval
*val
)
666 struct smbb_charger
*chg
= power_supply_get_drvdata(psy
);
670 case POWER_SUPPLY_PROP_CURRENT_MAX
:
671 rc
= smbb_charger_attr_write(chg
, ATTR_BAT_IMAX
, val
->intval
);
673 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
674 rc
= smbb_charger_attr_write(chg
, ATTR_BAT_VMAX
, val
->intval
);
684 static int smbb_battery_writable_property(struct power_supply
*psy
,
685 enum power_supply_property psp
)
688 case POWER_SUPPLY_PROP_CURRENT_MAX
:
689 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
696 static enum power_supply_property smbb_charger_properties
[] = {
697 POWER_SUPPLY_PROP_ONLINE
,
698 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT
,
699 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX
,
702 static enum power_supply_property smbb_battery_properties
[] = {
703 POWER_SUPPLY_PROP_STATUS
,
704 POWER_SUPPLY_PROP_HEALTH
,
705 POWER_SUPPLY_PROP_PRESENT
,
706 POWER_SUPPLY_PROP_CHARGE_TYPE
,
707 POWER_SUPPLY_PROP_CURRENT_MAX
,
708 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
709 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
710 POWER_SUPPLY_PROP_TECHNOLOGY
,
713 static const struct reg_off_mask_default
{
717 unsigned int rev_mask
;
718 } smbb_charger_setup
[] = {
719 /* The bootloader is supposed to set this... make sure anyway. */
720 { SMBB_MISC_BOOT_DONE
, BOOT_DONE
, BOOT_DONE
},
722 /* Disable software timer */
723 { SMBB_CHG_TCHG_MAX_EN
, TCHG_MAX_EN
, 0 },
725 /* Clear and disable watchdog */
726 { SMBB_CHG_WDOG_TIME
, 0xff, 160 },
727 { SMBB_CHG_WDOG_EN
, WDOG_EN
, 0 },
729 /* Use charger based EoC detection */
730 { SMBB_CHG_IBAT_TERM_CHG
, IBAT_TERM_CHG_IEOC
, IBAT_TERM_CHG_IEOC_CHG
},
732 /* Disable GSM PA load adjustment.
733 * The PA signal is incorrectly connected on v2.
735 { SMBB_CHG_CFG
, 0xff, 0x00, BIT(3) },
737 /* Use VBAT (not VSYS) to compensate for IR drop during fast charging */
738 { SMBB_BUCK_REG_MODE
, BUCK_REG_MODE
, BUCK_REG_MODE_VBAT
},
740 /* Enable battery temperature comparators */
741 { SMBB_BAT_BTC_CTRL
, BTC_CTRL_COMP_EN
, BTC_CTRL_COMP_EN
},
743 /* Stop USB enumeration timer */
744 { SMBB_USB_ENUM_TIMER_STOP
, ENUM_TIMER_STOP
, ENUM_TIMER_STOP
},
746 #if 0 /* FIXME supposedly only to disable hardware ARB termination */
747 { SMBB_USB_SEC_ACCESS
, SEC_ACCESS_MAGIC
},
748 { SMBB_USB_REV_BST
, 0xff, REV_BST_CHG_GONE
},
751 /* Stop USB enumeration timer, again */
752 { SMBB_USB_ENUM_TIMER_STOP
, ENUM_TIMER_STOP
, ENUM_TIMER_STOP
},
754 /* Enable charging */
755 { SMBB_CHG_CTRL
, CTRL_EN
, CTRL_EN
},
758 static char *smbb_bif
[] = { "smbb-bif" };
760 static const struct power_supply_desc bat_psy_desc
= {
762 .type
= POWER_SUPPLY_TYPE_BATTERY
,
763 .properties
= smbb_battery_properties
,
764 .num_properties
= ARRAY_SIZE(smbb_battery_properties
),
765 .get_property
= smbb_battery_get_property
,
766 .set_property
= smbb_battery_set_property
,
767 .property_is_writeable
= smbb_battery_writable_property
,
770 static const struct power_supply_desc usb_psy_desc
= {
771 .name
= "smbb-usbin",
772 .type
= POWER_SUPPLY_TYPE_USB
,
773 .properties
= smbb_charger_properties
,
774 .num_properties
= ARRAY_SIZE(smbb_charger_properties
),
775 .get_property
= smbb_usbin_get_property
,
776 .set_property
= smbb_usbin_set_property
,
777 .property_is_writeable
= smbb_charger_writable_property
,
780 static const struct power_supply_desc dc_psy_desc
= {
782 .type
= POWER_SUPPLY_TYPE_MAINS
,
783 .properties
= smbb_charger_properties
,
784 .num_properties
= ARRAY_SIZE(smbb_charger_properties
),
785 .get_property
= smbb_dcin_get_property
,
786 .set_property
= smbb_dcin_set_property
,
787 .property_is_writeable
= smbb_charger_writable_property
,
790 static int smbb_charger_probe(struct platform_device
*pdev
)
792 struct power_supply_config bat_cfg
= {};
793 struct power_supply_config usb_cfg
= {};
794 struct power_supply_config dc_cfg
= {};
795 struct smbb_charger
*chg
;
798 chg
= devm_kzalloc(&pdev
->dev
, sizeof(*chg
), GFP_KERNEL
);
802 chg
->dev
= &pdev
->dev
;
803 mutex_init(&chg
->statlock
);
805 chg
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
807 dev_err(&pdev
->dev
, "failed to locate regmap\n");
811 rc
= of_property_read_u32(pdev
->dev
.of_node
, "reg", &chg
->addr
);
813 dev_err(&pdev
->dev
, "missing or invalid 'reg' property\n");
817 rc
= regmap_read(chg
->regmap
, chg
->addr
+ SMBB_MISC_REV2
, &chg
->revision
);
819 dev_err(&pdev
->dev
, "unable to read revision\n");
824 if (chg
->revision
!= 2 && chg
->revision
!= 3) {
825 dev_err(&pdev
->dev
, "v1 hardware not supported\n");
828 dev_info(&pdev
->dev
, "Initializing SMBB rev %u", chg
->revision
);
830 chg
->dc_disabled
= of_property_read_bool(pdev
->dev
.of_node
, "qcom,disable-dc");
832 for (i
= 0; i
< _ATTR_CNT
; ++i
) {
833 rc
= smbb_charger_attr_parse(chg
, i
);
835 dev_err(&pdev
->dev
, "failed to parse/apply settings\n");
840 bat_cfg
.drv_data
= chg
;
841 bat_cfg
.of_node
= pdev
->dev
.of_node
;
842 chg
->bat_psy
= devm_power_supply_register(&pdev
->dev
,
845 if (IS_ERR(chg
->bat_psy
)) {
846 dev_err(&pdev
->dev
, "failed to register battery\n");
847 return PTR_ERR(chg
->bat_psy
);
850 usb_cfg
.drv_data
= chg
;
851 usb_cfg
.supplied_to
= smbb_bif
;
852 usb_cfg
.num_supplicants
= ARRAY_SIZE(smbb_bif
);
853 chg
->usb_psy
= devm_power_supply_register(&pdev
->dev
,
856 if (IS_ERR(chg
->usb_psy
)) {
857 dev_err(&pdev
->dev
, "failed to register USB power supply\n");
858 return PTR_ERR(chg
->usb_psy
);
861 chg
->edev
= devm_extcon_dev_allocate(&pdev
->dev
, smbb_usb_extcon_cable
);
862 if (IS_ERR(chg
->edev
)) {
863 dev_err(&pdev
->dev
, "failed to allocate extcon device\n");
867 rc
= devm_extcon_dev_register(&pdev
->dev
, chg
->edev
);
869 dev_err(&pdev
->dev
, "failed to register extcon device\n");
873 if (!chg
->dc_disabled
) {
874 dc_cfg
.drv_data
= chg
;
875 dc_cfg
.supplied_to
= smbb_bif
;
876 dc_cfg
.num_supplicants
= ARRAY_SIZE(smbb_bif
);
877 chg
->dc_psy
= devm_power_supply_register(&pdev
->dev
,
880 if (IS_ERR(chg
->dc_psy
)) {
881 dev_err(&pdev
->dev
, "failed to register DC power supply\n");
882 return PTR_ERR(chg
->dc_psy
);
886 for (i
= 0; i
< ARRAY_SIZE(smbb_charger_irqs
); ++i
) {
889 irq
= platform_get_irq_byname(pdev
, smbb_charger_irqs
[i
].name
);
891 dev_err(&pdev
->dev
, "failed to get irq '%s'\n",
892 smbb_charger_irqs
[i
].name
);
896 smbb_charger_irqs
[i
].handler(irq
, chg
);
898 rc
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
899 smbb_charger_irqs
[i
].handler
, IRQF_ONESHOT
,
900 smbb_charger_irqs
[i
].name
, chg
);
902 dev_err(&pdev
->dev
, "failed to request irq '%s'\n",
903 smbb_charger_irqs
[i
].name
);
908 chg
->jeita_ext_temp
= of_property_read_bool(pdev
->dev
.of_node
,
909 "qcom,jeita-extended-temp-range");
911 /* Set temperature range to [35%:70%] or [25%:80%] accordingly */
912 rc
= regmap_update_bits(chg
->regmap
, chg
->addr
+ SMBB_BAT_BTC_CTRL
,
913 BTC_CTRL_COLD_EXT
| BTC_CTRL_HOT_EXT_N
,
914 chg
->jeita_ext_temp
?
919 "unable to set %s temperature range\n",
920 chg
->jeita_ext_temp
? "JEITA extended" : "normal");
924 for (i
= 0; i
< ARRAY_SIZE(smbb_charger_setup
); ++i
) {
925 const struct reg_off_mask_default
*r
= &smbb_charger_setup
[i
];
927 if (r
->rev_mask
& BIT(chg
->revision
))
930 rc
= regmap_update_bits(chg
->regmap
, chg
->addr
+ r
->offset
,
934 "unable to initializing charging, bailing\n");
939 platform_set_drvdata(pdev
, chg
);
944 static int smbb_charger_remove(struct platform_device
*pdev
)
946 struct smbb_charger
*chg
;
948 chg
= platform_get_drvdata(pdev
);
950 regmap_update_bits(chg
->regmap
, chg
->addr
+ SMBB_CHG_CTRL
, CTRL_EN
, 0);
955 static const struct of_device_id smbb_charger_id_table
[] = {
956 { .compatible
= "qcom,pm8941-charger" },
959 MODULE_DEVICE_TABLE(of
, smbb_charger_id_table
);
961 static struct platform_driver smbb_charger_driver
= {
962 .probe
= smbb_charger_probe
,
963 .remove
= smbb_charger_remove
,
966 .of_match_table
= smbb_charger_id_table
,
969 module_platform_driver(smbb_charger_driver
);
971 MODULE_DESCRIPTION("Qualcomm Switch-Mode Battery Charger and Boost driver");
972 MODULE_LICENSE("GPL v2");