ti-bandgap.h 14 KB

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