blob: 6c534dcbc19cb3c8639da09bef6b48281ac3cd44 [file] [log] [blame]
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001/*
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08002 * bq2415x charger driver
3 *
Pali Rohár32260302013-11-19 11:18:04 +01004 * Copyright (C) 2011-2013 Pali Rohár <pali.rohar@gmail.com>
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08005 *
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 *
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -080016 * Datasheets:
17 * http://www.ti.com/product/bq24150
18 * http://www.ti.com/product/bq24150a
19 * http://www.ti.com/product/bq24152
20 * http://www.ti.com/product/bq24153
21 * http://www.ti.com/product/bq24153a
22 * http://www.ti.com/product/bq24155
Anda-Maria Nicolae0e1392d2015-03-05 13:23:57 +020023 * http://www.ti.com/product/bq24157s
24 * http://www.ti.com/product/bq24158
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -080025 */
Pali Rohár0bc98cc2012-11-02 20:18:11 +010026
Pali Rohár0bc98cc2012-11-02 20:18:11 +010027#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/param.h>
30#include <linux/err.h>
31#include <linux/workqueue.h>
32#include <linux/sysfs.h>
33#include <linux/platform_device.h>
34#include <linux/power_supply.h>
35#include <linux/idr.h>
36#include <linux/i2c.h>
37#include <linux/slab.h>
38
39#include <linux/power/bq2415x_charger.h>
40
41/* timeout for resetting chip timer */
42#define BQ2415X_TIMER_TIMEOUT 10
43
44#define BQ2415X_REG_STATUS 0x00
45#define BQ2415X_REG_CONTROL 0x01
46#define BQ2415X_REG_VOLTAGE 0x02
47#define BQ2415X_REG_VENDER 0x03
48#define BQ2415X_REG_CURRENT 0x04
49
50/* reset state for all registers */
51#define BQ2415X_RESET_STATUS BIT(6)
52#define BQ2415X_RESET_CONTROL (BIT(4)|BIT(5))
53#define BQ2415X_RESET_VOLTAGE (BIT(1)|BIT(3))
54#define BQ2415X_RESET_CURRENT (BIT(0)|BIT(3)|BIT(7))
55
56/* status register */
57#define BQ2415X_BIT_TMR_RST 7
58#define BQ2415X_BIT_OTG 7
59#define BQ2415X_BIT_EN_STAT 6
60#define BQ2415X_MASK_STAT (BIT(4)|BIT(5))
61#define BQ2415X_SHIFT_STAT 4
62#define BQ2415X_BIT_BOOST 3
63#define BQ2415X_MASK_FAULT (BIT(0)|BIT(1)|BIT(2))
64#define BQ2415X_SHIFT_FAULT 0
65
66/* control register */
67#define BQ2415X_MASK_LIMIT (BIT(6)|BIT(7))
68#define BQ2415X_SHIFT_LIMIT 6
69#define BQ2415X_MASK_VLOWV (BIT(4)|BIT(5))
70#define BQ2415X_SHIFT_VLOWV 4
71#define BQ2415X_BIT_TE 3
72#define BQ2415X_BIT_CE 2
73#define BQ2415X_BIT_HZ_MODE 1
74#define BQ2415X_BIT_OPA_MODE 0
75
76/* voltage register */
77#define BQ2415X_MASK_VO (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
78#define BQ2415X_SHIFT_VO 2
79#define BQ2415X_BIT_OTG_PL 1
80#define BQ2415X_BIT_OTG_EN 0
81
82/* vender register */
83#define BQ2415X_MASK_VENDER (BIT(5)|BIT(6)|BIT(7))
84#define BQ2415X_SHIFT_VENDER 5
85#define BQ2415X_MASK_PN (BIT(3)|BIT(4))
86#define BQ2415X_SHIFT_PN 3
87#define BQ2415X_MASK_REVISION (BIT(0)|BIT(1)|BIT(2))
88#define BQ2415X_SHIFT_REVISION 0
89
90/* current register */
91#define BQ2415X_MASK_RESET BIT(7)
92#define BQ2415X_MASK_VI_CHRG (BIT(4)|BIT(5)|BIT(6))
93#define BQ2415X_SHIFT_VI_CHRG 4
94/* N/A BIT(3) */
95#define BQ2415X_MASK_VI_TERM (BIT(0)|BIT(1)|BIT(2))
96#define BQ2415X_SHIFT_VI_TERM 0
97
98
99enum bq2415x_command {
100 BQ2415X_TIMER_RESET,
101 BQ2415X_OTG_STATUS,
102 BQ2415X_STAT_PIN_STATUS,
103 BQ2415X_STAT_PIN_ENABLE,
104 BQ2415X_STAT_PIN_DISABLE,
105 BQ2415X_CHARGE_STATUS,
106 BQ2415X_BOOST_STATUS,
107 BQ2415X_FAULT_STATUS,
108
109 BQ2415X_CHARGE_TERMINATION_STATUS,
110 BQ2415X_CHARGE_TERMINATION_ENABLE,
111 BQ2415X_CHARGE_TERMINATION_DISABLE,
112 BQ2415X_CHARGER_STATUS,
113 BQ2415X_CHARGER_ENABLE,
114 BQ2415X_CHARGER_DISABLE,
115 BQ2415X_HIGH_IMPEDANCE_STATUS,
116 BQ2415X_HIGH_IMPEDANCE_ENABLE,
117 BQ2415X_HIGH_IMPEDANCE_DISABLE,
118 BQ2415X_BOOST_MODE_STATUS,
119 BQ2415X_BOOST_MODE_ENABLE,
120 BQ2415X_BOOST_MODE_DISABLE,
121
122 BQ2415X_OTG_LEVEL,
123 BQ2415X_OTG_ACTIVATE_HIGH,
124 BQ2415X_OTG_ACTIVATE_LOW,
125 BQ2415X_OTG_PIN_STATUS,
126 BQ2415X_OTG_PIN_ENABLE,
127 BQ2415X_OTG_PIN_DISABLE,
128
129 BQ2415X_VENDER_CODE,
130 BQ2415X_PART_NUMBER,
131 BQ2415X_REVISION,
132};
133
134enum bq2415x_chip {
135 BQUNKNOWN,
136 BQ24150,
137 BQ24150A,
138 BQ24151,
139 BQ24151A,
140 BQ24152,
141 BQ24153,
142 BQ24153A,
143 BQ24155,
144 BQ24156,
145 BQ24156A,
Anda-Maria Nicolae0e1392d2015-03-05 13:23:57 +0200146 BQ24157S,
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100147 BQ24158,
148};
149
150static char *bq2415x_chip_name[] = {
151 "unknown",
152 "bq24150",
153 "bq24150a",
154 "bq24151",
155 "bq24151a",
156 "bq24152",
157 "bq24153",
158 "bq24153a",
159 "bq24155",
160 "bq24156",
161 "bq24156a",
Anda-Maria Nicolae0e1392d2015-03-05 13:23:57 +0200162 "bq24157s",
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100163 "bq24158",
164};
165
166struct bq2415x_device {
167 struct device *dev;
168 struct bq2415x_platform_data init_data;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100169 struct power_supply *charger;
170 struct power_supply_desc charger_desc;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100171 struct delayed_work work;
Sebastian Reichelfaffd232013-11-24 17:49:30 +0100172 struct power_supply *notify_psy;
Pali Rohár32260302013-11-19 11:18:04 +0100173 struct notifier_block nb;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100174 enum bq2415x_mode reported_mode;/* mode reported by hook function */
Pavel Machek881f9852015-02-11 11:47:16 +0100175 enum bq2415x_mode mode; /* currently configured mode */
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100176 enum bq2415x_chip chip;
177 const char *timer_error;
178 char *model;
179 char *name;
180 int autotimer; /* 1 - if driver automatically reset timer, 0 - not */
181 int automode; /* 1 - enabled, 0 - disabled; -1 - not supported */
182 int id;
183};
184
185/* each registered chip must have unique id */
186static DEFINE_IDR(bq2415x_id);
187
188static DEFINE_MUTEX(bq2415x_id_mutex);
189static DEFINE_MUTEX(bq2415x_timer_mutex);
190static DEFINE_MUTEX(bq2415x_i2c_mutex);
191
192/**** i2c read functions ****/
193
194/* read value from register */
195static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg)
196{
197 struct i2c_client *client = to_i2c_client(bq->dev);
198 struct i2c_msg msg[2];
199 u8 val;
200 int ret;
201
202 if (!client->adapter)
203 return -ENODEV;
204
205 msg[0].addr = client->addr;
206 msg[0].flags = 0;
207 msg[0].buf = &reg;
208 msg[0].len = sizeof(reg);
209 msg[1].addr = client->addr;
210 msg[1].flags = I2C_M_RD;
211 msg[1].buf = &val;
212 msg[1].len = sizeof(val);
213
214 mutex_lock(&bq2415x_i2c_mutex);
215 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
216 mutex_unlock(&bq2415x_i2c_mutex);
217
218 if (ret < 0)
219 return ret;
220
221 return val;
222}
223
224/* read value from register, apply mask and right shift it */
225static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800226 u8 mask, u8 shift)
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100227{
228 int ret;
229
230 if (shift > 8)
231 return -EINVAL;
232
233 ret = bq2415x_i2c_read(bq, reg);
234 if (ret < 0)
235 return ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800236 return (ret & mask) >> shift;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100237}
238
239/* read value from register and return one specified bit */
240static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit)
241{
242 if (bit > 8)
243 return -EINVAL;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800244 return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit);
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100245}
246
247/**** i2c write functions ****/
248
249/* write value to register */
250static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val)
251{
252 struct i2c_client *client = to_i2c_client(bq->dev);
253 struct i2c_msg msg[1];
254 u8 data[2];
255 int ret;
256
257 data[0] = reg;
258 data[1] = val;
259
260 msg[0].addr = client->addr;
261 msg[0].flags = 0;
262 msg[0].buf = data;
263 msg[0].len = ARRAY_SIZE(data);
264
265 mutex_lock(&bq2415x_i2c_mutex);
266 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
267 mutex_unlock(&bq2415x_i2c_mutex);
268
269 /* i2c_transfer returns number of messages transferred */
270 if (ret < 0)
271 return ret;
272 else if (ret != 1)
273 return -EIO;
274
275 return 0;
276}
277
278/* read value from register, change it with mask left shifted and write back */
279static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800280 u8 mask, u8 shift)
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100281{
282 int ret;
283
284 if (shift > 8)
285 return -EINVAL;
286
287 ret = bq2415x_i2c_read(bq, reg);
288 if (ret < 0)
289 return ret;
290
291 ret &= ~mask;
292 ret |= val << shift;
293
294 return bq2415x_i2c_write(bq, reg, ret);
295}
296
297/* change only one bit in register */
298static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800299 bool val, u8 bit)
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100300{
301 if (bit > 8)
302 return -EINVAL;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800303 return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit);
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100304}
305
306/**** global functions ****/
307
308/* exec command function */
309static int bq2415x_exec_command(struct bq2415x_device *bq,
310 enum bq2415x_command command)
311{
312 int ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800313
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100314 switch (command) {
315 case BQ2415X_TIMER_RESET:
316 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS,
317 1, BQ2415X_BIT_TMR_RST);
318 case BQ2415X_OTG_STATUS:
319 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
320 BQ2415X_BIT_OTG);
321 case BQ2415X_STAT_PIN_STATUS:
322 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
323 BQ2415X_BIT_EN_STAT);
324 case BQ2415X_STAT_PIN_ENABLE:
325 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1,
326 BQ2415X_BIT_EN_STAT);
327 case BQ2415X_STAT_PIN_DISABLE:
328 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0,
329 BQ2415X_BIT_EN_STAT);
330 case BQ2415X_CHARGE_STATUS:
331 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
332 BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT);
333 case BQ2415X_BOOST_STATUS:
334 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
335 BQ2415X_BIT_BOOST);
336 case BQ2415X_FAULT_STATUS:
337 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
338 BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT);
339
340 case BQ2415X_CHARGE_TERMINATION_STATUS:
341 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
342 BQ2415X_BIT_TE);
343 case BQ2415X_CHARGE_TERMINATION_ENABLE:
344 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
345 1, BQ2415X_BIT_TE);
346 case BQ2415X_CHARGE_TERMINATION_DISABLE:
347 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
348 0, BQ2415X_BIT_TE);
349 case BQ2415X_CHARGER_STATUS:
350 ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
351 BQ2415X_BIT_CE);
352 if (ret < 0)
353 return ret;
Anda-Maria Nicolae42d06312015-03-05 13:23:56 +0200354 return ret > 0 ? 0 : 1;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100355 case BQ2415X_CHARGER_ENABLE:
356 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
357 0, BQ2415X_BIT_CE);
358 case BQ2415X_CHARGER_DISABLE:
359 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
360 1, BQ2415X_BIT_CE);
361 case BQ2415X_HIGH_IMPEDANCE_STATUS:
362 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
363 BQ2415X_BIT_HZ_MODE);
364 case BQ2415X_HIGH_IMPEDANCE_ENABLE:
365 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
366 1, BQ2415X_BIT_HZ_MODE);
367 case BQ2415X_HIGH_IMPEDANCE_DISABLE:
368 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
369 0, BQ2415X_BIT_HZ_MODE);
370 case BQ2415X_BOOST_MODE_STATUS:
371 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
372 BQ2415X_BIT_OPA_MODE);
373 case BQ2415X_BOOST_MODE_ENABLE:
374 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
375 1, BQ2415X_BIT_OPA_MODE);
376 case BQ2415X_BOOST_MODE_DISABLE:
377 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
378 0, BQ2415X_BIT_OPA_MODE);
379
380 case BQ2415X_OTG_LEVEL:
381 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
382 BQ2415X_BIT_OTG_PL);
383 case BQ2415X_OTG_ACTIVATE_HIGH:
384 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
385 1, BQ2415X_BIT_OTG_PL);
386 case BQ2415X_OTG_ACTIVATE_LOW:
387 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
388 0, BQ2415X_BIT_OTG_PL);
389 case BQ2415X_OTG_PIN_STATUS:
390 return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
391 BQ2415X_BIT_OTG_EN);
392 case BQ2415X_OTG_PIN_ENABLE:
393 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
394 1, BQ2415X_BIT_OTG_EN);
395 case BQ2415X_OTG_PIN_DISABLE:
396 return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
397 0, BQ2415X_BIT_OTG_EN);
398
399 case BQ2415X_VENDER_CODE:
400 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
401 BQ2415X_MASK_VENDER, BQ2415X_SHIFT_VENDER);
402 case BQ2415X_PART_NUMBER:
403 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
404 BQ2415X_MASK_PN, BQ2415X_SHIFT_PN);
405 case BQ2415X_REVISION:
406 return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
407 BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION);
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100408 }
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800409 return -EINVAL;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100410}
411
412/* detect chip type */
413static enum bq2415x_chip bq2415x_detect_chip(struct bq2415x_device *bq)
414{
415 struct i2c_client *client = to_i2c_client(bq->dev);
416 int ret = bq2415x_exec_command(bq, BQ2415X_PART_NUMBER);
417
418 if (ret < 0)
419 return ret;
420
421 switch (client->addr) {
422 case 0x6b:
423 switch (ret) {
424 case 0:
425 if (bq->chip == BQ24151A)
426 return bq->chip;
Anda-Maria Nicolae42d06312015-03-05 13:23:56 +0200427 return BQ24151;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100428 case 1:
429 if (bq->chip == BQ24150A ||
430 bq->chip == BQ24152 ||
431 bq->chip == BQ24155)
432 return bq->chip;
Anda-Maria Nicolae42d06312015-03-05 13:23:56 +0200433 return BQ24150;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100434 case 2:
435 if (bq->chip == BQ24153A)
436 return bq->chip;
Anda-Maria Nicolae42d06312015-03-05 13:23:56 +0200437 return BQ24153;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100438 default:
439 return BQUNKNOWN;
440 }
441 break;
442
443 case 0x6a:
444 switch (ret) {
445 case 0:
446 if (bq->chip == BQ24156A)
447 return bq->chip;
Anda-Maria Nicolae42d06312015-03-05 13:23:56 +0200448 return BQ24156;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100449 case 2:
Anda-Maria Nicolae0e1392d2015-03-05 13:23:57 +0200450 if (bq->chip == BQ24157S)
451 return bq->chip;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100452 return BQ24158;
453 default:
454 return BQUNKNOWN;
455 }
456 break;
457 }
458
459 return BQUNKNOWN;
460}
461
462/* detect chip revision */
463static int bq2415x_detect_revision(struct bq2415x_device *bq)
464{
465 int ret = bq2415x_exec_command(bq, BQ2415X_REVISION);
466 int chip = bq2415x_detect_chip(bq);
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800467
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100468 if (ret < 0 || chip < 0)
469 return -1;
470
471 switch (chip) {
472 case BQ24150:
473 case BQ24150A:
474 case BQ24151:
475 case BQ24151A:
476 case BQ24152:
477 if (ret >= 0 && ret <= 3)
478 return ret;
Anda-Maria Nicolae42d06312015-03-05 13:23:56 +0200479 return -1;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100480 case BQ24153:
481 case BQ24153A:
482 case BQ24156:
483 case BQ24156A:
Anda-Maria Nicolae0e1392d2015-03-05 13:23:57 +0200484 case BQ24157S:
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100485 case BQ24158:
486 if (ret == 3)
487 return 0;
488 else if (ret == 1)
489 return 1;
Anda-Maria Nicolae42d06312015-03-05 13:23:56 +0200490 return -1;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100491 case BQ24155:
492 if (ret == 3)
493 return 3;
Anda-Maria Nicolae42d06312015-03-05 13:23:56 +0200494 return -1;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100495 case BQUNKNOWN:
496 return -1;
497 }
498
499 return -1;
500}
501
502/* return chip vender code */
503static int bq2415x_get_vender_code(struct bq2415x_device *bq)
504{
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800505 int ret;
506
507 ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100508 if (ret < 0)
509 return 0;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800510
511 /* convert to binary */
512 return (ret & 0x1) +
513 ((ret >> 1) & 0x1) * 10 +
514 ((ret >> 2) & 0x1) * 100;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100515}
516
517/* reset all chip registers to default state */
518static void bq2415x_reset_chip(struct bq2415x_device *bq)
519{
520 bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT);
521 bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE);
522 bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL);
523 bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS);
524 bq->timer_error = NULL;
525}
526
527/**** properties functions ****/
528
529/* set current limit in mA */
530static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA)
531{
532 int val;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800533
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100534 if (mA <= 100)
535 val = 0;
536 else if (mA <= 500)
537 val = 1;
538 else if (mA <= 800)
539 val = 2;
540 else
541 val = 3;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800542
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100543 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
544 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
545}
546
547/* get current limit in mA */
548static int bq2415x_get_current_limit(struct bq2415x_device *bq)
549{
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800550 int ret;
551
552 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100553 BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
554 if (ret < 0)
555 return ret;
556 else if (ret == 0)
557 return 100;
558 else if (ret == 1)
559 return 500;
560 else if (ret == 2)
561 return 800;
562 else if (ret == 3)
563 return 1800;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800564 return -EINVAL;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100565}
566
567/* set weak battery voltage in mV */
568static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
569{
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100570 int val;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800571
572 /* round to 100mV */
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100573 if (mV <= 3400 + 50)
574 val = 0;
575 else if (mV <= 3500 + 50)
576 val = 1;
577 else if (mV <= 3600 + 50)
578 val = 2;
579 else
580 val = 3;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800581
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100582 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
583 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
584}
585
586/* get weak battery voltage in mV */
587static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq)
588{
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800589 int ret;
590
591 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100592 BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
593 if (ret < 0)
594 return ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800595 return 100 * (34 + ret);
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100596}
597
598/* set battery regulation voltage in mV */
599static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800600 int mV)
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100601{
602 int val = (mV/10 - 350) / 2;
603
Alexandre Belloni3ed5cd72013-10-16 16:08:31 +0200604 /*
605 * According to datasheet, maximum battery regulation voltage is
606 * 4440mV which is b101111 = 47.
607 */
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100608 if (val < 0)
609 val = 0;
Alexandre Belloni3ed5cd72013-10-16 16:08:31 +0200610 else if (val > 47)
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100611 return -EINVAL;
612
613 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val,
614 BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
615}
616
617/* get battery regulation voltage in mV */
618static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq)
619{
620 int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE,
621 BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800622
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100623 if (ret < 0)
624 return ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800625 return 10 * (350 + 2*ret);
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100626}
627
628/* set charge current in mA (platform data must provide resistor sense) */
629static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA)
630{
631 int val;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800632
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100633 if (bq->init_data.resistor_sense <= 0)
634 return -ENOSYS;
635
636 val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100637 if (val < 0)
638 val = 0;
639 else if (val > 7)
640 val = 7;
641
642 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
643 BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET,
644 BQ2415X_SHIFT_VI_CHRG);
645}
646
647/* get charge current in mA (platform data must provide resistor sense) */
648static int bq2415x_get_charge_current(struct bq2415x_device *bq)
649{
650 int ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800651
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100652 if (bq->init_data.resistor_sense <= 0)
653 return -ENOSYS;
654
655 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
656 BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG);
657 if (ret < 0)
658 return ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800659 return (37400 + 6800*ret) / bq->init_data.resistor_sense;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100660}
661
662/* set termination current in mA (platform data must provide resistor sense) */
663static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA)
664{
665 int val;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800666
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100667 if (bq->init_data.resistor_sense <= 0)
668 return -ENOSYS;
669
670 val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100671 if (val < 0)
672 val = 0;
673 else if (val > 7)
674 val = 7;
675
676 return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
677 BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET,
678 BQ2415X_SHIFT_VI_TERM);
679}
680
681/* get termination current in mA (platform data must provide resistor sense) */
682static int bq2415x_get_termination_current(struct bq2415x_device *bq)
683{
684 int ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800685
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100686 if (bq->init_data.resistor_sense <= 0)
687 return -ENOSYS;
688
689 ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
690 BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM);
691 if (ret < 0)
692 return ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800693 return (3400 + 3400*ret) / bq->init_data.resistor_sense;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100694}
695
696/* set default value of property */
697#define bq2415x_set_default_value(bq, prop) \
698 do { \
699 int ret = 0; \
700 if (bq->init_data.prop != -1) \
701 ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
702 if (ret < 0) \
703 return ret; \
704 } while (0)
705
706/* set default values of all properties */
707static int bq2415x_set_defaults(struct bq2415x_device *bq)
708{
709 bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
710 bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
711 bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE);
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800712
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100713 bq2415x_set_default_value(bq, current_limit);
714 bq2415x_set_default_value(bq, weak_battery_voltage);
715 bq2415x_set_default_value(bq, battery_regulation_voltage);
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800716
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100717 if (bq->init_data.resistor_sense > 0) {
718 bq2415x_set_default_value(bq, charge_current);
719 bq2415x_set_default_value(bq, termination_current);
720 bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE);
721 }
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800722
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100723 bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
724 return 0;
725}
726
727/**** charger mode functions ****/
728
729/* set charger mode */
730static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode)
731{
732 int ret = 0;
733 int charger = 0;
734 int boost = 0;
735
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100736 if (mode == BQ2415X_MODE_BOOST)
737 boost = 1;
Pali Rohárac6324e2013-02-10 18:32:18 +0100738 else if (mode != BQ2415X_MODE_OFF)
739 charger = 1;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100740
741 if (!charger)
742 ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
743
744 if (!boost)
745 ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
746
747 if (ret < 0)
748 return ret;
749
750 switch (mode) {
Pali Rohárac6324e2013-02-10 18:32:18 +0100751 case BQ2415X_MODE_OFF:
752 dev_dbg(bq->dev, "changing mode to: Offline\n");
753 ret = bq2415x_set_current_limit(bq, 100);
754 break;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100755 case BQ2415X_MODE_NONE:
756 dev_dbg(bq->dev, "changing mode to: N/A\n");
757 ret = bq2415x_set_current_limit(bq, 100);
758 break;
759 case BQ2415X_MODE_HOST_CHARGER:
760 dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n");
761 ret = bq2415x_set_current_limit(bq, 500);
762 break;
763 case BQ2415X_MODE_DEDICATED_CHARGER:
764 dev_dbg(bq->dev, "changing mode to: Dedicated charger\n");
765 ret = bq2415x_set_current_limit(bq, 1800);
766 break;
767 case BQ2415X_MODE_BOOST: /* Boost mode */
768 dev_dbg(bq->dev, "changing mode to: Boost\n");
769 ret = bq2415x_set_current_limit(bq, 100);
770 break;
771 }
772
773 if (ret < 0)
774 return ret;
775
776 if (charger)
777 ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
778 else if (boost)
779 ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE);
780
781 if (ret < 0)
782 return ret;
783
784 bq2415x_set_default_value(bq, weak_battery_voltage);
785 bq2415x_set_default_value(bq, battery_regulation_voltage);
786
787 bq->mode = mode;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100788 sysfs_notify(&bq->charger->dev.kobj, NULL, "mode");
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100789
790 return 0;
791
792}
793
Pali Rohár32260302013-11-19 11:18:04 +0100794static int bq2415x_notifier_call(struct notifier_block *nb,
795 unsigned long val, void *v)
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100796{
Pali Rohár32260302013-11-19 11:18:04 +0100797 struct bq2415x_device *bq =
798 container_of(nb, struct bq2415x_device, nb);
799 struct power_supply *psy = v;
800 enum bq2415x_mode mode;
801 union power_supply_propval prop;
802 int ret;
803 int mA;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100804
Pali Rohár32260302013-11-19 11:18:04 +0100805 if (val != PSY_EVENT_PROP_CHANGED)
806 return NOTIFY_OK;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100807
Sebastian Reichelfaffd232013-11-24 17:49:30 +0100808 if (psy != bq->notify_psy)
Pali Rohár32260302013-11-19 11:18:04 +0100809 return NOTIFY_OK;
810
811 dev_dbg(bq->dev, "notifier call was called\n");
812
Krzysztof Kozlowskid7bdffb2015-03-12 08:44:09 +0100813 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX,
814 &prop);
Pali Rohár32260302013-11-19 11:18:04 +0100815 if (ret != 0)
816 return NOTIFY_OK;
817
818 mA = prop.intval;
819
820 if (mA == 0)
821 mode = BQ2415X_MODE_OFF;
822 else if (mA < 500)
823 mode = BQ2415X_MODE_NONE;
824 else if (mA < 1800)
825 mode = BQ2415X_MODE_HOST_CHARGER;
826 else
827 mode = BQ2415X_MODE_DEDICATED_CHARGER;
828
829 if (bq->reported_mode == mode)
830 return NOTIFY_OK;
831
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100832 bq->reported_mode = mode;
833
834 /* if automode is not enabled do not tell about reported_mode */
835 if (bq->automode < 1)
Pali Rohár32260302013-11-19 11:18:04 +0100836 return NOTIFY_OK;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100837
Sebastian Reichel3c018502014-04-07 13:14:04 +0200838 schedule_delayed_work(&bq->work, 0);
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100839
Pali Rohár32260302013-11-19 11:18:04 +0100840 return NOTIFY_OK;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100841}
842
843/**** timer functions ****/
844
845/* enable/disable auto resetting chip timer */
846static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
847{
848 mutex_lock(&bq2415x_timer_mutex);
849
850 if (bq->autotimer == state) {
851 mutex_unlock(&bq2415x_timer_mutex);
852 return;
853 }
854
855 bq->autotimer = state;
856
857 if (state) {
858 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
859 bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
860 bq->timer_error = NULL;
861 } else {
862 cancel_delayed_work_sync(&bq->work);
863 }
864
865 mutex_unlock(&bq2415x_timer_mutex);
866}
867
868/* called by bq2415x_timer_work on timer error */
869static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
870{
871 bq->timer_error = msg;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100872 sysfs_notify(&bq->charger->dev.kobj, NULL, "timer");
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100873 dev_err(bq->dev, "%s\n", msg);
874 if (bq->automode > 0)
875 bq->automode = 0;
Pali Rohárac6324e2013-02-10 18:32:18 +0100876 bq2415x_set_mode(bq, BQ2415X_MODE_OFF);
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100877 bq2415x_set_autotimer(bq, 0);
878}
879
880/* delayed work function for auto resetting chip timer */
881static void bq2415x_timer_work(struct work_struct *work)
882{
883 struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800884 work.work);
885 int ret;
886 int error;
887 int boost;
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100888
Sebastian Reichel3c018502014-04-07 13:14:04 +0200889 if (bq->automode > 0 && (bq->reported_mode != bq->mode)) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100890 sysfs_notify(&bq->charger->dev.kobj, NULL, "reported_mode");
Sebastian Reichel3c018502014-04-07 13:14:04 +0200891 bq2415x_set_mode(bq, bq->reported_mode);
892 }
893
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100894 if (!bq->autotimer)
895 return;
896
897 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
898 if (ret < 0) {
899 bq2415x_timer_error(bq, "Resetting timer failed");
900 return;
901 }
902
903 boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
904 if (boost < 0) {
905 bq2415x_timer_error(bq, "Unknown error");
906 return;
907 }
908
909 error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
910 if (error < 0) {
911 bq2415x_timer_error(bq, "Unknown error");
912 return;
913 }
914
915 if (boost) {
916 switch (error) {
917 /* Non fatal errors, chip is OK */
918 case 0: /* No error */
919 break;
920 case 6: /* Timer expired */
921 dev_err(bq->dev, "Timer expired\n");
922 break;
923 case 3: /* Battery voltage too low */
924 dev_err(bq->dev, "Battery voltage to low\n");
925 break;
926
927 /* Fatal errors, disable and reset chip */
928 case 1: /* Overvoltage protection (chip fried) */
929 bq2415x_timer_error(bq,
930 "Overvoltage protection (chip fried)");
931 return;
932 case 2: /* Overload */
933 bq2415x_timer_error(bq, "Overload");
934 return;
935 case 4: /* Battery overvoltage protection */
936 bq2415x_timer_error(bq,
937 "Battery overvoltage protection");
938 return;
939 case 5: /* Thermal shutdown (too hot) */
940 bq2415x_timer_error(bq,
941 "Thermal shutdown (too hot)");
942 return;
943 case 7: /* N/A */
944 bq2415x_timer_error(bq, "Unknown error");
945 return;
946 }
947 } else {
948 switch (error) {
949 /* Non fatal errors, chip is OK */
950 case 0: /* No error */
951 break;
952 case 2: /* Sleep mode */
953 dev_err(bq->dev, "Sleep mode\n");
954 break;
955 case 3: /* Poor input source */
956 dev_err(bq->dev, "Poor input source\n");
957 break;
958 case 6: /* Timer expired */
959 dev_err(bq->dev, "Timer expired\n");
960 break;
961 case 7: /* No battery */
962 dev_err(bq->dev, "No battery\n");
963 break;
964
965 /* Fatal errors, disable and reset chip */
966 case 1: /* Overvoltage protection (chip fried) */
967 bq2415x_timer_error(bq,
968 "Overvoltage protection (chip fried)");
969 return;
970 case 4: /* Battery overvoltage protection */
971 bq2415x_timer_error(bq,
972 "Battery overvoltage protection");
973 return;
974 case 5: /* Thermal shutdown (too hot) */
975 bq2415x_timer_error(bq,
976 "Thermal shutdown (too hot)");
977 return;
978 }
979 }
980
981 schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
982}
983
984/**** power supply interface code ****/
985
986static enum power_supply_property bq2415x_power_supply_props[] = {
987 /* TODO: maybe add more power supply properties */
988 POWER_SUPPLY_PROP_STATUS,
989 POWER_SUPPLY_PROP_MODEL_NAME,
990};
991
992static int bq2415x_power_supply_get_property(struct power_supply *psy,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -0800993 enum power_supply_property psp,
994 union power_supply_propval *val)
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100995{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100996 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +0100997 int ret;
998
999 switch (psp) {
1000 case POWER_SUPPLY_PROP_STATUS:
1001 ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
1002 if (ret < 0)
1003 return ret;
1004 else if (ret == 0) /* Ready */
1005 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1006 else if (ret == 1) /* Charge in progress */
1007 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1008 else if (ret == 2) /* Charge done */
1009 val->intval = POWER_SUPPLY_STATUS_FULL;
1010 else
1011 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
1012 break;
1013 case POWER_SUPPLY_PROP_MODEL_NAME:
1014 val->strval = bq->model;
1015 break;
1016 default:
1017 return -EINVAL;
1018 }
1019 return 0;
1020}
1021
1022static int bq2415x_power_supply_init(struct bq2415x_device *bq)
1023{
1024 int ret;
1025 int chip;
1026 char revstr[8];
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001027 struct power_supply_config psy_cfg = { .drv_data = bq, };
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001028
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001029 bq->charger_desc.name = bq->name;
1030 bq->charger_desc.type = POWER_SUPPLY_TYPE_USB;
1031 bq->charger_desc.properties = bq2415x_power_supply_props;
1032 bq->charger_desc.num_properties =
1033 ARRAY_SIZE(bq2415x_power_supply_props);
1034 bq->charger_desc.get_property = bq2415x_power_supply_get_property;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001035
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
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001057 bq->charger = power_supply_register(bq->dev, &bq->charger_desc,
1058 &psy_cfg);
1059 if (IS_ERR(bq->charger)) {
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001060 kfree(bq->model);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001061 return PTR_ERR(bq->charger);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001062 }
1063
1064 return 0;
1065}
1066
1067static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
1068{
1069 bq->autotimer = 0;
1070 if (bq->automode > 0)
1071 bq->automode = 0;
1072 cancel_delayed_work_sync(&bq->work);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001073 power_supply_unregister(bq->charger);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001074 kfree(bq->model);
1075}
1076
1077/**** additional sysfs entries for power supply interface ****/
1078
1079/* show *_status entries */
1080static ssize_t bq2415x_sysfs_show_status(struct device *dev,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001081 struct device_attribute *attr,
1082 char *buf)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001083{
1084 struct power_supply *psy = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001085 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001086 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;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001103 return sprintf(buf, "%d\n", ret);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001104}
1105
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001106/*
1107 * set timer entry:
1108 * auto - enable auto mode
1109 * off - disable auto mode
1110 * (other values) - reset chip timer
1111 */
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001112static ssize_t bq2415x_sysfs_set_timer(struct device *dev,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001113 struct device_attribute *attr,
1114 const char *buf,
1115 size_t count)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001116{
1117 struct power_supply *psy = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001118 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001119 int ret = 0;
1120
1121 if (strncmp(buf, "auto", 4) == 0)
1122 bq2415x_set_autotimer(bq, 1);
1123 else if (strncmp(buf, "off", 3) == 0)
1124 bq2415x_set_autotimer(bq, 0);
1125 else
1126 ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
1127
1128 if (ret < 0)
1129 return ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001130 return count;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001131}
1132
1133/* show timer entry (auto or off) */
1134static ssize_t bq2415x_sysfs_show_timer(struct device *dev,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001135 struct device_attribute *attr,
1136 char *buf)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001137{
1138 struct power_supply *psy = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001139 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001140
1141 if (bq->timer_error)
1142 return sprintf(buf, "%s\n", bq->timer_error);
1143
1144 if (bq->autotimer)
1145 return sprintf(buf, "auto\n");
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001146 return sprintf(buf, "off\n");
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001147}
1148
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001149/*
1150 * set mode entry:
1151 * auto - if automode is supported, enable it and set mode to reported
1152 * none - disable charger and boost mode
1153 * host - charging mode for host/hub chargers (current limit 500mA)
1154 * dedicated - charging mode for dedicated chargers (unlimited current limit)
1155 * boost - disable charger and enable boost mode
1156 */
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001157static ssize_t bq2415x_sysfs_set_mode(struct device *dev,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001158 struct device_attribute *attr,
1159 const char *buf,
1160 size_t count)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001161{
1162 struct power_supply *psy = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001163 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001164 enum bq2415x_mode mode;
1165 int ret = 0;
1166
1167 if (strncmp(buf, "auto", 4) == 0) {
1168 if (bq->automode < 0)
1169 return -ENOSYS;
1170 bq->automode = 1;
1171 mode = bq->reported_mode;
Pali Rohárac6324e2013-02-10 18:32:18 +01001172 } else if (strncmp(buf, "off", 3) == 0) {
1173 if (bq->automode > 0)
1174 bq->automode = 0;
1175 mode = BQ2415X_MODE_OFF;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001176 } else if (strncmp(buf, "none", 4) == 0) {
1177 if (bq->automode > 0)
1178 bq->automode = 0;
1179 mode = BQ2415X_MODE_NONE;
1180 } else if (strncmp(buf, "host", 4) == 0) {
1181 if (bq->automode > 0)
1182 bq->automode = 0;
1183 mode = BQ2415X_MODE_HOST_CHARGER;
1184 } else if (strncmp(buf, "dedicated", 9) == 0) {
1185 if (bq->automode > 0)
1186 bq->automode = 0;
1187 mode = BQ2415X_MODE_DEDICATED_CHARGER;
1188 } else if (strncmp(buf, "boost", 5) == 0) {
1189 if (bq->automode > 0)
1190 bq->automode = 0;
1191 mode = BQ2415X_MODE_BOOST;
1192 } else if (strncmp(buf, "reset", 5) == 0) {
1193 bq2415x_reset_chip(bq);
1194 bq2415x_set_defaults(bq);
1195 if (bq->automode <= 0)
1196 return count;
1197 bq->automode = 1;
1198 mode = bq->reported_mode;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001199 } else {
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001200 return -EINVAL;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001201 }
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001202
1203 ret = bq2415x_set_mode(bq, mode);
1204 if (ret < 0)
1205 return ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001206 return count;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001207}
1208
1209/* show mode entry (auto, none, host, dedicated or boost) */
1210static ssize_t bq2415x_sysfs_show_mode(struct device *dev,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001211 struct device_attribute *attr,
1212 char *buf)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001213{
1214 struct power_supply *psy = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001215 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001216 ssize_t ret = 0;
1217
1218 if (bq->automode > 0)
1219 ret += sprintf(buf+ret, "auto (");
1220
1221 switch (bq->mode) {
Pali Rohárac6324e2013-02-10 18:32:18 +01001222 case BQ2415X_MODE_OFF:
1223 ret += sprintf(buf+ret, "off");
1224 break;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001225 case BQ2415X_MODE_NONE:
1226 ret += sprintf(buf+ret, "none");
1227 break;
1228 case BQ2415X_MODE_HOST_CHARGER:
1229 ret += sprintf(buf+ret, "host");
1230 break;
1231 case BQ2415X_MODE_DEDICATED_CHARGER:
1232 ret += sprintf(buf+ret, "dedicated");
1233 break;
1234 case BQ2415X_MODE_BOOST:
1235 ret += sprintf(buf+ret, "boost");
1236 break;
1237 }
1238
1239 if (bq->automode > 0)
1240 ret += sprintf(buf+ret, ")");
1241
1242 ret += sprintf(buf+ret, "\n");
1243 return ret;
1244}
1245
1246/* show reported_mode entry (none, host, dedicated or boost) */
1247static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001248 struct device_attribute *attr,
1249 char *buf)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001250{
1251 struct power_supply *psy = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001252 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001253
1254 if (bq->automode < 0)
1255 return -EINVAL;
1256
1257 switch (bq->reported_mode) {
Pali Rohárac6324e2013-02-10 18:32:18 +01001258 case BQ2415X_MODE_OFF:
1259 return sprintf(buf, "off\n");
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001260 case BQ2415X_MODE_NONE:
1261 return sprintf(buf, "none\n");
1262 case BQ2415X_MODE_HOST_CHARGER:
1263 return sprintf(buf, "host\n");
1264 case BQ2415X_MODE_DEDICATED_CHARGER:
1265 return sprintf(buf, "dedicated\n");
1266 case BQ2415X_MODE_BOOST:
1267 return sprintf(buf, "boost\n");
1268 }
1269
1270 return -EINVAL;
1271}
1272
1273/* directly set raw value to chip register, format: 'register value' */
1274static ssize_t bq2415x_sysfs_set_registers(struct device *dev,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001275 struct device_attribute *attr,
1276 const char *buf,
1277 size_t count)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001278{
1279 struct power_supply *psy = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001280 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001281 ssize_t ret = 0;
1282 unsigned int reg;
1283 unsigned int val;
1284
1285 if (sscanf(buf, "%x %x", &reg, &val) != 2)
1286 return -EINVAL;
1287
1288 if (reg > 4 || val > 255)
1289 return -EINVAL;
1290
1291 ret = bq2415x_i2c_write(bq, reg, val);
1292 if (ret < 0)
1293 return ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001294 return count;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001295}
1296
1297/* print value of chip register, format: 'register=value' */
1298static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001299 u8 reg,
1300 char *buf)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001301{
1302 int ret = bq2415x_i2c_read(bq, reg);
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001303
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001304 if (ret < 0)
1305 return sprintf(buf, "%#.2x=error %d\n", reg, ret);
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001306 return sprintf(buf, "%#.2x=%#.2x\n", reg, ret);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001307}
1308
1309/* show all raw values of chip register, format per line: 'register=value' */
1310static ssize_t bq2415x_sysfs_show_registers(struct device *dev,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001311 struct device_attribute *attr,
1312 char *buf)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001313{
1314 struct power_supply *psy = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001315 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001316 ssize_t ret = 0;
1317
1318 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret);
1319 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret);
1320 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret);
1321 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret);
1322 ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret);
1323 return ret;
1324}
1325
1326/* set current and voltage limit entries (in mA or mV) */
1327static ssize_t bq2415x_sysfs_set_limit(struct device *dev,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001328 struct device_attribute *attr,
1329 const char *buf,
1330 size_t count)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001331{
1332 struct power_supply *psy = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001333 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001334 long val;
1335 int ret;
1336
1337 if (kstrtol(buf, 10, &val) < 0)
1338 return -EINVAL;
1339
1340 if (strcmp(attr->attr.name, "current_limit") == 0)
1341 ret = bq2415x_set_current_limit(bq, val);
1342 else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1343 ret = bq2415x_set_weak_battery_voltage(bq, val);
1344 else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1345 ret = bq2415x_set_battery_regulation_voltage(bq, val);
1346 else if (strcmp(attr->attr.name, "charge_current") == 0)
1347 ret = bq2415x_set_charge_current(bq, val);
1348 else if (strcmp(attr->attr.name, "termination_current") == 0)
1349 ret = bq2415x_set_termination_current(bq, val);
1350 else
1351 return -EINVAL;
1352
1353 if (ret < 0)
1354 return ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001355 return count;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001356}
1357
1358/* show current and voltage limit entries (in mA or mV) */
1359static ssize_t bq2415x_sysfs_show_limit(struct device *dev,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001360 struct device_attribute *attr,
1361 char *buf)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001362{
1363 struct power_supply *psy = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001364 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001365 int ret;
1366
1367 if (strcmp(attr->attr.name, "current_limit") == 0)
1368 ret = bq2415x_get_current_limit(bq);
1369 else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
1370 ret = bq2415x_get_weak_battery_voltage(bq);
1371 else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
1372 ret = bq2415x_get_battery_regulation_voltage(bq);
1373 else if (strcmp(attr->attr.name, "charge_current") == 0)
1374 ret = bq2415x_get_charge_current(bq);
1375 else if (strcmp(attr->attr.name, "termination_current") == 0)
1376 ret = bq2415x_get_termination_current(bq);
1377 else
1378 return -EINVAL;
1379
1380 if (ret < 0)
1381 return ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001382 return sprintf(buf, "%d\n", ret);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001383}
1384
1385/* set *_enable entries */
1386static ssize_t bq2415x_sysfs_set_enable(struct device *dev,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001387 struct device_attribute *attr,
1388 const char *buf,
1389 size_t count)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001390{
1391 struct power_supply *psy = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001392 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001393 enum bq2415x_command command;
1394 long val;
1395 int ret;
1396
1397 if (kstrtol(buf, 10, &val) < 0)
1398 return -EINVAL;
1399
1400 if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1401 command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE :
1402 BQ2415X_CHARGE_TERMINATION_DISABLE;
1403 else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1404 command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE :
1405 BQ2415X_HIGH_IMPEDANCE_DISABLE;
1406 else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1407 command = val ? BQ2415X_OTG_PIN_ENABLE :
1408 BQ2415X_OTG_PIN_DISABLE;
1409 else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1410 command = val ? BQ2415X_STAT_PIN_ENABLE :
1411 BQ2415X_STAT_PIN_DISABLE;
1412 else
1413 return -EINVAL;
1414
1415 ret = bq2415x_exec_command(bq, command);
1416 if (ret < 0)
1417 return ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001418 return count;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001419}
1420
1421/* show *_enable entries */
1422static ssize_t bq2415x_sysfs_show_enable(struct device *dev,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001423 struct device_attribute *attr,
1424 char *buf)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001425{
1426 struct power_supply *psy = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001427 struct bq2415x_device *bq = power_supply_get_drvdata(psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001428 enum bq2415x_command command;
1429 int ret;
1430
1431 if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
1432 command = BQ2415X_CHARGE_TERMINATION_STATUS;
1433 else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
1434 command = BQ2415X_HIGH_IMPEDANCE_STATUS;
1435 else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
1436 command = BQ2415X_OTG_PIN_STATUS;
1437 else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
1438 command = BQ2415X_STAT_PIN_STATUS;
1439 else
1440 return -EINVAL;
1441
1442 ret = bq2415x_exec_command(bq, command);
1443 if (ret < 0)
1444 return ret;
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001445 return sprintf(buf, "%d\n", ret);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001446}
1447
1448static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO,
1449 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1450static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO,
1451 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1452static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO,
1453 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1454static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO,
1455 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1456static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
1457 bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
1458
1459static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO,
1460 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1461static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
1462 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1463static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO,
1464 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1465static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
1466 bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
1467
1468static DEVICE_ATTR(reported_mode, S_IRUGO,
1469 bq2415x_sysfs_show_reported_mode, NULL);
1470static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
1471 bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
1472static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
1473 bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer);
1474
1475static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO,
1476 bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers);
1477
1478static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1479static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1480static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1481static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
1482
1483static struct attribute *bq2415x_sysfs_attributes[] = {
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001484 /*
1485 * TODO: some (appropriate) of these attrs should be switched to
1486 * use power supply class props.
1487 */
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001488 &dev_attr_current_limit.attr,
1489 &dev_attr_weak_battery_voltage.attr,
1490 &dev_attr_battery_regulation_voltage.attr,
1491 &dev_attr_charge_current.attr,
1492 &dev_attr_termination_current.attr,
1493
1494 &dev_attr_charge_termination_enable.attr,
1495 &dev_attr_high_impedance_enable.attr,
1496 &dev_attr_otg_pin_enable.attr,
1497 &dev_attr_stat_pin_enable.attr,
1498
1499 &dev_attr_reported_mode.attr,
1500 &dev_attr_mode.attr,
1501 &dev_attr_timer.attr,
1502
1503 &dev_attr_registers.attr,
1504
1505 &dev_attr_otg_status.attr,
1506 &dev_attr_charge_status.attr,
1507 &dev_attr_boost_status.attr,
1508 &dev_attr_fault_status.attr,
1509 NULL,
1510};
1511
1512static const struct attribute_group bq2415x_sysfs_attr_group = {
1513 .attrs = bq2415x_sysfs_attributes,
1514};
1515
1516static int bq2415x_sysfs_init(struct bq2415x_device *bq)
1517{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001518 return sysfs_create_group(&bq->charger->dev.kobj,
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001519 &bq2415x_sysfs_attr_group);
1520}
1521
1522static void bq2415x_sysfs_exit(struct bq2415x_device *bq)
1523{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001524 sysfs_remove_group(&bq->charger->dev.kobj, &bq2415x_sysfs_attr_group);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001525}
1526
1527/* main bq2415x probe function */
1528static int bq2415x_probe(struct i2c_client *client,
Anton Vorontsov6c47a3e2012-11-18 15:35:32 -08001529 const struct i2c_device_id *id)
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001530{
1531 int ret;
1532 int num;
1533 char *name;
1534 struct bq2415x_device *bq;
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001535 struct device_node *np = client->dev.of_node;
1536 struct bq2415x_platform_data *pdata = client->dev.platform_data;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001537
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001538 if (!np && !pdata) {
1539 dev_err(&client->dev, "platform data missing\n");
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001540 return -ENODEV;
1541 }
1542
1543 /* Get new ID for the new device */
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001544 mutex_lock(&bq2415x_id_mutex);
Tejun Heo05e2cef2013-02-27 17:04:37 -08001545 num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001546 mutex_unlock(&bq2415x_id_mutex);
Tejun Heo05e2cef2013-02-27 17:04:37 -08001547 if (num < 0)
1548 return num;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001549
1550 name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
1551 if (!name) {
1552 dev_err(&client->dev, "failed to allocate device name\n");
1553 ret = -ENOMEM;
1554 goto error_1;
1555 }
1556
Sachin Kamata9672e12012-11-27 11:28:46 +05301557 bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001558 if (!bq) {
1559 dev_err(&client->dev, "failed to allocate device data\n");
1560 ret = -ENOMEM;
1561 goto error_2;
1562 }
1563
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001564 if (np) {
1565 bq->notify_psy = power_supply_get_by_phandle(np, "ti,usb-charger-detection");
1566
Krzysztof Kozlowski0eaf4372014-10-15 16:25:09 +02001567 if (IS_ERR(bq->notify_psy)) {
1568 dev_info(&client->dev,
1569 "no 'ti,usb-charger-detection' property (err=%ld)\n",
1570 PTR_ERR(bq->notify_psy));
1571 bq->notify_psy = NULL;
1572 } else if (!bq->notify_psy) {
1573 ret = -EPROBE_DEFER;
1574 goto error_2;
1575 }
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001576 }
1577 else if (pdata->notify_device)
1578 bq->notify_psy = power_supply_get_by_name(pdata->notify_device);
1579 else
1580 bq->notify_psy = NULL;
1581
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001582 i2c_set_clientdata(client, bq);
1583
1584 bq->id = num;
1585 bq->dev = &client->dev;
1586 bq->chip = id->driver_data;
1587 bq->name = name;
Pali Rohárac6324e2013-02-10 18:32:18 +01001588 bq->mode = BQ2415X_MODE_OFF;
1589 bq->reported_mode = BQ2415X_MODE_OFF;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001590 bq->autotimer = 0;
1591 bq->automode = 0;
1592
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001593 if (np) {
1594 ret = of_property_read_u32(np, "ti,current-limit",
1595 &bq->init_data.current_limit);
1596 if (ret)
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001597 goto error_3;
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001598 ret = of_property_read_u32(np, "ti,weak-battery-voltage",
1599 &bq->init_data.weak_battery_voltage);
1600 if (ret)
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001601 goto error_3;
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001602 ret = of_property_read_u32(np, "ti,battery-regulation-voltage",
1603 &bq->init_data.battery_regulation_voltage);
1604 if (ret)
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001605 goto error_3;
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001606 ret = of_property_read_u32(np, "ti,charge-current",
1607 &bq->init_data.charge_current);
1608 if (ret)
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001609 goto error_3;
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001610 ret = of_property_read_u32(np, "ti,termination-current",
1611 &bq->init_data.termination_current);
1612 if (ret)
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001613 goto error_3;
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001614 ret = of_property_read_u32(np, "ti,resistor-sense",
1615 &bq->init_data.resistor_sense);
1616 if (ret)
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001617 goto error_3;
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001618 } else {
1619 memcpy(&bq->init_data, pdata, sizeof(bq->init_data));
1620 }
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001621
1622 bq2415x_reset_chip(bq);
1623
1624 ret = bq2415x_power_supply_init(bq);
1625 if (ret) {
1626 dev_err(bq->dev, "failed to register power supply: %d\n", ret);
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001627 goto error_3;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001628 }
1629
1630 ret = bq2415x_sysfs_init(bq);
1631 if (ret) {
1632 dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret);
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001633 goto error_4;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001634 }
1635
1636 ret = bq2415x_set_defaults(bq);
1637 if (ret) {
1638 dev_err(bq->dev, "failed to set default values: %d\n", ret);
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001639 goto error_5;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001640 }
1641
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001642 if (bq->notify_psy) {
Pali Rohár32260302013-11-19 11:18:04 +01001643 bq->nb.notifier_call = bq2415x_notifier_call;
1644 ret = power_supply_reg_notifier(&bq->nb);
1645 if (ret) {
1646 dev_err(bq->dev, "failed to reg notifier: %d\n", ret);
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001647 goto error_6;
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001648 }
Sebastian Reichelfaffd232013-11-24 17:49:30 +01001649
1650 /* Query for initial reported_mode and set it */
1651 bq2415x_notifier_call(&bq->nb, PSY_EVENT_PROP_CHANGED, bq->notify_psy);
1652 bq2415x_set_mode(bq, bq->reported_mode);
1653
Pali Rohár32260302013-11-19 11:18:04 +01001654 bq->automode = 1;
1655 dev_info(bq->dev, "automode enabled\n");
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001656 } else {
1657 bq->automode = -1;
1658 dev_info(bq->dev, "automode not supported\n");
1659 }
1660
1661 INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work);
1662 bq2415x_set_autotimer(bq, 1);
1663
1664 dev_info(bq->dev, "driver registered\n");
1665 return 0;
1666
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001667error_6:
Pali Rohár32260302013-11-19 11:18:04 +01001668error_5:
Sachin Kamata9672e12012-11-27 11:28:46 +05301669 bq2415x_sysfs_exit(bq);
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001670error_4:
Sachin Kamata9672e12012-11-27 11:28:46 +05301671 bq2415x_power_supply_exit(bq);
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001672error_3:
1673 if (bq->notify_psy)
1674 power_supply_put(bq->notify_psy);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001675error_2:
1676 kfree(name);
1677error_1:
1678 mutex_lock(&bq2415x_id_mutex);
1679 idr_remove(&bq2415x_id, num);
1680 mutex_unlock(&bq2415x_id_mutex);
1681
1682 return ret;
1683}
1684
1685/* main bq2415x remove function */
1686
1687static int bq2415x_remove(struct i2c_client *client)
1688{
1689 struct bq2415x_device *bq = i2c_get_clientdata(client);
1690
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001691 if (bq->notify_psy) {
Pali Rohár32260302013-11-19 11:18:04 +01001692 power_supply_unreg_notifier(&bq->nb);
Krzysztof Kozlowski1a8dbe62015-03-12 08:44:18 +01001693 power_supply_put(bq->notify_psy);
1694 }
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001695
1696 bq2415x_sysfs_exit(bq);
1697 bq2415x_power_supply_exit(bq);
1698
1699 bq2415x_reset_chip(bq);
1700
1701 mutex_lock(&bq2415x_id_mutex);
1702 idr_remove(&bq2415x_id, bq->id);
1703 mutex_unlock(&bq2415x_id_mutex);
1704
1705 dev_info(bq->dev, "driver unregistered\n");
1706
1707 kfree(bq->name);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001708
1709 return 0;
1710}
1711
1712static const struct i2c_device_id bq2415x_i2c_id_table[] = {
1713 { "bq2415x", BQUNKNOWN },
1714 { "bq24150", BQ24150 },
1715 { "bq24150a", BQ24150A },
1716 { "bq24151", BQ24151 },
1717 { "bq24151a", BQ24151A },
1718 { "bq24152", BQ24152 },
1719 { "bq24153", BQ24153 },
1720 { "bq24153a", BQ24153A },
1721 { "bq24155", BQ24155 },
1722 { "bq24156", BQ24156 },
1723 { "bq24156a", BQ24156A },
Anda-Maria Nicolae0e1392d2015-03-05 13:23:57 +02001724 { "bq24157s", BQ24157S },
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001725 { "bq24158", BQ24158 },
1726 {},
1727};
1728MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table);
1729
1730static struct i2c_driver bq2415x_driver = {
1731 .driver = {
1732 .name = "bq2415x-charger",
1733 },
1734 .probe = bq2415x_probe,
1735 .remove = bq2415x_remove,
1736 .id_table = bq2415x_i2c_id_table,
1737};
Sachin Kamat463df512012-11-27 11:28:45 +05301738module_i2c_driver(bq2415x_driver);
Pali Rohár0bc98cc2012-11-02 20:18:11 +01001739
1740MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
1741MODULE_DESCRIPTION("bq2415x charger driver");
1742MODULE_LICENSE("GPL");