db8500_thermal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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,
  91. unsigned long *temp)
  92. {
  93. struct db8500_thermal_zone *pzone = thermal->devdata;
  94. /*
  95. * TODO: There is no PRCMU interface to get temperature data currently,
  96. * so a pseudo temperature is returned , it works for thermal framework
  97. * and this will be fixed when the PRCMU interface is available.
  98. */
  99. *temp = pzone->cur_temp_pseudo;
  100. return 0;
  101. }
  102. /* Callback to get temperature changing trend */
  103. static int db8500_sys_get_trend(struct thermal_zone_device *thermal,
  104. int trip, enum thermal_trend *trend)
  105. {
  106. struct db8500_thermal_zone *pzone = thermal->devdata;
  107. *trend = pzone->trend;
  108. return 0;
  109. }
  110. /* Callback to get thermal zone mode */
  111. static int db8500_sys_get_mode(struct thermal_zone_device *thermal,
  112. enum thermal_device_mode *mode)
  113. {
  114. struct db8500_thermal_zone *pzone = thermal->devdata;
  115. mutex_lock(&pzone->th_lock);
  116. *mode = pzone->mode;
  117. mutex_unlock(&pzone->th_lock);
  118. return 0;
  119. }
  120. /* Callback to set thermal zone mode */
  121. static int db8500_sys_set_mode(struct thermal_zone_device *thermal,
  122. enum thermal_device_mode mode)
  123. {
  124. struct db8500_thermal_zone *pzone = thermal->devdata;
  125. mutex_lock(&pzone->th_lock);
  126. pzone->mode = mode;
  127. if (mode == THERMAL_DEVICE_ENABLED)
  128. schedule_work(&pzone->therm_work);
  129. mutex_unlock(&pzone->th_lock);
  130. return 0;
  131. }
  132. /* Callback to get trip point type */
  133. static int db8500_sys_get_trip_type(struct thermal_zone_device *thermal,
  134. int trip, enum thermal_trip_type *type)
  135. {
  136. struct db8500_thermal_zone *pzone = thermal->devdata;
  137. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  138. if (trip >= ptrips->num_trips)
  139. return -EINVAL;
  140. *type = ptrips->trip_points[trip].type;
  141. return 0;
  142. }
  143. /* Callback to get trip point temperature */
  144. static int db8500_sys_get_trip_temp(struct thermal_zone_device *thermal,
  145. int trip, unsigned long *temp)
  146. {
  147. struct db8500_thermal_zone *pzone = thermal->devdata;
  148. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  149. if (trip >= ptrips->num_trips)
  150. return -EINVAL;
  151. *temp = ptrips->trip_points[trip].temp;
  152. return 0;
  153. }
  154. /* Callback to get critical trip point temperature */
  155. static int db8500_sys_get_crit_temp(struct thermal_zone_device *thermal,
  156. unsigned long *temp)
  157. {
  158. struct db8500_thermal_zone *pzone = thermal->devdata;
  159. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  160. int i;
  161. for (i = ptrips->num_trips - 1; i > 0; i--) {
  162. if (ptrips->trip_points[i].type == THERMAL_TRIP_CRITICAL) {
  163. *temp = ptrips->trip_points[i].temp;
  164. return 0;
  165. }
  166. }
  167. return -EINVAL;
  168. }
  169. static struct thermal_zone_device_ops thdev_ops = {
  170. .bind = db8500_cdev_bind,
  171. .unbind = db8500_cdev_unbind,
  172. .get_temp = db8500_sys_get_temp,
  173. .get_trend = db8500_sys_get_trend,
  174. .get_mode = db8500_sys_get_mode,
  175. .set_mode = db8500_sys_set_mode,
  176. .get_trip_type = db8500_sys_get_trip_type,
  177. .get_trip_temp = db8500_sys_get_trip_temp,
  178. .get_crit_temp = db8500_sys_get_crit_temp,
  179. };
  180. static void db8500_thermal_update_config(struct db8500_thermal_zone *pzone,
  181. unsigned int idx, enum thermal_trend trend,
  182. unsigned long next_low, unsigned long next_high)
  183. {
  184. prcmu_stop_temp_sense();
  185. pzone->cur_index = idx;
  186. pzone->cur_temp_pseudo = (next_low + next_high)/2;
  187. pzone->trend = trend;
  188. prcmu_config_hotmon((u8)(next_low/1000), (u8)(next_high/1000));
  189. prcmu_start_temp_sense(PRCMU_DEFAULT_MEASURE_TIME);
  190. }
  191. static irqreturn_t prcmu_low_irq_handler(int irq, void *irq_data)
  192. {
  193. struct db8500_thermal_zone *pzone = irq_data;
  194. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  195. unsigned int idx = pzone->cur_index;
  196. unsigned long next_low, next_high;
  197. if (unlikely(idx == 0))
  198. /* Meaningless for thermal management, ignoring it */
  199. return IRQ_HANDLED;
  200. if (idx == 1) {
  201. next_high = ptrips->trip_points[0].temp;
  202. next_low = PRCMU_DEFAULT_LOW_TEMP;
  203. } else {
  204. next_high = ptrips->trip_points[idx-1].temp;
  205. next_low = ptrips->trip_points[idx-2].temp;
  206. }
  207. idx -= 1;
  208. db8500_thermal_update_config(pzone, idx, THERMAL_TREND_DROPPING,
  209. next_low, next_high);
  210. dev_dbg(&pzone->therm_dev->device,
  211. "PRCMU set max %ld, min %ld\n", next_high, next_low);
  212. schedule_work(&pzone->therm_work);
  213. return IRQ_HANDLED;
  214. }
  215. static irqreturn_t prcmu_high_irq_handler(int irq, void *irq_data)
  216. {
  217. struct db8500_thermal_zone *pzone = irq_data;
  218. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  219. unsigned int idx = pzone->cur_index;
  220. unsigned long next_low, next_high;
  221. if (idx < ptrips->num_trips - 1) {
  222. next_high = ptrips->trip_points[idx+1].temp;
  223. next_low = ptrips->trip_points[idx].temp;
  224. idx += 1;
  225. db8500_thermal_update_config(pzone, idx, THERMAL_TREND_RAISING,
  226. next_low, next_high);
  227. dev_dbg(&pzone->therm_dev->device,
  228. "PRCMU set max %ld, min %ld\n", next_high, next_low);
  229. } else if (idx == ptrips->num_trips - 1)
  230. pzone->cur_temp_pseudo = ptrips->trip_points[idx].temp + 1;
  231. schedule_work(&pzone->therm_work);
  232. return IRQ_HANDLED;
  233. }
  234. static void db8500_thermal_work(struct work_struct *work)
  235. {
  236. enum thermal_device_mode cur_mode;
  237. struct db8500_thermal_zone *pzone;
  238. pzone = container_of(work, struct db8500_thermal_zone, therm_work);
  239. mutex_lock(&pzone->th_lock);
  240. cur_mode = pzone->mode;
  241. mutex_unlock(&pzone->th_lock);
  242. if (cur_mode == THERMAL_DEVICE_DISABLED)
  243. return;
  244. thermal_zone_device_update(pzone->therm_dev);
  245. dev_dbg(&pzone->therm_dev->device, "thermal work finished.\n");
  246. }
  247. #ifdef CONFIG_OF
  248. static struct db8500_thsens_platform_data*
  249. db8500_thermal_parse_dt(struct platform_device *pdev)
  250. {
  251. struct db8500_thsens_platform_data *ptrips;
  252. struct device_node *np = pdev->dev.of_node;
  253. char prop_name[32];
  254. const char *tmp_str;
  255. u32 tmp_data;
  256. int i, j;
  257. ptrips = devm_kzalloc(&pdev->dev, sizeof(*ptrips), GFP_KERNEL);
  258. if (!ptrips)
  259. return NULL;
  260. if (of_property_read_u32(np, "num-trips", &tmp_data))
  261. goto err_parse_dt;
  262. if (tmp_data > THERMAL_MAX_TRIPS)
  263. goto err_parse_dt;
  264. ptrips->num_trips = tmp_data;
  265. for (i = 0; i < ptrips->num_trips; i++) {
  266. sprintf(prop_name, "trip%d-temp", i);
  267. if (of_property_read_u32(np, prop_name, &tmp_data))
  268. goto err_parse_dt;
  269. ptrips->trip_points[i].temp = tmp_data;
  270. sprintf(prop_name, "trip%d-type", i);
  271. if (of_property_read_string(np, prop_name, &tmp_str))
  272. goto err_parse_dt;
  273. if (!strcmp(tmp_str, "active"))
  274. ptrips->trip_points[i].type = THERMAL_TRIP_ACTIVE;
  275. else if (!strcmp(tmp_str, "passive"))
  276. ptrips->trip_points[i].type = THERMAL_TRIP_PASSIVE;
  277. else if (!strcmp(tmp_str, "hot"))
  278. ptrips->trip_points[i].type = THERMAL_TRIP_HOT;
  279. else if (!strcmp(tmp_str, "critical"))
  280. ptrips->trip_points[i].type = THERMAL_TRIP_CRITICAL;
  281. else
  282. goto err_parse_dt;
  283. sprintf(prop_name, "trip%d-cdev-num", i);
  284. if (of_property_read_u32(np, prop_name, &tmp_data))
  285. goto err_parse_dt;
  286. if (tmp_data > COOLING_DEV_MAX)
  287. goto err_parse_dt;
  288. for (j = 0; j < tmp_data; j++) {
  289. sprintf(prop_name, "trip%d-cdev-name%d", i, j);
  290. if (of_property_read_string(np, prop_name, &tmp_str))
  291. goto err_parse_dt;
  292. if (strlen(tmp_str) >= THERMAL_NAME_LENGTH)
  293. goto err_parse_dt;
  294. strcpy(ptrips->trip_points[i].cdev_name[j], tmp_str);
  295. }
  296. }
  297. return ptrips;
  298. err_parse_dt:
  299. dev_err(&pdev->dev, "Parsing device tree data error.\n");
  300. return NULL;
  301. }
  302. #else
  303. static inline struct db8500_thsens_platform_data*
  304. db8500_thermal_parse_dt(struct platform_device *pdev)
  305. {
  306. return NULL;
  307. }
  308. #endif
  309. static int db8500_thermal_probe(struct platform_device *pdev)
  310. {
  311. struct db8500_thermal_zone *pzone = NULL;
  312. struct db8500_thsens_platform_data *ptrips = NULL;
  313. struct device_node *np = pdev->dev.of_node;
  314. int low_irq, high_irq, ret = 0;
  315. unsigned long dft_low, dft_high;
  316. if (np)
  317. ptrips = db8500_thermal_parse_dt(pdev);
  318. else
  319. ptrips = dev_get_platdata(&pdev->dev);
  320. if (!ptrips)
  321. return -EINVAL;
  322. pzone = devm_kzalloc(&pdev->dev, sizeof(*pzone), GFP_KERNEL);
  323. if (!pzone)
  324. return -ENOMEM;
  325. mutex_init(&pzone->th_lock);
  326. mutex_lock(&pzone->th_lock);
  327. pzone->mode = THERMAL_DEVICE_DISABLED;
  328. pzone->trip_tab = ptrips;
  329. INIT_WORK(&pzone->therm_work, db8500_thermal_work);
  330. low_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_LOW");
  331. if (low_irq < 0) {
  332. dev_err(&pdev->dev, "Get IRQ_HOTMON_LOW failed.\n");
  333. ret = low_irq;
  334. goto out_unlock;
  335. }
  336. ret = devm_request_threaded_irq(&pdev->dev, low_irq, NULL,
  337. prcmu_low_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
  338. "dbx500_temp_low", pzone);
  339. if (ret < 0) {
  340. dev_err(&pdev->dev, "Failed to allocate temp low irq.\n");
  341. goto out_unlock;
  342. }
  343. high_irq = platform_get_irq_byname(pdev, "IRQ_HOTMON_HIGH");
  344. if (high_irq < 0) {
  345. dev_err(&pdev->dev, "Get IRQ_HOTMON_HIGH failed.\n");
  346. ret = high_irq;
  347. goto out_unlock;
  348. }
  349. ret = devm_request_threaded_irq(&pdev->dev, high_irq, NULL,
  350. prcmu_high_irq_handler, IRQF_NO_SUSPEND | IRQF_ONESHOT,
  351. "dbx500_temp_high", pzone);
  352. if (ret < 0) {
  353. dev_err(&pdev->dev, "Failed to allocate temp high irq.\n");
  354. goto out_unlock;
  355. }
  356. pzone->therm_dev = thermal_zone_device_register("db8500_thermal_zone",
  357. ptrips->num_trips, 0, pzone, &thdev_ops, NULL, 0, 0);
  358. if (IS_ERR(pzone->therm_dev)) {
  359. dev_err(&pdev->dev, "Register thermal zone device failed.\n");
  360. ret = PTR_ERR(pzone->therm_dev);
  361. goto out_unlock;
  362. }
  363. dev_info(&pdev->dev, "Thermal zone device registered.\n");
  364. dft_low = PRCMU_DEFAULT_LOW_TEMP;
  365. dft_high = ptrips->trip_points[0].temp;
  366. db8500_thermal_update_config(pzone, 0, THERMAL_TREND_STABLE,
  367. dft_low, dft_high);
  368. platform_set_drvdata(pdev, pzone);
  369. pzone->mode = THERMAL_DEVICE_ENABLED;
  370. out_unlock:
  371. mutex_unlock(&pzone->th_lock);
  372. return ret;
  373. }
  374. static int db8500_thermal_remove(struct platform_device *pdev)
  375. {
  376. struct db8500_thermal_zone *pzone = platform_get_drvdata(pdev);
  377. thermal_zone_device_unregister(pzone->therm_dev);
  378. cancel_work_sync(&pzone->therm_work);
  379. mutex_destroy(&pzone->th_lock);
  380. return 0;
  381. }
  382. static int db8500_thermal_suspend(struct platform_device *pdev,
  383. pm_message_t state)
  384. {
  385. struct db8500_thermal_zone *pzone = platform_get_drvdata(pdev);
  386. flush_work(&pzone->therm_work);
  387. prcmu_stop_temp_sense();
  388. return 0;
  389. }
  390. static int db8500_thermal_resume(struct platform_device *pdev)
  391. {
  392. struct db8500_thermal_zone *pzone = platform_get_drvdata(pdev);
  393. struct db8500_thsens_platform_data *ptrips = pzone->trip_tab;
  394. unsigned long dft_low, dft_high;
  395. dft_low = PRCMU_DEFAULT_LOW_TEMP;
  396. dft_high = ptrips->trip_points[0].temp;
  397. db8500_thermal_update_config(pzone, 0, THERMAL_TREND_STABLE,
  398. dft_low, dft_high);
  399. return 0;
  400. }
  401. #ifdef CONFIG_OF
  402. static const struct of_device_id db8500_thermal_match[] = {
  403. { .compatible = "stericsson,db8500-thermal" },
  404. {},
  405. };
  406. #endif
  407. static struct platform_driver db8500_thermal_driver = {
  408. .driver = {
  409. .name = "db8500-thermal",
  410. .of_match_table = of_match_ptr(db8500_thermal_match),
  411. },
  412. .probe = db8500_thermal_probe,
  413. .suspend = db8500_thermal_suspend,
  414. .resume = db8500_thermal_resume,
  415. .remove = db8500_thermal_remove,
  416. };
  417. module_platform_driver(db8500_thermal_driver);
  418. MODULE_AUTHOR("Hongbo Zhang <hongbo.zhang@stericsson.com>");
  419. MODULE_DESCRIPTION("DB8500 thermal driver");
  420. MODULE_LICENSE("GPL");