exynos-bus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Generic Exynos Bus frequency driver with DEVFREQ Framework
  3. *
  4. * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  5. * Author : Chanwoo Choi <cw00.choi@samsung.com>
  6. *
  7. * This driver support Exynos Bus frequency feature by using
  8. * DEVFREQ framework and is based on drivers/devfreq/exynos/exynos4_bus.c.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/devfreq.h>
  16. #include <linux/devfreq-event.h>
  17. #include <linux/device.h>
  18. #include <linux/export.h>
  19. #include <linux/module.h>
  20. #include <linux/of_device.h>
  21. #include <linux/pm_opp.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/slab.h>
  25. #define DEFAULT_SATURATION_RATIO 40
  26. #define DEFAULT_VOLTAGE_TOLERANCE 2
  27. struct exynos_bus {
  28. struct device *dev;
  29. struct devfreq *devfreq;
  30. struct devfreq_event_dev **edev;
  31. unsigned int edev_count;
  32. struct mutex lock;
  33. struct dev_pm_opp *curr_opp;
  34. struct regulator *regulator;
  35. struct clk *clk;
  36. unsigned int voltage_tolerance;
  37. unsigned int ratio;
  38. };
  39. /*
  40. * Control the devfreq-event device to get the current state of bus
  41. */
  42. #define exynos_bus_ops_edev(ops) \
  43. static int exynos_bus_##ops(struct exynos_bus *bus) \
  44. { \
  45. int i, ret; \
  46. \
  47. for (i = 0; i < bus->edev_count; i++) { \
  48. if (!bus->edev[i]) \
  49. continue; \
  50. ret = devfreq_event_##ops(bus->edev[i]); \
  51. if (ret < 0) \
  52. return ret; \
  53. } \
  54. \
  55. return 0; \
  56. }
  57. exynos_bus_ops_edev(enable_edev);
  58. exynos_bus_ops_edev(disable_edev);
  59. exynos_bus_ops_edev(set_event);
  60. static int exynos_bus_get_event(struct exynos_bus *bus,
  61. struct devfreq_event_data *edata)
  62. {
  63. struct devfreq_event_data event_data;
  64. unsigned long load_count = 0, total_count = 0;
  65. int i, ret = 0;
  66. for (i = 0; i < bus->edev_count; i++) {
  67. if (!bus->edev[i])
  68. continue;
  69. ret = devfreq_event_get_event(bus->edev[i], &event_data);
  70. if (ret < 0)
  71. return ret;
  72. if (i == 0 || event_data.load_count > load_count) {
  73. load_count = event_data.load_count;
  74. total_count = event_data.total_count;
  75. }
  76. }
  77. edata->load_count = load_count;
  78. edata->total_count = total_count;
  79. return ret;
  80. }
  81. /*
  82. * Must necessary function for devfreq governor
  83. */
  84. static int exynos_bus_target(struct device *dev, unsigned long *freq, u32 flags)
  85. {
  86. struct exynos_bus *bus = dev_get_drvdata(dev);
  87. struct dev_pm_opp *new_opp;
  88. unsigned long old_freq, new_freq, old_volt, new_volt, tol;
  89. int ret = 0;
  90. /* Get new opp-bus instance according to new bus clock */
  91. rcu_read_lock();
  92. new_opp = devfreq_recommended_opp(dev, freq, flags);
  93. if (IS_ERR(new_opp)) {
  94. dev_err(dev, "failed to get recommended opp instance\n");
  95. rcu_read_unlock();
  96. return PTR_ERR(new_opp);
  97. }
  98. new_freq = dev_pm_opp_get_freq(new_opp);
  99. new_volt = dev_pm_opp_get_voltage(new_opp);
  100. old_freq = dev_pm_opp_get_freq(bus->curr_opp);
  101. old_volt = dev_pm_opp_get_voltage(bus->curr_opp);
  102. rcu_read_unlock();
  103. if (old_freq == new_freq)
  104. return 0;
  105. tol = new_volt * bus->voltage_tolerance / 100;
  106. /* Change voltage and frequency according to new OPP level */
  107. mutex_lock(&bus->lock);
  108. if (old_freq < new_freq) {
  109. ret = regulator_set_voltage_tol(bus->regulator, new_volt, tol);
  110. if (ret < 0) {
  111. dev_err(bus->dev, "failed to set voltage\n");
  112. goto out;
  113. }
  114. }
  115. ret = clk_set_rate(bus->clk, new_freq);
  116. if (ret < 0) {
  117. dev_err(dev, "failed to change clock of bus\n");
  118. clk_set_rate(bus->clk, old_freq);
  119. goto out;
  120. }
  121. if (old_freq > new_freq) {
  122. ret = regulator_set_voltage_tol(bus->regulator, new_volt, tol);
  123. if (ret < 0) {
  124. dev_err(bus->dev, "failed to set voltage\n");
  125. goto out;
  126. }
  127. }
  128. bus->curr_opp = new_opp;
  129. dev_dbg(dev, "Set the frequency of bus (%lukHz -> %lukHz)\n",
  130. old_freq/1000, new_freq/1000);
  131. out:
  132. mutex_unlock(&bus->lock);
  133. return ret;
  134. }
  135. static int exynos_bus_get_dev_status(struct device *dev,
  136. struct devfreq_dev_status *stat)
  137. {
  138. struct exynos_bus *bus = dev_get_drvdata(dev);
  139. struct devfreq_event_data edata;
  140. int ret;
  141. rcu_read_lock();
  142. stat->current_frequency = dev_pm_opp_get_freq(bus->curr_opp);
  143. rcu_read_unlock();
  144. ret = exynos_bus_get_event(bus, &edata);
  145. if (ret < 0) {
  146. stat->total_time = stat->busy_time = 0;
  147. goto err;
  148. }
  149. stat->busy_time = (edata.load_count * 100) / bus->ratio;
  150. stat->total_time = edata.total_count;
  151. dev_dbg(dev, "Usage of devfreq-event : %lu/%lu\n", stat->busy_time,
  152. stat->total_time);
  153. err:
  154. ret = exynos_bus_set_event(bus);
  155. if (ret < 0) {
  156. dev_err(dev, "failed to set event to devfreq-event devices\n");
  157. return ret;
  158. }
  159. return ret;
  160. }
  161. static void exynos_bus_exit(struct device *dev)
  162. {
  163. struct exynos_bus *bus = dev_get_drvdata(dev);
  164. int ret;
  165. ret = exynos_bus_disable_edev(bus);
  166. if (ret < 0)
  167. dev_warn(dev, "failed to disable the devfreq-event devices\n");
  168. if (bus->regulator)
  169. regulator_disable(bus->regulator);
  170. dev_pm_opp_of_remove_table(dev);
  171. }
  172. static int exynos_bus_parse_of(struct device_node *np,
  173. struct exynos_bus *bus)
  174. {
  175. struct device *dev = bus->dev;
  176. unsigned long rate;
  177. int i, ret, count, size;
  178. /* Get the clock to provide each bus with source clock */
  179. bus->clk = devm_clk_get(dev, "bus");
  180. if (IS_ERR(bus->clk)) {
  181. dev_err(dev, "failed to get bus clock\n");
  182. return PTR_ERR(bus->clk);
  183. }
  184. ret = clk_prepare_enable(bus->clk);
  185. if (ret < 0) {
  186. dev_err(dev, "failed to get enable clock\n");
  187. return ret;
  188. }
  189. /* Get the freq/voltage OPP table to scale the bus frequency */
  190. rcu_read_lock();
  191. ret = dev_pm_opp_of_add_table(dev);
  192. if (ret < 0) {
  193. dev_err(dev, "failed to get OPP table\n");
  194. rcu_read_unlock();
  195. goto err_clk;
  196. }
  197. rate = clk_get_rate(bus->clk);
  198. bus->curr_opp = dev_pm_opp_find_freq_ceil(dev, &rate);
  199. if (IS_ERR(bus->curr_opp)) {
  200. dev_err(dev, "failed to find dev_pm_opp\n");
  201. rcu_read_unlock();
  202. ret = PTR_ERR(bus->curr_opp);
  203. goto err_opp;
  204. }
  205. rcu_read_unlock();
  206. /* Get the regulator to provide each bus with the power */
  207. bus->regulator = devm_regulator_get(dev, "vdd");
  208. if (IS_ERR(bus->regulator)) {
  209. dev_err(dev, "failed to get VDD regulator\n");
  210. ret = PTR_ERR(bus->regulator);
  211. goto err_opp;
  212. }
  213. ret = regulator_enable(bus->regulator);
  214. if (ret < 0) {
  215. dev_err(dev, "failed to enable VDD regulator\n");
  216. goto err_opp;
  217. }
  218. /*
  219. * Get the devfreq-event devices to get the current utilization of
  220. * buses. This raw data will be used in devfreq ondemand governor.
  221. */
  222. count = devfreq_event_get_edev_count(dev);
  223. if (count < 0) {
  224. dev_err(dev, "failed to get the count of devfreq-event dev\n");
  225. ret = count;
  226. goto err_regulator;
  227. }
  228. bus->edev_count = count;
  229. size = sizeof(*bus->edev) * count;
  230. bus->edev = devm_kzalloc(dev, size, GFP_KERNEL);
  231. if (!bus->edev) {
  232. ret = -ENOMEM;
  233. goto err_regulator;
  234. }
  235. for (i = 0; i < count; i++) {
  236. bus->edev[i] = devfreq_event_get_edev_by_phandle(dev, i);
  237. if (IS_ERR(bus->edev[i])) {
  238. ret = -EPROBE_DEFER;
  239. goto err_regulator;
  240. }
  241. }
  242. /*
  243. * Optionally, Get the saturation ratio according to Exynos SoC
  244. * When measuring the utilization of each AXI bus with devfreq-event
  245. * devices, the measured real cycle might be much lower than the
  246. * total cycle of bus during sampling rate. In result, the devfreq
  247. * simple-ondemand governor might not decide to change the current
  248. * frequency due to too utilization (= real cycle/total cycle).
  249. * So, this property is used to adjust the utilization when calculating
  250. * the busy_time in exynos_bus_get_dev_status().
  251. */
  252. if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
  253. bus->ratio = DEFAULT_SATURATION_RATIO;
  254. if (of_property_read_u32(np, "exynos,voltage-tolerance",
  255. &bus->voltage_tolerance))
  256. bus->voltage_tolerance = DEFAULT_VOLTAGE_TOLERANCE;
  257. return 0;
  258. err_regulator:
  259. regulator_disable(bus->regulator);
  260. err_opp:
  261. dev_pm_opp_of_remove_table(dev);
  262. err_clk:
  263. clk_disable_unprepare(bus->clk);
  264. return ret;
  265. }
  266. static int exynos_bus_probe(struct platform_device *pdev)
  267. {
  268. struct device *dev = &pdev->dev;
  269. struct device_node *np = dev->of_node;
  270. struct devfreq_dev_profile *profile;
  271. struct devfreq_simple_ondemand_data *ondemand_data;
  272. struct exynos_bus *bus;
  273. int ret;
  274. if (!np) {
  275. dev_err(dev, "failed to find devicetree node\n");
  276. return -EINVAL;
  277. }
  278. bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
  279. if (!bus)
  280. return -ENOMEM;
  281. mutex_init(&bus->lock);
  282. bus->dev = &pdev->dev;
  283. platform_set_drvdata(pdev, bus);
  284. /* Parse the device-tree to get the resource information */
  285. ret = exynos_bus_parse_of(np, bus);
  286. if (ret < 0)
  287. return ret;
  288. /* Initalize the struct profile and governor data */
  289. profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
  290. if (!profile)
  291. return -ENOMEM;
  292. profile->polling_ms = 50;
  293. profile->target = exynos_bus_target;
  294. profile->get_dev_status = exynos_bus_get_dev_status;
  295. profile->exit = exynos_bus_exit;
  296. ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
  297. if (!ondemand_data)
  298. return -ENOMEM;
  299. ondemand_data->upthreshold = 40;
  300. ondemand_data->downdifferential = 5;
  301. /* Add devfreq device to monitor and handle the exynos bus */
  302. bus->devfreq = devm_devfreq_add_device(dev, profile, "simple_ondemand",
  303. ondemand_data);
  304. if (IS_ERR(bus->devfreq)) {
  305. dev_err(dev, "failed to add devfreq device\n");
  306. return PTR_ERR(bus->devfreq);
  307. }
  308. /* Register opp_notifier to catch the change of OPP */
  309. ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
  310. if (ret < 0) {
  311. dev_err(dev, "failed to register opp notifier\n");
  312. return ret;
  313. }
  314. /*
  315. * Enable devfreq-event to get raw data which is used to determine
  316. * current bus load.
  317. */
  318. ret = exynos_bus_enable_edev(bus);
  319. if (ret < 0) {
  320. dev_err(dev, "failed to enable devfreq-event devices\n");
  321. return ret;
  322. }
  323. ret = exynos_bus_set_event(bus);
  324. if (ret < 0) {
  325. dev_err(dev, "failed to set event to devfreq-event devices\n");
  326. return ret;
  327. }
  328. return 0;
  329. }
  330. #ifdef CONFIG_PM_SLEEP
  331. static int exynos_bus_resume(struct device *dev)
  332. {
  333. struct exynos_bus *bus = dev_get_drvdata(dev);
  334. int ret;
  335. ret = exynos_bus_enable_edev(bus);
  336. if (ret < 0) {
  337. dev_err(dev, "failed to enable the devfreq-event devices\n");
  338. return ret;
  339. }
  340. return 0;
  341. }
  342. static int exynos_bus_suspend(struct device *dev)
  343. {
  344. struct exynos_bus *bus = dev_get_drvdata(dev);
  345. int ret;
  346. ret = exynos_bus_disable_edev(bus);
  347. if (ret < 0) {
  348. dev_err(dev, "failed to disable the devfreq-event devices\n");
  349. return ret;
  350. }
  351. return 0;
  352. }
  353. #endif
  354. static const struct dev_pm_ops exynos_bus_pm = {
  355. SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend, exynos_bus_resume)
  356. };
  357. static const struct of_device_id exynos_bus_of_match[] = {
  358. { .compatible = "samsung,exynos-bus", },
  359. { /* sentinel */ },
  360. };
  361. MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
  362. static struct platform_driver exynos_bus_platdrv = {
  363. .probe = exynos_bus_probe,
  364. .driver = {
  365. .name = "exynos-bus",
  366. .pm = &exynos_bus_pm,
  367. .of_match_table = of_match_ptr(exynos_bus_of_match),
  368. },
  369. };
  370. module_platform_driver(exynos_bus_platdrv);
  371. MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
  372. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  373. MODULE_LICENSE("GPL v2");