exynos-bus.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 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. dev_pm_opp_put_regulators(bus->opp_token);
  134. }
  135. static void exynos_bus_passive_exit(struct device *dev)
  136. {
  137. struct exynos_bus *bus = dev_get_drvdata(dev);
  138. platform_device_unregister(bus->icc_pdev);
  139. dev_pm_opp_of_remove_table(dev);
  140. }
  141. static int exynos_bus_parent_parse_of(struct device_node *np,
  142. struct exynos_bus *bus)
  143. {
  144. struct device *dev = bus->dev;
  145. const char *supplies[] = { "vdd", NULL };
  146. int i, ret, count, size;
  147. ret = dev_pm_opp_set_regulators(dev, supplies);
  148. if (ret < 0) {
  149. dev_err(dev, "failed to set regulators %d\n", ret);
  150. return ret;
  151. }
  152. bus->opp_token = ret;
  153. /*
  154. * Get the devfreq-event devices to get the current utilization of
  155. * buses. This raw data will be used in devfreq ondemand governor.
  156. */
  157. count = devfreq_event_get_edev_count(dev, "devfreq-events");
  158. if (count < 0) {
  159. dev_err(dev, "failed to get the count of devfreq-event dev\n");
  160. ret = count;
  161. goto err_regulator;
  162. }
  163. bus->edev_count = count;
  164. size = sizeof(*bus->edev) * count;
  165. bus->edev = devm_kzalloc(dev, size, GFP_KERNEL);
  166. if (!bus->edev) {
  167. ret = -ENOMEM;
  168. goto err_regulator;
  169. }
  170. for (i = 0; i < count; i++) {
  171. bus->edev[i] = devfreq_event_get_edev_by_phandle(dev,
  172. "devfreq-events", i);
  173. if (IS_ERR(bus->edev[i])) {
  174. ret = -EPROBE_DEFER;
  175. goto err_regulator;
  176. }
  177. }
  178. /*
  179. * Optionally, Get the saturation ratio according to Exynos SoC
  180. * When measuring the utilization of each AXI bus with devfreq-event
  181. * devices, the measured real cycle might be much lower than the
  182. * total cycle of bus during sampling rate. In result, the devfreq
  183. * simple-ondemand governor might not decide to change the current
  184. * frequency due to too utilization (= real cycle/total cycle).
  185. * So, this property is used to adjust the utilization when calculating
  186. * the busy_time in exynos_bus_get_dev_status().
  187. */
  188. if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
  189. bus->ratio = DEFAULT_SATURATION_RATIO;
  190. return 0;
  191. err_regulator:
  192. dev_pm_opp_put_regulators(bus->opp_token);
  193. return ret;
  194. }
  195. static int exynos_bus_parse_of(struct exynos_bus *bus)
  196. {
  197. struct device *dev = bus->dev;
  198. struct dev_pm_opp *opp;
  199. unsigned long rate;
  200. int ret;
  201. /* Get the clock to provide each bus with source clock */
  202. bus->clk = devm_clk_get_enabled(dev, "bus");
  203. if (IS_ERR(bus->clk))
  204. return dev_err_probe(dev, PTR_ERR(bus->clk),
  205. "failed to get bus clock\n");
  206. /* Get the freq and voltage from OPP table to scale the bus freq */
  207. ret = dev_pm_opp_of_add_table(dev);
  208. if (ret < 0) {
  209. dev_err(dev, "failed to get OPP table\n");
  210. return ret;
  211. }
  212. rate = clk_get_rate(bus->clk);
  213. opp = devfreq_recommended_opp(dev, &rate, 0);
  214. if (IS_ERR(opp)) {
  215. dev_err(dev, "failed to find dev_pm_opp\n");
  216. ret = PTR_ERR(opp);
  217. goto err_opp;
  218. }
  219. bus->curr_freq = dev_pm_opp_get_freq(opp);
  220. dev_pm_opp_put(opp);
  221. return 0;
  222. err_opp:
  223. dev_pm_opp_of_remove_table(dev);
  224. return ret;
  225. }
  226. static int exynos_bus_profile_init(struct exynos_bus *bus,
  227. struct devfreq_dev_profile *profile)
  228. {
  229. struct device *dev = bus->dev;
  230. struct devfreq_simple_ondemand_data *ondemand_data;
  231. int ret;
  232. /* Initialize the struct profile and governor data for parent device */
  233. profile->polling_ms = 50;
  234. profile->target = exynos_bus_target;
  235. profile->get_dev_status = exynos_bus_get_dev_status;
  236. profile->exit = exynos_bus_exit;
  237. ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
  238. if (!ondemand_data)
  239. return -ENOMEM;
  240. ondemand_data->upthreshold = 40;
  241. ondemand_data->downdifferential = 5;
  242. /* Add devfreq device to monitor and handle the exynos bus */
  243. bus->devfreq = devm_devfreq_add_device(dev, profile,
  244. DEVFREQ_GOV_SIMPLE_ONDEMAND,
  245. ondemand_data);
  246. if (IS_ERR(bus->devfreq)) {
  247. dev_err(dev, "failed to add devfreq device\n");
  248. return PTR_ERR(bus->devfreq);
  249. }
  250. /* Register opp_notifier to catch the change of OPP */
  251. ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
  252. if (ret < 0) {
  253. dev_err(dev, "failed to register opp notifier\n");
  254. return ret;
  255. }
  256. /*
  257. * Enable devfreq-event to get raw data which is used to determine
  258. * current bus load.
  259. */
  260. ret = exynos_bus_enable_edev(bus);
  261. if (ret < 0) {
  262. dev_err(dev, "failed to enable devfreq-event devices\n");
  263. return ret;
  264. }
  265. ret = exynos_bus_set_event(bus);
  266. if (ret < 0) {
  267. dev_err(dev, "failed to set event to devfreq-event devices\n");
  268. goto err_edev;
  269. }
  270. return 0;
  271. err_edev:
  272. if (exynos_bus_disable_edev(bus))
  273. dev_warn(dev, "failed to disable the devfreq-event devices\n");
  274. return ret;
  275. }
  276. static int exynos_bus_profile_init_passive(struct exynos_bus *bus,
  277. struct devfreq_dev_profile *profile)
  278. {
  279. struct device *dev = bus->dev;
  280. struct devfreq_passive_data *passive_data;
  281. struct devfreq *parent_devfreq;
  282. /* Initialize the struct profile and governor data for passive device */
  283. profile->target = exynos_bus_target;
  284. profile->exit = exynos_bus_passive_exit;
  285. /* Get the instance of parent devfreq device */
  286. parent_devfreq = devfreq_get_devfreq_by_phandle(dev, "devfreq", 0);
  287. if (IS_ERR(parent_devfreq))
  288. return -EPROBE_DEFER;
  289. passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
  290. if (!passive_data)
  291. return -ENOMEM;
  292. passive_data->parent = parent_devfreq;
  293. /* Add devfreq device for exynos bus with passive governor */
  294. bus->devfreq = devm_devfreq_add_device(dev, profile, DEVFREQ_GOV_PASSIVE,
  295. passive_data);
  296. if (IS_ERR(bus->devfreq)) {
  297. dev_err(dev,
  298. "failed to add devfreq dev with passive governor\n");
  299. return PTR_ERR(bus->devfreq);
  300. }
  301. return 0;
  302. }
  303. static int exynos_bus_probe(struct platform_device *pdev)
  304. {
  305. struct device *dev = &pdev->dev;
  306. struct device_node *np = dev->of_node, *node;
  307. struct devfreq_dev_profile *profile;
  308. struct exynos_bus *bus;
  309. int ret, max_state;
  310. unsigned long min_freq, max_freq;
  311. bool passive = false;
  312. if (!np) {
  313. dev_err(dev, "failed to find devicetree node\n");
  314. return -EINVAL;
  315. }
  316. bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
  317. if (!bus)
  318. return -ENOMEM;
  319. mutex_init(&bus->lock);
  320. bus->dev = &pdev->dev;
  321. platform_set_drvdata(pdev, bus);
  322. profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
  323. if (!profile)
  324. return -ENOMEM;
  325. node = of_parse_phandle(dev->of_node, "devfreq", 0);
  326. if (node) {
  327. of_node_put(node);
  328. passive = true;
  329. } else {
  330. ret = exynos_bus_parent_parse_of(np, bus);
  331. if (ret < 0)
  332. return ret;
  333. }
  334. /* Parse the device-tree to get the resource information */
  335. ret = exynos_bus_parse_of(bus);
  336. if (ret < 0)
  337. goto err_reg;
  338. if (passive)
  339. ret = exynos_bus_profile_init_passive(bus, profile);
  340. else
  341. ret = exynos_bus_profile_init(bus, profile);
  342. if (ret < 0)
  343. goto err;
  344. /* Create child platform device for the interconnect provider */
  345. if (of_property_present(dev->of_node, "#interconnect-cells")) {
  346. bus->icc_pdev = platform_device_register_data(
  347. dev, "exynos-generic-icc",
  348. PLATFORM_DEVID_AUTO, NULL, 0);
  349. if (IS_ERR(bus->icc_pdev)) {
  350. ret = PTR_ERR(bus->icc_pdev);
  351. goto err;
  352. }
  353. }
  354. max_state = bus->devfreq->max_state;
  355. min_freq = (bus->devfreq->freq_table[0] / 1000);
  356. max_freq = (bus->devfreq->freq_table[max_state - 1] / 1000);
  357. pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
  358. dev_name(dev), min_freq, max_freq);
  359. return 0;
  360. err:
  361. dev_pm_opp_of_remove_table(dev);
  362. err_reg:
  363. dev_pm_opp_put_regulators(bus->opp_token);
  364. return ret;
  365. }
  366. static void exynos_bus_shutdown(struct platform_device *pdev)
  367. {
  368. struct exynos_bus *bus = dev_get_drvdata(&pdev->dev);
  369. devfreq_suspend_device(bus->devfreq);
  370. }
  371. static int exynos_bus_resume(struct device *dev)
  372. {
  373. struct exynos_bus *bus = dev_get_drvdata(dev);
  374. int ret;
  375. ret = exynos_bus_enable_edev(bus);
  376. if (ret < 0) {
  377. dev_err(dev, "failed to enable the devfreq-event devices\n");
  378. return ret;
  379. }
  380. return 0;
  381. }
  382. static int exynos_bus_suspend(struct device *dev)
  383. {
  384. struct exynos_bus *bus = dev_get_drvdata(dev);
  385. int ret;
  386. ret = exynos_bus_disable_edev(bus);
  387. if (ret < 0) {
  388. dev_err(dev, "failed to disable the devfreq-event devices\n");
  389. return ret;
  390. }
  391. return 0;
  392. }
  393. static DEFINE_SIMPLE_DEV_PM_OPS(exynos_bus_pm,
  394. exynos_bus_suspend, exynos_bus_resume);
  395. static const struct of_device_id exynos_bus_of_match[] = {
  396. { .compatible = "samsung,exynos-bus", },
  397. { /* sentinel */ },
  398. };
  399. MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
  400. static struct platform_driver exynos_bus_platdrv = {
  401. .probe = exynos_bus_probe,
  402. .shutdown = exynos_bus_shutdown,
  403. .driver = {
  404. .name = "exynos-bus",
  405. .pm = pm_sleep_ptr(&exynos_bus_pm),
  406. .of_match_table = exynos_bus_of_match,
  407. },
  408. };
  409. module_platform_driver(exynos_bus_platdrv);
  410. MODULE_SOFTDEP("pre: exynos_ppmu");
  411. MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
  412. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  413. MODULE_LICENSE("GPL v2");