exynos-bus.c 12 KB

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