exynos_thermal.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /*
  2. * exynos_thermal.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/module.h>
  24. #include <linux/err.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/clk.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/sysfs.h>
  32. #include <linux/kobject.h>
  33. #include <linux/io.h>
  34. #include <linux/mutex.h>
  35. #include <linux/platform_data/exynos_thermal.h>
  36. #include <linux/thermal.h>
  37. #include <linux/cpufreq.h>
  38. #include <linux/cpu_cooling.h>
  39. #include <linux/of.h>
  40. #include <plat/cpu.h>
  41. /* Exynos generic registers */
  42. #define EXYNOS_TMU_REG_TRIMINFO 0x0
  43. #define EXYNOS_TMU_REG_CONTROL 0x20
  44. #define EXYNOS_TMU_REG_STATUS 0x28
  45. #define EXYNOS_TMU_REG_CURRENT_TEMP 0x40
  46. #define EXYNOS_TMU_REG_INTEN 0x70
  47. #define EXYNOS_TMU_REG_INTSTAT 0x74
  48. #define EXYNOS_TMU_REG_INTCLEAR 0x78
  49. #define EXYNOS_TMU_TRIM_TEMP_MASK 0xff
  50. #define EXYNOS_TMU_GAIN_SHIFT 8
  51. #define EXYNOS_TMU_REF_VOLTAGE_SHIFT 24
  52. #define EXYNOS_TMU_CORE_ON 3
  53. #define EXYNOS_TMU_CORE_OFF 2
  54. #define EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET 50
  55. /* Exynos4210 specific registers */
  56. #define EXYNOS4210_TMU_REG_THRESHOLD_TEMP 0x44
  57. #define EXYNOS4210_TMU_REG_TRIG_LEVEL0 0x50
  58. #define EXYNOS4210_TMU_REG_TRIG_LEVEL1 0x54
  59. #define EXYNOS4210_TMU_REG_TRIG_LEVEL2 0x58
  60. #define EXYNOS4210_TMU_REG_TRIG_LEVEL3 0x5C
  61. #define EXYNOS4210_TMU_REG_PAST_TEMP0 0x60
  62. #define EXYNOS4210_TMU_REG_PAST_TEMP1 0x64
  63. #define EXYNOS4210_TMU_REG_PAST_TEMP2 0x68
  64. #define EXYNOS4210_TMU_REG_PAST_TEMP3 0x6C
  65. #define EXYNOS4210_TMU_TRIG_LEVEL0_MASK 0x1
  66. #define EXYNOS4210_TMU_TRIG_LEVEL1_MASK 0x10
  67. #define EXYNOS4210_TMU_TRIG_LEVEL2_MASK 0x100
  68. #define EXYNOS4210_TMU_TRIG_LEVEL3_MASK 0x1000
  69. #define EXYNOS4210_TMU_INTCLEAR_VAL 0x1111
  70. /* Exynos5250 and Exynos4412 specific registers */
  71. #define EXYNOS_TMU_TRIMINFO_CON 0x14
  72. #define EXYNOS_THD_TEMP_RISE 0x50
  73. #define EXYNOS_THD_TEMP_FALL 0x54
  74. #define EXYNOS_EMUL_CON 0x80
  75. #define EXYNOS_TRIMINFO_RELOAD 0x1
  76. #define EXYNOS_TMU_CLEAR_RISE_INT 0x111
  77. #define EXYNOS_TMU_CLEAR_FALL_INT (0x111 << 16)
  78. #define EXYNOS_MUX_ADDR_VALUE 6
  79. #define EXYNOS_MUX_ADDR_SHIFT 20
  80. #define EXYNOS_TMU_TRIP_MODE_SHIFT 13
  81. #define EFUSE_MIN_VALUE 40
  82. #define EFUSE_MAX_VALUE 100
  83. /* In-kernel thermal framework related macros & definations */
  84. #define SENSOR_NAME_LEN 16
  85. #define MAX_TRIP_COUNT 8
  86. #define MAX_COOLING_DEVICE 4
  87. #define ACTIVE_INTERVAL 500
  88. #define IDLE_INTERVAL 10000
  89. #define MCELSIUS 1000
  90. /* CPU Zone information */
  91. #define PANIC_ZONE 4
  92. #define WARN_ZONE 3
  93. #define MONITOR_ZONE 2
  94. #define SAFE_ZONE 1
  95. #define GET_ZONE(trip) (trip + 2)
  96. #define GET_TRIP(zone) (zone - 2)
  97. #define EXYNOS_ZONE_COUNT 3
  98. struct exynos_tmu_data {
  99. struct exynos_tmu_platform_data *pdata;
  100. struct resource *mem;
  101. void __iomem *base;
  102. int irq;
  103. enum soc_type soc;
  104. struct work_struct irq_work;
  105. struct mutex lock;
  106. struct clk *clk;
  107. u8 temp_error1, temp_error2;
  108. };
  109. struct thermal_trip_point_conf {
  110. int trip_val[MAX_TRIP_COUNT];
  111. int trip_count;
  112. };
  113. struct thermal_cooling_conf {
  114. struct freq_clip_table freq_data[MAX_TRIP_COUNT];
  115. int freq_clip_count;
  116. };
  117. struct thermal_sensor_conf {
  118. char name[SENSOR_NAME_LEN];
  119. int (*read_temperature)(void *data);
  120. struct thermal_trip_point_conf trip_data;
  121. struct thermal_cooling_conf cooling_data;
  122. void *private_data;
  123. };
  124. struct exynos_thermal_zone {
  125. enum thermal_device_mode mode;
  126. struct thermal_zone_device *therm_dev;
  127. struct thermal_cooling_device *cool_dev[MAX_COOLING_DEVICE];
  128. unsigned int cool_dev_size;
  129. struct platform_device *exynos4_dev;
  130. struct thermal_sensor_conf *sensor_conf;
  131. bool bind;
  132. };
  133. static struct exynos_thermal_zone *th_zone;
  134. static void exynos_unregister_thermal(void);
  135. static int exynos_register_thermal(struct thermal_sensor_conf *sensor_conf);
  136. /* Get mode callback functions for thermal zone */
  137. static int exynos_get_mode(struct thermal_zone_device *thermal,
  138. enum thermal_device_mode *mode)
  139. {
  140. if (th_zone)
  141. *mode = th_zone->mode;
  142. return 0;
  143. }
  144. /* Set mode callback functions for thermal zone */
  145. static int exynos_set_mode(struct thermal_zone_device *thermal,
  146. enum thermal_device_mode mode)
  147. {
  148. if (!th_zone->therm_dev) {
  149. pr_notice("thermal zone not registered\n");
  150. return 0;
  151. }
  152. mutex_lock(&th_zone->therm_dev->lock);
  153. if (mode == THERMAL_DEVICE_ENABLED)
  154. th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
  155. else
  156. th_zone->therm_dev->polling_delay = 0;
  157. mutex_unlock(&th_zone->therm_dev->lock);
  158. th_zone->mode = mode;
  159. thermal_zone_device_update(th_zone->therm_dev);
  160. pr_info("thermal polling set for duration=%d msec\n",
  161. th_zone->therm_dev->polling_delay);
  162. return 0;
  163. }
  164. /* Get trip type callback functions for thermal zone */
  165. static int exynos_get_trip_type(struct thermal_zone_device *thermal, int trip,
  166. enum thermal_trip_type *type)
  167. {
  168. switch (GET_ZONE(trip)) {
  169. case MONITOR_ZONE:
  170. case WARN_ZONE:
  171. *type = THERMAL_TRIP_ACTIVE;
  172. break;
  173. case PANIC_ZONE:
  174. *type = THERMAL_TRIP_CRITICAL;
  175. break;
  176. default:
  177. return -EINVAL;
  178. }
  179. return 0;
  180. }
  181. /* Get trip temperature callback functions for thermal zone */
  182. static int exynos_get_trip_temp(struct thermal_zone_device *thermal, int trip,
  183. unsigned long *temp)
  184. {
  185. if (trip < GET_TRIP(MONITOR_ZONE) || trip > GET_TRIP(PANIC_ZONE))
  186. return -EINVAL;
  187. *temp = th_zone->sensor_conf->trip_data.trip_val[trip];
  188. /* convert the temperature into millicelsius */
  189. *temp = *temp * MCELSIUS;
  190. return 0;
  191. }
  192. /* Get critical temperature callback functions for thermal zone */
  193. static int exynos_get_crit_temp(struct thermal_zone_device *thermal,
  194. unsigned long *temp)
  195. {
  196. int ret;
  197. /* Panic zone */
  198. ret = exynos_get_trip_temp(thermal, GET_TRIP(PANIC_ZONE), temp);
  199. return ret;
  200. }
  201. static int exynos_get_frequency_level(unsigned int cpu, unsigned int freq)
  202. {
  203. int i = 0, ret = -EINVAL;
  204. struct cpufreq_frequency_table *table = NULL;
  205. #ifdef CONFIG_CPU_FREQ
  206. table = cpufreq_frequency_get_table(cpu);
  207. #endif
  208. if (!table)
  209. return ret;
  210. while (table[i].frequency != CPUFREQ_TABLE_END) {
  211. if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
  212. continue;
  213. if (table[i].frequency == freq)
  214. return i;
  215. i++;
  216. }
  217. return ret;
  218. }
  219. /* Bind callback functions for thermal zone */
  220. static int exynos_bind(struct thermal_zone_device *thermal,
  221. struct thermal_cooling_device *cdev)
  222. {
  223. int ret = 0, i, tab_size, level;
  224. struct freq_clip_table *tab_ptr, *clip_data;
  225. struct thermal_sensor_conf *data = th_zone->sensor_conf;
  226. tab_ptr = (struct freq_clip_table *)data->cooling_data.freq_data;
  227. tab_size = data->cooling_data.freq_clip_count;
  228. if (tab_ptr == NULL || tab_size == 0)
  229. return -EINVAL;
  230. /* find the cooling device registered*/
  231. for (i = 0; i < th_zone->cool_dev_size; i++)
  232. if (cdev == th_zone->cool_dev[i])
  233. break;
  234. /* No matching cooling device */
  235. if (i == th_zone->cool_dev_size)
  236. return 0;
  237. /* Bind the thermal zone to the cpufreq cooling device */
  238. for (i = 0; i < tab_size; i++) {
  239. clip_data = (struct freq_clip_table *)&(tab_ptr[i]);
  240. level = exynos_get_frequency_level(0, clip_data->freq_clip_max);
  241. if (level < 0)
  242. return 0;
  243. switch (GET_ZONE(i)) {
  244. case MONITOR_ZONE:
  245. case WARN_ZONE:
  246. if (thermal_zone_bind_cooling_device(thermal, i, cdev,
  247. level, level)) {
  248. pr_err("error binding cdev inst %d\n", i);
  249. ret = -EINVAL;
  250. }
  251. th_zone->bind = true;
  252. break;
  253. default:
  254. ret = -EINVAL;
  255. }
  256. }
  257. return ret;
  258. }
  259. /* Unbind callback functions for thermal zone */
  260. static int exynos_unbind(struct thermal_zone_device *thermal,
  261. struct thermal_cooling_device *cdev)
  262. {
  263. int ret = 0, i, tab_size;
  264. struct thermal_sensor_conf *data = th_zone->sensor_conf;
  265. if (th_zone->bind == false)
  266. return 0;
  267. tab_size = data->cooling_data.freq_clip_count;
  268. if (tab_size == 0)
  269. return -EINVAL;
  270. /* find the cooling device registered*/
  271. for (i = 0; i < th_zone->cool_dev_size; i++)
  272. if (cdev == th_zone->cool_dev[i])
  273. break;
  274. /* No matching cooling device */
  275. if (i == th_zone->cool_dev_size)
  276. return 0;
  277. /* Bind the thermal zone to the cpufreq cooling device */
  278. for (i = 0; i < tab_size; i++) {
  279. switch (GET_ZONE(i)) {
  280. case MONITOR_ZONE:
  281. case WARN_ZONE:
  282. if (thermal_zone_unbind_cooling_device(thermal, i,
  283. cdev)) {
  284. pr_err("error unbinding cdev inst=%d\n", i);
  285. ret = -EINVAL;
  286. }
  287. th_zone->bind = false;
  288. break;
  289. default:
  290. ret = -EINVAL;
  291. }
  292. }
  293. return ret;
  294. }
  295. /* Get temperature callback functions for thermal zone */
  296. static int exynos_get_temp(struct thermal_zone_device *thermal,
  297. unsigned long *temp)
  298. {
  299. void *data;
  300. if (!th_zone->sensor_conf) {
  301. pr_info("Temperature sensor not initialised\n");
  302. return -EINVAL;
  303. }
  304. data = th_zone->sensor_conf->private_data;
  305. *temp = th_zone->sensor_conf->read_temperature(data);
  306. /* convert the temperature into millicelsius */
  307. *temp = *temp * MCELSIUS;
  308. return 0;
  309. }
  310. /* Get the temperature trend */
  311. static int exynos_get_trend(struct thermal_zone_device *thermal,
  312. int trip, enum thermal_trend *trend)
  313. {
  314. if (thermal->temperature >= trip)
  315. *trend = THERMAL_TREND_RAISING;
  316. else
  317. *trend = THERMAL_TREND_DROPPING;
  318. return 0;
  319. }
  320. /* Operation callback functions for thermal zone */
  321. static struct thermal_zone_device_ops const exynos_dev_ops = {
  322. .bind = exynos_bind,
  323. .unbind = exynos_unbind,
  324. .get_temp = exynos_get_temp,
  325. .get_trend = exynos_get_trend,
  326. .get_mode = exynos_get_mode,
  327. .set_mode = exynos_set_mode,
  328. .get_trip_type = exynos_get_trip_type,
  329. .get_trip_temp = exynos_get_trip_temp,
  330. .get_crit_temp = exynos_get_crit_temp,
  331. };
  332. /*
  333. * This function may be called from interrupt based temperature sensor
  334. * when threshold is changed.
  335. */
  336. static void exynos_report_trigger(void)
  337. {
  338. unsigned int i;
  339. char data[10];
  340. char *envp[] = { data, NULL };
  341. if (!th_zone || !th_zone->therm_dev)
  342. return;
  343. if (th_zone->bind == false) {
  344. for (i = 0; i < th_zone->cool_dev_size; i++) {
  345. if (!th_zone->cool_dev[i])
  346. continue;
  347. exynos_bind(th_zone->therm_dev,
  348. th_zone->cool_dev[i]);
  349. }
  350. }
  351. thermal_zone_device_update(th_zone->therm_dev);
  352. mutex_lock(&th_zone->therm_dev->lock);
  353. /* Find the level for which trip happened */
  354. for (i = 0; i < th_zone->sensor_conf->trip_data.trip_count; i++) {
  355. if (th_zone->therm_dev->last_temperature <
  356. th_zone->sensor_conf->trip_data.trip_val[i] * MCELSIUS)
  357. break;
  358. }
  359. if (th_zone->mode == THERMAL_DEVICE_ENABLED) {
  360. if (i > 0)
  361. th_zone->therm_dev->polling_delay = ACTIVE_INTERVAL;
  362. else
  363. th_zone->therm_dev->polling_delay = IDLE_INTERVAL;
  364. }
  365. snprintf(data, sizeof(data), "%u", i);
  366. kobject_uevent_env(&th_zone->therm_dev->device.kobj, KOBJ_CHANGE, envp);
  367. mutex_unlock(&th_zone->therm_dev->lock);
  368. }
  369. /* Register with the in-kernel thermal management */
  370. static int exynos_register_thermal(struct thermal_sensor_conf *sensor_conf)
  371. {
  372. int ret;
  373. struct cpumask mask_val;
  374. if (!sensor_conf || !sensor_conf->read_temperature) {
  375. pr_err("Temperature sensor not initialised\n");
  376. return -EINVAL;
  377. }
  378. th_zone = kzalloc(sizeof(struct exynos_thermal_zone), GFP_KERNEL);
  379. if (!th_zone)
  380. return -ENOMEM;
  381. th_zone->sensor_conf = sensor_conf;
  382. cpumask_set_cpu(0, &mask_val);
  383. th_zone->cool_dev[0] = cpufreq_cooling_register(&mask_val);
  384. if (IS_ERR(th_zone->cool_dev[0])) {
  385. pr_err("Failed to register cpufreq cooling device\n");
  386. ret = -EINVAL;
  387. goto err_unregister;
  388. }
  389. th_zone->cool_dev_size++;
  390. th_zone->therm_dev = thermal_zone_device_register(sensor_conf->name,
  391. EXYNOS_ZONE_COUNT, 0, NULL, &exynos_dev_ops, NULL, 0,
  392. IDLE_INTERVAL);
  393. if (IS_ERR(th_zone->therm_dev)) {
  394. pr_err("Failed to register thermal zone device\n");
  395. ret = -EINVAL;
  396. goto err_unregister;
  397. }
  398. th_zone->mode = THERMAL_DEVICE_ENABLED;
  399. pr_info("Exynos: Kernel Thermal management registered\n");
  400. return 0;
  401. err_unregister:
  402. exynos_unregister_thermal();
  403. return ret;
  404. }
  405. /* Un-Register with the in-kernel thermal management */
  406. static void exynos_unregister_thermal(void)
  407. {
  408. int i;
  409. if (!th_zone)
  410. return;
  411. if (th_zone->therm_dev)
  412. thermal_zone_device_unregister(th_zone->therm_dev);
  413. for (i = 0; i < th_zone->cool_dev_size; i++) {
  414. if (th_zone->cool_dev[i])
  415. cpufreq_cooling_unregister(th_zone->cool_dev[i]);
  416. }
  417. kfree(th_zone);
  418. pr_info("Exynos: Kernel Thermal management unregistered\n");
  419. }
  420. /*
  421. * TMU treats temperature as a mapped temperature code.
  422. * The temperature is converted differently depending on the calibration type.
  423. */
  424. static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
  425. {
  426. struct exynos_tmu_platform_data *pdata = data->pdata;
  427. int temp_code;
  428. if (data->soc == SOC_ARCH_EXYNOS4210)
  429. /* temp should range between 25 and 125 */
  430. if (temp < 25 || temp > 125) {
  431. temp_code = -EINVAL;
  432. goto out;
  433. }
  434. switch (pdata->cal_type) {
  435. case TYPE_TWO_POINT_TRIMMING:
  436. temp_code = (temp - 25) *
  437. (data->temp_error2 - data->temp_error1) /
  438. (85 - 25) + data->temp_error1;
  439. break;
  440. case TYPE_ONE_POINT_TRIMMING:
  441. temp_code = temp + data->temp_error1 - 25;
  442. break;
  443. default:
  444. temp_code = temp + EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET;
  445. break;
  446. }
  447. out:
  448. return temp_code;
  449. }
  450. /*
  451. * Calculate a temperature value from a temperature code.
  452. * The unit of the temperature is degree Celsius.
  453. */
  454. static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
  455. {
  456. struct exynos_tmu_platform_data *pdata = data->pdata;
  457. int temp;
  458. if (data->soc == SOC_ARCH_EXYNOS4210)
  459. /* temp_code should range between 75 and 175 */
  460. if (temp_code < 75 || temp_code > 175) {
  461. temp = -ENODATA;
  462. goto out;
  463. }
  464. switch (pdata->cal_type) {
  465. case TYPE_TWO_POINT_TRIMMING:
  466. temp = (temp_code - data->temp_error1) * (85 - 25) /
  467. (data->temp_error2 - data->temp_error1) + 25;
  468. break;
  469. case TYPE_ONE_POINT_TRIMMING:
  470. temp = temp_code - data->temp_error1 + 25;
  471. break;
  472. default:
  473. temp = temp_code - EXYNOS_TMU_DEF_CODE_TO_TEMP_OFFSET;
  474. break;
  475. }
  476. out:
  477. return temp;
  478. }
  479. static int exynos_tmu_initialize(struct platform_device *pdev)
  480. {
  481. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  482. struct exynos_tmu_platform_data *pdata = data->pdata;
  483. unsigned int status, trim_info, rising_threshold;
  484. int ret = 0, threshold_code;
  485. mutex_lock(&data->lock);
  486. clk_enable(data->clk);
  487. status = readb(data->base + EXYNOS_TMU_REG_STATUS);
  488. if (!status) {
  489. ret = -EBUSY;
  490. goto out;
  491. }
  492. if (data->soc == SOC_ARCH_EXYNOS) {
  493. __raw_writel(EXYNOS_TRIMINFO_RELOAD,
  494. data->base + EXYNOS_TMU_TRIMINFO_CON);
  495. }
  496. /* Save trimming info in order to perform calibration */
  497. trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
  498. data->temp_error1 = trim_info & EXYNOS_TMU_TRIM_TEMP_MASK;
  499. data->temp_error2 = ((trim_info >> 8) & EXYNOS_TMU_TRIM_TEMP_MASK);
  500. if ((EFUSE_MIN_VALUE > data->temp_error1) ||
  501. (data->temp_error1 > EFUSE_MAX_VALUE) ||
  502. (data->temp_error2 != 0))
  503. data->temp_error1 = pdata->efuse_value;
  504. if (data->soc == SOC_ARCH_EXYNOS4210) {
  505. /* Write temperature code for threshold */
  506. threshold_code = temp_to_code(data, pdata->threshold);
  507. if (threshold_code < 0) {
  508. ret = threshold_code;
  509. goto out;
  510. }
  511. writeb(threshold_code,
  512. data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
  513. writeb(pdata->trigger_levels[0],
  514. data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL0);
  515. writeb(pdata->trigger_levels[1],
  516. data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL1);
  517. writeb(pdata->trigger_levels[2],
  518. data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL2);
  519. writeb(pdata->trigger_levels[3],
  520. data->base + EXYNOS4210_TMU_REG_TRIG_LEVEL3);
  521. writel(EXYNOS4210_TMU_INTCLEAR_VAL,
  522. data->base + EXYNOS_TMU_REG_INTCLEAR);
  523. } else if (data->soc == SOC_ARCH_EXYNOS) {
  524. /* Write temperature code for threshold */
  525. threshold_code = temp_to_code(data, pdata->trigger_levels[0]);
  526. if (threshold_code < 0) {
  527. ret = threshold_code;
  528. goto out;
  529. }
  530. rising_threshold = threshold_code;
  531. threshold_code = temp_to_code(data, pdata->trigger_levels[1]);
  532. if (threshold_code < 0) {
  533. ret = threshold_code;
  534. goto out;
  535. }
  536. rising_threshold |= (threshold_code << 8);
  537. threshold_code = temp_to_code(data, pdata->trigger_levels[2]);
  538. if (threshold_code < 0) {
  539. ret = threshold_code;
  540. goto out;
  541. }
  542. rising_threshold |= (threshold_code << 16);
  543. writel(rising_threshold,
  544. data->base + EXYNOS_THD_TEMP_RISE);
  545. writel(0, data->base + EXYNOS_THD_TEMP_FALL);
  546. writel(EXYNOS_TMU_CLEAR_RISE_INT|EXYNOS_TMU_CLEAR_FALL_INT,
  547. data->base + EXYNOS_TMU_REG_INTCLEAR);
  548. }
  549. out:
  550. clk_disable(data->clk);
  551. mutex_unlock(&data->lock);
  552. return ret;
  553. }
  554. static void exynos_tmu_control(struct platform_device *pdev, bool on)
  555. {
  556. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  557. struct exynos_tmu_platform_data *pdata = data->pdata;
  558. unsigned int con, interrupt_en;
  559. mutex_lock(&data->lock);
  560. clk_enable(data->clk);
  561. con = pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT |
  562. pdata->gain << EXYNOS_TMU_GAIN_SHIFT;
  563. if (data->soc == SOC_ARCH_EXYNOS) {
  564. con |= pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT;
  565. con |= (EXYNOS_MUX_ADDR_VALUE << EXYNOS_MUX_ADDR_SHIFT);
  566. }
  567. if (on) {
  568. con |= EXYNOS_TMU_CORE_ON;
  569. interrupt_en = pdata->trigger_level3_en << 12 |
  570. pdata->trigger_level2_en << 8 |
  571. pdata->trigger_level1_en << 4 |
  572. pdata->trigger_level0_en;
  573. } else {
  574. con |= EXYNOS_TMU_CORE_OFF;
  575. interrupt_en = 0; /* Disable all interrupts */
  576. }
  577. writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN);
  578. writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
  579. clk_disable(data->clk);
  580. mutex_unlock(&data->lock);
  581. }
  582. static int exynos_tmu_read(struct exynos_tmu_data *data)
  583. {
  584. u8 temp_code;
  585. int temp;
  586. mutex_lock(&data->lock);
  587. clk_enable(data->clk);
  588. temp_code = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
  589. temp = code_to_temp(data, temp_code);
  590. clk_disable(data->clk);
  591. mutex_unlock(&data->lock);
  592. return temp;
  593. }
  594. static void exynos_tmu_work(struct work_struct *work)
  595. {
  596. struct exynos_tmu_data *data = container_of(work,
  597. struct exynos_tmu_data, irq_work);
  598. mutex_lock(&data->lock);
  599. clk_enable(data->clk);
  600. if (data->soc == SOC_ARCH_EXYNOS)
  601. writel(EXYNOS_TMU_CLEAR_RISE_INT,
  602. data->base + EXYNOS_TMU_REG_INTCLEAR);
  603. else
  604. writel(EXYNOS4210_TMU_INTCLEAR_VAL,
  605. data->base + EXYNOS_TMU_REG_INTCLEAR);
  606. clk_disable(data->clk);
  607. mutex_unlock(&data->lock);
  608. exynos_report_trigger();
  609. enable_irq(data->irq);
  610. }
  611. static irqreturn_t exynos_tmu_irq(int irq, void *id)
  612. {
  613. struct exynos_tmu_data *data = id;
  614. disable_irq_nosync(irq);
  615. schedule_work(&data->irq_work);
  616. return IRQ_HANDLED;
  617. }
  618. static struct thermal_sensor_conf exynos_sensor_conf = {
  619. .name = "exynos-therm",
  620. .read_temperature = (int (*)(void *))exynos_tmu_read,
  621. };
  622. #if defined(CONFIG_CPU_EXYNOS4210)
  623. static struct exynos_tmu_platform_data const exynos4210_default_tmu_data = {
  624. .threshold = 80,
  625. .trigger_levels[0] = 5,
  626. .trigger_levels[1] = 20,
  627. .trigger_levels[2] = 30,
  628. .trigger_level0_en = 1,
  629. .trigger_level1_en = 1,
  630. .trigger_level2_en = 1,
  631. .trigger_level3_en = 0,
  632. .gain = 15,
  633. .reference_voltage = 7,
  634. .cal_type = TYPE_ONE_POINT_TRIMMING,
  635. .freq_tab[0] = {
  636. .freq_clip_max = 800 * 1000,
  637. .temp_level = 85,
  638. },
  639. .freq_tab[1] = {
  640. .freq_clip_max = 200 * 1000,
  641. .temp_level = 100,
  642. },
  643. .freq_tab_count = 2,
  644. .type = SOC_ARCH_EXYNOS4210,
  645. };
  646. #define EXYNOS4210_TMU_DRV_DATA (&exynos4210_default_tmu_data)
  647. #else
  648. #define EXYNOS4210_TMU_DRV_DATA (NULL)
  649. #endif
  650. #if defined(CONFIG_SOC_EXYNOS5250) || defined(CONFIG_SOC_EXYNOS4412)
  651. static struct exynos_tmu_platform_data const exynos_default_tmu_data = {
  652. .trigger_levels[0] = 85,
  653. .trigger_levels[1] = 103,
  654. .trigger_levels[2] = 110,
  655. .trigger_level0_en = 1,
  656. .trigger_level1_en = 1,
  657. .trigger_level2_en = 1,
  658. .trigger_level3_en = 0,
  659. .gain = 8,
  660. .reference_voltage = 16,
  661. .noise_cancel_mode = 4,
  662. .cal_type = TYPE_ONE_POINT_TRIMMING,
  663. .efuse_value = 55,
  664. .freq_tab[0] = {
  665. .freq_clip_max = 800 * 1000,
  666. .temp_level = 85,
  667. },
  668. .freq_tab[1] = {
  669. .freq_clip_max = 200 * 1000,
  670. .temp_level = 103,
  671. },
  672. .freq_tab_count = 2,
  673. .type = SOC_ARCH_EXYNOS,
  674. };
  675. #define EXYNOS_TMU_DRV_DATA (&exynos_default_tmu_data)
  676. #else
  677. #define EXYNOS_TMU_DRV_DATA (NULL)
  678. #endif
  679. #ifdef CONFIG_OF
  680. static const struct of_device_id exynos_tmu_match[] = {
  681. {
  682. .compatible = "samsung,exynos4210-tmu",
  683. .data = (void *)EXYNOS4210_TMU_DRV_DATA,
  684. },
  685. {
  686. .compatible = "samsung,exynos5250-tmu",
  687. .data = (void *)EXYNOS_TMU_DRV_DATA,
  688. },
  689. {},
  690. };
  691. MODULE_DEVICE_TABLE(of, exynos_tmu_match);
  692. #else
  693. #define exynos_tmu_match NULL
  694. #endif
  695. static struct platform_device_id exynos_tmu_driver_ids[] = {
  696. {
  697. .name = "exynos4210-tmu",
  698. .driver_data = (kernel_ulong_t)EXYNOS4210_TMU_DRV_DATA,
  699. },
  700. {
  701. .name = "exynos5250-tmu",
  702. .driver_data = (kernel_ulong_t)EXYNOS_TMU_DRV_DATA,
  703. },
  704. { },
  705. };
  706. MODULE_DEVICE_TABLE(platform, exynos_tmu_driver_ids);
  707. static inline struct exynos_tmu_platform_data *exynos_get_driver_data(
  708. struct platform_device *pdev)
  709. {
  710. #ifdef CONFIG_OF
  711. if (pdev->dev.of_node) {
  712. const struct of_device_id *match;
  713. match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
  714. if (!match)
  715. return NULL;
  716. return (struct exynos_tmu_platform_data *) match->data;
  717. }
  718. #endif
  719. return (struct exynos_tmu_platform_data *)
  720. platform_get_device_id(pdev)->driver_data;
  721. }
  722. static int exynos_tmu_probe(struct platform_device *pdev)
  723. {
  724. struct exynos_tmu_data *data;
  725. struct exynos_tmu_platform_data *pdata = pdev->dev.platform_data;
  726. int ret, i;
  727. if (!pdata)
  728. pdata = exynos_get_driver_data(pdev);
  729. if (!pdata) {
  730. dev_err(&pdev->dev, "No platform init data supplied.\n");
  731. return -ENODEV;
  732. }
  733. data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
  734. GFP_KERNEL);
  735. if (!data) {
  736. dev_err(&pdev->dev, "Failed to allocate driver structure\n");
  737. return -ENOMEM;
  738. }
  739. data->irq = platform_get_irq(pdev, 0);
  740. if (data->irq < 0) {
  741. dev_err(&pdev->dev, "Failed to get platform irq\n");
  742. return data->irq;
  743. }
  744. INIT_WORK(&data->irq_work, exynos_tmu_work);
  745. data->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  746. if (!data->mem) {
  747. dev_err(&pdev->dev, "Failed to get platform resource\n");
  748. return -ENOENT;
  749. }
  750. data->base = devm_request_and_ioremap(&pdev->dev, data->mem);
  751. if (!data->base) {
  752. dev_err(&pdev->dev, "Failed to ioremap memory\n");
  753. return -ENODEV;
  754. }
  755. ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
  756. IRQF_TRIGGER_RISING, "exynos-tmu", data);
  757. if (ret) {
  758. dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
  759. return ret;
  760. }
  761. data->clk = clk_get(NULL, "tmu_apbif");
  762. if (IS_ERR(data->clk)) {
  763. dev_err(&pdev->dev, "Failed to get clock\n");
  764. return PTR_ERR(data->clk);
  765. }
  766. if (pdata->type == SOC_ARCH_EXYNOS ||
  767. pdata->type == SOC_ARCH_EXYNOS4210)
  768. data->soc = pdata->type;
  769. else {
  770. ret = -EINVAL;
  771. dev_err(&pdev->dev, "Platform not supported\n");
  772. goto err_clk;
  773. }
  774. data->pdata = pdata;
  775. platform_set_drvdata(pdev, data);
  776. mutex_init(&data->lock);
  777. ret = exynos_tmu_initialize(pdev);
  778. if (ret) {
  779. dev_err(&pdev->dev, "Failed to initialize TMU\n");
  780. goto err_clk;
  781. }
  782. exynos_tmu_control(pdev, true);
  783. /* Register the sensor with thermal management interface */
  784. (&exynos_sensor_conf)->private_data = data;
  785. exynos_sensor_conf.trip_data.trip_count = pdata->trigger_level0_en +
  786. pdata->trigger_level1_en + pdata->trigger_level2_en +
  787. pdata->trigger_level3_en;
  788. for (i = 0; i < exynos_sensor_conf.trip_data.trip_count; i++)
  789. exynos_sensor_conf.trip_data.trip_val[i] =
  790. pdata->threshold + pdata->trigger_levels[i];
  791. exynos_sensor_conf.cooling_data.freq_clip_count =
  792. pdata->freq_tab_count;
  793. for (i = 0; i < pdata->freq_tab_count; i++) {
  794. exynos_sensor_conf.cooling_data.freq_data[i].freq_clip_max =
  795. pdata->freq_tab[i].freq_clip_max;
  796. exynos_sensor_conf.cooling_data.freq_data[i].temp_level =
  797. pdata->freq_tab[i].temp_level;
  798. }
  799. ret = exynos_register_thermal(&exynos_sensor_conf);
  800. if (ret) {
  801. dev_err(&pdev->dev, "Failed to register thermal interface\n");
  802. goto err_clk;
  803. }
  804. return 0;
  805. err_clk:
  806. platform_set_drvdata(pdev, NULL);
  807. clk_put(data->clk);
  808. return ret;
  809. }
  810. static int exynos_tmu_remove(struct platform_device *pdev)
  811. {
  812. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  813. exynos_tmu_control(pdev, false);
  814. exynos_unregister_thermal();
  815. clk_put(data->clk);
  816. platform_set_drvdata(pdev, NULL);
  817. return 0;
  818. }
  819. #ifdef CONFIG_PM_SLEEP
  820. static int exynos_tmu_suspend(struct device *dev)
  821. {
  822. exynos_tmu_control(to_platform_device(dev), false);
  823. return 0;
  824. }
  825. static int exynos_tmu_resume(struct device *dev)
  826. {
  827. struct platform_device *pdev = to_platform_device(dev);
  828. exynos_tmu_initialize(pdev);
  829. exynos_tmu_control(pdev, true);
  830. return 0;
  831. }
  832. static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
  833. exynos_tmu_suspend, exynos_tmu_resume);
  834. #define EXYNOS_TMU_PM (&exynos_tmu_pm)
  835. #else
  836. #define EXYNOS_TMU_PM NULL
  837. #endif
  838. static struct platform_driver exynos_tmu_driver = {
  839. .driver = {
  840. .name = "exynos-tmu",
  841. .owner = THIS_MODULE,
  842. .pm = EXYNOS_TMU_PM,
  843. .of_match_table = exynos_tmu_match,
  844. },
  845. .probe = exynos_tmu_probe,
  846. .remove = exynos_tmu_remove,
  847. .id_table = exynos_tmu_driver_ids,
  848. };
  849. module_platform_driver(exynos_tmu_driver);
  850. MODULE_DESCRIPTION("EXYNOS TMU Driver");
  851. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  852. MODULE_LICENSE("GPL");
  853. MODULE_ALIAS("platform:exynos-tmu");