imx_thermal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * Copyright 2013 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/cpu_cooling.h>
  10. #include <linux/cpufreq.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #include <linux/thermal.h>
  24. #include <linux/types.h>
  25. #define REG_SET 0x4
  26. #define REG_CLR 0x8
  27. #define REG_TOG 0xc
  28. #define MISC0 0x0150
  29. #define MISC0_REFTOP_SELBIASOFF (1 << 3)
  30. #define TEMPSENSE0 0x0180
  31. #define TEMPSENSE0_ALARM_VALUE_SHIFT 20
  32. #define TEMPSENSE0_ALARM_VALUE_MASK (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT)
  33. #define TEMPSENSE0_TEMP_CNT_SHIFT 8
  34. #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
  35. #define TEMPSENSE0_FINISHED (1 << 2)
  36. #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
  37. #define TEMPSENSE0_POWER_DOWN (1 << 0)
  38. #define TEMPSENSE1 0x0190
  39. #define TEMPSENSE1_MEASURE_FREQ 0xffff
  40. #define OCOTP_ANA1 0x04e0
  41. /* The driver supports 1 passive trip point and 1 critical trip point */
  42. enum imx_thermal_trip {
  43. IMX_TRIP_PASSIVE,
  44. IMX_TRIP_CRITICAL,
  45. IMX_TRIP_NUM,
  46. };
  47. /*
  48. * It defines the temperature in millicelsius for passive trip point
  49. * that will trigger cooling action when crossed.
  50. */
  51. #define IMX_TEMP_PASSIVE 85000
  52. #define IMX_POLLING_DELAY 2000 /* millisecond */
  53. #define IMX_PASSIVE_DELAY 1000
  54. struct imx_thermal_data {
  55. struct thermal_zone_device *tz;
  56. struct thermal_cooling_device *cdev;
  57. enum thermal_device_mode mode;
  58. struct regmap *tempmon;
  59. int c1, c2; /* See formula in imx_get_sensor_data() */
  60. unsigned long temp_passive;
  61. unsigned long temp_critical;
  62. unsigned long alarm_temp;
  63. unsigned long last_temp;
  64. bool irq_enabled;
  65. int irq;
  66. };
  67. static void imx_set_alarm_temp(struct imx_thermal_data *data,
  68. signed long alarm_temp)
  69. {
  70. struct regmap *map = data->tempmon;
  71. int alarm_value;
  72. data->alarm_temp = alarm_temp;
  73. alarm_value = (alarm_temp - data->c2) / data->c1;
  74. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_ALARM_VALUE_MASK);
  75. regmap_write(map, TEMPSENSE0 + REG_SET, alarm_value <<
  76. TEMPSENSE0_ALARM_VALUE_SHIFT);
  77. }
  78. static int imx_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
  79. {
  80. struct imx_thermal_data *data = tz->devdata;
  81. struct regmap *map = data->tempmon;
  82. unsigned int n_meas;
  83. bool wait;
  84. u32 val;
  85. if (data->mode == THERMAL_DEVICE_ENABLED) {
  86. /* Check if a measurement is currently in progress */
  87. regmap_read(map, TEMPSENSE0, &val);
  88. wait = !(val & TEMPSENSE0_FINISHED);
  89. } else {
  90. /*
  91. * Every time we measure the temperature, we will power on the
  92. * temperature sensor, enable measurements, take a reading,
  93. * disable measurements, power off the temperature sensor.
  94. */
  95. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  96. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  97. wait = true;
  98. }
  99. /*
  100. * According to the temp sensor designers, it may require up to ~17us
  101. * to complete a measurement.
  102. */
  103. if (wait)
  104. usleep_range(20, 50);
  105. regmap_read(map, TEMPSENSE0, &val);
  106. if (data->mode != THERMAL_DEVICE_ENABLED) {
  107. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  108. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  109. }
  110. if ((val & TEMPSENSE0_FINISHED) == 0) {
  111. dev_dbg(&tz->device, "temp measurement never finished\n");
  112. return -EAGAIN;
  113. }
  114. n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT;
  115. /* See imx_get_sensor_data() for formula derivation */
  116. *temp = data->c2 + data->c1 * n_meas;
  117. /* Update alarm value to next higher trip point */
  118. if (data->alarm_temp == data->temp_passive && *temp >= data->temp_passive)
  119. imx_set_alarm_temp(data, data->temp_critical);
  120. if (data->alarm_temp == data->temp_critical && *temp < data->temp_passive) {
  121. imx_set_alarm_temp(data, data->temp_passive);
  122. dev_dbg(&tz->device, "thermal alarm off: T < %lu\n",
  123. data->alarm_temp / 1000);
  124. }
  125. if (*temp != data->last_temp) {
  126. dev_dbg(&tz->device, "millicelsius: %ld\n", *temp);
  127. data->last_temp = *temp;
  128. }
  129. /* Reenable alarm IRQ if temperature below alarm temperature */
  130. if (!data->irq_enabled && *temp < data->alarm_temp) {
  131. data->irq_enabled = true;
  132. enable_irq(data->irq);
  133. }
  134. return 0;
  135. }
  136. static int imx_get_mode(struct thermal_zone_device *tz,
  137. enum thermal_device_mode *mode)
  138. {
  139. struct imx_thermal_data *data = tz->devdata;
  140. *mode = data->mode;
  141. return 0;
  142. }
  143. static int imx_set_mode(struct thermal_zone_device *tz,
  144. enum thermal_device_mode mode)
  145. {
  146. struct imx_thermal_data *data = tz->devdata;
  147. struct regmap *map = data->tempmon;
  148. if (mode == THERMAL_DEVICE_ENABLED) {
  149. tz->polling_delay = IMX_POLLING_DELAY;
  150. tz->passive_delay = IMX_PASSIVE_DELAY;
  151. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  152. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  153. if (!data->irq_enabled) {
  154. data->irq_enabled = true;
  155. enable_irq(data->irq);
  156. }
  157. } else {
  158. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  159. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  160. tz->polling_delay = 0;
  161. tz->passive_delay = 0;
  162. if (data->irq_enabled) {
  163. disable_irq(data->irq);
  164. data->irq_enabled = false;
  165. }
  166. }
  167. data->mode = mode;
  168. thermal_zone_device_update(tz);
  169. return 0;
  170. }
  171. static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
  172. enum thermal_trip_type *type)
  173. {
  174. *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
  175. THERMAL_TRIP_CRITICAL;
  176. return 0;
  177. }
  178. static int imx_get_crit_temp(struct thermal_zone_device *tz,
  179. unsigned long *temp)
  180. {
  181. struct imx_thermal_data *data = tz->devdata;
  182. *temp = data->temp_critical;
  183. return 0;
  184. }
  185. static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
  186. unsigned long *temp)
  187. {
  188. struct imx_thermal_data *data = tz->devdata;
  189. *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
  190. data->temp_critical;
  191. return 0;
  192. }
  193. static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
  194. unsigned long temp)
  195. {
  196. struct imx_thermal_data *data = tz->devdata;
  197. if (trip == IMX_TRIP_CRITICAL)
  198. return -EPERM;
  199. if (temp > IMX_TEMP_PASSIVE)
  200. return -EINVAL;
  201. data->temp_passive = temp;
  202. imx_set_alarm_temp(data, temp);
  203. return 0;
  204. }
  205. static int imx_bind(struct thermal_zone_device *tz,
  206. struct thermal_cooling_device *cdev)
  207. {
  208. int ret;
  209. ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
  210. THERMAL_NO_LIMIT,
  211. THERMAL_NO_LIMIT);
  212. if (ret) {
  213. dev_err(&tz->device,
  214. "binding zone %s with cdev %s failed:%d\n",
  215. tz->type, cdev->type, ret);
  216. return ret;
  217. }
  218. return 0;
  219. }
  220. static int imx_unbind(struct thermal_zone_device *tz,
  221. struct thermal_cooling_device *cdev)
  222. {
  223. int ret;
  224. ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
  225. if (ret) {
  226. dev_err(&tz->device,
  227. "unbinding zone %s with cdev %s failed:%d\n",
  228. tz->type, cdev->type, ret);
  229. return ret;
  230. }
  231. return 0;
  232. }
  233. static const struct thermal_zone_device_ops imx_tz_ops = {
  234. .bind = imx_bind,
  235. .unbind = imx_unbind,
  236. .get_temp = imx_get_temp,
  237. .get_mode = imx_get_mode,
  238. .set_mode = imx_set_mode,
  239. .get_trip_type = imx_get_trip_type,
  240. .get_trip_temp = imx_get_trip_temp,
  241. .get_crit_temp = imx_get_crit_temp,
  242. .set_trip_temp = imx_set_trip_temp,
  243. };
  244. static int imx_get_sensor_data(struct platform_device *pdev)
  245. {
  246. struct imx_thermal_data *data = platform_get_drvdata(pdev);
  247. struct regmap *map;
  248. int t1, t2, n1, n2;
  249. int ret;
  250. u32 val;
  251. map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  252. "fsl,tempmon-data");
  253. if (IS_ERR(map)) {
  254. ret = PTR_ERR(map);
  255. dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
  256. return ret;
  257. }
  258. ret = regmap_read(map, OCOTP_ANA1, &val);
  259. if (ret) {
  260. dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
  261. return ret;
  262. }
  263. if (val == 0 || val == ~0) {
  264. dev_err(&pdev->dev, "invalid sensor calibration data\n");
  265. return -EINVAL;
  266. }
  267. /*
  268. * Sensor data layout:
  269. * [31:20] - sensor value @ 25C
  270. * [19:8] - sensor value of hot
  271. * [7:0] - hot temperature value
  272. */
  273. n1 = val >> 20;
  274. n2 = (val & 0xfff00) >> 8;
  275. t2 = val & 0xff;
  276. t1 = 25; /* t1 always 25C */
  277. /*
  278. * Derived from linear interpolation,
  279. * Tmeas = T2 + (Nmeas - N2) * (T1 - T2) / (N1 - N2)
  280. * We want to reduce this down to the minimum computation necessary
  281. * for each temperature read. Also, we want Tmeas in millicelsius
  282. * and we don't want to lose precision from integer division. So...
  283. * milli_Tmeas = 1000 * T2 + 1000 * (Nmeas - N2) * (T1 - T2) / (N1 - N2)
  284. * Let constant c1 = 1000 * (T1 - T2) / (N1 - N2)
  285. * milli_Tmeas = (1000 * T2) + c1 * (Nmeas - N2)
  286. * milli_Tmeas = (1000 * T2) + (c1 * Nmeas) - (c1 * N2)
  287. * Let constant c2 = (1000 * T2) - (c1 * N2)
  288. * milli_Tmeas = c2 + (c1 * Nmeas)
  289. */
  290. data->c1 = 1000 * (t1 - t2) / (n1 - n2);
  291. data->c2 = 1000 * t2 - data->c1 * n2;
  292. /*
  293. * Set the default passive cooling trip point to 20 °C below the
  294. * maximum die temperature. Can be changed from userspace.
  295. */
  296. data->temp_passive = 1000 * (t2 - 20);
  297. /*
  298. * The maximum die temperature is t2, let's give 5 °C cushion
  299. * for noise and possible temperature rise between measurements.
  300. */
  301. data->temp_critical = 1000 * (t2 - 5);
  302. return 0;
  303. }
  304. static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
  305. {
  306. struct imx_thermal_data *data = dev;
  307. disable_irq_nosync(irq);
  308. data->irq_enabled = false;
  309. return IRQ_WAKE_THREAD;
  310. }
  311. static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
  312. {
  313. struct imx_thermal_data *data = dev;
  314. dev_dbg(&data->tz->device, "THERMAL ALARM: T > %lu\n",
  315. data->alarm_temp / 1000);
  316. thermal_zone_device_update(data->tz);
  317. return IRQ_HANDLED;
  318. }
  319. static int imx_thermal_probe(struct platform_device *pdev)
  320. {
  321. struct imx_thermal_data *data;
  322. struct cpumask clip_cpus;
  323. struct regmap *map;
  324. int measure_freq;
  325. int ret;
  326. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  327. if (!data)
  328. return -ENOMEM;
  329. map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
  330. if (IS_ERR(map)) {
  331. ret = PTR_ERR(map);
  332. dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
  333. return ret;
  334. }
  335. data->tempmon = map;
  336. data->irq = platform_get_irq(pdev, 0);
  337. if (data->irq < 0)
  338. return data->irq;
  339. ret = devm_request_threaded_irq(&pdev->dev, data->irq,
  340. imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
  341. 0, "imx_thermal", data);
  342. if (ret < 0) {
  343. dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
  344. return ret;
  345. }
  346. platform_set_drvdata(pdev, data);
  347. ret = imx_get_sensor_data(pdev);
  348. if (ret) {
  349. dev_err(&pdev->dev, "failed to get sensor data\n");
  350. return ret;
  351. }
  352. /* Make sure sensor is in known good state for measurements */
  353. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  354. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  355. regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
  356. regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
  357. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  358. cpumask_set_cpu(0, &clip_cpus);
  359. data->cdev = cpufreq_cooling_register(&clip_cpus);
  360. if (IS_ERR(data->cdev)) {
  361. ret = PTR_ERR(data->cdev);
  362. dev_err(&pdev->dev,
  363. "failed to register cpufreq cooling device: %d\n", ret);
  364. return ret;
  365. }
  366. data->tz = thermal_zone_device_register("imx_thermal_zone",
  367. IMX_TRIP_NUM,
  368. BIT(IMX_TRIP_PASSIVE), data,
  369. &imx_tz_ops, NULL,
  370. IMX_PASSIVE_DELAY,
  371. IMX_POLLING_DELAY);
  372. if (IS_ERR(data->tz)) {
  373. ret = PTR_ERR(data->tz);
  374. dev_err(&pdev->dev,
  375. "failed to register thermal zone device %d\n", ret);
  376. cpufreq_cooling_unregister(data->cdev);
  377. return ret;
  378. }
  379. /* Enable measurements at ~ 10 Hz */
  380. regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
  381. measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
  382. regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
  383. imx_set_alarm_temp(data, data->temp_passive);
  384. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  385. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  386. data->irq_enabled = true;
  387. data->mode = THERMAL_DEVICE_ENABLED;
  388. return 0;
  389. }
  390. static int imx_thermal_remove(struct platform_device *pdev)
  391. {
  392. struct imx_thermal_data *data = platform_get_drvdata(pdev);
  393. struct regmap *map = data->tempmon;
  394. /* Disable measurements */
  395. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  396. thermal_zone_device_unregister(data->tz);
  397. cpufreq_cooling_unregister(data->cdev);
  398. return 0;
  399. }
  400. #ifdef CONFIG_PM_SLEEP
  401. static int imx_thermal_suspend(struct device *dev)
  402. {
  403. struct imx_thermal_data *data = dev_get_drvdata(dev);
  404. struct regmap *map = data->tempmon;
  405. u32 val;
  406. regmap_read(map, TEMPSENSE0, &val);
  407. if ((val & TEMPSENSE0_POWER_DOWN) == 0) {
  408. /*
  409. * If a measurement is taking place, wait for a long enough
  410. * time for it to finish, and then check again. If it still
  411. * does not finish, something must go wrong.
  412. */
  413. udelay(50);
  414. regmap_read(map, TEMPSENSE0, &val);
  415. if ((val & TEMPSENSE0_POWER_DOWN) == 0)
  416. return -ETIMEDOUT;
  417. }
  418. return 0;
  419. }
  420. static int imx_thermal_resume(struct device *dev)
  421. {
  422. /* Nothing to do for now */
  423. return 0;
  424. }
  425. #endif
  426. static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
  427. imx_thermal_suspend, imx_thermal_resume);
  428. static const struct of_device_id of_imx_thermal_match[] = {
  429. { .compatible = "fsl,imx6q-tempmon", },
  430. { /* end */ }
  431. };
  432. static struct platform_driver imx_thermal = {
  433. .driver = {
  434. .name = "imx_thermal",
  435. .owner = THIS_MODULE,
  436. .pm = &imx_thermal_pm_ops,
  437. .of_match_table = of_imx_thermal_match,
  438. },
  439. .probe = imx_thermal_probe,
  440. .remove = imx_thermal_remove,
  441. };
  442. module_platform_driver(imx_thermal);
  443. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  444. MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
  445. MODULE_LICENSE("GPL v2");
  446. MODULE_ALIAS("platform:imx-thermal");