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> | |
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 | ||
99 | enum 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 | ||
134 | enum bq2415x_chip { | |
135 | BQUNKNOWN, | |
136 | BQ24150, | |
137 | BQ24150A, | |
138 | BQ24151, | |
139 | BQ24151A, | |
140 | BQ24152, | |
141 | BQ24153, | |
142 | BQ24153A, | |
143 | BQ24155, | |
144 | BQ24156, | |
145 | BQ24156A, | |
0e1392d9 | 146 | BQ24157S, |
0bc98cc6 PR |
147 | BQ24158, |
148 | }; | |
149 | ||
150 | static 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", | |
0e1392d9 | 162 | "bq24157s", |
0bc98cc6 PR |
163 | "bq24158", |
164 | }; | |
165 | ||
166 | struct bq2415x_device { | |
167 | struct device *dev; | |
168 | struct bq2415x_platform_data init_data; | |
297d716f KK |
169 | struct power_supply *charger; |
170 | struct power_supply_desc charger_desc; | |
0bc98cc6 | 171 | struct delayed_work work; |
faffd234 | 172 | struct power_supply *notify_psy; |
32260308 | 173 | struct notifier_block nb; |
0bc98cc6 | 174 | enum bq2415x_mode reported_mode;/* mode reported by hook function */ |
881f985a | 175 | enum bq2415x_mode mode; /* currently configured mode */ |
0bc98cc6 PR |
176 | 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 */ | |
186 | static DEFINE_IDR(bq2415x_id); | |
187 | ||
188 | static DEFINE_MUTEX(bq2415x_id_mutex); | |
189 | static DEFINE_MUTEX(bq2415x_timer_mutex); | |
190 | static DEFINE_MUTEX(bq2415x_i2c_mutex); | |
191 | ||
192 | /**** i2c read functions ****/ | |
193 | ||
194 | /* read value from register */ | |
195 | static 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 = ® | |
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 */ | |
225 | static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg, | |
6c47a3e0 | 226 | u8 mask, u8 shift) |
0bc98cc6 PR |
227 | { |
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; | |
6c47a3e0 | 236 | return (ret & mask) >> shift; |
0bc98cc6 PR |
237 | } |
238 | ||
239 | /* read value from register and return one specified bit */ | |
240 | static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit) | |
241 | { | |
242 | if (bit > 8) | |
243 | return -EINVAL; | |
6c47a3e0 | 244 | return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit); |
0bc98cc6 PR |
245 | } |
246 | ||
247 | /**** i2c write functions ****/ | |
248 | ||
249 | /* write value to register */ | |
250 | static 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 */ | |
279 | static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val, | |
6c47a3e0 | 280 | u8 mask, u8 shift) |
0bc98cc6 PR |
281 | { |
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 */ | |
298 | static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg, | |
6c47a3e0 | 299 | bool val, u8 bit) |
0bc98cc6 PR |
300 | { |
301 | if (bit > 8) | |
302 | return -EINVAL; | |
6c47a3e0 | 303 | return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit); |
0bc98cc6 PR |
304 | } |
305 | ||
306 | /**** global functions ****/ | |
307 | ||
308 | /* exec command function */ | |
309 | static int bq2415x_exec_command(struct bq2415x_device *bq, | |
310 | enum bq2415x_command command) | |
311 | { | |
312 | int ret; | |
6c47a3e0 | 313 | |
0bc98cc6 PR |
314 | 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; | |
42d0631b | 354 | return ret > 0 ? 0 : 1; |
0bc98cc6 PR |
355 | 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); | |
0bc98cc6 | 408 | } |
6c47a3e0 | 409 | return -EINVAL; |
0bc98cc6 PR |
410 | } |
411 | ||
412 | /* detect chip type */ | |
413 | static 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; | |
42d0631b | 427 | return BQ24151; |
0bc98cc6 PR |
428 | case 1: |
429 | if (bq->chip == BQ24150A || | |
430 | bq->chip == BQ24152 || | |
431 | bq->chip == BQ24155) | |
432 | return bq->chip; | |
42d0631b | 433 | return BQ24150; |
0bc98cc6 PR |
434 | case 2: |
435 | if (bq->chip == BQ24153A) | |
436 | return bq->chip; | |
42d0631b | 437 | return BQ24153; |
0bc98cc6 PR |
438 | 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; | |
42d0631b | 448 | return BQ24156; |
0bc98cc6 | 449 | case 2: |
0e1392d9 AMN |
450 | if (bq->chip == BQ24157S) |
451 | return bq->chip; | |
0bc98cc6 PR |
452 | return BQ24158; |
453 | default: | |
454 | return BQUNKNOWN; | |
455 | } | |
456 | break; | |
457 | } | |
458 | ||
459 | return BQUNKNOWN; | |
460 | } | |
461 | ||
462 | /* detect chip revision */ | |
463 | static 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); | |
6c47a3e0 | 467 | |
0bc98cc6 PR |
468 | 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; | |
42d0631b | 479 | return -1; |
0bc98cc6 PR |
480 | case BQ24153: |
481 | case BQ24153A: | |
482 | case BQ24156: | |
483 | case BQ24156A: | |
0e1392d9 | 484 | case BQ24157S: |
0bc98cc6 PR |
485 | case BQ24158: |
486 | if (ret == 3) | |
487 | return 0; | |
488 | else if (ret == 1) | |
489 | return 1; | |
42d0631b | 490 | return -1; |
0bc98cc6 PR |
491 | case BQ24155: |
492 | if (ret == 3) | |
493 | return 3; | |
42d0631b | 494 | return -1; |
0bc98cc6 PR |
495 | case BQUNKNOWN: |
496 | return -1; | |
497 | } | |
498 | ||
499 | return -1; | |
500 | } | |
501 | ||
502 | /* return chip vender code */ | |
503 | static int bq2415x_get_vender_code(struct bq2415x_device *bq) | |
504 | { | |
6c47a3e0 AV |
505 | int ret; |
506 | ||
507 | ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE); | |
0bc98cc6 PR |
508 | if (ret < 0) |
509 | return 0; | |
6c47a3e0 AV |
510 | |
511 | /* convert to binary */ | |
512 | return (ret & 0x1) + | |
513 | ((ret >> 1) & 0x1) * 10 + | |
514 | ((ret >> 2) & 0x1) * 100; | |
0bc98cc6 PR |
515 | } |
516 | ||
517 | /* reset all chip registers to default state */ | |
518 | static 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 */ | |
530 | static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA) | |
531 | { | |
532 | int val; | |
6c47a3e0 | 533 | |
0bc98cc6 PR |
534 | 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; | |
6c47a3e0 | 542 | |
0bc98cc6 PR |
543 | 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 */ | |
548 | static int bq2415x_get_current_limit(struct bq2415x_device *bq) | |
549 | { | |
6c47a3e0 AV |
550 | int ret; |
551 | ||
552 | ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL, | |
0bc98cc6 PR |
553 | 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; | |
6c47a3e0 | 564 | return -EINVAL; |
0bc98cc6 PR |
565 | } |
566 | ||
567 | /* set weak battery voltage in mV */ | |
568 | static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV) | |
569 | { | |
0bc98cc6 | 570 | int val; |
6c47a3e0 AV |
571 | |
572 | /* round to 100mV */ | |
0bc98cc6 PR |
573 | 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; | |
6c47a3e0 | 581 | |
0bc98cc6 PR |
582 | 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 */ | |
587 | static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq) | |
588 | { | |
6c47a3e0 AV |
589 | int ret; |
590 | ||
591 | ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL, | |
0bc98cc6 PR |
592 | BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV); |
593 | if (ret < 0) | |
594 | return ret; | |
6c47a3e0 | 595 | return 100 * (34 + ret); |
0bc98cc6 PR |
596 | } |
597 | ||
598 | /* set battery regulation voltage in mV */ | |
599 | static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq, | |
6c47a3e0 | 600 | int mV) |
0bc98cc6 PR |
601 | { |
602 | int val = (mV/10 - 350) / 2; | |
603 | ||
3ed5cd79 AB |
604 | /* |
605 | * According to datasheet, maximum battery regulation voltage is | |
606 | * 4440mV which is b101111 = 47. | |
607 | */ | |
0bc98cc6 PR |
608 | if (val < 0) |
609 | val = 0; | |
3ed5cd79 | 610 | else if (val > 47) |
0bc98cc6 PR |
611 | 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 */ | |
618 | static 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); | |
6c47a3e0 | 622 | |
0bc98cc6 PR |
623 | if (ret < 0) |
624 | return ret; | |
6c47a3e0 | 625 | return 10 * (350 + 2*ret); |
0bc98cc6 PR |
626 | } |
627 | ||
628 | /* set charge current in mA (platform data must provide resistor sense) */ | |
629 | static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA) | |
630 | { | |
631 | int val; | |
6c47a3e0 | 632 | |
0bc98cc6 PR |
633 | if (bq->init_data.resistor_sense <= 0) |
634 | return -ENOSYS; | |
635 | ||
636 | val = (mA * bq->init_data.resistor_sense - 37400) / 6800; | |
0bc98cc6 PR |
637 | 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) */ | |
648 | static int bq2415x_get_charge_current(struct bq2415x_device *bq) | |
649 | { | |
650 | int ret; | |
6c47a3e0 | 651 | |
0bc98cc6 PR |
652 | 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; | |
6c47a3e0 | 659 | return (37400 + 6800*ret) / bq->init_data.resistor_sense; |
0bc98cc6 PR |
660 | } |
661 | ||
662 | /* set termination current in mA (platform data must provide resistor sense) */ | |
663 | static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA) | |
664 | { | |
665 | int val; | |
6c47a3e0 | 666 | |
0bc98cc6 PR |
667 | if (bq->init_data.resistor_sense <= 0) |
668 | return -ENOSYS; | |
669 | ||
670 | val = (mA * bq->init_data.resistor_sense - 3400) / 3400; | |
0bc98cc6 PR |
671 | 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) */ | |
682 | static int bq2415x_get_termination_current(struct bq2415x_device *bq) | |
683 | { | |
684 | int ret; | |
6c47a3e0 | 685 | |
0bc98cc6 PR |
686 | 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; | |
6c47a3e0 | 693 | return (3400 + 3400*ret) / bq->init_data.resistor_sense; |
0bc98cc6 PR |
694 | } |
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 */ | |
707 | static 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); | |
6c47a3e0 | 712 | |
0bc98cc6 PR |
713 | 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); | |
6c47a3e0 | 716 | |
0bc98cc6 PR |
717 | 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 | } | |
6c47a3e0 | 722 | |
0bc98cc6 PR |
723 | bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE); |
724 | return 0; | |
725 | } | |
726 | ||
727 | /**** charger mode functions ****/ | |
728 | ||
729 | /* set charger mode */ | |
730 | static 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 | ||
0bc98cc6 PR |
736 | if (mode == BQ2415X_MODE_BOOST) |
737 | boost = 1; | |
ac6324e7 PR |
738 | else if (mode != BQ2415X_MODE_OFF) |
739 | charger = 1; | |
0bc98cc6 PR |
740 | |
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) { | |
ac6324e7 PR |
751 | case BQ2415X_MODE_OFF: |
752 | dev_dbg(bq->dev, "changing mode to: Offline\n"); | |
753 | ret = bq2415x_set_current_limit(bq, 100); | |
754 | break; | |
0bc98cc6 PR |
755 | 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; | |
297d716f | 788 | sysfs_notify(&bq->charger->dev.kobj, NULL, "mode"); |
0bc98cc6 PR |
789 | |
790 | return 0; | |
791 | ||
792 | } | |
793 | ||
32260308 PR |
794 | static int bq2415x_notifier_call(struct notifier_block *nb, |
795 | unsigned long val, void *v) | |
0bc98cc6 | 796 | { |
32260308 PR |
797 | 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; | |
0bc98cc6 | 804 | |
32260308 PR |
805 | if (val != PSY_EVENT_PROP_CHANGED) |
806 | return NOTIFY_OK; | |
807 | ||
faffd234 | 808 | if (psy != bq->notify_psy) |
32260308 PR |
809 | return NOTIFY_OK; |
810 | ||
811 | dev_dbg(bq->dev, "notifier call was called\n"); | |
812 | ||
d7bdffb9 KK |
813 | ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX, |
814 | &prop); | |
32260308 PR |
815 | 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; | |
0bc98cc6 | 831 | |
0bc98cc6 PR |
832 | bq->reported_mode = mode; |
833 | ||
834 | /* if automode is not enabled do not tell about reported_mode */ | |
835 | if (bq->automode < 1) | |
32260308 | 836 | return NOTIFY_OK; |
0bc98cc6 | 837 | |
3c018504 | 838 | schedule_delayed_work(&bq->work, 0); |
0bc98cc6 | 839 | |
32260308 | 840 | return NOTIFY_OK; |
0bc98cc6 PR |
841 | } |
842 | ||
843 | /**** timer functions ****/ | |
844 | ||
845 | /* enable/disable auto resetting chip timer */ | |
846 | static 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 */ | |
869 | static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg) | |
870 | { | |
871 | bq->timer_error = msg; | |
297d716f | 872 | sysfs_notify(&bq->charger->dev.kobj, NULL, "timer"); |
0bc98cc6 PR |
873 | dev_err(bq->dev, "%s\n", msg); |
874 | if (bq->automode > 0) | |
875 | bq->automode = 0; | |
ac6324e7 | 876 | bq2415x_set_mode(bq, BQ2415X_MODE_OFF); |
0bc98cc6 PR |
877 | bq2415x_set_autotimer(bq, 0); |
878 | } | |
879 | ||
880 | /* delayed work function for auto resetting chip timer */ | |
881 | static void bq2415x_timer_work(struct work_struct *work) | |
882 | { | |
883 | struct bq2415x_device *bq = container_of(work, struct bq2415x_device, | |
6c47a3e0 AV |
884 | work.work); |
885 | int ret; | |
886 | int error; | |
887 | int boost; | |
0bc98cc6 | 888 | |
3c018504 | 889 | if (bq->automode > 0 && (bq->reported_mode != bq->mode)) { |
297d716f | 890 | sysfs_notify(&bq->charger->dev.kobj, NULL, "reported_mode"); |
3c018504 SR |
891 | bq2415x_set_mode(bq, bq->reported_mode); |
892 | } | |
893 | ||
0bc98cc6 PR |
894 | 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 | ||
986 | static 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 | ||
992 | static int bq2415x_power_supply_get_property(struct power_supply *psy, | |
6c47a3e0 AV |
993 | enum power_supply_property psp, |
994 | union power_supply_propval *val) | |
0bc98cc6 | 995 | { |
297d716f | 996 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
997 | 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 | ||
1022 | static int bq2415x_power_supply_init(struct bq2415x_device *bq) | |
1023 | { | |
1024 | int ret; | |
1025 | int chip; | |
1026 | char revstr[8]; | |
297d716f | 1027 | struct power_supply_config psy_cfg = { .drv_data = bq, }; |
0bc98cc6 | 1028 | |
297d716f KK |
1029 | 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; | |
0bc98cc6 PR |
1035 | |
1036 | ret = bq2415x_detect_chip(bq); | |
1037 | if (ret < 0) | |
1038 | chip = BQUNKNOWN; | |
1039 | else | |
1040 | chip = ret; | |
1041 | ||
1042 | ret = bq2415x_detect_revision(bq); | |
1043 | if (ret < 0) | |
1044 | strcpy(revstr, "unknown"); | |
1045 | else | |
1046 | sprintf(revstr, "1.%d", ret); | |
1047 | ||
1048 | bq->model = kasprintf(GFP_KERNEL, | |
1049 | "chip %s, revision %s, vender code %.3d", | |
1050 | bq2415x_chip_name[chip], revstr, | |
1051 | bq2415x_get_vender_code(bq)); | |
1052 | if (!bq->model) { | |
1053 | dev_err(bq->dev, "failed to allocate model name\n"); | |
1054 | return -ENOMEM; | |
1055 | } | |
1056 | ||
297d716f KK |
1057 | bq->charger = power_supply_register(bq->dev, &bq->charger_desc, |
1058 | &psy_cfg); | |
1059 | if (IS_ERR(bq->charger)) { | |
0bc98cc6 | 1060 | kfree(bq->model); |
297d716f | 1061 | return PTR_ERR(bq->charger); |
0bc98cc6 PR |
1062 | } |
1063 | ||
1064 | return 0; | |
1065 | } | |
1066 | ||
1067 | static 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); | |
297d716f | 1073 | power_supply_unregister(bq->charger); |
0bc98cc6 PR |
1074 | kfree(bq->model); |
1075 | } | |
1076 | ||
1077 | /**** additional sysfs entries for power supply interface ****/ | |
1078 | ||
1079 | /* show *_status entries */ | |
1080 | static ssize_t bq2415x_sysfs_show_status(struct device *dev, | |
6c47a3e0 AV |
1081 | struct device_attribute *attr, |
1082 | char *buf) | |
0bc98cc6 PR |
1083 | { |
1084 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1085 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1086 | enum bq2415x_command command; |
1087 | int ret; | |
1088 | ||
1089 | if (strcmp(attr->attr.name, "otg_status") == 0) | |
1090 | command = BQ2415X_OTG_STATUS; | |
1091 | else if (strcmp(attr->attr.name, "charge_status") == 0) | |
1092 | command = BQ2415X_CHARGE_STATUS; | |
1093 | else if (strcmp(attr->attr.name, "boost_status") == 0) | |
1094 | command = BQ2415X_BOOST_STATUS; | |
1095 | else if (strcmp(attr->attr.name, "fault_status") == 0) | |
1096 | command = BQ2415X_FAULT_STATUS; | |
1097 | else | |
1098 | return -EINVAL; | |
1099 | ||
1100 | ret = bq2415x_exec_command(bq, command); | |
1101 | if (ret < 0) | |
1102 | return ret; | |
6c47a3e0 | 1103 | return sprintf(buf, "%d\n", ret); |
0bc98cc6 PR |
1104 | } |
1105 | ||
6c47a3e0 AV |
1106 | /* |
1107 | * set timer entry: | |
1108 | * auto - enable auto mode | |
1109 | * off - disable auto mode | |
1110 | * (other values) - reset chip timer | |
1111 | */ | |
0bc98cc6 | 1112 | static ssize_t bq2415x_sysfs_set_timer(struct device *dev, |
6c47a3e0 AV |
1113 | struct device_attribute *attr, |
1114 | const char *buf, | |
1115 | size_t count) | |
0bc98cc6 PR |
1116 | { |
1117 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1118 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1119 | 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; | |
6c47a3e0 | 1130 | return count; |
0bc98cc6 PR |
1131 | } |
1132 | ||
1133 | /* show timer entry (auto or off) */ | |
1134 | static ssize_t bq2415x_sysfs_show_timer(struct device *dev, | |
6c47a3e0 AV |
1135 | struct device_attribute *attr, |
1136 | char *buf) | |
0bc98cc6 PR |
1137 | { |
1138 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1139 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1140 | |
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"); | |
6c47a3e0 | 1146 | return sprintf(buf, "off\n"); |
0bc98cc6 PR |
1147 | } |
1148 | ||
6c47a3e0 AV |
1149 | /* |
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 | */ | |
0bc98cc6 | 1157 | static ssize_t bq2415x_sysfs_set_mode(struct device *dev, |
6c47a3e0 AV |
1158 | struct device_attribute *attr, |
1159 | const char *buf, | |
1160 | size_t count) | |
0bc98cc6 PR |
1161 | { |
1162 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1163 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1164 | 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; | |
ac6324e7 PR |
1172 | } else if (strncmp(buf, "off", 3) == 0) { |
1173 | if (bq->automode > 0) | |
1174 | bq->automode = 0; | |
1175 | mode = BQ2415X_MODE_OFF; | |
0bc98cc6 PR |
1176 | } 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; | |
6c47a3e0 | 1199 | } else { |
0bc98cc6 | 1200 | return -EINVAL; |
6c47a3e0 | 1201 | } |
0bc98cc6 PR |
1202 | |
1203 | ret = bq2415x_set_mode(bq, mode); | |
1204 | if (ret < 0) | |
1205 | return ret; | |
6c47a3e0 | 1206 | return count; |
0bc98cc6 PR |
1207 | } |
1208 | ||
1209 | /* show mode entry (auto, none, host, dedicated or boost) */ | |
1210 | static ssize_t bq2415x_sysfs_show_mode(struct device *dev, | |
6c47a3e0 AV |
1211 | struct device_attribute *attr, |
1212 | char *buf) | |
0bc98cc6 PR |
1213 | { |
1214 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1215 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1216 | ssize_t ret = 0; |
1217 | ||
1218 | if (bq->automode > 0) | |
1219 | ret += sprintf(buf+ret, "auto ("); | |
1220 | ||
1221 | switch (bq->mode) { | |
ac6324e7 PR |
1222 | case BQ2415X_MODE_OFF: |
1223 | ret += sprintf(buf+ret, "off"); | |
1224 | break; | |
0bc98cc6 PR |
1225 | 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) */ | |
1247 | static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev, | |
6c47a3e0 AV |
1248 | struct device_attribute *attr, |
1249 | char *buf) | |
0bc98cc6 PR |
1250 | { |
1251 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1252 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1253 | |
1254 | if (bq->automode < 0) | |
1255 | return -EINVAL; | |
1256 | ||
1257 | switch (bq->reported_mode) { | |
ac6324e7 PR |
1258 | case BQ2415X_MODE_OFF: |
1259 | return sprintf(buf, "off\n"); | |
0bc98cc6 PR |
1260 | 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' */ | |
1274 | static ssize_t bq2415x_sysfs_set_registers(struct device *dev, | |
6c47a3e0 AV |
1275 | struct device_attribute *attr, |
1276 | const char *buf, | |
1277 | size_t count) | |
0bc98cc6 PR |
1278 | { |
1279 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1280 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1281 | ssize_t ret = 0; |
1282 | unsigned int reg; | |
1283 | unsigned int val; | |
1284 | ||
1285 | if (sscanf(buf, "%x %x", ®, &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; | |
6c47a3e0 | 1294 | return count; |
0bc98cc6 PR |
1295 | } |
1296 | ||
1297 | /* print value of chip register, format: 'register=value' */ | |
1298 | static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq, | |
6c47a3e0 AV |
1299 | u8 reg, |
1300 | char *buf) | |
0bc98cc6 PR |
1301 | { |
1302 | int ret = bq2415x_i2c_read(bq, reg); | |
6c47a3e0 | 1303 | |
0bc98cc6 PR |
1304 | if (ret < 0) |
1305 | return sprintf(buf, "%#.2x=error %d\n", reg, ret); | |
6c47a3e0 | 1306 | return sprintf(buf, "%#.2x=%#.2x\n", reg, ret); |
0bc98cc6 PR |
1307 | } |
1308 | ||
1309 | /* show all raw values of chip register, format per line: 'register=value' */ | |
1310 | static ssize_t bq2415x_sysfs_show_registers(struct device *dev, | |
6c47a3e0 AV |
1311 | struct device_attribute *attr, |
1312 | char *buf) | |
0bc98cc6 PR |
1313 | { |
1314 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1315 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1316 | 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) */ | |
1327 | static ssize_t bq2415x_sysfs_set_limit(struct device *dev, | |
6c47a3e0 AV |
1328 | struct device_attribute *attr, |
1329 | const char *buf, | |
1330 | size_t count) | |
0bc98cc6 PR |
1331 | { |
1332 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1333 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1334 | 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; | |
6c47a3e0 | 1355 | return count; |
0bc98cc6 PR |
1356 | } |
1357 | ||
1358 | /* show current and voltage limit entries (in mA or mV) */ | |
1359 | static ssize_t bq2415x_sysfs_show_limit(struct device *dev, | |
6c47a3e0 AV |
1360 | struct device_attribute *attr, |
1361 | char *buf) | |
0bc98cc6 PR |
1362 | { |
1363 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1364 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1365 | 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; | |
6c47a3e0 | 1382 | return sprintf(buf, "%d\n", ret); |
0bc98cc6 PR |
1383 | } |
1384 | ||
1385 | /* set *_enable entries */ | |
1386 | static ssize_t bq2415x_sysfs_set_enable(struct device *dev, | |
6c47a3e0 AV |
1387 | struct device_attribute *attr, |
1388 | const char *buf, | |
1389 | size_t count) | |
0bc98cc6 PR |
1390 | { |
1391 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1392 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1393 | 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; | |
6c47a3e0 | 1418 | return count; |
0bc98cc6 PR |
1419 | } |
1420 | ||
1421 | /* show *_enable entries */ | |
1422 | static ssize_t bq2415x_sysfs_show_enable(struct device *dev, | |
6c47a3e0 AV |
1423 | struct device_attribute *attr, |
1424 | char *buf) | |
0bc98cc6 PR |
1425 | { |
1426 | struct power_supply *psy = dev_get_drvdata(dev); | |
297d716f | 1427 | struct bq2415x_device *bq = power_supply_get_drvdata(psy); |
0bc98cc6 PR |
1428 | 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; | |
6c47a3e0 | 1445 | return sprintf(buf, "%d\n", ret); |
0bc98cc6 PR |
1446 | } |
1447 | ||
1448 | static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO, | |
1449 | bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); | |
1450 | static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO, | |
1451 | bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); | |
1452 | static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO, | |
1453 | bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); | |
1454 | static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO, | |
1455 | bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); | |
1456 | static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO, | |
1457 | bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit); | |
1458 | ||
1459 | static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO, | |
1460 | bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); | |
1461 | static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO, | |
1462 | bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); | |
1463 | static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO, | |
1464 | bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); | |
1465 | static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO, | |
1466 | bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable); | |
1467 | ||
1468 | static DEVICE_ATTR(reported_mode, S_IRUGO, | |
1469 | bq2415x_sysfs_show_reported_mode, NULL); | |
1470 | static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO, | |
1471 | bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode); | |
1472 | static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO, | |
1473 | bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer); | |
1474 | ||
1475 | static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO, | |
1476 | bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers); | |
1477 | ||
1478 | static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); | |
1479 | static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); | |
1480 | static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); | |
1481 | static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL); | |
1482 | ||
1483 | static struct attribute *bq2415x_sysfs_attributes[] = { | |
6c47a3e0 AV |
1484 | /* |
1485 | * TODO: some (appropriate) of these attrs should be switched to | |
1486 | * use power supply class props. | |
1487 | */ | |
0bc98cc6 PR |
1488 | &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 | ||
1512 | static const struct attribute_group bq2415x_sysfs_attr_group = { | |
1513 | .attrs = bq2415x_sysfs_attributes, | |
1514 | }; | |
1515 | ||
1516 | static int bq2415x_sysfs_init(struct bq2415x_device *bq) | |
1517 | { | |
297d716f | 1518 | return sysfs_create_group(&bq->charger->dev.kobj, |
0bc98cc6 PR |
1519 | &bq2415x_sysfs_attr_group); |
1520 | } | |
1521 | ||
1522 | static void bq2415x_sysfs_exit(struct bq2415x_device *bq) | |
1523 | { | |
297d716f | 1524 | sysfs_remove_group(&bq->charger->dev.kobj, &bq2415x_sysfs_attr_group); |
0bc98cc6 PR |
1525 | } |
1526 | ||
1527 | /* main bq2415x probe function */ | |
1528 | static int bq2415x_probe(struct i2c_client *client, | |
6c47a3e0 | 1529 | const struct i2c_device_id *id) |
0bc98cc6 PR |
1530 | { |
1531 | int ret; | |
1532 | int num; | |
1533 | char *name; | |
1534 | struct bq2415x_device *bq; | |
faffd234 SR |
1535 | struct device_node *np = client->dev.of_node; |
1536 | struct bq2415x_platform_data *pdata = client->dev.platform_data; | |
0bc98cc6 | 1537 | |
faffd234 SR |
1538 | if (!np && !pdata) { |
1539 | dev_err(&client->dev, "platform data missing\n"); | |
0bc98cc6 PR |
1540 | return -ENODEV; |
1541 | } | |
1542 | ||
1543 | /* Get new ID for the new device */ | |
0bc98cc6 | 1544 | mutex_lock(&bq2415x_id_mutex); |
05e2cefa | 1545 | num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL); |
0bc98cc6 | 1546 | mutex_unlock(&bq2415x_id_mutex); |
05e2cefa TH |
1547 | if (num < 0) |
1548 | return num; | |
0bc98cc6 PR |
1549 | |
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 | ||
a9672e13 | 1557 | bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL); |
0bc98cc6 PR |
1558 | if (!bq) { |
1559 | dev_err(&client->dev, "failed to allocate device data\n"); | |
1560 | ret = -ENOMEM; | |
1561 | goto error_2; | |
1562 | } | |
1563 | ||
faffd234 SR |
1564 | if (np) { |
1565 | bq->notify_psy = power_supply_get_by_phandle(np, "ti,usb-charger-detection"); | |
1566 | ||
0eaf437a KK |
1567 | 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 | } | |
faffd234 SR |
1576 | } |
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 | ||
0bc98cc6 PR |
1582 | 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; | |
ac6324e7 PR |
1588 | bq->mode = BQ2415X_MODE_OFF; |
1589 | bq->reported_mode = BQ2415X_MODE_OFF; | |
0bc98cc6 PR |
1590 | bq->autotimer = 0; |
1591 | bq->automode = 0; | |
1592 | ||
faffd234 SR |
1593 | if (np) { |
1594 | ret = of_property_read_u32(np, "ti,current-limit", | |
1595 | &bq->init_data.current_limit); | |
1596 | if (ret) | |
21e863b2 | 1597 | goto error_2; |
faffd234 SR |
1598 | ret = of_property_read_u32(np, "ti,weak-battery-voltage", |
1599 | &bq->init_data.weak_battery_voltage); | |
1600 | if (ret) | |
21e863b2 | 1601 | goto error_2; |
faffd234 SR |
1602 | ret = of_property_read_u32(np, "ti,battery-regulation-voltage", |
1603 | &bq->init_data.battery_regulation_voltage); | |
1604 | if (ret) | |
21e863b2 | 1605 | goto error_2; |
faffd234 SR |
1606 | ret = of_property_read_u32(np, "ti,charge-current", |
1607 | &bq->init_data.charge_current); | |
1608 | if (ret) | |
21e863b2 | 1609 | goto error_2; |
faffd234 SR |
1610 | ret = of_property_read_u32(np, "ti,termination-current", |
1611 | &bq->init_data.termination_current); | |
1612 | if (ret) | |
21e863b2 | 1613 | goto error_2; |
faffd234 SR |
1614 | ret = of_property_read_u32(np, "ti,resistor-sense", |
1615 | &bq->init_data.resistor_sense); | |
1616 | if (ret) | |
21e863b2 | 1617 | goto error_2; |
faffd234 SR |
1618 | } else { |
1619 | memcpy(&bq->init_data, pdata, sizeof(bq->init_data)); | |
1620 | } | |
0bc98cc6 PR |
1621 | |
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); | |
a9672e13 | 1627 | goto error_2; |
0bc98cc6 PR |
1628 | } |
1629 | ||
1630 | ret = bq2415x_sysfs_init(bq); | |
1631 | if (ret) { | |
1632 | dev_err(bq->dev, "failed to create sysfs entries: %d\n", ret); | |
a9672e13 | 1633 | goto error_3; |
0bc98cc6 PR |
1634 | } |
1635 | ||
1636 | ret = bq2415x_set_defaults(bq); | |
1637 | if (ret) { | |
1638 | dev_err(bq->dev, "failed to set default values: %d\n", ret); | |
a9672e13 | 1639 | goto error_4; |
0bc98cc6 PR |
1640 | } |
1641 | ||
faffd234 | 1642 | if (bq->notify_psy) { |
32260308 PR |
1643 | 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); | |
1647 | goto error_5; | |
1648 | } | |
faffd234 SR |
1649 | |
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 | ||
32260308 PR |
1654 | bq->automode = 1; |
1655 | dev_info(bq->dev, "automode enabled\n"); | |
0bc98cc6 PR |
1656 | } 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 | ||
32260308 | 1667 | error_5: |
0bc98cc6 | 1668 | error_4: |
a9672e13 | 1669 | bq2415x_sysfs_exit(bq); |
0bc98cc6 | 1670 | error_3: |
a9672e13 | 1671 | bq2415x_power_supply_exit(bq); |
0bc98cc6 PR |
1672 | error_2: |
1673 | kfree(name); | |
1674 | error_1: | |
1675 | mutex_lock(&bq2415x_id_mutex); | |
1676 | idr_remove(&bq2415x_id, num); | |
1677 | mutex_unlock(&bq2415x_id_mutex); | |
1678 | ||
1679 | return ret; | |
1680 | } | |
1681 | ||
1682 | /* main bq2415x remove function */ | |
1683 | ||
1684 | static int bq2415x_remove(struct i2c_client *client) | |
1685 | { | |
1686 | struct bq2415x_device *bq = i2c_get_clientdata(client); | |
1687 | ||
faffd234 | 1688 | if (bq->notify_psy) |
32260308 | 1689 | power_supply_unreg_notifier(&bq->nb); |
0bc98cc6 PR |
1690 | |
1691 | bq2415x_sysfs_exit(bq); | |
1692 | bq2415x_power_supply_exit(bq); | |
1693 | ||
1694 | bq2415x_reset_chip(bq); | |
1695 | ||
1696 | mutex_lock(&bq2415x_id_mutex); | |
1697 | idr_remove(&bq2415x_id, bq->id); | |
1698 | mutex_unlock(&bq2415x_id_mutex); | |
1699 | ||
1700 | dev_info(bq->dev, "driver unregistered\n"); | |
1701 | ||
1702 | kfree(bq->name); | |
0bc98cc6 PR |
1703 | |
1704 | return 0; | |
1705 | } | |
1706 | ||
1707 | static const struct i2c_device_id bq2415x_i2c_id_table[] = { | |
1708 | { "bq2415x", BQUNKNOWN }, | |
1709 | { "bq24150", BQ24150 }, | |
1710 | { "bq24150a", BQ24150A }, | |
1711 | { "bq24151", BQ24151 }, | |
1712 | { "bq24151a", BQ24151A }, | |
1713 | { "bq24152", BQ24152 }, | |
1714 | { "bq24153", BQ24153 }, | |
1715 | { "bq24153a", BQ24153A }, | |
1716 | { "bq24155", BQ24155 }, | |
1717 | { "bq24156", BQ24156 }, | |
1718 | { "bq24156a", BQ24156A }, | |
0e1392d9 | 1719 | { "bq24157s", BQ24157S }, |
0bc98cc6 PR |
1720 | { "bq24158", BQ24158 }, |
1721 | {}, | |
1722 | }; | |
1723 | MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table); | |
1724 | ||
1725 | static struct i2c_driver bq2415x_driver = { | |
1726 | .driver = { | |
1727 | .name = "bq2415x-charger", | |
1728 | }, | |
1729 | .probe = bq2415x_probe, | |
1730 | .remove = bq2415x_remove, | |
1731 | .id_table = bq2415x_i2c_id_table, | |
1732 | }; | |
463df514 | 1733 | module_i2c_driver(bq2415x_driver); |
0bc98cc6 PR |
1734 | |
1735 | MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>"); | |
1736 | MODULE_DESCRIPTION("bq2415x charger driver"); | |
1737 | MODULE_LICENSE("GPL"); |