1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ROHM BD99954 charger driver
4 *
5 * Copyright (C) 2020 Rohm Semiconductors
6 * Originally written by:
7 * Mikko Mutanen <mikko.mutanen@fi.rohmeurope.com>
8 * Markus Laine <markus.laine@fi.rohmeurope.com>
9 * Bugs added by:
10 * Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
11 */
12
13 /*
14 * The battery charging profile of BD99954.
15 *
16 * Curve (1) represents charging current.
17 * Curve (2) represents battery voltage.
18 *
19 * The BD99954 data sheet divides charging to three phases.
20 * a) Trickle-charge with constant current (8).
21 * b) pre-charge with constant current (6)
22 * c) fast-charge, first with constant current (5) phase. After
23 * the battery voltage has reached target level (4) we have constant
24 * voltage phase until charging current has dropped to termination
25 * level (7)
26 *
27 * V ^ ^ I
28 * . .
29 * . .
30 *(4)` `.` ` ` ` ` ` ` ` ` ` ` ` ` ` ----------------------------.
31 * . :/ .
32 * . o----+/:/ ` ` ` ` ` ` ` ` ` ` ` ` `.` ` (5)
33 * . + :: + .
34 * . + /- -- .
35 * . +`/- + .
36 * . o/- -: .
37 * . .s. +` .
38 * . .--+ `/ .
39 * . ..`` + .: .
40 * . -` + -- .
41 * . (2) ...`` + :- .
42 * . ...`` + -: .
43 *(3)` `.`."" ` ` ` `+-------- ` ` ` ` ` ` `.:` ` ` ` ` ` ` ` ` .` ` (6)
44 * . + `:. .
45 * . + -: .
46 * . + -:. .
47 * . + .--. .
48 * . (1) + `.+` ` ` `.` ` (7)
49 * -..............` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` + ` ` ` .` ` (8)
50 * . + -
51 * -------------------------------------------------+++++++++-->
52 * | trickle | pre | fast |
53 *
54 * Details of DT properties for different limits can be found from BD99954
55 * device tree binding documentation.
56 */
57
58 #include <linux/delay.h>
59 #include <linux/interrupt.h>
60 #include <linux/i2c.h>
61 #include <linux/kernel.h>
62 #include <linux/linear_range.h>
63 #include <linux/module.h>
64 #include <linux/power_supply.h>
65 #include <linux/property.h>
66 #include <linux/regmap.h>
67 #include <linux/types.h>
68
69 #include "bd99954-charger.h"
70
71 /* Initial field values, converted to initial register values */
72 struct bd9995x_init_data {
73 u16 vsysreg_set; /* VSYS Regulation Setting */
74 u16 ibus_lim_set; /* VBUS input current limitation */
75 u16 icc_lim_set; /* VCC/VACP Input Current Limit Setting */
76 u16 itrich_set; /* Trickle-charge Current Setting */
77 u16 iprech_set; /* Pre-Charge Current Setting */
78 u16 ichg_set; /* Fast-Charge constant current */
79 u16 vfastchg_reg_set1; /* Fast Charging Regulation Voltage */
80 u16 vprechg_th_set; /* Pre-charge Voltage Threshold Setting */
81 u16 vrechg_set; /* Re-charge Battery Voltage Setting */
82 u16 vbatovp_set; /* Battery Over Voltage Threshold Setting */
83 u16 iterm_set; /* Charging termination current */
84 };
85
86 struct bd9995x_state {
87 u8 online;
88 u16 chgstm_status;
89 u16 vbat_vsys_status;
90 u16 vbus_vcc_status;
91 };
92
93 struct bd9995x_device {
94 struct i2c_client *client;
95 struct device *dev;
96 struct power_supply *charger;
97
98 struct regmap *rmap;
99 struct regmap_field *rmap_fields[F_MAX_FIELDS];
100
101 int chip_id;
102 int chip_rev;
103 struct bd9995x_init_data init_data;
104 struct bd9995x_state state;
105
106 struct mutex lock; /* Protect state data */
107 };
108
109 static const struct regmap_range bd9995x_readonly_reg_ranges[] = {
110 regmap_reg_range(CHGSTM_STATUS, SEL_ILIM_VAL),
111 regmap_reg_range(IOUT_DACIN_VAL, IOUT_DACIN_VAL),
112 regmap_reg_range(VCC_UCD_STATUS, VCC_IDD_STATUS),
113 regmap_reg_range(VBUS_UCD_STATUS, VBUS_IDD_STATUS),
114 regmap_reg_range(CHIP_ID, CHIP_REV),
115 regmap_reg_range(SYSTEM_STATUS, SYSTEM_STATUS),
116 regmap_reg_range(IBATP_VAL, VBAT_AVE_VAL),
117 regmap_reg_range(VTH_VAL, EXTIADP_AVE_VAL),
118 };
119
120 static const struct regmap_access_table bd9995x_writeable_regs = {
121 .no_ranges = bd9995x_readonly_reg_ranges,
122 .n_no_ranges = ARRAY_SIZE(bd9995x_readonly_reg_ranges),
123 };
124
125 static const struct regmap_range bd9995x_volatile_reg_ranges[] = {
126 regmap_reg_range(CHGSTM_STATUS, WDT_STATUS),
127 regmap_reg_range(VCC_UCD_STATUS, VCC_IDD_STATUS),
128 regmap_reg_range(VBUS_UCD_STATUS, VBUS_IDD_STATUS),
129 regmap_reg_range(INT0_STATUS, INT7_STATUS),
130 regmap_reg_range(SYSTEM_STATUS, SYSTEM_CTRL_SET),
131 regmap_reg_range(IBATP_VAL, EXTIADP_AVE_VAL), /* Measurement regs */
132 };
133
134 static const struct regmap_access_table bd9995x_volatile_regs = {
135 .yes_ranges = bd9995x_volatile_reg_ranges,
136 .n_yes_ranges = ARRAY_SIZE(bd9995x_volatile_reg_ranges),
137 };
138
139 static const struct regmap_range_cfg regmap_range_cfg[] = {
140 {
141 .selector_reg = MAP_SET,
142 .selector_mask = 0xFFFF,
143 .selector_shift = 0,
144 .window_start = 0,
145 .window_len = 0x100,
146 .range_min = 0 * 0x100,
147 .range_max = 3 * 0x100,
148 },
149 };
150
151 static const struct regmap_config bd9995x_regmap_config = {
152 .reg_bits = 8,
153 .val_bits = 16,
154 .reg_stride = 1,
155
156 .max_register = 3 * 0x100,
157 .cache_type = REGCACHE_MAPLE,
158
159 .ranges = regmap_range_cfg,
160 .num_ranges = ARRAY_SIZE(regmap_range_cfg),
161 .val_format_endian = REGMAP_ENDIAN_LITTLE,
162 .wr_table = &bd9995x_writeable_regs,
163 .volatile_table = &bd9995x_volatile_regs,
164 };
165
166 enum bd9995x_chrg_fault {
167 CHRG_FAULT_NORMAL,
168 CHRG_FAULT_INPUT,
169 CHRG_FAULT_THERMAL_SHUTDOWN,
170 CHRG_FAULT_TIMER_EXPIRED,
171 };
172
bd9995x_get_prop_batt_health(struct bd9995x_device * bd)173 static int bd9995x_get_prop_batt_health(struct bd9995x_device *bd)
174 {
175 int ret, tmp;
176
177 ret = regmap_field_read(bd->rmap_fields[F_BATTEMP], &tmp);
178 if (ret)
179 return POWER_SUPPLY_HEALTH_UNKNOWN;
180
181 /* TODO: Check these against datasheet page 34 */
182
183 switch (tmp) {
184 case ROOM:
185 return POWER_SUPPLY_HEALTH_GOOD;
186 case HOT1:
187 case HOT2:
188 case HOT3:
189 return POWER_SUPPLY_HEALTH_OVERHEAT;
190 case COLD1:
191 case COLD2:
192 return POWER_SUPPLY_HEALTH_COLD;
193 case TEMP_DIS:
194 case BATT_OPEN:
195 default:
196 return POWER_SUPPLY_HEALTH_UNKNOWN;
197 }
198 }
199
bd9995x_get_prop_charge_type(struct bd9995x_device * bd)200 static int bd9995x_get_prop_charge_type(struct bd9995x_device *bd)
201 {
202 int ret, tmp;
203
204 ret = regmap_field_read(bd->rmap_fields[F_CHGSTM_STATE], &tmp);
205 if (ret)
206 return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
207
208 switch (tmp) {
209 case CHGSTM_TRICKLE_CHARGE:
210 case CHGSTM_PRE_CHARGE:
211 return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
212 case CHGSTM_FAST_CHARGE:
213 return POWER_SUPPLY_CHARGE_TYPE_FAST;
214 case CHGSTM_TOP_OFF:
215 case CHGSTM_DONE:
216 case CHGSTM_SUSPEND:
217 return POWER_SUPPLY_CHARGE_TYPE_NONE;
218 default: /* Rest of the states are error related, no charging */
219 return POWER_SUPPLY_CHARGE_TYPE_NONE;
220 }
221 }
222
bd9995x_get_prop_batt_present(struct bd9995x_device * bd)223 static bool bd9995x_get_prop_batt_present(struct bd9995x_device *bd)
224 {
225 int ret, tmp;
226
227 ret = regmap_field_read(bd->rmap_fields[F_BATTEMP], &tmp);
228 if (ret)
229 return false;
230
231 return tmp != BATT_OPEN;
232 }
233
bd9995x_get_prop_batt_voltage(struct bd9995x_device * bd)234 static int bd9995x_get_prop_batt_voltage(struct bd9995x_device *bd)
235 {
236 int ret, tmp;
237
238 ret = regmap_field_read(bd->rmap_fields[F_VBAT_VAL], &tmp);
239 if (ret)
240 return 0;
241
242 tmp = min(tmp, 19200);
243
244 return tmp * 1000;
245 }
246
bd9995x_get_prop_batt_current(struct bd9995x_device * bd)247 static int bd9995x_get_prop_batt_current(struct bd9995x_device *bd)
248 {
249 int ret, tmp;
250
251 ret = regmap_field_read(bd->rmap_fields[F_IBATP_VAL], &tmp);
252 if (ret)
253 return 0;
254
255 return tmp * 1000;
256 }
257
258 #define DEFAULT_BATTERY_TEMPERATURE 250
259
bd9995x_get_prop_batt_temp(struct bd9995x_device * bd)260 static int bd9995x_get_prop_batt_temp(struct bd9995x_device *bd)
261 {
262 int ret, tmp;
263
264 ret = regmap_field_read(bd->rmap_fields[F_THERM_VAL], &tmp);
265 if (ret)
266 return DEFAULT_BATTERY_TEMPERATURE;
267
268 return (200 - tmp) * 10;
269 }
270
bd9995x_power_supply_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)271 static int bd9995x_power_supply_get_property(struct power_supply *psy,
272 enum power_supply_property psp,
273 union power_supply_propval *val)
274 {
275 int ret, tmp;
276 struct bd9995x_device *bd = power_supply_get_drvdata(psy);
277 struct bd9995x_state state;
278
279 mutex_lock(&bd->lock);
280 state = bd->state;
281 mutex_unlock(&bd->lock);
282
283 switch (psp) {
284 case POWER_SUPPLY_PROP_STATUS:
285 switch (state.chgstm_status) {
286 case CHGSTM_TRICKLE_CHARGE:
287 case CHGSTM_PRE_CHARGE:
288 case CHGSTM_FAST_CHARGE:
289 case CHGSTM_TOP_OFF:
290 val->intval = POWER_SUPPLY_STATUS_CHARGING;
291 break;
292
293 case CHGSTM_DONE:
294 val->intval = POWER_SUPPLY_STATUS_FULL;
295 break;
296
297 case CHGSTM_SUSPEND:
298 case CHGSTM_TEMPERATURE_ERROR_1:
299 case CHGSTM_TEMPERATURE_ERROR_2:
300 case CHGSTM_TEMPERATURE_ERROR_3:
301 case CHGSTM_TEMPERATURE_ERROR_4:
302 case CHGSTM_TEMPERATURE_ERROR_5:
303 case CHGSTM_TEMPERATURE_ERROR_6:
304 case CHGSTM_TEMPERATURE_ERROR_7:
305 case CHGSTM_THERMAL_SHUT_DOWN_1:
306 case CHGSTM_THERMAL_SHUT_DOWN_2:
307 case CHGSTM_THERMAL_SHUT_DOWN_3:
308 case CHGSTM_THERMAL_SHUT_DOWN_4:
309 case CHGSTM_THERMAL_SHUT_DOWN_5:
310 case CHGSTM_THERMAL_SHUT_DOWN_6:
311 case CHGSTM_THERMAL_SHUT_DOWN_7:
312 case CHGSTM_BATTERY_ERROR:
313 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
314 break;
315
316 default:
317 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
318 break;
319 }
320 break;
321
322 case POWER_SUPPLY_PROP_MANUFACTURER:
323 val->strval = BD9995X_MANUFACTURER;
324 break;
325
326 case POWER_SUPPLY_PROP_ONLINE:
327 val->intval = state.online;
328 break;
329
330 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
331 ret = regmap_field_read(bd->rmap_fields[F_IBATP_VAL], &tmp);
332 if (ret)
333 return ret;
334 val->intval = tmp * 1000;
335 break;
336
337 case POWER_SUPPLY_PROP_CHARGE_AVG:
338 ret = regmap_field_read(bd->rmap_fields[F_IBATP_AVE_VAL], &tmp);
339 if (ret)
340 return ret;
341 val->intval = tmp * 1000;
342 break;
343
344 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
345 /*
346 * Currently the DT uses this property to give the
347 * target current for fast-charging constant current phase.
348 * I think it is correct in a sense.
349 *
350 * Yet, this prop we read and return here is the programmed
351 * safety limit for combined input currents. This feels
352 * also correct in a sense.
353 *
354 * However, this results a mismatch to DT value and value
355 * read from sysfs.
356 */
357 ret = regmap_field_read(bd->rmap_fields[F_SEL_ILIM_VAL], &tmp);
358 if (ret)
359 return ret;
360 val->intval = tmp * 1000;
361 break;
362
363 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
364 if (!state.online) {
365 val->intval = 0;
366 break;
367 }
368
369 ret = regmap_field_read(bd->rmap_fields[F_VFASTCHG_REG_SET1],
370 &tmp);
371 if (ret)
372 return ret;
373
374 /*
375 * The actual range : 2560 to 19200 mV. No matter what the
376 * register says
377 */
378 val->intval = clamp_val(tmp << 4, 2560, 19200);
379 val->intval *= 1000;
380 break;
381
382 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
383 ret = regmap_field_read(bd->rmap_fields[F_ITERM_SET], &tmp);
384 if (ret)
385 return ret;
386 /* Start step is 64 mA */
387 val->intval = tmp << 6;
388 /* Maximum is 1024 mA - no matter what register says */
389 val->intval = min(val->intval, 1024);
390 val->intval *= 1000;
391 break;
392
393 /* Battery properties which we access through charger */
394 case POWER_SUPPLY_PROP_PRESENT:
395 val->intval = bd9995x_get_prop_batt_present(bd);
396 break;
397
398 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
399 val->intval = bd9995x_get_prop_batt_voltage(bd);
400 break;
401
402 case POWER_SUPPLY_PROP_CURRENT_NOW:
403 val->intval = bd9995x_get_prop_batt_current(bd);
404 break;
405
406 case POWER_SUPPLY_PROP_CHARGE_TYPE:
407 val->intval = bd9995x_get_prop_charge_type(bd);
408 break;
409
410 case POWER_SUPPLY_PROP_HEALTH:
411 val->intval = bd9995x_get_prop_batt_health(bd);
412 break;
413
414 case POWER_SUPPLY_PROP_TEMP:
415 val->intval = bd9995x_get_prop_batt_temp(bd);
416 break;
417
418 case POWER_SUPPLY_PROP_TECHNOLOGY:
419 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
420 break;
421
422 case POWER_SUPPLY_PROP_MODEL_NAME:
423 val->strval = "bd99954";
424 break;
425
426 default:
427 return -EINVAL;
428
429 }
430
431 return 0;
432 }
433
bd9995x_get_chip_state(struct bd9995x_device * bd,struct bd9995x_state * state)434 static int bd9995x_get_chip_state(struct bd9995x_device *bd,
435 struct bd9995x_state *state)
436 {
437 int i, ret, tmp;
438 struct {
439 struct regmap_field *id;
440 u16 *data;
441 } state_fields[] = {
442 {
443 bd->rmap_fields[F_CHGSTM_STATE], &state->chgstm_status,
444 }, {
445 bd->rmap_fields[F_VBAT_VSYS_STATUS],
446 &state->vbat_vsys_status,
447 }, {
448 bd->rmap_fields[F_VBUS_VCC_STATUS],
449 &state->vbus_vcc_status,
450 },
451 };
452
453
454 for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
455 ret = regmap_field_read(state_fields[i].id, &tmp);
456 if (ret)
457 return ret;
458
459 *state_fields[i].data = tmp;
460 }
461
462 if (state->vbus_vcc_status & STATUS_VCC_DET ||
463 state->vbus_vcc_status & STATUS_VBUS_DET)
464 state->online = 1;
465 else
466 state->online = 0;
467
468 return 0;
469 }
470
bd9995x_irq_handler_thread(int irq,void * private)471 static irqreturn_t bd9995x_irq_handler_thread(int irq, void *private)
472 {
473 struct bd9995x_device *bd = private;
474 int ret, status, mask, i;
475 unsigned long tmp;
476 struct bd9995x_state state;
477
478 /*
479 * The bd9995x does not seem to generate big amount of interrupts.
480 * The logic regarding which interrupts can cause relevant
481 * status changes seem to be pretty complex.
482 *
483 * So lets implement really simple and hopefully bullet-proof handler:
484 * It does not really matter which IRQ we handle, we just go and
485 * re-read all interesting statuses + give the framework a nudge.
486 *
487 * Other option would be building a _complex_ and error prone logic
488 * trying to decide what could have been changed (resulting this IRQ
489 * we are now handling). During the normal operation the BD99954 does
490 * not seem to be generating much of interrupts so benefit from such
491 * logic would probably be minimal.
492 */
493
494 ret = regmap_read(bd->rmap, INT0_STATUS, &status);
495 if (ret) {
496 dev_err(bd->dev, "Failed to read IRQ status\n");
497 return IRQ_NONE;
498 }
499
500 ret = regmap_field_read(bd->rmap_fields[F_INT0_SET], &mask);
501 if (ret) {
502 dev_err(bd->dev, "Failed to read IRQ mask\n");
503 return IRQ_NONE;
504 }
505
506 /* Handle only IRQs that are not masked */
507 status &= mask;
508 tmp = status;
509
510 /* Lowest bit does not represent any sub-registers */
511 tmp >>= 1;
512
513 /*
514 * Mask and ack IRQs we will handle (+ the idiot bit)
515 */
516 ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], 0);
517 if (ret) {
518 dev_err(bd->dev, "Failed to mask F_INT0\n");
519 return IRQ_NONE;
520 }
521
522 ret = regmap_write(bd->rmap, INT0_STATUS, status);
523 if (ret) {
524 dev_err(bd->dev, "Failed to ack F_INT0\n");
525 goto err_umask;
526 }
527
528 for_each_set_bit(i, &tmp, 7) {
529 int sub_status, sub_mask;
530 static const int sub_status_reg[] = {
531 INT1_STATUS, INT2_STATUS, INT3_STATUS, INT4_STATUS,
532 INT5_STATUS, INT6_STATUS, INT7_STATUS,
533 };
534 struct regmap_field *sub_mask_f[] = {
535 bd->rmap_fields[F_INT1_SET],
536 bd->rmap_fields[F_INT2_SET],
537 bd->rmap_fields[F_INT3_SET],
538 bd->rmap_fields[F_INT4_SET],
539 bd->rmap_fields[F_INT5_SET],
540 bd->rmap_fields[F_INT6_SET],
541 bd->rmap_fields[F_INT7_SET],
542 };
543
544 /* Clear sub IRQs */
545 ret = regmap_read(bd->rmap, sub_status_reg[i], &sub_status);
546 if (ret) {
547 dev_err(bd->dev, "Failed to read IRQ sub-status\n");
548 goto err_umask;
549 }
550
551 ret = regmap_field_read(sub_mask_f[i], &sub_mask);
552 if (ret) {
553 dev_err(bd->dev, "Failed to read IRQ sub-mask\n");
554 goto err_umask;
555 }
556
557 /* Ack active sub-statuses */
558 sub_status &= sub_mask;
559
560 ret = regmap_write(bd->rmap, sub_status_reg[i], sub_status);
561 if (ret) {
562 dev_err(bd->dev, "Failed to ack sub-IRQ\n");
563 goto err_umask;
564 }
565 }
566
567 ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], mask);
568 if (ret)
569 /* May as well retry once */
570 goto err_umask;
571
572 /* Read whole chip state */
573 ret = bd9995x_get_chip_state(bd, &state);
574 if (ret < 0) {
575 dev_err(bd->dev, "Failed to read chip state\n");
576 } else {
577 mutex_lock(&bd->lock);
578 bd->state = state;
579 mutex_unlock(&bd->lock);
580
581 power_supply_changed(bd->charger);
582 }
583
584 return IRQ_HANDLED;
585
586 err_umask:
587 ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], mask);
588 if (ret)
589 dev_err(bd->dev,
590 "Failed to un-mask F_INT0 - IRQ permanently disabled\n");
591
592 return IRQ_NONE;
593 }
594
__bd9995x_chip_reset(struct bd9995x_device * bd)595 static int __bd9995x_chip_reset(struct bd9995x_device *bd)
596 {
597 int ret, state;
598 int rst_check_counter = 10;
599 u16 tmp = ALLRST | OTPLD;
600
601 ret = regmap_raw_write(bd->rmap, SYSTEM_CTRL_SET, &tmp, 2);
602 if (ret < 0)
603 return ret;
604
605 do {
606 ret = regmap_field_read(bd->rmap_fields[F_OTPLD_STATE], &state);
607 if (ret)
608 return ret;
609
610 msleep(10);
611 } while (state == 0 && --rst_check_counter);
612
613 if (!rst_check_counter) {
614 dev_err(bd->dev, "chip reset not completed\n");
615 return -ETIMEDOUT;
616 }
617
618 tmp = 0;
619 ret = regmap_raw_write(bd->rmap, SYSTEM_CTRL_SET, &tmp, 2);
620
621 return ret;
622 }
623
bd9995x_hw_init(struct bd9995x_device * bd)624 static int bd9995x_hw_init(struct bd9995x_device *bd)
625 {
626 int ret;
627 int i;
628 struct bd9995x_state state;
629 struct bd9995x_init_data *id = &bd->init_data;
630
631 const struct {
632 enum bd9995x_fields id;
633 u16 value;
634 } init_data[] = {
635 /* Enable the charging trigger after SDP charger attached */
636 {F_SDP_CHG_TRIG_EN, 1},
637 /* Enable charging trigger after SDP charger attached */
638 {F_SDP_CHG_TRIG, 1},
639 /* Disable charging trigger by BC1.2 detection */
640 {F_VBUS_BC_DISEN, 1},
641 /* Disable charging trigger by BC1.2 detection */
642 {F_VCC_BC_DISEN, 1},
643 /* Disable automatic limitation of the input current */
644 {F_ILIM_AUTO_DISEN, 1},
645 /* Select current limitation when SDP charger attached*/
646 {F_SDP_500_SEL, 1},
647 /* Select current limitation when DCP charger attached */
648 {F_DCP_2500_SEL, 1},
649 {F_VSYSREG_SET, id->vsysreg_set},
650 /* Activate USB charging and DC/DC converter */
651 {F_USB_SUS, 0},
652 /* DCDC clock: 1200 kHz*/
653 {F_DCDC_CLK_SEL, 3},
654 /* Enable charging */
655 {F_CHG_EN, 1},
656 /* Disable Input current Limit setting voltage measurement */
657 {F_EXTIADPEN, 0},
658 /* Disable input current limiting */
659 {F_VSYS_PRIORITY, 1},
660 {F_IBUS_LIM_SET, id->ibus_lim_set},
661 {F_ICC_LIM_SET, id->icc_lim_set},
662 /* Charge Termination Current Setting to 0*/
663 {F_ITERM_SET, id->iterm_set},
664 /* Trickle-charge Current Setting */
665 {F_ITRICH_SET, id->itrich_set},
666 /* Pre-charge Current setting */
667 {F_IPRECH_SET, id->iprech_set},
668 /* Fast Charge Current for constant current phase */
669 {F_ICHG_SET, id->ichg_set},
670 /* Fast Charge Voltage Regulation Setting */
671 {F_VFASTCHG_REG_SET1, id->vfastchg_reg_set1},
672 /* Set Pre-charge Voltage Threshold for trickle charging. */
673 {F_VPRECHG_TH_SET, id->vprechg_th_set},
674 {F_VRECHG_SET, id->vrechg_set},
675 {F_VBATOVP_SET, id->vbatovp_set},
676 /* Reverse buck boost voltage Setting */
677 {F_VRBOOST_SET, 0},
678 /* Disable fast-charging watchdog */
679 {F_WDT_FST, 0},
680 /* Disable pre-charging watchdog */
681 {F_WDT_PRE, 0},
682 /* Power save off */
683 {F_POWER_SAVE_MODE, 0},
684 {F_INT1_SET, INT1_ALL},
685 {F_INT2_SET, INT2_ALL},
686 {F_INT3_SET, INT3_ALL},
687 {F_INT4_SET, INT4_ALL},
688 {F_INT5_SET, INT5_ALL},
689 {F_INT6_SET, INT6_ALL},
690 {F_INT7_SET, INT7_ALL},
691 };
692
693 /*
694 * Currently we initialize charger to a known state at startup.
695 * If we want to allow for example the boot code to initialize
696 * charger we should get rid of this.
697 */
698 ret = __bd9995x_chip_reset(bd);
699 if (ret < 0)
700 return ret;
701
702 /* Initialize currents/voltages and other parameters */
703 for (i = 0; i < ARRAY_SIZE(init_data); i++) {
704 ret = regmap_field_write(bd->rmap_fields[init_data[i].id],
705 init_data[i].value);
706 if (ret) {
707 dev_err(bd->dev, "failed to initialize charger (%d)\n",
708 ret);
709 return ret;
710 }
711 }
712
713 ret = bd9995x_get_chip_state(bd, &state);
714 if (ret < 0)
715 return ret;
716
717 mutex_lock(&bd->lock);
718 bd->state = state;
719 mutex_unlock(&bd->lock);
720
721 return 0;
722 }
723
724 static enum power_supply_property bd9995x_power_supply_props[] = {
725 POWER_SUPPLY_PROP_MANUFACTURER,
726 POWER_SUPPLY_PROP_STATUS,
727 POWER_SUPPLY_PROP_ONLINE,
728 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
729 POWER_SUPPLY_PROP_CHARGE_AVG,
730 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
731 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
732 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
733 /* Battery props we access through charger */
734 POWER_SUPPLY_PROP_PRESENT,
735 POWER_SUPPLY_PROP_VOLTAGE_NOW,
736 POWER_SUPPLY_PROP_CURRENT_NOW,
737 POWER_SUPPLY_PROP_CHARGE_TYPE,
738 POWER_SUPPLY_PROP_HEALTH,
739 POWER_SUPPLY_PROP_TEMP,
740 POWER_SUPPLY_PROP_TECHNOLOGY,
741 POWER_SUPPLY_PROP_MODEL_NAME,
742 };
743
744 static const struct power_supply_desc bd9995x_power_supply_desc = {
745 .name = "bd9995x-charger",
746 .type = POWER_SUPPLY_TYPE_USB,
747 .properties = bd9995x_power_supply_props,
748 .num_properties = ARRAY_SIZE(bd9995x_power_supply_props),
749 .get_property = bd9995x_power_supply_get_property,
750 };
751
752 /*
753 * Limit configurations for vbus-input-current and vcc-vacp-input-current
754 * Minimum limit is 0 uA. Max is 511 * 32000 uA = 16352000 uA. This is
755 * configured by writing a register so that each increment in register
756 * value equals to 32000 uA limit increment.
757 *
758 * Eg, value 0x0 is limit 0, value 0x1 is limit 32000, ...
759 * Describe the setting in linear_range table.
760 */
761 static const struct linear_range input_current_limit_ranges[] = {
762 LINEAR_RANGE(0, 0x0, 0x1ff, 32000),
763 };
764
765 /* Possible trickle, pre-charging and termination current values */
766 static const struct linear_range charging_current_ranges[] = {
767 LINEAR_RANGE(0, 0x0, 0x10, 64000),
768 LINEAR_RANGE(1024000, 0x11, 0x1f, 0),
769 };
770
771 /*
772 * Fast charging voltage regulation, starting re-charging limit
773 * and battery over voltage protection have same possible values
774 */
775 static const struct linear_range charge_voltage_regulation_ranges[] = {
776 LINEAR_RANGE(2560000, 0, 0xA0, 0),
777 LINEAR_RANGE(2560000, 0xA0, 0x4B0, 16000),
778 LINEAR_RANGE(19200000, 0x4B0, 0x7FF, 0),
779 };
780
781 /* Possible VSYS voltage regulation values */
782 static const struct linear_range vsys_voltage_regulation_ranges[] = {
783 LINEAR_RANGE(2560000, 0, 0x28, 0),
784 LINEAR_RANGE(2560000, 0x28, 0x12C, 64000),
785 LINEAR_RANGE(19200000, 0x12C, 0x1FF, 0),
786 };
787
788 /* Possible settings for switching from trickle to pre-charging limits */
789 static const struct linear_range trickle_to_pre_threshold_ranges[] = {
790 LINEAR_RANGE(2048000, 0, 0x20, 0),
791 LINEAR_RANGE(2048000, 0x20, 0x12C, 64000),
792 LINEAR_RANGE(19200000, 0x12C, 0x1FF, 0),
793 };
794
795 /* Possible current values for fast-charging constant current phase */
796 static const struct linear_range fast_charge_current_ranges[] = {
797 LINEAR_RANGE(0, 0, 0xFF, 64000),
798 };
799
800 struct battery_init {
801 const char *name;
802 int *info_data;
803 const struct linear_range *range;
804 int ranges;
805 u16 *data;
806 };
807
808 struct dt_init {
809 char *prop;
810 const struct linear_range *range;
811 int ranges;
812 u16 *data;
813 };
814
bd9995x_fw_probe(struct bd9995x_device * bd)815 static int bd9995x_fw_probe(struct bd9995x_device *bd)
816 {
817 int ret;
818 struct power_supply_battery_info *info;
819 u32 property;
820 int i;
821 int regval;
822 bool found;
823 struct bd9995x_init_data *init = &bd->init_data;
824 struct battery_init battery_inits[] = {
825 {
826 .name = "trickle-charging current",
827 .range = &charging_current_ranges[0],
828 .ranges = 2,
829 .data = &init->itrich_set,
830 }, {
831 .name = "pre-charging current",
832 .range = &charging_current_ranges[0],
833 .ranges = 2,
834 .data = &init->iprech_set,
835 }, {
836 .name = "pre-to-trickle charge voltage threshold",
837 .range = &trickle_to_pre_threshold_ranges[0],
838 .ranges = 2,
839 .data = &init->vprechg_th_set,
840 }, {
841 .name = "charging termination current",
842 .range = &charging_current_ranges[0],
843 .ranges = 2,
844 .data = &init->iterm_set,
845 }, {
846 .name = "charging re-start voltage",
847 .range = &charge_voltage_regulation_ranges[0],
848 .ranges = 2,
849 .data = &init->vrechg_set,
850 }, {
851 .name = "battery overvoltage limit",
852 .range = &charge_voltage_regulation_ranges[0],
853 .ranges = 2,
854 .data = &init->vbatovp_set,
855 }, {
856 .name = "fast-charging max current",
857 .range = &fast_charge_current_ranges[0],
858 .ranges = 1,
859 .data = &init->ichg_set,
860 }, {
861 .name = "fast-charging voltage",
862 .range = &charge_voltage_regulation_ranges[0],
863 .ranges = 2,
864 .data = &init->vfastchg_reg_set1,
865 },
866 };
867 struct dt_init props[] = {
868 {
869 .prop = "rohm,vsys-regulation-microvolt",
870 .range = &vsys_voltage_regulation_ranges[0],
871 .ranges = 2,
872 .data = &init->vsysreg_set,
873 }, {
874 .prop = "rohm,vbus-input-current-limit-microamp",
875 .range = &input_current_limit_ranges[0],
876 .ranges = 1,
877 .data = &init->ibus_lim_set,
878 }, {
879 .prop = "rohm,vcc-input-current-limit-microamp",
880 .range = &input_current_limit_ranges[0],
881 .ranges = 1,
882 .data = &init->icc_lim_set,
883 },
884 };
885
886 /*
887 * The power_supply_get_battery_info() does not support getting values
888 * from ACPI. Let's fix it if ACPI is required here.
889 */
890 ret = power_supply_get_battery_info(bd->charger, &info);
891 if (ret < 0)
892 return ret;
893
894 /* Put pointers to the generic battery info */
895 battery_inits[0].info_data = &info->tricklecharge_current_ua;
896 battery_inits[1].info_data = &info->precharge_current_ua;
897 battery_inits[2].info_data = &info->precharge_voltage_max_uv;
898 battery_inits[3].info_data = &info->charge_term_current_ua;
899 battery_inits[4].info_data = &info->charge_restart_voltage_uv;
900 battery_inits[5].info_data = &info->overvoltage_limit_uv;
901 battery_inits[6].info_data = &info->constant_charge_current_max_ua;
902 battery_inits[7].info_data = &info->constant_charge_voltage_max_uv;
903
904 for (i = 0; i < ARRAY_SIZE(battery_inits); i++) {
905 int val = *battery_inits[i].info_data;
906 const struct linear_range *range = battery_inits[i].range;
907 int ranges = battery_inits[i].ranges;
908
909 if (val == -EINVAL)
910 continue;
911
912 ret = linear_range_get_selector_low_array(range, ranges, val,
913 ®val, &found);
914 if (ret) {
915 dev_err(bd->dev, "Unsupported value for %s\n",
916 battery_inits[i].name);
917
918 power_supply_put_battery_info(bd->charger, info);
919 return -EINVAL;
920 }
921 if (!found) {
922 dev_warn(bd->dev,
923 "Unsupported value for %s - using smaller\n",
924 battery_inits[i].name);
925 }
926 *(battery_inits[i].data) = regval;
927 }
928
929 power_supply_put_battery_info(bd->charger, info);
930
931 for (i = 0; i < ARRAY_SIZE(props); i++) {
932 ret = device_property_read_u32(bd->dev, props[i].prop,
933 &property);
934 if (ret < 0) {
935 dev_err(bd->dev, "failed to read %s", props[i].prop);
936
937 return ret;
938 }
939
940 ret = linear_range_get_selector_low_array(props[i].range,
941 props[i].ranges,
942 property, ®val,
943 &found);
944 if (ret) {
945 dev_err(bd->dev, "Unsupported value for '%s'\n",
946 props[i].prop);
947
948 return -EINVAL;
949 }
950
951 if (!found) {
952 dev_warn(bd->dev,
953 "Unsupported value for '%s' - using smaller\n",
954 props[i].prop);
955 }
956
957 *(props[i].data) = regval;
958 }
959
960 return 0;
961 }
962
bd9995x_chip_reset(void * bd)963 static void bd9995x_chip_reset(void *bd)
964 {
965 __bd9995x_chip_reset(bd);
966 }
967
bd9995x_probe(struct i2c_client * client)968 static int bd9995x_probe(struct i2c_client *client)
969 {
970 struct device *dev = &client->dev;
971 struct bd9995x_device *bd;
972 struct power_supply_config psy_cfg = {};
973 int ret;
974 int i;
975
976 bd = devm_kzalloc(dev, sizeof(*bd), GFP_KERNEL);
977 if (!bd)
978 return -ENOMEM;
979
980 bd->client = client;
981 bd->dev = dev;
982 psy_cfg.drv_data = bd;
983 psy_cfg.fwnode = dev_fwnode(dev);
984
985 mutex_init(&bd->lock);
986
987 bd->rmap = devm_regmap_init_i2c(client, &bd9995x_regmap_config);
988 if (IS_ERR(bd->rmap)) {
989 dev_err(dev, "Failed to setup register access via i2c\n");
990 return PTR_ERR(bd->rmap);
991 }
992
993 for (i = 0; i < ARRAY_SIZE(bd9995x_reg_fields); i++) {
994 const struct reg_field *reg_fields = bd9995x_reg_fields;
995
996 bd->rmap_fields[i] = devm_regmap_field_alloc(dev, bd->rmap,
997 reg_fields[i]);
998 if (IS_ERR(bd->rmap_fields[i])) {
999 dev_err(dev, "cannot allocate regmap field\n");
1000 return PTR_ERR(bd->rmap_fields[i]);
1001 }
1002 }
1003
1004 i2c_set_clientdata(client, bd);
1005
1006 ret = regmap_field_read(bd->rmap_fields[F_CHIP_ID], &bd->chip_id);
1007 if (ret) {
1008 dev_err(dev, "Cannot read chip ID.\n");
1009 return ret;
1010 }
1011
1012 if (bd->chip_id != BD99954_ID) {
1013 dev_err(dev, "Chip with ID=0x%x, not supported!\n",
1014 bd->chip_id);
1015 return -ENODEV;
1016 }
1017
1018 ret = regmap_field_read(bd->rmap_fields[F_CHIP_REV], &bd->chip_rev);
1019 if (ret) {
1020 dev_err(dev, "Cannot read revision.\n");
1021 return ret;
1022 }
1023
1024 dev_info(bd->dev, "Found BD99954 chip rev %d\n", bd->chip_rev);
1025
1026 /*
1027 * We need to init the psy before we can call
1028 * power_supply_get_battery_info() for it
1029 */
1030 bd->charger = devm_power_supply_register(bd->dev,
1031 &bd9995x_power_supply_desc,
1032 &psy_cfg);
1033 if (IS_ERR(bd->charger)) {
1034 dev_err(dev, "Failed to register power supply\n");
1035 return PTR_ERR(bd->charger);
1036 }
1037
1038 ret = bd9995x_fw_probe(bd);
1039 if (ret < 0) {
1040 dev_err(dev, "Cannot read device properties.\n");
1041 return ret;
1042 }
1043
1044 ret = bd9995x_hw_init(bd);
1045 if (ret < 0) {
1046 dev_err(dev, "Cannot initialize the chip.\n");
1047 return ret;
1048 }
1049
1050 ret = devm_add_action_or_reset(dev, bd9995x_chip_reset, bd);
1051 if (ret)
1052 return ret;
1053
1054 return devm_request_threaded_irq(dev, client->irq, NULL,
1055 bd9995x_irq_handler_thread,
1056 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1057 BD9995X_IRQ_PIN, bd);
1058 }
1059
1060 static const struct of_device_id bd9995x_of_match[] = {
1061 { .compatible = "rohm,bd99954", },
1062 { }
1063 };
1064 MODULE_DEVICE_TABLE(of, bd9995x_of_match);
1065
1066 static struct i2c_driver bd9995x_driver = {
1067 .driver = {
1068 .name = "bd9995x-charger",
1069 .of_match_table = bd9995x_of_match,
1070 },
1071 .probe = bd9995x_probe,
1072 };
1073 module_i2c_driver(bd9995x_driver);
1074
1075 MODULE_AUTHOR("Laine Markus <markus.laine@fi.rohmeurope.com>");
1076 MODULE_DESCRIPTION("ROHM BD99954 charger driver");
1077 MODULE_LICENSE("GPL");
1078