]>
Commit | Line | Data |
---|---|---|
0bc98cc6 | 1 | /* |
6c47a3e0 AV |
2 | * bq2415x charger driver |
3 | * | |
32260308 | 4 | * Copyright (C) 2011-2013 Pali Rohár <pali.rohar@gmail.com> |
6c47a3e0 AV |
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 | * | |
6c47a3e0 AV |
16 | * 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 | |
0e1392d9 AMN |
23 | * http://www.ti.com/product/bq24157s |
24 | * http://www.ti.com/product/bq24158 | |
6c47a3e0 | 25 | */ |
0bc98cc6 | 26 | |
0bc98cc6 PR |
27 | #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> | |
b01e7c3b | 38 | #include <linux/acpi.h> |
0bc98cc6 PR |
39 | |
40 | #include <linux/power/bq2415x_charger.h> | |
41 | ||
42 | /* timeout for resetting chip timer */ | |
43 | #define BQ2415X_TIMER_TIMEOUT 10 | |
44 | ||
45 | #define BQ2415X_REG_STATUS 0x00 | |
46 | #define BQ2415X_REG_CONTROL 0x01 | |
47 | #define BQ2415X_REG_VOLTAGE 0x02 | |
48 | #define BQ2415X_REG_VENDER 0x03 | |
49 | #define BQ2415X_REG_CURRENT 0x04 | |
50 | ||
51 | /* reset state for all registers */ | |
52 | #define BQ2415X_RESET_STATUS BIT(6) | |
53 | #define BQ2415X_RESET_CONTROL (BIT(4)|BIT(5)) | |
54 | #define BQ2415X_RESET_VOLTAGE (BIT(1)|BIT(3)) | |
55 | #define BQ2415X_RESET_CURRENT (BIT(0)|BIT(3)|BIT(7)) | |
56 | ||
57 | /* status register */ | |
58 | #define BQ2415X_BIT_TMR_RST 7 | |
59 | #define BQ2415X_BIT_OTG 7 | |
60 | #define BQ2415X_BIT_EN_STAT 6 | |
61 | #define BQ2415X_MASK_STAT (BIT(4)|BIT(5)) | |
62 | #define BQ2415X_SHIFT_STAT 4 | |
63 | #define BQ2415X_BIT_BOOST 3 | |
64 | #define BQ2415X_MASK_FAULT (BIT(0)|BIT(1)|BIT(2)) | |
65 | #define BQ2415X_SHIFT_FAULT 0 | |
66 | ||
67 | /* control register */ | |
68 | #define BQ2415X_MASK_LIMIT (BIT(6)|BIT(7)) | |
69 | #define BQ2415X_SHIFT_LIMIT 6 | |
70 | #define BQ2415X_MASK_VLOWV (BIT(4)|BIT(5)) | |
71 | #define BQ2415X_SHIFT_VLOWV 4 | |
72 | #define BQ2415X_BIT_TE 3 | |
73 | #define BQ2415X_BIT_CE 2 | |
74 | #define BQ2415X_BIT_HZ_MODE 1 | |
75 | #define BQ2415X_BIT_OPA_MODE 0 | |
76 | ||
77 | /* voltage register */ | |
78 | #define BQ2415X_MASK_VO (BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7)) | |
79 | #define BQ2415X_SHIFT_VO 2 | |
80 | #define BQ2415X_BIT_OTG_PL 1 | |
81 | #define BQ2415X_BIT_OTG_EN 0 | |
82 | ||
83 | /* vender register */ | |
84 | #define BQ2415X_MASK_VENDER (BIT(5)|BIT(6)|BIT(7)) | |
85 | #define BQ2415X_SHIFT_VENDER 5 | |
86 | #define BQ2415X_MASK_PN (BIT(3)|BIT(4)) | |
87 | #define BQ2415X_SHIFT_PN 3 | |
88 | #define BQ2415X_MASK_REVISION (BIT(0)|BIT(1)|BIT(2)) | |
89 | #define BQ2415X_SHIFT_REVISION 0 | |
90 | ||
91 | /* current register */ | |
92 | #define BQ2415X_MASK_RESET BIT(7) | |
93 | #define BQ2415X_MASK_VI_CHRG (BIT(4)|BIT(5)|BIT(6)) | |
94 | #define BQ2415X_SHIFT_VI_CHRG 4 | |
95 | /* N/A BIT(3) */ | |
96 | #define BQ2415X_MASK_VI_TERM (BIT(0)|BIT(1)|BIT(2)) | |
97 | #define BQ2415X_SHIFT_VI_TERM 0 | |
98 | ||
99 | ||
100 | enum bq2415x_command { | |
101 | BQ2415X_TIMER_RESET, | |
102 | BQ2415X_OTG_STATUS, | |
103 | BQ2415X_STAT_PIN_STATUS, | |
104 | BQ2415X_STAT_PIN_ENABLE, | |
105 | BQ2415X_STAT_PIN_DISABLE, | |
106 | BQ2415X_CHARGE_STATUS, | |
107 | BQ2415X_BOOST_STATUS, | |
108 | BQ2415X_FAULT_STATUS, | |
109 | ||
110 | BQ2415X_CHARGE_TERMINATION_STATUS, | |
111 | BQ2415X_CHARGE_TERMINATION_ENABLE, | |
112 | BQ2415X_CHARGE_TERMINATION_DISABLE, | |
113 | BQ2415X_CHARGER_STATUS, | |
114 | BQ2415X_CHARGER_ENABLE, | |
115 | BQ2415X_CHARGER_DISABLE, | |
116 | BQ2415X_HIGH_IMPEDANCE_STATUS, | |
117 | BQ2415X_HIGH_IMPEDANCE_ENABLE, | |
118 | BQ2415X_HIGH_IMPEDANCE_DISABLE, | |
119 | BQ2415X_BOOST_MODE_STATUS, | |
120 | BQ2415X_BOOST_MODE_ENABLE, | |
121 | BQ2415X_BOOST_MODE_DISABLE, | |
122 | ||
123 | BQ2415X_OTG_LEVEL, | |
124 | BQ2415X_OTG_ACTIVATE_HIGH, | |
125 | BQ2415X_OTG_ACTIVATE_LOW, | |
126 | BQ2415X_OTG_PIN_STATUS, | |
127 | BQ2415X_OTG_PIN_ENABLE, | |
128 | BQ2415X_OTG_PIN_DISABLE, | |
129 | ||
130 | BQ2415X_VENDER_CODE, | |
131 | BQ2415X_PART_NUMBER, | |
132 | BQ2415X_REVISION, | |
133 | }; | |
134 | ||
135 | enum bq2415x_chip { | |
136 | BQUNKNOWN, | |
137 | BQ24150, | |
138 | BQ24150A, | |
139 | BQ24151, | |
140 | BQ24151A, | |
141 | BQ24152, | |
142 | BQ24153, | |
143 | BQ24153A, | |
144 | BQ24155, | |
145 | BQ24156, | |
146 | BQ24156A, | |
0e1392d9 | 147 | BQ24157S, |
0bc98cc6 PR |
148 | BQ24158, |
149 | }; | |
150 | ||
151 | static char *bq2415x_chip_name[] = { | |
152 | "unknown", | |
153 | "bq24150", | |
154 | "bq24150a", | |
155 | "bq24151", | |
156 | "bq24151a", | |
157 | "bq24152", | |
158 | "bq24153", | |
159 | "bq24153a", | |
160 | "bq24155", | |
161 | "bq24156", | |
162 | "bq24156a", | |
0e1392d9 | 163 | "bq24157s", |
0bc98cc6 PR |
164 | "bq24158", |
165 | }; | |
166 | ||
167 | struct bq2415x_device { | |
168 | struct device *dev; | |
169 | struct bq2415x_platform_data init_data; | |
297d716f KK |
170 | struct power_supply *charger; |
171 | struct power_supply_desc charger_desc; | |
0bc98cc6 | 172 | struct delayed_work work; |
faffd234 | 173 | struct power_supply *notify_psy; |
32260308 | 174 | struct notifier_block nb; |
0bc98cc6 | 175 | enum bq2415x_mode reported_mode;/* mode reported by hook function */ |
881f985a | 176 | enum bq2415x_mode mode; /* currently configured mode */ |
0bc98cc6 PR |
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 = ® | |
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, | |
6c47a3e0 | 227 | u8 mask, u8 shift) |
0bc98cc6 PR |
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; | |
6c47a3e0 | 237 | return (ret & mask) >> shift; |
0bc98cc6 PR |
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; | |
6c47a3e0 | 245 | return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit); |
0bc98cc6 PR |
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, | |
6c47a3e0 | 281 | u8 mask, u8 shift) |
0bc98cc6 PR |
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, | |
6c47a3e0 | 300 | bool val, u8 bit) |
0bc98cc6 PR |
301 | { |
302 | if (bit > 8) | |
303 | return -EINVAL; | |
6c47a3e0 | 304 | return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit); |
0bc98cc6 PR |
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; | |
6c47a3e0 | 314 | |
0bc98cc6 PR |
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; | |
42d0631b | 355 | return ret > 0 ? 0 : 1; |
0bc98cc6 PR |
356 | case BQ2415X_CHARGER_ENABLE: |
357 | return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, | |
358 | 0, BQ2415X_BIT_CE); | |
359 | case BQ2415X_CHARGER_DISABLE: | |
360 | return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, | |
361 | 1, BQ2415X_BIT_CE); | |
362 | case BQ2415X_HIGH_IMPEDANCE_STATUS: | |
363 | return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL, | |
364 | BQ2415X_BIT_HZ_MODE); | |
365 | case BQ2415X_HIGH_IMPEDANCE_ENABLE: | |
366 | return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, | |
367 | 1, BQ2415X_BIT_HZ_MODE); | |
368 | case BQ2415X_HIGH_IMPEDANCE_DISABLE: | |
369 | return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, | |
370 | 0, BQ2415X_BIT_HZ_MODE); | |
371 | case BQ2415X_BOOST_MODE_STATUS: | |
372 | return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL, | |
373 | BQ2415X_BIT_OPA_MODE); | |
374 | case BQ2415X_BOOST_MODE_ENABLE: | |
375 | return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, | |
376 | 1, BQ2415X_BIT_OPA_MODE); | |
377 | case BQ2415X_BOOST_MODE_DISABLE: | |
378 | return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL, | |
379 | 0, BQ2415X_BIT_OPA_MODE); | |
380 | ||
381 | case BQ2415X_OTG_LEVEL: | |
382 | return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE, | |
383 | BQ2415X_BIT_OTG_PL); | |
384 | case BQ2415X_OTG_ACTIVATE_HIGH: | |
385 | return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE, | |
386 | 1, BQ2415X_BIT_OTG_PL); | |
387 | case BQ2415X_OTG_ACTIVATE_LOW: | |
388 | return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE, | |
389 | 0, BQ2415X_BIT_OTG_PL); | |
390 | case BQ2415X_OTG_PIN_STATUS: | |
391 | return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE, | |
392 | BQ2415X_BIT_OTG_EN); | |
393 | case BQ2415X_OTG_PIN_ENABLE: | |
394 | return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE, | |
395 | 1, BQ2415X_BIT_OTG_EN); | |
396 | case BQ2415X_OTG_PIN_DISABLE: | |
397 | return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE, | |
398 | 0, BQ2415X_BIT_OTG_EN); | |
399 | ||
400 | case BQ2415X_VENDER_CODE: | |
401 | return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER, | |
402 | BQ2415X_MASK_VENDER, BQ2415X_SHIFT_VENDER); | |
403 | case BQ2415X_PART_NUMBER: | |
404 | return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER, | |
405 | BQ2415X_MASK_PN, BQ2415X_SHIFT_PN); | |
406 | case BQ2415X_REVISION: | |
407 | return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER, | |
408 | BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION); | |
0bc98cc6 | 409 | } |
6c47a3e0 | 410 | return -EINVAL; |
0bc98cc6 PR |
411 | } |
412 | ||
413 | /* detect chip type */ | |
414 | static enum bq2415x_chip bq2415x_detect_chip(struct bq2415x_device *bq) | |
415 | { | |
416 | struct i2c_client *client = to_i2c_client(bq->dev); | |
417 | int ret = bq2415x_exec_command(bq, BQ2415X_PART_NUMBER); | |
418 | ||
419 | if (ret < 0) | |
420 | return ret; | |
421 | ||
422 | switch (client->addr) { | |
423 | case 0x6b: | |
424 | switch (ret) { | |
425 | case 0: | |
426 | if (bq->chip == BQ24151A) | |
427 | return bq->chip; | |
42d0631b | 428 | return BQ24151; |
0bc98cc6 PR |
429 | case 1: |
430 | if (bq->chip == BQ24150A || | |
431 | bq->chip == BQ24152 || | |
432 | bq->chip == BQ24155) | |
433 | return bq->chip; | |
42d0631b | 434 | return BQ24150; |
0bc98cc6 PR |
435 | case 2: |
436 | if (bq->chip == BQ24153A) | |
437 | return bq->chip; | |
42d0631b | 438 | return BQ24153; |
0bc98cc6 PR |
439 | default: |
440 | return BQUNKNOWN; | |
441 | } | |
442 | break; | |
443 | ||
444 | case 0x6a: | |
445 | switch (ret) { | |
446 | case 0: | |
447 | if (bq->chip == BQ24156A) | |
448 | return bq->chip; | |
42d0631b | 449 | return BQ24156; |
0bc98cc6 | 450 | case 2: |
0e1392d9 AMN |
451 | if (bq->chip == BQ24157S) |
452 | return bq->chip; | |
0bc98cc6 PR |
453 | return BQ24158; |
454 | default: | |
455 | return BQUNKNOWN; | |
456 | } | |
457 | break; | |
458 | } | |
459 | ||
460 | return BQUNKNOWN; | |
461 | } | |
462 | ||
463 | /* detect chip revision */ | |
464 | static int bq2415x_detect_revision(struct bq2415x_device *bq) | |
465 | { | |
466 | int ret = bq2415x_exec_command(bq, BQ2415X_REVISION); | |
467 | int chip = bq2415x_detect_chip(bq); | |
6c47a3e0 | 468 | |
0bc98cc6 PR |
469 | if (ret < 0 || chip < 0) |
470 | return -1; | |
471 | ||
472 | switch (chip) { | |
473 | case BQ24150: | |
474 | case BQ24150A: | |
475 | case BQ24151: | |
476 | case BQ24151A: | |
477 | case BQ24152: | |
478 | if (ret >= 0 && ret <= 3) | |
479 | return ret; | |
42d0631b | 480 | return -1; |
0bc98cc6 PR |
481 | case BQ24153: |
482 | case BQ24153A: | |
483 | case BQ24156: | |
484 | case BQ24156A: | |
0e1392d9 | 485 | case BQ24157S: |
0bc98cc6 PR |
486 | case BQ24158: |
487 | if (ret == 3) | |
488 | return 0; | |
489 | else if (ret == 1) | |
490 | return 1; | |
42d0631b | 491 | return -1; |
0bc98cc6 PR |
492 | case BQ24155: |
493 | if (ret == 3) | |
494 | return 3; | |
42d0631b | 495 | return -1; |
0bc98cc6 PR |
496 | case BQUNKNOWN: |
497 | return -1; | |
498 | } | |
499 | ||
500 | return -1; | |
501 | } | |
502 | ||
503 | /* return chip vender code */ | |
504 | static int bq2415x_get_vender_code(struct bq2415x_device *bq) | |
505 | { | |
6c47a3e0 AV |
506 | int ret; |
507 | ||
508 | ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE); | |
0bc98cc6 PR |
509 | if (ret < 0) |
510 | return 0; | |
6c47a3e0 AV |
511 | |
512 | /* convert to binary */ | |
513 | return (ret & 0x1) + | |
514 | ((ret >> 1) & 0x1) * 10 + | |
515 | ((ret >> 2) & 0x1) * 100; | |
0bc98cc6 PR |
516 | } |
517 | ||
518 | /* reset all chip registers to default state */ | |
519 | static void bq2415x_reset_chip(struct bq2415x_device *bq) | |
520 | { | |
521 | bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT); | |
522 | bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE); | |
523 | bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL); | |
524 | bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS); | |
525 | bq->timer_error = NULL; | |
526 | } | |
527 | ||
528 | /**** properties functions ****/ | |
529 | ||
530 | /* set current limit in mA */ | |
531 | static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA) | |
532 | { | |
533 | int val; | |
6c47a3e0 | 534 | |
0bc98cc6 PR |
535 | if (mA <= 100) |
536 | val = 0; | |
537 | else if (mA <= 500) | |
538 | val = 1; | |
539 | else if (mA <= 800) | |
540 | val = 2; | |
541 | else | |
542 | val = 3; | |
6c47a3e0 | 543 | |
0bc98cc6 PR |
544 | return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val, |
545 | BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT); | |
546 | } | |
547 | ||
548 | /* get current limit in mA */ | |
549 | static int bq2415x_get_current_limit(struct bq2415x_device *bq) | |
550 | { | |
6c47a3e0 AV |
551 | int ret; |
552 | ||
553 | ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL, | |
0bc98cc6 PR |
554 | BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT); |
555 | if (ret < 0) | |
556 | return ret; | |
557 | else if (ret == 0) | |
558 | return 100; | |
559 | else if (ret == 1) | |
560 | return 500; | |
561 | else if (ret == 2) | |
562 | return 800; | |
563 | else if (ret == 3) | |
564 | return 1800; | |
6c47a3e0 | 565 | return -EINVAL; |
0bc98cc6 PR |
566 | } |
567 | ||
568 | /* set weak battery voltage in mV */ | |
569 | static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV) | |
570 | { | |
0bc98cc6 | 571 | int val; |
6c47a3e0 AV |
572 | |
573 | /* round to 100mV */ | |
0bc98cc6 PR |
574 | if (mV <= 3400 + 50) |
575 | val = 0; | |
576 | else if (mV <= 3500 + 50) | |
577 | val = 1; | |
578 | else if (mV <= 3600 + 50) | |
579 | val = 2; | |
580 | else | |
581 | val = 3; | |
6c47a3e0 | 582 | |
0bc98cc6 PR |
583 | return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val, |
584 | BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV); | |
585 | } | |
586 | ||
587 | /* get weak battery voltage in mV */ | |
588 | static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq) | |
589 | { | |
6c47a3e0 AV |
590 | int ret; |
591 | ||
592 | ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL, | |
0bc98cc6 PR |
593 | BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV); |
594 | if (ret < 0) | |
595 | return ret; | |
6c47a3e0 | 596 | return 100 * (34 + ret); |
0bc98cc6 PR |
597 | } |
598 | ||
599 | /* set battery regulation voltage in mV */ | |
600 | static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq, | |
6c47a3e0 | 601 | int mV) |
0bc98cc6 PR |
602 | { |
603 | int val = (mV/10 - 350) / 2; | |
604 | ||
3ed5cd79 AB |
605 | /* |
606 | * According to datasheet, maximum battery regulation voltage is | |
607 | * 4440mV which is b101111 = 47. | |
608 | */ | |
0bc98cc6 PR |
609 | if (val < 0) |
610 | val = 0; | |
3ed5cd79 | 611 | else if (val > 47) |
0bc98cc6 PR |
612 | return -EINVAL; |
613 | ||
614 | return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val, | |
615 | BQ2415X_MASK_VO, BQ2415X_SHIFT_VO); | |
616 | } | |
617 | ||
618 | /* get battery regulation voltage in mV */ | |
619 | static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq) | |
620 | { | |
621 | int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE, | |
622 | BQ2415X_MASK_VO, BQ2415X_SHIFT_VO); | |
6c47a3e0 | 623 | |
0bc98cc6 PR |
624 | if (ret < 0) |
625 | return ret; | |
6c47a3e0 | 626 | return 10 * (350 + 2*ret); |
0bc98cc6 PR |
627 | } |
628 | ||
629 | /* set charge current in mA (platform data must provide resistor sense) */ | |
630 | static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA) | |
631 | { | |
632 | int val; | |
6c47a3e0 | 633 | |
0bc98cc6 | 634 | if (bq->init_data.resistor_sense <= 0) |
f6d8b774 | 635 | return -EINVAL; |
0bc98cc6 PR |
636 | |
637 | val = (mA * bq->init_data.resistor_sense - 37400) / 6800; | |
0bc98cc6 PR |
638 | if (val < 0) |
639 | val = 0; | |
640 | else if (val > 7) | |
641 | val = 7; | |
642 | ||
643 | return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val, | |
644 | BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET, | |
645 | BQ2415X_SHIFT_VI_CHRG); | |
646 | } | |
647 | ||
648 | /* get charge current in mA (platform data must provide resistor sense) */ | |
649 | static int bq2415x_get_charge_current(struct bq2415x_device *bq) | |
650 | { | |
651 | int ret; | |
6c47a3e0 | 652 | |
0bc98cc6 | 653 | if (bq->init_data.resistor_sense <= 0) |
f6d8b774 | 654 | return -EINVAL; |
0bc98cc6 PR |
655 | |
656 | ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT, | |
657 | BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG); | |
658 | if (ret < 0) | |
659 | return ret; | |
6c47a3e0 | 660 | return (37400 + 6800*ret) / bq->init_data.resistor_sense; |
0bc98cc6 PR |
661 | } |
662 | ||
663 | /* set termination current in mA (platform data must provide resistor sense) */ | |
664 | static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA) | |
665 | { | |
666 | int val; | |
6c47a3e0 | 667 | |
0bc98cc6 | 668 | if (bq->init_data.resistor_sense <= 0) |
f6d8b774 | 669 | return -EINVAL; |
0bc98cc6 PR |
670 | |
671 | val = (mA * bq->init_data.resistor_sense - 3400) / 3400; | |
0bc98cc6 PR |
672 | if (val < 0) |
673 | val = 0; | |
674 | else if (val > 7) | |
675 | val = 7; | |
676 | ||
677 | return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val, | |
678 | BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET, | |
679 | BQ2415X_SHIFT_VI_TERM); | |
680 | } | |
681 | ||
682 | /* get termination current in mA (platform data must provide resistor sense) */ | |
683 | static int bq2415x_get_termination_current(struct bq2415x_device *bq) | |
684 | { | |
685 | int ret; | |
6c47a3e0 | 686 | |
0bc98cc6 | 687 | if (bq->init_data.resistor_sense <= 0) |
f6d8b774 | 688 | return -EINVAL; |
0bc98cc6 PR |
689 | |
690 | ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT, | |
691 | BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM); | |
692 | if (ret < 0) | |
693 | return ret; | |
6c47a3e0 | 694 | return (3400 + 3400*ret) / bq->init_data.resistor_sense; |
0bc98cc6 PR |
695 | } |
696 | ||
697 | /* set default value of property */ | |
698 | #define bq2415x_set_default_value(bq, prop) \ | |
699 | do { \ | |
700 | int ret = 0; \ | |
701 | if (bq->init_data.prop != -1) \ | |
702 | ret = bq2415x_set_##prop(bq, bq->init_data.prop); \ | |
703 | if (ret < 0) \ | |
704 | return ret; \ | |
705 | } while (0) | |
706 | ||
707 | /* set default values of all properties */ | |
708 | static int bq2415x_set_defaults(struct bq2415x_device *bq) | |
709 | { | |
710 | bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE); | |
711 | bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE); | |
712 | bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE); | |
6c47a3e0 | 713 | |
0bc98cc6 PR |
714 | bq2415x_set_default_value(bq, current_limit); |
715 | bq2415x_set_default_value(bq, weak_battery_voltage); | |
716 | bq2415x_set_default_value(bq, battery_regulation_voltage); | |
6c47a3e0 | 717 | |
0bc98cc6 PR |
718 | if (bq->init_data.resistor_sense > 0) { |
719 | bq2415x_set_default_value(bq, charge_current); | |
720 | bq2415x_set_default_value(bq, termination_current); | |
721 | bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE); | |
722 | } | |
6c47a3e0 | 723 | |
0bc98cc6 PR |
724 | bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE); |
725 | return 0; | |
726 | } | |
727 | ||
728 | /**** charger mode functions ****/ | |
729 | ||
730 | /* set charger mode */ | |
731 | static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode) | |
732 | { | |
733 | int ret = 0; | |
734 | int charger = 0; | |
735 | int boost = 0; | |
736 | ||
0bc98cc6 PR |
737 | if (mode == BQ2415X_MODE_BOOST) |
738 | boost = 1; | |
ac6324e7 PR |
739 | else if (mode != BQ2415X_MODE_OFF) |
740 | charger = 1; | |
0bc98cc6 PR |
741 | |
742 | if (!charger) | |
743 | ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE); | |
744 | ||
745 | if (!boost) | |
746 | ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE); | |
747 | ||
748 | if (ret < 0) | |
749 | return ret; | |
750 | ||
751 | switch (mode) { | |
ac6324e7 PR |
752 | case BQ2415X_MODE_OFF: |
753 | dev_dbg(bq->dev, "changing mode to: Offline\n"); | |
754 | ret = bq2415x_set_current_limit(bq, 100); | |
755 | break; | |
0bc98cc6 PR |
756 | case BQ2415X_MODE_NONE: |
757 | dev_dbg(bq->dev, "changing mode to: N/A\n"); | |
758 | ret = bq2415x_set_current_limit(bq, 100); | |
759 | break; | |
760 | case BQ2415X_MODE_HOST_CHARGER: | |
761 | dev_dbg(bq->dev, "changing mode to: Host/HUB charger\n"); | |
762 | ret = bq2415x_set_current_limit(bq, 500); | |
763 | break; | |
764 | case BQ2415X_MODE_DEDICATED_CHARGER: | |
765 | dev_dbg(bq->dev, "changing mode to: Dedicated charger\n"); | |
766 | ret = bq2415x_set_current_limit(bq, 1800); | |
767 | break; | |
768 | case BQ2415X_MODE_BOOST: /* Boost mode */ | |
769 | dev_dbg(bq->dev, "changing mode to: Boost\n"); | |
770 | ret = bq2415x_set_current_limit(bq, 100); | |
771 | break; | |
772 | } | |
773 | ||
774 | if (ret < 0) | |
775 | return ret; | |
776 | ||
777 | if (charger) | |
778 | ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE); | |
779 | else if (boost) | |
780 | ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE); | |
781 | ||
782 | if (ret < 0) | |
783 | return ret; | |
784 | ||
785 | bq2415x_set_default_value(bq, weak_battery_voltage); | |
786 | bq2415x_set_default_value(bq, battery_regulation_voltage); | |
787 | ||
788 | bq->mode = mode; | |
297d716f | 789 | sysfs_notify(&bq->charger->dev.kobj, NULL, "mode"); |
0bc98cc6 PR |
790 | |
791 | return 0; | |
792 | ||
793 | } | |
794 | ||
32260308 PR |
795 | static int bq2415x_notifier_call(struct notifier_block *nb, |
796 | unsigned long val, void *v) | |
0bc98cc6 | 797 | { |
32260308 PR |
798 | struct bq2415x_device *bq = |
799 | container_of(nb, struct bq2415x_device, nb); | |
800 | struct power_supply *psy = v; | |
801 | enum bq2415x_mode mode; | |
802 | union power_supply_propval prop; | |
803 | int ret; | |
804 | int mA; | |
0bc98cc6 | 805 | |
32260308 PR |
806 | if (val != PSY_EVENT_PROP_CHANGED) |
807 | return NOTIFY_OK; | |
808 | ||
faffd234 | 809 | if (psy != bq->notify_psy) |
32260308 PR |
810 | return NOTIFY_OK; |
811 | ||
812 | dev_dbg(bq->dev, "notifier call was called\n"); | |
813 | ||
d7bdffb9 KK |
814 | ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX, |
815 | &prop); | |
32260308 PR |
816 | if (ret != 0) |
817 | return NOTIFY_OK; | |
818 | ||
819 | mA = prop.intval; | |
820 | ||
821 | if (mA == 0) | |
822 | mode = BQ2415X_MODE_OFF; | |
823 | else if (mA < 500) | |
824 | mode = BQ2415X_MODE_NONE; | |
825 | else if (mA < 1800) | |
826 | mode = BQ2415X_MODE_HOST_CHARGER; | |
827 | else | |
828 | mode = BQ2415X_MODE_DEDICATED_CHARGER; | |
829 | ||
830 | if (bq->reported_mode == mode) | |
831 | return NOTIFY_OK; | |
0bc98cc6 | 832 | |
0bc98cc6 PR |
833 | bq->reported_mode = mode; |
834 | ||
835 | /* if automode is not enabled do not tell about reported_mode */ | |
836 | if (bq->automode < 1) | |
32260308 | 837 | return NOTIFY_OK; |
0bc98cc6 | 838 | |
3c018504 | 839 | schedule_delayed_work(&bq->work, 0); |
0bc98cc6 | 840 | |
32260308 | 841 | return NOTIFY_OK; |
0bc98cc6 PR |
842 | } |
843 | ||
844 | /**** timer functions ****/ | |
845 | ||
846 | /* enable/disable auto resetting chip timer */ | |
847 | static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state) | |
848 | { | |
849 | mutex_lock(&bq2415x_timer_mutex); | |
850 | ||
851 | if (bq->autotimer == state) { | |
852 | mutex_unlock(&bq2415x_timer_mutex); | |
853 | return; | |
854 | } | |
855 | ||
856 | bq->autotimer = state; | |
857 | ||
858 | if (state) { | |
859 | schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ); | |
860 | bq2415x_exec_command(bq, BQ2415X_TIMER_RESET); | |
861 | bq->timer_error = NULL; | |
862 | } else { | |
863 | cancel_delayed_work_sync(&bq->work); | |
864 | } | |
865 | ||
866 | mutex_unlock(&bq2415x_timer_mutex); | |
867 | } | |
868 | ||
869 | /* called by bq2415x_timer_work on timer error */ | |
870 | static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg) | |
871 | { | |
872 | bq->timer_error = msg; | |
297d716f | 873 | sysfs_notify(&bq->charger->dev.kobj, NULL, "timer"); |
0bc98cc6 PR |
874 | dev_err(bq->dev, "%s\n", msg); |
875 | if (bq->automode > 0) | |
876 | bq->automode = 0; | |
ac6324e7 | 877 | bq2415x_set_mode(bq, BQ2415X_MODE_OFF); |
0bc98cc6 PR |
878 | bq2415x_set_autotimer(bq, 0); |
879 | } | |
880 | ||
881 | /* delayed work function for auto resetting chip timer */ | |
882 | static void bq2415x_timer_work(struct work_struct *work) | |
883 | { | |
884 | struct bq2415x_device *bq = container_of(work, struct bq2415x_device, | |
6c47a3e0 AV |
885 | work.work); |
886 | int ret; | |
887 | int error; | |
888 | int boost; | |
0bc98cc6 | 889 | |
3c018504 | 890 | if (bq->automode > 0 && (bq->reported_mode != bq->mode)) { |
297d716f | 891 | sysfs_notify(&bq->charger->dev.kobj, NULL, "reported_mode"); |
3c018504 SR |
892 | bq2415x_set_mode(bq, bq->reported_mode); |
893 | } | |
894 | ||
0bc98cc6 PR |
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, | |
6c47a3e0 AV |
994 | enum power_supply_property psp, |
995 | union power_supply_propval *val) | |
0bc98cc6 | 996 | { |
297d716f | 997 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
998 | int ret; |
999 | ||
1000 | switch (psp) { | |
1001 | case POWER_SUPPLY_PROP_STATUS: | |
1002 | ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS); | |
1003 | if (ret < 0) | |
1004 | return ret; | |
1005 | else if (ret == 0) /* Ready */ | |
1006 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; | |
1007 | else if (ret == 1) /* Charge in progress */ | |
1008 | val->intval = POWER_SUPPLY_STATUS_CHARGING; | |
1009 | else if (ret == 2) /* Charge done */ | |
1010 | val->intval = POWER_SUPPLY_STATUS_FULL; | |
1011 | else | |
1012 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; | |
1013 | break; | |
1014 | case POWER_SUPPLY_PROP_MODEL_NAME: | |
1015 | val->strval = bq->model; | |
1016 | break; | |
1017 | default: | |
1018 | return -EINVAL; | |
1019 | } | |
1020 | return 0; | |
1021 | } | |
1022 | ||
1023 | static int bq2415x_power_supply_init(struct bq2415x_device *bq) | |
1024 | { | |
1025 | int ret; | |
1026 | int chip; | |
1027 | char revstr[8]; | |
297d716f | 1028 | struct power_supply_config psy_cfg = { .drv_data = bq, }; |
0bc98cc6 | 1029 | |
297d716f KK |
1030 | bq->charger_desc.name = bq->name; |
1031 | bq->charger_desc.type = POWER_SUPPLY_TYPE_USB; | |
1032 | bq->charger_desc.properties = bq2415x_power_supply_props; | |
1033 | bq->charger_desc.num_properties = | |
1034 | ARRAY_SIZE(bq2415x_power_supply_props); | |
1035 | bq->charger_desc.get_property = bq2415x_power_supply_get_property; | |
0bc98cc6 PR |
1036 | |
1037 | ret = bq2415x_detect_chip(bq); | |
1038 | if (ret < 0) | |
1039 | chip = BQUNKNOWN; | |
1040 | else | |
1041 | chip = ret; | |
1042 | ||
1043 | ret = bq2415x_detect_revision(bq); | |
1044 | if (ret < 0) | |
1045 | strcpy(revstr, "unknown"); | |
1046 | else | |
1047 | sprintf(revstr, "1.%d", ret); | |
1048 | ||
1049 | bq->model = kasprintf(GFP_KERNEL, | |
1050 | "chip %s, revision %s, vender code %.3d", | |
1051 | bq2415x_chip_name[chip], revstr, | |
1052 | bq2415x_get_vender_code(bq)); | |
1053 | if (!bq->model) { | |
1054 | dev_err(bq->dev, "failed to allocate model name\n"); | |
1055 | return -ENOMEM; | |
1056 | } | |
1057 | ||
297d716f KK |
1058 | bq->charger = power_supply_register(bq->dev, &bq->charger_desc, |
1059 | &psy_cfg); | |
1060 | if (IS_ERR(bq->charger)) { | |
0bc98cc6 | 1061 | kfree(bq->model); |
297d716f | 1062 | return PTR_ERR(bq->charger); |
0bc98cc6 PR |
1063 | } |
1064 | ||
1065 | return 0; | |
1066 | } | |
1067 | ||
1068 | static void bq2415x_power_supply_exit(struct bq2415x_device *bq) | |
1069 | { | |
1070 | bq->autotimer = 0; | |
1071 | if (bq->automode > 0) | |
1072 | bq->automode = 0; | |
1073 | cancel_delayed_work_sync(&bq->work); | |
297d716f | 1074 | power_supply_unregister(bq->charger); |
0bc98cc6 PR |
1075 | kfree(bq->model); |
1076 | } | |
1077 | ||
1078 | /**** additional sysfs entries for power supply interface ****/ | |
1079 | ||
1080 | /* show *_status entries */ | |
1081 | static ssize_t bq2415x_sysfs_show_status(struct device *dev, | |
6c47a3e0 AV |
1082 | struct device_attribute *attr, |
1083 | char *buf) | |
0bc98cc6 PR |
1084 | { |
1085 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1086 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1087 | enum bq2415x_command command; |
1088 | int ret; | |
1089 | ||
1090 | if (strcmp(attr->attr.name, "otg_status") == 0) | |
1091 | command = BQ2415X_OTG_STATUS; | |
1092 | else if (strcmp(attr->attr.name, "charge_status") == 0) | |
1093 | command = BQ2415X_CHARGE_STATUS; | |
1094 | else if (strcmp(attr->attr.name, "boost_status") == 0) | |
1095 | command = BQ2415X_BOOST_STATUS; | |
1096 | else if (strcmp(attr->attr.name, "fault_status") == 0) | |
1097 | command = BQ2415X_FAULT_STATUS; | |
1098 | else | |
1099 | return -EINVAL; | |
1100 | ||
1101 | ret = bq2415x_exec_command(bq, command); | |
1102 | if (ret < 0) | |
1103 | return ret; | |
6c47a3e0 | 1104 | return sprintf(buf, "%d\n", ret); |
0bc98cc6 PR |
1105 | } |
1106 | ||
6c47a3e0 AV |
1107 | /* |
1108 | * set timer entry: | |
1109 | * auto - enable auto mode | |
1110 | * off - disable auto mode | |
1111 | * (other values) - reset chip timer | |
1112 | */ | |
0bc98cc6 | 1113 | static ssize_t bq2415x_sysfs_set_timer(struct device *dev, |
6c47a3e0 AV |
1114 | struct device_attribute *attr, |
1115 | const char *buf, | |
1116 | size_t count) | |
0bc98cc6 PR |
1117 | { |
1118 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1119 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
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; | |
6c47a3e0 | 1131 | return count; |
0bc98cc6 PR |
1132 | } |
1133 | ||
1134 | /* show timer entry (auto or off) */ | |
1135 | static ssize_t bq2415x_sysfs_show_timer(struct device *dev, | |
6c47a3e0 AV |
1136 | struct device_attribute *attr, |
1137 | char *buf) | |
0bc98cc6 PR |
1138 | { |
1139 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1140 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1141 | |
1142 | if (bq->timer_error) | |
1143 | return sprintf(buf, "%s\n", bq->timer_error); | |
1144 | ||
1145 | if (bq->autotimer) | |
1146 | return sprintf(buf, "auto\n"); | |
6c47a3e0 | 1147 | return sprintf(buf, "off\n"); |
0bc98cc6 PR |
1148 | } |
1149 | ||
6c47a3e0 AV |
1150 | /* |
1151 | * set mode entry: | |
1152 | * auto - if automode is supported, enable it and set mode to reported | |
1153 | * none - disable charger and boost mode | |
1154 | * host - charging mode for host/hub chargers (current limit 500mA) | |
1155 | * dedicated - charging mode for dedicated chargers (unlimited current limit) | |
1156 | * boost - disable charger and enable boost mode | |
1157 | */ | |
0bc98cc6 | 1158 | static ssize_t bq2415x_sysfs_set_mode(struct device *dev, |
6c47a3e0 AV |
1159 | struct device_attribute *attr, |
1160 | const char *buf, | |
1161 | size_t count) | |
0bc98cc6 PR |
1162 | { |
1163 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1164 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1165 | enum bq2415x_mode mode; |
1166 | int ret = 0; | |
1167 | ||
1168 | if (strncmp(buf, "auto", 4) == 0) { | |
1169 | if (bq->automode < 0) | |
f6d8b774 | 1170 | return -EINVAL; |
0bc98cc6 PR |
1171 | bq->automode = 1; |
1172 | mode = bq->reported_mode; | |
ac6324e7 PR |
1173 | } else if (strncmp(buf, "off", 3) == 0) { |
1174 | if (bq->automode > 0) | |
1175 | bq->automode = 0; | |
1176 | mode = BQ2415X_MODE_OFF; | |
0bc98cc6 PR |
1177 | } else if (strncmp(buf, "none", 4) == 0) { |
1178 | if (bq->automode > 0) | |
1179 | bq->automode = 0; | |
1180 | mode = BQ2415X_MODE_NONE; | |
1181 | } else if (strncmp(buf, "host", 4) == 0) { | |
1182 | if (bq->automode > 0) | |
1183 | bq->automode = 0; | |
1184 | mode = BQ2415X_MODE_HOST_CHARGER; | |
1185 | } else if (strncmp(buf, "dedicated", 9) == 0) { | |
1186 | if (bq->automode > 0) | |
1187 | bq->automode = 0; | |
1188 | mode = BQ2415X_MODE_DEDICATED_CHARGER; | |
1189 | } else if (strncmp(buf, "boost", 5) == 0) { | |
1190 | if (bq->automode > 0) | |
1191 | bq->automode = 0; | |
1192 | mode = BQ2415X_MODE_BOOST; | |
1193 | } else if (strncmp(buf, "reset", 5) == 0) { | |
1194 | bq2415x_reset_chip(bq); | |
1195 | bq2415x_set_defaults(bq); | |
1196 | if (bq->automode <= 0) | |
1197 | return count; | |
1198 | bq->automode = 1; | |
1199 | mode = bq->reported_mode; | |
6c47a3e0 | 1200 | } else { |
0bc98cc6 | 1201 | return -EINVAL; |
6c47a3e0 | 1202 | } |
0bc98cc6 PR |
1203 | |
1204 | ret = bq2415x_set_mode(bq, mode); | |
1205 | if (ret < 0) | |
1206 | return ret; | |
6c47a3e0 | 1207 | return count; |
0bc98cc6 PR |
1208 | } |
1209 | ||
1210 | /* show mode entry (auto, none, host, dedicated or boost) */ | |
1211 | static ssize_t bq2415x_sysfs_show_mode(struct device *dev, | |
6c47a3e0 AV |
1212 | struct device_attribute *attr, |
1213 | char *buf) | |
0bc98cc6 PR |
1214 | { |
1215 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1216 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1217 | ssize_t ret = 0; |
1218 | ||
1219 | if (bq->automode > 0) | |
1220 | ret += sprintf(buf+ret, "auto ("); | |
1221 | ||
1222 | switch (bq->mode) { | |
ac6324e7 PR |
1223 | case BQ2415X_MODE_OFF: |
1224 | ret += sprintf(buf+ret, "off"); | |
1225 | break; | |
0bc98cc6 PR |
1226 | case BQ2415X_MODE_NONE: |
1227 | ret += sprintf(buf+ret, "none"); | |
1228 | break; | |
1229 | case BQ2415X_MODE_HOST_CHARGER: | |
1230 | ret += sprintf(buf+ret, "host"); | |
1231 | break; | |
1232 | case BQ2415X_MODE_DEDICATED_CHARGER: | |
1233 | ret += sprintf(buf+ret, "dedicated"); | |
1234 | break; | |
1235 | case BQ2415X_MODE_BOOST: | |
1236 | ret += sprintf(buf+ret, "boost"); | |
1237 | break; | |
1238 | } | |
1239 | ||
1240 | if (bq->automode > 0) | |
1241 | ret += sprintf(buf+ret, ")"); | |
1242 | ||
1243 | ret += sprintf(buf+ret, "\n"); | |
1244 | return ret; | |
1245 | } | |
1246 | ||
1247 | /* show reported_mode entry (none, host, dedicated or boost) */ | |
1248 | static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev, | |
6c47a3e0 AV |
1249 | struct device_attribute *attr, |
1250 | char *buf) | |
0bc98cc6 PR |
1251 | { |
1252 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1253 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1254 | |
1255 | if (bq->automode < 0) | |
1256 | return -EINVAL; | |
1257 | ||
1258 | switch (bq->reported_mode) { | |
ac6324e7 PR |
1259 | case BQ2415X_MODE_OFF: |
1260 | return sprintf(buf, "off\n"); | |
0bc98cc6 PR |
1261 | case BQ2415X_MODE_NONE: |
1262 | return sprintf(buf, "none\n"); | |
1263 | case BQ2415X_MODE_HOST_CHARGER: | |
1264 | return sprintf(buf, "host\n"); | |
1265 | case BQ2415X_MODE_DEDICATED_CHARGER: | |
1266 | return sprintf(buf, "dedicated\n"); | |
1267 | case BQ2415X_MODE_BOOST: | |
1268 | return sprintf(buf, "boost\n"); | |
1269 | } | |
1270 | ||
1271 | return -EINVAL; | |
1272 | } | |
1273 | ||
1274 | /* directly set raw value to chip register, format: 'register value' */ | |
1275 | static ssize_t bq2415x_sysfs_set_registers(struct device *dev, | |
6c47a3e0 AV |
1276 | struct device_attribute *attr, |
1277 | const char *buf, | |
1278 | size_t count) | |
0bc98cc6 PR |
1279 | { |
1280 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1281 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1282 | ssize_t ret = 0; |
1283 | unsigned int reg; | |
1284 | unsigned int val; | |
1285 | ||
1286 | if (sscanf(buf, "%x %x", ®, &val) != 2) | |
1287 | return -EINVAL; | |
1288 | ||
1289 | if (reg > 4 || val > 255) | |
1290 | return -EINVAL; | |
1291 | ||
1292 | ret = bq2415x_i2c_write(bq, reg, val); | |
1293 | if (ret < 0) | |
1294 | return ret; | |
6c47a3e0 | 1295 | return count; |
0bc98cc6 PR |
1296 | } |
1297 | ||
1298 | /* print value of chip register, format: 'register=value' */ | |
1299 | static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq, | |
6c47a3e0 AV |
1300 | u8 reg, |
1301 | char *buf) | |
0bc98cc6 PR |
1302 | { |
1303 | int ret = bq2415x_i2c_read(bq, reg); | |
6c47a3e0 | 1304 | |
0bc98cc6 PR |
1305 | if (ret < 0) |
1306 | return sprintf(buf, "%#.2x=error %d\n", reg, ret); | |
6c47a3e0 | 1307 | return sprintf(buf, "%#.2x=%#.2x\n", reg, ret); |
0bc98cc6 PR |
1308 | } |
1309 | ||
1310 | /* show all raw values of chip register, format per line: 'register=value' */ | |
1311 | static ssize_t bq2415x_sysfs_show_registers(struct device *dev, | |
6c47a3e0 AV |
1312 | struct device_attribute *attr, |
1313 | char *buf) | |
0bc98cc6 PR |
1314 | { |
1315 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1316 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1317 | ssize_t ret = 0; |
1318 | ||
1319 | ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret); | |
1320 | ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret); | |
1321 | ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret); | |
1322 | ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret); | |
1323 | ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret); | |
1324 | return ret; | |
1325 | } | |
1326 | ||
1327 | /* set current and voltage limit entries (in mA or mV) */ | |
1328 | static ssize_t bq2415x_sysfs_set_limit(struct device *dev, | |
6c47a3e0 AV |
1329 | struct device_attribute *attr, |
1330 | const char *buf, | |
1331 | size_t count) | |
0bc98cc6 PR |
1332 | { |
1333 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1334 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1335 | long val; |
1336 | int ret; | |
1337 | ||
1338 | if (kstrtol(buf, 10, &val) < 0) | |
1339 | return -EINVAL; | |
1340 | ||
1341 | if (strcmp(attr->attr.name, "current_limit") == 0) | |
1342 | ret = bq2415x_set_current_limit(bq, val); | |
1343 | else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0) | |
1344 | ret = bq2415x_set_weak_battery_voltage(bq, val); | |
1345 | else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0) | |
1346 | ret = bq2415x_set_battery_regulation_voltage(bq, val); | |
1347 | else if (strcmp(attr->attr.name, "charge_current") == 0) | |
1348 | ret = bq2415x_set_charge_current(bq, val); | |
1349 | else if (strcmp(attr->attr.name, "termination_current") == 0) | |
1350 | ret = bq2415x_set_termination_current(bq, val); | |
1351 | else | |
1352 | return -EINVAL; | |
1353 | ||
1354 | if (ret < 0) | |
1355 | return ret; | |
6c47a3e0 | 1356 | return count; |
0bc98cc6 PR |
1357 | } |
1358 | ||
1359 | /* show current and voltage limit entries (in mA or mV) */ | |
1360 | static ssize_t bq2415x_sysfs_show_limit(struct device *dev, | |
6c47a3e0 AV |
1361 | struct device_attribute *attr, |
1362 | char *buf) | |
0bc98cc6 PR |
1363 | { |
1364 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1365 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1366 | int ret; |
1367 | ||
1368 | if (strcmp(attr->attr.name, "current_limit") == 0) | |
1369 | ret = bq2415x_get_current_limit(bq); | |
1370 | else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0) | |
1371 | ret = bq2415x_get_weak_battery_voltage(bq); | |
1372 | else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0) | |
1373 | ret = bq2415x_get_battery_regulation_voltage(bq); | |
1374 | else if (strcmp(attr->attr.name, "charge_current") == 0) | |
1375 | ret = bq2415x_get_charge_current(bq); | |
1376 | else if (strcmp(attr->attr.name, "termination_current") == 0) | |
1377 | ret = bq2415x_get_termination_current(bq); | |
1378 | else | |
1379 | return -EINVAL; | |
1380 | ||
1381 | if (ret < 0) | |
1382 | return ret; | |
6c47a3e0 | 1383 | return sprintf(buf, "%d\n", ret); |
0bc98cc6 PR |
1384 | } |
1385 | ||
1386 | /* set *_enable entries */ | |
1387 | static ssize_t bq2415x_sysfs_set_enable(struct device *dev, | |
6c47a3e0 AV |
1388 | struct device_attribute *attr, |
1389 | const char *buf, | |
1390 | size_t count) | |
0bc98cc6 PR |
1391 | { |
1392 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1393 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1394 | enum bq2415x_command command; |
1395 | long val; | |
1396 | int ret; | |
1397 | ||
1398 | if (kstrtol(buf, 10, &val) < 0) | |
1399 | return -EINVAL; | |
1400 | ||
1401 | if (strcmp(attr->attr.name, "charge_termination_enable") == 0) | |
1402 | command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE : | |
1403 | BQ2415X_CHARGE_TERMINATION_DISABLE; | |
1404 | else if (strcmp(attr->attr.name, "high_impedance_enable") == 0) | |
1405 | command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE : | |
1406 | BQ2415X_HIGH_IMPEDANCE_DISABLE; | |
1407 | else if (strcmp(attr->attr.name, "otg_pin_enable") == 0) | |
1408 | command = val ? BQ2415X_OTG_PIN_ENABLE : | |
1409 | BQ2415X_OTG_PIN_DISABLE; | |
1410 | else if (strcmp(attr->attr.name, "stat_pin_enable") == 0) | |
1411 | command = val ? BQ2415X_STAT_PIN_ENABLE : | |
1412 | BQ2415X_STAT_PIN_DISABLE; | |
1413 | else | |
1414 | return -EINVAL; | |
1415 | ||
1416 | ret = bq2415x_exec_command(bq, command); | |
1417 | if (ret < 0) | |
1418 | return ret; | |
6c47a3e0 | 1419 | return count; |
0bc98cc6 PR |
1420 | } |
1421 | ||
1422 | /* show *_enable entries */ | |
1423 | static ssize_t bq2415x_sysfs_show_enable(struct device *dev, | |
6c47a3e0 AV |
1424 | struct device_attribute *attr, |
1425 | char *buf) | |
0bc98cc6 PR |
1426 | { |
1427 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1428 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1429 | enum bq2415x_command command; |
1430 | int ret; | |
1431 | ||
1432 | if (strcmp(attr->attr.name, "charge_termination_enable") == 0) | |
1433 | command = BQ2415X_CHARGE_TERMINATION_STATUS; | |
1434 | else if (strcmp(attr->attr.name, "high_impedance_enable") == 0) | |
1435 | command = BQ2415X_HIGH_IMPEDANCE_STATUS; | |
1436 | else if (strcmp(attr->attr.name, "otg_pin_enable") == 0) | |
1437 | command = BQ2415X_OTG_PIN_STATUS; | |
1438 | else if (strcmp(attr->attr.name, "stat_pin_enable") == 0) | |
1439 | command = BQ2415X_STAT_PIN_STATUS; | |
1440 | else | |
1441 | return -EINVAL; | |
1442 | ||
1443 | ret = bq2415x_exec_command(bq, command); | |
1444 | if (ret < 0) | |
1445 | return ret; | |
6c47a3e0 | 1446 | return sprintf(buf, "%d\n", ret); |
0bc98cc6 PR |
1447 | } |
1448 | ||
1449 | static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO, | |
1450 | bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); | |
1451 | static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO, | |
1452 | bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); | |
1453 | static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO, | |
1454 | bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); | |
1455 | static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO, | |
1456 | bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); | |
1457 | static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO, | |
1458 | bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); | |
1459 | ||
1460 | static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO, | |
1461 | bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); | |
1462 | static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO, | |
1463 | bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); | |
1464 | static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO, | |
1465 | bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); | |
1466 | static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO, | |
1467 | bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); | |
1468 | ||
1469 | static DEVICE_ATTR(reported_mode, S_IRUGO, | |
1470 | bq2415x_sysfs_show_reported_mode, NULL); | |
1471 | static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, | |
1472 | bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode); | |
1473 | static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO, | |
1474 | bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer); | |
1475 | ||
1476 | static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO, | |
1477 | bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers); | |
1478 | ||
1479 | static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); | |
1480 | static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); | |
1481 | static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); | |
1482 | static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); | |
1483 | ||
1484 | static struct attribute *bq2415x_sysfs_attributes[] = { | |
6c47a3e0 AV |
1485 | /* |
1486 | * TODO: some (appropriate) of these attrs should be switched to | |
1487 | * use power supply class props. | |
1488 | */ | |
0bc98cc6 PR |
1489 | &dev_attr_current_limit.attr, |
1490 | &dev_attr_weak_battery_voltage.attr, | |
1491 | &dev_attr_battery_regulation_voltage.attr, | |
1492 | &dev_attr_charge_current.attr, | |
1493 | &dev_attr_termination_current.attr, | |
1494 | ||
1495 | &dev_attr_charge_termination_enable.attr, | |
1496 | &dev_attr_high_impedance_enable.attr, | |
1497 | &dev_attr_otg_pin_enable.attr, | |
1498 | &dev_attr_stat_pin_enable.attr, | |
1499 | ||
1500 | &dev_attr_reported_mode.attr, | |
1501 | &dev_attr_mode.attr, | |
1502 | &dev_attr_timer.attr, | |
1503 | ||
1504 | &dev_attr_registers.attr, | |
1505 | ||
1506 | &dev_attr_otg_status.attr, | |
1507 | &dev_attr_charge_status.attr, | |
1508 | &dev_attr_boost_status.attr, | |
1509 | &dev_attr_fault_status.attr, | |
1510 | NULL, | |
1511 | }; | |
1512 | ||
1513 | static const struct attribute_group bq2415x_sysfs_attr_group = { | |
1514 | .attrs = bq2415x_sysfs_attributes, | |
1515 | }; | |
1516 | ||
1517 | static int bq2415x_sysfs_init(struct bq2415x_device *bq) | |
1518 | { | |
297d716f | 1519 | return sysfs_create_group(&bq->charger->dev.kobj, |
0bc98cc6 PR |
1520 | &bq2415x_sysfs_attr_group); |
1521 | } | |
1522 | ||
1523 | static void bq2415x_sysfs_exit(struct bq2415x_device *bq) | |
1524 | { | |
297d716f | 1525 | sysfs_remove_group(&bq->charger->dev.kobj, &bq2415x_sysfs_attr_group); |
0bc98cc6 PR |
1526 | } |
1527 | ||
1528 | /* main bq2415x probe function */ | |
1529 | static int bq2415x_probe(struct i2c_client *client, | |
6c47a3e0 | 1530 | const struct i2c_device_id *id) |
0bc98cc6 PR |
1531 | { |
1532 | int ret; | |
1533 | int num; | |
b01e7c3b | 1534 | char *name = NULL; |
0bc98cc6 | 1535 | struct bq2415x_device *bq; |
faffd234 SR |
1536 | struct device_node *np = client->dev.of_node; |
1537 | struct bq2415x_platform_data *pdata = client->dev.platform_data; | |
b01e7c3b | 1538 | const struct acpi_device_id *acpi_id = NULL; |
0bc98cc6 | 1539 | |
b01e7c3b AMN |
1540 | if (!np && !pdata && !ACPI_HANDLE(&client->dev)) { |
1541 | dev_err(&client->dev, "Neither devicetree, nor platform data, nor ACPI support\n"); | |
0bc98cc6 PR |
1542 | return -ENODEV; |
1543 | } | |
1544 | ||
1545 | /* Get new ID for the new device */ | |
0bc98cc6 | 1546 | mutex_lock(&bq2415x_id_mutex); |
05e2cefa | 1547 | num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL); |
0bc98cc6 | 1548 | mutex_unlock(&bq2415x_id_mutex); |
05e2cefa TH |
1549 | if (num < 0) |
1550 | return num; | |
0bc98cc6 | 1551 | |
b01e7c3b AMN |
1552 | if (id) { |
1553 | name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num); | |
1554 | } else if (ACPI_HANDLE(&client->dev)) { | |
1555 | acpi_id = | |
1556 | acpi_match_device(client->dev.driver->acpi_match_table, | |
1557 | &client->dev); | |
1558 | name = kasprintf(GFP_KERNEL, "%s-%d", acpi_id->id, num); | |
1559 | } | |
0bc98cc6 PR |
1560 | if (!name) { |
1561 | dev_err(&client->dev, "failed to allocate device name\n"); | |
1562 | ret = -ENOMEM; | |
1563 | goto error_1; | |
1564 | } | |
1565 | ||
a9672e13 | 1566 | bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL); |
0bc98cc6 | 1567 | if (!bq) { |
0bc98cc6 PR |
1568 | ret = -ENOMEM; |
1569 | goto error_2; | |
1570 | } | |
1571 | ||
faffd234 | 1572 | if (np) { |
f6d8b774 AMN |
1573 | bq->notify_psy = power_supply_get_by_phandle(np, |
1574 | "ti,usb-charger-detection"); | |
faffd234 | 1575 | |
0eaf437a KK |
1576 | if (IS_ERR(bq->notify_psy)) { |
1577 | dev_info(&client->dev, | |
f6d8b774 | 1578 | "no 'ti,usb-charger-detection' property (err=%ld)\n", |
0eaf437a KK |
1579 | PTR_ERR(bq->notify_psy)); |
1580 | bq->notify_psy = NULL; | |
1581 | } else if (!bq->notify_psy) { | |
1582 | ret = -EPROBE_DEFER; | |
1583 | goto error_2; | |
1584 | } | |
b01e7c3b | 1585 | } else if (pdata && pdata->notify_device) { |
faffd234 | 1586 | bq->notify_psy = power_supply_get_by_name(pdata->notify_device); |
f6d8b774 | 1587 | } else { |
faffd234 | 1588 | bq->notify_psy = NULL; |
f6d8b774 | 1589 | } |
faffd234 | 1590 | |
0bc98cc6 PR |
1591 | i2c_set_clientdata(client, bq); |
1592 | ||
1593 | bq->id = num; | |
1594 | bq->dev = &client->dev; | |
b01e7c3b AMN |
1595 | if (id) |
1596 | bq->chip = id->driver_data; | |
1597 | else if (ACPI_HANDLE(bq->dev)) | |
1598 | bq->chip = acpi_id->driver_data; | |
0bc98cc6 | 1599 | bq->name = name; |
ac6324e7 PR |
1600 | bq->mode = BQ2415X_MODE_OFF; |
1601 | bq->reported_mode = BQ2415X_MODE_OFF; | |
0bc98cc6 PR |
1602 | bq->autotimer = 0; |
1603 | bq->automode = 0; | |
1604 | ||
b01e7c3b AMN |
1605 | if (np || ACPI_HANDLE(bq->dev)) { |
1606 | ret = device_property_read_u32(bq->dev, | |
1607 | "ti,current-limit", | |
1608 | &bq->init_data.current_limit); | |
faffd234 | 1609 | if (ret) |
1a8dbe6f | 1610 | goto error_3; |
b01e7c3b AMN |
1611 | ret = device_property_read_u32(bq->dev, |
1612 | "ti,weak-battery-voltage", | |
1613 | &bq->init_data.weak_battery_voltage); | |
faffd234 | 1614 | if (ret) |
1a8dbe6f | 1615 | goto error_3; |
b01e7c3b AMN |
1616 | ret = device_property_read_u32(bq->dev, |
1617 | "ti,battery-regulation-voltage", | |
faffd234 SR |
1618 | &bq->init_data.battery_regulation_voltage); |
1619 | if (ret) | |
1a8dbe6f | 1620 | goto error_3; |
b01e7c3b AMN |
1621 | ret = device_property_read_u32(bq->dev, |
1622 | "ti,charge-current", | |
1623 | &bq->init_data.charge_current); | |
faffd234 | 1624 | if (ret) |
1a8dbe6f | 1625 | goto error_3; |
b01e7c3b AMN |
1626 | ret = device_property_read_u32(bq->dev, |
1627 | "ti,termination-current", | |
1628 | &bq->init_data.termination_current); | |
faffd234 | 1629 | if (ret) |
1a8dbe6f | 1630 | goto error_3; |
b01e7c3b AMN |
1631 | ret = device_property_read_u32(bq->dev, |
1632 | "ti,resistor-sense", | |
1633 | &bq->init_data.resistor_sense); | |
faffd234 | 1634 | if (ret) |
1a8dbe6f | 1635 | goto error_3; |
faffd234 SR |
1636 | } else { |
1637 | memcpy(&bq->init_data, pdata, sizeof(bq->init_data)); | |
1638 | } | |
0bc98cc6 PR |
1639 | |
1640 | bq2415x_reset_chip(bq); | |
1641 | ||
1642 | ret = bq2415x_power_supply_init(bq); | |
1643 | if (ret) { | |
1644 | dev_err(bq->dev, "failed to register power supply: %d\n", ret); | |
1a8dbe6f | 1645 | goto error_3; |
0bc98cc6 PR |
1646 | } |
1647 | ||
1648 | ret = bq2415x_sysfs_init(bq); | |
1649 | if (ret) { | |
1650 | dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret); | |
1a8dbe6f | 1651 | goto error_4; |
0bc98cc6 PR |
1652 | } |
1653 | ||
1654 | ret = bq2415x_set_defaults(bq); | |
1655 | if (ret) { | |
1656 | dev_err(bq->dev, "failed to set default values: %d\n", ret); | |
1a8dbe6f | 1657 | goto error_5; |
0bc98cc6 PR |
1658 | } |
1659 | ||
faffd234 | 1660 | if (bq->notify_psy) { |
32260308 PR |
1661 | bq->nb.notifier_call = bq2415x_notifier_call; |
1662 | ret = power_supply_reg_notifier(&bq->nb); | |
1663 | if (ret) { | |
1664 | dev_err(bq->dev, "failed to reg notifier: %d\n", ret); | |
1a8dbe6f | 1665 | goto error_6; |
32260308 | 1666 | } |
faffd234 SR |
1667 | |
1668 | /* Query for initial reported_mode and set it */ | |
f6d8b774 AMN |
1669 | bq2415x_notifier_call(&bq->nb, PSY_EVENT_PROP_CHANGED, |
1670 | bq->notify_psy); | |
faffd234 SR |
1671 | bq2415x_set_mode(bq, bq->reported_mode); |
1672 | ||
32260308 PR |
1673 | bq->automode = 1; |
1674 | dev_info(bq->dev, "automode enabled\n"); | |
0bc98cc6 PR |
1675 | } else { |
1676 | bq->automode = -1; | |
1677 | dev_info(bq->dev, "automode not supported\n"); | |
1678 | } | |
1679 | ||
1680 | INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work); | |
1681 | bq2415x_set_autotimer(bq, 1); | |
1682 | ||
1683 | dev_info(bq->dev, "driver registered\n"); | |
1684 | return 0; | |
1685 | ||
1a8dbe6f | 1686 | error_6: |
32260308 | 1687 | error_5: |
a9672e13 | 1688 | bq2415x_sysfs_exit(bq); |
1a8dbe6f | 1689 | error_4: |
a9672e13 | 1690 | bq2415x_power_supply_exit(bq); |
1a8dbe6f KK |
1691 | error_3: |
1692 | if (bq->notify_psy) | |
1693 | power_supply_put(bq->notify_psy); | |
0bc98cc6 PR |
1694 | error_2: |
1695 | kfree(name); | |
1696 | error_1: | |
1697 | mutex_lock(&bq2415x_id_mutex); | |
1698 | idr_remove(&bq2415x_id, num); | |
1699 | mutex_unlock(&bq2415x_id_mutex); | |
1700 | ||
1701 | return ret; | |
1702 | } | |
1703 | ||
1704 | /* main bq2415x remove function */ | |
1705 | ||
1706 | static int bq2415x_remove(struct i2c_client *client) | |
1707 | { | |
1708 | struct bq2415x_device *bq = i2c_get_clientdata(client); | |
1709 | ||
1a8dbe6f | 1710 | if (bq->notify_psy) { |
32260308 | 1711 | power_supply_unreg_notifier(&bq->nb); |
1a8dbe6f KK |
1712 | power_supply_put(bq->notify_psy); |
1713 | } | |
0bc98cc6 PR |
1714 | |
1715 | bq2415x_sysfs_exit(bq); | |
1716 | bq2415x_power_supply_exit(bq); | |
1717 | ||
1718 | bq2415x_reset_chip(bq); | |
1719 | ||
1720 | mutex_lock(&bq2415x_id_mutex); | |
1721 | idr_remove(&bq2415x_id, bq->id); | |
1722 | mutex_unlock(&bq2415x_id_mutex); | |
1723 | ||
1724 | dev_info(bq->dev, "driver unregistered\n"); | |
1725 | ||
1726 | kfree(bq->name); | |
0bc98cc6 PR |
1727 | |
1728 | return 0; | |
1729 | } | |
1730 | ||
1731 | static const struct i2c_device_id bq2415x_i2c_id_table[] = { | |
1732 | { "bq2415x", BQUNKNOWN }, | |
1733 | { "bq24150", BQ24150 }, | |
1734 | { "bq24150a", BQ24150A }, | |
1735 | { "bq24151", BQ24151 }, | |
1736 | { "bq24151a", BQ24151A }, | |
1737 | { "bq24152", BQ24152 }, | |
1738 | { "bq24153", BQ24153 }, | |
1739 | { "bq24153a", BQ24153A }, | |
1740 | { "bq24155", BQ24155 }, | |
1741 | { "bq24156", BQ24156 }, | |
1742 | { "bq24156a", BQ24156A }, | |
0e1392d9 | 1743 | { "bq24157s", BQ24157S }, |
0bc98cc6 PR |
1744 | { "bq24158", BQ24158 }, |
1745 | {}, | |
1746 | }; | |
1747 | MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table); | |
1748 | ||
b01e7c3b AMN |
1749 | static const struct acpi_device_id bq2415x_i2c_acpi_match[] = { |
1750 | { "BQ2415X", BQUNKNOWN }, | |
1751 | { "BQ241500", BQ24150 }, | |
1752 | { "BQA24150", BQ24150A }, | |
1753 | { "BQ241510", BQ24151 }, | |
1754 | { "BQA24151", BQ24151A }, | |
1755 | { "BQ241520", BQ24152 }, | |
1756 | { "BQ241530", BQ24153 }, | |
1757 | { "BQA24153", BQ24153A }, | |
1758 | { "BQ241550", BQ24155 }, | |
1759 | { "BQ241560", BQ24156 }, | |
1760 | { "BQA24156", BQ24156A }, | |
1761 | { "BQS24157", BQ24157S }, | |
1762 | { "BQ241580", BQ24158 }, | |
1763 | {}, | |
1764 | }; | |
1765 | MODULE_DEVICE_TABLE(acpi, bq2415x_i2c_acpi_match); | |
1766 | ||
0bc98cc6 PR |
1767 | static struct i2c_driver bq2415x_driver = { |
1768 | .driver = { | |
1769 | .name = "bq2415x-charger", | |
b01e7c3b | 1770 | .acpi_match_table = ACPI_PTR(bq2415x_i2c_acpi_match), |
0bc98cc6 PR |
1771 | }, |
1772 | .probe = bq2415x_probe, | |
1773 | .remove = bq2415x_remove, | |
1774 | .id_table = bq2415x_i2c_id_table, | |
1775 | }; | |
463df514 | 1776 | module_i2c_driver(bq2415x_driver); |
0bc98cc6 PR |
1777 | |
1778 | MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>"); | |
1779 | MODULE_DESCRIPTION("bq2415x charger driver"); | |
1780 | MODULE_LICENSE("GPL"); |