db8500_thermal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * db8500_thermal.c - DB8500 Thermal Management Implementation
  3. *
  4. * Copyright (C) 2012 ST-Ericsson
  5. * Copyright (C) 2012 Linaro Ltd.
  6. *
  7. * Author: Hongbo Zhang <hongbo.zhang@linaro.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/cpu_cooling.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/mfd/dbx500-prcmu.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_data/db8500_thermal.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/thermal.h>
  28. #define PRCMU_DEFAULT_MEASURE_TIME 0xFFF
  29. #define PRCMU_DEFAULT_LOW_TEMP 0
  30. struct db8500_thermal_zone {
  31. struct thermal_zone_device *therm_dev;
  32. struct mutex th_lock;
  33. struct work_struct therm_work;
  34. struct db8500_thsens_platform_data *trip_tab;
  35. enum thermal_device_mode mode;
  36. enum thermal_trend trend;
  37. unsigned long cur_temp_pseudo;
  38. unsigned int cur_index;
  39. };
  40. /* Local function to check if thermal zone matches cooling devices */
  41. static int db8500_thermal_match_cdev(struct thermal_cooling_device *cdev,
  42. struct db8500_trip_point *trip_point)
  43. {
  44. int i;
  45. if (!strlen(cdev->type))
  46. return -EINVAL;
  47. for (i = 0; i < COOLING_DEV_MAX; i++) {
  48. if (!strcmp(trip_point->cdev_name[i], cdev->type))
  49. return 0;
  50. }
  51. return -ENODEV;
  52. }
  53. /* Callback to bind cooling device to thermal zone */
  54. static int db8500_cdev_bind(struct thermal_zone_device *thermal,
  55. struct thermal_cooling_device *cdev)
  56. {
  57. struct db8500_thermal_zone *pzone = thermal->devdata;
  58. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  59. unsigned long max_state, upper, lower;
  60. int i, ret = -EINVAL;
  61. cdev->ops->get_max_state(cdev, &max_state);
  62. for (i = 0; i < ptrips->num_trips; i++) {
  63. if (db8500_thermal_match_cdev(cdev, &ptrips->trip_points[i]))
  64. continue;
  65. upper = lower = i > max_state ? max_state : i;
  66. ret = thermal_zone_bind_cooling_device(thermal, i, cdev,
  67. upper, lower, THERMAL_WEIGHT_DEFAULT);
  68. dev_info(&cdev->device, "%s bind to %d: %d-%s\n", cdev->type,
  69. i, ret, ret ? "fail" : "succeed");
  70. }
  71. return ret;
  72. }
  73. /* Callback to unbind cooling device from thermal zone */
  74. static int db8500_cdev_unbind(struct thermal_zone_device *thermal,
  75. struct thermal_cooling_device *cdev)
  76. {
  77. struct db8500_thermal_zone *pzone = thermal->devdata;
  78. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  79. int i, ret = -EINVAL;
  80. for (i = 0; i < ptrips->num_trips; i++) {
  81. if (db8500_thermal_match_cdev(cdev, &ptrips->trip_points[i]))
  82. continue;
  83. ret = thermal_zone_unbind_cooling_device(thermal, i, cdev);
  84. dev_info(&cdev->device, "%s unbind from %d: %s\n", cdev->type,
  85. i, ret ? "fail" : "succeed");
  86. }
  87. return ret;
  88. }
  89. /* Callback to get current temperature */
  90. static int db8500_sys_get_temp(struct thermal_zone_device *thermal, int *temp)
  91. {
  92. struct db8500_thermal_zone *pzone = thermal->devdata;
  93. /*
  94. * TODO: There is no PRCMU interface to get temperature data currently,
  95. * so a pseudo temperature is returned , it works for thermal framework
  96. * and this will be fixed when the PRCMU interface is available.
  97. */
  98. *temp = pzone->cur_temp_pseudo;
  99. return 0;
  100. }
  101. /* Callback to get temperature changing trend */
  102. static int db8500_sys_get_trend(struct thermal_zone_device *thermal,
  103. int trip, enum thermal_trend *trend)
  104. {
  105. struct db8500_thermal_zone *pzone = thermal->devdata;
  106. *trend = pzone->trend;
  107. return 0;
  108. }
  109. /* Callback to get thermal zone mode */
  110. static int db8500_sys_get_mode(struct thermal_zone_device *thermal,
  111. enum thermal_device_mode *mode)
  112. {
  113. struct db8500_thermal_zone *pzone = thermal->devdata;
  114. mutex_lock(&pzone->th_lock);
  115. *mode = pzone->mode;
  116. mutex_unlock(&pzone->th_lock);
  117. return 0;
  118. }
  119. /* Callback to set thermal zone mode */
  120. static int db8500_sys_set_mode(struct thermal_zone_device *thermal,
  121. enum thermal_device_mode mode)
  122. {
  123. struct db8500_thermal_zone *pzone = thermal->devdata;
  124. mutex_lock(&pzone->th_lock);
  125. pzone->mode = mode;
  126. if (mode == THERMAL_DEVICE_ENABLED)
  127. schedule_work(&pzone->therm_work);
  128. mutex_unlock(&pzone->th_lock);
  129. return 0;
  130. }
  131. /* Callback to get trip point type */
  132. static int db8500_sys_get_trip_type(struct thermal_zone_device *thermal,
  133. int trip, enum thermal_trip_type *type)
  134. {
  135. struct db8500_thermal_zone *pzone = thermal->devdata;
  136. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  137. if (trip >= ptrips->num_trips)
  138. return -EINVAL;
  139. *type = ptrips->trip_points[trip].type;
  140. return 0;
  141. }
  142. /* Callback to get trip point temperature */
  143. static int db8500_sys_get_trip_temp(struct thermal_zone_device *thermal,
  144. int trip, int *temp)
  145. {
  146. struct db8500_thermal_zone *pzone = thermal->devdata;
  147. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  148. if (trip >= ptrips->num_trips)
  149. return -EINVAL;
  150. *temp = ptrips->trip_points[trip].temp;
  151. return 0;
  152. }
  153. /* Callback to get critical trip point temperature */
  154. static int db8500_sys_get_crit_temp(struct thermal_zone_device *thermal,
  155. int *temp)
  156. {
  157. struct db8500_thermal_zone *pzone = thermal->devdata;
  158. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  159. int i;
  160. for (i = ptrips->num_trips - 1; i > 0; i--) {
  161. if (ptrips->trip_points[i].type == THERMAL_TRIP_CRITICAL) {
  162. *temp = ptrips->trip_points[i].temp;
  163. return 0;
  164. }
  165. }
  166. return -EINVAL;
  167. }
  168. static struct thermal_zone_device_ops thdev_ops = {
  169. .bind = db8500_cdev_bind,
  170. .unbind = db8500_cdev_unbind,
  171. .get_temp = db8500_sys_get_temp,
  172. .get_trend = db8500_sys_get_trend,
  173. .get_mode = db8500_sys_get_mode,
  174. .set_mode = db8500_sys_set_mode,
  175. .get_trip_type = db8500_sys_get_trip_type,
  176. .get_trip_temp = db8500_sys_get_trip_temp,
  177. .get_crit_temp = db8500_sys_get_crit_temp,
  178. };
  179. static void db8500_thermal_update_config(struct db8500_thermal_zone *pzone,
  180. unsigned int idx, enum thermal_trend trend,
  181. unsigned long next_low, unsigned long next_high)
  182. {
  183. prcmu_stop_temp_sense();
  184. pzone->cur_index = idx;
  185. pzone->cur_temp_pseudo = (next_low + next_high)/2;
  186. pzone->trend = trend;
  187. prcmu_config_hotmon((u8)(next_low/1000), (u8)(next_high/1000));
  188. prcmu_start_temp_sense(PRCMU_DEFAULT_MEASURE_TIME);
  189. }
  190. static irqreturn_t prcmu_low_irq_handler(int irq, void *irq_data)
  191. {
  192. struct db8500_thermal_zone *pzone = irq_data;
  193. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  194. unsigned int idx = pzone->cur_index;
  195. unsigned long next_low, next_high;
  196. if (unlikely(idx == 0))
  197. /* Meaningless for thermal management, ignoring it */
  198. return IRQ_HANDLED;
  199. if (idx == 1) {
  200. next_high = ptrips->trip_points[0].temp;
  201. next_low = PRCMU_DEFAULT_LOW_TEMP;
  202. } else {
  203. next_high = ptrips->trip_points[idx-1].temp;
  204. next_low = ptrips->trip_points[idx-2].temp;
  205. }
  206. idx -= 1;
  207. db8500_thermal_update_config(pzone, idx, THERMAL_TREND_DROPPING,
  208. next_low, next_high);
  209. dev_dbg(&pzone->therm_dev->device,
  210. "PRCMU set max %ld, min %ld\n", next_high, next_low);
  211. schedule_work(&pzone->therm_work);
  212. return IRQ_HANDLED;
  213. }
  214. static irqreturn_t prcmu_high_irq_handler(int irq, void *irq_data)
  215. {
  216. struct db8500_thermal_zone *pzone = irq_data;
  217. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  218. unsigned int idx = pzone->cur_index;
  219. unsigned long next_low, next_high;
  220. if (idx < ptrips->num_trips - 1) {
  221. next_high = ptrips->trip_points[idx+1].temp;
  222. next_low = ptrips->trip_points[idx].temp;
  223. idx += 1;
  224. db8500_thermal_update_config(pzone, idx, THERMAL_TREND_RAISING,
  225. next_low, next_high);
  226. dev_dbg(&pzone->therm_dev->device,
  227. "PRCMU set max %ld, min %ld\n", next_high, next_low);
  228. } else if (idx == ptrips->num_trips - 1)
  229. pzone->cur_temp_pseudo = ptrips->trip_points[idx].temp + 1;
  230. schedule_work(&pzone->therm_work);
  231. return IRQ_HANDLED;
  232. }
  233. static void db8500_thermal_work(struct work_struct *work)
  234. {
  235. enum thermal_device_mode cur_mode;
  236. struct db8500_thermal_zone *pzone;
  237. pzone = container_of(work, struct db8500_thermal_zone, therm_work);
  238. mutex_lock(&pzone->th_lock);
  239. cur_mode = pzone->mode;
  240. mutex_unlock(&pzone->th_lock);
  241. if (cur_mode == THERMAL_DEVICE_DISABLED)
  242. return;
  243. thermal_zone_device_update(pzone->therm_dev);
  244. dev_dbg(&pzone->therm_dev->device, "thermal work finished.\n");
  245. }
  246. #ifdef CONFIG_OF
  247. static struct db8500_thsens_platform_data*
  248. db8500_thermal_parse_dt(struct platform_device *pdev)
  249. {
  250. struct db8500_thsens_platform_data *ptrips;
  251. struct device_node *np = pdev->dev.of_node;
  252. char prop_name[32];
  253. const char *tmp_str;
  254. u32 tmp_data;
  255. int i, j;
  256. ptrips = devm_kzalloc(&pdev->dev, sizeof(*ptrips), GFP_KERNEL);
  257. if (!ptrips)
  258. return NULL;
  259. if (of_property_read_u32(np, "num-trips", &tmp_data))
  260. goto err_parse_dt;
  261. if (tmp_data > THERMAL_MAX_TRIPS)
  262. goto err_parse_dt;
  263. ptrips->num_trips = tmp_data;
  264. for (i = 0; i < ptrips->num_trips; i++) {
  265. sprintf(prop_name, "trip%d-temp", i);
  266. if (of_property_read_u32(np, prop_name, &tmp_data))
  267. goto err_parse_dt;
  268. ptrips->trip_points[i].temp = tmp_data;
  269. sprintf(prop_name, "trip%d-type", i);
  270. if (of_property_read_string(np, prop_name, &tmp_str))
  271. goto err_parse_dt;
  272. if (!strcmp(tmp_str, "active"))
  273. ptrips->trip_points[i].type = THERMAL_TRIP_ACTIVE;
  274. else if (!strcmp(tmp_str, "passive"))
  275. ptrips->trip_points[i].type = THERMAL_TRIP_PASSIVE;
  276. else if (!strcmp(tmp_str, "hot"))
  277. ptrips->trip_points[i].type = THERMAL_TRIP_HOT;
  278. else if (!strcmp(tmp_str, "critical"))
  279. ptrips->trip_points[i].type = THERMAL_TRIP_CRITICAL;
  280. else
  281. goto err_parse_dt;
  282. sprintf(prop_name, "trip%d-cdev-num", i);
  283. if (of_property_read_u32(np, prop_name, &tmp_data))
  284. goto err_parse_dt;
  285. if (tmp_data > COOLING_DEV_MAX)
  286. goto err_parse_dt;
  287. for (j = 0; j < tmp_data; j++) {
  288. sprintf(prop_name, "trip%d-cdev-name%d", i, j);
  289. if (of_property_read_string(np, prop_name, &tmp_str))
  290. goto err_parse_dt;
  291. if (strlen(tmp_str) >= THERMAL_NAME_LENGTH)
  292. goto err_parse_dt;
  293. strcpy(ptrips->trip_points[i].cdev_name[j], tmp_str);
  294. }
  295. }
  296. return ptrips;
  297. err_parse_dt:
  298. dev_err(&pdev->dev, "Parsing device tree data error.\n");
  299. return NULL;
  300. }
  301. #else
  302. static inline struct db8500_thsens_platform_data*
  303. db8500_thermal_parse_dt(struct platform_device *pdev)
  304. {
  305. return NULL;
  306. }
  307. #endif
  308. static int db8500_thermal_probe(struct platform_device *pdev)
  309. {
  310. struct db8500_thermal_zone *pzone = NULL;
  311. struct db8500_thsens_platform_data *ptrips = NULL;
  312. struct device_node *np = pdev->dev.of_node;
  313. int low_irq, high_irq, ret = 0;
  314. unsigned long dft_low, dft_high;
  315. if (np)
  316. ptrips = db8500_thermal_parse_dt(pdev);
  317. else
  318. ptrips = dev_get_platdata(&pdev->dev);
  319. if (!ptrips)
  320. return -EINVAL;
  321. pzone = devm_kzalloc(&pdev->dev, sizeof(*pzone), GFP_KERNEL);
  322. if (!pzone)
  323. return -ENOMEM;
  324. mutex_init(&pzone->th_lock);
  325. mutex_lock(&pzone->th_lock);
  326. pzone->mode = THERMAL_DEVICE_DISABLED;
  327. pzone->trip_tab = ptrips;
  328. INIT_WORK(&pzone->therm_work, db8500_thermal_work);
  329. low_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_LOW");
  330. if (low_irq < 0) {
  331. dev_err(&pdev->dev, "Get IRQ_HOTMON_LOW failed.\n");
  332. ret = low_irq;
  333. goto out_unlock;
  334. }
  335. ret = devm_request_threaded_irq(&pdev->dev, low_irq, NULL,
  336. prcmu_low_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
  337. "dbx500_temp_low", pzone);
  338. if (ret < 0) {
  339. dev_err(&pdev->dev, "Failed to allocate temp low irq.\n");
  340. goto out_unlock;
  341. }
  342. high_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_HIGH");
  343. if (high_irq < 0) {
  344. dev_err(&pdev->dev, "Get IRQ_HOTMON_HIGH failed.\n");
  345. ret = high_irq;
  346. goto out_unlock;
  347. }
  348. ret = devm_request_threaded_irq(&pdev->dev, high_irq, NULL,
  349. prcmu_high_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
  350. "dbx500_temp_high", pzone);
  351. if (ret < 0) {
  352. dev_err(&pdev->dev, "Failed to allocate temp high irq.\n");
  353. goto out_unlock;
  354. }
  355. pzone->therm_dev = thermal_zone_device_register("db8500_thermal_zone",
  356. ptrips->num_trips, 0, pzone, &thdev_ops, NULL, 0, 0);
  357. if (IS_ERR(pzone->therm_dev)) {
  358. dev_err(&pdev->dev, "Register thermal zone device failed.\n");
  359. ret = PTR_ERR(pzone->therm_dev);
  360. goto out_unlock;
  361. }
  362. dev_info(&pdev->dev, "Thermal zone device registered.\n");
  363. dft_low = PRCMU_DEFAULT_LOW_TEMP;
  364. dft_high = ptrips->trip_points[0].temp;
  365. db8500_thermal_update_config(pzone, 0, THERMAL_TREND_STABLE,
  366. dft_low, dft_high);
  367. platform_set_drvdata(pdev, pzone);
  368. pzone->mode = THERMAL_DEVICE_ENABLED;
  369. out_unlock:
  370. mutex_unlock(&pzone->th_lock);
  371. return ret;
  372. }
  373. static int db8500_thermal_remove(struct platform_device *pdev)
  374. {
  375. struct db8500_thermal_zone *pzone = platform_get_drvdata(pdev);
  376. thermal_zone_device_unregister(pzone->therm_dev);
  377. cancel_work_sync(&pzone->therm_work);
  378. mutex_destroy(&pzone->th_lock);
  379. return 0;
  380. }
  381. static int db8500_thermal_suspend(struct platform_device *pdev,
  382. pm_message_t state)
  383. {
  384. struct db8500_thermal_zone *pzone = platform_get_drvdata(pdev);
  385. flush_work(&pzone->therm_work);
  386. prcmu_stop_temp_sense();
  387. return 0;
  388. }
  389. static int db8500_thermal_resume(struct platform_device *pdev)
  390. {
  391. struct db8500_thermal_zone *pzone = platform_get_drvdata(pdev);
  392. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  393. unsigned long dft_low, dft_high;
  394. dft_low = PRCMU_DEFAULT_LOW_TEMP;
  395. dft_high = ptrips->trip_points[0].temp;
  396. db8500_thermal_update_config(pzone, 0, THERMAL_TREND_STABLE,
  397. dft_low, dft_high);
  398. return 0;
  399. }
  400. #ifdef CONFIG_OF
  401. static const struct of_device_id db8500_thermal_match[] = {
  402. { .compatible = "stericsson,db8500-thermal" },
  403. {},
  404. };
  405. #endif
  406. static struct platform_driver db8500_thermal_driver = {
  407. .driver = {
  408. .name = "db8500-thermal",
  409. .of_match_table = of_match_ptr(db8500_thermal_match),
  410. },
  411. .probe = db8500_thermal_probe,
  412. .suspend = db8500_thermal_suspend,
  413. .resume = db8500_thermal_resume,
  414. .remove = db8500_thermal_remove,
  415. };
  416. module_platform_driver(db8500_thermal_driver);
  417. MODULE_AUTHOR("Hongbo Zhang <hongbo.zhang@stericsson.com>");
  418. MODULE_DESCRIPTION("DB8500 thermal driver");
  419. MODULE_LICENSE("GPL");