exynos_tmu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. * exynos_tmu.c - Samsung EXYNOS TMU (Thermal Management Unit)
  3. *
  4. * Copyright (C) 2011 Samsung Electronics
  5. * Donggeun Kim <dg77.kim@samsung.com>
  6. * Amit Daniel Kachhap <amit.kachhap@linaro.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/clk.h>
  24. #include <linux/io.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/platform_device.h>
  31. #include "exynos_thermal_common.h"
  32. #include "exynos_tmu.h"
  33. #include "exynos_tmu_data.h"
  34. /**
  35. * struct exynos_tmu_data : A structure to hold the private data of the TMU
  36. driver
  37. * @id: identifier of the one instance of the TMU controller.
  38. * @pdata: pointer to the tmu platform/configuration data
  39. * @base: base address of the single instance of the TMU controller.
  40. * @base_common: base address of the common registers of the TMU controller.
  41. * @irq: irq number of the TMU controller.
  42. * @soc: id of the SOC type.
  43. * @irq_work: pointer to the irq work structure.
  44. * @lock: lock to implement synchronization.
  45. * @clk: pointer to the clock structure.
  46. * @temp_error1: fused value of the first point trim.
  47. * @temp_error2: fused value of the second point trim.
  48. * @reg_conf: pointer to structure to register with core thermal.
  49. */
  50. struct exynos_tmu_data {
  51. int id;
  52. struct exynos_tmu_platform_data *pdata;
  53. void __iomem *base;
  54. void __iomem *base_common;
  55. int irq;
  56. enum soc_type soc;
  57. struct work_struct irq_work;
  58. struct mutex lock;
  59. struct clk *clk;
  60. u8 temp_error1, temp_error2;
  61. struct thermal_sensor_conf *reg_conf;
  62. };
  63. /*
  64. * TMU treats temperature as a mapped temperature code.
  65. * The temperature is converted differently depending on the calibration type.
  66. */
  67. static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
  68. {
  69. struct exynos_tmu_platform_data *pdata = data->pdata;
  70. int temp_code;
  71. if (data->soc == SOC_ARCH_EXYNOS4210)
  72. /* temp should range between 25 and 125 */
  73. if (temp < 25 || temp > 125) {
  74. temp_code = -EINVAL;
  75. goto out;
  76. }
  77. switch (pdata->cal_type) {
  78. case TYPE_TWO_POINT_TRIMMING:
  79. temp_code = (temp - pdata->first_point_trim) *
  80. (data->temp_error2 - data->temp_error1) /
  81. (pdata->second_point_trim - pdata->first_point_trim) +
  82. data->temp_error1;
  83. break;
  84. case TYPE_ONE_POINT_TRIMMING:
  85. temp_code = temp + data->temp_error1 - pdata->first_point_trim;
  86. break;
  87. default:
  88. temp_code = temp + pdata->default_temp_offset;
  89. break;
  90. }
  91. out:
  92. return temp_code;
  93. }
  94. /*
  95. * Calculate a temperature value from a temperature code.
  96. * The unit of the temperature is degree Celsius.
  97. */
  98. static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
  99. {
  100. struct exynos_tmu_platform_data *pdata = data->pdata;
  101. int temp;
  102. if (data->soc == SOC_ARCH_EXYNOS4210)
  103. /* temp_code should range between 75 and 175 */
  104. if (temp_code < 75 || temp_code > 175) {
  105. temp = -ENODATA;
  106. goto out;
  107. }
  108. switch (pdata->cal_type) {
  109. case TYPE_TWO_POINT_TRIMMING:
  110. temp = (temp_code - data->temp_error1) *
  111. (pdata->second_point_trim - pdata->first_point_trim) /
  112. (data->temp_error2 - data->temp_error1) +
  113. pdata->first_point_trim;
  114. break;
  115. case TYPE_ONE_POINT_TRIMMING:
  116. temp = temp_code - data->temp_error1 + pdata->first_point_trim;
  117. break;
  118. default:
  119. temp = temp_code - pdata->default_temp_offset;
  120. break;
  121. }
  122. out:
  123. return temp;
  124. }
  125. static int exynos_tmu_initialize(struct platform_device *pdev)
  126. {
  127. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  128. struct exynos_tmu_platform_data *pdata = data->pdata;
  129. const struct exynos_tmu_registers *reg = pdata->registers;
  130. unsigned int status, trim_info = 0, con;
  131. unsigned int rising_threshold = 0, falling_threshold = 0;
  132. int ret = 0, threshold_code, i, trigger_levs = 0;
  133. mutex_lock(&data->lock);
  134. clk_enable(data->clk);
  135. if (TMU_SUPPORTS(pdata, READY_STATUS)) {
  136. status = readb(data->base + reg->tmu_status);
  137. if (!status) {
  138. ret = -EBUSY;
  139. goto out;
  140. }
  141. }
  142. if (TMU_SUPPORTS(pdata, TRIM_RELOAD))
  143. __raw_writel(1, data->base + reg->triminfo_ctrl);
  144. /* Save trimming info in order to perform calibration */
  145. if (data->soc == SOC_ARCH_EXYNOS5440) {
  146. /*
  147. * For exynos5440 soc triminfo value is swapped between TMU0 and
  148. * TMU2, so the below logic is needed.
  149. */
  150. switch (data->id) {
  151. case 0:
  152. trim_info = readl(data->base +
  153. EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data);
  154. break;
  155. case 1:
  156. trim_info = readl(data->base + reg->triminfo_data);
  157. break;
  158. case 2:
  159. trim_info = readl(data->base -
  160. EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data);
  161. }
  162. } else {
  163. trim_info = readl(data->base + reg->triminfo_data);
  164. }
  165. data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
  166. data->temp_error2 = ((trim_info >> reg->triminfo_85_shift) &
  167. EXYNOS_TMU_TEMP_MASK);
  168. if ((pdata->min_efuse_value > data->temp_error1) ||
  169. (data->temp_error1 > pdata->max_efuse_value) ||
  170. (data->temp_error2 != 0))
  171. data->temp_error1 = pdata->efuse_value;
  172. if (pdata->max_trigger_level > MAX_THRESHOLD_LEVS) {
  173. dev_err(&pdev->dev, "Invalid max trigger level\n");
  174. goto out;
  175. }
  176. for (i = 0; i < pdata->max_trigger_level; i++) {
  177. if (!pdata->trigger_levels[i])
  178. continue;
  179. if ((pdata->trigger_type[i] == HW_TRIP) &&
  180. (!pdata->trigger_levels[pdata->max_trigger_level - 1])) {
  181. dev_err(&pdev->dev, "Invalid hw trigger level\n");
  182. ret = -EINVAL;
  183. goto out;
  184. }
  185. /* Count trigger levels except the HW trip*/
  186. if (!(pdata->trigger_type[i] == HW_TRIP))
  187. trigger_levs++;
  188. }
  189. if (data->soc == SOC_ARCH_EXYNOS4210) {
  190. /* Write temperature code for threshold */
  191. threshold_code = temp_to_code(data, pdata->threshold);
  192. if (threshold_code < 0) {
  193. ret = threshold_code;
  194. goto out;
  195. }
  196. writeb(threshold_code,
  197. data->base + reg->threshold_temp);
  198. for (i = 0; i < trigger_levs; i++)
  199. writeb(pdata->trigger_levels[i], data->base +
  200. reg->threshold_th0 + i * sizeof(reg->threshold_th0));
  201. writel(reg->inten_rise_mask, data->base + reg->tmu_intclear);
  202. } else {
  203. /* Write temperature code for rising and falling threshold */
  204. for (i = 0;
  205. i < trigger_levs && i < EXYNOS_MAX_TRIGGER_PER_REG; i++) {
  206. threshold_code = temp_to_code(data,
  207. pdata->trigger_levels[i]);
  208. if (threshold_code < 0) {
  209. ret = threshold_code;
  210. goto out;
  211. }
  212. rising_threshold |= threshold_code << 8 * i;
  213. if (pdata->threshold_falling) {
  214. threshold_code = temp_to_code(data,
  215. pdata->trigger_levels[i] -
  216. pdata->threshold_falling);
  217. if (threshold_code > 0)
  218. falling_threshold |=
  219. threshold_code << 8 * i;
  220. }
  221. }
  222. writel(rising_threshold,
  223. data->base + reg->threshold_th0);
  224. writel(falling_threshold,
  225. data->base + reg->threshold_th1);
  226. writel((reg->inten_rise_mask << reg->inten_rise_shift) |
  227. (reg->inten_fall_mask << reg->inten_fall_shift),
  228. data->base + reg->tmu_intclear);
  229. /* if last threshold limit is also present */
  230. i = pdata->max_trigger_level - 1;
  231. if (pdata->trigger_levels[i] &&
  232. (pdata->trigger_type[i] == HW_TRIP)) {
  233. threshold_code = temp_to_code(data,
  234. pdata->trigger_levels[i]);
  235. if (threshold_code < 0) {
  236. ret = threshold_code;
  237. goto out;
  238. }
  239. if (i == EXYNOS_MAX_TRIGGER_PER_REG - 1) {
  240. /* 1-4 level to be assigned in th0 reg */
  241. rising_threshold |= threshold_code << 8 * i;
  242. writel(rising_threshold,
  243. data->base + reg->threshold_th0);
  244. } else if (i == EXYNOS_MAX_TRIGGER_PER_REG) {
  245. /* 5th level to be assigned in th2 reg */
  246. rising_threshold =
  247. threshold_code << reg->threshold_th3_l0_shift;
  248. writel(rising_threshold,
  249. data->base + reg->threshold_th2);
  250. }
  251. con = readl(data->base + reg->tmu_ctrl);
  252. con |= (1 << reg->therm_trip_en_shift);
  253. writel(con, data->base + reg->tmu_ctrl);
  254. }
  255. }
  256. /*Clear the PMIN in the common TMU register*/
  257. if (reg->tmu_pmin && !data->id)
  258. writel(0, data->base_common + reg->tmu_pmin);
  259. out:
  260. clk_disable(data->clk);
  261. mutex_unlock(&data->lock);
  262. return ret;
  263. }
  264. static void exynos_tmu_control(struct platform_device *pdev, bool on)
  265. {
  266. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  267. struct exynos_tmu_platform_data *pdata = data->pdata;
  268. const struct exynos_tmu_registers *reg = pdata->registers;
  269. unsigned int con, interrupt_en;
  270. mutex_lock(&data->lock);
  271. clk_enable(data->clk);
  272. con = readl(data->base + reg->tmu_ctrl);
  273. if (pdata->reference_voltage) {
  274. con &= ~(reg->buf_vref_sel_mask << reg->buf_vref_sel_shift);
  275. con |= pdata->reference_voltage << reg->buf_vref_sel_shift;
  276. }
  277. if (pdata->gain) {
  278. con &= ~(reg->buf_slope_sel_mask << reg->buf_slope_sel_shift);
  279. con |= (pdata->gain << reg->buf_slope_sel_shift);
  280. }
  281. if (pdata->noise_cancel_mode) {
  282. con &= ~(reg->therm_trip_mode_mask <<
  283. reg->therm_trip_mode_shift);
  284. con |= (pdata->noise_cancel_mode << reg->therm_trip_mode_shift);
  285. }
  286. if (on) {
  287. con |= (1 << reg->core_en_shift);
  288. interrupt_en =
  289. pdata->trigger_enable[3] << reg->inten_rise3_shift |
  290. pdata->trigger_enable[2] << reg->inten_rise2_shift |
  291. pdata->trigger_enable[1] << reg->inten_rise1_shift |
  292. pdata->trigger_enable[0] << reg->inten_rise0_shift;
  293. if (TMU_SUPPORTS(pdata, FALLING_TRIP))
  294. interrupt_en |=
  295. interrupt_en << reg->inten_fall0_shift;
  296. } else {
  297. con &= ~(1 << reg->core_en_shift);
  298. interrupt_en = 0; /* Disable all interrupts */
  299. }
  300. writel(interrupt_en, data->base + reg->tmu_inten);
  301. writel(con, data->base + reg->tmu_ctrl);
  302. clk_disable(data->clk);
  303. mutex_unlock(&data->lock);
  304. }
  305. static int exynos_tmu_read(struct exynos_tmu_data *data)
  306. {
  307. struct exynos_tmu_platform_data *pdata = data->pdata;
  308. const struct exynos_tmu_registers *reg = pdata->registers;
  309. u8 temp_code;
  310. int temp;
  311. mutex_lock(&data->lock);
  312. clk_enable(data->clk);
  313. temp_code = readb(data->base + reg->tmu_cur_temp);
  314. temp = code_to_temp(data, temp_code);
  315. clk_disable(data->clk);
  316. mutex_unlock(&data->lock);
  317. return temp;
  318. }
  319. #ifdef CONFIG_THERMAL_EMULATION
  320. static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
  321. {
  322. struct exynos_tmu_data *data = drv_data;
  323. struct exynos_tmu_platform_data *pdata = data->pdata;
  324. const struct exynos_tmu_registers *reg = pdata->registers;
  325. unsigned int val;
  326. int ret = -EINVAL;
  327. if (!TMU_SUPPORTS(pdata, EMULATION))
  328. goto out;
  329. if (temp && temp < MCELSIUS)
  330. goto out;
  331. mutex_lock(&data->lock);
  332. clk_enable(data->clk);
  333. val = readl(data->base + reg->emul_con);
  334. if (temp) {
  335. temp /= MCELSIUS;
  336. if (TMU_SUPPORTS(pdata, EMUL_TIME)) {
  337. val &= ~(EXYNOS_EMUL_TIME_MASK << reg->emul_time_shift);
  338. val |= (EXYNOS_EMUL_TIME << reg->emul_time_shift);
  339. }
  340. val &= ~(EXYNOS_EMUL_DATA_MASK << reg->emul_temp_shift);
  341. val |= (temp_to_code(data, temp) << reg->emul_temp_shift) |
  342. EXYNOS_EMUL_ENABLE;
  343. } else {
  344. val &= ~EXYNOS_EMUL_ENABLE;
  345. }
  346. writel(val, data->base + reg->emul_con);
  347. clk_disable(data->clk);
  348. mutex_unlock(&data->lock);
  349. return 0;
  350. out:
  351. return ret;
  352. }
  353. #else
  354. static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
  355. { return -EINVAL; }
  356. #endif/*CONFIG_THERMAL_EMULATION*/
  357. static void exynos_tmu_work(struct work_struct *work)
  358. {
  359. struct exynos_tmu_data *data = container_of(work,
  360. struct exynos_tmu_data, irq_work);
  361. struct exynos_tmu_platform_data *pdata = data->pdata;
  362. const struct exynos_tmu_registers *reg = pdata->registers;
  363. unsigned int val_irq, val_type;
  364. /* Find which sensor generated this interrupt */
  365. if (reg->tmu_irqstatus) {
  366. val_type = readl(data->base_common + reg->tmu_irqstatus);
  367. if (!((val_type >> data->id) & 0x1))
  368. goto out;
  369. }
  370. exynos_report_trigger(data->reg_conf);
  371. mutex_lock(&data->lock);
  372. clk_enable(data->clk);
  373. /* TODO: take action based on particular interrupt */
  374. val_irq = readl(data->base + reg->tmu_intstat);
  375. /* clear the interrupts */
  376. writel(val_irq, data->base + reg->tmu_intclear);
  377. clk_disable(data->clk);
  378. mutex_unlock(&data->lock);
  379. out:
  380. enable_irq(data->irq);
  381. }
  382. static irqreturn_t exynos_tmu_irq(int irq, void *id)
  383. {
  384. struct exynos_tmu_data *data = id;
  385. disable_irq_nosync(irq);
  386. schedule_work(&data->irq_work);
  387. return IRQ_HANDLED;
  388. }
  389. #ifdef CONFIG_OF
  390. static const struct of_device_id exynos_tmu_match[] = {
  391. {
  392. .compatible = "samsung,exynos4210-tmu",
  393. .data = (void *)EXYNOS4210_TMU_DRV_DATA,
  394. },
  395. {
  396. .compatible = "samsung,exynos4412-tmu",
  397. .data = (void *)EXYNOS5250_TMU_DRV_DATA,
  398. },
  399. {
  400. .compatible = "samsung,exynos5250-tmu",
  401. .data = (void *)EXYNOS5250_TMU_DRV_DATA,
  402. },
  403. {},
  404. };
  405. MODULE_DEVICE_TABLE(of, exynos_tmu_match);
  406. #endif
  407. static inline struct exynos_tmu_platform_data *exynos_get_driver_data(
  408. struct platform_device *pdev, int id)
  409. {
  410. #ifdef CONFIG_OF
  411. struct exynos_tmu_init_data *data_table;
  412. struct exynos_tmu_platform_data *tmu_data;
  413. if (pdev->dev.of_node) {
  414. const struct of_device_id *match;
  415. match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
  416. if (!match)
  417. return NULL;
  418. data_table = (struct exynos_tmu_init_data *) match->data;
  419. if (!data_table || id >= data_table->tmu_count)
  420. return NULL;
  421. tmu_data = data_table->tmu_data;
  422. return (struct exynos_tmu_platform_data *) (tmu_data + id);
  423. }
  424. #endif
  425. return NULL;
  426. }
  427. static int exynos_map_dt_data(struct platform_device *pdev)
  428. {
  429. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  430. struct exynos_tmu_platform_data *pdata;
  431. struct resource res;
  432. if (!data)
  433. return -ENODEV;
  434. data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
  435. if (data->id < 0)
  436. data->id = 0;
  437. data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  438. if (data->irq <= 0) {
  439. dev_err(&pdev->dev, "failed to get IRQ\n");
  440. return -ENODEV;
  441. }
  442. if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
  443. dev_err(&pdev->dev, "failed to get Resource 0\n");
  444. return -ENODEV;
  445. }
  446. data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
  447. if (!data->base) {
  448. dev_err(&pdev->dev, "Failed to ioremap memory\n");
  449. return -EADDRNOTAVAIL;
  450. }
  451. pdata = exynos_get_driver_data(pdev, data->id);
  452. if (!pdata) {
  453. dev_err(&pdev->dev, "No platform init data supplied.\n");
  454. return -ENODEV;
  455. }
  456. data->pdata = pdata;
  457. /*
  458. * Check if the TMU shares some registers and then try to map the
  459. * memory of common registers.
  460. */
  461. if (!TMU_SUPPORTS(pdata, SHARED_MEMORY))
  462. return 0;
  463. if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
  464. dev_err(&pdev->dev, "failed to get Resource 1\n");
  465. return -ENODEV;
  466. }
  467. data->base_common = devm_ioremap(&pdev->dev, res.start,
  468. resource_size(&res));
  469. if (!data->base) {
  470. dev_err(&pdev->dev, "Failed to ioremap memory\n");
  471. return -ENOMEM;
  472. }
  473. return 0;
  474. }
  475. static int exynos_tmu_probe(struct platform_device *pdev)
  476. {
  477. struct exynos_tmu_data *data;
  478. struct exynos_tmu_platform_data *pdata;
  479. struct thermal_sensor_conf *sensor_conf;
  480. int ret, i;
  481. data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
  482. GFP_KERNEL);
  483. if (!data) {
  484. dev_err(&pdev->dev, "Failed to allocate driver structure\n");
  485. return -ENOMEM;
  486. }
  487. platform_set_drvdata(pdev, data);
  488. mutex_init(&data->lock);
  489. ret = exynos_map_dt_data(pdev);
  490. if (ret)
  491. return ret;
  492. pdata = data->pdata;
  493. INIT_WORK(&data->irq_work, exynos_tmu_work);
  494. data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
  495. if (IS_ERR(data->clk)) {
  496. dev_err(&pdev->dev, "Failed to get clock\n");
  497. return PTR_ERR(data->clk);
  498. }
  499. ret = clk_prepare(data->clk);
  500. if (ret)
  501. return ret;
  502. if (pdata->type == SOC_ARCH_EXYNOS ||
  503. pdata->type == SOC_ARCH_EXYNOS4210 ||
  504. pdata->type == SOC_ARCH_EXYNOS5440)
  505. data->soc = pdata->type;
  506. else {
  507. ret = -EINVAL;
  508. dev_err(&pdev->dev, "Platform not supported\n");
  509. goto err_clk;
  510. }
  511. ret = exynos_tmu_initialize(pdev);
  512. if (ret) {
  513. dev_err(&pdev->dev, "Failed to initialize TMU\n");
  514. goto err_clk;
  515. }
  516. exynos_tmu_control(pdev, true);
  517. /* Allocate a structure to register with the exynos core thermal */
  518. sensor_conf = devm_kzalloc(&pdev->dev,
  519. sizeof(struct thermal_sensor_conf), GFP_KERNEL);
  520. if (!sensor_conf) {
  521. dev_err(&pdev->dev, "Failed to allocate registration struct\n");
  522. ret = -ENOMEM;
  523. goto err_clk;
  524. }
  525. sprintf(sensor_conf->name, "therm_zone%d", data->id);
  526. sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read;
  527. sensor_conf->write_emul_temp =
  528. (int (*)(void *, unsigned long))exynos_tmu_set_emulation;
  529. sensor_conf->driver_data = data;
  530. sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] +
  531. pdata->trigger_enable[1] + pdata->trigger_enable[2]+
  532. pdata->trigger_enable[3];
  533. for (i = 0; i < sensor_conf->trip_data.trip_count; i++) {
  534. sensor_conf->trip_data.trip_val[i] =
  535. pdata->threshold + pdata->trigger_levels[i];
  536. sensor_conf->trip_data.trip_type[i] =
  537. pdata->trigger_type[i];
  538. }
  539. sensor_conf->trip_data.trigger_falling = pdata->threshold_falling;
  540. sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count;
  541. for (i = 0; i < pdata->freq_tab_count; i++) {
  542. sensor_conf->cooling_data.freq_data[i].freq_clip_max =
  543. pdata->freq_tab[i].freq_clip_max;
  544. sensor_conf->cooling_data.freq_data[i].temp_level =
  545. pdata->freq_tab[i].temp_level;
  546. }
  547. sensor_conf->dev = &pdev->dev;
  548. /* Register the sensor with thermal management interface */
  549. ret = exynos_register_thermal(sensor_conf);
  550. if (ret) {
  551. dev_err(&pdev->dev, "Failed to register thermal interface\n");
  552. goto err_clk;
  553. }
  554. data->reg_conf = sensor_conf;
  555. ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
  556. IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
  557. if (ret) {
  558. dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
  559. goto err_clk;
  560. }
  561. return 0;
  562. err_clk:
  563. clk_unprepare(data->clk);
  564. return ret;
  565. }
  566. static int exynos_tmu_remove(struct platform_device *pdev)
  567. {
  568. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  569. exynos_tmu_control(pdev, false);
  570. exynos_unregister_thermal(data->reg_conf);
  571. clk_unprepare(data->clk);
  572. return 0;
  573. }
  574. #ifdef CONFIG_PM_SLEEP
  575. static int exynos_tmu_suspend(struct device *dev)
  576. {
  577. exynos_tmu_control(to_platform_device(dev), false);
  578. return 0;
  579. }
  580. static int exynos_tmu_resume(struct device *dev)
  581. {
  582. struct platform_device *pdev = to_platform_device(dev);
  583. exynos_tmu_initialize(pdev);
  584. exynos_tmu_control(pdev, true);
  585. return 0;
  586. }
  587. static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
  588. exynos_tmu_suspend, exynos_tmu_resume);
  589. #define EXYNOS_TMU_PM (&exynos_tmu_pm)
  590. #else
  591. #define EXYNOS_TMU_PM NULL
  592. #endif
  593. static struct platform_driver exynos_tmu_driver = {
  594. .driver = {
  595. .name = "exynos-tmu",
  596. .owner = THIS_MODULE,
  597. .pm = EXYNOS_TMU_PM,
  598. .of_match_table = of_match_ptr(exynos_tmu_match),
  599. },
  600. .probe = exynos_tmu_probe,
  601. .remove = exynos_tmu_remove,
  602. };
  603. module_platform_driver(exynos_tmu_driver);
  604. MODULE_DESCRIPTION("EXYNOS TMU Driver");
  605. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  606. MODULE_LICENSE("GPL");
  607. MODULE_ALIAS("platform:exynos-tmu");