1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/interrupt.h>
17#include <linux/delay.h>
18#include <linux/slab.h>
19#include <linux/platform_device.h>
20#include <linux/power_supply.h>
21#include <linux/completion.h>
22#include <linux/workqueue.h>
23#include <linux/jiffies.h>
24#include <linux/of.h>
25#include <linux/mfd/core.h>
26#include <linux/mfd/abx500.h>
27#include <linux/mfd/abx500/ab8500.h>
28#include <linux/mfd/abx500/ab8500-bm.h>
29#include <linux/iio/consumer.h>
30
31#define VTVOUT_V 1800
32
33#define BTEMP_THERMAL_LOW_LIMIT -10
34#define BTEMP_THERMAL_MED_LIMIT 0
35#define BTEMP_THERMAL_HIGH_LIMIT_52 52
36#define BTEMP_THERMAL_HIGH_LIMIT_57 57
37#define BTEMP_THERMAL_HIGH_LIMIT_62 62
38
39#define BTEMP_BATCTRL_CURR_SRC_7UA 7
40#define BTEMP_BATCTRL_CURR_SRC_20UA 20
41
42#define BTEMP_BATCTRL_CURR_SRC_16UA 16
43#define BTEMP_BATCTRL_CURR_SRC_18UA 18
44
45#define BTEMP_BATCTRL_CURR_SRC_60UA 60
46#define BTEMP_BATCTRL_CURR_SRC_120UA 120
47
48
49
50
51
52
53struct ab8500_btemp_interrupts {
54 char *name;
55 irqreturn_t (*isr)(int irq, void *data);
56};
57
58struct ab8500_btemp_events {
59 bool batt_rem;
60 bool btemp_high;
61 bool btemp_medhigh;
62 bool btemp_lowmed;
63 bool btemp_low;
64 bool ac_conn;
65 bool usb_conn;
66};
67
68struct ab8500_btemp_ranges {
69 int btemp_high_limit;
70 int btemp_med_limit;
71 int btemp_low_limit;
72};
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93struct ab8500_btemp {
94 struct device *dev;
95 struct list_head node;
96 int curr_source;
97 int bat_temp;
98 int prev_bat_temp;
99 struct ab8500 *parent;
100 struct iio_channel *btemp_ball;
101 struct iio_channel *bat_ctrl;
102 struct ab8500_fg *fg;
103 struct abx500_bm_data *bm;
104 struct power_supply *btemp_psy;
105 struct ab8500_btemp_events events;
106 struct ab8500_btemp_ranges btemp_ranges;
107 struct workqueue_struct *btemp_wq;
108 struct delayed_work btemp_periodic_work;
109 bool initialized;
110};
111
112
113static enum power_supply_property ab8500_btemp_props[] = {
114 POWER_SUPPLY_PROP_PRESENT,
115 POWER_SUPPLY_PROP_ONLINE,
116 POWER_SUPPLY_PROP_TECHNOLOGY,
117 POWER_SUPPLY_PROP_TEMP,
118};
119
120static LIST_HEAD(ab8500_btemp_list);
121
122
123
124
125
126struct ab8500_btemp *ab8500_btemp_get(void)
127{
128 return list_first_entry(&ab8500_btemp_list, struct ab8500_btemp, node);
129}
130EXPORT_SYMBOL(ab8500_btemp_get);
131
132
133
134
135
136
137
138
139
140
141
142static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
143 int v_batctrl, int inst_curr)
144{
145 int rbs;
146
147 if (is_ab8500_1p1_or_earlier(di->parent)) {
148
149
150
151
152 return (450000 * (v_batctrl)) / (1800 - v_batctrl);
153 }
154
155 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL) {
156
157
158
159
160 rbs = (v_batctrl * 1000
161 - di->bm->gnd_lift_resistance * inst_curr)
162 / di->curr_source;
163 } else {
164
165
166
167
168 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
169 }
170
171 return rbs;
172}
173
174
175
176
177
178
179
180static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
181{
182 int vbtemp, ret;
183 static int prev;
184
185 ret = iio_read_channel_processed(di->bat_ctrl, &vbtemp);
186 if (ret < 0) {
187 dev_err(di->dev,
188 "%s ADC conversion failed, using previous value",
189 __func__);
190 return prev;
191 }
192 prev = vbtemp;
193 return vbtemp;
194}
195
196
197
198
199
200
201
202
203static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
204 bool enable)
205{
206 int curr;
207 int ret = 0;
208
209
210
211
212
213 if (is_ab8500_1p1_or_earlier(di->parent))
214 return 0;
215
216
217 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) {
218
219 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
220 curr = BAT_CTRL_7U_ENA;
221 else
222 curr = BAT_CTRL_20U_ENA;
223
224 dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
225
226 ret = abx500_mask_and_set_register_interruptible(di->dev,
227 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
228 FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH);
229 if (ret) {
230 dev_err(di->dev, "%s failed setting cmp_force\n",
231 __func__);
232 return ret;
233 }
234
235
236
237
238
239
240 udelay(32);
241
242 ret = abx500_set_register_interruptible(di->dev,
243 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
244 FORCE_BAT_CTRL_CMP_HIGH | curr);
245 if (ret) {
246 dev_err(di->dev, "%s failed enabling current source\n",
247 __func__);
248 goto disable_curr_source;
249 }
250 } else if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) {
251 dev_dbg(di->dev, "Disable BATCTRL curr source\n");
252
253
254 ret = abx500_mask_and_set_register_interruptible(
255 di->dev,
256 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
257 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
258 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
259
260 if (ret) {
261 dev_err(di->dev, "%s failed disabling current source\n",
262 __func__);
263 goto disable_curr_source;
264 }
265
266
267 ret = abx500_mask_and_set_register_interruptible(di->dev,
268 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
269 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
270 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
271 if (ret) {
272 dev_err(di->dev, "%s failed enabling PU and comp\n",
273 __func__);
274 goto enable_pu_comp;
275 }
276
277
278
279
280
281
282 udelay(32);
283
284
285 ret = abx500_mask_and_set_register_interruptible(di->dev,
286 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
287 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
288 if (ret) {
289 dev_err(di->dev, "%s failed disabling force comp\n",
290 __func__);
291 goto disable_force_comp;
292 }
293 }
294 return ret;
295
296
297
298
299
300disable_curr_source:
301
302 ret = abx500_mask_and_set_register_interruptible(di->dev,
303 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
304 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
305 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
306
307 if (ret) {
308 dev_err(di->dev, "%s failed disabling current source\n",
309 __func__);
310 return ret;
311 }
312enable_pu_comp:
313
314 ret = abx500_mask_and_set_register_interruptible(di->dev,
315 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
316 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
317 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
318 if (ret) {
319 dev_err(di->dev, "%s failed enabling PU and comp\n",
320 __func__);
321 return ret;
322 }
323
324disable_force_comp:
325
326
327
328
329
330 udelay(32);
331
332
333 ret = abx500_mask_and_set_register_interruptible(di->dev,
334 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
335 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
336 if (ret) {
337 dev_err(di->dev, "%s failed disabling force comp\n",
338 __func__);
339 return ret;
340 }
341
342 return ret;
343}
344
345
346
347
348
349
350
351
352static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
353{
354 int ret;
355 int batctrl = 0;
356 int res;
357 int inst_curr;
358 int i;
359
360
361
362
363
364 ret = ab8500_btemp_curr_source_enable(di, true);
365 if (ret) {
366 dev_err(di->dev, "%s curr source enabled failed\n", __func__);
367 return ret;
368 }
369
370 if (!di->fg)
371 di->fg = ab8500_fg_get();
372 if (!di->fg) {
373 dev_err(di->dev, "No fg found\n");
374 return -EINVAL;
375 }
376
377 ret = ab8500_fg_inst_curr_start(di->fg);
378
379 if (ret) {
380 dev_err(di->dev, "Failed to start current measurement\n");
381 return ret;
382 }
383
384 do {
385 msleep(20);
386 } while (!ab8500_fg_inst_curr_started(di->fg));
387
388 i = 0;
389
390 do {
391 batctrl += ab8500_btemp_read_batctrl_voltage(di);
392 i++;
393 msleep(20);
394 } while (!ab8500_fg_inst_curr_done(di->fg));
395 batctrl /= i;
396
397 ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
398 if (ret) {
399 dev_err(di->dev, "Failed to finalize current measurement\n");
400 return ret;
401 }
402
403 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
404
405 ret = ab8500_btemp_curr_source_enable(di, false);
406 if (ret) {
407 dev_err(di->dev, "%s curr source disable failed\n", __func__);
408 return ret;
409 }
410
411 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
412 __func__, batctrl, res, inst_curr, i);
413
414 return res;
415}
416
417
418
419
420
421
422
423
424
425
426
427static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
428 const struct abx500_res_to_temp *tbl, int tbl_size, int res)
429{
430 int i;
431
432
433
434
435
436
437 if (res > tbl[0].resist)
438 i = 0;
439 else if (res <= tbl[tbl_size - 1].resist)
440 i = tbl_size - 2;
441 else {
442 i = 0;
443 while (!(res <= tbl[i].resist &&
444 res > tbl[i + 1].resist))
445 i++;
446 }
447
448 return tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
449 (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
450}
451
452
453
454
455
456
457
458static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
459{
460 int temp, ret;
461 static int prev;
462 int rbat, rntc, vntc;
463 u8 id;
464
465 id = di->bm->batt_id;
466
467 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
468 id != BATTERY_UNKNOWN) {
469
470 rbat = ab8500_btemp_get_batctrl_res(di);
471 if (rbat < 0) {
472 dev_err(di->dev, "%s get batctrl res failed\n",
473 __func__);
474
475
476
477
478 return BTEMP_THERMAL_LOW_LIMIT;
479 }
480
481 temp = ab8500_btemp_res_to_temp(di,
482 di->bm->bat_type[id].r_to_t_tbl,
483 di->bm->bat_type[id].n_temp_tbl_elements, rbat);
484 } else {
485 ret = iio_read_channel_processed(di->btemp_ball, &vntc);
486 if (ret < 0) {
487 dev_err(di->dev,
488 "%s ADC conversion failed,"
489 " using previous value\n", __func__);
490 return prev;
491 }
492
493
494
495
496 rntc = 230000 * vntc / (VTVOUT_V - vntc);
497
498 temp = ab8500_btemp_res_to_temp(di,
499 di->bm->bat_type[id].r_to_t_tbl,
500 di->bm->bat_type[id].n_temp_tbl_elements, rntc);
501 prev = temp;
502 }
503 dev_dbg(di->dev, "Battery temperature is %d\n", temp);
504 return temp;
505}
506
507
508
509
510
511
512
513
514
515static int ab8500_btemp_id(struct ab8500_btemp *di)
516{
517 int res;
518 u8 i;
519
520 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
521 di->bm->batt_id = BATTERY_UNKNOWN;
522
523 res = ab8500_btemp_get_batctrl_res(di);
524 if (res < 0) {
525 dev_err(di->dev, "%s get batctrl res failed\n", __func__);
526 return -ENXIO;
527 }
528
529
530 for (i = BATTERY_UNKNOWN + 1; i < di->bm->n_btypes; i++) {
531 if ((res <= di->bm->bat_type[i].resis_high) &&
532 (res >= di->bm->bat_type[i].resis_low)) {
533 dev_dbg(di->dev, "Battery detected on %s"
534 " low %d < res %d < high: %d"
535 " index: %d\n",
536 di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ?
537 "BATCTRL" : "BATTEMP",
538 di->bm->bat_type[i].resis_low, res,
539 di->bm->bat_type[i].resis_high, i);
540
541 di->bm->batt_id = i;
542 break;
543 }
544 }
545
546 if (di->bm->batt_id == BATTERY_UNKNOWN) {
547 dev_warn(di->dev, "Battery identified as unknown"
548 ", resistance %d Ohm\n", res);
549 return -ENXIO;
550 }
551
552
553
554
555
556 if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
557 di->bm->batt_id == 1) {
558 dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
559 di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
560 }
561
562 return di->bm->batt_id;
563}
564
565
566
567
568
569
570
571static void ab8500_btemp_periodic_work(struct work_struct *work)
572{
573 int interval;
574 int bat_temp;
575 struct ab8500_btemp *di = container_of(work,
576 struct ab8500_btemp, btemp_periodic_work.work);
577
578 if (!di->initialized) {
579
580 if (ab8500_btemp_id(di) < 0)
581 dev_warn(di->dev, "failed to identify the battery\n");
582 }
583
584 bat_temp = ab8500_btemp_measure_temp(di);
585
586
587
588
589
590
591 if ((bat_temp == di->prev_bat_temp) || !di->initialized) {
592 if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) {
593 di->initialized = true;
594 di->bat_temp = bat_temp;
595 power_supply_changed(di->btemp_psy);
596 }
597 } else if (bat_temp < di->prev_bat_temp) {
598 di->bat_temp--;
599 power_supply_changed(di->btemp_psy);
600 } else if (bat_temp > di->prev_bat_temp) {
601 di->bat_temp++;
602 power_supply_changed(di->btemp_psy);
603 }
604 di->prev_bat_temp = bat_temp;
605
606 if (di->events.ac_conn || di->events.usb_conn)
607 interval = di->bm->temp_interval_chg;
608 else
609 interval = di->bm->temp_interval_nochg;
610
611
612 queue_delayed_work(di->btemp_wq,
613 &di->btemp_periodic_work,
614 round_jiffies(interval * HZ));
615}
616
617
618
619
620
621
622
623
624static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
625{
626 struct ab8500_btemp *di = _di;
627 dev_err(di->dev, "Battery removal detected!\n");
628
629 di->events.batt_rem = true;
630 power_supply_changed(di->btemp_psy);
631
632 return IRQ_HANDLED;
633}
634
635
636
637
638
639
640
641
642static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
643{
644 struct ab8500_btemp *di = _di;
645
646 if (is_ab8500_3p3_or_earlier(di->parent)) {
647 dev_dbg(di->dev, "Ignore false btemp low irq"
648 " for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
649 } else {
650 dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
651
652 di->events.btemp_low = true;
653 di->events.btemp_high = false;
654 di->events.btemp_medhigh = false;
655 di->events.btemp_lowmed = false;
656 power_supply_changed(di->btemp_psy);
657 }
658
659 return IRQ_HANDLED;
660}
661
662
663
664
665
666
667
668
669static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
670{
671 struct ab8500_btemp *di = _di;
672
673 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
674
675 di->events.btemp_high = true;
676 di->events.btemp_medhigh = false;
677 di->events.btemp_lowmed = false;
678 di->events.btemp_low = false;
679 power_supply_changed(di->btemp_psy);
680
681 return IRQ_HANDLED;
682}
683
684
685
686
687
688
689
690
691static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
692{
693 struct ab8500_btemp *di = _di;
694
695 dev_dbg(di->dev, "Battery temperature is between low and medium\n");
696
697 di->events.btemp_lowmed = true;
698 di->events.btemp_medhigh = false;
699 di->events.btemp_high = false;
700 di->events.btemp_low = false;
701 power_supply_changed(di->btemp_psy);
702
703 return IRQ_HANDLED;
704}
705
706
707
708
709
710
711
712
713static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
714{
715 struct ab8500_btemp *di = _di;
716
717 dev_dbg(di->dev, "Battery temperature is between medium and high\n");
718
719 di->events.btemp_medhigh = true;
720 di->events.btemp_lowmed = false;
721 di->events.btemp_high = false;
722 di->events.btemp_low = false;
723 power_supply_changed(di->btemp_psy);
724
725 return IRQ_HANDLED;
726}
727
728
729
730
731
732
733
734
735
736static void ab8500_btemp_periodic(struct ab8500_btemp *di,
737 bool enable)
738{
739 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
740 enable);
741
742
743
744
745 cancel_delayed_work_sync(&di->btemp_periodic_work);
746
747 if (enable)
748 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
749}
750
751
752
753
754
755
756
757int ab8500_btemp_get_temp(struct ab8500_btemp *di)
758{
759 int temp = 0;
760
761
762
763
764
765 if (is_ab8500_3p3_or_earlier(di->parent)) {
766 temp = di->bat_temp * 10;
767 } else {
768 if (di->events.btemp_low) {
769 if (temp > di->btemp_ranges.btemp_low_limit)
770 temp = di->btemp_ranges.btemp_low_limit * 10;
771 else
772 temp = di->bat_temp * 10;
773 } else if (di->events.btemp_high) {
774 if (temp < di->btemp_ranges.btemp_high_limit)
775 temp = di->btemp_ranges.btemp_high_limit * 10;
776 else
777 temp = di->bat_temp * 10;
778 } else if (di->events.btemp_lowmed) {
779 if (temp > di->btemp_ranges.btemp_med_limit)
780 temp = di->btemp_ranges.btemp_med_limit * 10;
781 else
782 temp = di->bat_temp * 10;
783 } else if (di->events.btemp_medhigh) {
784 if (temp < di->btemp_ranges.btemp_med_limit)
785 temp = di->btemp_ranges.btemp_med_limit * 10;
786 else
787 temp = di->bat_temp * 10;
788 } else
789 temp = di->bat_temp * 10;
790 }
791 return temp;
792}
793EXPORT_SYMBOL(ab8500_btemp_get_temp);
794
795
796
797
798
799
800
801int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp)
802{
803 return btemp->bat_temp * 1000;
804}
805EXPORT_SYMBOL(ab8500_btemp_get_batctrl_temp);
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821static int ab8500_btemp_get_property(struct power_supply *psy,
822 enum power_supply_property psp,
823 union power_supply_propval *val)
824{
825 struct ab8500_btemp *di = power_supply_get_drvdata(psy);
826
827 switch (psp) {
828 case POWER_SUPPLY_PROP_PRESENT:
829 case POWER_SUPPLY_PROP_ONLINE:
830 if (di->events.batt_rem)
831 val->intval = 0;
832 else
833 val->intval = 1;
834 break;
835 case POWER_SUPPLY_PROP_TECHNOLOGY:
836 val->intval = di->bm->bat_type[di->bm->batt_id].name;
837 break;
838 case POWER_SUPPLY_PROP_TEMP:
839 val->intval = ab8500_btemp_get_temp(di);
840 break;
841 default:
842 return -EINVAL;
843 }
844 return 0;
845}
846
847static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
848{
849 struct power_supply *psy;
850 struct power_supply *ext = dev_get_drvdata(dev);
851 const char **supplicants = (const char **)ext->supplied_to;
852 struct ab8500_btemp *di;
853 union power_supply_propval ret;
854 int j;
855
856 psy = (struct power_supply *)data;
857 di = power_supply_get_drvdata(psy);
858
859
860
861
862
863 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
864 if (j < 0)
865 return 0;
866
867
868 for (j = 0; j < ext->desc->num_properties; j++) {
869 enum power_supply_property prop;
870 prop = ext->desc->properties[j];
871
872 if (power_supply_get_property(ext, prop, &ret))
873 continue;
874
875 switch (prop) {
876 case POWER_SUPPLY_PROP_PRESENT:
877 switch (ext->desc->type) {
878 case POWER_SUPPLY_TYPE_MAINS:
879
880 if (!ret.intval && di->events.ac_conn) {
881 di->events.ac_conn = false;
882 }
883
884 else if (ret.intval && !di->events.ac_conn) {
885 di->events.ac_conn = true;
886 if (!di->events.usb_conn)
887 ab8500_btemp_periodic(di, true);
888 }
889 break;
890 case POWER_SUPPLY_TYPE_USB:
891
892 if (!ret.intval && di->events.usb_conn) {
893 di->events.usb_conn = false;
894 }
895
896 else if (ret.intval && !di->events.usb_conn) {
897 di->events.usb_conn = true;
898 if (!di->events.ac_conn)
899 ab8500_btemp_periodic(di, true);
900 }
901 break;
902 default:
903 break;
904 }
905 break;
906 default:
907 break;
908 }
909 }
910 return 0;
911}
912
913
914
915
916
917
918
919
920
921
922static void ab8500_btemp_external_power_changed(struct power_supply *psy)
923{
924 struct ab8500_btemp *di = power_supply_get_drvdata(psy);
925
926 class_for_each_device(power_supply_class, NULL,
927 di->btemp_psy, ab8500_btemp_get_ext_psy_data);
928}
929
930
931static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
932 {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
933 {"BTEMP_LOW", ab8500_btemp_templow_handler},
934 {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
935 {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
936 {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
937};
938
939#if defined(CONFIG_PM)
940static int ab8500_btemp_resume(struct platform_device *pdev)
941{
942 struct ab8500_btemp *di = platform_get_drvdata(pdev);
943
944 ab8500_btemp_periodic(di, true);
945
946 return 0;
947}
948
949static int ab8500_btemp_suspend(struct platform_device *pdev,
950 pm_message_t state)
951{
952 struct ab8500_btemp *di = platform_get_drvdata(pdev);
953
954 ab8500_btemp_periodic(di, false);
955
956 return 0;
957}
958#else
959#define ab8500_btemp_suspend NULL
960#define ab8500_btemp_resume NULL
961#endif
962
963static int ab8500_btemp_remove(struct platform_device *pdev)
964{
965 struct ab8500_btemp *di = platform_get_drvdata(pdev);
966 int i, irq;
967
968
969 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
970 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
971 free_irq(irq, di);
972 }
973
974
975 destroy_workqueue(di->btemp_wq);
976
977 flush_scheduled_work();
978 power_supply_unregister(di->btemp_psy);
979
980 return 0;
981}
982
983static char *supply_interface[] = {
984 "ab8500_chargalg",
985 "ab8500_fg",
986};
987
988static const struct power_supply_desc ab8500_btemp_desc = {
989 .name = "ab8500_btemp",
990 .type = POWER_SUPPLY_TYPE_BATTERY,
991 .properties = ab8500_btemp_props,
992 .num_properties = ARRAY_SIZE(ab8500_btemp_props),
993 .get_property = ab8500_btemp_get_property,
994 .external_power_changed = ab8500_btemp_external_power_changed,
995};
996
997static int ab8500_btemp_probe(struct platform_device *pdev)
998{
999 struct device_node *np = pdev->dev.of_node;
1000 struct abx500_bm_data *plat = pdev->dev.platform_data;
1001 struct power_supply_config psy_cfg = {};
1002 struct ab8500_btemp *di;
1003 int irq, i, ret = 0;
1004 u8 val;
1005
1006 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1007 if (!di) {
1008 dev_err(&pdev->dev, "%s no mem for ab8500_btemp\n", __func__);
1009 return -ENOMEM;
1010 }
1011
1012 if (!plat) {
1013 dev_err(&pdev->dev, "no battery management data supplied\n");
1014 return -EINVAL;
1015 }
1016 di->bm = plat;
1017
1018 if (np) {
1019 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
1020 if (ret) {
1021 dev_err(&pdev->dev, "failed to get battery information\n");
1022 return ret;
1023 }
1024 }
1025
1026
1027 di->dev = &pdev->dev;
1028 di->parent = dev_get_drvdata(pdev->dev.parent);
1029
1030
1031 di->btemp_ball = devm_iio_channel_get(&pdev->dev, "btemp_ball");
1032 if (IS_ERR(di->btemp_ball)) {
1033 if (PTR_ERR(di->btemp_ball) == -ENODEV)
1034 return -EPROBE_DEFER;
1035 dev_err(&pdev->dev, "failed to get BTEMP BALL ADC channel\n");
1036 return PTR_ERR(di->btemp_ball);
1037 }
1038 di->bat_ctrl = devm_iio_channel_get(&pdev->dev, "bat_ctrl");
1039 if (IS_ERR(di->bat_ctrl)) {
1040 if (PTR_ERR(di->bat_ctrl) == -ENODEV)
1041 return -EPROBE_DEFER;
1042 dev_err(&pdev->dev, "failed to get BAT CTRL ADC channel\n");
1043 return PTR_ERR(di->bat_ctrl);
1044 }
1045
1046 di->initialized = false;
1047
1048 psy_cfg.supplied_to = supply_interface;
1049 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
1050 psy_cfg.drv_data = di;
1051
1052
1053 di->btemp_wq =
1054 alloc_workqueue("ab8500_btemp_wq", WQ_MEM_RECLAIM, 0);
1055 if (di->btemp_wq == NULL) {
1056 dev_err(di->dev, "failed to create work queue\n");
1057 return -ENOMEM;
1058 }
1059
1060
1061 INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
1062 ab8500_btemp_periodic_work);
1063
1064
1065 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
1066 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
1067
1068 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1069 AB8500_BTEMP_HIGH_TH, &val);
1070 if (ret < 0) {
1071 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1072 goto free_btemp_wq;
1073 }
1074 switch (val) {
1075 case BTEMP_HIGH_TH_57_0:
1076 case BTEMP_HIGH_TH_57_1:
1077 di->btemp_ranges.btemp_high_limit =
1078 BTEMP_THERMAL_HIGH_LIMIT_57;
1079 break;
1080 case BTEMP_HIGH_TH_52:
1081 di->btemp_ranges.btemp_high_limit =
1082 BTEMP_THERMAL_HIGH_LIMIT_52;
1083 break;
1084 case BTEMP_HIGH_TH_62:
1085 di->btemp_ranges.btemp_high_limit =
1086 BTEMP_THERMAL_HIGH_LIMIT_62;
1087 break;
1088 }
1089
1090
1091 di->btemp_psy = power_supply_register(di->dev, &ab8500_btemp_desc,
1092 &psy_cfg);
1093 if (IS_ERR(di->btemp_psy)) {
1094 dev_err(di->dev, "failed to register BTEMP psy\n");
1095 ret = PTR_ERR(di->btemp_psy);
1096 goto free_btemp_wq;
1097 }
1098
1099
1100 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1101 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1102 if (irq < 0) {
1103 ret = irq;
1104 goto free_irq;
1105 }
1106
1107 ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
1108 IRQF_SHARED | IRQF_NO_SUSPEND,
1109 ab8500_btemp_irq[i].name, di);
1110
1111 if (ret) {
1112 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
1113 , ab8500_btemp_irq[i].name, irq, ret);
1114 goto free_irq;
1115 }
1116 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
1117 ab8500_btemp_irq[i].name, irq, ret);
1118 }
1119
1120 platform_set_drvdata(pdev, di);
1121
1122
1123 ab8500_btemp_periodic(di, true);
1124 list_add_tail(&di->node, &ab8500_btemp_list);
1125
1126 return ret;
1127
1128free_irq:
1129
1130 for (i = i - 1; i >= 0; i--) {
1131 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1132 free_irq(irq, di);
1133 }
1134
1135 power_supply_unregister(di->btemp_psy);
1136free_btemp_wq:
1137 destroy_workqueue(di->btemp_wq);
1138 return ret;
1139}
1140
1141static const struct of_device_id ab8500_btemp_match[] = {
1142 { .compatible = "stericsson,ab8500-btemp", },
1143 { },
1144};
1145
1146static struct platform_driver ab8500_btemp_driver = {
1147 .probe = ab8500_btemp_probe,
1148 .remove = ab8500_btemp_remove,
1149 .suspend = ab8500_btemp_suspend,
1150 .resume = ab8500_btemp_resume,
1151 .driver = {
1152 .name = "ab8500-btemp",
1153 .of_match_table = ab8500_btemp_match,
1154 },
1155};
1156
1157static int __init ab8500_btemp_init(void)
1158{
1159 return platform_driver_register(&ab8500_btemp_driver);
1160}
1161
1162static void __exit ab8500_btemp_exit(void)
1163{
1164 platform_driver_unregister(&ab8500_btemp_driver);
1165}
1166
1167device_initcall(ab8500_btemp_init);
1168module_exit(ab8500_btemp_exit);
1169
1170MODULE_LICENSE("GPL v2");
1171MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1172MODULE_ALIAS("platform:ab8500-btemp");
1173MODULE_DESCRIPTION("AB8500 battery temperature driver");
1174