Blame view

drivers/power/supply/bq2415x_charger.c 46.3 KB
0bc98cc61   Pali Rohár   power_supply: Add...
1
  /*
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
2
3
   * bq2415x charger driver
   *
32260308b   Pali Rohár   bq2415x_charger: ...
4
   * Copyright (C) 2011-2013  Pali Rohár <pali.rohar@gmail.com>
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
5
6
7
8
9
10
11
12
13
14
15
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
16
17
18
19
20
21
22
   * Datasheets:
   * http://www.ti.com/product/bq24150
   * http://www.ti.com/product/bq24150a
   * http://www.ti.com/product/bq24152
   * http://www.ti.com/product/bq24153
   * http://www.ti.com/product/bq24153a
   * http://www.ti.com/product/bq24155
0e1392d9d   Anda-Maria Nicolae   bq2415x_charger: ...
23
24
   * http://www.ti.com/product/bq24157s
   * http://www.ti.com/product/bq24158
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
25
   */
0bc98cc61   Pali Rohár   power_supply: Add...
26

0bc98cc61   Pali Rohár   power_supply: Add...
27
28
29
30
31
32
33
34
35
36
37
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/param.h>
  #include <linux/err.h>
  #include <linux/workqueue.h>
  #include <linux/sysfs.h>
  #include <linux/platform_device.h>
  #include <linux/power_supply.h>
  #include <linux/idr.h>
  #include <linux/i2c.h>
  #include <linux/slab.h>
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
38
  #include <linux/acpi.h>
0bc98cc61   Pali Rohár   power_supply: Add...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  
  #include <linux/power/bq2415x_charger.h>
  
  /* timeout for resetting chip timer */
  #define BQ2415X_TIMER_TIMEOUT		10
  
  #define BQ2415X_REG_STATUS		0x00
  #define BQ2415X_REG_CONTROL		0x01
  #define BQ2415X_REG_VOLTAGE		0x02
  #define BQ2415X_REG_VENDER		0x03
  #define BQ2415X_REG_CURRENT		0x04
  
  /* reset state for all registers */
  #define BQ2415X_RESET_STATUS		BIT(6)
  #define BQ2415X_RESET_CONTROL		(BIT(4)|BIT(5))
  #define BQ2415X_RESET_VOLTAGE		(BIT(1)|BIT(3))
  #define BQ2415X_RESET_CURRENT		(BIT(0)|BIT(3)|BIT(7))
  
  /* status register */
  #define BQ2415X_BIT_TMR_RST		7
  #define BQ2415X_BIT_OTG			7
  #define BQ2415X_BIT_EN_STAT		6
  #define BQ2415X_MASK_STAT		(BIT(4)|BIT(5))
  #define BQ2415X_SHIFT_STAT		4
  #define BQ2415X_BIT_BOOST		3
  #define BQ2415X_MASK_FAULT		(BIT(0)|BIT(1)|BIT(2))
  #define BQ2415X_SHIFT_FAULT		0
  
  /* control register */
  #define BQ2415X_MASK_LIMIT		(BIT(6)|BIT(7))
  #define BQ2415X_SHIFT_LIMIT		6
  #define BQ2415X_MASK_VLOWV		(BIT(4)|BIT(5))
  #define BQ2415X_SHIFT_VLOWV		4
  #define BQ2415X_BIT_TE			3
  #define BQ2415X_BIT_CE			2
  #define BQ2415X_BIT_HZ_MODE		1
  #define BQ2415X_BIT_OPA_MODE		0
  
  /* voltage register */
  #define BQ2415X_MASK_VO		(BIT(2)|BIT(3)|BIT(4)|BIT(5)|BIT(6)|BIT(7))
  #define BQ2415X_SHIFT_VO		2
  #define BQ2415X_BIT_OTG_PL		1
  #define BQ2415X_BIT_OTG_EN		0
  
  /* vender register */
  #define BQ2415X_MASK_VENDER		(BIT(5)|BIT(6)|BIT(7))
  #define BQ2415X_SHIFT_VENDER		5
  #define BQ2415X_MASK_PN			(BIT(3)|BIT(4))
  #define BQ2415X_SHIFT_PN		3
  #define BQ2415X_MASK_REVISION		(BIT(0)|BIT(1)|BIT(2))
  #define BQ2415X_SHIFT_REVISION		0
  
  /* current register */
  #define BQ2415X_MASK_RESET		BIT(7)
  #define BQ2415X_MASK_VI_CHRG		(BIT(4)|BIT(5)|BIT(6))
  #define BQ2415X_SHIFT_VI_CHRG		4
  /* N/A					BIT(3) */
  #define BQ2415X_MASK_VI_TERM		(BIT(0)|BIT(1)|BIT(2))
  #define BQ2415X_SHIFT_VI_TERM		0
  
  
  enum bq2415x_command {
  	BQ2415X_TIMER_RESET,
  	BQ2415X_OTG_STATUS,
  	BQ2415X_STAT_PIN_STATUS,
  	BQ2415X_STAT_PIN_ENABLE,
  	BQ2415X_STAT_PIN_DISABLE,
  	BQ2415X_CHARGE_STATUS,
  	BQ2415X_BOOST_STATUS,
  	BQ2415X_FAULT_STATUS,
  
  	BQ2415X_CHARGE_TERMINATION_STATUS,
  	BQ2415X_CHARGE_TERMINATION_ENABLE,
  	BQ2415X_CHARGE_TERMINATION_DISABLE,
  	BQ2415X_CHARGER_STATUS,
  	BQ2415X_CHARGER_ENABLE,
  	BQ2415X_CHARGER_DISABLE,
  	BQ2415X_HIGH_IMPEDANCE_STATUS,
  	BQ2415X_HIGH_IMPEDANCE_ENABLE,
  	BQ2415X_HIGH_IMPEDANCE_DISABLE,
  	BQ2415X_BOOST_MODE_STATUS,
  	BQ2415X_BOOST_MODE_ENABLE,
  	BQ2415X_BOOST_MODE_DISABLE,
  
  	BQ2415X_OTG_LEVEL,
  	BQ2415X_OTG_ACTIVATE_HIGH,
  	BQ2415X_OTG_ACTIVATE_LOW,
  	BQ2415X_OTG_PIN_STATUS,
  	BQ2415X_OTG_PIN_ENABLE,
  	BQ2415X_OTG_PIN_DISABLE,
  
  	BQ2415X_VENDER_CODE,
  	BQ2415X_PART_NUMBER,
  	BQ2415X_REVISION,
  };
  
  enum bq2415x_chip {
  	BQUNKNOWN,
  	BQ24150,
  	BQ24150A,
  	BQ24151,
  	BQ24151A,
  	BQ24152,
  	BQ24153,
  	BQ24153A,
  	BQ24155,
  	BQ24156,
  	BQ24156A,
0e1392d9d   Anda-Maria Nicolae   bq2415x_charger: ...
147
  	BQ24157S,
0bc98cc61   Pali Rohár   power_supply: Add...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  	BQ24158,
  };
  
  static char *bq2415x_chip_name[] = {
  	"unknown",
  	"bq24150",
  	"bq24150a",
  	"bq24151",
  	"bq24151a",
  	"bq24152",
  	"bq24153",
  	"bq24153a",
  	"bq24155",
  	"bq24156",
  	"bq24156a",
0e1392d9d   Anda-Maria Nicolae   bq2415x_charger: ...
163
  	"bq24157s",
0bc98cc61   Pali Rohár   power_supply: Add...
164
165
166
167
168
169
  	"bq24158",
  };
  
  struct bq2415x_device {
  	struct device *dev;
  	struct bq2415x_platform_data init_data;
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
170
171
  	struct power_supply *charger;
  	struct power_supply_desc charger_desc;
0bc98cc61   Pali Rohár   power_supply: Add...
172
  	struct delayed_work work;
b68c31614   Pali Rohár   bq2415x_charger: ...
173
  	struct device_node *notify_node;
32260308b   Pali Rohár   bq2415x_charger: ...
174
  	struct notifier_block nb;
0bc98cc61   Pali Rohár   power_supply: Add...
175
  	enum bq2415x_mode reported_mode;/* mode reported by hook function */
881f985a2   Pavel Machek   bq2415x_charger, ...
176
  	enum bq2415x_mode mode;		/* currently configured mode */
0bc98cc61   Pali Rohár   power_supply: Add...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  	enum bq2415x_chip chip;
  	const char *timer_error;
  	char *model;
  	char *name;
  	int autotimer;	/* 1 - if driver automatically reset timer, 0 - not */
  	int automode;	/* 1 - enabled, 0 - disabled; -1 - not supported */
  	int id;
  };
  
  /* each registered chip must have unique id */
  static DEFINE_IDR(bq2415x_id);
  
  static DEFINE_MUTEX(bq2415x_id_mutex);
  static DEFINE_MUTEX(bq2415x_timer_mutex);
  static DEFINE_MUTEX(bq2415x_i2c_mutex);
  
  /**** i2c read functions ****/
  
  /* read value from register */
  static int bq2415x_i2c_read(struct bq2415x_device *bq, u8 reg)
  {
  	struct i2c_client *client = to_i2c_client(bq->dev);
  	struct i2c_msg msg[2];
  	u8 val;
  	int ret;
  
  	if (!client->adapter)
  		return -ENODEV;
  
  	msg[0].addr = client->addr;
  	msg[0].flags = 0;
  	msg[0].buf = &reg;
  	msg[0].len = sizeof(reg);
  	msg[1].addr = client->addr;
  	msg[1].flags = I2C_M_RD;
  	msg[1].buf = &val;
  	msg[1].len = sizeof(val);
  
  	mutex_lock(&bq2415x_i2c_mutex);
  	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  	mutex_unlock(&bq2415x_i2c_mutex);
  
  	if (ret < 0)
  		return ret;
  
  	return val;
  }
  
  /* read value from register, apply mask and right shift it */
  static int bq2415x_i2c_read_mask(struct bq2415x_device *bq, u8 reg,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
227
  				 u8 mask, u8 shift)
