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