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.
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 struct power_supply *notify_psy;
174 struct notifier_block nb;
175 enum bq2415x_mode reported_mode;/* mode reported by hook function */
176 enum bq2415x_mode mode; /* current configured mode */
177 enum bq2415x_chip chip;
178 const char *timer_error;
181 int autotimer; /* 1 - if driver automatically reset timer, 0 - not */
182 int automode; /* 1 - enabled, 0 - disabled; -1 - not supported */
186 /* each registered chip must have unique id */
187 static DEFINE_IDR(bq2415x_id);
189 static DEFINE_MUTEX(bq2415x_id_mutex);
190 static DEFINE_MUTEX(bq2415x_timer_mutex);
191 static DEFINE_MUTEX(bq2415x_i2c_mutex);
193 /**** i2c read functions ****/
195 /* read value from register */
196 static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg)
198 struct i2c_client *client = to_i2c_client(bq->dev);
199 struct i2c_msg msg[2];
203 if (!client->adapter)
206 msg[0].addr = client->addr;
209 msg[0].len = sizeof(reg);
210 msg[1].addr = client->addr;
211 msg[1].flags = I2C_M_RD;
213 msg[1].len = sizeof(val);
215 mutex_lock(&bq2415x_i2c_mutex);
216 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
217 mutex_unlock(&bq2415x_i2c_mutex);
225 /* read value from register, apply mask and right shift it */
226 static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
234 ret = bq2415x_i2c_read(bq, reg);
237 return (ret & mask) >> shift;
240 /* read value from register and return one specified bit */
241 static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit)
245 return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit);
248 /**** i2c write functions ****/
250 /* write value to register */
251 static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val)
253 struct i2c_client *client = to_i2c_client(bq->dev);
254 struct i2c_msg msg[1];
261 msg[0].addr = client->addr;
264 msg[0].len = ARRAY_SIZE(data);
266 mutex_lock(&bq2415x_i2c_mutex);
267 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
268 mutex_unlock(&bq2415x_i2c_mutex);
270 /* i2c_transfer returns number of messages transferred */
279 /* read value from register, change it with mask left shifted and write back */
280 static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val,
288 ret = bq2415x_i2c_read(bq, reg);
295 return bq2415x_i2c_write(bq, reg, ret);
298 /* change only one bit in register */
299 static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg,
304 return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit);
307 /**** global functions ****/
309 /* exec command function */
310 static int bq2415x_exec_command(struct bq2415x_device *bq,
311 enum bq2415x_command command)
316 case BQ2415X_TIMER_RESET:
317 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS,
318 1, BQ2415X_BIT_TMR_RST);
319 case BQ2415X_OTG_STATUS:
320 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
322 case BQ2415X_STAT_PIN_STATUS:
323 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
324 BQ2415X_BIT_EN_STAT);
325 case BQ2415X_STAT_PIN_ENABLE:
326 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1,
327 BQ2415X_BIT_EN_STAT);
328 case BQ2415X_STAT_PIN_DISABLE:
329 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0,
330 BQ2415X_BIT_EN_STAT);
331 case BQ2415X_CHARGE_STATUS:
332 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
333 BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT);
334 case BQ2415X_BOOST_STATUS:
335 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
337 case BQ2415X_FAULT_STATUS:
338 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
339 BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT);
341 case BQ2415X_CHARGE_TERMINATION_STATUS:
342 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
344 case BQ2415X_CHARGE_TERMINATION_ENABLE:
345 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
347 case BQ2415X_CHARGE_TERMINATION_DISABLE:
348 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
350 case BQ2415X_CHARGER_STATUS:
351 ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
356 return ret > 0 ? 0 : 1;
357 case BQ2415X_CHARGER_ENABLE:
358 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
360 case BQ2415X_CHARGER_DISABLE:
361 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
363 case BQ2415X_HIGH_IMPEDANCE_STATUS:
364 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
365 BQ2415X_BIT_HZ_MODE);
366 case BQ2415X_HIGH_IMPEDANCE_ENABLE:
367 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
368 1, BQ2415X_BIT_HZ_MODE);
369 case BQ2415X_HIGH_IMPEDANCE_DISABLE:
370 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
371 0, BQ2415X_BIT_HZ_MODE);
372 case BQ2415X_BOOST_MODE_STATUS:
373 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
374 BQ2415X_BIT_OPA_MODE);
375 case BQ2415X_BOOST_MODE_ENABLE:
376 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
377 1, BQ2415X_BIT_OPA_MODE);
378 case BQ2415X_BOOST_MODE_DISABLE:
379 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
380 0, BQ2415X_BIT_OPA_MODE);
382 case BQ2415X_OTG_LEVEL:
383 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
385 case BQ2415X_OTG_ACTIVATE_HIGH:
386 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
387 1, BQ2415X_BIT_OTG_PL);
388 case BQ2415X_OTG_ACTIVATE_LOW:
389 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
390 0, BQ2415X_BIT_OTG_PL);
391 case BQ2415X_OTG_PIN_STATUS:
392 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
394 case BQ2415X_OTG_PIN_ENABLE:
395 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
396 1, BQ2415X_BIT_OTG_EN);
397 case BQ2415X_OTG_PIN_DISABLE:
398 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
399 0, BQ2415X_BIT_OTG_EN);
401 case BQ2415X_VENDER_CODE:
402 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
403 BQ2415X_MASK_VENDER, BQ2415X_SHIFT_VENDER);
404 case BQ2415X_PART_NUMBER:
405 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
406 BQ2415X_MASK_PN, BQ2415X_SHIFT_PN);
407 case BQ2415X_REVISION:
408 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
409 BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION);
414 /* detect chip type */
415 static enum bq2415x_chip bq2415x_detect_chip(struct bq2415x_device *bq)
417 struct i2c_client *client = to_i2c_client(bq->dev);
418 int ret = bq2415x_exec_command(bq, BQ2415X_PART_NUMBER);
423 switch (client->addr) {
427 if (bq->chip == BQ24151A)
432 if (bq->chip == BQ24150A ||
433 bq->chip == BQ24152 ||
439 if (bq->chip == BQ24153A)
451 if (bq->chip == BQ24156A)
466 /* detect chip revision */
467 static int bq2415x_detect_revision(struct bq2415x_device *bq)
469 int ret = bq2415x_exec_command(bq, BQ2415X_REVISION);
470 int chip = bq2415x_detect_chip(bq);
472 if (ret < 0 || chip < 0)
481 if (ret >= 0 && ret <= 3)
508 /* return chip vender code */
509 static int bq2415x_get_vender_code(struct bq2415x_device *bq)
513 ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
517 /* convert to binary */
519 ((ret >> 1) & 0x1) * 10 +
520 ((ret >> 2) & 0x1) * 100;
523 /* reset all chip registers to default state */
524 static void bq2415x_reset_chip(struct bq2415x_device *bq)
526 bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT);
527 bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE);
528 bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL);
529 bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS);
530 bq->timer_error = NULL;
533 /**** properties functions ****/
535 /* set current limit in mA */
536 static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA)
549 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
550 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
553 /* get current limit in mA */
554 static int bq2415x_get_current_limit(struct bq2415x_device *bq)
558 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
559 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
573 /* set weak battery voltage in mV */
574 static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
581 else if (mV <= 3500 + 50)
583 else if (mV <= 3600 + 50)
588 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
589 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
592 /* get weak battery voltage in mV */
593 static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq)
597 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
598 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
601 return 100 * (34 + ret);
604 /* set battery regulation voltage in mV */
605 static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
608 int val = (mV/10 - 350) / 2;
611 * According to datasheet, maximum battery regulation voltage is
612 * 4440mV which is b101111 = 47.
619 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val,
620 BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
623 /* get battery regulation voltage in mV */
624 static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq)
626 int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE,
627 BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
631 return 10 * (350 + 2*ret);
634 /* set charge current in mA (platform data must provide resistor sense) */
635 static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA)
639 if (bq->init_data.resistor_sense <= 0)
642 val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
648 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
649 BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET,
650 BQ2415X_SHIFT_VI_CHRG);
653 /* get charge current in mA (platform data must provide resistor sense) */
654 static int bq2415x_get_charge_current(struct bq2415x_device *bq)
658 if (bq->init_data.resistor_sense <= 0)
661 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
662 BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG);
665 return (37400 + 6800*ret) / bq->init_data.resistor_sense;
668 /* set termination current in mA (platform data must provide resistor sense) */
669 static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA)
673 if (bq->init_data.resistor_sense <= 0)
676 val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
682 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
683 BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET,
684 BQ2415X_SHIFT_VI_TERM);
687 /* get termination current in mA (platform data must provide resistor sense) */
688 static int bq2415x_get_termination_current(struct bq2415x_device *bq)
692 if (bq->init_data.resistor_sense <= 0)
695 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
696 BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM);
699 return (3400 + 3400*ret) / bq->init_data.resistor_sense;
702 /* set default value of property */
703 #define bq2415x_set_default_value(bq, prop) \
706 if (bq->init_data.prop != -1) \
707 ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
712 /* set default values of all properties */
713 static int bq2415x_set_defaults(struct bq2415x_device *bq)
715 bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
716 bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
717 bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE);
719 bq2415x_set_default_value(bq, current_limit);
720 bq2415x_set_default_value(bq, weak_battery_voltage);
721 bq2415x_set_default_value(bq, battery_regulation_voltage);
723 if (bq->init_data.resistor_sense > 0) {
724 bq2415x_set_default_value(bq, charge_current);
725 bq2415x_set_default_value(bq, termination_current);
726 bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE);
729 bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
733 /**** charger mode functions ****/
735 /* set charger mode */
736 static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode)
742 if (mode == BQ2415X_MODE_BOOST)
744 else if (mode != BQ2415X_MODE_OFF)
748 ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
751 ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
757 case BQ2415X_MODE_OFF:
758 dev_dbg(bq->dev, "changing mode to: Offline\n");
759 ret = bq2415x_set_current_limit(bq, 100);
761 case BQ2415X_MODE_NONE:
762 dev_dbg(bq->dev, "changing mode to: N/A\n");
763 ret = bq2415x_set_current_limit(bq, 100);
765 case BQ2415X_MODE_HOST_CHARGER:
766 dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n");
767 ret = bq2415x_set_current_limit(bq, 500);
769 case BQ2415X_MODE_DEDICATED_CHARGER:
770 dev_dbg(bq->dev, "changing mode to: Dedicated charger\n");
771 ret = bq2415x_set_current_limit(bq, 1800);
773 case BQ2415X_MODE_BOOST: /* Boost mode */
774 dev_dbg(bq->dev, "changing mode to: Boost\n");
775 ret = bq2415x_set_current_limit(bq, 100);
783 ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
785 ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE);
790 bq2415x_set_default_value(bq, weak_battery_voltage);
791 bq2415x_set_default_value(bq, battery_regulation_voltage);
794 sysfs_notify(&bq->charger.dev->kobj, NULL, "mode");
800 static int bq2415x_notifier_call(struct notifier_block *nb,
801 unsigned long val, void *v)
803 struct bq2415x_device *bq =
804 container_of(nb, struct bq2415x_device, nb);
805 struct power_supply *psy = v;
806 enum bq2415x_mode mode;
807 union power_supply_propval prop;
811 if (val != PSY_EVENT_PROP_CHANGED)
814 if (psy != bq->notify_psy)
817 dev_dbg(bq->dev, "notifier call was called\n");
819 ret = psy->get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX, &prop);
826 mode = BQ2415X_MODE_OFF;
828 mode = BQ2415X_MODE_NONE;
830 mode = BQ2415X_MODE_HOST_CHARGER;
832 mode = BQ2415X_MODE_DEDICATED_CHARGER;
834 if (bq->reported_mode == mode)
837 bq->reported_mode = mode;
839 /* if automode is not enabled do not tell about reported_mode */
840 if (bq->automode < 1)
843 sysfs_notify(&bq->charger.dev->kobj, NULL, "reported_mode");
844 bq2415x_set_mode(bq, bq->reported_mode);
849 /**** timer functions ****/
851 /* enable/disable auto resetting chip timer */
852 static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
854 mutex_lock(&bq2415x_timer_mutex);
856 if (bq->autotimer == state) {
857 mutex_unlock(&bq2415x_timer_mutex);
861 bq->autotimer = state;
864 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
865 bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
866 bq->timer_error = NULL;
868 cancel_delayed_work_sync(&bq->work);
871 mutex_unlock(&bq2415x_timer_mutex);
874 /* called by bq2415x_timer_work on timer error */
875 static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
877 bq->timer_error = msg;
878 sysfs_notify(&bq->charger.dev->kobj, NULL, "timer");
879 dev_err(bq->dev, "%s\n", msg);
880 if (bq->automode > 0)
882 bq2415x_set_mode(bq, BQ2415X_MODE_OFF);
883 bq2415x_set_autotimer(bq, 0);
886 /* delayed work function for auto resetting chip timer */
887 static void bq2415x_timer_work(struct work_struct *work)
889 struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
898 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
900 bq2415x_timer_error(bq, "Resetting timer failed");
904 boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
906 bq2415x_timer_error(bq, "Unknown error");
910 error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
912 bq2415x_timer_error(bq, "Unknown error");
918 /* Non fatal errors, chip is OK */
919 case 0: /* No error */
921 case 6: /* Timer expired */
922 dev_err(bq->dev, "Timer expired\n");
924 case 3: /* Battery voltage too low */
925 dev_err(bq->dev, "Battery voltage to low\n");
928 /* Fatal errors, disable and reset chip */
929 case 1: /* Overvoltage protection (chip fried) */
930 bq2415x_timer_error(bq,
931 "Overvoltage protection (chip fried)");
933 case 2: /* Overload */
934 bq2415x_timer_error(bq, "Overload");
936 case 4: /* Battery overvoltage protection */
937 bq2415x_timer_error(bq,
938 "Battery overvoltage protection");
940 case 5: /* Thermal shutdown (too hot) */
941 bq2415x_timer_error(bq,
942 "Thermal shutdown (too hot)");
945 bq2415x_timer_error(bq, "Unknown error");
950 /* Non fatal errors, chip is OK */
951 case 0: /* No error */
953 case 2: /* Sleep mode */
954 dev_err(bq->dev, "Sleep mode\n");
956 case 3: /* Poor input source */
957 dev_err(bq->dev, "Poor input source\n");
959 case 6: /* Timer expired */
960 dev_err(bq->dev, "Timer expired\n");
962 case 7: /* No battery */
963 dev_err(bq->dev, "No battery\n");
966 /* Fatal errors, disable and reset chip */
967 case 1: /* Overvoltage protection (chip fried) */
968 bq2415x_timer_error(bq,
969 "Overvoltage protection (chip fried)");
971 case 4: /* Battery overvoltage protection */
972 bq2415x_timer_error(bq,
973 "Battery overvoltage protection");
975 case 5: /* Thermal shutdown (too hot) */
976 bq2415x_timer_error(bq,
977 "Thermal shutdown (too hot)");
982 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
985 /**** power supply interface code ****/
987 static enum power_supply_property bq2415x_power_supply_props[] = {
988 /* TODO: maybe add more power supply properties */
989 POWER_SUPPLY_PROP_STATUS,
990 POWER_SUPPLY_PROP_MODEL_NAME,
993 static int bq2415x_power_supply_get_property(struct power_supply *psy,
994 enum power_supply_property psp,
995 union power_supply_propval *val)
997 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1002 case POWER_SUPPLY_PROP_STATUS:
1003 ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
1006 else if (ret == 0) /* Ready */
1007 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1008 else if (ret == 1) /* Charge in progress */
1009 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1010 else if (ret == 2) /* Charge done */
1011 val->intval = POWER_SUPPLY_STATUS_FULL;
1013 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
1015 case POWER_SUPPLY_PROP_MODEL_NAME:
1016 val->strval = bq->model;
1024 static int bq2415x_power_supply_init(struct bq2415x_device *bq)
1030 bq->charger.name = bq->name;
1031 bq->charger.type = POWER_SUPPLY_TYPE_USB;
1032 bq->charger.properties = bq2415x_power_supply_props;
1033 bq->charger.num_properties = ARRAY_SIZE(bq2415x_power_supply_props);
1034 bq->charger.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 ret = power_supply_register(bq->dev, &bq->charger);
1066 static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
1069 if (bq->automode > 0)
1071 cancel_delayed_work_sync(&bq->work);
1072 power_supply_unregister(&bq->charger);
1076 /**** additional sysfs entries for power supply interface ****/
1078 /* show *_status entries */
1079 static ssize_t bq2415x_sysfs_show_status(struct device *dev,
1080 struct device_attribute *attr,
1083 struct power_supply *psy = dev_get_drvdata(dev);
1084 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
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 = container_of(psy, struct bq2415x_device,
1122 if (strncmp(buf, "auto", 4) == 0)
1123 bq2415x_set_autotimer(bq, 1);
1124 else if (strncmp(buf, "off", 3) == 0)
1125 bq2415x_set_autotimer(bq, 0);
1127 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
1134 /* show timer entry (auto or off) */
1135 static ssize_t bq2415x_sysfs_show_timer(struct device *dev,
1136 struct device_attribute *attr,
1139 struct power_supply *psy = dev_get_drvdata(dev);
1140 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1143 if (bq->timer_error)
1144 return sprintf(buf, "%s\n", bq->timer_error);
1147 return sprintf(buf, "auto\n");
1148 return sprintf(buf, "off\n");
1153 * auto - if automode is supported, enable it and set mode to reported
1154 * none - disable charger and boost mode
1155 * host - charging mode for host/hub chargers (current limit 500mA)
1156 * dedicated - charging mode for dedicated chargers (unlimited current limit)
1157 * boost - disable charger and enable boost mode
1159 static ssize_t bq2415x_sysfs_set_mode(struct device *dev,
1160 struct device_attribute *attr,
1164 struct power_supply *psy = dev_get_drvdata(dev);
1165 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1167 enum bq2415x_mode mode;
1170 if (strncmp(buf, "auto", 4) == 0) {
1171 if (bq->automode < 0)
1174 mode = bq->reported_mode;
1175 } else if (strncmp(buf, "off", 3) == 0) {
1176 if (bq->automode > 0)
1178 mode = BQ2415X_MODE_OFF;
1179 } else if (strncmp(buf, "none", 4) == 0) {
1180 if (bq->automode > 0)
1182 mode = BQ2415X_MODE_NONE;
1183 } else if (strncmp(buf, "host", 4) == 0) {
1184 if (bq->automode > 0)
1186 mode = BQ2415X_MODE_HOST_CHARGER;
1187 } else if (strncmp(buf, "dedicated", 9) == 0) {
1188 if (bq->automode > 0)
1190 mode = BQ2415X_MODE_DEDICATED_CHARGER;
1191 } else if (strncmp(buf, "boost", 5) == 0) {
1192 if (bq->automode > 0)
1194 mode = BQ2415X_MODE_BOOST;
1195 } else if (strncmp(buf, "reset", 5) == 0) {
1196 bq2415x_reset_chip(bq);
1197 bq2415x_set_defaults(bq);
1198 if (bq->automode <= 0)
1201 mode = bq->reported_mode;
1206 ret = bq2415x_set_mode(bq, mode);
1212 /* show mode entry (auto, none, host, dedicated or boost) */
1213 static ssize_t bq2415x_sysfs_show_mode(struct device *dev,
1214 struct device_attribute *attr,
1217 struct power_supply *psy = dev_get_drvdata(dev);
1218 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1222 if (bq->automode > 0)
1223 ret += sprintf(buf+ret, "auto (");
1226 case BQ2415X_MODE_OFF:
1227 ret += sprintf(buf+ret, "off");
1229 case BQ2415X_MODE_NONE:
1230 ret += sprintf(buf+ret, "none");
1232 case BQ2415X_MODE_HOST_CHARGER:
1233 ret += sprintf(buf+ret, "host");
1235 case BQ2415X_MODE_DEDICATED_CHARGER:
1236 ret += sprintf(buf+ret, "dedicated");
1238 case BQ2415X_MODE_BOOST:
1239 ret += sprintf(buf+ret, "boost");
1243 if (bq->automode > 0)
1244 ret += sprintf(buf+ret, ")");
1246 ret += sprintf(buf+ret, "\n");
1250 /* show reported_mode entry (none, host, dedicated or boost) */
1251 static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
1252 struct device_attribute *attr,
1255 struct power_supply *psy = dev_get_drvdata(dev);
1256 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1259 if (bq->automode < 0)
1262 switch (bq->reported_mode) {
1263 case BQ2415X_MODE_OFF:
1264 return sprintf(buf, "off\n");
1265 case BQ2415X_MODE_NONE:
1266 return sprintf(buf, "none\n");
1267 case BQ2415X_MODE_HOST_CHARGER:
1268 return sprintf(buf, "host\n");
1269 case BQ2415X_MODE_DEDICATED_CHARGER:
1270 return sprintf(buf, "dedicated\n");
1271 case BQ2415X_MODE_BOOST:
1272 return sprintf(buf, "boost\n");
1278 /* directly set raw value to chip register, format: 'register value' */
1279 static ssize_t bq2415x_sysfs_set_registers(struct device *dev,
1280 struct device_attribute *attr,
1284 struct power_supply *psy = dev_get_drvdata(dev);
1285 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1291 if (sscanf(buf, "%x %x", ®, &val) != 2)
1294 if (reg > 4 || val > 255)
1297 ret = bq2415x_i2c_write(bq, reg, val);
1303 /* print value of chip register, format: 'register=value' */
1304 static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
1308 int ret = bq2415x_i2c_read(bq, reg);
1311 return sprintf(buf, "%#.2x=error %d\n", reg, ret);
1312 return sprintf(buf, "%#.2x=%#.2x\n", reg, ret);
1315 /* show all raw values of chip register, format per line: 'register=value' */
1316 static ssize_t bq2415x_sysfs_show_registers(struct device *dev,
1317 struct device_attribute *attr,
1320 struct power_supply *psy = dev_get_drvdata(dev);
1321 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1325 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret);
1326 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret);
1327 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret);
1328 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret);
1329 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret);
1333 /* set current and voltage limit entries (in mA or mV) */
1334 static ssize_t bq2415x_sysfs_set_limit(struct device *dev,
1335 struct device_attribute *attr,
1339 struct power_supply *psy = dev_get_drvdata(dev);
1340 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1345 if (kstrtol(buf, 10, &val) < 0)
1348 if (strcmp(attr->attr.name, "current_limit") == 0)
1349 ret = bq2415x_set_current_limit(bq, val);
1350 else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1351 ret = bq2415x_set_weak_battery_voltage(bq, val);
1352 else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1353 ret = bq2415x_set_battery_regulation_voltage(bq, val);
1354 else if (strcmp(attr->attr.name, "charge_current") == 0)
1355 ret = bq2415x_set_charge_current(bq, val);
1356 else if (strcmp(attr->attr.name, "termination_current") == 0)
1357 ret = bq2415x_set_termination_current(bq, val);
1366 /* show current and voltage limit entries (in mA or mV) */
1367 static ssize_t bq2415x_sysfs_show_limit(struct device *dev,
1368 struct device_attribute *attr,
1371 struct power_supply *psy = dev_get_drvdata(dev);
1372 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1376 if (strcmp(attr->attr.name, "current_limit") == 0)
1377 ret = bq2415x_get_current_limit(bq);
1378 else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1379 ret = bq2415x_get_weak_battery_voltage(bq);
1380 else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1381 ret = bq2415x_get_battery_regulation_voltage(bq);
1382 else if (strcmp(attr->attr.name, "charge_current") == 0)
1383 ret = bq2415x_get_charge_current(bq);
1384 else if (strcmp(attr->attr.name, "termination_current") == 0)
1385 ret = bq2415x_get_termination_current(bq);
1391 return sprintf(buf, "%d\n", ret);
1394 /* set *_enable entries */
1395 static ssize_t bq2415x_sysfs_set_enable(struct device *dev,
1396 struct device_attribute *attr,
1400 struct power_supply *psy = dev_get_drvdata(dev);
1401 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1403 enum bq2415x_command command;
1407 if (kstrtol(buf, 10, &val) < 0)
1410 if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1411 command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE :
1412 BQ2415X_CHARGE_TERMINATION_DISABLE;
1413 else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1414 command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE :
1415 BQ2415X_HIGH_IMPEDANCE_DISABLE;
1416 else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1417 command = val ? BQ2415X_OTG_PIN_ENABLE :
1418 BQ2415X_OTG_PIN_DISABLE;
1419 else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1420 command = val ? BQ2415X_STAT_PIN_ENABLE :
1421 BQ2415X_STAT_PIN_DISABLE;
1425 ret = bq2415x_exec_command(bq, command);
1431 /* show *_enable entries */
1432 static ssize_t bq2415x_sysfs_show_enable(struct device *dev,
1433 struct device_attribute *attr,
1436 struct power_supply *psy = dev_get_drvdata(dev);
1437 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1439 enum bq2415x_command command;
1442 if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1443 command = BQ2415X_CHARGE_TERMINATION_STATUS;
1444 else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1445 command = BQ2415X_HIGH_IMPEDANCE_STATUS;
1446 else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1447 command = BQ2415X_OTG_PIN_STATUS;
1448 else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1449 command = BQ2415X_STAT_PIN_STATUS;
1453 ret = bq2415x_exec_command(bq, command);
1456 return sprintf(buf, "%d\n", ret);
1459 static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO,
1460 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1461 static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO,
1462 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1463 static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO,
1464 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1465 static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO,
1466 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1467 static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
1468 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1470 static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO,
1471 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1472 static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
1473 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1474 static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO,
1475 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1476 static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
1477 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1479 static DEVICE_ATTR(reported_mode, S_IRUGO,
1480 bq2415x_sysfs_show_reported_mode, NULL);
1481 static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
1482 bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
1483 static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
1484 bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer);
1486 static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO,
1487 bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers);
1489 static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1490 static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1491 static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1492 static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1494 static struct attribute *bq2415x_sysfs_attributes[] = {
1496 * TODO: some (appropriate) of these attrs should be switched to
1497 * use power supply class props.
1499 &dev_attr_current_limit.attr,
1500 &dev_attr_weak_battery_voltage.attr,
1501 &dev_attr_battery_regulation_voltage.attr,
1502 &dev_attr_charge_current.attr,
1503 &dev_attr_termination_current.attr,
1505 &dev_attr_charge_termination_enable.attr,
1506 &dev_attr_high_impedance_enable.attr,
1507 &dev_attr_otg_pin_enable.attr,
1508 &dev_attr_stat_pin_enable.attr,
1510 &dev_attr_reported_mode.attr,
1511 &dev_attr_mode.attr,
1512 &dev_attr_timer.attr,
1514 &dev_attr_registers.attr,
1516 &dev_attr_otg_status.attr,
1517 &dev_attr_charge_status.attr,
1518 &dev_attr_boost_status.attr,
1519 &dev_attr_fault_status.attr,
1523 static const struct attribute_group bq2415x_sysfs_attr_group = {
1524 .attrs = bq2415x_sysfs_attributes,
1527 static int bq2415x_sysfs_init(struct bq2415x_device *bq)
1529 return sysfs_create_group(&bq->charger.dev->kobj,
1530 &bq2415x_sysfs_attr_group);
1533 static void bq2415x_sysfs_exit(struct bq2415x_device *bq)
1535 sysfs_remove_group(&bq->charger.dev->kobj, &bq2415x_sysfs_attr_group);
1538 /* main bq2415x probe function */
1539 static int bq2415x_probe(struct i2c_client *client,
1540 const struct i2c_device_id *id)
1545 struct bq2415x_device *bq;
1546 struct device_node *np = client->dev.of_node;
1547 struct bq2415x_platform_data *pdata = client->dev.platform_data;
1549 if (!np && !pdata) {
1550 dev_err(&client->dev, "platform data missing\n");
1554 /* Get new ID for the new device */
1555 mutex_lock(&bq2415x_id_mutex);
1556 num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL);
1557 mutex_unlock(&bq2415x_id_mutex);
1561 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
1563 dev_err(&client->dev, "failed to allocate device name\n");
1568 bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
1570 dev_err(&client->dev, "failed to allocate device data\n");
1576 bq->notify_psy = power_supply_get_by_phandle(np, "ti,usb-charger-detection");
1578 if (!bq->notify_psy)
1579 return -EPROBE_DEFER;
1581 else if (pdata->notify_device)
1582 bq->notify_psy = power_supply_get_by_name(pdata->notify_device);
1584 bq->notify_psy = NULL;
1586 i2c_set_clientdata(client, bq);
1589 bq->dev = &client->dev;
1590 bq->chip = id->driver_data;
1592 bq->mode = BQ2415X_MODE_OFF;
1593 bq->reported_mode = BQ2415X_MODE_OFF;
1598 ret = of_property_read_u32(np, "ti,current-limit",
1599 &bq->init_data.current_limit);
1602 ret = of_property_read_u32(np, "ti,weak-battery-voltage",
1603 &bq->init_data.weak_battery_voltage);
1606 ret = of_property_read_u32(np, "ti,battery-regulation-voltage",
1607 &bq->init_data.battery_regulation_voltage);
1610 ret = of_property_read_u32(np, "ti,charge-current",
1611 &bq->init_data.charge_current);
1614 ret = of_property_read_u32(np, "ti,termination-current",
1615 &bq->init_data.termination_current);
1618 ret = of_property_read_u32(np, "ti,resistor-sense",
1619 &bq->init_data.resistor_sense);
1623 memcpy(&bq->init_data, pdata, sizeof(bq->init_data));
1626 bq2415x_reset_chip(bq);
1628 ret = bq2415x_power_supply_init(bq);
1630 dev_err(bq->dev, "failed to register power supply: %d\n", ret);
1634 ret = bq2415x_sysfs_init(bq);
1636 dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret);
1640 ret = bq2415x_set_defaults(bq);
1642 dev_err(bq->dev, "failed to set default values: %d\n", ret);
1646 if (bq->notify_psy) {
1647 bq->nb.notifier_call = bq2415x_notifier_call;
1648 ret = power_supply_reg_notifier(&bq->nb);
1650 dev_err(bq->dev, "failed to reg notifier: %d\n", ret);
1654 /* Query for initial reported_mode and set it */
1655 bq2415x_notifier_call(&bq->nb, PSY_EVENT_PROP_CHANGED, bq->notify_psy);
1656 bq2415x_set_mode(bq, bq->reported_mode);
1659 dev_info(bq->dev, "automode enabled\n");
1662 dev_info(bq->dev, "automode not supported\n");
1665 INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work);
1666 bq2415x_set_autotimer(bq, 1);
1668 dev_info(bq->dev, "driver registered\n");
1673 bq2415x_sysfs_exit(bq);
1675 bq2415x_power_supply_exit(bq);
1679 mutex_lock(&bq2415x_id_mutex);
1680 idr_remove(&bq2415x_id, num);
1681 mutex_unlock(&bq2415x_id_mutex);
1686 /* main bq2415x remove function */
1688 static int bq2415x_remove(struct i2c_client *client)
1690 struct bq2415x_device *bq = i2c_get_clientdata(client);
1693 power_supply_unreg_notifier(&bq->nb);
1695 bq2415x_sysfs_exit(bq);
1696 bq2415x_power_supply_exit(bq);
1698 bq2415x_reset_chip(bq);
1700 mutex_lock(&bq2415x_id_mutex);
1701 idr_remove(&bq2415x_id, bq->id);
1702 mutex_unlock(&bq2415x_id_mutex);
1704 dev_info(bq->dev, "driver unregistered\n");
1711 static const struct i2c_device_id bq2415x_i2c_id_table[] = {
1712 { "bq2415x", BQUNKNOWN },
1713 { "bq24150", BQ24150 },
1714 { "bq24150a", BQ24150A },
1715 { "bq24151", BQ24151 },
1716 { "bq24151a", BQ24151A },
1717 { "bq24152", BQ24152 },
1718 { "bq24153", BQ24153 },
1719 { "bq24153a", BQ24153A },
1720 { "bq24155", BQ24155 },
1721 { "bq24156", BQ24156 },
1722 { "bq24156a", BQ24156A },
1723 { "bq24158", BQ24158 },
1726 MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table);
1728 static struct i2c_driver bq2415x_driver = {
1730 .name = "bq2415x-charger",
1732 .probe = bq2415x_probe,
1733 .remove = bq2415x_remove,
1734 .id_table = bq2415x_i2c_id_table,
1736 module_i2c_driver(bq2415x_driver);
1738 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
1739 MODULE_DESCRIPTION("bq2415x charger driver");
1740 MODULE_LICENSE("GPL");