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