imx_thermal.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright 2013 Freescale Semiconductor, Inc.
  4. #include <linux/clk.h>
  5. #include <linux/cpu.h>
  6. #include <linux/cpufreq.h>
  7. #include <linux/cpu_cooling.h>
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/slab.h>
  21. #include <linux/thermal.h>
  22. #include <linux/types.h>
  23. #include <linux/nvmem-consumer.h>
  24. #define REG_SET 0x4
  25. #define REG_CLR 0x8
  26. #define REG_TOG 0xc
  27. /* i.MX6 specific */
  28. #define IMX6_MISC0 0x0150
  29. #define IMX6_MISC0_REFTOP_SELBIASOFF (1 << 3)
  30. #define IMX6_MISC1 0x0160
  31. #define IMX6_MISC1_IRQ_TEMPHIGH (1 << 29)
  32. /* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
  33. #define IMX6_MISC1_IRQ_TEMPLOW (1 << 28)
  34. #define IMX6_MISC1_IRQ_TEMPPANIC (1 << 27)
  35. #define IMX6_TEMPSENSE0 0x0180
  36. #define IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT 20
  37. #define IMX6_TEMPSENSE0_ALARM_VALUE_MASK (0xfff << 20)
  38. #define IMX6_TEMPSENSE0_TEMP_CNT_SHIFT 8
  39. #define IMX6_TEMPSENSE0_TEMP_CNT_MASK (0xfff << 8)
  40. #define IMX6_TEMPSENSE0_FINISHED (1 << 2)
  41. #define IMX6_TEMPSENSE0_MEASURE_TEMP (1 << 1)
  42. #define IMX6_TEMPSENSE0_POWER_DOWN (1 << 0)
  43. #define IMX6_TEMPSENSE1 0x0190
  44. #define IMX6_TEMPSENSE1_MEASURE_FREQ 0xffff
  45. #define IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT 0
  46. #define OCOTP_MEM0 0x0480
  47. #define OCOTP_ANA1 0x04e0
  48. /* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
  49. #define IMX6_TEMPSENSE2 0x0290
  50. #define IMX6_TEMPSENSE2_LOW_VALUE_SHIFT 0
  51. #define IMX6_TEMPSENSE2_LOW_VALUE_MASK 0xfff
  52. #define IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT 16
  53. #define IMX6_TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
  54. /* i.MX7 specific */
  55. #define IMX7_ANADIG_DIGPROG 0x800
  56. #define IMX7_TEMPSENSE0 0x300
  57. #define IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT 18
  58. #define IMX7_TEMPSENSE0_PANIC_ALARM_MASK (0x1ff << 18)
  59. #define IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT 9
  60. #define IMX7_TEMPSENSE0_HIGH_ALARM_MASK (0x1ff << 9)
  61. #define IMX7_TEMPSENSE0_LOW_ALARM_SHIFT 0
  62. #define IMX7_TEMPSENSE0_LOW_ALARM_MASK 0x1ff
  63. #define IMX7_TEMPSENSE1 0x310
  64. #define IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT 16
  65. #define IMX7_TEMPSENSE1_MEASURE_FREQ_MASK (0xffff << 16)
  66. #define IMX7_TEMPSENSE1_FINISHED (1 << 11)
  67. #define IMX7_TEMPSENSE1_MEASURE_TEMP (1 << 10)
  68. #define IMX7_TEMPSENSE1_POWER_DOWN (1 << 9)
  69. #define IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT 0
  70. #define IMX7_TEMPSENSE1_TEMP_VALUE_MASK 0x1ff
  71. /* The driver supports 1 passive trip point and 1 critical trip point */
  72. enum imx_thermal_trip {
  73. IMX_TRIP_PASSIVE,
  74. IMX_TRIP_CRITICAL,
  75. IMX_TRIP_NUM,
  76. };
  77. #define IMX_POLLING_DELAY 2000 /* millisecond */
  78. #define IMX_PASSIVE_DELAY 1000
  79. #define TEMPMON_IMX6Q 1
  80. #define TEMPMON_IMX6SX 2
  81. #define TEMPMON_IMX7D 3
  82. struct thermal_soc_data {
  83. u32 version;
  84. u32 sensor_ctrl;
  85. u32 power_down_mask;
  86. u32 measure_temp_mask;
  87. u32 measure_freq_ctrl;
  88. u32 measure_freq_mask;
  89. u32 measure_freq_shift;
  90. u32 temp_data;
  91. u32 temp_value_mask;
  92. u32 temp_value_shift;
  93. u32 temp_valid_mask;
  94. u32 panic_alarm_ctrl;
  95. u32 panic_alarm_mask;
  96. u32 panic_alarm_shift;
  97. u32 high_alarm_ctrl;
  98. u32 high_alarm_mask;
  99. u32 high_alarm_shift;
  100. u32 low_alarm_ctrl;
  101. u32 low_alarm_mask;
  102. u32 low_alarm_shift;
  103. };
  104. static struct thermal_soc_data thermal_imx6q_data = {
  105. .version = TEMPMON_IMX6Q,
  106. .sensor_ctrl = IMX6_TEMPSENSE0,
  107. .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
  108. .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
  109. .measure_freq_ctrl = IMX6_TEMPSENSE1,
  110. .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
  111. .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
  112. .temp_data = IMX6_TEMPSENSE0,
  113. .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
  114. .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
  115. .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
  116. .high_alarm_ctrl = IMX6_TEMPSENSE0,
  117. .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
  118. .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
  119. };
  120. static struct thermal_soc_data thermal_imx6sx_data = {
  121. .version = TEMPMON_IMX6SX,
  122. .sensor_ctrl = IMX6_TEMPSENSE0,
  123. .power_down_mask = IMX6_TEMPSENSE0_POWER_DOWN,
  124. .measure_temp_mask = IMX6_TEMPSENSE0_MEASURE_TEMP,
  125. .measure_freq_ctrl = IMX6_TEMPSENSE1,
  126. .measure_freq_shift = IMX6_TEMPSENSE1_MEASURE_FREQ_SHIFT,
  127. .measure_freq_mask = IMX6_TEMPSENSE1_MEASURE_FREQ,
  128. .temp_data = IMX6_TEMPSENSE0,
  129. .temp_value_mask = IMX6_TEMPSENSE0_TEMP_CNT_MASK,
  130. .temp_value_shift = IMX6_TEMPSENSE0_TEMP_CNT_SHIFT,
  131. .temp_valid_mask = IMX6_TEMPSENSE0_FINISHED,
  132. .high_alarm_ctrl = IMX6_TEMPSENSE0,
  133. .high_alarm_mask = IMX6_TEMPSENSE0_ALARM_VALUE_MASK,
  134. .high_alarm_shift = IMX6_TEMPSENSE0_ALARM_VALUE_SHIFT,
  135. .panic_alarm_ctrl = IMX6_TEMPSENSE2,
  136. .panic_alarm_mask = IMX6_TEMPSENSE2_PANIC_VALUE_MASK,
  137. .panic_alarm_shift = IMX6_TEMPSENSE2_PANIC_VALUE_SHIFT,
  138. .low_alarm_ctrl = IMX6_TEMPSENSE2,
  139. .low_alarm_mask = IMX6_TEMPSENSE2_LOW_VALUE_MASK,
  140. .low_alarm_shift = IMX6_TEMPSENSE2_LOW_VALUE_SHIFT,
  141. };
  142. static struct thermal_soc_data thermal_imx7d_data = {
  143. .version = TEMPMON_IMX7D,
  144. .sensor_ctrl = IMX7_TEMPSENSE1,
  145. .power_down_mask = IMX7_TEMPSENSE1_POWER_DOWN,
  146. .measure_temp_mask = IMX7_TEMPSENSE1_MEASURE_TEMP,
  147. .measure_freq_ctrl = IMX7_TEMPSENSE1,
  148. .measure_freq_shift = IMX7_TEMPSENSE1_MEASURE_FREQ_SHIFT,
  149. .measure_freq_mask = IMX7_TEMPSENSE1_MEASURE_FREQ_MASK,
  150. .temp_data = IMX7_TEMPSENSE1,
  151. .temp_value_mask = IMX7_TEMPSENSE1_TEMP_VALUE_MASK,
  152. .temp_value_shift = IMX7_TEMPSENSE1_TEMP_VALUE_SHIFT,
  153. .temp_valid_mask = IMX7_TEMPSENSE1_FINISHED,
  154. .panic_alarm_ctrl = IMX7_TEMPSENSE1,
  155. .panic_alarm_mask = IMX7_TEMPSENSE0_PANIC_ALARM_MASK,
  156. .panic_alarm_shift = IMX7_TEMPSENSE0_PANIC_ALARM_SHIFT,
  157. .high_alarm_ctrl = IMX7_TEMPSENSE0,
  158. .high_alarm_mask = IMX7_TEMPSENSE0_HIGH_ALARM_MASK,
  159. .high_alarm_shift = IMX7_TEMPSENSE0_HIGH_ALARM_SHIFT,
  160. .low_alarm_ctrl = IMX7_TEMPSENSE0,
  161. .low_alarm_mask = IMX7_TEMPSENSE0_LOW_ALARM_MASK,
  162. .low_alarm_shift = IMX7_TEMPSENSE0_LOW_ALARM_SHIFT,
  163. };
  164. struct imx_thermal_data {
  165. struct cpufreq_policy *policy;
  166. struct thermal_zone_device *tz;
  167. struct thermal_cooling_device *cdev;
  168. enum thermal_device_mode mode;
  169. struct regmap *tempmon;
  170. u32 c1, c2; /* See formula in imx_init_calib() */
  171. int temp_passive;
  172. int temp_critical;
  173. int temp_max;
  174. int alarm_temp;
  175. int last_temp;
  176. bool irq_enabled;
  177. int irq;
  178. struct clk *thermal_clk;
  179. const struct thermal_soc_data *socdata;
  180. const char *temp_grade;
  181. };
  182. static void imx_set_panic_temp(struct imx_thermal_data *data,
  183. int panic_temp)
  184. {
  185. const struct thermal_soc_data *soc_data = data->socdata;
  186. struct regmap *map = data->tempmon;
  187. int critical_value;
  188. critical_value = (data->c2 - panic_temp) / data->c1;
  189. regmap_write(map, soc_data->panic_alarm_ctrl + REG_CLR,
  190. soc_data->panic_alarm_mask);
  191. regmap_write(map, soc_data->panic_alarm_ctrl + REG_SET,
  192. critical_value << soc_data->panic_alarm_shift);
  193. }
  194. static void imx_set_alarm_temp(struct imx_thermal_data *data,
  195. int alarm_temp)
  196. {
  197. struct regmap *map = data->tempmon;
  198. const struct thermal_soc_data *soc_data = data->socdata;
  199. int alarm_value;
  200. data->alarm_temp = alarm_temp;
  201. if (data->socdata->version == TEMPMON_IMX7D)
  202. alarm_value = alarm_temp / 1000 + data->c1 - 25;
  203. else
  204. alarm_value = (data->c2 - alarm_temp) / data->c1;
  205. regmap_write(map, soc_data->high_alarm_ctrl + REG_CLR,
  206. soc_data->high_alarm_mask);
  207. regmap_write(map, soc_data->high_alarm_ctrl + REG_SET,
  208. alarm_value << soc_data->high_alarm_shift);
  209. }
  210. static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
  211. {
  212. struct imx_thermal_data *data = tz->devdata;
  213. const struct thermal_soc_data *soc_data = data->socdata;
  214. struct regmap *map = data->tempmon;
  215. unsigned int n_meas;
  216. bool wait;
  217. u32 val;
  218. if (data->mode == THERMAL_DEVICE_ENABLED) {
  219. /* Check if a measurement is currently in progress */
  220. regmap_read(map, soc_data->temp_data, &val);
  221. wait = !(val & soc_data->temp_valid_mask);
  222. } else {
  223. /*
  224. * Every time we measure the temperature, we will power on the
  225. * temperature sensor, enable measurements, take a reading,
  226. * disable measurements, power off the temperature sensor.
  227. */
  228. regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
  229. soc_data->power_down_mask);
  230. regmap_write(map, soc_data->sensor_ctrl + REG_SET,
  231. soc_data->measure_temp_mask);
  232. wait = true;
  233. }
  234. /*
  235. * According to the temp sensor designers, it may require up to ~17us
  236. * to complete a measurement.
  237. */
  238. if (wait)
  239. usleep_range(20, 50);
  240. regmap_read(map, soc_data->temp_data, &val);
  241. if (data->mode != THERMAL_DEVICE_ENABLED) {
  242. regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
  243. soc_data->measure_temp_mask);
  244. regmap_write(map, soc_data->sensor_ctrl + REG_SET,
  245. soc_data->power_down_mask);
  246. }
  247. if ((val & soc_data->temp_valid_mask) == 0) {
  248. dev_dbg(&tz->device, "temp measurement never finished\n");
  249. return -EAGAIN;
  250. }
  251. n_meas = (val & soc_data->temp_value_mask)
  252. >> soc_data->temp_value_shift;
  253. /* See imx_init_calib() for formula derivation */
  254. if (data->socdata->version == TEMPMON_IMX7D)
  255. *temp = (n_meas - data->c1 + 25) * 1000;
  256. else
  257. *temp = data->c2 - n_meas * data->c1;
  258. /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
  259. if (data->socdata->version == TEMPMON_IMX6Q) {
  260. if (data->alarm_temp == data->temp_passive &&
  261. *temp >= data->temp_passive)
  262. imx_set_alarm_temp(data, data->temp_critical);
  263. if (data->alarm_temp == data->temp_critical &&
  264. *temp < data->temp_passive) {
  265. imx_set_alarm_temp(data, data->temp_passive);
  266. dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
  267. data->alarm_temp / 1000);
  268. }
  269. }
  270. if (*temp != data->last_temp) {
  271. dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
  272. data->last_temp = *temp;
  273. }
  274. /* Reenable alarm IRQ if temperature below alarm temperature */
  275. if (!data->irq_enabled && *temp < data->alarm_temp) {
  276. data->irq_enabled = true;
  277. enable_irq(data->irq);
  278. }
  279. return 0;
  280. }
  281. static int imx_get_mode(struct thermal_zone_device *tz,
  282. enum thermal_device_mode *mode)
  283. {
  284. struct imx_thermal_data *data = tz->devdata;
  285. *mode = data->mode;
  286. return 0;
  287. }
  288. static int imx_set_mode(struct thermal_zone_device *tz,
  289. enum thermal_device_mode mode)
  290. {
  291. struct imx_thermal_data *data = tz->devdata;
  292. struct regmap *map = data->tempmon;
  293. const struct thermal_soc_data *soc_data = data->socdata;
  294. if (mode == THERMAL_DEVICE_ENABLED) {
  295. tz->polling_delay = IMX_POLLING_DELAY;
  296. tz->passive_delay = IMX_PASSIVE_DELAY;
  297. regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
  298. soc_data->power_down_mask);
  299. regmap_write(map, soc_data->sensor_ctrl + REG_SET,
  300. soc_data->measure_temp_mask);
  301. if (!data->irq_enabled) {
  302. data->irq_enabled = true;
  303. enable_irq(data->irq);
  304. }
  305. } else {
  306. regmap_write(map, soc_data->sensor_ctrl + REG_CLR,
  307. soc_data->measure_temp_mask);
  308. regmap_write(map, soc_data->sensor_ctrl + REG_SET,
  309. soc_data->power_down_mask);
  310. tz->polling_delay = 0;
  311. tz->passive_delay = 0;
  312. if (data->irq_enabled) {
  313. disable_irq(data->irq);
  314. data->irq_enabled = false;
  315. }
  316. }
  317. data->mode = mode;
  318. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  319. return 0;
  320. }
  321. static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
  322. enum thermal_trip_type *type)
  323. {
  324. *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
  325. THERMAL_TRIP_CRITICAL;
  326. return 0;
  327. }
  328. static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
  329. {
  330. struct imx_thermal_data *data = tz->devdata;
  331. *temp = data->temp_critical;
  332. return 0;
  333. }
  334. static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
  335. int *temp)
  336. {
  337. struct imx_thermal_data *data = tz->devdata;
  338. *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
  339. data->temp_critical;
  340. return 0;
  341. }
  342. static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
  343. int temp)
  344. {
  345. struct imx_thermal_data *data = tz->devdata;
  346. /* do not allow changing critical threshold */
  347. if (trip == IMX_TRIP_CRITICAL)
  348. return -EPERM;
  349. /* do not allow passive to be set higher than critical */
  350. if (temp < 0 || temp > data->temp_critical)
  351. return -EINVAL;
  352. data->temp_passive = temp;
  353. imx_set_alarm_temp(data, temp);
  354. return 0;
  355. }
  356. static int imx_bind(struct thermal_zone_device *tz,
  357. struct thermal_cooling_device *cdev)
  358. {
  359. int ret;
  360. ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
  361. THERMAL_NO_LIMIT,
  362. THERMAL_NO_LIMIT,
  363. THERMAL_WEIGHT_DEFAULT);
  364. if (ret) {
  365. dev_err(&tz->device,
  366. "binding zone %s with cdev %s failed:%d\n",
  367. tz->type, cdev->type, ret);
  368. return ret;
  369. }
  370. return 0;
  371. }
  372. static int imx_unbind(struct thermal_zone_device *tz,
  373. struct thermal_cooling_device *cdev)
  374. {
  375. int ret;
  376. ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
  377. if (ret) {
  378. dev_err(&tz->device,
  379. "unbinding zone %s with cdev %s failed:%d\n",
  380. tz->type, cdev->type, ret);
  381. return ret;
  382. }
  383. return 0;
  384. }
  385. static struct thermal_zone_device_ops imx_tz_ops = {
  386. .bind = imx_bind,
  387. .unbind = imx_unbind,
  388. .get_temp = imx_get_temp,
  389. .get_mode = imx_get_mode,
  390. .set_mode = imx_set_mode,
  391. .get_trip_type = imx_get_trip_type,
  392. .get_trip_temp = imx_get_trip_temp,
  393. .get_crit_temp = imx_get_crit_temp,
  394. .set_trip_temp = imx_set_trip_temp,
  395. };
  396. static int imx_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
  397. {
  398. struct imx_thermal_data *data = platform_get_drvdata(pdev);
  399. int n1;
  400. u64 temp64;
  401. if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
  402. dev_err(&pdev->dev, "invalid sensor calibration data\n");
  403. return -EINVAL;
  404. }
  405. /*
  406. * On i.MX7D, we only use the calibration data at 25C to get the temp,
  407. * Tmeas = ( Nmeas - n1) + 25; n1 is the fuse value for 25C.
  408. */
  409. if (data->socdata->version == TEMPMON_IMX7D) {
  410. data->c1 = (ocotp_ana1 >> 9) & 0x1ff;
  411. return 0;
  412. }
  413. /*
  414. * The sensor is calibrated at 25 °C (aka T1) and the value measured
  415. * (aka N1) at this temperature is provided in bits [31:20] in the
  416. * i.MX's OCOTP value ANA1.
  417. * To find the actual temperature T, the following formula has to be used
  418. * when reading value n from the sensor:
  419. *
  420. * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
  421. * = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
  422. * = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
  423. * = c2 - c1 * N
  424. *
  425. * with
  426. *
  427. * T1' = 28.580661 °C
  428. * c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
  429. * c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
  430. * = T1' + N1 * c1
  431. */
  432. n1 = ocotp_ana1 >> 20;
  433. temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
  434. temp64 *= 1000; /* to get result in °mC */
  435. do_div(temp64, 15423 * n1 - 4148468);
  436. data->c1 = temp64;
  437. data->c2 = n1 * data->c1 + 28581;
  438. return 0;
  439. }
  440. static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
  441. {
  442. struct imx_thermal_data *data = platform_get_drvdata(pdev);
  443. /* The maximum die temp is specified by the Temperature Grade */
  444. switch ((ocotp_mem0 >> 6) & 0x3) {
  445. case 0: /* Commercial (0 to 95 °C) */
  446. data->temp_grade = "Commercial";
  447. data->temp_max = 95000;
  448. break;
  449. case 1: /* Extended Commercial (-20 °C to 105 °C) */
  450. data->temp_grade = "Extended Commercial";
  451. data->temp_max = 105000;
  452. break;
  453. case 2: /* Industrial (-40 °C to 105 °C) */
  454. data->temp_grade = "Industrial";
  455. data->temp_max = 105000;
  456. break;
  457. case 3: /* Automotive (-40 °C to 125 °C) */
  458. data->temp_grade = "Automotive";
  459. data->temp_max = 125000;
  460. break;
  461. }
  462. /*
  463. * Set the critical trip point at 5 °C under max
  464. * Set the passive trip point at 10 °C under max (changeable via sysfs)
  465. */
  466. data->temp_critical = data->temp_max - (1000 * 5);
  467. data->temp_passive = data->temp_max - (1000 * 10);
  468. }
  469. static int imx_init_from_tempmon_data(struct platform_device *pdev)
  470. {
  471. struct regmap *map;
  472. int ret;
  473. u32 val;
  474. map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  475. "fsl,tempmon-data");
  476. if (IS_ERR(map)) {
  477. ret = PTR_ERR(map);
  478. dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
  479. return ret;
  480. }
  481. ret = regmap_read(map, OCOTP_ANA1, &val);
  482. if (ret) {
  483. dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
  484. return ret;
  485. }
  486. ret = imx_init_calib(pdev, val);
  487. if (ret)
  488. return ret;
  489. ret = regmap_read(map, OCOTP_MEM0, &val);
  490. if (ret) {
  491. dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
  492. return ret;
  493. }
  494. imx_init_temp_grade(pdev, val);
  495. return 0;
  496. }
  497. static int imx_init_from_nvmem_cells(struct platform_device *pdev)
  498. {
  499. int ret;
  500. u32 val;
  501. ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
  502. if (ret)
  503. return ret;
  504. ret = imx_init_calib(pdev, val);
  505. if (ret)
  506. return ret;
  507. ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
  508. if (ret)
  509. return ret;
  510. imx_init_temp_grade(pdev, val);
  511. return 0;
  512. }
  513. static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
  514. {
  515. struct imx_thermal_data *data = dev;
  516. disable_irq_nosync(irq);
  517. data->irq_enabled = false;
  518. return IRQ_WAKE_THREAD;
  519. }
  520. static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
  521. {
  522. struct imx_thermal_data *data = dev;
  523. dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
  524. data->alarm_temp / 1000);
  525. thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
  526. return IRQ_HANDLED;
  527. }
  528. static const struct of_device_id of_imx_thermal_match[] = {
  529. { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
  530. { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
  531. { .compatible = "fsl,imx7d-tempmon", .data = &thermal_imx7d_data, },
  532. { /* end */ }
  533. };
  534. MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
  535. /*
  536. * Create cooling device in case no #cooling-cells property is available in
  537. * CPU node
  538. */
  539. static int imx_thermal_register_legacy_cooling(struct imx_thermal_data *data)
  540. {
  541. struct device_node *np = of_get_cpu_node(data->policy->cpu, NULL);
  542. int ret;
  543. if (!np || !of_find_property(np, "#cooling-cells", NULL)) {
  544. data->cdev = cpufreq_cooling_register(data->policy);
  545. if (IS_ERR(data->cdev)) {
  546. ret = PTR_ERR(data->cdev);
  547. cpufreq_cpu_put(data->policy);
  548. return ret;
  549. }
  550. }
  551. return 0;
  552. }
  553. static int imx_thermal_probe(struct platform_device *pdev)
  554. {
  555. struct imx_thermal_data *data;
  556. struct regmap *map;
  557. int measure_freq;
  558. int ret;
  559. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  560. if (!data)
  561. return -ENOMEM;
  562. map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
  563. if (IS_ERR(map)) {
  564. ret = PTR_ERR(map);
  565. dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
  566. return ret;
  567. }
  568. data->tempmon = map;
  569. data->socdata = of_device_get_match_data(&pdev->dev);
  570. if (!data->socdata) {
  571. dev_err(&pdev->dev, "no device match found\n");
  572. return -ENODEV;
  573. }
  574. /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
  575. if (data->socdata->version == TEMPMON_IMX6SX) {
  576. regmap_write(map, IMX6_MISC1 + REG_CLR,
  577. IMX6_MISC1_IRQ_TEMPHIGH | IMX6_MISC1_IRQ_TEMPLOW
  578. | IMX6_MISC1_IRQ_TEMPPANIC);
  579. /*
  580. * reset value of LOW ALARM is incorrect, set it to lowest
  581. * value to avoid false trigger of low alarm.
  582. */
  583. regmap_write(map, data->socdata->low_alarm_ctrl + REG_SET,
  584. data->socdata->low_alarm_mask);
  585. }
  586. data->irq = platform_get_irq(pdev, 0);
  587. if (data->irq < 0)
  588. return data->irq;
  589. platform_set_drvdata(pdev, data);
  590. if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
  591. ret = imx_init_from_nvmem_cells(pdev);
  592. if (ret == -EPROBE_DEFER)
  593. return ret;
  594. if (ret) {
  595. dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
  596. ret);
  597. return ret;
  598. }
  599. } else {
  600. ret = imx_init_from_tempmon_data(pdev);
  601. if (ret) {
  602. dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
  603. return ret;
  604. }
  605. }
  606. /* Make sure sensor is in known good state for measurements */
  607. regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
  608. data->socdata->power_down_mask);
  609. regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
  610. data->socdata->measure_temp_mask);
  611. regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
  612. data->socdata->measure_freq_mask);
  613. if (data->socdata->version != TEMPMON_IMX7D)
  614. regmap_write(map, IMX6_MISC0 + REG_SET,
  615. IMX6_MISC0_REFTOP_SELBIASOFF);
  616. regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
  617. data->socdata->power_down_mask);
  618. data->policy = cpufreq_cpu_get(0);
  619. if (!data->policy) {
  620. pr_debug("%s: CPUFreq policy not found\n", __func__);
  621. return -EPROBE_DEFER;
  622. }
  623. ret = imx_thermal_register_legacy_cooling(data);
  624. if (ret) {
  625. dev_err(&pdev->dev,
  626. "failed to register cpufreq cooling device: %d\n", ret);
  627. return ret;
  628. }
  629. data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
  630. if (IS_ERR(data->thermal_clk)) {
  631. ret = PTR_ERR(data->thermal_clk);
  632. if (ret != -EPROBE_DEFER)
  633. dev_err(&pdev->dev,
  634. "failed to get thermal clk: %d\n", ret);
  635. cpufreq_cooling_unregister(data->cdev);
  636. cpufreq_cpu_put(data->policy);
  637. return ret;
  638. }
  639. /*
  640. * Thermal sensor needs clk on to get correct value, normally
  641. * we should enable its clk before taking measurement and disable
  642. * clk after measurement is done, but if alarm function is enabled,
  643. * hardware will auto measure the temperature periodically, so we
  644. * need to keep the clk always on for alarm function.
  645. */
  646. ret = clk_prepare_enable(data->thermal_clk);
  647. if (ret) {
  648. dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
  649. cpufreq_cooling_unregister(data->cdev);
  650. cpufreq_cpu_put(data->policy);
  651. return ret;
  652. }
  653. data->tz = thermal_zone_device_register("imx_thermal_zone",
  654. IMX_TRIP_NUM,
  655. BIT(IMX_TRIP_PASSIVE), data,
  656. &imx_tz_ops, NULL,
  657. IMX_PASSIVE_DELAY,
  658. IMX_POLLING_DELAY);
  659. if (IS_ERR(data->tz)) {
  660. ret = PTR_ERR(data->tz);
  661. dev_err(&pdev->dev,
  662. "failed to register thermal zone device %d\n", ret);
  663. clk_disable_unprepare(data->thermal_clk);
  664. cpufreq_cooling_unregister(data->cdev);
  665. cpufreq_cpu_put(data->policy);
  666. return ret;
  667. }
  668. dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
  669. " critical:%dC passive:%dC\n", data->temp_grade,
  670. data->temp_max / 1000, data->temp_critical / 1000,
  671. data->temp_passive / 1000);
  672. /* Enable measurements at ~ 10 Hz */
  673. regmap_write(map, data->socdata->measure_freq_ctrl + REG_CLR,
  674. data->socdata->measure_freq_mask);
  675. measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
  676. regmap_write(map, data->socdata->measure_freq_ctrl + REG_SET,
  677. measure_freq << data->socdata->measure_freq_shift);
  678. imx_set_alarm_temp(data, data->temp_passive);
  679. if (data->socdata->version == TEMPMON_IMX6SX)
  680. imx_set_panic_temp(data, data->temp_critical);
  681. regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
  682. data->socdata->power_down_mask);
  683. regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
  684. data->socdata->measure_temp_mask);
  685. data->irq_enabled = true;
  686. data->mode = THERMAL_DEVICE_ENABLED;
  687. ret = devm_request_threaded_irq(&pdev->dev, data->irq,
  688. imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
  689. 0, "imx_thermal", data);
  690. if (ret < 0) {
  691. dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
  692. clk_disable_unprepare(data->thermal_clk);
  693. thermal_zone_device_unregister(data->tz);
  694. cpufreq_cooling_unregister(data->cdev);
  695. cpufreq_cpu_put(data->policy);
  696. return ret;
  697. }
  698. return 0;
  699. }
  700. static int imx_thermal_remove(struct platform_device *pdev)
  701. {
  702. struct imx_thermal_data *data = platform_get_drvdata(pdev);
  703. struct regmap *map = data->tempmon;
  704. /* Disable measurements */
  705. regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
  706. data->socdata->power_down_mask);
  707. if (!IS_ERR(data->thermal_clk))
  708. clk_disable_unprepare(data->thermal_clk);
  709. thermal_zone_device_unregister(data->tz);
  710. cpufreq_cooling_unregister(data->cdev);
  711. cpufreq_cpu_put(data->policy);
  712. return 0;
  713. }
  714. #ifdef CONFIG_PM_SLEEP
  715. static int imx_thermal_suspend(struct device *dev)
  716. {
  717. struct imx_thermal_data *data = dev_get_drvdata(dev);
  718. struct regmap *map = data->tempmon;
  719. /*
  720. * Need to disable thermal sensor, otherwise, when thermal core
  721. * try to get temperature before thermal sensor resume, a wrong
  722. * temperature will be read as the thermal sensor is powered
  723. * down.
  724. */
  725. regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
  726. data->socdata->measure_temp_mask);
  727. regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
  728. data->socdata->power_down_mask);
  729. data->mode = THERMAL_DEVICE_DISABLED;
  730. clk_disable_unprepare(data->thermal_clk);
  731. return 0;
  732. }
  733. static int imx_thermal_resume(struct device *dev)
  734. {
  735. struct imx_thermal_data *data = dev_get_drvdata(dev);
  736. struct regmap *map = data->tempmon;
  737. int ret;
  738. ret = clk_prepare_enable(data->thermal_clk);
  739. if (ret)
  740. return ret;
  741. /* Enabled thermal sensor after resume */
  742. regmap_write(map, data->socdata->sensor_ctrl + REG_CLR,
  743. data->socdata->power_down_mask);
  744. regmap_write(map, data->socdata->sensor_ctrl + REG_SET,
  745. data->socdata->measure_temp_mask);
  746. data->mode = THERMAL_DEVICE_ENABLED;
  747. return 0;
  748. }
  749. #endif
  750. static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
  751. imx_thermal_suspend, imx_thermal_resume);
  752. static struct platform_driver imx_thermal = {
  753. .driver = {
  754. .name = "imx_thermal",
  755. .pm = &imx_thermal_pm_ops,
  756. .of_match_table = of_imx_thermal_match,
  757. },
  758. .probe = imx_thermal_probe,
  759. .remove = imx_thermal_remove,
  760. };
  761. module_platform_driver(imx_thermal);
  762. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  763. MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
  764. MODULE_LICENSE("GPL v2");
  765. MODULE_ALIAS("platform:imx-thermal");