1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * bq2415x charger driver
5 * Copyright (C) 2011-2013 Pali Rohár <pali.rohar@gmail.com>
8 * http://www.ti.com/product/bq24150
9 * http://www.ti.com/product/bq24150a
10 * http://www.ti.com/product/bq24152
11 * http://www.ti.com/product/bq24153
12 * http://www.ti.com/product/bq24153a
13 * http://www.ti.com/product/bq24155
14 * http://www.ti.com/product/bq24157s
15 * http://www.ti.com/product/bq24158
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/param.h>
21 #include <linux/err.h>
22 #include <linux/workqueue.h>
23 #include <linux/sysfs.h>
24 #include <linux/platform_device.h>
25 #include <linux/power_supply.h>
26 #include <linux/idr.h>
27 #include <linux/i2c.h>
28 #include <linux/slab.h>
29 #include <linux/acpi.h>
31 #include <linux/power/bq2415x_charger.h>
33 /* timeout for resetting chip timer */
34 #define BQ2415X_TIMER_TIMEOUT 10
36 #define BQ2415X_REG_STATUS 0x00
37 #define BQ2415X_REG_CONTROL 0x01
38 #define BQ2415X_REG_VOLTAGE 0x02
39 #define BQ2415X_REG_VENDER 0x03
40 #define BQ2415X_REG_CURRENT 0x04
42 /* reset state for all registers */
43 #define BQ2415X_RESET_STATUS BIT(6)
44 #define BQ2415X_RESET_CONTROL (BIT(4)|BIT(5))
45 #define BQ2415X_RESET_VOLTAGE (BIT(1)|BIT(3))
46 #define BQ2415X_RESET_CURRENT (BIT(0)|BIT(3)|BIT(7))
49 #define BQ2415X_BIT_TMR_RST 7
50 #define BQ2415X_BIT_OTG 7
51 #define BQ2415X_BIT_EN_STAT 6
52 #define BQ2415X_MASK_STAT (BIT(4)|BIT(5))
53 #define BQ2415X_SHIFT_STAT 4
54 #define BQ2415X_BIT_BOOST 3
55 #define BQ2415X_MASK_FAULT (BIT(0)|BIT(1)|BIT(2))
56 #define BQ2415X_SHIFT_FAULT 0
58 /* control register */
59 #define BQ2415X_MASK_LIMIT (BIT(6)|BIT(7))
60 #define BQ2415X_SHIFT_LIMIT 6
61 #define BQ2415X_MASK_VLOWV (BIT(4)|BIT(5))
62 #define BQ2415X_SHIFT_VLOWV 4
63 #define BQ2415X_BIT_TE 3
64 #define BQ2415X_BIT_CE 2
65 #define BQ2415X_BIT_HZ_MODE 1
66 #define BQ2415X_BIT_OPA_MODE 0
68 /* voltage register */
69 #define BQ2415X_MASK_VO (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
70 #define BQ2415X_SHIFT_VO 2
71 #define BQ2415X_BIT_OTG_PL 1
72 #define BQ2415X_BIT_OTG_EN 0
75 #define BQ2415X_MASK_VENDER (BIT(5)|BIT(6)|BIT(7))
76 #define BQ2415X_SHIFT_VENDER 5
77 #define BQ2415X_MASK_PN (BIT(3)|BIT(4))
78 #define BQ2415X_SHIFT_PN 3
79 #define BQ2415X_MASK_REVISION (BIT(0)|BIT(1)|BIT(2))
80 #define BQ2415X_SHIFT_REVISION 0
82 /* current register */
83 #define BQ2415X_MASK_RESET BIT(7)
84 #define BQ2415X_MASK_VI_CHRG (BIT(4)|BIT(5)|BIT(6))
85 #define BQ2415X_SHIFT_VI_CHRG 4
87 #define BQ2415X_MASK_VI_TERM (BIT(0)|BIT(1)|BIT(2))
88 #define BQ2415X_SHIFT_VI_TERM 0
91 enum bq2415x_command
{
94 BQ2415X_STAT_PIN_STATUS
,
95 BQ2415X_STAT_PIN_ENABLE
,
96 BQ2415X_STAT_PIN_DISABLE
,
97 BQ2415X_CHARGE_STATUS
,
101 BQ2415X_CHARGE_TERMINATION_STATUS
,
102 BQ2415X_CHARGE_TERMINATION_ENABLE
,
103 BQ2415X_CHARGE_TERMINATION_DISABLE
,
104 BQ2415X_CHARGER_STATUS
,
105 BQ2415X_CHARGER_ENABLE
,
106 BQ2415X_CHARGER_DISABLE
,
107 BQ2415X_HIGH_IMPEDANCE_STATUS
,
108 BQ2415X_HIGH_IMPEDANCE_ENABLE
,
109 BQ2415X_HIGH_IMPEDANCE_DISABLE
,
110 BQ2415X_BOOST_MODE_STATUS
,
111 BQ2415X_BOOST_MODE_ENABLE
,
112 BQ2415X_BOOST_MODE_DISABLE
,
115 BQ2415X_OTG_ACTIVATE_HIGH
,
116 BQ2415X_OTG_ACTIVATE_LOW
,
117 BQ2415X_OTG_PIN_STATUS
,
118 BQ2415X_OTG_PIN_ENABLE
,
119 BQ2415X_OTG_PIN_DISABLE
,
142 static char *bq2415x_chip_name
[] = {
158 struct bq2415x_device
{
160 struct bq2415x_platform_data init_data
;
161 struct power_supply
*charger
;
162 struct power_supply_desc charger_desc
;
163 struct delayed_work work
;
164 struct device_node
*notify_node
;
165 struct notifier_block nb
;
166 enum bq2415x_mode reported_mode
;/* mode reported by hook function */
167 enum bq2415x_mode mode
; /* currently configured mode */
168 enum bq2415x_chip chip
;
169 const char *timer_error
;
172 int autotimer
; /* 1 - if driver automatically reset timer, 0 - not */
173 int automode
; /* 1 - enabled, 0 - disabled; -1 - not supported */
177 /* each registered chip must have unique id */
178 static DEFINE_IDR(bq2415x_id
);
180 static DEFINE_MUTEX(bq2415x_id_mutex
);
181 static DEFINE_MUTEX(bq2415x_timer_mutex
);
182 static DEFINE_MUTEX(bq2415x_i2c_mutex
);
184 /**** i2c read functions ****/
186 /* read value from register */
187 static int bq2415x_i2c_read(struct bq2415x_device
*bq
, u8 reg
)
189 struct i2c_client
*client
= to_i2c_client(bq
->dev
);
190 struct i2c_msg msg
[2];
194 if (!client
->adapter
)
197 msg
[0].addr
= client
->addr
;
200 msg
[0].len
= sizeof(reg
);
201 msg
[1].addr
= client
->addr
;
202 msg
[1].flags
= I2C_M_RD
;
204 msg
[1].len
= sizeof(val
);
206 mutex_lock(&bq2415x_i2c_mutex
);
207 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
208 mutex_unlock(&bq2415x_i2c_mutex
);
216 /* read value from register, apply mask and right shift it */
217 static int bq2415x_i2c_read_mask(struct bq2415x_device
*bq
, u8 reg
,
225 ret
= bq2415x_i2c_read(bq
, reg
);
228 return (ret
& mask
) >> shift
;
231 /* read value from register and return one specified bit */
232 static int bq2415x_i2c_read_bit(struct bq2415x_device
*bq
, u8 reg
, u8 bit
)
236 return bq2415x_i2c_read_mask(bq
, reg
, BIT(bit
), bit
);
239 /**** i2c write functions ****/
241 /* write value to register */
242 static int bq2415x_i2c_write(struct bq2415x_device
*bq
, u8 reg
, u8 val
)
244 struct i2c_client
*client
= to_i2c_client(bq
->dev
);
245 struct i2c_msg msg
[1];
252 msg
[0].addr
= client
->addr
;
255 msg
[0].len
= ARRAY_SIZE(data
);
257 mutex_lock(&bq2415x_i2c_mutex
);
258 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
259 mutex_unlock(&bq2415x_i2c_mutex
);
261 /* i2c_transfer returns number of messages transferred */
270 /* read value from register, change it with mask left shifted and write back */
271 static int bq2415x_i2c_write_mask(struct bq2415x_device
*bq
, u8 reg
, u8 val
,
279 ret
= bq2415x_i2c_read(bq
, reg
);
286 return bq2415x_i2c_write(bq
, reg
, ret
);
289 /* change only one bit in register */
290 static int bq2415x_i2c_write_bit(struct bq2415x_device
*bq
, u8 reg
,
295 return bq2415x_i2c_write_mask(bq
, reg
, val
, BIT(bit
), bit
);
298 /**** global functions ****/
300 /* exec command function */
301 static int bq2415x_exec_command(struct bq2415x_device
*bq
,
302 enum bq2415x_command command
)
307 case BQ2415X_TIMER_RESET
:
308 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_STATUS
,
309 1, BQ2415X_BIT_TMR_RST
);
310 case BQ2415X_OTG_STATUS
:
311 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_STATUS
,
313 case BQ2415X_STAT_PIN_STATUS
:
314 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_STATUS
,
315 BQ2415X_BIT_EN_STAT
);
316 case BQ2415X_STAT_PIN_ENABLE
:
317 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_STATUS
, 1,
318 BQ2415X_BIT_EN_STAT
);
319 case BQ2415X_STAT_PIN_DISABLE
:
320 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_STATUS
, 0,
321 BQ2415X_BIT_EN_STAT
);
322 case BQ2415X_CHARGE_STATUS
:
323 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_STATUS
,
324 BQ2415X_MASK_STAT
, BQ2415X_SHIFT_STAT
);
325 case BQ2415X_BOOST_STATUS
:
326 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_STATUS
,
328 case BQ2415X_FAULT_STATUS
:
329 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_STATUS
,
330 BQ2415X_MASK_FAULT
, BQ2415X_SHIFT_FAULT
);
332 case BQ2415X_CHARGE_TERMINATION_STATUS
:
333 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_CONTROL
,
335 case BQ2415X_CHARGE_TERMINATION_ENABLE
:
336 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
338 case BQ2415X_CHARGE_TERMINATION_DISABLE
:
339 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
341 case BQ2415X_CHARGER_STATUS
:
342 ret
= bq2415x_i2c_read_bit(bq
, BQ2415X_REG_CONTROL
,
346 return ret
> 0 ? 0 : 1;
347 case BQ2415X_CHARGER_ENABLE
:
348 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
350 case BQ2415X_CHARGER_DISABLE
:
351 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
353 case BQ2415X_HIGH_IMPEDANCE_STATUS
:
354 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_CONTROL
,
355 BQ2415X_BIT_HZ_MODE
);
356 case BQ2415X_HIGH_IMPEDANCE_ENABLE
:
357 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
358 1, BQ2415X_BIT_HZ_MODE
);
359 case BQ2415X_HIGH_IMPEDANCE_DISABLE
:
360 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
361 0, BQ2415X_BIT_HZ_MODE
);
362 case BQ2415X_BOOST_MODE_STATUS
:
363 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_CONTROL
,
364 BQ2415X_BIT_OPA_MODE
);
365 case BQ2415X_BOOST_MODE_ENABLE
:
366 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
367 1, BQ2415X_BIT_OPA_MODE
);
368 case BQ2415X_BOOST_MODE_DISABLE
:
369 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
370 0, BQ2415X_BIT_OPA_MODE
);
372 case BQ2415X_OTG_LEVEL
:
373 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_VOLTAGE
,
375 case BQ2415X_OTG_ACTIVATE_HIGH
:
376 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_VOLTAGE
,
377 1, BQ2415X_BIT_OTG_PL
);
378 case BQ2415X_OTG_ACTIVATE_LOW
:
379 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_VOLTAGE
,
380 0, BQ2415X_BIT_OTG_PL
);
381 case BQ2415X_OTG_PIN_STATUS
:
382 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_VOLTAGE
,
384 case BQ2415X_OTG_PIN_ENABLE
:
385 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_VOLTAGE
,
386 1, BQ2415X_BIT_OTG_EN
);
387 case BQ2415X_OTG_PIN_DISABLE
:
388 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_VOLTAGE
,
389 0, BQ2415X_BIT_OTG_EN
);
391 case BQ2415X_VENDER_CODE
:
392 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_VENDER
,
393 BQ2415X_MASK_VENDER
, BQ2415X_SHIFT_VENDER
);
394 case BQ2415X_PART_NUMBER
:
395 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_VENDER
,
396 BQ2415X_MASK_PN
, BQ2415X_SHIFT_PN
);
397 case BQ2415X_REVISION
:
398 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_VENDER
,
399 BQ2415X_MASK_REVISION
, BQ2415X_SHIFT_REVISION
);
404 /* detect chip type */
405 static enum bq2415x_chip
bq2415x_detect_chip(struct bq2415x_device
*bq
)
407 struct i2c_client
*client
= to_i2c_client(bq
->dev
);
408 int ret
= bq2415x_exec_command(bq
, BQ2415X_PART_NUMBER
);
413 switch (client
->addr
) {
417 if (bq
->chip
== BQ24151A
)
421 if (bq
->chip
== BQ24150A
||
422 bq
->chip
== BQ24152
||
427 if (bq
->chip
== BQ24153A
)
438 if (bq
->chip
== BQ24156A
)
442 if (bq
->chip
== BQ24157S
)
454 /* detect chip revision */
455 static int bq2415x_detect_revision(struct bq2415x_device
*bq
)
457 int ret
= bq2415x_exec_command(bq
, BQ2415X_REVISION
);
458 int chip
= bq2415x_detect_chip(bq
);
460 if (ret
< 0 || chip
< 0)
469 if (ret
>= 0 && ret
<= 3)
494 /* return chip vender code */
495 static int bq2415x_get_vender_code(struct bq2415x_device
*bq
)
499 ret
= bq2415x_exec_command(bq
, BQ2415X_VENDER_CODE
);
503 /* convert to binary */
505 ((ret
>> 1) & 0x1) * 10 +
506 ((ret
>> 2) & 0x1) * 100;
509 /* reset all chip registers to default state */
510 static void bq2415x_reset_chip(struct bq2415x_device
*bq
)
512 bq2415x_i2c_write(bq
, BQ2415X_REG_CURRENT
, BQ2415X_RESET_CURRENT
);
513 bq2415x_i2c_write(bq
, BQ2415X_REG_VOLTAGE
, BQ2415X_RESET_VOLTAGE
);
514 bq2415x_i2c_write(bq
, BQ2415X_REG_CONTROL
, BQ2415X_RESET_CONTROL
);
515 bq2415x_i2c_write(bq
, BQ2415X_REG_STATUS
, BQ2415X_RESET_STATUS
);
516 bq
->timer_error
= NULL
;
519 /**** properties functions ****/
521 /* set current limit in mA */
522 static int bq2415x_set_current_limit(struct bq2415x_device
*bq
, int mA
)
535 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_CONTROL
, val
,
536 BQ2415X_MASK_LIMIT
, BQ2415X_SHIFT_LIMIT
);
539 /* get current limit in mA */
540 static int bq2415x_get_current_limit(struct bq2415x_device
*bq
)
544 ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_CONTROL
,
545 BQ2415X_MASK_LIMIT
, BQ2415X_SHIFT_LIMIT
);
559 /* set weak battery voltage in mV */
560 static int bq2415x_set_weak_battery_voltage(struct bq2415x_device
*bq
, int mV
)
567 else if (mV
<= 3500 + 50)
569 else if (mV
<= 3600 + 50)
574 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_CONTROL
, val
,
575 BQ2415X_MASK_VLOWV
, BQ2415X_SHIFT_VLOWV
);
578 /* get weak battery voltage in mV */
579 static int bq2415x_get_weak_battery_voltage(struct bq2415x_device
*bq
)
583 ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_CONTROL
,
584 BQ2415X_MASK_VLOWV
, BQ2415X_SHIFT_VLOWV
);
587 return 100 * (34 + ret
);
590 /* set battery regulation voltage in mV */
591 static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device
*bq
,
594 int val
= (mV
/10 - 350) / 2;
597 * According to datasheet, maximum battery regulation voltage is
598 * 4440mV which is b101111 = 47.
605 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_VOLTAGE
, val
,
606 BQ2415X_MASK_VO
, BQ2415X_SHIFT_VO
);
609 /* get battery regulation voltage in mV */
610 static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device
*bq
)
612 int ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_VOLTAGE
,
613 BQ2415X_MASK_VO
, BQ2415X_SHIFT_VO
);
617 return 10 * (350 + 2*ret
);
620 /* set charge current in mA (platform data must provide resistor sense) */
621 static int bq2415x_set_charge_current(struct bq2415x_device
*bq
, int mA
)
625 if (bq
->init_data
.resistor_sense
<= 0)
628 val
= (mA
* bq
->init_data
.resistor_sense
- 37400) / 6800;
634 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_CURRENT
, val
,
635 BQ2415X_MASK_VI_CHRG
| BQ2415X_MASK_RESET
,
636 BQ2415X_SHIFT_VI_CHRG
);
639 /* get charge current in mA (platform data must provide resistor sense) */
640 static int bq2415x_get_charge_current(struct bq2415x_device
*bq
)
644 if (bq
->init_data
.resistor_sense
<= 0)
647 ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_CURRENT
,
648 BQ2415X_MASK_VI_CHRG
, BQ2415X_SHIFT_VI_CHRG
);
651 return (37400 + 6800*ret
) / bq
->init_data
.resistor_sense
;
654 /* set termination current in mA (platform data must provide resistor sense) */
655 static int bq2415x_set_termination_current(struct bq2415x_device
*bq
, int mA
)
659 if (bq
->init_data
.resistor_sense
<= 0)
662 val
= (mA
* bq
->init_data
.resistor_sense
- 3400) / 3400;
668 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_CURRENT
, val
,
669 BQ2415X_MASK_VI_TERM
| BQ2415X_MASK_RESET
,
670 BQ2415X_SHIFT_VI_TERM
);
673 /* get termination current in mA (platform data must provide resistor sense) */
674 static int bq2415x_get_termination_current(struct bq2415x_device
*bq
)
678 if (bq
->init_data
.resistor_sense
<= 0)
681 ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_CURRENT
,
682 BQ2415X_MASK_VI_TERM
, BQ2415X_SHIFT_VI_TERM
);
685 return (3400 + 3400*ret
) / bq
->init_data
.resistor_sense
;
688 /* set default value of property */
689 #define bq2415x_set_default_value(bq, prop) \
692 if (bq->init_data.prop != -1) \
693 ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
698 /* set default values of all properties */
699 static int bq2415x_set_defaults(struct bq2415x_device
*bq
)
701 bq2415x_exec_command(bq
, BQ2415X_BOOST_MODE_DISABLE
);
702 bq2415x_exec_command(bq
, BQ2415X_CHARGER_DISABLE
);
703 bq2415x_exec_command(bq
, BQ2415X_CHARGE_TERMINATION_DISABLE
);
705 bq2415x_set_default_value(bq
, current_limit
);
706 bq2415x_set_default_value(bq
, weak_battery_voltage
);
707 bq2415x_set_default_value(bq
, battery_regulation_voltage
);
709 if (bq
->init_data
.resistor_sense
> 0) {
710 bq2415x_set_default_value(bq
, charge_current
);
711 bq2415x_set_default_value(bq
, termination_current
);
712 bq2415x_exec_command(bq
, BQ2415X_CHARGE_TERMINATION_ENABLE
);
715 bq2415x_exec_command(bq
, BQ2415X_CHARGER_ENABLE
);
719 /**** charger mode functions ****/
721 /* set charger mode */
722 static int bq2415x_set_mode(struct bq2415x_device
*bq
, enum bq2415x_mode mode
)
728 if (mode
== BQ2415X_MODE_BOOST
)
730 else if (mode
!= BQ2415X_MODE_OFF
)
734 ret
= bq2415x_exec_command(bq
, BQ2415X_CHARGER_DISABLE
);
737 ret
= bq2415x_exec_command(bq
, BQ2415X_BOOST_MODE_DISABLE
);
743 case BQ2415X_MODE_OFF
:
744 dev_dbg(bq
->dev
, "changing mode to: Offline\n");
745 ret
= bq2415x_set_current_limit(bq
, 100);
747 case BQ2415X_MODE_NONE
:
748 dev_dbg(bq
->dev
, "changing mode to: N/A\n");
749 ret
= bq2415x_set_current_limit(bq
, 100);
751 case BQ2415X_MODE_HOST_CHARGER
:
752 dev_dbg(bq
->dev
, "changing mode to: Host/HUB charger\n");
753 ret
= bq2415x_set_current_limit(bq
, 500);
755 case BQ2415X_MODE_DEDICATED_CHARGER
:
756 dev_dbg(bq
->dev
, "changing mode to: Dedicated charger\n");
757 ret
= bq2415x_set_current_limit(bq
, 1800);
759 case BQ2415X_MODE_BOOST
: /* Boost mode */
760 dev_dbg(bq
->dev
, "changing mode to: Boost\n");
761 ret
= bq2415x_set_current_limit(bq
, 100);
769 ret
= bq2415x_exec_command(bq
, BQ2415X_CHARGER_ENABLE
);
771 ret
= bq2415x_exec_command(bq
, BQ2415X_BOOST_MODE_ENABLE
);
776 bq2415x_set_default_value(bq
, weak_battery_voltage
);
777 bq2415x_set_default_value(bq
, battery_regulation_voltage
);
780 sysfs_notify(&bq
->charger
->dev
.kobj
, NULL
, "mode");
786 static bool bq2415x_update_reported_mode(struct bq2415x_device
*bq
, int mA
)
788 enum bq2415x_mode mode
;
791 mode
= BQ2415X_MODE_OFF
;
793 mode
= BQ2415X_MODE_NONE
;
795 mode
= BQ2415X_MODE_HOST_CHARGER
;
797 mode
= BQ2415X_MODE_DEDICATED_CHARGER
;
799 if (bq
->reported_mode
== mode
)
802 bq
->reported_mode
= mode
;
806 static int bq2415x_notifier_call(struct notifier_block
*nb
,
807 unsigned long val
, void *v
)
809 struct bq2415x_device
*bq
=
810 container_of(nb
, struct bq2415x_device
, nb
);
811 struct power_supply
*psy
= v
;
812 union power_supply_propval prop
;
815 if (val
!= PSY_EVENT_PROP_CHANGED
)
818 /* Ignore event if it was not send by notify_node/notify_device */
819 if (bq
->notify_node
) {
820 if (!psy
->dev
.parent
||
821 psy
->dev
.parent
->of_node
!= bq
->notify_node
)
823 } else if (bq
->init_data
.notify_device
) {
824 if (strcmp(psy
->desc
->name
, bq
->init_data
.notify_device
) != 0)
828 dev_dbg(bq
->dev
, "notifier call was called\n");
830 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_CURRENT_MAX
,
835 if (!bq2415x_update_reported_mode(bq
, prop
.intval
))
838 /* if automode is not enabled do not tell about reported_mode */
839 if (bq
->automode
< 1)
842 schedule_delayed_work(&bq
->work
, 0);
847 /**** timer functions ****/
849 /* enable/disable auto resetting chip timer */
850 static void bq2415x_set_autotimer(struct bq2415x_device
*bq
, int state
)
852 mutex_lock(&bq2415x_timer_mutex
);
854 if (bq
->autotimer
== state
) {
855 mutex_unlock(&bq2415x_timer_mutex
);
859 bq
->autotimer
= state
;
862 schedule_delayed_work(&bq
->work
, BQ2415X_TIMER_TIMEOUT
* HZ
);
863 bq2415x_exec_command(bq
, BQ2415X_TIMER_RESET
);
864 bq
->timer_error
= NULL
;
866 cancel_delayed_work_sync(&bq
->work
);
869 mutex_unlock(&bq2415x_timer_mutex
);
872 /* called by bq2415x_timer_work on timer error */
873 static void bq2415x_timer_error(struct bq2415x_device
*bq
, const char *msg
)
875 bq
->timer_error
= msg
;
876 sysfs_notify(&bq
->charger
->dev
.kobj
, NULL
, "timer");
877 dev_err(bq
->dev
, "%s\n", msg
);
878 if (bq
->automode
> 0)
880 bq2415x_set_mode(bq
, BQ2415X_MODE_OFF
);
881 bq2415x_set_autotimer(bq
, 0);
884 /* delayed work function for auto resetting chip timer */
885 static void bq2415x_timer_work(struct work_struct
*work
)
887 struct bq2415x_device
*bq
= container_of(work
, struct bq2415x_device
,
893 if (bq
->automode
> 0 && (bq
->reported_mode
!= bq
->mode
)) {
894 sysfs_notify(&bq
->charger
->dev
.kobj
, NULL
, "reported_mode");
895 bq2415x_set_mode(bq
, bq
->reported_mode
);
901 ret
= bq2415x_exec_command(bq
, BQ2415X_TIMER_RESET
);
903 bq2415x_timer_error(bq
, "Resetting timer failed");
907 boost
= bq2415x_exec_command(bq
, BQ2415X_BOOST_MODE_STATUS
);
909 bq2415x_timer_error(bq
, "Unknown error");
913 error
= bq2415x_exec_command(bq
, BQ2415X_FAULT_STATUS
);
915 bq2415x_timer_error(bq
, "Unknown error");
921 /* Non fatal errors, chip is OK */
922 case 0: /* No error */
924 case 6: /* Timer expired */
925 dev_err(bq
->dev
, "Timer expired\n");
927 case 3: /* Battery voltage too low */
928 dev_err(bq
->dev
, "Battery voltage to low\n");
931 /* Fatal errors, disable and reset chip */
932 case 1: /* Overvoltage protection (chip fried) */
933 bq2415x_timer_error(bq
,
934 "Overvoltage protection (chip fried)");
936 case 2: /* Overload */
937 bq2415x_timer_error(bq
, "Overload");
939 case 4: /* Battery overvoltage protection */
940 bq2415x_timer_error(bq
,
941 "Battery overvoltage protection");
943 case 5: /* Thermal shutdown (too hot) */
944 bq2415x_timer_error(bq
,
945 "Thermal shutdown (too hot)");
948 bq2415x_timer_error(bq
, "Unknown error");
953 /* Non fatal errors, chip is OK */
954 case 0: /* No error */
956 case 2: /* Sleep mode */
957 dev_err(bq
->dev
, "Sleep mode\n");
959 case 3: /* Poor input source */
960 dev_err(bq
->dev
, "Poor input source\n");
962 case 6: /* Timer expired */
963 dev_err(bq
->dev
, "Timer expired\n");
965 case 7: /* No battery */
966 dev_err(bq
->dev
, "No battery\n");
969 /* Fatal errors, disable and reset chip */
970 case 1: /* Overvoltage protection (chip fried) */
971 bq2415x_timer_error(bq
,
972 "Overvoltage protection (chip fried)");
974 case 4: /* Battery overvoltage protection */
975 bq2415x_timer_error(bq
,
976 "Battery overvoltage protection");
978 case 5: /* Thermal shutdown (too hot) */
979 bq2415x_timer_error(bq
,
980 "Thermal shutdown (too hot)");
985 schedule_delayed_work(&bq
->work
, BQ2415X_TIMER_TIMEOUT
* HZ
);
988 /**** power supply interface code ****/
990 static enum power_supply_property bq2415x_power_supply_props
[] = {
991 /* TODO: maybe add more power supply properties */
992 POWER_SUPPLY_PROP_STATUS
,
993 POWER_SUPPLY_PROP_MODEL_NAME
,
996 static int bq2415x_power_supply_get_property(struct power_supply
*psy
,
997 enum power_supply_property psp
,
998 union power_supply_propval
*val
)
1000 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1004 case POWER_SUPPLY_PROP_STATUS
:
1005 ret
= bq2415x_exec_command(bq
, BQ2415X_CHARGE_STATUS
);
1008 else if (ret
== 0) /* Ready */
1009 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1010 else if (ret
== 1) /* Charge in progress */
1011 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
1012 else if (ret
== 2) /* Charge done */
1013 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
1015 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
1017 case POWER_SUPPLY_PROP_MODEL_NAME
:
1018 val
->strval
= bq
->model
;
1026 static void bq2415x_power_supply_exit(struct bq2415x_device
*bq
)
1029 if (bq
->automode
> 0)
1031 cancel_delayed_work_sync(&bq
->work
);
1032 power_supply_unregister(bq
->charger
);
1036 /**** additional sysfs entries for power supply interface ****/
1038 /* show *_status entries */
1039 static ssize_t
bq2415x_sysfs_show_status(struct device
*dev
,
1040 struct device_attribute
*attr
,
1043 struct power_supply
*psy
= dev_get_drvdata(dev
);
1044 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1045 enum bq2415x_command command
;
1048 if (strcmp(attr
->attr
.name
, "otg_status") == 0)
1049 command
= BQ2415X_OTG_STATUS
;
1050 else if (strcmp(attr
->attr
.name
, "charge_status") == 0)
1051 command
= BQ2415X_CHARGE_STATUS
;
1052 else if (strcmp(attr
->attr
.name
, "boost_status") == 0)
1053 command
= BQ2415X_BOOST_STATUS
;
1054 else if (strcmp(attr
->attr
.name
, "fault_status") == 0)
1055 command
= BQ2415X_FAULT_STATUS
;
1059 ret
= bq2415x_exec_command(bq
, command
);
1062 return sprintf(buf
, "%d\n", ret
);
1067 * auto - enable auto mode
1068 * off - disable auto mode
1069 * (other values) - reset chip timer
1071 static ssize_t
bq2415x_sysfs_set_timer(struct device
*dev
,
1072 struct device_attribute
*attr
,
1076 struct power_supply
*psy
= dev_get_drvdata(dev
);
1077 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1080 if (strncmp(buf
, "auto", 4) == 0)
1081 bq2415x_set_autotimer(bq
, 1);
1082 else if (strncmp(buf
, "off", 3) == 0)
1083 bq2415x_set_autotimer(bq
, 0);
1085 ret
= bq2415x_exec_command(bq
, BQ2415X_TIMER_RESET
);
1092 /* show timer entry (auto or off) */
1093 static ssize_t
bq2415x_sysfs_show_timer(struct device
*dev
,
1094 struct device_attribute
*attr
,
1097 struct power_supply
*psy
= dev_get_drvdata(dev
);
1098 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1100 if (bq
->timer_error
)
1101 return sprintf(buf
, "%s\n", bq
->timer_error
);
1104 return sprintf(buf
, "auto\n");
1105 return sprintf(buf
, "off\n");
1110 * auto - if automode is supported, enable it and set mode to reported
1111 * none - disable charger and boost mode
1112 * host - charging mode for host/hub chargers (current limit 500mA)
1113 * dedicated - charging mode for dedicated chargers (unlimited current limit)
1114 * boost - disable charger and enable boost mode
1116 static ssize_t
bq2415x_sysfs_set_mode(struct device
*dev
,
1117 struct device_attribute
*attr
,
1121 struct power_supply
*psy
= dev_get_drvdata(dev
);
1122 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1123 enum bq2415x_mode mode
;
1126 if (strncmp(buf
, "auto", 4) == 0) {
1127 if (bq
->automode
< 0)
1130 mode
= bq
->reported_mode
;
1131 } else if (strncmp(buf
, "off", 3) == 0) {
1132 if (bq
->automode
> 0)
1134 mode
= BQ2415X_MODE_OFF
;
1135 } else if (strncmp(buf
, "none", 4) == 0) {
1136 if (bq
->automode
> 0)
1138 mode
= BQ2415X_MODE_NONE
;
1139 } else if (strncmp(buf
, "host", 4) == 0) {
1140 if (bq
->automode
> 0)
1142 mode
= BQ2415X_MODE_HOST_CHARGER
;
1143 } else if (strncmp(buf
, "dedicated", 9) == 0) {
1144 if (bq
->automode
> 0)
1146 mode
= BQ2415X_MODE_DEDICATED_CHARGER
;
1147 } else if (strncmp(buf
, "boost", 5) == 0) {
1148 if (bq
->automode
> 0)
1150 mode
= BQ2415X_MODE_BOOST
;
1151 } else if (strncmp(buf
, "reset", 5) == 0) {
1152 bq2415x_reset_chip(bq
);
1153 bq2415x_set_defaults(bq
);
1154 if (bq
->automode
<= 0)
1157 mode
= bq
->reported_mode
;
1162 ret
= bq2415x_set_mode(bq
, mode
);
1168 /* show mode entry (auto, none, host, dedicated or boost) */
1169 static ssize_t
bq2415x_sysfs_show_mode(struct device
*dev
,
1170 struct device_attribute
*attr
,
1173 struct power_supply
*psy
= dev_get_drvdata(dev
);
1174 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1177 if (bq
->automode
> 0)
1178 ret
+= sprintf(buf
+ret
, "auto (");
1181 case BQ2415X_MODE_OFF
:
1182 ret
+= sprintf(buf
+ret
, "off");
1184 case BQ2415X_MODE_NONE
:
1185 ret
+= sprintf(buf
+ret
, "none");
1187 case BQ2415X_MODE_HOST_CHARGER
:
1188 ret
+= sprintf(buf
+ret
, "host");
1190 case BQ2415X_MODE_DEDICATED_CHARGER
:
1191 ret
+= sprintf(buf
+ret
, "dedicated");
1193 case BQ2415X_MODE_BOOST
:
1194 ret
+= sprintf(buf
+ret
, "boost");
1198 if (bq
->automode
> 0)
1199 ret
+= sprintf(buf
+ret
, ")");
1201 ret
+= sprintf(buf
+ret
, "\n");
1205 /* show reported_mode entry (none, host, dedicated or boost) */
1206 static ssize_t
bq2415x_sysfs_show_reported_mode(struct device
*dev
,
1207 struct device_attribute
*attr
,
1210 struct power_supply
*psy
= dev_get_drvdata(dev
);
1211 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1213 if (bq
->automode
< 0)
1216 switch (bq
->reported_mode
) {
1217 case BQ2415X_MODE_OFF
:
1218 return sprintf(buf
, "off\n");
1219 case BQ2415X_MODE_NONE
:
1220 return sprintf(buf
, "none\n");
1221 case BQ2415X_MODE_HOST_CHARGER
:
1222 return sprintf(buf
, "host\n");
1223 case BQ2415X_MODE_DEDICATED_CHARGER
:
1224 return sprintf(buf
, "dedicated\n");
1225 case BQ2415X_MODE_BOOST
:
1226 return sprintf(buf
, "boost\n");
1232 /* directly set raw value to chip register, format: 'register value' */
1233 static ssize_t
bq2415x_sysfs_set_registers(struct device
*dev
,
1234 struct device_attribute
*attr
,
1238 struct power_supply
*psy
= dev_get_drvdata(dev
);
1239 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1244 if (sscanf(buf
, "%x %x", ®
, &val
) != 2)
1247 if (reg
> 4 || val
> 255)
1250 ret
= bq2415x_i2c_write(bq
, reg
, val
);
1256 /* print value of chip register, format: 'register=value' */
1257 static ssize_t
bq2415x_sysfs_print_reg(struct bq2415x_device
*bq
,
1261 int ret
= bq2415x_i2c_read(bq
, reg
);
1264 return sprintf(buf
, "%#.2x=error %d\n", reg
, ret
);
1265 return sprintf(buf
, "%#.2x=%#.2x\n", reg
, ret
);
1268 /* show all raw values of chip register, format per line: 'register=value' */
1269 static ssize_t
bq2415x_sysfs_show_registers(struct device
*dev
,
1270 struct device_attribute
*attr
,
1273 struct power_supply
*psy
= dev_get_drvdata(dev
);
1274 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1277 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_STATUS
, buf
+ret
);
1278 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_CONTROL
, buf
+ret
);
1279 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_VOLTAGE
, buf
+ret
);
1280 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_VENDER
, buf
+ret
);
1281 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_CURRENT
, buf
+ret
);
1285 /* set current and voltage limit entries (in mA or mV) */
1286 static ssize_t
bq2415x_sysfs_set_limit(struct device
*dev
,
1287 struct device_attribute
*attr
,
1291 struct power_supply
*psy
= dev_get_drvdata(dev
);
1292 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1296 if (kstrtol(buf
, 10, &val
) < 0)
1299 if (strcmp(attr
->attr
.name
, "current_limit") == 0)
1300 ret
= bq2415x_set_current_limit(bq
, val
);
1301 else if (strcmp(attr
->attr
.name
, "weak_battery_voltage") == 0)
1302 ret
= bq2415x_set_weak_battery_voltage(bq
, val
);
1303 else if (strcmp(attr
->attr
.name
, "battery_regulation_voltage") == 0)
1304 ret
= bq2415x_set_battery_regulation_voltage(bq
, val
);
1305 else if (strcmp(attr
->attr
.name
, "charge_current") == 0)
1306 ret
= bq2415x_set_charge_current(bq
, val
);
1307 else if (strcmp(attr
->attr
.name
, "termination_current") == 0)
1308 ret
= bq2415x_set_termination_current(bq
, val
);
1317 /* show current and voltage limit entries (in mA or mV) */
1318 static ssize_t
bq2415x_sysfs_show_limit(struct device
*dev
,
1319 struct device_attribute
*attr
,
1322 struct power_supply
*psy
= dev_get_drvdata(dev
);
1323 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1326 if (strcmp(attr
->attr
.name
, "current_limit") == 0)
1327 ret
= bq2415x_get_current_limit(bq
);
1328 else if (strcmp(attr
->attr
.name
, "weak_battery_voltage") == 0)
1329 ret
= bq2415x_get_weak_battery_voltage(bq
);
1330 else if (strcmp(attr
->attr
.name
, "battery_regulation_voltage") == 0)
1331 ret
= bq2415x_get_battery_regulation_voltage(bq
);
1332 else if (strcmp(attr
->attr
.name
, "charge_current") == 0)
1333 ret
= bq2415x_get_charge_current(bq
);
1334 else if (strcmp(attr
->attr
.name
, "termination_current") == 0)
1335 ret
= bq2415x_get_termination_current(bq
);
1341 return sprintf(buf
, "%d\n", ret
);
1344 /* set *_enable entries */
1345 static ssize_t
bq2415x_sysfs_set_enable(struct device
*dev
,
1346 struct device_attribute
*attr
,
1350 struct power_supply
*psy
= dev_get_drvdata(dev
);
1351 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1352 enum bq2415x_command command
;
1356 if (kstrtol(buf
, 10, &val
) < 0)
1359 if (strcmp(attr
->attr
.name
, "charge_termination_enable") == 0)
1360 command
= val
? BQ2415X_CHARGE_TERMINATION_ENABLE
:
1361 BQ2415X_CHARGE_TERMINATION_DISABLE
;
1362 else if (strcmp(attr
->attr
.name
, "high_impedance_enable") == 0)
1363 command
= val
? BQ2415X_HIGH_IMPEDANCE_ENABLE
:
1364 BQ2415X_HIGH_IMPEDANCE_DISABLE
;
1365 else if (strcmp(attr
->attr
.name
, "otg_pin_enable") == 0)
1366 command
= val
? BQ2415X_OTG_PIN_ENABLE
:
1367 BQ2415X_OTG_PIN_DISABLE
;
1368 else if (strcmp(attr
->attr
.name
, "stat_pin_enable") == 0)
1369 command
= val
? BQ2415X_STAT_PIN_ENABLE
:
1370 BQ2415X_STAT_PIN_DISABLE
;
1374 ret
= bq2415x_exec_command(bq
, command
);
1380 /* show *_enable entries */
1381 static ssize_t
bq2415x_sysfs_show_enable(struct device
*dev
,
1382 struct device_attribute
*attr
,
1385 struct power_supply
*psy
= dev_get_drvdata(dev
);
1386 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1387 enum bq2415x_command command
;
1390 if (strcmp(attr
->attr
.name
, "charge_termination_enable") == 0)
1391 command
= BQ2415X_CHARGE_TERMINATION_STATUS
;
1392 else if (strcmp(attr
->attr
.name
, "high_impedance_enable") == 0)
1393 command
= BQ2415X_HIGH_IMPEDANCE_STATUS
;
1394 else if (strcmp(attr
->attr
.name
, "otg_pin_enable") == 0)
1395 command
= BQ2415X_OTG_PIN_STATUS
;
1396 else if (strcmp(attr
->attr
.name
, "stat_pin_enable") == 0)
1397 command
= BQ2415X_STAT_PIN_STATUS
;
1401 ret
= bq2415x_exec_command(bq
, command
);
1404 return sprintf(buf
, "%d\n", ret
);
1407 static DEVICE_ATTR(current_limit
, S_IWUSR
| S_IRUGO
,
1408 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1409 static DEVICE_ATTR(weak_battery_voltage
, S_IWUSR
| S_IRUGO
,
1410 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1411 static DEVICE_ATTR(battery_regulation_voltage
, S_IWUSR
| S_IRUGO
,
1412 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1413 static DEVICE_ATTR(charge_current
, S_IWUSR
| S_IRUGO
,
1414 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1415 static DEVICE_ATTR(termination_current
, S_IWUSR
| S_IRUGO
,
1416 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1418 static DEVICE_ATTR(charge_termination_enable
, S_IWUSR
| S_IRUGO
,
1419 bq2415x_sysfs_show_enable
, bq2415x_sysfs_set_enable
);
1420 static DEVICE_ATTR(high_impedance_enable
, S_IWUSR
| S_IRUGO
,
1421 bq2415x_sysfs_show_enable
, bq2415x_sysfs_set_enable
);
1422 static DEVICE_ATTR(otg_pin_enable
, S_IWUSR
| S_IRUGO
,
1423 bq2415x_sysfs_show_enable
, bq2415x_sysfs_set_enable
);
1424 static DEVICE_ATTR(stat_pin_enable
, S_IWUSR
| S_IRUGO
,
1425 bq2415x_sysfs_show_enable
, bq2415x_sysfs_set_enable
);
1427 static DEVICE_ATTR(reported_mode
, S_IRUGO
,
1428 bq2415x_sysfs_show_reported_mode
, NULL
);
1429 static DEVICE_ATTR(mode
, S_IWUSR
| S_IRUGO
,
1430 bq2415x_sysfs_show_mode
, bq2415x_sysfs_set_mode
);
1431 static DEVICE_ATTR(timer
, S_IWUSR
| S_IRUGO
,
1432 bq2415x_sysfs_show_timer
, bq2415x_sysfs_set_timer
);
1434 static DEVICE_ATTR(registers
, S_IWUSR
| S_IRUGO
,
1435 bq2415x_sysfs_show_registers
, bq2415x_sysfs_set_registers
);
1437 static DEVICE_ATTR(otg_status
, S_IRUGO
, bq2415x_sysfs_show_status
, NULL
);
1438 static DEVICE_ATTR(charge_status
, S_IRUGO
, bq2415x_sysfs_show_status
, NULL
);
1439 static DEVICE_ATTR(boost_status
, S_IRUGO
, bq2415x_sysfs_show_status
, NULL
);
1440 static DEVICE_ATTR(fault_status
, S_IRUGO
, bq2415x_sysfs_show_status
, NULL
);
1442 static struct attribute
*bq2415x_sysfs_attrs
[] = {
1444 * TODO: some (appropriate) of these attrs should be switched to
1445 * use power supply class props.
1447 &dev_attr_current_limit
.attr
,
1448 &dev_attr_weak_battery_voltage
.attr
,
1449 &dev_attr_battery_regulation_voltage
.attr
,
1450 &dev_attr_charge_current
.attr
,
1451 &dev_attr_termination_current
.attr
,
1453 &dev_attr_charge_termination_enable
.attr
,
1454 &dev_attr_high_impedance_enable
.attr
,
1455 &dev_attr_otg_pin_enable
.attr
,
1456 &dev_attr_stat_pin_enable
.attr
,
1458 &dev_attr_reported_mode
.attr
,
1459 &dev_attr_mode
.attr
,
1460 &dev_attr_timer
.attr
,
1462 &dev_attr_registers
.attr
,
1464 &dev_attr_otg_status
.attr
,
1465 &dev_attr_charge_status
.attr
,
1466 &dev_attr_boost_status
.attr
,
1467 &dev_attr_fault_status
.attr
,
1471 ATTRIBUTE_GROUPS(bq2415x_sysfs
);
1473 static int bq2415x_power_supply_init(struct bq2415x_device
*bq
)
1478 struct power_supply_config psy_cfg
= {
1480 .of_node
= bq
->dev
->of_node
,
1481 .attr_grp
= bq2415x_sysfs_groups
,
1484 bq
->charger_desc
.name
= bq
->name
;
1485 bq
->charger_desc
.type
= POWER_SUPPLY_TYPE_USB
;
1486 bq
->charger_desc
.properties
= bq2415x_power_supply_props
;
1487 bq
->charger_desc
.num_properties
=
1488 ARRAY_SIZE(bq2415x_power_supply_props
);
1489 bq
->charger_desc
.get_property
= bq2415x_power_supply_get_property
;
1491 ret
= bq2415x_detect_chip(bq
);
1497 ret
= bq2415x_detect_revision(bq
);
1499 strcpy(revstr
, "unknown");
1501 sprintf(revstr
, "1.%d", ret
);
1503 bq
->model
= kasprintf(GFP_KERNEL
,
1504 "chip %s, revision %s, vender code %.3d",
1505 bq2415x_chip_name
[chip
], revstr
,
1506 bq2415x_get_vender_code(bq
));
1508 dev_err(bq
->dev
, "failed to allocate model name\n");
1512 bq
->charger
= power_supply_register(bq
->dev
, &bq
->charger_desc
,
1514 if (IS_ERR(bq
->charger
)) {
1516 return PTR_ERR(bq
->charger
);
1522 /* main bq2415x probe function */
1523 static int bq2415x_probe(struct i2c_client
*client
,
1524 const struct i2c_device_id
*id
)
1529 struct bq2415x_device
*bq
;
1530 struct device_node
*np
= client
->dev
.of_node
;
1531 struct bq2415x_platform_data
*pdata
= client
->dev
.platform_data
;
1532 const struct acpi_device_id
*acpi_id
= NULL
;
1533 struct power_supply
*notify_psy
= NULL
;
1534 union power_supply_propval prop
;
1536 if (!np
&& !pdata
&& !ACPI_HANDLE(&client
->dev
)) {
1537 dev_err(&client
->dev
, "Neither devicetree, nor platform data, nor ACPI support\n");
1541 /* Get new ID for the new device */
1542 mutex_lock(&bq2415x_id_mutex
);
1543 num
= idr_alloc(&bq2415x_id
, client
, 0, 0, GFP_KERNEL
);
1544 mutex_unlock(&bq2415x_id_mutex
);
1549 name
= kasprintf(GFP_KERNEL
, "%s-%d", id
->name
, num
);
1550 } else if (ACPI_HANDLE(&client
->dev
)) {
1552 acpi_match_device(client
->dev
.driver
->acpi_match_table
,
1555 dev_err(&client
->dev
, "failed to match device name\n");
1559 name
= kasprintf(GFP_KERNEL
, "%s-%d", acpi_id
->id
, num
);
1562 dev_err(&client
->dev
, "failed to allocate device name\n");
1567 bq
= devm_kzalloc(&client
->dev
, sizeof(*bq
), GFP_KERNEL
);
1573 i2c_set_clientdata(client
, bq
);
1576 bq
->dev
= &client
->dev
;
1578 bq
->chip
= id
->driver_data
;
1579 else if (ACPI_HANDLE(bq
->dev
))
1580 bq
->chip
= acpi_id
->driver_data
;
1582 bq
->mode
= BQ2415X_MODE_OFF
;
1583 bq
->reported_mode
= BQ2415X_MODE_OFF
;
1587 if (np
|| ACPI_HANDLE(bq
->dev
)) {
1588 ret
= device_property_read_u32(bq
->dev
,
1590 &bq
->init_data
.current_limit
);
1593 ret
= device_property_read_u32(bq
->dev
,
1594 "ti,weak-battery-voltage",
1595 &bq
->init_data
.weak_battery_voltage
);
1598 ret
= device_property_read_u32(bq
->dev
,
1599 "ti,battery-regulation-voltage",
1600 &bq
->init_data
.battery_regulation_voltage
);
1603 ret
= device_property_read_u32(bq
->dev
,
1604 "ti,charge-current",
1605 &bq
->init_data
.charge_current
);
1608 ret
= device_property_read_u32(bq
->dev
,
1609 "ti,termination-current",
1610 &bq
->init_data
.termination_current
);
1613 ret
= device_property_read_u32(bq
->dev
,
1614 "ti,resistor-sense",
1615 &bq
->init_data
.resistor_sense
);
1619 bq
->notify_node
= of_parse_phandle(np
,
1620 "ti,usb-charger-detection", 0);
1622 memcpy(&bq
->init_data
, pdata
, sizeof(bq
->init_data
));
1625 bq2415x_reset_chip(bq
);
1627 ret
= bq2415x_power_supply_init(bq
);
1629 dev_err(bq
->dev
, "failed to register power supply: %d\n", ret
);
1633 ret
= bq2415x_set_defaults(bq
);
1635 dev_err(bq
->dev
, "failed to set default values: %d\n", ret
);
1639 if (bq
->notify_node
|| bq
->init_data
.notify_device
) {
1640 bq
->nb
.notifier_call
= bq2415x_notifier_call
;
1641 ret
= power_supply_reg_notifier(&bq
->nb
);
1643 dev_err(bq
->dev
, "failed to reg notifier: %d\n", ret
);
1648 dev_info(bq
->dev
, "automode supported, waiting for events\n");
1651 dev_info(bq
->dev
, "automode not supported\n");
1654 /* Query for initial reported_mode and set it */
1655 if (bq
->nb
.notifier_call
) {
1657 notify_psy
= power_supply_get_by_phandle(np
,
1658 "ti,usb-charger-detection");
1659 if (IS_ERR(notify_psy
))
1661 } else if (bq
->init_data
.notify_device
) {
1662 notify_psy
= power_supply_get_by_name(
1663 bq
->init_data
.notify_device
);
1667 ret
= power_supply_get_property(notify_psy
,
1668 POWER_SUPPLY_PROP_CURRENT_MAX
, &prop
);
1669 power_supply_put(notify_psy
);
1672 bq2415x_update_reported_mode(bq
, prop
.intval
);
1673 bq2415x_set_mode(bq
, bq
->reported_mode
);
1677 INIT_DELAYED_WORK(&bq
->work
, bq2415x_timer_work
);
1678 bq2415x_set_autotimer(bq
, 1);
1680 dev_info(bq
->dev
, "driver registered\n");
1684 bq2415x_power_supply_exit(bq
);
1687 of_node_put(bq
->notify_node
);
1690 mutex_lock(&bq2415x_id_mutex
);
1691 idr_remove(&bq2415x_id
, num
);
1692 mutex_unlock(&bq2415x_id_mutex
);
1697 /* main bq2415x remove function */
1699 static int bq2415x_remove(struct i2c_client
*client
)
1701 struct bq2415x_device
*bq
= i2c_get_clientdata(client
);
1703 if (bq
->nb
.notifier_call
)
1704 power_supply_unreg_notifier(&bq
->nb
);
1706 of_node_put(bq
->notify_node
);
1707 bq2415x_power_supply_exit(bq
);
1709 bq2415x_reset_chip(bq
);
1711 mutex_lock(&bq2415x_id_mutex
);
1712 idr_remove(&bq2415x_id
, bq
->id
);
1713 mutex_unlock(&bq2415x_id_mutex
);
1715 dev_info(bq
->dev
, "driver unregistered\n");
1722 static const struct i2c_device_id bq2415x_i2c_id_table
[] = {
1723 { "bq2415x", BQUNKNOWN
},
1724 { "bq24150", BQ24150
},
1725 { "bq24150a", BQ24150A
},
1726 { "bq24151", BQ24151
},
1727 { "bq24151a", BQ24151A
},
1728 { "bq24152", BQ24152
},
1729 { "bq24153", BQ24153
},
1730 { "bq24153a", BQ24153A
},
1731 { "bq24155", BQ24155
},
1732 { "bq24156", BQ24156
},
1733 { "bq24156a", BQ24156A
},
1734 { "bq24157s", BQ24157S
},
1735 { "bq24158", BQ24158
},
1738 MODULE_DEVICE_TABLE(i2c
, bq2415x_i2c_id_table
);
1741 static const struct acpi_device_id bq2415x_i2c_acpi_match
[] = {
1742 { "BQ2415X", BQUNKNOWN
},
1743 { "BQ241500", BQ24150
},
1744 { "BQA24150", BQ24150A
},
1745 { "BQ241510", BQ24151
},
1746 { "BQA24151", BQ24151A
},
1747 { "BQ241520", BQ24152
},
1748 { "BQ241530", BQ24153
},
1749 { "BQA24153", BQ24153A
},
1750 { "BQ241550", BQ24155
},
1751 { "BQ241560", BQ24156
},
1752 { "BQA24156", BQ24156A
},
1753 { "BQS24157", BQ24157S
},
1754 { "BQ241580", BQ24158
},
1757 MODULE_DEVICE_TABLE(acpi
, bq2415x_i2c_acpi_match
);
1761 static const struct of_device_id bq2415x_of_match_table
[] = {
1762 { .compatible
= "ti,bq24150" },
1763 { .compatible
= "ti,bq24150a" },
1764 { .compatible
= "ti,bq24151" },
1765 { .compatible
= "ti,bq24151a" },
1766 { .compatible
= "ti,bq24152" },
1767 { .compatible
= "ti,bq24153" },
1768 { .compatible
= "ti,bq24153a" },
1769 { .compatible
= "ti,bq24155" },
1770 { .compatible
= "ti,bq24156" },
1771 { .compatible
= "ti,bq24156a" },
1772 { .compatible
= "ti,bq24157s" },
1773 { .compatible
= "ti,bq24158" },
1776 MODULE_DEVICE_TABLE(of
, bq2415x_of_match_table
);
1779 static struct i2c_driver bq2415x_driver
= {
1781 .name
= "bq2415x-charger",
1782 .of_match_table
= of_match_ptr(bq2415x_of_match_table
),
1783 .acpi_match_table
= ACPI_PTR(bq2415x_i2c_acpi_match
),
1785 .probe
= bq2415x_probe
,
1786 .remove
= bq2415x_remove
,
1787 .id_table
= bq2415x_i2c_id_table
,
1789 module_i2c_driver(bq2415x_driver
);
1791 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
1792 MODULE_DESCRIPTION("bq2415x charger driver");
1793 MODULE_LICENSE("GPL");