imx_thermal.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. #include <linux/nvmem-consumer.h>
  28. #define REG_SET 0x4
  29. #define REG_CLR 0x8
  30. #define REG_TOG 0xc
  31. #define MISC0 0x0150
  32. #define MISC0_REFTOP_SELBIASOFF (1 << 3)
  33. #define MISC1 0x0160
  34. #define MISC1_IRQ_TEMPHIGH (1 << 29)
  35. /* Below LOW and PANIC bits are only for TEMPMON_IMX6SX */
  36. #define MISC1_IRQ_TEMPLOW (1 << 28)
  37. #define MISC1_IRQ_TEMPPANIC (1 << 27)
  38. #define TEMPSENSE0 0x0180
  39. #define TEMPSENSE0_ALARM_VALUE_SHIFT 20
  40. #define TEMPSENSE0_ALARM_VALUE_MASK (0xfff << TEMPSENSE0_ALARM_VALUE_SHIFT)
  41. #define TEMPSENSE0_TEMP_CNT_SHIFT 8
  42. #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
  43. #define TEMPSENSE0_FINISHED (1 << 2)
  44. #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
  45. #define TEMPSENSE0_POWER_DOWN (1 << 0)
  46. #define TEMPSENSE1 0x0190
  47. #define TEMPSENSE1_MEASURE_FREQ 0xffff
  48. /* Below TEMPSENSE2 is only for TEMPMON_IMX6SX */
  49. #define TEMPSENSE2 0x0290
  50. #define TEMPSENSE2_LOW_VALUE_SHIFT 0
  51. #define TEMPSENSE2_LOW_VALUE_MASK 0xfff
  52. #define TEMPSENSE2_PANIC_VALUE_SHIFT 16
  53. #define TEMPSENSE2_PANIC_VALUE_MASK 0xfff0000
  54. #define OCOTP_MEM0 0x0480
  55. #define OCOTP_ANA1 0x04e0
  56. /* The driver supports 1 passive trip point and 1 critical trip point */
  57. enum imx_thermal_trip {
  58. IMX_TRIP_PASSIVE,
  59. IMX_TRIP_CRITICAL,
  60. IMX_TRIP_NUM,
  61. };
  62. #define IMX_POLLING_DELAY 2000 /* millisecond */
  63. #define IMX_PASSIVE_DELAY 1000
  64. #define TEMPMON_IMX6Q 1
  65. #define TEMPMON_IMX6SX 2
  66. struct thermal_soc_data {
  67. u32 version;
  68. };
  69. static struct thermal_soc_data thermal_imx6q_data = {
  70. .version = TEMPMON_IMX6Q,
  71. };
  72. static struct thermal_soc_data thermal_imx6sx_data = {
  73. .version = TEMPMON_IMX6SX,
  74. };
  75. struct imx_thermal_data {
  76. struct cpufreq_policy *policy;
  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_init_calib() */
  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_init_calib() 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_init_calib(struct platform_device *pdev, u32 ocotp_ana1)
  287. {
  288. struct imx_thermal_data *data = platform_get_drvdata(pdev);
  289. int n1;
  290. u64 temp64;
  291. if (ocotp_ana1 == 0 || ocotp_ana1 == ~0) {
  292. dev_err(&pdev->dev, "invalid sensor calibration data\n");
  293. return -EINVAL;
  294. }
  295. /*
  296. * The sensor is calibrated at 25 °C (aka T1) and the value measured
  297. * (aka N1) at this temperature is provided in bits [31:20] in the
  298. * i.MX's OCOTP value ANA1.
  299. * To find the actual temperature T, the following formula has to be used
  300. * when reading value n from the sensor:
  301. *
  302. * T = T1 + (N - N1) / (0.4148468 - 0.0015423 * N1) °C + 3.580661 °C
  303. * = [T1' - N1 / (0.4148468 - 0.0015423 * N1) °C] + N / (0.4148468 - 0.0015423 * N1) °C
  304. * = [T1' + N1 / (0.0015423 * N1 - 0.4148468) °C] - N / (0.0015423 * N1 - 0.4148468) °C
  305. * = c2 - c1 * N
  306. *
  307. * with
  308. *
  309. * T1' = 28.580661 °C
  310. * c1 = 1 / (0.0015423 * N1 - 0.4297157) °C
  311. * c2 = T1' + N1 / (0.0015423 * N1 - 0.4148468) °C
  312. * = T1' + N1 * c1
  313. */
  314. n1 = ocotp_ana1 >> 20;
  315. temp64 = 10000000; /* use 10^7 as fixed point constant for values in formula */
  316. temp64 *= 1000; /* to get result in °mC */
  317. do_div(temp64, 15423 * n1 - 4148468);
  318. data->c1 = temp64;
  319. data->c2 = n1 * data->c1 + 28581;
  320. return 0;
  321. }
  322. static void imx_init_temp_grade(struct platform_device *pdev, u32 ocotp_mem0)
  323. {
  324. struct imx_thermal_data *data = platform_get_drvdata(pdev);
  325. /* The maximum die temp is specified by the Temperature Grade */
  326. switch ((ocotp_mem0 >> 6) & 0x3) {
  327. case 0: /* Commercial (0 to 95 °C) */
  328. data->temp_grade = "Commercial";
  329. data->temp_max = 95000;
  330. break;
  331. case 1: /* Extended Commercial (-20 °C to 105 °C) */
  332. data->temp_grade = "Extended Commercial";
  333. data->temp_max = 105000;
  334. break;
  335. case 2: /* Industrial (-40 °C to 105 °C) */
  336. data->temp_grade = "Industrial";
  337. data->temp_max = 105000;
  338. break;
  339. case 3: /* Automotive (-40 °C to 125 °C) */
  340. data->temp_grade = "Automotive";
  341. data->temp_max = 125000;
  342. break;
  343. }
  344. /*
  345. * Set the critical trip point at 5 °C under max
  346. * Set the passive trip point at 10 °C under max (changeable via sysfs)
  347. */
  348. data->temp_critical = data->temp_max - (1000 * 5);
  349. data->temp_passive = data->temp_max - (1000 * 10);
  350. }
  351. static int imx_init_from_tempmon_data(struct platform_device *pdev)
  352. {
  353. struct regmap *map;
  354. int ret;
  355. u32 val;
  356. map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  357. "fsl,tempmon-data");
  358. if (IS_ERR(map)) {
  359. ret = PTR_ERR(map);
  360. dev_err(&pdev->dev, "failed to get sensor regmap: %d\n", ret);
  361. return ret;
  362. }
  363. ret = regmap_read(map, OCOTP_ANA1, &val);
  364. if (ret) {
  365. dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
  366. return ret;
  367. }
  368. ret = imx_init_calib(pdev, val);
  369. if (ret)
  370. return ret;
  371. ret = regmap_read(map, OCOTP_MEM0, &val);
  372. if (ret) {
  373. dev_err(&pdev->dev, "failed to read sensor data: %d\n", ret);
  374. return ret;
  375. }
  376. imx_init_temp_grade(pdev, val);
  377. return 0;
  378. }
  379. static int imx_init_from_nvmem_cells(struct platform_device *pdev)
  380. {
  381. int ret;
  382. u32 val;
  383. ret = nvmem_cell_read_u32(&pdev->dev, "calib", &val);
  384. if (ret)
  385. return ret;
  386. imx_init_calib(pdev, val);
  387. ret = nvmem_cell_read_u32(&pdev->dev, "temp_grade", &val);
  388. if (ret)
  389. return ret;
  390. imx_init_temp_grade(pdev, val);
  391. return 0;
  392. }
  393. static irqreturn_t imx_thermal_alarm_irq(int irq, void *dev)
  394. {
  395. struct imx_thermal_data *data = dev;
  396. disable_irq_nosync(irq);
  397. data->irq_enabled = false;
  398. return IRQ_WAKE_THREAD;
  399. }
  400. static irqreturn_t imx_thermal_alarm_irq_thread(int irq, void *dev)
  401. {
  402. struct imx_thermal_data *data = dev;
  403. dev_dbg(&data->tz->device, "THERMAL ALARM: T > %d\n",
  404. data->alarm_temp / 1000);
  405. thermal_zone_device_update(data->tz, THERMAL_EVENT_UNSPECIFIED);
  406. return IRQ_HANDLED;
  407. }
  408. static const struct of_device_id of_imx_thermal_match[] = {
  409. { .compatible = "fsl,imx6q-tempmon", .data = &thermal_imx6q_data, },
  410. { .compatible = "fsl,imx6sx-tempmon", .data = &thermal_imx6sx_data, },
  411. { /* end */ }
  412. };
  413. MODULE_DEVICE_TABLE(of, of_imx_thermal_match);
  414. static int imx_thermal_probe(struct platform_device *pdev)
  415. {
  416. struct imx_thermal_data *data;
  417. struct regmap *map;
  418. int measure_freq;
  419. int ret;
  420. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  421. if (!data)
  422. return -ENOMEM;
  423. map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "fsl,tempmon");
  424. if (IS_ERR(map)) {
  425. ret = PTR_ERR(map);
  426. dev_err(&pdev->dev, "failed to get tempmon regmap: %d\n", ret);
  427. return ret;
  428. }
  429. data->tempmon = map;
  430. data->socdata = of_device_get_match_data(&pdev->dev);
  431. if (!data->socdata) {
  432. dev_err(&pdev->dev, "no device match found\n");
  433. return -ENODEV;
  434. }
  435. /* make sure the IRQ flag is clear before enabling irq on i.MX6SX */
  436. if (data->socdata->version == TEMPMON_IMX6SX) {
  437. regmap_write(map, MISC1 + REG_CLR, MISC1_IRQ_TEMPHIGH |
  438. MISC1_IRQ_TEMPLOW | MISC1_IRQ_TEMPPANIC);
  439. /*
  440. * reset value of LOW ALARM is incorrect, set it to lowest
  441. * value to avoid false trigger of low alarm.
  442. */
  443. regmap_write(map, TEMPSENSE2 + REG_SET,
  444. TEMPSENSE2_LOW_VALUE_MASK);
  445. }
  446. data->irq = platform_get_irq(pdev, 0);
  447. if (data->irq < 0)
  448. return data->irq;
  449. platform_set_drvdata(pdev, data);
  450. if (of_find_property(pdev->dev.of_node, "nvmem-cells", NULL)) {
  451. ret = imx_init_from_nvmem_cells(pdev);
  452. if (ret == -EPROBE_DEFER)
  453. return ret;
  454. if (ret) {
  455. dev_err(&pdev->dev, "failed to init from nvmem: %d\n",
  456. ret);
  457. return ret;
  458. }
  459. } else {
  460. ret = imx_init_from_tempmon_data(pdev);
  461. if (ret) {
  462. dev_err(&pdev->dev, "failed to init from from fsl,tempmon-data\n");
  463. return ret;
  464. }
  465. }
  466. /* Make sure sensor is in known good state for measurements */
  467. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  468. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  469. regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
  470. regmap_write(map, MISC0 + REG_SET, MISC0_REFTOP_SELBIASOFF);
  471. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  472. data->policy = cpufreq_cpu_get(0);
  473. if (!data->policy) {
  474. pr_debug("%s: CPUFreq policy not found\n", __func__);
  475. return -EPROBE_DEFER;
  476. }
  477. data->cdev = cpufreq_cooling_register(data->policy);
  478. if (IS_ERR(data->cdev)) {
  479. ret = PTR_ERR(data->cdev);
  480. dev_err(&pdev->dev,
  481. "failed to register cpufreq cooling device: %d\n", ret);
  482. cpufreq_cpu_put(data->policy);
  483. return ret;
  484. }
  485. data->thermal_clk = devm_clk_get(&pdev->dev, NULL);
  486. if (IS_ERR(data->thermal_clk)) {
  487. ret = PTR_ERR(data->thermal_clk);
  488. if (ret != -EPROBE_DEFER)
  489. dev_err(&pdev->dev,
  490. "failed to get thermal clk: %d\n", ret);
  491. cpufreq_cooling_unregister(data->cdev);
  492. cpufreq_cpu_put(data->policy);
  493. return ret;
  494. }
  495. /*
  496. * Thermal sensor needs clk on to get correct value, normally
  497. * we should enable its clk before taking measurement and disable
  498. * clk after measurement is done, but if alarm function is enabled,
  499. * hardware will auto measure the temperature periodically, so we
  500. * need to keep the clk always on for alarm function.
  501. */
  502. ret = clk_prepare_enable(data->thermal_clk);
  503. if (ret) {
  504. dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
  505. cpufreq_cooling_unregister(data->cdev);
  506. cpufreq_cpu_put(data->policy);
  507. return ret;
  508. }
  509. data->tz = thermal_zone_device_register("imx_thermal_zone",
  510. IMX_TRIP_NUM,
  511. BIT(IMX_TRIP_PASSIVE), data,
  512. &imx_tz_ops, NULL,
  513. IMX_PASSIVE_DELAY,
  514. IMX_POLLING_DELAY);
  515. if (IS_ERR(data->tz)) {
  516. ret = PTR_ERR(data->tz);
  517. dev_err(&pdev->dev,
  518. "failed to register thermal zone device %d\n", ret);
  519. clk_disable_unprepare(data->thermal_clk);
  520. cpufreq_cooling_unregister(data->cdev);
  521. cpufreq_cpu_put(data->policy);
  522. return ret;
  523. }
  524. dev_info(&pdev->dev, "%s CPU temperature grade - max:%dC"
  525. " critical:%dC passive:%dC\n", data->temp_grade,
  526. data->temp_max / 1000, data->temp_critical / 1000,
  527. data->temp_passive / 1000);
  528. /* Enable measurements at ~ 10 Hz */
  529. regmap_write(map, TEMPSENSE1 + REG_CLR, TEMPSENSE1_MEASURE_FREQ);
  530. measure_freq = DIV_ROUND_UP(32768, 10); /* 10 Hz */
  531. regmap_write(map, TEMPSENSE1 + REG_SET, measure_freq);
  532. imx_set_alarm_temp(data, data->temp_passive);
  533. if (data->socdata->version == TEMPMON_IMX6SX)
  534. imx_set_panic_temp(data, data->temp_critical);
  535. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  536. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  537. data->irq_enabled = true;
  538. data->mode = THERMAL_DEVICE_ENABLED;
  539. ret = devm_request_threaded_irq(&pdev->dev, data->irq,
  540. imx_thermal_alarm_irq, imx_thermal_alarm_irq_thread,
  541. 0, "imx_thermal", data);
  542. if (ret < 0) {
  543. dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
  544. clk_disable_unprepare(data->thermal_clk);
  545. thermal_zone_device_unregister(data->tz);
  546. cpufreq_cooling_unregister(data->cdev);
  547. cpufreq_cpu_put(data->policy);
  548. return ret;
  549. }
  550. return 0;
  551. }
  552. static int imx_thermal_remove(struct platform_device *pdev)
  553. {
  554. struct imx_thermal_data *data = platform_get_drvdata(pdev);
  555. struct regmap *map = data->tempmon;
  556. /* Disable measurements */
  557. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  558. if (!IS_ERR(data->thermal_clk))
  559. clk_disable_unprepare(data->thermal_clk);
  560. thermal_zone_device_unregister(data->tz);
  561. cpufreq_cooling_unregister(data->cdev);
  562. cpufreq_cpu_put(data->policy);
  563. return 0;
  564. }
  565. #ifdef CONFIG_PM_SLEEP
  566. static int imx_thermal_suspend(struct device *dev)
  567. {
  568. struct imx_thermal_data *data = dev_get_drvdata(dev);
  569. struct regmap *map = data->tempmon;
  570. /*
  571. * Need to disable thermal sensor, otherwise, when thermal core
  572. * try to get temperature before thermal sensor resume, a wrong
  573. * temperature will be read as the thermal sensor is powered
  574. * down.
  575. */
  576. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_MEASURE_TEMP);
  577. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_POWER_DOWN);
  578. data->mode = THERMAL_DEVICE_DISABLED;
  579. clk_disable_unprepare(data->thermal_clk);
  580. return 0;
  581. }
  582. static int imx_thermal_resume(struct device *dev)
  583. {
  584. struct imx_thermal_data *data = dev_get_drvdata(dev);
  585. struct regmap *map = data->tempmon;
  586. int ret;
  587. ret = clk_prepare_enable(data->thermal_clk);
  588. if (ret)
  589. return ret;
  590. /* Enabled thermal sensor after resume */
  591. regmap_write(map, TEMPSENSE0 + REG_CLR, TEMPSENSE0_POWER_DOWN);
  592. regmap_write(map, TEMPSENSE0 + REG_SET, TEMPSENSE0_MEASURE_TEMP);
  593. data->mode = THERMAL_DEVICE_ENABLED;
  594. return 0;
  595. }
  596. #endif
  597. static SIMPLE_DEV_PM_OPS(imx_thermal_pm_ops,
  598. imx_thermal_suspend, imx_thermal_resume);
  599. static struct platform_driver imx_thermal = {
  600. .driver = {
  601. .name = "imx_thermal",
  602. .pm = &imx_thermal_pm_ops,
  603. .of_match_table = of_imx_thermal_match,
  604. },
  605. .probe = imx_thermal_probe,
  606. .remove = imx_thermal_remove,
  607. };
  608. module_platform_driver(imx_thermal);
  609. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  610. MODULE_DESCRIPTION("Thermal driver for Freescale i.MX SoCs");
  611. MODULE_LICENSE("GPL v2");
  612. MODULE_ALIAS("platform:imx-thermal");