exynos-bus.c 13 KB

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