imx_thermal.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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_MEM0 0x0480
  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. #define IMX_POLLING_DELAY 2000 /* millisecond */
  61. #define IMX_PASSIVE_DELAY 1000
  62. #define FACTOR0 10000000
  63. #define FACTOR1 15976
  64. #define FACTOR2 4297157
  65. #define TEMPMON_IMX6Q 1
  66. #define TEMPMON_IMX6SX 2
  67. struct thermal_soc_data {
  68. u32 version;
  69. };
  70. static struct thermal_soc_data thermal_imx6q_data = {
  71. .version = TEMPMON_IMX6Q,
  72. };
  73. static struct thermal_soc_data thermal_imx6sx_data = {
  74. .version = TEMPMON_IMX6SX,
  75. };
  76. struct imx_thermal_data {
  77. struct thermal_zone_device *tz;
  78. struct thermal_cooling_device *cdev;
  79. enum thermal_device_mode mode;
  80. struct regmap *tempmon;
  81. u32 c1, c2; /* See formula in imx_get_sensor_data() */
  82. int temp_passive;
  83. int temp_critical;
  84. int temp_max;
  85. int alarm_temp;
  86. int last_temp;
  87. bool irq_enabled;
  88. int irq;
  89. struct clk *thermal_clk;
  90. const struct thermal_soc_data *socdata;
  91. const char *temp_grade;
  92. };
  93. static void imx_set_panic_temp(struct imx_thermal_data *data,
  94. int panic_temp)
  95. {
  96. struct regmap *map = data->tempmon;
  97. int critical_value;
  98. critical_value = (data->c2 - panic_temp) / data->c1;
  99. regmap_write(map, TEMPSENSE2 + REG_CLR, TEMPSENSE2_PANIC_VALUE_MASK);
  100. regmap_write(map, TEMPSENSE2 + REG_SET, critical_value <<
  101. TEMPSENSE2_PANIC_VALUE_SHIFT);
  102. }
  103. static void imx_set_alarm_temp(struct imx_thermal_data *data,
  104. int alarm_temp)
  105. {
  106. struct regmap *map = data->tempmon;
  107. int alarm_value;
  108. data->alarm_temp = alarm_temp;
  109. alarm_value = (data->c2 - alarm_temp) / data->c1;
  110. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_ALARM_VALUE_MASK);
  111. regmap_write(map, TEMPSENSE0 + REG_SET, alarm_value <<
  112. TEMPSENSE0_ALARM_VALUE_SHIFT);
  113. }
  114. static int imx_get_temp(struct thermal_zone_device *tz, int *temp)
  115. {
  116. struct imx_thermal_data *data = tz->devdata;
  117. struct regmap *map = data->tempmon;
  118. unsigned int n_meas;
  119. bool wait;
  120. u32 val;
  121. if (data->mode == THERMAL_DEVICE_ENABLED) {
  122. /* Check if a measurement is currently in progress */
  123. regmap_read(map, TEMPSENSE0, &val);
  124. wait = !(val & TEMPSENSE0_FINISHED);
  125. } else {
  126. /*
  127. * Every time we measure the temperature, we will power on the
  128. * temperature sensor, enable measurements, take a reading,
  129. * disable measurements, power off the temperature sensor.
  130. */
  131. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  132. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  133. wait = true;
  134. }
  135. /*
  136. * According to the temp sensor designers, it may require up to ~17us
  137. * to complete a measurement.
  138. */
  139. if (wait)
  140. usleep_range(20, 50);
  141. regmap_read(map, TEMPSENSE0, &val);
  142. if (data->mode != THERMAL_DEVICE_ENABLED) {
  143. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  144. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  145. }
  146. if ((val & TEMPSENSE0_FINISHED) == 0) {
  147. dev_dbg(&tz->device, "temp measurement never finished\n");
  148. return -EAGAIN;
  149. }
  150. n_meas = (val & TEMPSENSE0_TEMP_CNT_MASK) >> TEMPSENSE0_TEMP_CNT_SHIFT;
  151. /* See imx_get_sensor_data() for formula derivation */
  152. *temp = data->c2 - n_meas * data->c1;
  153. /* Update alarm value to next higher trip point for TEMPMON_IMX6Q */
  154. if (data->socdata->version == TEMPMON_IMX6Q) {
  155. if (data->alarm_temp == data->temp_passive &&
  156. *temp >= data->temp_passive)
  157. imx_set_alarm_temp(data, data->temp_critical);
  158. if (data->alarm_temp == data->temp_critical &&
  159. *temp < data->temp_passive) {
  160. imx_set_alarm_temp(data, data->temp_passive);
  161. dev_dbg(&tz->device, "thermal alarm off: T < %d\n",
  162. data->alarm_temp / 1000);
  163. }
  164. }
  165. if (*temp != data->last_temp) {
  166. dev_dbg(&tz->device, "millicelsius: %d\n", *temp);
  167. data->last_temp = *temp;
  168. }
  169. /* Reenable alarm IRQ if temperature below alarm temperature */
  170. if (!data->irq_enabled && *temp < data->alarm_temp) {
  171. data->irq_enabled = true;
  172. enable_irq(data->irq);
  173. }
  174. return 0;
  175. }
  176. static int imx_get_mode(struct thermal_zone_device *tz,
  177. enum thermal_device_mode *mode)
  178. {
  179. struct imx_thermal_data *data = tz->devdata;
  180. *mode = data->mode;
  181. return 0;
  182. }
  183. static int imx_set_mode(struct thermal_zone_device *tz,
  184. enum thermal_device_mode mode)
  185. {
  186. struct imx_thermal_data *data = tz->devdata;
  187. struct regmap *map = data->tempmon;
  188. if (mode == THERMAL_DEVICE_ENABLED) {
  189. tz->polling_delay = IMX_POLLING_DELAY;
  190. tz->passive_delay = IMX_PASSIVE_DELAY;
  191. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  192. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  193. if (!data->irq_enabled) {
  194. data->irq_enabled = true;
  195. enable_irq(data->irq);
  196. }
  197. } else {
  198. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  199. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  200. tz->polling_delay = 0;
  201. tz->passive_delay = 0;
  202. if (data->irq_enabled) {
  203. disable_irq(data->irq);
  204. data->irq_enabled = false;
  205. }
  206. }
  207. data->mode = mode;
  208. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  209. return 0;
  210. }
  211. static int imx_get_trip_type(struct thermal_zone_device *tz, int trip,
  212. enum thermal_trip_type *type)
  213. {
  214. *type = (trip == IMX_TRIP_PASSIVE) ? THERMAL_TRIP_PASSIVE :
  215. THERMAL_TRIP_CRITICAL;
  216. return 0;
  217. }
  218. static int imx_get_crit_temp(struct thermal_zone_device *tz, int *temp)
  219. {
  220. struct imx_thermal_data *data = tz->devdata;
  221. *temp = data->temp_critical;
  222. return 0;
  223. }
  224. static int imx_get_trip_temp(struct thermal_zone_device *tz, int trip,
  225. int *temp)
  226. {
  227. struct imx_thermal_data *data = tz->devdata;
  228. *temp = (trip == IMX_TRIP_PASSIVE) ? data->temp_passive :
  229. data->temp_critical;
  230. return 0;
  231. }
  232. static int imx_set_trip_temp(struct thermal_zone_device *tz, int trip,
  233. int temp)
  234. {
  235. struct imx_thermal_data *data = tz->devdata;
  236. /* do not allow changing critical threshold */
  237. if (trip == IMX_TRIP_CRITICAL)
  238. return -EPERM;
  239. /* do not allow passive to be set higher than critical */
  240. if (temp < 0 || temp > data->temp_critical)
  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. /* use OTP for thermal grade */
  340. ret = regmap_read(map, OCOTP_MEM0, &val);
  341. if (ret) {
  342. dev_err(&pdev->dev, "failed to read temp grade: %d\n", ret);
  343. return ret;
  344. }
  345. /* The maximum die temp is specified by the Temperature Grade */
  346. switch ((val >> 6) & 0x3) {
  347. case 0: /* Commercial (0 to 95C) */
  348. data->temp_grade = "Commercial";
  349. data->temp_max = 95000;
  350. break;
  351. case 1: /* Extended Commercial (-20 to 105C) */
  352. data->temp_grade = "Extended Commercial";
  353. data->temp_max = 105000;
  354. break;
  355. case 2: /* Industrial (-40 to 105C) */
  356. data->temp_grade = "Industrial";
  357. data->temp_max = 105000;
  358. break;
  359. case 3: /* Automotive (-40 to 125C) */
  360. data->temp_grade = "Automotive";
  361. data->temp_max = 125000;
  362. break;
  363. }
  364. /*
  365. * Set the critical trip point at 5C under max
  366. * Set the passive trip point at 10C under max (can change via sysfs)
  367. */
  368. data->temp_critical = data->temp_max - (1000 * 5);
  369. data->temp_passive = data->temp_max - (1000 * 10);
  370. return 0;
  371. }
  372. static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
  373. {
  374. struct imx_thermal_data *data = dev;
  375. disable_irq_nosync(irq);
  376. data->irq_enabled = false;
  377. return IRQ_WAKE_THREAD;
  378. }
  379. static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
  380. {
  381. struct imx_thermal_data *data = dev;
  382. dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
  383. data->alarm_temp / 1000);
  384. thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
  385. return IRQ_HANDLED;
  386. }
  387. static const struct of_device_id of_imx_thermal_match[] = {
  388. { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
  389. { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
  390. { /* end */ }
  391. };
  392. MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
  393. static int imx_thermal_probe(struct platform_device *pdev)
  394. {
  395. struct imx_thermal_data *data;
  396. struct regmap *map;
  397. int measure_freq;
  398. int ret;
  399. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  400. if (!data)
  401. return -ENOMEM;
  402. map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
  403. if (IS_ERR(map)) {
  404. ret = PTR_ERR(map);
  405. dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
  406. return ret;
  407. }
  408. data->tempmon = map;
  409. data->socdata = of_device_get_match_data(&pdev->dev);
  410. /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
  411. if (data->socdata->version == TEMPMON_IMX6SX) {
  412. regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH |
  413. MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC);
  414. /*
  415. * reset value of LOW ALARM is incorrect, set it to lowest
  416. * value to avoid false trigger of low alarm.
  417. */
  418. regmap_write(map, TEMPSENSE2 + REG_SET,
  419. TEMPSENSE2_LOW_VALUE_MASK);
  420. }
  421. data->irq = platform_get_irq(pdev, 0);
  422. if (data->irq < 0)
  423. return data->irq;
  424. platform_set_drvdata(pdev, data);
  425. ret = imx_get_sensor_data(pdev);
  426. if (ret) {
  427. dev_err(&pdev->dev, "failed to get sensor data\n");
  428. return ret;
  429. }
  430. /* Make sure sensor is in known good state for measurements */
  431. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  432. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  433. regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
  434. regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
  435. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  436. data->cdev = cpufreq_cooling_register(cpu_present_mask);
  437. if (IS_ERR(data->cdev)) {
  438. ret = PTR_ERR(data->cdev);
  439. if (ret != -EPROBE_DEFER)
  440. dev_err(&pdev->dev,
  441. "failed to register cpufreq cooling device: %d\n",
  442. ret);
  443. return ret;
  444. }
  445. data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
  446. if (IS_ERR(data->thermal_clk)) {
  447. ret = PTR_ERR(data->thermal_clk);
  448. if (ret != -EPROBE_DEFER)
  449. dev_err(&pdev->dev,
  450. "failed to get thermal clk: %d\n", ret);
  451. cpufreq_cooling_unregister(data->cdev);
  452. return ret;
  453. }
  454. /*
  455. * Thermal sensor needs clk on to get correct value, normally
  456. * we should enable its clk before taking measurement and disable
  457. * clk after measurement is done, but if alarm function is enabled,
  458. * hardware will auto measure the temperature periodically, so we
  459. * need to keep the clk always on for alarm function.
  460. */
  461. ret = clk_prepare_enable(data->thermal_clk);
  462. if (ret) {
  463. dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
  464. cpufreq_cooling_unregister(data->cdev);
  465. return ret;
  466. }
  467. data->tz = thermal_zone_device_register("imx_thermal_zone",
  468. IMX_TRIP_NUM,
  469. BIT(IMX_TRIP_PASSIVE), data,
  470. &imx_tz_ops, NULL,
  471. IMX_PASSIVE_DELAY,
  472. IMX_POLLING_DELAY);
  473. if (IS_ERR(data->tz)) {
  474. ret = PTR_ERR(data->tz);
  475. dev_err(&pdev->dev,
  476. "failed to register thermal zone device %d\n", ret);
  477. clk_disable_unprepare(data->thermal_clk);
  478. cpufreq_cooling_unregister(data->cdev);
  479. return ret;
  480. }
  481. dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
  482. " critical:%dC passive:%dC\n", data->temp_grade,
  483. data->temp_max / 1000, data->temp_critical / 1000,
  484. data->temp_passive / 1000);
  485. /* Enable measurements at ~ 10 Hz */
  486. regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
  487. measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
  488. regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
  489. imx_set_alarm_temp(data, data->temp_passive);
  490. if (data->socdata->version == TEMPMON_IMX6SX)
  491. imx_set_panic_temp(data, data->temp_critical);
  492. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  493. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  494. data->irq_enabled = true;
  495. data->mode = THERMAL_DEVICE_ENABLED;
  496. ret = devm_request_threaded_irq(&pdev->dev, data->irq,
  497. imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
  498. 0, "imx_thermal", data);
  499. if (ret < 0) {
  500. dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
  501. clk_disable_unprepare(data->thermal_clk);
  502. thermal_zone_device_unregister(data->tz);
  503. cpufreq_cooling_unregister(data->cdev);
  504. return ret;
  505. }
  506. return 0;
  507. }
  508. static int imx_thermal_remove(struct platform_device *pdev)
  509. {
  510. struct imx_thermal_data *data = platform_get_drvdata(pdev);
  511. struct regmap *map = data->tempmon;
  512. /* Disable measurements */
  513. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  514. if (!IS_ERR(data->thermal_clk))
  515. clk_disable_unprepare(data->thermal_clk);
  516. thermal_zone_device_unregister(data->tz);
  517. cpufreq_cooling_unregister(data->cdev);
  518. return 0;
  519. }
  520. #ifdef CONFIG_PM_SLEEP
  521. static int imx_thermal_suspend(struct device *dev)
  522. {
  523. struct imx_thermal_data *data = dev_get_drvdata(dev);
  524. struct regmap *map = data->tempmon;
  525. /*
  526. * Need to disable thermal sensor, otherwise, when thermal core
  527. * try to get temperature before thermal sensor resume, a wrong
  528. * temperature will be read as the thermal sensor is powered
  529. * down.
  530. */
  531. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  532. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  533. data->mode = THERMAL_DEVICE_DISABLED;
  534. clk_disable_unprepare(data->thermal_clk);
  535. return 0;
  536. }
  537. static int imx_thermal_resume(struct device *dev)
  538. {
  539. struct imx_thermal_data *data = dev_get_drvdata(dev);
  540. struct regmap *map = data->tempmon;
  541. clk_prepare_enable(data->thermal_clk);
  542. /* Enabled thermal sensor after resume */
  543. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  544. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  545. data->mode = THERMAL_DEVICE_ENABLED;
  546. return 0;
  547. }
  548. #endif
  549. static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
  550. imx_thermal_suspend, imx_thermal_resume);
  551. static struct platform_driver imx_thermal = {
  552. .driver = {
  553. .name = "imx_thermal",
  554. .pm = &imx_thermal_pm_ops,
  555. .of_match_table = of_imx_thermal_match,
  556. },
  557. .probe = imx_thermal_probe,
  558. .remove = imx_thermal_remove,
  559. };
  560. module_platform_driver(imx_thermal);
  561. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  562. MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
  563. MODULE_LICENSE("GPL v2");
  564. MODULE_ALIAS("platform:imx-thermal");