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