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/kernel.h>
32 #include <linux/module.h>
33 #include <linux/param.h>
34 #include <linux/err.h>
35 #include <linux/workqueue.h>
36 #include <linux/sysfs.h>
37 #include <linux/platform_device.h>
38 #include <linux/power_supply.h>
39 #include <linux/idr.h>
40 #include <linux/i2c.h>
41 #include <linux/slab.h>
43 #include <linux/power/bq2415x_charger.h>
45 /* timeout for resetting chip timer */
46 #define BQ2415X_TIMER_TIMEOUT 10
48 #define BQ2415X_REG_STATUS 0x00
49 #define BQ2415X_REG_CONTROL 0x01
50 #define BQ2415X_REG_VOLTAGE 0x02
51 #define BQ2415X_REG_VENDER 0x03
52 #define BQ2415X_REG_CURRENT 0x04
54 /* reset state for all registers */
55 #define BQ2415X_RESET_STATUS BIT(6)
56 #define BQ2415X_RESET_CONTROL (BIT(4)|BIT(5))
57 #define BQ2415X_RESET_VOLTAGE (BIT(1)|BIT(3))
58 #define BQ2415X_RESET_CURRENT (BIT(0)|BIT(3)|BIT(7))
61 #define BQ2415X_BIT_TMR_RST 7
62 #define BQ2415X_BIT_OTG 7
63 #define BQ2415X_BIT_EN_STAT 6
64 #define BQ2415X_MASK_STAT (BIT(4)|BIT(5))
65 #define BQ2415X_SHIFT_STAT 4
66 #define BQ2415X_BIT_BOOST 3
67 #define BQ2415X_MASK_FAULT (BIT(0)|BIT(1)|BIT(2))
68 #define BQ2415X_SHIFT_FAULT 0
70 /* control register */
71 #define BQ2415X_MASK_LIMIT (BIT(6)|BIT(7))
72 #define BQ2415X_SHIFT_LIMIT 6
73 #define BQ2415X_MASK_VLOWV (BIT(4)|BIT(5))
74 #define BQ2415X_SHIFT_VLOWV 4
75 #define BQ2415X_BIT_TE 3
76 #define BQ2415X_BIT_CE 2
77 #define BQ2415X_BIT_HZ_MODE 1
78 #define BQ2415X_BIT_OPA_MODE 0
80 /* voltage register */
81 #define BQ2415X_MASK_VO (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
82 #define BQ2415X_SHIFT_VO 2
83 #define BQ2415X_BIT_OTG_PL 1
84 #define BQ2415X_BIT_OTG_EN 0
87 #define BQ2415X_MASK_VENDER (BIT(5)|BIT(6)|BIT(7))
88 #define BQ2415X_SHIFT_VENDER 5
89 #define BQ2415X_MASK_PN (BIT(3)|BIT(4))
90 #define BQ2415X_SHIFT_PN 3
91 #define BQ2415X_MASK_REVISION (BIT(0)|BIT(1)|BIT(2))
92 #define BQ2415X_SHIFT_REVISION 0
94 /* current register */
95 #define BQ2415X_MASK_RESET BIT(7)
96 #define BQ2415X_MASK_VI_CHRG (BIT(4)|BIT(5)|BIT(6))
97 #define BQ2415X_SHIFT_VI_CHRG 4
99 #define BQ2415X_MASK_VI_TERM (BIT(0)|BIT(1)|BIT(2))
100 #define BQ2415X_SHIFT_VI_TERM 0
103 enum bq2415x_command {
106 BQ2415X_STAT_PIN_STATUS,
107 BQ2415X_STAT_PIN_ENABLE,
108 BQ2415X_STAT_PIN_DISABLE,
109 BQ2415X_CHARGE_STATUS,
110 BQ2415X_BOOST_STATUS,
111 BQ2415X_FAULT_STATUS,
113 BQ2415X_CHARGE_TERMINATION_STATUS,
114 BQ2415X_CHARGE_TERMINATION_ENABLE,
115 BQ2415X_CHARGE_TERMINATION_DISABLE,
116 BQ2415X_CHARGER_STATUS,
117 BQ2415X_CHARGER_ENABLE,
118 BQ2415X_CHARGER_DISABLE,
119 BQ2415X_HIGH_IMPEDANCE_STATUS,
120 BQ2415X_HIGH_IMPEDANCE_ENABLE,
121 BQ2415X_HIGH_IMPEDANCE_DISABLE,
122 BQ2415X_BOOST_MODE_STATUS,
123 BQ2415X_BOOST_MODE_ENABLE,
124 BQ2415X_BOOST_MODE_DISABLE,
127 BQ2415X_OTG_ACTIVATE_HIGH,
128 BQ2415X_OTG_ACTIVATE_LOW,
129 BQ2415X_OTG_PIN_STATUS,
130 BQ2415X_OTG_PIN_ENABLE,
131 BQ2415X_OTG_PIN_DISABLE,
153 static char *bq2415x_chip_name[] = {
168 struct bq2415x_device {
170 struct bq2415x_platform_data init_data;
171 struct power_supply charger;
172 struct delayed_work work;
173 enum bq2415x_mode reported_mode;/* mode reported by hook function */
174 enum bq2415x_mode mode; /* current configured mode */
175 enum bq2415x_chip chip;
176 const char *timer_error;
179 int autotimer; /* 1 - if driver automatically reset timer, 0 - not */
180 int automode; /* 1 - enabled, 0 - disabled; -1 - not supported */
184 /* each registered chip must have unique id */
185 static DEFINE_IDR(bq2415x_id);
187 static DEFINE_MUTEX(bq2415x_id_mutex);
188 static DEFINE_MUTEX(bq2415x_timer_mutex);
189 static DEFINE_MUTEX(bq2415x_i2c_mutex);
191 /**** i2c read functions ****/
193 /* read value from register */
194 static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg)
196 struct i2c_client *client = to_i2c_client(bq->dev);
197 struct i2c_msg msg[2];
201 if (!client->adapter)
204 msg[0].addr = client->addr;
207 msg[0].len = sizeof(reg);
208 msg[1].addr = client->addr;
209 msg[1].flags = I2C_M_RD;
211 msg[1].len = sizeof(val);
213 mutex_lock(&bq2415x_i2c_mutex);
214 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
215 mutex_unlock(&bq2415x_i2c_mutex);
223 /* read value from register, apply mask and right shift it */
224 static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
232 ret = bq2415x_i2c_read(bq, reg);
235 return (ret & mask) >> shift;
238 /* read value from register and return one specified bit */
239 static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit)
243 return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit);
246 /**** i2c write functions ****/
248 /* write value to register */
249 static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val)
251 struct i2c_client *client = to_i2c_client(bq->dev);
252 struct i2c_msg msg[1];
259 msg[0].addr = client->addr;
262 msg[0].len = ARRAY_SIZE(data);
264 mutex_lock(&bq2415x_i2c_mutex);
265 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
266 mutex_unlock(&bq2415x_i2c_mutex);
268 /* i2c_transfer returns number of messages transferred */
277 /* read value from register, change it with mask left shifted and write back */
278 static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val,
286 ret = bq2415x_i2c_read(bq, reg);
293 return bq2415x_i2c_write(bq, reg, ret);
296 /* change only one bit in register */
297 static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg,
302 return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit);
305 /**** global functions ****/
307 /* exec command function */
308 static int bq2415x_exec_command(struct bq2415x_device *bq,
309 enum bq2415x_command command)
314 case BQ2415X_TIMER_RESET:
315 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS,
316 1, BQ2415X_BIT_TMR_RST);
317 case BQ2415X_OTG_STATUS:
318 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
320 case BQ2415X_STAT_PIN_STATUS:
321 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
322 BQ2415X_BIT_EN_STAT);
323 case BQ2415X_STAT_PIN_ENABLE:
324 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1,
325 BQ2415X_BIT_EN_STAT);
326 case BQ2415X_STAT_PIN_DISABLE:
327 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0,
328 BQ2415X_BIT_EN_STAT);
329 case BQ2415X_CHARGE_STATUS:
330 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
331 BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT);
332 case BQ2415X_BOOST_STATUS:
333 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
335 case BQ2415X_FAULT_STATUS:
336 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
337 BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT);
339 case BQ2415X_CHARGE_TERMINATION_STATUS:
340 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
342 case BQ2415X_CHARGE_TERMINATION_ENABLE:
343 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
345 case BQ2415X_CHARGE_TERMINATION_DISABLE:
346 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
348 case BQ2415X_CHARGER_STATUS:
349 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)
430 if (bq->chip == BQ24150A ||
431 bq->chip == BQ24152 ||
437 if (bq->chip == BQ24153A)
449 if (bq->chip == BQ24156A)
464 /* detect chip revision */
465 static int bq2415x_detect_revision(struct bq2415x_device *bq)
467 int ret = bq2415x_exec_command(bq, BQ2415X_REVISION);
468 int chip = bq2415x_detect_chip(bq);
470 if (ret < 0 || chip < 0)
479 if (ret >= 0 && ret <= 3)
506 /* return chip vender code */
507 static int bq2415x_get_vender_code(struct bq2415x_device *bq)
511 ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
515 /* convert to binary */
517 ((ret >> 1) & 0x1) * 10 +
518 ((ret >> 2) & 0x1) * 100;
521 /* reset all chip registers to default state */
522 static void bq2415x_reset_chip(struct bq2415x_device *bq)
524 bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT);
525 bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE);
526 bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL);
527 bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS);
528 bq->timer_error = NULL;
531 /**** properties functions ****/
533 /* set current limit in mA */
534 static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA)
547 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
548 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
551 /* get current limit in mA */
552 static int bq2415x_get_current_limit(struct bq2415x_device *bq)
556 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
557 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
571 /* set weak battery voltage in mV */
572 static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
579 else if (mV <= 3500 + 50)
581 else if (mV <= 3600 + 50)
586 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
587 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
590 /* get weak battery voltage in mV */
591 static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq)
595 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
596 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
599 return 100 * (34 + ret);
602 /* set battery regulation voltage in mV */
603 static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
606 int val = (mV/10 - 350) / 2;
609 * According to datasheet, maximum battery regulation voltage is
610 * 4440mV which is b101111 = 47.
617 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val,
618 BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
621 /* get battery regulation voltage in mV */
622 static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq)
624 int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE,
625 BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
629 return 10 * (350 + 2*ret);
632 /* set charge current in mA (platform data must provide resistor sense) */
633 static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA)
637 if (bq->init_data.resistor_sense <= 0)
640 val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
646 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
647 BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET,
648 BQ2415X_SHIFT_VI_CHRG);
651 /* get charge current in mA (platform data must provide resistor sense) */
652 static int bq2415x_get_charge_current(struct bq2415x_device *bq)
656 if (bq->init_data.resistor_sense <= 0)
659 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
660 BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG);
663 return (37400 + 6800*ret) / bq->init_data.resistor_sense;
666 /* set termination current in mA (platform data must provide resistor sense) */
667 static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA)
671 if (bq->init_data.resistor_sense <= 0)
674 val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
680 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
681 BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET,
682 BQ2415X_SHIFT_VI_TERM);
685 /* get termination current in mA (platform data must provide resistor sense) */
686 static int bq2415x_get_termination_current(struct bq2415x_device *bq)
690 if (bq->init_data.resistor_sense <= 0)
693 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
694 BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM);
697 return (3400 + 3400*ret) / bq->init_data.resistor_sense;
700 /* set default value of property */
701 #define bq2415x_set_default_value(bq, prop) \
704 if (bq->init_data.prop != -1) \
705 ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
710 /* set default values of all properties */
711 static int bq2415x_set_defaults(struct bq2415x_device *bq)
713 bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
714 bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
715 bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE);
717 bq2415x_set_default_value(bq, current_limit);
718 bq2415x_set_default_value(bq, weak_battery_voltage);
719 bq2415x_set_default_value(bq, battery_regulation_voltage);
721 if (bq->init_data.resistor_sense > 0) {
722 bq2415x_set_default_value(bq, charge_current);
723 bq2415x_set_default_value(bq, termination_current);
724 bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE);
727 bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
731 /**** charger mode functions ****/
733 /* set charger mode */
734 static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode)
740 if (mode == BQ2415X_MODE_BOOST)
742 else if (mode != BQ2415X_MODE_OFF)
746 ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
749 ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
755 case BQ2415X_MODE_OFF:
756 dev_dbg(bq->dev, "changing mode to: Offline\n");
757 ret = bq2415x_set_current_limit(bq, 100);
759 case BQ2415X_MODE_NONE:
760 dev_dbg(bq->dev, "changing mode to: N/A\n");
761 ret = bq2415x_set_current_limit(bq, 100);
763 case BQ2415X_MODE_HOST_CHARGER:
764 dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n");
765 ret = bq2415x_set_current_limit(bq, 500);
767 case BQ2415X_MODE_DEDICATED_CHARGER:
768 dev_dbg(bq->dev, "changing mode to: Dedicated charger\n");
769 ret = bq2415x_set_current_limit(bq, 1800);
771 case BQ2415X_MODE_BOOST: /* Boost mode */
772 dev_dbg(bq->dev, "changing mode to: Boost\n");
773 ret = bq2415x_set_current_limit(bq, 100);
781 ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
783 ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE);
788 bq2415x_set_default_value(bq, weak_battery_voltage);
789 bq2415x_set_default_value(bq, battery_regulation_voltage);
792 sysfs_notify(&bq->charger.dev->kobj, NULL, "mode");
798 /* hook function called by other driver which set reported mode */
799 static void bq2415x_hook_function(enum bq2415x_mode mode, void *data)
801 struct bq2415x_device *bq = data;
806 dev_dbg(bq->dev, "hook function was called\n");
807 bq->reported_mode = mode;
809 /* if automode is not enabled do not tell about reported_mode */
810 if (bq->automode < 1)
813 sysfs_notify(&bq->charger.dev->kobj, NULL, "reported_mode");
814 bq2415x_set_mode(bq, bq->reported_mode);
818 /**** timer functions ****/
820 /* enable/disable auto resetting chip timer */
821 static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
823 mutex_lock(&bq2415x_timer_mutex);
825 if (bq->autotimer == state) {
826 mutex_unlock(&bq2415x_timer_mutex);
830 bq->autotimer = state;
833 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
834 bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
835 bq->timer_error = NULL;
837 cancel_delayed_work_sync(&bq->work);
840 mutex_unlock(&bq2415x_timer_mutex);
843 /* called by bq2415x_timer_work on timer error */
844 static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
846 bq->timer_error = msg;
847 sysfs_notify(&bq->charger.dev->kobj, NULL, "timer");
848 dev_err(bq->dev, "%s\n", msg);
849 if (bq->automode > 0)
851 bq2415x_set_mode(bq, BQ2415X_MODE_OFF);
852 bq2415x_set_autotimer(bq, 0);
855 /* delayed work function for auto resetting chip timer */
856 static void bq2415x_timer_work(struct work_struct *work)
858 struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
867 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
869 bq2415x_timer_error(bq, "Resetting timer failed");
873 boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
875 bq2415x_timer_error(bq, "Unknown error");
879 error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
881 bq2415x_timer_error(bq, "Unknown error");
887 /* Non fatal errors, chip is OK */
888 case 0: /* No error */
890 case 6: /* Timer expired */
891 dev_err(bq->dev, "Timer expired\n");
893 case 3: /* Battery voltage too low */
894 dev_err(bq->dev, "Battery voltage to low\n");
897 /* Fatal errors, disable and reset chip */
898 case 1: /* Overvoltage protection (chip fried) */
899 bq2415x_timer_error(bq,
900 "Overvoltage protection (chip fried)");
902 case 2: /* Overload */
903 bq2415x_timer_error(bq, "Overload");
905 case 4: /* Battery overvoltage protection */
906 bq2415x_timer_error(bq,
907 "Battery overvoltage protection");
909 case 5: /* Thermal shutdown (too hot) */
910 bq2415x_timer_error(bq,
911 "Thermal shutdown (too hot)");
914 bq2415x_timer_error(bq, "Unknown error");
919 /* Non fatal errors, chip is OK */
920 case 0: /* No error */
922 case 2: /* Sleep mode */
923 dev_err(bq->dev, "Sleep mode\n");
925 case 3: /* Poor input source */
926 dev_err(bq->dev, "Poor input source\n");
928 case 6: /* Timer expired */
929 dev_err(bq->dev, "Timer expired\n");
931 case 7: /* No battery */
932 dev_err(bq->dev, "No battery\n");
935 /* Fatal errors, disable and reset chip */
936 case 1: /* Overvoltage protection (chip fried) */
937 bq2415x_timer_error(bq,
938 "Overvoltage protection (chip fried)");
940 case 4: /* Battery overvoltage protection */
941 bq2415x_timer_error(bq,
942 "Battery overvoltage protection");
944 case 5: /* Thermal shutdown (too hot) */
945 bq2415x_timer_error(bq,
946 "Thermal shutdown (too hot)");
951 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
954 /**** power supply interface code ****/
956 static enum power_supply_property bq2415x_power_supply_props[] = {
957 /* TODO: maybe add more power supply properties */
958 POWER_SUPPLY_PROP_STATUS,
959 POWER_SUPPLY_PROP_MODEL_NAME,
962 static int bq2415x_power_supply_get_property(struct power_supply *psy,
963 enum power_supply_property psp,
964 union power_supply_propval *val)
966 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
971 case POWER_SUPPLY_PROP_STATUS:
972 ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
975 else if (ret == 0) /* Ready */
976 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
977 else if (ret == 1) /* Charge in progress */
978 val->intval = POWER_SUPPLY_STATUS_CHARGING;
979 else if (ret == 2) /* Charge done */
980 val->intval = POWER_SUPPLY_STATUS_FULL;
982 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
984 case POWER_SUPPLY_PROP_MODEL_NAME:
985 val->strval = bq->model;
993 static int bq2415x_power_supply_init(struct bq2415x_device *bq)
999 bq->charger.name = bq->name;
1000 bq->charger.type = POWER_SUPPLY_TYPE_USB;
1001 bq->charger.properties = bq2415x_power_supply_props;
1002 bq->charger.num_properties = ARRAY_SIZE(bq2415x_power_supply_props);
1003 bq->charger.get_property = bq2415x_power_supply_get_property;
1005 ret = bq2415x_detect_chip(bq);
1011 ret = bq2415x_detect_revision(bq);
1013 strcpy(revstr, "unknown");
1015 sprintf(revstr, "1.%d", ret);
1017 bq->model = kasprintf(GFP_KERNEL,
1018 "chip %s, revision %s, vender code %.3d",
1019 bq2415x_chip_name[chip], revstr,
1020 bq2415x_get_vender_code(bq));
1022 dev_err(bq->dev, "failed to allocate model name\n");
1026 ret = power_supply_register(bq->dev, &bq->charger);
1035 static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
1038 if (bq->automode > 0)
1040 cancel_delayed_work_sync(&bq->work);
1041 power_supply_unregister(&bq->charger);
1045 /**** additional sysfs entries for power supply interface ****/
1047 /* show *_status entries */
1048 static ssize_t bq2415x_sysfs_show_status(struct device *dev,
1049 struct device_attribute *attr,
1052 struct power_supply *psy = dev_get_drvdata(dev);
1053 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1055 enum bq2415x_command command;
1058 if (strcmp(attr->attr.name, "otg_status") == 0)
1059 command = BQ2415X_OTG_STATUS;
1060 else if (strcmp(attr->attr.name, "charge_status") == 0)
1061 command = BQ2415X_CHARGE_STATUS;
1062 else if (strcmp(attr->attr.name, "boost_status") == 0)
1063 command = BQ2415X_BOOST_STATUS;
1064 else if (strcmp(attr->attr.name, "fault_status") == 0)
1065 command = BQ2415X_FAULT_STATUS;
1069 ret = bq2415x_exec_command(bq, command);
1072 return sprintf(buf, "%d\n", ret);
1077 * auto - enable auto mode
1078 * off - disable auto mode
1079 * (other values) - reset chip timer
1081 static ssize_t bq2415x_sysfs_set_timer(struct device *dev,
1082 struct device_attribute *attr,
1086 struct power_supply *psy = dev_get_drvdata(dev);
1087 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1091 if (strncmp(buf, "auto", 4) == 0)
1092 bq2415x_set_autotimer(bq, 1);
1093 else if (strncmp(buf, "off", 3) == 0)
1094 bq2415x_set_autotimer(bq, 0);
1096 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
1103 /* show timer entry (auto or off) */
1104 static ssize_t bq2415x_sysfs_show_timer(struct device *dev,
1105 struct device_attribute *attr,
1108 struct power_supply *psy = dev_get_drvdata(dev);
1109 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1112 if (bq->timer_error)
1113 return sprintf(buf, "%s\n", bq->timer_error);
1116 return sprintf(buf, "auto\n");
1117 return sprintf(buf, "off\n");
1122 * auto - if automode is supported, enable it and set mode to reported
1123 * none - disable charger and boost mode
1124 * host - charging mode for host/hub chargers (current limit 500mA)
1125 * dedicated - charging mode for dedicated chargers (unlimited current limit)
1126 * boost - disable charger and enable boost mode
1128 static ssize_t bq2415x_sysfs_set_mode(struct device *dev,
1129 struct device_attribute *attr,
1133 struct power_supply *psy = dev_get_drvdata(dev);
1134 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1136 enum bq2415x_mode mode;
1139 if (strncmp(buf, "auto", 4) == 0) {
1140 if (bq->automode < 0)
1143 mode = bq->reported_mode;
1144 } else if (strncmp(buf, "off", 3) == 0) {
1145 if (bq->automode > 0)
1147 mode = BQ2415X_MODE_OFF;
1148 } else if (strncmp(buf, "none", 4) == 0) {
1149 if (bq->automode > 0)
1151 mode = BQ2415X_MODE_NONE;
1152 } else if (strncmp(buf, "host", 4) == 0) {
1153 if (bq->automode > 0)
1155 mode = BQ2415X_MODE_HOST_CHARGER;
1156 } else if (strncmp(buf, "dedicated", 9) == 0) {
1157 if (bq->automode > 0)
1159 mode = BQ2415X_MODE_DEDICATED_CHARGER;
1160 } else if (strncmp(buf, "boost", 5) == 0) {
1161 if (bq->automode > 0)
1163 mode = BQ2415X_MODE_BOOST;
1164 } else if (strncmp(buf, "reset", 5) == 0) {
1165 bq2415x_reset_chip(bq);
1166 bq2415x_set_defaults(bq);
1167 if (bq->automode <= 0)
1170 mode = bq->reported_mode;
1175 ret = bq2415x_set_mode(bq, mode);
1181 /* show mode entry (auto, none, host, dedicated or boost) */
1182 static ssize_t bq2415x_sysfs_show_mode(struct device *dev,
1183 struct device_attribute *attr,
1186 struct power_supply *psy = dev_get_drvdata(dev);
1187 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1191 if (bq->automode > 0)
1192 ret += sprintf(buf+ret, "auto (");
1195 case BQ2415X_MODE_OFF:
1196 ret += sprintf(buf+ret, "off");
1198 case BQ2415X_MODE_NONE:
1199 ret += sprintf(buf+ret, "none");
1201 case BQ2415X_MODE_HOST_CHARGER:
1202 ret += sprintf(buf+ret, "host");
1204 case BQ2415X_MODE_DEDICATED_CHARGER:
1205 ret += sprintf(buf+ret, "dedicated");
1207 case BQ2415X_MODE_BOOST:
1208 ret += sprintf(buf+ret, "boost");
1212 if (bq->automode > 0)
1213 ret += sprintf(buf+ret, ")");
1215 ret += sprintf(buf+ret, "\n");
1219 /* show reported_mode entry (none, host, dedicated or boost) */
1220 static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
1221 struct device_attribute *attr,
1224 struct power_supply *psy = dev_get_drvdata(dev);
1225 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1228 if (bq->automode < 0)
1231 switch (bq->reported_mode) {
1232 case BQ2415X_MODE_OFF:
1233 return sprintf(buf, "off\n");
1234 case BQ2415X_MODE_NONE:
1235 return sprintf(buf, "none\n");
1236 case BQ2415X_MODE_HOST_CHARGER:
1237 return sprintf(buf, "host\n");
1238 case BQ2415X_MODE_DEDICATED_CHARGER:
1239 return sprintf(buf, "dedicated\n");
1240 case BQ2415X_MODE_BOOST:
1241 return sprintf(buf, "boost\n");
1247 /* directly set raw value to chip register, format: 'register value' */
1248 static ssize_t bq2415x_sysfs_set_registers(struct device *dev,
1249 struct device_attribute *attr,
1253 struct power_supply *psy = dev_get_drvdata(dev);
1254 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1260 if (sscanf(buf, "%x %x", ®, &val) != 2)
1263 if (reg > 4 || val > 255)
1266 ret = bq2415x_i2c_write(bq, reg, val);
1272 /* print value of chip register, format: 'register=value' */
1273 static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
1277 int ret = bq2415x_i2c_read(bq, reg);
1280 return sprintf(buf, "%#.2x=error %d\n", reg, ret);
1281 return sprintf(buf, "%#.2x=%#.2x\n", reg, ret);
1284 /* show all raw values of chip register, format per line: 'register=value' */
1285 static ssize_t bq2415x_sysfs_show_registers(struct device *dev,
1286 struct device_attribute *attr,
1289 struct power_supply *psy = dev_get_drvdata(dev);
1290 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1294 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret);
1295 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret);
1296 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret);
1297 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret);
1298 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret);
1302 /* set current and voltage limit entries (in mA or mV) */
1303 static ssize_t bq2415x_sysfs_set_limit(struct device *dev,
1304 struct device_attribute *attr,
1308 struct power_supply *psy = dev_get_drvdata(dev);
1309 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1314 if (kstrtol(buf, 10, &val) < 0)
1317 if (strcmp(attr->attr.name, "current_limit") == 0)
1318 ret = bq2415x_set_current_limit(bq, val);
1319 else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1320 ret = bq2415x_set_weak_battery_voltage(bq, val);
1321 else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1322 ret = bq2415x_set_battery_regulation_voltage(bq, val);
1323 else if (strcmp(attr->attr.name, "charge_current") == 0)
1324 ret = bq2415x_set_charge_current(bq, val);
1325 else if (strcmp(attr->attr.name, "termination_current") == 0)
1326 ret = bq2415x_set_termination_current(bq, val);
1335 /* show current and voltage limit entries (in mA or mV) */
1336 static ssize_t bq2415x_sysfs_show_limit(struct device *dev,
1337 struct device_attribute *attr,
1340 struct power_supply *psy = dev_get_drvdata(dev);
1341 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1345 if (strcmp(attr->attr.name, "current_limit") == 0)
1346 ret = bq2415x_get_current_limit(bq);
1347 else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1348 ret = bq2415x_get_weak_battery_voltage(bq);
1349 else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1350 ret = bq2415x_get_battery_regulation_voltage(bq);
1351 else if (strcmp(attr->attr.name, "charge_current") == 0)
1352 ret = bq2415x_get_charge_current(bq);
1353 else if (strcmp(attr->attr.name, "termination_current") == 0)
1354 ret = bq2415x_get_termination_current(bq);
1360 return sprintf(buf, "%d\n", ret);
1363 /* set *_enable entries */
1364 static ssize_t bq2415x_sysfs_set_enable(struct device *dev,
1365 struct device_attribute *attr,
1369 struct power_supply *psy = dev_get_drvdata(dev);
1370 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1372 enum bq2415x_command command;
1376 if (kstrtol(buf, 10, &val) < 0)
1379 if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1380 command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE :
1381 BQ2415X_CHARGE_TERMINATION_DISABLE;
1382 else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1383 command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE :
1384 BQ2415X_HIGH_IMPEDANCE_DISABLE;
1385 else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1386 command = val ? BQ2415X_OTG_PIN_ENABLE :
1387 BQ2415X_OTG_PIN_DISABLE;
1388 else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1389 command = val ? BQ2415X_STAT_PIN_ENABLE :
1390 BQ2415X_STAT_PIN_DISABLE;
1394 ret = bq2415x_exec_command(bq, command);
1400 /* show *_enable entries */
1401 static ssize_t bq2415x_sysfs_show_enable(struct device *dev,
1402 struct device_attribute *attr,
1405 struct power_supply *psy = dev_get_drvdata(dev);
1406 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1408 enum bq2415x_command command;
1411 if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1412 command = BQ2415X_CHARGE_TERMINATION_STATUS;
1413 else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1414 command = BQ2415X_HIGH_IMPEDANCE_STATUS;
1415 else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1416 command = BQ2415X_OTG_PIN_STATUS;
1417 else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1418 command = BQ2415X_STAT_PIN_STATUS;
1422 ret = bq2415x_exec_command(bq, command);
1425 return sprintf(buf, "%d\n", ret);
1428 static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO,
1429 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1430 static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO,
1431 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1432 static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO,
1433 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1434 static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO,
1435 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1436 static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
1437 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1439 static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO,
1440 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1441 static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
1442 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1443 static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO,
1444 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1445 static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
1446 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1448 static DEVICE_ATTR(reported_mode, S_IRUGO,
1449 bq2415x_sysfs_show_reported_mode, NULL);
1450 static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
1451 bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
1452 static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
1453 bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer);
1455 static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO,
1456 bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers);
1458 static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1459 static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1460 static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1461 static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1463 static struct attribute *bq2415x_sysfs_attributes[] = {
1465 * TODO: some (appropriate) of these attrs should be switched to
1466 * use power supply class props.
1468 &dev_attr_current_limit.attr,
1469 &dev_attr_weak_battery_voltage.attr,
1470 &dev_attr_battery_regulation_voltage.attr,
1471 &dev_attr_charge_current.attr,
1472 &dev_attr_termination_current.attr,
1474 &dev_attr_charge_termination_enable.attr,
1475 &dev_attr_high_impedance_enable.attr,
1476 &dev_attr_otg_pin_enable.attr,
1477 &dev_attr_stat_pin_enable.attr,
1479 &dev_attr_reported_mode.attr,
1480 &dev_attr_mode.attr,
1481 &dev_attr_timer.attr,
1483 &dev_attr_registers.attr,
1485 &dev_attr_otg_status.attr,
1486 &dev_attr_charge_status.attr,
1487 &dev_attr_boost_status.attr,
1488 &dev_attr_fault_status.attr,
1492 static const struct attribute_group bq2415x_sysfs_attr_group = {
1493 .attrs = bq2415x_sysfs_attributes,
1496 static int bq2415x_sysfs_init(struct bq2415x_device *bq)
1498 return sysfs_create_group(&bq->charger.dev->kobj,
1499 &bq2415x_sysfs_attr_group);
1502 static void bq2415x_sysfs_exit(struct bq2415x_device *bq)
1504 sysfs_remove_group(&bq->charger.dev->kobj, &bq2415x_sysfs_attr_group);
1507 /* main bq2415x probe function */
1508 static int bq2415x_probe(struct i2c_client *client,
1509 const struct i2c_device_id *id)
1514 struct bq2415x_device *bq;
1516 if (!client->dev.platform_data) {
1517 dev_err(&client->dev, "platform data not set\n");
1521 /* Get new ID for the new device */
1522 mutex_lock(&bq2415x_id_mutex);
1523 num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL);
1524 mutex_unlock(&bq2415x_id_mutex);
1528 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
1530 dev_err(&client->dev, "failed to allocate device name\n");
1535 bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
1537 dev_err(&client->dev, "failed to allocate device data\n");
1542 i2c_set_clientdata(client, bq);
1545 bq->dev = &client->dev;
1546 bq->chip = id->driver_data;
1548 bq->mode = BQ2415X_MODE_OFF;
1549 bq->reported_mode = BQ2415X_MODE_OFF;
1553 memcpy(&bq->init_data, client->dev.platform_data,
1554 sizeof(bq->init_data));
1556 bq2415x_reset_chip(bq);
1558 ret = bq2415x_power_supply_init(bq);
1560 dev_err(bq->dev, "failed to register power supply: %d\n", ret);
1564 ret = bq2415x_sysfs_init(bq);
1566 dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret);
1570 ret = bq2415x_set_defaults(bq);
1572 dev_err(bq->dev, "failed to set default values: %d\n", ret);
1576 if (bq->init_data.set_mode_hook) {
1577 if (bq->init_data.set_mode_hook(
1578 bq2415x_hook_function, bq)) {
1580 bq2415x_set_mode(bq, bq->reported_mode);
1581 dev_info(bq->dev, "automode enabled\n");
1584 dev_info(bq->dev, "automode failed\n");
1588 dev_info(bq->dev, "automode not supported\n");
1591 INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work);
1592 bq2415x_set_autotimer(bq, 1);
1594 dev_info(bq->dev, "driver registered\n");
1598 bq2415x_sysfs_exit(bq);
1600 bq2415x_power_supply_exit(bq);
1604 mutex_lock(&bq2415x_id_mutex);
1605 idr_remove(&bq2415x_id, num);
1606 mutex_unlock(&bq2415x_id_mutex);
1611 /* main bq2415x remove function */
1613 static int bq2415x_remove(struct i2c_client *client)
1615 struct bq2415x_device *bq = i2c_get_clientdata(client);
1617 if (bq->init_data.set_mode_hook)
1618 bq->init_data.set_mode_hook(NULL, NULL);
1620 bq2415x_sysfs_exit(bq);
1621 bq2415x_power_supply_exit(bq);
1623 bq2415x_reset_chip(bq);
1625 mutex_lock(&bq2415x_id_mutex);
1626 idr_remove(&bq2415x_id, bq->id);
1627 mutex_unlock(&bq2415x_id_mutex);
1629 dev_info(bq->dev, "driver unregistered\n");
1636 static const struct i2c_device_id bq2415x_i2c_id_table[] = {
1637 { "bq2415x", BQUNKNOWN },
1638 { "bq24150", BQ24150 },
1639 { "bq24150a", BQ24150A },
1640 { "bq24151", BQ24151 },
1641 { "bq24151a", BQ24151A },
1642 { "bq24152", BQ24152 },
1643 { "bq24153", BQ24153 },
1644 { "bq24153a", BQ24153A },
1645 { "bq24155", BQ24155 },
1646 { "bq24156", BQ24156 },
1647 { "bq24156a", BQ24156A },
1648 { "bq24158", BQ24158 },
1651 MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table);
1653 static struct i2c_driver bq2415x_driver = {
1655 .name = "bq2415x-charger",
1657 .probe = bq2415x_probe,
1658 .remove = bq2415x_remove,
1659 .id_table = bq2415x_i2c_id_table,
1661 module_i2c_driver(bq2415x_driver);
1663 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
1664 MODULE_DESCRIPTION("bq2415x charger driver");
1665 MODULE_LICENSE("GPL");