Update bq2415x_charger.patch: Show error message from bq2415x_timer_error in timer...
[kernel-power] / kernel-power-2.6.28 / debian / patches / bq2415x_charger.patch
1 --- /dev/null
2 +++ kernel-power/drivers/power/bq2415x_charger.c
3 @@ -0,0 +1,1644 @@
4 +/*
5 +    bq2415x_charger.c - bq2415x charger driver
6 +    Copyright (C) 2011-2012  Pali Rohár <pali.rohar@gmail.com>
7 +
8 +    This program is free software; you can redistribute it and/or modify
9 +    it under the terms of the GNU General Public License as published by
10 +    the Free Software Foundation; either version 2 of the License, or
11 +    (at your option) any later version.
12 +
13 +    This program is distributed in the hope that it will be useful,
14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 +    GNU General Public License for more details.
17 +
18 +    You should have received a copy of the GNU General Public License along
19 +    with this program; if not, write to the Free Software Foundation, Inc.,
20 +    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 +*/
22 +
23 +/*
24 +  Datasheets:
25 +  http://www.ti.com/product/bq24150
26 +  http://www.ti.com/product/bq24150a
27 +  http://www.ti.com/product/bq24152
28 +  http://www.ti.com/product/bq24153
29 +  http://www.ti.com/product/bq24153a
30 +  http://www.ti.com/product/bq24155
31 +*/
32 +
33 +#include <linux/version.h>
34 +#include <linux/kernel.h>
35 +#include <linux/module.h>
36 +#include <linux/param.h>
37 +#include <linux/err.h>
38 +#include <linux/workqueue.h>
39 +#include <linux/sysfs.h>
40 +#include <linux/platform_device.h>
41 +#include <linux/power_supply.h>
42 +#include <linux/idr.h>
43 +#include <linux/i2c.h>
44 +#include <linux/slab.h>
45 +
46 +#include <linux/power/bq2415x_charger.h>
47 +
48 +/* timeout for resetting chip timer */
49 +#define BQ2415X_TIMER_TIMEOUT          10
50 +
51 +#define BQ2415X_REG_STATUS             0x00
52 +#define BQ2415X_REG_CONTROL            0x01
53 +#define BQ2415X_REG_VOLTAGE            0x02
54 +#define BQ2415X_REG_VENDER             0x03
55 +#define BQ2415X_REG_CURRENT            0x04
56 +
57 +/* reset state for all registers */
58 +#define BQ2415X_RESET_STATUS           BIT(6)
59 +#define BQ2415X_RESET_CONTROL          (BIT(4)|BIT(5))
60 +#define BQ2415X_RESET_VOLTAGE          (BIT(1)|BIT(3))
61 +#define BQ2415X_RESET_CURRENT          (BIT(0)|BIT(3)|BIT(7))
62 +
63 +/* status register */
64 +#define BQ2415X_BIT_TMR_RST            7
65 +#define BQ2415X_BIT_OTG                        7
66 +#define BQ2415X_BIT_EN_STAT            6
67 +#define BQ2415X_MASK_STAT              (BIT(4)|BIT(5))
68 +#define BQ2415X_SHIFT_STAT             4
69 +#define BQ2415X_BIT_BOOST              3
70 +#define BQ2415X_MASK_FAULT             (BIT(0)|BIT(1)|BIT(2))
71 +#define BQ2415X_SHIFT_FAULT            0
72 +
73 +/* control register */
74 +#define BQ2415X_MASK_LIMIT             (BIT(6)|BIT(7))
75 +#define BQ2415X_SHIFT_LIMIT            6
76 +#define BQ2415X_MASK_VLOWV             (BIT(4)|BIT(5))
77 +#define BQ2415X_SHIFT_VLOWV            4
78 +#define BQ2415X_BIT_TE                 3
79 +#define BQ2415X_BIT_CE                 2
80 +#define BQ2415X_BIT_HZ_MODE            1
81 +#define BQ2415X_BIT_OPA_MODE           0
82 +
83 +/* voltage register */
84 +#define BQ2415X_MASK_VO                (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
85 +#define BQ2415X_SHIFT_VO               2
86 +#define BQ2415X_BIT_OTG_PL             1
87 +#define BQ2415X_BIT_OTG_EN             0
88 +
89 +/* vender register */
90 +#define BQ2415X_MASK_VENDER            (BIT(5)|BIT(6)|BIT(7))
91 +#define BQ2415X_SHIFT_VENDER           5
92 +#define BQ2415X_MASK_PN                        (BIT(3)|BIT(4))
93 +#define BQ2415X_SHIFT_PN               3
94 +#define BQ2415X_MASK_REVISION          (BIT(0)|BIT(1)|BIT(2))
95 +#define BQ2415X_SHIFT_REVISION         0
96 +
97 +/* current register */
98 +#define BQ2415X_MASK_RESET             BIT(7)
99 +#define BQ2415X_MASK_VI_CHRG           (BIT(4)|BIT(5)|BIT(6))
100 +#define BQ2415X_SHIFT_VI_CHRG          4
101 +/* N/A                                 BIT(3) */
102 +#define BQ2415X_MASK_VI_TERM           (BIT(0)|BIT(1)|BIT(2))
103 +#define BQ2415X_SHIFT_VI_TERM          0
104 +
105 +
106 +enum bq2415x_command {
107 +       BQ2415X_TIMER_RESET,
108 +       BQ2415X_OTG_STATUS,
109 +       BQ2415X_STAT_PIN_STATUS,
110 +       BQ2415X_STAT_PIN_ENABLE,
111 +       BQ2415X_STAT_PIN_DISABLE,
112 +       BQ2415X_CHARGE_STATUS,
113 +       BQ2415X_BOOST_STATUS,
114 +       BQ2415X_FAULT_STATUS,
115 +
116 +       BQ2415X_CHARGE_TERMINATION_STATUS,
117 +       BQ2415X_CHARGE_TERMINATION_ENABLE,
118 +       BQ2415X_CHARGE_TERMINATION_DISABLE,
119 +       BQ2415X_CHARGER_STATUS,
120 +       BQ2415X_CHARGER_ENABLE,
121 +       BQ2415X_CHARGER_DISABLE,
122 +       BQ2415X_HIGH_IMPEDANCE_STATUS,
123 +       BQ2415X_HIGH_IMPEDANCE_ENABLE,
124 +       BQ2415X_HIGH_IMPEDANCE_DISABLE,
125 +       BQ2415X_BOOST_MODE_STATUS,
126 +       BQ2415X_BOOST_MODE_ENABLE,
127 +       BQ2415X_BOOST_MODE_DISABLE,
128 +
129 +       BQ2415X_OTG_LEVEL,
130 +       BQ2415X_OTG_ACTIVATE_HIGH,
131 +       BQ2415X_OTG_ACTIVATE_LOW,
132 +       BQ2415X_OTG_PIN_STATUS,
133 +       BQ2415X_OTG_PIN_ENABLE,
134 +       BQ2415X_OTG_PIN_DISABLE,
135 +
136 +       BQ2415X_VENDER_CODE,
137 +       BQ2415X_PART_NUMBER,
138 +       BQ2415X_REVISION,
139 +};
140 +
141 +enum bq2415x_chip {
142 +       BQUNKNOWN,
143 +       BQ24150,
144 +       BQ24150A,
145 +       BQ24151,
146 +       BQ24151A,
147 +       BQ24152,
148 +       BQ24153,
149 +       BQ24153A,
150 +       BQ24155,
151 +       BQ24156,
152 +       BQ24156A,
153 +       BQ24158,
154 +};
155 +
156 +static char *bq2415x_chip_name[] = {
157 +       "unknown",
158 +       "bq24150",
159 +       "bq24150a",
160 +       "bq24151",
161 +       "bq24151a",
162 +       "bq24152",
163 +       "bq24153",
164 +       "bq24153a",
165 +       "bq24155",
166 +       "bq24156",
167 +       "bq24156a",
168 +       "bq24158",
169 +};
170 +
171 +struct bq2415x_device {
172 +       struct device *dev;
173 +       struct bq2415x_platform_data init_data;
174 +       struct power_supply charger;
175 +       struct delayed_work work;
176 +       enum bq2415x_mode reported_mode;/* mode reported by hook function */
177 +       enum bq2415x_mode mode;         /* current configured mode */
178 +       enum bq2415x_chip chip;
179 +       const char *timer_error;
180 +       char *model;
181 +       char *name;
182 +       int autotimer;  /* 1 - if driver automatically reset timer, 0 - not */
183 +       int automode;   /* 1 - enabled, 0 - disabled; -1 - not supported */
184 +       int id;
185 +};
186 +
187 +/* each registered chip must have unique id */
188 +static DEFINE_IDR(bq2415x_id);
189 +
190 +static DEFINE_MUTEX(bq2415x_id_mutex);
191 +static DEFINE_MUTEX(bq2415x_timer_mutex);
192 +static DEFINE_MUTEX(bq2415x_i2c_mutex);
193 +
194 +/**** i2c read functions ****/
195 +
196 +/* read value from register */
197 +static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg)
198 +{
199 +       struct i2c_client *client = to_i2c_client(bq->dev);
200 +       struct i2c_msg msg[2];
201 +       u8 val;
202 +       int ret;
203 +
204 +       if (!client->adapter)
205 +               return -ENODEV;
206 +
207 +       msg[0].addr = client->addr;
208 +       msg[0].flags = 0;
209 +       msg[0].buf = &reg;
210 +       msg[0].len = sizeof(reg);
211 +       msg[1].addr = client->addr;
212 +       msg[1].flags = I2C_M_RD;
213 +       msg[1].buf = &val;
214 +       msg[1].len = sizeof(val);
215 +
216 +       mutex_lock(&bq2415x_i2c_mutex);
217 +       ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
218 +       mutex_unlock(&bq2415x_i2c_mutex);
219 +
220 +       if (ret < 0)
221 +               return ret;
222 +
223 +       return val;
224 +}
225 +
226 +/* read value from register, apply mask and right shift it */
227 +static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
228 +                               u8 mask, u8 shift)
229 +{
230 +       int ret;
231 +
232 +       if (shift > 8)
233 +               return -EINVAL;
234 +
235 +       ret = bq2415x_i2c_read(bq, reg);
236 +       if (ret < 0)
237 +               return ret;
238 +       else
239 +               return (ret & mask) >> shift;
240 +}
241 +
242 +/* read value from register and return one specified bit */
243 +static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit)
244 +{
245 +       if (bit > 8)
246 +               return -EINVAL;
247 +       else
248 +               return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit);
249 +}
250 +
251 +/**** i2c write functions ****/
252 +
253 +/* write value to register */
254 +static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val)
255 +{
256 +       struct i2c_client *client = to_i2c_client(bq->dev);
257 +       struct i2c_msg msg[1];
258 +       u8 data[2];
259 +       int ret;
260 +
261 +       data[0] = reg;
262 +       data[1] = val;
263 +
264 +       msg[0].addr = client->addr;
265 +       msg[0].flags = 0;
266 +       msg[0].buf = data;
267 +       msg[0].len = ARRAY_SIZE(data);
268 +
269 +       mutex_lock(&bq2415x_i2c_mutex);
270 +       ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
271 +       mutex_unlock(&bq2415x_i2c_mutex);
272 +
273 +       /* i2c_transfer returns number of messages transferred */
274 +       if (ret < 0)
275 +               return ret;
276 +       else if (ret != 1)
277 +               return -EIO;
278 +
279 +       return 0;
280 +}
281 +
282 +/* read value from register, change it with mask left shifted and write back */
283 +static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val,
284 +                               u8 mask, u8 shift)
285 +{
286 +       int ret;
287 +
288 +       if (shift > 8)
289 +               return -EINVAL;
290 +
291 +       ret = bq2415x_i2c_read(bq, reg);
292 +       if (ret < 0)
293 +               return ret;
294 +
295 +       ret &= ~mask;
296 +       ret |= val << shift;
297 +
298 +       return bq2415x_i2c_write(bq, reg, ret);
299 +}
300 +
301 +/* change only one bit in register */
302 +static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg,
303 +                               bool val, u8 bit)
304 +{
305 +       if (bit > 8)
306 +               return -EINVAL;
307 +       else
308 +               return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit);
309 +}
310 +
311 +/**** global functions ****/
312 +
313 +/* exec command function */
314 +static int bq2415x_exec_command(struct bq2415x_device *bq,
315 +                               enum bq2415x_command command)
316 +{
317 +       int ret;
318 +       switch (command) {
319 +       case BQ2415X_TIMER_RESET:
320 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS,
321 +                               1, BQ2415X_BIT_TMR_RST);
322 +       case BQ2415X_OTG_STATUS:
323 +               return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
324 +                               BQ2415X_BIT_OTG);
325 +       case BQ2415X_STAT_PIN_STATUS:
326 +               return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
327 +                               BQ2415X_BIT_EN_STAT);
328 +       case BQ2415X_STAT_PIN_ENABLE:
329 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1,
330 +                               BQ2415X_BIT_EN_STAT);
331 +       case BQ2415X_STAT_PIN_DISABLE:
332 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0,
333 +                               BQ2415X_BIT_EN_STAT);
334 +       case BQ2415X_CHARGE_STATUS:
335 +               return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
336 +                               BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT);
337 +       case BQ2415X_BOOST_STATUS:
338 +               return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
339 +                               BQ2415X_BIT_BOOST);
340 +       case BQ2415X_FAULT_STATUS:
341 +               return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
342 +                       BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT);
343 +
344 +       case BQ2415X_CHARGE_TERMINATION_STATUS:
345 +               return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
346 +                               BQ2415X_BIT_TE);
347 +       case BQ2415X_CHARGE_TERMINATION_ENABLE:
348 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
349 +                               1, BQ2415X_BIT_TE);
350 +       case BQ2415X_CHARGE_TERMINATION_DISABLE:
351 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
352 +                               0, BQ2415X_BIT_TE);
353 +       case BQ2415X_CHARGER_STATUS:
354 +               ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
355 +                       BQ2415X_BIT_CE);
356 +               if (ret < 0)
357 +                       return ret;
358 +               else
359 +                       return ret > 0 ? 0 : 1;
360 +       case BQ2415X_CHARGER_ENABLE:
361 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
362 +                               0, BQ2415X_BIT_CE);
363 +       case BQ2415X_CHARGER_DISABLE:
364 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
365 +                               1, BQ2415X_BIT_CE);
366 +       case BQ2415X_HIGH_IMPEDANCE_STATUS:
367 +               return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
368 +                               BQ2415X_BIT_HZ_MODE);
369 +       case BQ2415X_HIGH_IMPEDANCE_ENABLE:
370 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
371 +                               1, BQ2415X_BIT_HZ_MODE);
372 +       case BQ2415X_HIGH_IMPEDANCE_DISABLE:
373 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
374 +                               0, BQ2415X_BIT_HZ_MODE);
375 +       case BQ2415X_BOOST_MODE_STATUS:
376 +               return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
377 +                               BQ2415X_BIT_OPA_MODE);
378 +       case BQ2415X_BOOST_MODE_ENABLE:
379 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
380 +                               1, BQ2415X_BIT_OPA_MODE);
381 +       case BQ2415X_BOOST_MODE_DISABLE:
382 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
383 +                               0, BQ2415X_BIT_OPA_MODE);
384 +
385 +       case BQ2415X_OTG_LEVEL:
386 +               return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
387 +                               BQ2415X_BIT_OTG_PL);
388 +       case BQ2415X_OTG_ACTIVATE_HIGH:
389 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
390 +                               1, BQ2415X_BIT_OTG_PL);
391 +       case BQ2415X_OTG_ACTIVATE_LOW:
392 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
393 +                               0, BQ2415X_BIT_OTG_PL);
394 +       case BQ2415X_OTG_PIN_STATUS:
395 +               return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
396 +                               BQ2415X_BIT_OTG_EN);
397 +       case BQ2415X_OTG_PIN_ENABLE:
398 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
399 +                               1, BQ2415X_BIT_OTG_EN);
400 +       case BQ2415X_OTG_PIN_DISABLE:
401 +               return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
402 +                               0, BQ2415X_BIT_OTG_EN);
403 +
404 +       case BQ2415X_VENDER_CODE:
405 +               return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
406 +                       BQ2415X_MASK_VENDER, BQ2415X_SHIFT_VENDER);
407 +       case BQ2415X_PART_NUMBER:
408 +               return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
409 +                               BQ2415X_MASK_PN, BQ2415X_SHIFT_PN);
410 +       case BQ2415X_REVISION:
411 +               return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
412 +                       BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION);
413 +
414 +       default:
415 +               return -EINVAL;
416 +       }
417 +}
418 +
419 +/* detect chip type */
420 +static enum bq2415x_chip bq2415x_detect_chip(struct bq2415x_device *bq)
421 +{
422 +       struct i2c_client *client = to_i2c_client(bq->dev);
423 +       int ret = bq2415x_exec_command(bq, BQ2415X_PART_NUMBER);
424 +
425 +       if (ret < 0)
426 +               return ret;
427 +
428 +       switch (client->addr) {
429 +       case 0x6b:
430 +               switch (ret) {
431 +               case 0:
432 +                       if (bq->chip == BQ24151A)
433 +                               return bq->chip;
434 +                       else
435 +                               return BQ24151;
436 +               case 1:
437 +                       if (bq->chip == BQ24150A ||
438 +                               bq->chip == BQ24152 ||
439 +                               bq->chip == BQ24155)
440 +                               return bq->chip;
441 +                       else
442 +                               return BQ24150;
443 +               case 2:
444 +                       if (bq->chip == BQ24153A)
445 +                               return bq->chip;
446 +                       else
447 +                               return BQ24153;
448 +               default:
449 +                       return BQUNKNOWN;
450 +               }
451 +               break;
452 +
453 +       case 0x6a:
454 +               switch (ret) {
455 +               case 0:
456 +                       if (bq->chip == BQ24156A)
457 +                               return bq->chip;
458 +                       else
459 +                               return BQ24156;
460 +               case 2:
461 +                       return BQ24158;
462 +               default:
463 +                       return BQUNKNOWN;
464 +               }
465 +               break;
466 +       }
467 +
468 +       return BQUNKNOWN;
469 +}
470 +
471 +/* detect chip revision */
472 +static int bq2415x_detect_revision(struct bq2415x_device *bq)
473 +{
474 +       int ret = bq2415x_exec_command(bq, BQ2415X_REVISION);
475 +       int chip = bq2415x_detect_chip(bq);
476 +       if (ret < 0 || chip < 0)
477 +               return -1;
478 +
479 +       switch (chip) {
480 +       case BQ24150:
481 +       case BQ24150A:
482 +       case BQ24151:
483 +       case BQ24151A:
484 +       case BQ24152:
485 +               if (ret >= 0 && ret <= 3)
486 +                       return ret;
487 +               else
488 +                       return -1;
489 +
490 +       case BQ24153:
491 +       case BQ24153A:
492 +       case BQ24156:
493 +       case BQ24156A:
494 +       case BQ24158:
495 +               if (ret == 3)
496 +                       return 0;
497 +               else if (ret == 1)
498 +                       return 1;
499 +               else
500 +                       return -1;
501 +
502 +       case BQ24155:
503 +               if (ret == 3)
504 +                       return 3;
505 +               else
506 +                       return -1;
507 +
508 +       case BQUNKNOWN:
509 +               return -1;
510 +       }
511 +
512 +       return -1;
513 +}
514 +
515 +/* return chip vender code */
516 +static int bq2415x_get_vender_code(struct bq2415x_device *bq)
517 +{
518 +       int ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
519 +       if (ret < 0)
520 +               return 0;
521 +       else /* convert to binary */
522 +               return (ret & 0x1) +
523 +                       ((ret >> 1) & 0x1) * 10 +
524 +                       ((ret >> 2) & 0x1) * 100;
525 +}
526 +
527 +/* reset all chip registers to default state */
528 +static void bq2415x_reset_chip(struct bq2415x_device *bq)
529 +{
530 +       bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT);
531 +       bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE);
532 +       bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL);
533 +       bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS);
534 +       bq->timer_error = NULL;
535 +}
536 +
537 +/**** properties functions ****/
538 +
539 +/* set current limit in mA */
540 +static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA)
541 +{
542 +       int val;
543 +       if (mA <= 100)
544 +               val = 0;
545 +       else if (mA <= 500)
546 +               val = 1;
547 +       else if (mA <= 800)
548 +               val = 2;
549 +       else
550 +               val = 3;
551 +       return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
552 +                       BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
553 +}
554 +
555 +/* get current limit in mA */
556 +static int bq2415x_get_current_limit(struct bq2415x_device *bq)
557 +{
558 +       int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
559 +                       BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
560 +       if (ret < 0)
561 +               return ret;
562 +       else if (ret == 0)
563 +               return 100;
564 +       else if (ret == 1)
565 +               return 500;
566 +       else if (ret == 2)
567 +               return 800;
568 +       else if (ret == 3)
569 +               return 1800;
570 +       else
571 +               return -EINVAL;
572 +}
573 +
574 +/* set weak battery voltage in mV */
575 +static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
576 +{
577 +       /* round to 100mV */
578 +       int val;
579 +       if (mV <= 3400 + 50)
580 +               val = 0;
581 +       else if (mV <= 3500 + 50)
582 +               val = 1;
583 +       else if (mV <= 3600 + 50)
584 +               val = 2;
585 +       else
586 +               val = 3;
587 +       return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
588 +                       BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
589 +}
590 +
591 +/* get weak battery voltage in mV */
592 +static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq)
593 +{
594 +       int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
595 +                       BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
596 +       if (ret < 0)
597 +               return ret;
598 +       else
599 +               return 100 * (34 + ret);
600 +}
601 +
602 +/* set battery regulation voltage in mV */
603 +static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
604 +                                               int mV)
605 +{
606 +       int val = (mV/10 - 350) / 2;
607 +
608 +       if (val < 0)
609 +               val = 0;
610 +       else if (val > 94) /* FIXME: Max is 94 or 122 ? Set max value ? */
611 +               return -EINVAL;
612 +
613 +       return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val,
614 +                       BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
615 +}
616 +
617 +/* get battery regulation voltage in mV */
618 +static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq)
619 +{
620 +       int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE,
621 +                       BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
622 +       if (ret < 0)
623 +               return ret;
624 +       else
625 +               return 10 * (350 + 2*ret);
626 +}
627 +
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)
630 +{
631 +       int val;
632 +       if (bq->init_data.resistor_sense <= 0)
633 +               return -ENOSYS;
634 +
635 +       val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
636 +
637 +       if (val < 0)
638 +               val = 0;
639 +       else if (val > 7)
640 +               val = 7;
641 +
642 +       return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
643 +                       BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET,
644 +                       BQ2415X_SHIFT_VI_CHRG);
645 +}
646 +
647 +/* get charge current in mA (platform data must provide resistor sense) */
648 +static int bq2415x_get_charge_current(struct bq2415x_device *bq)
649 +{
650 +       int ret;
651 +       if (bq->init_data.resistor_sense <= 0)
652 +               return -ENOSYS;
653 +
654 +       ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
655 +                       BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG);
656 +       if (ret < 0)
657 +               return ret;
658 +       else
659 +               return (37400 + 6800*ret) / bq->init_data.resistor_sense;
660 +}
661 +
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)
664 +{
665 +       int val;
666 +       if (bq->init_data.resistor_sense <= 0)
667 +               return -ENOSYS;
668 +
669 +       val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
670 +
671 +       if (val < 0)
672 +               val = 0;
673 +       else if (val > 7)
674 +               val = 7;
675 +
676 +       return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
677 +                       BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET,
678 +                       BQ2415X_SHIFT_VI_TERM);
679 +}
680 +
681 +/* get termination current in mA (platform data must provide resistor sense) */
682 +static int bq2415x_get_termination_current(struct bq2415x_device *bq)
683 +{
684 +       int ret;
685 +       if (bq->init_data.resistor_sense <= 0)
686 +               return -ENOSYS;
687 +
688 +       ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
689 +                       BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM);
690 +       if (ret < 0)
691 +               return ret;
692 +       else
693 +               return (3400 + 3400*ret) / bq->init_data.resistor_sense;
694 +}
695 +
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)
705 +
706 +/* set default values of all properties */
707 +static int bq2415x_set_defaults(struct bq2415x_device *bq)
708 +{
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);
712 +       bq2415x_set_default_value(bq, current_limit);
713 +       bq2415x_set_default_value(bq, weak_battery_voltage);
714 +       bq2415x_set_default_value(bq, battery_regulation_voltage);
715 +       if (bq->init_data.resistor_sense > 0) {
716 +               bq2415x_set_default_value(bq, charge_current);
717 +               bq2415x_set_default_value(bq, termination_current);
718 +               bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE);
719 +       }
720 +       bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
721 +       return 0;
722 +}
723 +
724 +/**** charger mode functions ****/
725 +
726 +/* set charger mode */
727 +static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode)
728 +{
729 +       int ret = 0;
730 +       int charger = 0;
731 +       int boost = 0;
732 +
733 +       if (mode == BQ2415X_MODE_HOST_CHARGER ||
734 +               mode == BQ2415X_MODE_DEDICATED_CHARGER)
735 +                       charger = 1;
736 +
737 +       if (mode == BQ2415X_MODE_BOOST)
738 +               boost = 1;
739 +
740 +       if (!charger)
741 +               ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
742 +
743 +       if (!boost)
744 +               ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
745 +
746 +       if (ret < 0)
747 +               return ret;
748 +
749 +       switch (mode) {
750 +       case BQ2415X_MODE_NONE:
751 +               dev_dbg(bq->dev, "changing mode to: N/A\n");
752 +               ret = bq2415x_set_current_limit(bq, 100);
753 +               break;
754 +       case BQ2415X_MODE_HOST_CHARGER:
755 +               dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n");
756 +               ret = bq2415x_set_current_limit(bq, 500);
757 +               break;
758 +       case BQ2415X_MODE_DEDICATED_CHARGER:
759 +               dev_dbg(bq->dev, "changing mode to: Dedicated charger\n");
760 +               ret = bq2415x_set_current_limit(bq, 1800);
761 +               break;
762 +       case BQ2415X_MODE_BOOST: /* Boost mode */
763 +               dev_dbg(bq->dev, "changing mode to: Boost\n");
764 +               ret = bq2415x_set_current_limit(bq, 100);
765 +               break;
766 +       }
767 +
768 +       if (ret < 0)
769 +               return ret;
770 +
771 +       if (charger)
772 +               ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
773 +       else if (boost)
774 +               ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE);
775 +
776 +       if (ret < 0)
777 +               return ret;
778 +
779 +       bq2415x_set_default_value(bq, weak_battery_voltage);
780 +       bq2415x_set_default_value(bq, battery_regulation_voltage);
781 +
782 +       bq->mode = mode;
783 +       sysfs_notify(&bq->charger.dev->kobj, NULL, "mode");
784 +
785 +       return 0;
786 +
787 +}
788 +
789 +/* hook function called by other driver which set reported mode */
790 +static void bq2415x_hook_function(enum bq2415x_mode mode, void *data)
791 +{
792 +       struct bq2415x_device *bq = data;
793 +
794 +       if (!bq)
795 +               return;
796 +
797 +       dev_dbg(bq->dev, "hook function was called\n");
798 +       bq->reported_mode = mode;
799 +
800 +       /* if automode is not enabled do not tell about reported_mode */
801 +       if (bq->automode < 1)
802 +               return;
803 +
804 +       sysfs_notify(&bq->charger.dev->kobj, NULL, "reported_mode");
805 +       bq2415x_set_mode(bq, bq->reported_mode);
806 +
807 +}
808 +
809 +/**** timer functions ****/
810 +
811 +/* enable/disable auto resetting chip timer */
812 +static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
813 +{
814 +       mutex_lock(&bq2415x_timer_mutex);
815 +
816 +       if (bq->autotimer == state) {
817 +               mutex_unlock(&bq2415x_timer_mutex);
818 +               return;
819 +       }
820 +
821 +       bq->autotimer = state;
822 +
823 +       if (state) {
824 +               schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
825 +               bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
826 +               bq->timer_error = NULL;
827 +       } else {
828 +               cancel_delayed_work_sync(&bq->work);
829 +       }
830 +
831 +       mutex_unlock(&bq2415x_timer_mutex);
832 +}
833 +
834 +/* called by bq2415x_timer_work on timer error */
835 +static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
836 +{
837 +       bq->timer_error = msg;
838 +       sysfs_notify(&bq->charger.dev->kobj, NULL, "timer");
839 +       dev_err(bq->dev, "%s\n", msg);
840 +       if (bq->automode > 0)
841 +               bq->automode = 0;
842 +       bq2415x_set_mode(bq, BQ2415X_MODE_NONE);
843 +       bq2415x_set_autotimer(bq, 0);
844 +}
845 +
846 +/* delayed work function for auto resetting chip timer */
847 +static void bq2415x_timer_work(struct work_struct *work)
848 +{
849 +       struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
850 +                                               work.work);
851 +       int ret, error, boost;
852 +
853 +       if (!bq->autotimer)
854 +               return;
855 +
856 +       ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
857 +       if (ret < 0) {
858 +               bq2415x_timer_error(bq, "Resetting timer failed");
859 +               return;
860 +       }
861 +
862 +       boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
863 +       if (boost < 0) {
864 +               bq2415x_timer_error(bq, "Unknown error");
865 +               return;
866 +       }
867 +
868 +       error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
869 +       if (error < 0) {
870 +               bq2415x_timer_error(bq, "Unknown error");
871 +               return;
872 +       }
873 +
874 +       if (boost) {
875 +               switch (error) {
876 +               /* Non fatal errors, chip is OK */
877 +               case 0: /* No error */
878 +                       break;
879 +               case 6: /* Timer expired */
880 +                       dev_err(bq->dev, "Timer expired\n");
881 +                       break;
882 +               case 3: /* Battery voltage too low */
883 +                       dev_err(bq->dev, "Battery voltage to low\n");
884 +                       break;
885 +
886 +               /* Fatal errors, disable and reset chip */
887 +               case 1: /* Overvoltage protection (chip fried) */
888 +                       bq2415x_timer_error(bq,
889 +                               "Overvoltage protection (chip fried)");
890 +                       return;
891 +               case 2: /* Overload */
892 +                       bq2415x_timer_error(bq, "Overload");
893 +                       return;
894 +               case 4: /* Battery overvoltage protection */
895 +                       bq2415x_timer_error(bq,
896 +                               "Battery overvoltage protection");
897 +                       return;
898 +               case 5: /* Thermal shutdown (too hot) */
899 +                       bq2415x_timer_error(bq,
900 +                                       "Thermal shutdown (too hot)");
901 +                       return;
902 +               case 7: /* N/A */
903 +                       bq2415x_timer_error(bq, "Unknown error");
904 +                       return;
905 +               }
906 +       } else {
907 +               switch (error) {
908 +               /* Non fatal errors, chip is OK */
909 +               case 0: /* No error */
910 +                       break;
911 +               case 2: /* Sleep mode */
912 +                       dev_err(bq->dev, "Sleep mode\n");
913 +                       break;
914 +               case 3: /* Poor input source */
915 +                       dev_err(bq->dev, "Poor input source\n");
916 +                       break;
917 +               case 6: /* Timer expired */
918 +                       dev_err(bq->dev, "Timer expired\n");
919 +                       break;
920 +               case 7: /* No battery */
921 +                       dev_err(bq->dev, "No battery\n");
922 +                       break;
923 +
924 +               /* Fatal errors, disable and reset chip */
925 +               case 1: /* Overvoltage protection (chip fried) */
926 +                       bq2415x_timer_error(bq,
927 +                               "Overvoltage protection (chip fried)");
928 +                       return;
929 +               case 4: /* Battery overvoltage protection */
930 +                       bq2415x_timer_error(bq,
931 +                               "Battery overvoltage protection");
932 +                       return;
933 +               case 5: /* Thermal shutdown (too hot) */
934 +                       bq2415x_timer_error(bq,
935 +                               "Thermal shutdown (too hot)");
936 +                       return;
937 +               }
938 +       }
939 +
940 +       schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
941 +}
942 +
943 +/**** power supply interface code ****/
944 +
945 +static enum power_supply_property bq2415x_power_supply_props[] = {
946 +       /* TODO: maybe add more power supply properties */
947 +       POWER_SUPPLY_PROP_STATUS,
948 +       POWER_SUPPLY_PROP_MODEL_NAME,
949 +};
950 +
951 +static int bq2415x_power_supply_get_property(struct power_supply *psy,
952 +       enum power_supply_property psp, union power_supply_propval *val)
953 +{
954 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
955 +                                               charger);
956 +       int ret;
957 +
958 +       switch (psp) {
959 +       case POWER_SUPPLY_PROP_STATUS:
960 +               ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
961 +               if (ret < 0)
962 +                       return ret;
963 +               else if (ret == 0) /* Ready */
964 +                       val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
965 +               else if (ret == 1) /* Charge in progress */
966 +                       val->intval = POWER_SUPPLY_STATUS_CHARGING;
967 +               else if (ret == 2) /* Charge done */
968 +                       val->intval = POWER_SUPPLY_STATUS_FULL;
969 +               else
970 +                       val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
971 +               break;
972 +       case POWER_SUPPLY_PROP_MODEL_NAME:
973 +               val->strval = bq->model;
974 +               break;
975 +       default:
976 +               return -EINVAL;
977 +       }
978 +       return 0;
979 +}
980 +
981 +static int bq2415x_power_supply_init(struct bq2415x_device *bq)
982 +{
983 +       int ret;
984 +       int chip;
985 +       char revstr[8];
986 +
987 +       bq->charger.name = bq->name;
988 +       bq->charger.type = POWER_SUPPLY_TYPE_USB;
989 +       bq->charger.properties = bq2415x_power_supply_props;
990 +       bq->charger.num_properties = ARRAY_SIZE(bq2415x_power_supply_props);
991 +       bq->charger.get_property = bq2415x_power_supply_get_property;
992 +
993 +       ret = bq2415x_detect_chip(bq);
994 +       if (ret < 0)
995 +               chip = BQUNKNOWN;
996 +       else
997 +               chip = ret;
998 +
999 +       ret = bq2415x_detect_revision(bq);
1000 +       if (ret < 0)
1001 +               strcpy(revstr, "unknown");
1002 +       else
1003 +               sprintf(revstr, "1.%d", ret);
1004 +
1005 +       bq->model = kasprintf(GFP_KERNEL,
1006 +                               "chip %s, revision %s, vender code %.3d",
1007 +                               bq2415x_chip_name[chip], revstr,
1008 +                               bq2415x_get_vender_code(bq));
1009 +       if (!bq->model) {
1010 +               dev_err(bq->dev, "failed to allocate model name\n");
1011 +               return -ENOMEM;
1012 +       }
1013 +
1014 +       ret = power_supply_register(bq->dev, &bq->charger);
1015 +       if (ret) {
1016 +               kfree(bq->model);
1017 +               return ret;
1018 +       }
1019 +
1020 +       return 0;
1021 +}
1022 +
1023 +static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
1024 +{
1025 +       bq->autotimer = 0;
1026 +       if (bq->automode > 0)
1027 +               bq->automode = 0;
1028 +       cancel_delayed_work_sync(&bq->work);
1029 +       power_supply_unregister(&bq->charger);
1030 +       kfree(bq->model);
1031 +}
1032 +
1033 +/**** additional sysfs entries for power supply interface ****/
1034 +
1035 +/* show *_status entries */
1036 +static ssize_t bq2415x_sysfs_show_status(struct device *dev,
1037 +               struct device_attribute *attr, char *buf)
1038 +{
1039 +       struct power_supply *psy = dev_get_drvdata(dev);
1040 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1041 +                                               charger);
1042 +       enum bq2415x_command command;
1043 +       int ret;
1044 +
1045 +       if (strcmp(attr->attr.name, "otg_status") == 0)
1046 +               command = BQ2415X_OTG_STATUS;
1047 +       else if (strcmp(attr->attr.name, "charge_status") == 0)
1048 +               command = BQ2415X_CHARGE_STATUS;
1049 +       else if (strcmp(attr->attr.name, "boost_status") == 0)
1050 +               command = BQ2415X_BOOST_STATUS;
1051 +       else if (strcmp(attr->attr.name, "fault_status") == 0)
1052 +               command = BQ2415X_FAULT_STATUS;
1053 +       else
1054 +               return -EINVAL;
1055 +
1056 +       ret = bq2415x_exec_command(bq, command);
1057 +       if (ret < 0)
1058 +               return ret;
1059 +       else
1060 +               return sprintf(buf, "%d\n", ret);
1061 +}
1062 +
1063 +/* set timer entry:
1064 +     auto - enable auto mode
1065 +     off - disable auto mode
1066 +     (other values) - reset chip timer
1067 +*/
1068 +static ssize_t bq2415x_sysfs_set_timer(struct device *dev,
1069 +               struct device_attribute *attr, const char *buf, size_t count)
1070 +{
1071 +       struct power_supply *psy = dev_get_drvdata(dev);
1072 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1073 +                                               charger);
1074 +       int ret = 0;
1075 +
1076 +       if (strncmp(buf, "auto", 4) == 0)
1077 +               bq2415x_set_autotimer(bq, 1);
1078 +       else if (strncmp(buf, "off", 3) == 0)
1079 +               bq2415x_set_autotimer(bq, 0);
1080 +       else
1081 +               ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
1082 +
1083 +       if (ret < 0)
1084 +               return ret;
1085 +       else
1086 +               return count;
1087 +}
1088 +
1089 +/* show timer entry (auto or off) */
1090 +static ssize_t bq2415x_sysfs_show_timer(struct device *dev,
1091 +               struct device_attribute *attr, char *buf)
1092 +{
1093 +       struct power_supply *psy = dev_get_drvdata(dev);
1094 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1095 +                                               charger);
1096 +
1097 +       if (bq->timer_error)
1098 +               return sprintf(buf, "%s\n", bq->timer_error);
1099 +
1100 +       if (bq->autotimer)
1101 +               return sprintf(buf, "auto\n");
1102 +       else
1103 +               return sprintf(buf, "off\n");
1104 +}
1105 +
1106 +/* set mode entry:
1107 +     auto - if automode is supported, enable it and set mode to reported
1108 +     none - disable charger and boost mode
1109 +     host - charging mode for host/hub chargers (current limit 500mA)
1110 +     dedicated - charging mode for dedicated chargers (unlimited current limit)
1111 +     boost - disable charger and enable boost mode
1112 +*/
1113 +static ssize_t bq2415x_sysfs_set_mode(struct device *dev,
1114 +               struct device_attribute *attr, const char *buf, size_t count)
1115 +{
1116 +       struct power_supply *psy = dev_get_drvdata(dev);
1117 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1118 +                                               charger);
1119 +       enum bq2415x_mode mode;
1120 +       int ret = 0;
1121 +
1122 +       if (strncmp(buf, "auto", 4) == 0) {
1123 +               if (bq->automode < 0)
1124 +                       return -ENOSYS;
1125 +               bq->automode = 1;
1126 +               mode = bq->reported_mode;
1127 +       } else if (strncmp(buf, "none", 4) == 0) {
1128 +               if (bq->automode > 0)
1129 +                       bq->automode = 0;
1130 +               mode = BQ2415X_MODE_NONE;
1131 +       } else if (strncmp(buf, "host", 4) == 0) {
1132 +               if (bq->automode > 0)
1133 +                       bq->automode = 0;
1134 +               mode = BQ2415X_MODE_HOST_CHARGER;
1135 +       } else if (strncmp(buf, "dedicated", 9) == 0) {
1136 +               if (bq->automode > 0)
1137 +                       bq->automode = 0;
1138 +               mode = BQ2415X_MODE_DEDICATED_CHARGER;
1139 +       } else if (strncmp(buf, "boost", 5) == 0) {
1140 +               if (bq->automode > 0)
1141 +                       bq->automode = 0;
1142 +               mode = BQ2415X_MODE_BOOST;
1143 +       } else if (strncmp(buf, "reset", 5) == 0) {
1144 +               bq2415x_reset_chip(bq);
1145 +               bq2415x_set_defaults(bq);
1146 +               if (bq->automode <= 0)
1147 +                       return count;
1148 +               bq->automode = 1;
1149 +               mode = bq->reported_mode;
1150 +       } else
1151 +               return -EINVAL;
1152 +
1153 +       ret = bq2415x_set_mode(bq, mode);
1154 +       if (ret < 0)
1155 +               return ret;
1156 +       else
1157 +               return count;
1158 +}
1159 +
1160 +/* show mode entry (auto, none, host, dedicated or boost) */
1161 +static ssize_t bq2415x_sysfs_show_mode(struct device *dev,
1162 +               struct device_attribute *attr, char *buf)
1163 +{
1164 +       struct power_supply *psy = dev_get_drvdata(dev);
1165 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1166 +                                               charger);
1167 +       ssize_t ret = 0;
1168 +
1169 +       if (bq->automode > 0)
1170 +               ret += sprintf(buf+ret, "auto (");
1171 +
1172 +       switch (bq->mode) {
1173 +       case BQ2415X_MODE_NONE:
1174 +               ret += sprintf(buf+ret, "none");
1175 +               break;
1176 +       case BQ2415X_MODE_HOST_CHARGER:
1177 +               ret += sprintf(buf+ret, "host");
1178 +               break;
1179 +       case BQ2415X_MODE_DEDICATED_CHARGER:
1180 +               ret += sprintf(buf+ret, "dedicated");
1181 +               break;
1182 +       case BQ2415X_MODE_BOOST:
1183 +               ret += sprintf(buf+ret, "boost");
1184 +               break;
1185 +       }
1186 +
1187 +       if (bq->automode > 0)
1188 +               ret += sprintf(buf+ret, ")");
1189 +
1190 +       ret += sprintf(buf+ret, "\n");
1191 +       return ret;
1192 +}
1193 +
1194 +/* show reported_mode entry (none, host, dedicated or boost) */
1195 +static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
1196 +               struct device_attribute *attr, char *buf)
1197 +{
1198 +       struct power_supply *psy = dev_get_drvdata(dev);
1199 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1200 +                                               charger);
1201 +
1202 +       if (bq->automode < 0)
1203 +               return -EINVAL;
1204 +
1205 +       switch (bq->reported_mode) {
1206 +       case BQ2415X_MODE_NONE:
1207 +               return sprintf(buf, "none\n");
1208 +       case BQ2415X_MODE_HOST_CHARGER:
1209 +               return sprintf(buf, "host\n");
1210 +       case BQ2415X_MODE_DEDICATED_CHARGER:
1211 +               return sprintf(buf, "dedicated\n");
1212 +       case BQ2415X_MODE_BOOST:
1213 +               return sprintf(buf, "boost\n");
1214 +       }
1215 +
1216 +       return -EINVAL;
1217 +}
1218 +
1219 +/* directly set raw value to chip register, format: 'register value' */
1220 +static ssize_t bq2415x_sysfs_set_registers(struct device *dev,
1221 +               struct device_attribute *attr, const char *buf, size_t count)
1222 +{
1223 +       struct power_supply *psy = dev_get_drvdata(dev);
1224 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1225 +                                               charger);
1226 +       ssize_t ret = 0;
1227 +       unsigned int reg;
1228 +       unsigned int val;
1229 +
1230 +       if (sscanf(buf, "%x %x", &reg, &val) != 2)
1231 +               return -EINVAL;
1232 +
1233 +       if (reg > 4 || val > 255)
1234 +               return -EINVAL;
1235 +
1236 +       ret = bq2415x_i2c_write(bq, reg, val);
1237 +       if (ret < 0)
1238 +               return ret;
1239 +       else
1240 +               return count;
1241 +}
1242 +
1243 +/* print value of chip register, format: 'register=value' */
1244 +static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
1245 +                                       u8 reg, char *buf)
1246 +{
1247 +       int ret = bq2415x_i2c_read(bq, reg);
1248 +       if (ret < 0)
1249 +               return sprintf(buf, "%#.2x=error %d\n", reg, ret);
1250 +       else
1251 +               return sprintf(buf, "%#.2x=%#.2x\n", reg, ret);
1252 +}
1253 +
1254 +/* show all raw values of chip register, format per line: 'register=value' */
1255 +static ssize_t bq2415x_sysfs_show_registers(struct device *dev,
1256 +               struct device_attribute *attr, char *buf)
1257 +{
1258 +       struct power_supply *psy = dev_get_drvdata(dev);
1259 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1260 +                                               charger);
1261 +       ssize_t ret = 0;
1262 +
1263 +       ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret);
1264 +       ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret);
1265 +       ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret);
1266 +       ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret);
1267 +       ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret);
1268 +       return ret;
1269 +}
1270 +
1271 +/* set current and voltage limit entries (in mA or mV) */
1272 +static ssize_t bq2415x_sysfs_set_limit(struct device *dev,
1273 +               struct device_attribute *attr, const char *buf, size_t count)
1274 +{
1275 +       struct power_supply *psy = dev_get_drvdata(dev);
1276 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1277 +                                               charger);
1278 +       long val;
1279 +       int ret;
1280 +
1281 +       if (kstrtol(buf, 10, &val) < 0)
1282 +               return -EINVAL;
1283 +
1284 +       if (strcmp(attr->attr.name, "current_limit") == 0)
1285 +               ret = bq2415x_set_current_limit(bq, val);
1286 +       else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1287 +               ret = bq2415x_set_weak_battery_voltage(bq, val);
1288 +       else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1289 +               ret = bq2415x_set_battery_regulation_voltage(bq, val);
1290 +       else if (strcmp(attr->attr.name, "charge_current") == 0)
1291 +               ret = bq2415x_set_charge_current(bq, val);
1292 +       else if (strcmp(attr->attr.name, "termination_current") == 0)
1293 +               ret = bq2415x_set_termination_current(bq, val);
1294 +       else
1295 +               return -EINVAL;
1296 +
1297 +       if (ret < 0)
1298 +               return ret;
1299 +       else
1300 +               return count;
1301 +}
1302 +
1303 +/* show current and voltage limit entries (in mA or mV) */
1304 +static ssize_t bq2415x_sysfs_show_limit(struct device *dev,
1305 +               struct device_attribute *attr, char *buf)
1306 +{
1307 +       struct power_supply *psy = dev_get_drvdata(dev);
1308 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1309 +                                               charger);
1310 +       int ret;
1311 +
1312 +       if (strcmp(attr->attr.name, "current_limit") == 0)
1313 +               ret = bq2415x_get_current_limit(bq);
1314 +       else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1315 +               ret = bq2415x_get_weak_battery_voltage(bq);
1316 +       else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1317 +               ret = bq2415x_get_battery_regulation_voltage(bq);
1318 +       else if (strcmp(attr->attr.name, "charge_current") == 0)
1319 +               ret = bq2415x_get_charge_current(bq);
1320 +       else if (strcmp(attr->attr.name, "termination_current") == 0)
1321 +               ret = bq2415x_get_termination_current(bq);
1322 +       else
1323 +               return -EINVAL;
1324 +
1325 +       if (ret < 0)
1326 +               return ret;
1327 +       else
1328 +               return sprintf(buf, "%d\n", ret);
1329 +}
1330 +
1331 +/* set *_enable entries */
1332 +static ssize_t bq2415x_sysfs_set_enable(struct device *dev,
1333 +               struct device_attribute *attr, const char *buf, size_t count)
1334 +{
1335 +       struct power_supply *psy = dev_get_drvdata(dev);
1336 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1337 +                                               charger);
1338 +       enum bq2415x_command command;
1339 +       long val;
1340 +       int ret;
1341 +
1342 +       if (kstrtol(buf, 10, &val) < 0)
1343 +               return -EINVAL;
1344 +
1345 +       if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1346 +               command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE :
1347 +                       BQ2415X_CHARGE_TERMINATION_DISABLE;
1348 +       else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1349 +               command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE :
1350 +                       BQ2415X_HIGH_IMPEDANCE_DISABLE;
1351 +       else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1352 +               command = val ? BQ2415X_OTG_PIN_ENABLE :
1353 +                       BQ2415X_OTG_PIN_DISABLE;
1354 +       else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1355 +               command = val ? BQ2415X_STAT_PIN_ENABLE :
1356 +                       BQ2415X_STAT_PIN_DISABLE;
1357 +       else
1358 +               return -EINVAL;
1359 +
1360 +       ret = bq2415x_exec_command(bq, command);
1361 +       if (ret < 0)
1362 +               return ret;
1363 +       else
1364 +               return count;
1365 +}
1366 +
1367 +/* show *_enable entries */
1368 +static ssize_t bq2415x_sysfs_show_enable(struct device *dev,
1369 +               struct device_attribute *attr, char *buf)
1370 +{
1371 +       struct power_supply *psy = dev_get_drvdata(dev);
1372 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1373 +                                               charger);
1374 +       enum bq2415x_command command;
1375 +       int ret;
1376 +
1377 +       if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1378 +               command = BQ2415X_CHARGE_TERMINATION_STATUS;
1379 +       else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1380 +               command = BQ2415X_HIGH_IMPEDANCE_STATUS;
1381 +       else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1382 +               command = BQ2415X_OTG_PIN_STATUS;
1383 +       else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1384 +               command = BQ2415X_STAT_PIN_STATUS;
1385 +       else
1386 +               return -EINVAL;
1387 +
1388 +       ret = bq2415x_exec_command(bq, command);
1389 +       if (ret < 0)
1390 +               return ret;
1391 +       else
1392 +               return sprintf(buf, "%d\n", ret);
1393 +}
1394 +
1395 +static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO,
1396 +               bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1397 +static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO,
1398 +               bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1399 +static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO,
1400 +               bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1401 +static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO,
1402 +               bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1403 +static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
1404 +               bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1405 +
1406 +static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO,
1407 +               bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1408 +static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
1409 +               bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1410 +static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO,
1411 +               bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1412 +static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
1413 +               bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1414 +
1415 +static DEVICE_ATTR(reported_mode, S_IRUGO,
1416 +               bq2415x_sysfs_show_reported_mode, NULL);
1417 +static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
1418 +               bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
1419 +static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
1420 +               bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer);
1421 +
1422 +static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO,
1423 +               bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers);
1424 +
1425 +static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1426 +static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1427 +static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1428 +static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1429 +
1430 +static struct attribute *bq2415x_sysfs_attributes[] = {
1431 +       &dev_attr_current_limit.attr,
1432 +       &dev_attr_weak_battery_voltage.attr,
1433 +       &dev_attr_battery_regulation_voltage.attr,
1434 +       &dev_attr_charge_current.attr,
1435 +       &dev_attr_termination_current.attr,
1436 +
1437 +       &dev_attr_charge_termination_enable.attr,
1438 +       &dev_attr_high_impedance_enable.attr,
1439 +       &dev_attr_otg_pin_enable.attr,
1440 +       &dev_attr_stat_pin_enable.attr,
1441 +
1442 +       &dev_attr_reported_mode.attr,
1443 +       &dev_attr_mode.attr,
1444 +       &dev_attr_timer.attr,
1445 +
1446 +       &dev_attr_registers.attr,
1447 +
1448 +       &dev_attr_otg_status.attr,
1449 +       &dev_attr_charge_status.attr,
1450 +       &dev_attr_boost_status.attr,
1451 +       &dev_attr_fault_status.attr,
1452 +       NULL,
1453 +};
1454 +
1455 +static const struct attribute_group bq2415x_sysfs_attr_group = {
1456 +       .attrs = bq2415x_sysfs_attributes,
1457 +};
1458 +
1459 +static int bq2415x_sysfs_init(struct bq2415x_device *bq)
1460 +{
1461 +       return sysfs_create_group(&bq->charger.dev->kobj,
1462 +                       &bq2415x_sysfs_attr_group);
1463 +}
1464 +
1465 +static void bq2415x_sysfs_exit(struct bq2415x_device *bq)
1466 +{
1467 +       sysfs_remove_group(&bq->charger.dev->kobj, &bq2415x_sysfs_attr_group);
1468 +}
1469 +
1470 +/* main bq2415x probe function */
1471 +static int bq2415x_probe(struct i2c_client *client,
1472 +               const struct i2c_device_id *id)
1473 +{
1474 +       int ret;
1475 +       int num;
1476 +       char *name;
1477 +       struct bq2415x_device *bq;
1478 +
1479 +       if (!client->dev.platform_data) {
1480 +               dev_err(&client->dev, "platform data not set\n");
1481 +               return -ENODEV;
1482 +       }
1483 +
1484 +       /* Get new ID for the new device */
1485 +       ret = idr_pre_get(&bq2415x_id, GFP_KERNEL);
1486 +       if (ret == 0)
1487 +               return -ENOMEM;
1488 +
1489 +       mutex_lock(&bq2415x_id_mutex);
1490 +       ret = idr_get_new(&bq2415x_id, client, &num);
1491 +       mutex_unlock(&bq2415x_id_mutex);
1492 +
1493 +       if (ret < 0)
1494 +               return ret;
1495 +
1496 +       name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
1497 +       if (!name) {
1498 +               dev_err(&client->dev, "failed to allocate device name\n");
1499 +               ret = -ENOMEM;
1500 +               goto error_1;
1501 +       }
1502 +
1503 +       bq = kzalloc(sizeof(*bq), GFP_KERNEL);
1504 +       if (!bq) {
1505 +               dev_err(&client->dev, "failed to allocate device data\n");
1506 +               ret = -ENOMEM;
1507 +               goto error_2;
1508 +       }
1509 +
1510 +       i2c_set_clientdata(client, bq);
1511 +
1512 +       bq->id = num;
1513 +       bq->dev = &client->dev;
1514 +       bq->chip = id->driver_data;
1515 +       bq->name = name;
1516 +       bq->mode = BQ2415X_MODE_NONE;
1517 +       bq->reported_mode = BQ2415X_MODE_NONE;
1518 +       bq->autotimer = 0;
1519 +       bq->automode = 0;
1520 +
1521 +       memcpy(&bq->init_data, client->dev.platform_data,
1522 +                       sizeof(bq->init_data));
1523 +
1524 +       bq2415x_reset_chip(bq);
1525 +
1526 +       ret = bq2415x_power_supply_init(bq);
1527 +       if (ret) {
1528 +               dev_err(bq->dev, "failed to register power supply: %d\n", ret);
1529 +               goto error_3;
1530 +       }
1531 +
1532 +       ret = bq2415x_sysfs_init(bq);
1533 +       if (ret) {
1534 +               dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret);
1535 +               goto error_4;
1536 +       }
1537 +
1538 +       ret = bq2415x_set_defaults(bq);
1539 +       if (ret) {
1540 +               dev_err(bq->dev, "failed to set default values: %d\n", ret);
1541 +               goto error_5;
1542 +       }
1543 +
1544 +       if (bq->init_data.set_mode_hook) {
1545 +               if (bq->init_data.set_mode_hook(
1546 +                               bq2415x_hook_function, bq)) {
1547 +                       bq->automode = 1;
1548 +                       bq2415x_set_mode(bq, bq->reported_mode);
1549 +                       dev_info(bq->dev, "automode enabled\n");
1550 +               } else {
1551 +                       bq->automode = -1;
1552 +                       dev_info(bq->dev, "automode failed\n");
1553 +               }
1554 +       } else {
1555 +               bq->automode = -1;
1556 +               dev_info(bq->dev, "automode not supported\n");
1557 +       }
1558 +
1559 +       INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work);
1560 +       bq2415x_set_autotimer(bq, 1);
1561 +
1562 +       dev_info(bq->dev, "driver registered\n");
1563 +       return 0;
1564 +
1565 +error_5:
1566 +       bq2415x_sysfs_exit(bq);
1567 +error_4:
1568 +       bq2415x_power_supply_exit(bq);
1569 +error_3:
1570 +       kfree(bq);
1571 +error_2:
1572 +       kfree(name);
1573 +error_1:
1574 +       mutex_lock(&bq2415x_id_mutex);
1575 +       idr_remove(&bq2415x_id, num);
1576 +       mutex_unlock(&bq2415x_id_mutex);
1577 +
1578 +       return ret;
1579 +}
1580 +
1581 +/* main bq2415x remove function */
1582 +
1583 +static int bq2415x_remove(struct i2c_client *client)
1584 +{
1585 +       struct bq2415x_device *bq = i2c_get_clientdata(client);
1586 +
1587 +       if (bq->init_data.set_mode_hook)
1588 +               bq->init_data.set_mode_hook(NULL, NULL);
1589 +
1590 +       bq2415x_sysfs_exit(bq);
1591 +       bq2415x_power_supply_exit(bq);
1592 +
1593 +       bq2415x_reset_chip(bq);
1594 +
1595 +       mutex_lock(&bq2415x_id_mutex);
1596 +       idr_remove(&bq2415x_id, bq->id);
1597 +       mutex_unlock(&bq2415x_id_mutex);
1598 +
1599 +       dev_info(bq->dev, "driver unregistered\n");
1600 +
1601 +       kfree(bq->name);
1602 +       kfree(bq);
1603 +
1604 +       return 0;
1605 +}
1606 +
1607 +static const struct i2c_device_id bq2415x_i2c_id_table[] = {
1608 +       { "bq2415x", BQUNKNOWN },
1609 +       { "bq24150", BQ24150 },
1610 +       { "bq24150a", BQ24150A },
1611 +       { "bq24151", BQ24151 },
1612 +       { "bq24151a", BQ24151A },
1613 +       { "bq24152", BQ24152 },
1614 +       { "bq24153", BQ24153 },
1615 +       { "bq24153a", BQ24153A },
1616 +       { "bq24155", BQ24155 },
1617 +       { "bq24156", BQ24156 },
1618 +       { "bq24156a", BQ24156A },
1619 +       { "bq24158", BQ24158 },
1620 +       {},
1621 +};
1622 +MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table);
1623 +
1624 +static struct i2c_driver bq2415x_driver = {
1625 +       .driver = {
1626 +               .name = "bq2415x-charger",
1627 +       },
1628 +       .probe = bq2415x_probe,
1629 +       .remove = bq2415x_remove,
1630 +       .id_table = bq2415x_i2c_id_table,
1631 +};
1632 +
1633 +static int __init bq2415x_init(void)
1634 +{
1635 +       return i2c_add_driver(&bq2415x_driver);
1636 +}
1637 +module_init(bq2415x_init);
1638 +
1639 +static void __exit bq2415x_exit(void)
1640 +{
1641 +       i2c_del_driver(&bq2415x_driver);
1642 +}
1643 +module_exit(bq2415x_exit);
1644 +
1645 +MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
1646 +MODULE_DESCRIPTION("bq2415x charger driver");
1647 +MODULE_LICENSE("GPL");
1648 --- /dev/null
1649 +++ kernel-power/include/linux/power/bq2415x_charger.h
1650 @@ -0,0 +1,90 @@
1651 +/*
1652 +    bq2415x_charger.h - bq2415x charger driver
1653 +    Copyright (C) 2011-2012  Pali Rohár <pali.rohar@gmail.com>
1654 +
1655 +    This program is free software; you can redistribute it and/or modify
1656 +    it under the terms of the GNU General Public License as published by
1657 +    the Free Software Foundation; either version 2 of the License, or
1658 +    (at your option) any later version.
1659 +
1660 +    This program is distributed in the hope that it will be useful,
1661 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
1662 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1663 +    GNU General Public License for more details.
1664 +
1665 +    You should have received a copy of the GNU General Public License along
1666 +    with this program; if not, write to the Free Software Foundation, Inc.,
1667 +    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1668 +*/
1669 +
1670 +#ifndef BQ2415X_CHARGER_H
1671 +#define BQ2415X_CHARGER_H
1672 +
1673 +/*
1674 +  This is platform data for bq2415x chip. It contains default board voltages
1675 +  and currents which can be also later configured via sysfs. If value is -1
1676 +  then default chip value (specified in datasheet) will be used.
1677 +
1678 +  Value resistor_sense is needed for for configuring charge and termination
1679 +  current. It it is less or equal to zero, configuring charge and termination
1680 +  current will not be possible.
1681 +
1682 +  Function set_mode_hook is needed for automode (setting correct current limit
1683 +  when charger is connected/disconnected or setting boost mode). When is NULL,
1684 +  automode function is disabled. When is not NULL, it must have this prototype:
1685 +
1686 +    int (*set_mode_hook)(
1687 +      void (*hook)(enum bq2415x_mode mode, void *data),
1688 +      void *data)
1689 +
1690 +  hook is hook function (see below) and data is pointer to driver private data
1691 +
1692 +  bq2415x driver will call it as:
1693 +
1694 +    platform_data->set_mode_hook(bq2415x_hook_function, bq2415x_device);
1695 +
1696 +  Board/platform function set_mode_hook return non zero value when hook
1697 +  function was successful registered. Platform code should call that hook
1698 +  function (which get from pointer, with data) every time when charger was
1699 +  connected/disconnected or require to enable boost mode. bq2415x driver then
1700 +  will set correct current limit, enable/disable charger or boost mode.
1701 +
1702 +  Hook function has this prototype:
1703 +
1704 +    void hook(enum bq2415x_mode mode, void *data);
1705 +
1706 +  mode is bq2415x mode (charger or boost)
1707 +  data is pointer to driver private data (which get from set_charger_type_hook)
1708 +
1709 +  When bq driver is being unloaded, it call function:
1710 +
1711 +    platform_data->set_mode_hook(NULL, NULL);
1712 +
1713 +  (hook function and driver private data are NULL)
1714 +
1715 +  After that board/platform code must not call driver hook function! It is
1716 +  possible that pointer to hook function will not be valid and calling will
1717 +  cause undefined result.
1718 +
1719 +*/
1720 +
1721 +/* Supported modes with maximal current limit */
1722 +enum bq2415x_mode {
1723 +       BQ2415X_MODE_NONE,              /* unknown or no charger (100mA) */
1724 +       BQ2415X_MODE_HOST_CHARGER,      /* usb host/hub charger (500mA) */
1725 +       BQ2415X_MODE_DEDICATED_CHARGER, /* dedicated charger (unlimited) */
1726 +       BQ2415X_MODE_BOOST,             /* boost mode (charging disabled) */
1727 +};
1728 +
1729 +struct bq2415x_platform_data {
1730 +       int current_limit;              /* mA */
1731 +       int weak_battery_voltage;       /* mV */
1732 +       int battery_regulation_voltage; /* mV */
1733 +       int charge_current;             /* mA */
1734 +       int termination_current;        /* mA */
1735 +       int resistor_sense;             /* m ohm */
1736 +       int (*set_mode_hook)(void (*hook)(enum bq2415x_mode mode, void *data),
1737 +                            void *data);
1738 +};
1739 +
1740 +#endif