0bc98cc61   Pali Rohár   power_supply: Add...
228
229
230
231
232
233
234
235
236
  {
  	int ret;
  
  	if (shift > 8)
  		return -EINVAL;
  
  	ret = bq2415x_i2c_read(bq, reg);
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
237
  	return (ret & mask) >> shift;
0bc98cc61   Pali Rohár   power_supply: Add...
238
239
240
241
242
243
244
  }
  
  /* read value from register and return one specified bit */
  static int bq2415x_i2c_read_bit(struct bq2415x_device *bq, u8 reg, u8 bit)
  {
  	if (bit > 8)
  		return -EINVAL;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
245
  	return bq2415x_i2c_read_mask(bq, reg, BIT(bit), bit);
0bc98cc61   Pali Rohár   power_supply: Add...
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  }
  
  /**** i2c write functions ****/
  
  /* write value to register */
  static int bq2415x_i2c_write(struct bq2415x_device *bq, u8 reg, u8 val)
  {
  	struct i2c_client *client = to_i2c_client(bq->dev);
  	struct i2c_msg msg[1];
  	u8 data[2];
  	int ret;
  
  	data[0] = reg;
  	data[1] = val;
  
  	msg[0].addr = client->addr;
  	msg[0].flags = 0;
  	msg[0].buf = data;
  	msg[0].len = ARRAY_SIZE(data);
  
  	mutex_lock(&bq2415x_i2c_mutex);
  	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
  	mutex_unlock(&bq2415x_i2c_mutex);
  
  	/* i2c_transfer returns number of messages transferred */
  	if (ret < 0)
  		return ret;
  	else if (ret != 1)
  		return -EIO;
  
  	return 0;
  }
  
  /* read value from register, change it with mask left shifted and write back */
  static int bq2415x_i2c_write_mask(struct bq2415x_device *bq, u8 reg, u8 val,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
281
  				  u8 mask, u8 shift)
0bc98cc61   Pali Rohár   power_supply: Add...
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  {
  	int ret;
  
  	if (shift > 8)
  		return -EINVAL;
  
  	ret = bq2415x_i2c_read(bq, reg);
  	if (ret < 0)
  		return ret;
  
  	ret &= ~mask;
  	ret |= val << shift;
  
  	return bq2415x_i2c_write(bq, reg, ret);
  }
  
  /* change only one bit in register */
  static int bq2415x_i2c_write_bit(struct bq2415x_device *bq, u8 reg,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
300
  				 bool val, u8 bit)
0bc98cc61   Pali Rohár   power_supply: Add...
301
302
303
  {
  	if (bit > 8)
  		return -EINVAL;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
304
  	return bq2415x_i2c_write_mask(bq, reg, val, BIT(bit), bit);
0bc98cc61   Pali Rohár   power_supply: Add...
305
306
307
308
309
310
311
312
313
  }
  
  /**** global functions ****/
  
  /* exec command function */
  static int bq2415x_exec_command(struct bq2415x_device *bq,
  				enum bq2415x_command command)
  {
  	int ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
314

0bc98cc61   Pali Rohár   power_supply: Add...
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  	switch (command) {
  	case BQ2415X_TIMER_RESET:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS,
  				1, BQ2415X_BIT_TMR_RST);
  	case BQ2415X_OTG_STATUS:
  		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
  				BQ2415X_BIT_OTG);
  	case BQ2415X_STAT_PIN_STATUS:
  		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
  				BQ2415X_BIT_EN_STAT);
  	case BQ2415X_STAT_PIN_ENABLE:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 1,
  				BQ2415X_BIT_EN_STAT);
  	case BQ2415X_STAT_PIN_DISABLE:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_STATUS, 0,
  				BQ2415X_BIT_EN_STAT);
  	case BQ2415X_CHARGE_STATUS:
  		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
  				BQ2415X_MASK_STAT, BQ2415X_SHIFT_STAT);
  	case BQ2415X_BOOST_STATUS:
  		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_STATUS,
  				BQ2415X_BIT_BOOST);
  	case BQ2415X_FAULT_STATUS:
  		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_STATUS,
  			BQ2415X_MASK_FAULT, BQ2415X_SHIFT_FAULT);
  
  	case BQ2415X_CHARGE_TERMINATION_STATUS:
  		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
  				BQ2415X_BIT_TE);
  	case BQ2415X_CHARGE_TERMINATION_ENABLE:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  				1, BQ2415X_BIT_TE);
  	case BQ2415X_CHARGE_TERMINATION_DISABLE:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  				0, BQ2415X_BIT_TE);
  	case BQ2415X_CHARGER_STATUS:
  		ret = bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
  			BQ2415X_BIT_CE);
  		if (ret < 0)
  			return ret;
42d0631bb   Anda-Maria Nicolae   bq2415x_charger: ...
355
  		return ret > 0 ? 0 : 1;
0bc98cc61   Pali Rohár   power_supply: Add...
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
  	case BQ2415X_CHARGER_ENABLE:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  				0, BQ2415X_BIT_CE);
  	case BQ2415X_CHARGER_DISABLE:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  				1, BQ2415X_BIT_CE);
  	case BQ2415X_HIGH_IMPEDANCE_STATUS:
  		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
  				BQ2415X_BIT_HZ_MODE);
  	case BQ2415X_HIGH_IMPEDANCE_ENABLE:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  				1, BQ2415X_BIT_HZ_MODE);
  	case BQ2415X_HIGH_IMPEDANCE_DISABLE:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  				0, BQ2415X_BIT_HZ_MODE);
  	case BQ2415X_BOOST_MODE_STATUS:
  		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_CONTROL,
  				BQ2415X_BIT_OPA_MODE);
  	case BQ2415X_BOOST_MODE_ENABLE:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  				1, BQ2415X_BIT_OPA_MODE);
  	case BQ2415X_BOOST_MODE_DISABLE:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_CONTROL,
  				0, BQ2415X_BIT_OPA_MODE);
  
  	case BQ2415X_OTG_LEVEL:
  		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
  				BQ2415X_BIT_OTG_PL);
  	case BQ2415X_OTG_ACTIVATE_HIGH:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
  				1, BQ2415X_BIT_OTG_PL);
  	case BQ2415X_OTG_ACTIVATE_LOW:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
  				0, BQ2415X_BIT_OTG_PL);
  	case BQ2415X_OTG_PIN_STATUS:
  		return bq2415x_i2c_read_bit(bq, BQ2415X_REG_VOLTAGE,
  				BQ2415X_BIT_OTG_EN);
  	case BQ2415X_OTG_PIN_ENABLE:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
  				1, BQ2415X_BIT_OTG_EN);
  	case BQ2415X_OTG_PIN_DISABLE:
  		return bq2415x_i2c_write_bit(bq, BQ2415X_REG_VOLTAGE,
  				0, BQ2415X_BIT_OTG_EN);
  
  	case BQ2415X_VENDER_CODE:
  		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
  			BQ2415X_MASK_VENDER, BQ2415X_SHIFT_VENDER);
  	case BQ2415X_PART_NUMBER:
  		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
  				BQ2415X_MASK_PN, BQ2415X_SHIFT_PN);
  	case BQ2415X_REVISION:
  		return bq2415x_i2c_read_mask(bq, BQ2415X_REG_VENDER,
  			BQ2415X_MASK_REVISION, BQ2415X_SHIFT_REVISION);
0bc98cc61   Pali Rohár   power_supply: Add...
409
  	}
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
410
  	return -EINVAL;
