ab8500_btemp.c 31 KB

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