ti-bandgap.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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_counter_delay_mask: mask to bandgap_mask_ctrl.mask_counter_delay
  79. * @mask_freeze_mask: mask to bandgap_mask_ctrl.mask_free
  80. * @bgap_mode_ctrl: BANDGAP_MODE_CTRL register offset
  81. * @mode_ctrl_mask: mask to bandgap_mode_ctrl.mode_ctrl
  82. * @bgap_counter: BANDGAP_COUNTER register offset
  83. * @counter_mask: mask to bandgap_counter.counter
  84. * @bgap_threshold: BANDGAP_THRESHOLD register offset (TALERT thresholds)
  85. * @threshold_thot_mask: mask to bandgap_threhold.thot
  86. * @threshold_tcold_mask: mask to bandgap_threhold.tcold
  87. * @tshut_threshold: TSHUT_THRESHOLD register offset (TSHUT thresholds)
  88. * @tshut_hot_mask: mask to tshut_threhold.thot
  89. * @tshut_cold_mask: mask to tshut_threhold.thot
  90. * @bgap_status: BANDGAP_STATUS register offset
  91. * @status_hot_mask: mask to bandgap_status.hot
  92. * @status_cold_mask: mask to bandgap_status.cold
  93. * @ctrl_dtemp_1: CTRL_DTEMP1 register offset
  94. * @ctrl_dtemp_2: CTRL_DTEMP2 register offset
  95. * @bgap_efuse: BANDGAP_EFUSE register offset
  96. *
  97. * The register offsets and bitfields might change across
  98. * OMAP and variants versions. Hence this struct serves as a
  99. * descriptor map on how to access the registers and the bitfields.
  100. *
  101. * This descriptor contains registers of all versions of bandgap chips.
  102. * Not all versions will use all registers, depending on the available
  103. * features. Please read TRMs for descriptive explanation on each bitfield.
  104. */
  105. struct temp_sensor_registers {
  106. u32 temp_sensor_ctrl;
  107. u32 bgap_tempsoff_mask;
  108. u32 bgap_soc_mask;
  109. u32 bgap_eocz_mask;
  110. u32 bgap_dtemp_mask;
  111. u32 bgap_mask_ctrl;
  112. u32 mask_hot_mask;
  113. u32 mask_cold_mask;
  114. u32 mask_counter_delay_mask;
  115. u32 mask_freeze_mask;
  116. u32 bgap_mode_ctrl;
  117. u32 mode_ctrl_mask;
  118. u32 bgap_counter;
  119. u32 counter_mask;
  120. u32 bgap_threshold;
  121. u32 threshold_thot_mask;
  122. u32 threshold_tcold_mask;
  123. u32 tshut_threshold;
  124. u32 tshut_hot_mask;
  125. u32 tshut_cold_mask;
  126. u32 bgap_status;
  127. u32 status_hot_mask;
  128. u32 status_cold_mask;
  129. u32 ctrl_dtemp_1;
  130. u32 ctrl_dtemp_2;
  131. u32 bgap_efuse;
  132. };
  133. /**
  134. * struct temp_sensor_data - The thresholds and limits for temperature sensors.
  135. * @tshut_hot: temperature to trigger a thermal reset (initial value)
  136. * @tshut_cold: temp to get the plat out of reset due to thermal (init val)
  137. * @t_hot: temperature to trigger a thermal alert (high initial value)
  138. * @t_cold: temperature to trigger a thermal alert (low initial value)
  139. * @min_freq: sensor minimum clock rate
  140. * @max_freq: sensor maximum clock rate
  141. *
  142. * This data structure will hold the required thresholds and temperature limits
  143. * for a specific temperature sensor, like shutdown temperature, alert
  144. * temperature, clock / rate used, ADC conversion limits and update intervals
  145. */
  146. struct temp_sensor_data {
  147. u32 tshut_hot;
  148. u32 tshut_cold;
  149. u32 t_hot;
  150. u32 t_cold;
  151. u32 min_freq;
  152. u32 max_freq;
  153. };
  154. struct ti_bandgap_data;
  155. /**
  156. * struct temp_sensor_regval - temperature sensor register values and priv data
  157. * @bg_mode_ctrl: temp sensor control register value
  158. * @bg_ctrl: bandgap ctrl register value
  159. * @bg_counter: bandgap counter value
  160. * @bg_threshold: bandgap threshold register value
  161. * @tshut_threshold: bandgap tshut register value
  162. * @data: private data
  163. *
  164. * Data structure to save and restore bandgap register set context. Only
  165. * required registers are shadowed, when needed.
  166. */
  167. struct temp_sensor_regval {
  168. u32 bg_mode_ctrl;
  169. u32 bg_ctrl;
  170. u32 bg_counter;
  171. u32 bg_threshold;
  172. u32 tshut_threshold;
  173. void *data;
  174. };
  175. /**
  176. * struct ti_bandgap - bandgap device structure
  177. * @dev: struct device pointer
  178. * @base: io memory base address
  179. * @conf: struct with bandgap configuration set (# sensors, conv_table, etc)
  180. * @regval: temperature sensor register values
  181. * @fclock: pointer to functional clock of temperature sensor
  182. * @div_clk: pointer to divider clock of temperature sensor fclk
  183. * @lock: spinlock for ti_bandgap structure
  184. * @irq: MPU IRQ number for thermal alert
  185. * @tshut_gpio: GPIO where Tshut signal is routed
  186. * @clk_rate: Holds current clock rate
  187. *
  188. * The bandgap device structure representing the bandgap device instance.
  189. * It holds most of the dynamic stuff. Configurations and sensor specific
  190. * entries are inside the @conf structure.
  191. */
  192. struct ti_bandgap {
  193. struct device *dev;
  194. void __iomem *base;
  195. const struct ti_bandgap_data *conf;
  196. struct temp_sensor_regval *regval;
  197. struct clk *fclock;
  198. struct clk *div_clk;
  199. spinlock_t lock; /* shields this struct */
  200. int irq;
  201. int tshut_gpio;
  202. u32 clk_rate;
  203. };
  204. /**
  205. * struct ti_temp_sensor - bandgap temperature sensor configuration data
  206. * @ts_data: pointer to struct with thresholds, limits of temperature sensor
  207. * @registers: pointer to the list of register offsets and bitfields
  208. * @domain: the name of the domain where the sensor is located
  209. * @slope_pcb: sensor gradient slope info for hotspot extrapolation equation
  210. * with no external influence
  211. * @constant_pcb: sensor gradient const info for hotspot extrapolation equation
  212. * with no external influence
  213. * @register_cooling: function to describe how this sensor is going to be cooled
  214. * @unregister_cooling: function to release cooling data
  215. *
  216. * Data structure to describe a temperature sensor handled by a bandgap device.
  217. * It should provide configuration details on this sensor, such as how to
  218. * access the registers affecting this sensor, shadow register buffer, how to
  219. * assess the gradient from hotspot, how to cooldown the domain when sensor
  220. * reports too hot temperature.
  221. */
  222. struct ti_temp_sensor {
  223. struct temp_sensor_data *ts_data;
  224. struct temp_sensor_registers *registers;
  225. char *domain;
  226. /* for hotspot extrapolation */
  227. const int slope_pcb;
  228. const int constant_pcb;
  229. int (*register_cooling)(struct ti_bandgap *bgp, int id);
  230. int (*unregister_cooling)(struct ti_bandgap *bgp, int id);
  231. };
  232. /**
  233. * DOC: ti bandgap feature types
  234. *
  235. * TI_BANDGAP_FEATURE_TSHUT - used when the thermal shutdown signal output
  236. * of a bandgap device instance is routed to the processor. This means
  237. * the system must react and perform the shutdown by itself (handle an
  238. * IRQ, for instance).
  239. *
  240. * TI_BANDGAP_FEATURE_TSHUT_CONFIG - used when the bandgap device has control
  241. * over the thermal shutdown configuration. This means that the thermal
  242. * shutdown thresholds are programmable, for instance.
  243. *
  244. * TI_BANDGAP_FEATURE_TALERT - used when the bandgap device instance outputs
  245. * a signal representing violation of programmable alert thresholds.
  246. *
  247. * TI_BANDGAP_FEATURE_MODE_CONFIG - used when it is possible to choose which
  248. * mode, continuous or one shot, the bandgap device instance will operate.
  249. *
  250. * TI_BANDGAP_FEATURE_COUNTER - used when the bandgap device instance allows
  251. * programming the update interval of its internal state machine.
  252. *
  253. * TI_BANDGAP_FEATURE_POWER_SWITCH - used when the bandgap device allows
  254. * itself to be switched on/off.
  255. *
  256. * TI_BANDGAP_FEATURE_CLK_CTRL - used when the clocks feeding the bandgap
  257. * device are gateable or not.
  258. *
  259. * TI_BANDGAP_FEATURE_FREEZE_BIT - used when the bandgap device features
  260. * a history buffer that its update can be freezed/unfreezed.
  261. *
  262. * TI_BANDGAP_FEATURE_COUNTER_DELAY - used when the bandgap device features
  263. * a delay programming based on distinct values.
  264. *
  265. * TI_BANDGAP_FEATURE_HISTORY_BUFFER - used when the bandgap device features
  266. * a history buffer of temperatures.
  267. *
  268. * TI_BANDGAP_FEATURE_ERRATA_814 - used to workaorund when the bandgap device
  269. * has Errata 814
  270. * TI_BANDGAP_FEATURE_UNRELIABLE - used when the sensor readings are too
  271. * inaccurate.
  272. * TI_BANDGAP_HAS(b, f) - macro to check if a bandgap device is capable of a
  273. * specific feature (above) or not. Return non-zero, if yes.
  274. */
  275. #define TI_BANDGAP_FEATURE_TSHUT BIT(0)
  276. #define TI_BANDGAP_FEATURE_TSHUT_CONFIG BIT(1)
  277. #define TI_BANDGAP_FEATURE_TALERT BIT(2)
  278. #define TI_BANDGAP_FEATURE_MODE_CONFIG BIT(3)
  279. #define TI_BANDGAP_FEATURE_COUNTER BIT(4)
  280. #define TI_BANDGAP_FEATURE_POWER_SWITCH BIT(5)
  281. #define TI_BANDGAP_FEATURE_CLK_CTRL BIT(6)
  282. #define TI_BANDGAP_FEATURE_FREEZE_BIT BIT(7)
  283. #define TI_BANDGAP_FEATURE_COUNTER_DELAY BIT(8)
  284. #define TI_BANDGAP_FEATURE_HISTORY_BUFFER BIT(9)
  285. #define TI_BANDGAP_FEATURE_ERRATA_814 BIT(10)
  286. #define TI_BANDGAP_FEATURE_UNRELIABLE BIT(11)
  287. #define TI_BANDGAP_HAS(b, f) \
  288. ((b)->conf->features & TI_BANDGAP_FEATURE_ ## f)
  289. /**
  290. * struct ti_bandgap_data - ti bandgap data configuration structure
  291. * @features: a bitwise flag set to describe the device features
  292. * @conv_table: Pointer to ADC to temperature conversion table
  293. * @adc_start_val: ADC conversion table starting value
  294. * @adc_end_val: ADC conversion table ending value
  295. * @fclock_name: clock name of the functional clock
  296. * @div_ck_name: clock name of the clock divisor
  297. * @sensor_count: count of temperature sensor within this bandgap device
  298. * @report_temperature: callback to report thermal alert to thermal API
  299. * @expose_sensor: callback to export sensor to thermal API
  300. * @remove_sensor: callback to destroy sensor from thermal API
  301. * @sensors: array of sensors present in this bandgap instance
  302. *
  303. * This is a data structure which should hold most of the static configuration
  304. * of a bandgap device instance. It should describe which features this instance
  305. * is capable of, the clock names to feed this device, the amount of sensors and
  306. * their configuration representation, and how to export and unexport them to
  307. * a thermal API.
  308. */
  309. struct ti_bandgap_data {
  310. unsigned int features;
  311. const int *conv_table;
  312. u32 adc_start_val;
  313. u32 adc_end_val;
  314. char *fclock_name;
  315. char *div_ck_name;
  316. int sensor_count;
  317. int (*report_temperature)(struct ti_bandgap *bgp, int id);
  318. int (*expose_sensor)(struct ti_bandgap *bgp, int id, char *domain);
  319. int (*remove_sensor)(struct ti_bandgap *bgp, int id);
  320. /* this needs to be at the end */
  321. struct ti_temp_sensor sensors[];
  322. };
  323. int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot);
  324. int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val);
  325. int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold);
  326. int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val);
  327. int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
  328. int *interval);
  329. int ti_bandgap_write_update_interval(struct ti_bandgap *bgp, int id,
  330. u32 interval);
  331. int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
  332. int *temperature);
  333. int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data);
  334. void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id);
  335. int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend);
  336. #ifdef CONFIG_OMAP3_THERMAL
  337. extern const struct ti_bandgap_data omap34xx_data;
  338. extern const struct ti_bandgap_data omap36xx_data;
  339. #else
  340. #define omap34xx_data NULL
  341. #define omap36xx_data NULL
  342. #endif
  343. #ifdef CONFIG_OMAP4_THERMAL
  344. extern const struct ti_bandgap_data omap4430_data;
  345. extern const struct ti_bandgap_data omap4460_data;
  346. extern const struct ti_bandgap_data omap4470_data;
  347. #else
  348. #define omap4430_data NULL
  349. #define omap4460_data NULL
  350. #define omap4470_data NULL
  351. #endif
  352. #ifdef CONFIG_OMAP5_THERMAL
  353. extern const struct ti_bandgap_data omap5430_data;
  354. #else
  355. #define omap5430_data NULL
  356. #endif
  357. #ifdef CONFIG_DRA752_THERMAL
  358. extern const struct ti_bandgap_data dra752_data;
  359. #else
  360. #define dra752_data NULL
  361. #endif
  362. #endif