gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / power / bq2415x_charger.c
blob6c534dcbc19cb3c8639da09bef6b48281ac3cd44
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 * Datasheets:
17 * http://www.ti.com/product/bq24150
18 * http://www.ti.com/product/bq24150a
19 * http://www.ti.com/product/bq24152
20 * http://www.ti.com/product/bq24153
21 * http://www.ti.com/product/bq24153a
22 * http://www.ti.com/product/bq24155
23 * http://www.ti.com/product/bq24157s
24 * http://www.ti.com/product/bq24158
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/param.h>
30 #include <linux/err.h>
31 #include <linux/workqueue.h>
32 #include <linux/sysfs.h>
33 #include <linux/platform_device.h>
34 #include <linux/power_supply.h>
35 #include <linux/idr.h>
36 #include <linux/i2c.h>
37 #include <linux/slab.h>
39 #include <linux/power/bq2415x_charger.h>
41 /* timeout for resetting chip timer */
42 #define BQ2415X_TIMER_TIMEOUT 10
44 #define BQ2415X_REG_STATUS 0x00
45 #define BQ2415X_REG_CONTROL 0x01
46 #define BQ2415X_REG_VOLTAGE 0x02
47 #define BQ2415X_REG_VENDER 0x03
48 #define BQ2415X_REG_CURRENT 0x04
50 /* reset state for all registers */
51 #define BQ2415X_RESET_STATUS BIT(6)
52 #define BQ2415X_RESET_CONTROL (BIT(4)|BIT(5))
53 #define BQ2415X_RESET_VOLTAGE (BIT(1)|BIT(3))
54 #define BQ2415X_RESET_CURRENT (BIT(0)|BIT(3)|BIT(7))
56 /* status register */
57 #define BQ2415X_BIT_TMR_RST 7
58 #define BQ2415X_BIT_OTG 7
59 #define BQ2415X_BIT_EN_STAT 6
60 #define BQ2415X_MASK_STAT (BIT(4)|BIT(5))
61 #define BQ2415X_SHIFT_STAT 4
62 #define BQ2415X_BIT_BOOST 3
63 #define BQ2415X_MASK_FAULT (BIT(0)|BIT(1)|BIT(2))
64 #define BQ2415X_SHIFT_FAULT 0
66 /* control register */
67 #define BQ2415X_MASK_LIMIT (BIT(6)|BIT(7))
68 #define BQ2415X_SHIFT_LIMIT 6
69 #define BQ2415X_MASK_VLOWV (BIT(4)|BIT(5))
70 #define BQ2415X_SHIFT_VLOWV 4
71 #define BQ2415X_BIT_TE 3
72 #define BQ2415X_BIT_CE 2
73 #define BQ2415X_BIT_HZ_MODE 1
74 #define BQ2415X_BIT_OPA_MODE 0
76 /* voltage register */
77 #define BQ2415X_MASK_VO (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
78 #define BQ2415X_SHIFT_VO 2
79 #define BQ2415X_BIT_OTG_PL 1
80 #define BQ2415X_BIT_OTG_EN 0
82 /* vender register */
83 #define BQ2415X_MASK_VENDER (BIT(5)|BIT(6)|BIT(7))
84 #define BQ2415X_SHIFT_VENDER 5
85 #define BQ2415X_MASK_PN (BIT(3)|BIT(4))
86 #define BQ2415X_SHIFT_PN 3
87 #define BQ2415X_MASK_REVISION (BIT(0)|BIT(1)|BIT(2))
88 #define BQ2415X_SHIFT_REVISION 0
90 /* current register */
91 #define BQ2415X_MASK_RESET BIT(7)
92 #define BQ2415X_MASK_VI_CHRG (BIT(4)|BIT(5)|BIT(6))
93 #define BQ2415X_SHIFT_VI_CHRG 4
94 /* N/A BIT(3) */
95 #define BQ2415X_MASK_VI_TERM (BIT(0)|BIT(1)|BIT(2))
96 #define BQ2415X_SHIFT_VI_TERM 0
99 enum bq2415x_command {
100 BQ2415X_TIMER_RESET,
101 BQ2415X_OTG_STATUS,
102 BQ2415X_STAT_PIN_STATUS,
103 BQ2415X_STAT_PIN_ENABLE,
104 BQ2415X_STAT_PIN_DISABLE,
105 BQ2415X_CHARGE_STATUS,
106 BQ2415X_BOOST_STATUS,
107 BQ2415X_FAULT_STATUS,
109 BQ2415X_CHARGE_TERMINATION_STATUS,
110 BQ2415X_CHARGE_TERMINATION_ENABLE,
111 BQ2415X_CHARGE_TERMINATION_DISABLE,
112 BQ2415X_CHARGER_STATUS,
113 BQ2415X_CHARGER_ENABLE,
114 BQ2415X_CHARGER_DISABLE,
115 BQ2415X_HIGH_IMPEDANCE_STATUS,
116 BQ2415X_HIGH_IMPEDANCE_ENABLE,
117 BQ2415X_HIGH_IMPEDANCE_DISABLE,
118 BQ2415X_BOOST_MODE_STATUS,
119 BQ2415X_BOOST_MODE_ENABLE,
120 BQ2415X_BOOST_MODE_DISABLE,
122 BQ2415X_OTG_LEVEL,
123 BQ2415X_OTG_ACTIVATE_HIGH,
124 BQ2415X_OTG_ACTIVATE_LOW,
125 BQ2415X_OTG_PIN_STATUS,
126 BQ2415X_OTG_PIN_ENABLE,
127 BQ2415X_OTG_PIN_DISABLE,
129 BQ2415X_VENDER_CODE,
130 BQ2415X_PART_NUMBER,
131 BQ2415X_REVISION,
134 enum bq2415x_chip {
135 BQUNKNOWN,
136 BQ24150,
137 BQ24150A,
138 BQ24151,
139 BQ24151A,
140 BQ24152,
141 BQ24153,
142 BQ24153A,
143 BQ24155,
144 BQ24156,
145 BQ24156A,
146 BQ24157S,
147 BQ24158,
150 static char *bq2415x_chip_name[] = {
151 "unknown",
152 "bq24150",
153 "bq24150a",
154 "bq24151",
155 "bq24151a",
156 "bq24152",
157 "bq24153",
158 "bq24153a",
159 "bq24155",
160 "bq24156",
161 "bq24156a",
162 "bq24157s",
163 "bq24158",
166 struct bq2415x_device {
167 struct device *dev;
168 struct bq2415x_platform_data init_data;
169 struct power_supply *charger;
170 struct power_supply_desc charger_desc;
171 struct delayed_work work;
172 struct power_supply *notify_psy;
173 struct notifier_block nb;
174 enum bq2415x_mode reported_mode;/* mode reported by hook function */
175 enum bq2415x_mode mode; /* currently configured mode */
176 enum bq2415x_chip chip;
177 const char *timer_error;
178 char *model;
179 char *name;
180 int autotimer; /* 1 - if driver automatically reset timer, 0 - not */
181 int automode; /* 1 - enabled, 0 - disabled; -1 - not supported */
182 int id;
185 /* each registered chip must have unique id */
186 static DEFINE_IDR(bq2415x_id);
188 static DEFINE_MUTEX(bq2415x_id_mutex);
189 static DEFINE_MUTEX(bq2415x_timer_mutex);
190 static DEFINE_MUTEX(bq2415x_i2c_mutex);
192 /**** i2c read functions ****/
194 /* read value from register */
195 static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg)
197 struct i2c_client *client = to_i2c_client(bq->dev);
198 struct i2c_msg msg[2];
199 u8 val;
200 int ret;
202 if (!client->adapter)
203 return -ENODEV;
205 msg[0].addr = client->addr;
206 msg[0].flags = 0;
207 msg[0].buf = &reg;
208 msg[0].len = sizeof(reg);
209 msg[1].addr = client->addr;
210 msg[1].flags = I2C_M_RD;
211 msg[1].buf = &val;
212 msg[1].len = sizeof(val);
214 mutex_lock(&bq2415x_i2c_mutex);
215 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
216 mutex_unlock(&bq2415x_i2c_mutex);
218 if (ret < 0)
219 return ret;
221 return val;
224 /* read value from register, apply mask and right shift it */
225 static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
226 u8 mask, u8 shift)
228 int ret;
230 if (shift > 8)
231 return -EINVAL;
233 ret = bq2415x_i2c_read(bq, reg);
234 if (ret < 0)
235 return ret;
236 return (ret & mask) >> shift;
239 /* read value from register and return one specified bit */
240 static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit)
242 if (bit > 8)
243 return -EINVAL;
244 return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit);
247 /**** i2c write functions ****/
249 /* write value to register */
250 static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val)
252 struct i2c_client *client = to_i2c_client(bq->dev);
253 struct i2c_msg msg[1];
254 u8 data[2];
255 int ret;
257 data[0] = reg;
258 data[1] = val;
260 msg[0].addr = client->addr;
261 msg[0].flags = 0;
262 msg[0].buf = data;
263 msg[0].len = ARRAY_SIZE(data);
265 mutex_lock(&bq2415x_i2c_mutex);
266 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
267 mutex_unlock(&bq2415x_i2c_mutex);
269 /* i2c_transfer returns number of messages transferred */
270 if (ret < 0)
271 return ret;
272 else if (ret != 1)
273 return -EIO;
275 return 0;
278 /* read value from register, change it with mask left shifted and write back */
279 static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val,
280 u8 mask, u8 shift)
282 int ret;
284 if (shift > 8)
285 return -EINVAL;
287 ret = bq2415x_i2c_read(bq, reg);
288 if (ret < 0)
289 return ret;
291 ret &= ~mask;
292 ret |= val << shift;
294 return bq2415x_i2c_write(bq, reg, ret);
297 /* change only one bit in register */
298 static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg,
299 bool val, u8 bit)
301 if (bit > 8)
302 return -EINVAL;
303 return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit);
306 /**** global functions ****/
308 /* exec command function */
309 static int bq2415x_exec_command(struct bq2415x_device *bq,
310 enum bq2415x_command command)
312 int ret;
314 switch (command) {
315 case BQ2415X_TIMER_RESET:
316 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS,
317 1, BQ2415X_BIT_TMR_RST);
318 case BQ2415X_OTG_STATUS:
319 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
320 BQ2415X_BIT_OTG);
321 case BQ2415X_STAT_PIN_STATUS:
322 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
323 BQ2415X_BIT_EN_STAT);
324 case BQ2415X_STAT_PIN_ENABLE:
325 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1,
326 BQ2415X_BIT_EN_STAT);
327 case BQ2415X_STAT_PIN_DISABLE:
328 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0,
329 BQ2415X_BIT_EN_STAT);
330 case BQ2415X_CHARGE_STATUS:
331 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
332 BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT);
333 case BQ2415X_BOOST_STATUS:
334 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
335 BQ2415X_BIT_BOOST);
336 case BQ2415X_FAULT_STATUS:
337 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
338 BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT);
340 case BQ2415X_CHARGE_TERMINATION_STATUS:
341 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
342 BQ2415X_BIT_TE);
343 case BQ2415X_CHARGE_TERMINATION_ENABLE:
344 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
345 1, BQ2415X_BIT_TE);
346 case BQ2415X_CHARGE_TERMINATION_DISABLE:
347 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
348 0, BQ2415X_BIT_TE);
349 case BQ2415X_CHARGER_STATUS:
350 ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
351 BQ2415X_BIT_CE);
352 if (ret < 0)
353 return ret;
354 return ret > 0 ? 0 : 1;
355 case BQ2415X_CHARGER_ENABLE:
356 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
357 0, BQ2415X_BIT_CE);
358 case BQ2415X_CHARGER_DISABLE:
359 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
360 1, BQ2415X_BIT_CE);
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,
382 BQ2415X_BIT_OTG_PL);
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,
391 BQ2415X_BIT_OTG_EN);
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);
409 return -EINVAL;
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);
418 if (ret < 0)
419 return ret;
421 switch (client->addr) {
422 case 0x6b:
423 switch (ret) {
424 case 0:
425 if (bq->chip == BQ24151A)
426 return bq->chip;
427 return BQ24151;
428 case 1:
429 if (bq->chip == BQ24150A ||
430 bq->chip == BQ24152 ||
431 bq->chip == BQ24155)
432 return bq->chip;
433 return BQ24150;
434 case 2:
435 if (bq->chip == BQ24153A)
436 return bq->chip;
437 return BQ24153;
438 default:
439 return BQUNKNOWN;
441 break;
443 case 0x6a:
444 switch (ret) {
445 case 0:
446 if (bq->chip == BQ24156A)
447 return bq->chip;
448 return BQ24156;
449 case 2:
450 if (bq->chip == BQ24157S)
451 return bq->chip;
452 return BQ24158;
453 default:
454 return BQUNKNOWN;
456 break;
459 return BQUNKNOWN;
462 /* detect chip revision */
463 static int bq2415x_detect_revision(struct bq2415x_device *bq)
465 int ret = bq2415x_exec_command(bq, BQ2415X_REVISION);
466 int chip = bq2415x_detect_chip(bq);
468 if (ret < 0 || chip < 0)
469 return -1;
471 switch (chip) {
472 case BQ24150:
473 case BQ24150A:
474 case BQ24151:
475 case BQ24151A:
476 case BQ24152:
477 if (ret >= 0 && ret <= 3)
478 return ret;
479 return -1;
480 case BQ24153:
481 case BQ24153A:
482 case BQ24156:
483 case BQ24156A:
484 case BQ24157S:
485 case BQ24158:
486 if (ret == 3)
487 return 0;
488 else if (ret == 1)
489 return 1;
490 return -1;
491 case BQ24155:
492 if (ret == 3)
493 return 3;
494 return -1;
495 case BQUNKNOWN:
496 return -1;
499 return -1;
502 /* return chip vender code */
503 static int bq2415x_get_vender_code(struct bq2415x_device *bq)
505 int ret;
507 ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
508 if (ret < 0)
509 return 0;
511 /* convert to binary */
512 return (ret & 0x1) +
513 ((ret >> 1) & 0x1) * 10 +
514 ((ret >> 2) & 0x1) * 100;
517 /* reset all chip registers to default state */
518 static void bq2415x_reset_chip(struct bq2415x_device *bq)
520 bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT);
521 bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE);
522 bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL);
523 bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS);
524 bq->timer_error = NULL;
527 /**** properties functions ****/
529 /* set current limit in mA */
530 static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA)
532 int val;
534 if (mA <= 100)
535 val = 0;
536 else if (mA <= 500)
537 val = 1;
538 else if (mA <= 800)
539 val = 2;
540 else
541 val = 3;
543 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
544 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
547 /* get current limit in mA */
548 static int bq2415x_get_current_limit(struct bq2415x_device *bq)
550 int ret;
552 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
553 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
554 if (ret < 0)
555 return ret;
556 else if (ret == 0)
557 return 100;
558 else if (ret == 1)
559 return 500;
560 else if (ret == 2)
561 return 800;
562 else if (ret == 3)
563 return 1800;
564 return -EINVAL;
567 /* set weak battery voltage in mV */
568 static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
570 int val;
572 /* round to 100mV */
573 if (mV <= 3400 + 50)
574 val = 0;
575 else if (mV <= 3500 + 50)
576 val = 1;
577 else if (mV <= 3600 + 50)
578 val = 2;
579 else
580 val = 3;
582 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
583 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
586 /* get weak battery voltage in mV */
587 static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq)
589 int ret;
591 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
592 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
593 if (ret < 0)
594 return ret;
595 return 100 * (34 + ret);
598 /* set battery regulation voltage in mV */
599 static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
600 int mV)
602 int val = (mV/10 - 350) / 2;
605 * According to datasheet, maximum battery regulation voltage is
606 * 4440mV which is b101111 = 47.
608 if (val < 0)
609 val = 0;
610 else if (val > 47)
611 return -EINVAL;
613 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val,
614 BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
617 /* get battery regulation voltage in mV */
618 static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq)
620 int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE,
621 BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
623 if (ret < 0)
624 return ret;
625 return 10 * (350 + 2*ret);
628 /* set charge current in mA (platform data must provide resistor sense) */
629 static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA)
631 int val;
633 if (bq->init_data.resistor_sense <= 0)
634 return -ENOSYS;
636 val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
637 if (val < 0)
638 val = 0;
639 else if (val > 7)
640 val = 7;
642 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
643 BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET,
644 BQ2415X_SHIFT_VI_CHRG);
647 /* get charge current in mA (platform data must provide resistor sense) */
648 static int bq2415x_get_charge_current(struct bq2415x_device *bq)
650 int ret;
652 if (bq->init_data.resistor_sense <= 0)
653 return -ENOSYS;
655 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
656 BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG);
657 if (ret < 0)
658 return ret;
659 return (37400 + 6800*ret) / bq->init_data.resistor_sense;
662 /* set termination current in mA (platform data must provide resistor sense) */
663 static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA)
665 int val;
667 if (bq->init_data.resistor_sense <= 0)
668 return -ENOSYS;
670 val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
671 if (val < 0)
672 val = 0;
673 else if (val > 7)
674 val = 7;
676 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
677 BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET,
678 BQ2415X_SHIFT_VI_TERM);
681 /* get termination current in mA (platform data must provide resistor sense) */
682 static int bq2415x_get_termination_current(struct bq2415x_device *bq)
684 int ret;
686 if (bq->init_data.resistor_sense <= 0)
687 return -ENOSYS;
689 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
690 BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM);
691 if (ret < 0)
692 return ret;
693 return (3400 + 3400*ret) / bq->init_data.resistor_sense;
696 /* set default value of property */
697 #define bq2415x_set_default_value(bq, prop) \
698 do { \
699 int ret = 0; \
700 if (bq->init_data.prop != -1) \
701 ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
702 if (ret < 0) \
703 return ret; \
704 } while (0)
706 /* set default values of all properties */
707 static int bq2415x_set_defaults(struct bq2415x_device *bq)
709 bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
710 bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
711 bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE);
713 bq2415x_set_default_value(bq, current_limit);
714 bq2415x_set_default_value(bq, weak_battery_voltage);
715 bq2415x_set_default_value(bq, battery_regulation_voltage);
717 if (bq->init_data.resistor_sense > 0) {
718 bq2415x_set_default_value(bq, charge_current);
719 bq2415x_set_default_value(bq, termination_current);
720 bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE);
723 bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
724 return 0;
727 /**** charger mode functions ****/
729 /* set charger mode */
730 static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode)
732 int ret = 0;
733 int charger = 0;
734 int boost = 0;
736 if (mode == BQ2415X_MODE_BOOST)
737 boost = 1;
738 else if (mode != BQ2415X_MODE_OFF)
739 charger = 1;
741 if (!charger)
742 ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
744 if (!boost)
745 ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
747 if (ret < 0)
748 return ret;
750 switch (mode) {
751 case BQ2415X_MODE_OFF:
752 dev_dbg(bq->dev, "changing mode to: Offline\n");
753 ret = bq2415x_set_current_limit(bq, 100);
754 break;
755 case BQ2415X_MODE_NONE:
756 dev_dbg(bq->dev, "changing mode to: N/A\n");
757 ret = bq2415x_set_current_limit(bq, 100);
758 break;
759 case BQ2415X_MODE_HOST_CHARGER:
760 dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n");
761 ret = bq2415x_set_current_limit(bq, 500);
762 break;
763 case BQ2415X_MODE_DEDICATED_CHARGER:
764 dev_dbg(bq->dev, "changing mode to: Dedicated charger\n");
765 ret = bq2415x_set_current_limit(bq, 1800);
766 break;
767 case BQ2415X_MODE_BOOST: /* Boost mode */
768 dev_dbg(bq->dev, "changing mode to: Boost\n");
769 ret = bq2415x_set_current_limit(bq, 100);
770 break;
773 if (ret < 0)
774 return ret;
776 if (charger)
777 ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
778 else if (boost)
779 ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE);
781 if (ret < 0)
782 return ret;
784 bq2415x_set_default_value(bq, weak_battery_voltage);
785 bq2415x_set_default_value(bq, battery_regulation_voltage);
787 bq->mode = mode;
788 sysfs_notify(&bq->charger->dev.kobj, NULL, "mode");
790 return 0;
794 static int bq2415x_notifier_call(struct notifier_block *nb,
795 unsigned long val, void *v)
797 struct bq2415x_device *bq =
798 container_of(nb, struct bq2415x_device, nb);
799 struct power_supply *psy = v;
800 enum bq2415x_mode mode;
801 union power_supply_propval prop;
802 int ret;
803 int mA;
805 if (val != PSY_EVENT_PROP_CHANGED)
806 return NOTIFY_OK;
808 if (psy != bq->notify_psy)
809 return NOTIFY_OK;
811 dev_dbg(bq->dev, "notifier call was called\n");
813 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX,
814 &prop);
815 if (ret != 0)
816 return NOTIFY_OK;
818 mA = prop.intval;
820 if (mA == 0)
821 mode = BQ2415X_MODE_OFF;
822 else if (mA < 500)
823 mode = BQ2415X_MODE_NONE;
824 else if (mA < 1800)
825 mode = BQ2415X_MODE_HOST_CHARGER;
826 else
827 mode = BQ2415X_MODE_DEDICATED_CHARGER;
829 if (bq->reported_mode == mode)
830 return NOTIFY_OK;
832 bq->reported_mode = mode;
834 /* if automode is not enabled do not tell about reported_mode */
835 if (bq->automode < 1)
836 return NOTIFY_OK;
838 schedule_delayed_work(&bq->work, 0);
840 return NOTIFY_OK;
843 /**** timer functions ****/
845 /* enable/disable auto resetting chip timer */
846 static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
848 mutex_lock(&bq2415x_timer_mutex);
850 if (bq->autotimer == state) {
851 mutex_unlock(&bq2415x_timer_mutex);
852 return;
855 bq->autotimer = state;
857 if (state) {
858 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
859 bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
860 bq->timer_error = NULL;
861 } else {
862 cancel_delayed_work_sync(&bq->work);
865 mutex_unlock(&bq2415x_timer_mutex);
868 /* called by bq2415x_timer_work on timer error */
869 static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
871 bq->timer_error = msg;
872 sysfs_notify(&bq->charger->dev.kobj, NULL, "timer");
873 dev_err(bq->dev, "%s\n", msg);
874 if (bq->automode > 0)
875 bq->automode = 0;
876 bq2415x_set_mode(bq, BQ2415X_MODE_OFF);
877 bq2415x_set_autotimer(bq, 0);
880 /* delayed work function for auto resetting chip timer */
881 static void bq2415x_timer_work(struct work_struct *work)
883 struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
884 work.work);
885 int ret;
886 int error;
887 int boost;
889 if (bq->automode > 0 && (bq->reported_mode != bq->mode)) {
890 sysfs_notify(&bq->charger->dev.kobj, NULL, "reported_mode");
891 bq2415x_set_mode(bq, bq->reported_mode);
894 if (!bq->autotimer)
895 return;
897 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
898 if (ret < 0) {
899 bq2415x_timer_error(bq, "Resetting timer failed");
900 return;
903 boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
904 if (boost < 0) {
905 bq2415x_timer_error(bq, "Unknown error");
906 return;
909 error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
910 if (error < 0) {
911 bq2415x_timer_error(bq, "Unknown error");
912 return;
915 if (boost) {
916 switch (error) {
917 /* Non fatal errors, chip is OK */
918 case 0: /* No error */
919 break;
920 case 6: /* Timer expired */
921 dev_err(bq->dev, "Timer expired\n");
922 break;
923 case 3: /* Battery voltage too low */
924 dev_err(bq->dev, "Battery voltage to low\n");
925 break;
927 /* Fatal errors, disable and reset chip */
928 case 1: /* Overvoltage protection (chip fried) */
929 bq2415x_timer_error(bq,
930 "Overvoltage protection (chip fried)");
931 return;
932 case 2: /* Overload */
933 bq2415x_timer_error(bq, "Overload");
934 return;
935 case 4: /* Battery overvoltage protection */
936 bq2415x_timer_error(bq,
937 "Battery overvoltage protection");
938 return;
939 case 5: /* Thermal shutdown (too hot) */
940 bq2415x_timer_error(bq,
941 "Thermal shutdown (too hot)");
942 return;
943 case 7: /* N/A */
944 bq2415x_timer_error(bq, "Unknown error");
945 return;
947 } else {
948 switch (error) {
949 /* Non fatal errors, chip is OK */
950 case 0: /* No error */
951 break;
952 case 2: /* Sleep mode */
953 dev_err(bq->dev, "Sleep mode\n");
954 break;
955 case 3: /* Poor input source */
956 dev_err(bq->dev, "Poor input source\n");
957 break;
958 case 6: /* Timer expired */
959 dev_err(bq->dev, "Timer expired\n");
960 break;
961 case 7: /* No battery */
962 dev_err(bq->dev, "No battery\n");
963 break;
965 /* Fatal errors, disable and reset chip */
966 case 1: /* Overvoltage protection (chip fried) */
967 bq2415x_timer_error(bq,
968 "Overvoltage protection (chip fried)");
969 return;
970 case 4: /* Battery overvoltage protection */
971 bq2415x_timer_error(bq,
972 "Battery overvoltage protection");
973 return;
974 case 5: /* Thermal shutdown (too hot) */
975 bq2415x_timer_error(bq,
976 "Thermal shutdown (too hot)");
977 return;
981 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
984 /**** power supply interface code ****/
986 static enum power_supply_property bq2415x_power_supply_props[] = {
987 /* TODO: maybe add more power supply properties */
988 POWER_SUPPLY_PROP_STATUS,
989 POWER_SUPPLY_PROP_MODEL_NAME,
992 static int bq2415x_power_supply_get_property(struct power_supply *psy,
993 enum power_supply_property psp,
994 union power_supply_propval *val)
996 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
997 int ret;
999 switch (psp) {
1000 case POWER_SUPPLY_PROP_STATUS:
1001 ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
1002 if (ret < 0)
1003 return ret;
1004 else if (ret == 0) /* Ready */
1005 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1006 else if (ret == 1) /* Charge in progress */
1007 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1008 else if (ret == 2) /* Charge done */
1009 val->intval = POWER_SUPPLY_STATUS_FULL;
1010 else
1011 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
1012 break;
1013 case POWER_SUPPLY_PROP_MODEL_NAME:
1014 val->strval = bq->model;
1015 break;
1016 default:
1017 return -EINVAL;
1019 return 0;
1022 static int bq2415x_power_supply_init(struct bq2415x_device *bq)
1024 int ret;
1025 int chip;
1026 char revstr[8];
1027 struct power_supply_config psy_cfg = { .drv_data = bq, };
1029 bq->charger_desc.name = bq->name;
1030 bq->charger_desc.type = POWER_SUPPLY_TYPE_USB;
1031 bq->charger_desc.properties = bq2415x_power_supply_props;
1032 bq->charger_desc.num_properties =
1033 ARRAY_SIZE(bq2415x_power_supply_props);
1034 bq->charger_desc.get_property = bq2415x_power_supply_get_property;
1036 ret = bq2415x_detect_chip(bq);
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 bq->charger = power_supply_register(bq->dev, &bq->charger_desc,
1058 &psy_cfg);
1059 if (IS_ERR(bq->charger)) {
1060 kfree(bq->model);
1061 return PTR_ERR(bq->charger);
1064 return 0;
1067 static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
1069 bq->autotimer = 0;
1070 if (bq->automode > 0)
1071 bq->automode = 0;
1072 cancel_delayed_work_sync(&bq->work);
1073 power_supply_unregister(bq->charger);
1074 kfree(bq->model);
1077 /**** additional sysfs entries for power supply interface ****/
1079 /* show *_status entries */
1080 static ssize_t bq2415x_sysfs_show_status(struct device *dev,
1081 struct device_attribute *attr,
1082 char *buf)
1084 struct power_supply *psy = dev_get_drvdata(dev);
1085 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
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 = power_supply_get_drvdata(psy);
1119 int ret = 0;
1121 if (strncmp(buf, "auto", 4) == 0)
1122 bq2415x_set_autotimer(bq, 1);
1123 else if (strncmp(buf, "off", 3) == 0)
1124 bq2415x_set_autotimer(bq, 0);
1125 else
1126 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
1128 if (ret < 0)
1129 return ret;
1130 return count;
1133 /* show timer entry (auto or off) */
1134 static ssize_t bq2415x_sysfs_show_timer(struct device *dev,
1135 struct device_attribute *attr,
1136 char *buf)
1138 struct power_supply *psy = dev_get_drvdata(dev);
1139 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
1141 if (bq->timer_error)
1142 return sprintf(buf, "%s\n", bq->timer_error);
1144 if (bq->autotimer)
1145 return sprintf(buf, "auto\n");
1146 return sprintf(buf, "off\n");
1150 * set mode entry:
1151 * auto - if automode is supported, enable it and set mode to reported
1152 * none - disable charger and boost mode
1153 * host - charging mode for host/hub chargers (current limit 500mA)
1154 * dedicated - charging mode for dedicated chargers (unlimited current limit)
1155 * boost - disable charger and enable boost mode
1157 static ssize_t bq2415x_sysfs_set_mode(struct device *dev,
1158 struct device_attribute *attr,
1159 const char *buf,
1160 size_t count)
1162 struct power_supply *psy = dev_get_drvdata(dev);
1163 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
1164 enum bq2415x_mode mode;
1165 int ret = 0;
1167 if (strncmp(buf, "auto", 4) == 0) {
1168 if (bq->automode < 0)
1169 return -ENOSYS;
1170 bq->automode = 1;
1171 mode = bq->reported_mode;
1172 } else if (strncmp(buf, "off", 3) == 0) {
1173 if (bq->automode > 0)
1174 bq->automode = 0;
1175 mode = BQ2415X_MODE_OFF;
1176 } else if (strncmp(buf, "none", 4) == 0) {
1177 if (bq->automode > 0)
1178 bq->automode = 0;
1179 mode = BQ2415X_MODE_NONE;
1180 } else if (strncmp(buf, "host", 4) == 0) {
1181 if (bq->automode > 0)
1182 bq->automode = 0;
1183 mode = BQ2415X_MODE_HOST_CHARGER;
1184 } else if (strncmp(buf, "dedicated", 9) == 0) {
1185 if (bq->automode > 0)
1186 bq->automode = 0;
1187 mode = BQ2415X_MODE_DEDICATED_CHARGER;
1188 } else if (strncmp(buf, "boost", 5) == 0) {
1189 if (bq->automode > 0)
1190 bq->automode = 0;
1191 mode = BQ2415X_MODE_BOOST;
1192 } else if (strncmp(buf, "reset", 5) == 0) {
1193 bq2415x_reset_chip(bq);
1194 bq2415x_set_defaults(bq);
1195 if (bq->automode <= 0)
1196 return count;
1197 bq->automode = 1;
1198 mode = bq->reported_mode;
1199 } else {
1200 return -EINVAL;
1203 ret = bq2415x_set_mode(bq, mode);
1204 if (ret < 0)
1205 return ret;
1206 return count;
1209 /* show mode entry (auto, none, host, dedicated or boost) */
1210 static ssize_t bq2415x_sysfs_show_mode(struct device *dev,
1211 struct device_attribute *attr,
1212 char *buf)
1214 struct power_supply *psy = dev_get_drvdata(dev);
1215 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
1216 ssize_t ret = 0;
1218 if (bq->automode > 0)
1219 ret += sprintf(buf+ret, "auto (");
1221 switch (bq->mode) {
1222 case BQ2415X_MODE_OFF:
1223 ret += sprintf(buf+ret, "off");
1224 break;
1225 case BQ2415X_MODE_NONE:
1226 ret += sprintf(buf+ret, "none");
1227 break;
1228 case BQ2415X_MODE_HOST_CHARGER:
1229 ret += sprintf(buf+ret, "host");
1230 break;
1231 case BQ2415X_MODE_DEDICATED_CHARGER:
1232 ret += sprintf(buf+ret, "dedicated");
1233 break;
1234 case BQ2415X_MODE_BOOST:
1235 ret += sprintf(buf+ret, "boost");
1236 break;
1239 if (bq->automode > 0)
1240 ret += sprintf(buf+ret, ")");
1242 ret += sprintf(buf+ret, "\n");
1243 return ret;
1246 /* show reported_mode entry (none, host, dedicated or boost) */
1247 static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
1248 struct device_attribute *attr,
1249 char *buf)
1251 struct power_supply *psy = dev_get_drvdata(dev);
1252 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
1254 if (bq->automode < 0)
1255 return -EINVAL;
1257 switch (bq->reported_mode) {
1258 case BQ2415X_MODE_OFF:
1259 return sprintf(buf, "off\n");
1260 case BQ2415X_MODE_NONE:
1261 return sprintf(buf, "none\n");
1262 case BQ2415X_MODE_HOST_CHARGER:
1263 return sprintf(buf, "host\n");
1264 case BQ2415X_MODE_DEDICATED_CHARGER:
1265 return sprintf(buf, "dedicated\n");
1266 case BQ2415X_MODE_BOOST:
1267 return sprintf(buf, "boost\n");
1270 return -EINVAL;
1273 /* directly set raw value to chip register, format: 'register value' */
1274 static ssize_t bq2415x_sysfs_set_registers(struct device *dev,
1275 struct device_attribute *attr,
1276 const char *buf,
1277 size_t count)
1279 struct power_supply *psy = dev_get_drvdata(dev);
1280 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
1281 ssize_t ret = 0;
1282 unsigned int reg;
1283 unsigned int val;
1285 if (sscanf(buf, "%x %x", &reg, &val) != 2)
1286 return -EINVAL;
1288 if (reg > 4 || val > 255)
1289 return -EINVAL;
1291 ret = bq2415x_i2c_write(bq, reg, val);
1292 if (ret < 0)
1293 return ret;
1294 return count;
1297 /* print value of chip register, format: 'register=value' */
1298 static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
1299 u8 reg,
1300 char *buf)
1302 int ret = bq2415x_i2c_read(bq, reg);
1304 if (ret < 0)
1305 return sprintf(buf, "%#.2x=error %d\n", reg, ret);
1306 return sprintf(buf, "%#.2x=%#.2x\n", reg, ret);
1309 /* show all raw values of chip register, format per line: 'register=value' */
1310 static ssize_t bq2415x_sysfs_show_registers(struct device *dev,
1311 struct device_attribute *attr,
1312 char *buf)
1314 struct power_supply *psy = dev_get_drvdata(dev);
1315 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
1316 ssize_t ret = 0;
1318 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret);
1319 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret);
1320 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret);
1321 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret);
1322 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret);
1323 return ret;
1326 /* set current and voltage limit entries (in mA or mV) */
1327 static ssize_t bq2415x_sysfs_set_limit(struct device *dev,
1328 struct device_attribute *attr,
1329 const char *buf,
1330 size_t count)
1332 struct power_supply *psy = dev_get_drvdata(dev);
1333 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
1334 long val;
1335 int ret;
1337 if (kstrtol(buf, 10, &val) < 0)
1338 return -EINVAL;
1340 if (strcmp(attr->attr.name, "current_limit") == 0)
1341 ret = bq2415x_set_current_limit(bq, val);
1342 else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1343 ret = bq2415x_set_weak_battery_voltage(bq, val);
1344 else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1345 ret = bq2415x_set_battery_regulation_voltage(bq, val);
1346 else if (strcmp(attr->attr.name, "charge_current") == 0)
1347 ret = bq2415x_set_charge_current(bq, val);
1348 else if (strcmp(attr->attr.name, "termination_current") == 0)
1349 ret = bq2415x_set_termination_current(bq, val);
1350 else
1351 return -EINVAL;
1353 if (ret < 0)
1354 return ret;
1355 return count;
1358 /* show current and voltage limit entries (in mA or mV) */
1359 static ssize_t bq2415x_sysfs_show_limit(struct device *dev,
1360 struct device_attribute *attr,
1361 char *buf)
1363 struct power_supply *psy = dev_get_drvdata(dev);
1364 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
1365 int ret;
1367 if (strcmp(attr->attr.name, "current_limit") == 0)
1368 ret = bq2415x_get_current_limit(bq);
1369 else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1370 ret = bq2415x_get_weak_battery_voltage(bq);
1371 else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1372 ret = bq2415x_get_battery_regulation_voltage(bq);
1373 else if (strcmp(attr->attr.name, "charge_current") == 0)
1374 ret = bq2415x_get_charge_current(bq);
1375 else if (strcmp(attr->attr.name, "termination_current") == 0)
1376 ret = bq2415x_get_termination_current(bq);
1377 else
1378 return -EINVAL;
1380 if (ret < 0)
1381 return ret;
1382 return sprintf(buf, "%d\n", ret);
1385 /* set *_enable entries */
1386 static ssize_t bq2415x_sysfs_set_enable(struct device *dev,
1387 struct device_attribute *attr,
1388 const char *buf,
1389 size_t count)
1391 struct power_supply *psy = dev_get_drvdata(dev);
1392 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
1393 enum bq2415x_command command;
1394 long val;
1395 int ret;
1397 if (kstrtol(buf, 10, &val) < 0)
1398 return -EINVAL;
1400 if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1401 command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE :
1402 BQ2415X_CHARGE_TERMINATION_DISABLE;
1403 else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1404 command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE :
1405 BQ2415X_HIGH_IMPEDANCE_DISABLE;
1406 else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1407 command = val ? BQ2415X_OTG_PIN_ENABLE :
1408 BQ2415X_OTG_PIN_DISABLE;
1409 else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1410 command = val ? BQ2415X_STAT_PIN_ENABLE :
1411 BQ2415X_STAT_PIN_DISABLE;
1412 else
1413 return -EINVAL;
1415 ret = bq2415x_exec_command(bq, command);
1416 if (ret < 0)
1417 return ret;
1418 return count;
1421 /* show *_enable entries */
1422 static ssize_t bq2415x_sysfs_show_enable(struct device *dev,
1423 struct device_attribute *attr,
1424 char *buf)
1426 struct power_supply *psy = dev_get_drvdata(dev);
1427 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
1428 enum bq2415x_command command;
1429 int ret;
1431 if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1432 command = BQ2415X_CHARGE_TERMINATION_STATUS;
1433 else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1434 command = BQ2415X_HIGH_IMPEDANCE_STATUS;
1435 else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1436 command = BQ2415X_OTG_PIN_STATUS;
1437 else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1438 command = BQ2415X_STAT_PIN_STATUS;
1439 else
1440 return -EINVAL;
1442 ret = bq2415x_exec_command(bq, command);
1443 if (ret < 0)
1444 return ret;
1445 return sprintf(buf, "%d\n", ret);
1448 static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO,
1449 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1450 static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO,
1451 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1452 static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO,
1453 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1454 static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO,
1455 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1456 static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
1457 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1459 static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO,
1460 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1461 static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
1462 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1463 static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO,
1464 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1465 static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
1466 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1468 static DEVICE_ATTR(reported_mode, S_IRUGO,
1469 bq2415x_sysfs_show_reported_mode, NULL);
1470 static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
1471 bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
1472 static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
1473 bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer);
1475 static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO,
1476 bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers);
1478 static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1479 static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1480 static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1481 static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1483 static struct attribute *bq2415x_sysfs_attributes[] = {
1485 * TODO: some (appropriate) of these attrs should be switched to
1486 * use power supply class props.
1488 &dev_attr_current_limit.attr,
1489 &dev_attr_weak_battery_voltage.attr,
1490 &dev_attr_battery_regulation_voltage.attr,
1491 &dev_attr_charge_current.attr,
1492 &dev_attr_termination_current.attr,
1494 &dev_attr_charge_termination_enable.attr,
1495 &dev_attr_high_impedance_enable.attr,
1496 &dev_attr_otg_pin_enable.attr,
1497 &dev_attr_stat_pin_enable.attr,
1499 &dev_attr_reported_mode.attr,
1500 &dev_attr_mode.attr,
1501 &dev_attr_timer.attr,
1503 &dev_attr_registers.attr,
1505 &dev_attr_otg_status.attr,
1506 &dev_attr_charge_status.attr,
1507 &dev_attr_boost_status.attr,
1508 &dev_attr_fault_status.attr,
1509 NULL,
1512 static const struct attribute_group bq2415x_sysfs_attr_group = {
1513 .attrs = bq2415x_sysfs_attributes,
1516 static int bq2415x_sysfs_init(struct bq2415x_device *bq)
1518 return sysfs_create_group(&bq->charger->dev.kobj,
1519 &bq2415x_sysfs_attr_group);
1522 static void bq2415x_sysfs_exit(struct bq2415x_device *bq)
1524 sysfs_remove_group(&bq->charger->dev.kobj, &bq2415x_sysfs_attr_group);
1527 /* main bq2415x probe function */
1528 static int bq2415x_probe(struct i2c_client *client,
1529 const struct i2c_device_id *id)
1531 int ret;
1532 int num;
1533 char *name;
1534 struct bq2415x_device *bq;
1535 struct device_node *np = client->dev.of_node;
1536 struct bq2415x_platform_data *pdata = client->dev.platform_data;
1538 if (!np && !pdata) {
1539 dev_err(&client->dev, "platform data missing\n");
1540 return -ENODEV;
1543 /* Get new ID for the new device */
1544 mutex_lock(&bq2415x_id_mutex);
1545 num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL);
1546 mutex_unlock(&bq2415x_id_mutex);
1547 if (num < 0)
1548 return num;
1550 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
1551 if (!name) {
1552 dev_err(&client->dev, "failed to allocate device name\n");
1553 ret = -ENOMEM;
1554 goto error_1;
1557 bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
1558 if (!bq) {
1559 dev_err(&client->dev, "failed to allocate device data\n");
1560 ret = -ENOMEM;
1561 goto error_2;
1564 if (np) {
1565 bq->notify_psy = power_supply_get_by_phandle(np, "ti,usb-charger-detection");
1567 if (IS_ERR(bq->notify_psy)) {
1568 dev_info(&client->dev,
1569 "no 'ti,usb-charger-detection' property (err=%ld)\n",
1570 PTR_ERR(bq->notify_psy));
1571 bq->notify_psy = NULL;
1572 } else if (!bq->notify_psy) {
1573 ret = -EPROBE_DEFER;
1574 goto error_2;
1577 else if (pdata->notify_device)
1578 bq->notify_psy = power_supply_get_by_name(pdata->notify_device);
1579 else
1580 bq->notify_psy = NULL;
1582 i2c_set_clientdata(client, bq);
1584 bq->id = num;
1585 bq->dev = &client->dev;
1586 bq->chip = id->driver_data;
1587 bq->name = name;
1588 bq->mode = BQ2415X_MODE_OFF;
1589 bq->reported_mode = BQ2415X_MODE_OFF;
1590 bq->autotimer = 0;
1591 bq->automode = 0;
1593 if (np) {
1594 ret = of_property_read_u32(np, "ti,current-limit",
1595 &bq->init_data.current_limit);
1596 if (ret)
1597 goto error_3;
1598 ret = of_property_read_u32(np, "ti,weak-battery-voltage",
1599 &bq->init_data.weak_battery_voltage);
1600 if (ret)
1601 goto error_3;
1602 ret = of_property_read_u32(np, "ti,battery-regulation-voltage",
1603 &bq->init_data.battery_regulation_voltage);
1604 if (ret)
1605 goto error_3;
1606 ret = of_property_read_u32(np, "ti,charge-current",
1607 &bq->init_data.charge_current);
1608 if (ret)
1609 goto error_3;
1610 ret = of_property_read_u32(np, "ti,termination-current",
1611 &bq->init_data.termination_current);
1612 if (ret)
1613 goto error_3;
1614 ret = of_property_read_u32(np, "ti,resistor-sense",
1615 &bq->init_data.resistor_sense);
1616 if (ret)
1617 goto error_3;
1618 } else {
1619 memcpy(&bq->init_data, pdata, sizeof(bq->init_data));
1622 bq2415x_reset_chip(bq);
1624 ret = bq2415x_power_supply_init(bq);
1625 if (ret) {
1626 dev_err(bq->dev, "failed to register power supply: %d\n", ret);
1627 goto error_3;
1630 ret = bq2415x_sysfs_init(bq);
1631 if (ret) {
1632 dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret);
1633 goto error_4;
1636 ret = bq2415x_set_defaults(bq);
1637 if (ret) {
1638 dev_err(bq->dev, "failed to set default values: %d\n", ret);
1639 goto error_5;
1642 if (bq->notify_psy) {
1643 bq->nb.notifier_call = bq2415x_notifier_call;
1644 ret = power_supply_reg_notifier(&bq->nb);
1645 if (ret) {
1646 dev_err(bq->dev, "failed to reg notifier: %d\n", ret);
1647 goto error_6;
1650 /* Query for initial reported_mode and set it */
1651 bq2415x_notifier_call(&bq->nb, PSY_EVENT_PROP_CHANGED, bq->notify_psy);
1652 bq2415x_set_mode(bq, bq->reported_mode);
1654 bq->automode = 1;
1655 dev_info(bq->dev, "automode enabled\n");
1656 } else {
1657 bq->automode = -1;
1658 dev_info(bq->dev, "automode not supported\n");
1661 INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work);
1662 bq2415x_set_autotimer(bq, 1);
1664 dev_info(bq->dev, "driver registered\n");
1665 return 0;
1667 error_6:
1668 error_5:
1669 bq2415x_sysfs_exit(bq);
1670 error_4:
1671 bq2415x_power_supply_exit(bq);
1672 error_3:
1673 if (bq->notify_psy)
1674 power_supply_put(bq->notify_psy);
1675 error_2:
1676 kfree(name);
1677 error_1:
1678 mutex_lock(&bq2415x_id_mutex);
1679 idr_remove(&bq2415x_id, num);
1680 mutex_unlock(&bq2415x_id_mutex);
1682 return ret;
1685 /* main bq2415x remove function */
1687 static int bq2415x_remove(struct i2c_client *client)
1689 struct bq2415x_device *bq = i2c_get_clientdata(client);
1691 if (bq->notify_psy) {
1692 power_supply_unreg_notifier(&bq->nb);
1693 power_supply_put(bq->notify_psy);
1696 bq2415x_sysfs_exit(bq);
1697 bq2415x_power_supply_exit(bq);
1699 bq2415x_reset_chip(bq);
1701 mutex_lock(&bq2415x_id_mutex);
1702 idr_remove(&bq2415x_id, bq->id);
1703 mutex_unlock(&bq2415x_id_mutex);
1705 dev_info(bq->dev, "driver unregistered\n");
1707 kfree(bq->name);
1709 return 0;
1712 static const struct i2c_device_id bq2415x_i2c_id_table[] = {
1713 { "bq2415x", BQUNKNOWN },
1714 { "bq24150", BQ24150 },
1715 { "bq24150a", BQ24150A },
1716 { "bq24151", BQ24151 },
1717 { "bq24151a", BQ24151A },
1718 { "bq24152", BQ24152 },
1719 { "bq24153", BQ24153 },
1720 { "bq24153a", BQ24153A },
1721 { "bq24155", BQ24155 },
1722 { "bq24156", BQ24156 },
1723 { "bq24156a", BQ24156A },
1724 { "bq24157s", BQ24157S },
1725 { "bq24158", BQ24158 },
1728 MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table);
1730 static struct i2c_driver bq2415x_driver = {
1731 .driver = {
1732 .name = "bq2415x-charger",
1734 .probe = bq2415x_probe,
1735 .remove = bq2415x_remove,
1736 .id_table = bq2415x_i2c_id_table,
1738 module_i2c_driver(bq2415x_driver);
1740 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
1741 MODULE_DESCRIPTION("bq2415x charger driver");
1742 MODULE_LICENSE("GPL");