ti-bandgap.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  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, 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. if (IS_ERR(bgp->fclock)) {
  1120. dev_err(&pdev->dev, "failed to request fclock reference\n");
  1121. ret = PTR_ERR(bgp->fclock);
  1122. goto free_irqs;
  1123. }
  1124. bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
  1125. if (IS_ERR(bgp->div_clk)) {
  1126. dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
  1127. ret = PTR_ERR(bgp->div_clk);
  1128. goto free_irqs;
  1129. }
  1130. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1131. struct temp_sensor_registers *tsr;
  1132. u32 val;
  1133. tsr = bgp->conf->sensors[i].registers;
  1134. /*
  1135. * check if the efuse has a non-zero value if not
  1136. * it is an untrimmed sample and the temperatures
  1137. * may not be accurate
  1138. */
  1139. val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
  1140. if (!val)
  1141. dev_info(&pdev->dev,
  1142. "Non-trimmed BGAP, Temp not accurate\n");
  1143. }
  1144. clk_rate = clk_round_rate(bgp->div_clk,
  1145. bgp->conf->sensors[0].ts_data->max_freq);
  1146. if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
  1147. clk_rate <= 0) {
  1148. ret = -ENODEV;
  1149. dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
  1150. goto put_clks;
  1151. }
  1152. ret = clk_set_rate(bgp->div_clk, clk_rate);
  1153. if (ret)
  1154. dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
  1155. bgp->clk_rate = clk_rate;
  1156. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1157. clk_prepare_enable(bgp->fclock);
  1158. spin_lock_init(&bgp->lock);
  1159. bgp->dev = &pdev->dev;
  1160. platform_set_drvdata(pdev, bgp);
  1161. ti_bandgap_power(bgp, true);
  1162. /* Set default counter to 1 for now */
  1163. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1164. for (i = 0; i < bgp->conf->sensor_count; i++)
  1165. RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
  1166. /* Set default thresholds for alert and shutdown */
  1167. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1168. struct temp_sensor_data *ts_data;
  1169. ts_data = bgp->conf->sensors[i].ts_data;
  1170. if (TI_BANDGAP_HAS(bgp, TALERT)) {
  1171. /* Set initial Talert thresholds */
  1172. RMW_BITS(bgp, i, bgap_threshold,
  1173. threshold_tcold_mask, ts_data->t_cold);
  1174. RMW_BITS(bgp, i, bgap_threshold,
  1175. threshold_thot_mask, ts_data->t_hot);
  1176. /* Enable the alert events */
  1177. RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
  1178. RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
  1179. }
  1180. if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
  1181. /* Set initial Tshut thresholds */
  1182. RMW_BITS(bgp, i, tshut_threshold,
  1183. tshut_hot_mask, ts_data->tshut_hot);
  1184. RMW_BITS(bgp, i, tshut_threshold,
  1185. tshut_cold_mask, ts_data->tshut_cold);
  1186. }
  1187. }
  1188. if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
  1189. ti_bandgap_set_continuous_mode(bgp);
  1190. /* Set .250 seconds time as default counter */
  1191. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1192. for (i = 0; i < bgp->conf->sensor_count; i++)
  1193. RMW_BITS(bgp, i, bgap_counter, counter_mask,
  1194. bgp->clk_rate / 4);
  1195. /* Every thing is good? Then expose the sensors */
  1196. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1197. char *domain;
  1198. if (bgp->conf->sensors[i].register_cooling) {
  1199. ret = bgp->conf->sensors[i].register_cooling(bgp, i);
  1200. if (ret)
  1201. goto remove_sensors;
  1202. }
  1203. if (bgp->conf->expose_sensor) {
  1204. domain = bgp->conf->sensors[i].domain;
  1205. ret = bgp->conf->expose_sensor(bgp, i, domain);
  1206. if (ret)
  1207. goto remove_last_cooling;
  1208. }
  1209. }
  1210. /*
  1211. * Enable the Interrupts once everything is set. Otherwise irq handler
  1212. * might be called as soon as it is enabled where as rest of framework
  1213. * is still getting initialised.
  1214. */
  1215. if (TI_BANDGAP_HAS(bgp, TALERT)) {
  1216. ret = ti_bandgap_talert_init(bgp, pdev);
  1217. if (ret) {
  1218. dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
  1219. i = bgp->conf->sensor_count;
  1220. goto disable_clk;
  1221. }
  1222. }
  1223. return 0;
  1224. remove_last_cooling:
  1225. if (bgp->conf->sensors[i].unregister_cooling)
  1226. bgp->conf->sensors[i].unregister_cooling(bgp, i);
  1227. remove_sensors:
  1228. for (i--; i >= 0; i--) {
  1229. if (bgp->conf->sensors[i].unregister_cooling)
  1230. bgp->conf->sensors[i].unregister_cooling(bgp, i);
  1231. if (bgp->conf->remove_sensor)
  1232. bgp->conf->remove_sensor(bgp, i);
  1233. }
  1234. ti_bandgap_power(bgp, false);
  1235. disable_clk:
  1236. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1237. clk_disable_unprepare(bgp->fclock);
  1238. put_clks:
  1239. clk_put(bgp->fclock);
  1240. clk_put(bgp->div_clk);
  1241. free_irqs:
  1242. if (TI_BANDGAP_HAS(bgp, TSHUT)) {
  1243. free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
  1244. gpio_free(bgp->tshut_gpio);
  1245. }
  1246. return ret;
  1247. }
  1248. static
  1249. int ti_bandgap_remove(struct platform_device *pdev)
  1250. {
  1251. struct ti_bandgap *bgp = platform_get_drvdata(pdev);
  1252. int i;
  1253. /* First thing is to remove sensor interfaces */
  1254. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1255. if (bgp->conf->sensors[i].unregister_cooling)
  1256. bgp->conf->sensors[i].unregister_cooling(bgp, i);
  1257. if (bgp->conf->remove_sensor)
  1258. bgp->conf->remove_sensor(bgp, i);
  1259. }
  1260. ti_bandgap_power(bgp, false);
  1261. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1262. clk_disable_unprepare(bgp->fclock);
  1263. clk_put(bgp->fclock);
  1264. clk_put(bgp->div_clk);
  1265. if (TI_BANDGAP_HAS(bgp, TALERT))
  1266. free_irq(bgp->irq, bgp);
  1267. if (TI_BANDGAP_HAS(bgp, TSHUT)) {
  1268. free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
  1269. gpio_free(bgp->tshut_gpio);
  1270. }
  1271. return 0;
  1272. }
  1273. #ifdef CONFIG_PM_SLEEP
  1274. static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
  1275. {
  1276. int i;
  1277. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1278. struct temp_sensor_registers *tsr;
  1279. struct temp_sensor_regval *rval;
  1280. rval = &bgp->regval[i];
  1281. tsr = bgp->conf->sensors[i].registers;
  1282. if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
  1283. rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
  1284. tsr->bgap_mode_ctrl);
  1285. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1286. rval->bg_counter = ti_bandgap_readl(bgp,
  1287. tsr->bgap_counter);
  1288. if (TI_BANDGAP_HAS(bgp, TALERT)) {
  1289. rval->bg_threshold = ti_bandgap_readl(bgp,
  1290. tsr->bgap_threshold);
  1291. rval->bg_ctrl = ti_bandgap_readl(bgp,
  1292. tsr->bgap_mask_ctrl);
  1293. }
  1294. if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
  1295. rval->tshut_threshold = ti_bandgap_readl(bgp,
  1296. tsr->tshut_threshold);
  1297. }
  1298. return 0;
  1299. }
  1300. static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
  1301. {
  1302. int i;
  1303. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1304. struct temp_sensor_registers *tsr;
  1305. struct temp_sensor_regval *rval;
  1306. u32 val = 0;
  1307. rval = &bgp->regval[i];
  1308. tsr = bgp->conf->sensors[i].registers;
  1309. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1310. val = ti_bandgap_readl(bgp, tsr->bgap_counter);
  1311. if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
  1312. ti_bandgap_writel(bgp, rval->tshut_threshold,
  1313. tsr->tshut_threshold);
  1314. /* Force immediate temperature measurement and update
  1315. * of the DTEMP field
  1316. */
  1317. ti_bandgap_force_single_read(bgp, i);
  1318. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1319. ti_bandgap_writel(bgp, rval->bg_counter,
  1320. tsr->bgap_counter);
  1321. if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
  1322. ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
  1323. tsr->bgap_mode_ctrl);
  1324. if (TI_BANDGAP_HAS(bgp, TALERT)) {
  1325. ti_bandgap_writel(bgp, rval->bg_threshold,
  1326. tsr->bgap_threshold);
  1327. ti_bandgap_writel(bgp, rval->bg_ctrl,
  1328. tsr->bgap_mask_ctrl);
  1329. }
  1330. }
  1331. return 0;
  1332. }
  1333. static int ti_bandgap_suspend(struct device *dev)
  1334. {
  1335. struct ti_bandgap *bgp = dev_get_drvdata(dev);
  1336. int err;
  1337. err = ti_bandgap_save_ctxt(bgp);
  1338. ti_bandgap_power(bgp, false);
  1339. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1340. clk_disable_unprepare(bgp->fclock);
  1341. return err;
  1342. }
  1343. static int ti_bandgap_resume(struct device *dev)
  1344. {
  1345. struct ti_bandgap *bgp = dev_get_drvdata(dev);
  1346. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1347. clk_prepare_enable(bgp->fclock);
  1348. ti_bandgap_power(bgp, true);
  1349. return ti_bandgap_restore_ctxt(bgp);
  1350. }
  1351. static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
  1352. ti_bandgap_resume);
  1353. #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
  1354. #else
  1355. #define DEV_PM_OPS NULL
  1356. #endif
  1357. static const struct of_device_id of_ti_bandgap_match[] = {
  1358. #ifdef CONFIG_OMAP3_THERMAL
  1359. {
  1360. .compatible = "ti,omap34xx-bandgap",
  1361. .data = (void *)&omap34xx_data,
  1362. },
  1363. {
  1364. .compatible = "ti,omap36xx-bandgap",
  1365. .data = (void *)&omap36xx_data,
  1366. },
  1367. #endif
  1368. #ifdef CONFIG_OMAP4_THERMAL
  1369. {
  1370. .compatible = "ti,omap4430-bandgap",
  1371. .data = (void *)&omap4430_data,
  1372. },
  1373. {
  1374. .compatible = "ti,omap4460-bandgap",
  1375. .data = (void *)&omap4460_data,
  1376. },
  1377. {
  1378. .compatible = "ti,omap4470-bandgap",
  1379. .data = (void *)&omap4470_data,
  1380. },
  1381. #endif
  1382. #ifdef CONFIG_OMAP5_THERMAL
  1383. {
  1384. .compatible = "ti,omap5430-bandgap",
  1385. .data = (void *)&omap5430_data,
  1386. },
  1387. #endif
  1388. #ifdef CONFIG_DRA752_THERMAL
  1389. {
  1390. .compatible = "ti,dra752-bandgap",
  1391. .data = (void *)&dra752_data,
  1392. },
  1393. #endif
  1394. /* Sentinel */
  1395. { },
  1396. };
  1397. MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
  1398. static struct platform_driver ti_bandgap_sensor_driver = {
  1399. .probe = ti_bandgap_probe,
  1400. .remove = ti_bandgap_remove,
  1401. .driver = {
  1402. .name = "ti-soc-thermal",
  1403. .pm = DEV_PM_OPS,
  1404. .of_match_table = of_ti_bandgap_match,
  1405. },
  1406. };
  1407. module_platform_driver(ti_bandgap_sensor_driver);
  1408. MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
  1409. MODULE_LICENSE("GPL v2");
  1410. MODULE_ALIAS("platform:ti-soc-thermal");
  1411. MODULE_AUTHOR("Texas Instrument Inc.");