1 // SPDX-License-Identifier: GPL-2.0
2 // BQ256XX Battery Charger Driver
3 // Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
4
5 #include <linux/err.h>
6 #include <linux/i2c.h>
7 #include <linux/init.h>
8 #include <linux/interrupt.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/power_supply.h>
13 #include <linux/regmap.h>
14 #include <linux/types.h>
15 #include <linux/usb/phy.h>
16 #include <linux/device.h>
17 #include <linux/moduleparam.h>
18 #include <linux/slab.h>
19 #include <linux/acpi.h>
20
21 #define BQ256XX_MANUFACTURER "Texas Instruments"
22
23 #define BQ256XX_INPUT_CURRENT_LIMIT 0x00
24 #define BQ256XX_CHARGER_CONTROL_0 0x01
25 #define BQ256XX_CHARGE_CURRENT_LIMIT 0x02
26 #define BQ256XX_PRECHG_AND_TERM_CURR_LIM 0x03
27 #define BQ256XX_BATTERY_VOLTAGE_LIMIT 0x04
28 #define BQ256XX_CHARGER_CONTROL_1 0x05
29 #define BQ256XX_CHARGER_CONTROL_2 0x06
30 #define BQ256XX_CHARGER_CONTROL_3 0x07
31 #define BQ256XX_CHARGER_STATUS_0 0x08
32 #define BQ256XX_CHARGER_STATUS_1 0x09
33 #define BQ256XX_CHARGER_STATUS_2 0x0a
34 #define BQ256XX_PART_INFORMATION 0x0b
35 #define BQ256XX_CHARGER_CONTROL_4 0x0c
36
37 #define BQ256XX_IINDPM_MASK GENMASK(4, 0)
38 #define BQ256XX_IINDPM_STEP_uA 100000
39 #define BQ256XX_IINDPM_OFFSET_uA 100000
40 #define BQ256XX_IINDPM_MIN_uA 100000
41 #define BQ256XX_IINDPM_MAX_uA 3200000
42 #define BQ256XX_IINDPM_DEF_uA 2400000
43
44 #define BQ256XX_TS_IGNORE BIT(6)
45 #define BQ256XX_TS_IGNORE_SHIFT 6
46
47 #define BQ256XX_VINDPM_MASK GENMASK(3, 0)
48 #define BQ256XX_VINDPM_STEP_uV 100000
49 #define BQ256XX_VINDPM_OFFSET_uV 3900000
50 #define BQ256XX_VINDPM_MIN_uV 3900000
51 #define BQ256XX_VINDPM_MAX_uV 5400000
52 #define BQ256XX_VINDPM_DEF_uV 4500000
53
54 #define BQ256XX_VBATREG_MASK GENMASK(7, 3)
55 #define BQ2560X_VBATREG_STEP_uV 32000
56 #define BQ2560X_VBATREG_OFFSET_uV 3856000
57 #define BQ2560X_VBATREG_MIN_uV 3856000
58 #define BQ2560X_VBATREG_MAX_uV 4624000
59 #define BQ2560X_VBATREG_DEF_uV 4208000
60 #define BQ25601D_VBATREG_OFFSET_uV 3847000
61 #define BQ25601D_VBATREG_MIN_uV 3847000
62 #define BQ25601D_VBATREG_MAX_uV 4615000
63 #define BQ25601D_VBATREG_DEF_uV 4199000
64 #define BQ2561X_VBATREG_STEP_uV 10000
65 #define BQ25611D_VBATREG_MIN_uV 3494000
66 #define BQ25611D_VBATREG_MAX_uV 4510000
67 #define BQ25611D_VBATREG_DEF_uV 4190000
68 #define BQ25618_VBATREG_MIN_uV 3504000
69 #define BQ25618_VBATREG_MAX_uV 4500000
70 #define BQ25618_VBATREG_DEF_uV 4200000
71 #define BQ256XX_VBATREG_BIT_SHIFT 3
72 #define BQ2561X_VBATREG_THRESH 0x8
73 #define BQ25611D_VBATREG_THRESH_uV 4290000
74 #define BQ25618_VBATREG_THRESH_uV 4300000
75
76 #define BQ256XX_CHG_CONFIG_MASK BIT(4)
77 #define BQ256XX_CHG_CONFIG_BIT_SHIFT 4
78
79 #define BQ256XX_ITERM_MASK GENMASK(3, 0)
80 #define BQ256XX_ITERM_STEP_uA 60000
81 #define BQ256XX_ITERM_OFFSET_uA 60000
82 #define BQ256XX_ITERM_MIN_uA 60000
83 #define BQ256XX_ITERM_MAX_uA 780000
84 #define BQ256XX_ITERM_DEF_uA 180000
85 #define BQ25618_ITERM_STEP_uA 20000
86 #define BQ25618_ITERM_OFFSET_uA 20000
87 #define BQ25618_ITERM_MIN_uA 20000
88 #define BQ25618_ITERM_MAX_uA 260000
89 #define BQ25618_ITERM_DEF_uA 60000
90
91 #define BQ256XX_IPRECHG_MASK GENMASK(7, 4)
92 #define BQ256XX_IPRECHG_STEP_uA 60000
93 #define BQ256XX_IPRECHG_OFFSET_uA 60000
94 #define BQ256XX_IPRECHG_MIN_uA 60000
95 #define BQ256XX_IPRECHG_MAX_uA 780000
96 #define BQ256XX_IPRECHG_DEF_uA 180000
97 #define BQ25618_IPRECHG_STEP_uA 20000
98 #define BQ25618_IPRECHG_OFFSET_uA 20000
99 #define BQ25618_IPRECHG_MIN_uA 20000
100 #define BQ25618_IPRECHG_MAX_uA 260000
101 #define BQ25618_IPRECHG_DEF_uA 40000
102 #define BQ256XX_IPRECHG_BIT_SHIFT 4
103
104 #define BQ256XX_ICHG_MASK GENMASK(5, 0)
105 #define BQ256XX_ICHG_STEP_uA 60000
106 #define BQ256XX_ICHG_MIN_uA 0
107 #define BQ256XX_ICHG_MAX_uA 3000000
108 #define BQ2560X_ICHG_DEF_uA 2040000
109 #define BQ25611D_ICHG_DEF_uA 1020000
110 #define BQ25618_ICHG_STEP_uA 20000
111 #define BQ25618_ICHG_MIN_uA 0
112 #define BQ25618_ICHG_MAX_uA 1500000
113 #define BQ25618_ICHG_DEF_uA 340000
114 #define BQ25618_ICHG_THRESH 0x3c
115 #define BQ25618_ICHG_THRESH_uA 1180000
116
117 #define BQ256XX_VBUS_STAT_MASK GENMASK(7, 5)
118 #define BQ256XX_VBUS_STAT_NO_INPUT 0
119 #define BQ256XX_VBUS_STAT_USB_SDP BIT(5)
120 #define BQ256XX_VBUS_STAT_USB_CDP BIT(6)
121 #define BQ256XX_VBUS_STAT_USB_DCP (BIT(6) | BIT(5))
122 #define BQ256XX_VBUS_STAT_USB_OTG (BIT(7) | BIT(6) | BIT(5))
123
124 #define BQ256XX_CHRG_STAT_MASK GENMASK(4, 3)
125 #define BQ256XX_CHRG_STAT_NOT_CHRGING 0
126 #define BQ256XX_CHRG_STAT_PRECHRGING BIT(3)
127 #define BQ256XX_CHRG_STAT_FAST_CHRGING BIT(4)
128 #define BQ256XX_CHRG_STAT_CHRG_TERM (BIT(4) | BIT(3))
129
130 #define BQ256XX_PG_STAT_MASK BIT(2)
131 #define BQ256XX_WDT_FAULT_MASK BIT(7)
132 #define BQ256XX_CHRG_FAULT_MASK GENMASK(5, 4)
133 #define BQ256XX_CHRG_FAULT_NORMAL 0
134 #define BQ256XX_CHRG_FAULT_INPUT BIT(4)
135 #define BQ256XX_CHRG_FAULT_THERM BIT(5)
136 #define BQ256XX_CHRG_FAULT_CST_EXPIRE (BIT(5) | BIT(4))
137 #define BQ256XX_BAT_FAULT_MASK BIT(3)
138 #define BQ256XX_NTC_FAULT_MASK GENMASK(2, 0)
139 #define BQ256XX_NTC_FAULT_WARM BIT(1)
140 #define BQ256XX_NTC_FAULT_COOL (BIT(1) | BIT(0))
141 #define BQ256XX_NTC_FAULT_COLD (BIT(2) | BIT(0))
142 #define BQ256XX_NTC_FAULT_HOT (BIT(2) | BIT(1))
143
144 #define BQ256XX_NUM_WD_VAL 4
145 #define BQ256XX_WATCHDOG_MASK GENMASK(5, 4)
146 #define BQ256XX_WATCHDOG_MAX 1600000
147 #define BQ256XX_WATCHDOG_DIS 0
148 #define BQ256XX_WDT_BIT_SHIFT 4
149
150 #define BQ256XX_REG_RST BIT(7)
151
152 /**
153 * struct bq256xx_init_data -
154 * @ichg: fast charge current
155 * @iindpm: input current limit
156 * @vbatreg: charge voltage
157 * @iterm: termination current
158 * @iprechg: precharge current
159 * @vindpm: input voltage limit
160 * @ichg_max: maximum fast charge current
161 * @vbatreg_max: maximum charge voltage
162 * @ts_ignore: TS_IGNORE flag
163 */
164 struct bq256xx_init_data {
165 u32 ichg;
166 u32 iindpm;
167 u32 vbatreg;
168 u32 iterm;
169 u32 iprechg;
170 u32 vindpm;
171 u32 ichg_max;
172 u32 vbatreg_max;
173 bool ts_ignore;
174 };
175
176 /**
177 * struct bq256xx_state -
178 * @vbus_stat: VBUS status according to BQ256XX_CHARGER_STATUS_0
179 * @chrg_stat: charging status according to BQ256XX_CHARGER_STATUS_0
180 * @online: PG status according to BQ256XX_CHARGER_STATUS_0
181 *
182 * @wdt_fault: watchdog fault according to BQ256XX_CHARGER_STATUS_1
183 * @bat_fault: battery fault according to BQ256XX_CHARGER_STATUS_1
184 * @chrg_fault: charging fault according to BQ256XX_CHARGER_STATUS_1
185 * @ntc_fault: TS fault according to BQ256XX_CHARGER_STATUS_1
186 */
187 struct bq256xx_state {
188 u8 vbus_stat;
189 u8 chrg_stat;
190 bool online;
191
192 u8 wdt_fault;
193 u8 bat_fault;
194 u8 chrg_fault;
195 u8 ntc_fault;
196 };
197
198 enum bq256xx_id {
199 BQ25600,
200 BQ25600D,
201 BQ25601,
202 BQ25601D,
203 BQ25618,
204 BQ25619,
205 BQ25611D,
206 };
207
208 /**
209 * struct bq256xx_device -
210 * @client: i2c client structure
211 * @regmap: register map structure
212 * @dev: device structure
213 * @charger: power supply registered for the charger
214 * @battery: power supply registered for the battery
215 * @lock: mutex lock structure
216 *
217 * @usb2_phy: usb_phy identifier
218 * @usb3_phy: usb_phy identifier
219 * @usb_nb: notifier block
220 * @usb_work: usb work queue
221 * @usb_event: usb_event code
222 *
223 * @model_name: i2c name string
224 *
225 * @init_data: initialization data
226 * @chip_info: device variant information
227 * @state: device status and faults
228 * @watchdog_timer: watchdog timer value in milliseconds
229 */
230 struct bq256xx_device {
231 struct i2c_client *client;
232 struct device *dev;
233 struct power_supply *charger;
234 struct power_supply *battery;
235 struct mutex lock;
236 struct regmap *regmap;
237
238 struct usb_phy *usb2_phy;
239 struct usb_phy *usb3_phy;
240 struct notifier_block usb_nb;
241 struct work_struct usb_work;
242 unsigned long usb_event;
243
244 char model_name[I2C_NAME_SIZE];
245
246 struct bq256xx_init_data init_data;
247 const struct bq256xx_chip_info *chip_info;
248 struct bq256xx_state state;
249 int watchdog_timer;
250 };
251
252 /**
253 * struct bq256xx_chip_info -
254 * @model_id: device instance
255 *
256 * @bq256xx_regmap_config: regmap configuration struct
257 * @bq256xx_get_ichg: pointer to instance specific get_ichg function
258 * @bq256xx_get_iindpm: pointer to instance specific get_iindpm function
259 * @bq256xx_get_vbatreg: pointer to instance specific get_vbatreg function
260 * @bq256xx_get_iterm: pointer to instance specific get_iterm function
261 * @bq256xx_get_iprechg: pointer to instance specific get_iprechg function
262 * @bq256xx_get_vindpm: pointer to instance specific get_vindpm function
263 *
264 * @bq256xx_set_ichg: pointer to instance specific set_ichg function
265 * @bq256xx_set_iindpm: pointer to instance specific set_iindpm function
266 * @bq256xx_set_vbatreg: pointer to instance specific set_vbatreg function
267 * @bq256xx_set_iterm: pointer to instance specific set_iterm function
268 * @bq256xx_set_iprechg: pointer to instance specific set_iprechg function
269 * @bq256xx_set_vindpm: pointer to instance specific set_vindpm function
270 * @bq256xx_set_charge_type: pointer to instance specific set_charge_type function
271 * @bq256xx_set_ts_ignore: pointer to instance specific set_ts_ignore function
272 *
273 * @bq256xx_def_ichg: default ichg value in microamps
274 * @bq256xx_def_iindpm: default iindpm value in microamps
275 * @bq256xx_def_vbatreg: default vbatreg value in microvolts
276 * @bq256xx_def_iterm: default iterm value in microamps
277 * @bq256xx_def_iprechg: default iprechg value in microamps
278 * @bq256xx_def_vindpm: default vindpm value in microvolts
279 *
280 * @bq256xx_max_ichg: maximum charge current in microamps
281 * @bq256xx_max_vbatreg: maximum battery regulation voltage in microvolts
282 *
283 * @has_usb_detect: indicates whether device has BC1.2 detection
284 */
285 struct bq256xx_chip_info {
286 int model_id;
287
288 const struct regmap_config *bq256xx_regmap_config;
289
290 int (*bq256xx_get_ichg)(struct bq256xx_device *bq);
291 int (*bq256xx_get_iindpm)(struct bq256xx_device *bq);
292 int (*bq256xx_get_vbatreg)(struct bq256xx_device *bq);
293 int (*bq256xx_get_iterm)(struct bq256xx_device *bq);
294 int (*bq256xx_get_iprechg)(struct bq256xx_device *bq);
295 int (*bq256xx_get_vindpm)(struct bq256xx_device *bq);
296
297 int (*bq256xx_set_ichg)(struct bq256xx_device *bq, int ichg);
298 int (*bq256xx_set_iindpm)(struct bq256xx_device *bq, int iindpm);
299 int (*bq256xx_set_vbatreg)(struct bq256xx_device *bq, int vbatreg);
300 int (*bq256xx_set_iterm)(struct bq256xx_device *bq, int iterm);
301 int (*bq256xx_set_iprechg)(struct bq256xx_device *bq, int iprechg);
302 int (*bq256xx_set_vindpm)(struct bq256xx_device *bq, int vindpm);
303 int (*bq256xx_set_charge_type)(struct bq256xx_device *bq, int type);
304 int (*bq256xx_set_ts_ignore)(struct bq256xx_device *bq, bool ts_ignore);
305
306 int bq256xx_def_ichg;
307 int bq256xx_def_iindpm;
308 int bq256xx_def_vbatreg;
309 int bq256xx_def_iterm;
310 int bq256xx_def_iprechg;
311 int bq256xx_def_vindpm;
312
313 int bq256xx_max_ichg;
314 int bq256xx_max_vbatreg;
315
316 bool has_usb_detect;
317 };
318
319 static int bq256xx_watchdog_time[BQ256XX_NUM_WD_VAL] = {
320 0, 40000, 80000, 1600000
321 };
322
323 static const int bq25611d_vbatreg_values[] = {
324 3494000, 3590000, 3686000, 3790000, 3894000, 3990000, 4090000, 4140000,
325 4190000
326 };
327
328 static const int bq25618_619_vbatreg_values[] = {
329 3504000, 3600000, 3696000, 3800000, 3904000, 4000000, 4100000, 4150000,
330 4200000
331 };
332
333 static const int bq25618_619_ichg_values[] = {
334 1290000, 1360000, 1430000, 1500000
335 };
336
bq256xx_array_parse(int array_size,int val,const int array[])337 static int bq256xx_array_parse(int array_size, int val, const int array[])
338 {
339 int i = 0;
340
341 if (val < array[i])
342 return i - 1;
343
344 if (val >= array[array_size - 1])
345 return array_size - 1;
346
347 for (i = 1; i < array_size; i++) {
348 if (val == array[i])
349 return i;
350
351 if (val > array[i - 1] && val < array[i]) {
352 if (val < array[i])
353 return i - 1;
354 else
355 return i;
356 }
357 }
358 return -EINVAL;
359 }
360
bq256xx_usb_notifier(struct notifier_block * nb,unsigned long val,void * priv)361 static int bq256xx_usb_notifier(struct notifier_block *nb, unsigned long val,
362 void *priv)
363 {
364 struct bq256xx_device *bq =
365 container_of(nb, struct bq256xx_device, usb_nb);
366
367 bq->usb_event = val;
368 queue_work(system_power_efficient_wq, &bq->usb_work);
369
370 return NOTIFY_OK;
371 }
372
bq256xx_usb_work(struct work_struct * data)373 static void bq256xx_usb_work(struct work_struct *data)
374 {
375 struct bq256xx_device *bq =
376 container_of(data, struct bq256xx_device, usb_work);
377
378 switch (bq->usb_event) {
379 case USB_EVENT_ID:
380 break;
381 case USB_EVENT_NONE:
382 power_supply_changed(bq->charger);
383 break;
384 default:
385 dev_err(bq->dev, "Error switching to charger mode.\n");
386 break;
387 }
388 }
389
390 static struct reg_default bq2560x_reg_defs[] = {
391 {BQ256XX_INPUT_CURRENT_LIMIT, 0x17},
392 {BQ256XX_CHARGER_CONTROL_0, 0x1a},
393 {BQ256XX_CHARGE_CURRENT_LIMIT, 0xa2},
394 {BQ256XX_PRECHG_AND_TERM_CURR_LIM, 0x22},
395 {BQ256XX_BATTERY_VOLTAGE_LIMIT, 0x58},
396 {BQ256XX_CHARGER_CONTROL_1, 0x9f},
397 {BQ256XX_CHARGER_CONTROL_2, 0x66},
398 {BQ256XX_CHARGER_CONTROL_3, 0x4c},
399 };
400
401 static struct reg_default bq25611d_reg_defs[] = {
402 {BQ256XX_INPUT_CURRENT_LIMIT, 0x17},
403 {BQ256XX_CHARGER_CONTROL_0, 0x1a},
404 {BQ256XX_CHARGE_CURRENT_LIMIT, 0x91},
405 {BQ256XX_PRECHG_AND_TERM_CURR_LIM, 0x12},
406 {BQ256XX_BATTERY_VOLTAGE_LIMIT, 0x40},
407 {BQ256XX_CHARGER_CONTROL_1, 0x9e},
408 {BQ256XX_CHARGER_CONTROL_2, 0xe6},
409 {BQ256XX_CHARGER_CONTROL_3, 0x4c},
410 {BQ256XX_PART_INFORMATION, 0x54},
411 {BQ256XX_CHARGER_CONTROL_4, 0x75},
412 };
413
414 static struct reg_default bq25618_619_reg_defs[] = {
415 {BQ256XX_INPUT_CURRENT_LIMIT, 0x17},
416 {BQ256XX_CHARGER_CONTROL_0, 0x1a},
417 {BQ256XX_CHARGE_CURRENT_LIMIT, 0x91},
418 {BQ256XX_PRECHG_AND_TERM_CURR_LIM, 0x12},
419 {BQ256XX_BATTERY_VOLTAGE_LIMIT, 0x40},
420 {BQ256XX_CHARGER_CONTROL_1, 0x9e},
421 {BQ256XX_CHARGER_CONTROL_2, 0xe6},
422 {BQ256XX_CHARGER_CONTROL_3, 0x4c},
423 {BQ256XX_PART_INFORMATION, 0x2c},
424 {BQ256XX_CHARGER_CONTROL_4, 0x75},
425 };
426
bq256xx_get_state(struct bq256xx_device * bq,struct bq256xx_state * state)427 static int bq256xx_get_state(struct bq256xx_device *bq,
428 struct bq256xx_state *state)
429 {
430 unsigned int charger_status_0;
431 unsigned int charger_status_1;
432 int ret;
433
434 ret = regmap_read(bq->regmap, BQ256XX_CHARGER_STATUS_0,
435 &charger_status_0);
436 if (ret)
437 return ret;
438
439 ret = regmap_read(bq->regmap, BQ256XX_CHARGER_STATUS_1,
440 &charger_status_1);
441 if (ret)
442 return ret;
443
444 state->vbus_stat = charger_status_0 & BQ256XX_VBUS_STAT_MASK;
445 state->chrg_stat = charger_status_0 & BQ256XX_CHRG_STAT_MASK;
446 state->online = charger_status_0 & BQ256XX_PG_STAT_MASK;
447
448 state->wdt_fault = charger_status_1 & BQ256XX_WDT_FAULT_MASK;
449 state->bat_fault = charger_status_1 & BQ256XX_BAT_FAULT_MASK;
450 state->chrg_fault = charger_status_1 & BQ256XX_CHRG_FAULT_MASK;
451 state->ntc_fault = charger_status_1 & BQ256XX_NTC_FAULT_MASK;
452
453 return 0;
454 }
455
bq256xx_set_charge_type(struct bq256xx_device * bq,int type)456 static int bq256xx_set_charge_type(struct bq256xx_device *bq, int type)
457 {
458 int chg_config = 0;
459
460 switch (type) {
461 case POWER_SUPPLY_CHARGE_TYPE_NONE:
462 chg_config = 0x0;
463 break;
464 case POWER_SUPPLY_CHARGE_TYPE_TRICKLE:
465 case POWER_SUPPLY_CHARGE_TYPE_FAST:
466 chg_config = 0x1;
467 break;
468 default:
469 return -EINVAL;
470 }
471
472 return regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_0,
473 BQ256XX_CHG_CONFIG_MASK,
474 (chg_config ? 1 : 0) << BQ256XX_CHG_CONFIG_BIT_SHIFT);
475 }
476
bq256xx_get_ichg_curr(struct bq256xx_device * bq)477 static int bq256xx_get_ichg_curr(struct bq256xx_device *bq)
478 {
479 unsigned int charge_current_limit;
480 unsigned int ichg_reg_code;
481 int ret;
482
483 ret = regmap_read(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT,
484 &charge_current_limit);
485 if (ret)
486 return ret;
487
488 ichg_reg_code = charge_current_limit & BQ256XX_ICHG_MASK;
489
490 return ichg_reg_code * BQ256XX_ICHG_STEP_uA;
491 }
492
bq25618_619_get_ichg_curr(struct bq256xx_device * bq)493 static int bq25618_619_get_ichg_curr(struct bq256xx_device *bq)
494 {
495 unsigned int charge_current_limit;
496 unsigned int ichg_reg_code;
497 int ret;
498
499 ret = regmap_read(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT,
500 &charge_current_limit);
501 if (ret)
502 return ret;
503
504 ichg_reg_code = charge_current_limit & BQ256XX_ICHG_MASK;
505
506 if (ichg_reg_code < BQ25618_ICHG_THRESH)
507 return ichg_reg_code * BQ25618_ICHG_STEP_uA;
508
509 return bq25618_619_ichg_values[ichg_reg_code - BQ25618_ICHG_THRESH];
510 }
511
bq256xx_set_ichg_curr(struct bq256xx_device * bq,int ichg)512 static int bq256xx_set_ichg_curr(struct bq256xx_device *bq, int ichg)
513 {
514 unsigned int ichg_reg_code;
515 int ichg_max = bq->init_data.ichg_max;
516
517 ichg = clamp(ichg, BQ256XX_ICHG_MIN_uA, ichg_max);
518 ichg_reg_code = ichg / BQ256XX_ICHG_STEP_uA;
519
520 return regmap_update_bits(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT,
521 BQ256XX_ICHG_MASK, ichg_reg_code);
522 }
523
bq25618_619_set_ichg_curr(struct bq256xx_device * bq,int ichg)524 static int bq25618_619_set_ichg_curr(struct bq256xx_device *bq, int ichg)
525 {
526 int array_size = ARRAY_SIZE(bq25618_619_ichg_values);
527 unsigned int ichg_reg_code;
528 int ichg_max = bq->init_data.ichg_max;
529
530 ichg = clamp(ichg, BQ25618_ICHG_MIN_uA, ichg_max);
531
532 if (ichg <= BQ25618_ICHG_THRESH_uA) {
533 ichg_reg_code = ichg / BQ25618_ICHG_STEP_uA;
534 } else {
535 ichg_reg_code = bq256xx_array_parse(array_size, ichg,
536 bq25618_619_ichg_values) + BQ25618_ICHG_THRESH;
537 }
538
539 return regmap_update_bits(bq->regmap, BQ256XX_CHARGE_CURRENT_LIMIT,
540 BQ256XX_ICHG_MASK, ichg_reg_code);
541 }
542
bq25618_619_get_chrg_volt(struct bq256xx_device * bq)543 static int bq25618_619_get_chrg_volt(struct bq256xx_device *bq)
544 {
545 unsigned int battery_volt_lim;
546 unsigned int vbatreg_reg_code;
547 int ret;
548
549 ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
550 &battery_volt_lim);
551
552 if (ret)
553 return ret;
554
555 vbatreg_reg_code = (battery_volt_lim & BQ256XX_VBATREG_MASK) >>
556 BQ256XX_VBATREG_BIT_SHIFT;
557
558 if (vbatreg_reg_code > BQ2561X_VBATREG_THRESH)
559 return ((vbatreg_reg_code - BQ2561X_VBATREG_THRESH) *
560 BQ2561X_VBATREG_STEP_uV) +
561 BQ25618_VBATREG_THRESH_uV;
562
563 return bq25618_619_vbatreg_values[vbatreg_reg_code];
564 }
565
bq25611d_get_chrg_volt(struct bq256xx_device * bq)566 static int bq25611d_get_chrg_volt(struct bq256xx_device *bq)
567 {
568 unsigned int battery_volt_lim;
569 unsigned int vbatreg_reg_code;
570 int ret;
571
572 ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
573 &battery_volt_lim);
574 if (ret)
575 return ret;
576
577 vbatreg_reg_code = (battery_volt_lim & BQ256XX_VBATREG_MASK) >>
578 BQ256XX_VBATREG_BIT_SHIFT;
579
580 if (vbatreg_reg_code > BQ2561X_VBATREG_THRESH)
581 return ((vbatreg_reg_code - BQ2561X_VBATREG_THRESH) *
582 BQ2561X_VBATREG_STEP_uV) +
583 BQ25611D_VBATREG_THRESH_uV;
584
585 return bq25611d_vbatreg_values[vbatreg_reg_code];
586 }
587
bq2560x_get_chrg_volt(struct bq256xx_device * bq)588 static int bq2560x_get_chrg_volt(struct bq256xx_device *bq)
589 {
590 unsigned int battery_volt_lim;
591 unsigned int vbatreg_reg_code;
592 int ret;
593
594 ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
595 &battery_volt_lim);
596 if (ret)
597 return ret;
598
599 vbatreg_reg_code = (battery_volt_lim & BQ256XX_VBATREG_MASK) >>
600 BQ256XX_VBATREG_BIT_SHIFT;
601
602 return (vbatreg_reg_code * BQ2560X_VBATREG_STEP_uV)
603 + BQ2560X_VBATREG_OFFSET_uV;
604 }
605
bq25601d_get_chrg_volt(struct bq256xx_device * bq)606 static int bq25601d_get_chrg_volt(struct bq256xx_device *bq)
607 {
608 unsigned int battery_volt_lim;
609 unsigned int vbatreg_reg_code;
610 int ret;
611
612 ret = regmap_read(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
613 &battery_volt_lim);
614 if (ret)
615 return ret;
616
617 vbatreg_reg_code = (battery_volt_lim & BQ256XX_VBATREG_MASK) >>
618 BQ256XX_VBATREG_BIT_SHIFT;
619
620 return (vbatreg_reg_code * BQ2560X_VBATREG_STEP_uV)
621 + BQ25601D_VBATREG_OFFSET_uV;
622 }
623
bq25618_619_set_chrg_volt(struct bq256xx_device * bq,int vbatreg)624 static int bq25618_619_set_chrg_volt(struct bq256xx_device *bq, int vbatreg)
625 {
626 int array_size = ARRAY_SIZE(bq25618_619_vbatreg_values);
627 unsigned int vbatreg_reg_code;
628 int vbatreg_max = bq->init_data.vbatreg_max;
629
630 vbatreg = clamp(vbatreg, BQ25618_VBATREG_MIN_uV, vbatreg_max);
631
632 if (vbatreg > BQ25618_VBATREG_THRESH_uV)
633 vbatreg_reg_code = ((vbatreg -
634 BQ25618_VBATREG_THRESH_uV) /
635 (BQ2561X_VBATREG_STEP_uV)) + BQ2561X_VBATREG_THRESH;
636 else {
637 vbatreg_reg_code = bq256xx_array_parse(array_size, vbatreg,
638 bq25618_619_vbatreg_values);
639 }
640
641 return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
642 BQ256XX_VBATREG_MASK, vbatreg_reg_code <<
643 BQ256XX_VBATREG_BIT_SHIFT);
644 }
645
bq25611d_set_chrg_volt(struct bq256xx_device * bq,int vbatreg)646 static int bq25611d_set_chrg_volt(struct bq256xx_device *bq, int vbatreg)
647 {
648 int array_size = ARRAY_SIZE(bq25611d_vbatreg_values);
649 unsigned int vbatreg_reg_code;
650 int vbatreg_max = bq->init_data.vbatreg_max;
651
652 vbatreg = clamp(vbatreg, BQ25611D_VBATREG_MIN_uV, vbatreg_max);
653
654 if (vbatreg > BQ25611D_VBATREG_THRESH_uV)
655 vbatreg_reg_code = ((vbatreg -
656 BQ25611D_VBATREG_THRESH_uV) /
657 (BQ2561X_VBATREG_STEP_uV)) + BQ2561X_VBATREG_THRESH;
658 else {
659 vbatreg_reg_code = bq256xx_array_parse(array_size, vbatreg,
660 bq25611d_vbatreg_values);
661 }
662
663 return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
664 BQ256XX_VBATREG_MASK, vbatreg_reg_code <<
665 BQ256XX_VBATREG_BIT_SHIFT);
666 }
667
bq2560x_set_chrg_volt(struct bq256xx_device * bq,int vbatreg)668 static int bq2560x_set_chrg_volt(struct bq256xx_device *bq, int vbatreg)
669 {
670 unsigned int vbatreg_reg_code;
671 int vbatreg_max = bq->init_data.vbatreg_max;
672
673 vbatreg = clamp(vbatreg, BQ2560X_VBATREG_MIN_uV, vbatreg_max);
674
675 vbatreg_reg_code = (vbatreg - BQ2560X_VBATREG_OFFSET_uV) /
676 BQ2560X_VBATREG_STEP_uV;
677
678 return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
679 BQ256XX_VBATREG_MASK, vbatreg_reg_code <<
680 BQ256XX_VBATREG_BIT_SHIFT);
681 }
682
bq25601d_set_chrg_volt(struct bq256xx_device * bq,int vbatreg)683 static int bq25601d_set_chrg_volt(struct bq256xx_device *bq, int vbatreg)
684 {
685 unsigned int vbatreg_reg_code;
686 int vbatreg_max = bq->init_data.vbatreg_max;
687
688 vbatreg = clamp(vbatreg, BQ25601D_VBATREG_MIN_uV, vbatreg_max);
689
690 vbatreg_reg_code = (vbatreg - BQ25601D_VBATREG_OFFSET_uV) /
691 BQ2560X_VBATREG_STEP_uV;
692
693 return regmap_update_bits(bq->regmap, BQ256XX_BATTERY_VOLTAGE_LIMIT,
694 BQ256XX_VBATREG_MASK, vbatreg_reg_code <<
695 BQ256XX_VBATREG_BIT_SHIFT);
696 }
697
bq256xx_set_ts_ignore(struct bq256xx_device * bq,bool ts_ignore)698 static int bq256xx_set_ts_ignore(struct bq256xx_device *bq, bool ts_ignore)
699 {
700 return regmap_update_bits(bq->regmap, BQ256XX_INPUT_CURRENT_LIMIT,
701 BQ256XX_TS_IGNORE, (ts_ignore ? 1 : 0) << BQ256XX_TS_IGNORE_SHIFT);
702 }
703
bq256xx_get_prechrg_curr(struct bq256xx_device * bq)704 static int bq256xx_get_prechrg_curr(struct bq256xx_device *bq)
705 {
706 unsigned int prechg_and_term_curr_lim;
707 unsigned int iprechg_reg_code;
708 int ret;
709
710 ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
711 &prechg_and_term_curr_lim);
712 if (ret)
713 return ret;
714
715 iprechg_reg_code = (prechg_and_term_curr_lim & BQ256XX_IPRECHG_MASK)
716 >> BQ256XX_IPRECHG_BIT_SHIFT;
717
718 return (iprechg_reg_code * BQ256XX_IPRECHG_STEP_uA) +
719 BQ256XX_IPRECHG_OFFSET_uA;
720 }
721
bq256xx_set_prechrg_curr(struct bq256xx_device * bq,int iprechg)722 static int bq256xx_set_prechrg_curr(struct bq256xx_device *bq, int iprechg)
723 {
724 unsigned int iprechg_reg_code;
725
726 iprechg = clamp(iprechg, BQ256XX_IPRECHG_MIN_uA,
727 BQ256XX_IPRECHG_MAX_uA);
728
729 iprechg_reg_code = ((iprechg - BQ256XX_IPRECHG_OFFSET_uA) /
730 BQ256XX_IPRECHG_STEP_uA) << BQ256XX_IPRECHG_BIT_SHIFT;
731
732 return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
733 BQ256XX_IPRECHG_MASK, iprechg_reg_code);
734 }
735
bq25618_619_get_prechrg_curr(struct bq256xx_device * bq)736 static int bq25618_619_get_prechrg_curr(struct bq256xx_device *bq)
737 {
738 unsigned int prechg_and_term_curr_lim;
739 unsigned int iprechg_reg_code;
740 int ret;
741
742 ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
743 &prechg_and_term_curr_lim);
744 if (ret)
745 return ret;
746
747 iprechg_reg_code = (prechg_and_term_curr_lim & BQ256XX_IPRECHG_MASK)
748 >> BQ256XX_IPRECHG_BIT_SHIFT;
749
750 return (iprechg_reg_code * BQ25618_IPRECHG_STEP_uA) +
751 BQ25618_IPRECHG_OFFSET_uA;
752 }
753
bq25618_619_set_prechrg_curr(struct bq256xx_device * bq,int iprechg)754 static int bq25618_619_set_prechrg_curr(struct bq256xx_device *bq, int iprechg)
755 {
756 unsigned int iprechg_reg_code;
757
758 iprechg = clamp(iprechg, BQ25618_IPRECHG_MIN_uA,
759 BQ25618_IPRECHG_MAX_uA);
760
761 iprechg_reg_code = ((iprechg - BQ25618_IPRECHG_OFFSET_uA) /
762 BQ25618_IPRECHG_STEP_uA) << BQ256XX_IPRECHG_BIT_SHIFT;
763
764 return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
765 BQ256XX_IPRECHG_MASK, iprechg_reg_code);
766 }
767
bq256xx_get_term_curr(struct bq256xx_device * bq)768 static int bq256xx_get_term_curr(struct bq256xx_device *bq)
769 {
770 unsigned int prechg_and_term_curr_lim;
771 unsigned int iterm_reg_code;
772 int ret;
773
774 ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
775 &prechg_and_term_curr_lim);
776 if (ret)
777 return ret;
778
779 iterm_reg_code = prechg_and_term_curr_lim & BQ256XX_ITERM_MASK;
780
781 return (iterm_reg_code * BQ256XX_ITERM_STEP_uA) +
782 BQ256XX_ITERM_OFFSET_uA;
783 }
784
bq256xx_set_term_curr(struct bq256xx_device * bq,int iterm)785 static int bq256xx_set_term_curr(struct bq256xx_device *bq, int iterm)
786 {
787 unsigned int iterm_reg_code;
788
789 iterm = clamp(iterm, BQ256XX_ITERM_MIN_uA, BQ256XX_ITERM_MAX_uA);
790
791 iterm_reg_code = (iterm - BQ256XX_ITERM_OFFSET_uA) /
792 BQ256XX_ITERM_STEP_uA;
793
794 return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
795 BQ256XX_ITERM_MASK, iterm_reg_code);
796 }
797
bq25618_619_get_term_curr(struct bq256xx_device * bq)798 static int bq25618_619_get_term_curr(struct bq256xx_device *bq)
799 {
800 unsigned int prechg_and_term_curr_lim;
801 unsigned int iterm_reg_code;
802 int ret;
803
804 ret = regmap_read(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
805 &prechg_and_term_curr_lim);
806 if (ret)
807 return ret;
808
809 iterm_reg_code = prechg_and_term_curr_lim & BQ256XX_ITERM_MASK;
810
811 return (iterm_reg_code * BQ25618_ITERM_STEP_uA) +
812 BQ25618_ITERM_OFFSET_uA;
813 }
814
bq25618_619_set_term_curr(struct bq256xx_device * bq,int iterm)815 static int bq25618_619_set_term_curr(struct bq256xx_device *bq, int iterm)
816 {
817 unsigned int iterm_reg_code;
818
819 iterm = clamp(iterm, BQ25618_ITERM_MIN_uA, BQ25618_ITERM_MAX_uA);
820
821 iterm_reg_code = (iterm - BQ25618_ITERM_OFFSET_uA) /
822 BQ25618_ITERM_STEP_uA;
823
824 return regmap_update_bits(bq->regmap, BQ256XX_PRECHG_AND_TERM_CURR_LIM,
825 BQ256XX_ITERM_MASK, iterm_reg_code);
826 }
827
bq256xx_get_input_volt_lim(struct bq256xx_device * bq)828 static int bq256xx_get_input_volt_lim(struct bq256xx_device *bq)
829 {
830 unsigned int charger_control_2;
831 unsigned int vindpm_reg_code;
832 int ret;
833
834 ret = regmap_read(bq->regmap, BQ256XX_CHARGER_CONTROL_2,
835 &charger_control_2);
836 if (ret)
837 return ret;
838
839 vindpm_reg_code = charger_control_2 & BQ256XX_VINDPM_MASK;
840
841 return (vindpm_reg_code * BQ256XX_VINDPM_STEP_uV) +
842 BQ256XX_VINDPM_OFFSET_uV;
843 }
844
bq256xx_set_input_volt_lim(struct bq256xx_device * bq,int vindpm)845 static int bq256xx_set_input_volt_lim(struct bq256xx_device *bq, int vindpm)
846 {
847 unsigned int vindpm_reg_code;
848
849 vindpm = clamp(vindpm, BQ256XX_VINDPM_MIN_uV, BQ256XX_VINDPM_MAX_uV);
850
851 vindpm_reg_code = (vindpm - BQ256XX_VINDPM_OFFSET_uV) /
852 BQ256XX_VINDPM_STEP_uV;
853
854 return regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_2,
855 BQ256XX_VINDPM_MASK, vindpm_reg_code);
856 }
857
bq256xx_get_input_curr_lim(struct bq256xx_device * bq)858 static int bq256xx_get_input_curr_lim(struct bq256xx_device *bq)
859 {
860 unsigned int input_current_limit;
861 unsigned int iindpm_reg_code;
862 int ret;
863
864 ret = regmap_read(bq->regmap, BQ256XX_INPUT_CURRENT_LIMIT,
865 &input_current_limit);
866 if (ret)
867 return ret;
868
869 iindpm_reg_code = input_current_limit & BQ256XX_IINDPM_MASK;
870
871 return (iindpm_reg_code * BQ256XX_IINDPM_STEP_uA) +
872 BQ256XX_IINDPM_OFFSET_uA;
873 }
874
bq256xx_set_input_curr_lim(struct bq256xx_device * bq,int iindpm)875 static int bq256xx_set_input_curr_lim(struct bq256xx_device *bq, int iindpm)
876 {
877 unsigned int iindpm_reg_code;
878
879 iindpm = clamp(iindpm, BQ256XX_IINDPM_MIN_uA, BQ256XX_IINDPM_MAX_uA);
880
881 iindpm_reg_code = (iindpm - BQ256XX_IINDPM_OFFSET_uA) /
882 BQ256XX_IINDPM_STEP_uA;
883
884 return regmap_update_bits(bq->regmap, BQ256XX_INPUT_CURRENT_LIMIT,
885 BQ256XX_IINDPM_MASK, iindpm_reg_code);
886 }
887
bq256xx_charger_reset(void * data)888 static void bq256xx_charger_reset(void *data)
889 {
890 struct bq256xx_device *bq = data;
891
892 regmap_update_bits(bq->regmap, BQ256XX_PART_INFORMATION,
893 BQ256XX_REG_RST, BQ256XX_REG_RST);
894
895 if (!IS_ERR_OR_NULL(bq->usb2_phy))
896 usb_unregister_notifier(bq->usb2_phy, &bq->usb_nb);
897
898 if (!IS_ERR_OR_NULL(bq->usb3_phy))
899 usb_unregister_notifier(bq->usb3_phy, &bq->usb_nb);
900 }
901
bq256xx_set_charger_property(struct power_supply * psy,enum power_supply_property prop,const union power_supply_propval * val)902 static int bq256xx_set_charger_property(struct power_supply *psy,
903 enum power_supply_property prop,
904 const union power_supply_propval *val)
905 {
906 struct bq256xx_device *bq = power_supply_get_drvdata(psy);
907 int ret = -EINVAL;
908
909 switch (prop) {
910 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
911 ret = bq->chip_info->bq256xx_set_iindpm(bq, val->intval);
912 if (ret)
913 return ret;
914 break;
915
916 case POWER_SUPPLY_PROP_STATUS:
917 break;
918
919 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
920 ret = bq->chip_info->bq256xx_set_vbatreg(bq, val->intval);
921 if (ret)
922 return ret;
923 break;
924
925 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
926 ret = bq->chip_info->bq256xx_set_ichg(bq, val->intval);
927 if (ret)
928 return ret;
929 break;
930
931 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
932 ret = bq->chip_info->bq256xx_set_iprechg(bq, val->intval);
933 if (ret)
934 return ret;
935 break;
936
937 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
938 ret = bq->chip_info->bq256xx_set_iterm(bq, val->intval);
939 if (ret)
940 return ret;
941 break;
942
943 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
944 ret = bq->chip_info->bq256xx_set_vindpm(bq, val->intval);
945 if (ret)
946 return ret;
947 break;
948
949 case POWER_SUPPLY_PROP_CHARGE_TYPE:
950 ret = bq->chip_info->bq256xx_set_charge_type(bq, val->intval);
951 if (ret)
952 return ret;
953 break;
954
955 default:
956 break;
957 }
958
959 return ret;
960 }
961
962
bq256xx_get_battery_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)963 static int bq256xx_get_battery_property(struct power_supply *psy,
964 enum power_supply_property psp,
965 union power_supply_propval *val)
966 {
967 struct bq256xx_device *bq = power_supply_get_drvdata(psy);
968
969 switch (psp) {
970 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
971 val->intval = bq->init_data.ichg_max;
972 break;
973
974 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
975 val->intval = bq->init_data.vbatreg_max;
976 break;
977
978 default:
979 return -EINVAL;
980 }
981
982 return 0;
983 }
984
bq256xx_get_charger_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)985 static int bq256xx_get_charger_property(struct power_supply *psy,
986 enum power_supply_property psp,
987 union power_supply_propval *val)
988 {
989 struct bq256xx_device *bq = power_supply_get_drvdata(psy);
990 struct bq256xx_state state;
991 int ret = 0;
992
993 mutex_lock(&bq->lock);
994 ret = bq256xx_get_state(bq, &state);
995 mutex_unlock(&bq->lock);
996 if (ret)
997 return ret;
998
999 switch (psp) {
1000 case POWER_SUPPLY_PROP_STATUS:
1001 if (state.vbus_stat == BQ256XX_VBUS_STAT_NO_INPUT ||
1002 state.vbus_stat == BQ256XX_VBUS_STAT_USB_OTG)
1003 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1004 else if (state.chrg_stat == BQ256XX_CHRG_STAT_NOT_CHRGING)
1005 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1006 else if (state.chrg_stat == BQ256XX_CHRG_STAT_CHRG_TERM)
1007 val->intval = POWER_SUPPLY_STATUS_FULL;
1008 else
1009 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1010 break;
1011
1012 case POWER_SUPPLY_PROP_HEALTH:
1013 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
1014 if (state.wdt_fault) {
1015 val->intval =
1016 POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE;
1017 } else if (state.bat_fault) {
1018 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1019 } else {
1020 switch (state.chrg_stat) {
1021 case BQ256XX_CHRG_FAULT_INPUT:
1022 val->intval =
1023 POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1024 break;
1025 case BQ256XX_CHRG_FAULT_THERM:
1026 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1027 break;
1028 case BQ256XX_CHRG_FAULT_CST_EXPIRE:
1029 val->intval =
1030 POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
1031 break;
1032 default:
1033 break;
1034 }
1035
1036 switch (state.ntc_fault) {
1037 case BQ256XX_NTC_FAULT_WARM:
1038 val->intval = POWER_SUPPLY_HEALTH_WARM;
1039 break;
1040 case BQ256XX_NTC_FAULT_COOL:
1041 val->intval = POWER_SUPPLY_HEALTH_COOL;
1042 break;
1043 case BQ256XX_NTC_FAULT_COLD:
1044 val->intval = POWER_SUPPLY_HEALTH_COLD;
1045 break;
1046 case BQ256XX_NTC_FAULT_HOT:
1047 val->intval = POWER_SUPPLY_HEALTH_HOT;
1048 break;
1049 default:
1050 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1051 break;
1052 }
1053 }
1054 break;
1055
1056 case POWER_SUPPLY_PROP_USB_TYPE:
1057 if (bq->chip_info->has_usb_detect) {
1058 switch (state.vbus_stat) {
1059 case BQ256XX_VBUS_STAT_USB_SDP:
1060 val->intval = POWER_SUPPLY_USB_TYPE_SDP;
1061 break;
1062 case BQ256XX_VBUS_STAT_USB_CDP:
1063 val->intval = POWER_SUPPLY_USB_TYPE_CDP;
1064 break;
1065 case BQ256XX_VBUS_STAT_USB_DCP:
1066 val->intval = POWER_SUPPLY_USB_TYPE_DCP;
1067 break;
1068 case BQ256XX_VBUS_STAT_USB_OTG:
1069 val->intval = POWER_SUPPLY_USB_TYPE_ACA;
1070 break;
1071 default:
1072 val->intval = POWER_SUPPLY_USB_TYPE_UNKNOWN;
1073 break;
1074 }
1075 } else {
1076 switch (state.vbus_stat) {
1077 case BQ256XX_VBUS_STAT_USB_SDP:
1078 val->intval = POWER_SUPPLY_USB_TYPE_SDP;
1079 break;
1080 case BQ256XX_VBUS_STAT_USB_OTG:
1081 val->intval = POWER_SUPPLY_USB_TYPE_ACA;
1082 break;
1083 default:
1084 val->intval = POWER_SUPPLY_USB_TYPE_UNKNOWN;
1085 break;
1086 }
1087 }
1088 break;
1089
1090 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1091 switch (state.chrg_stat) {
1092 case BQ256XX_CHRG_STAT_NOT_CHRGING:
1093 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
1094 break;
1095 case BQ256XX_CHRG_STAT_PRECHRGING:
1096 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1097 break;
1098 case BQ256XX_CHRG_STAT_FAST_CHRGING:
1099 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
1100 break;
1101 case BQ256XX_CHRG_STAT_CHRG_TERM:
1102 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
1103 break;
1104 default:
1105 val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
1106 }
1107 break;
1108
1109 case POWER_SUPPLY_PROP_MANUFACTURER:
1110 val->strval = BQ256XX_MANUFACTURER;
1111 break;
1112
1113 case POWER_SUPPLY_PROP_MODEL_NAME:
1114 val->strval = bq->model_name;
1115 break;
1116
1117 case POWER_SUPPLY_PROP_ONLINE:
1118 val->intval = state.online;
1119 break;
1120
1121 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
1122 ret = bq->chip_info->bq256xx_get_vindpm(bq);
1123 if (ret < 0)
1124 return ret;
1125 val->intval = ret;
1126 break;
1127
1128 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1129 ret = bq->chip_info->bq256xx_get_iindpm(bq);
1130 if (ret < 0)
1131 return ret;
1132 val->intval = ret;
1133 break;
1134
1135 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1136 ret = bq->chip_info->bq256xx_get_vbatreg(bq);
1137 if (ret < 0)
1138 return ret;
1139 val->intval = ret;
1140 break;
1141
1142 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1143 ret = bq->chip_info->bq256xx_get_ichg(bq);
1144 if (ret < 0)
1145 return ret;
1146 val->intval = ret;
1147 break;
1148
1149 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
1150 ret = bq->chip_info->bq256xx_get_iprechg(bq);
1151 if (ret < 0)
1152 return ret;
1153 val->intval = ret;
1154 break;
1155
1156 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
1157 ret = bq->chip_info->bq256xx_get_iterm(bq);
1158 if (ret < 0)
1159 return ret;
1160 val->intval = ret;
1161 break;
1162
1163 default:
1164 return -EINVAL;
1165 }
1166
1167 return ret;
1168 }
1169
bq256xx_state_changed(struct bq256xx_device * bq,struct bq256xx_state * new_state)1170 static bool bq256xx_state_changed(struct bq256xx_device *bq,
1171 struct bq256xx_state *new_state)
1172 {
1173 struct bq256xx_state old_state;
1174
1175 mutex_lock(&bq->lock);
1176 old_state = bq->state;
1177 mutex_unlock(&bq->lock);
1178
1179 return memcmp(&old_state, new_state, sizeof(struct bq256xx_state)) != 0;
1180 }
1181
bq256xx_irq_handler_thread(int irq,void * private)1182 static irqreturn_t bq256xx_irq_handler_thread(int irq, void *private)
1183 {
1184 struct bq256xx_device *bq = private;
1185 struct bq256xx_state state;
1186 int ret;
1187
1188 ret = bq256xx_get_state(bq, &state);
1189 if (ret < 0)
1190 goto irq_out;
1191
1192 if (!bq256xx_state_changed(bq, &state))
1193 goto irq_out;
1194
1195 mutex_lock(&bq->lock);
1196 bq->state = state;
1197 mutex_unlock(&bq->lock);
1198
1199 power_supply_changed(bq->charger);
1200
1201 irq_out:
1202 return IRQ_HANDLED;
1203 }
1204
1205 static enum power_supply_property bq256xx_power_supply_props[] = {
1206 POWER_SUPPLY_PROP_MANUFACTURER,
1207 POWER_SUPPLY_PROP_MODEL_NAME,
1208 POWER_SUPPLY_PROP_STATUS,
1209 POWER_SUPPLY_PROP_ONLINE,
1210 POWER_SUPPLY_PROP_HEALTH,
1211 POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT,
1212 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
1213 POWER_SUPPLY_PROP_CHARGE_TYPE,
1214 POWER_SUPPLY_PROP_USB_TYPE,
1215 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1216 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1217 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
1218 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
1219 };
1220
1221 static enum power_supply_property bq256xx_battery_props[] = {
1222 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
1223 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
1224 };
1225
bq256xx_property_is_writeable(struct power_supply * psy,enum power_supply_property prop)1226 static int bq256xx_property_is_writeable(struct power_supply *psy,
1227 enum power_supply_property prop)
1228 {
1229 switch (prop) {
1230 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1231 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1232 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1233 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
1234 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
1235 case POWER_SUPPLY_PROP_STATUS:
1236 case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT:
1237 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1238 return true;
1239 default:
1240 return false;
1241 }
1242 }
1243
1244 static const struct power_supply_desc bq256xx_power_supply_desc = {
1245 .name = "bq256xx-charger",
1246 .type = POWER_SUPPLY_TYPE_USB,
1247 .usb_types = BIT(POWER_SUPPLY_USB_TYPE_SDP) |
1248 BIT(POWER_SUPPLY_USB_TYPE_CDP) |
1249 BIT(POWER_SUPPLY_USB_TYPE_DCP) |
1250 BIT(POWER_SUPPLY_USB_TYPE_ACA) |
1251 BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN),
1252 .properties = bq256xx_power_supply_props,
1253 .num_properties = ARRAY_SIZE(bq256xx_power_supply_props),
1254 .get_property = bq256xx_get_charger_property,
1255 .set_property = bq256xx_set_charger_property,
1256 .property_is_writeable = bq256xx_property_is_writeable,
1257 };
1258
1259 static struct power_supply_desc bq256xx_battery_desc = {
1260 .name = "bq256xx-battery",
1261 .type = POWER_SUPPLY_TYPE_BATTERY,
1262 .get_property = bq256xx_get_battery_property,
1263 .properties = bq256xx_battery_props,
1264 .num_properties = ARRAY_SIZE(bq256xx_battery_props),
1265 .property_is_writeable = bq256xx_property_is_writeable,
1266 };
1267
1268
bq256xx_is_volatile_reg(struct device * dev,unsigned int reg)1269 static bool bq256xx_is_volatile_reg(struct device *dev, unsigned int reg)
1270 {
1271 switch (reg) {
1272 case BQ256XX_INPUT_CURRENT_LIMIT:
1273 case BQ256XX_CHARGER_STATUS_0...BQ256XX_CHARGER_STATUS_2:
1274 return true;
1275 default:
1276 return false;
1277 }
1278 }
1279
1280 static const struct regmap_config bq25600_regmap_config = {
1281 .reg_bits = 8,
1282 .val_bits = 8,
1283
1284 .max_register = BQ256XX_PART_INFORMATION,
1285 .reg_defaults = bq2560x_reg_defs,
1286 .num_reg_defaults = ARRAY_SIZE(bq2560x_reg_defs),
1287 .cache_type = REGCACHE_FLAT,
1288 .volatile_reg = bq256xx_is_volatile_reg,
1289 };
1290
1291 static const struct regmap_config bq25611d_regmap_config = {
1292 .reg_bits = 8,
1293 .val_bits = 8,
1294
1295 .max_register = BQ256XX_CHARGER_CONTROL_4,
1296 .reg_defaults = bq25611d_reg_defs,
1297 .num_reg_defaults = ARRAY_SIZE(bq25611d_reg_defs),
1298 .cache_type = REGCACHE_FLAT,
1299 .volatile_reg = bq256xx_is_volatile_reg,
1300 };
1301
1302 static const struct regmap_config bq25618_619_regmap_config = {
1303 .reg_bits = 8,
1304 .val_bits = 8,
1305
1306 .max_register = BQ256XX_CHARGER_CONTROL_4,
1307 .reg_defaults = bq25618_619_reg_defs,
1308 .num_reg_defaults = ARRAY_SIZE(bq25618_619_reg_defs),
1309 .cache_type = REGCACHE_FLAT,
1310 .volatile_reg = bq256xx_is_volatile_reg,
1311 };
1312
1313 static const struct bq256xx_chip_info bq256xx_chip_info_tbl[] = {
1314 [BQ25600] = {
1315 .model_id = BQ25600,
1316 .bq256xx_regmap_config = &bq25600_regmap_config,
1317 .bq256xx_get_ichg = bq256xx_get_ichg_curr,
1318 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1319 .bq256xx_get_vbatreg = bq2560x_get_chrg_volt,
1320 .bq256xx_get_iterm = bq256xx_get_term_curr,
1321 .bq256xx_get_iprechg = bq256xx_get_prechrg_curr,
1322 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1323 .bq256xx_set_ts_ignore = NULL,
1324
1325 .bq256xx_set_ichg = bq256xx_set_ichg_curr,
1326 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1327 .bq256xx_set_vbatreg = bq2560x_set_chrg_volt,
1328 .bq256xx_set_iterm = bq256xx_set_term_curr,
1329 .bq256xx_set_iprechg = bq256xx_set_prechrg_curr,
1330 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1331 .bq256xx_set_charge_type = bq256xx_set_charge_type,
1332
1333 .bq256xx_def_ichg = BQ2560X_ICHG_DEF_uA,
1334 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1335 .bq256xx_def_vbatreg = BQ2560X_VBATREG_DEF_uV,
1336 .bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA,
1337 .bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA,
1338 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1339
1340 .bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA,
1341 .bq256xx_max_vbatreg = BQ2560X_VBATREG_MAX_uV,
1342
1343 .has_usb_detect = false,
1344 },
1345
1346 [BQ25600D] = {
1347 .model_id = BQ25600D,
1348 .bq256xx_regmap_config = &bq25600_regmap_config,
1349 .bq256xx_get_ichg = bq256xx_get_ichg_curr,
1350 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1351 .bq256xx_get_vbatreg = bq2560x_get_chrg_volt,
1352 .bq256xx_get_iterm = bq256xx_get_term_curr,
1353 .bq256xx_get_iprechg = bq256xx_get_prechrg_curr,
1354 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1355
1356 .bq256xx_set_ichg = bq256xx_set_ichg_curr,
1357 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1358 .bq256xx_set_vbatreg = bq2560x_set_chrg_volt,
1359 .bq256xx_set_iterm = bq256xx_set_term_curr,
1360 .bq256xx_set_iprechg = bq256xx_set_prechrg_curr,
1361 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1362 .bq256xx_set_charge_type = bq256xx_set_charge_type,
1363 .bq256xx_set_ts_ignore = NULL,
1364
1365 .bq256xx_def_ichg = BQ2560X_ICHG_DEF_uA,
1366 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1367 .bq256xx_def_vbatreg = BQ2560X_VBATREG_DEF_uV,
1368 .bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA,
1369 .bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA,
1370 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1371
1372 .bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA,
1373 .bq256xx_max_vbatreg = BQ2560X_VBATREG_MAX_uV,
1374
1375 .has_usb_detect = true,
1376 },
1377
1378 [BQ25601] = {
1379 .model_id = BQ25601,
1380 .bq256xx_regmap_config = &bq25600_regmap_config,
1381 .bq256xx_get_ichg = bq256xx_get_ichg_curr,
1382 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1383 .bq256xx_get_vbatreg = bq2560x_get_chrg_volt,
1384 .bq256xx_get_iterm = bq256xx_get_term_curr,
1385 .bq256xx_get_iprechg = bq256xx_get_prechrg_curr,
1386 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1387
1388 .bq256xx_set_ichg = bq256xx_set_ichg_curr,
1389 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1390 .bq256xx_set_vbatreg = bq2560x_set_chrg_volt,
1391 .bq256xx_set_iterm = bq256xx_set_term_curr,
1392 .bq256xx_set_iprechg = bq256xx_set_prechrg_curr,
1393 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1394 .bq256xx_set_charge_type = bq256xx_set_charge_type,
1395 .bq256xx_set_ts_ignore = NULL,
1396
1397 .bq256xx_def_ichg = BQ2560X_ICHG_DEF_uA,
1398 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1399 .bq256xx_def_vbatreg = BQ2560X_VBATREG_DEF_uV,
1400 .bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA,
1401 .bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA,
1402 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1403
1404 .bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA,
1405 .bq256xx_max_vbatreg = BQ2560X_VBATREG_MAX_uV,
1406
1407 .has_usb_detect = false,
1408 },
1409
1410 [BQ25601D] = {
1411 .model_id = BQ25601D,
1412 .bq256xx_regmap_config = &bq25600_regmap_config,
1413 .bq256xx_get_ichg = bq256xx_get_ichg_curr,
1414 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1415 .bq256xx_get_vbatreg = bq25601d_get_chrg_volt,
1416 .bq256xx_get_iterm = bq256xx_get_term_curr,
1417 .bq256xx_get_iprechg = bq256xx_get_prechrg_curr,
1418 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1419
1420 .bq256xx_set_ichg = bq256xx_set_ichg_curr,
1421 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1422 .bq256xx_set_vbatreg = bq25601d_set_chrg_volt,
1423 .bq256xx_set_iterm = bq256xx_set_term_curr,
1424 .bq256xx_set_iprechg = bq256xx_set_prechrg_curr,
1425 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1426 .bq256xx_set_charge_type = bq256xx_set_charge_type,
1427 .bq256xx_set_ts_ignore = NULL,
1428
1429 .bq256xx_def_ichg = BQ2560X_ICHG_DEF_uA,
1430 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1431 .bq256xx_def_vbatreg = BQ2560X_VBATREG_DEF_uV,
1432 .bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA,
1433 .bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA,
1434 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1435
1436 .bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA,
1437 .bq256xx_max_vbatreg = BQ2560X_VBATREG_MAX_uV,
1438
1439 .has_usb_detect = true,
1440 },
1441
1442 [BQ25611D] = {
1443 .model_id = BQ25611D,
1444 .bq256xx_regmap_config = &bq25611d_regmap_config,
1445 .bq256xx_get_ichg = bq256xx_get_ichg_curr,
1446 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1447 .bq256xx_get_vbatreg = bq25611d_get_chrg_volt,
1448 .bq256xx_get_iterm = bq256xx_get_term_curr,
1449 .bq256xx_get_iprechg = bq256xx_get_prechrg_curr,
1450 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1451
1452 .bq256xx_set_ichg = bq256xx_set_ichg_curr,
1453 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1454 .bq256xx_set_vbatreg = bq25611d_set_chrg_volt,
1455 .bq256xx_set_iterm = bq256xx_set_term_curr,
1456 .bq256xx_set_iprechg = bq256xx_set_prechrg_curr,
1457 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1458 .bq256xx_set_charge_type = bq256xx_set_charge_type,
1459 .bq256xx_set_ts_ignore = bq256xx_set_ts_ignore,
1460
1461 .bq256xx_def_ichg = BQ25611D_ICHG_DEF_uA,
1462 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1463 .bq256xx_def_vbatreg = BQ25611D_VBATREG_DEF_uV,
1464 .bq256xx_def_iterm = BQ256XX_ITERM_DEF_uA,
1465 .bq256xx_def_iprechg = BQ256XX_IPRECHG_DEF_uA,
1466 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1467
1468 .bq256xx_max_ichg = BQ256XX_ICHG_MAX_uA,
1469 .bq256xx_max_vbatreg = BQ25611D_VBATREG_MAX_uV,
1470
1471 .has_usb_detect = true,
1472 },
1473
1474 [BQ25618] = {
1475 .model_id = BQ25618,
1476 .bq256xx_regmap_config = &bq25618_619_regmap_config,
1477 .bq256xx_get_ichg = bq25618_619_get_ichg_curr,
1478 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1479 .bq256xx_get_vbatreg = bq25618_619_get_chrg_volt,
1480 .bq256xx_get_iterm = bq25618_619_get_term_curr,
1481 .bq256xx_get_iprechg = bq25618_619_get_prechrg_curr,
1482 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1483
1484 .bq256xx_set_ichg = bq25618_619_set_ichg_curr,
1485 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1486 .bq256xx_set_vbatreg = bq25618_619_set_chrg_volt,
1487 .bq256xx_set_iterm = bq25618_619_set_term_curr,
1488 .bq256xx_set_iprechg = bq25618_619_set_prechrg_curr,
1489 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1490 .bq256xx_set_charge_type = bq256xx_set_charge_type,
1491 .bq256xx_set_ts_ignore = bq256xx_set_ts_ignore,
1492
1493 .bq256xx_def_ichg = BQ25618_ICHG_DEF_uA,
1494 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1495 .bq256xx_def_vbatreg = BQ25618_VBATREG_DEF_uV,
1496 .bq256xx_def_iterm = BQ25618_ITERM_DEF_uA,
1497 .bq256xx_def_iprechg = BQ25618_IPRECHG_DEF_uA,
1498 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1499
1500 .bq256xx_max_ichg = BQ25618_ICHG_MAX_uA,
1501 .bq256xx_max_vbatreg = BQ25618_VBATREG_MAX_uV,
1502
1503 .has_usb_detect = false,
1504 },
1505
1506 [BQ25619] = {
1507 .model_id = BQ25619,
1508 .bq256xx_regmap_config = &bq25618_619_regmap_config,
1509 .bq256xx_get_ichg = bq25618_619_get_ichg_curr,
1510 .bq256xx_get_iindpm = bq256xx_get_input_curr_lim,
1511 .bq256xx_get_vbatreg = bq25618_619_get_chrg_volt,
1512 .bq256xx_get_iterm = bq25618_619_get_term_curr,
1513 .bq256xx_get_iprechg = bq25618_619_get_prechrg_curr,
1514 .bq256xx_get_vindpm = bq256xx_get_input_volt_lim,
1515
1516 .bq256xx_set_ichg = bq25618_619_set_ichg_curr,
1517 .bq256xx_set_iindpm = bq256xx_set_input_curr_lim,
1518 .bq256xx_set_vbatreg = bq25618_619_set_chrg_volt,
1519 .bq256xx_set_iterm = bq25618_619_set_term_curr,
1520 .bq256xx_set_iprechg = bq25618_619_set_prechrg_curr,
1521 .bq256xx_set_vindpm = bq256xx_set_input_volt_lim,
1522 .bq256xx_set_charge_type = bq256xx_set_charge_type,
1523 .bq256xx_set_ts_ignore = bq256xx_set_ts_ignore,
1524
1525 .bq256xx_def_ichg = BQ25618_ICHG_DEF_uA,
1526 .bq256xx_def_iindpm = BQ256XX_IINDPM_DEF_uA,
1527 .bq256xx_def_vbatreg = BQ25618_VBATREG_DEF_uV,
1528 .bq256xx_def_iterm = BQ25618_ITERM_DEF_uA,
1529 .bq256xx_def_iprechg = BQ25618_IPRECHG_DEF_uA,
1530 .bq256xx_def_vindpm = BQ256XX_VINDPM_DEF_uV,
1531
1532 .bq256xx_max_ichg = BQ25618_ICHG_MAX_uA,
1533 .bq256xx_max_vbatreg = BQ25618_VBATREG_MAX_uV,
1534
1535 .has_usb_detect = false,
1536 },
1537 };
1538
bq256xx_power_supply_init(struct bq256xx_device * bq,struct power_supply_config * psy_cfg,struct device * dev)1539 static int bq256xx_power_supply_init(struct bq256xx_device *bq,
1540 struct power_supply_config *psy_cfg, struct device *dev)
1541 {
1542 bq->charger = devm_power_supply_register(bq->dev,
1543 &bq256xx_power_supply_desc,
1544 psy_cfg);
1545 if (IS_ERR(bq->charger)) {
1546 dev_err(dev, "power supply register charger failed\n");
1547 return PTR_ERR(bq->charger);
1548 }
1549
1550 bq->battery = devm_power_supply_register(bq->dev,
1551 &bq256xx_battery_desc,
1552 psy_cfg);
1553 if (IS_ERR(bq->battery)) {
1554 dev_err(dev, "power supply register battery failed\n");
1555 return PTR_ERR(bq->battery);
1556 }
1557 return 0;
1558 }
1559
bq256xx_hw_init(struct bq256xx_device * bq)1560 static int bq256xx_hw_init(struct bq256xx_device *bq)
1561 {
1562 struct power_supply_battery_info *bat_info;
1563 int wd_reg_val = BQ256XX_WATCHDOG_DIS;
1564 int ret = 0;
1565 int i;
1566
1567 for (i = 0; i < BQ256XX_NUM_WD_VAL; i++) {
1568 if (bq->watchdog_timer == bq256xx_watchdog_time[i]) {
1569 wd_reg_val = i;
1570 break;
1571 }
1572 if (i + 1 < BQ256XX_NUM_WD_VAL &&
1573 bq->watchdog_timer > bq256xx_watchdog_time[i] &&
1574 bq->watchdog_timer < bq256xx_watchdog_time[i + 1])
1575 wd_reg_val = i;
1576 }
1577 ret = regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_1,
1578 BQ256XX_WATCHDOG_MASK, wd_reg_val <<
1579 BQ256XX_WDT_BIT_SHIFT);
1580 if (ret)
1581 return ret;
1582
1583 ret = power_supply_get_battery_info(bq->charger, &bat_info);
1584 if (ret == -ENOMEM)
1585 return ret;
1586
1587 if (ret) {
1588 dev_warn(bq->dev, "battery info missing, default values will be applied\n");
1589
1590 bat_info->constant_charge_current_max_ua =
1591 bq->chip_info->bq256xx_def_ichg;
1592
1593 bat_info->constant_charge_voltage_max_uv =
1594 bq->chip_info->bq256xx_def_vbatreg;
1595
1596 bat_info->precharge_current_ua =
1597 bq->chip_info->bq256xx_def_iprechg;
1598
1599 bat_info->charge_term_current_ua =
1600 bq->chip_info->bq256xx_def_iterm;
1601
1602 bq->init_data.ichg_max =
1603 bq->chip_info->bq256xx_max_ichg;
1604
1605 bq->init_data.vbatreg_max =
1606 bq->chip_info->bq256xx_max_vbatreg;
1607 } else {
1608 bq->init_data.ichg_max =
1609 bat_info->constant_charge_current_max_ua;
1610
1611 bq->init_data.vbatreg_max =
1612 bat_info->constant_charge_voltage_max_uv;
1613 }
1614
1615 ret = bq->chip_info->bq256xx_set_vindpm(bq, bq->init_data.vindpm);
1616 if (ret)
1617 return ret;
1618
1619 ret = bq->chip_info->bq256xx_set_iindpm(bq, bq->init_data.iindpm);
1620 if (ret)
1621 return ret;
1622
1623 ret = bq->chip_info->bq256xx_set_ichg(bq,
1624 bq->chip_info->bq256xx_def_ichg);
1625 if (ret)
1626 return ret;
1627
1628 ret = bq->chip_info->bq256xx_set_iprechg(bq,
1629 bat_info->precharge_current_ua);
1630 if (ret)
1631 return ret;
1632
1633 ret = bq->chip_info->bq256xx_set_vbatreg(bq,
1634 bq->chip_info->bq256xx_def_vbatreg);
1635 if (ret)
1636 return ret;
1637
1638 ret = bq->chip_info->bq256xx_set_iterm(bq,
1639 bat_info->charge_term_current_ua);
1640 if (ret)
1641 return ret;
1642
1643 if (bq->chip_info->bq256xx_set_ts_ignore) {
1644 ret = bq->chip_info->bq256xx_set_ts_ignore(bq, bq->init_data.ts_ignore);
1645 if (ret)
1646 return ret;
1647 }
1648
1649 power_supply_put_battery_info(bq->charger, bat_info);
1650
1651 return 0;
1652 }
1653
bq256xx_parse_dt(struct bq256xx_device * bq,struct power_supply_config * psy_cfg,struct device * dev)1654 static int bq256xx_parse_dt(struct bq256xx_device *bq,
1655 struct power_supply_config *psy_cfg, struct device *dev)
1656 {
1657 int ret = 0;
1658
1659 psy_cfg->drv_data = bq;
1660 psy_cfg->of_node = dev->of_node;
1661
1662 ret = device_property_read_u32(bq->dev, "ti,watchdog-timeout-ms",
1663 &bq->watchdog_timer);
1664 if (ret)
1665 bq->watchdog_timer = BQ256XX_WATCHDOG_DIS;
1666
1667 if (bq->watchdog_timer > BQ256XX_WATCHDOG_MAX ||
1668 bq->watchdog_timer < BQ256XX_WATCHDOG_DIS)
1669 return -EINVAL;
1670
1671 ret = device_property_read_u32(bq->dev,
1672 "input-voltage-limit-microvolt",
1673 &bq->init_data.vindpm);
1674 if (ret)
1675 bq->init_data.vindpm = bq->chip_info->bq256xx_def_vindpm;
1676
1677 ret = device_property_read_u32(bq->dev,
1678 "input-current-limit-microamp",
1679 &bq->init_data.iindpm);
1680 if (ret)
1681 bq->init_data.iindpm = bq->chip_info->bq256xx_def_iindpm;
1682
1683 bq->init_data.ts_ignore = device_property_read_bool(bq->dev, "ti,no-thermistor");
1684
1685 return 0;
1686 }
1687
bq256xx_probe(struct i2c_client * client)1688 static int bq256xx_probe(struct i2c_client *client)
1689 {
1690 const struct i2c_device_id *id = i2c_client_get_device_id(client);
1691 struct device *dev = &client->dev;
1692 struct bq256xx_device *bq;
1693 struct power_supply_config psy_cfg = { };
1694
1695 int ret;
1696
1697 bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
1698 if (!bq)
1699 return -ENOMEM;
1700
1701 bq->client = client;
1702 bq->dev = dev;
1703 bq->chip_info = i2c_get_match_data(client);
1704
1705 mutex_init(&bq->lock);
1706
1707 strscpy(bq->model_name, id->name, sizeof(bq->model_name));
1708
1709 bq->regmap = devm_regmap_init_i2c(client,
1710 bq->chip_info->bq256xx_regmap_config);
1711
1712 if (IS_ERR(bq->regmap)) {
1713 dev_err(dev, "Failed to allocate register map\n");
1714 return PTR_ERR(bq->regmap);
1715 }
1716
1717 i2c_set_clientdata(client, bq);
1718
1719 ret = bq256xx_parse_dt(bq, &psy_cfg, dev);
1720 if (ret) {
1721 dev_err(dev, "Failed to read device tree properties%d\n", ret);
1722 return ret;
1723 }
1724
1725 ret = devm_add_action_or_reset(dev, bq256xx_charger_reset, bq);
1726 if (ret)
1727 return ret;
1728
1729 /* OTG reporting */
1730 bq->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1731 if (!IS_ERR_OR_NULL(bq->usb2_phy)) {
1732 INIT_WORK(&bq->usb_work, bq256xx_usb_work);
1733 bq->usb_nb.notifier_call = bq256xx_usb_notifier;
1734 usb_register_notifier(bq->usb2_phy, &bq->usb_nb);
1735 }
1736
1737 bq->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
1738 if (!IS_ERR_OR_NULL(bq->usb3_phy)) {
1739 INIT_WORK(&bq->usb_work, bq256xx_usb_work);
1740 bq->usb_nb.notifier_call = bq256xx_usb_notifier;
1741 usb_register_notifier(bq->usb3_phy, &bq->usb_nb);
1742 }
1743
1744 if (client->irq) {
1745 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1746 bq256xx_irq_handler_thread,
1747 IRQF_TRIGGER_FALLING |
1748 IRQF_ONESHOT,
1749 dev_name(&client->dev), bq);
1750 if (ret < 0) {
1751 dev_err(dev, "get irq fail: %d\n", ret);
1752 return ret;
1753 }
1754 }
1755
1756 ret = bq256xx_power_supply_init(bq, &psy_cfg, dev);
1757 if (ret) {
1758 dev_err(dev, "Failed to register power supply\n");
1759 return ret;
1760 }
1761
1762 ret = bq256xx_hw_init(bq);
1763 if (ret) {
1764 dev_err(dev, "Cannot initialize the chip.\n");
1765 return ret;
1766 }
1767
1768 return ret;
1769 }
1770
1771 static const struct i2c_device_id bq256xx_i2c_ids[] = {
1772 { "bq25600", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25600] },
1773 { "bq25600d", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25600D] },
1774 { "bq25601", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25601] },
1775 { "bq25601d", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25601D] },
1776 { "bq25611d", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25611D] },
1777 { "bq25618", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25618] },
1778 { "bq25619", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25619] },
1779 {}
1780 };
1781 MODULE_DEVICE_TABLE(i2c, bq256xx_i2c_ids);
1782
1783 static const struct of_device_id bq256xx_of_match[] = {
1784 { .compatible = "ti,bq25600", .data = &bq256xx_chip_info_tbl[BQ25600] },
1785 { .compatible = "ti,bq25600d", .data = &bq256xx_chip_info_tbl[BQ25600D] },
1786 { .compatible = "ti,bq25601", .data = &bq256xx_chip_info_tbl[BQ25601] },
1787 { .compatible = "ti,bq25601d", .data = &bq256xx_chip_info_tbl[BQ25601D] },
1788 { .compatible = "ti,bq25611d", .data = &bq256xx_chip_info_tbl[BQ25611D] },
1789 { .compatible = "ti,bq25618", .data = &bq256xx_chip_info_tbl[BQ25618] },
1790 { .compatible = "ti,bq25619", .data = &bq256xx_chip_info_tbl[BQ25619] },
1791 {}
1792 };
1793 MODULE_DEVICE_TABLE(of, bq256xx_of_match);
1794
1795 static const struct acpi_device_id bq256xx_acpi_match[] = {
1796 { "bq25600", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25600] },
1797 { "bq25600d", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25600D] },
1798 { "bq25601", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25601] },
1799 { "bq25601d", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25601D] },
1800 { "bq25611d", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25611D] },
1801 { "bq25618", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25618] },
1802 { "bq25619", (kernel_ulong_t)&bq256xx_chip_info_tbl[BQ25619] },
1803 {}
1804 };
1805 MODULE_DEVICE_TABLE(acpi, bq256xx_acpi_match);
1806
1807 static struct i2c_driver bq256xx_driver = {
1808 .driver = {
1809 .name = "bq256xx-charger",
1810 .of_match_table = bq256xx_of_match,
1811 .acpi_match_table = bq256xx_acpi_match,
1812 },
1813 .probe = bq256xx_probe,
1814 .id_table = bq256xx_i2c_ids,
1815 };
1816 module_i2c_driver(bq256xx_driver);
1817
1818 MODULE_AUTHOR("Ricardo Rivera-Matos <r-rivera-matos@ti.com>");
1819 MODULE_DESCRIPTION("bq256xx charger driver");
1820 MODULE_LICENSE("GPL v2");
1821