Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ab8500_fg.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) ST-Ericsson AB 2012
3  *
4  * Main and Back-up battery management driver.
5  *
6  * Note: Backup battery management is required in case of Li-Ion battery and not
7  * for capacitive battery. HREF boards have capacitive battery and hence backup
8  * battery management is not used and the supported code is available in this
9  * driver.
10  *
11  * License Terms: GNU General Public License v2
12  * Author:
13  * Johan Palsson <[email protected]>
14  * Karl Komierowski <[email protected]>
15  * Arun R Murthy <[email protected]>
16  */
17 
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/interrupt.h>
22 #include <linux/platform_device.h>
23 #include <linux/power_supply.h>
24 #include <linux/kobject.h>
26 #include <linux/mfd/abx500.h>
27 #include <linux/slab.h>
29 #include <linux/delay.h>
31 #include <linux/mfd/abx500.h>
32 #include <linux/time.h>
33 #include <linux/completion.h>
34 
35 #define MILLI_TO_MICRO 1000
36 #define FG_LSB_IN_MA 1627
37 #define QLSB_NANO_AMP_HOURS_X10 1129
38 #define INS_CURR_TIMEOUT (3 * HZ)
39 
40 #define SEC_TO_SAMPLE(S) (S * 4)
41 
42 #define NBR_AVG_SAMPLES 20
43 
44 #define LOW_BAT_CHECK_INTERVAL (2 * HZ)
45 
46 #define VALID_CAPACITY_SEC (45 * 60) /* 45 minutes */
47 #define BATT_OK_MIN 2360 /* mV */
48 #define BATT_OK_INCREMENT 50 /* mV */
49 #define BATT_OK_MAX_NR_INCREMENTS 0xE
50 
51 /* FG constants */
52 #define BATT_OVV 0x01
53 
54 #define interpolate(x, x1, y1, x2, y2) \
55  ((y1) + ((((y2) - (y1)) * ((x) - (x1))) / ((x2) - (x1))));
56 
57 #define to_ab8500_fg_device_info(x) container_of((x), \
58  struct ab8500_fg, fg_psy);
59 
66  char *name;
67  irqreturn_t (*isr)(int irq, void *data);
68 };
69 
78 };
79 
80 static char *discharge_state[] = {
81  "DISCHARGE_INIT",
82  "DISCHARGE_INITMEASURING",
83  "DISCHARGE_INIT_RECOVERY",
84  "DISCHARGE_RECOVERY",
85  "DISCHARGE_READOUT_INIT",
86  "DISCHARGE_READOUT",
87  "DISCHARGE_WAKEUP",
88 };
89 
93 };
94 
95 static char *charge_state[] = {
96  "CHARGE_INIT",
97  "CHARGE_READOUT",
98 };
99 
104 };
105 
107  int avg;
110  int pos;
112  int sum;
113 };
114 
117  int max_mah;
118  int mah;
119  int permille;
120  int level;
121  int prev_mah;
124  int user_mah;
125 };
126 
129  bool conv_done;
130  bool charging;
134  bool low_bat;
135  bool bat_ovv;
137  bool calibrate;
138  bool user_cap;
140 };
141 
143  struct list_head list;
144  int *result;
145 };
146 
188 struct ab8500_fg {
189  struct device *dev;
190  struct list_head node;
191  int irq;
192  int vbat;
193  int vbat_nom;
195  int avg_curr;
196  int bat_temp;
201  int init_cnt;
213  struct ab8500 *parent;
225  struct mutex cc_lock;
227 };
228 static LIST_HEAD(ab8500_fg_list);
229 
235 {
236  struct ab8500_fg *fg;
237 
238  if (list_empty(&ab8500_fg_list))
239  return NULL;
240 
241  fg = list_first_entry(&ab8500_fg_list, struct ab8500_fg, node);
242  return fg;
243 }
244 
245 /* Main battery properties */
246 static enum power_supply_property ab8500_fg_props[] = {
258 };
259 
260 /*
261  * This array maps the raw hex value to lowbat voltage used by the AB8500
262  * Values taken from the UM0836
263  */
264 static int ab8500_fg_lowbat_voltage_map[] = {
265  2300 ,
266  2325 ,
267  2350 ,
268  2375 ,
269  2400 ,
270  2425 ,
271  2450 ,
272  2475 ,
273  2500 ,
274  2525 ,
275  2550 ,
276  2575 ,
277  2600 ,
278  2625 ,
279  2650 ,
280  2675 ,
281  2700 ,
282  2725 ,
283  2750 ,
284  2775 ,
285  2800 ,
286  2825 ,
287  2850 ,
288  2875 ,
289  2900 ,
290  2925 ,
291  2950 ,
292  2975 ,
293  3000 ,
294  3025 ,
295  3050 ,
296  3075 ,
297  3100 ,
298  3125 ,
299  3150 ,
300  3175 ,
301  3200 ,
302  3225 ,
303  3250 ,
304  3275 ,
305  3300 ,
306  3325 ,
307  3350 ,
308  3375 ,
309  3400 ,
310  3425 ,
311  3450 ,
312  3475 ,
313  3500 ,
314  3525 ,
315  3550 ,
316  3575 ,
317  3600 ,
318  3625 ,
319  3650 ,
320  3675 ,
321  3700 ,
322  3725 ,
323  3750 ,
324  3775 ,
325  3800 ,
326  3825 ,
327  3850 ,
328  3850 ,
329 };
330 
331 static u8 ab8500_volt_to_regval(int voltage)
332 {
333  int i;
334 
335  if (voltage < ab8500_fg_lowbat_voltage_map[0])
336  return 0;
337 
338  for (i = 0; i < ARRAY_SIZE(ab8500_fg_lowbat_voltage_map); i++) {
339  if (voltage < ab8500_fg_lowbat_voltage_map[i])
340  return (u8) i - 1;
341  }
342 
343  /* If not captured above, return index of last element */
344  return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1;
345 }
346 
354 static int ab8500_fg_is_low_curr(struct ab8500_fg *di, int curr)
355 {
356  /*
357  * We want to know if we're in low current mode
358  */
359  if (curr > -di->bat->fg_params->high_curr_threshold)
360  return true;
361  else
362  return false;
363 }
364 
373 static int ab8500_fg_add_cap_sample(struct ab8500_fg *di, int sample)
374 {
375  struct timespec ts;
376  struct ab8500_fg_avg_cap *avg = &di->avg_cap;
377 
378  getnstimeofday(&ts);
379 
380  do {
381  avg->sum += sample - avg->samples[avg->pos];
382  avg->samples[avg->pos] = sample;
383  avg->time_stamps[avg->pos] = ts.tv_sec;
384  avg->pos++;
385 
386  if (avg->pos == NBR_AVG_SAMPLES)
387  avg->pos = 0;
388 
389  if (avg->nbr_samples < NBR_AVG_SAMPLES)
390  avg->nbr_samples++;
391 
392  /*
393  * Check the time stamp for each sample. If too old,
394  * replace with latest sample
395  */
396  } while (ts.tv_sec - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]);
397 
398  avg->avg = avg->sum / avg->nbr_samples;
399 
400  return avg->avg;
401 }
402 
409 static void ab8500_fg_clear_cap_samples(struct ab8500_fg *di)
410 {
411  int i;
412  struct ab8500_fg_avg_cap *avg = &di->avg_cap;
413 
414  avg->pos = 0;
415  avg->nbr_samples = 0;
416  avg->sum = 0;
417  avg->avg = 0;
418 
419  for (i = 0; i < NBR_AVG_SAMPLES; i++) {
420  avg->samples[i] = 0;
421  avg->time_stamps[i] = 0;
422  }
423 }
424 
432 static void ab8500_fg_fill_cap_sample(struct ab8500_fg *di, int sample)
433 {
434  int i;
435  struct timespec ts;
436  struct ab8500_fg_avg_cap *avg = &di->avg_cap;
437 
438  getnstimeofday(&ts);
439 
440  for (i = 0; i < NBR_AVG_SAMPLES; i++) {
441  avg->samples[i] = sample;
442  avg->time_stamps[i] = ts.tv_sec;
443  }
444 
445  avg->pos = 0;
447  avg->sum = sample * NBR_AVG_SAMPLES;
448  avg->avg = sample;
449 }
450 
459 static int ab8500_fg_coulomb_counter(struct ab8500_fg *di, bool enable)
460 {
461  int ret = 0;
462  mutex_lock(&di->cc_lock);
463  if (enable) {
464  /* To be able to reprogram the number of samples, we have to
465  * first stop the CC and then enable it again */
467  AB8500_RTC_CC_CONF_REG, 0x00);
468  if (ret)
469  goto cc_err;
470 
471  /* Program the samples */
474  di->fg_samples);
475  if (ret)
476  goto cc_err;
477 
478  /* Start the CC */
482  if (ret)
483  goto cc_err;
484 
485  di->flags.fg_enabled = true;
486  } else {
487  /* Clear any pending read requests */
490  if (ret)
491  goto cc_err;
492 
495  if (ret)
496  goto cc_err;
497 
498  /* Stop the CC */
501  if (ret)
502  goto cc_err;
503 
504  di->flags.fg_enabled = false;
505 
506  }
507  dev_dbg(di->dev, " CC enabled: %d Samples: %d\n",
508  enable, di->fg_samples);
509 
510  mutex_unlock(&di->cc_lock);
511 
512  return ret;
513 cc_err:
514  dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__);
515  mutex_unlock(&di->cc_lock);
516  return ret;
517 }
518 
528 {
529  u8 reg_val;
530  int ret;
531 
532  mutex_lock(&di->cc_lock);
533 
535  AB8500_RTC_CC_CONF_REG, &reg_val);
536  if (ret < 0)
537  goto fail;
538 
539  if (!(reg_val & CC_PWR_UP_ENA)) {
540  dev_dbg(di->dev, "%s Enable FG\n", __func__);
541  di->turn_off_fg = true;
542 
543  /* Program the samples */
546  SEC_TO_SAMPLE(10));
547  if (ret)
548  goto fail;
549 
550  /* Start the CC */
553  (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
554  if (ret)
555  goto fail;
556  } else {
557  di->turn_off_fg = false;
558  }
559 
560  /* Return and WFI */
562  enable_irq(di->irq);
563 
564  /* Note: cc_lock is still locked */
565  return 0;
566 fail:
567  mutex_unlock(&di->cc_lock);
568  return ret;
569 }
570 
578 {
579  return completion_done(&di->ab8500_fg_complete);
580 }
581 
592 {
593  u8 low, high;
594  int val;
595  int ret;
596  int timeout;
597 
598  if (!completion_done(&di->ab8500_fg_complete)) {
601  dev_dbg(di->dev, "Finalize time: %d ms\n",
602  ((INS_CURR_TIMEOUT - timeout) * 1000) / HZ);
603  if (!timeout) {
604  ret = -ETIME;
605  disable_irq(di->irq);
606  dev_err(di->dev, "completion timed out [%d]\n",
607  __LINE__);
608  goto fail;
609  }
610  }
611 
612  disable_irq(di->irq);
613 
616  READ_REQ, READ_REQ);
617 
618  /* 100uS between read request and read is needed */
619  usleep_range(100, 100);
620 
621  /* Read CC Sample conversion value Low and high */
624  if (ret < 0)
625  goto fail;
626 
629  if (ret < 0)
630  goto fail;
631 
632  /*
633  * negative value for Discharging
634  * convert 2's compliment into decimal
635  */
636  if (high & 0x10)
637  val = (low | (high << 8) | 0xFFFFE000);
638  else
639  val = (low | (high << 8));
640 
641  /*
642  * Convert to unit value in mA
643  * Full scale input voltage is
644  * 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA
645  * Given a 250ms conversion cycle time the LSB corresponds
646  * to 112.9 nAh. Convert to current by dividing by the conversion
647  * time in hours (250ms = 1 / (3600 * 4)h)
648  * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
649  */
650  val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) /
651  (1000 * di->bat->fg_res);
652 
653  if (di->turn_off_fg) {
654  dev_dbg(di->dev, "%s Disable FG\n", __func__);
655 
656  /* Clear any pending read requests */
659  if (ret)
660  goto fail;
661 
662  /* Stop the CC */
665  if (ret)
666  goto fail;
667  }
668  mutex_unlock(&di->cc_lock);
669  (*res) = val;
670 
671  return 0;
672 fail:
673  mutex_unlock(&di->cc_lock);
674  return ret;
675 }
676 
685 {
686  int ret;
687  int res = 0;
688 
689  ret = ab8500_fg_inst_curr_start(di);
690  if (ret) {
691  dev_err(di->dev, "Failed to initialize fg_inst\n");
692  return 0;
693  }
694 
695  ret = ab8500_fg_inst_curr_finalize(di, &res);
696  if (ret) {
697  dev_err(di->dev, "Failed to finalize fg_inst\n");
698  return 0;
699  }
700 
701  return res;
702 }
703 
711 static void ab8500_fg_acc_cur_work(struct work_struct *work)
712 {
713  int val;
714  int ret;
715  u8 low, med, high;
716 
717  struct ab8500_fg *di = container_of(work,
718  struct ab8500_fg, fg_acc_cur_work);
719 
720  mutex_lock(&di->cc_lock);
723  if (ret)
724  goto exit;
725 
728  if (ret < 0)
729  goto exit;
730 
733  if (ret < 0)
734  goto exit;
735 
738  if (ret < 0)
739  goto exit;
740 
741  /* Check for sign bit in case of negative value, 2's compliment */
742  if (high & 0x10)
743  val = (low | (med << 8) | (high << 16) | 0xFFE00000);
744  else
745  val = (low | (med << 8) | (high << 16));
746 
747  /*
748  * Convert to uAh
749  * Given a 250ms conversion cycle time the LSB corresponds
750  * to 112.9 nAh.
751  * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
752  */
753  di->accu_charge = (val * QLSB_NANO_AMP_HOURS_X10) /
754  (100 * di->bat->fg_res);
755 
756  /*
757  * Convert to unit value in mA
758  * Full scale input voltage is
759  * 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA
760  * Given a 250ms conversion cycle time the LSB corresponds
761  * to 112.9 nAh. Convert to current by dividing by the conversion
762  * time in hours (= samples / (3600 * 4)h)
763  * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
764  */
765  di->avg_curr = (val * QLSB_NANO_AMP_HOURS_X10 * 36) /
766  (1000 * di->bat->fg_res * (di->fg_samples / 4));
767 
768  di->flags.conv_done = true;
769 
770  mutex_unlock(&di->cc_lock);
771 
772  queue_work(di->fg_wq, &di->fg_work);
773 
774  return;
775 exit:
776  dev_err(di->dev,
777  "Failed to read or write gas gauge registers\n");
778  mutex_unlock(&di->cc_lock);
779  queue_work(di->fg_wq, &di->fg_work);
780 }
781 
788 static int ab8500_fg_bat_voltage(struct ab8500_fg *di)
789 {
790  int vbat;
791  static int prev;
792 
794  if (vbat < 0) {
795  dev_err(di->dev,
796  "%s gpadc conversion failed, using previous value\n",
797  __func__);
798  return prev;
799  }
800 
801  prev = vbat;
802  return vbat;
803 }
804 
812 static int ab8500_fg_volt_to_capacity(struct ab8500_fg *di, int voltage)
813 {
814  int i, tbl_size;
815  struct abx500_v_to_cap *tbl;
816  int cap = 0;
817 
818  tbl = di->bat->bat_type[di->bat->batt_id].v_to_cap_tbl,
819  tbl_size = di->bat->bat_type[di->bat->batt_id].n_v_cap_tbl_elements;
820 
821  for (i = 0; i < tbl_size; ++i) {
822  if (voltage > tbl[i].voltage)
823  break;
824  }
825 
826  if ((i > 0) && (i < tbl_size)) {
827  cap = interpolate(voltage,
828  tbl[i].voltage,
829  tbl[i].capacity * 10,
830  tbl[i-1].voltage,
831  tbl[i-1].capacity * 10);
832  } else if (i == 0) {
833  cap = 1000;
834  } else {
835  cap = 0;
836  }
837 
838  dev_dbg(di->dev, "%s Vbat: %d, Cap: %d per mille",
839  __func__, voltage, cap);
840 
841  return cap;
842 }
843 
851 static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di)
852 {
853  di->vbat = ab8500_fg_bat_voltage(di);
854  return ab8500_fg_volt_to_capacity(di, di->vbat);
855 }
856 
864 static int ab8500_fg_battery_resistance(struct ab8500_fg *di)
865 {
866  int i, tbl_size;
867  struct batres_vs_temp *tbl;
868  int resist = 0;
869 
870  tbl = di->bat->bat_type[di->bat->batt_id].batres_tbl;
871  tbl_size = di->bat->bat_type[di->bat->batt_id].n_batres_tbl_elements;
872 
873  for (i = 0; i < tbl_size; ++i) {
874  if (di->bat_temp / 10 > tbl[i].temp)
875  break;
876  }
877 
878  if ((i > 0) && (i < tbl_size)) {
879  resist = interpolate(di->bat_temp / 10,
880  tbl[i].temp,
881  tbl[i].resist,
882  tbl[i-1].temp,
883  tbl[i-1].resist);
884  } else if (i == 0) {
885  resist = tbl[0].resist;
886  } else {
887  resist = tbl[tbl_size - 1].resist;
888  }
889 
890  dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d"
891  " fg resistance %d, total: %d (mOhm)\n",
892  __func__, di->bat_temp, resist, di->bat->fg_res / 10,
893  (di->bat->fg_res / 10) + resist);
894 
895  /* fg_res variable is in 0.1mOhm */
896  resist += di->bat->fg_res / 10;
897 
898  return resist;
899 }
900 
908 static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di)
909 {
910  int vbat_comp, res;
911  int i = 0;
912  int vbat = 0;
913 
915 
916  do {
917  vbat += ab8500_fg_bat_voltage(di);
918  i++;
919  msleep(5);
920  } while (!ab8500_fg_inst_curr_done(di));
921 
923 
924  di->vbat = vbat / i;
925  res = ab8500_fg_battery_resistance(di);
926 
927  /* Use Ohms law to get the load compensated voltage */
928  vbat_comp = di->vbat - (di->inst_curr * res) / 1000;
929 
930  dev_dbg(di->dev, "%s Measured Vbat: %dmV,Compensated Vbat %dmV, "
931  "R: %dmOhm, Current: %dmA Vbat Samples: %d\n",
932  __func__, di->vbat, vbat_comp, res, di->inst_curr, i);
933 
934  return ab8500_fg_volt_to_capacity(di, vbat_comp);
935 }
936 
944 static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg *di, int cap_mah)
945 {
946  return (cap_mah * 1000) / di->bat_cap.max_mah_design;
947 }
948 
956 static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg *di, int cap_pm)
957 {
958  return cap_pm * di->bat_cap.max_mah_design / 1000;
959 }
960 
968 static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg *di, int cap_mah)
969 {
970  u64 div_res;
971  u32 div_rem;
972 
973  div_res = ((u64) cap_mah) * ((u64) di->vbat_nom);
974  div_rem = do_div(div_res, 1000);
975 
976  /* Make sure to round upwards if necessary */
977  if (div_rem >= 1000 / 2)
978  div_res++;
979 
980  return (int) div_res;
981 }
982 
990 static int ab8500_fg_calc_cap_charging(struct ab8500_fg *di)
991 {
992  dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
993  __func__,
994  di->bat_cap.mah,
995  di->accu_charge);
996 
997  /* Capacity should not be less than 0 */
998  if (di->bat_cap.mah + di->accu_charge > 0)
999  di->bat_cap.mah += di->accu_charge;
1000  else
1001  di->bat_cap.mah = 0;
1002  /*
1003  * We force capacity to 100% once when the algorithm
1004  * reports that it's full.
1005  */
1006  if (di->bat_cap.mah >= di->bat_cap.max_mah_design ||
1007  di->flags.force_full) {
1008  di->bat_cap.mah = di->bat_cap.max_mah_design;
1009  }
1010 
1011  ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1012  di->bat_cap.permille =
1013  ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1014 
1015  /* We need to update battery voltage and inst current when charging */
1016  di->vbat = ab8500_fg_bat_voltage(di);
1018 
1019  return di->bat_cap.mah;
1020 }
1021 
1031 static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg *di, bool comp)
1032 {
1033  int permille, mah;
1034 
1035  if (comp)
1036  permille = ab8500_fg_load_comp_volt_to_capacity(di);
1037  else
1038  permille = ab8500_fg_uncomp_volt_to_capacity(di);
1039 
1040  mah = ab8500_fg_convert_permille_to_mah(di, permille);
1041 
1042  di->bat_cap.mah = ab8500_fg_add_cap_sample(di, mah);
1043  di->bat_cap.permille =
1044  ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1045 
1046  return di->bat_cap.mah;
1047 }
1048 
1057 static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg *di)
1058 {
1059  int permille_volt, permille;
1060 
1061  dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
1062  __func__,
1063  di->bat_cap.mah,
1064  di->accu_charge);
1065 
1066  /* Capacity should not be less than 0 */
1067  if (di->bat_cap.mah + di->accu_charge > 0)
1068  di->bat_cap.mah += di->accu_charge;
1069  else
1070  di->bat_cap.mah = 0;
1071 
1072  if (di->bat_cap.mah >= di->bat_cap.max_mah_design)
1073  di->bat_cap.mah = di->bat_cap.max_mah_design;
1074 
1075  /*
1076  * Check against voltage based capacity. It can not be lower
1077  * than what the uncompensated voltage says
1078  */
1079  permille = ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1080  permille_volt = ab8500_fg_uncomp_volt_to_capacity(di);
1081 
1082  if (permille < permille_volt) {
1083  di->bat_cap.permille = permille_volt;
1084  di->bat_cap.mah = ab8500_fg_convert_permille_to_mah(di,
1085  di->bat_cap.permille);
1086 
1087  dev_dbg(di->dev, "%s voltage based: perm %d perm_volt %d\n",
1088  __func__,
1089  permille,
1090  permille_volt);
1091 
1092  ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1093  } else {
1094  ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1095  di->bat_cap.permille =
1096  ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1097  }
1098 
1099  return di->bat_cap.mah;
1100 }
1101 
1108 static int ab8500_fg_capacity_level(struct ab8500_fg *di)
1109 {
1110  int ret, percent;
1111 
1112  percent = di->bat_cap.permille / 10;
1113 
1114  if (percent <= di->bat->cap_levels->critical ||
1115  di->flags.low_bat)
1117  else if (percent <= di->bat->cap_levels->low)
1119  else if (percent <= di->bat->cap_levels->normal)
1121  else if (percent <= di->bat->cap_levels->high)
1123  else
1125 
1126  return ret;
1127 }
1128 
1137 static void ab8500_fg_check_capacity_limits(struct ab8500_fg *di, bool init)
1138 {
1139  bool changed = false;
1140 
1141  di->bat_cap.level = ab8500_fg_capacity_level(di);
1142 
1143  if (di->bat_cap.level != di->bat_cap.prev_level) {
1144  /*
1145  * We do not allow reported capacity level to go up
1146  * unless we're charging or if we're in init
1147  */
1148  if (!(!di->flags.charging && di->bat_cap.level >
1149  di->bat_cap.prev_level) || init) {
1150  dev_dbg(di->dev, "level changed from %d to %d\n",
1151  di->bat_cap.prev_level,
1152  di->bat_cap.level);
1153  di->bat_cap.prev_level = di->bat_cap.level;
1154  changed = true;
1155  } else {
1156  dev_dbg(di->dev, "level not allowed to go up "
1157  "since no charger is connected: %d to %d\n",
1158  di->bat_cap.prev_level,
1159  di->bat_cap.level);
1160  }
1161  }
1162 
1163  /*
1164  * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate
1165  * shutdown
1166  */
1167  if (di->flags.low_bat) {
1168  dev_dbg(di->dev, "Battery low, set capacity to 0\n");
1169  di->bat_cap.prev_percent = 0;
1170  di->bat_cap.permille = 0;
1171  di->bat_cap.prev_mah = 0;
1172  di->bat_cap.mah = 0;
1173  changed = true;
1174  } else if (di->flags.fully_charged) {
1175  /*
1176  * We report 100% if algorithm reported fully charged
1177  * unless capacity drops too much
1178  */
1179  if (di->flags.force_full) {
1180  di->bat_cap.prev_percent = di->bat_cap.permille / 10;
1181  di->bat_cap.prev_mah = di->bat_cap.mah;
1182  } else if (!di->flags.force_full &&
1183  di->bat_cap.prev_percent !=
1184  (di->bat_cap.permille) / 10 &&
1185  (di->bat_cap.permille / 10) <
1186  di->bat->fg_params->maint_thres) {
1187  dev_dbg(di->dev,
1188  "battery reported full "
1189  "but capacity dropping: %d\n",
1190  di->bat_cap.permille / 10);
1191  di->bat_cap.prev_percent = di->bat_cap.permille / 10;
1192  di->bat_cap.prev_mah = di->bat_cap.mah;
1193 
1194  changed = true;
1195  }
1196  } else if (di->bat_cap.prev_percent != di->bat_cap.permille / 10) {
1197  if (di->bat_cap.permille / 10 == 0) {
1198  /*
1199  * We will not report 0% unless we've got
1200  * the LOW_BAT IRQ, no matter what the FG
1201  * algorithm says.
1202  */
1203  di->bat_cap.prev_percent = 1;
1204  di->bat_cap.permille = 1;
1205  di->bat_cap.prev_mah = 1;
1206  di->bat_cap.mah = 1;
1207 
1208  changed = true;
1209  } else if (!(!di->flags.charging &&
1210  (di->bat_cap.permille / 10) >
1211  di->bat_cap.prev_percent) || init) {
1212  /*
1213  * We do not allow reported capacity to go up
1214  * unless we're charging or if we're in init
1215  */
1216  dev_dbg(di->dev,
1217  "capacity changed from %d to %d (%d)\n",
1218  di->bat_cap.prev_percent,
1219  di->bat_cap.permille / 10,
1220  di->bat_cap.permille);
1221  di->bat_cap.prev_percent = di->bat_cap.permille / 10;
1222  di->bat_cap.prev_mah = di->bat_cap.mah;
1223 
1224  changed = true;
1225  } else {
1226  dev_dbg(di->dev, "capacity not allowed to go up since "
1227  "no charger is connected: %d to %d (%d)\n",
1228  di->bat_cap.prev_percent,
1229  di->bat_cap.permille / 10,
1230  di->bat_cap.permille);
1231  }
1232  }
1233 
1234  if (changed) {
1236  if (di->flags.fully_charged && di->flags.force_full) {
1237  dev_dbg(di->dev, "Battery full, notifying.\n");
1238  di->flags.force_full = false;
1239  sysfs_notify(&di->fg_kobject, NULL, "charge_full");
1240  }
1241  sysfs_notify(&di->fg_kobject, NULL, "charge_now");
1242  }
1243 }
1244 
1245 static void ab8500_fg_charge_state_to(struct ab8500_fg *di,
1247 {
1248  dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n",
1249  di->charge_state,
1250  charge_state[di->charge_state],
1251  new_state,
1252  charge_state[new_state]);
1253 
1254  di->charge_state = new_state;
1255 }
1256 
1257 static void ab8500_fg_discharge_state_to(struct ab8500_fg *di,
1259 {
1260  dev_dbg(di->dev, "Disharge state from %d [%s] to %d [%s]\n",
1261  di->discharge_state,
1262  discharge_state[di->discharge_state],
1263  new_state,
1264  discharge_state[new_state]);
1265 
1266  di->discharge_state = new_state;
1267 }
1268 
1275 static void ab8500_fg_algorithm_charging(struct ab8500_fg *di)
1276 {
1277  /*
1278  * If we change to discharge mode
1279  * we should start with recovery
1280  */
1282  ab8500_fg_discharge_state_to(di,
1284 
1285  switch (di->charge_state) {
1286  case AB8500_FG_CHARGE_INIT:
1287  di->fg_samples = SEC_TO_SAMPLE(
1288  di->bat->fg_params->accu_charging);
1289 
1290  ab8500_fg_coulomb_counter(di, true);
1291  ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_READOUT);
1292 
1293  break;
1294 
1296  /*
1297  * Read the FG and calculate the new capacity
1298  */
1299  mutex_lock(&di->cc_lock);
1300  if (!di->flags.conv_done) {
1301  /* Wasn't the CC IRQ that got us here */
1302  mutex_unlock(&di->cc_lock);
1303  dev_dbg(di->dev, "%s CC conv not done\n",
1304  __func__);
1305 
1306  break;
1307  }
1308  di->flags.conv_done = false;
1309  mutex_unlock(&di->cc_lock);
1310 
1311  ab8500_fg_calc_cap_charging(di);
1312 
1313  break;
1314 
1315  default:
1316  break;
1317  }
1318 
1319  /* Check capacity limits */
1320  ab8500_fg_check_capacity_limits(di, false);
1321 }
1322 
1323 static void force_capacity(struct ab8500_fg *di)
1324 {
1325  int cap;
1326 
1327  ab8500_fg_clear_cap_samples(di);
1328  cap = di->bat_cap.user_mah;
1329  if (cap > di->bat_cap.max_mah_design) {
1330  dev_dbg(di->dev, "Remaining cap %d can't be bigger than total"
1331  " %d\n", cap, di->bat_cap.max_mah_design);
1332  cap = di->bat_cap.max_mah_design;
1333  }
1334  ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah);
1335  di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap);
1336  di->bat_cap.mah = cap;
1337  ab8500_fg_check_capacity_limits(di, true);
1338 }
1339 
1340 static bool check_sysfs_capacity(struct ab8500_fg *di)
1341 {
1342  int cap, lower, upper;
1343  int cap_permille;
1344 
1345  cap = di->bat_cap.user_mah;
1346 
1347  cap_permille = ab8500_fg_convert_mah_to_permille(di,
1348  di->bat_cap.user_mah);
1349 
1350  lower = di->bat_cap.permille - di->bat->fg_params->user_cap_limit * 10;
1351  upper = di->bat_cap.permille + di->bat->fg_params->user_cap_limit * 10;
1352 
1353  if (lower < 0)
1354  lower = 0;
1355  /* 1000 is permille, -> 100 percent */
1356  if (upper > 1000)
1357  upper = 1000;
1358 
1359  dev_dbg(di->dev, "Capacity limits:"
1360  " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n",
1361  lower, cap_permille, upper, cap, di->bat_cap.mah);
1362 
1363  /* If within limits, use the saved capacity and exit estimation...*/
1364  if (cap_permille > lower && cap_permille < upper) {
1365  dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap);
1366  force_capacity(di);
1367  return true;
1368  }
1369  dev_dbg(di->dev, "Capacity from user out of limits, ignoring");
1370  return false;
1371 }
1372 
1379 static void ab8500_fg_algorithm_discharging(struct ab8500_fg *di)
1380 {
1381  int sleep_time;
1382 
1383  /* If we change to charge mode we should start with init */
1385  ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
1386 
1387  switch (di->discharge_state) {
1389  /* We use the FG IRQ to work on */
1390  di->init_cnt = 0;
1391  di->fg_samples = SEC_TO_SAMPLE(di->bat->fg_params->init_timer);
1392  ab8500_fg_coulomb_counter(di, true);
1393  ab8500_fg_discharge_state_to(di,
1395 
1396  /* Intentional fallthrough */
1398  /*
1399  * Discard a number of samples during startup.
1400  * After that, use compensated voltage for a few
1401  * samples to get an initial capacity.
1402  * Then go to READOUT
1403  */
1404  sleep_time = di->bat->fg_params->init_timer;
1405 
1406  /* Discard the first [x] seconds */
1407  if (di->init_cnt >
1408  di->bat->fg_params->init_discard_time) {
1409  ab8500_fg_calc_cap_discharge_voltage(di, true);
1410 
1411  ab8500_fg_check_capacity_limits(di, true);
1412  }
1413 
1414  di->init_cnt += sleep_time;
1415  if (di->init_cnt > di->bat->fg_params->init_total_time)
1416  ab8500_fg_discharge_state_to(di,
1418 
1419  break;
1420 
1422  di->recovery_cnt = 0;
1423  di->recovery_needed = true;
1424  ab8500_fg_discharge_state_to(di,
1426 
1427  /* Intentional fallthrough */
1428 
1430  sleep_time = di->bat->fg_params->recovery_sleep_timer;
1431 
1432  /*
1433  * We should check the power consumption
1434  * If low, go to READOUT (after x min) or
1435  * RECOVERY_SLEEP if time left.
1436  * If high, go to READOUT
1437  */
1439 
1440  if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
1441  if (di->recovery_cnt >
1442  di->bat->fg_params->recovery_total_time) {
1443  di->fg_samples = SEC_TO_SAMPLE(
1444  di->bat->fg_params->accu_high_curr);
1445  ab8500_fg_coulomb_counter(di, true);
1446  ab8500_fg_discharge_state_to(di,
1448  di->recovery_needed = false;
1449  } else {
1451  &di->fg_periodic_work,
1452  sleep_time * HZ);
1453  }
1454  di->recovery_cnt += sleep_time;
1455  } else {
1456  di->fg_samples = SEC_TO_SAMPLE(
1457  di->bat->fg_params->accu_high_curr);
1458  ab8500_fg_coulomb_counter(di, true);
1459  ab8500_fg_discharge_state_to(di,
1461  }
1462  break;
1463 
1465  di->fg_samples = SEC_TO_SAMPLE(
1466  di->bat->fg_params->accu_high_curr);
1467  ab8500_fg_coulomb_counter(di, true);
1468  ab8500_fg_discharge_state_to(di,
1470  break;
1471 
1474 
1475  if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
1476  /* Detect mode change */
1477  if (di->high_curr_mode) {
1478  di->high_curr_mode = false;
1479  di->high_curr_cnt = 0;
1480  }
1481 
1482  if (di->recovery_needed) {
1483  ab8500_fg_discharge_state_to(di,
1485 
1487  &di->fg_periodic_work, 0);
1488 
1489  break;
1490  }
1491 
1492  ab8500_fg_calc_cap_discharge_voltage(di, true);
1493  } else {
1494  mutex_lock(&di->cc_lock);
1495  if (!di->flags.conv_done) {
1496  /* Wasn't the CC IRQ that got us here */
1497  mutex_unlock(&di->cc_lock);
1498  dev_dbg(di->dev, "%s CC conv not done\n",
1499  __func__);
1500 
1501  break;
1502  }
1503  di->flags.conv_done = false;
1504  mutex_unlock(&di->cc_lock);
1505 
1506  /* Detect mode change */
1507  if (!di->high_curr_mode) {
1508  di->high_curr_mode = true;
1509  di->high_curr_cnt = 0;
1510  }
1511 
1512  di->high_curr_cnt +=
1513  di->bat->fg_params->accu_high_curr;
1514  if (di->high_curr_cnt >
1515  di->bat->fg_params->high_curr_time)
1516  di->recovery_needed = true;
1517 
1518  ab8500_fg_calc_cap_discharge_fg(di);
1519  }
1520 
1521  ab8500_fg_check_capacity_limits(di, false);
1522 
1523  break;
1524 
1526  ab8500_fg_coulomb_counter(di, true);
1528 
1529  ab8500_fg_calc_cap_discharge_voltage(di, true);
1530 
1531  di->fg_samples = SEC_TO_SAMPLE(
1532  di->bat->fg_params->accu_high_curr);
1533  ab8500_fg_coulomb_counter(di, true);
1534  ab8500_fg_discharge_state_to(di,
1536 
1537  ab8500_fg_check_capacity_limits(di, false);
1538 
1539  break;
1540 
1541  default:
1542  break;
1543  }
1544 }
1545 
1551 static void ab8500_fg_algorithm_calibrate(struct ab8500_fg *di)
1552 {
1553  int ret;
1554 
1555  switch (di->calib_state) {
1556  case AB8500_FG_CALIB_INIT:
1557  dev_dbg(di->dev, "Calibration ongoing...\n");
1558 
1562  if (ret < 0)
1563  goto err;
1564 
1568  if (ret < 0)
1569  goto err;
1571  break;
1572  case AB8500_FG_CALIB_END:
1576  if (ret < 0)
1577  goto err;
1578  di->flags.calibrate = false;
1579  dev_dbg(di->dev, "Calibration done...\n");
1581  break;
1582  case AB8500_FG_CALIB_WAIT:
1583  dev_dbg(di->dev, "Calibration WFI\n");
1584  default:
1585  break;
1586  }
1587  return;
1588 err:
1589  /* Something went wrong, don't calibrate then */
1590  dev_err(di->dev, "failed to calibrate the CC\n");
1591  di->flags.calibrate = false;
1594 }
1595 
1602 static void ab8500_fg_algorithm(struct ab8500_fg *di)
1603 {
1604  if (di->flags.calibrate)
1605  ab8500_fg_algorithm_calibrate(di);
1606  else {
1607  if (di->flags.charging)
1608  ab8500_fg_algorithm_charging(di);
1609  else
1610  ab8500_fg_algorithm_discharging(di);
1611  }
1612 
1613  dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d "
1614  "%d %d %d %d %d %d %d\n",
1615  di->bat_cap.max_mah_design,
1616  di->bat_cap.mah,
1617  di->bat_cap.permille,
1618  di->bat_cap.level,
1619  di->bat_cap.prev_mah,
1620  di->bat_cap.prev_percent,
1621  di->bat_cap.prev_level,
1622  di->vbat,
1623  di->inst_curr,
1624  di->avg_curr,
1625  di->accu_charge,
1626  di->flags.charging,
1627  di->charge_state,
1628  di->discharge_state,
1629  di->high_curr_mode,
1630  di->recovery_needed);
1631 }
1632 
1639 static void ab8500_fg_periodic_work(struct work_struct *work)
1640 {
1641  struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1642  fg_periodic_work.work);
1643 
1644  if (di->init_capacity) {
1645  /* A dummy read that will return 0 */
1647  /* Get an initial capacity calculation */
1648  ab8500_fg_calc_cap_discharge_voltage(di, true);
1649  ab8500_fg_check_capacity_limits(di, true);
1650  di->init_capacity = false;
1651 
1653  } else if (di->flags.user_cap) {
1654  if (check_sysfs_capacity(di)) {
1655  ab8500_fg_check_capacity_limits(di, true);
1656  if (di->flags.charging)
1657  ab8500_fg_charge_state_to(di,
1659  else
1660  ab8500_fg_discharge_state_to(di,
1662  }
1663  di->flags.user_cap = false;
1665  } else
1666  ab8500_fg_algorithm(di);
1667 
1668 }
1669 
1676 static void ab8500_fg_check_hw_failure_work(struct work_struct *work)
1677 {
1678  int ret;
1679  u8 reg_value;
1680 
1681  struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1683 
1684  /*
1685  * If we have had a battery over-voltage situation,
1686  * check ovv-bit to see if it should be reset.
1687  */
1688  if (di->flags.bat_ovv) {
1691  &reg_value);
1692  if (ret < 0) {
1693  dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1694  return;
1695  }
1696  if ((reg_value & BATT_OVV) != BATT_OVV) {
1697  dev_dbg(di->dev, "Battery recovered from OVV\n");
1698  di->flags.bat_ovv = false;
1700  return;
1701  }
1702 
1703  /* Not yet recovered from ovv, reschedule this test */
1705  round_jiffies(HZ));
1706  }
1707 }
1708 
1715 static void ab8500_fg_low_bat_work(struct work_struct *work)
1716 {
1717  int vbat;
1718 
1719  struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1720  fg_low_bat_work.work);
1721 
1722  vbat = ab8500_fg_bat_voltage(di);
1723 
1724  /* Check if LOW_BAT still fulfilled */
1725  if (vbat < di->bat->fg_params->lowbat_threshold) {
1726  di->flags.low_bat = true;
1727  dev_warn(di->dev, "Battery voltage still LOW\n");
1728 
1729  /*
1730  * We need to re-schedule this check to be able to detect
1731  * if the voltage increases again during charging
1732  */
1735  } else {
1736  di->flags.low_bat = false;
1737  dev_warn(di->dev, "Battery voltage OK again\n");
1738  }
1739 
1740  /* This is needed to dispatch LOW_BAT */
1741  ab8500_fg_check_capacity_limits(di, false);
1742 
1743  /* Set this flag to check if LOW_BAT IRQ still occurs */
1744  di->flags.low_bat_delay = false;
1745 }
1746 
1757 static int ab8500_fg_battok_calc(struct ab8500_fg *di, int target)
1758 {
1759  if (target > BATT_OK_MIN +
1762  if (target < BATT_OK_MIN)
1763  return 0;
1764  return (target - BATT_OK_MIN) / BATT_OK_INCREMENT;
1765 }
1766 
1773 static int ab8500_fg_battok_init_hw_register(struct ab8500_fg *di)
1774 {
1775  int selected;
1776  int sel0;
1777  int sel1;
1778  int cbp_sel0;
1779  int cbp_sel1;
1780  int ret;
1781  int new_val;
1782 
1783  sel0 = di->bat->fg_params->battok_falling_th_sel0;
1784  sel1 = di->bat->fg_params->battok_raising_th_sel1;
1785 
1786  cbp_sel0 = ab8500_fg_battok_calc(di, sel0);
1787  cbp_sel1 = ab8500_fg_battok_calc(di, sel1);
1788 
1789  selected = BATT_OK_MIN + cbp_sel0 * BATT_OK_INCREMENT;
1790 
1791  if (selected != sel0)
1792  dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1793  sel0, selected, cbp_sel0);
1794 
1795  selected = BATT_OK_MIN + cbp_sel1 * BATT_OK_INCREMENT;
1796 
1797  if (selected != sel1)
1798  dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1799  sel1, selected, cbp_sel1);
1800 
1801  new_val = cbp_sel0 | (cbp_sel1 << 4);
1802 
1803  dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1);
1805  AB8500_BATT_OK_REG, new_val);
1806  return ret;
1807 }
1808 
1815 static void ab8500_fg_instant_work(struct work_struct *work)
1816 {
1817  struct ab8500_fg *di = container_of(work, struct ab8500_fg, fg_work);
1818 
1819  ab8500_fg_algorithm(di);
1820 }
1821 
1829 static irqreturn_t ab8500_fg_cc_data_end_handler(int irq, void *_di)
1830 {
1831  struct ab8500_fg *di = _di;
1833  return IRQ_HANDLED;
1834 }
1835 
1843 static irqreturn_t ab8500_fg_cc_int_calib_handler(int irq, void *_di)
1844 {
1845  struct ab8500_fg *di = _di;
1848  return IRQ_HANDLED;
1849 }
1850 
1858 static irqreturn_t ab8500_fg_cc_convend_handler(int irq, void *_di)
1859 {
1860  struct ab8500_fg *di = _di;
1861 
1862  queue_work(di->fg_wq, &di->fg_acc_cur_work);
1863 
1864  return IRQ_HANDLED;
1865 }
1866 
1874 static irqreturn_t ab8500_fg_batt_ovv_handler(int irq, void *_di)
1875 {
1876  struct ab8500_fg *di = _di;
1877 
1878  dev_dbg(di->dev, "Battery OVV\n");
1879  di->flags.bat_ovv = true;
1881 
1882  /* Schedule a new HW failure check */
1884 
1885  return IRQ_HANDLED;
1886 }
1887 
1895 static irqreturn_t ab8500_fg_lowbatf_handler(int irq, void *_di)
1896 {
1897  struct ab8500_fg *di = _di;
1898 
1899  if (!di->flags.low_bat_delay) {
1900  dev_warn(di->dev, "Battery voltage is below LOW threshold\n");
1901  di->flags.low_bat_delay = true;
1902  /*
1903  * Start a timer to check LOW_BAT again after some time
1904  * This is done to avoid shutdown on single voltage dips
1905  */
1908  }
1909  return IRQ_HANDLED;
1910 }
1911 
1930 static int ab8500_fg_get_property(struct power_supply *psy,
1931  enum power_supply_property psp,
1932  union power_supply_propval *val)
1933 {
1934  struct ab8500_fg *di;
1935 
1936  di = to_ab8500_fg_device_info(psy);
1937 
1938  /*
1939  * If battery is identified as unknown and charging of unknown
1940  * batteries is disabled, we always report 100% capacity and
1941  * capacity level UNKNOWN, since we can't calculate
1942  * remaining capacity
1943  */
1944 
1945  switch (psp) {
1947  if (di->flags.bat_ovv)
1948  val->intval = BATT_OVV_VALUE * 1000;
1949  else
1950  val->intval = di->vbat * 1000;
1951  break;
1953  val->intval = di->inst_curr * 1000;
1954  break;
1956  val->intval = di->avg_curr * 1000;
1957  break;
1959  val->intval = ab8500_fg_convert_mah_to_uwh(di,
1960  di->bat_cap.max_mah_design);
1961  break;
1963  val->intval = ab8500_fg_convert_mah_to_uwh(di,
1964  di->bat_cap.max_mah);
1965  break;
1967  if (di->flags.batt_unknown && !di->bat->chg_unknown_bat &&
1968  di->flags.batt_id_received)
1969  val->intval = ab8500_fg_convert_mah_to_uwh(di,
1970  di->bat_cap.max_mah);
1971  else
1972  val->intval = ab8500_fg_convert_mah_to_uwh(di,
1973  di->bat_cap.prev_mah);
1974  break;
1976  val->intval = di->bat_cap.max_mah_design;
1977  break;
1979  val->intval = di->bat_cap.max_mah;
1980  break;
1982  if (di->flags.batt_unknown && !di->bat->chg_unknown_bat &&
1983  di->flags.batt_id_received)
1984  val->intval = di->bat_cap.max_mah;
1985  else
1986  val->intval = di->bat_cap.prev_mah;
1987  break;
1989  if (di->flags.batt_unknown && !di->bat->chg_unknown_bat &&
1990  di->flags.batt_id_received)
1991  val->intval = 100;
1992  else
1993  val->intval = di->bat_cap.prev_percent;
1994  break;
1996  if (di->flags.batt_unknown && !di->bat->chg_unknown_bat &&
1997  di->flags.batt_id_received)
1999  else
2000  val->intval = di->bat_cap.prev_level;
2001  break;
2002  default:
2003  return -EINVAL;
2004  }
2005  return 0;
2006 }
2007 
2008 static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data)
2009 {
2010  struct power_supply *psy;
2011  struct power_supply *ext;
2012  struct ab8500_fg *di;
2013  union power_supply_propval ret;
2014  int i, j;
2015  bool psy_found = false;
2016 
2017  psy = (struct power_supply *)data;
2018  ext = dev_get_drvdata(dev);
2019  di = to_ab8500_fg_device_info(psy);
2020 
2021  /*
2022  * For all psy where the name of your driver
2023  * appears in any supplied_to
2024  */
2025  for (i = 0; i < ext->num_supplicants; i++) {
2026  if (!strcmp(ext->supplied_to[i], psy->name))
2027  psy_found = true;
2028  }
2029 
2030  if (!psy_found)
2031  return 0;
2032 
2033  /* Go through all properties for the psy */
2034  for (j = 0; j < ext->num_properties; j++) {
2035  enum power_supply_property prop;
2036  prop = ext->properties[j];
2037 
2038  if (ext->get_property(ext, prop, &ret))
2039  continue;
2040 
2041  switch (prop) {
2043  switch (ext->type) {
2045  switch (ret.intval) {
2049  if (!di->flags.charging)
2050  break;
2051  di->flags.charging = false;
2052  di->flags.fully_charged = false;
2053  queue_work(di->fg_wq, &di->fg_work);
2054  break;
2056  if (di->flags.fully_charged)
2057  break;
2058  di->flags.fully_charged = true;
2059  di->flags.force_full = true;
2060  /* Save current capacity as maximum */
2061  di->bat_cap.max_mah = di->bat_cap.mah;
2062  queue_work(di->fg_wq, &di->fg_work);
2063  break;
2065  if (di->flags.charging)
2066  break;
2067  di->flags.charging = true;
2068  di->flags.fully_charged = false;
2069  queue_work(di->fg_wq, &di->fg_work);
2070  break;
2071  };
2072  default:
2073  break;
2074  };
2075  break;
2077  switch (ext->type) {
2079  if (!di->flags.batt_id_received) {
2080  const struct abx500_battery_type *b;
2081 
2082  b = &(di->bat->bat_type[di->bat->batt_id]);
2083 
2084  di->flags.batt_id_received = true;
2085 
2086  di->bat_cap.max_mah_design =
2087  MILLI_TO_MICRO *
2088  b->charge_full_design;
2089 
2090  di->bat_cap.max_mah =
2091  di->bat_cap.max_mah_design;
2092 
2093  di->vbat_nom = b->nominal_voltage;
2094  }
2095 
2096  if (ret.intval)
2097  di->flags.batt_unknown = false;
2098  else
2099  di->flags.batt_unknown = true;
2100  break;
2101  default:
2102  break;
2103  }
2104  break;
2106  switch (ext->type) {
2108  if (di->flags.batt_id_received)
2109  di->bat_temp = ret.intval;
2110  break;
2111  default:
2112  break;
2113  }
2114  break;
2115  default:
2116  break;
2117  }
2118  }
2119  return 0;
2120 }
2121 
2128 static int ab8500_fg_init_hw_registers(struct ab8500_fg *di)
2129 {
2130  int ret;
2131 
2132  /* Set VBAT OVV threshold */
2138  if (ret) {
2139  dev_err(di->dev, "failed to set BATT_OVV\n");
2140  goto out;
2141  }
2142 
2143  /* Enable VBAT OVV detection */
2147  BATT_OVV_ENA,
2148  BATT_OVV_ENA);
2149  if (ret) {
2150  dev_err(di->dev, "failed to enable BATT_OVV\n");
2151  goto out;
2152  }
2153 
2154  /* Low Battery Voltage */
2158  ab8500_volt_to_regval(
2159  di->bat->fg_params->lowbat_threshold) << 1 |
2160  LOW_BAT_ENABLE);
2161  if (ret) {
2162  dev_err(di->dev, "%s write failed\n", __func__);
2163  goto out;
2164  }
2165 
2166  /* Battery OK threshold */
2167  ret = ab8500_fg_battok_init_hw_register(di);
2168  if (ret) {
2169  dev_err(di->dev, "BattOk init write failed.\n");
2170  goto out;
2171  }
2172 out:
2173  return ret;
2174 }
2175 
2185 static void ab8500_fg_external_power_changed(struct power_supply *psy)
2186 {
2187  struct ab8500_fg *di = to_ab8500_fg_device_info(psy);
2188 
2190  &di->fg_psy, ab8500_fg_get_ext_psy_data);
2191 }
2192 
2201 static void ab8500_fg_reinit_work(struct work_struct *work)
2202 {
2203  struct ab8500_fg *di = container_of(work, struct ab8500_fg,
2204  fg_reinit_work.work);
2205 
2206  if (di->flags.calibrate == false) {
2207  dev_dbg(di->dev, "Resetting FG state machine to init.\n");
2208  ab8500_fg_clear_cap_samples(di);
2209  ab8500_fg_calc_cap_discharge_voltage(di, true);
2210  ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
2211  ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
2213 
2214  } else {
2215  dev_err(di->dev, "Residual offset calibration ongoing "
2216  "retrying..\n");
2217  /* Wait one second until next try*/
2219  round_jiffies(1));
2220  }
2221 }
2222 
2230 {
2231  struct ab8500_fg *di = ab8500_fg_get();
2232  /* User won't be notified if a null pointer returned. */
2233  if (di != NULL)
2234  queue_delayed_work(di->fg_wq, &di->fg_reinit_work, 0);
2235 }
2236 
2237 /* Exposure to the sysfs interface */
2238 
2240  struct attribute attr;
2241  ssize_t (*show)(struct ab8500_fg *, char *);
2242  ssize_t (*store)(struct ab8500_fg *, const char *, size_t);
2243 };
2244 
2245 static ssize_t charge_full_show(struct ab8500_fg *di, char *buf)
2246 {
2247  return sprintf(buf, "%d\n", di->bat_cap.max_mah);
2248 }
2249 
2250 static ssize_t charge_full_store(struct ab8500_fg *di, const char *buf,
2251  size_t count)
2252 {
2253  unsigned long charge_full;
2254  ssize_t ret = -EINVAL;
2255 
2256  ret = strict_strtoul(buf, 10, &charge_full);
2257 
2258  dev_dbg(di->dev, "Ret %zd charge_full %lu", ret, charge_full);
2259 
2260  if (!ret) {
2261  di->bat_cap.max_mah = (int) charge_full;
2262  ret = count;
2263  }
2264  return ret;
2265 }
2266 
2267 static ssize_t charge_now_show(struct ab8500_fg *di, char *buf)
2268 {
2269  return sprintf(buf, "%d\n", di->bat_cap.prev_mah);
2270 }
2271 
2272 static ssize_t charge_now_store(struct ab8500_fg *di, const char *buf,
2273  size_t count)
2274 {
2275  unsigned long charge_now;
2276  ssize_t ret;
2277 
2278  ret = strict_strtoul(buf, 10, &charge_now);
2279 
2280  dev_dbg(di->dev, "Ret %zd charge_now %lu was %d",
2281  ret, charge_now, di->bat_cap.prev_mah);
2282 
2283  if (!ret) {
2284  di->bat_cap.user_mah = (int) charge_now;
2285  di->flags.user_cap = true;
2286  ret = count;
2288  }
2289  return ret;
2290 }
2291 
2292 static struct ab8500_fg_sysfs_entry charge_full_attr =
2293  __ATTR(charge_full, 0644, charge_full_show, charge_full_store);
2294 
2295 static struct ab8500_fg_sysfs_entry charge_now_attr =
2296  __ATTR(charge_now, 0644, charge_now_show, charge_now_store);
2297 
2298 static ssize_t
2299 ab8500_fg_show(struct kobject *kobj, struct attribute *attr, char *buf)
2300 {
2301  struct ab8500_fg_sysfs_entry *entry;
2302  struct ab8500_fg *di;
2303 
2304  entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2305  di = container_of(kobj, struct ab8500_fg, fg_kobject);
2306 
2307  if (!entry->show)
2308  return -EIO;
2309 
2310  return entry->show(di, buf);
2311 }
2312 static ssize_t
2313 ab8500_fg_store(struct kobject *kobj, struct attribute *attr, const char *buf,
2314  size_t count)
2315 {
2316  struct ab8500_fg_sysfs_entry *entry;
2317  struct ab8500_fg *di;
2318 
2319  entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2320  di = container_of(kobj, struct ab8500_fg, fg_kobject);
2321 
2322  if (!entry->store)
2323  return -EIO;
2324 
2325  return entry->store(di, buf, count);
2326 }
2327 
2328 static const struct sysfs_ops ab8500_fg_sysfs_ops = {
2329  .show = ab8500_fg_show,
2330  .store = ab8500_fg_store,
2331 };
2332 
2333 static struct attribute *ab8500_fg_attrs[] = {
2334  &charge_full_attr.attr,
2335  &charge_now_attr.attr,
2336  NULL,
2337 };
2338 
2339 static struct kobj_type ab8500_fg_ktype = {
2340  .sysfs_ops = &ab8500_fg_sysfs_ops,
2341  .default_attrs = ab8500_fg_attrs,
2342 };
2343 
2350 static void ab8500_fg_sysfs_exit(struct ab8500_fg *di)
2351 {
2352  kobject_del(&di->fg_kobject);
2353 }
2354 
2362 static int ab8500_fg_sysfs_init(struct ab8500_fg *di)
2363 {
2364  int ret = 0;
2365 
2366  ret = kobject_init_and_add(&di->fg_kobject,
2367  &ab8500_fg_ktype,
2368  NULL, "battery");
2369  if (ret < 0)
2370  dev_err(di->dev, "failed to create sysfs entry\n");
2371 
2372  return ret;
2373 }
2374 /* Exposure to the sysfs interface <<END>> */
2375 
2376 #if defined(CONFIG_PM)
2377 static int ab8500_fg_resume(struct platform_device *pdev)
2378 {
2379  struct ab8500_fg *di = platform_get_drvdata(pdev);
2380 
2381  /*
2382  * Change state if we're not charging. If we're charging we will wake
2383  * up on the FG IRQ
2384  */
2385  if (!di->flags.charging) {
2386  ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_WAKEUP);
2387  queue_work(di->fg_wq, &di->fg_work);
2388  }
2389 
2390  return 0;
2391 }
2392 
2393 static int ab8500_fg_suspend(struct platform_device *pdev,
2395 {
2396  struct ab8500_fg *di = platform_get_drvdata(pdev);
2397 
2399 
2400  /*
2401  * If the FG is enabled we will disable it before going to suspend
2402  * only if we're not charging
2403  */
2404  if (di->flags.fg_enabled && !di->flags.charging)
2405  ab8500_fg_coulomb_counter(di, false);
2406 
2407  return 0;
2408 }
2409 #else
2410 #define ab8500_fg_suspend NULL
2411 #define ab8500_fg_resume NULL
2412 #endif
2413 
2414 static int __devexit ab8500_fg_remove(struct platform_device *pdev)
2415 {
2416  int ret = 0;
2417  struct ab8500_fg *di = platform_get_drvdata(pdev);
2418 
2419  list_del(&di->node);
2420 
2421  /* Disable coulomb counter */
2422  ret = ab8500_fg_coulomb_counter(di, false);
2423  if (ret)
2424  dev_err(di->dev, "failed to disable coulomb counter\n");
2425 
2426  destroy_workqueue(di->fg_wq);
2427  ab8500_fg_sysfs_exit(di);
2428 
2431  platform_set_drvdata(pdev, NULL);
2432  kfree(di);
2433  return ret;
2434 }
2435 
2436 /* ab8500 fg driver interrupts and their respective isr */
2437 static struct ab8500_fg_interrupts ab8500_fg_irq[] = {
2438  {"NCONV_ACCU", ab8500_fg_cc_convend_handler},
2439  {"BATT_OVV", ab8500_fg_batt_ovv_handler},
2440  {"LOW_BAT_F", ab8500_fg_lowbatf_handler},
2441  {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler},
2442  {"CCEOC", ab8500_fg_cc_data_end_handler},
2443 };
2444 
2445 static int __devinit ab8500_fg_probe(struct platform_device *pdev)
2446 {
2447  int i, irq;
2448  int ret = 0;
2449  struct abx500_bm_plat_data *plat_data = pdev->dev.platform_data;
2450  struct ab8500_fg *di;
2451 
2452  if (!plat_data) {
2453  dev_err(&pdev->dev, "No platform data\n");
2454  return -EINVAL;
2455  }
2456 
2457  di = kzalloc(sizeof(*di), GFP_KERNEL);
2458  if (!di)
2459  return -ENOMEM;
2460 
2461  mutex_init(&di->cc_lock);
2462 
2463  /* get parent data */
2464  di->dev = &pdev->dev;
2465  di->parent = dev_get_drvdata(pdev->dev.parent);
2466  di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2467 
2468  /* get fg specific platform data */
2469  di->pdata = plat_data->fg;
2470  if (!di->pdata) {
2471  dev_err(di->dev, "no fg platform data supplied\n");
2472  ret = -EINVAL;
2473  goto free_device_info;
2474  }
2475 
2476  /* get battery specific platform data */
2477  di->bat = plat_data->battery;
2478  if (!di->bat) {
2479  dev_err(di->dev, "no battery platform data supplied\n");
2480  ret = -EINVAL;
2481  goto free_device_info;
2482  }
2483 
2484  di->fg_psy.name = "ab8500_fg";
2485  di->fg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
2486  di->fg_psy.properties = ab8500_fg_props;
2487  di->fg_psy.num_properties = ARRAY_SIZE(ab8500_fg_props);
2488  di->fg_psy.get_property = ab8500_fg_get_property;
2489  di->fg_psy.supplied_to = di->pdata->supplied_to;
2490  di->fg_psy.num_supplicants = di->pdata->num_supplicants;
2491  di->fg_psy.external_power_changed = ab8500_fg_external_power_changed;
2492 
2493  di->bat_cap.max_mah_design = MILLI_TO_MICRO *
2494  di->bat->bat_type[di->bat->batt_id].charge_full_design;
2495 
2496  di->bat_cap.max_mah = di->bat_cap.max_mah_design;
2497 
2498  di->vbat_nom = di->bat->bat_type[di->bat->batt_id].nominal_voltage;
2499 
2500  di->init_capacity = true;
2501 
2502  ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
2503  ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
2504 
2505  /* Create a work queue for running the FG algorithm */
2506  di->fg_wq = create_singlethread_workqueue("ab8500_fg_wq");
2507  if (di->fg_wq == NULL) {
2508  dev_err(di->dev, "failed to create work queue\n");
2509  ret = -ENOMEM;
2510  goto free_device_info;
2511  }
2512 
2513  /* Init work for running the fg algorithm instantly */
2514  INIT_WORK(&di->fg_work, ab8500_fg_instant_work);
2515 
2516  /* Init work for getting the battery accumulated current */
2517  INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work);
2518 
2519  /* Init work for reinitialising the fg algorithm */
2521  ab8500_fg_reinit_work);
2522 
2523  /* Work delayed Queue to run the state machine */
2525  ab8500_fg_periodic_work);
2526 
2527  /* Work to check low battery condition */
2529  ab8500_fg_low_bat_work);
2530 
2531  /* Init work for HW failure check */
2533  ab8500_fg_check_hw_failure_work);
2534 
2535  /* Initialize OVV, and other registers */
2536  ret = ab8500_fg_init_hw_registers(di);
2537  if (ret) {
2538  dev_err(di->dev, "failed to initialize registers\n");
2539  goto free_inst_curr_wq;
2540  }
2541 
2542  /* Consider battery unknown until we're informed otherwise */
2543  di->flags.batt_unknown = true;
2544  di->flags.batt_id_received = false;
2545 
2546  /* Register FG power supply class */
2547  ret = power_supply_register(di->dev, &di->fg_psy);
2548  if (ret) {
2549  dev_err(di->dev, "failed to register FG psy\n");
2550  goto free_inst_curr_wq;
2551  }
2552 
2553  di->fg_samples = SEC_TO_SAMPLE(di->bat->fg_params->init_timer);
2554  ab8500_fg_coulomb_counter(di, true);
2555 
2556  /* Initialize completion used to notify completion of inst current */
2557  init_completion(&di->ab8500_fg_complete);
2558 
2559  /* Register interrupts */
2560  for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq); i++) {
2561  irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
2562  ret = request_threaded_irq(irq, NULL, ab8500_fg_irq[i].isr,
2564  ab8500_fg_irq[i].name, di);
2565 
2566  if (ret != 0) {
2567  dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
2568  , ab8500_fg_irq[i].name, irq, ret);
2569  goto free_irq;
2570  }
2571  dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
2572  ab8500_fg_irq[i].name, irq, ret);
2573  }
2574  di->irq = platform_get_irq_byname(pdev, "CCEOC");
2575  disable_irq(di->irq);
2576 
2577  platform_set_drvdata(pdev, di);
2578 
2579  ret = ab8500_fg_sysfs_init(di);
2580  if (ret) {
2581  dev_err(di->dev, "failed to create sysfs entry\n");
2582  goto free_irq;
2583  }
2584 
2585  /* Calibrate the fg first time */
2586  di->flags.calibrate = true;
2588 
2589  /* Use room temp as default value until we get an update from driver. */
2590  di->bat_temp = 210;
2591 
2592  /* Run the FG algorithm */
2594 
2595  list_add_tail(&di->node, &ab8500_fg_list);
2596 
2597  return ret;
2598 
2599 free_irq:
2601 
2602  /* We also have to free all successfully registered irqs */
2603  for (i = i - 1; i >= 0; i--) {
2604  irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
2605  free_irq(irq, di);
2606  }
2607 free_inst_curr_wq:
2608  destroy_workqueue(di->fg_wq);
2609 free_device_info:
2610  kfree(di);
2611 
2612  return ret;
2613 }
2614 
2615 static struct platform_driver ab8500_fg_driver = {
2616  .probe = ab8500_fg_probe,
2617  .remove = __devexit_p(ab8500_fg_remove),
2618  .suspend = ab8500_fg_suspend,
2619  .resume = ab8500_fg_resume,
2620  .driver = {
2621  .name = "ab8500-fg",
2622  .owner = THIS_MODULE,
2623  },
2624 };
2625 
2626 static int __init ab8500_fg_init(void)
2627 {
2628  return platform_driver_register(&ab8500_fg_driver);
2629 }
2630 
2631 static void __exit ab8500_fg_exit(void)
2632 {
2633  platform_driver_unregister(&ab8500_fg_driver);
2634 }
2635 
2636 subsys_initcall_sync(ab8500_fg_init);
2637 module_exit(ab8500_fg_exit);
2638 
2639 MODULE_LICENSE("GPL v2");
2640 MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
2641 MODULE_ALIAS("platform:ab8500-fg");
2642 MODULE_DESCRIPTION("AB8500 Fuel Gauge driver");