bq2415x_charger.patch: Make sure that hook function can be called for init reported...
[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,1636 @@
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 +       dev_dbg(bq->dev, "hook function was called\n");
796 +       bq->reported_mode = mode;
797 +
798 +       /* if automode is not enabled do not tell about reported_mode */
799 +       if (bq->automode < 1)
800 +               return;
801 +
802 +       sysfs_notify(&bq->charger.dev->kobj, NULL, "reported_mode");
803 +       bq2415x_set_mode(bq, bq->reported_mode);
804 +
805 +}
806 +
807 +/**** timer functions ****/
808 +
809 +/* enable/disable auto resetting chip timer */
810 +static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
811 +{
812 +       mutex_lock(&bq2415x_timer_mutex);
813 +
814 +       if (bq->autotimer == state) {
815 +               mutex_unlock(&bq2415x_timer_mutex);
816 +               return;
817 +       }
818 +
819 +       bq->autotimer = state;
820 +
821 +       if (state) {
822 +               schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
823 +               bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
824 +       } else {
825 +               cancel_delayed_work_sync(&bq->work);
826 +       }
827 +
828 +       mutex_unlock(&bq2415x_timer_mutex);
829 +}
830 +
831 +/* called by bq2415x_timer_work on timer error */
832 +static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
833 +{
834 +       dev_err(bq->dev, "%s\n", msg);
835 +       if (bq->automode > 0)
836 +               bq->automode = 0;
837 +       bq2415x_set_mode(bq, BQ2415X_MODE_NONE);
838 +       bq2415x_set_autotimer(bq, 0);
839 +}
840 +
841 +/* delayed work function for auto resetting chip timer */
842 +static void bq2415x_timer_work(struct work_struct *work)
843 +{
844 +       struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
845 +                                               work.work);
846 +       int ret, error, boost;
847 +
848 +       if (!bq->autotimer)
849 +               return;
850 +
851 +       ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
852 +       if (ret < 0) {
853 +               bq2415x_timer_error(bq, "Resetting timer failed");
854 +               return;
855 +       }
856 +
857 +       boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
858 +       if (boost < 0) {
859 +               bq2415x_timer_error(bq, "Unknown error");
860 +               return;
861 +       }
862 +
863 +       error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
864 +       if (error < 0) {
865 +               bq2415x_timer_error(bq, "Unknown error");
866 +               return;
867 +       }
868 +
869 +       if (boost) {
870 +               switch (error) {
871 +               /* Non fatal errors, chip is OK */
872 +               case 0: /* No error */
873 +                       break;
874 +               case 6: /* Timer expired */
875 +                       dev_err(bq->dev, "Timer expired\n");
876 +                       break;
877 +               case 3: /* Battery voltage too low */
878 +                       dev_err(bq->dev, "Battery voltage to low\n");
879 +                       break;
880 +
881 +               /* Fatal errors, disable and reset chip */
882 +               case 1: /* Overvoltage protection (chip fried) */
883 +                       bq2415x_timer_error(bq,
884 +                               "Overvoltage protection (chip fried)");
885 +                       return;
886 +               case 2: /* Overload */
887 +                       bq2415x_timer_error(bq, "Overload");
888 +                       return;
889 +               case 4: /* Battery overvoltage protection */
890 +                       bq2415x_timer_error(bq,
891 +                               "Battery overvoltage protection");
892 +                       return;
893 +               case 5: /* Thermal shutdown (too hot) */
894 +                       bq2415x_timer_error(bq,
895 +                                       "Thermal shutdown (too hot)");
896 +                       return;
897 +               case 7: /* N/A */
898 +                       bq2415x_timer_error(bq, "Unknown error");
899 +                       return;
900 +               }
901 +       } else {
902 +               switch (error) {
903 +               /* Non fatal errors, chip is OK */
904 +               case 0: /* No error */
905 +                       break;
906 +               case 2: /* Sleep mode */
907 +                       dev_err(bq->dev, "Sleep mode\n");
908 +                       break;
909 +               case 3: /* Poor input source */
910 +                       dev_err(bq->dev, "Poor input source\n");
911 +                       break;
912 +               case 6: /* Timer expired */
913 +                       dev_err(bq->dev, "Timer expired\n");
914 +                       break;
915 +               case 7: /* No battery */
916 +                       dev_err(bq->dev, "No battery\n");
917 +                       break;
918 +
919 +               /* Fatal errors, disable and reset chip */
920 +               case 1: /* Overvoltage protection (chip fried) */
921 +                       bq2415x_timer_error(bq,
922 +                               "Overvoltage protection (chip fried)");
923 +                       return;
924 +               case 4: /* Battery overvoltage protection */
925 +                       bq2415x_timer_error(bq,
926 +                               "Battery overvoltage protection");
927 +                       return;
928 +               case 5: /* Thermal shutdown (too hot) */
929 +                       bq2415x_timer_error(bq,
930 +                               "Thermal shutdown (too hot)");
931 +                       return;
932 +               }
933 +       }
934 +
935 +       schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
936 +}
937 +
938 +/**** power supply interface code ****/
939 +
940 +static enum power_supply_property bq2415x_power_supply_props[] = {
941 +       /* TODO: maybe add more power supply properties */
942 +       POWER_SUPPLY_PROP_STATUS,
943 +       POWER_SUPPLY_PROP_MODEL_NAME,
944 +};
945 +
946 +static int bq2415x_power_supply_get_property(struct power_supply *psy,
947 +       enum power_supply_property psp, union power_supply_propval *val)
948 +{
949 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
950 +                                               charger);
951 +       int ret;
952 +
953 +       switch (psp) {
954 +       case POWER_SUPPLY_PROP_STATUS:
955 +               ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
956 +               if (ret < 0)
957 +                       return ret;
958 +               else if (ret == 0) /* Ready */
959 +                       val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
960 +               else if (ret == 1) /* Charge in progress */
961 +                       val->intval = POWER_SUPPLY_STATUS_CHARGING;
962 +               else if (ret == 2) /* Charge done */
963 +                       val->intval = POWER_SUPPLY_STATUS_FULL;
964 +               else
965 +                       val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
966 +               break;
967 +       case POWER_SUPPLY_PROP_MODEL_NAME:
968 +               val->strval = bq->model;
969 +               break;
970 +       default:
971 +               return -EINVAL;
972 +       }
973 +       return 0;
974 +}
975 +
976 +static int bq2415x_power_supply_init(struct bq2415x_device *bq)
977 +{
978 +       int ret;
979 +       int chip;
980 +       char revstr[8];
981 +
982 +       bq->charger.name = bq->name;
983 +       bq->charger.type = POWER_SUPPLY_TYPE_USB;
984 +       bq->charger.properties = bq2415x_power_supply_props;
985 +       bq->charger.num_properties = ARRAY_SIZE(bq2415x_power_supply_props);
986 +       bq->charger.get_property = bq2415x_power_supply_get_property;
987 +
988 +       ret = bq2415x_detect_chip(bq);
989 +       if (ret < 0)
990 +               chip = BQUNKNOWN;
991 +       else
992 +               chip = ret;
993 +
994 +       ret = bq2415x_detect_revision(bq);
995 +       if (ret < 0)
996 +               strcpy(revstr, "unknown");
997 +       else
998 +               sprintf(revstr, "1.%d", ret);
999 +
1000 +       bq->model = kasprintf(GFP_KERNEL,
1001 +                               "chip %s, revision %s, vender code %.3d",
1002 +                               bq2415x_chip_name[chip], revstr,
1003 +                               bq2415x_get_vender_code(bq));
1004 +       if (!bq->model) {
1005 +               dev_err(bq->dev, "failed to allocate model name\n");
1006 +               return -ENOMEM;
1007 +       }
1008 +
1009 +       ret = power_supply_register(bq->dev, &bq->charger);
1010 +       if (ret) {
1011 +               kfree(bq->model);
1012 +               return ret;
1013 +       }
1014 +
1015 +       return 0;
1016 +}
1017 +
1018 +static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
1019 +{
1020 +       bq->autotimer = 0;
1021 +       if (bq->automode > 0)
1022 +               bq->automode = 0;
1023 +       cancel_delayed_work_sync(&bq->work);
1024 +       power_supply_unregister(&bq->charger);
1025 +       kfree(bq->model);
1026 +}
1027 +
1028 +/**** additional sysfs entries for power supply interface ****/
1029 +
1030 +/* show *_status entries */
1031 +static ssize_t bq2415x_sysfs_show_status(struct device *dev,
1032 +               struct device_attribute *attr, char *buf)
1033 +{
1034 +       struct power_supply *psy = dev_get_drvdata(dev);
1035 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1036 +                                               charger);
1037 +       enum bq2415x_command command;
1038 +       int ret;
1039 +
1040 +       if (strcmp(attr->attr.name, "otg_status") == 0)
1041 +               command = BQ2415X_OTG_STATUS;
1042 +       else if (strcmp(attr->attr.name, "charge_status") == 0)
1043 +               command = BQ2415X_CHARGE_STATUS;
1044 +       else if (strcmp(attr->attr.name, "boost_status") == 0)
1045 +               command = BQ2415X_BOOST_STATUS;
1046 +       else if (strcmp(attr->attr.name, "fault_status") == 0)
1047 +               command = BQ2415X_FAULT_STATUS;
1048 +       else
1049 +               return -EINVAL;
1050 +
1051 +       ret = bq2415x_exec_command(bq, command);
1052 +       if (ret < 0)
1053 +               return ret;
1054 +       else
1055 +               return sprintf(buf, "%d\n", ret);
1056 +}
1057 +
1058 +/* set timer entry:
1059 +     auto - enable auto mode
1060 +     off - disable auto mode
1061 +     (other values) - reset chip timer
1062 +*/
1063 +static ssize_t bq2415x_sysfs_set_timer(struct device *dev,
1064 +               struct device_attribute *attr, const char *buf, size_t count)
1065 +{
1066 +       struct power_supply *psy = dev_get_drvdata(dev);
1067 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1068 +                                               charger);
1069 +       int ret = 0;
1070 +
1071 +       if (strncmp(buf, "auto", 4) == 0)
1072 +               bq2415x_set_autotimer(bq, 1);
1073 +       else if (strncmp(buf, "off", 3) == 0)
1074 +               bq2415x_set_autotimer(bq, 0);
1075 +       else
1076 +               ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
1077 +
1078 +       if (ret < 0)
1079 +               return ret;
1080 +       else
1081 +               return count;
1082 +}
1083 +
1084 +/* show timer entry (auto or off) */
1085 +static ssize_t bq2415x_sysfs_show_timer(struct device *dev,
1086 +               struct device_attribute *attr, char *buf)
1087 +{
1088 +       struct power_supply *psy = dev_get_drvdata(dev);
1089 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1090 +                                               charger);
1091 +
1092 +       if (bq->autotimer)
1093 +               return sprintf(buf, "auto\n");
1094 +       else
1095 +               return sprintf(buf, "off\n");
1096 +}
1097 +
1098 +/* set mode entry:
1099 +     auto - if automode is supported, enable it and set mode to reported
1100 +     none - disable charger and boost mode
1101 +     host - charging mode for host/hub chargers (current limit 500mA)
1102 +     dedicated - charging mode for dedicated chargers (unlimited current limit)
1103 +     boost - disable charger and enable boost mode
1104 +*/
1105 +static ssize_t bq2415x_sysfs_set_mode(struct device *dev,
1106 +               struct device_attribute *attr, const char *buf, size_t count)
1107 +{
1108 +       struct power_supply *psy = dev_get_drvdata(dev);
1109 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1110 +                                               charger);
1111 +       enum bq2415x_mode mode;
1112 +       int ret = 0;
1113 +
1114 +       if (strncmp(buf, "auto", 4) == 0) {
1115 +               if (bq->automode < 0)
1116 +                       return -ENOSYS;
1117 +               bq->automode = 1;
1118 +               mode = bq->reported_mode;
1119 +       } else if (strncmp(buf, "none", 4) == 0) {
1120 +               if (bq->automode > 0)
1121 +                       bq->automode = 0;
1122 +               mode = BQ2415X_MODE_NONE;
1123 +       } else if (strncmp(buf, "host", 4) == 0) {
1124 +               if (bq->automode > 0)
1125 +                       bq->automode = 0;
1126 +               mode = BQ2415X_MODE_HOST_CHARGER;
1127 +       } else if (strncmp(buf, "dedicated", 9) == 0) {
1128 +               if (bq->automode > 0)
1129 +                       bq->automode = 0;
1130 +               mode = BQ2415X_MODE_DEDICATED_CHARGER;
1131 +       } else if (strncmp(buf, "boost", 5) == 0) {
1132 +               if (bq->automode > 0)
1133 +                       bq->automode = 0;
1134 +               mode = BQ2415X_MODE_BOOST;
1135 +       } else if (strncmp(buf, "reset", 5) == 0) {
1136 +               bq2415x_reset_chip(bq);
1137 +               bq2415x_set_defaults(bq);
1138 +               if (bq->automode <= 0)
1139 +                       return count;
1140 +               bq->automode = 1;
1141 +               mode = bq->reported_mode;
1142 +       } else
1143 +               return -EINVAL;
1144 +
1145 +       ret = bq2415x_set_mode(bq, mode);
1146 +       if (ret < 0)
1147 +               return ret;
1148 +       else
1149 +               return count;
1150 +}
1151 +
1152 +/* show mode entry (auto, none, host, dedicated or boost) */
1153 +static ssize_t bq2415x_sysfs_show_mode(struct device *dev,
1154 +               struct device_attribute *attr, char *buf)
1155 +{
1156 +       struct power_supply *psy = dev_get_drvdata(dev);
1157 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1158 +                                               charger);
1159 +       ssize_t ret = 0;
1160 +
1161 +       if (bq->automode > 0)
1162 +               ret += sprintf(buf+ret, "auto (");
1163 +
1164 +       switch (bq->mode) {
1165 +       case BQ2415X_MODE_NONE:
1166 +               ret += sprintf(buf+ret, "none");
1167 +               break;
1168 +       case BQ2415X_MODE_HOST_CHARGER:
1169 +               ret += sprintf(buf+ret, "host");
1170 +               break;
1171 +       case BQ2415X_MODE_DEDICATED_CHARGER:
1172 +               ret += sprintf(buf+ret, "dedicated");
1173 +               break;
1174 +       case BQ2415X_MODE_BOOST:
1175 +               ret += sprintf(buf+ret, "boost");
1176 +               break;
1177 +       }
1178 +
1179 +       if (bq->automode > 0)
1180 +               ret += sprintf(buf+ret, ")");
1181 +
1182 +       ret += sprintf(buf+ret, "\n");
1183 +       return ret;
1184 +}
1185 +
1186 +/* show reported_mode entry (none, host, dedicated or boost) */
1187 +static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
1188 +               struct device_attribute *attr, char *buf)
1189 +{
1190 +       struct power_supply *psy = dev_get_drvdata(dev);
1191 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1192 +                                               charger);
1193 +
1194 +       if (bq->automode < 0)
1195 +               return -EINVAL;
1196 +
1197 +       switch (bq->reported_mode) {
1198 +       case BQ2415X_MODE_NONE:
1199 +               return sprintf(buf, "none\n");
1200 +       case BQ2415X_MODE_HOST_CHARGER:
1201 +               return sprintf(buf, "host\n");
1202 +       case BQ2415X_MODE_DEDICATED_CHARGER:
1203 +               return sprintf(buf, "dedicated\n");
1204 +       case BQ2415X_MODE_BOOST:
1205 +               return sprintf(buf, "boost\n");
1206 +       }
1207 +
1208 +       return -EINVAL;
1209 +}
1210 +
1211 +/* directly set raw value to chip register, format: 'register value' */
1212 +static ssize_t bq2415x_sysfs_set_registers(struct device *dev,
1213 +               struct device_attribute *attr, const char *buf, size_t count)
1214 +{
1215 +       struct power_supply *psy = dev_get_drvdata(dev);
1216 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1217 +                                               charger);
1218 +       ssize_t ret = 0;
1219 +       unsigned int reg;
1220 +       unsigned int val;
1221 +
1222 +       if (sscanf(buf, "%x %x", &reg, &val) != 2)
1223 +               return -EINVAL;
1224 +
1225 +       if (reg > 4 || val > 255)
1226 +               return -EINVAL;
1227 +
1228 +       ret = bq2415x_i2c_write(bq, reg, val);
1229 +       if (ret < 0)
1230 +               return ret;
1231 +       else
1232 +               return count;
1233 +}
1234 +
1235 +/* print value of chip register, format: 'register=value' */
1236 +static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
1237 +                                       u8 reg, char *buf)
1238 +{
1239 +       int ret = bq2415x_i2c_read(bq, reg);
1240 +       if (ret < 0)
1241 +               return sprintf(buf, "%#.2x=error %d\n", reg, ret);
1242 +       else
1243 +               return sprintf(buf, "%#.2x=%#.2x\n", reg, ret);
1244 +}
1245 +
1246 +/* show all raw values of chip register, format per line: 'register=value' */
1247 +static ssize_t bq2415x_sysfs_show_registers(struct device *dev,
1248 +               struct device_attribute *attr, char *buf)
1249 +{
1250 +       struct power_supply *psy = dev_get_drvdata(dev);
1251 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1252 +                                               charger);
1253 +       ssize_t ret = 0;
1254 +
1255 +       ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret);
1256 +       ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret);
1257 +       ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret);
1258 +       ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret);
1259 +       ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret);
1260 +       return ret;
1261 +}
1262 +
1263 +/* set current and voltage limit entries (in mA or mV) */
1264 +static ssize_t bq2415x_sysfs_set_limit(struct device *dev,
1265 +               struct device_attribute *attr, const char *buf, size_t count)
1266 +{
1267 +       struct power_supply *psy = dev_get_drvdata(dev);
1268 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1269 +                                               charger);
1270 +       long val;
1271 +       int ret;
1272 +
1273 +       if (kstrtol(buf, 10, &val) < 0)
1274 +               return -EINVAL;
1275 +
1276 +       if (strcmp(attr->attr.name, "current_limit") == 0)
1277 +               ret = bq2415x_set_current_limit(bq, val);
1278 +       else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1279 +               ret = bq2415x_set_weak_battery_voltage(bq, val);
1280 +       else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1281 +               ret = bq2415x_set_battery_regulation_voltage(bq, val);
1282 +       else if (strcmp(attr->attr.name, "charge_current") == 0)
1283 +               ret = bq2415x_set_charge_current(bq, val);
1284 +       else if (strcmp(attr->attr.name, "termination_current") == 0)
1285 +               ret = bq2415x_set_termination_current(bq, val);
1286 +       else
1287 +               return -EINVAL;
1288 +
1289 +       if (ret < 0)
1290 +               return ret;
1291 +       else
1292 +               return count;
1293 +}
1294 +
1295 +/* show current and voltage limit entries (in mA or mV) */
1296 +static ssize_t bq2415x_sysfs_show_limit(struct device *dev,
1297 +               struct device_attribute *attr, char *buf)
1298 +{
1299 +       struct power_supply *psy = dev_get_drvdata(dev);
1300 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1301 +                                               charger);
1302 +       int ret;
1303 +
1304 +       if (strcmp(attr->attr.name, "current_limit") == 0)
1305 +               ret = bq2415x_get_current_limit(bq);
1306 +       else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1307 +               ret = bq2415x_get_weak_battery_voltage(bq);
1308 +       else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1309 +               ret = bq2415x_get_battery_regulation_voltage(bq);
1310 +       else if (strcmp(attr->attr.name, "charge_current") == 0)
1311 +               ret = bq2415x_get_charge_current(bq);
1312 +       else if (strcmp(attr->attr.name, "termination_current") == 0)
1313 +               ret = bq2415x_get_termination_current(bq);
1314 +       else
1315 +               return -EINVAL;
1316 +
1317 +       if (ret < 0)
1318 +               return ret;
1319 +       else
1320 +               return sprintf(buf, "%d\n", ret);
1321 +}
1322 +
1323 +/* set *_enable entries */
1324 +static ssize_t bq2415x_sysfs_set_enable(struct device *dev,
1325 +               struct device_attribute *attr, const char *buf, size_t count)
1326 +{
1327 +       struct power_supply *psy = dev_get_drvdata(dev);
1328 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1329 +                                               charger);
1330 +       enum bq2415x_command command;
1331 +       long val;
1332 +       int ret;
1333 +
1334 +       if (kstrtol(buf, 10, &val) < 0)
1335 +               return -EINVAL;
1336 +
1337 +       if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1338 +               command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE :
1339 +                       BQ2415X_CHARGE_TERMINATION_DISABLE;
1340 +       else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1341 +               command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE :
1342 +                       BQ2415X_HIGH_IMPEDANCE_DISABLE;
1343 +       else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1344 +               command = val ? BQ2415X_OTG_PIN_ENABLE :
1345 +                       BQ2415X_OTG_PIN_DISABLE;
1346 +       else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1347 +               command = val ? BQ2415X_STAT_PIN_ENABLE :
1348 +                       BQ2415X_STAT_PIN_DISABLE;
1349 +       else
1350 +               return -EINVAL;
1351 +
1352 +       ret = bq2415x_exec_command(bq, command);
1353 +       if (ret < 0)
1354 +               return ret;
1355 +       else
1356 +               return count;
1357 +}
1358 +
1359 +/* show *_enable entries */
1360 +static ssize_t bq2415x_sysfs_show_enable(struct device *dev,
1361 +               struct device_attribute *attr, char *buf)
1362 +{
1363 +       struct power_supply *psy = dev_get_drvdata(dev);
1364 +       struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
1365 +                                               charger);
1366 +       enum bq2415x_command command;
1367 +       int ret;
1368 +
1369 +       if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1370 +               command = BQ2415X_CHARGE_TERMINATION_STATUS;
1371 +       else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1372 +               command = BQ2415X_HIGH_IMPEDANCE_STATUS;
1373 +       else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1374 +               command = BQ2415X_OTG_PIN_STATUS;
1375 +       else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1376 +               command = BQ2415X_STAT_PIN_STATUS;
1377 +       else
1378 +               return -EINVAL;
1379 +
1380 +       ret = bq2415x_exec_command(bq, command);
1381 +       if (ret < 0)
1382 +               return ret;
1383 +       else
1384 +               return sprintf(buf, "%d\n", ret);
1385 +}
1386 +
1387 +static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO,
1388 +               bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1389 +static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO,
1390 +               bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1391 +static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO,
1392 +               bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1393 +static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO,
1394 +               bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1395 +static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
1396 +               bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1397 +
1398 +static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO,
1399 +               bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1400 +static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
1401 +               bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1402 +static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO,
1403 +               bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1404 +static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
1405 +               bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1406 +
1407 +static DEVICE_ATTR(reported_mode, S_IRUGO,
1408 +               bq2415x_sysfs_show_reported_mode, NULL);
1409 +static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
1410 +               bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
1411 +static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
1412 +               bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer);
1413 +
1414 +static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO,
1415 +               bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers);
1416 +
1417 +static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1418 +static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1419 +static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1420 +static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1421 +
1422 +static struct attribute *bq2415x_sysfs_attributes[] = {
1423 +       &dev_attr_current_limit.attr,
1424 +       &dev_attr_weak_battery_voltage.attr,
1425 +       &dev_attr_battery_regulation_voltage.attr,
1426 +       &dev_attr_charge_current.attr,
1427 +       &dev_attr_termination_current.attr,
1428 +
1429 +       &dev_attr_charge_termination_enable.attr,
1430 +       &dev_attr_high_impedance_enable.attr,
1431 +       &dev_attr_otg_pin_enable.attr,
1432 +       &dev_attr_stat_pin_enable.attr,
1433 +
1434 +       &dev_attr_reported_mode.attr,
1435 +       &dev_attr_mode.attr,
1436 +       &dev_attr_timer.attr,
1437 +
1438 +       &dev_attr_registers.attr,
1439 +
1440 +       &dev_attr_otg_status.attr,
1441 +       &dev_attr_charge_status.attr,
1442 +       &dev_attr_boost_status.attr,
1443 +       &dev_attr_fault_status.attr,
1444 +       NULL,
1445 +};
1446 +
1447 +static const struct attribute_group bq2415x_sysfs_attr_group = {
1448 +       .attrs = bq2415x_sysfs_attributes,
1449 +};
1450 +
1451 +static int bq2415x_sysfs_init(struct bq2415x_device *bq)
1452 +{
1453 +       return sysfs_create_group(&bq->charger.dev->kobj,
1454 +                       &bq2415x_sysfs_attr_group);
1455 +}
1456 +
1457 +static void bq2415x_sysfs_exit(struct bq2415x_device *bq)
1458 +{
1459 +       sysfs_remove_group(&bq->charger.dev->kobj, &bq2415x_sysfs_attr_group);
1460 +}
1461 +
1462 +/* main bq2415x probe function */
1463 +static int bq2415x_probe(struct i2c_client *client,
1464 +               const struct i2c_device_id *id)
1465 +{
1466 +       int ret;
1467 +       int num;
1468 +       char *name;
1469 +       struct bq2415x_device *bq;
1470 +
1471 +       if (!client->dev.platform_data) {
1472 +               dev_err(&client->dev, "platform data not set\n");
1473 +               return -ENODEV;
1474 +       }
1475 +
1476 +       /* Get new ID for the new device */
1477 +       ret = idr_pre_get(&bq2415x_id, GFP_KERNEL);
1478 +       if (ret == 0)
1479 +               return -ENOMEM;
1480 +
1481 +       mutex_lock(&bq2415x_id_mutex);
1482 +       ret = idr_get_new(&bq2415x_id, client, &num);
1483 +       mutex_unlock(&bq2415x_id_mutex);
1484 +
1485 +       if (ret < 0)
1486 +               return ret;
1487 +
1488 +       name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
1489 +       if (!name) {
1490 +               dev_err(&client->dev, "failed to allocate device name\n");
1491 +               ret = -ENOMEM;
1492 +               goto error_1;
1493 +       }
1494 +
1495 +       bq = kzalloc(sizeof(*bq), GFP_KERNEL);
1496 +       if (!bq) {
1497 +               dev_err(&client->dev, "failed to allocate device data\n");
1498 +               ret = -ENOMEM;
1499 +               goto error_2;
1500 +       }
1501 +
1502 +       i2c_set_clientdata(client, bq);
1503 +
1504 +       bq->id = num;
1505 +       bq->dev = &client->dev;
1506 +       bq->chip = id->driver_data;
1507 +       bq->name = name;
1508 +       bq->mode = BQ2415X_MODE_NONE;
1509 +       bq->reported_mode = BQ2415X_MODE_NONE;
1510 +       bq->autotimer = 0;
1511 +       bq->automode = 0;
1512 +
1513 +       memcpy(&bq->init_data, client->dev.platform_data,
1514 +                       sizeof(bq->init_data));
1515 +
1516 +       bq2415x_reset_chip(bq);
1517 +
1518 +       ret = bq2415x_power_supply_init(bq);
1519 +       if (ret) {
1520 +               dev_err(bq->dev, "failed to register power supply: %d\n", ret);
1521 +               goto error_3;
1522 +       }
1523 +
1524 +       ret = bq2415x_sysfs_init(bq);
1525 +       if (ret) {
1526 +               dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret);
1527 +               goto error_4;
1528 +       }
1529 +
1530 +       ret = bq2415x_set_defaults(bq);
1531 +       if (ret) {
1532 +               dev_err(bq->dev, "failed to set default values: %d\n", ret);
1533 +               goto error_5;
1534 +       }
1535 +
1536 +       if (bq->init_data.set_mode_hook) {
1537 +               if (bq->init_data.set_mode_hook(
1538 +                               bq2415x_hook_function, bq)) {
1539 +                       bq->automode = 1;
1540 +                       bq2415x_set_mode(bq, bq->reported_mode);
1541 +                       dev_info(bq->dev, "automode enabled\n");
1542 +               } else {
1543 +                       bq->automode = -1;
1544 +                       dev_info(bq->dev, "automode failed\n");
1545 +               }
1546 +       } else {
1547 +               bq->automode = -1;
1548 +               dev_info(bq->dev, "automode not supported\n");
1549 +       }
1550 +
1551 +       INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work);
1552 +       bq2415x_set_autotimer(bq, 1);
1553 +
1554 +       dev_info(bq->dev, "driver registered\n");
1555 +       return 0;
1556 +
1557 +error_5:
1558 +       bq2415x_sysfs_exit(bq);
1559 +error_4:
1560 +       bq2415x_power_supply_exit(bq);
1561 +error_3:
1562 +       kfree(bq);
1563 +error_2:
1564 +       kfree(name);
1565 +error_1:
1566 +       mutex_lock(&bq2415x_id_mutex);
1567 +       idr_remove(&bq2415x_id, num);
1568 +       mutex_unlock(&bq2415x_id_mutex);
1569 +
1570 +       return ret;
1571 +}
1572 +
1573 +/* main bq2415x remove function */
1574 +
1575 +static int bq2415x_remove(struct i2c_client *client)
1576 +{
1577 +       struct bq2415x_device *bq = i2c_get_clientdata(client);
1578 +
1579 +       if (bq->init_data.set_mode_hook)
1580 +               bq->init_data.set_mode_hook(NULL, NULL);
1581 +
1582 +       bq2415x_sysfs_exit(bq);
1583 +       bq2415x_power_supply_exit(bq);
1584 +
1585 +       bq2415x_reset_chip(bq);
1586 +
1587 +       mutex_lock(&bq2415x_id_mutex);
1588 +       idr_remove(&bq2415x_id, bq->id);
1589 +       mutex_unlock(&bq2415x_id_mutex);
1590 +
1591 +       dev_info(bq->dev, "driver unregistered\n");
1592 +
1593 +       kfree(bq->name);
1594 +       kfree(bq);
1595 +
1596 +       return 0;
1597 +}
1598 +
1599 +static const struct i2c_device_id bq2415x_i2c_id_table[] = {
1600 +       { "bq2415x", BQUNKNOWN },
1601 +       { "bq24150", BQ24150 },
1602 +       { "bq24150a", BQ24150A },
1603 +       { "bq24151", BQ24151 },
1604 +       { "bq24151a", BQ24151A },
1605 +       { "bq24152", BQ24152 },
1606 +       { "bq24153", BQ24153 },
1607 +       { "bq24153a", BQ24153A },
1608 +       { "bq24155", BQ24155 },
1609 +       { "bq24156", BQ24156 },
1610 +       { "bq24156a", BQ24156A },
1611 +       { "bq24158", BQ24158 },
1612 +       {},
1613 +};
1614 +MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table);
1615 +
1616 +static struct i2c_driver bq2415x_driver = {
1617 +       .driver = {
1618 +               .name = "bq2415x-charger",
1619 +       },
1620 +       .probe = bq2415x_probe,
1621 +       .remove = bq2415x_remove,
1622 +       .id_table = bq2415x_i2c_id_table,
1623 +};
1624 +
1625 +static int __init bq2415x_init(void)
1626 +{
1627 +       return i2c_add_driver(&bq2415x_driver);
1628 +}
1629 +module_init(bq2415x_init);
1630 +
1631 +static void __exit bq2415x_exit(void)
1632 +{
1633 +       i2c_del_driver(&bq2415x_driver);
1634 +}
1635 +module_exit(bq2415x_exit);
1636 +
1637 +MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
1638 +MODULE_DESCRIPTION("bq2415x charger driver");
1639 +MODULE_LICENSE("GPL");
1640 --- /dev/null
1641 +++ kernel-power/include/linux/power/bq2415x_charger.h
1642 @@ -0,0 +1,90 @@
1643 +/*
1644 +    bq2415x_charger.h - bq2415x charger driver
1645 +    Copyright (C) 2011-2012  Pali Rohár <pali.rohar@gmail.com>
1646 +
1647 +    This program is free software; you can redistribute it and/or modify
1648 +    it under the terms of the GNU General Public License as published by
1649 +    the Free Software Foundation; either version 2 of the License, or
1650 +    (at your option) any later version.
1651 +
1652 +    This program is distributed in the hope that it will be useful,
1653 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
1654 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1655 +    GNU General Public License for more details.
1656 +
1657 +    You should have received a copy of the GNU General Public License along
1658 +    with this program; if not, write to the Free Software Foundation, Inc.,
1659 +    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1660 +*/
1661 +
1662 +#ifndef BQ2415X_CHARGER_H
1663 +#define BQ2415X_CHARGER_H
1664 +
1665 +/*
1666 +  This is platform data for bq2415x chip. It contains default board voltages
1667 +  and currents which can be also later configured via sysfs. If value is -1
1668 +  then default chip value (specified in datasheet) will be used.
1669 +
1670 +  Value resistor_sense is needed for for configuring charge and termination
1671 +  current. It it is less or equal to zero, configuring charge and termination
1672 +  current will not be possible.
1673 +
1674 +  Function set_mode_hook is needed for automode (setting correct current limit
1675 +  when charger is connected/disconnected or setting boost mode). When is NULL,
1676 +  automode function is disabled. When is not NULL, it must have this prototype:
1677 +
1678 +    int (*set_mode_hook)(
1679 +      void (*hook)(enum bq2415x_mode mode, void *data),
1680 +      void *data)
1681 +
1682 +  hook is hook function (see below) and data is pointer to driver private data
1683 +
1684 +  bq2415x driver will call it as:
1685 +
1686 +    platform_data->set_mode_hook(bq2415x_hook_function, bq2415x_device);
1687 +
1688 +  Board/platform function set_mode_hook return non zero value when hook
1689 +  function was successful registered. Platform code should call that hook
1690 +  function (which get from pointer, with data) every time when charger was
1691 +  connected/disconnected or require to enable boost mode. bq2415x driver then
1692 +  will set correct current limit, enable/disable charger or boost mode.
1693 +
1694 +  Hook function has this prototype:
1695 +
1696 +    void hook(enum bq2415x_mode mode, void *data);
1697 +
1698 +  mode is bq2415x mode (charger or boost)
1699 +  data is pointer to driver private data (which get from set_charger_type_hook)
1700 +
1701 +  When bq driver is being unloaded, it call function:
1702 +
1703 +    platform_data->set_mode_hook(NULL, NULL);
1704 +
1705 +  (hook function and driver private data are NULL)
1706 +
1707 +  After that board/platform code must not call driver hook function! It is
1708 +  possible that pointer to hook function will not be valid and calling will
1709 +  cause undefined result.
1710 +
1711 +*/
1712 +
1713 +/* Supported modes with maximal current limit */
1714 +enum bq2415x_mode {
1715 +       BQ2415X_MODE_NONE,              /* unknown or no charger (100mA) */
1716 +       BQ2415X_MODE_HOST_CHARGER,      /* usb host/hub charger (500mA) */
1717 +       BQ2415X_MODE_DEDICATED_CHARGER, /* dedicated charger (unlimited) */
1718 +       BQ2415X_MODE_BOOST,             /* boost mode (charging disabled) */
1719 +};
1720 +
1721 +struct bq2415x_platform_data {
1722 +       int current_limit;              /* mA */
1723 +       int weak_battery_voltage;       /* mV */
1724 +       int battery_regulation_voltage; /* mV */
1725 +       int charge_current;             /* mA */
1726 +       int termination_current;        /* mA */
1727 +       int resistor_sense;             /* m ohm */
1728 +       int (*set_mode_hook)(void (*hook)(enum bq2415x_mode mode, void *data),
1729 +                            void *data);
1730 +};
1731 +
1732 +#endif