ti-bandgap.c 43 KB

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