ab8500_btemp.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2012
  3. *
  4. * Battery temperature driver for AB8500
  5. *
  6. * License Terms: GNU General Public License v2
  7. * Author:
  8. * Johan Palsson <johan.palsson@stericsson.com>
  9. * Karl Komierowski <karl.komierowski@stericsson.com>
  10. * Arun R Murthy <arun.murthy@stericsson.com>
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/slab.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/power_supply.h>
  20. #include <linux/completion.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/of.h>
  24. #include <linux/mfd/core.h>
  25. #include <linux/mfd/abx500.h>
  26. #include <linux/mfd/abx500/ab8500.h>
  27. #include <linux/mfd/abx500/ab8500-bm.h>
  28. #include <linux/mfd/abx500/ab8500-gpadc.h>
  29. #define VTVOUT_V 1800
  30. #define BTEMP_THERMAL_LOW_LIMIT -10
  31. #define BTEMP_THERMAL_MED_LIMIT 0
  32. #define BTEMP_THERMAL_HIGH_LIMIT_52 52
  33. #define BTEMP_THERMAL_HIGH_LIMIT_57 57
  34. #define BTEMP_THERMAL_HIGH_LIMIT_62 62
  35. #define BTEMP_BATCTRL_CURR_SRC_7UA 7
  36. #define BTEMP_BATCTRL_CURR_SRC_20UA 20
  37. #define BTEMP_BATCTRL_CURR_SRC_16UA 16
  38. #define BTEMP_BATCTRL_CURR_SRC_18UA 18
  39. #define to_ab8500_btemp_device_info(x) container_of((x), \
  40. struct ab8500_btemp, btemp_psy);
  41. /**
  42. * struct ab8500_btemp_interrupts - ab8500 interrupts
  43. * @name: name of the interrupt
  44. * @isr function pointer to the isr
  45. */
  46. struct ab8500_btemp_interrupts {
  47. char *name;
  48. irqreturn_t (*isr)(int irq, void *data);
  49. };
  50. struct ab8500_btemp_events {
  51. bool batt_rem;
  52. bool btemp_high;
  53. bool btemp_medhigh;
  54. bool btemp_lowmed;
  55. bool btemp_low;
  56. bool ac_conn;
  57. bool usb_conn;
  58. };
  59. struct ab8500_btemp_ranges {
  60. int btemp_high_limit;
  61. int btemp_med_limit;
  62. int btemp_low_limit;
  63. };
  64. /**
  65. * struct ab8500_btemp - ab8500 BTEMP device information
  66. * @dev: Pointer to the structure device
  67. * @node: List of AB8500 BTEMPs, hence prepared for reentrance
  68. * @curr_source: What current source we use, in uA
  69. * @bat_temp: Dispatched battery temperature in degree Celcius
  70. * @prev_bat_temp Last measured battery temperature in degree Celcius
  71. * @parent: Pointer to the struct ab8500
  72. * @gpadc: Pointer to the struct gpadc
  73. * @fg: Pointer to the struct fg
  74. * @bm: Platform specific battery management information
  75. * @btemp_psy: Structure for BTEMP specific battery properties
  76. * @events: Structure for information about events triggered
  77. * @btemp_ranges: Battery temperature range structure
  78. * @btemp_wq: Work queue for measuring the temperature periodically
  79. * @btemp_periodic_work: Work for measuring the temperature periodically
  80. * @initialized: True if battery id read.
  81. */
  82. struct ab8500_btemp {
  83. struct device *dev;
  84. struct list_head node;
  85. int curr_source;
  86. int bat_temp;
  87. int prev_bat_temp;
  88. struct ab8500 *parent;
  89. struct ab8500_gpadc *gpadc;
  90. struct ab8500_fg *fg;
  91. struct abx500_bm_data *bm;
  92. struct power_supply btemp_psy;
  93. struct ab8500_btemp_events events;
  94. struct ab8500_btemp_ranges btemp_ranges;
  95. struct workqueue_struct *btemp_wq;
  96. struct delayed_work btemp_periodic_work;
  97. bool initialized;
  98. };
  99. /* BTEMP power supply properties */
  100. static enum power_supply_property ab8500_btemp_props[] = {
  101. POWER_SUPPLY_PROP_PRESENT,
  102. POWER_SUPPLY_PROP_ONLINE,
  103. POWER_SUPPLY_PROP_TECHNOLOGY,
  104. POWER_SUPPLY_PROP_TEMP,
  105. };
  106. static LIST_HEAD(ab8500_btemp_list);
  107. /**
  108. * ab8500_btemp_get() - returns a reference to the primary AB8500 BTEMP
  109. * (i.e. the first BTEMP in the instance list)
  110. */
  111. struct ab8500_btemp *ab8500_btemp_get(void)
  112. {
  113. struct ab8500_btemp *btemp;
  114. btemp = list_first_entry(&ab8500_btemp_list, struct ab8500_btemp, node);
  115. return btemp;
  116. }
  117. /**
  118. * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
  119. * @di: pointer to the ab8500_btemp structure
  120. * @v_batctrl: measured batctrl voltage
  121. * @inst_curr: measured instant current
  122. *
  123. * This function returns the battery resistance that is
  124. * derived from the BATCTRL voltage.
  125. * Returns value in Ohms.
  126. */
  127. static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
  128. int v_batctrl, int inst_curr)
  129. {
  130. int rbs;
  131. if (is_ab8500_1p1_or_earlier(di->parent)) {
  132. /*
  133. * For ABB cut1.0 and 1.1 BAT_CTRL is internally
  134. * connected to 1.8V through a 450k resistor
  135. */
  136. return (450000 * (v_batctrl)) / (1800 - v_batctrl);
  137. }
  138. if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL) {
  139. /*
  140. * If the battery has internal NTC, we use the current
  141. * source to calculate the resistance, 7uA or 20uA
  142. */
  143. rbs = (v_batctrl * 1000
  144. - di->bm->gnd_lift_resistance * inst_curr)
  145. / di->curr_source;
  146. } else {
  147. /*
  148. * BAT_CTRL is internally
  149. * connected to 1.8V through a 80k resistor
  150. */
  151. rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
  152. }
  153. return rbs;
  154. }
  155. /**
  156. * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
  157. * @di: pointer to the ab8500_btemp structure
  158. *
  159. * This function returns the voltage on BATCTRL. Returns value in mV.
  160. */
  161. static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
  162. {
  163. int vbtemp;
  164. static int prev;
  165. vbtemp = ab8500_gpadc_convert(di->gpadc, BAT_CTRL);
  166. if (vbtemp < 0) {
  167. dev_err(di->dev,
  168. "%s gpadc conversion failed, using previous value",
  169. __func__);
  170. return prev;
  171. }
  172. prev = vbtemp;
  173. return vbtemp;
  174. }
  175. /**
  176. * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source
  177. * @di: pointer to the ab8500_btemp structure
  178. * @enable: enable or disable the current source
  179. *
  180. * Enable or disable the current sources for the BatCtrl AD channel
  181. */
  182. static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
  183. bool enable)
  184. {
  185. int curr;
  186. int ret = 0;
  187. /*
  188. * BATCTRL current sources are included on AB8500 cut2.0
  189. * and future versions
  190. */
  191. if (is_ab8500_1p1_or_earlier(di->parent))
  192. return 0;
  193. /* Only do this for batteries with internal NTC */
  194. if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) {
  195. if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
  196. if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_16UA)
  197. curr = BAT_CTRL_16U_ENA;
  198. else
  199. curr = BAT_CTRL_18U_ENA;
  200. } else {
  201. if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
  202. curr = BAT_CTRL_7U_ENA;
  203. else
  204. curr = BAT_CTRL_20U_ENA;
  205. }
  206. dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
  207. ret = abx500_mask_and_set_register_interruptible(di->dev,
  208. AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
  209. FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH);
  210. if (ret) {
  211. dev_err(di->dev, "%s failed setting cmp_force\n",
  212. __func__);
  213. return ret;
  214. }
  215. /*
  216. * We have to wait one 32kHz cycle before enabling
  217. * the current source, since ForceBatCtrlCmpHigh needs
  218. * to be written in a separate cycle
  219. */
  220. udelay(32);
  221. ret = abx500_set_register_interruptible(di->dev,
  222. AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
  223. FORCE_BAT_CTRL_CMP_HIGH | curr);
  224. if (ret) {
  225. dev_err(di->dev, "%s failed enabling current source\n",
  226. __func__);
  227. goto disable_curr_source;
  228. }
  229. } else if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) {
  230. dev_dbg(di->dev, "Disable BATCTRL curr source\n");
  231. if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
  232. /* Write 0 to the curr bits */
  233. ret = abx500_mask_and_set_register_interruptible(
  234. di->dev,
  235. AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
  236. BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA,
  237. ~(BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA));
  238. } else {
  239. /* Write 0 to the curr bits */
  240. ret = abx500_mask_and_set_register_interruptible(
  241. di->dev,
  242. AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
  243. BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
  244. ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
  245. }
  246. if (ret) {
  247. dev_err(di->dev, "%s failed disabling current source\n",
  248. __func__);
  249. goto disable_curr_source;
  250. }
  251. /* Enable Pull-Up and comparator */
  252. ret = abx500_mask_and_set_register_interruptible(di->dev,
  253. AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
  254. BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
  255. BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
  256. if (ret) {
  257. dev_err(di->dev, "%s failed enabling PU and comp\n",
  258. __func__);
  259. goto enable_pu_comp;
  260. }
  261. /*
  262. * We have to wait one 32kHz cycle before disabling
  263. * ForceBatCtrlCmpHigh since this needs to be written
  264. * in a separate cycle
  265. */
  266. udelay(32);
  267. /* Disable 'force comparator' */
  268. ret = abx500_mask_and_set_register_interruptible(di->dev,
  269. AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
  270. FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
  271. if (ret) {
  272. dev_err(di->dev, "%s failed disabling force comp\n",
  273. __func__);
  274. goto disable_force_comp;
  275. }
  276. }
  277. return ret;
  278. /*
  279. * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
  280. * if we got an error above
  281. */
  282. disable_curr_source:
  283. if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
  284. /* Write 0 to the curr bits */
  285. ret = abx500_mask_and_set_register_interruptible(di->dev,
  286. AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
  287. BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA,
  288. ~(BAT_CTRL_16U_ENA | BAT_CTRL_18U_ENA));
  289. } else {
  290. /* Write 0 to the curr bits */
  291. ret = abx500_mask_and_set_register_interruptible(di->dev,
  292. AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
  293. BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
  294. ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
  295. }
  296. if (ret) {
  297. dev_err(di->dev, "%s failed disabling current source\n",
  298. __func__);
  299. return ret;
  300. }
  301. enable_pu_comp:
  302. /* Enable Pull-Up and comparator */
  303. ret = abx500_mask_and_set_register_interruptible(di->dev,
  304. AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
  305. BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
  306. BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
  307. if (ret) {
  308. dev_err(di->dev, "%s failed enabling PU and comp\n",
  309. __func__);
  310. return ret;
  311. }
  312. disable_force_comp:
  313. /*
  314. * We have to wait one 32kHz cycle before disabling
  315. * ForceBatCtrlCmpHigh since this needs to be written
  316. * in a separate cycle
  317. */
  318. udelay(32);
  319. /* Disable 'force comparator' */
  320. ret = abx500_mask_and_set_register_interruptible(di->dev,
  321. AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
  322. FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
  323. if (ret) {
  324. dev_err(di->dev, "%s failed disabling force comp\n",
  325. __func__);
  326. return ret;
  327. }
  328. return ret;
  329. }
  330. /**
  331. * ab8500_btemp_get_batctrl_res() - get battery resistance
  332. * @di: pointer to the ab8500_btemp structure
  333. *
  334. * This function returns the battery pack identification resistance.
  335. * Returns value in Ohms.
  336. */
  337. static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
  338. {
  339. int ret;
  340. int batctrl = 0;
  341. int res;
  342. int inst_curr;
  343. int i;
  344. /*
  345. * BATCTRL current sources are included on AB8500 cut2.0
  346. * and future versions
  347. */
  348. ret = ab8500_btemp_curr_source_enable(di, true);
  349. if (ret) {
  350. dev_err(di->dev, "%s curr source enabled failed\n", __func__);
  351. return ret;
  352. }
  353. if (!di->fg)
  354. di->fg = ab8500_fg_get();
  355. if (!di->fg) {
  356. dev_err(di->dev, "No fg found\n");
  357. return -EINVAL;
  358. }
  359. ret = ab8500_fg_inst_curr_start(di->fg);
  360. if (ret) {
  361. dev_err(di->dev, "Failed to start current measurement\n");
  362. return ret;
  363. }
  364. do {
  365. msleep(20);
  366. } while (!ab8500_fg_inst_curr_started(di->fg));
  367. i = 0;
  368. do {
  369. batctrl += ab8500_btemp_read_batctrl_voltage(di);
  370. i++;
  371. msleep(20);
  372. } while (!ab8500_fg_inst_curr_done(di->fg));
  373. batctrl /= i;
  374. ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
  375. if (ret) {
  376. dev_err(di->dev, "Failed to finalize current measurement\n");
  377. return ret;
  378. }
  379. res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
  380. ret = ab8500_btemp_curr_source_enable(di, false);
  381. if (ret) {
  382. dev_err(di->dev, "%s curr source disable failed\n", __func__);
  383. return ret;
  384. }
  385. dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
  386. __func__, batctrl, res, inst_curr, i);
  387. return res;
  388. }
  389. /**
  390. * ab8500_btemp_res_to_temp() - resistance to temperature
  391. * @di: pointer to the ab8500_btemp structure
  392. * @tbl: pointer to the resiatance to temperature table
  393. * @tbl_size: size of the resistance to temperature table
  394. * @res: resistance to calculate the temperature from
  395. *
  396. * This function returns the battery temperature in degrees Celcius
  397. * based on the NTC resistance.
  398. */
  399. static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
  400. const struct abx500_res_to_temp *tbl, int tbl_size, int res)
  401. {
  402. int i, temp;
  403. /*
  404. * Calculate the formula for the straight line
  405. * Simple interpolation if we are within
  406. * the resistance table limits, extrapolate
  407. * if resistance is outside the limits.
  408. */
  409. if (res > tbl[0].resist)
  410. i = 0;
  411. else if (res <= tbl[tbl_size - 1].resist)
  412. i = tbl_size - 2;
  413. else {
  414. i = 0;
  415. while (!(res <= tbl[i].resist &&
  416. res > tbl[i + 1].resist))
  417. i++;
  418. }
  419. temp = tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
  420. (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
  421. return temp;
  422. }
  423. /**
  424. * ab8500_btemp_measure_temp() - measure battery temperature
  425. * @di: pointer to the ab8500_btemp structure
  426. *
  427. * Returns battery temperature (on success) else the previous temperature
  428. */
  429. static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
  430. {
  431. int temp;
  432. static int prev;
  433. int rbat, rntc, vntc;
  434. u8 id;
  435. id = di->bm->batt_id;
  436. if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
  437. id != BATTERY_UNKNOWN) {
  438. rbat = ab8500_btemp_get_batctrl_res(di);
  439. if (rbat < 0) {
  440. dev_err(di->dev, "%s get batctrl res failed\n",
  441. __func__);
  442. /*
  443. * Return out-of-range temperature so that
  444. * charging is stopped
  445. */
  446. return BTEMP_THERMAL_LOW_LIMIT;
  447. }
  448. temp = ab8500_btemp_res_to_temp(di,
  449. di->bm->bat_type[id].r_to_t_tbl,
  450. di->bm->bat_type[id].n_temp_tbl_elements, rbat);
  451. } else {
  452. vntc = ab8500_gpadc_convert(di->gpadc, BTEMP_BALL);
  453. if (vntc < 0) {
  454. dev_err(di->dev,
  455. "%s gpadc conversion failed,"
  456. " using previous value\n", __func__);
  457. return prev;
  458. }
  459. /*
  460. * The PCB NTC is sourced from VTVOUT via a 230kOhm
  461. * resistor.
  462. */
  463. rntc = 230000 * vntc / (VTVOUT_V - vntc);
  464. temp = ab8500_btemp_res_to_temp(di,
  465. di->bm->bat_type[id].r_to_t_tbl,
  466. di->bm->bat_type[id].n_temp_tbl_elements, rntc);
  467. prev = temp;
  468. }
  469. dev_dbg(di->dev, "Battery temperature is %d\n", temp);
  470. return temp;
  471. }
  472. /**
  473. * ab8500_btemp_id() - Identify the connected battery
  474. * @di: pointer to the ab8500_btemp structure
  475. *
  476. * This function will try to identify the battery by reading the ID
  477. * resistor. Some brands use a combined ID resistor with a NTC resistor to
  478. * both be able to identify and to read the temperature of it.
  479. */
  480. static int ab8500_btemp_id(struct ab8500_btemp *di)
  481. {
  482. int res;
  483. u8 i;
  484. if (is_ab9540(di->parent) || is_ab8505(di->parent))
  485. di->curr_source = BTEMP_BATCTRL_CURR_SRC_16UA;
  486. else
  487. di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
  488. di->bm->batt_id = BATTERY_UNKNOWN;
  489. res = ab8500_btemp_get_batctrl_res(di);
  490. if (res < 0) {
  491. dev_err(di->dev, "%s get batctrl res failed\n", __func__);
  492. return -ENXIO;
  493. }
  494. /* BATTERY_UNKNOWN is defined on position 0, skip it! */
  495. for (i = BATTERY_UNKNOWN + 1; i < di->bm->n_btypes; i++) {
  496. if ((res <= di->bm->bat_type[i].resis_high) &&
  497. (res >= di->bm->bat_type[i].resis_low)) {
  498. dev_dbg(di->dev, "Battery detected on %s"
  499. " low %d < res %d < high: %d"
  500. " index: %d\n",
  501. di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL ?
  502. "BATCTRL" : "BATTEMP",
  503. di->bm->bat_type[i].resis_low, res,
  504. di->bm->bat_type[i].resis_high, i);
  505. di->bm->batt_id = i;
  506. break;
  507. }
  508. }
  509. if (di->bm->batt_id == BATTERY_UNKNOWN) {
  510. dev_warn(di->dev, "Battery identified as unknown"
  511. ", resistance %d Ohm\n", res);
  512. return -ENXIO;
  513. }
  514. /*
  515. * We only have to change current source if the
  516. * detected type is Type 1, else we use the 7uA source
  517. */
  518. if (di->bm->adc_therm == ABx500_ADC_THERM_BATCTRL &&
  519. di->bm->batt_id == 1) {
  520. if (is_ab9540(di->parent) || is_ab8505(di->parent)) {
  521. dev_dbg(di->dev, "Set BATCTRL current source to 16uA\n");
  522. di->curr_source = BTEMP_BATCTRL_CURR_SRC_16UA;
  523. } else {
  524. dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
  525. di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
  526. }
  527. }
  528. return di->bm->batt_id;
  529. }
  530. /**
  531. * ab8500_btemp_periodic_work() - Measuring the temperature periodically
  532. * @work: pointer to the work_struct structure
  533. *
  534. * Work function for measuring the temperature periodically
  535. */
  536. static void ab8500_btemp_periodic_work(struct work_struct *work)
  537. {
  538. int interval;
  539. int bat_temp;
  540. struct ab8500_btemp *di = container_of(work,
  541. struct ab8500_btemp, btemp_periodic_work.work);
  542. if (!di->initialized) {
  543. /* Identify the battery */
  544. if (ab8500_btemp_id(di) < 0)
  545. dev_warn(di->dev, "failed to identify the battery\n");
  546. }
  547. bat_temp = ab8500_btemp_measure_temp(di);
  548. /*
  549. * Filter battery temperature.
  550. * Allow direct updates on temperature only if two samples result in
  551. * same temperature. Else only allow 1 degree change from previous
  552. * reported value in the direction of the new measurement.
  553. */
  554. if ((bat_temp == di->prev_bat_temp) || !di->initialized) {
  555. if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) {
  556. di->initialized = true;
  557. di->bat_temp = bat_temp;
  558. power_supply_changed(&di->btemp_psy);
  559. }
  560. } else if (bat_temp < di->prev_bat_temp) {
  561. di->bat_temp--;
  562. power_supply_changed(&di->btemp_psy);
  563. } else if (bat_temp > di->prev_bat_temp) {
  564. di->bat_temp++;
  565. power_supply_changed(&di->btemp_psy);
  566. }
  567. di->prev_bat_temp = bat_temp;
  568. if (di->events.ac_conn || di->events.usb_conn)
  569. interval = di->bm->temp_interval_chg;
  570. else
  571. interval = di->bm->temp_interval_nochg;
  572. /* Schedule a new measurement */
  573. queue_delayed_work(di->btemp_wq,
  574. &di->btemp_periodic_work,
  575. round_jiffies(interval * HZ));
  576. }
  577. /**
  578. * ab8500_btemp_batctrlindb_handler() - battery removal detected
  579. * @irq: interrupt number
  580. * @_di: void pointer that has to address of ab8500_btemp
  581. *
  582. * Returns IRQ status(IRQ_HANDLED)
  583. */
  584. static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
  585. {
  586. struct ab8500_btemp *di = _di;
  587. dev_err(di->dev, "Battery removal detected!\n");
  588. di->events.batt_rem = true;
  589. power_supply_changed(&di->btemp_psy);
  590. return IRQ_HANDLED;
  591. }
  592. /**
  593. * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
  594. * @irq: interrupt number
  595. * @_di: void pointer that has to address of ab8500_btemp
  596. *
  597. * Returns IRQ status(IRQ_HANDLED)
  598. */
  599. static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
  600. {
  601. struct ab8500_btemp *di = _di;
  602. if (is_ab8500_3p3_or_earlier(di->parent)) {
  603. dev_dbg(di->dev, "Ignore false btemp low irq"
  604. " for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
  605. } else {
  606. dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
  607. di->events.btemp_low = true;
  608. di->events.btemp_high = false;
  609. di->events.btemp_medhigh = false;
  610. di->events.btemp_lowmed = false;
  611. power_supply_changed(&di->btemp_psy);
  612. }
  613. return IRQ_HANDLED;
  614. }
  615. /**
  616. * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
  617. * @irq: interrupt number
  618. * @_di: void pointer that has to address of ab8500_btemp
  619. *
  620. * Returns IRQ status(IRQ_HANDLED)
  621. */
  622. static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
  623. {
  624. struct ab8500_btemp *di = _di;
  625. dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
  626. di->events.btemp_high = true;
  627. di->events.btemp_medhigh = false;
  628. di->events.btemp_lowmed = false;
  629. di->events.btemp_low = false;
  630. power_supply_changed(&di->btemp_psy);
  631. return IRQ_HANDLED;
  632. }
  633. /**
  634. * ab8500_btemp_lowmed_handler() - battery temp between low and medium
  635. * @irq: interrupt number
  636. * @_di: void pointer that has to address of ab8500_btemp
  637. *
  638. * Returns IRQ status(IRQ_HANDLED)
  639. */
  640. static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
  641. {
  642. struct ab8500_btemp *di = _di;
  643. dev_dbg(di->dev, "Battery temperature is between low and medium\n");
  644. di->events.btemp_lowmed = true;
  645. di->events.btemp_medhigh = false;
  646. di->events.btemp_high = false;
  647. di->events.btemp_low = false;
  648. power_supply_changed(&di->btemp_psy);
  649. return IRQ_HANDLED;
  650. }
  651. /**
  652. * ab8500_btemp_medhigh_handler() - battery temp between medium and high
  653. * @irq: interrupt number
  654. * @_di: void pointer that has to address of ab8500_btemp
  655. *
  656. * Returns IRQ status(IRQ_HANDLED)
  657. */
  658. static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
  659. {
  660. struct ab8500_btemp *di = _di;
  661. dev_dbg(di->dev, "Battery temperature is between medium and high\n");
  662. di->events.btemp_medhigh = true;
  663. di->events.btemp_lowmed = false;
  664. di->events.btemp_high = false;
  665. di->events.btemp_low = false;
  666. power_supply_changed(&di->btemp_psy);
  667. return IRQ_HANDLED;
  668. }
  669. /**
  670. * ab8500_btemp_periodic() - Periodic temperature measurements
  671. * @di: pointer to the ab8500_btemp structure
  672. * @enable: enable or disable periodic temperature measurements
  673. *
  674. * Starts of stops periodic temperature measurements. Periodic measurements
  675. * should only be done when a charger is connected.
  676. */
  677. static void ab8500_btemp_periodic(struct ab8500_btemp *di,
  678. bool enable)
  679. {
  680. dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
  681. enable);
  682. /*
  683. * Make sure a new measurement is done directly by cancelling
  684. * any pending work
  685. */
  686. cancel_delayed_work_sync(&di->btemp_periodic_work);
  687. if (enable)
  688. queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
  689. }
  690. /**
  691. * ab8500_btemp_get_temp() - get battery temperature
  692. * @di: pointer to the ab8500_btemp structure
  693. *
  694. * Returns battery temperature
  695. */
  696. static int ab8500_btemp_get_temp(struct ab8500_btemp *di)
  697. {
  698. int temp = 0;
  699. /*
  700. * The BTEMP events are not reliabe on AB8500 cut3.3
  701. * and prior versions
  702. */
  703. if (is_ab8500_3p3_or_earlier(di->parent)) {
  704. temp = di->bat_temp * 10;
  705. } else {
  706. if (di->events.btemp_low) {
  707. if (temp > di->btemp_ranges.btemp_low_limit)
  708. temp = di->btemp_ranges.btemp_low_limit * 10;
  709. else
  710. temp = di->bat_temp * 10;
  711. } else if (di->events.btemp_high) {
  712. if (temp < di->btemp_ranges.btemp_high_limit)
  713. temp = di->btemp_ranges.btemp_high_limit * 10;
  714. else
  715. temp = di->bat_temp * 10;
  716. } else if (di->events.btemp_lowmed) {
  717. if (temp > di->btemp_ranges.btemp_med_limit)
  718. temp = di->btemp_ranges.btemp_med_limit * 10;
  719. else
  720. temp = di->bat_temp * 10;
  721. } else if (di->events.btemp_medhigh) {
  722. if (temp < di->btemp_ranges.btemp_med_limit)
  723. temp = di->btemp_ranges.btemp_med_limit * 10;
  724. else
  725. temp = di->bat_temp * 10;
  726. } else
  727. temp = di->bat_temp * 10;
  728. }
  729. return temp;
  730. }
  731. /**
  732. * ab8500_btemp_get_batctrl_temp() - get the temperature
  733. * @btemp: pointer to the btemp structure
  734. *
  735. * Returns the batctrl temperature in millidegrees
  736. */
  737. int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp)
  738. {
  739. return btemp->bat_temp * 1000;
  740. }
  741. /**
  742. * ab8500_btemp_get_property() - get the btemp properties
  743. * @psy: pointer to the power_supply structure
  744. * @psp: pointer to the power_supply_property structure
  745. * @val: pointer to the power_supply_propval union
  746. *
  747. * This function gets called when an application tries to get the btemp
  748. * properties by reading the sysfs files.
  749. * online: presence of the battery
  750. * present: presence of the battery
  751. * technology: battery technology
  752. * temp: battery temperature
  753. * Returns error code in case of failure else 0(on success)
  754. */
  755. static int ab8500_btemp_get_property(struct power_supply *psy,
  756. enum power_supply_property psp,
  757. union power_supply_propval *val)
  758. {
  759. struct ab8500_btemp *di;
  760. di = to_ab8500_btemp_device_info(psy);
  761. switch (psp) {
  762. case POWER_SUPPLY_PROP_PRESENT:
  763. case POWER_SUPPLY_PROP_ONLINE:
  764. if (di->events.batt_rem)
  765. val->intval = 0;
  766. else
  767. val->intval = 1;
  768. break;
  769. case POWER_SUPPLY_PROP_TECHNOLOGY:
  770. val->intval = di->bm->bat_type[di->bm->batt_id].name;
  771. break;
  772. case POWER_SUPPLY_PROP_TEMP:
  773. val->intval = ab8500_btemp_get_temp(di);
  774. break;
  775. default:
  776. return -EINVAL;
  777. }
  778. return 0;
  779. }
  780. static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
  781. {
  782. struct power_supply *psy;
  783. struct power_supply *ext;
  784. struct ab8500_btemp *di;
  785. union power_supply_propval ret;
  786. int i, j;
  787. bool psy_found = false;
  788. psy = (struct power_supply *)data;
  789. ext = dev_get_drvdata(dev);
  790. di = to_ab8500_btemp_device_info(psy);
  791. /*
  792. * For all psy where the name of your driver
  793. * appears in any supplied_to
  794. */
  795. for (i = 0; i < ext->num_supplicants; i++) {
  796. if (!strcmp(ext->supplied_to[i], psy->name))
  797. psy_found = true;
  798. }
  799. if (!psy_found)
  800. return 0;
  801. /* Go through all properties for the psy */
  802. for (j = 0; j < ext->num_properties; j++) {
  803. enum power_supply_property prop;
  804. prop = ext->properties[j];
  805. if (ext->get_property(ext, prop, &ret))
  806. continue;
  807. switch (prop) {
  808. case POWER_SUPPLY_PROP_PRESENT:
  809. switch (ext->type) {
  810. case POWER_SUPPLY_TYPE_MAINS:
  811. /* AC disconnected */
  812. if (!ret.intval && di->events.ac_conn) {
  813. di->events.ac_conn = false;
  814. }
  815. /* AC connected */
  816. else if (ret.intval && !di->events.ac_conn) {
  817. di->events.ac_conn = true;
  818. if (!di->events.usb_conn)
  819. ab8500_btemp_periodic(di, true);
  820. }
  821. break;
  822. case POWER_SUPPLY_TYPE_USB:
  823. /* USB disconnected */
  824. if (!ret.intval && di->events.usb_conn) {
  825. di->events.usb_conn = false;
  826. }
  827. /* USB connected */
  828. else if (ret.intval && !di->events.usb_conn) {
  829. di->events.usb_conn = true;
  830. if (!di->events.ac_conn)
  831. ab8500_btemp_periodic(di, true);
  832. }
  833. break;
  834. default:
  835. break;
  836. }
  837. break;
  838. default:
  839. break;
  840. }
  841. }
  842. return 0;
  843. }
  844. /**
  845. * ab8500_btemp_external_power_changed() - callback for power supply changes
  846. * @psy: pointer to the structure power_supply
  847. *
  848. * This function is pointing to the function pointer external_power_changed
  849. * of the structure power_supply.
  850. * This function gets executed when there is a change in the external power
  851. * supply to the btemp.
  852. */
  853. static void ab8500_btemp_external_power_changed(struct power_supply *psy)
  854. {
  855. struct ab8500_btemp *di = to_ab8500_btemp_device_info(psy);
  856. class_for_each_device(power_supply_class, NULL,
  857. &di->btemp_psy, ab8500_btemp_get_ext_psy_data);
  858. }
  859. /* ab8500 btemp driver interrupts and their respective isr */
  860. static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
  861. {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
  862. {"BTEMP_LOW", ab8500_btemp_templow_handler},
  863. {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
  864. {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
  865. {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
  866. };
  867. #if defined(CONFIG_PM)
  868. static int ab8500_btemp_resume(struct platform_device *pdev)
  869. {
  870. struct ab8500_btemp *di = platform_get_drvdata(pdev);
  871. ab8500_btemp_periodic(di, true);
  872. return 0;
  873. }
  874. static int ab8500_btemp_suspend(struct platform_device *pdev,
  875. pm_message_t state)
  876. {
  877. struct ab8500_btemp *di = platform_get_drvdata(pdev);
  878. ab8500_btemp_periodic(di, false);
  879. return 0;
  880. }
  881. #else
  882. #define ab8500_btemp_suspend NULL
  883. #define ab8500_btemp_resume NULL
  884. #endif
  885. static int ab8500_btemp_remove(struct platform_device *pdev)
  886. {
  887. struct ab8500_btemp *di = platform_get_drvdata(pdev);
  888. int i, irq;
  889. /* Disable interrupts */
  890. for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
  891. irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
  892. free_irq(irq, di);
  893. }
  894. /* Delete the work queue */
  895. destroy_workqueue(di->btemp_wq);
  896. flush_scheduled_work();
  897. power_supply_unregister(&di->btemp_psy);
  898. platform_set_drvdata(pdev, NULL);
  899. return 0;
  900. }
  901. static char *supply_interface[] = {
  902. "ab8500_chargalg",
  903. "ab8500_fg",
  904. };
  905. static int ab8500_btemp_probe(struct platform_device *pdev)
  906. {
  907. struct device_node *np = pdev->dev.of_node;
  908. struct abx500_bm_data *plat = pdev->dev.platform_data;
  909. struct ab8500_btemp *di;
  910. int irq, i, ret = 0;
  911. u8 val;
  912. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  913. if (!di) {
  914. dev_err(&pdev->dev, "%s no mem for ab8500_btemp\n", __func__);
  915. return -ENOMEM;
  916. }
  917. if (!plat) {
  918. dev_err(&pdev->dev, "no battery management data supplied\n");
  919. return -EINVAL;
  920. }
  921. di->bm = plat;
  922. if (np) {
  923. ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
  924. if (ret) {
  925. dev_err(&pdev->dev, "failed to get battery information\n");
  926. return ret;
  927. }
  928. }
  929. /* get parent data */
  930. di->dev = &pdev->dev;
  931. di->parent = dev_get_drvdata(pdev->dev.parent);
  932. di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  933. di->initialized = false;
  934. /* BTEMP supply */
  935. di->btemp_psy.name = "ab8500_btemp";
  936. di->btemp_psy.type = POWER_SUPPLY_TYPE_BATTERY;
  937. di->btemp_psy.properties = ab8500_btemp_props;
  938. di->btemp_psy.num_properties = ARRAY_SIZE(ab8500_btemp_props);
  939. di->btemp_psy.get_property = ab8500_btemp_get_property;
  940. di->btemp_psy.supplied_to = supply_interface;
  941. di->btemp_psy.num_supplicants = ARRAY_SIZE(supply_interface);
  942. di->btemp_psy.external_power_changed =
  943. ab8500_btemp_external_power_changed;
  944. /* Create a work queue for the btemp */
  945. di->btemp_wq =
  946. create_singlethread_workqueue("ab8500_btemp_wq");
  947. if (di->btemp_wq == NULL) {
  948. dev_err(di->dev, "failed to create work queue\n");
  949. return -ENOMEM;
  950. }
  951. /* Init work for measuring temperature periodically */
  952. INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
  953. ab8500_btemp_periodic_work);
  954. /* Set BTEMP thermal limits. Low and Med are fixed */
  955. di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
  956. di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
  957. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  958. AB8500_BTEMP_HIGH_TH, &val);
  959. if (ret < 0) {
  960. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  961. goto free_btemp_wq;
  962. }
  963. switch (val) {
  964. case BTEMP_HIGH_TH_57_0:
  965. case BTEMP_HIGH_TH_57_1:
  966. di->btemp_ranges.btemp_high_limit =
  967. BTEMP_THERMAL_HIGH_LIMIT_57;
  968. break;
  969. case BTEMP_HIGH_TH_52:
  970. di->btemp_ranges.btemp_high_limit =
  971. BTEMP_THERMAL_HIGH_LIMIT_52;
  972. break;
  973. case BTEMP_HIGH_TH_62:
  974. di->btemp_ranges.btemp_high_limit =
  975. BTEMP_THERMAL_HIGH_LIMIT_62;
  976. break;
  977. }
  978. /* Register BTEMP power supply class */
  979. ret = power_supply_register(di->dev, &di->btemp_psy);
  980. if (ret) {
  981. dev_err(di->dev, "failed to register BTEMP psy\n");
  982. goto free_btemp_wq;
  983. }
  984. /* Register interrupts */
  985. for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
  986. irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
  987. ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
  988. IRQF_SHARED | IRQF_NO_SUSPEND,
  989. ab8500_btemp_irq[i].name, di);
  990. if (ret) {
  991. dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
  992. , ab8500_btemp_irq[i].name, irq, ret);
  993. goto free_irq;
  994. }
  995. dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
  996. ab8500_btemp_irq[i].name, irq, ret);
  997. }
  998. platform_set_drvdata(pdev, di);
  999. /* Kick off periodic temperature measurements */
  1000. ab8500_btemp_periodic(di, true);
  1001. list_add_tail(&di->node, &ab8500_btemp_list);
  1002. return ret;
  1003. free_irq:
  1004. power_supply_unregister(&di->btemp_psy);
  1005. /* We also have to free all successfully registered irqs */
  1006. for (i = i - 1; i >= 0; i--) {
  1007. irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
  1008. free_irq(irq, di);
  1009. }
  1010. free_btemp_wq:
  1011. destroy_workqueue(di->btemp_wq);
  1012. return ret;
  1013. }
  1014. static const struct of_device_id ab8500_btemp_match[] = {
  1015. { .compatible = "stericsson,ab8500-btemp", },
  1016. { },
  1017. };
  1018. static struct platform_driver ab8500_btemp_driver = {
  1019. .probe = ab8500_btemp_probe,
  1020. .remove = ab8500_btemp_remove,
  1021. .suspend = ab8500_btemp_suspend,
  1022. .resume = ab8500_btemp_resume,
  1023. .driver = {
  1024. .name = "ab8500-btemp",
  1025. .owner = THIS_MODULE,
  1026. .of_match_table = ab8500_btemp_match,
  1027. },
  1028. };
  1029. static int __init ab8500_btemp_init(void)
  1030. {
  1031. return platform_driver_register(&ab8500_btemp_driver);
  1032. }
  1033. static void __exit ab8500_btemp_exit(void)
  1034. {
  1035. platform_driver_unregister(&ab8500_btemp_driver);
  1036. }
  1037. device_initcall(ab8500_btemp_init);
  1038. module_exit(ab8500_btemp_exit);
  1039. MODULE_LICENSE("GPL v2");
  1040. MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
  1041. MODULE_ALIAS("platform:ab8500-btemp");
  1042. MODULE_DESCRIPTION("AB8500 battery temperature driver");