2 * bq2415x charger driver
4 * Copyright (C) 2011-2013 Pali Rohár <pali.rohar@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 * http://www.ti.com/product/bq24150
18 * http://www.ti.com/product/bq24150a
19 * http://www.ti.com/product/bq24152
20 * http://www.ti.com/product/bq24153
21 * http://www.ti.com/product/bq24153a
22 * http://www.ti.com/product/bq24155
23 * http://www.ti.com/product/bq24157s
24 * http://www.ti.com/product/bq24158
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/param.h>
30 #include <linux/err.h>
31 #include <linux/workqueue.h>
32 #include <linux/sysfs.h>
33 #include <linux/platform_device.h>
34 #include <linux/power_supply.h>
35 #include <linux/idr.h>
36 #include <linux/i2c.h>
37 #include <linux/slab.h>
39 #include <linux/power/bq2415x_charger.h>
41 /* timeout for resetting chip timer */
42 #define BQ2415X_TIMER_TIMEOUT 10
44 #define BQ2415X_REG_STATUS 0x00
45 #define BQ2415X_REG_CONTROL 0x01
46 #define BQ2415X_REG_VOLTAGE 0x02
47 #define BQ2415X_REG_VENDER 0x03
48 #define BQ2415X_REG_CURRENT 0x04
50 /* reset state for all registers */
51 #define BQ2415X_RESET_STATUS BIT(6)
52 #define BQ2415X_RESET_CONTROL (BIT(4)|BIT(5))
53 #define BQ2415X_RESET_VOLTAGE (BIT(1)|BIT(3))
54 #define BQ2415X_RESET_CURRENT (BIT(0)|BIT(3)|BIT(7))
57 #define BQ2415X_BIT_TMR_RST 7
58 #define BQ2415X_BIT_OTG 7
59 #define BQ2415X_BIT_EN_STAT 6
60 #define BQ2415X_MASK_STAT (BIT(4)|BIT(5))
61 #define BQ2415X_SHIFT_STAT 4
62 #define BQ2415X_BIT_BOOST 3
63 #define BQ2415X_MASK_FAULT (BIT(0)|BIT(1)|BIT(2))
64 #define BQ2415X_SHIFT_FAULT 0
66 /* control register */
67 #define BQ2415X_MASK_LIMIT (BIT(6)|BIT(7))
68 #define BQ2415X_SHIFT_LIMIT 6
69 #define BQ2415X_MASK_VLOWV (BIT(4)|BIT(5))
70 #define BQ2415X_SHIFT_VLOWV 4
71 #define BQ2415X_BIT_TE 3
72 #define BQ2415X_BIT_CE 2
73 #define BQ2415X_BIT_HZ_MODE 1
74 #define BQ2415X_BIT_OPA_MODE 0
76 /* voltage register */
77 #define BQ2415X_MASK_VO (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
78 #define BQ2415X_SHIFT_VO 2
79 #define BQ2415X_BIT_OTG_PL 1
80 #define BQ2415X_BIT_OTG_EN 0
83 #define BQ2415X_MASK_VENDER (BIT(5)|BIT(6)|BIT(7))
84 #define BQ2415X_SHIFT_VENDER 5
85 #define BQ2415X_MASK_PN (BIT(3)|BIT(4))
86 #define BQ2415X_SHIFT_PN 3
87 #define BQ2415X_MASK_REVISION (BIT(0)|BIT(1)|BIT(2))
88 #define BQ2415X_SHIFT_REVISION 0
90 /* current register */
91 #define BQ2415X_MASK_RESET BIT(7)
92 #define BQ2415X_MASK_VI_CHRG (BIT(4)|BIT(5)|BIT(6))
93 #define BQ2415X_SHIFT_VI_CHRG 4
95 #define BQ2415X_MASK_VI_TERM (BIT(0)|BIT(1)|BIT(2))
96 #define BQ2415X_SHIFT_VI_TERM 0
99 enum bq2415x_command
{
102 BQ2415X_STAT_PIN_STATUS
,
103 BQ2415X_STAT_PIN_ENABLE
,
104 BQ2415X_STAT_PIN_DISABLE
,
105 BQ2415X_CHARGE_STATUS
,
106 BQ2415X_BOOST_STATUS
,
107 BQ2415X_FAULT_STATUS
,
109 BQ2415X_CHARGE_TERMINATION_STATUS
,
110 BQ2415X_CHARGE_TERMINATION_ENABLE
,
111 BQ2415X_CHARGE_TERMINATION_DISABLE
,
112 BQ2415X_CHARGER_STATUS
,
113 BQ2415X_CHARGER_ENABLE
,
114 BQ2415X_CHARGER_DISABLE
,
115 BQ2415X_HIGH_IMPEDANCE_STATUS
,
116 BQ2415X_HIGH_IMPEDANCE_ENABLE
,
117 BQ2415X_HIGH_IMPEDANCE_DISABLE
,
118 BQ2415X_BOOST_MODE_STATUS
,
119 BQ2415X_BOOST_MODE_ENABLE
,
120 BQ2415X_BOOST_MODE_DISABLE
,
123 BQ2415X_OTG_ACTIVATE_HIGH
,
124 BQ2415X_OTG_ACTIVATE_LOW
,
125 BQ2415X_OTG_PIN_STATUS
,
126 BQ2415X_OTG_PIN_ENABLE
,
127 BQ2415X_OTG_PIN_DISABLE
,
150 static char *bq2415x_chip_name
[] = {
166 struct bq2415x_device
{
168 struct bq2415x_platform_data init_data
;
169 struct power_supply
*charger
;
170 struct power_supply_desc charger_desc
;
171 struct delayed_work work
;
172 struct power_supply
*notify_psy
;
173 struct notifier_block nb
;
174 enum bq2415x_mode reported_mode
;/* mode reported by hook function */
175 enum bq2415x_mode mode
; /* currently configured mode */
176 enum bq2415x_chip chip
;
177 const char *timer_error
;
180 int autotimer
; /* 1 - if driver automatically reset timer, 0 - not */
181 int automode
; /* 1 - enabled, 0 - disabled; -1 - not supported */
185 /* each registered chip must have unique id */
186 static DEFINE_IDR(bq2415x_id
);
188 static DEFINE_MUTEX(bq2415x_id_mutex
);
189 static DEFINE_MUTEX(bq2415x_timer_mutex
);
190 static DEFINE_MUTEX(bq2415x_i2c_mutex
);
192 /**** i2c read functions ****/
194 /* read value from register */
195 static int bq2415x_i2c_read(struct bq2415x_device
*bq
, u8 reg
)
197 struct i2c_client
*client
= to_i2c_client(bq
->dev
);
198 struct i2c_msg msg
[2];
202 if (!client
->adapter
)
205 msg
[0].addr
= client
->addr
;
208 msg
[0].len
= sizeof(reg
);
209 msg
[1].addr
= client
->addr
;
210 msg
[1].flags
= I2C_M_RD
;
212 msg
[1].len
= sizeof(val
);
214 mutex_lock(&bq2415x_i2c_mutex
);
215 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
216 mutex_unlock(&bq2415x_i2c_mutex
);
224 /* read value from register, apply mask and right shift it */
225 static int bq2415x_i2c_read_mask(struct bq2415x_device
*bq
, u8 reg
,
233 ret
= bq2415x_i2c_read(bq
, reg
);
236 return (ret
& mask
) >> shift
;
239 /* read value from register and return one specified bit */
240 static int bq2415x_i2c_read_bit(struct bq2415x_device
*bq
, u8 reg
, u8 bit
)
244 return bq2415x_i2c_read_mask(bq
, reg
, BIT(bit
), bit
);
247 /**** i2c write functions ****/
249 /* write value to register */
250 static int bq2415x_i2c_write(struct bq2415x_device
*bq
, u8 reg
, u8 val
)
252 struct i2c_client
*client
= to_i2c_client(bq
->dev
);
253 struct i2c_msg msg
[1];
260 msg
[0].addr
= client
->addr
;
263 msg
[0].len
= ARRAY_SIZE(data
);
265 mutex_lock(&bq2415x_i2c_mutex
);
266 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
267 mutex_unlock(&bq2415x_i2c_mutex
);
269 /* i2c_transfer returns number of messages transferred */
278 /* read value from register, change it with mask left shifted and write back */
279 static int bq2415x_i2c_write_mask(struct bq2415x_device
*bq
, u8 reg
, u8 val
,
287 ret
= bq2415x_i2c_read(bq
, reg
);
294 return bq2415x_i2c_write(bq
, reg
, ret
);
297 /* change only one bit in register */
298 static int bq2415x_i2c_write_bit(struct bq2415x_device
*bq
, u8 reg
,
303 return bq2415x_i2c_write_mask(bq
, reg
, val
, BIT(bit
), bit
);
306 /**** global functions ****/
308 /* exec command function */
309 static int bq2415x_exec_command(struct bq2415x_device
*bq
,
310 enum bq2415x_command command
)
315 case BQ2415X_TIMER_RESET
:
316 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_STATUS
,
317 1, BQ2415X_BIT_TMR_RST
);
318 case BQ2415X_OTG_STATUS
:
319 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_STATUS
,
321 case BQ2415X_STAT_PIN_STATUS
:
322 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_STATUS
,
323 BQ2415X_BIT_EN_STAT
);
324 case BQ2415X_STAT_PIN_ENABLE
:
325 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_STATUS
, 1,
326 BQ2415X_BIT_EN_STAT
);
327 case BQ2415X_STAT_PIN_DISABLE
:
328 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_STATUS
, 0,
329 BQ2415X_BIT_EN_STAT
);
330 case BQ2415X_CHARGE_STATUS
:
331 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_STATUS
,
332 BQ2415X_MASK_STAT
, BQ2415X_SHIFT_STAT
);
333 case BQ2415X_BOOST_STATUS
:
334 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_STATUS
,
336 case BQ2415X_FAULT_STATUS
:
337 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_STATUS
,
338 BQ2415X_MASK_FAULT
, BQ2415X_SHIFT_FAULT
);
340 case BQ2415X_CHARGE_TERMINATION_STATUS
:
341 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_CONTROL
,
343 case BQ2415X_CHARGE_TERMINATION_ENABLE
:
344 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
346 case BQ2415X_CHARGE_TERMINATION_DISABLE
:
347 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
349 case BQ2415X_CHARGER_STATUS
:
350 ret
= bq2415x_i2c_read_bit(bq
, BQ2415X_REG_CONTROL
,
354 return ret
> 0 ? 0 : 1;
355 case BQ2415X_CHARGER_ENABLE
:
356 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
358 case BQ2415X_CHARGER_DISABLE
:
359 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
361 case BQ2415X_HIGH_IMPEDANCE_STATUS
:
362 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_CONTROL
,
363 BQ2415X_BIT_HZ_MODE
);
364 case BQ2415X_HIGH_IMPEDANCE_ENABLE
:
365 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
366 1, BQ2415X_BIT_HZ_MODE
);
367 case BQ2415X_HIGH_IMPEDANCE_DISABLE
:
368 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
369 0, BQ2415X_BIT_HZ_MODE
);
370 case BQ2415X_BOOST_MODE_STATUS
:
371 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_CONTROL
,
372 BQ2415X_BIT_OPA_MODE
);
373 case BQ2415X_BOOST_MODE_ENABLE
:
374 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
375 1, BQ2415X_BIT_OPA_MODE
);
376 case BQ2415X_BOOST_MODE_DISABLE
:
377 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
378 0, BQ2415X_BIT_OPA_MODE
);
380 case BQ2415X_OTG_LEVEL
:
381 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_VOLTAGE
,
383 case BQ2415X_OTG_ACTIVATE_HIGH
:
384 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_VOLTAGE
,
385 1, BQ2415X_BIT_OTG_PL
);
386 case BQ2415X_OTG_ACTIVATE_LOW
:
387 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_VOLTAGE
,
388 0, BQ2415X_BIT_OTG_PL
);
389 case BQ2415X_OTG_PIN_STATUS
:
390 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_VOLTAGE
,
392 case BQ2415X_OTG_PIN_ENABLE
:
393 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_VOLTAGE
,
394 1, BQ2415X_BIT_OTG_EN
);
395 case BQ2415X_OTG_PIN_DISABLE
:
396 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_VOLTAGE
,
397 0, BQ2415X_BIT_OTG_EN
);
399 case BQ2415X_VENDER_CODE
:
400 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_VENDER
,
401 BQ2415X_MASK_VENDER
, BQ2415X_SHIFT_VENDER
);
402 case BQ2415X_PART_NUMBER
:
403 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_VENDER
,
404 BQ2415X_MASK_PN
, BQ2415X_SHIFT_PN
);
405 case BQ2415X_REVISION
:
406 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_VENDER
,
407 BQ2415X_MASK_REVISION
, BQ2415X_SHIFT_REVISION
);
412 /* detect chip type */
413 static enum bq2415x_chip
bq2415x_detect_chip(struct bq2415x_device
*bq
)
415 struct i2c_client
*client
= to_i2c_client(bq
->dev
);
416 int ret
= bq2415x_exec_command(bq
, BQ2415X_PART_NUMBER
);
421 switch (client
->addr
) {
425 if (bq
->chip
== BQ24151A
)
429 if (bq
->chip
== BQ24150A
||
430 bq
->chip
== BQ24152
||
435 if (bq
->chip
== BQ24153A
)
446 if (bq
->chip
== BQ24156A
)
450 if (bq
->chip
== BQ24157S
)
462 /* detect chip revision */
463 static int bq2415x_detect_revision(struct bq2415x_device
*bq
)
465 int ret
= bq2415x_exec_command(bq
, BQ2415X_REVISION
);
466 int chip
= bq2415x_detect_chip(bq
);
468 if (ret
< 0 || chip
< 0)
477 if (ret
>= 0 && ret
<= 3)
502 /* return chip vender code */
503 static int bq2415x_get_vender_code(struct bq2415x_device
*bq
)
507 ret
= bq2415x_exec_command(bq
, BQ2415X_VENDER_CODE
);
511 /* convert to binary */
513 ((ret
>> 1) & 0x1) * 10 +
514 ((ret
>> 2) & 0x1) * 100;
517 /* reset all chip registers to default state */
518 static void bq2415x_reset_chip(struct bq2415x_device
*bq
)
520 bq2415x_i2c_write(bq
, BQ2415X_REG_CURRENT
, BQ2415X_RESET_CURRENT
);
521 bq2415x_i2c_write(bq
, BQ2415X_REG_VOLTAGE
, BQ2415X_RESET_VOLTAGE
);
522 bq2415x_i2c_write(bq
, BQ2415X_REG_CONTROL
, BQ2415X_RESET_CONTROL
);
523 bq2415x_i2c_write(bq
, BQ2415X_REG_STATUS
, BQ2415X_RESET_STATUS
);
524 bq
->timer_error
= NULL
;
527 /**** properties functions ****/
529 /* set current limit in mA */
530 static int bq2415x_set_current_limit(struct bq2415x_device
*bq
, int mA
)
543 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_CONTROL
, val
,
544 BQ2415X_MASK_LIMIT
, BQ2415X_SHIFT_LIMIT
);
547 /* get current limit in mA */
548 static int bq2415x_get_current_limit(struct bq2415x_device
*bq
)
552 ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_CONTROL
,
553 BQ2415X_MASK_LIMIT
, BQ2415X_SHIFT_LIMIT
);
567 /* set weak battery voltage in mV */
568 static int bq2415x_set_weak_battery_voltage(struct bq2415x_device
*bq
, int mV
)
575 else if (mV
<= 3500 + 50)
577 else if (mV
<= 3600 + 50)
582 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_CONTROL
, val
,
583 BQ2415X_MASK_VLOWV
, BQ2415X_SHIFT_VLOWV
);
586 /* get weak battery voltage in mV */
587 static int bq2415x_get_weak_battery_voltage(struct bq2415x_device
*bq
)
591 ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_CONTROL
,
592 BQ2415X_MASK_VLOWV
, BQ2415X_SHIFT_VLOWV
);
595 return 100 * (34 + ret
);
598 /* set battery regulation voltage in mV */
599 static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device
*bq
,
602 int val
= (mV
/10 - 350) / 2;
605 * According to datasheet, maximum battery regulation voltage is
606 * 4440mV which is b101111 = 47.
613 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_VOLTAGE
, val
,
614 BQ2415X_MASK_VO
, BQ2415X_SHIFT_VO
);
617 /* get battery regulation voltage in mV */
618 static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device
*bq
)
620 int ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_VOLTAGE
,
621 BQ2415X_MASK_VO
, BQ2415X_SHIFT_VO
);
625 return 10 * (350 + 2*ret
);
628 /* set charge current in mA (platform data must provide resistor sense) */
629 static int bq2415x_set_charge_current(struct bq2415x_device
*bq
, int mA
)
633 if (bq
->init_data
.resistor_sense
<= 0)
636 val
= (mA
* bq
->init_data
.resistor_sense
- 37400) / 6800;
642 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_CURRENT
, val
,
643 BQ2415X_MASK_VI_CHRG
| BQ2415X_MASK_RESET
,
644 BQ2415X_SHIFT_VI_CHRG
);
647 /* get charge current in mA (platform data must provide resistor sense) */
648 static int bq2415x_get_charge_current(struct bq2415x_device
*bq
)
652 if (bq
->init_data
.resistor_sense
<= 0)
655 ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_CURRENT
,
656 BQ2415X_MASK_VI_CHRG
, BQ2415X_SHIFT_VI_CHRG
);
659 return (37400 + 6800*ret
) / bq
->init_data
.resistor_sense
;
662 /* set termination current in mA (platform data must provide resistor sense) */
663 static int bq2415x_set_termination_current(struct bq2415x_device
*bq
, int mA
)
667 if (bq
->init_data
.resistor_sense
<= 0)
670 val
= (mA
* bq
->init_data
.resistor_sense
- 3400) / 3400;
676 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_CURRENT
, val
,
677 BQ2415X_MASK_VI_TERM
| BQ2415X_MASK_RESET
,
678 BQ2415X_SHIFT_VI_TERM
);
681 /* get termination current in mA (platform data must provide resistor sense) */
682 static int bq2415x_get_termination_current(struct bq2415x_device
*bq
)
686 if (bq
->init_data
.resistor_sense
<= 0)
689 ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_CURRENT
,
690 BQ2415X_MASK_VI_TERM
, BQ2415X_SHIFT_VI_TERM
);
693 return (3400 + 3400*ret
) / bq
->init_data
.resistor_sense
;
696 /* set default value of property */
697 #define bq2415x_set_default_value(bq, prop) \
700 if (bq->init_data.prop != -1) \
701 ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
706 /* set default values of all properties */
707 static int bq2415x_set_defaults(struct bq2415x_device
*bq
)
709 bq2415x_exec_command(bq
, BQ2415X_BOOST_MODE_DISABLE
);
710 bq2415x_exec_command(bq
, BQ2415X_CHARGER_DISABLE
);
711 bq2415x_exec_command(bq
, BQ2415X_CHARGE_TERMINATION_DISABLE
);
713 bq2415x_set_default_value(bq
, current_limit
);
714 bq2415x_set_default_value(bq
, weak_battery_voltage
);
715 bq2415x_set_default_value(bq
, battery_regulation_voltage
);
717 if (bq
->init_data
.resistor_sense
> 0) {
718 bq2415x_set_default_value(bq
, charge_current
);
719 bq2415x_set_default_value(bq
, termination_current
);
720 bq2415x_exec_command(bq
, BQ2415X_CHARGE_TERMINATION_ENABLE
);
723 bq2415x_exec_command(bq
, BQ2415X_CHARGER_ENABLE
);
727 /**** charger mode functions ****/
729 /* set charger mode */
730 static int bq2415x_set_mode(struct bq2415x_device
*bq
, enum bq2415x_mode mode
)
736 if (mode
== BQ2415X_MODE_BOOST
)
738 else if (mode
!= BQ2415X_MODE_OFF
)
742 ret
= bq2415x_exec_command(bq
, BQ2415X_CHARGER_DISABLE
);
745 ret
= bq2415x_exec_command(bq
, BQ2415X_BOOST_MODE_DISABLE
);
751 case BQ2415X_MODE_OFF
:
752 dev_dbg(bq
->dev
, "changing mode to: Offline\n");
753 ret
= bq2415x_set_current_limit(bq
, 100);
755 case BQ2415X_MODE_NONE
:
756 dev_dbg(bq
->dev
, "changing mode to: N/A\n");
757 ret
= bq2415x_set_current_limit(bq
, 100);
759 case BQ2415X_MODE_HOST_CHARGER
:
760 dev_dbg(bq
->dev
, "changing mode to: Host/HUB charger\n");
761 ret
= bq2415x_set_current_limit(bq
, 500);
763 case BQ2415X_MODE_DEDICATED_CHARGER
:
764 dev_dbg(bq
->dev
, "changing mode to: Dedicated charger\n");
765 ret
= bq2415x_set_current_limit(bq
, 1800);
767 case BQ2415X_MODE_BOOST
: /* Boost mode */
768 dev_dbg(bq
->dev
, "changing mode to: Boost\n");
769 ret
= bq2415x_set_current_limit(bq
, 100);
777 ret
= bq2415x_exec_command(bq
, BQ2415X_CHARGER_ENABLE
);
779 ret
= bq2415x_exec_command(bq
, BQ2415X_BOOST_MODE_ENABLE
);
784 bq2415x_set_default_value(bq
, weak_battery_voltage
);
785 bq2415x_set_default_value(bq
, battery_regulation_voltage
);
788 sysfs_notify(&bq
->charger
->dev
.kobj
, NULL
, "mode");
794 static int bq2415x_notifier_call(struct notifier_block
*nb
,
795 unsigned long val
, void *v
)
797 struct bq2415x_device
*bq
=
798 container_of(nb
, struct bq2415x_device
, nb
);
799 struct power_supply
*psy
= v
;
800 enum bq2415x_mode mode
;
801 union power_supply_propval prop
;
805 if (val
!= PSY_EVENT_PROP_CHANGED
)
808 if (psy
!= bq
->notify_psy
)
811 dev_dbg(bq
->dev
, "notifier call was called\n");
813 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_CURRENT_MAX
,
821 mode
= BQ2415X_MODE_OFF
;
823 mode
= BQ2415X_MODE_NONE
;
825 mode
= BQ2415X_MODE_HOST_CHARGER
;
827 mode
= BQ2415X_MODE_DEDICATED_CHARGER
;
829 if (bq
->reported_mode
== mode
)
832 bq
->reported_mode
= mode
;
834 /* if automode is not enabled do not tell about reported_mode */
835 if (bq
->automode
< 1)
838 schedule_delayed_work(&bq
->work
, 0);
843 /**** timer functions ****/
845 /* enable/disable auto resetting chip timer */
846 static void bq2415x_set_autotimer(struct bq2415x_device
*bq
, int state
)
848 mutex_lock(&bq2415x_timer_mutex
);
850 if (bq
->autotimer
== state
) {
851 mutex_unlock(&bq2415x_timer_mutex
);
855 bq
->autotimer
= state
;
858 schedule_delayed_work(&bq
->work
, BQ2415X_TIMER_TIMEOUT
* HZ
);
859 bq2415x_exec_command(bq
, BQ2415X_TIMER_RESET
);
860 bq
->timer_error
= NULL
;
862 cancel_delayed_work_sync(&bq
->work
);
865 mutex_unlock(&bq2415x_timer_mutex
);
868 /* called by bq2415x_timer_work on timer error */
869 static void bq2415x_timer_error(struct bq2415x_device
*bq
, const char *msg
)
871 bq
->timer_error
= msg
;
872 sysfs_notify(&bq
->charger
->dev
.kobj
, NULL
, "timer");
873 dev_err(bq
->dev
, "%s\n", msg
);
874 if (bq
->automode
> 0)
876 bq2415x_set_mode(bq
, BQ2415X_MODE_OFF
);
877 bq2415x_set_autotimer(bq
, 0);
880 /* delayed work function for auto resetting chip timer */
881 static void bq2415x_timer_work(struct work_struct
*work
)
883 struct bq2415x_device
*bq
= container_of(work
, struct bq2415x_device
,
889 if (bq
->automode
> 0 && (bq
->reported_mode
!= bq
->mode
)) {
890 sysfs_notify(&bq
->charger
->dev
.kobj
, NULL
, "reported_mode");
891 bq2415x_set_mode(bq
, bq
->reported_mode
);
897 ret
= bq2415x_exec_command(bq
, BQ2415X_TIMER_RESET
);
899 bq2415x_timer_error(bq
, "Resetting timer failed");
903 boost
= bq2415x_exec_command(bq
, BQ2415X_BOOST_MODE_STATUS
);
905 bq2415x_timer_error(bq
, "Unknown error");
909 error
= bq2415x_exec_command(bq
, BQ2415X_FAULT_STATUS
);
911 bq2415x_timer_error(bq
, "Unknown error");
917 /* Non fatal errors, chip is OK */
918 case 0: /* No error */
920 case 6: /* Timer expired */
921 dev_err(bq
->dev
, "Timer expired\n");
923 case 3: /* Battery voltage too low */
924 dev_err(bq
->dev
, "Battery voltage to low\n");
927 /* Fatal errors, disable and reset chip */
928 case 1: /* Overvoltage protection (chip fried) */
929 bq2415x_timer_error(bq
,
930 "Overvoltage protection (chip fried)");
932 case 2: /* Overload */
933 bq2415x_timer_error(bq
, "Overload");
935 case 4: /* Battery overvoltage protection */
936 bq2415x_timer_error(bq
,
937 "Battery overvoltage protection");
939 case 5: /* Thermal shutdown (too hot) */
940 bq2415x_timer_error(bq
,
941 "Thermal shutdown (too hot)");
944 bq2415x_timer_error(bq
, "Unknown error");
949 /* Non fatal errors, chip is OK */
950 case 0: /* No error */
952 case 2: /* Sleep mode */
953 dev_err(bq
->dev
, "Sleep mode\n");
955 case 3: /* Poor input source */
956 dev_err(bq
->dev
, "Poor input source\n");
958 case 6: /* Timer expired */
959 dev_err(bq
->dev
, "Timer expired\n");
961 case 7: /* No battery */
962 dev_err(bq
->dev
, "No battery\n");
965 /* Fatal errors, disable and reset chip */
966 case 1: /* Overvoltage protection (chip fried) */
967 bq2415x_timer_error(bq
,
968 "Overvoltage protection (chip fried)");
970 case 4: /* Battery overvoltage protection */
971 bq2415x_timer_error(bq
,
972 "Battery overvoltage protection");
974 case 5: /* Thermal shutdown (too hot) */
975 bq2415x_timer_error(bq
,
976 "Thermal shutdown (too hot)");
981 schedule_delayed_work(&bq
->work
, BQ2415X_TIMER_TIMEOUT
* HZ
);
984 /**** power supply interface code ****/
986 static enum power_supply_property bq2415x_power_supply_props
[] = {
987 /* TODO: maybe add more power supply properties */
988 POWER_SUPPLY_PROP_STATUS
,
989 POWER_SUPPLY_PROP_MODEL_NAME
,
992 static int bq2415x_power_supply_get_property(struct power_supply
*psy
,
993 enum power_supply_property psp
,
994 union power_supply_propval
*val
)
996 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1000 case POWER_SUPPLY_PROP_STATUS
:
1001 ret
= bq2415x_exec_command(bq
, BQ2415X_CHARGE_STATUS
);
1004 else if (ret
== 0) /* Ready */
1005 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
1006 else if (ret
== 1) /* Charge in progress */
1007 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
1008 else if (ret
== 2) /* Charge done */
1009 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
1011 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
1013 case POWER_SUPPLY_PROP_MODEL_NAME
:
1014 val
->strval
= bq
->model
;
1022 static int bq2415x_power_supply_init(struct bq2415x_device
*bq
)
1027 struct power_supply_config psy_cfg
= { .drv_data
= bq
, };
1029 bq
->charger_desc
.name
= bq
->name
;
1030 bq
->charger_desc
.type
= POWER_SUPPLY_TYPE_USB
;
1031 bq
->charger_desc
.properties
= bq2415x_power_supply_props
;
1032 bq
->charger_desc
.num_properties
=
1033 ARRAY_SIZE(bq2415x_power_supply_props
);
1034 bq
->charger_desc
.get_property
= bq2415x_power_supply_get_property
;
1036 ret
= bq2415x_detect_chip(bq
);
1042 ret
= bq2415x_detect_revision(bq
);
1044 strcpy(revstr
, "unknown");
1046 sprintf(revstr
, "1.%d", ret
);
1048 bq
->model
= kasprintf(GFP_KERNEL
,
1049 "chip %s, revision %s, vender code %.3d",
1050 bq2415x_chip_name
[chip
], revstr
,
1051 bq2415x_get_vender_code(bq
));
1053 dev_err(bq
->dev
, "failed to allocate model name\n");
1057 bq
->charger
= power_supply_register(bq
->dev
, &bq
->charger_desc
,
1059 if (IS_ERR(bq
->charger
)) {
1061 return PTR_ERR(bq
->charger
);
1067 static void bq2415x_power_supply_exit(struct bq2415x_device
*bq
)
1070 if (bq
->automode
> 0)
1072 cancel_delayed_work_sync(&bq
->work
);
1073 power_supply_unregister(bq
->charger
);
1077 /**** additional sysfs entries for power supply interface ****/
1079 /* show *_status entries */
1080 static ssize_t
bq2415x_sysfs_show_status(struct device
*dev
,
1081 struct device_attribute
*attr
,
1084 struct power_supply
*psy
= dev_get_drvdata(dev
);
1085 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1086 enum bq2415x_command command
;
1089 if (strcmp(attr
->attr
.name
, "otg_status") == 0)
1090 command
= BQ2415X_OTG_STATUS
;
1091 else if (strcmp(attr
->attr
.name
, "charge_status") == 0)
1092 command
= BQ2415X_CHARGE_STATUS
;
1093 else if (strcmp(attr
->attr
.name
, "boost_status") == 0)
1094 command
= BQ2415X_BOOST_STATUS
;
1095 else if (strcmp(attr
->attr
.name
, "fault_status") == 0)
1096 command
= BQ2415X_FAULT_STATUS
;
1100 ret
= bq2415x_exec_command(bq
, command
);
1103 return sprintf(buf
, "%d\n", ret
);
1108 * auto - enable auto mode
1109 * off - disable auto mode
1110 * (other values) - reset chip timer
1112 static ssize_t
bq2415x_sysfs_set_timer(struct device
*dev
,
1113 struct device_attribute
*attr
,
1117 struct power_supply
*psy
= dev_get_drvdata(dev
);
1118 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1121 if (strncmp(buf
, "auto", 4) == 0)
1122 bq2415x_set_autotimer(bq
, 1);
1123 else if (strncmp(buf
, "off", 3) == 0)
1124 bq2415x_set_autotimer(bq
, 0);
1126 ret
= bq2415x_exec_command(bq
, BQ2415X_TIMER_RESET
);
1133 /* show timer entry (auto or off) */
1134 static ssize_t
bq2415x_sysfs_show_timer(struct device
*dev
,
1135 struct device_attribute
*attr
,
1138 struct power_supply
*psy
= dev_get_drvdata(dev
);
1139 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1141 if (bq
->timer_error
)
1142 return sprintf(buf
, "%s\n", bq
->timer_error
);
1145 return sprintf(buf
, "auto\n");
1146 return sprintf(buf
, "off\n");
1151 * auto - if automode is supported, enable it and set mode to reported
1152 * none - disable charger and boost mode
1153 * host - charging mode for host/hub chargers (current limit 500mA)
1154 * dedicated - charging mode for dedicated chargers (unlimited current limit)
1155 * boost - disable charger and enable boost mode
1157 static ssize_t
bq2415x_sysfs_set_mode(struct device
*dev
,
1158 struct device_attribute
*attr
,
1162 struct power_supply
*psy
= dev_get_drvdata(dev
);
1163 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1164 enum bq2415x_mode mode
;
1167 if (strncmp(buf
, "auto", 4) == 0) {
1168 if (bq
->automode
< 0)
1171 mode
= bq
->reported_mode
;
1172 } else if (strncmp(buf
, "off", 3) == 0) {
1173 if (bq
->automode
> 0)
1175 mode
= BQ2415X_MODE_OFF
;
1176 } else if (strncmp(buf
, "none", 4) == 0) {
1177 if (bq
->automode
> 0)
1179 mode
= BQ2415X_MODE_NONE
;
1180 } else if (strncmp(buf
, "host", 4) == 0) {
1181 if (bq
->automode
> 0)
1183 mode
= BQ2415X_MODE_HOST_CHARGER
;
1184 } else if (strncmp(buf
, "dedicated", 9) == 0) {
1185 if (bq
->automode
> 0)
1187 mode
= BQ2415X_MODE_DEDICATED_CHARGER
;
1188 } else if (strncmp(buf
, "boost", 5) == 0) {
1189 if (bq
->automode
> 0)
1191 mode
= BQ2415X_MODE_BOOST
;
1192 } else if (strncmp(buf
, "reset", 5) == 0) {
1193 bq2415x_reset_chip(bq
);
1194 bq2415x_set_defaults(bq
);
1195 if (bq
->automode
<= 0)
1198 mode
= bq
->reported_mode
;
1203 ret
= bq2415x_set_mode(bq
, mode
);
1209 /* show mode entry (auto, none, host, dedicated or boost) */
1210 static ssize_t
bq2415x_sysfs_show_mode(struct device
*dev
,
1211 struct device_attribute
*attr
,
1214 struct power_supply
*psy
= dev_get_drvdata(dev
);
1215 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1218 if (bq
->automode
> 0)
1219 ret
+= sprintf(buf
+ret
, "auto (");
1222 case BQ2415X_MODE_OFF
:
1223 ret
+= sprintf(buf
+ret
, "off");
1225 case BQ2415X_MODE_NONE
:
1226 ret
+= sprintf(buf
+ret
, "none");
1228 case BQ2415X_MODE_HOST_CHARGER
:
1229 ret
+= sprintf(buf
+ret
, "host");
1231 case BQ2415X_MODE_DEDICATED_CHARGER
:
1232 ret
+= sprintf(buf
+ret
, "dedicated");
1234 case BQ2415X_MODE_BOOST
:
1235 ret
+= sprintf(buf
+ret
, "boost");
1239 if (bq
->automode
> 0)
1240 ret
+= sprintf(buf
+ret
, ")");
1242 ret
+= sprintf(buf
+ret
, "\n");
1246 /* show reported_mode entry (none, host, dedicated or boost) */
1247 static ssize_t
bq2415x_sysfs_show_reported_mode(struct device
*dev
,
1248 struct device_attribute
*attr
,
1251 struct power_supply
*psy
= dev_get_drvdata(dev
);
1252 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1254 if (bq
->automode
< 0)
1257 switch (bq
->reported_mode
) {
1258 case BQ2415X_MODE_OFF
:
1259 return sprintf(buf
, "off\n");
1260 case BQ2415X_MODE_NONE
:
1261 return sprintf(buf
, "none\n");
1262 case BQ2415X_MODE_HOST_CHARGER
:
1263 return sprintf(buf
, "host\n");
1264 case BQ2415X_MODE_DEDICATED_CHARGER
:
1265 return sprintf(buf
, "dedicated\n");
1266 case BQ2415X_MODE_BOOST
:
1267 return sprintf(buf
, "boost\n");
1273 /* directly set raw value to chip register, format: 'register value' */
1274 static ssize_t
bq2415x_sysfs_set_registers(struct device
*dev
,
1275 struct device_attribute
*attr
,
1279 struct power_supply
*psy
= dev_get_drvdata(dev
);
1280 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1285 if (sscanf(buf
, "%x %x", ®
, &val
) != 2)
1288 if (reg
> 4 || val
> 255)
1291 ret
= bq2415x_i2c_write(bq
, reg
, val
);
1297 /* print value of chip register, format: 'register=value' */
1298 static ssize_t
bq2415x_sysfs_print_reg(struct bq2415x_device
*bq
,
1302 int ret
= bq2415x_i2c_read(bq
, reg
);
1305 return sprintf(buf
, "%#.2x=error %d\n", reg
, ret
);
1306 return sprintf(buf
, "%#.2x=%#.2x\n", reg
, ret
);
1309 /* show all raw values of chip register, format per line: 'register=value' */
1310 static ssize_t
bq2415x_sysfs_show_registers(struct device
*dev
,
1311 struct device_attribute
*attr
,
1314 struct power_supply
*psy
= dev_get_drvdata(dev
);
1315 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1318 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_STATUS
, buf
+ret
);
1319 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_CONTROL
, buf
+ret
);
1320 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_VOLTAGE
, buf
+ret
);
1321 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_VENDER
, buf
+ret
);
1322 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_CURRENT
, buf
+ret
);
1326 /* set current and voltage limit entries (in mA or mV) */
1327 static ssize_t
bq2415x_sysfs_set_limit(struct device
*dev
,
1328 struct device_attribute
*attr
,
1332 struct power_supply
*psy
= dev_get_drvdata(dev
);
1333 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1337 if (kstrtol(buf
, 10, &val
) < 0)
1340 if (strcmp(attr
->attr
.name
, "current_limit") == 0)
1341 ret
= bq2415x_set_current_limit(bq
, val
);
1342 else if (strcmp(attr
->attr
.name
, "weak_battery_voltage") == 0)
1343 ret
= bq2415x_set_weak_battery_voltage(bq
, val
);
1344 else if (strcmp(attr
->attr
.name
, "battery_regulation_voltage") == 0)
1345 ret
= bq2415x_set_battery_regulation_voltage(bq
, val
);
1346 else if (strcmp(attr
->attr
.name
, "charge_current") == 0)
1347 ret
= bq2415x_set_charge_current(bq
, val
);
1348 else if (strcmp(attr
->attr
.name
, "termination_current") == 0)
1349 ret
= bq2415x_set_termination_current(bq
, val
);
1358 /* show current and voltage limit entries (in mA or mV) */
1359 static ssize_t
bq2415x_sysfs_show_limit(struct device
*dev
,
1360 struct device_attribute
*attr
,
1363 struct power_supply
*psy
= dev_get_drvdata(dev
);
1364 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1367 if (strcmp(attr
->attr
.name
, "current_limit") == 0)
1368 ret
= bq2415x_get_current_limit(bq
);
1369 else if (strcmp(attr
->attr
.name
, "weak_battery_voltage") == 0)
1370 ret
= bq2415x_get_weak_battery_voltage(bq
);
1371 else if (strcmp(attr
->attr
.name
, "battery_regulation_voltage") == 0)
1372 ret
= bq2415x_get_battery_regulation_voltage(bq
);
1373 else if (strcmp(attr
->attr
.name
, "charge_current") == 0)
1374 ret
= bq2415x_get_charge_current(bq
);
1375 else if (strcmp(attr
->attr
.name
, "termination_current") == 0)
1376 ret
= bq2415x_get_termination_current(bq
);
1382 return sprintf(buf
, "%d\n", ret
);
1385 /* set *_enable entries */
1386 static ssize_t
bq2415x_sysfs_set_enable(struct device
*dev
,
1387 struct device_attribute
*attr
,
1391 struct power_supply
*psy
= dev_get_drvdata(dev
);
1392 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1393 enum bq2415x_command command
;
1397 if (kstrtol(buf
, 10, &val
) < 0)
1400 if (strcmp(attr
->attr
.name
, "charge_termination_enable") == 0)
1401 command
= val
? BQ2415X_CHARGE_TERMINATION_ENABLE
:
1402 BQ2415X_CHARGE_TERMINATION_DISABLE
;
1403 else if (strcmp(attr
->attr
.name
, "high_impedance_enable") == 0)
1404 command
= val
? BQ2415X_HIGH_IMPEDANCE_ENABLE
:
1405 BQ2415X_HIGH_IMPEDANCE_DISABLE
;
1406 else if (strcmp(attr
->attr
.name
, "otg_pin_enable") == 0)
1407 command
= val
? BQ2415X_OTG_PIN_ENABLE
:
1408 BQ2415X_OTG_PIN_DISABLE
;
1409 else if (strcmp(attr
->attr
.name
, "stat_pin_enable") == 0)
1410 command
= val
? BQ2415X_STAT_PIN_ENABLE
:
1411 BQ2415X_STAT_PIN_DISABLE
;
1415 ret
= bq2415x_exec_command(bq
, command
);
1421 /* show *_enable entries */
1422 static ssize_t
bq2415x_sysfs_show_enable(struct device
*dev
,
1423 struct device_attribute
*attr
,
1426 struct power_supply
*psy
= dev_get_drvdata(dev
);
1427 struct bq2415x_device
*bq
= power_supply_get_drvdata(psy
);
1428 enum bq2415x_command command
;
1431 if (strcmp(attr
->attr
.name
, "charge_termination_enable") == 0)
1432 command
= BQ2415X_CHARGE_TERMINATION_STATUS
;
1433 else if (strcmp(attr
->attr
.name
, "high_impedance_enable") == 0)
1434 command
= BQ2415X_HIGH_IMPEDANCE_STATUS
;
1435 else if (strcmp(attr
->attr
.name
, "otg_pin_enable") == 0)
1436 command
= BQ2415X_OTG_PIN_STATUS
;
1437 else if (strcmp(attr
->attr
.name
, "stat_pin_enable") == 0)
1438 command
= BQ2415X_STAT_PIN_STATUS
;
1442 ret
= bq2415x_exec_command(bq
, command
);
1445 return sprintf(buf
, "%d\n", ret
);
1448 static DEVICE_ATTR(current_limit
, S_IWUSR
| S_IRUGO
,
1449 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1450 static DEVICE_ATTR(weak_battery_voltage
, S_IWUSR
| S_IRUGO
,
1451 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1452 static DEVICE_ATTR(battery_regulation_voltage
, S_IWUSR
| S_IRUGO
,
1453 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1454 static DEVICE_ATTR(charge_current
, S_IWUSR
| S_IRUGO
,
1455 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1456 static DEVICE_ATTR(termination_current
, S_IWUSR
| S_IRUGO
,
1457 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1459 static DEVICE_ATTR(charge_termination_enable
, S_IWUSR
| S_IRUGO
,
1460 bq2415x_sysfs_show_enable
, bq2415x_sysfs_set_enable
);
1461 static DEVICE_ATTR(high_impedance_enable
, S_IWUSR
| S_IRUGO
,
1462 bq2415x_sysfs_show_enable
, bq2415x_sysfs_set_enable
);
1463 static DEVICE_ATTR(otg_pin_enable
, S_IWUSR
| S_IRUGO
,
1464 bq2415x_sysfs_show_enable
, bq2415x_sysfs_set_enable
);
1465 static DEVICE_ATTR(stat_pin_enable
, S_IWUSR
| S_IRUGO
,
1466 bq2415x_sysfs_show_enable
, bq2415x_sysfs_set_enable
);
1468 static DEVICE_ATTR(reported_mode
, S_IRUGO
,
1469 bq2415x_sysfs_show_reported_mode
, NULL
);
1470 static DEVICE_ATTR(mode
, S_IWUSR
| S_IRUGO
,
1471 bq2415x_sysfs_show_mode
, bq2415x_sysfs_set_mode
);
1472 static DEVICE_ATTR(timer
, S_IWUSR
| S_IRUGO
,
1473 bq2415x_sysfs_show_timer
, bq2415x_sysfs_set_timer
);
1475 static DEVICE_ATTR(registers
, S_IWUSR
| S_IRUGO
,
1476 bq2415x_sysfs_show_registers
, bq2415x_sysfs_set_registers
);
1478 static DEVICE_ATTR(otg_status
, S_IRUGO
, bq2415x_sysfs_show_status
, NULL
);
1479 static DEVICE_ATTR(charge_status
, S_IRUGO
, bq2415x_sysfs_show_status
, NULL
);
1480 static DEVICE_ATTR(boost_status
, S_IRUGO
, bq2415x_sysfs_show_status
, NULL
);
1481 static DEVICE_ATTR(fault_status
, S_IRUGO
, bq2415x_sysfs_show_status
, NULL
);
1483 static struct attribute
*bq2415x_sysfs_attributes
[] = {
1485 * TODO: some (appropriate) of these attrs should be switched to
1486 * use power supply class props.
1488 &dev_attr_current_limit
.attr
,
1489 &dev_attr_weak_battery_voltage
.attr
,
1490 &dev_attr_battery_regulation_voltage
.attr
,
1491 &dev_attr_charge_current
.attr
,
1492 &dev_attr_termination_current
.attr
,
1494 &dev_attr_charge_termination_enable
.attr
,
1495 &dev_attr_high_impedance_enable
.attr
,
1496 &dev_attr_otg_pin_enable
.attr
,
1497 &dev_attr_stat_pin_enable
.attr
,
1499 &dev_attr_reported_mode
.attr
,
1500 &dev_attr_mode
.attr
,
1501 &dev_attr_timer
.attr
,
1503 &dev_attr_registers
.attr
,
1505 &dev_attr_otg_status
.attr
,
1506 &dev_attr_charge_status
.attr
,
1507 &dev_attr_boost_status
.attr
,
1508 &dev_attr_fault_status
.attr
,
1512 static const struct attribute_group bq2415x_sysfs_attr_group
= {
1513 .attrs
= bq2415x_sysfs_attributes
,
1516 static int bq2415x_sysfs_init(struct bq2415x_device
*bq
)
1518 return sysfs_create_group(&bq
->charger
->dev
.kobj
,
1519 &bq2415x_sysfs_attr_group
);
1522 static void bq2415x_sysfs_exit(struct bq2415x_device
*bq
)
1524 sysfs_remove_group(&bq
->charger
->dev
.kobj
, &bq2415x_sysfs_attr_group
);
1527 /* main bq2415x probe function */
1528 static int bq2415x_probe(struct i2c_client
*client
,
1529 const struct i2c_device_id
*id
)
1534 struct bq2415x_device
*bq
;
1535 struct device_node
*np
= client
->dev
.of_node
;
1536 struct bq2415x_platform_data
*pdata
= client
->dev
.platform_data
;
1538 if (!np
&& !pdata
) {
1539 dev_err(&client
->dev
, "platform data missing\n");
1543 /* Get new ID for the new device */
1544 mutex_lock(&bq2415x_id_mutex
);
1545 num
= idr_alloc(&bq2415x_id
, client
, 0, 0, GFP_KERNEL
);
1546 mutex_unlock(&bq2415x_id_mutex
);
1550 name
= kasprintf(GFP_KERNEL
, "%s-%d", id
->name
, num
);
1552 dev_err(&client
->dev
, "failed to allocate device name\n");
1557 bq
= devm_kzalloc(&client
->dev
, sizeof(*bq
), GFP_KERNEL
);
1559 dev_err(&client
->dev
, "failed to allocate device data\n");
1565 bq
->notify_psy
= power_supply_get_by_phandle(np
, "ti,usb-charger-detection");
1567 if (IS_ERR(bq
->notify_psy
)) {
1568 dev_info(&client
->dev
,
1569 "no 'ti,usb-charger-detection' property (err=%ld)\n",
1570 PTR_ERR(bq
->notify_psy
));
1571 bq
->notify_psy
= NULL
;
1572 } else if (!bq
->notify_psy
) {
1573 ret
= -EPROBE_DEFER
;
1577 else if (pdata
->notify_device
)
1578 bq
->notify_psy
= power_supply_get_by_name(pdata
->notify_device
);
1580 bq
->notify_psy
= NULL
;
1582 i2c_set_clientdata(client
, bq
);
1585 bq
->dev
= &client
->dev
;
1586 bq
->chip
= id
->driver_data
;
1588 bq
->mode
= BQ2415X_MODE_OFF
;
1589 bq
->reported_mode
= BQ2415X_MODE_OFF
;
1594 ret
= of_property_read_u32(np
, "ti,current-limit",
1595 &bq
->init_data
.current_limit
);
1598 ret
= of_property_read_u32(np
, "ti,weak-battery-voltage",
1599 &bq
->init_data
.weak_battery_voltage
);
1602 ret
= of_property_read_u32(np
, "ti,battery-regulation-voltage",
1603 &bq
->init_data
.battery_regulation_voltage
);
1606 ret
= of_property_read_u32(np
, "ti,charge-current",
1607 &bq
->init_data
.charge_current
);
1610 ret
= of_property_read_u32(np
, "ti,termination-current",
1611 &bq
->init_data
.termination_current
);
1614 ret
= of_property_read_u32(np
, "ti,resistor-sense",
1615 &bq
->init_data
.resistor_sense
);
1619 memcpy(&bq
->init_data
, pdata
, sizeof(bq
->init_data
));
1622 bq2415x_reset_chip(bq
);
1624 ret
= bq2415x_power_supply_init(bq
);
1626 dev_err(bq
->dev
, "failed to register power supply: %d\n", ret
);
1630 ret
= bq2415x_sysfs_init(bq
);
1632 dev_err(bq
->dev
, "failed to create sysfs entries: %d\n", ret
);
1636 ret
= bq2415x_set_defaults(bq
);
1638 dev_err(bq
->dev
, "failed to set default values: %d\n", ret
);
1642 if (bq
->notify_psy
) {
1643 bq
->nb
.notifier_call
= bq2415x_notifier_call
;
1644 ret
= power_supply_reg_notifier(&bq
->nb
);
1646 dev_err(bq
->dev
, "failed to reg notifier: %d\n", ret
);
1650 /* Query for initial reported_mode and set it */
1651 bq2415x_notifier_call(&bq
->nb
, PSY_EVENT_PROP_CHANGED
, bq
->notify_psy
);
1652 bq2415x_set_mode(bq
, bq
->reported_mode
);
1655 dev_info(bq
->dev
, "automode enabled\n");
1658 dev_info(bq
->dev
, "automode not supported\n");
1661 INIT_DELAYED_WORK(&bq
->work
, bq2415x_timer_work
);
1662 bq2415x_set_autotimer(bq
, 1);
1664 dev_info(bq
->dev
, "driver registered\n");
1669 bq2415x_sysfs_exit(bq
);
1671 bq2415x_power_supply_exit(bq
);
1674 power_supply_put(bq
->notify_psy
);
1678 mutex_lock(&bq2415x_id_mutex
);
1679 idr_remove(&bq2415x_id
, num
);
1680 mutex_unlock(&bq2415x_id_mutex
);
1685 /* main bq2415x remove function */
1687 static int bq2415x_remove(struct i2c_client
*client
)
1689 struct bq2415x_device
*bq
= i2c_get_clientdata(client
);
1691 if (bq
->notify_psy
) {
1692 power_supply_unreg_notifier(&bq
->nb
);
1693 power_supply_put(bq
->notify_psy
);
1696 bq2415x_sysfs_exit(bq
);
1697 bq2415x_power_supply_exit(bq
);
1699 bq2415x_reset_chip(bq
);
1701 mutex_lock(&bq2415x_id_mutex
);
1702 idr_remove(&bq2415x_id
, bq
->id
);
1703 mutex_unlock(&bq2415x_id_mutex
);
1705 dev_info(bq
->dev
, "driver unregistered\n");
1712 static const struct i2c_device_id bq2415x_i2c_id_table
[] = {
1713 { "bq2415x", BQUNKNOWN
},
1714 { "bq24150", BQ24150
},
1715 { "bq24150a", BQ24150A
},
1716 { "bq24151", BQ24151
},
1717 { "bq24151a", BQ24151A
},
1718 { "bq24152", BQ24152
},
1719 { "bq24153", BQ24153
},
1720 { "bq24153a", BQ24153A
},
1721 { "bq24155", BQ24155
},
1722 { "bq24156", BQ24156
},
1723 { "bq24156a", BQ24156A
},
1724 { "bq24157s", BQ24157S
},
1725 { "bq24158", BQ24158
},
1728 MODULE_DEVICE_TABLE(i2c
, bq2415x_i2c_id_table
);
1730 static struct i2c_driver bq2415x_driver
= {
1732 .name
= "bq2415x-charger",
1734 .probe
= bq2415x_probe
,
1735 .remove
= bq2415x_remove
,
1736 .id_table
= bq2415x_i2c_id_table
,
1738 module_i2c_driver(bq2415x_driver
);
1740 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
1741 MODULE_DESCRIPTION("bq2415x charger driver");
1742 MODULE_LICENSE("GPL");