ab8500_bmdata.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/export.h>
  3. #include <linux/power_supply.h>
  4. #include <linux/of.h>
  5. #include <linux/mfd/abx500.h>
  6. #include <linux/mfd/abx500/ab8500.h>
  7. #include <linux/mfd/abx500/ab8500-bm.h>
  8. /*
  9. * These are the defined batteries that uses a NTC and ID resistor placed
  10. * inside of the battery pack.
  11. * Note that the res_to_temp table must be strictly sorted by falling resistance
  12. * values to work.
  13. */
  14. const struct abx500_res_to_temp ab8500_temp_tbl_a_thermistor[] = {
  15. {-5, 53407},
  16. { 0, 48594},
  17. { 5, 43804},
  18. {10, 39188},
  19. {15, 34870},
  20. {20, 30933},
  21. {25, 27422},
  22. {30, 24347},
  23. {35, 21694},
  24. {40, 19431},
  25. {45, 17517},
  26. {50, 15908},
  27. {55, 14561},
  28. {60, 13437},
  29. {65, 12500},
  30. };
  31. EXPORT_SYMBOL(ab8500_temp_tbl_a_thermistor);
  32. const int ab8500_temp_tbl_a_size = ARRAY_SIZE(ab8500_temp_tbl_a_thermistor);
  33. EXPORT_SYMBOL(ab8500_temp_tbl_a_size);
  34. const struct abx500_res_to_temp ab8500_temp_tbl_b_thermistor[] = {
  35. {-5, 200000},
  36. { 0, 159024},
  37. { 5, 151921},
  38. {10, 144300},
  39. {15, 136424},
  40. {20, 128565},
  41. {25, 120978},
  42. {30, 113875},
  43. {35, 107397},
  44. {40, 101629},
  45. {45, 96592},
  46. {50, 92253},
  47. {55, 88569},
  48. {60, 85461},
  49. {65, 82869},
  50. };
  51. EXPORT_SYMBOL(ab8500_temp_tbl_b_thermistor);
  52. const int ab8500_temp_tbl_b_size = ARRAY_SIZE(ab8500_temp_tbl_b_thermistor);
  53. EXPORT_SYMBOL(ab8500_temp_tbl_b_size);
  54. static const struct abx500_v_to_cap cap_tbl_a_thermistor[] = {
  55. {4171, 100},
  56. {4114, 95},
  57. {4009, 83},
  58. {3947, 74},
  59. {3907, 67},
  60. {3863, 59},
  61. {3830, 56},
  62. {3813, 53},
  63. {3791, 46},
  64. {3771, 33},
  65. {3754, 25},
  66. {3735, 20},
  67. {3717, 17},
  68. {3681, 13},
  69. {3664, 8},
  70. {3651, 6},
  71. {3635, 5},
  72. {3560, 3},
  73. {3408, 1},
  74. {3247, 0},
  75. };
  76. static const struct abx500_v_to_cap cap_tbl_b_thermistor[] = {
  77. {4161, 100},
  78. {4124, 98},
  79. {4044, 90},
  80. {4003, 85},
  81. {3966, 80},
  82. {3933, 75},
  83. {3888, 67},
  84. {3849, 60},
  85. {3813, 55},
  86. {3787, 47},
  87. {3772, 30},
  88. {3751, 25},
  89. {3718, 20},
  90. {3681, 16},
  91. {3660, 14},
  92. {3589, 10},
  93. {3546, 7},
  94. {3495, 4},
  95. {3404, 2},
  96. {3250, 0},
  97. };
  98. static const struct abx500_v_to_cap cap_tbl[] = {
  99. {4186, 100},
  100. {4163, 99},
  101. {4114, 95},
  102. {4068, 90},
  103. {3990, 80},
  104. {3926, 70},
  105. {3898, 65},
  106. {3866, 60},
  107. {3833, 55},
  108. {3812, 50},
  109. {3787, 40},
  110. {3768, 30},
  111. {3747, 25},
  112. {3730, 20},
  113. {3705, 15},
  114. {3699, 14},
  115. {3684, 12},
  116. {3672, 9},
  117. {3657, 7},
  118. {3638, 6},
  119. {3556, 4},
  120. {3424, 2},
  121. {3317, 1},
  122. {3094, 0},
  123. };
  124. /*
  125. * Note that the res_to_temp table must be strictly sorted by falling
  126. * resistance values to work.
  127. */
  128. static const struct abx500_res_to_temp temp_tbl[] = {
  129. {-5, 214834},
  130. { 0, 162943},
  131. { 5, 124820},
  132. {10, 96520},
  133. {15, 75306},
  134. {20, 59254},
  135. {25, 47000},
  136. {30, 37566},
  137. {35, 30245},
  138. {40, 24520},
  139. {45, 20010},
  140. {50, 16432},
  141. {55, 13576},
  142. {60, 11280},
  143. {65, 9425},
  144. };
  145. /*
  146. * Note that the batres_vs_temp table must be strictly sorted by falling
  147. * temperature values to work.
  148. */
  149. static const struct batres_vs_temp temp_to_batres_tbl_thermistor[] = {
  150. { 40, 120},
  151. { 30, 135},
  152. { 20, 165},
  153. { 10, 230},
  154. { 00, 325},
  155. {-10, 445},
  156. {-20, 595},
  157. };
  158. /*
  159. * Note that the batres_vs_temp table must be strictly sorted by falling
  160. * temperature values to work.
  161. */
  162. static const struct batres_vs_temp temp_to_batres_tbl_ext_thermistor[] = {
  163. { 60, 300},
  164. { 30, 300},
  165. { 20, 300},
  166. { 10, 300},
  167. { 00, 300},
  168. {-10, 300},
  169. {-20, 300},
  170. };
  171. /* battery resistance table for LI ION 9100 battery */
  172. static const struct batres_vs_temp temp_to_batres_tbl_9100[] = {
  173. { 60, 180},
  174. { 30, 180},
  175. { 20, 180},
  176. { 10, 180},
  177. { 00, 180},
  178. {-10, 180},
  179. {-20, 180},
  180. };
  181. static struct abx500_battery_type bat_type_thermistor[] = {
  182. [BATTERY_UNKNOWN] = {
  183. /* First element always represent the UNKNOWN battery */
  184. .name = POWER_SUPPLY_TECHNOLOGY_UNKNOWN,
  185. .resis_high = 0,
  186. .resis_low = 0,
  187. .battery_resistance = 300,
  188. .charge_full_design = 612,
  189. .nominal_voltage = 3700,
  190. .termination_vol = 4050,
  191. .termination_curr = 200,
  192. .recharge_cap = 95,
  193. .normal_cur_lvl = 400,
  194. .normal_vol_lvl = 4100,
  195. .maint_a_cur_lvl = 400,
  196. .maint_a_vol_lvl = 4050,
  197. .maint_a_chg_timer_h = 60,
  198. .maint_b_cur_lvl = 400,
  199. .maint_b_vol_lvl = 4000,
  200. .maint_b_chg_timer_h = 200,
  201. .low_high_cur_lvl = 300,
  202. .low_high_vol_lvl = 4000,
  203. .n_temp_tbl_elements = ARRAY_SIZE(temp_tbl),
  204. .r_to_t_tbl = temp_tbl,
  205. .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl),
  206. .v_to_cap_tbl = cap_tbl,
  207. .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
  208. .batres_tbl = temp_to_batres_tbl_thermistor,
  209. },
  210. {
  211. .name = POWER_SUPPLY_TECHNOLOGY_LIPO,
  212. .resis_high = 53407,
  213. .resis_low = 12500,
  214. .battery_resistance = 300,
  215. .charge_full_design = 900,
  216. .nominal_voltage = 3600,
  217. .termination_vol = 4150,
  218. .termination_curr = 80,
  219. .recharge_cap = 95,
  220. .normal_cur_lvl = 700,
  221. .normal_vol_lvl = 4200,
  222. .maint_a_cur_lvl = 600,
  223. .maint_a_vol_lvl = 4150,
  224. .maint_a_chg_timer_h = 60,
  225. .maint_b_cur_lvl = 600,
  226. .maint_b_vol_lvl = 4100,
  227. .maint_b_chg_timer_h = 200,
  228. .low_high_cur_lvl = 300,
  229. .low_high_vol_lvl = 4000,
  230. .n_temp_tbl_elements = ARRAY_SIZE(ab8500_temp_tbl_a_thermistor),
  231. .r_to_t_tbl = ab8500_temp_tbl_a_thermistor,
  232. .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl_a_thermistor),
  233. .v_to_cap_tbl = cap_tbl_a_thermistor,
  234. .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
  235. .batres_tbl = temp_to_batres_tbl_thermistor,
  236. },
  237. {
  238. .name = POWER_SUPPLY_TECHNOLOGY_LIPO,
  239. .resis_high = 200000,
  240. .resis_low = 82869,
  241. .battery_resistance = 300,
  242. .charge_full_design = 900,
  243. .nominal_voltage = 3600,
  244. .termination_vol = 4150,
  245. .termination_curr = 80,
  246. .recharge_cap = 95,
  247. .normal_cur_lvl = 700,
  248. .normal_vol_lvl = 4200,
  249. .maint_a_cur_lvl = 600,
  250. .maint_a_vol_lvl = 4150,
  251. .maint_a_chg_timer_h = 60,
  252. .maint_b_cur_lvl = 600,
  253. .maint_b_vol_lvl = 4100,
  254. .maint_b_chg_timer_h = 200,
  255. .low_high_cur_lvl = 300,
  256. .low_high_vol_lvl = 4000,
  257. .n_temp_tbl_elements = ARRAY_SIZE(ab8500_temp_tbl_b_thermistor),
  258. .r_to_t_tbl = ab8500_temp_tbl_b_thermistor,
  259. .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl_b_thermistor),
  260. .v_to_cap_tbl = cap_tbl_b_thermistor,
  261. .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
  262. .batres_tbl = temp_to_batres_tbl_thermistor,
  263. },
  264. };
  265. static struct abx500_battery_type bat_type_ext_thermistor[] = {
  266. [BATTERY_UNKNOWN] = {
  267. /* First element always represent the UNKNOWN battery */
  268. .name = POWER_SUPPLY_TECHNOLOGY_UNKNOWN,
  269. .resis_high = 0,
  270. .resis_low = 0,
  271. .battery_resistance = 300,
  272. .charge_full_design = 612,
  273. .nominal_voltage = 3700,
  274. .termination_vol = 4050,
  275. .termination_curr = 200,
  276. .recharge_cap = 95,
  277. .normal_cur_lvl = 400,
  278. .normal_vol_lvl = 4100,
  279. .maint_a_cur_lvl = 400,
  280. .maint_a_vol_lvl = 4050,
  281. .maint_a_chg_timer_h = 60,
  282. .maint_b_cur_lvl = 400,
  283. .maint_b_vol_lvl = 4000,
  284. .maint_b_chg_timer_h = 200,
  285. .low_high_cur_lvl = 300,
  286. .low_high_vol_lvl = 4000,
  287. .n_temp_tbl_elements = ARRAY_SIZE(temp_tbl),
  288. .r_to_t_tbl = temp_tbl,
  289. .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl),
  290. .v_to_cap_tbl = cap_tbl,
  291. .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
  292. .batres_tbl = temp_to_batres_tbl_thermistor,
  293. },
  294. /*
  295. * These are the batteries that doesn't have an internal NTC resistor to measure
  296. * its temperature. The temperature in this case is measure with a NTC placed
  297. * near the battery but on the PCB.
  298. */
  299. {
  300. .name = POWER_SUPPLY_TECHNOLOGY_LIPO,
  301. .resis_high = 76000,
  302. .resis_low = 53000,
  303. .battery_resistance = 300,
  304. .charge_full_design = 900,
  305. .nominal_voltage = 3700,
  306. .termination_vol = 4150,
  307. .termination_curr = 100,
  308. .recharge_cap = 95,
  309. .normal_cur_lvl = 700,
  310. .normal_vol_lvl = 4200,
  311. .maint_a_cur_lvl = 600,
  312. .maint_a_vol_lvl = 4150,
  313. .maint_a_chg_timer_h = 60,
  314. .maint_b_cur_lvl = 600,
  315. .maint_b_vol_lvl = 4100,
  316. .maint_b_chg_timer_h = 200,
  317. .low_high_cur_lvl = 300,
  318. .low_high_vol_lvl = 4000,
  319. .n_temp_tbl_elements = ARRAY_SIZE(temp_tbl),
  320. .r_to_t_tbl = temp_tbl,
  321. .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl),
  322. .v_to_cap_tbl = cap_tbl,
  323. .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
  324. .batres_tbl = temp_to_batres_tbl_thermistor,
  325. },
  326. {
  327. .name = POWER_SUPPLY_TECHNOLOGY_LION,
  328. .resis_high = 30000,
  329. .resis_low = 10000,
  330. .battery_resistance = 300,
  331. .charge_full_design = 950,
  332. .nominal_voltage = 3700,
  333. .termination_vol = 4150,
  334. .termination_curr = 100,
  335. .recharge_cap = 95,
  336. .normal_cur_lvl = 700,
  337. .normal_vol_lvl = 4200,
  338. .maint_a_cur_lvl = 600,
  339. .maint_a_vol_lvl = 4150,
  340. .maint_a_chg_timer_h = 60,
  341. .maint_b_cur_lvl = 600,
  342. .maint_b_vol_lvl = 4100,
  343. .maint_b_chg_timer_h = 200,
  344. .low_high_cur_lvl = 300,
  345. .low_high_vol_lvl = 4000,
  346. .n_temp_tbl_elements = ARRAY_SIZE(temp_tbl),
  347. .r_to_t_tbl = temp_tbl,
  348. .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl),
  349. .v_to_cap_tbl = cap_tbl,
  350. .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
  351. .batres_tbl = temp_to_batres_tbl_thermistor,
  352. },
  353. {
  354. .name = POWER_SUPPLY_TECHNOLOGY_LION,
  355. .resis_high = 95000,
  356. .resis_low = 76001,
  357. .battery_resistance = 300,
  358. .charge_full_design = 950,
  359. .nominal_voltage = 3700,
  360. .termination_vol = 4150,
  361. .termination_curr = 100,
  362. .recharge_cap = 95,
  363. .normal_cur_lvl = 700,
  364. .normal_vol_lvl = 4200,
  365. .maint_a_cur_lvl = 600,
  366. .maint_a_vol_lvl = 4150,
  367. .maint_a_chg_timer_h = 60,
  368. .maint_b_cur_lvl = 600,
  369. .maint_b_vol_lvl = 4100,
  370. .maint_b_chg_timer_h = 200,
  371. .low_high_cur_lvl = 300,
  372. .low_high_vol_lvl = 4000,
  373. .n_temp_tbl_elements = ARRAY_SIZE(temp_tbl),
  374. .r_to_t_tbl = temp_tbl,
  375. .n_v_cap_tbl_elements = ARRAY_SIZE(cap_tbl),
  376. .v_to_cap_tbl = cap_tbl,
  377. .n_batres_tbl_elements = ARRAY_SIZE(temp_to_batres_tbl_thermistor),
  378. .batres_tbl = temp_to_batres_tbl_thermistor,
  379. },
  380. };
  381. static const struct abx500_bm_capacity_levels cap_levels = {
  382. .critical = 2,
  383. .low = 10,
  384. .normal = 70,
  385. .high = 95,
  386. .full = 100,
  387. };
  388. static const struct abx500_fg_parameters fg = {
  389. .recovery_sleep_timer = 10,
  390. .recovery_total_time = 100,
  391. .init_timer = 1,
  392. .init_discard_time = 5,
  393. .init_total_time = 40,
  394. .high_curr_time = 60,
  395. .accu_charging = 30,
  396. .accu_high_curr = 30,
  397. .high_curr_threshold = 50,
  398. .lowbat_threshold = 3100,
  399. .battok_falling_th_sel0 = 2860,
  400. .battok_raising_th_sel1 = 2860,
  401. .maint_thres = 95,
  402. .user_cap_limit = 15,
  403. .pcut_enable = 1,
  404. .pcut_max_time = 127,
  405. .pcut_flag_time = 112,
  406. .pcut_max_restart = 15,
  407. .pcut_debounce_time = 2,
  408. };
  409. static const struct abx500_maxim_parameters ab8500_maxi_params = {
  410. .ena_maxi = true,
  411. .chg_curr = 910,
  412. .wait_cycles = 10,
  413. .charger_curr_step = 100,
  414. };
  415. static const struct abx500_bm_charger_parameters chg = {
  416. .usb_volt_max = 5500,
  417. .usb_curr_max = 1500,
  418. .ac_volt_max = 7500,
  419. .ac_curr_max = 1500,
  420. };
  421. /*
  422. * This array maps the raw hex value to charger output current used by the
  423. * AB8500 values
  424. */
  425. static int ab8500_charge_output_curr_map[] = {
  426. 100, 200, 300, 400, 500, 600, 700, 800,
  427. 900, 1000, 1100, 1200, 1300, 1400, 1500, 1500,
  428. };
  429. /*
  430. * This array maps the raw hex value to charger input current used by the
  431. * AB8500 values
  432. */
  433. static int ab8500_charge_input_curr_map[] = {
  434. 50, 98, 193, 290, 380, 450, 500, 600,
  435. 700, 800, 900, 1000, 1100, 1300, 1400, 1500,
  436. };
  437. struct abx500_bm_data ab8500_bm_data = {
  438. .temp_under = 3,
  439. .temp_low = 8,
  440. .temp_high = 43,
  441. .temp_over = 48,
  442. .main_safety_tmr_h = 4,
  443. .temp_interval_chg = 20,
  444. .temp_interval_nochg = 120,
  445. .usb_safety_tmr_h = 4,
  446. .bkup_bat_v = BUP_VCH_SEL_2P6V,
  447. .bkup_bat_i = BUP_ICH_SEL_150UA,
  448. .no_maintenance = false,
  449. .capacity_scaling = false,
  450. .adc_therm = ABx500_ADC_THERM_BATCTRL,
  451. .chg_unknown_bat = false,
  452. .enable_overshoot = false,
  453. .fg_res = 100,
  454. .cap_levels = &cap_levels,
  455. .bat_type = bat_type_thermistor,
  456. .n_btypes = ARRAY_SIZE(bat_type_thermistor),
  457. .batt_id = 0,
  458. .interval_charging = 5,
  459. .interval_not_charging = 120,
  460. .temp_hysteresis = 3,
  461. .gnd_lift_resistance = 34,
  462. .chg_output_curr = ab8500_charge_output_curr_map,
  463. .n_chg_out_curr = ARRAY_SIZE(ab8500_charge_output_curr_map),
  464. .maxi = &ab8500_maxi_params,
  465. .chg_params = &chg,
  466. .fg_params = &fg,
  467. .chg_input_curr = ab8500_charge_input_curr_map,
  468. .n_chg_in_curr = ARRAY_SIZE(ab8500_charge_input_curr_map),
  469. };
  470. int ab8500_bm_of_probe(struct device *dev,
  471. struct device_node *np,
  472. struct abx500_bm_data *bm)
  473. {
  474. const struct batres_vs_temp *tmp_batres_tbl;
  475. struct device_node *battery_node;
  476. const char *btech;
  477. int i;
  478. /* get phandle to 'battery-info' node */
  479. battery_node = of_parse_phandle(np, "battery", 0);
  480. if (!battery_node) {
  481. dev_err(dev, "battery node or reference missing\n");
  482. return -EINVAL;
  483. }
  484. btech = of_get_property(battery_node, "stericsson,battery-type", NULL);
  485. if (!btech) {
  486. dev_warn(dev, "missing property battery-name/type\n");
  487. of_node_put(battery_node);
  488. return -EINVAL;
  489. }
  490. if (strncmp(btech, "LION", 4) == 0) {
  491. bm->no_maintenance = true;
  492. bm->chg_unknown_bat = true;
  493. bm->bat_type[BATTERY_UNKNOWN].charge_full_design = 2600;
  494. bm->bat_type[BATTERY_UNKNOWN].termination_vol = 4150;
  495. bm->bat_type[BATTERY_UNKNOWN].recharge_cap = 95;
  496. bm->bat_type[BATTERY_UNKNOWN].normal_cur_lvl = 520;
  497. bm->bat_type[BATTERY_UNKNOWN].normal_vol_lvl = 4200;
  498. }
  499. if (of_property_read_bool(battery_node, "thermistor-on-batctrl")) {
  500. if (strncmp(btech, "LION", 4) == 0)
  501. tmp_batres_tbl = temp_to_batres_tbl_9100;
  502. else
  503. tmp_batres_tbl = temp_to_batres_tbl_thermistor;
  504. } else {
  505. bm->n_btypes = 4;
  506. bm->bat_type = bat_type_ext_thermistor;
  507. bm->adc_therm = ABx500_ADC_THERM_BATTEMP;
  508. tmp_batres_tbl = temp_to_batres_tbl_ext_thermistor;
  509. }
  510. /* select the battery resolution table */
  511. for (i = 0; i < bm->n_btypes; ++i)
  512. bm->bat_type[i].batres_tbl = tmp_batres_tbl;
  513. of_node_put(battery_node);
  514. return 0;
  515. }