13 #include <linux/module.h>
14 #include <linux/slab.h>
16 #include <linux/i2c.h>
20 #define LP8788_NUM_INTREGS 2
21 #define DEFAULT_DEBOUNCE_MSEC 270
24 #define LP8727_CTRL1 0x1
25 #define LP8727_CTRL2 0x2
26 #define LP8727_SWCTRL 0x3
27 #define LP8727_INT1 0x4
28 #define LP8727_INT2 0x5
29 #define LP8727_STATUS1 0x6
30 #define LP8727_STATUS2 0x7
31 #define LP8727_CHGCTRL2 0x9
34 #define LP8727_CP_EN BIT(0)
35 #define LP8727_ADC_EN BIT(1)
36 #define LP8727_ID200_EN BIT(4)
39 #define LP8727_CHGDET_EN BIT(1)
40 #define LP8727_INT_EN BIT(6)
43 #define LP8727_SW_DM1_DM (0x0 << 0)
44 #define LP8727_SW_DM1_HiZ (0x7 << 0)
45 #define LP8727_SW_DP2_DP (0x0 << 3)
46 #define LP8727_SW_DP2_HiZ (0x7 << 3)
49 #define LP8727_IDNO (0xF << 0)
50 #define LP8727_VBUS BIT(4)
53 #define LP8727_CHGSTAT (3 << 4)
54 #define LP8727_CHPORT BIT(6)
55 #define LP8727_DCPORT BIT(7)
56 #define LP8727_STAT_EOC 0x30
59 #define LP8727_TEMP_STAT (3 << 5)
60 #define LP8727_TEMP_SHIFT 5
63 #define LP8727_ICHG_SHIFT 4
112 return (ret != len) ? -
EIO : 0;
117 return lp8727_read_bytes(pchg, reg, data, 1);
131 static bool lp8727_is_charger_attached(
const char *
name,
int id)
135 else if (!
strcmp(name,
"usb"))
141 static int lp8727_init_device(
struct lp8727_chg *pchg)
161 static int lp8727_is_dedicated_charger(
struct lp8727_chg *pchg)
169 static int lp8727_is_usb_charger(
struct lp8727_chg *pchg)
177 static inline void lp8727_ctrl_switch(
struct lp8727_chg *pchg,
u8 sw)
182 static void lp8727_id_detection(
struct lp8727_chg *pchg,
u8 id,
int vbusin)
194 if (lp8727_is_dedicated_charger(pchg)) {
197 }
else if (lp8727_is_usb_charger(pchg)) {
213 lp8727_ctrl_switch(pchg, swctrl);
216 static void lp8727_enable_chgdet(
struct lp8727_chg *pchg)
225 static void lp8727_delayed_func(
struct work_struct *_work)
234 dev_err(pchg->
dev,
"can not read INT registers\n");
241 lp8727_id_detection(pchg, idno, vbus);
242 lp8727_enable_chgdet(pchg);
257 static int lp8727_setup_irq(
struct lp8727_chg *pchg)
261 unsigned delay_msec = pchg->
pdata ? pchg->
pdata->debounce_msec :
267 dev_warn(pchg->
dev,
"invalid irq number: %d\n", irq);
284 static void lp8727_release_irq(
struct lp8727_chg *pchg)
305 static char *battery_supplied_to[] = {
335 static int lp8727_battery_get_property(
struct power_supply *psy,
346 if (!lp8727_is_charger_attached(psy->
name, pchg->
devid)) {
361 val->
intval = lp8727_is_high_temperature(temp) ?
400 static void lp8727_charger_changed(
struct power_supply *psy)
408 if (!lp8727_is_charger_attached(psy->
name, pchg->
devid))
420 static int lp8727_register_psy(
struct lp8727_chg *pchg)
432 psy->
ac.properties = lp8727_charger_prop;
433 psy->
ac.num_properties =
ARRAY_SIZE(lp8727_charger_prop);
434 psy->
ac.get_property = lp8727_charger_get_property;
435 psy->
ac.supplied_to = battery_supplied_to;
436 psy->
ac.num_supplicants =
ARRAY_SIZE(battery_supplied_to);
441 psy->
usb.name =
"usb";
443 psy->
usb.properties = lp8727_charger_prop;
445 psy->
usb.get_property = lp8727_charger_get_property;
446 psy->
usb.supplied_to = battery_supplied_to;
452 psy->
batt.name =
"main_batt";
454 psy->
batt.properties = lp8727_battery_prop;
456 psy->
batt.get_property = lp8727_battery_get_property;
457 psy->
batt.external_power_changed = lp8727_charger_changed;
472 static void lp8727_unregister_psy(
struct lp8727_chg *pchg)
498 pchg->
pdata = cl->
dev.platform_data;
499 i2c_set_clientdata(cl, pchg);
503 ret = lp8727_init_device(pchg);
505 dev_err(pchg->
dev,
"i2c communication err: %d", ret);
509 ret = lp8727_register_psy(pchg);
511 dev_err(pchg->
dev,
"power supplies register err: %d", ret);
515 ret = lp8727_setup_irq(pchg);
517 dev_err(pchg->
dev,
"irq handler err: %d", ret);
518 lp8727_unregister_psy(pchg);
527 struct lp8727_chg *pchg = i2c_get_clientdata(cl);
529 lp8727_release_irq(pchg);
530 lp8727_unregister_psy(pchg);
544 .probe = lp8727_probe,
546 .id_table = lp8727_ids,