imx_thermal.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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/cpufreq.h>
  11. #include <linux/cpu_cooling.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_MEM0 0x0480
  54. #define OCOTP_ANA1 0x04e0
  55. /* The driver supports 1 passive trip point and 1 critical trip point */
  56. enum imx_thermal_trip {
  57. IMX_TRIP_PASSIVE,
  58. IMX_TRIP_CRITICAL,
  59. IMX_TRIP_NUM,
  60. };
  61. #define IMX_POLLING_DELAY 2000 /* millisecond */
  62. #define IMX_PASSIVE_DELAY 1000
  63. #define FACTOR0 10000000
  64. #define FACTOR1 15976
  65. #define FACTOR2 4297157
  66. #define TEMPMON_IMX6Q 1
  67. #define TEMPMON_IMX6SX 2
  68. struct thermal_soc_data {
  69. u32 version;
  70. };
  71. static struct thermal_soc_data thermal_imx6q_data = {
  72. .version = TEMPMON_IMX6Q,
  73. };
  74. static struct thermal_soc_data thermal_imx6sx_data = {
  75. .version = TEMPMON_IMX6SX,
  76. };
  77. struct imx_thermal_data {
  78. struct cpufreq_policy *policy;
  79. struct thermal_zone_device *tz;
  80. struct thermal_cooling_device *cdev;
  81. enum thermal_device_mode mode;
  82. struct regmap *tempmon;
  83. u32 c1, c2; /* See formula in imx_get_sensor_data() */
  84. int temp_passive;
  85. int temp_critical;
  86. int temp_max;
  87. int alarm_temp;
  88. int last_temp;
  89. bool irq_enabled;
  90. int irq;
  91. struct clk *thermal_clk;
  92. const struct thermal_soc_data *socdata;
  93. const char *temp_grade;
  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, THERMAL_EVENT_UNSPECIFIED);
  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. /* do not allow changing critical threshold */
  239. if (trip == IMX_TRIP_CRITICAL)
  240. return -EPERM;
  241. /* do not allow passive to be set higher than critical */
  242. if (temp < 0 || temp > data->temp_critical)
  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. THERMAL_WEIGHT_DEFAULT);
  256. if (ret) {
  257. dev_err(&tz->device,
  258. "binding zone %s with cdev %s failed:%d\n",
  259. tz->type, cdev->type, ret);
  260. return ret;
  261. }
  262. return 0;
  263. }
  264. static int imx_unbind(struct thermal_zone_device *tz,
  265. struct thermal_cooling_device *cdev)
  266. {
  267. int ret;
  268. ret = thermal_zone_unbind_cooling_device(tz, IMX_TRIP_PASSIVE, cdev);
  269. if (ret) {
  270. dev_err(&tz->device,
  271. "unbinding zone %s with cdev %s failed:%d\n",
  272. tz->type, cdev->type, ret);
  273. return ret;
  274. }
  275. return 0;
  276. }
  277. static struct thermal_zone_device_ops imx_tz_ops = {
  278. .bind = imx_bind,
  279. .unbind = imx_unbind,
  280. .get_temp = imx_get_temp,
  281. .get_mode = imx_get_mode,
  282. .set_mode = imx_set_mode,
  283. .get_trip_type = imx_get_trip_type,
  284. .get_trip_temp = imx_get_trip_temp,
  285. .get_crit_temp = imx_get_crit_temp,
  286. .set_trip_temp = imx_set_trip_temp,
  287. };
  288. static int imx_get_sensor_data(struct platform_device *pdev)
  289. {
  290. struct imx_thermal_data *data = platform_get_drvdata(pdev);
  291. struct regmap *map;
  292. int t1, n1;
  293. int ret;
  294. u32 val;
  295. u64 temp64;
  296. map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  297. "fsl,tempmon-data");
  298. if (IS_ERR(map)) {
  299. ret = PTR_ERR(map);
  300. dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
  301. return ret;
  302. }
  303. ret = regmap_read(map, OCOTP_ANA1, &val);
  304. if (ret) {
  305. dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
  306. return ret;
  307. }
  308. if (val == 0 || val == ~0) {
  309. dev_err(&pdev->dev, "invalid sensor calibration data\n");
  310. return -EINVAL;
  311. }
  312. /*
  313. * Sensor data layout:
  314. * [31:20] - sensor value @ 25C
  315. * Use universal formula now and only need sensor value @ 25C
  316. * slope = 0.4297157 - (0.0015976 * 25C fuse)
  317. */
  318. n1 = val >> 20;
  319. t1 = 25; /* t1 always 25C */
  320. /*
  321. * Derived from linear interpolation:
  322. * slope = 0.4297157 - (0.0015976 * 25C fuse)
  323. * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
  324. * (Nmeas - n1) / (Tmeas - t1) = slope
  325. * We want to reduce this down to the minimum computation necessary
  326. * for each temperature read. Also, we want Tmeas in millicelsius
  327. * and we don't want to lose precision from integer division. So...
  328. * Tmeas = (Nmeas - n1) / slope + t1
  329. * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
  330. * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
  331. * Let constant c1 = (-1000 / slope)
  332. * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
  333. * Let constant c2 = n1 *c1 + 1000 * t1
  334. * milli_Tmeas = c2 - Nmeas * c1
  335. */
  336. temp64 = FACTOR0;
  337. temp64 *= 1000;
  338. do_div(temp64, FACTOR1 * n1 - FACTOR2);
  339. data->c1 = temp64;
  340. data->c2 = n1 * data->c1 + 1000 * t1;
  341. /* use OTP for thermal grade */
  342. ret = regmap_read(map, OCOTP_MEM0, &val);
  343. if (ret) {
  344. dev_err(&pdev->dev, "failed to read temp grade: %d\n", ret);
  345. return ret;
  346. }
  347. /* The maximum die temp is specified by the Temperature Grade */
  348. switch ((val >> 6) & 0x3) {
  349. case 0: /* Commercial (0 to 95C) */
  350. data->temp_grade = "Commercial";
  351. data->temp_max = 95000;
  352. break;
  353. case 1: /* Extended Commercial (-20 to 105C) */
  354. data->temp_grade = "Extended Commercial";
  355. data->temp_max = 105000;
  356. break;
  357. case 2: /* Industrial (-40 to 105C) */
  358. data->temp_grade = "Industrial";
  359. data->temp_max = 105000;
  360. break;
  361. case 3: /* Automotive (-40 to 125C) */
  362. data->temp_grade = "Automotive";
  363. data->temp_max = 125000;
  364. break;
  365. }
  366. /*
  367. * Set the critical trip point at 5C under max
  368. * Set the passive trip point at 10C under max (can change via sysfs)
  369. */
  370. data->temp_critical = data->temp_max - (1000 * 5);
  371. data->temp_passive = data->temp_max - (1000 * 10);
  372. return 0;
  373. }
  374. static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
  375. {
  376. struct imx_thermal_data *data = dev;
  377. disable_irq_nosync(irq);
  378. data->irq_enabled = false;
  379. return IRQ_WAKE_THREAD;
  380. }
  381. static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
  382. {
  383. struct imx_thermal_data *data = dev;
  384. dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
  385. data->alarm_temp / 1000);
  386. thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
  387. return IRQ_HANDLED;
  388. }
  389. static const struct of_device_id of_imx_thermal_match[] = {
  390. { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
  391. { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
  392. { /* end */ }
  393. };
  394. MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
  395. static int imx_thermal_probe(struct platform_device *pdev)
  396. {
  397. struct imx_thermal_data *data;
  398. struct regmap *map;
  399. int measure_freq;
  400. int ret;
  401. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  402. if (!data)
  403. return -ENOMEM;
  404. map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
  405. if (IS_ERR(map)) {
  406. ret = PTR_ERR(map);
  407. dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
  408. return ret;
  409. }
  410. data->tempmon = map;
  411. data->socdata = of_device_get_match_data(&pdev->dev);
  412. if (!data->socdata) {
  413. dev_err(&pdev->dev, "no device match found\n");
  414. return -ENODEV;
  415. }
  416. /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
  417. if (data->socdata->version == TEMPMON_IMX6SX) {
  418. regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH |
  419. MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC);
  420. /*
  421. * reset value of LOW ALARM is incorrect, set it to lowest
  422. * value to avoid false trigger of low alarm.
  423. */
  424. regmap_write(map, TEMPSENSE2 + REG_SET,
  425. TEMPSENSE2_LOW_VALUE_MASK);
  426. }
  427. data->irq = platform_get_irq(pdev, 0);
  428. if (data->irq < 0)
  429. return data->irq;
  430. platform_set_drvdata(pdev, data);
  431. ret = imx_get_sensor_data(pdev);
  432. if (ret) {
  433. dev_err(&pdev->dev, "failed to get sensor data\n");
  434. return ret;
  435. }
  436. /* Make sure sensor is in known good state for measurements */
  437. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  438. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  439. regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
  440. regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
  441. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  442. data->policy = cpufreq_cpu_get(0);
  443. if (!data->policy) {
  444. pr_debug("%s: CPUFreq policy not found\n", __func__);
  445. return -EPROBE_DEFER;
  446. }
  447. data->cdev = cpufreq_cooling_register(data->policy);
  448. if (IS_ERR(data->cdev)) {
  449. ret = PTR_ERR(data->cdev);
  450. dev_err(&pdev->dev,
  451. "failed to register cpufreq cooling device: %d\n", ret);
  452. cpufreq_cpu_put(data->policy);
  453. return ret;
  454. }
  455. data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
  456. if (IS_ERR(data->thermal_clk)) {
  457. ret = PTR_ERR(data->thermal_clk);
  458. if (ret != -EPROBE_DEFER)
  459. dev_err(&pdev->dev,
  460. "failed to get thermal clk: %d\n", ret);
  461. cpufreq_cooling_unregister(data->cdev);
  462. cpufreq_cpu_put(data->policy);
  463. return ret;
  464. }
  465. /*
  466. * Thermal sensor needs clk on to get correct value, normally
  467. * we should enable its clk before taking measurement and disable
  468. * clk after measurement is done, but if alarm function is enabled,
  469. * hardware will auto measure the temperature periodically, so we
  470. * need to keep the clk always on for alarm function.
  471. */
  472. ret = clk_prepare_enable(data->thermal_clk);
  473. if (ret) {
  474. dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
  475. cpufreq_cooling_unregister(data->cdev);
  476. cpufreq_cpu_put(data->policy);
  477. return ret;
  478. }
  479. data->tz = thermal_zone_device_register("imx_thermal_zone",
  480. IMX_TRIP_NUM,
  481. BIT(IMX_TRIP_PASSIVE), data,
  482. &imx_tz_ops, NULL,
  483. IMX_PASSIVE_DELAY,
  484. IMX_POLLING_DELAY);
  485. if (IS_ERR(data->tz)) {
  486. ret = PTR_ERR(data->tz);
  487. dev_err(&pdev->dev,
  488. "failed to register thermal zone device %d\n", ret);
  489. clk_disable_unprepare(data->thermal_clk);
  490. cpufreq_cooling_unregister(data->cdev);
  491. cpufreq_cpu_put(data->policy);
  492. return ret;
  493. }
  494. dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
  495. " critical:%dC passive:%dC\n", data->temp_grade,
  496. data->temp_max / 1000, data->temp_critical / 1000,
  497. data->temp_passive / 1000);
  498. /* Enable measurements at ~ 10 Hz */
  499. regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
  500. measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
  501. regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
  502. imx_set_alarm_temp(data, data->temp_passive);
  503. if (data->socdata->version == TEMPMON_IMX6SX)
  504. imx_set_panic_temp(data, data->temp_critical);
  505. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  506. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  507. data->irq_enabled = true;
  508. data->mode = THERMAL_DEVICE_ENABLED;
  509. ret = devm_request_threaded_irq(&pdev->dev, data->irq,
  510. imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
  511. 0, "imx_thermal", data);
  512. if (ret < 0) {
  513. dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
  514. clk_disable_unprepare(data->thermal_clk);
  515. thermal_zone_device_unregister(data->tz);
  516. cpufreq_cooling_unregister(data->cdev);
  517. cpufreq_cpu_put(data->policy);
  518. return ret;
  519. }
  520. return 0;
  521. }
  522. static int imx_thermal_remove(struct platform_device *pdev)
  523. {
  524. struct imx_thermal_data *data = platform_get_drvdata(pdev);
  525. struct regmap *map = data->tempmon;
  526. /* Disable measurements */
  527. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  528. if (!IS_ERR(data->thermal_clk))
  529. clk_disable_unprepare(data->thermal_clk);
  530. thermal_zone_device_unregister(data->tz);
  531. cpufreq_cooling_unregister(data->cdev);
  532. cpufreq_cpu_put(data->policy);
  533. return 0;
  534. }
  535. #ifdef CONFIG_PM_SLEEP
  536. static int imx_thermal_suspend(struct device *dev)
  537. {
  538. struct imx_thermal_data *data = dev_get_drvdata(dev);
  539. struct regmap *map = data->tempmon;
  540. /*
  541. * Need to disable thermal sensor, otherwise, when thermal core
  542. * try to get temperature before thermal sensor resume, a wrong
  543. * temperature will be read as the thermal sensor is powered
  544. * down.
  545. */
  546. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  547. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  548. data->mode = THERMAL_DEVICE_DISABLED;
  549. clk_disable_unprepare(data->thermal_clk);
  550. return 0;
  551. }
  552. static int imx_thermal_resume(struct device *dev)
  553. {
  554. struct imx_thermal_data *data = dev_get_drvdata(dev);
  555. struct regmap *map = data->tempmon;
  556. int ret;
  557. ret = clk_prepare_enable(data->thermal_clk);
  558. if (ret)
  559. return ret;
  560. /* Enabled thermal sensor after resume */
  561. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  562. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  563. data->mode = THERMAL_DEVICE_ENABLED;
  564. return 0;
  565. }
  566. #endif
  567. static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
  568. imx_thermal_suspend, imx_thermal_resume);
  569. static struct platform_driver imx_thermal = {
  570. .driver = {
  571. .name = "imx_thermal",
  572. .pm = &imx_thermal_pm_ops,
  573. .of_match_table = of_imx_thermal_match,
  574. },
  575. .probe = imx_thermal_probe,
  576. .remove = imx_thermal_remove,
  577. };
  578. module_platform_driver(imx_thermal);
  579. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  580. MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
  581. MODULE_LICENSE("GPL v2");
  582. MODULE_ALIAS("platform:imx-thermal");