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