PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / power / bq2415x_charger.c
blob79a37f6d3307290f6cc12cc9be2d818087ab2c7e
1 /*
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.
22 * Datasheets:
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))
60 /* status register */
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
86 /* vender register */
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
98 /* N/A BIT(3) */
99 #define BQ2415X_MASK_VI_TERM (BIT(0)|BIT(1)|BIT(2))
100 #define BQ2415X_SHIFT_VI_TERM 0
103 enum bq2415x_command {
104 BQ2415X_TIMER_RESET,
105 BQ2415X_OTG_STATUS,
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,
126 BQ2415X_OTG_LEVEL,
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,
133 BQ2415X_VENDER_CODE,
134 BQ2415X_PART_NUMBER,
135 BQ2415X_REVISION,
138 enum bq2415x_chip {
139 BQUNKNOWN,
140 BQ24150,
141 BQ24150A,
142 BQ24151,
143 BQ24151A,
144 BQ24152,
145 BQ24153,
146 BQ24153A,
147 BQ24155,
148 BQ24156,
149 BQ24156A,
150 BQ24158,
153 static char *bq2415x_chip_name[] = {
154 "unknown",
155 "bq24150",
156 "bq24150a",
157 "bq24151",
158 "bq24151a",
159 "bq24152",
160 "bq24153",
161 "bq24153a",
162 "bq24155",
163 "bq24156",
164 "bq24156a",
165 "bq24158",
168 struct bq2415x_device {
169 struct device *dev;
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;
179 char *model;
180 char *name;
181 int autotimer; /* 1 - if driver automatically reset timer, 0 - not */
182 int automode; /* 1 - enabled, 0 - disabled; -1 - not supported */
183 int id;
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];
200 u8 val;
201 int ret;
203 if (!client->adapter)
204 return -ENODEV;
206 msg[0].addr = client->addr;
207 msg[0].flags = 0;
208 msg[0].buf = &reg;
209 msg[0].len = sizeof(reg);
210 msg[1].addr = client->addr;
211 msg[1].flags = I2C_M_RD;
212 msg[1].buf = &val;
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);
219 if (ret < 0)
220 return ret;
222 return val;
225 /* read value from register, apply mask and right shift it */
226 static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
227 u8 mask, u8 shift)
229 int ret;
231 if (shift > 8)
232 return -EINVAL;
234 ret = bq2415x_i2c_read(bq, reg);
235 if (ret < 0)
236 return ret;
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)
243 if (bit > 8)
244 return -EINVAL;
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];
255 u8 data[2];
256 int ret;
258 data[0] = reg;
259 data[1] = val;
261 msg[0].addr = client->addr;
262 msg[0].flags = 0;
263 msg[0].buf = data;
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 */
271 if (ret < 0)
272 return ret;
273 else if (ret != 1)
274 return -EIO;
276 return 0;
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,
281 u8 mask, u8 shift)
283 int ret;
285 if (shift > 8)
286 return -EINVAL;
288 ret = bq2415x_i2c_read(bq, reg);
289 if (ret < 0)
290 return ret;
292 ret &= ~mask;
293 ret |= val << shift;
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,
300 bool val, u8 bit)
302 if (bit > 8)
303 return -EINVAL;
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)
313 int ret;
315 switch (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,
321 BQ2415X_BIT_OTG);
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,
336 BQ2415X_BIT_BOOST);
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,
343 BQ2415X_BIT_TE);
344 case BQ2415X_CHARGE_TERMINATION_ENABLE:
345 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
346 1, BQ2415X_BIT_TE);
347 case BQ2415X_CHARGE_TERMINATION_DISABLE:
348 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
349 0, BQ2415X_BIT_TE);
350 case BQ2415X_CHARGER_STATUS:
351 ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
352 BQ2415X_BIT_CE);
353 if (ret < 0)
354 return ret;
355 else
356 return ret > 0 ? 0 : 1;
357 case BQ2415X_CHARGER_ENABLE:
358 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
359 0, BQ2415X_BIT_CE);
360 case BQ2415X_CHARGER_DISABLE:
361 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
362 1, BQ2415X_BIT_CE);
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,
384 BQ2415X_BIT_OTG_PL);
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,
393 BQ2415X_BIT_OTG_EN);
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);
411 return -EINVAL;
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);
420 if (ret < 0)
421 return ret;
423 switch (client->addr) {
424 case 0x6b:
425 switch (ret) {
426 case 0:
427 if (bq->chip == BQ24151A)
428 return bq->chip;
429 else
430 return BQ24151;
431 case 1:
432 if (bq->chip == BQ24150A ||
433 bq->chip == BQ24152 ||
434 bq->chip == BQ24155)
435 return bq->chip;
436 else
437 return BQ24150;
438 case 2:
439 if (bq->chip == BQ24153A)
440 return bq->chip;
441 else
442 return BQ24153;
443 default:
444 return BQUNKNOWN;
446 break;
448 case 0x6a:
449 switch (ret) {
450 case 0:
451 if (bq->chip == BQ24156A)
452 return bq->chip;
453 else
454 return BQ24156;
455 case 2:
456 return BQ24158;
457 default:
458 return BQUNKNOWN;
460 break;
463 return BQUNKNOWN;
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)
473 return -1;
475 switch (chip) {
476 case BQ24150:
477 case BQ24150A:
478 case BQ24151:
479 case BQ24151A:
480 case BQ24152:
481 if (ret >= 0 && ret <= 3)
482 return ret;
483 else
484 return -1;
485 case BQ24153:
486 case BQ24153A:
487 case BQ24156:
488 case BQ24156A:
489 case BQ24158:
490 if (ret == 3)
491 return 0;
492 else if (ret == 1)
493 return 1;
494 else
495 return -1;
496 case BQ24155:
497 if (ret == 3)
498 return 3;
499 else
500 return -1;
501 case BQUNKNOWN:
502 return -1;
505 return -1;
508 /* return chip vender code */
509 static int bq2415x_get_vender_code(struct bq2415x_device *bq)
511 int ret;
513 ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
514 if (ret < 0)
515 return 0;
517 /* convert to binary */
518 return (ret & 0x1) +
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)
538 int val;
540 if (mA <= 100)
541 val = 0;
542 else if (mA <= 500)
543 val = 1;
544 else if (mA <= 800)
545 val = 2;
546 else
547 val = 3;
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)
556 int ret;
558 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
559 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
560 if (ret < 0)
561 return ret;
562 else if (ret == 0)
563 return 100;
564 else if (ret == 1)
565 return 500;
566 else if (ret == 2)
567 return 800;
568 else if (ret == 3)
569 return 1800;
570 return -EINVAL;
573 /* set weak battery voltage in mV */
574 static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
576 int val;
578 /* round to 100mV */
579 if (mV <= 3400 + 50)
580 val = 0;
581 else if (mV <= 3500 + 50)
582 val = 1;
583 else if (mV <= 3600 + 50)
584 val = 2;
585 else
586 val = 3;
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)
595 int ret;
597 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
598 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
599 if (ret < 0)
600 return ret;
601 return 100 * (34 + ret);
604 /* set battery regulation voltage in mV */
605 static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
606 int mV)
608 int val = (mV/10 - 350) / 2;
611 * According to datasheet, maximum battery regulation voltage is
612 * 4440mV which is b101111 = 47.
614 if (val < 0)
615 val = 0;
616 else if (val > 47)
617 return -EINVAL;
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);
629 if (ret < 0)
630 return ret;
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)
637 int val;
639 if (bq->init_data.resistor_sense <= 0)
640 return -ENOSYS;
642 val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
643 if (val < 0)
644 val = 0;
645 else if (val > 7)
646 val = 7;
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)
656 int ret;
658 if (bq->init_data.resistor_sense <= 0)
659 return -ENOSYS;
661 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
662 BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG);
663 if (ret < 0)
664 return ret;
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)
671 int val;
673 if (bq->init_data.resistor_sense <= 0)
674 return -ENOSYS;
676 val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
677 if (val < 0)
678 val = 0;
679 else if (val > 7)
680 val = 7;
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)
690 int ret;
692 if (bq->init_data.resistor_sense <= 0)
693 return -ENOSYS;
695 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
696 BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM);
697 if (ret < 0)
698 return ret;
699 return (3400 + 3400*ret) / bq->init_data.resistor_sense;
702 /* set default value of property */
703 #define bq2415x_set_default_value(bq, prop) \
704 do { \
705 int ret = 0; \
706 if (bq->init_data.prop != -1) \
707 ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
708 if (ret < 0) \
709 return ret; \
710 } while (0)
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);
730 return 0;
733 /**** charger mode functions ****/
735 /* set charger mode */
736 static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode)
738 int ret = 0;
739 int charger = 0;
740 int boost = 0;
742 if (mode == BQ2415X_MODE_BOOST)
743 boost = 1;
744 else if (mode != BQ2415X_MODE_OFF)
745 charger = 1;
747 if (!charger)
748 ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
750 if (!boost)
751 ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
753 if (ret < 0)
754 return ret;
756 switch (mode) {
757 case BQ2415X_MODE_OFF:
758 dev_dbg(bq->dev, "changing mode to: Offline\n");
759 ret = bq2415x_set_current_limit(bq, 100);
760 break;
761 case BQ2415X_MODE_NONE:
762 dev_dbg(bq->dev, "changing mode to: N/A\n");
763 ret = bq2415x_set_current_limit(bq, 100);
764 break;
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);
768 break;
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);
772 break;
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);
776 break;
779 if (ret < 0)
780 return ret;
782 if (charger)
783 ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
784 else if (boost)
785 ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE);
787 if (ret < 0)
788 return ret;
790 bq2415x_set_default_value(bq, weak_battery_voltage);
791 bq2415x_set_default_value(bq, battery_regulation_voltage);
793 bq->mode = mode;
794 sysfs_notify(&bq->charger.dev->kobj, NULL, "mode");
796 return 0;
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;
808 int ret;
809 int mA;
811 if (val != PSY_EVENT_PROP_CHANGED)
812 return NOTIFY_OK;
814 if (psy != bq->notify_psy)
815 return NOTIFY_OK;
817 dev_dbg(bq->dev, "notifier call was called\n");
819 ret = psy->get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX, &prop);
820 if (ret != 0)
821 return NOTIFY_OK;
823 mA = prop.intval;
825 if (mA == 0)
826 mode = BQ2415X_MODE_OFF;
827 else if (mA < 500)
828 mode = BQ2415X_MODE_NONE;
829 else if (mA < 1800)
830 mode = BQ2415X_MODE_HOST_CHARGER;
831 else
832 mode = BQ2415X_MODE_DEDICATED_CHARGER;
834 if (bq->reported_mode == mode)
835 return NOTIFY_OK;
837 bq->reported_mode = mode;
839 /* if automode is not enabled do not tell about reported_mode */
840 if (bq->automode < 1)
841 return NOTIFY_OK;
843 sysfs_notify(&bq->charger.dev->kobj, NULL, "reported_mode");
844 bq2415x_set_mode(bq, bq->reported_mode);
846 return NOTIFY_OK;
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);
858 return;
861 bq->autotimer = state;
863 if (state) {
864 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
865 bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
866 bq->timer_error = NULL;
867 } else {
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)
881 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,
890 work.work);
891 int ret;
892 int error;
893 int boost;
895 if (!bq->autotimer)
896 return;
898 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
899 if (ret < 0) {
900 bq2415x_timer_error(bq, "Resetting timer failed");
901 return;
904 boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
905 if (boost < 0) {
906 bq2415x_timer_error(bq, "Unknown error");
907 return;
910 error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
911 if (error < 0) {
912 bq2415x_timer_error(bq, "Unknown error");
913 return;
916 if (boost) {
917 switch (error) {
918 /* Non fatal errors, chip is OK */
919 case 0: /* No error */
920 break;
921 case 6: /* Timer expired */
922 dev_err(bq->dev, "Timer expired\n");
923 break;
924 case 3: /* Battery voltage too low */
925 dev_err(bq->dev, "Battery voltage to low\n");
926 break;
928 /* Fatal errors, disable and reset chip */
929 case 1: /* Overvoltage protection (chip fried) */
930 bq2415x_timer_error(bq,
931 "Overvoltage protection (chip fried)");
932 return;
933 case 2: /* Overload */
934 bq2415x_timer_error(bq, "Overload");
935 return;
936 case 4: /* Battery overvoltage protection */
937 bq2415x_timer_error(bq,
938 "Battery overvoltage protection");
939 return;
940 case 5: /* Thermal shutdown (too hot) */
941 bq2415x_timer_error(bq,
942 "Thermal shutdown (too hot)");
943 return;
944 case 7: /* N/A */
945 bq2415x_timer_error(bq, "Unknown error");
946 return;
948 } else {
949 switch (error) {
950 /* Non fatal errors, chip is OK */
951 case 0: /* No error */
952 break;
953 case 2: /* Sleep mode */
954 dev_err(bq->dev, "Sleep mode\n");
955 break;
956 case 3: /* Poor input source */
957 dev_err(bq->dev, "Poor input source\n");
958 break;
959 case 6: /* Timer expired */
960 dev_err(bq->dev, "Timer expired\n");
961 break;
962 case 7: /* No battery */
963 dev_err(bq->dev, "No battery\n");
964 break;
966 /* Fatal errors, disable and reset chip */
967 case 1: /* Overvoltage protection (chip fried) */
968 bq2415x_timer_error(bq,
969 "Overvoltage protection (chip fried)");
970 return;
971 case 4: /* Battery overvoltage protection */
972 bq2415x_timer_error(bq,
973 "Battery overvoltage protection");
974 return;
975 case 5: /* Thermal shutdown (too hot) */
976 bq2415x_timer_error(bq,
977 "Thermal shutdown (too hot)");
978 return;
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,
998 charger);
999 int ret;
1001 switch (psp) {
1002 case POWER_SUPPLY_PROP_STATUS:
1003 ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
1004 if (ret < 0)
1005 return ret;
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;
1012 else
1013 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
1014 break;
1015 case POWER_SUPPLY_PROP_MODEL_NAME:
1016 val->strval = bq->model;
1017 break;
1018 default:
1019 return -EINVAL;
1021 return 0;
1024 static int bq2415x_power_supply_init(struct bq2415x_device *bq)
1026 int ret;
1027 int chip;
1028 char revstr[8];
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);
1037 if (ret < 0)
1038 chip = BQUNKNOWN;
1039 else
1040 chip = ret;
1042 ret = bq2415x_detect_revision(bq);
1043 if (ret < 0)
1044 strcpy(revstr, "unknown");
1045 else
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));
1052 if (!bq->model) {
1053 dev_err(bq->dev, "failed to allocate model name\n");
1054 return -ENOMEM;
1057 ret = power_supply_register(bq->dev, &bq->charger);
1058 if (ret) {
1059 kfree(bq->model);
1060 return ret;
1063 return 0;
1066 static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
1068 bq->autotimer = 0;
1069 if (bq->automode > 0)
1070 bq->automode = 0;
1071 cancel_delayed_work_sync(&bq->work);
1072 power_supply_unregister(&bq->charger);
1073 kfree(bq->model);
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,
1081 char *buf)
1083 struct power_supply *psy = dev_get_drvdata(dev);
1084 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1085 charger);
1086 enum bq2415x_command command;
1087 int ret;
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;
1097 else
1098 return -EINVAL;
1100 ret = bq2415x_exec_command(bq, command);
1101 if (ret < 0)
1102 return ret;
1103 return sprintf(buf, "%d\n", ret);
1107 * set timer entry:
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,
1114 const char *buf,
1115 size_t count)
1117 struct power_supply *psy = dev_get_drvdata(dev);
1118 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1119 charger);
1120 int ret = 0;
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);
1126 else
1127 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
1129 if (ret < 0)
1130 return ret;
1131 return count;
1134 /* show timer entry (auto or off) */
1135 static ssize_t bq2415x_sysfs_show_timer(struct device *dev,
1136 struct device_attribute *attr,
1137 char *buf)
1139 struct power_supply *psy = dev_get_drvdata(dev);
1140 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1141 charger);
1143 if (bq->timer_error)
1144 return sprintf(buf, "%s\n", bq->timer_error);
1146 if (bq->autotimer)
1147 return sprintf(buf, "auto\n");
1148 return sprintf(buf, "off\n");
1152 * set mode entry:
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,
1161 const char *buf,
1162 size_t count)
1164 struct power_supply *psy = dev_get_drvdata(dev);
1165 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1166 charger);
1167 enum bq2415x_mode mode;
1168 int ret = 0;
1170 if (strncmp(buf, "auto", 4) == 0) {
1171 if (bq->automode < 0)
1172 return -ENOSYS;
1173 bq->automode = 1;
1174 mode = bq->reported_mode;
1175 } else if (strncmp(buf, "off", 3) == 0) {
1176 if (bq->automode > 0)
1177 bq->automode = 0;
1178 mode = BQ2415X_MODE_OFF;
1179 } else if (strncmp(buf, "none", 4) == 0) {
1180 if (bq->automode > 0)
1181 bq->automode = 0;
1182 mode = BQ2415X_MODE_NONE;
1183 } else if (strncmp(buf, "host", 4) == 0) {
1184 if (bq->automode > 0)
1185 bq->automode = 0;
1186 mode = BQ2415X_MODE_HOST_CHARGER;
1187 } else if (strncmp(buf, "dedicated", 9) == 0) {
1188 if (bq->automode > 0)
1189 bq->automode = 0;
1190 mode = BQ2415X_MODE_DEDICATED_CHARGER;
1191 } else if (strncmp(buf, "boost", 5) == 0) {
1192 if (bq->automode > 0)
1193 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)
1199 return count;
1200 bq->automode = 1;
1201 mode = bq->reported_mode;
1202 } else {
1203 return -EINVAL;
1206 ret = bq2415x_set_mode(bq, mode);
1207 if (ret < 0)
1208 return ret;
1209 return count;
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,
1215 char *buf)
1217 struct power_supply *psy = dev_get_drvdata(dev);
1218 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1219 charger);
1220 ssize_t ret = 0;
1222 if (bq->automode > 0)
1223 ret += sprintf(buf+ret, "auto (");
1225 switch (bq->mode) {
1226 case BQ2415X_MODE_OFF:
1227 ret += sprintf(buf+ret, "off");
1228 break;
1229 case BQ2415X_MODE_NONE:
1230 ret += sprintf(buf+ret, "none");
1231 break;
1232 case BQ2415X_MODE_HOST_CHARGER:
1233 ret += sprintf(buf+ret, "host");
1234 break;
1235 case BQ2415X_MODE_DEDICATED_CHARGER:
1236 ret += sprintf(buf+ret, "dedicated");
1237 break;
1238 case BQ2415X_MODE_BOOST:
1239 ret += sprintf(buf+ret, "boost");
1240 break;
1243 if (bq->automode > 0)
1244 ret += sprintf(buf+ret, ")");
1246 ret += sprintf(buf+ret, "\n");
1247 return ret;
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,
1253 char *buf)
1255 struct power_supply *psy = dev_get_drvdata(dev);
1256 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1257 charger);
1259 if (bq->automode < 0)
1260 return -EINVAL;
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");
1275 return -EINVAL;
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,
1281 const char *buf,
1282 size_t count)
1284 struct power_supply *psy = dev_get_drvdata(dev);
1285 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1286 charger);
1287 ssize_t ret = 0;
1288 unsigned int reg;
1289 unsigned int val;
1291 if (sscanf(buf, "%x %x", &reg, &val) != 2)
1292 return -EINVAL;
1294 if (reg > 4 || val > 255)
1295 return -EINVAL;
1297 ret = bq2415x_i2c_write(bq, reg, val);
1298 if (ret < 0)
1299 return ret;
1300 return count;
1303 /* print value of chip register, format: 'register=value' */
1304 static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
1305 u8 reg,
1306 char *buf)
1308 int ret = bq2415x_i2c_read(bq, reg);
1310 if (ret < 0)
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,
1318 char *buf)
1320 struct power_supply *psy = dev_get_drvdata(dev);
1321 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1322 charger);
1323 ssize_t ret = 0;
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);
1330 return 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,
1336 const char *buf,
1337 size_t count)
1339 struct power_supply *psy = dev_get_drvdata(dev);
1340 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1341 charger);
1342 long val;
1343 int ret;
1345 if (kstrtol(buf, 10, &val) < 0)
1346 return -EINVAL;
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);
1358 else
1359 return -EINVAL;
1361 if (ret < 0)
1362 return ret;
1363 return count;
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,
1369 char *buf)
1371 struct power_supply *psy = dev_get_drvdata(dev);
1372 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1373 charger);
1374 int ret;
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);
1386 else
1387 return -EINVAL;
1389 if (ret < 0)
1390 return ret;
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,
1397 const char *buf,
1398 size_t count)
1400 struct power_supply *psy = dev_get_drvdata(dev);
1401 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1402 charger);
1403 enum bq2415x_command command;
1404 long val;
1405 int ret;
1407 if (kstrtol(buf, 10, &val) < 0)
1408 return -EINVAL;
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;
1422 else
1423 return -EINVAL;
1425 ret = bq2415x_exec_command(bq, command);
1426 if (ret < 0)
1427 return ret;
1428 return count;
1431 /* show *_enable entries */
1432 static ssize_t bq2415x_sysfs_show_enable(struct device *dev,
1433 struct device_attribute *attr,
1434 char *buf)
1436 struct power_supply *psy = dev_get_drvdata(dev);
1437 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1438 charger);
1439 enum bq2415x_command command;
1440 int ret;
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;
1450 else
1451 return -EINVAL;
1453 ret = bq2415x_exec_command(bq, command);
1454 if (ret < 0)
1455 return ret;
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,
1520 NULL,
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)
1542 int ret;
1543 int num;
1544 char *name;
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");
1551 return -ENODEV;
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);
1558 if (num < 0)
1559 return num;
1561 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
1562 if (!name) {
1563 dev_err(&client->dev, "failed to allocate device name\n");
1564 ret = -ENOMEM;
1565 goto error_1;
1568 bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
1569 if (!bq) {
1570 dev_err(&client->dev, "failed to allocate device data\n");
1571 ret = -ENOMEM;
1572 goto error_2;
1575 if (np) {
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);
1583 else
1584 bq->notify_psy = NULL;
1586 i2c_set_clientdata(client, bq);
1588 bq->id = num;
1589 bq->dev = &client->dev;
1590 bq->chip = id->driver_data;
1591 bq->name = name;
1592 bq->mode = BQ2415X_MODE_OFF;
1593 bq->reported_mode = BQ2415X_MODE_OFF;
1594 bq->autotimer = 0;
1595 bq->automode = 0;
1597 if (np) {
1598 ret = of_property_read_u32(np, "ti,current-limit",
1599 &bq->init_data.current_limit);
1600 if (ret)
1601 return ret;
1602 ret = of_property_read_u32(np, "ti,weak-battery-voltage",
1603 &bq->init_data.weak_battery_voltage);
1604 if (ret)
1605 return ret;
1606 ret = of_property_read_u32(np, "ti,battery-regulation-voltage",
1607 &bq->init_data.battery_regulation_voltage);
1608 if (ret)
1609 return ret;
1610 ret = of_property_read_u32(np, "ti,charge-current",
1611 &bq->init_data.charge_current);
1612 if (ret)
1613 return ret;
1614 ret = of_property_read_u32(np, "ti,termination-current",
1615 &bq->init_data.termination_current);
1616 if (ret)
1617 return ret;
1618 ret = of_property_read_u32(np, "ti,resistor-sense",
1619 &bq->init_data.resistor_sense);
1620 if (ret)
1621 return ret;
1622 } else {
1623 memcpy(&bq->init_data, pdata, sizeof(bq->init_data));
1626 bq2415x_reset_chip(bq);
1628 ret = bq2415x_power_supply_init(bq);
1629 if (ret) {
1630 dev_err(bq->dev, "failed to register power supply: %d\n", ret);
1631 goto error_2;
1634 ret = bq2415x_sysfs_init(bq);
1635 if (ret) {
1636 dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret);
1637 goto error_3;
1640 ret = bq2415x_set_defaults(bq);
1641 if (ret) {
1642 dev_err(bq->dev, "failed to set default values: %d\n", ret);
1643 goto error_4;
1646 if (bq->notify_psy) {
1647 bq->nb.notifier_call = bq2415x_notifier_call;
1648 ret = power_supply_reg_notifier(&bq->nb);
1649 if (ret) {
1650 dev_err(bq->dev, "failed to reg notifier: %d\n", ret);
1651 goto error_5;
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);
1658 bq->automode = 1;
1659 dev_info(bq->dev, "automode enabled\n");
1660 } else {
1661 bq->automode = -1;
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");
1669 return 0;
1671 error_5:
1672 error_4:
1673 bq2415x_sysfs_exit(bq);
1674 error_3:
1675 bq2415x_power_supply_exit(bq);
1676 error_2:
1677 kfree(name);
1678 error_1:
1679 mutex_lock(&bq2415x_id_mutex);
1680 idr_remove(&bq2415x_id, num);
1681 mutex_unlock(&bq2415x_id_mutex);
1683 return ret;
1686 /* main bq2415x remove function */
1688 static int bq2415x_remove(struct i2c_client *client)
1690 struct bq2415x_device *bq = i2c_get_clientdata(client);
1692 if (bq->notify_psy)
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");
1706 kfree(bq->name);
1708 return 0;
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 = {
1729 .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");