ti-bandgap.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. /*
  2. * TI Bandgap temperature sensor driver
  3. *
  4. * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/
  5. * Author: J Keerthy <j-keerthy@ti.com>
  6. * Author: Moiz Sonasath <m-sonasath@ti.com>
  7. * Couple of fixes, DT and MFD adaptation:
  8. * Eduardo Valentin <eduardo.valentin@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/export.h>
  27. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/clk.h>
  31. #include <linux/gpio.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/err.h>
  34. #include <linux/types.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/reboot.h>
  37. #include <linux/of_device.h>
  38. #include <linux/of_platform.h>
  39. #include <linux/of_irq.h>
  40. #include <linux/io.h>
  41. #include "ti-bandgap.h"
  42. /*** Helper functions to access registers and their bitfields ***/
  43. /**
  44. * ti_bandgap_readl() - simple read helper function
  45. * @bgp: pointer to ti_bandgap structure
  46. * @reg: desired register (offset) to be read
  47. *
  48. * Helper function to read bandgap registers. It uses the io remapped area.
  49. * Return: the register value.
  50. */
  51. static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
  52. {
  53. return readl(bgp->base + reg);
  54. }
  55. /**
  56. * ti_bandgap_writel() - simple write helper function
  57. * @bgp: pointer to ti_bandgap structure
  58. * @val: desired register value to be written
  59. * @reg: desired register (offset) to be written
  60. *
  61. * Helper function to write bandgap registers. It uses the io remapped area.
  62. */
  63. static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
  64. {
  65. writel(val, bgp->base + reg);
  66. }
  67. /**
  68. * DOC: macro to update bits.
  69. *
  70. * RMW_BITS() - used to read, modify and update bandgap bitfields.
  71. * The value passed will be shifted.
  72. */
  73. #define RMW_BITS(bgp, id, reg, mask, val) \
  74. do { \
  75. struct temp_sensor_registers *t; \
  76. u32 r; \
  77. \
  78. t = bgp->conf->sensors[(id)].registers; \
  79. r = ti_bandgap_readl(bgp, t->reg); \
  80. r &= ~t->mask; \
  81. r |= (val) << __ffs(t->mask); \
  82. ti_bandgap_writel(bgp, r, t->reg); \
  83. } while (0)
  84. /*** Basic helper functions ***/
  85. /**
  86. * ti_bandgap_power() - controls the power state of a bandgap device
  87. * @bgp: pointer to ti_bandgap structure
  88. * @on: desired power state (1 - on, 0 - off)
  89. *
  90. * Used to power on/off a bandgap device instance. Only used on those
  91. * that features tempsoff bit.
  92. *
  93. * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
  94. */
  95. static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
  96. {
  97. int i, ret = 0;
  98. if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH)) {
  99. ret = -ENOTSUPP;
  100. goto exit;
  101. }
  102. for (i = 0; i < bgp->conf->sensor_count; i++)
  103. /* active on 0 */
  104. RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
  105. exit:
  106. return ret;
  107. }
  108. /**
  109. * ti_bandgap_read_temp() - helper function to read sensor temperature
  110. * @bgp: pointer to ti_bandgap structure
  111. * @id: bandgap sensor id
  112. *
  113. * Function to concentrate the steps to read sensor temperature register.
  114. * This function is desired because, depending on bandgap device version,
  115. * it might be needed to freeze the bandgap state machine, before fetching
  116. * the register value.
  117. *
  118. * Return: temperature in ADC values.
  119. */
  120. static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
  121. {
  122. struct temp_sensor_registers *tsr;
  123. u32 temp, reg;
  124. tsr = bgp->conf->sensors[id].registers;
  125. reg = tsr->temp_sensor_ctrl;
  126. if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
  127. RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
  128. /*
  129. * In case we cannot read from cur_dtemp / dtemp_0,
  130. * then we read from the last valid temp read
  131. */
  132. reg = tsr->ctrl_dtemp_1;
  133. }
  134. /* read temperature */
  135. temp = ti_bandgap_readl(bgp, reg);
  136. temp &= tsr->bgap_dtemp_mask;
  137. if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
  138. RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
  139. return temp;
  140. }
  141. /*** IRQ handlers ***/
  142. /**
  143. * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
  144. * @irq: IRQ number
  145. * @data: private data (struct ti_bandgap *)
  146. *
  147. * This is the Talert handler. Use it only if bandgap device features
  148. * HAS(TALERT). This handler goes over all sensors and checks their
  149. * conditions and acts accordingly. In case there are events pending,
  150. * it will reset the event mask to wait for the opposite event (next event).
  151. * Every time there is a new event, it will be reported to thermal layer.
  152. *
  153. * Return: IRQ_HANDLED
  154. */
  155. static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
  156. {
  157. struct ti_bandgap *bgp = data;
  158. struct temp_sensor_registers *tsr;
  159. u32 t_hot = 0, t_cold = 0, ctrl;
  160. int i;
  161. spin_lock(&bgp->lock);
  162. for (i = 0; i < bgp->conf->sensor_count; i++) {
  163. tsr = bgp->conf->sensors[i].registers;
  164. ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
  165. /* Read the status of t_hot */
  166. t_hot = ctrl & tsr->status_hot_mask;
  167. /* Read the status of t_cold */
  168. t_cold = ctrl & tsr->status_cold_mask;
  169. if (!t_cold && !t_hot)
  170. continue;
  171. ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
  172. /*
  173. * One TALERT interrupt: Two sources
  174. * If the interrupt is due to t_hot then mask t_hot and
  175. * and unmask t_cold else mask t_cold and unmask t_hot
  176. */
  177. if (t_hot) {
  178. ctrl &= ~tsr->mask_hot_mask;
  179. ctrl |= tsr->mask_cold_mask;
  180. } else if (t_cold) {
  181. ctrl &= ~tsr->mask_cold_mask;
  182. ctrl |= tsr->mask_hot_mask;
  183. }
  184. ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
  185. dev_dbg(bgp->dev,
  186. "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
  187. __func__, bgp->conf->sensors[i].domain,
  188. t_hot, t_cold);
  189. /* report temperature to whom may concern */
  190. if (bgp->conf->report_temperature)
  191. bgp->conf->report_temperature(bgp, i);
  192. }
  193. spin_unlock(&bgp->lock);
  194. return IRQ_HANDLED;
  195. }
  196. /**
  197. * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
  198. * @irq: IRQ number
  199. * @data: private data (unused)
  200. *
  201. * This is the Tshut handler. Use it only if bandgap device features
  202. * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown
  203. * the system.
  204. *
  205. * Return: IRQ_HANDLED
  206. */
  207. static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
  208. {
  209. pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
  210. __func__);
  211. orderly_poweroff(true);
  212. return IRQ_HANDLED;
  213. }
  214. /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
  215. /**
  216. * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
  217. * @bgp: struct ti_bandgap pointer
  218. * @adc_val: value in ADC representation
  219. * @t: address where to write the resulting temperature in mCelsius
  220. *
  221. * Simple conversion from ADC representation to mCelsius. In case the ADC value
  222. * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
  223. * The conversion table is indexed by the ADC values.
  224. *
  225. * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val
  226. * argument is out of the ADC conv table range.
  227. */
  228. static
  229. int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
  230. {
  231. const struct ti_bandgap_data *conf = bgp->conf;
  232. int ret = 0;
  233. /* look up for temperature in the table and return the temperature */
  234. if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) {
  235. ret = -ERANGE;
  236. goto exit;
  237. }
  238. *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
  239. exit:
  240. return ret;
  241. }
  242. /**
  243. * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
  244. * @bgp: struct ti_bandgap pointer
  245. * @temp: value in mCelsius
  246. * @adc: address where to write the resulting temperature in ADC representation
  247. *
  248. * Simple conversion from mCelsius to ADC values. In case the temp value
  249. * is out of the ADC conv table range, it returns -ERANGE, 0 on success.
  250. * The conversion table is indexed by the ADC values.
  251. *
  252. * Return: 0 if conversion was successful, else -ERANGE in case the @temp
  253. * argument is out of the ADC conv table range.
  254. */
  255. static
  256. int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
  257. {
  258. const struct ti_bandgap_data *conf = bgp->conf;
  259. const int *conv_table = bgp->conf->conv_table;
  260. int high, low, mid, ret = 0;
  261. low = 0;
  262. high = conf->adc_end_val - conf->adc_start_val;
  263. mid = (high + low) / 2;
  264. if (temp < conv_table[low] || temp > conv_table[high]) {
  265. ret = -ERANGE;
  266. goto exit;
  267. }
  268. while (low < high) {
  269. if (temp < conv_table[mid])
  270. high = mid - 1;
  271. else
  272. low = mid + 1;
  273. mid = (low + high) / 2;
  274. }
  275. *adc = conf->adc_start_val + low;
  276. exit:
  277. return ret;
  278. }
  279. /**
  280. * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
  281. * @bgp: struct ti_bandgap pointer
  282. * @adc_val: temperature value in ADC representation
  283. * @hyst_val: hysteresis value in mCelsius
  284. * @sum: address where to write the resulting temperature (in ADC scale)
  285. *
  286. * Adds an hysteresis value (in mCelsius) to a ADC temperature value.
  287. *
  288. * Return: 0 on success, -ERANGE otherwise.
  289. */
  290. static
  291. int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
  292. u32 *sum)
  293. {
  294. int temp, ret;
  295. /*
  296. * Need to add in the mcelsius domain, so we have a temperature
  297. * the conv_table range
  298. */
  299. ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
  300. if (ret < 0)
  301. goto exit;
  302. temp += hyst_val;
  303. ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
  304. exit:
  305. return ret;
  306. }
  307. /*** Helper functions handling device Alert/Shutdown signals ***/
  308. /**
  309. * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
  310. * @bgp: struct ti_bandgap pointer
  311. * @id: bandgap sensor id
  312. * @t_hot: hot temperature value to trigger alert signal
  313. * @t_cold: cold temperature value to trigger alert signal
  314. *
  315. * Checks the requested t_hot and t_cold values and configures the IRQ event
  316. * masks accordingly. Call this function only if bandgap features HAS(TALERT).
  317. */
  318. static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
  319. u32 t_hot, u32 t_cold)
  320. {
  321. struct temp_sensor_registers *tsr;
  322. u32 temp, reg_val;
  323. /* Read the current on die temperature */
  324. temp = ti_bandgap_read_temp(bgp, id);
  325. tsr = bgp->conf->sensors[id].registers;
  326. reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
  327. if (temp < t_hot)
  328. reg_val |= tsr->mask_hot_mask;
  329. else
  330. reg_val &= ~tsr->mask_hot_mask;
  331. if (t_cold < temp)
  332. reg_val |= tsr->mask_cold_mask;
  333. else
  334. reg_val &= ~tsr->mask_cold_mask;
  335. ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
  336. }
  337. /**
  338. * ti_bandgap_update_alert_threshold() - sequence to update thresholds
  339. * @bgp: struct ti_bandgap pointer
  340. * @id: bandgap sensor id
  341. * @val: value (ADC) of a new threshold
  342. * @hot: desired threshold to be updated. true if threshold hot, false if
  343. * threshold cold
  344. *
  345. * It will program the required thresholds (hot and cold) for TALERT signal.
  346. * This function can be used to update t_hot or t_cold, depending on @hot value.
  347. * It checks the resulting t_hot and t_cold values, based on the new passed @val
  348. * and configures the thresholds so that t_hot is always greater than t_cold.
  349. * Call this function only if bandgap features HAS(TALERT).
  350. *
  351. * Return: 0 if no error, else corresponding error
  352. */
  353. static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
  354. int val, bool hot)
  355. {
  356. struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
  357. struct temp_sensor_registers *tsr;
  358. u32 thresh_val, reg_val, t_hot, t_cold;
  359. int err = 0;
  360. tsr = bgp->conf->sensors[id].registers;
  361. /* obtain the current value */
  362. thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
  363. t_cold = (thresh_val & tsr->threshold_tcold_mask) >>
  364. __ffs(tsr->threshold_tcold_mask);
  365. t_hot = (thresh_val & tsr->threshold_thot_mask) >>
  366. __ffs(tsr->threshold_thot_mask);
  367. if (hot)
  368. t_hot = val;
  369. else
  370. t_cold = val;
  371. if (t_cold > t_hot) {
  372. if (hot)
  373. err = ti_bandgap_add_hyst(bgp, t_hot,
  374. -ts_data->hyst_val,
  375. &t_cold);
  376. else
  377. err = ti_bandgap_add_hyst(bgp, t_cold,
  378. ts_data->hyst_val,
  379. &t_hot);
  380. }
  381. /* write the new threshold values */
  382. reg_val = thresh_val &
  383. ~(tsr->threshold_thot_mask | tsr->threshold_tcold_mask);
  384. reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask)) |
  385. (t_cold << __ffs(tsr->threshold_tcold_mask));
  386. ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
  387. if (err) {
  388. dev_err(bgp->dev, "failed to reprogram thot threshold\n");
  389. err = -EIO;
  390. goto exit;
  391. }
  392. ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
  393. exit:
  394. return err;
  395. }
  396. /**
  397. * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
  398. * @bgp: struct ti_bandgap pointer
  399. * @id: bandgap sensor id
  400. *
  401. * Checks if the bandgap pointer is valid and if the sensor id is also
  402. * applicable.
  403. *
  404. * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
  405. * @id cannot index @bgp sensors.
  406. */
  407. static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
  408. {
  409. int ret = 0;
  410. if (IS_ERR_OR_NULL(bgp)) {
  411. pr_err("%s: invalid bandgap pointer\n", __func__);
  412. ret = -EINVAL;
  413. goto exit;
  414. }
  415. if ((id < 0) || (id >= bgp->conf->sensor_count)) {
  416. dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
  417. __func__, id);
  418. ret = -ERANGE;
  419. }
  420. exit:
  421. return ret;
  422. }
  423. /**
  424. * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
  425. * @bgp: struct ti_bandgap pointer
  426. * @id: bandgap sensor id
  427. * @val: value (mCelsius) of a new threshold
  428. * @hot: desired threshold to be updated. true if threshold hot, false if
  429. * threshold cold
  430. *
  431. * It will update the required thresholds (hot and cold) for TALERT signal.
  432. * This function can be used to update t_hot or t_cold, depending on @hot value.
  433. * Validates the mCelsius range and update the requested threshold.
  434. * Call this function only if bandgap features HAS(TALERT).
  435. *
  436. * Return: 0 if no error, else corresponding error value.
  437. */
  438. static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
  439. bool hot)
  440. {
  441. struct temp_sensor_data *ts_data;
  442. struct temp_sensor_registers *tsr;
  443. u32 adc_val;
  444. int ret;
  445. ret = ti_bandgap_validate(bgp, id);
  446. if (ret)
  447. goto exit;
  448. if (!TI_BANDGAP_HAS(bgp, TALERT)) {
  449. ret = -ENOTSUPP;
  450. goto exit;
  451. }
  452. ts_data = bgp->conf->sensors[id].ts_data;
  453. tsr = bgp->conf->sensors[id].registers;
  454. if (hot) {
  455. if (val < ts_data->min_temp + ts_data->hyst_val)
  456. ret = -EINVAL;
  457. } else {
  458. if (val > ts_data->max_temp + ts_data->hyst_val)
  459. ret = -EINVAL;
  460. }
  461. if (ret)
  462. goto exit;
  463. ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
  464. if (ret < 0)
  465. goto exit;
  466. spin_lock(&bgp->lock);
  467. ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
  468. spin_unlock(&bgp->lock);
  469. exit:
  470. return ret;
  471. }
  472. /**
  473. * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
  474. * @bgp: struct ti_bandgap pointer
  475. * @id: bandgap sensor id
  476. * @val: value (mCelsius) of a threshold
  477. * @hot: desired threshold to be read. true if threshold hot, false if
  478. * threshold cold
  479. *
  480. * It will fetch the required thresholds (hot and cold) for TALERT signal.
  481. * This function can be used to read t_hot or t_cold, depending on @hot value.
  482. * Call this function only if bandgap features HAS(TALERT).
  483. *
  484. * Return: 0 if no error, -ENOTSUPP if it has no TALERT support, or the
  485. * corresponding error value if some operation fails.
  486. */
  487. static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
  488. int *val, bool hot)
  489. {
  490. struct temp_sensor_registers *tsr;
  491. u32 temp, mask;
  492. int ret = 0;
  493. ret = ti_bandgap_validate(bgp, id);
  494. if (ret)
  495. goto exit;
  496. if (!TI_BANDGAP_HAS(bgp, TALERT)) {
  497. ret = -ENOTSUPP;
  498. goto exit;
  499. }
  500. tsr = bgp->conf->sensors[id].registers;
  501. if (hot)
  502. mask = tsr->threshold_thot_mask;
  503. else
  504. mask = tsr->threshold_tcold_mask;
  505. temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
  506. temp = (temp & mask) >> __ffs(mask);
  507. ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
  508. if (ret) {
  509. dev_err(bgp->dev, "failed to read thot\n");
  510. ret = -EIO;
  511. goto exit;
  512. }
  513. *val = temp;
  514. exit:
  515. return ret;
  516. }
  517. /*** Exposed APIs ***/
  518. /**
  519. * ti_bandgap_read_thot() - reads sensor current thot
  520. * @bgp: pointer to bandgap instance
  521. * @id: sensor id
  522. * @thot: resulting current thot value
  523. *
  524. * Return: 0 on success or the proper error code
  525. */
  526. int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
  527. {
  528. return _ti_bandgap_read_threshold(bgp, id, thot, true);
  529. }
  530. /**
  531. * ti_bandgap_write_thot() - sets sensor current thot
  532. * @bgp: pointer to bandgap instance
  533. * @id: sensor id
  534. * @val: desired thot value
  535. *
  536. * Return: 0 on success or the proper error code
  537. */
  538. int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
  539. {
  540. return _ti_bandgap_write_threshold(bgp, id, val, true);
  541. }
  542. /**
  543. * ti_bandgap_read_tcold() - reads sensor current tcold
  544. * @bgp: pointer to bandgap instance
  545. * @id: sensor id
  546. * @tcold: resulting current tcold value
  547. *
  548. * Return: 0 on success or the proper error code
  549. */
  550. int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
  551. {
  552. return _ti_bandgap_read_threshold(bgp, id, tcold, false);
  553. }
  554. /**
  555. * ti_bandgap_write_tcold() - sets the sensor tcold
  556. * @bgp: pointer to bandgap instance
  557. * @id: sensor id
  558. * @val: desired tcold value
  559. *
  560. * Return: 0 on success or the proper error code
  561. */
  562. int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
  563. {
  564. return _ti_bandgap_write_threshold(bgp, id, val, false);
  565. }
  566. /**
  567. * ti_bandgap_read_counter() - read the sensor counter
  568. * @bgp: pointer to bandgap instance
  569. * @id: sensor id
  570. * @interval: resulting update interval in miliseconds
  571. */
  572. static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id,
  573. int *interval)
  574. {
  575. struct temp_sensor_registers *tsr;
  576. int time;
  577. tsr = bgp->conf->sensors[id].registers;
  578. time = ti_bandgap_readl(bgp, tsr->bgap_counter);
  579. time = (time & tsr->counter_mask) >>
  580. __ffs(tsr->counter_mask);
  581. time = time * 1000 / bgp->clk_rate;
  582. *interval = time;
  583. }
  584. /**
  585. * ti_bandgap_read_counter_delay() - read the sensor counter delay
  586. * @bgp: pointer to bandgap instance
  587. * @id: sensor id
  588. * @interval: resulting update interval in miliseconds
  589. */
  590. static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id,
  591. int *interval)
  592. {
  593. struct temp_sensor_registers *tsr;
  594. int reg_val;
  595. tsr = bgp->conf->sensors[id].registers;
  596. reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
  597. reg_val = (reg_val & tsr->mask_counter_delay_mask) >>
  598. __ffs(tsr->mask_counter_delay_mask);
  599. switch (reg_val) {
  600. case 0:
  601. *interval = 0;
  602. break;
  603. case 1:
  604. *interval = 1;
  605. break;
  606. case 2:
  607. *interval = 10;
  608. break;
  609. case 3:
  610. *interval = 100;
  611. break;
  612. case 4:
  613. *interval = 250;
  614. break;
  615. case 5:
  616. *interval = 500;
  617. break;
  618. default:
  619. dev_warn(bgp->dev, "Wrong counter delay value read from register %X",
  620. reg_val);
  621. }
  622. }
  623. /**
  624. * ti_bandgap_read_update_interval() - read the sensor update interval
  625. * @bgp: pointer to bandgap instance
  626. * @id: sensor id
  627. * @interval: resulting update interval in miliseconds
  628. *
  629. * Return: 0 on success or the proper error code
  630. */
  631. int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
  632. int *interval)
  633. {
  634. int ret = 0;
  635. ret = ti_bandgap_validate(bgp, id);
  636. if (ret)
  637. goto exit;
  638. if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
  639. !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
  640. ret = -ENOTSUPP;
  641. goto exit;
  642. }
  643. if (TI_BANDGAP_HAS(bgp, COUNTER)) {
  644. ti_bandgap_read_counter(bgp, id, interval);
  645. goto exit;
  646. }
  647. ti_bandgap_read_counter_delay(bgp, id, interval);
  648. exit:
  649. return ret;
  650. }
  651. /**
  652. * ti_bandgap_write_counter_delay() - set the counter_delay
  653. * @bgp: pointer to bandgap instance
  654. * @id: sensor id
  655. * @interval: desired update interval in miliseconds
  656. *
  657. * Return: 0 on success or the proper error code
  658. */
  659. static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id,
  660. u32 interval)
  661. {
  662. int rval;
  663. switch (interval) {
  664. case 0: /* Immediate conversion */
  665. rval = 0x0;
  666. break;
  667. case 1: /* Conversion after ever 1ms */
  668. rval = 0x1;
  669. break;
  670. case 10: /* Conversion after ever 10ms */
  671. rval = 0x2;
  672. break;
  673. case 100: /* Conversion after ever 100ms */
  674. rval = 0x3;
  675. break;
  676. case 250: /* Conversion after ever 250ms */
  677. rval = 0x4;
  678. break;
  679. case 500: /* Conversion after ever 500ms */
  680. rval = 0x5;
  681. break;
  682. default:
  683. dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval);
  684. return -EINVAL;
  685. }
  686. spin_lock(&bgp->lock);
  687. RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval);
  688. spin_unlock(&bgp->lock);
  689. return 0;
  690. }
  691. /**
  692. * ti_bandgap_write_counter() - set the bandgap sensor counter
  693. * @bgp: pointer to bandgap instance
  694. * @id: sensor id
  695. * @interval: desired update interval in miliseconds
  696. */
  697. static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id,
  698. u32 interval)
  699. {
  700. interval = interval * bgp->clk_rate / 1000;
  701. spin_lock(&bgp->lock);
  702. RMW_BITS(bgp, id, bgap_counter, counter_mask, interval);
  703. spin_unlock(&bgp->lock);
  704. }
  705. /**
  706. * ti_bandgap_write_update_interval() - set the update interval
  707. * @bgp: pointer to bandgap instance
  708. * @id: sensor id
  709. * @interval: desired update interval in miliseconds
  710. *
  711. * Return: 0 on success or the proper error code
  712. */
  713. int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
  714. int id, u32 interval)
  715. {
  716. int ret = ti_bandgap_validate(bgp, id);
  717. if (ret)
  718. goto exit;
  719. if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
  720. !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
  721. ret = -ENOTSUPP;
  722. goto exit;
  723. }
  724. if (TI_BANDGAP_HAS(bgp, COUNTER)) {
  725. ti_bandgap_write_counter(bgp, id, interval);
  726. goto exit;
  727. }
  728. ret = ti_bandgap_write_counter_delay(bgp, id, interval);
  729. exit:
  730. return ret;
  731. }
  732. /**
  733. * ti_bandgap_read_temperature() - report current temperature
  734. * @bgp: pointer to bandgap instance
  735. * @id: sensor id
  736. * @temperature: resulting temperature
  737. *
  738. * Return: 0 on success or the proper error code
  739. */
  740. int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
  741. int *temperature)
  742. {
  743. u32 temp;
  744. int ret;
  745. ret = ti_bandgap_validate(bgp, id);
  746. if (ret)
  747. return ret;
  748. spin_lock(&bgp->lock);
  749. temp = ti_bandgap_read_temp(bgp, id);
  750. spin_unlock(&bgp->lock);
  751. ret |= ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
  752. if (ret)
  753. return -EIO;
  754. *temperature = temp;
  755. return 0;
  756. }
  757. /**
  758. * ti_bandgap_set_sensor_data() - helper function to store thermal
  759. * framework related data.
  760. * @bgp: pointer to bandgap instance
  761. * @id: sensor id
  762. * @data: thermal framework related data to be stored
  763. *
  764. * Return: 0 on success or the proper error code
  765. */
  766. int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
  767. {
  768. int ret = ti_bandgap_validate(bgp, id);
  769. if (ret)
  770. return ret;
  771. bgp->regval[id].data = data;
  772. return 0;
  773. }
  774. /**
  775. * ti_bandgap_get_sensor_data() - helper function to get thermal
  776. * framework related data.
  777. * @bgp: pointer to bandgap instance
  778. * @id: sensor id
  779. *
  780. * Return: data stored by set function with sensor id on success or NULL
  781. */
  782. void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
  783. {
  784. int ret = ti_bandgap_validate(bgp, id);
  785. if (ret)
  786. return ERR_PTR(ret);
  787. return bgp->regval[id].data;
  788. }
  789. /*** Helper functions used during device initialization ***/
  790. /**
  791. * ti_bandgap_force_single_read() - executes 1 single ADC conversion
  792. * @bgp: pointer to struct ti_bandgap
  793. * @id: sensor id which it is desired to read 1 temperature
  794. *
  795. * Used to initialize the conversion state machine and set it to a valid
  796. * state. Called during device initialization and context restore events.
  797. *
  798. * Return: 0
  799. */
  800. static int
  801. ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
  802. {
  803. u32 temp = 0, counter = 1000;
  804. /* Select single conversion mode */
  805. if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
  806. RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
  807. /* Start of Conversion = 1 */
  808. RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
  809. /* Wait until DTEMP is updated */
  810. temp = ti_bandgap_read_temp(bgp, id);
  811. while ((temp == 0) && --counter)
  812. temp = ti_bandgap_read_temp(bgp, id);
  813. /* REVISIT: Check correct condition for end of conversion */
  814. /* Start of Conversion = 0 */
  815. RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
  816. return 0;
  817. }
  818. /**
  819. * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
  820. * @bgp: pointer to struct ti_bandgap
  821. *
  822. * Call this function only if HAS(MODE_CONFIG) is set. As this driver may
  823. * be used for junction temperature monitoring, it is desirable that the
  824. * sensors are operational all the time, so that alerts are generated
  825. * properly.
  826. *
  827. * Return: 0
  828. */
  829. static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
  830. {
  831. int i;
  832. for (i = 0; i < bgp->conf->sensor_count; i++) {
  833. /* Perform a single read just before enabling continuous */
  834. ti_bandgap_force_single_read(bgp, i);
  835. RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
  836. }
  837. return 0;
  838. }
  839. /**
  840. * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor
  841. * @bgp: pointer to struct ti_bandgap
  842. * @id: id of the individual sensor
  843. * @trend: Pointer to trend.
  844. *
  845. * This function needs to be called to fetch the temperature trend of a
  846. * Particular sensor. The function computes the difference in temperature
  847. * w.r.t time. For the bandgaps with built in history buffer the temperatures
  848. * are read from the buffer and for those without the Buffer -ENOTSUPP is
  849. * returned.
  850. *
  851. * Return: 0 if no error, else return corresponding error. If no
  852. * error then the trend value is passed on to trend parameter
  853. */
  854. int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend)
  855. {
  856. struct temp_sensor_registers *tsr;
  857. u32 temp1, temp2, reg1, reg2;
  858. int t1, t2, interval, ret = 0;
  859. ret = ti_bandgap_validate(bgp, id);
  860. if (ret)
  861. goto exit;
  862. if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) ||
  863. !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
  864. ret = -ENOTSUPP;
  865. goto exit;
  866. }
  867. tsr = bgp->conf->sensors[id].registers;
  868. /* Freeze and read the last 2 valid readings */
  869. reg1 = tsr->ctrl_dtemp_1;
  870. reg2 = tsr->ctrl_dtemp_2;
  871. /* read temperature from history buffer */
  872. temp1 = ti_bandgap_readl(bgp, reg1);
  873. temp1 &= tsr->bgap_dtemp_mask;
  874. temp2 = ti_bandgap_readl(bgp, reg2);
  875. temp2 &= tsr->bgap_dtemp_mask;
  876. /* Convert from adc values to mCelsius temperature */
  877. ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1);
  878. if (ret)
  879. goto exit;
  880. ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
  881. if (ret)
  882. goto exit;
  883. /* Fetch the update interval */
  884. ret = ti_bandgap_read_update_interval(bgp, id, &interval);
  885. if (ret || !interval)
  886. goto exit;
  887. *trend = (t1 - t2) / interval;
  888. dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
  889. t1, t2, *trend);
  890. exit:
  891. return ret;
  892. }
  893. /**
  894. * ti_bandgap_tshut_init() - setup and initialize tshut handling
  895. * @bgp: pointer to struct ti_bandgap
  896. * @pdev: pointer to device struct platform_device
  897. *
  898. * Call this function only in case the bandgap features HAS(TSHUT).
  899. * In this case, the driver needs to handle the TSHUT signal as an IRQ.
  900. * The IRQ is wired as a GPIO, and for this purpose, it is required
  901. * to specify which GPIO line is used. TSHUT IRQ is fired anytime
  902. * one of the bandgap sensors violates the TSHUT high/hot threshold.
  903. * And in that case, the system must go off.
  904. *
  905. * Return: 0 if no error, else error status
  906. */
  907. static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
  908. struct platform_device *pdev)
  909. {
  910. int gpio_nr = bgp->tshut_gpio;
  911. int status;
  912. /* Request for gpio_86 line */
  913. status = gpio_request(gpio_nr, "tshut");
  914. if (status < 0) {
  915. dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
  916. return status;
  917. }
  918. status = gpio_direction_input(gpio_nr);
  919. if (status) {
  920. dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
  921. return status;
  922. }
  923. status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
  924. IRQF_TRIGGER_RISING, "tshut", NULL);
  925. if (status) {
  926. gpio_free(gpio_nr);
  927. dev_err(bgp->dev, "request irq failed for TSHUT");
  928. }
  929. return 0;
  930. }
  931. /**
  932. * ti_bandgap_alert_init() - setup and initialize talert handling
  933. * @bgp: pointer to struct ti_bandgap
  934. * @pdev: pointer to device struct platform_device
  935. *
  936. * Call this function only in case the bandgap features HAS(TALERT).
  937. * In this case, the driver needs to handle the TALERT signals as an IRQs.
  938. * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
  939. * are violated. In these situation, the driver must reprogram the thresholds,
  940. * accordingly to specified policy.
  941. *
  942. * Return: 0 if no error, else return corresponding error.
  943. */
  944. static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
  945. struct platform_device *pdev)
  946. {
  947. int ret;
  948. bgp->irq = platform_get_irq(pdev, 0);
  949. if (bgp->irq < 0) {
  950. dev_err(&pdev->dev, "get_irq failed\n");
  951. return bgp->irq;
  952. }
  953. ret = request_threaded_irq(bgp->irq, NULL,
  954. ti_bandgap_talert_irq_handler,
  955. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  956. "talert", bgp);
  957. if (ret) {
  958. dev_err(&pdev->dev, "Request threaded irq failed.\n");
  959. return ret;
  960. }
  961. return 0;
  962. }
  963. static const struct of_device_id of_ti_bandgap_match[];
  964. /**
  965. * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
  966. * @pdev: pointer to device struct platform_device
  967. *
  968. * Used to read the device tree properties accordingly to the bandgap
  969. * matching version. Based on bandgap version and its capabilities it
  970. * will build a struct ti_bandgap out of the required DT entries.
  971. *
  972. * Return: valid bandgap structure if successful, else returns ERR_PTR
  973. * return value must be verified with IS_ERR.
  974. */
  975. static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
  976. {
  977. struct device_node *node = pdev->dev.of_node;
  978. const struct of_device_id *of_id;
  979. struct ti_bandgap *bgp;
  980. struct resource *res;
  981. u32 prop;
  982. int i;
  983. /* just for the sake */
  984. if (!node) {
  985. dev_err(&pdev->dev, "no platform information available\n");
  986. return ERR_PTR(-EINVAL);
  987. }
  988. bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
  989. if (!bgp) {
  990. dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
  991. return ERR_PTR(-ENOMEM);
  992. }
  993. of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
  994. if (of_id)
  995. bgp->conf = of_id->data;
  996. /* register shadow for context save and restore */
  997. bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
  998. bgp->conf->sensor_count, GFP_KERNEL);
  999. if (!bgp) {
  1000. dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
  1001. return ERR_PTR(-ENOMEM);
  1002. }
  1003. i = 0;
  1004. do {
  1005. void __iomem *chunk;
  1006. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  1007. if (!res)
  1008. break;
  1009. chunk = devm_ioremap_resource(&pdev->dev, res);
  1010. if (i == 0)
  1011. bgp->base = chunk;
  1012. if (IS_ERR(chunk))
  1013. return ERR_CAST(chunk);
  1014. i++;
  1015. } while (res);
  1016. if (TI_BANDGAP_HAS(bgp, TSHUT)) {
  1017. if (of_property_read_u32(node, "ti,tshut-gpio", &prop) < 0) {
  1018. dev_err(&pdev->dev, "missing tshut gpio in device tree\n");
  1019. return ERR_PTR(-EINVAL);
  1020. }
  1021. bgp->tshut_gpio = prop;
  1022. if (!gpio_is_valid(bgp->tshut_gpio)) {
  1023. dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
  1024. bgp->tshut_gpio);
  1025. return ERR_PTR(-EINVAL);
  1026. }
  1027. }
  1028. return bgp;
  1029. }
  1030. /*** Device driver call backs ***/
  1031. static
  1032. int ti_bandgap_probe(struct platform_device *pdev)
  1033. {
  1034. struct ti_bandgap *bgp;
  1035. int clk_rate, ret = 0, i;
  1036. bgp = ti_bandgap_build(pdev);
  1037. if (IS_ERR_OR_NULL(bgp)) {
  1038. dev_err(&pdev->dev, "failed to fetch platform data\n");
  1039. return PTR_ERR(bgp);
  1040. }
  1041. bgp->dev = &pdev->dev;
  1042. if (TI_BANDGAP_HAS(bgp, TSHUT)) {
  1043. ret = ti_bandgap_tshut_init(bgp, pdev);
  1044. if (ret) {
  1045. dev_err(&pdev->dev,
  1046. "failed to initialize system tshut IRQ\n");
  1047. return ret;
  1048. }
  1049. }
  1050. bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
  1051. ret = IS_ERR_OR_NULL(bgp->fclock);
  1052. if (ret) {
  1053. dev_err(&pdev->dev, "failed to request fclock reference\n");
  1054. goto free_irqs;
  1055. }
  1056. bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
  1057. ret = IS_ERR_OR_NULL(bgp->div_clk);
  1058. if (ret) {
  1059. dev_err(&pdev->dev,
  1060. "failed to request div_ts_ck clock ref\n");
  1061. goto free_irqs;
  1062. }
  1063. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1064. struct temp_sensor_registers *tsr;
  1065. u32 val;
  1066. tsr = bgp->conf->sensors[i].registers;
  1067. /*
  1068. * check if the efuse has a non-zero value if not
  1069. * it is an untrimmed sample and the temperatures
  1070. * may not be accurate
  1071. */
  1072. val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
  1073. if (ret || !val)
  1074. dev_info(&pdev->dev,
  1075. "Non-trimmed BGAP, Temp not accurate\n");
  1076. }
  1077. clk_rate = clk_round_rate(bgp->div_clk,
  1078. bgp->conf->sensors[0].ts_data->max_freq);
  1079. if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
  1080. clk_rate == 0xffffffff) {
  1081. ret = -ENODEV;
  1082. dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
  1083. goto put_clks;
  1084. }
  1085. ret = clk_set_rate(bgp->div_clk, clk_rate);
  1086. if (ret)
  1087. dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
  1088. bgp->clk_rate = clk_rate;
  1089. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1090. clk_prepare_enable(bgp->fclock);
  1091. spin_lock_init(&bgp->lock);
  1092. bgp->dev = &pdev->dev;
  1093. platform_set_drvdata(pdev, bgp);
  1094. ti_bandgap_power(bgp, true);
  1095. /* Set default counter to 1 for now */
  1096. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1097. for (i = 0; i < bgp->conf->sensor_count; i++)
  1098. RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
  1099. /* Set default thresholds for alert and shutdown */
  1100. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1101. struct temp_sensor_data *ts_data;
  1102. ts_data = bgp->conf->sensors[i].ts_data;
  1103. if (TI_BANDGAP_HAS(bgp, TALERT)) {
  1104. /* Set initial Talert thresholds */
  1105. RMW_BITS(bgp, i, bgap_threshold,
  1106. threshold_tcold_mask, ts_data->t_cold);
  1107. RMW_BITS(bgp, i, bgap_threshold,
  1108. threshold_thot_mask, ts_data->t_hot);
  1109. /* Enable the alert events */
  1110. RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
  1111. RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
  1112. }
  1113. if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
  1114. /* Set initial Tshut thresholds */
  1115. RMW_BITS(bgp, i, tshut_threshold,
  1116. tshut_hot_mask, ts_data->tshut_hot);
  1117. RMW_BITS(bgp, i, tshut_threshold,
  1118. tshut_cold_mask, ts_data->tshut_cold);
  1119. }
  1120. }
  1121. if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
  1122. ti_bandgap_set_continuous_mode(bgp);
  1123. /* Set .250 seconds time as default counter */
  1124. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1125. for (i = 0; i < bgp->conf->sensor_count; i++)
  1126. RMW_BITS(bgp, i, bgap_counter, counter_mask,
  1127. bgp->clk_rate / 4);
  1128. /* Every thing is good? Then expose the sensors */
  1129. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1130. char *domain;
  1131. if (bgp->conf->sensors[i].register_cooling) {
  1132. ret = bgp->conf->sensors[i].register_cooling(bgp, i);
  1133. if (ret)
  1134. goto remove_sensors;
  1135. }
  1136. if (bgp->conf->expose_sensor) {
  1137. domain = bgp->conf->sensors[i].domain;
  1138. ret = bgp->conf->expose_sensor(bgp, i, domain);
  1139. if (ret)
  1140. goto remove_last_cooling;
  1141. }
  1142. }
  1143. /*
  1144. * Enable the Interrupts once everything is set. Otherwise irq handler
  1145. * might be called as soon as it is enabled where as rest of framework
  1146. * is still getting initialised.
  1147. */
  1148. if (TI_BANDGAP_HAS(bgp, TALERT)) {
  1149. ret = ti_bandgap_talert_init(bgp, pdev);
  1150. if (ret) {
  1151. dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
  1152. i = bgp->conf->sensor_count;
  1153. goto disable_clk;
  1154. }
  1155. }
  1156. return 0;
  1157. remove_last_cooling:
  1158. if (bgp->conf->sensors[i].unregister_cooling)
  1159. bgp->conf->sensors[i].unregister_cooling(bgp, i);
  1160. remove_sensors:
  1161. for (i--; i >= 0; i--) {
  1162. if (bgp->conf->sensors[i].unregister_cooling)
  1163. bgp->conf->sensors[i].unregister_cooling(bgp, i);
  1164. if (bgp->conf->remove_sensor)
  1165. bgp->conf->remove_sensor(bgp, i);
  1166. }
  1167. ti_bandgap_power(bgp, false);
  1168. disable_clk:
  1169. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1170. clk_disable_unprepare(bgp->fclock);
  1171. put_clks:
  1172. clk_put(bgp->fclock);
  1173. clk_put(bgp->div_clk);
  1174. free_irqs:
  1175. if (TI_BANDGAP_HAS(bgp, TSHUT)) {
  1176. free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
  1177. gpio_free(bgp->tshut_gpio);
  1178. }
  1179. return ret;
  1180. }
  1181. static
  1182. int ti_bandgap_remove(struct platform_device *pdev)
  1183. {
  1184. struct ti_bandgap *bgp = platform_get_drvdata(pdev);
  1185. int i;
  1186. /* First thing is to remove sensor interfaces */
  1187. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1188. if (bgp->conf->sensors[i].unregister_cooling)
  1189. bgp->conf->sensors[i].unregister_cooling(bgp, i);
  1190. if (bgp->conf->remove_sensor)
  1191. bgp->conf->remove_sensor(bgp, i);
  1192. }
  1193. ti_bandgap_power(bgp, false);
  1194. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1195. clk_disable_unprepare(bgp->fclock);
  1196. clk_put(bgp->fclock);
  1197. clk_put(bgp->div_clk);
  1198. if (TI_BANDGAP_HAS(bgp, TALERT))
  1199. free_irq(bgp->irq, bgp);
  1200. if (TI_BANDGAP_HAS(bgp, TSHUT)) {
  1201. free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
  1202. gpio_free(bgp->tshut_gpio);
  1203. }
  1204. return 0;
  1205. }
  1206. #ifdef CONFIG_PM
  1207. static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
  1208. {
  1209. int i;
  1210. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1211. struct temp_sensor_registers *tsr;
  1212. struct temp_sensor_regval *rval;
  1213. rval = &bgp->regval[i];
  1214. tsr = bgp->conf->sensors[i].registers;
  1215. if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
  1216. rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
  1217. tsr->bgap_mode_ctrl);
  1218. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1219. rval->bg_counter = ti_bandgap_readl(bgp,
  1220. tsr->bgap_counter);
  1221. if (TI_BANDGAP_HAS(bgp, TALERT)) {
  1222. rval->bg_threshold = ti_bandgap_readl(bgp,
  1223. tsr->bgap_threshold);
  1224. rval->bg_ctrl = ti_bandgap_readl(bgp,
  1225. tsr->bgap_mask_ctrl);
  1226. }
  1227. if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
  1228. rval->tshut_threshold = ti_bandgap_readl(bgp,
  1229. tsr->tshut_threshold);
  1230. }
  1231. return 0;
  1232. }
  1233. static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
  1234. {
  1235. int i;
  1236. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1237. struct temp_sensor_registers *tsr;
  1238. struct temp_sensor_regval *rval;
  1239. u32 val = 0;
  1240. rval = &bgp->regval[i];
  1241. tsr = bgp->conf->sensors[i].registers;
  1242. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1243. val = ti_bandgap_readl(bgp, tsr->bgap_counter);
  1244. if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
  1245. ti_bandgap_writel(bgp, rval->tshut_threshold,
  1246. tsr->tshut_threshold);
  1247. /* Force immediate temperature measurement and update
  1248. * of the DTEMP field
  1249. */
  1250. ti_bandgap_force_single_read(bgp, i);
  1251. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1252. ti_bandgap_writel(bgp, rval->bg_counter,
  1253. tsr->bgap_counter);
  1254. if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
  1255. ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
  1256. tsr->bgap_mode_ctrl);
  1257. if (TI_BANDGAP_HAS(bgp, TALERT)) {
  1258. ti_bandgap_writel(bgp, rval->bg_threshold,
  1259. tsr->bgap_threshold);
  1260. ti_bandgap_writel(bgp, rval->bg_ctrl,
  1261. tsr->bgap_mask_ctrl);
  1262. }
  1263. }
  1264. return 0;
  1265. }
  1266. static int ti_bandgap_suspend(struct device *dev)
  1267. {
  1268. struct ti_bandgap *bgp = dev_get_drvdata(dev);
  1269. int err;
  1270. err = ti_bandgap_save_ctxt(bgp);
  1271. ti_bandgap_power(bgp, false);
  1272. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1273. clk_disable_unprepare(bgp->fclock);
  1274. return err;
  1275. }
  1276. static int ti_bandgap_resume(struct device *dev)
  1277. {
  1278. struct ti_bandgap *bgp = dev_get_drvdata(dev);
  1279. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1280. clk_prepare_enable(bgp->fclock);
  1281. ti_bandgap_power(bgp, true);
  1282. return ti_bandgap_restore_ctxt(bgp);
  1283. }
  1284. static const struct dev_pm_ops ti_bandgap_dev_pm_ops = {
  1285. SET_SYSTEM_SLEEP_PM_OPS(ti_bandgap_suspend,
  1286. ti_bandgap_resume)
  1287. };
  1288. #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
  1289. #else
  1290. #define DEV_PM_OPS NULL
  1291. #endif
  1292. static const struct of_device_id of_ti_bandgap_match[] = {
  1293. #ifdef CONFIG_OMAP4_THERMAL
  1294. {
  1295. .compatible = "ti,omap4430-bandgap",
  1296. .data = (void *)&omap4430_data,
  1297. },
  1298. {
  1299. .compatible = "ti,omap4460-bandgap",
  1300. .data = (void *)&omap4460_data,
  1301. },
  1302. {
  1303. .compatible = "ti,omap4470-bandgap",
  1304. .data = (void *)&omap4470_data,
  1305. },
  1306. #endif
  1307. #ifdef CONFIG_OMAP5_THERMAL
  1308. {
  1309. .compatible = "ti,omap5430-bandgap",
  1310. .data = (void *)&omap5430_data,
  1311. },
  1312. #endif
  1313. /* Sentinel */
  1314. { },
  1315. };
  1316. MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
  1317. static struct platform_driver ti_bandgap_sensor_driver = {
  1318. .probe = ti_bandgap_probe,
  1319. .remove = ti_bandgap_remove,
  1320. .driver = {
  1321. .name = "ti-soc-thermal",
  1322. .pm = DEV_PM_OPS,
  1323. .of_match_table = of_ti_bandgap_match,
  1324. },
  1325. };
  1326. module_platform_driver(ti_bandgap_sensor_driver);
  1327. MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
  1328. MODULE_LICENSE("GPL v2");
  1329. MODULE_ALIAS("platform:ti-soc-thermal");
  1330. MODULE_AUTHOR("Texas Instrument Inc.");