ti-bandgap.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * OMAP4 Bandgap temperature sensor driver
  3. *
  4. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  5. * Contact:
  6. * Eduardo Valentin <eduardo.valentin@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #ifndef __TI_BANDGAP_H
  24. #define __TI_BANDGAP_H
  25. #include <linux/spinlock.h>
  26. #include <linux/types.h>
  27. #include <linux/err.h>
  28. /**
  29. * DOC: bandgap driver data structure
  30. * ==================================
  31. *
  32. * +----------+----------------+
  33. * | struct temp_sensor_regval |
  34. * +---------------------------+
  35. * * (Array of)
  36. * |
  37. * |
  38. * +-------------------+ +-----------------+
  39. * | struct ti_bandgap |-->| struct device * |
  40. * +----------+--------+ +-----------------+
  41. * |
  42. * |
  43. * V
  44. * +------------------------+
  45. * | struct ti_bandgap_data |
  46. * +------------------------+
  47. * |
  48. * |
  49. * * (Array of)
  50. * +------------+------------------------------------------------------+
  51. * | +----------+------------+ +-------------------------+ |
  52. * | | struct ti_temp_sensor |-->| struct temp_sensor_data | |
  53. * | +-----------------------+ +------------+------------+ |
  54. * | | |
  55. * | + |
  56. * | V |
  57. * | +----------+-------------------+ |
  58. * | | struct temp_sensor_registers | |
  59. * | +------------------------------+ |
  60. * | |
  61. * +-------------------------------------------------------------------+
  62. *
  63. * Above is a simple diagram describing how the data structure below
  64. * are organized. For each bandgap device there should be a ti_bandgap_data
  65. * containing the device instance configuration, as well as, an array of
  66. * sensors, representing every sensor instance present in this bandgap.
  67. */
  68. /**
  69. * struct temp_sensor_registers - descriptor to access registers and bitfields
  70. * @temp_sensor_ctrl: TEMP_SENSOR_CTRL register offset
  71. * @bgap_tempsoff_mask: mask to temp_sensor_ctrl.tempsoff
  72. * @bgap_soc_mask: mask to temp_sensor_ctrl.soc
  73. * @bgap_eocz_mask: mask to temp_sensor_ctrl.eocz
  74. * @bgap_dtemp_mask: mask to temp_sensor_ctrl.dtemp
  75. * @bgap_mask_ctrl: BANDGAP_MASK_CTRL register offset
  76. * @mask_hot_mask: mask to bandgap_mask_ctrl.mask_hot
  77. * @mask_cold_mask: mask to bandgap_mask_ctrl.mask_cold
  78. * @mask_sidlemode_mask: mask to bandgap_mask_ctrl.mask_sidlemode
  79. * @mask_counter_delay_mask: mask to bandgap_mask_ctrl.mask_counter_delay
  80. * @mask_freeze_mask: mask to bandgap_mask_ctrl.mask_free
  81. * @mask_clear_mask: mask to bandgap_mask_ctrl.mask_clear
  82. * @mask_clear_accum_mask: mask to bandgap_mask_ctrl.mask_clear_accum
  83. * @bgap_mode_ctrl: BANDGAP_MODE_CTRL register offset
  84. * @mode_ctrl_mask: mask to bandgap_mode_ctrl.mode_ctrl
  85. * @bgap_counter: BANDGAP_COUNTER register offset
  86. * @counter_mask: mask to bandgap_counter.counter
  87. * @bgap_threshold: BANDGAP_THRESHOLD register offset (TALERT thresholds)
  88. * @threshold_thot_mask: mask to bandgap_threhold.thot
  89. * @threshold_tcold_mask: mask to bandgap_threhold.tcold
  90. * @tshut_threshold: TSHUT_THRESHOLD register offset (TSHUT thresholds)
  91. * @tshut_efuse_mask: mask to tshut_threshold.tshut_efuse
  92. * @tshut_efuse_shift: shift to tshut_threshold.tshut_efuse
  93. * @tshut_hot_mask: mask to tshut_threhold.thot
  94. * @tshut_cold_mask: mask to tshut_threhold.thot
  95. * @bgap_status: BANDGAP_STATUS register offset
  96. * @status_clean_stop_mask: mask to bandgap_status.clean_stop
  97. * @status_bgap_alert_mask: mask to bandgap_status.bandgap_alert
  98. * @status_hot_mask: mask to bandgap_status.hot
  99. * @status_cold_mask: mask to bandgap_status.cold
  100. * @bgap_cumul_dtemp: BANDGAP_CUMUL_DTEMP register offset
  101. * @ctrl_dtemp_0: CTRL_DTEMP0 register offset
  102. * @ctrl_dtemp_1: CTRL_DTEMP1 register offset
  103. * @ctrl_dtemp_2: CTRL_DTEMP2 register offset
  104. * @ctrl_dtemp_3: CTRL_DTEMP3 register offset
  105. * @ctrl_dtemp_4: CTRL_DTEMP4 register offset
  106. * @bgap_efuse: BANDGAP_EFUSE register offset
  107. *
  108. * The register offsets and bitfields might change across
  109. * OMAP and variants versions. Hence this struct serves as a
  110. * descriptor map on how to access the registers and the bitfields.
  111. *
  112. * This descriptor contains registers of all versions of bandgap chips.
  113. * Not all versions will use all registers, depending on the available
  114. * features. Please read TRMs for descriptive explanation on each bitfield.
  115. */
  116. struct temp_sensor_registers {
  117. u32 temp_sensor_ctrl;
  118. u32 bgap_tempsoff_mask;
  119. u32 bgap_soc_mask;
  120. u32 bgap_eocz_mask; /* not used: but needs revisit */
  121. u32 bgap_dtemp_mask;
  122. u32 bgap_mask_ctrl;
  123. u32 mask_hot_mask;
  124. u32 mask_cold_mask;
  125. u32 mask_sidlemode_mask; /* not used: but may be needed for pm */
  126. u32 mask_counter_delay_mask;
  127. u32 mask_freeze_mask;
  128. u32 mask_clear_mask; /* not used: but needed for trending */
  129. u32 mask_clear_accum_mask; /* not used: but needed for trending */
  130. u32 bgap_mode_ctrl;
  131. u32 mode_ctrl_mask;
  132. u32 bgap_counter;
  133. u32 counter_mask;
  134. u32 bgap_threshold;
  135. u32 threshold_thot_mask;
  136. u32 threshold_tcold_mask;
  137. u32 tshut_threshold;
  138. u32 tshut_efuse_mask; /* not used */
  139. u32 tshut_efuse_shift; /* not used */
  140. u32 tshut_hot_mask;
  141. u32 tshut_cold_mask;
  142. u32 bgap_status;
  143. u32 status_clean_stop_mask; /* not used: but needed for trending */
  144. u32 status_bgap_alert_mask; /* not used */
  145. u32 status_hot_mask;
  146. u32 status_cold_mask;
  147. u32 bgap_cumul_dtemp; /* not used: but needed for trending */
  148. u32 ctrl_dtemp_0; /* not used: but needed for trending */
  149. u32 ctrl_dtemp_1; /* not used: but needed for trending */
  150. u32 ctrl_dtemp_2; /* not used: but needed for trending */
  151. u32 ctrl_dtemp_3; /* not used: but needed for trending */
  152. u32 ctrl_dtemp_4; /* not used: but needed for trending */
  153. u32 bgap_efuse;
  154. };
  155. /**
  156. * struct temp_sensor_data - The thresholds and limits for temperature sensors.
  157. * @tshut_hot: temperature to trigger a thermal reset (initial value)
  158. * @tshut_cold: temp to get the plat out of reset due to thermal (init val)
  159. * @t_hot: temperature to trigger a thermal alert (high initial value)
  160. * @t_cold: temperature to trigger a thermal alert (low initial value)
  161. * @min_freq: sensor minimum clock rate
  162. * @max_freq: sensor maximum clock rate
  163. * @max_temp: sensor maximum temperature
  164. * @min_temp: sensor minimum temperature
  165. * @hyst_val: temperature hysteresis considered while converting ADC values
  166. * @update_int1: update interval
  167. * @update_int2: update interval
  168. *
  169. * This data structure will hold the required thresholds and temperature limits
  170. * for a specific temperature sensor, like shutdown temperature, alert
  171. * temperature, clock / rate used, ADC conversion limits and update intervals
  172. */
  173. struct temp_sensor_data {
  174. u32 tshut_hot;
  175. u32 tshut_cold;
  176. u32 t_hot;
  177. u32 t_cold;
  178. u32 min_freq;
  179. u32 max_freq;
  180. int max_temp;
  181. int min_temp;
  182. int hyst_val;
  183. u32 update_int1; /* not used */
  184. u32 update_int2; /* not used */
  185. };
  186. struct ti_bandgap_data;
  187. /**
  188. * struct temp_sensor_regval - temperature sensor register values and priv data
  189. * @bg_mode_ctrl: temp sensor control register value
  190. * @bg_ctrl: bandgap ctrl register value
  191. * @bg_counter: bandgap counter value
  192. * @bg_threshold: bandgap threshold register value
  193. * @tshut_threshold: bandgap tshut register value
  194. * @data: private data
  195. *
  196. * Data structure to save and restore bandgap register set context. Only
  197. * required registers are shadowed, when needed.
  198. */
  199. struct temp_sensor_regval {
  200. u32 bg_mode_ctrl;
  201. u32 bg_ctrl;
  202. u32 bg_counter;
  203. u32 bg_threshold;
  204. u32 tshut_threshold;
  205. void *data;
  206. };
  207. /**
  208. * struct ti_bandgap - bandgap device structure
  209. * @dev: struct device pointer
  210. * @base: io memory base address
  211. * @conf: struct with bandgap configuration set (# sensors, conv_table, etc)
  212. * @regval: temperature sensor register values
  213. * @fclock: pointer to functional clock of temperature sensor
  214. * @div_clk: pointer to divider clock of temperature sensor fclk
  215. * @lock: spinlock for ti_bandgap structure
  216. * @irq: MPU IRQ number for thermal alert
  217. * @tshut_gpio: GPIO where Tshut signal is routed
  218. * @clk_rate: Holds current clock rate
  219. *
  220. * The bandgap device structure representing the bandgap device instance.
  221. * It holds most of the dynamic stuff. Configurations and sensor specific
  222. * entries are inside the @conf structure.
  223. */
  224. struct ti_bandgap {
  225. struct device *dev;
  226. void __iomem *base;
  227. const struct ti_bandgap_data *conf;
  228. struct temp_sensor_regval *regval;
  229. struct clk *fclock;
  230. struct clk *div_clk;
  231. spinlock_t lock; /* shields this struct */
  232. int irq;
  233. int tshut_gpio;
  234. u32 clk_rate;
  235. };
  236. /**
  237. * struct ti_temp_sensor - bandgap temperature sensor configuration data
  238. * @ts_data: pointer to struct with thresholds, limits of temperature sensor
  239. * @registers: pointer to the list of register offsets and bitfields
  240. * @domain: the name of the domain where the sensor is located
  241. * @slope: sensor gradient slope info for hotspot extrapolation equation
  242. * @constant: sensor gradient const info for hotspot extrapolation equation
  243. * @slope_pcb: sensor gradient slope info for hotspot extrapolation equation
  244. * with no external influence
  245. * @constant_pcb: sensor gradient const info for hotspot extrapolation equation
  246. * with no external influence
  247. * @register_cooling: function to describe how this sensor is going to be cooled
  248. * @unregister_cooling: function to release cooling data
  249. *
  250. * Data structure to describe a temperature sensor handled by a bandgap device.
  251. * It should provide configuration details on this sensor, such as how to
  252. * access the registers affecting this sensor, shadow register buffer, how to
  253. * assess the gradient from hotspot, how to cooldown the domain when sensor
  254. * reports too hot temperature.
  255. */
  256. struct ti_temp_sensor {
  257. struct temp_sensor_data *ts_data;
  258. struct temp_sensor_registers *registers;
  259. char *domain;
  260. /* for hotspot extrapolation */
  261. const int slope;
  262. const int constant;
  263. const int slope_pcb;
  264. const int constant_pcb;
  265. int (*register_cooling)(struct ti_bandgap *bgp, int id);
  266. int (*unregister_cooling)(struct ti_bandgap *bgp, int id);
  267. };
  268. /**
  269. * DOC: ti bandgap feature types
  270. *
  271. * TI_BANDGAP_FEATURE_TSHUT - used when the thermal shutdown signal output
  272. * of a bandgap device instance is routed to the processor. This means
  273. * the system must react and perform the shutdown by itself (handle an
  274. * IRQ, for instance).
  275. *
  276. * TI_BANDGAP_FEATURE_TSHUT_CONFIG - used when the bandgap device has control
  277. * over the thermal shutdown configuration. This means that the thermal
  278. * shutdown thresholds are programmable, for instance.
  279. *
  280. * TI_BANDGAP_FEATURE_TALERT - used when the bandgap device instance outputs
  281. * a signal representing violation of programmable alert thresholds.
  282. *
  283. * TI_BANDGAP_FEATURE_MODE_CONFIG - used when it is possible to choose which
  284. * mode, continuous or one shot, the bandgap device instance will operate.
  285. *
  286. * TI_BANDGAP_FEATURE_COUNTER - used when the bandgap device instance allows
  287. * programming the update interval of its internal state machine.
  288. *
  289. * TI_BANDGAP_FEATURE_POWER_SWITCH - used when the bandgap device allows
  290. * itself to be switched on/off.
  291. *
  292. * TI_BANDGAP_FEATURE_CLK_CTRL - used when the clocks feeding the bandgap
  293. * device are gateable or not.
  294. *
  295. * TI_BANDGAP_FEATURE_FREEZE_BIT - used when the bandgap device features
  296. * a history buffer that its update can be freezed/unfreezed.
  297. *
  298. * TI_BANDGAP_FEATURE_COUNTER_DELAY - used when the bandgap device features
  299. * a delay programming based on distinct values.
  300. *
  301. * TI_BANDGAP_FEATURE_HISTORY_BUFFER - used when the bandgap device features
  302. * a history buffer of temperatures.
  303. *
  304. * TI_BANDGAP_FEATURE_ERRATA_814 - used to workaorund when the bandgap device
  305. * has Errata 814
  306. * TI_BANDGAP_FEATURE_ERRATA_813 - used to workaorund when the bandgap device
  307. * has Errata 813
  308. * TI_BANDGAP_FEATURE_UNRELIABLE - used when the sensor readings are too
  309. * inaccurate.
  310. * TI_BANDGAP_HAS(b, f) - macro to check if a bandgap device is capable of a
  311. * specific feature (above) or not. Return non-zero, if yes.
  312. */
  313. #define TI_BANDGAP_FEATURE_TSHUT BIT(0)
  314. #define TI_BANDGAP_FEATURE_TSHUT_CONFIG BIT(1)
  315. #define TI_BANDGAP_FEATURE_TALERT BIT(2)
  316. #define TI_BANDGAP_FEATURE_MODE_CONFIG BIT(3)
  317. #define TI_BANDGAP_FEATURE_COUNTER BIT(4)
  318. #define TI_BANDGAP_FEATURE_POWER_SWITCH BIT(5)
  319. #define TI_BANDGAP_FEATURE_CLK_CTRL BIT(6)
  320. #define TI_BANDGAP_FEATURE_FREEZE_BIT BIT(7)
  321. #define TI_BANDGAP_FEATURE_COUNTER_DELAY BIT(8)
  322. #define TI_BANDGAP_FEATURE_HISTORY_BUFFER BIT(9)
  323. #define TI_BANDGAP_FEATURE_ERRATA_814 BIT(10)
  324. #define TI_BANDGAP_FEATURE_ERRATA_813 BIT(11)
  325. #define TI_BANDGAP_FEATURE_UNRELIABLE BIT(12)
  326. #define TI_BANDGAP_HAS(b, f) \
  327. ((b)->conf->features & TI_BANDGAP_FEATURE_ ## f)
  328. /**
  329. * struct ti_bandgap_data - ti bandgap data configuration structure
  330. * @features: a bitwise flag set to describe the device features
  331. * @conv_table: Pointer to ADC to temperature conversion table
  332. * @adc_start_val: ADC conversion table starting value
  333. * @adc_end_val: ADC conversion table ending value
  334. * @fclock_name: clock name of the functional clock
  335. * @div_ck_name: clock name of the clock divisor
  336. * @sensor_count: count of temperature sensor within this bandgap device
  337. * @report_temperature: callback to report thermal alert to thermal API
  338. * @expose_sensor: callback to export sensor to thermal API
  339. * @remove_sensor: callback to destroy sensor from thermal API
  340. * @sensors: array of sensors present in this bandgap instance
  341. *
  342. * This is a data structure which should hold most of the static configuration
  343. * of a bandgap device instance. It should describe which features this instance
  344. * is capable of, the clock names to feed this device, the amount of sensors and
  345. * their configuration representation, and how to export and unexport them to
  346. * a thermal API.
  347. */
  348. struct ti_bandgap_data {
  349. unsigned int features;
  350. const int *conv_table;
  351. u32 adc_start_val;
  352. u32 adc_end_val;
  353. char *fclock_name;
  354. char *div_ck_name;
  355. int sensor_count;
  356. int (*report_temperature)(struct ti_bandgap *bgp, int id);
  357. int (*expose_sensor)(struct ti_bandgap *bgp, int id, char *domain);
  358. int (*remove_sensor)(struct ti_bandgap *bgp, int id);
  359. /* this needs to be at the end */
  360. struct ti_temp_sensor sensors[];
  361. };
  362. int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot);
  363. int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val);
  364. int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold);
  365. int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val);
  366. int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
  367. int *interval);
  368. int ti_bandgap_write_update_interval(struct ti_bandgap *bgp, int id,
  369. u32 interval);
  370. int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
  371. int *temperature);
  372. int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data);
  373. void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id);
  374. int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend);
  375. #ifdef CONFIG_OMAP3_THERMAL
  376. extern const struct ti_bandgap_data omap34xx_data;
  377. extern const struct ti_bandgap_data omap36xx_data;
  378. #else
  379. #define omap34xx_data NULL
  380. #define omap36xx_data NULL
  381. #endif
  382. #ifdef CONFIG_OMAP4_THERMAL
  383. extern const struct ti_bandgap_data omap4430_data;
  384. extern const struct ti_bandgap_data omap4460_data;
  385. extern const struct ti_bandgap_data omap4470_data;
  386. #else
  387. #define omap4430_data NULL
  388. #define omap4460_data NULL
  389. #define omap4470_data NULL
  390. #endif
  391. #ifdef CONFIG_OMAP5_THERMAL
  392. extern const struct ti_bandgap_data omap5430_data;
  393. #else
  394. #define omap5430_data NULL
  395. #endif
  396. #ifdef CONFIG_DRA752_THERMAL
  397. extern const struct ti_bandgap_data dra752_data;
  398. #else
  399. #define dra752_data NULL
  400. #endif
  401. #endif