0bc98cc61   Pali Rohár   power_supply: Add...
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
  }
  
  /* detect chip type */
  static enum bq2415x_chip bq2415x_detect_chip(struct bq2415x_device *bq)
  {
  	struct i2c_client *client = to_i2c_client(bq->dev);
  	int ret = bq2415x_exec_command(bq, BQ2415X_PART_NUMBER);
  
  	if (ret < 0)
  		return ret;
  
  	switch (client->addr) {
  	case 0x6b:
  		switch (ret) {
  		case 0:
  			if (bq->chip == BQ24151A)
  				return bq->chip;
42d0631bb   Anda-Maria Nicolae   bq2415x_charger: ...
428
  			return BQ24151;
0bc98cc61   Pali Rohár   power_supply: Add...
429
430
431
432
433
  		case 1:
  			if (bq->chip == BQ24150A ||
  				bq->chip == BQ24152 ||
  				bq->chip == BQ24155)
  				return bq->chip;
42d0631bb   Anda-Maria Nicolae   bq2415x_charger: ...
434
  			return BQ24150;
0bc98cc61   Pali Rohár   power_supply: Add...
435
436
437
  		case 2:
  			if (bq->chip == BQ24153A)
  				return bq->chip;
42d0631bb   Anda-Maria Nicolae   bq2415x_charger: ...
438
  			return BQ24153;
0bc98cc61   Pali Rohár   power_supply: Add...
439
440
441
442
443
444
445
446
447
448
  		default:
  			return BQUNKNOWN;
  		}
  		break;
  
  	case 0x6a:
  		switch (ret) {
  		case 0:
  			if (bq->chip == BQ24156A)
  				return bq->chip;
42d0631bb   Anda-Maria Nicolae   bq2415x_charger: ...
449
  			return BQ24156;
0bc98cc61   Pali Rohár   power_supply: Add...
450
  		case 2:
0e1392d9d   Anda-Maria Nicolae   bq2415x_charger: ...
451
452
  			if (bq->chip == BQ24157S)
  				return bq->chip;
0bc98cc61   Pali Rohár   power_supply: Add...
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
  			return BQ24158;
  		default:
  			return BQUNKNOWN;
  		}
  		break;
  	}
  
  	return BQUNKNOWN;
  }
  
  /* detect chip revision */
  static int bq2415x_detect_revision(struct bq2415x_device *bq)
  {
  	int ret = bq2415x_exec_command(bq, BQ2415X_REVISION);
  	int chip = bq2415x_detect_chip(bq);
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
468

0bc98cc61   Pali Rohár   power_supply: Add...
469
470
471
472
473
474
475
476
477
478
479
  	if (ret < 0 || chip < 0)
  		return -1;
  
  	switch (chip) {
  	case BQ24150:
  	case BQ24150A:
  	case BQ24151:
  	case BQ24151A:
  	case BQ24152:
  		if (ret >= 0 && ret <= 3)
  			return ret;
42d0631bb   Anda-Maria Nicolae   bq2415x_charger: ...
480
  		return -1;
0bc98cc61   Pali Rohár   power_supply: Add...
481
482
483
484
  	case BQ24153:
  	case BQ24153A:
  	case BQ24156:
  	case BQ24156A:
0e1392d9d   Anda-Maria Nicolae   bq2415x_charger: ...
485
  	case BQ24157S:
0bc98cc61   Pali Rohár   power_supply: Add...
486
487
488
489
490
  	case BQ24158:
  		if (ret == 3)
  			return 0;
  		else if (ret == 1)
  			return 1;
42d0631bb   Anda-Maria Nicolae   bq2415x_charger: ...
491
  		return -1;
0bc98cc61   Pali Rohár   power_supply: Add...
492
493
494
  	case BQ24155:
  		if (ret == 3)
  			return 3;
42d0631bb   Anda-Maria Nicolae   bq2415x_charger: ...
495
  		return -1;
0bc98cc61   Pali Rohár   power_supply: Add...
496
497
498
499
500
501
502
503
504
505
  	case BQUNKNOWN:
  		return -1;
  	}
  
  	return -1;
  }
  
  /* return chip vender code */
  static int bq2415x_get_vender_code(struct bq2415x_device *bq)
  {
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
506
507
508
  	int ret;
  
  	ret = bq2415x_exec_command(bq, BQ2415X_VENDER_CODE);
0bc98cc61   Pali Rohár   power_supply: Add...
509
510
  	if (ret < 0)
  		return 0;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
511
512
513
514
515
  
  	/* convert to binary */
  	return (ret & 0x1) +
  	       ((ret >> 1) & 0x1) * 10 +
  	       ((ret >> 2) & 0x1) * 100;
0bc98cc61   Pali Rohár   power_supply: Add...
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
  }
  
  /* reset all chip registers to default state */
  static void bq2415x_reset_chip(struct bq2415x_device *bq)
  {
  	bq2415x_i2c_write(bq, BQ2415X_REG_CURRENT, BQ2415X_RESET_CURRENT);
  	bq2415x_i2c_write(bq, BQ2415X_REG_VOLTAGE, BQ2415X_RESET_VOLTAGE);
  	bq2415x_i2c_write(bq, BQ2415X_REG_CONTROL, BQ2415X_RESET_CONTROL);
  	bq2415x_i2c_write(bq, BQ2415X_REG_STATUS, BQ2415X_RESET_STATUS);
  	bq->timer_error = NULL;
  }
  
  /**** properties functions ****/
  
  /* set current limit in mA */
  static int bq2415x_set_current_limit(struct bq2415x_device *bq, int mA)
  {
  	int val;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
534

0bc98cc61   Pali Rohár   power_supply: Add...
535
536
537
538
539
540
541
542
  	if (mA <= 100)
  		val = 0;
  	else if (mA <= 500)
  		val = 1;
  	else if (mA <= 800)
  		val = 2;
  	else
  		val = 3;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
543

0bc98cc61   Pali Rohár   power_supply: Add...
544
545
546
547
548
549
550
  	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
  			BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
  }
  
  /* get current limit in mA */
  static int bq2415x_get_current_limit(struct bq2415x_device *bq)
  {
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
551
552
553
  	int ret;
  
  	ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
0bc98cc61   Pali Rohár   power_supply: Add...
554
555
556
557
558
559
560
561
562
563
564
  			BQ2415X_MASK_LIMIT, BQ2415X_SHIFT_LIMIT);
  	if (ret < 0)
  		return ret;
  	else if (ret == 0)
  		return 100;
  	else if (ret == 1)
  		return 500;
  	else if (ret == 2)
  		return 800;
  	else if (ret == 3)
  		return 1800;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
565
  	return -EINVAL;
0bc98cc61   Pali Rohár   power_supply: Add...
566
567
568
569
570
  }
  
  /* set weak battery voltage in mV */
  static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
  {
0bc98cc61   Pali Rohár   power_supply: Add...
571
  	int val;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
572
573
  
  	/* round to 100mV */
0bc98cc61   Pali Rohár   power_supply: Add...
574
575
576
577
578
579
580
581
  	if (mV <= 3400 + 50)
  		val = 0;
  	else if (mV <= 3500 + 50)
  		val = 1;
  	else if (mV <= 3600 + 50)
  		val = 2;
  	else
  		val = 3;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
582

0bc98cc61   Pali Rohár   power_supply: Add...
583
584
585
586
587
588
589
  	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
  			BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
  }
  
  /* get weak battery voltage in mV */
  static int bq2415x_get_weak_battery_voltage(struct bq2415x_device *bq)
  {
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
590
591
592
  	int ret;
  
  	ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CONTROL,
0bc98cc61   Pali Rohár   power_supply: Add...
593
594
595
  			BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
596
  	return 100 * (34 + ret);
0bc98cc61   Pali Rohár   power_supply: Add...
597
598
599
600
  }
  
  /* set battery regulation voltage in mV */
  static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
601
  						  int mV)
0bc98cc61   Pali Rohár   power_supply: Add...
602
603
  {
  	int val = (mV/10 - 350) / 2;
3ed5cd79b   Alexandre Belloni   bq2415x_charger: ...
604
605
606
607
  	/*
  	 * According to datasheet, maximum battery regulation voltage is
  	 * 4440mV which is b101111 = 47.
  	 */
0bc98cc61   Pali Rohár   power_supply: Add...
608
609
  	if (val < 0)
  		val = 0;
3ed5cd79b   Alexandre Belloni   bq2415x_charger: ...
610
  	else if (val > 47)
0bc98cc61   Pali Rohár   power_supply: Add...
611
612
613
614
615
616
617
618
619
620
621
  		return -EINVAL;
  
  	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val,
  			BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
  }
  
  /* get battery regulation voltage in mV */
  static int bq2415x_get_battery_regulation_voltage(struct bq2415x_device *bq)
  {
  	int ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_VOLTAGE,
  			BQ2415X_MASK_VO, BQ2415X_SHIFT_VO);
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
622

0bc98cc61   Pali Rohár   power_supply: Add...
623
624
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
625
  	return 10 * (350 + 2*ret);
0bc98cc61   Pali Rohár   power_supply: Add...
626
627
628
629
630
631
  }
  
  /* set charge current in mA (platform data must provide resistor sense) */
  static int bq2415x_set_charge_current(struct bq2415x_device *bq, int mA)
  {
  	int val;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
632

0bc98cc61   Pali Rohár   power_supply: Add...
633
  	if (bq->init_data.resistor_sense <= 0)
f6d8b7744   Anda-Maria Nicolae   power_supply: bq2...
634
  		return -EINVAL;
0bc98cc61   Pali Rohár   power_supply: Add...
635
636
  
  	val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
0bc98cc61   Pali Rohár   power_supply: Add...
637
638
639
640
641
642
643
644
645
646
647
648
649
650
  	if (val < 0)
  		val = 0;
  	else if (val > 7)
  		val = 7;
  
  	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
  			BQ2415X_MASK_VI_CHRG | BQ2415X_MASK_RESET,
  			BQ2415X_SHIFT_VI_CHRG);
  }
  
  /* get charge current in mA (platform data must provide resistor sense) */
  static int bq2415x_get_charge_current(struct bq2415x_device *bq)
  {
  	int ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
651

0bc98cc61   Pali Rohár   power_supply: Add...
652
  	if (bq->init_data.resistor_sense <= 0)
f6d8b7744   Anda-Maria Nicolae   power_supply: bq2...
653
  		return -EINVAL;
0bc98cc61   Pali Rohár   power_supply: Add...
654
655
656
657
658
  
  	ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
  			BQ2415X_MASK_VI_CHRG, BQ2415X_SHIFT_VI_CHRG);
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
659
  	return (37400 + 6800*ret) / bq->init_data.resistor_sense;
0bc98cc61   Pali Rohár   power_supply: Add...
660
661
662
663
664
665
  }
  
  /* set termination current in mA (platform data must provide resistor sense) */
  static int bq2415x_set_termination_current(struct bq2415x_device *bq, int mA)
  {
  	int val;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
666

0bc98cc61   Pali Rohár   power_supply: Add...
667
  	if (bq->init_data.resistor_sense <= 0)
f6d8b7744   Anda-Maria Nicolae   power_supply: bq2...
668
  		return -EINVAL;
0bc98cc61   Pali Rohár   power_supply: Add...
669
670
  
  	val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
0bc98cc61   Pali Rohár   power_supply: Add...
671
672
673
674
675
676
677
678
679
680
681
682
683
684
  	if (val < 0)
  		val = 0;
  	else if (val > 7)
  		val = 7;
  
  	return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CURRENT, val,
  			BQ2415X_MASK_VI_TERM | BQ2415X_MASK_RESET,
  			BQ2415X_SHIFT_VI_TERM);
  }
  
  /* get termination current in mA (platform data must provide resistor sense) */
  static int bq2415x_get_termination_current(struct bq2415x_device *bq)
  {
  	int ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
685

0bc98cc61   Pali Rohár   power_supply: Add...
686
  	if (bq->init_data.resistor_sense <= 0)
f6d8b7744   Anda-Maria Nicolae   power_supply: bq2...
687
  		return -EINVAL;
0bc98cc61   Pali Rohár   power_supply: Add...
688
689
690
691
692
  
  	ret = bq2415x_i2c_read_mask(bq, BQ2415X_REG_CURRENT,
  			BQ2415X_MASK_VI_TERM, BQ2415X_SHIFT_VI_TERM);
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
693
  	return (3400 + 3400*ret) / bq->init_data.resistor_sense;
0bc98cc61   Pali Rohár   power_supply: Add...
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
  }
  
  /* set default value of property */
  #define bq2415x_set_default_value(bq, prop) \
  	do { \
  		int ret = 0; \
  		if (bq->init_data.prop != -1) \
  			ret = bq2415x_set_##prop(bq, bq->init_data.prop); \
  		if (ret < 0) \
  			return ret; \
  	} while (0)
  
  /* set default values of all properties */
  static int bq2415x_set_defaults(struct bq2415x_device *bq)
  {
  	bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
  	bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
  	bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_DISABLE);
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
712

0bc98cc61   Pali Rohár   power_supply: Add...
713
714
715
  	bq2415x_set_default_value(bq, current_limit);
  	bq2415x_set_default_value(bq, weak_battery_voltage);
  	bq2415x_set_default_value(bq, battery_regulation_voltage);
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
716

