ab8500_btemp.c 30 KB

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