ti-bandgap.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  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)
  889. goto unfreeze;
  890. /* Set the interval to 1 ms if bandgap counter delay is not set */
  891. if (interval == 0)
  892. interval = 1;
  893. *trend = (t1 - t2) / interval;
  894. dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n",
  895. t1, t2, *trend);
  896. unfreeze:
  897. RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
  898. spin_unlock(&bgp->lock);
  899. exit:
  900. return ret;
  901. }
  902. /**
  903. * ti_bandgap_tshut_init() - setup and initialize tshut handling
  904. * @bgp: pointer to struct ti_bandgap
  905. * @pdev: pointer to device struct platform_device
  906. *
  907. * Call this function only in case the bandgap features HAS(TSHUT).
  908. * In this case, the driver needs to handle the TSHUT signal as an IRQ.
  909. * The IRQ is wired as a GPIO, and for this purpose, it is required
  910. * to specify which GPIO line is used. TSHUT IRQ is fired anytime
  911. * one of the bandgap sensors violates the TSHUT high/hot threshold.
  912. * And in that case, the system must go off.
  913. *
  914. * Return: 0 if no error, else error status
  915. */
  916. static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
  917. struct platform_device *pdev)
  918. {
  919. int gpio_nr = bgp->tshut_gpio;
  920. int status;
  921. /* Request for gpio_86 line */
  922. status = gpio_request(gpio_nr, "tshut");
  923. if (status < 0) {
  924. dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
  925. return status;
  926. }
  927. status = gpio_direction_input(gpio_nr);
  928. if (status) {
  929. dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
  930. return status;
  931. }
  932. status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
  933. IRQF_TRIGGER_RISING, "tshut", NULL);
  934. if (status) {
  935. gpio_free(gpio_nr);
  936. dev_err(bgp->dev, "request irq failed for TSHUT");
  937. }
  938. return 0;
  939. }
  940. /**
  941. * ti_bandgap_alert_init() - setup and initialize talert handling
  942. * @bgp: pointer to struct ti_bandgap
  943. * @pdev: pointer to device struct platform_device
  944. *
  945. * Call this function only in case the bandgap features HAS(TALERT).
  946. * In this case, the driver needs to handle the TALERT signals as an IRQs.
  947. * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold)
  948. * are violated. In these situation, the driver must reprogram the thresholds,
  949. * accordingly to specified policy.
  950. *
  951. * Return: 0 if no error, else return corresponding error.
  952. */
  953. static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
  954. struct platform_device *pdev)
  955. {
  956. int ret;
  957. bgp->irq = platform_get_irq(pdev, 0);
  958. if (bgp->irq < 0) {
  959. dev_err(&pdev->dev, "get_irq failed\n");
  960. return bgp->irq;
  961. }
  962. ret = request_threaded_irq(bgp->irq, NULL,
  963. ti_bandgap_talert_irq_handler,
  964. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  965. "talert", bgp);
  966. if (ret) {
  967. dev_err(&pdev->dev, "Request threaded irq failed.\n");
  968. return ret;
  969. }
  970. return 0;
  971. }
  972. static const struct of_device_id of_ti_bandgap_match[];
  973. /**
  974. * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
  975. * @pdev: pointer to device struct platform_device
  976. *
  977. * Used to read the device tree properties accordingly to the bandgap
  978. * matching version. Based on bandgap version and its capabilities it
  979. * will build a struct ti_bandgap out of the required DT entries.
  980. *
  981. * Return: valid bandgap structure if successful, else returns ERR_PTR
  982. * return value must be verified with IS_ERR.
  983. */
  984. static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
  985. {
  986. struct device_node *node = pdev->dev.of_node;
  987. const struct of_device_id *of_id;
  988. struct ti_bandgap *bgp;
  989. struct resource *res;
  990. int i;
  991. /* just for the sake */
  992. if (!node) {
  993. dev_err(&pdev->dev, "no platform information available\n");
  994. return ERR_PTR(-EINVAL);
  995. }
  996. bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
  997. if (!bgp) {
  998. dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
  999. return ERR_PTR(-ENOMEM);
  1000. }
  1001. of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
  1002. if (of_id)
  1003. bgp->conf = of_id->data;
  1004. /* register shadow for context save and restore */
  1005. bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) *
  1006. bgp->conf->sensor_count, GFP_KERNEL);
  1007. if (!bgp) {
  1008. dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
  1009. return ERR_PTR(-ENOMEM);
  1010. }
  1011. i = 0;
  1012. do {
  1013. void __iomem *chunk;
  1014. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  1015. if (!res)
  1016. break;
  1017. chunk = devm_ioremap_resource(&pdev->dev, res);
  1018. if (i == 0)
  1019. bgp->base = chunk;
  1020. if (IS_ERR(chunk))
  1021. return ERR_CAST(chunk);
  1022. i++;
  1023. } while (res);
  1024. if (TI_BANDGAP_HAS(bgp, TSHUT)) {
  1025. bgp->tshut_gpio = of_get_gpio(node, 0);
  1026. if (!gpio_is_valid(bgp->tshut_gpio)) {
  1027. dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
  1028. bgp->tshut_gpio);
  1029. return ERR_PTR(-EINVAL);
  1030. }
  1031. }
  1032. return bgp;
  1033. }
  1034. /*** Device driver call backs ***/
  1035. static
  1036. int ti_bandgap_probe(struct platform_device *pdev)
  1037. {
  1038. struct ti_bandgap *bgp;
  1039. int clk_rate, ret = 0, i;
  1040. bgp = ti_bandgap_build(pdev);
  1041. if (IS_ERR(bgp)) {
  1042. dev_err(&pdev->dev, "failed to fetch platform data\n");
  1043. return PTR_ERR(bgp);
  1044. }
  1045. bgp->dev = &pdev->dev;
  1046. if (TI_BANDGAP_HAS(bgp, TSHUT)) {
  1047. ret = ti_bandgap_tshut_init(bgp, pdev);
  1048. if (ret) {
  1049. dev_err(&pdev->dev,
  1050. "failed to initialize system tshut IRQ\n");
  1051. return ret;
  1052. }
  1053. }
  1054. bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
  1055. ret = IS_ERR(bgp->fclock);
  1056. if (ret) {
  1057. dev_err(&pdev->dev, "failed to request fclock reference\n");
  1058. ret = PTR_ERR(bgp->fclock);
  1059. goto free_irqs;
  1060. }
  1061. bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
  1062. ret = IS_ERR(bgp->div_clk);
  1063. if (ret) {
  1064. dev_err(&pdev->dev,
  1065. "failed to request div_ts_ck clock ref\n");
  1066. ret = PTR_ERR(bgp->div_clk);
  1067. goto free_irqs;
  1068. }
  1069. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1070. struct temp_sensor_registers *tsr;
  1071. u32 val;
  1072. tsr = bgp->conf->sensors[i].registers;
  1073. /*
  1074. * check if the efuse has a non-zero value if not
  1075. * it is an untrimmed sample and the temperatures
  1076. * may not be accurate
  1077. */
  1078. val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
  1079. if (ret || !val)
  1080. dev_info(&pdev->dev,
  1081. "Non-trimmed BGAP, Temp not accurate\n");
  1082. }
  1083. clk_rate = clk_round_rate(bgp->div_clk,
  1084. bgp->conf->sensors[0].ts_data->max_freq);
  1085. if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq ||
  1086. clk_rate == 0xffffffff) {
  1087. ret = -ENODEV;
  1088. dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
  1089. goto put_clks;
  1090. }
  1091. ret = clk_set_rate(bgp->div_clk, clk_rate);
  1092. if (ret)
  1093. dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
  1094. bgp->clk_rate = clk_rate;
  1095. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1096. clk_prepare_enable(bgp->fclock);
  1097. spin_lock_init(&bgp->lock);
  1098. bgp->dev = &pdev->dev;
  1099. platform_set_drvdata(pdev, bgp);
  1100. ti_bandgap_power(bgp, true);
  1101. /* Set default counter to 1 for now */
  1102. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1103. for (i = 0; i < bgp->conf->sensor_count; i++)
  1104. RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
  1105. /* Set default thresholds for alert and shutdown */
  1106. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1107. struct temp_sensor_data *ts_data;
  1108. ts_data = bgp->conf->sensors[i].ts_data;
  1109. if (TI_BANDGAP_HAS(bgp, TALERT)) {
  1110. /* Set initial Talert thresholds */
  1111. RMW_BITS(bgp, i, bgap_threshold,
  1112. threshold_tcold_mask, ts_data->t_cold);
  1113. RMW_BITS(bgp, i, bgap_threshold,
  1114. threshold_thot_mask, ts_data->t_hot);
  1115. /* Enable the alert events */
  1116. RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
  1117. RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
  1118. }
  1119. if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
  1120. /* Set initial Tshut thresholds */
  1121. RMW_BITS(bgp, i, tshut_threshold,
  1122. tshut_hot_mask, ts_data->tshut_hot);
  1123. RMW_BITS(bgp, i, tshut_threshold,
  1124. tshut_cold_mask, ts_data->tshut_cold);
  1125. }
  1126. }
  1127. if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
  1128. ti_bandgap_set_continuous_mode(bgp);
  1129. /* Set .250 seconds time as default counter */
  1130. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1131. for (i = 0; i < bgp->conf->sensor_count; i++)
  1132. RMW_BITS(bgp, i, bgap_counter, counter_mask,
  1133. bgp->clk_rate / 4);
  1134. /* Every thing is good? Then expose the sensors */
  1135. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1136. char *domain;
  1137. if (bgp->conf->sensors[i].register_cooling) {
  1138. ret = bgp->conf->sensors[i].register_cooling(bgp, i);
  1139. if (ret)
  1140. goto remove_sensors;
  1141. }
  1142. if (bgp->conf->expose_sensor) {
  1143. domain = bgp->conf->sensors[i].domain;
  1144. ret = bgp->conf->expose_sensor(bgp, i, domain);
  1145. if (ret)
  1146. goto remove_last_cooling;
  1147. }
  1148. }
  1149. /*
  1150. * Enable the Interrupts once everything is set. Otherwise irq handler
  1151. * might be called as soon as it is enabled where as rest of framework
  1152. * is still getting initialised.
  1153. */
  1154. if (TI_BANDGAP_HAS(bgp, TALERT)) {
  1155. ret = ti_bandgap_talert_init(bgp, pdev);
  1156. if (ret) {
  1157. dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
  1158. i = bgp->conf->sensor_count;
  1159. goto disable_clk;
  1160. }
  1161. }
  1162. return 0;
  1163. remove_last_cooling:
  1164. if (bgp->conf->sensors[i].unregister_cooling)
  1165. bgp->conf->sensors[i].unregister_cooling(bgp, i);
  1166. remove_sensors:
  1167. for (i--; i >= 0; i--) {
  1168. if (bgp->conf->sensors[i].unregister_cooling)
  1169. bgp->conf->sensors[i].unregister_cooling(bgp, i);
  1170. if (bgp->conf->remove_sensor)
  1171. bgp->conf->remove_sensor(bgp, i);
  1172. }
  1173. ti_bandgap_power(bgp, false);
  1174. disable_clk:
  1175. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1176. clk_disable_unprepare(bgp->fclock);
  1177. put_clks:
  1178. clk_put(bgp->fclock);
  1179. clk_put(bgp->div_clk);
  1180. free_irqs:
  1181. if (TI_BANDGAP_HAS(bgp, TSHUT)) {
  1182. free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
  1183. gpio_free(bgp->tshut_gpio);
  1184. }
  1185. return ret;
  1186. }
  1187. static
  1188. int ti_bandgap_remove(struct platform_device *pdev)
  1189. {
  1190. struct ti_bandgap *bgp = platform_get_drvdata(pdev);
  1191. int i;
  1192. /* First thing is to remove sensor interfaces */
  1193. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1194. if (bgp->conf->sensors[i].unregister_cooling)
  1195. bgp->conf->sensors[i].unregister_cooling(bgp, i);
  1196. if (bgp->conf->remove_sensor)
  1197. bgp->conf->remove_sensor(bgp, i);
  1198. }
  1199. ti_bandgap_power(bgp, false);
  1200. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1201. clk_disable_unprepare(bgp->fclock);
  1202. clk_put(bgp->fclock);
  1203. clk_put(bgp->div_clk);
  1204. if (TI_BANDGAP_HAS(bgp, TALERT))
  1205. free_irq(bgp->irq, bgp);
  1206. if (TI_BANDGAP_HAS(bgp, TSHUT)) {
  1207. free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
  1208. gpio_free(bgp->tshut_gpio);
  1209. }
  1210. return 0;
  1211. }
  1212. #ifdef CONFIG_PM
  1213. static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
  1214. {
  1215. int i;
  1216. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1217. struct temp_sensor_registers *tsr;
  1218. struct temp_sensor_regval *rval;
  1219. rval = &bgp->regval[i];
  1220. tsr = bgp->conf->sensors[i].registers;
  1221. if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
  1222. rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
  1223. tsr->bgap_mode_ctrl);
  1224. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1225. rval->bg_counter = ti_bandgap_readl(bgp,
  1226. tsr->bgap_counter);
  1227. if (TI_BANDGAP_HAS(bgp, TALERT)) {
  1228. rval->bg_threshold = ti_bandgap_readl(bgp,
  1229. tsr->bgap_threshold);
  1230. rval->bg_ctrl = ti_bandgap_readl(bgp,
  1231. tsr->bgap_mask_ctrl);
  1232. }
  1233. if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
  1234. rval->tshut_threshold = ti_bandgap_readl(bgp,
  1235. tsr->tshut_threshold);
  1236. }
  1237. return 0;
  1238. }
  1239. static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
  1240. {
  1241. int i;
  1242. for (i = 0; i < bgp->conf->sensor_count; i++) {
  1243. struct temp_sensor_registers *tsr;
  1244. struct temp_sensor_regval *rval;
  1245. u32 val = 0;
  1246. rval = &bgp->regval[i];
  1247. tsr = bgp->conf->sensors[i].registers;
  1248. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1249. val = ti_bandgap_readl(bgp, tsr->bgap_counter);
  1250. if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
  1251. ti_bandgap_writel(bgp, rval->tshut_threshold,
  1252. tsr->tshut_threshold);
  1253. /* Force immediate temperature measurement and update
  1254. * of the DTEMP field
  1255. */
  1256. ti_bandgap_force_single_read(bgp, i);
  1257. if (TI_BANDGAP_HAS(bgp, COUNTER))
  1258. ti_bandgap_writel(bgp, rval->bg_counter,
  1259. tsr->bgap_counter);
  1260. if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
  1261. ti_bandgap_writel(bgp, rval->bg_mode_ctrl,
  1262. tsr->bgap_mode_ctrl);
  1263. if (TI_BANDGAP_HAS(bgp, TALERT)) {
  1264. ti_bandgap_writel(bgp, rval->bg_threshold,
  1265. tsr->bgap_threshold);
  1266. ti_bandgap_writel(bgp, rval->bg_ctrl,
  1267. tsr->bgap_mask_ctrl);
  1268. }
  1269. }
  1270. return 0;
  1271. }
  1272. static int ti_bandgap_suspend(struct device *dev)
  1273. {
  1274. struct ti_bandgap *bgp = dev_get_drvdata(dev);
  1275. int err;
  1276. err = ti_bandgap_save_ctxt(bgp);
  1277. ti_bandgap_power(bgp, false);
  1278. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1279. clk_disable_unprepare(bgp->fclock);
  1280. return err;
  1281. }
  1282. static int ti_bandgap_resume(struct device *dev)
  1283. {
  1284. struct ti_bandgap *bgp = dev_get_drvdata(dev);
  1285. if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
  1286. clk_prepare_enable(bgp->fclock);
  1287. ti_bandgap_power(bgp, true);
  1288. return ti_bandgap_restore_ctxt(bgp);
  1289. }
  1290. static const struct dev_pm_ops ti_bandgap_dev_pm_ops = {
  1291. SET_SYSTEM_SLEEP_PM_OPS(ti_bandgap_suspend,
  1292. ti_bandgap_resume)
  1293. };
  1294. #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
  1295. #else
  1296. #define DEV_PM_OPS NULL
  1297. #endif
  1298. static const struct of_device_id of_ti_bandgap_match[] = {
  1299. #ifdef CONFIG_OMAP4_THERMAL
  1300. {
  1301. .compatible = "ti,omap4430-bandgap",
  1302. .data = (void *)&omap4430_data,
  1303. },
  1304. {
  1305. .compatible = "ti,omap4460-bandgap",
  1306. .data = (void *)&omap4460_data,
  1307. },
  1308. {
  1309. .compatible = "ti,omap4470-bandgap",
  1310. .data = (void *)&omap4470_data,
  1311. },
  1312. #endif
  1313. #ifdef CONFIG_OMAP5_THERMAL
  1314. {
  1315. .compatible = "ti,omap5430-bandgap",
  1316. .data = (void *)&omap5430_data,
  1317. },
  1318. #endif
  1319. #ifdef CONFIG_DRA752_THERMAL
  1320. {
  1321. .compatible = "ti,dra752-bandgap",
  1322. .data = (void *)&dra752_data,
  1323. },
  1324. #endif
  1325. /* Sentinel */
  1326. { },
  1327. };
  1328. MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
  1329. static struct platform_driver ti_bandgap_sensor_driver = {
  1330. .probe = ti_bandgap_probe,
  1331. .remove = ti_bandgap_remove,
  1332. .driver = {
  1333. .name = "ti-soc-thermal",
  1334. .pm = DEV_PM_OPS,
  1335. .of_match_table = of_ti_bandgap_match,
  1336. },
  1337. };
  1338. module_platform_driver(ti_bandgap_sensor_driver);
  1339. MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
  1340. MODULE_LICENSE("GPL v2");
  1341. MODULE_ALIAS("platform:ti-soc-thermal");
  1342. MODULE_AUTHOR("Texas Instrument Inc.");