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