2 * bq2415x charger driver
4 * Copyright (C) 2011-2012 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.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * http://www.ti.com/product/bq24150
24 * http://www.ti.com/product/bq24150a
25 * http://www.ti.com/product/bq24152
26 * http://www.ti.com/product/bq24153
27 * http://www.ti.com/product/bq24153a
28 * http://www.ti.com/product/bq24155
31 #include <linux/version.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/param.h>
35 #include <linux/err.h>
36 #include <linux/workqueue.h>
37 #include <linux/sysfs.h>
38 #include <linux/platform_device.h>
39 #include <linux/power_supply.h>
40 #include <linux/idr.h>
41 #include <linux/i2c.h>
42 #include <linux/slab.h>
44 #include <linux/power/bq2415x_charger.h>
46 /* timeout for resetting chip timer */
47 #define BQ2415X_TIMER_TIMEOUT 10
49 #define BQ2415X_REG_STATUS 0x00
50 #define BQ2415X_REG_CONTROL 0x01
51 #define BQ2415X_REG_VOLTAGE 0x02
52 #define BQ2415X_REG_VENDER 0x03
53 #define BQ2415X_REG_CURRENT 0x04
55 /* reset state for all registers */
56 #define BQ2415X_RESET_STATUS BIT(6)
57 #define BQ2415X_RESET_CONTROL (BIT(4)|BIT(5))
58 #define BQ2415X_RESET_VOLTAGE (BIT(1)|BIT(3))
59 #define BQ2415X_RESET_CURRENT (BIT(0)|BIT(3)|BIT(7))
62 #define BQ2415X_BIT_TMR_RST 7
63 #define BQ2415X_BIT_OTG 7
64 #define BQ2415X_BIT_EN_STAT 6
65 #define BQ2415X_MASK_STAT (BIT(4)|BIT(5))
66 #define BQ2415X_SHIFT_STAT 4
67 #define BQ2415X_BIT_BOOST 3
68 #define BQ2415X_MASK_FAULT (BIT(0)|BIT(1)|BIT(2))
69 #define BQ2415X_SHIFT_FAULT 0
71 /* control register */
72 #define BQ2415X_MASK_LIMIT (BIT(6)|BIT(7))
73 #define BQ2415X_SHIFT_LIMIT 6
74 #define BQ2415X_MASK_VLOWV (BIT(4)|BIT(5))
75 #define BQ2415X_SHIFT_VLOWV 4
76 #define BQ2415X_BIT_TE 3
77 #define BQ2415X_BIT_CE 2
78 #define BQ2415X_BIT_HZ_MODE 1
79 #define BQ2415X_BIT_OPA_MODE 0
81 /* voltage register */
82 #define BQ2415X_MASK_VO (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
83 #define BQ2415X_SHIFT_VO 2
84 #define BQ2415X_BIT_OTG_PL 1
85 #define BQ2415X_BIT_OTG_EN 0
88 #define BQ2415X_MASK_VENDER (BIT(5)|BIT(6)|BIT(7))
89 #define BQ2415X_SHIFT_VENDER 5
90 #define BQ2415X_MASK_PN (BIT(3)|BIT(4))
91 #define BQ2415X_SHIFT_PN 3
92 #define BQ2415X_MASK_REVISION (BIT(0)|BIT(1)|BIT(2))
93 #define BQ2415X_SHIFT_REVISION 0
95 /* current register */
96 #define BQ2415X_MASK_RESET BIT(7)
97 #define BQ2415X_MASK_VI_CHRG (BIT(4)|BIT(5)|BIT(6))
98 #define BQ2415X_SHIFT_VI_CHRG 4
100 #define BQ2415X_MASK_VI_TERM (BIT(0)|BIT(1)|BIT(2))
101 #define BQ2415X_SHIFT_VI_TERM 0
104 enum bq2415x_command
{
107 BQ2415X_STAT_PIN_STATUS
,
108 BQ2415X_STAT_PIN_ENABLE
,
109 BQ2415X_STAT_PIN_DISABLE
,
110 BQ2415X_CHARGE_STATUS
,
111 BQ2415X_BOOST_STATUS
,
112 BQ2415X_FAULT_STATUS
,
114 BQ2415X_CHARGE_TERMINATION_STATUS
,
115 BQ2415X_CHARGE_TERMINATION_ENABLE
,
116 BQ2415X_CHARGE_TERMINATION_DISABLE
,
117 BQ2415X_CHARGER_STATUS
,
118 BQ2415X_CHARGER_ENABLE
,
119 BQ2415X_CHARGER_DISABLE
,
120 BQ2415X_HIGH_IMPEDANCE_STATUS
,
121 BQ2415X_HIGH_IMPEDANCE_ENABLE
,
122 BQ2415X_HIGH_IMPEDANCE_DISABLE
,
123 BQ2415X_BOOST_MODE_STATUS
,
124 BQ2415X_BOOST_MODE_ENABLE
,
125 BQ2415X_BOOST_MODE_DISABLE
,
128 BQ2415X_OTG_ACTIVATE_HIGH
,
129 BQ2415X_OTG_ACTIVATE_LOW
,
130 BQ2415X_OTG_PIN_STATUS
,
131 BQ2415X_OTG_PIN_ENABLE
,
132 BQ2415X_OTG_PIN_DISABLE
,
154 static char *bq2415x_chip_name
[] = {
169 struct bq2415x_device
{
171 struct bq2415x_platform_data init_data
;
172 struct power_supply charger
;
173 struct delayed_work work
;
174 enum bq2415x_mode reported_mode
;/* mode reported by hook function */
175 enum bq2415x_mode mode
; /* current 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
,
355 return ret
> 0 ? 0 : 1;
356 case BQ2415X_CHARGER_ENABLE
:
357 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
359 case BQ2415X_CHARGER_DISABLE
:
360 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
362 case BQ2415X_HIGH_IMPEDANCE_STATUS
:
363 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_CONTROL
,
364 BQ2415X_BIT_HZ_MODE
);
365 case BQ2415X_HIGH_IMPEDANCE_ENABLE
:
366 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
367 1, BQ2415X_BIT_HZ_MODE
);
368 case BQ2415X_HIGH_IMPEDANCE_DISABLE
:
369 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
370 0, BQ2415X_BIT_HZ_MODE
);
371 case BQ2415X_BOOST_MODE_STATUS
:
372 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_CONTROL
,
373 BQ2415X_BIT_OPA_MODE
);
374 case BQ2415X_BOOST_MODE_ENABLE
:
375 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
376 1, BQ2415X_BIT_OPA_MODE
);
377 case BQ2415X_BOOST_MODE_DISABLE
:
378 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_CONTROL
,
379 0, BQ2415X_BIT_OPA_MODE
);
381 case BQ2415X_OTG_LEVEL
:
382 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_VOLTAGE
,
384 case BQ2415X_OTG_ACTIVATE_HIGH
:
385 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_VOLTAGE
,
386 1, BQ2415X_BIT_OTG_PL
);
387 case BQ2415X_OTG_ACTIVATE_LOW
:
388 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_VOLTAGE
,
389 0, BQ2415X_BIT_OTG_PL
);
390 case BQ2415X_OTG_PIN_STATUS
:
391 return bq2415x_i2c_read_bit(bq
, BQ2415X_REG_VOLTAGE
,
393 case BQ2415X_OTG_PIN_ENABLE
:
394 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_VOLTAGE
,
395 1, BQ2415X_BIT_OTG_EN
);
396 case BQ2415X_OTG_PIN_DISABLE
:
397 return bq2415x_i2c_write_bit(bq
, BQ2415X_REG_VOLTAGE
,
398 0, BQ2415X_BIT_OTG_EN
);
400 case BQ2415X_VENDER_CODE
:
401 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_VENDER
,
402 BQ2415X_MASK_VENDER
, BQ2415X_SHIFT_VENDER
);
403 case BQ2415X_PART_NUMBER
:
404 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_VENDER
,
405 BQ2415X_MASK_PN
, BQ2415X_SHIFT_PN
);
406 case BQ2415X_REVISION
:
407 return bq2415x_i2c_read_mask(bq
, BQ2415X_REG_VENDER
,
408 BQ2415X_MASK_REVISION
, BQ2415X_SHIFT_REVISION
);
413 /* detect chip type */
414 static enum bq2415x_chip
bq2415x_detect_chip(struct bq2415x_device
*bq
)
416 struct i2c_client
*client
= to_i2c_client(bq
->dev
);
417 int ret
= bq2415x_exec_command(bq
, BQ2415X_PART_NUMBER
);
422 switch (client
->addr
) {
426 if (bq
->chip
== BQ24151A
)
431 if (bq
->chip
== BQ24150A
||
432 bq
->chip
== BQ24152
||
438 if (bq
->chip
== BQ24153A
)
450 if (bq
->chip
== BQ24156A
)
465 /* detect chip revision */
466 static int bq2415x_detect_revision(struct bq2415x_device
*bq
)
468 int ret
= bq2415x_exec_command(bq
, BQ2415X_REVISION
);
469 int chip
= bq2415x_detect_chip(bq
);
471 if (ret
< 0 || chip
< 0)
480 if (ret
>= 0 && ret
<= 3)
507 /* return chip vender code */
508 static int bq2415x_get_vender_code(struct bq2415x_device
*bq
)
512 ret
= bq2415x_exec_command(bq
, BQ2415X_VENDER_CODE
);
516 /* convert to binary */
518 ((ret
>> 1) & 0x1) * 10 +
519 ((ret
>> 2) & 0x1) * 100;
522 /* reset all chip registers to default state */
523 static void bq2415x_reset_chip(struct bq2415x_device
*bq
)
525 bq2415x_i2c_write(bq
, BQ2415X_REG_CURRENT
, BQ2415X_RESET_CURRENT
);
526 bq2415x_i2c_write(bq
, BQ2415X_REG_VOLTAGE
, BQ2415X_RESET_VOLTAGE
);
527 bq2415x_i2c_write(bq
, BQ2415X_REG_CONTROL
, BQ2415X_RESET_CONTROL
);
528 bq2415x_i2c_write(bq
, BQ2415X_REG_STATUS
, BQ2415X_RESET_STATUS
);
529 bq
->timer_error
= NULL
;
532 /**** properties functions ****/
534 /* set current limit in mA */
535 static int bq2415x_set_current_limit(struct bq2415x_device
*bq
, int mA
)
548 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_CONTROL
, val
,
549 BQ2415X_MASK_LIMIT
, BQ2415X_SHIFT_LIMIT
);
552 /* get current limit in mA */
553 static int bq2415x_get_current_limit(struct bq2415x_device
*bq
)
557 ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_CONTROL
,
558 BQ2415X_MASK_LIMIT
, BQ2415X_SHIFT_LIMIT
);
572 /* set weak battery voltage in mV */
573 static int bq2415x_set_weak_battery_voltage(struct bq2415x_device
*bq
, int mV
)
580 else if (mV
<= 3500 + 50)
582 else if (mV
<= 3600 + 50)
587 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_CONTROL
, val
,
588 BQ2415X_MASK_VLOWV
, BQ2415X_SHIFT_VLOWV
);
591 /* get weak battery voltage in mV */
592 static int bq2415x_get_weak_battery_voltage(struct bq2415x_device
*bq
)
596 ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_CONTROL
,
597 BQ2415X_MASK_VLOWV
, BQ2415X_SHIFT_VLOWV
);
600 return 100 * (34 + ret
);
603 /* set battery regulation voltage in mV */
604 static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device
*bq
,
607 int val
= (mV
/10 - 350) / 2;
611 else if (val
> 94) /* FIXME: Max is 94 or 122 ? Set max value ? */
614 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_VOLTAGE
, val
,
615 BQ2415X_MASK_VO
, BQ2415X_SHIFT_VO
);
618 /* get battery regulation voltage in mV */
619 static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device
*bq
)
621 int ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_VOLTAGE
,
622 BQ2415X_MASK_VO
, BQ2415X_SHIFT_VO
);
626 return 10 * (350 + 2*ret
);
629 /* set charge current in mA (platform data must provide resistor sense) */
630 static int bq2415x_set_charge_current(struct bq2415x_device
*bq
, int mA
)
634 if (bq
->init_data
.resistor_sense
<= 0)
637 val
= (mA
* bq
->init_data
.resistor_sense
- 37400) / 6800;
643 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_CURRENT
, val
,
644 BQ2415X_MASK_VI_CHRG
| BQ2415X_MASK_RESET
,
645 BQ2415X_SHIFT_VI_CHRG
);
648 /* get charge current in mA (platform data must provide resistor sense) */
649 static int bq2415x_get_charge_current(struct bq2415x_device
*bq
)
653 if (bq
->init_data
.resistor_sense
<= 0)
656 ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_CURRENT
,
657 BQ2415X_MASK_VI_CHRG
, BQ2415X_SHIFT_VI_CHRG
);
660 return (37400 + 6800*ret
) / bq
->init_data
.resistor_sense
;
663 /* set termination current in mA (platform data must provide resistor sense) */
664 static int bq2415x_set_termination_current(struct bq2415x_device
*bq
, int mA
)
668 if (bq
->init_data
.resistor_sense
<= 0)
671 val
= (mA
* bq
->init_data
.resistor_sense
- 3400) / 3400;
677 return bq2415x_i2c_write_mask(bq
, BQ2415X_REG_CURRENT
, val
,
678 BQ2415X_MASK_VI_TERM
| BQ2415X_MASK_RESET
,
679 BQ2415X_SHIFT_VI_TERM
);
682 /* get termination current in mA (platform data must provide resistor sense) */
683 static int bq2415x_get_termination_current(struct bq2415x_device
*bq
)
687 if (bq
->init_data
.resistor_sense
<= 0)
690 ret
= bq2415x_i2c_read_mask(bq
, BQ2415X_REG_CURRENT
,
691 BQ2415X_MASK_VI_TERM
, BQ2415X_SHIFT_VI_TERM
);
694 return (3400 + 3400*ret
) / bq
->init_data
.resistor_sense
;
697 /* set default value of property */
698 #define bq2415x_set_default_value(bq, prop) \
701 if (bq->init_data.prop != -1) \
702 ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
707 /* set default values of all properties */
708 static int bq2415x_set_defaults(struct bq2415x_device
*bq
)
710 bq2415x_exec_command(bq
, BQ2415X_BOOST_MODE_DISABLE
);
711 bq2415x_exec_command(bq
, BQ2415X_CHARGER_DISABLE
);
712 bq2415x_exec_command(bq
, BQ2415X_CHARGE_TERMINATION_DISABLE
);
714 bq2415x_set_default_value(bq
, current_limit
);
715 bq2415x_set_default_value(bq
, weak_battery_voltage
);
716 bq2415x_set_default_value(bq
, battery_regulation_voltage
);
718 if (bq
->init_data
.resistor_sense
> 0) {
719 bq2415x_set_default_value(bq
, charge_current
);
720 bq2415x_set_default_value(bq
, termination_current
);
721 bq2415x_exec_command(bq
, BQ2415X_CHARGE_TERMINATION_ENABLE
);
724 bq2415x_exec_command(bq
, BQ2415X_CHARGER_ENABLE
);
728 /**** charger mode functions ****/
730 /* set charger mode */
731 static int bq2415x_set_mode(struct bq2415x_device
*bq
, enum bq2415x_mode mode
)
737 if (mode
== BQ2415X_MODE_HOST_CHARGER
||
738 mode
== BQ2415X_MODE_DEDICATED_CHARGER
)
741 if (mode
== BQ2415X_MODE_BOOST
)
745 ret
= bq2415x_exec_command(bq
, BQ2415X_CHARGER_DISABLE
);
748 ret
= bq2415x_exec_command(bq
, BQ2415X_BOOST_MODE_DISABLE
);
754 case BQ2415X_MODE_NONE
:
755 dev_dbg(bq
->dev
, "changing mode to: N/A\n");
756 ret
= bq2415x_set_current_limit(bq
, 100);
758 case BQ2415X_MODE_HOST_CHARGER
:
759 dev_dbg(bq
->dev
, "changing mode to: Host/HUB charger\n");
760 ret
= bq2415x_set_current_limit(bq
, 500);
762 case BQ2415X_MODE_DEDICATED_CHARGER
:
763 dev_dbg(bq
->dev
, "changing mode to: Dedicated charger\n");
764 ret
= bq2415x_set_current_limit(bq
, 1800);
766 case BQ2415X_MODE_BOOST
: /* Boost mode */
767 dev_dbg(bq
->dev
, "changing mode to: Boost\n");
768 ret
= bq2415x_set_current_limit(bq
, 100);
776 ret
= bq2415x_exec_command(bq
, BQ2415X_CHARGER_ENABLE
);
778 ret
= bq2415x_exec_command(bq
, BQ2415X_BOOST_MODE_ENABLE
);
783 bq2415x_set_default_value(bq
, weak_battery_voltage
);
784 bq2415x_set_default_value(bq
, battery_regulation_voltage
);
787 sysfs_notify(&bq
->charger
.dev
->kobj
, NULL
, "mode");
793 /* hook function called by other driver which set reported mode */
794 static void bq2415x_hook_function(enum bq2415x_mode mode
, void *data
)
796 struct bq2415x_device
*bq
= data
;
801 dev_dbg(bq
->dev
, "hook function was called\n");
802 bq
->reported_mode
= mode
;
804 /* if automode is not enabled do not tell about reported_mode */
805 if (bq
->automode
< 1)
808 sysfs_notify(&bq
->charger
.dev
->kobj
, NULL
, "reported_mode");
809 bq2415x_set_mode(bq
, bq
->reported_mode
);
813 /**** timer functions ****/
815 /* enable/disable auto resetting chip timer */
816 static void bq2415x_set_autotimer(struct bq2415x_device
*bq
, int state
)
818 mutex_lock(&bq2415x_timer_mutex
);
820 if (bq
->autotimer
== state
) {
821 mutex_unlock(&bq2415x_timer_mutex
);
825 bq
->autotimer
= state
;
828 schedule_delayed_work(&bq
->work
, BQ2415X_TIMER_TIMEOUT
* HZ
);
829 bq2415x_exec_command(bq
, BQ2415X_TIMER_RESET
);
830 bq
->timer_error
= NULL
;
832 cancel_delayed_work_sync(&bq
->work
);
835 mutex_unlock(&bq2415x_timer_mutex
);
838 /* called by bq2415x_timer_work on timer error */
839 static void bq2415x_timer_error(struct bq2415x_device
*bq
, const char *msg
)
841 bq
->timer_error
= msg
;
842 sysfs_notify(&bq
->charger
.dev
->kobj
, NULL
, "timer");
843 dev_err(bq
->dev
, "%s\n", msg
);
844 if (bq
->automode
> 0)
846 bq2415x_set_mode(bq
, BQ2415X_MODE_NONE
);
847 bq2415x_set_autotimer(bq
, 0);
850 /* delayed work function for auto resetting chip timer */
851 static void bq2415x_timer_work(struct work_struct
*work
)
853 struct bq2415x_device
*bq
= container_of(work
, struct bq2415x_device
,
862 ret
= bq2415x_exec_command(bq
, BQ2415X_TIMER_RESET
);
864 bq2415x_timer_error(bq
, "Resetting timer failed");
868 boost
= bq2415x_exec_command(bq
, BQ2415X_BOOST_MODE_STATUS
);
870 bq2415x_timer_error(bq
, "Unknown error");
874 error
= bq2415x_exec_command(bq
, BQ2415X_FAULT_STATUS
);
876 bq2415x_timer_error(bq
, "Unknown error");
882 /* Non fatal errors, chip is OK */
883 case 0: /* No error */
885 case 6: /* Timer expired */
886 dev_err(bq
->dev
, "Timer expired\n");
888 case 3: /* Battery voltage too low */
889 dev_err(bq
->dev
, "Battery voltage to low\n");
892 /* Fatal errors, disable and reset chip */
893 case 1: /* Overvoltage protection (chip fried) */
894 bq2415x_timer_error(bq
,
895 "Overvoltage protection (chip fried)");
897 case 2: /* Overload */
898 bq2415x_timer_error(bq
, "Overload");
900 case 4: /* Battery overvoltage protection */
901 bq2415x_timer_error(bq
,
902 "Battery overvoltage protection");
904 case 5: /* Thermal shutdown (too hot) */
905 bq2415x_timer_error(bq
,
906 "Thermal shutdown (too hot)");
909 bq2415x_timer_error(bq
, "Unknown error");
914 /* Non fatal errors, chip is OK */
915 case 0: /* No error */
917 case 2: /* Sleep mode */
918 dev_err(bq
->dev
, "Sleep mode\n");
920 case 3: /* Poor input source */
921 dev_err(bq
->dev
, "Poor input source\n");
923 case 6: /* Timer expired */
924 dev_err(bq
->dev
, "Timer expired\n");
926 case 7: /* No battery */
927 dev_err(bq
->dev
, "No battery\n");
930 /* Fatal errors, disable and reset chip */
931 case 1: /* Overvoltage protection (chip fried) */
932 bq2415x_timer_error(bq
,
933 "Overvoltage protection (chip fried)");
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)");
946 schedule_delayed_work(&bq
->work
, BQ2415X_TIMER_TIMEOUT
* HZ
);
949 /**** power supply interface code ****/
951 static enum power_supply_property bq2415x_power_supply_props
[] = {
952 /* TODO: maybe add more power supply properties */
953 POWER_SUPPLY_PROP_STATUS
,
954 POWER_SUPPLY_PROP_MODEL_NAME
,
957 static int bq2415x_power_supply_get_property(struct power_supply
*psy
,
958 enum power_supply_property psp
,
959 union power_supply_propval
*val
)
961 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
966 case POWER_SUPPLY_PROP_STATUS
:
967 ret
= bq2415x_exec_command(bq
, BQ2415X_CHARGE_STATUS
);
970 else if (ret
== 0) /* Ready */
971 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
972 else if (ret
== 1) /* Charge in progress */
973 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
974 else if (ret
== 2) /* Charge done */
975 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
977 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
979 case POWER_SUPPLY_PROP_MODEL_NAME
:
980 val
->strval
= bq
->model
;
988 static int bq2415x_power_supply_init(struct bq2415x_device
*bq
)
994 bq
->charger
.name
= bq
->name
;
995 bq
->charger
.type
= POWER_SUPPLY_TYPE_USB
;
996 bq
->charger
.properties
= bq2415x_power_supply_props
;
997 bq
->charger
.num_properties
= ARRAY_SIZE(bq2415x_power_supply_props
);
998 bq
->charger
.get_property
= bq2415x_power_supply_get_property
;
1000 ret
= bq2415x_detect_chip(bq
);
1006 ret
= bq2415x_detect_revision(bq
);
1008 strcpy(revstr
, "unknown");
1010 sprintf(revstr
, "1.%d", ret
);
1012 bq
->model
= kasprintf(GFP_KERNEL
,
1013 "chip %s, revision %s, vender code %.3d",
1014 bq2415x_chip_name
[chip
], revstr
,
1015 bq2415x_get_vender_code(bq
));
1017 dev_err(bq
->dev
, "failed to allocate model name\n");
1021 ret
= power_supply_register(bq
->dev
, &bq
->charger
);
1030 static void bq2415x_power_supply_exit(struct bq2415x_device
*bq
)
1033 if (bq
->automode
> 0)
1035 cancel_delayed_work_sync(&bq
->work
);
1036 power_supply_unregister(&bq
->charger
);
1040 /**** additional sysfs entries for power supply interface ****/
1042 /* show *_status entries */
1043 static ssize_t
bq2415x_sysfs_show_status(struct device
*dev
,
1044 struct device_attribute
*attr
,
1047 struct power_supply
*psy
= dev_get_drvdata(dev
);
1048 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
1050 enum bq2415x_command command
;
1053 if (strcmp(attr
->attr
.name
, "otg_status") == 0)
1054 command
= BQ2415X_OTG_STATUS
;
1055 else if (strcmp(attr
->attr
.name
, "charge_status") == 0)
1056 command
= BQ2415X_CHARGE_STATUS
;
1057 else if (strcmp(attr
->attr
.name
, "boost_status") == 0)
1058 command
= BQ2415X_BOOST_STATUS
;
1059 else if (strcmp(attr
->attr
.name
, "fault_status") == 0)
1060 command
= BQ2415X_FAULT_STATUS
;
1064 ret
= bq2415x_exec_command(bq
, command
);
1067 return sprintf(buf
, "%d\n", ret
);
1072 * auto - enable auto mode
1073 * off - disable auto mode
1074 * (other values) - reset chip timer
1076 static ssize_t
bq2415x_sysfs_set_timer(struct device
*dev
,
1077 struct device_attribute
*attr
,
1081 struct power_supply
*psy
= dev_get_drvdata(dev
);
1082 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
1086 if (strncmp(buf
, "auto", 4) == 0)
1087 bq2415x_set_autotimer(bq
, 1);
1088 else if (strncmp(buf
, "off", 3) == 0)
1089 bq2415x_set_autotimer(bq
, 0);
1091 ret
= bq2415x_exec_command(bq
, BQ2415X_TIMER_RESET
);
1098 /* show timer entry (auto or off) */
1099 static ssize_t
bq2415x_sysfs_show_timer(struct device
*dev
,
1100 struct device_attribute
*attr
,
1103 struct power_supply
*psy
= dev_get_drvdata(dev
);
1104 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
1107 if (bq
->timer_error
)
1108 return sprintf(buf
, "%s\n", bq
->timer_error
);
1111 return sprintf(buf
, "auto\n");
1112 return sprintf(buf
, "off\n");
1117 * auto - if automode is supported, enable it and set mode to reported
1118 * none - disable charger and boost mode
1119 * host - charging mode for host/hub chargers (current limit 500mA)
1120 * dedicated - charging mode for dedicated chargers (unlimited current limit)
1121 * boost - disable charger and enable boost mode
1123 static ssize_t
bq2415x_sysfs_set_mode(struct device
*dev
,
1124 struct device_attribute
*attr
,
1128 struct power_supply
*psy
= dev_get_drvdata(dev
);
1129 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
1131 enum bq2415x_mode mode
;
1134 if (strncmp(buf
, "auto", 4) == 0) {
1135 if (bq
->automode
< 0)
1138 mode
= bq
->reported_mode
;
1139 } else if (strncmp(buf
, "none", 4) == 0) {
1140 if (bq
->automode
> 0)
1142 mode
= BQ2415X_MODE_NONE
;
1143 } else if (strncmp(buf
, "host", 4) == 0) {
1144 if (bq
->automode
> 0)
1146 mode
= BQ2415X_MODE_HOST_CHARGER
;
1147 } else if (strncmp(buf
, "dedicated", 9) == 0) {
1148 if (bq
->automode
> 0)
1150 mode
= BQ2415X_MODE_DEDICATED_CHARGER
;
1151 } else if (strncmp(buf
, "boost", 5) == 0) {
1152 if (bq
->automode
> 0)
1154 mode
= BQ2415X_MODE_BOOST
;
1155 } else if (strncmp(buf
, "reset", 5) == 0) {
1156 bq2415x_reset_chip(bq
);
1157 bq2415x_set_defaults(bq
);
1158 if (bq
->automode
<= 0)
1161 mode
= bq
->reported_mode
;
1166 ret
= bq2415x_set_mode(bq
, mode
);
1172 /* show mode entry (auto, none, host, dedicated or boost) */
1173 static ssize_t
bq2415x_sysfs_show_mode(struct device
*dev
,
1174 struct device_attribute
*attr
,
1177 struct power_supply
*psy
= dev_get_drvdata(dev
);
1178 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
1182 if (bq
->automode
> 0)
1183 ret
+= sprintf(buf
+ret
, "auto (");
1186 case BQ2415X_MODE_NONE
:
1187 ret
+= sprintf(buf
+ret
, "none");
1189 case BQ2415X_MODE_HOST_CHARGER
:
1190 ret
+= sprintf(buf
+ret
, "host");
1192 case BQ2415X_MODE_DEDICATED_CHARGER
:
1193 ret
+= sprintf(buf
+ret
, "dedicated");
1195 case BQ2415X_MODE_BOOST
:
1196 ret
+= sprintf(buf
+ret
, "boost");
1200 if (bq
->automode
> 0)
1201 ret
+= sprintf(buf
+ret
, ")");
1203 ret
+= sprintf(buf
+ret
, "\n");
1207 /* show reported_mode entry (none, host, dedicated or boost) */
1208 static ssize_t
bq2415x_sysfs_show_reported_mode(struct device
*dev
,
1209 struct device_attribute
*attr
,
1212 struct power_supply
*psy
= dev_get_drvdata(dev
);
1213 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
1216 if (bq
->automode
< 0)
1219 switch (bq
->reported_mode
) {
1220 case BQ2415X_MODE_NONE
:
1221 return sprintf(buf
, "none\n");
1222 case BQ2415X_MODE_HOST_CHARGER
:
1223 return sprintf(buf
, "host\n");
1224 case BQ2415X_MODE_DEDICATED_CHARGER
:
1225 return sprintf(buf
, "dedicated\n");
1226 case BQ2415X_MODE_BOOST
:
1227 return sprintf(buf
, "boost\n");
1233 /* directly set raw value to chip register, format: 'register value' */
1234 static ssize_t
bq2415x_sysfs_set_registers(struct device
*dev
,
1235 struct device_attribute
*attr
,
1239 struct power_supply
*psy
= dev_get_drvdata(dev
);
1240 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
1246 if (sscanf(buf
, "%x %x", ®
, &val
) != 2)
1249 if (reg
> 4 || val
> 255)
1252 ret
= bq2415x_i2c_write(bq
, reg
, val
);
1258 /* print value of chip register, format: 'register=value' */
1259 static ssize_t
bq2415x_sysfs_print_reg(struct bq2415x_device
*bq
,
1263 int ret
= bq2415x_i2c_read(bq
, reg
);
1266 return sprintf(buf
, "%#.2x=error %d\n", reg
, ret
);
1267 return sprintf(buf
, "%#.2x=%#.2x\n", reg
, ret
);
1270 /* show all raw values of chip register, format per line: 'register=value' */
1271 static ssize_t
bq2415x_sysfs_show_registers(struct device
*dev
,
1272 struct device_attribute
*attr
,
1275 struct power_supply
*psy
= dev_get_drvdata(dev
);
1276 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
1280 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_STATUS
, buf
+ret
);
1281 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_CONTROL
, buf
+ret
);
1282 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_VOLTAGE
, buf
+ret
);
1283 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_VENDER
, buf
+ret
);
1284 ret
+= bq2415x_sysfs_print_reg(bq
, BQ2415X_REG_CURRENT
, buf
+ret
);
1288 /* set current and voltage limit entries (in mA or mV) */
1289 static ssize_t
bq2415x_sysfs_set_limit(struct device
*dev
,
1290 struct device_attribute
*attr
,
1294 struct power_supply
*psy
= dev_get_drvdata(dev
);
1295 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
1300 if (kstrtol(buf
, 10, &val
) < 0)
1303 if (strcmp(attr
->attr
.name
, "current_limit") == 0)
1304 ret
= bq2415x_set_current_limit(bq
, val
);
1305 else if (strcmp(attr
->attr
.name
, "weak_battery_voltage") == 0)
1306 ret
= bq2415x_set_weak_battery_voltage(bq
, val
);
1307 else if (strcmp(attr
->attr
.name
, "battery_regulation_voltage") == 0)
1308 ret
= bq2415x_set_battery_regulation_voltage(bq
, val
);
1309 else if (strcmp(attr
->attr
.name
, "charge_current") == 0)
1310 ret
= bq2415x_set_charge_current(bq
, val
);
1311 else if (strcmp(attr
->attr
.name
, "termination_current") == 0)
1312 ret
= bq2415x_set_termination_current(bq
, val
);
1321 /* show current and voltage limit entries (in mA or mV) */
1322 static ssize_t
bq2415x_sysfs_show_limit(struct device
*dev
,
1323 struct device_attribute
*attr
,
1326 struct power_supply
*psy
= dev_get_drvdata(dev
);
1327 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
1331 if (strcmp(attr
->attr
.name
, "current_limit") == 0)
1332 ret
= bq2415x_get_current_limit(bq
);
1333 else if (strcmp(attr
->attr
.name
, "weak_battery_voltage") == 0)
1334 ret
= bq2415x_get_weak_battery_voltage(bq
);
1335 else if (strcmp(attr
->attr
.name
, "battery_regulation_voltage") == 0)
1336 ret
= bq2415x_get_battery_regulation_voltage(bq
);
1337 else if (strcmp(attr
->attr
.name
, "charge_current") == 0)
1338 ret
= bq2415x_get_charge_current(bq
);
1339 else if (strcmp(attr
->attr
.name
, "termination_current") == 0)
1340 ret
= bq2415x_get_termination_current(bq
);
1346 return sprintf(buf
, "%d\n", ret
);
1349 /* set *_enable entries */
1350 static ssize_t
bq2415x_sysfs_set_enable(struct device
*dev
,
1351 struct device_attribute
*attr
,
1355 struct power_supply
*psy
= dev_get_drvdata(dev
);
1356 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
1358 enum bq2415x_command command
;
1362 if (kstrtol(buf
, 10, &val
) < 0)
1365 if (strcmp(attr
->attr
.name
, "charge_termination_enable") == 0)
1366 command
= val
? BQ2415X_CHARGE_TERMINATION_ENABLE
:
1367 BQ2415X_CHARGE_TERMINATION_DISABLE
;
1368 else if (strcmp(attr
->attr
.name
, "high_impedance_enable") == 0)
1369 command
= val
? BQ2415X_HIGH_IMPEDANCE_ENABLE
:
1370 BQ2415X_HIGH_IMPEDANCE_DISABLE
;
1371 else if (strcmp(attr
->attr
.name
, "otg_pin_enable") == 0)
1372 command
= val
? BQ2415X_OTG_PIN_ENABLE
:
1373 BQ2415X_OTG_PIN_DISABLE
;
1374 else if (strcmp(attr
->attr
.name
, "stat_pin_enable") == 0)
1375 command
= val
? BQ2415X_STAT_PIN_ENABLE
:
1376 BQ2415X_STAT_PIN_DISABLE
;
1380 ret
= bq2415x_exec_command(bq
, command
);
1386 /* show *_enable entries */
1387 static ssize_t
bq2415x_sysfs_show_enable(struct device
*dev
,
1388 struct device_attribute
*attr
,
1391 struct power_supply
*psy
= dev_get_drvdata(dev
);
1392 struct bq2415x_device
*bq
= container_of(psy
, struct bq2415x_device
,
1394 enum bq2415x_command command
;
1397 if (strcmp(attr
->attr
.name
, "charge_termination_enable") == 0)
1398 command
= BQ2415X_CHARGE_TERMINATION_STATUS
;
1399 else if (strcmp(attr
->attr
.name
, "high_impedance_enable") == 0)
1400 command
= BQ2415X_HIGH_IMPEDANCE_STATUS
;
1401 else if (strcmp(attr
->attr
.name
, "otg_pin_enable") == 0)
1402 command
= BQ2415X_OTG_PIN_STATUS
;
1403 else if (strcmp(attr
->attr
.name
, "stat_pin_enable") == 0)
1404 command
= BQ2415X_STAT_PIN_STATUS
;
1408 ret
= bq2415x_exec_command(bq
, command
);
1411 return sprintf(buf
, "%d\n", ret
);
1414 static DEVICE_ATTR(current_limit
, S_IWUSR
| S_IRUGO
,
1415 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1416 static DEVICE_ATTR(weak_battery_voltage
, S_IWUSR
| S_IRUGO
,
1417 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1418 static DEVICE_ATTR(battery_regulation_voltage
, S_IWUSR
| S_IRUGO
,
1419 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1420 static DEVICE_ATTR(charge_current
, S_IWUSR
| S_IRUGO
,
1421 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1422 static DEVICE_ATTR(termination_current
, S_IWUSR
| S_IRUGO
,
1423 bq2415x_sysfs_show_limit
, bq2415x_sysfs_set_limit
);
1425 static DEVICE_ATTR(charge_termination_enable
, S_IWUSR
| S_IRUGO
,
1426 bq2415x_sysfs_show_enable
, bq2415x_sysfs_set_enable
);
1427 static DEVICE_ATTR(high_impedance_enable
, S_IWUSR
| S_IRUGO
,
1428 bq2415x_sysfs_show_enable
, bq2415x_sysfs_set_enable
);
1429 static DEVICE_ATTR(otg_pin_enable
, S_IWUSR
| S_IRUGO
,
1430 bq2415x_sysfs_show_enable
, bq2415x_sysfs_set_enable
);
1431 static DEVICE_ATTR(stat_pin_enable
, S_IWUSR
| S_IRUGO
,
1432 bq2415x_sysfs_show_enable
, bq2415x_sysfs_set_enable
);
1434 static DEVICE_ATTR(reported_mode
, S_IRUGO
,
1435 bq2415x_sysfs_show_reported_mode
, NULL
);
1436 static DEVICE_ATTR(mode
, S_IWUSR
| S_IRUGO
,
1437 bq2415x_sysfs_show_mode
, bq2415x_sysfs_set_mode
);
1438 static DEVICE_ATTR(timer
, S_IWUSR
| S_IRUGO
,
1439 bq2415x_sysfs_show_timer
, bq2415x_sysfs_set_timer
);
1441 static DEVICE_ATTR(registers
, S_IWUSR
| S_IRUGO
,
1442 bq2415x_sysfs_show_registers
, bq2415x_sysfs_set_registers
);
1444 static DEVICE_ATTR(otg_status
, S_IRUGO
, bq2415x_sysfs_show_status
, NULL
);
1445 static DEVICE_ATTR(charge_status
, S_IRUGO
, bq2415x_sysfs_show_status
, NULL
);
1446 static DEVICE_ATTR(boost_status
, S_IRUGO
, bq2415x_sysfs_show_status
, NULL
);
1447 static DEVICE_ATTR(fault_status
, S_IRUGO
, bq2415x_sysfs_show_status
, NULL
);
1449 static struct attribute
*bq2415x_sysfs_attributes
[] = {
1451 * TODO: some (appropriate) of these attrs should be switched to
1452 * use power supply class props.
1454 &dev_attr_current_limit
.attr
,
1455 &dev_attr_weak_battery_voltage
.attr
,
1456 &dev_attr_battery_regulation_voltage
.attr
,
1457 &dev_attr_charge_current
.attr
,
1458 &dev_attr_termination_current
.attr
,
1460 &dev_attr_charge_termination_enable
.attr
,
1461 &dev_attr_high_impedance_enable
.attr
,
1462 &dev_attr_otg_pin_enable
.attr
,
1463 &dev_attr_stat_pin_enable
.attr
,
1465 &dev_attr_reported_mode
.attr
,
1466 &dev_attr_mode
.attr
,
1467 &dev_attr_timer
.attr
,
1469 &dev_attr_registers
.attr
,
1471 &dev_attr_otg_status
.attr
,
1472 &dev_attr_charge_status
.attr
,
1473 &dev_attr_boost_status
.attr
,
1474 &dev_attr_fault_status
.attr
,
1478 static const struct attribute_group bq2415x_sysfs_attr_group
= {
1479 .attrs
= bq2415x_sysfs_attributes
,
1482 static int bq2415x_sysfs_init(struct bq2415x_device
*bq
)
1484 return sysfs_create_group(&bq
->charger
.dev
->kobj
,
1485 &bq2415x_sysfs_attr_group
);
1488 static void bq2415x_sysfs_exit(struct bq2415x_device
*bq
)
1490 sysfs_remove_group(&bq
->charger
.dev
->kobj
, &bq2415x_sysfs_attr_group
);
1493 /* main bq2415x probe function */
1494 static int bq2415x_probe(struct i2c_client
*client
,
1495 const struct i2c_device_id
*id
)
1500 struct bq2415x_device
*bq
;
1502 if (!client
->dev
.platform_data
) {
1503 dev_err(&client
->dev
, "platform data not set\n");
1507 /* Get new ID for the new device */
1508 ret
= idr_pre_get(&bq2415x_id
, GFP_KERNEL
);
1512 mutex_lock(&bq2415x_id_mutex
);
1513 ret
= idr_get_new(&bq2415x_id
, client
, &num
);
1514 mutex_unlock(&bq2415x_id_mutex
);
1519 name
= kasprintf(GFP_KERNEL
, "%s-%d", id
->name
, num
);
1521 dev_err(&client
->dev
, "failed to allocate device name\n");
1526 bq
= kzalloc(sizeof(*bq
), GFP_KERNEL
);
1528 dev_err(&client
->dev
, "failed to allocate device data\n");
1533 i2c_set_clientdata(client
, bq
);
1536 bq
->dev
= &client
->dev
;
1537 bq
->chip
= id
->driver_data
;
1539 bq
->mode
= BQ2415X_MODE_NONE
;
1540 bq
->reported_mode
= BQ2415X_MODE_NONE
;
1544 memcpy(&bq
->init_data
, client
->dev
.platform_data
,
1545 sizeof(bq
->init_data
));
1547 bq2415x_reset_chip(bq
);
1549 ret
= bq2415x_power_supply_init(bq
);
1551 dev_err(bq
->dev
, "failed to register power supply: %d\n", ret
);
1555 ret
= bq2415x_sysfs_init(bq
);
1557 dev_err(bq
->dev
, "failed to create sysfs entries: %d\n", ret
);
1561 ret
= bq2415x_set_defaults(bq
);
1563 dev_err(bq
->dev
, "failed to set default values: %d\n", ret
);
1567 if (bq
->init_data
.set_mode_hook
) {
1568 if (bq
->init_data
.set_mode_hook(
1569 bq2415x_hook_function
, bq
)) {
1571 bq2415x_set_mode(bq
, bq
->reported_mode
);
1572 dev_info(bq
->dev
, "automode enabled\n");
1575 dev_info(bq
->dev
, "automode failed\n");
1579 dev_info(bq
->dev
, "automode not supported\n");
1582 INIT_DELAYED_WORK(&bq
->work
, bq2415x_timer_work
);
1583 bq2415x_set_autotimer(bq
, 1);
1585 dev_info(bq
->dev
, "driver registered\n");
1589 bq2415x_sysfs_exit(bq
);
1591 bq2415x_power_supply_exit(bq
);
1597 mutex_lock(&bq2415x_id_mutex
);
1598 idr_remove(&bq2415x_id
, num
);
1599 mutex_unlock(&bq2415x_id_mutex
);
1604 /* main bq2415x remove function */
1606 static int bq2415x_remove(struct i2c_client
*client
)
1608 struct bq2415x_device
*bq
= i2c_get_clientdata(client
);
1610 if (bq
->init_data
.set_mode_hook
)
1611 bq
->init_data
.set_mode_hook(NULL
, NULL
);
1613 bq2415x_sysfs_exit(bq
);
1614 bq2415x_power_supply_exit(bq
);
1616 bq2415x_reset_chip(bq
);
1618 mutex_lock(&bq2415x_id_mutex
);
1619 idr_remove(&bq2415x_id
, bq
->id
);
1620 mutex_unlock(&bq2415x_id_mutex
);
1622 dev_info(bq
->dev
, "driver unregistered\n");
1630 static const struct i2c_device_id bq2415x_i2c_id_table
[] = {
1631 { "bq2415x", BQUNKNOWN
},
1632 { "bq24150", BQ24150
},
1633 { "bq24150a", BQ24150A
},
1634 { "bq24151", BQ24151
},
1635 { "bq24151a", BQ24151A
},
1636 { "bq24152", BQ24152
},
1637 { "bq24153", BQ24153
},
1638 { "bq24153a", BQ24153A
},
1639 { "bq24155", BQ24155
},
1640 { "bq24156", BQ24156
},
1641 { "bq24156a", BQ24156A
},
1642 { "bq24158", BQ24158
},
1645 MODULE_DEVICE_TABLE(i2c
, bq2415x_i2c_id_table
);
1647 static struct i2c_driver bq2415x_driver
= {
1649 .name
= "bq2415x-charger",
1651 .probe
= bq2415x_probe
,
1652 .remove
= bq2415x_remove
,
1653 .id_table
= bq2415x_i2c_id_table
,
1656 static int __init
bq2415x_init(void)
1658 return i2c_add_driver(&bq2415x_driver
);
1660 module_init(bq2415x_init
);
1662 static void __exit
bq2415x_exit(void)
1664 i2c_del_driver(&bq2415x_driver
);
1666 module_exit(bq2415x_exit
);
1668 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
1669 MODULE_DESCRIPTION("bq2415x charger driver");
1670 MODULE_LICENSE("GPL");