ab8500_btemp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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 <[email protected]>
  9. * Karl Komierowski <[email protected]>
  10. * Arun R Murthy <[email protected]>
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/component.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/delay.h>
  18. #include <linux/slab.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/power_supply.h>
  21. #include <linux/completion.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/of.h>
  25. #include <linux/mfd/core.h>
  26. #include <linux/mfd/abx500.h>
  27. #include <linux/mfd/abx500/ab8500.h>
  28. #include <linux/thermal.h>
  29. #include <linux/iio/consumer.h>
  30. #include <linux/fixp-arith.h>
  31. #include "ab8500-bm.h"
  32. #define BTEMP_THERMAL_LOW_LIMIT -10
  33. #define BTEMP_THERMAL_MED_LIMIT 0
  34. #define BTEMP_THERMAL_HIGH_LIMIT_52 52
  35. #define BTEMP_THERMAL_HIGH_LIMIT_57 57
  36. #define BTEMP_THERMAL_HIGH_LIMIT_62 62
  37. #define BTEMP_BATCTRL_CURR_SRC_7UA 7
  38. #define BTEMP_BATCTRL_CURR_SRC_20UA 20
  39. #define BTEMP_BATCTRL_CURR_SRC_16UA 16
  40. #define BTEMP_BATCTRL_CURR_SRC_18UA 18
  41. #define BTEMP_BATCTRL_CURR_SRC_60UA 60
  42. #define BTEMP_BATCTRL_CURR_SRC_120UA 120
  43. /**
  44. * struct ab8500_btemp_interrupts - ab8500 interrupts
  45. * @name: name of the interrupt
  46. * @isr function pointer to the isr
  47. */
  48. struct ab8500_btemp_interrupts {
  49. char *name;
  50. irqreturn_t (*isr)(int irq, void *data);
  51. };
  52. struct ab8500_btemp_events {
  53. bool batt_rem;
  54. bool btemp_high;
  55. bool btemp_medhigh;
  56. bool btemp_lowmed;
  57. bool btemp_low;
  58. bool ac_conn;
  59. bool usb_conn;
  60. };
  61. struct ab8500_btemp_ranges {
  62. int btemp_high_limit;
  63. int btemp_med_limit;
  64. int btemp_low_limit;
  65. };
  66. /**
  67. * struct ab8500_btemp - ab8500 BTEMP device information
  68. * @dev: Pointer to the structure device
  69. * @node: List of AB8500 BTEMPs, hence prepared for reentrance
  70. * @curr_source: What current source we use, in uA
  71. * @bat_temp: Dispatched battery temperature in degree Celsius
  72. * @prev_bat_temp Last measured battery temperature in degree Celsius
  73. * @parent: Pointer to the struct ab8500
  74. * @tz: Thermal zone for the battery
  75. * @adc_bat_ctrl: ADC channel for the battery control
  76. * @fg: Pointer to the struct fg
  77. * @bm: Platform specific battery management information
  78. * @btemp_psy: Structure for BTEMP specific battery properties
  79. * @events: Structure for information about events triggered
  80. * @btemp_ranges: Battery temperature range structure
  81. * @btemp_wq: Work queue for measuring the temperature periodically
  82. * @btemp_periodic_work: Work for measuring the temperature periodically
  83. * @initialized: True if battery id read.
  84. */
  85. struct ab8500_btemp {
  86. struct device *dev;
  87. struct list_head node;
  88. int curr_source;
  89. int bat_temp;
  90. int prev_bat_temp;
  91. struct ab8500 *parent;
  92. struct thermal_zone_device *tz;
  93. struct iio_channel *bat_ctrl;
  94. struct ab8500_fg *fg;
  95. struct ab8500_bm_data *bm;
  96. struct power_supply *btemp_psy;
  97. struct ab8500_btemp_events events;
  98. struct ab8500_btemp_ranges btemp_ranges;
  99. struct workqueue_struct *btemp_wq;
  100. struct delayed_work btemp_periodic_work;
  101. bool initialized;
  102. };
  103. /* BTEMP power supply properties */
  104. static enum power_supply_property ab8500_btemp_props[] = {
  105. POWER_SUPPLY_PROP_PRESENT,
  106. POWER_SUPPLY_PROP_ONLINE,
  107. POWER_SUPPLY_PROP_TEMP,
  108. };
  109. static LIST_HEAD(ab8500_btemp_list);
  110. /**
  111. * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
  112. * @di: pointer to the ab8500_btemp structure
  113. * @v_batctrl: measured batctrl voltage
  114. * @inst_curr: measured instant current
  115. *
  116. * This function returns the battery resistance that is
  117. * derived from the BATCTRL voltage.
  118. * Returns value in Ohms.
  119. */
  120. static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
  121. int v_batctrl, int inst_curr)
  122. {
  123. if (is_ab8500_1p1_or_earlier(di->parent)) {
  124. /*
  125. * For ABB cut1.0 and 1.1 BAT_CTRL is internally
  126. * connected to 1.8V through a 450k resistor
  127. */
  128. return (450000 * (v_batctrl)) / (1800 - v_batctrl);
  129. }
  130. /*
  131. * BAT_CTRL is internally
  132. * connected to 1.8V through a 80k resistor
  133. */
  134. return (80000 * (v_batctrl)) / (1800 - v_batctrl);
  135. }
  136. /**
  137. * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
  138. * @di: pointer to the ab8500_btemp structure
  139. *
  140. * This function returns the voltage on BATCTRL. Returns value in mV.
  141. */
  142. static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
  143. {
  144. int vbtemp, ret;
  145. static int prev;
  146. ret = iio_read_channel_processed(di->bat_ctrl, &vbtemp);
  147. if (ret < 0) {
  148. dev_err(di->dev,
  149. "%s ADC conversion failed, using previous value",
  150. __func__);
  151. return prev;
  152. }
  153. prev = vbtemp;
  154. return vbtemp;
  155. }
  156. /**
  157. * ab8500_btemp_get_batctrl_res() - get battery resistance
  158. * @di: pointer to the ab8500_btemp structure
  159. *
  160. * This function returns the battery pack identification resistance.
  161. * Returns value in Ohms.
  162. */
  163. static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
  164. {
  165. int ret;
  166. int batctrl = 0;
  167. int res;
  168. int inst_curr;
  169. int i;
  170. if (!di->fg)
  171. di->fg = ab8500_fg_get();
  172. if (!di->fg) {
  173. dev_err(di->dev, "No fg found\n");
  174. return -EINVAL;
  175. }
  176. ret = ab8500_fg_inst_curr_start(di->fg);
  177. if (ret) {
  178. dev_err(di->dev, "Failed to start current measurement\n");
  179. return ret;
  180. }
  181. do {
  182. msleep(20);
  183. } while (!ab8500_fg_inst_curr_started(di->fg));
  184. i = 0;
  185. do {
  186. batctrl += ab8500_btemp_read_batctrl_voltage(di);
  187. i++;
  188. msleep(20);
  189. } while (!ab8500_fg_inst_curr_done(di->fg));
  190. batctrl /= i;
  191. ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
  192. if (ret) {
  193. dev_err(di->dev, "Failed to finalize current measurement\n");
  194. return ret;
  195. }
  196. res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
  197. dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
  198. __func__, batctrl, res, inst_curr, i);
  199. return res;
  200. }
  201. /**
  202. * ab8500_btemp_id() - Identify the connected battery
  203. * @di: pointer to the ab8500_btemp structure
  204. *
  205. * This function will try to identify the battery by reading the ID
  206. * resistor. Some brands use a combined ID resistor with a NTC resistor to
  207. * both be able to identify and to read the temperature of it.
  208. */
  209. static int ab8500_btemp_id(struct ab8500_btemp *di)
  210. {
  211. struct power_supply_battery_info *bi = di->bm->bi;
  212. int res;
  213. di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
  214. res = ab8500_btemp_get_batctrl_res(di);
  215. if (res < 0) {
  216. dev_err(di->dev, "%s get batctrl res failed\n", __func__);
  217. return -ENXIO;
  218. }
  219. if (power_supply_battery_bti_in_range(bi, res)) {
  220. dev_info(di->dev, "Battery detected on BATCTRL (pin C3)"
  221. " resistance %d Ohm = %d Ohm +/- %d%%\n",
  222. res, bi->bti_resistance_ohm,
  223. bi->bti_resistance_tolerance);
  224. } else {
  225. dev_warn(di->dev, "Battery identified as unknown"
  226. ", resistance %d Ohm\n", res);
  227. return -ENXIO;
  228. }
  229. return 0;
  230. }
  231. /**
  232. * ab8500_btemp_periodic_work() - Measuring the temperature periodically
  233. * @work: pointer to the work_struct structure
  234. *
  235. * Work function for measuring the temperature periodically
  236. */
  237. static void ab8500_btemp_periodic_work(struct work_struct *work)
  238. {
  239. int interval;
  240. int bat_temp;
  241. struct ab8500_btemp *di = container_of(work,
  242. struct ab8500_btemp, btemp_periodic_work.work);
  243. /* Assume 25 degrees celsius as start temperature */
  244. static int prev = 25;
  245. int ret;
  246. if (!di->initialized) {
  247. /* Identify the battery */
  248. if (ab8500_btemp_id(di) < 0)
  249. dev_warn(di->dev, "failed to identify the battery\n");
  250. }
  251. /* Failover if a reading is erroneous, use last meausurement */
  252. ret = thermal_zone_get_temp(di->tz, &bat_temp);
  253. if (ret) {
  254. dev_err(di->dev, "error reading temperature\n");
  255. bat_temp = prev;
  256. } else {
  257. /* Convert from millicentigrades to centigrades */
  258. bat_temp /= 1000;
  259. prev = bat_temp;
  260. }
  261. /*
  262. * Filter battery temperature.
  263. * Allow direct updates on temperature only if two samples result in
  264. * same temperature. Else only allow 1 degree change from previous
  265. * reported value in the direction of the new measurement.
  266. */
  267. if ((bat_temp == di->prev_bat_temp) || !di->initialized) {
  268. if ((di->bat_temp != di->prev_bat_temp) || !di->initialized) {
  269. di->initialized = true;
  270. di->bat_temp = bat_temp;
  271. power_supply_changed(di->btemp_psy);
  272. }
  273. } else if (bat_temp < di->prev_bat_temp) {
  274. di->bat_temp--;
  275. power_supply_changed(di->btemp_psy);
  276. } else if (bat_temp > di->prev_bat_temp) {
  277. di->bat_temp++;
  278. power_supply_changed(di->btemp_psy);
  279. }
  280. di->prev_bat_temp = bat_temp;
  281. if (di->events.ac_conn || di->events.usb_conn)
  282. interval = di->bm->temp_interval_chg;
  283. else
  284. interval = di->bm->temp_interval_nochg;
  285. /* Schedule a new measurement */
  286. queue_delayed_work(di->btemp_wq,
  287. &di->btemp_periodic_work,
  288. round_jiffies(interval * HZ));
  289. }
  290. /**
  291. * ab8500_btemp_batctrlindb_handler() - battery removal detected
  292. * @irq: interrupt number
  293. * @_di: void pointer that has to address of ab8500_btemp
  294. *
  295. * Returns IRQ status(IRQ_HANDLED)
  296. */
  297. static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
  298. {
  299. struct ab8500_btemp *di = _di;
  300. dev_err(di->dev, "Battery removal detected!\n");
  301. di->events.batt_rem = true;
  302. power_supply_changed(di->btemp_psy);
  303. return IRQ_HANDLED;
  304. }
  305. /**
  306. * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
  307. * @irq: interrupt number
  308. * @_di: void pointer that has to address of ab8500_btemp
  309. *
  310. * Returns IRQ status(IRQ_HANDLED)
  311. */
  312. static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
  313. {
  314. struct ab8500_btemp *di = _di;
  315. if (is_ab8500_3p3_or_earlier(di->parent)) {
  316. dev_dbg(di->dev, "Ignore false btemp low irq"
  317. " for ABB cut 1.0, 1.1, 2.0 and 3.3\n");
  318. } else {
  319. dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
  320. di->events.btemp_low = true;
  321. di->events.btemp_high = false;
  322. di->events.btemp_medhigh = false;
  323. di->events.btemp_lowmed = false;
  324. power_supply_changed(di->btemp_psy);
  325. }
  326. return IRQ_HANDLED;
  327. }
  328. /**
  329. * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
  330. * @irq: interrupt number
  331. * @_di: void pointer that has to address of ab8500_btemp
  332. *
  333. * Returns IRQ status(IRQ_HANDLED)
  334. */
  335. static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
  336. {
  337. struct ab8500_btemp *di = _di;
  338. dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
  339. di->events.btemp_high = true;
  340. di->events.btemp_medhigh = false;
  341. di->events.btemp_lowmed = false;
  342. di->events.btemp_low = false;
  343. power_supply_changed(di->btemp_psy);
  344. return IRQ_HANDLED;
  345. }
  346. /**
  347. * ab8500_btemp_lowmed_handler() - battery temp between low and medium
  348. * @irq: interrupt number
  349. * @_di: void pointer that has to address of ab8500_btemp
  350. *
  351. * Returns IRQ status(IRQ_HANDLED)
  352. */
  353. static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
  354. {
  355. struct ab8500_btemp *di = _di;
  356. dev_dbg(di->dev, "Battery temperature is between low and medium\n");
  357. di->events.btemp_lowmed = true;
  358. di->events.btemp_medhigh = false;
  359. di->events.btemp_high = false;
  360. di->events.btemp_low = false;
  361. power_supply_changed(di->btemp_psy);
  362. return IRQ_HANDLED;
  363. }
  364. /**
  365. * ab8500_btemp_medhigh_handler() - battery temp between medium and high
  366. * @irq: interrupt number
  367. * @_di: void pointer that has to address of ab8500_btemp
  368. *
  369. * Returns IRQ status(IRQ_HANDLED)
  370. */
  371. static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
  372. {
  373. struct ab8500_btemp *di = _di;
  374. dev_dbg(di->dev, "Battery temperature is between medium and high\n");
  375. di->events.btemp_medhigh = true;
  376. di->events.btemp_lowmed = false;
  377. di->events.btemp_high = false;
  378. di->events.btemp_low = false;
  379. power_supply_changed(di->btemp_psy);
  380. return IRQ_HANDLED;
  381. }
  382. /**
  383. * ab8500_btemp_periodic() - Periodic temperature measurements
  384. * @di: pointer to the ab8500_btemp structure
  385. * @enable: enable or disable periodic temperature measurements
  386. *
  387. * Starts of stops periodic temperature measurements. Periodic measurements
  388. * should only be done when a charger is connected.
  389. */
  390. static void ab8500_btemp_periodic(struct ab8500_btemp *di,
  391. bool enable)
  392. {
  393. dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
  394. enable);
  395. /*
  396. * Make sure a new measurement is done directly by cancelling
  397. * any pending work
  398. */
  399. cancel_delayed_work_sync(&di->btemp_periodic_work);
  400. if (enable)
  401. queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
  402. }
  403. /**
  404. * ab8500_btemp_get_temp() - get battery temperature
  405. * @di: pointer to the ab8500_btemp structure
  406. *
  407. * Returns battery temperature
  408. */
  409. static int ab8500_btemp_get_temp(struct ab8500_btemp *di)
  410. {
  411. int temp = 0;
  412. /*
  413. * The BTEMP events are not reliabe on AB8500 cut3.3
  414. * and prior versions
  415. */
  416. if (is_ab8500_3p3_or_earlier(di->parent)) {
  417. temp = di->bat_temp * 10;
  418. } else {
  419. if (di->events.btemp_low) {
  420. if (temp > di->btemp_ranges.btemp_low_limit)
  421. temp = di->btemp_ranges.btemp_low_limit * 10;
  422. else
  423. temp = di->bat_temp * 10;
  424. } else if (di->events.btemp_high) {
  425. if (temp < di->btemp_ranges.btemp_high_limit)
  426. temp = di->btemp_ranges.btemp_high_limit * 10;
  427. else
  428. temp = di->bat_temp * 10;
  429. } else if (di->events.btemp_lowmed) {
  430. if (temp > di->btemp_ranges.btemp_med_limit)
  431. temp = di->btemp_ranges.btemp_med_limit * 10;
  432. else
  433. temp = di->bat_temp * 10;
  434. } else if (di->events.btemp_medhigh) {
  435. if (temp < di->btemp_ranges.btemp_med_limit)
  436. temp = di->btemp_ranges.btemp_med_limit * 10;
  437. else
  438. temp = di->bat_temp * 10;
  439. } else
  440. temp = di->bat_temp * 10;
  441. }
  442. return temp;
  443. }
  444. /**
  445. * ab8500_btemp_get_property() - get the btemp properties
  446. * @psy: pointer to the power_supply structure
  447. * @psp: pointer to the power_supply_property structure
  448. * @val: pointer to the power_supply_propval union
  449. *
  450. * This function gets called when an application tries to get the btemp
  451. * properties by reading the sysfs files.
  452. * online: presence of the battery
  453. * present: presence of the battery
  454. * technology: battery technology
  455. * temp: battery temperature
  456. * Returns error code in case of failure else 0(on success)
  457. */
  458. static int ab8500_btemp_get_property(struct power_supply *psy,
  459. enum power_supply_property psp,
  460. union power_supply_propval *val)
  461. {
  462. struct ab8500_btemp *di = power_supply_get_drvdata(psy);
  463. switch (psp) {
  464. case POWER_SUPPLY_PROP_PRESENT:
  465. case POWER_SUPPLY_PROP_ONLINE:
  466. if (di->events.batt_rem)
  467. val->intval = 0;
  468. else
  469. val->intval = 1;
  470. break;
  471. case POWER_SUPPLY_PROP_TEMP:
  472. val->intval = ab8500_btemp_get_temp(di);
  473. break;
  474. default:
  475. return -EINVAL;
  476. }
  477. return 0;
  478. }
  479. static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
  480. {
  481. struct power_supply *psy;
  482. struct power_supply *ext = dev_get_drvdata(dev);
  483. const char **supplicants = (const char **)ext->supplied_to;
  484. struct ab8500_btemp *di;
  485. union power_supply_propval ret;
  486. int j;
  487. psy = (struct power_supply *)data;
  488. di = power_supply_get_drvdata(psy);
  489. /*
  490. * For all psy where the name of your driver
  491. * appears in any supplied_to
  492. */
  493. j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
  494. if (j < 0)
  495. return 0;
  496. /* Go through all properties for the psy */
  497. for (j = 0; j < ext->desc->num_properties; j++) {
  498. enum power_supply_property prop;
  499. prop = ext->desc->properties[j];
  500. if (power_supply_get_property(ext, prop, &ret))
  501. continue;
  502. switch (prop) {
  503. case POWER_SUPPLY_PROP_PRESENT:
  504. switch (ext->desc->type) {
  505. case POWER_SUPPLY_TYPE_MAINS:
  506. /* AC disconnected */
  507. if (!ret.intval && di->events.ac_conn) {
  508. di->events.ac_conn = false;
  509. }
  510. /* AC connected */
  511. else if (ret.intval && !di->events.ac_conn) {
  512. di->events.ac_conn = true;
  513. if (!di->events.usb_conn)
  514. ab8500_btemp_periodic(di, true);
  515. }
  516. break;
  517. case POWER_SUPPLY_TYPE_USB:
  518. /* USB disconnected */
  519. if (!ret.intval && di->events.usb_conn) {
  520. di->events.usb_conn = false;
  521. }
  522. /* USB connected */
  523. else if (ret.intval && !di->events.usb_conn) {
  524. di->events.usb_conn = true;
  525. if (!di->events.ac_conn)
  526. ab8500_btemp_periodic(di, true);
  527. }
  528. break;
  529. default:
  530. break;
  531. }
  532. break;
  533. default:
  534. break;
  535. }
  536. }
  537. return 0;
  538. }
  539. /**
  540. * ab8500_btemp_external_power_changed() - callback for power supply changes
  541. * @psy: pointer to the structure power_supply
  542. *
  543. * This function is pointing to the function pointer external_power_changed
  544. * of the structure power_supply.
  545. * This function gets executed when there is a change in the external power
  546. * supply to the btemp.
  547. */
  548. static void ab8500_btemp_external_power_changed(struct power_supply *psy)
  549. {
  550. class_for_each_device(power_supply_class, NULL, psy,
  551. ab8500_btemp_get_ext_psy_data);
  552. }
  553. /* ab8500 btemp driver interrupts and their respective isr */
  554. static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
  555. {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
  556. {"BTEMP_LOW", ab8500_btemp_templow_handler},
  557. {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
  558. {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
  559. {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
  560. };
  561. static int __maybe_unused ab8500_btemp_resume(struct device *dev)
  562. {
  563. struct ab8500_btemp *di = dev_get_drvdata(dev);
  564. ab8500_btemp_periodic(di, true);
  565. return 0;
  566. }
  567. static int __maybe_unused ab8500_btemp_suspend(struct device *dev)
  568. {
  569. struct ab8500_btemp *di = dev_get_drvdata(dev);
  570. ab8500_btemp_periodic(di, false);
  571. return 0;
  572. }
  573. static char *supply_interface[] = {
  574. "ab8500_chargalg",
  575. "ab8500_fg",
  576. };
  577. static const struct power_supply_desc ab8500_btemp_desc = {
  578. .name = "ab8500_btemp",
  579. .type = POWER_SUPPLY_TYPE_UNKNOWN,
  580. .properties = ab8500_btemp_props,
  581. .num_properties = ARRAY_SIZE(ab8500_btemp_props),
  582. .get_property = ab8500_btemp_get_property,
  583. .external_power_changed = ab8500_btemp_external_power_changed,
  584. };
  585. static int ab8500_btemp_bind(struct device *dev, struct device *master,
  586. void *data)
  587. {
  588. struct ab8500_btemp *di = dev_get_drvdata(dev);
  589. /* Create a work queue for the btemp */
  590. di->btemp_wq =
  591. alloc_workqueue("ab8500_btemp_wq", WQ_MEM_RECLAIM, 0);
  592. if (di->btemp_wq == NULL) {
  593. dev_err(dev, "failed to create work queue\n");
  594. return -ENOMEM;
  595. }
  596. /* Kick off periodic temperature measurements */
  597. ab8500_btemp_periodic(di, true);
  598. return 0;
  599. }
  600. static void ab8500_btemp_unbind(struct device *dev, struct device *master,
  601. void *data)
  602. {
  603. struct ab8500_btemp *di = dev_get_drvdata(dev);
  604. /* Delete the work queue */
  605. destroy_workqueue(di->btemp_wq);
  606. }
  607. static const struct component_ops ab8500_btemp_component_ops = {
  608. .bind = ab8500_btemp_bind,
  609. .unbind = ab8500_btemp_unbind,
  610. };
  611. static int ab8500_btemp_probe(struct platform_device *pdev)
  612. {
  613. struct power_supply_config psy_cfg = {};
  614. struct device *dev = &pdev->dev;
  615. struct ab8500_btemp *di;
  616. int irq, i, ret = 0;
  617. u8 val;
  618. di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
  619. if (!di)
  620. return -ENOMEM;
  621. di->bm = &ab8500_bm_data;
  622. /* get parent data */
  623. di->dev = dev;
  624. di->parent = dev_get_drvdata(pdev->dev.parent);
  625. /* Get thermal zone and ADC */
  626. di->tz = thermal_zone_get_zone_by_name("battery-thermal");
  627. if (IS_ERR(di->tz)) {
  628. ret = PTR_ERR(di->tz);
  629. /*
  630. * This usually just means we are probing before the thermal
  631. * zone, so just defer.
  632. */
  633. if (ret == -ENODEV)
  634. ret = -EPROBE_DEFER;
  635. return dev_err_probe(dev, ret,
  636. "failed to get battery thermal zone\n");
  637. }
  638. di->bat_ctrl = devm_iio_channel_get(dev, "bat_ctrl");
  639. if (IS_ERR(di->bat_ctrl)) {
  640. ret = dev_err_probe(dev, PTR_ERR(di->bat_ctrl),
  641. "failed to get BAT CTRL ADC channel\n");
  642. return ret;
  643. }
  644. di->initialized = false;
  645. psy_cfg.supplied_to = supply_interface;
  646. psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
  647. psy_cfg.drv_data = di;
  648. /* Init work for measuring temperature periodically */
  649. INIT_DEFERRABLE_WORK(&di->btemp_periodic_work,
  650. ab8500_btemp_periodic_work);
  651. /* Set BTEMP thermal limits. Low and Med are fixed */
  652. di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
  653. di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
  654. ret = abx500_get_register_interruptible(dev, AB8500_CHARGER,
  655. AB8500_BTEMP_HIGH_TH, &val);
  656. if (ret < 0) {
  657. dev_err(dev, "%s ab8500 read failed\n", __func__);
  658. return ret;
  659. }
  660. switch (val) {
  661. case BTEMP_HIGH_TH_57_0:
  662. case BTEMP_HIGH_TH_57_1:
  663. di->btemp_ranges.btemp_high_limit =
  664. BTEMP_THERMAL_HIGH_LIMIT_57;
  665. break;
  666. case BTEMP_HIGH_TH_52:
  667. di->btemp_ranges.btemp_high_limit =
  668. BTEMP_THERMAL_HIGH_LIMIT_52;
  669. break;
  670. case BTEMP_HIGH_TH_62:
  671. di->btemp_ranges.btemp_high_limit =
  672. BTEMP_THERMAL_HIGH_LIMIT_62;
  673. break;
  674. }
  675. /* Register BTEMP power supply class */
  676. di->btemp_psy = devm_power_supply_register(dev, &ab8500_btemp_desc,
  677. &psy_cfg);
  678. if (IS_ERR(di->btemp_psy)) {
  679. dev_err(dev, "failed to register BTEMP psy\n");
  680. return PTR_ERR(di->btemp_psy);
  681. }
  682. /* Register interrupts */
  683. for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
  684. irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
  685. if (irq < 0)
  686. return irq;
  687. ret = devm_request_threaded_irq(dev, irq, NULL,
  688. ab8500_btemp_irq[i].isr,
  689. IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
  690. ab8500_btemp_irq[i].name, di);
  691. if (ret) {
  692. dev_err(dev, "failed to request %s IRQ %d: %d\n"
  693. , ab8500_btemp_irq[i].name, irq, ret);
  694. return ret;
  695. }
  696. dev_dbg(dev, "Requested %s IRQ %d: %d\n",
  697. ab8500_btemp_irq[i].name, irq, ret);
  698. }
  699. platform_set_drvdata(pdev, di);
  700. list_add_tail(&di->node, &ab8500_btemp_list);
  701. return component_add(dev, &ab8500_btemp_component_ops);
  702. }
  703. static int ab8500_btemp_remove(struct platform_device *pdev)
  704. {
  705. component_del(&pdev->dev, &ab8500_btemp_component_ops);
  706. return 0;
  707. }
  708. static SIMPLE_DEV_PM_OPS(ab8500_btemp_pm_ops, ab8500_btemp_suspend, ab8500_btemp_resume);
  709. static const struct of_device_id ab8500_btemp_match[] = {
  710. { .compatible = "stericsson,ab8500-btemp", },
  711. { },
  712. };
  713. MODULE_DEVICE_TABLE(of, ab8500_btemp_match);
  714. struct platform_driver ab8500_btemp_driver = {
  715. .probe = ab8500_btemp_probe,
  716. .remove = ab8500_btemp_remove,
  717. .driver = {
  718. .name = "ab8500-btemp",
  719. .of_match_table = ab8500_btemp_match,
  720. .pm = &ab8500_btemp_pm_ops,
  721. },
  722. };
  723. MODULE_LICENSE("GPL v2");
  724. MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
  725. MODULE_ALIAS("platform:ab8500-btemp");
  726. MODULE_DESCRIPTION("AB8500 battery temperature driver");