0bc98cc61   Pali Rohár   power_supply: Add...
717
718
719
720
721
  	if (bq->init_data.resistor_sense > 0) {
  		bq2415x_set_default_value(bq, charge_current);
  		bq2415x_set_default_value(bq, termination_current);
  		bq2415x_exec_command(bq, BQ2415X_CHARGE_TERMINATION_ENABLE);
  	}
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
722

0bc98cc61   Pali Rohár   power_supply: Add...
723
724
725
726
727
728
729
730
731
732
733
734
  	bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
  	return 0;
  }
  
  /**** charger mode functions ****/
  
  /* set charger mode */
  static int bq2415x_set_mode(struct bq2415x_device *bq, enum bq2415x_mode mode)
  {
  	int ret = 0;
  	int charger = 0;
  	int boost = 0;
0bc98cc61   Pali Rohár   power_supply: Add...
735
736
  	if (mode == BQ2415X_MODE_BOOST)
  		boost = 1;
ac6324e70   Pali Rohár   bq2415x_charger: ...
737
738
  	else if (mode != BQ2415X_MODE_OFF)
  		charger = 1;
0bc98cc61   Pali Rohár   power_supply: Add...
739
740
741
742
743
744
745
746
747
748
749
  
  	if (!charger)
  		ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_DISABLE);
  
  	if (!boost)
  		ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_DISABLE);
  
  	if (ret < 0)
  		return ret;
  
  	switch (mode) {
ac6324e70   Pali Rohár   bq2415x_charger: ...
750
751
752
753
754
  	case BQ2415X_MODE_OFF:
  		dev_dbg(bq->dev, "changing mode to: Offline
  ");
  		ret = bq2415x_set_current_limit(bq, 100);
  		break;
0bc98cc61   Pali Rohár   power_supply: Add...
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
  	case BQ2415X_MODE_NONE:
  		dev_dbg(bq->dev, "changing mode to: N/A
  ");
  		ret = bq2415x_set_current_limit(bq, 100);
  		break;
  	case BQ2415X_MODE_HOST_CHARGER:
  		dev_dbg(bq->dev, "changing mode to: Host/HUB charger
  ");
  		ret = bq2415x_set_current_limit(bq, 500);
  		break;
  	case BQ2415X_MODE_DEDICATED_CHARGER:
  		dev_dbg(bq->dev, "changing mode to: Dedicated charger
  ");
  		ret = bq2415x_set_current_limit(bq, 1800);
  		break;
  	case BQ2415X_MODE_BOOST: /* Boost mode */
  		dev_dbg(bq->dev, "changing mode to: Boost
  ");
  		ret = bq2415x_set_current_limit(bq, 100);
  		break;
  	}
  
  	if (ret < 0)
  		return ret;
  
  	if (charger)
  		ret = bq2415x_exec_command(bq, BQ2415X_CHARGER_ENABLE);
  	else if (boost)
  		ret = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_ENABLE);
  
  	if (ret < 0)
  		return ret;
  
  	bq2415x_set_default_value(bq, weak_battery_voltage);
  	bq2415x_set_default_value(bq, battery_regulation_voltage);
  
  	bq->mode = mode;
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
792
  	sysfs_notify(&bq->charger->dev.kobj, NULL, "mode");
0bc98cc61   Pali Rohár   power_supply: Add...
793
794
795
796
  
  	return 0;
  
  }
b68c31614   Pali Rohár   bq2415x_charger: ...
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
  static bool bq2415x_update_reported_mode(struct bq2415x_device *bq, int mA)
  {
  	enum bq2415x_mode mode;
  
  	if (mA == 0)
  		mode = BQ2415X_MODE_OFF;
  	else if (mA < 500)
  		mode = BQ2415X_MODE_NONE;
  	else if (mA < 1800)
  		mode = BQ2415X_MODE_HOST_CHARGER;
  	else
  		mode = BQ2415X_MODE_DEDICATED_CHARGER;
  
  	if (bq->reported_mode == mode)
  		return false;
  
  	bq->reported_mode = mode;
  	return true;
  }
32260308b   Pali Rohár   bq2415x_charger: ...
816
817
  static int bq2415x_notifier_call(struct notifier_block *nb,
  		unsigned long val, void *v)
0bc98cc61   Pali Rohár   power_supply: Add...
818
  {
32260308b   Pali Rohár   bq2415x_charger: ...
819
820
821
  	struct bq2415x_device *bq =
  		container_of(nb, struct bq2415x_device, nb);
  	struct power_supply *psy = v;
32260308b   Pali Rohár   bq2415x_charger: ...
822
823
  	union power_supply_propval prop;
  	int ret;
0bc98cc61   Pali Rohár   power_supply: Add...
824

32260308b   Pali Rohár   bq2415x_charger: ...
825
826
  	if (val != PSY_EVENT_PROP_CHANGED)
  		return NOTIFY_OK;
b68c31614   Pali Rohár   bq2415x_charger: ...
827
828
829
830
831
832
833
834
835
  	/* Ignore event if it was not send by notify_node/notify_device */
  	if (bq->notify_node) {
  		if (!psy->dev.parent ||
  		    psy->dev.parent->of_node != bq->notify_node)
  			return NOTIFY_OK;
  	} else if (bq->init_data.notify_device) {
  		if (strcmp(psy->desc->name, bq->init_data.notify_device) != 0)
  			return NOTIFY_OK;
  	}
32260308b   Pali Rohár   bq2415x_charger: ...
836
837
838
  
  	dev_dbg(bq->dev, "notifier call was called
  ");
d7bdffb91   Krzysztof Kozlowski   power_supply: bq2...
839
840
  	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX,
  			&prop);
32260308b   Pali Rohár   bq2415x_charger: ...
841
842
  	if (ret != 0)
  		return NOTIFY_OK;
b68c31614   Pali Rohár   bq2415x_charger: ...
843
  	if (!bq2415x_update_reported_mode(bq, prop.intval))
32260308b   Pali Rohár   bq2415x_charger: ...
844
  		return NOTIFY_OK;
0bc98cc61   Pali Rohár   power_supply: Add...
845

0bc98cc61   Pali Rohár   power_supply: Add...
846
847
  	/* if automode is not enabled do not tell about reported_mode */
  	if (bq->automode < 1)
32260308b   Pali Rohár   bq2415x_charger: ...
848
  		return NOTIFY_OK;
0bc98cc61   Pali Rohár   power_supply: Add...
849

3c0185046   Sebastian Reichel   bq2415x_charger: ...
850
  	schedule_delayed_work(&bq->work, 0);
0bc98cc61   Pali Rohár   power_supply: Add...
851

32260308b   Pali Rohár   bq2415x_charger: ...
852
  	return NOTIFY_OK;
0bc98cc61   Pali Rohár   power_supply: Add...
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
  }
  
  /**** timer functions ****/
  
  /* enable/disable auto resetting chip timer */
  static void bq2415x_set_autotimer(struct bq2415x_device *bq, int state)
  {
  	mutex_lock(&bq2415x_timer_mutex);
  
  	if (bq->autotimer == state) {
  		mutex_unlock(&bq2415x_timer_mutex);
  		return;
  	}
  
  	bq->autotimer = state;
  
  	if (state) {
  		schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
  		bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
  		bq->timer_error = NULL;
  	} else {
  		cancel_delayed_work_sync(&bq->work);
  	}
  
  	mutex_unlock(&bq2415x_timer_mutex);
  }
  
  /* called by bq2415x_timer_work on timer error */
  static void bq2415x_timer_error(struct bq2415x_device *bq, const char *msg)
  {
  	bq->timer_error = msg;
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
884
  	sysfs_notify(&bq->charger->dev.kobj, NULL, "timer");
0bc98cc61   Pali Rohár   power_supply: Add...
885
886
887
888
  	dev_err(bq->dev, "%s
  ", msg);
  	if (bq->automode > 0)
  		bq->automode = 0;
ac6324e70   Pali Rohár   bq2415x_charger: ...
889
  	bq2415x_set_mode(bq, BQ2415X_MODE_OFF);
0bc98cc61   Pali Rohár   power_supply: Add...
890
891
892
893
894
895
896
  	bq2415x_set_autotimer(bq, 0);
  }
  
  /* delayed work function for auto resetting chip timer */
  static void bq2415x_timer_work(struct work_struct *work)
  {
  	struct bq2415x_device *bq = container_of(work, struct bq2415x_device,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
897
898
899
900
  						 work.work);
  	int ret;
  	int error;
  	int boost;
0bc98cc61   Pali Rohár   power_supply: Add...
901

3c0185046   Sebastian Reichel   bq2415x_charger: ...
902
  	if (bq->automode > 0 && (bq->reported_mode != bq->mode)) {
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
903
  		sysfs_notify(&bq->charger->dev.kobj, NULL, "reported_mode");
3c0185046   Sebastian Reichel   bq2415x_charger: ...
904
905
  		bq2415x_set_mode(bq, bq->reported_mode);
  	}
0bc98cc61   Pali Rohár   power_supply: Add...
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
  	if (!bq->autotimer)
  		return;
  
  	ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
  	if (ret < 0) {
  		bq2415x_timer_error(bq, "Resetting timer failed");
  		return;
  	}
  
  	boost = bq2415x_exec_command(bq, BQ2415X_BOOST_MODE_STATUS);
  	if (boost < 0) {
  		bq2415x_timer_error(bq, "Unknown error");
  		return;
  	}
  
  	error = bq2415x_exec_command(bq, BQ2415X_FAULT_STATUS);
  	if (error < 0) {
  		bq2415x_timer_error(bq, "Unknown error");
  		return;
  	}
  
  	if (boost) {
  		switch (error) {
  		/* Non fatal errors, chip is OK */
  		case 0: /* No error */
  			break;
  		case 6: /* Timer expired */
  			dev_err(bq->dev, "Timer expired
  ");
  			break;
  		case 3: /* Battery voltage too low */
  			dev_err(bq->dev, "Battery voltage to low
  ");
  			break;
  
  		/* Fatal errors, disable and reset chip */
  		case 1: /* Overvoltage protection (chip fried) */
  			bq2415x_timer_error(bq,
  				"Overvoltage protection (chip fried)");
  			return;
  		case 2: /* Overload */
  			bq2415x_timer_error(bq, "Overload");
  			return;
  		case 4: /* Battery overvoltage protection */
  			bq2415x_timer_error(bq,
  				"Battery overvoltage protection");
  			return;
  		case 5: /* Thermal shutdown (too hot) */
  			bq2415x_timer_error(bq,
  					"Thermal shutdown (too hot)");
  			return;
  		case 7: /* N/A */
  			bq2415x_timer_error(bq, "Unknown error");
  			return;
  		}
  	} else {
  		switch (error) {
  		/* Non fatal errors, chip is OK */
  		case 0: /* No error */
  			break;
  		case 2: /* Sleep mode */
  			dev_err(bq->dev, "Sleep mode
  ");
  			break;
  		case 3: /* Poor input source */
  			dev_err(bq->dev, "Poor input source
  ");
  			break;
  		case 6: /* Timer expired */
  			dev_err(bq->dev, "Timer expired
  ");
  			break;
  		case 7: /* No battery */
  			dev_err(bq->dev, "No battery
  ");
  			break;
  
  		/* Fatal errors, disable and reset chip */
  		case 1: /* Overvoltage protection (chip fried) */
  			bq2415x_timer_error(bq,
  				"Overvoltage protection (chip fried)");
  			return;
  		case 4: /* Battery overvoltage protection */
  			bq2415x_timer_error(bq,
  				"Battery overvoltage protection");
  			return;
  		case 5: /* Thermal shutdown (too hot) */
  			bq2415x_timer_error(bq,
  				"Thermal shutdown (too hot)");
  			return;
  		}
  	}
  
  	schedule_delayed_work(&bq->work, BQ2415X_TIMER_TIMEOUT * HZ);
  }
  
  /**** power supply interface code ****/
  
  static enum power_supply_property bq2415x_power_supply_props[] = {
  	/* TODO: maybe add more power supply properties */
  	POWER_SUPPLY_PROP_STATUS,
  	POWER_SUPPLY_PROP_MODEL_NAME,
  };
  
  static int bq2415x_power_supply_get_property(struct power_supply *psy,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1011
1012
  					     enum power_supply_property psp,
  					     union power_supply_propval *val)
0bc98cc61   Pali Rohár   power_supply: Add...
1013
  {
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1014
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
  	int ret;
  
  	switch (psp) {
  	case POWER_SUPPLY_PROP_STATUS:
  		ret = bq2415x_exec_command(bq, BQ2415X_CHARGE_STATUS);
  		if (ret < 0)
  			return ret;
  		else if (ret == 0) /* Ready */
  			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  		else if (ret == 1) /* Charge in progress */
  			val->intval = POWER_SUPPLY_STATUS_CHARGING;
  		else if (ret == 2) /* Charge done */
  			val->intval = POWER_SUPPLY_STATUS_FULL;
  		else
  			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  		break;
  	case POWER_SUPPLY_PROP_MODEL_NAME:
  		val->strval = bq->model;
  		break;
  	default:
  		return -EINVAL;
  	}
  	return 0;
  }
  
  static int bq2415x_power_supply_init(struct bq2415x_device *bq)
  {
  	int ret;
  	int chip;
  	char revstr[8];
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1045
  	struct power_supply_config psy_cfg = { .drv_data = bq, };
0bc98cc61   Pali Rohár   power_supply: Add...
1046

297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1047
1048
1049
1050
1051
1052
  	bq->charger_desc.name = bq->name;
  	bq->charger_desc.type = POWER_SUPPLY_TYPE_USB;
  	bq->charger_desc.properties = bq2415x_power_supply_props;
  	bq->charger_desc.num_properties =
  			ARRAY_SIZE(bq2415x_power_supply_props);
  	bq->charger_desc.get_property = bq2415x_power_supply_get_property;
0bc98cc61   Pali Rohár   power_supply: Add...
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
  
  	ret = bq2415x_detect_chip(bq);
  	if (ret < 0)
  		chip = BQUNKNOWN;
  	else
  		chip = ret;
  
  	ret = bq2415x_detect_revision(bq);
  	if (ret < 0)
  		strcpy(revstr, "unknown");
  	else
  		sprintf(revstr, "1.%d", ret);
  
  	bq->model = kasprintf(GFP_KERNEL,
  				"chip %s, revision %s, vender code %.3d",
  				bq2415x_chip_name[chip], revstr,
  				bq2415x_get_vender_code(bq));
  	if (!bq->model) {
  		dev_err(bq->dev, "failed to allocate model name
  ");
  		return -ENOMEM;
  	}
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1075
1076
1077
  	bq->charger = power_supply_register(bq->dev, &bq->charger_desc,
  					    &psy_cfg);
  	if (IS_ERR(bq->charger)) {
0bc98cc61   Pali Rohár   power_supply: Add...
1078
  		kfree(bq->model);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1079
  		return PTR_ERR(bq->charger);
0bc98cc61   Pali Rohár   power_supply: Add...
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
  	}
  
  	return 0;
  }
  
  static void bq2415x_power_supply_exit(struct bq2415x_device *bq)
  {
  	bq->autotimer = 0;
  	if (bq->automode > 0)
  		bq->automode = 0;
  	cancel_delayed_work_sync(&bq->work);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1091
  	power_supply_unregister(bq->charger);
0bc98cc61   Pali Rohár   power_supply: Add...
1092
1093
1094
1095
1096
1097
1098
  	kfree(bq->model);
  }
  
  /**** additional sysfs entries for power supply interface ****/
  
  /* show *_status entries */
  static ssize_t bq2415x_sysfs_show_status(struct device *dev,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1099
1100
  					 struct device_attribute *attr,
  					 char *buf)
0bc98cc61   Pali Rohár   power_supply: Add...
1101
1102
  {
  	struct power_supply *psy = dev_get_drvdata(dev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1103
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
  	enum bq2415x_command command;
  	int ret;
  
  	if (strcmp(attr->attr.name, "otg_status") == 0)
  		command = BQ2415X_OTG_STATUS;
  	else if (strcmp(attr->attr.name, "charge_status") == 0)
  		command = BQ2415X_CHARGE_STATUS;
  	else if (strcmp(attr->attr.name, "boost_status") == 0)
  		command = BQ2415X_BOOST_STATUS;
  	else if (strcmp(attr->attr.name, "fault_status") == 0)
  		command = BQ2415X_FAULT_STATUS;
  	else
  		return -EINVAL;
  
  	ret = bq2415x_exec_command(bq, command);
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1121
1122
  	return sprintf(buf, "%d
  ", ret);
0bc98cc61   Pali Rohár   power_supply: Add...
1123
  }
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1124
1125
1126
1127
1128
1129
  /*
   * set timer entry:
   *    auto - enable auto mode
   *    off - disable auto mode
   *    (other values) - reset chip timer
   */
0bc98cc61   Pali Rohár   power_supply: Add...
1130
  static ssize_t bq2415x_sysfs_set_timer(struct device *dev,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1131
1132
1133
  				       struct device_attribute *attr,
  				       const char *buf,
  				       size_t count)
0bc98cc61   Pali Rohár   power_supply: Add...
1134
1135
  {
  	struct power_supply *psy = dev_get_drvdata(dev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1136
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
  	int ret = 0;
  
  	if (strncmp(buf, "auto", 4) == 0)
  		bq2415x_set_autotimer(bq, 1);
  	else if (strncmp(buf, "off", 3) == 0)
  		bq2415x_set_autotimer(bq, 0);
  	else
  		ret = bq2415x_exec_command(bq, BQ2415X_TIMER_RESET);
  
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1148
  	return count;
0bc98cc61   Pali Rohár   power_supply: Add...
1149
1150
1151
1152
  }
  
  /* show timer entry (auto or off) */
  static ssize_t bq2415x_sysfs_show_timer(struct device *dev,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1153
1154
  					struct device_attribute *attr,
  					char *buf)
0bc98cc61   Pali Rohár   power_supply: Add...
1155
1156
  {
  	struct power_supply *psy = dev_get_drvdata(dev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1157
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1158
1159
1160
1161
1162
1163
1164
1165
  
  	if (bq->timer_error)
  		return sprintf(buf, "%s
  ", bq->timer_error);
  
  	if (bq->autotimer)
  		return sprintf(buf, "auto
  ");
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1166
1167
  	return sprintf(buf, "off
  ");
0bc98cc61   Pali Rohár   power_supply: Add...
1168
  }
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1169
1170
1171
1172
1173
1174
1175
1176
  /*
   * set mode entry:
   *    auto - if automode is supported, enable it and set mode to reported
   *    none - disable charger and boost mode
   *    host - charging mode for host/hub chargers (current limit 500mA)
   *    dedicated - charging mode for dedicated chargers (unlimited current limit)
   *    boost - disable charger and enable boost mode
   */
0bc98cc61   Pali Rohár   power_supply: Add...
1177
  static ssize_t bq2415x_sysfs_set_mode(struct device *dev,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1178
1179
1180
  				      struct device_attribute *attr,
  				      const char *buf,
  				      size_t count)
0bc98cc61   Pali Rohár   power_supply: Add...
1181
1182
  {
  	struct power_supply *psy = dev_get_drvdata(dev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1183
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1184
1185
1186
1187
1188
  	enum bq2415x_mode mode;
  	int ret = 0;
  
  	if (strncmp(buf, "auto", 4) == 0) {
  		if (bq->automode < 0)
f6d8b7744   Anda-Maria Nicolae   power_supply: bq2...
1189
  			return -EINVAL;
0bc98cc61   Pali Rohár   power_supply: Add...
1190
1191
  		bq->automode = 1;
  		mode = bq->reported_mode;
ac6324e70   Pali Rohár   bq2415x_charger: ...
1192
1193
1194
1195
  	} else if (strncmp(buf, "off", 3) == 0) {
  		if (bq->automode > 0)
  			bq->automode = 0;
  		mode = BQ2415X_MODE_OFF;
0bc98cc61   Pali Rohár   power_supply: Add...
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
  	} else if (strncmp(buf, "none", 4) == 0) {
  		if (bq->automode > 0)
  			bq->automode = 0;
  		mode = BQ2415X_MODE_NONE;
  	} else if (strncmp(buf, "host", 4) == 0) {
  		if (bq->automode > 0)
  			bq->automode = 0;
  		mode = BQ2415X_MODE_HOST_CHARGER;
  	} else if (strncmp(buf, "dedicated", 9) == 0) {
  		if (bq->automode > 0)
  			bq->automode = 0;
  		mode = BQ2415X_MODE_DEDICATED_CHARGER;
  	} else if (strncmp(buf, "boost", 5) == 0) {
  		if (bq->automode > 0)
  			bq->automode = 0;
  		mode = BQ2415X_MODE_BOOST;
  	} else if (strncmp(buf, "reset", 5) == 0) {
  		bq2415x_reset_chip(bq);
  		bq2415x_set_defaults(bq);
  		if (bq->automode <= 0)
  			return count;
  		bq->automode = 1;
  		mode = bq->reported_mode;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1219
  	} else {
0bc98cc61   Pali Rohár   power_supply: Add...
1220
  		return -EINVAL;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1221
  	}
0bc98cc61   Pali Rohár   power_supply: Add...
1222
1223
1224
1225
  
  	ret = bq2415x_set_mode(bq, mode);
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1226
  	return count;
0bc98cc61   Pali Rohár   power_supply: Add...
1227
1228
1229
1230
  }
  
  /* show mode entry (auto, none, host, dedicated or boost) */
  static ssize_t bq2415x_sysfs_show_mode(struct device *dev,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1231
1232
  				       struct device_attribute *attr,
  				       char *buf)
0bc98cc61   Pali Rohár   power_supply: Add...
1233
1234
  {
  	struct power_supply *psy = dev_get_drvdata(dev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1235
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1236
1237
1238
1239
1240
1241
  	ssize_t ret = 0;
  
  	if (bq->automode > 0)
  		ret += sprintf(buf+ret, "auto (");
  
  	switch (bq->mode) {
ac6324e70   Pali Rohár   bq2415x_charger: ...
1242
1243
1244
  	case BQ2415X_MODE_OFF:
  		ret += sprintf(buf+ret, "off");
  		break;
0bc98cc61   Pali Rohár   power_supply: Add...
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
  	case BQ2415X_MODE_NONE:
  		ret += sprintf(buf+ret, "none");
  		break;
  	case BQ2415X_MODE_HOST_CHARGER:
  		ret += sprintf(buf+ret, "host");
  		break;
  	case BQ2415X_MODE_DEDICATED_CHARGER:
  		ret += sprintf(buf+ret, "dedicated");
  		break;
  	case BQ2415X_MODE_BOOST:
  		ret += sprintf(buf+ret, "boost");
  		break;
  	}
  
  	if (bq->automode > 0)
  		ret += sprintf(buf+ret, ")");
  
  	ret += sprintf(buf+ret, "
  ");
  	return ret;
  }
  
  /* show reported_mode entry (none, host, dedicated or boost) */
  static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1269
1270
  						struct device_attribute *attr,
  						char *buf)
0bc98cc61   Pali Rohár   power_supply: Add...
1271
1272
  {
  	struct power_supply *psy = dev_get_drvdata(dev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1273
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1274
1275
1276
1277
1278
  
  	if (bq->automode < 0)
  		return -EINVAL;
  
  	switch (bq->reported_mode) {
ac6324e70   Pali Rohár   bq2415x_charger: ...
1279
1280
1281
  	case BQ2415X_MODE_OFF:
  		return sprintf(buf, "off
  ");
0bc98cc61   Pali Rohár   power_supply: Add...
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
  	case BQ2415X_MODE_NONE:
  		return sprintf(buf, "none
  ");
  	case BQ2415X_MODE_HOST_CHARGER:
  		return sprintf(buf, "host
  ");
  	case BQ2415X_MODE_DEDICATED_CHARGER:
  		return sprintf(buf, "dedicated
  ");
  	case BQ2415X_MODE_BOOST:
  		return sprintf(buf, "boost
  ");
  	}
  
  	return -EINVAL;
  }
  
  /* directly set raw value to chip register, format: 'register value' */
  static ssize_t bq2415x_sysfs_set_registers(struct device *dev,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1301
1302
1303
  					   struct device_attribute *attr,
  					   const char *buf,
  					   size_t count)
0bc98cc61   Pali Rohár   power_supply: Add...
1304
1305
  {
  	struct power_supply *psy = dev_get_drvdata(dev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1306
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
  	ssize_t ret = 0;
  	unsigned int reg;
  	unsigned int val;
  
  	if (sscanf(buf, "%x %x", &reg, &val) != 2)
  		return -EINVAL;
  
  	if (reg > 4 || val > 255)
  		return -EINVAL;
  
  	ret = bq2415x_i2c_write(bq, reg, val);
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1320
  	return count;
0bc98cc61   Pali Rohár   power_supply: Add...
1321
1322
1323
1324
  }
  
  /* print value of chip register, format: 'register=value' */
  static ssize_t bq2415x_sysfs_print_reg(struct bq2415x_device *bq,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1325
1326
  				       u8 reg,
  				       char *buf)
0bc98cc61   Pali Rohár   power_supply: Add...
1327
1328
  {
  	int ret = bq2415x_i2c_read(bq, reg);
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1329

0bc98cc61   Pali Rohár   power_supply: Add...
1330
1331
1332
  	if (ret < 0)
  		return sprintf(buf, "%#.2x=error %d
  ", reg, ret);
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1333
1334
  	return sprintf(buf, "%#.2x=%#.2x
  ", reg, ret);
0bc98cc61   Pali Rohár   power_supply: Add...
1335
1336
1337
1338
  }
  
  /* show all raw values of chip register, format per line: 'register=value' */
  static ssize_t bq2415x_sysfs_show_registers(struct device *dev,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1339
1340
  					    struct device_attribute *attr,
  					    char *buf)
0bc98cc61   Pali Rohár   power_supply: Add...
1341
1342
  {
  	struct power_supply *psy = dev_get_drvdata(dev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1343
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
  	ssize_t ret = 0;
  
  	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_STATUS, buf+ret);
  	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CONTROL, buf+ret);
  	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VOLTAGE, buf+ret);
  	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_VENDER, buf+ret);
  	ret += bq2415x_sysfs_print_reg(bq, BQ2415X_REG_CURRENT, buf+ret);
  	return ret;
  }
  
  /* set current and voltage limit entries (in mA or mV) */
  static ssize_t bq2415x_sysfs_set_limit(struct device *dev,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1356
1357
1358
  				       struct device_attribute *attr,
  				       const char *buf,
  				       size_t count)
0bc98cc61   Pali Rohár   power_supply: Add...
1359
1360
  {
  	struct power_supply *psy = dev_get_drvdata(dev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1361
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
  	long val;
  	int ret;
  
  	if (kstrtol(buf, 10, &val) < 0)
  		return -EINVAL;
  
  	if (strcmp(attr->attr.name, "current_limit") == 0)
  		ret = bq2415x_set_current_limit(bq, val);
  	else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
  		ret = bq2415x_set_weak_battery_voltage(bq, val);
  	else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
  		ret = bq2415x_set_battery_regulation_voltage(bq, val);
  	else if (strcmp(attr->attr.name, "charge_current") == 0)
  		ret = bq2415x_set_charge_current(bq, val);
  	else if (strcmp(attr->attr.name, "termination_current") == 0)
  		ret = bq2415x_set_termination_current(bq, val);
  	else
  		return -EINVAL;
  
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1383
  	return count;
0bc98cc61   Pali Rohár   power_supply: Add...
1384
1385
1386
1387
  }
  
  /* show current and voltage limit entries (in mA or mV) */
  static ssize_t bq2415x_sysfs_show_limit(struct device *dev,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1388
1389
  					struct device_attribute *attr,
  					char *buf)
0bc98cc61   Pali Rohár   power_supply: Add...
1390
1391
  {
  	struct power_supply *psy = dev_get_drvdata(dev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1392
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
  	int ret;
  
  	if (strcmp(attr->attr.name, "current_limit") == 0)
  		ret = bq2415x_get_current_limit(bq);
  	else if (strcmp(attr->attr.name, "weak_battery_voltage") == 0)
  		ret = bq2415x_get_weak_battery_voltage(bq);
  	else if (strcmp(attr->attr.name, "battery_regulation_voltage") == 0)
  		ret = bq2415x_get_battery_regulation_voltage(bq);
  	else if (strcmp(attr->attr.name, "charge_current") == 0)
  		ret = bq2415x_get_charge_current(bq);
  	else if (strcmp(attr->attr.name, "termination_current") == 0)
  		ret = bq2415x_get_termination_current(bq);
  	else
  		return -EINVAL;
  
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1410
1411
  	return sprintf(buf, "%d
  ", ret);
0bc98cc61   Pali Rohár   power_supply: Add...
1412
1413
1414
1415
  }
  
  /* set *_enable entries */
  static ssize_t bq2415x_sysfs_set_enable(struct device *dev,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1416
1417
1418
  					struct device_attribute *attr,
  					const char *buf,
  					size_t count)
0bc98cc61   Pali Rohár   power_supply: Add...
1419
1420
  {
  	struct power_supply *psy = dev_get_drvdata(dev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1421
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
  	enum bq2415x_command command;
  	long val;
  	int ret;
  
  	if (kstrtol(buf, 10, &val) < 0)
  		return -EINVAL;
  
  	if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
  		command = val ? BQ2415X_CHARGE_TERMINATION_ENABLE :
  			BQ2415X_CHARGE_TERMINATION_DISABLE;
  	else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
  		command = val ? BQ2415X_HIGH_IMPEDANCE_ENABLE :
  			BQ2415X_HIGH_IMPEDANCE_DISABLE;
  	else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
  		command = val ? BQ2415X_OTG_PIN_ENABLE :
  			BQ2415X_OTG_PIN_DISABLE;
  	else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
  		command = val ? BQ2415X_STAT_PIN_ENABLE :
  			BQ2415X_STAT_PIN_DISABLE;
  	else
  		return -EINVAL;
  
  	ret = bq2415x_exec_command(bq, command);
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1447
  	return count;
0bc98cc61   Pali Rohár   power_supply: Add...
1448
1449
1450
1451
  }
  
  /* show *_enable entries */
  static ssize_t bq2415x_sysfs_show_enable(struct device *dev,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1452
1453
  					 struct device_attribute *attr,
  					 char *buf)
0bc98cc61   Pali Rohár   power_supply: Add...
1454
1455
  {
  	struct power_supply *psy = dev_get_drvdata(dev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1456
  	struct bq2415x_device *bq = power_supply_get_drvdata(psy);
0bc98cc61   Pali Rohár   power_supply: Add...
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
  	enum bq2415x_command command;
  	int ret;
  
  	if (strcmp(attr->attr.name, "charge_termination_enable") == 0)
  		command = BQ2415X_CHARGE_TERMINATION_STATUS;
  	else if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
  		command = BQ2415X_HIGH_IMPEDANCE_STATUS;
  	else if (strcmp(attr->attr.name, "otg_pin_enable") == 0)
  		command = BQ2415X_OTG_PIN_STATUS;
  	else if (strcmp(attr->attr.name, "stat_pin_enable") == 0)
  		command = BQ2415X_STAT_PIN_STATUS;
  	else
  		return -EINVAL;
  
  	ret = bq2415x_exec_command(bq, command);
  	if (ret < 0)
  		return ret;
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1474
1475
  	return sprintf(buf, "%d
  ", ret);
0bc98cc61   Pali Rohár   power_supply: Add...
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
  }
  
  static DEVICE_ATTR(current_limit, S_IWUSR | S_IRUGO,
  		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
  static DEVICE_ATTR(weak_battery_voltage, S_IWUSR | S_IRUGO,
  		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
  static DEVICE_ATTR(battery_regulation_voltage, S_IWUSR | S_IRUGO,
  		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
  static DEVICE_ATTR(charge_current, S_IWUSR | S_IRUGO,
  		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
  static DEVICE_ATTR(termination_current, S_IWUSR | S_IRUGO,
  		bq2415x_sysfs_show_limit, bq2415x_sysfs_set_limit);
  
  static DEVICE_ATTR(charge_termination_enable, S_IWUSR | S_IRUGO,
  		bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
  static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
  		bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
  static DEVICE_ATTR(otg_pin_enable, S_IWUSR | S_IRUGO,
  		bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
  static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
  		bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
  
  static DEVICE_ATTR(reported_mode, S_IRUGO,
  		bq2415x_sysfs_show_reported_mode, NULL);
  static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
  		bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
  static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
  		bq2415x_sysfs_show_timer, bq2415x_sysfs_set_timer);
  
  static DEVICE_ATTR(registers, S_IWUSR | S_IRUGO,
  		bq2415x_sysfs_show_registers, bq2415x_sysfs_set_registers);
  
  static DEVICE_ATTR(otg_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
  static DEVICE_ATTR(charge_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
  static DEVICE_ATTR(boost_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
  static DEVICE_ATTR(fault_status, S_IRUGO, bq2415x_sysfs_show_status, NULL);
  
  static struct attribute *bq2415x_sysfs_attributes[] = {
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1514
1515
1516
1517
  	/*
  	 * TODO: some (appropriate) of these attrs should be switched to
  	 * use power supply class props.
  	 */
0bc98cc61   Pali Rohár   power_supply: Add...
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
  	&dev_attr_current_limit.attr,
  	&dev_attr_weak_battery_voltage.attr,
  	&dev_attr_battery_regulation_voltage.attr,
  	&dev_attr_charge_current.attr,
  	&dev_attr_termination_current.attr,
  
  	&dev_attr_charge_termination_enable.attr,
  	&dev_attr_high_impedance_enable.attr,
  	&dev_attr_otg_pin_enable.attr,
  	&dev_attr_stat_pin_enable.attr,
  
  	&dev_attr_reported_mode.attr,
  	&dev_attr_mode.attr,
  	&dev_attr_timer.attr,
  
  	&dev_attr_registers.attr,
  
  	&dev_attr_otg_status.attr,
  	&dev_attr_charge_status.attr,
  	&dev_attr_boost_status.attr,
  	&dev_attr_fault_status.attr,
  	NULL,
  };
  
  static const struct attribute_group bq2415x_sysfs_attr_group = {
  	.attrs = bq2415x_sysfs_attributes,
  };
  
  static int bq2415x_sysfs_init(struct bq2415x_device *bq)
  {
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1548
  	return sysfs_create_group(&bq->charger->dev.kobj,
0bc98cc61   Pali Rohár   power_supply: Add...
1549
1550
1551
1552
1553
  			&bq2415x_sysfs_attr_group);
  }
  
  static void bq2415x_sysfs_exit(struct bq2415x_device *bq)
  {
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
1554
  	sysfs_remove_group(&bq->charger->dev.kobj, &bq2415x_sysfs_attr_group);
0bc98cc61   Pali Rohár   power_supply: Add...
1555
1556
1557
1558
  }
  
  /* main bq2415x probe function */
  static int bq2415x_probe(struct i2c_client *client,
6c47a3e00   Anton Vorontsov   bq2415x_charger: ...
1559
  			 const struct i2c_device_id *id)
0bc98cc61   Pali Rohár   power_supply: Add...
1560
1561
1562
  {
  	int ret;
  	int num;
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1563
  	char *name = NULL;
0bc98cc61   Pali Rohár   power_supply: Add...
1564
  	struct bq2415x_device *bq;
faffd234c   Sebastian Reichel   bq2415x_charger: ...
1565
1566
  	struct device_node *np = client->dev.of_node;
  	struct bq2415x_platform_data *pdata = client->dev.platform_data;
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1567
  	const struct acpi_device_id *acpi_id = NULL;
b68c31614   Pali Rohár   bq2415x_charger: ...
1568
1569
  	struct power_supply *notify_psy = NULL;
  	union power_supply_propval prop;
0bc98cc61   Pali Rohár   power_supply: Add...
1570

b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1571
1572
1573
  	if (!np && !pdata && !ACPI_HANDLE(&client->dev)) {
  		dev_err(&client->dev, "Neither devicetree, nor platform data, nor ACPI support
  ");
0bc98cc61   Pali Rohár   power_supply: Add...
1574
1575
1576
1577
  		return -ENODEV;
  	}
  
  	/* Get new ID for the new device */
0bc98cc61   Pali Rohár   power_supply: Add...
1578
  	mutex_lock(&bq2415x_id_mutex);
05e2cefab   Tejun Heo   power: convert to...
1579
  	num = idr_alloc(&bq2415x_id, client, 0, 0, GFP_KERNEL);
0bc98cc61   Pali Rohár   power_supply: Add...
1580
  	mutex_unlock(&bq2415x_id_mutex);
05e2cefab   Tejun Heo   power: convert to...
1581
1582
  	if (num < 0)
  		return num;
0bc98cc61   Pali Rohár   power_supply: Add...
1583

b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1584
1585
1586
1587
1588
1589
1590
1591
  	if (id) {
  		name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
  	} else if (ACPI_HANDLE(&client->dev)) {
  		acpi_id =
  			acpi_match_device(client->dev.driver->acpi_match_table,
  					  &client->dev);
  		name = kasprintf(GFP_KERNEL, "%s-%d", acpi_id->id, num);
  	}
0bc98cc61   Pali Rohár   power_supply: Add...
1592
1593
1594
1595
1596
1597
  	if (!name) {
  		dev_err(&client->dev, "failed to allocate device name
  ");
  		ret = -ENOMEM;
  		goto error_1;
  	}
a9672e135   Sachin Kamat   bq2415x_charger: ...
1598
  	bq = devm_kzalloc(&client->dev, sizeof(*bq), GFP_KERNEL);
0bc98cc61   Pali Rohár   power_supply: Add...
1599
  	if (!bq) {
0bc98cc61   Pali Rohár   power_supply: Add...
1600
1601
1602
1603
1604
1605
1606
1607
  		ret = -ENOMEM;
  		goto error_2;
  	}
  
  	i2c_set_clientdata(client, bq);
  
  	bq->id = num;
  	bq->dev = &client->dev;
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1608
1609
1610
1611
  	if (id)
  		bq->chip = id->driver_data;
  	else if (ACPI_HANDLE(bq->dev))
  		bq->chip = acpi_id->driver_data;
0bc98cc61   Pali Rohár   power_supply: Add...
1612
  	bq->name = name;
ac6324e70   Pali Rohár   bq2415x_charger: ...
1613
1614
  	bq->mode = BQ2415X_MODE_OFF;
  	bq->reported_mode = BQ2415X_MODE_OFF;
0bc98cc61   Pali Rohár   power_supply: Add...
1615
1616
  	bq->autotimer = 0;
  	bq->automode = 0;
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1617
1618
1619
1620
  	if (np || ACPI_HANDLE(bq->dev)) {
  		ret = device_property_read_u32(bq->dev,
  					       "ti,current-limit",
  					       &bq->init_data.current_limit);
faffd234c   Sebastian Reichel   bq2415x_charger: ...
1621
  		if (ret)
b68c31614   Pali Rohár   bq2415x_charger: ...
1622
  			goto error_2;
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1623
1624
1625
  		ret = device_property_read_u32(bq->dev,
  					"ti,weak-battery-voltage",
  					&bq->init_data.weak_battery_voltage);
faffd234c   Sebastian Reichel   bq2415x_charger: ...
1626
  		if (ret)
b68c31614   Pali Rohár   bq2415x_charger: ...
1627
  			goto error_2;
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1628
1629
  		ret = device_property_read_u32(bq->dev,
  				"ti,battery-regulation-voltage",
faffd234c   Sebastian Reichel   bq2415x_charger: ...
1630
1631
  				&bq->init_data.battery_regulation_voltage);
  		if (ret)
b68c31614   Pali Rohár   bq2415x_charger: ...
1632
  			goto error_2;
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1633
1634
1635
  		ret = device_property_read_u32(bq->dev,
  					       "ti,charge-current",
  					       &bq->init_data.charge_current);
faffd234c   Sebastian Reichel   bq2415x_charger: ...
1636
  		if (ret)
b68c31614   Pali Rohár   bq2415x_charger: ...
1637
  			goto error_2;
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1638
1639
1640
  		ret = device_property_read_u32(bq->dev,
  				"ti,termination-current",
  				&bq->init_data.termination_current);
faffd234c   Sebastian Reichel   bq2415x_charger: ...
1641
  		if (ret)
b68c31614   Pali Rohár   bq2415x_charger: ...
1642
  			goto error_2;
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1643
1644
1645
  		ret = device_property_read_u32(bq->dev,
  					       "ti,resistor-sense",
  					       &bq->init_data.resistor_sense);
faffd234c   Sebastian Reichel   bq2415x_charger: ...
1646
  		if (ret)
b68c31614   Pali Rohár   bq2415x_charger: ...
1647
1648
1649
1650
  			goto error_2;
  		if (np)
  			bq->notify_node = of_parse_phandle(np,
  						"ti,usb-charger-detection", 0);
faffd234c   Sebastian Reichel   bq2415x_charger: ...
1651
1652
1653
  	} else {
  		memcpy(&bq->init_data, pdata, sizeof(bq->init_data));
  	}
0bc98cc61   Pali Rohár   power_supply: Add...
1654
1655
1656
1657
1658
1659
1660
  
  	bq2415x_reset_chip(bq);
  
  	ret = bq2415x_power_supply_init(bq);
  	if (ret) {
  		dev_err(bq->dev, "failed to register power supply: %d
  ", ret);
b68c31614   Pali Rohár   bq2415x_charger: ...
1661
  		goto error_2;
0bc98cc61   Pali Rohár   power_supply: Add...
1662
1663
1664
1665
1666
1667
  	}
  
  	ret = bq2415x_sysfs_init(bq);
  	if (ret) {
  		dev_err(bq->dev, "failed to create sysfs entries: %d
  ", ret);
b68c31614   Pali Rohár   bq2415x_charger: ...
1668
  		goto error_3;
0bc98cc61   Pali Rohár   power_supply: Add...
1669
1670
1671
1672
1673
1674
  	}
  
  	ret = bq2415x_set_defaults(bq);
  	if (ret) {
  		dev_err(bq->dev, "failed to set default values: %d
  ", ret);
b68c31614   Pali Rohár   bq2415x_charger: ...
1675
  		goto error_4;
0bc98cc61   Pali Rohár   power_supply: Add...
1676
  	}
b68c31614   Pali Rohár   bq2415x_charger: ...
1677
  	if (bq->notify_node || bq->init_data.notify_device) {
32260308b   Pali Rohár   bq2415x_charger: ...
1678
1679
1680
1681
1682
  		bq->nb.notifier_call = bq2415x_notifier_call;
  		ret = power_supply_reg_notifier(&bq->nb);
  		if (ret) {
  			dev_err(bq->dev, "failed to reg notifier: %d
  ", ret);
b68c31614   Pali Rohár   bq2415x_charger: ...
1683
  			goto error_4;
32260308b   Pali Rohár   bq2415x_charger: ...
1684
  		}
faffd234c   Sebastian Reichel   bq2415x_charger: ...
1685

32260308b   Pali Rohár   bq2415x_charger: ...
1686
  		bq->automode = 1;
b68c31614   Pali Rohár   bq2415x_charger: ...
1687
1688
  		dev_info(bq->dev, "automode supported, waiting for events
  ");
0bc98cc61   Pali Rohár   power_supply: Add...
1689
1690
1691
1692
1693
  	} else {
  		bq->automode = -1;
  		dev_info(bq->dev, "automode not supported
  ");
  	}
b68c31614   Pali Rohár   bq2415x_charger: ...
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
  	/* Query for initial reported_mode and set it */
  	if (bq->nb.notifier_call) {
  		if (np) {
  			notify_psy = power_supply_get_by_phandle(np,
  						"ti,usb-charger-detection");
  			if (IS_ERR(notify_psy))
  				notify_psy = NULL;
  		} else if (bq->init_data.notify_device) {
  			notify_psy = power_supply_get_by_name(
  						bq->init_data.notify_device);
  		}
  	}
  	if (notify_psy) {
  		ret = power_supply_get_property(notify_psy,
  					POWER_SUPPLY_PROP_CURRENT_MAX, &prop);
  		power_supply_put(notify_psy);
  
  		if (ret == 0) {
  			bq2415x_update_reported_mode(bq, prop.intval);
  			bq2415x_set_mode(bq, bq->reported_mode);
  		}
  	}
0bc98cc61   Pali Rohár   power_supply: Add...
1716
1717
1718
1719
1720
1721
  	INIT_DELAYED_WORK(&bq->work, bq2415x_timer_work);
  	bq2415x_set_autotimer(bq, 1);
  
  	dev_info(bq->dev, "driver registered
  ");
  	return 0;
1a8dbe6f9   Krzysztof Kozlowski   power_supply: bq2...
1722
  error_4:
b68c31614   Pali Rohár   bq2415x_charger: ...
1723
  	bq2415x_sysfs_exit(bq);
1a8dbe6f9   Krzysztof Kozlowski   power_supply: bq2...
1724
  error_3:
b68c31614   Pali Rohár   bq2415x_charger: ...
1725
  	bq2415x_power_supply_exit(bq);
0bc98cc61   Pali Rohár   power_supply: Add...
1726
  error_2:
b7e16ec6e   Markus Elfring   power: bq2415x_ch...
1727
  	if (bq)
b68c31614   Pali Rohár   bq2415x_charger: ...
1728
  		of_node_put(bq->notify_node);
0bc98cc61   Pali Rohár   power_supply: Add...
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
  	kfree(name);
  error_1:
  	mutex_lock(&bq2415x_id_mutex);
  	idr_remove(&bq2415x_id, num);
  	mutex_unlock(&bq2415x_id_mutex);
  
  	return ret;
  }
  
  /* main bq2415x remove function */
  
  static int bq2415x_remove(struct i2c_client *client)
  {
  	struct bq2415x_device *bq = i2c_get_clientdata(client);
b68c31614   Pali Rohár   bq2415x_charger: ...
1743
  	if (bq->nb.notifier_call)
32260308b   Pali Rohár   bq2415x_charger: ...
1744
  		power_supply_unreg_notifier(&bq->nb);
b68c31614   Pali Rohár   bq2415x_charger: ...
1745

b7e16ec6e   Markus Elfring   power: bq2415x_ch...
1746
  	of_node_put(bq->notify_node);
0bc98cc61   Pali Rohár   power_supply: Add...
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
  	bq2415x_sysfs_exit(bq);
  	bq2415x_power_supply_exit(bq);
  
  	bq2415x_reset_chip(bq);
  
  	mutex_lock(&bq2415x_id_mutex);
  	idr_remove(&bq2415x_id, bq->id);
  	mutex_unlock(&bq2415x_id_mutex);
  
  	dev_info(bq->dev, "driver unregistered
  ");
  
  	kfree(bq->name);
0bc98cc61   Pali Rohár   power_supply: Add...
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
  
  	return 0;
  }
  
  static const struct i2c_device_id bq2415x_i2c_id_table[] = {
  	{ "bq2415x", BQUNKNOWN },
  	{ "bq24150", BQ24150 },
  	{ "bq24150a", BQ24150A },
  	{ "bq24151", BQ24151 },
  	{ "bq24151a", BQ24151A },
  	{ "bq24152", BQ24152 },
  	{ "bq24153", BQ24153 },
  	{ "bq24153a", BQ24153A },
  	{ "bq24155", BQ24155 },
  	{ "bq24156", BQ24156 },
  	{ "bq24156a", BQ24156A },
0e1392d9d   Anda-Maria Nicolae   bq2415x_charger: ...
1776
  	{ "bq24157s", BQ24157S },
0bc98cc61   Pali Rohár   power_supply: Add...
1777
1778
1779
1780
  	{ "bq24158", BQ24158 },
  	{},
  };
  MODULE_DEVICE_TABLE(i2c, bq2415x_i2c_id_table);
ec3d7d08c   Pali Rohár   power_supply: bq2...
1781
  #ifdef CONFIG_ACPI
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
  static const struct acpi_device_id bq2415x_i2c_acpi_match[] = {
  	{ "BQ2415X", BQUNKNOWN },
  	{ "BQ241500", BQ24150 },
  	{ "BQA24150", BQ24150A },
  	{ "BQ241510", BQ24151 },
  	{ "BQA24151", BQ24151A },
  	{ "BQ241520", BQ24152 },
  	{ "BQ241530", BQ24153 },
  	{ "BQA24153", BQ24153A },
  	{ "BQ241550", BQ24155 },
  	{ "BQ241560", BQ24156 },
  	{ "BQA24156", BQ24156A },
  	{ "BQS24157", BQ24157S },
  	{ "BQ241580", BQ24158 },
  	{},
  };
  MODULE_DEVICE_TABLE(acpi, bq2415x_i2c_acpi_match);
ec3d7d08c   Pali Rohár   power_supply: bq2...
1799
  #endif
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1800

523e55014   Pali Rohár   power_supply: bq2...
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
  #ifdef CONFIG_OF
  static const struct of_device_id bq2415x_of_match_table[] = {
  	{ .compatible = "ti,bq24150" },
  	{ .compatible = "ti,bq24150a" },
  	{ .compatible = "ti,bq24151" },
  	{ .compatible = "ti,bq24151a" },
  	{ .compatible = "ti,bq24152" },
  	{ .compatible = "ti,bq24153" },
  	{ .compatible = "ti,bq24153a" },
  	{ .compatible = "ti,bq24155" },
  	{ .compatible = "ti,bq24156" },
  	{ .compatible = "ti,bq24156a" },
  	{ .compatible = "ti,bq24157s" },
  	{ .compatible = "ti,bq24158" },
  	{},
  };
  MODULE_DEVICE_TABLE(of, bq2415x_of_match_table);
  #endif
0bc98cc61   Pali Rohár   power_supply: Add...
1819
1820
1821
  static struct i2c_driver bq2415x_driver = {
  	.driver = {
  		.name = "bq2415x-charger",
523e55014   Pali Rohár   power_supply: bq2...
1822
  		.of_match_table = of_match_ptr(bq2415x_of_match_table),
b01e7c3b8   Anda-Maria Nicolae   power_supply: bq2...
1823
  		.acpi_match_table = ACPI_PTR(bq2415x_i2c_acpi_match),
0bc98cc61   Pali Rohár   power_supply: Add...
1824
1825
1826
1827
1828
  	},
  	.probe = bq2415x_probe,
  	.remove = bq2415x_remove,
  	.id_table = bq2415x_i2c_id_table,
  };
463df5147   Sachin Kamat   bq2415x_charger: ...
1829
  module_i2c_driver(bq2415x_driver);
0bc98cc61   Pali Rohár   power_supply: Add...
1830
1831
1832
1833
  
  MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  MODULE_DESCRIPTION("bq2415x charger driver");
  MODULE_LICENSE("GPL");