mmc: sdhci-pxav3: handle mmc_of_parse() errors during probe
[linux-2.6.git] / drivers / power / bq2415x_charger.c
blob0727f9256138589cf0184756b46adc5f216df4bd
1 /*
2 * bq2415x charger driver
4 * Copyright (C) 2011-2012 Pali Rohár <pali.rohar@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 enum bq2415x_mode reported_mode;/* mode reported by hook function */
174 enum bq2415x_mode mode; /* current configured mode */
175 enum bq2415x_chip chip;
176 const char *timer_error;
177 char *model;
178 char *name;
179 int autotimer; /* 1 - if driver automatically reset timer, 0 - not */
180 int automode; /* 1 - enabled, 0 - disabled; -1 - not supported */
181 int id;
184 /* each registered chip must have unique id */
185 static DEFINE_IDR(bq2415x_id);
187 static DEFINE_MUTEX(bq2415x_id_mutex);
188 static DEFINE_MUTEX(bq2415x_timer_mutex);
189 static DEFINE_MUTEX(bq2415x_i2c_mutex);
191 /**** i2c read functions ****/
193 /* read value from register */
194 static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg)
196 struct i2c_client *client = to_i2c_client(bq->dev);
197 struct i2c_msg msg[2];
198 u8 val;
199 int ret;
201 if (!client->adapter)
202 return -ENODEV;
204 msg[0].addr = client->addr;
205 msg[0].flags = 0;
206 msg[0].buf = &reg;
207 msg[0].len = sizeof(reg);
208 msg[1].addr = client->addr;
209 msg[1].flags = I2C_M_RD;
210 msg[1].buf = &val;
211 msg[1].len = sizeof(val);
213 mutex_lock(&bq2415x_i2c_mutex);
214 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
215 mutex_unlock(&bq2415x_i2c_mutex);
217 if (ret < 0)
218 return ret;
220 return val;
223 /* read value from register, apply mask and right shift it */
224 static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
225 u8 mask, u8 shift)
227 int ret;
229 if (shift > 8)
230 return -EINVAL;
232 ret = bq2415x_i2c_read(bq, reg);
233 if (ret < 0)
234 return ret;
235 return (ret & mask) >> shift;
238 /* read value from register and return one specified bit */
239 static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit)
241 if (bit > 8)
242 return -EINVAL;
243 return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit);
246 /**** i2c write functions ****/
248 /* write value to register */
249 static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val)
251 struct i2c_client *client = to_i2c_client(bq->dev);
252 struct i2c_msg msg[1];
253 u8 data[2];
254 int ret;
256 data[0] = reg;
257 data[1] = val;
259 msg[0].addr = client->addr;
260 msg[0].flags = 0;
261 msg[0].buf = data;
262 msg[0].len = ARRAY_SIZE(data);
264 mutex_lock(&bq2415x_i2c_mutex);
265 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
266 mutex_unlock(&bq2415x_i2c_mutex);
268 /* i2c_transfer returns number of messages transferred */
269 if (ret < 0)
270 return ret;
271 else if (ret != 1)
272 return -EIO;
274 return 0;
277 /* read value from register, change it with mask left shifted and write back */
278 static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val,
279 u8 mask, u8 shift)
281 int ret;
283 if (shift > 8)
284 return -EINVAL;
286 ret = bq2415x_i2c_read(bq, reg);
287 if (ret < 0)
288 return ret;
290 ret &= ~mask;
291 ret |= val << shift;
293 return bq2415x_i2c_write(bq, reg, ret);
296 /* change only one bit in register */
297 static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg,
298 bool val, u8 bit)
300 if (bit > 8)
301 return -EINVAL;
302 return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit);
305 /**** global functions ****/
307 /* exec command function */
308 static int bq2415x_exec_command(struct bq2415x_device *bq,
309 enum bq2415x_command command)
311 int ret;
313 switch (command) {
314 case BQ2415X_TIMER_RESET:
315 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS,
316 1, BQ2415X_BIT_TMR_RST);
317 case BQ2415X_OTG_STATUS:
318 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
319 BQ2415X_BIT_OTG);
320 case BQ2415X_STAT_PIN_STATUS:
321 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
322 BQ2415X_BIT_EN_STAT);
323 case BQ2415X_STAT_PIN_ENABLE:
324 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1,
325 BQ2415X_BIT_EN_STAT);
326 case BQ2415X_STAT_PIN_DISABLE:
327 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0,
328 BQ2415X_BIT_EN_STAT);
329 case BQ2415X_CHARGE_STATUS:
330 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
331 BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT);
332 case BQ2415X_BOOST_STATUS:
333 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
334 BQ2415X_BIT_BOOST);
335 case BQ2415X_FAULT_STATUS:
336 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
337 BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT);
339 case BQ2415X_CHARGE_TERMINATION_STATUS:
340 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
341 BQ2415X_BIT_TE);
342 case BQ2415X_CHARGE_TERMINATION_ENABLE:
343 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
344 1, BQ2415X_BIT_TE);
345 case BQ2415X_CHARGE_TERMINATION_DISABLE:
346 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
347 0, BQ2415X_BIT_TE);
348 case BQ2415X_CHARGER_STATUS:
349 ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
350 BQ2415X_BIT_CE);
351 if (ret < 0)
352 return ret;
353 else
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 else
428 return BQ24151;
429 case 1:
430 if (bq->chip == BQ24150A ||
431 bq->chip == BQ24152 ||
432 bq->chip == BQ24155)
433 return bq->chip;
434 else
435 return BQ24150;
436 case 2:
437 if (bq->chip == BQ24153A)
438 return bq->chip;
439 else
440 return BQ24153;
441 default:
442 return BQUNKNOWN;
444 break;
446 case 0x6a:
447 switch (ret) {
448 case 0:
449 if (bq->chip == BQ24156A)
450 return bq->chip;
451 else
452 return BQ24156;
453 case 2:
454 return BQ24158;
455 default:
456 return BQUNKNOWN;
458 break;
461 return BQUNKNOWN;
464 /* detect chip revision */
465 static int bq2415x_detect_revision(struct bq2415x_device *bq)
467 int ret = bq2415x_exec_command(bq, BQ2415X_REVISION);
468 int chip = bq2415x_detect_chip(bq);
470 if (ret < 0 || chip < 0)
471 return -1;
473 switch (chip) {
474 case BQ24150:
475 case BQ24150A:
476 case BQ24151:
477 case BQ24151A:
478 case BQ24152:
479 if (ret >= 0 && ret <= 3)
480 return ret;
481 else
482 return -1;
483 case BQ24153:
484 case BQ24153A:
485 case BQ24156:
486 case BQ24156A:
487 case BQ24158:
488 if (ret == 3)
489 return 0;
490 else if (ret == 1)
491 return 1;
492 else
493 return -1;
494 case BQ24155:
495 if (ret == 3)
496 return 3;
497 else
498 return -1;
499 case BQUNKNOWN:
500 return -1;
503 return -1;
506 /* return chip vender code */
507 static int bq2415x_get_vender_code(struct bq2415x_device *bq)
509 int ret;
511 ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
512 if (ret < 0)
513 return 0;
515 /* convert to binary */
516 return (ret & 0x1) +
517 ((ret >> 1) & 0x1) * 10 +
518 ((ret >> 2) & 0x1) * 100;
521 /* reset all chip registers to default state */
522 static void bq2415x_reset_chip(struct bq2415x_device *bq)
524 bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT);
525 bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE);
526 bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL);
527 bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS);
528 bq->timer_error = NULL;
531 /**** properties functions ****/
533 /* set current limit in mA */
534 static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA)
536 int val;
538 if (mA <= 100)
539 val = 0;
540 else if (mA <= 500)
541 val = 1;
542 else if (mA <= 800)
543 val = 2;
544 else
545 val = 3;
547 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
548 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
551 /* get current limit in mA */
552 static int bq2415x_get_current_limit(struct bq2415x_device *bq)
554 int ret;
556 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
557 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
558 if (ret < 0)
559 return ret;
560 else if (ret == 0)
561 return 100;
562 else if (ret == 1)
563 return 500;
564 else if (ret == 2)
565 return 800;
566 else if (ret == 3)
567 return 1800;
568 return -EINVAL;
571 /* set weak battery voltage in mV */
572 static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
574 int val;
576 /* round to 100mV */
577 if (mV <= 3400 + 50)
578 val = 0;
579 else if (mV <= 3500 + 50)
580 val = 1;
581 else if (mV <= 3600 + 50)
582 val = 2;
583 else
584 val = 3;
586 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
587 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
590 /* get weak battery voltage in mV */
591 static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq)
593 int ret;
595 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
596 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
597 if (ret < 0)
598 return ret;
599 return 100 * (34 + ret);
602 /* set battery regulation voltage in mV */
603 static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
604 int mV)
606 int val = (mV/10 - 350) / 2;
608 if (val < 0)
609 val = 0;
610 else if (val > 94) /* FIXME: Max is 94 or 122 ? Set max value ? */
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 /* hook function called by other driver which set reported mode */
795 static void bq2415x_hook_function(enum bq2415x_mode mode, void *data)
797 struct bq2415x_device *bq = data;
799 if (!bq)
800 return;
802 dev_dbg(bq->dev, "hook function was called\n");
803 bq->reported_mode = mode;
805 /* if automode is not enabled do not tell about reported_mode */
806 if (bq->automode < 1)
807 return;
809 sysfs_notify(&bq->charger.dev->kobj, NULL, "reported_mode");
810 bq2415x_set_mode(bq, bq->reported_mode);
814 /**** timer functions ****/
816 /* enable/disable auto resetting chip timer */
817 static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
819 mutex_lock(&bq2415x_timer_mutex);
821 if (bq->autotimer == state) {
822 mutex_unlock(&bq2415x_timer_mutex);
823 return;
826 bq->autotimer = state;
828 if (state) {
829 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
830 bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
831 bq->timer_error = NULL;
832 } else {
833 cancel_delayed_work_sync(&bq->work);
836 mutex_unlock(&bq2415x_timer_mutex);
839 /* called by bq2415x_timer_work on timer error */
840 static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
842 bq->timer_error = msg;
843 sysfs_notify(&bq->charger.dev->kobj, NULL, "timer");
844 dev_err(bq->dev, "%s\n", msg);
845 if (bq->automode > 0)
846 bq->automode = 0;
847 bq2415x_set_mode(bq, BQ2415X_MODE_OFF);
848 bq2415x_set_autotimer(bq, 0);
851 /* delayed work function for auto resetting chip timer */
852 static void bq2415x_timer_work(struct work_struct *work)
854 struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
855 work.work);
856 int ret;
857 int error;
858 int boost;
860 if (!bq->autotimer)
861 return;
863 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
864 if (ret < 0) {
865 bq2415x_timer_error(bq, "Resetting timer failed");
866 return;
869 boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
870 if (boost < 0) {
871 bq2415x_timer_error(bq, "Unknown error");
872 return;
875 error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
876 if (error < 0) {
877 bq2415x_timer_error(bq, "Unknown error");
878 return;
881 if (boost) {
882 switch (error) {
883 /* Non fatal errors, chip is OK */
884 case 0: /* No error */
885 break;
886 case 6: /* Timer expired */
887 dev_err(bq->dev, "Timer expired\n");
888 break;
889 case 3: /* Battery voltage too low */
890 dev_err(bq->dev, "Battery voltage to low\n");
891 break;
893 /* Fatal errors, disable and reset chip */
894 case 1: /* Overvoltage protection (chip fried) */
895 bq2415x_timer_error(bq,
896 "Overvoltage protection (chip fried)");
897 return;
898 case 2: /* Overload */
899 bq2415x_timer_error(bq, "Overload");
900 return;
901 case 4: /* Battery overvoltage protection */
902 bq2415x_timer_error(bq,
903 "Battery overvoltage protection");
904 return;
905 case 5: /* Thermal shutdown (too hot) */
906 bq2415x_timer_error(bq,
907 "Thermal shutdown (too hot)");
908 return;
909 case 7: /* N/A */
910 bq2415x_timer_error(bq, "Unknown error");
911 return;
913 } else {
914 switch (error) {
915 /* Non fatal errors, chip is OK */
916 case 0: /* No error */
917 break;
918 case 2: /* Sleep mode */
919 dev_err(bq->dev, "Sleep mode\n");
920 break;
921 case 3: /* Poor input source */
922 dev_err(bq->dev, "Poor input source\n");
923 break;
924 case 6: /* Timer expired */
925 dev_err(bq->dev, "Timer expired\n");
926 break;
927 case 7: /* No battery */
928 dev_err(bq->dev, "No battery\n");
929 break;
931 /* Fatal errors, disable and reset chip */
932 case 1: /* Overvoltage protection (chip fried) */
933 bq2415x_timer_error(bq,
934 "Overvoltage protection (chip fried)");
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;
947 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
950 /**** power supply interface code ****/
952 static enum power_supply_property bq2415x_power_supply_props[] = {
953 /* TODO: maybe add more power supply properties */
954 POWER_SUPPLY_PROP_STATUS,
955 POWER_SUPPLY_PROP_MODEL_NAME,
958 static int bq2415x_power_supply_get_property(struct power_supply *psy,
959 enum power_supply_property psp,
960 union power_supply_propval *val)
962 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
963 charger);
964 int ret;
966 switch (psp) {
967 case POWER_SUPPLY_PROP_STATUS:
968 ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
969 if (ret < 0)
970 return ret;
971 else if (ret == 0) /* Ready */
972 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
973 else if (ret == 1) /* Charge in progress */
974 val->intval = POWER_SUPPLY_STATUS_CHARGING;
975 else if (ret == 2) /* Charge done */
976 val->intval = POWER_SUPPLY_STATUS_FULL;
977 else
978 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
979 break;
980 case POWER_SUPPLY_PROP_MODEL_NAME:
981 val->strval = bq->model;
982 break;
983 default:
984 return -EINVAL;
986 return 0;
989 static int bq2415x_power_supply_init(struct bq2415x_device *bq)
991 int ret;
992 int chip;
993 char revstr[8];
995 bq->charger.name = bq->name;
996 bq->charger.type = POWER_SUPPLY_TYPE_USB;
997 bq->charger.properties = bq2415x_power_supply_props;
998 bq->charger.num_properties = ARRAY_SIZE(bq2415x_power_supply_props);
999 bq->charger.get_property = bq2415x_power_supply_get_property;
1001 ret = bq2415x_detect_chip(bq);
1002 if (ret < 0)
1003 chip = BQUNKNOWN;
1004 else
1005 chip = ret;
1007 ret = bq2415x_detect_revision(bq);
1008 if (ret < 0)
1009 strcpy(revstr, "unknown");
1010 else
1011 sprintf(revstr, "1.%d", ret);
1013 bq->model = kasprintf(GFP_KERNEL,
1014 "chip %s, revision %s, vender code %.3d",
1015 bq2415x_chip_name[chip], revstr,
1016 bq2415x_get_vender_code(bq));
1017 if (!bq->model) {
1018 dev_err(bq->dev, "failed to allocate model name\n");
1019 return -ENOMEM;
1022 ret = power_supply_register(bq->dev, &bq->charger);
1023 if (ret) {
1024 kfree(bq->model);
1025 return ret;
1028 return 0;
1031 static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
1033 bq->autotimer = 0;
1034 if (bq->automode > 0)
1035 bq->automode = 0;
1036 cancel_delayed_work_sync(&bq->work);
1037 power_supply_unregister(&bq->charger);
1038 kfree(bq->model);
1041 /**** additional sysfs entries for power supply interface ****/
1043 /* show *_status entries */
1044 static ssize_t bq2415x_sysfs_show_status(struct device *dev,
1045 struct device_attribute *attr,
1046 char *buf)
1048 struct power_supply *psy = dev_get_drvdata(dev);
1049 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1050 charger);
1051 enum bq2415x_command command;
1052 int ret;
1054 if (strcmp(attr->attr.name, "otg_status") == 0)
1055 command = BQ2415X_OTG_STATUS;
1056 else if (strcmp(attr->attr.name, "charge_status") == 0)
1057 command = BQ2415X_CHARGE_STATUS;
1058 else if (strcmp(attr->attr.name, "boost_status") == 0)
1059 command = BQ2415X_BOOST_STATUS;
1060 else if (strcmp(attr->attr.name, "fault_status") == 0)
1061 command = BQ2415X_FAULT_STATUS;
1062 else
1063 return -EINVAL;
1065 ret = bq2415x_exec_command(bq, command);
1066 if (ret < 0)
1067 return ret;
1068 return sprintf(buf, "%d\n", ret);
1072 * set timer entry:
1073 * auto - enable auto mode
1074 * off - disable auto mode
1075 * (other values) - reset chip timer
1077 static ssize_t bq2415x_sysfs_set_timer(struct device *dev,
1078 struct device_attribute *attr,
1079 const char *buf,
1080 size_t count)
1082 struct power_supply *psy = dev_get_drvdata(dev);
1083 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1084 charger);
1085 int ret = 0;
1087 if (strncmp(buf, "auto", 4) == 0)
1088 bq2415x_set_autotimer(bq, 1);
1089 else if (strncmp(buf, "off", 3) == 0)
1090 bq2415x_set_autotimer(bq, 0);
1091 else
1092 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
1094 if (ret < 0)
1095 return ret;
1096 return count;
1099 /* show timer entry (auto or off) */
1100 static ssize_t bq2415x_sysfs_show_timer(struct device *dev,
1101 struct device_attribute *attr,
1102 char *buf)
1104 struct power_supply *psy = dev_get_drvdata(dev);
1105 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1106 charger);
1108 if (bq->timer_error)
1109 return sprintf(buf, "%s\n", bq->timer_error);
1111 if (bq->autotimer)
1112 return sprintf(buf, "auto\n");
1113 return sprintf(buf, "off\n");
1117 * set mode entry:
1118 * auto - if automode is supported, enable it and set mode to reported
1119 * none - disable charger and boost mode
1120 * host - charging mode for host/hub chargers (current limit 500mA)
1121 * dedicated - charging mode for dedicated chargers (unlimited current limit)
1122 * boost - disable charger and enable boost mode
1124 static ssize_t bq2415x_sysfs_set_mode(struct device *dev,
1125 struct device_attribute *attr,
1126 const char *buf,
1127 size_t count)
1129 struct power_supply *psy = dev_get_drvdata(dev);
1130 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1131 charger);
1132 enum bq2415x_mode mode;
1133 int ret = 0;
1135 if (strncmp(buf, "auto", 4) == 0) {
1136 if (bq->automode < 0)
1137 return -ENOSYS;
1138 bq->automode = 1;
1139 mode = bq->reported_mode;
1140 } else if (strncmp(buf, "off", 3) == 0) {
1141 if (bq->automode > 0)
1142 bq->automode = 0;
1143 mode = BQ2415X_MODE_OFF;
1144 } else if (strncmp(buf, "none", 4) == 0) {
1145 if (bq->automode > 0)
1146 bq->automode = 0;
1147 mode = BQ2415X_MODE_NONE;
1148 } else if (strncmp(buf, "host", 4) == 0) {
1149 if (bq->automode > 0)
1150 bq->automode = 0;
1151 mode = BQ2415X_MODE_HOST_CHARGER;
1152 } else if (strncmp(buf, "dedicated", 9) == 0) {
1153 if (bq->automode > 0)
1154 bq->automode = 0;
1155 mode = BQ2415X_MODE_DEDICATED_CHARGER;
1156 } else if (strncmp(buf, "boost", 5) == 0) {
1157 if (bq->automode > 0)
1158 bq->automode = 0;
1159 mode = BQ2415X_MODE_BOOST;
1160 } else if (strncmp(buf, "reset", 5) == 0) {
1161 bq2415x_reset_chip(bq);
1162 bq2415x_set_defaults(bq);
1163 if (bq->automode <= 0)
1164 return count;
1165 bq->automode = 1;
1166 mode = bq->reported_mode;
1167 } else {
1168 return -EINVAL;
1171 ret = bq2415x_set_mode(bq, mode);
1172 if (ret < 0)
1173 return ret;
1174 return count;
1177 /* show mode entry (auto, none, host, dedicated or boost) */
1178 static ssize_t bq2415x_sysfs_show_mode(struct device *dev,
1179 struct device_attribute *attr,
1180 char *buf)
1182 struct power_supply *psy = dev_get_drvdata(dev);
1183 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1184 charger);
1185 ssize_t ret = 0;
1187 if (bq->automode > 0)
1188 ret += sprintf(buf+ret, "auto (");
1190 switch (bq->mode) {
1191 case BQ2415X_MODE_OFF:
1192 ret += sprintf(buf+ret, "off");
1193 break;
1194 case BQ2415X_MODE_NONE:
1195 ret += sprintf(buf+ret, "none");
1196 break;
1197 case BQ2415X_MODE_HOST_CHARGER:
1198 ret += sprintf(buf+ret, "host");
1199 break;
1200 case BQ2415X_MODE_DEDICATED_CHARGER:
1201 ret += sprintf(buf+ret, "dedicated");
1202 break;
1203 case BQ2415X_MODE_BOOST:
1204 ret += sprintf(buf+ret, "boost");
1205 break;
1208 if (bq->automode > 0)
1209 ret += sprintf(buf+ret, ")");
1211 ret += sprintf(buf+ret, "\n");
1212 return ret;
1215 /* show reported_mode entry (none, host, dedicated or boost) */
1216 static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
1217 struct device_attribute *attr,
1218 char *buf)
1220 struct power_supply *psy = dev_get_drvdata(dev);
1221 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1222 charger);
1224 if (bq->automode < 0)
1225 return -EINVAL;
1227 switch (bq->reported_mode) {
1228 case BQ2415X_MODE_OFF:
1229 return sprintf(buf, "off\n");
1230 case BQ2415X_MODE_NONE:
1231 return sprintf(buf, "none\n");
1232 case BQ2415X_MODE_HOST_CHARGER:
1233 return sprintf(buf, "host\n");
1234 case BQ2415X_MODE_DEDICATED_CHARGER:
1235 return sprintf(buf, "dedicated\n");
1236 case BQ2415X_MODE_BOOST:
1237 return sprintf(buf, "boost\n");
1240 return -EINVAL;
1243 /* directly set raw value to chip register, format: 'register value' */
1244 static ssize_t bq2415x_sysfs_set_registers(struct device *dev,
1245 struct device_attribute *attr,
1246 const char *buf,
1247 size_t count)
1249 struct power_supply *psy = dev_get_drvdata(dev);
1250 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1251 charger);
1252 ssize_t ret = 0;
1253 unsigned int reg;
1254 unsigned int val;
1256 if (sscanf(buf, "%x %x", &reg, &val) != 2)
1257 return -EINVAL;
1259 if (reg > 4 || val > 255)
1260 return -EINVAL;
1262 ret = bq2415x_i2c_write(bq, reg, val);
1263 if (ret < 0)
1264 return ret;
1265 return count;
1268 /* print value of chip register, format: 'register=value' */
1269 static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
1270 u8 reg,
1271 char *buf)
1273 int ret = bq2415x_i2c_read(bq, reg);
1275 if (ret < 0)
1276 return sprintf(buf, "%#.2x=error %d\n", reg, ret);
1277 return sprintf(buf, "%#.2x=%#.2x\n", reg, ret);
1280 /* show all raw values of chip register, format per line: 'register=value' */
1281 static ssize_t bq2415x_sysfs_show_registers(struct device *dev,
1282 struct device_attribute *attr,
1283 char *buf)
1285 struct power_supply *psy = dev_get_drvdata(dev);
1286 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1287 charger);
1288 ssize_t ret = 0;
1290 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret);
1291 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret);
1292 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret);
1293 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret);
1294 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret);
1295 return ret;
1298 /* set current and voltage limit entries (in mA or mV) */
1299 static ssize_t bq2415x_sysfs_set_limit(struct device *dev,
1300 struct device_attribute *attr,
1301 const char *buf,
1302 size_t count)
1304 struct power_supply *psy = dev_get_drvdata(dev);
1305 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1306 charger);
1307 long val;
1308 int ret;
1310 if (kstrtol(buf, 10, &val) < 0)
1311 return -EINVAL;
1313 if (strcmp(attr->attr.name, "current_limit") == 0)
1314 ret = bq2415x_set_current_limit(bq, val);
1315 else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1316 ret = bq2415x_set_weak_battery_voltage(bq, val);
1317 else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1318 ret = bq2415x_set_battery_regulation_voltage(bq, val);
1319 else if (strcmp(attr->attr.name, "charge_current") == 0)
1320 ret = bq2415x_set_charge_current(bq, val);
1321 else if (strcmp(attr->attr.name, "termination_current") == 0)
1322 ret = bq2415x_set_termination_current(bq, val);
1323 else
1324 return -EINVAL;
1326 if (ret < 0)
1327 return ret;
1328 return count;
1331 /* show current and voltage limit entries (in mA or mV) */
1332 static ssize_t bq2415x_sysfs_show_limit(struct device *dev,
1333 struct device_attribute *attr,
1334 char *buf)
1336 struct power_supply *psy = dev_get_drvdata(dev);
1337 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1338 charger);
1339 int ret;
1341 if (strcmp(attr->attr.name, "current_limit") == 0)
1342 ret = bq2415x_get_current_limit(bq);
1343 else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1344 ret = bq2415x_get_weak_battery_voltage(bq);
1345 else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1346 ret = bq2415x_get_battery_regulation_voltage(bq);
1347 else if (strcmp(attr->attr.name, "charge_current") == 0)
1348 ret = bq2415x_get_charge_current(bq);
1349 else if (strcmp(attr->attr.name, "termination_current") == 0)
1350 ret = bq2415x_get_termination_current(bq);
1351 else
1352 return -EINVAL;
1354 if (ret < 0)
1355 return ret;
1356 return sprintf(buf, "%d\n", ret);
1359 /* set *_enable entries */
1360 static ssize_t bq2415x_sysfs_set_enable(struct device *dev,
1361 struct device_attribute *attr,
1362 const char *buf,
1363 size_t count)
1365 struct power_supply *psy = dev_get_drvdata(dev);
1366 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1367 charger);
1368 enum bq2415x_command command;
1369 long val;
1370 int ret;
1372 if (kstrtol(buf, 10, &val) < 0)
1373 return -EINVAL;
1375 if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1376 command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE :
1377 BQ2415X_CHARGE_TERMINATION_DISABLE;
1378 else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1379 command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE :
1380 BQ2415X_HIGH_IMPEDANCE_DISABLE;
1381 else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1382 command = val ? BQ2415X_OTG_PIN_ENABLE :
1383 BQ2415X_OTG_PIN_DISABLE;
1384 else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1385 command = val ? BQ2415X_STAT_PIN_ENABLE :
1386 BQ2415X_STAT_PIN_DISABLE;
1387 else
1388 return -EINVAL;
1390 ret = bq2415x_exec_command(bq, command);
1391 if (ret < 0)
1392 return ret;
1393 return count;
1396 /* show *_enable entries */
1397 static ssize_t bq2415x_sysfs_show_enable(struct device *dev,
1398 struct device_attribute *attr,
1399 char *buf)
1401 struct power_supply *psy = dev_get_drvdata(dev);
1402 struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1403 charger);
1404 enum bq2415x_command command;
1405 int ret;
1407 if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1408 command = BQ2415X_CHARGE_TERMINATION_STATUS;
1409 else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1410 command = BQ2415X_HIGH_IMPEDANCE_STATUS;
1411 else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1412 command = BQ2415X_OTG_PIN_STATUS;
1413 else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1414 command = BQ2415X_STAT_PIN_STATUS;
1415 else
1416 return -EINVAL;
1418 ret = bq2415x_exec_command(bq, command);
1419 if (ret < 0)
1420 return ret;
1421 return sprintf(buf, "%d\n", ret);
1424 static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO,
1425 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1426 static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO,
1427 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1428 static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO,
1429 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1430 static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO,
1431 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1432 static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
1433 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1435 static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO,
1436 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1437 static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
1438 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1439 static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO,
1440 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1441 static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
1442 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1444 static DEVICE_ATTR(reported_mode, S_IRUGO,
1445 bq2415x_sysfs_show_reported_mode, NULL);
1446 static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
1447 bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
1448 static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
1449 bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer);
1451 static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO,
1452 bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers);
1454 static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1455 static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1456 static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1457 static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1459 static struct attribute *bq2415x_sysfs_attributes[] = {
1461 * TODO: some (appropriate) of these attrs should be switched to
1462 * use power supply class props.
1464 &dev_attr_current_limit.attr,
1465 &dev_attr_weak_battery_voltage.attr,
1466 &dev_attr_battery_regulation_voltage.attr,
1467 &dev_attr_charge_current.attr,
1468 &dev_attr_termination_current.attr,
1470 &dev_attr_charge_termination_enable.attr,
1471 &dev_attr_high_impedance_enable.attr,
1472 &dev_attr_otg_pin_enable.attr,
1473 &dev_attr_stat_pin_enable.attr,
1475 &dev_attr_reported_mode.attr,
1476 &dev_attr_mode.attr,
1477 &dev_attr_timer.attr,
1479 &dev_attr_registers.attr,
1481 &dev_attr_otg_status.attr,
1482 &dev_attr_charge_status.attr,
1483 &dev_attr_boost_status.attr,
1484 &dev_attr_fault_status.attr,
1485 NULL,
1488 static const struct attribute_group bq2415x_sysfs_attr_group = {
1489 .attrs = bq2415x_sysfs_attributes,
1492 static int bq2415x_sysfs_init(struct bq2415x_device *bq)
1494 return sysfs_create_group(&bq->charger.dev->kobj,
1495 &bq2415x_sysfs_attr_group);
1498 static void bq2415x_sysfs_exit(struct bq2415x_device *bq)
1500 sysfs_remove_group(&bq->charger.dev->kobj, &bq2415x_sysfs_attr_group);
1503 /* main bq2415x probe function */
1504 static int bq2415x_probe(struct i2c_client *client,
1505 const struct i2c_device_id *id)
1507 int ret;
1508 int num;
1509 char *name;
1510 struct bq2415x_device *bq;
1512 if (!client->dev.platform_data) {
1513 dev_err(&client->dev, "platform data not set\n");
1514 return -ENODEV;
1517 /* Get new ID for the new device */
1518 mutex_lock(&bq2415x_id_mutex);
1519 num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL);
1520 mutex_unlock(&bq2415x_id_mutex);
1521 if (num < 0)
1522 return num;
1524 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
1525 if (!name) {
1526 dev_err(&client->dev, "failed to allocate device name\n");
1527 ret = -ENOMEM;
1528 goto error_1;
1531 bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
1532 if (!bq) {
1533 dev_err(&client->dev, "failed to allocate device data\n");
1534 ret = -ENOMEM;
1535 goto error_2;
1538 i2c_set_clientdata(client, bq);
1540 bq->id = num;
1541 bq->dev = &client->dev;
1542 bq->chip = id->driver_data;
1543 bq->name = name;
1544 bq->mode = BQ2415X_MODE_OFF;
1545 bq->reported_mode = BQ2415X_MODE_OFF;
1546 bq->autotimer = 0;
1547 bq->automode = 0;
1549 memcpy(&bq->init_data, client->dev.platform_data,
1550 sizeof(bq->init_data));
1552 bq2415x_reset_chip(bq);
1554 ret = bq2415x_power_supply_init(bq);
1555 if (ret) {
1556 dev_err(bq->dev, "failed to register power supply: %d\n", ret);
1557 goto error_2;
1560 ret = bq2415x_sysfs_init(bq);
1561 if (ret) {
1562 dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret);
1563 goto error_3;
1566 ret = bq2415x_set_defaults(bq);
1567 if (ret) {
1568 dev_err(bq->dev, "failed to set default values: %d\n", ret);
1569 goto error_4;
1572 if (bq->init_data.set_mode_hook) {
1573 if (bq->init_data.set_mode_hook(
1574 bq2415x_hook_function, bq)) {
1575 bq->automode = 1;
1576 bq2415x_set_mode(bq, bq->reported_mode);
1577 dev_info(bq->dev, "automode enabled\n");
1578 } else {
1579 bq->automode = -1;
1580 dev_info(bq->dev, "automode failed\n");
1582 } else {
1583 bq->automode = -1;
1584 dev_info(bq->dev, "automode not supported\n");
1587 INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work);
1588 bq2415x_set_autotimer(bq, 1);
1590 dev_info(bq->dev, "driver registered\n");
1591 return 0;
1593 error_4:
1594 bq2415x_sysfs_exit(bq);
1595 error_3:
1596 bq2415x_power_supply_exit(bq);
1597 error_2:
1598 kfree(name);
1599 error_1:
1600 mutex_lock(&bq2415x_id_mutex);
1601 idr_remove(&bq2415x_id, num);
1602 mutex_unlock(&bq2415x_id_mutex);
1604 return ret;
1607 /* main bq2415x remove function */
1609 static int bq2415x_remove(struct i2c_client *client)
1611 struct bq2415x_device *bq = i2c_get_clientdata(client);
1613 if (bq->init_data.set_mode_hook)
1614 bq->init_data.set_mode_hook(NULL, NULL);
1616 bq2415x_sysfs_exit(bq);
1617 bq2415x_power_supply_exit(bq);
1619 bq2415x_reset_chip(bq);
1621 mutex_lock(&bq2415x_id_mutex);
1622 idr_remove(&bq2415x_id, bq->id);
1623 mutex_unlock(&bq2415x_id_mutex);
1625 dev_info(bq->dev, "driver unregistered\n");
1627 kfree(bq->name);
1629 return 0;
1632 static const struct i2c_device_id bq2415x_i2c_id_table[] = {
1633 { "bq2415x", BQUNKNOWN },
1634 { "bq24150", BQ24150 },
1635 { "bq24150a", BQ24150A },
1636 { "bq24151", BQ24151 },
1637 { "bq24151a", BQ24151A },
1638 { "bq24152", BQ24152 },
1639 { "bq24153", BQ24153 },
1640 { "bq24153a", BQ24153A },
1641 { "bq24155", BQ24155 },
1642 { "bq24156", BQ24156 },
1643 { "bq24156a", BQ24156A },
1644 { "bq24158", BQ24158 },
1647 MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table);
1649 static struct i2c_driver bq2415x_driver = {
1650 .driver = {
1651 .name = "bq2415x-charger",
1653 .probe = bq2415x_probe,
1654 .remove = bq2415x_remove,
1655 .id_table = bq2415x_i2c_id_table,
1657 module_i2c_driver(bq2415x_driver);
1659 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
1660 MODULE_DESCRIPTION("bq2415x charger driver");
1661 MODULE_LICENSE("GPL");