imx_thermal.c 15 KB

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