k3_bandgap.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * TI Bandgap temperature sensor driver for K3 SoC Family
  4. *
  5. * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
  6. */
  7. #include <linux/err.h>
  8. #include <linux/init.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/thermal.h>
  16. #include <linux/types.h>
  17. #define K3_VTM_DEVINFO_PWR0_OFFSET 0x4
  18. #define K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK 0xf0
  19. #define K3_VTM_TMPSENS0_CTRL_OFFSET 0x80
  20. #define K3_VTM_REGS_PER_TS 0x10
  21. #define K3_VTM_TS_STAT_DTEMP_MASK 0x3ff
  22. #define K3_VTM_TMPSENS_CTRL_CBIASSEL BIT(0)
  23. #define K3_VTM_TMPSENS_CTRL_SOC BIT(5)
  24. #define K3_VTM_TMPSENS_CTRL_CLRZ BIT(6)
  25. #define K3_VTM_TMPSENS_CTRL_CLKON_REQ BIT(7)
  26. #define K3_VTM_ADC_BEGIN_VAL 540
  27. #define K3_VTM_ADC_END_VAL 944
  28. static const int k3_adc_to_temp[] = {
  29. -40000, -40000, -40000, -40000, -39800, -39400, -39000, -38600, -38200,
  30. -37800, -37400, -37000, -36600, -36200, -35800, -35300, -34700, -34200,
  31. -33800, -33400, -33000, -32600, -32200, -31800, -31400, -31000, -30600,
  32. -30200, -29800, -29400, -29000, -28600, -28200, -27700, -27100, -26600,
  33. -26200, -25800, -25400, -25000, -24600, -24200, -23800, -23400, -23000,
  34. -22600, -22200, -21800, -21400, -21000, -20500, -19900, -19400, -19000,
  35. -18600, -18200, -17800, -17400, -17000, -16600, -16200, -15800, -15400,
  36. -15000, -14600, -14200, -13800, -13400, -13000, -12500, -11900, -11400,
  37. -11000, -10600, -10200, -9800, -9400, -9000, -8600, -8200, -7800, -7400,
  38. -7000, -6600, -6200, -5800, -5400, -5000, -4500, -3900, -3400, -3000,
  39. -2600, -2200, -1800, -1400, -1000, -600, -200, 200, 600, 1000, 1400,
  40. 1800, 2200, 2600, 3000, 3400, 3900, 4500, 5000, 5400, 5800, 6200, 6600,
  41. 7000, 7400, 7800, 8200, 8600, 9000, 9400, 9800, 10200, 10600, 11000,
  42. 11400, 11800, 12200, 12700, 13300, 13800, 14200, 14600, 15000, 15400,
  43. 15800, 16200, 16600, 17000, 17400, 17800, 18200, 18600, 19000, 19400,
  44. 19800, 20200, 20600, 21000, 21400, 21900, 22500, 23000, 23400, 23800,
  45. 24200, 24600, 25000, 25400, 25800, 26200, 26600, 27000, 27400, 27800,
  46. 28200, 28600, 29000, 29400, 29800, 30200, 30600, 31000, 31400, 31900,
  47. 32500, 33000, 33400, 33800, 34200, 34600, 35000, 35400, 35800, 36200,
  48. 36600, 37000, 37400, 37800, 38200, 38600, 39000, 39400, 39800, 40200,
  49. 40600, 41000, 41400, 41800, 42200, 42600, 43100, 43700, 44200, 44600,
  50. 45000, 45400, 45800, 46200, 46600, 47000, 47400, 47800, 48200, 48600,
  51. 49000, 49400, 49800, 50200, 50600, 51000, 51400, 51800, 52200, 52600,
  52. 53000, 53400, 53800, 54200, 54600, 55000, 55400, 55900, 56500, 57000,
  53. 57400, 57800, 58200, 58600, 59000, 59400, 59800, 60200, 60600, 61000,
  54. 61400, 61800, 62200, 62600, 63000, 63400, 63800, 64200, 64600, 65000,
  55. 65400, 65800, 66200, 66600, 67000, 67400, 67800, 68200, 68600, 69000,
  56. 69400, 69800, 70200, 70600, 71000, 71500, 72100, 72600, 73000, 73400,
  57. 73800, 74200, 74600, 75000, 75400, 75800, 76200, 76600, 77000, 77400,
  58. 77800, 78200, 78600, 79000, 79400, 79800, 80200, 80600, 81000, 81400,
  59. 81800, 82200, 82600, 83000, 83400, 83800, 84200, 84600, 85000, 85400,
  60. 85800, 86200, 86600, 87000, 87400, 87800, 88200, 88600, 89000, 89400,
  61. 89800, 90200, 90600, 91000, 91400, 91800, 92200, 92600, 93000, 93400,
  62. 93800, 94200, 94600, 95000, 95400, 95800, 96200, 96600, 97000, 97500,
  63. 98100, 98600, 99000, 99400, 99800, 100200, 100600, 101000, 101400,
  64. 101800, 102200, 102600, 103000, 103400, 103800, 104200, 104600, 105000,
  65. 105400, 105800, 106200, 106600, 107000, 107400, 107800, 108200, 108600,
  66. 109000, 109400, 109800, 110200, 110600, 111000, 111400, 111800, 112200,
  67. 112600, 113000, 113400, 113800, 114200, 114600, 115000, 115400, 115800,
  68. 116200, 116600, 117000, 117400, 117800, 118200, 118600, 119000, 119400,
  69. 119800, 120200, 120600, 121000, 121400, 121800, 122200, 122600, 123000,
  70. 123400, 123800, 124200, 124600, 124900, 125000,
  71. };
  72. struct k3_bandgap {
  73. void __iomem *base;
  74. const struct k3_bandgap_data *conf;
  75. };
  76. /* common data structures */
  77. struct k3_thermal_data {
  78. struct thermal_zone_device *tzd;
  79. struct k3_bandgap *bgp;
  80. int sensor_id;
  81. u32 ctrl_offset;
  82. u32 stat_offset;
  83. };
  84. static unsigned int vtm_get_best_value(unsigned int s0, unsigned int s1,
  85. unsigned int s2)
  86. {
  87. int d01 = abs(s0 - s1);
  88. int d02 = abs(s0 - s2);
  89. int d12 = abs(s1 - s2);
  90. if (d01 <= d02 && d01 <= d12)
  91. return (s0 + s1) / 2;
  92. if (d02 <= d01 && d02 <= d12)
  93. return (s0 + s2) / 2;
  94. return (s1 + s2) / 2;
  95. }
  96. static int k3_bgp_read_temp(struct k3_thermal_data *devdata,
  97. int *temp)
  98. {
  99. struct k3_bandgap *bgp;
  100. unsigned int dtemp, s0, s1, s2;
  101. bgp = devdata->bgp;
  102. /*
  103. * Errata is applicable for am654 pg 1.0 silicon. There
  104. * is a variation of the order for 8-10 degree centigrade.
  105. * Work around that by getting the average of two closest
  106. * readings out of three readings everytime we want to
  107. * report temperatures.
  108. *
  109. * Errata workaround.
  110. */
  111. s0 = readl(bgp->base + devdata->stat_offset) &
  112. K3_VTM_TS_STAT_DTEMP_MASK;
  113. s1 = readl(bgp->base + devdata->stat_offset) &
  114. K3_VTM_TS_STAT_DTEMP_MASK;
  115. s2 = readl(bgp->base + devdata->stat_offset) &
  116. K3_VTM_TS_STAT_DTEMP_MASK;
  117. dtemp = vtm_get_best_value(s0, s1, s2);
  118. if (dtemp < K3_VTM_ADC_BEGIN_VAL || dtemp > K3_VTM_ADC_END_VAL)
  119. return -EINVAL;
  120. *temp = k3_adc_to_temp[dtemp - K3_VTM_ADC_BEGIN_VAL];
  121. return 0;
  122. }
  123. static int k3_thermal_get_temp(void *devdata, int *temp)
  124. {
  125. struct k3_thermal_data *data = devdata;
  126. int ret = 0;
  127. ret = k3_bgp_read_temp(data, temp);
  128. if (ret)
  129. return ret;
  130. return ret;
  131. }
  132. static const struct thermal_zone_of_device_ops k3_of_thermal_ops = {
  133. .get_temp = k3_thermal_get_temp,
  134. };
  135. static const struct of_device_id of_k3_bandgap_match[];
  136. static int k3_bandgap_probe(struct platform_device *pdev)
  137. {
  138. int ret = 0, cnt, val, id;
  139. struct resource *res;
  140. struct device *dev = &pdev->dev;
  141. struct k3_bandgap *bgp;
  142. struct k3_thermal_data *data;
  143. if (ARRAY_SIZE(k3_adc_to_temp) != (K3_VTM_ADC_END_VAL + 1 -
  144. K3_VTM_ADC_BEGIN_VAL))
  145. return -EINVAL;
  146. bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
  147. if (!bgp)
  148. return -ENOMEM;
  149. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  150. bgp->base = devm_ioremap_resource(dev, res);
  151. if (IS_ERR(bgp->base))
  152. return PTR_ERR(bgp->base);
  153. pm_runtime_enable(dev);
  154. ret = pm_runtime_get_sync(dev);
  155. if (ret < 0) {
  156. pm_runtime_put_noidle(dev);
  157. pm_runtime_disable(dev);
  158. return ret;
  159. }
  160. /* Get the sensor count in the VTM */
  161. val = readl(bgp->base + K3_VTM_DEVINFO_PWR0_OFFSET);
  162. cnt = val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK;
  163. cnt >>= __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK);
  164. data = devm_kcalloc(dev, cnt, sizeof(*data), GFP_KERNEL);
  165. if (!data) {
  166. ret = -ENOMEM;
  167. goto err_alloc;
  168. }
  169. /* Register the thermal sensors */
  170. for (id = 0; id < cnt; id++) {
  171. data[id].sensor_id = id;
  172. data[id].bgp = bgp;
  173. data[id].ctrl_offset = K3_VTM_TMPSENS0_CTRL_OFFSET +
  174. id * K3_VTM_REGS_PER_TS;
  175. data[id].stat_offset = data[id].ctrl_offset + 0x8;
  176. val = readl(data[id].bgp->base + data[id].ctrl_offset);
  177. val |= (K3_VTM_TMPSENS_CTRL_SOC |
  178. K3_VTM_TMPSENS_CTRL_CLRZ |
  179. K3_VTM_TMPSENS_CTRL_CLKON_REQ);
  180. val &= ~K3_VTM_TMPSENS_CTRL_CBIASSEL;
  181. writel(val, data[id].bgp->base + data[id].ctrl_offset);
  182. data[id].tzd =
  183. devm_thermal_zone_of_sensor_register(dev, id,
  184. &data[id],
  185. &k3_of_thermal_ops);
  186. if (IS_ERR(data[id].tzd)) {
  187. dev_err(dev, "thermal zone device is NULL\n");
  188. ret = PTR_ERR(data[id].tzd);
  189. goto err_alloc;
  190. }
  191. }
  192. platform_set_drvdata(pdev, bgp);
  193. return 0;
  194. err_alloc:
  195. pm_runtime_put_sync(dev);
  196. pm_runtime_disable(dev);
  197. return ret;
  198. }
  199. static int k3_bandgap_remove(struct platform_device *pdev)
  200. {
  201. pm_runtime_put_sync(&pdev->dev);
  202. pm_runtime_disable(&pdev->dev);
  203. return 0;
  204. }
  205. static const struct of_device_id of_k3_bandgap_match[] = {
  206. {
  207. .compatible = "ti,am654-vtm",
  208. },
  209. { /* sentinel */ },
  210. };
  211. MODULE_DEVICE_TABLE(of, of_k3_bandgap_match);
  212. static struct platform_driver k3_bandgap_sensor_driver = {
  213. .probe = k3_bandgap_probe,
  214. .remove = k3_bandgap_remove,
  215. .driver = {
  216. .name = "k3-soc-thermal",
  217. .of_match_table = of_k3_bandgap_match,
  218. },
  219. };
  220. module_platform_driver(k3_bandgap_sensor_driver);
  221. MODULE_DESCRIPTION("K3 bandgap temperature sensor driver");
  222. MODULE_LICENSE("GPL v2");
  223. MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");