exynos_thermal.c 28 KB

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