imx_thermal.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. unsigned long temp_passive;
  87. unsigned long temp_critical;
  88. unsigned long alarm_temp;
  89. unsigned long 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. signed long 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. signed long 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, unsigned long *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 < %lu\n",
  164. data->alarm_temp / 1000);
  165. }
  166. }
  167. if (*temp != data->last_temp) {
  168. dev_dbg(&tz->device, "millicelsius: %ld\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,
  221. unsigned long *temp)
  222. {
  223. struct imx_thermal_data *data = tz->devdata;
  224. *temp = data->temp_critical;
  225. return 0;
  226. }
  227. static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
  228. unsigned long *temp)
  229. {
  230. struct imx_thermal_data *data = tz->devdata;
  231. *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
  232. data->temp_critical;
  233. return 0;
  234. }
  235. static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
  236. unsigned long temp)
  237. {
  238. struct imx_thermal_data *data = tz->devdata;
  239. if (trip == IMX_TRIP_CRITICAL)
  240. return -EPERM;
  241. if (temp > IMX_TEMP_PASSIVE)
  242. return -EINVAL;
  243. data->temp_passive = temp;
  244. imx_set_alarm_temp(data, temp);
  245. return 0;
  246. }
  247. static int imx_bind(struct thermal_zone_device *tz,
  248. struct thermal_cooling_device *cdev)
  249. {
  250. int ret;
  251. ret = thermal_zone_bind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev,
  252. THERMAL_NO_LIMIT,
  253. THERMAL_NO_LIMIT,
  254. THERMAL_WEIGHT_DEFAULT);
  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 regmap *map;
  379. int measure_freq;
  380. int ret;
  381. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  382. if (!data)
  383. return -ENOMEM;
  384. map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
  385. if (IS_ERR(map)) {
  386. ret = PTR_ERR(map);
  387. dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
  388. return ret;
  389. }
  390. data->tempmon = map;
  391. data->socdata = of_id->data;
  392. /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
  393. if (data->socdata->version == TEMPMON_IMX6SX) {
  394. regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH |
  395. MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC);
  396. /*
  397. * reset value of LOW ALARM is incorrect, set it to lowest
  398. * value to avoid false trigger of low alarm.
  399. */
  400. regmap_write(map, TEMPSENSE2 + REG_SET,
  401. TEMPSENSE2_LOW_VALUE_MASK);
  402. }
  403. data->irq = platform_get_irq(pdev, 0);
  404. if (data->irq < 0)
  405. return data->irq;
  406. ret = devm_request_threaded_irq(&pdev->dev, data->irq,
  407. imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
  408. 0, "imx_thermal", data);
  409. if (ret < 0) {
  410. dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
  411. return ret;
  412. }
  413. platform_set_drvdata(pdev, data);
  414. ret = imx_get_sensor_data(pdev);
  415. if (ret) {
  416. dev_err(&pdev->dev, "failed to get sensor data\n");
  417. return ret;
  418. }
  419. /* Make sure sensor is in known good state for measurements */
  420. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  421. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  422. regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
  423. regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
  424. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  425. data->cdev = cpufreq_cooling_register(cpu_present_mask);
  426. if (IS_ERR(data->cdev)) {
  427. ret = PTR_ERR(data->cdev);
  428. if (ret != -EPROBE_DEFER)
  429. dev_err(&pdev->dev,
  430. "failed to register cpufreq cooling device: %d\n",
  431. ret);
  432. return ret;
  433. }
  434. data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
  435. if (IS_ERR(data->thermal_clk)) {
  436. ret = PTR_ERR(data->thermal_clk);
  437. if (ret != -EPROBE_DEFER)
  438. dev_err(&pdev->dev,
  439. "failed to get thermal clk: %d\n", ret);
  440. cpufreq_cooling_unregister(data->cdev);
  441. return ret;
  442. }
  443. /*
  444. * Thermal sensor needs clk on to get correct value, normally
  445. * we should enable its clk before taking measurement and disable
  446. * clk after measurement is done, but if alarm function is enabled,
  447. * hardware will auto measure the temperature periodically, so we
  448. * need to keep the clk always on for alarm function.
  449. */
  450. ret = clk_prepare_enable(data->thermal_clk);
  451. if (ret) {
  452. dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
  453. cpufreq_cooling_unregister(data->cdev);
  454. return ret;
  455. }
  456. data->tz = thermal_zone_device_register("imx_thermal_zone",
  457. IMX_TRIP_NUM,
  458. BIT(IMX_TRIP_PASSIVE), data,
  459. &imx_tz_ops, NULL,
  460. IMX_PASSIVE_DELAY,
  461. IMX_POLLING_DELAY);
  462. if (IS_ERR(data->tz)) {
  463. ret = PTR_ERR(data->tz);
  464. dev_err(&pdev->dev,
  465. "failed to register thermal zone device %d\n", ret);
  466. clk_disable_unprepare(data->thermal_clk);
  467. cpufreq_cooling_unregister(data->cdev);
  468. return ret;
  469. }
  470. /* Enable measurements at ~ 10 Hz */
  471. regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
  472. measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
  473. regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
  474. imx_set_alarm_temp(data, data->temp_passive);
  475. if (data->socdata->version == TEMPMON_IMX6SX)
  476. imx_set_panic_temp(data, data->temp_critical);
  477. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  478. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  479. data->irq_enabled = true;
  480. data->mode = THERMAL_DEVICE_ENABLED;
  481. return 0;
  482. }
  483. static int imx_thermal_remove(struct platform_device *pdev)
  484. {
  485. struct imx_thermal_data *data = platform_get_drvdata(pdev);
  486. struct regmap *map = data->tempmon;
  487. /* Disable measurements */
  488. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  489. if (!IS_ERR(data->thermal_clk))
  490. clk_disable_unprepare(data->thermal_clk);
  491. thermal_zone_device_unregister(data->tz);
  492. cpufreq_cooling_unregister(data->cdev);
  493. return 0;
  494. }
  495. #ifdef CONFIG_PM_SLEEP
  496. static int imx_thermal_suspend(struct device *dev)
  497. {
  498. struct imx_thermal_data *data = dev_get_drvdata(dev);
  499. struct regmap *map = data->tempmon;
  500. /*
  501. * Need to disable thermal sensor, otherwise, when thermal core
  502. * try to get temperature before thermal sensor resume, a wrong
  503. * temperature will be read as the thermal sensor is powered
  504. * down.
  505. */
  506. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  507. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  508. data->mode = THERMAL_DEVICE_DISABLED;
  509. clk_disable_unprepare(data->thermal_clk);
  510. return 0;
  511. }
  512. static int imx_thermal_resume(struct device *dev)
  513. {
  514. struct imx_thermal_data *data = dev_get_drvdata(dev);
  515. struct regmap *map = data->tempmon;
  516. clk_prepare_enable(data->thermal_clk);
  517. /* Enabled thermal sensor after resume */
  518. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  519. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  520. data->mode = THERMAL_DEVICE_ENABLED;
  521. return 0;
  522. }
  523. #endif
  524. static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
  525. imx_thermal_suspend, imx_thermal_resume);
  526. static struct platform_driver imx_thermal = {
  527. .driver = {
  528. .name = "imx_thermal",
  529. .pm = &imx_thermal_pm_ops,
  530. .of_match_table = of_imx_thermal_match,
  531. },
  532. .probe = imx_thermal_probe,
  533. .remove = imx_thermal_remove,
  534. };
  535. module_platform_driver(imx_thermal);
  536. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  537. MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
  538. MODULE_LICENSE("GPL v2");
  539. MODULE_ALIAS("platform:imx-thermal");