imx_thermal.c 17 KB

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