ab8500_btemp.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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: Battery temperature in degree Celcius
  70. * @prev_bat_temp Last dispatched battery temperature
  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. struct ab8500_btemp *di = container_of(work,
  540. struct ab8500_btemp, btemp_periodic_work.work);
  541. if (!di->initialized) {
  542. di->initialized = true;
  543. /* Identify the battery */
  544. if (ab8500_btemp_id(di) < 0)
  545. dev_warn(di->dev, "failed to identify the battery\n");
  546. }
  547. di->bat_temp = ab8500_btemp_measure_temp(di);
  548. if (di->bat_temp != di->prev_bat_temp) {
  549. di->prev_bat_temp = di->bat_temp;
  550. power_supply_changed(&di->btemp_psy);
  551. }
  552. if (di->events.ac_conn || di->events.usb_conn)
  553. interval = di->bm->temp_interval_chg;
  554. else
  555. interval = di->bm->temp_interval_nochg;
  556. /* Schedule a new measurement */
  557. queue_delayed_work(di->btemp_wq,
  558. &di->btemp_periodic_work,
  559. round_jiffies(interval * HZ));
  560. }
  561. /**
  562. * ab8500_btemp_batctrlindb_handler() - battery removal detected
  563. * @irq: interrupt number
  564. * @_di: void pointer that has to address of ab8500_btemp
  565. *
  566. * Returns IRQ status(IRQ_HANDLED)
  567. */
  568. static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
  569. {
  570. struct ab8500_btemp *di = _di;
  571. dev_err(di->dev, "Battery removal detected!\n");
  572. di->events.batt_rem = true;
  573. power_supply_changed(&di->btemp_psy);
  574. return IRQ_HANDLED;
  575. }
  576. /**
  577. * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
  578. * @irq: interrupt number
  579. * @_di: void pointer that has to address of ab8500_btemp
  580. *
  581. * Returns IRQ status(IRQ_HANDLED)
  582. */
  583. static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
  584. {
  585. struct ab8500_btemp *di = _di;
  586. if (is_ab8500_3p3_or_earlier(di->parent)) {
  587. dev_dbg(di->dev, "Ignore false btemp low irq"
  588. " for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
  589. } else {
  590. dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
  591. di->events.btemp_low = true;
  592. di->events.btemp_high = false;
  593. di->events.btemp_medhigh = false;
  594. di->events.btemp_lowmed = false;
  595. power_supply_changed(&di->btemp_psy);
  596. }
  597. return IRQ_HANDLED;
  598. }
  599. /**
  600. * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
  601. * @irq: interrupt number
  602. * @_di: void pointer that has to address of ab8500_btemp
  603. *
  604. * Returns IRQ status(IRQ_HANDLED)
  605. */
  606. static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
  607. {
  608. struct ab8500_btemp *di = _di;
  609. dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
  610. di->events.btemp_high = true;
  611. di->events.btemp_medhigh = false;
  612. di->events.btemp_lowmed = false;
  613. di->events.btemp_low = false;
  614. power_supply_changed(&di->btemp_psy);
  615. return IRQ_HANDLED;
  616. }
  617. /**
  618. * ab8500_btemp_lowmed_handler() - battery temp between low and medium
  619. * @irq: interrupt number
  620. * @_di: void pointer that has to address of ab8500_btemp
  621. *
  622. * Returns IRQ status(IRQ_HANDLED)
  623. */
  624. static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
  625. {
  626. struct ab8500_btemp *di = _di;
  627. dev_dbg(di->dev, "Battery temperature is between low and medium\n");
  628. di->events.btemp_lowmed = true;
  629. di->events.btemp_medhigh = false;
  630. di->events.btemp_high = false;
  631. di->events.btemp_low = false;
  632. power_supply_changed(&di->btemp_psy);
  633. return IRQ_HANDLED;
  634. }
  635. /**
  636. * ab8500_btemp_medhigh_handler() - battery temp between medium and high
  637. * @irq: interrupt number
  638. * @_di: void pointer that has to address of ab8500_btemp
  639. *
  640. * Returns IRQ status(IRQ_HANDLED)
  641. */
  642. static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
  643. {
  644. struct ab8500_btemp *di = _di;
  645. dev_dbg(di->dev, "Battery temperature is between medium and high\n");
  646. di->events.btemp_medhigh = true;
  647. di->events.btemp_lowmed = false;
  648. di->events.btemp_high = false;
  649. di->events.btemp_low = false;
  650. power_supply_changed(&di->btemp_psy);
  651. return IRQ_HANDLED;
  652. }
  653. /**
  654. * ab8500_btemp_periodic() - Periodic temperature measurements
  655. * @di: pointer to the ab8500_btemp structure
  656. * @enable: enable or disable periodic temperature measurements
  657. *
  658. * Starts of stops periodic temperature measurements. Periodic measurements
  659. * should only be done when a charger is connected.
  660. */
  661. static void ab8500_btemp_periodic(struct ab8500_btemp *di,
  662. bool enable)
  663. {
  664. dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
  665. enable);
  666. /*
  667. * Make sure a new measurement is done directly by cancelling
  668. * any pending work
  669. */
  670. cancel_delayed_work_sync(&di->btemp_periodic_work);
  671. if (enable)
  672. queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
  673. }
  674. /**
  675. * ab8500_btemp_get_temp() - get battery temperature
  676. * @di: pointer to the ab8500_btemp structure
  677. *
  678. * Returns battery temperature
  679. */
  680. static int ab8500_btemp_get_temp(struct ab8500_btemp *di)
  681. {
  682. int temp = 0;
  683. /*
  684. * The BTEMP events are not reliabe on AB8500 cut3.3
  685. * and prior versions
  686. */
  687. if (is_ab8500_3p3_or_earlier(di->parent)) {
  688. temp = di->bat_temp * 10;
  689. } else {
  690. if (di->events.btemp_low) {
  691. if (temp > di->btemp_ranges.btemp_low_limit)
  692. temp = di->btemp_ranges.btemp_low_limit * 10;
  693. else
  694. temp = di->bat_temp * 10;
  695. } else if (di->events.btemp_high) {
  696. if (temp < di->btemp_ranges.btemp_high_limit)
  697. temp = di->btemp_ranges.btemp_high_limit * 10;
  698. else
  699. temp = di->bat_temp * 10;
  700. } else if (di->events.btemp_lowmed) {
  701. if (temp > di->btemp_ranges.btemp_med_limit)
  702. temp = di->btemp_ranges.btemp_med_limit * 10;
  703. else
  704. temp = di->bat_temp * 10;
  705. } else if (di->events.btemp_medhigh) {
  706. if (temp < di->btemp_ranges.btemp_med_limit)
  707. temp = di->btemp_ranges.btemp_med_limit * 10;
  708. else
  709. temp = di->bat_temp * 10;
  710. } else
  711. temp = di->bat_temp * 10;
  712. }
  713. return temp;
  714. }
  715. /**
  716. * ab8500_btemp_get_batctrl_temp() - get the temperature
  717. * @btemp: pointer to the btemp structure
  718. *
  719. * Returns the batctrl temperature in millidegrees
  720. */
  721. int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp)
  722. {
  723. return btemp->bat_temp * 1000;
  724. }
  725. /**
  726. * ab8500_btemp_get_property() - get the btemp properties
  727. * @psy: pointer to the power_supply structure
  728. * @psp: pointer to the power_supply_property structure
  729. * @val: pointer to the power_supply_propval union
  730. *
  731. * This function gets called when an application tries to get the btemp
  732. * properties by reading the sysfs files.
  733. * online: presence of the battery
  734. * present: presence of the battery
  735. * technology: battery technology
  736. * temp: battery temperature
  737. * Returns error code in case of failure else 0(on success)
  738. */
  739. static int ab8500_btemp_get_property(struct power_supply *psy,
  740. enum power_supply_property psp,
  741. union power_supply_propval *val)
  742. {
  743. struct ab8500_btemp *di;
  744. di = to_ab8500_btemp_device_info(psy);
  745. switch (psp) {
  746. case POWER_SUPPLY_PROP_PRESENT:
  747. case POWER_SUPPLY_PROP_ONLINE:
  748. if (di->events.batt_rem)
  749. val->intval = 0;
  750. else
  751. val->intval = 1;
  752. break;
  753. case POWER_SUPPLY_PROP_TECHNOLOGY:
  754. val->intval = di->bm->bat_type[di->bm->batt_id].name;
  755. break;
  756. case POWER_SUPPLY_PROP_TEMP:
  757. val->intval = ab8500_btemp_get_temp(di);
  758. break;
  759. default:
  760. return -EINVAL;
  761. }
  762. return 0;
  763. }
  764. static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
  765. {
  766. struct power_supply *psy;
  767. struct power_supply *ext;
  768. struct ab8500_btemp *di;
  769. union power_supply_propval ret;
  770. int i, j;
  771. bool psy_found = false;
  772. psy = (struct power_supply *)data;
  773. ext = dev_get_drvdata(dev);
  774. di = to_ab8500_btemp_device_info(psy);
  775. /*
  776. * For all psy where the name of your driver
  777. * appears in any supplied_to
  778. */
  779. for (i = 0; i < ext->num_supplicants; i++) {
  780. if (!strcmp(ext->supplied_to[i], psy->name))
  781. psy_found = true;
  782. }
  783. if (!psy_found)
  784. return 0;
  785. /* Go through all properties for the psy */
  786. for (j = 0; j < ext->num_properties; j++) {
  787. enum power_supply_property prop;
  788. prop = ext->properties[j];
  789. if (ext->get_property(ext, prop, &ret))
  790. continue;
  791. switch (prop) {
  792. case POWER_SUPPLY_PROP_PRESENT:
  793. switch (ext->type) {
  794. case POWER_SUPPLY_TYPE_MAINS:
  795. /* AC disconnected */
  796. if (!ret.intval && di->events.ac_conn) {
  797. di->events.ac_conn = false;
  798. }
  799. /* AC connected */
  800. else if (ret.intval && !di->events.ac_conn) {
  801. di->events.ac_conn = true;
  802. if (!di->events.usb_conn)
  803. ab8500_btemp_periodic(di, true);
  804. }
  805. break;
  806. case POWER_SUPPLY_TYPE_USB:
  807. /* USB disconnected */
  808. if (!ret.intval && di->events.usb_conn) {
  809. di->events.usb_conn = false;
  810. }
  811. /* USB connected */
  812. else if (ret.intval && !di->events.usb_conn) {
  813. di->events.usb_conn = true;
  814. if (!di->events.ac_conn)
  815. ab8500_btemp_periodic(di, true);
  816. }
  817. break;
  818. default:
  819. break;
  820. }
  821. break;
  822. default:
  823. break;
  824. }
  825. }
  826. return 0;
  827. }
  828. /**
  829. * ab8500_btemp_external_power_changed() - callback for power supply changes
  830. * @psy: pointer to the structure power_supply
  831. *
  832. * This function is pointing to the function pointer external_power_changed
  833. * of the structure power_supply.
  834. * This function gets executed when there is a change in the external power
  835. * supply to the btemp.
  836. */
  837. static void ab8500_btemp_external_power_changed(struct power_supply *psy)
  838. {
  839. struct ab8500_btemp *di = to_ab8500_btemp_device_info(psy);
  840. class_for_each_device(power_supply_class, NULL,
  841. &di->btemp_psy, ab8500_btemp_get_ext_psy_data);
  842. }
  843. /* ab8500 btemp driver interrupts and their respective isr */
  844. static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
  845. {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
  846. {"BTEMP_LOW", ab8500_btemp_templow_handler},
  847. {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
  848. {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
  849. {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
  850. };
  851. #if defined(CONFIG_PM)
  852. static int ab8500_btemp_resume(struct platform_device *pdev)
  853. {
  854. struct ab8500_btemp *di = platform_get_drvdata(pdev);
  855. ab8500_btemp_periodic(di, true);
  856. return 0;
  857. }
  858. static int ab8500_btemp_suspend(struct platform_device *pdev,
  859. pm_message_t state)
  860. {
  861. struct ab8500_btemp *di = platform_get_drvdata(pdev);
  862. ab8500_btemp_periodic(di, false);
  863. return 0;
  864. }
  865. #else
  866. #define ab8500_btemp_suspend NULL
  867. #define ab8500_btemp_resume NULL
  868. #endif
  869. static int ab8500_btemp_remove(struct platform_device *pdev)
  870. {
  871. struct ab8500_btemp *di = platform_get_drvdata(pdev);
  872. int i, irq;
  873. /* Disable interrupts */
  874. for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
  875. irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
  876. free_irq(irq, di);
  877. }
  878. /* Delete the work queue */
  879. destroy_workqueue(di->btemp_wq);
  880. flush_scheduled_work();
  881. power_supply_unregister(&di->btemp_psy);
  882. platform_set_drvdata(pdev, NULL);
  883. return 0;
  884. }
  885. static char *supply_interface[] = {
  886. "ab8500_chargalg",
  887. "ab8500_fg",
  888. };
  889. static int ab8500_btemp_probe(struct platform_device *pdev)
  890. {
  891. struct device_node *np = pdev->dev.of_node;
  892. struct abx500_bm_data *plat = pdev->dev.platform_data;
  893. struct ab8500_btemp *di;
  894. int irq, i, ret = 0;
  895. u8 val;
  896. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  897. if (!di) {
  898. dev_err(&pdev->dev, "%s no mem for ab8500_btemp\n", __func__);
  899. return -ENOMEM;
  900. }
  901. if (!plat) {
  902. dev_err(&pdev->dev, "no battery management data supplied\n");
  903. return -EINVAL;
  904. }
  905. di->bm = plat;
  906. if (np) {
  907. ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
  908. if (ret) {
  909. dev_err(&pdev->dev, "failed to get battery information\n");
  910. return ret;
  911. }
  912. }
  913. /* get parent data */
  914. di->dev = &pdev->dev;
  915. di->parent = dev_get_drvdata(pdev->dev.parent);
  916. di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  917. di->initialized = false;
  918. /* BTEMP supply */
  919. di->btemp_psy.name = "ab8500_btemp";
  920. di->btemp_psy.type = POWER_SUPPLY_TYPE_BATTERY;
  921. di->btemp_psy.properties = ab8500_btemp_props;
  922. di->btemp_psy.num_properties = ARRAY_SIZE(ab8500_btemp_props);
  923. di->btemp_psy.get_property = ab8500_btemp_get_property;
  924. di->btemp_psy.supplied_to = supply_interface;
  925. di->btemp_psy.num_supplicants = ARRAY_SIZE(supply_interface);
  926. di->btemp_psy.external_power_changed =
  927. ab8500_btemp_external_power_changed;
  928. /* Create a work queue for the btemp */
  929. di->btemp_wq =
  930. create_singlethread_workqueue("ab8500_btemp_wq");
  931. if (di->btemp_wq == NULL) {
  932. dev_err(di->dev, "failed to create work queue\n");
  933. return -ENOMEM;
  934. }
  935. /* Init work for measuring temperature periodically */
  936. INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
  937. ab8500_btemp_periodic_work);
  938. /* Set BTEMP thermal limits. Low and Med are fixed */
  939. di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
  940. di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
  941. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  942. AB8500_BTEMP_HIGH_TH, &val);
  943. if (ret < 0) {
  944. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  945. goto free_btemp_wq;
  946. }
  947. switch (val) {
  948. case BTEMP_HIGH_TH_57_0:
  949. case BTEMP_HIGH_TH_57_1:
  950. di->btemp_ranges.btemp_high_limit =
  951. BTEMP_THERMAL_HIGH_LIMIT_57;
  952. break;
  953. case BTEMP_HIGH_TH_52:
  954. di->btemp_ranges.btemp_high_limit =
  955. BTEMP_THERMAL_HIGH_LIMIT_52;
  956. break;
  957. case BTEMP_HIGH_TH_62:
  958. di->btemp_ranges.btemp_high_limit =
  959. BTEMP_THERMAL_HIGH_LIMIT_62;
  960. break;
  961. }
  962. /* Register BTEMP power supply class */
  963. ret = power_supply_register(di->dev, &di->btemp_psy);
  964. if (ret) {
  965. dev_err(di->dev, "failed to register BTEMP psy\n");
  966. goto free_btemp_wq;
  967. }
  968. /* Register interrupts */
  969. for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
  970. irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
  971. ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
  972. IRQF_SHARED | IRQF_NO_SUSPEND,
  973. ab8500_btemp_irq[i].name, di);
  974. if (ret) {
  975. dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
  976. , ab8500_btemp_irq[i].name, irq, ret);
  977. goto free_irq;
  978. }
  979. dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
  980. ab8500_btemp_irq[i].name, irq, ret);
  981. }
  982. platform_set_drvdata(pdev, di);
  983. /* Kick off periodic temperature measurements */
  984. ab8500_btemp_periodic(di, true);
  985. list_add_tail(&di->node, &ab8500_btemp_list);
  986. return ret;
  987. free_irq:
  988. power_supply_unregister(&di->btemp_psy);
  989. /* We also have to free all successfully registered irqs */
  990. for (i = i - 1; i >= 0; i--) {
  991. irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
  992. free_irq(irq, di);
  993. }
  994. free_btemp_wq:
  995. destroy_workqueue(di->btemp_wq);
  996. return ret;
  997. }
  998. static const struct of_device_id ab8500_btemp_match[] = {
  999. { .compatible = "stericsson,ab8500-btemp", },
  1000. { },
  1001. };
  1002. static struct platform_driver ab8500_btemp_driver = {
  1003. .probe = ab8500_btemp_probe,
  1004. .remove = ab8500_btemp_remove,
  1005. .suspend = ab8500_btemp_suspend,
  1006. .resume = ab8500_btemp_resume,
  1007. .driver = {
  1008. .name = "ab8500-btemp",
  1009. .owner = THIS_MODULE,
  1010. .of_match_table = ab8500_btemp_match,
  1011. },
  1012. };
  1013. static int __init ab8500_btemp_init(void)
  1014. {
  1015. return platform_driver_register(&ab8500_btemp_driver);
  1016. }
  1017. static void __exit ab8500_btemp_exit(void)
  1018. {
  1019. platform_driver_unregister(&ab8500_btemp_driver);
  1020. }
  1021. device_initcall(ab8500_btemp_init);
  1022. module_exit(ab8500_btemp_exit);
  1023. MODULE_LICENSE("GPL v2");
  1024. MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
  1025. MODULE_ALIAS("platform:ab8500-btemp");
  1026. MODULE_DESCRIPTION("AB8500 battery temperature driver");