imx_thermal.c 17 KB

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