exynos-bus.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 device_node *np,
  196. struct exynos_bus *bus)
  197. {
  198. struct device *dev = bus->dev;
  199. struct dev_pm_opp *opp;
  200. unsigned long rate;
  201. int ret;
  202. /* Get the clock to provide each bus with source clock */
  203. bus->clk = devm_clk_get_enabled(dev, "bus");
  204. if (IS_ERR(bus->clk))
  205. return dev_err_probe(dev, PTR_ERR(bus->clk),
  206. "failed to get bus clock\n");
  207. /* Get the freq and voltage from OPP table to scale the bus freq */
  208. ret = dev_pm_opp_of_add_table(dev);
  209. if (ret < 0) {
  210. dev_err(dev, "failed to get OPP table\n");
  211. return ret;
  212. }
  213. rate = clk_get_rate(bus->clk);
  214. opp = devfreq_recommended_opp(dev, &rate, 0);
  215. if (IS_ERR(opp)) {
  216. dev_err(dev, "failed to find dev_pm_opp\n");
  217. ret = PTR_ERR(opp);
  218. goto err_opp;
  219. }
  220. bus->curr_freq = dev_pm_opp_get_freq(opp);
  221. dev_pm_opp_put(opp);
  222. return 0;
  223. err_opp:
  224. dev_pm_opp_of_remove_table(dev);
  225. return ret;
  226. }
  227. static int exynos_bus_profile_init(struct exynos_bus *bus,
  228. struct devfreq_dev_profile *profile)
  229. {
  230. struct device *dev = bus->dev;
  231. struct devfreq_simple_ondemand_data *ondemand_data;
  232. int ret;
  233. /* Initialize the struct profile and governor data for parent device */
  234. profile->polling_ms = 50;
  235. profile->target = exynos_bus_target;
  236. profile->get_dev_status = exynos_bus_get_dev_status;
  237. profile->exit = exynos_bus_exit;
  238. ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
  239. if (!ondemand_data)
  240. return -ENOMEM;
  241. ondemand_data->upthreshold = 40;
  242. ondemand_data->downdifferential = 5;
  243. /* Add devfreq device to monitor and handle the exynos bus */
  244. bus->devfreq = devm_devfreq_add_device(dev, profile,
  245. DEVFREQ_GOV_SIMPLE_ONDEMAND,
  246. ondemand_data);
  247. if (IS_ERR(bus->devfreq)) {
  248. dev_err(dev, "failed to add devfreq device\n");
  249. return PTR_ERR(bus->devfreq);
  250. }
  251. /* Register opp_notifier to catch the change of OPP */
  252. ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
  253. if (ret < 0) {
  254. dev_err(dev, "failed to register opp notifier\n");
  255. return ret;
  256. }
  257. /*
  258. * Enable devfreq-event to get raw data which is used to determine
  259. * current bus load.
  260. */
  261. ret = exynos_bus_enable_edev(bus);
  262. if (ret < 0) {
  263. dev_err(dev, "failed to enable devfreq-event devices\n");
  264. return ret;
  265. }
  266. ret = exynos_bus_set_event(bus);
  267. if (ret < 0) {
  268. dev_err(dev, "failed to set event to devfreq-event devices\n");
  269. goto err_edev;
  270. }
  271. return 0;
  272. err_edev:
  273. if (exynos_bus_disable_edev(bus))
  274. dev_warn(dev, "failed to disable the devfreq-event devices\n");
  275. return ret;
  276. }
  277. static int exynos_bus_profile_init_passive(struct exynos_bus *bus,
  278. struct devfreq_dev_profile *profile)
  279. {
  280. struct device *dev = bus->dev;
  281. struct devfreq_passive_data *passive_data;
  282. struct devfreq *parent_devfreq;
  283. /* Initialize the struct profile and governor data for passive device */
  284. profile->target = exynos_bus_target;
  285. profile->exit = exynos_bus_passive_exit;
  286. /* Get the instance of parent devfreq device */
  287. parent_devfreq = devfreq_get_devfreq_by_phandle(dev, "devfreq", 0);
  288. if (IS_ERR(parent_devfreq))
  289. return -EPROBE_DEFER;
  290. passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
  291. if (!passive_data)
  292. return -ENOMEM;
  293. passive_data->parent = parent_devfreq;
  294. /* Add devfreq device for exynos bus with passive governor */
  295. bus->devfreq = devm_devfreq_add_device(dev, profile, DEVFREQ_GOV_PASSIVE,
  296. passive_data);
  297. if (IS_ERR(bus->devfreq)) {
  298. dev_err(dev,
  299. "failed to add devfreq dev with passive governor\n");
  300. return PTR_ERR(bus->devfreq);
  301. }
  302. return 0;
  303. }
  304. static int exynos_bus_probe(struct platform_device *pdev)
  305. {
  306. struct device *dev = &pdev->dev;
  307. struct device_node *np = dev->of_node, *node;
  308. struct devfreq_dev_profile *profile;
  309. struct exynos_bus *bus;
  310. int ret, max_state;
  311. unsigned long min_freq, max_freq;
  312. bool passive = false;
  313. if (!np) {
  314. dev_err(dev, "failed to find devicetree node\n");
  315. return -EINVAL;
  316. }
  317. bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
  318. if (!bus)
  319. return -ENOMEM;
  320. mutex_init(&bus->lock);
  321. bus->dev = &pdev->dev;
  322. platform_set_drvdata(pdev, bus);
  323. profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
  324. if (!profile)
  325. return -ENOMEM;
  326. node = of_parse_phandle(dev->of_node, "devfreq", 0);
  327. if (node) {
  328. of_node_put(node);
  329. passive = true;
  330. } else {
  331. ret = exynos_bus_parent_parse_of(np, bus);
  332. if (ret < 0)
  333. return ret;
  334. }
  335. /* Parse the device-tree to get the resource information */
  336. ret = exynos_bus_parse_of(np, bus);
  337. if (ret < 0)
  338. goto err_reg;
  339. if (passive)
  340. ret = exynos_bus_profile_init_passive(bus, profile);
  341. else
  342. ret = exynos_bus_profile_init(bus, profile);
  343. if (ret < 0)
  344. goto err;
  345. /* Create child platform device for the interconnect provider */
  346. if (of_property_present(dev->of_node, "#interconnect-cells")) {
  347. bus->icc_pdev = platform_device_register_data(
  348. dev, "exynos-generic-icc",
  349. PLATFORM_DEVID_AUTO, NULL, 0);
  350. if (IS_ERR(bus->icc_pdev)) {
  351. ret = PTR_ERR(bus->icc_pdev);
  352. goto err;
  353. }
  354. }
  355. max_state = bus->devfreq->max_state;
  356. min_freq = (bus->devfreq->freq_table[0] / 1000);
  357. max_freq = (bus->devfreq->freq_table[max_state - 1] / 1000);
  358. pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
  359. dev_name(dev), min_freq, max_freq);
  360. return 0;
  361. err:
  362. dev_pm_opp_of_remove_table(dev);
  363. err_reg:
  364. dev_pm_opp_put_regulators(bus->opp_token);
  365. return ret;
  366. }
  367. static void exynos_bus_shutdown(struct platform_device *pdev)
  368. {
  369. struct exynos_bus *bus = dev_get_drvdata(&pdev->dev);
  370. devfreq_suspend_device(bus->devfreq);
  371. }
  372. static int exynos_bus_resume(struct device *dev)
  373. {
  374. struct exynos_bus *bus = dev_get_drvdata(dev);
  375. int ret;
  376. ret = exynos_bus_enable_edev(bus);
  377. if (ret < 0) {
  378. dev_err(dev, "failed to enable the devfreq-event devices\n");
  379. return ret;
  380. }
  381. return 0;
  382. }
  383. static int exynos_bus_suspend(struct device *dev)
  384. {
  385. struct exynos_bus *bus = dev_get_drvdata(dev);
  386. int ret;
  387. ret = exynos_bus_disable_edev(bus);
  388. if (ret < 0) {
  389. dev_err(dev, "failed to disable the devfreq-event devices\n");
  390. return ret;
  391. }
  392. return 0;
  393. }
  394. static DEFINE_SIMPLE_DEV_PM_OPS(exynos_bus_pm,
  395. exynos_bus_suspend, exynos_bus_resume);
  396. static const struct of_device_id exynos_bus_of_match[] = {
  397. { .compatible = "samsung,exynos-bus", },
  398. { /* sentinel */ },
  399. };
  400. MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
  401. static struct platform_driver exynos_bus_platdrv = {
  402. .probe = exynos_bus_probe,
  403. .shutdown = exynos_bus_shutdown,
  404. .driver = {
  405. .name = "exynos-bus",
  406. .pm = pm_sleep_ptr(&exynos_bus_pm),
  407. .of_match_table = exynos_bus_of_match,
  408. },
  409. };
  410. module_platform_driver(exynos_bus_platdrv);
  411. MODULE_SOFTDEP("pre: exynos_ppmu");
  412. MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
  413. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  414. MODULE_LICENSE("GPL v2");