imx_thermal.c 15 KB

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