ti-bandgap.c 40 KB

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