exynos-bus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Generic Exynos Bus frequency driver with DEVFREQ Framework
  3. *
  4. * Copyright (c) 2016 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 simple-ondemand 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. clk_disable_unprepare(bus->clk);
  172. }
  173. /*
  174. * Must necessary function for devfreq passive governor
  175. */
  176. static int exynos_bus_passive_target(struct device *dev, unsigned long *freq,
  177. u32 flags)
  178. {
  179. struct exynos_bus *bus = dev_get_drvdata(dev);
  180. struct dev_pm_opp *new_opp;
  181. unsigned long old_freq, new_freq;
  182. int ret = 0;
  183. /* Get new opp-bus instance according to new bus clock */
  184. rcu_read_lock();
  185. new_opp = devfreq_recommended_opp(dev, freq, flags);
  186. if (IS_ERR(new_opp)) {
  187. dev_err(dev, "failed to get recommended opp instance\n");
  188. rcu_read_unlock();
  189. return PTR_ERR(new_opp);
  190. }
  191. new_freq = dev_pm_opp_get_freq(new_opp);
  192. old_freq = dev_pm_opp_get_freq(bus->curr_opp);
  193. rcu_read_unlock();
  194. if (old_freq == new_freq)
  195. return 0;
  196. /* Change the frequency according to new OPP level */
  197. mutex_lock(&bus->lock);
  198. ret = clk_set_rate(bus->clk, new_freq);
  199. if (ret < 0) {
  200. dev_err(dev, "failed to set the clock of bus\n");
  201. goto out;
  202. }
  203. *freq = new_freq;
  204. bus->curr_opp = new_opp;
  205. dev_dbg(dev, "Set the frequency of bus (%lukHz -> %lukHz)\n",
  206. old_freq/1000, new_freq/1000);
  207. out:
  208. mutex_unlock(&bus->lock);
  209. return ret;
  210. }
  211. static void exynos_bus_passive_exit(struct device *dev)
  212. {
  213. struct exynos_bus *bus = dev_get_drvdata(dev);
  214. dev_pm_opp_of_remove_table(dev);
  215. clk_disable_unprepare(bus->clk);
  216. }
  217. static int exynos_bus_parent_parse_of(struct device_node *np,
  218. struct exynos_bus *bus)
  219. {
  220. struct device *dev = bus->dev;
  221. int i, ret, count, size;
  222. /* Get the regulator to provide each bus with the power */
  223. bus->regulator = devm_regulator_get(dev, "vdd");
  224. if (IS_ERR(bus->regulator)) {
  225. dev_err(dev, "failed to get VDD regulator\n");
  226. return PTR_ERR(bus->regulator);
  227. }
  228. ret = regulator_enable(bus->regulator);
  229. if (ret < 0) {
  230. dev_err(dev, "failed to enable VDD regulator\n");
  231. return ret;
  232. }
  233. /*
  234. * Get the devfreq-event devices to get the current utilization of
  235. * buses. This raw data will be used in devfreq ondemand governor.
  236. */
  237. count = devfreq_event_get_edev_count(dev);
  238. if (count < 0) {
  239. dev_err(dev, "failed to get the count of devfreq-event dev\n");
  240. ret = count;
  241. goto err_regulator;
  242. }
  243. bus->edev_count = count;
  244. size = sizeof(*bus->edev) * count;
  245. bus->edev = devm_kzalloc(dev, size, GFP_KERNEL);
  246. if (!bus->edev) {
  247. ret = -ENOMEM;
  248. goto err_regulator;
  249. }
  250. for (i = 0; i < count; i++) {
  251. bus->edev[i] = devfreq_event_get_edev_by_phandle(dev, i);
  252. if (IS_ERR(bus->edev[i])) {
  253. ret = -EPROBE_DEFER;
  254. goto err_regulator;
  255. }
  256. }
  257. /*
  258. * Optionally, Get the saturation ratio according to Exynos SoC
  259. * When measuring the utilization of each AXI bus with devfreq-event
  260. * devices, the measured real cycle might be much lower than the
  261. * total cycle of bus during sampling rate. In result, the devfreq
  262. * simple-ondemand governor might not decide to change the current
  263. * frequency due to too utilization (= real cycle/total cycle).
  264. * So, this property is used to adjust the utilization when calculating
  265. * the busy_time in exynos_bus_get_dev_status().
  266. */
  267. if (of_property_read_u32(np, "exynos,saturation-ratio", &bus->ratio))
  268. bus->ratio = DEFAULT_SATURATION_RATIO;
  269. if (of_property_read_u32(np, "exynos,voltage-tolerance",
  270. &bus->voltage_tolerance))
  271. bus->voltage_tolerance = DEFAULT_VOLTAGE_TOLERANCE;
  272. return 0;
  273. err_regulator:
  274. regulator_disable(bus->regulator);
  275. return ret;
  276. }
  277. static int exynos_bus_parse_of(struct device_node *np,
  278. struct exynos_bus *bus)
  279. {
  280. struct device *dev = bus->dev;
  281. unsigned long rate;
  282. int ret;
  283. /* Get the clock to provide each bus with source clock */
  284. bus->clk = devm_clk_get(dev, "bus");
  285. if (IS_ERR(bus->clk)) {
  286. dev_err(dev, "failed to get bus clock\n");
  287. return PTR_ERR(bus->clk);
  288. }
  289. ret = clk_prepare_enable(bus->clk);
  290. if (ret < 0) {
  291. dev_err(dev, "failed to get enable clock\n");
  292. return ret;
  293. }
  294. /* Get the freq and voltage from OPP table to scale the bus freq */
  295. rcu_read_lock();
  296. ret = dev_pm_opp_of_add_table(dev);
  297. if (ret < 0) {
  298. dev_err(dev, "failed to get OPP table\n");
  299. rcu_read_unlock();
  300. goto err_clk;
  301. }
  302. rate = clk_get_rate(bus->clk);
  303. bus->curr_opp = devfreq_recommended_opp(dev, &rate, 0);
  304. if (IS_ERR(bus->curr_opp)) {
  305. dev_err(dev, "failed to find dev_pm_opp\n");
  306. rcu_read_unlock();
  307. ret = PTR_ERR(bus->curr_opp);
  308. goto err_opp;
  309. }
  310. rcu_read_unlock();
  311. return 0;
  312. err_opp:
  313. dev_pm_opp_of_remove_table(dev);
  314. err_clk:
  315. clk_disable_unprepare(bus->clk);
  316. return ret;
  317. }
  318. static int exynos_bus_probe(struct platform_device *pdev)
  319. {
  320. struct device *dev = &pdev->dev;
  321. struct device_node *np = dev->of_node, *node;
  322. struct devfreq_dev_profile *profile;
  323. struct devfreq_simple_ondemand_data *ondemand_data;
  324. struct devfreq_passive_data *passive_data;
  325. struct devfreq *parent_devfreq;
  326. struct exynos_bus *bus;
  327. int ret, max_state;
  328. unsigned long min_freq, max_freq;
  329. if (!np) {
  330. dev_err(dev, "failed to find devicetree node\n");
  331. return -EINVAL;
  332. }
  333. bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
  334. if (!bus)
  335. return -ENOMEM;
  336. mutex_init(&bus->lock);
  337. bus->dev = &pdev->dev;
  338. platform_set_drvdata(pdev, bus);
  339. /* Parse the device-tree to get the resource information */
  340. ret = exynos_bus_parse_of(np, bus);
  341. if (ret < 0)
  342. return ret;
  343. profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
  344. if (!profile) {
  345. ret = -ENOMEM;
  346. goto err;
  347. }
  348. node = of_parse_phandle(dev->of_node, "devfreq", 0);
  349. if (node) {
  350. of_node_put(node);
  351. goto passive;
  352. } else {
  353. ret = exynos_bus_parent_parse_of(np, bus);
  354. }
  355. if (ret < 0)
  356. goto err;
  357. /* Initialize the struct profile and governor data for parent device */
  358. profile->polling_ms = 50;
  359. profile->target = exynos_bus_target;
  360. profile->get_dev_status = exynos_bus_get_dev_status;
  361. profile->exit = exynos_bus_exit;
  362. ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
  363. if (!ondemand_data) {
  364. ret = -ENOMEM;
  365. goto err;
  366. }
  367. ondemand_data->upthreshold = 40;
  368. ondemand_data->downdifferential = 5;
  369. /* Add devfreq device to monitor and handle the exynos bus */
  370. bus->devfreq = devm_devfreq_add_device(dev, profile, "simple_ondemand",
  371. ondemand_data);
  372. if (IS_ERR(bus->devfreq)) {
  373. dev_err(dev, "failed to add devfreq device\n");
  374. ret = PTR_ERR(bus->devfreq);
  375. goto err;
  376. }
  377. /* Register opp_notifier to catch the change of OPP */
  378. ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
  379. if (ret < 0) {
  380. dev_err(dev, "failed to register opp notifier\n");
  381. goto err;
  382. }
  383. /*
  384. * Enable devfreq-event to get raw data which is used to determine
  385. * current bus load.
  386. */
  387. ret = exynos_bus_enable_edev(bus);
  388. if (ret < 0) {
  389. dev_err(dev, "failed to enable devfreq-event devices\n");
  390. goto err;
  391. }
  392. ret = exynos_bus_set_event(bus);
  393. if (ret < 0) {
  394. dev_err(dev, "failed to set event to devfreq-event devices\n");
  395. goto err;
  396. }
  397. goto out;
  398. passive:
  399. /* Initialize the struct profile and governor data for passive device */
  400. profile->target = exynos_bus_passive_target;
  401. profile->exit = exynos_bus_passive_exit;
  402. /* Get the instance of parent devfreq device */
  403. parent_devfreq = devfreq_get_devfreq_by_phandle(dev, 0);
  404. if (IS_ERR(parent_devfreq)) {
  405. ret = -EPROBE_DEFER;
  406. goto err;
  407. }
  408. passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
  409. if (!passive_data) {
  410. ret = -ENOMEM;
  411. goto err;
  412. }
  413. passive_data->parent = parent_devfreq;
  414. /* Add devfreq device for exynos bus with passive governor */
  415. bus->devfreq = devm_devfreq_add_device(dev, profile, "passive",
  416. passive_data);
  417. if (IS_ERR(bus->devfreq)) {
  418. dev_err(dev,
  419. "failed to add devfreq dev with passive governor\n");
  420. ret = PTR_ERR(bus->devfreq);
  421. goto err;
  422. }
  423. out:
  424. max_state = bus->devfreq->profile->max_state;
  425. min_freq = (bus->devfreq->profile->freq_table[0] / 1000);
  426. max_freq = (bus->devfreq->profile->freq_table[max_state - 1] / 1000);
  427. pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
  428. dev_name(dev), min_freq, max_freq);
  429. return 0;
  430. err:
  431. dev_pm_opp_of_remove_table(dev);
  432. clk_disable_unprepare(bus->clk);
  433. return ret;
  434. }
  435. #ifdef CONFIG_PM_SLEEP
  436. static int exynos_bus_resume(struct device *dev)
  437. {
  438. struct exynos_bus *bus = dev_get_drvdata(dev);
  439. int ret;
  440. ret = exynos_bus_enable_edev(bus);
  441. if (ret < 0) {
  442. dev_err(dev, "failed to enable the devfreq-event devices\n");
  443. return ret;
  444. }
  445. return 0;
  446. }
  447. static int exynos_bus_suspend(struct device *dev)
  448. {
  449. struct exynos_bus *bus = dev_get_drvdata(dev);
  450. int ret;
  451. ret = exynos_bus_disable_edev(bus);
  452. if (ret < 0) {
  453. dev_err(dev, "failed to disable the devfreq-event devices\n");
  454. return ret;
  455. }
  456. return 0;
  457. }
  458. #endif
  459. static const struct dev_pm_ops exynos_bus_pm = {
  460. SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend, exynos_bus_resume)
  461. };
  462. static const struct of_device_id exynos_bus_of_match[] = {
  463. { .compatible = "samsung,exynos-bus", },
  464. { /* sentinel */ },
  465. };
  466. MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
  467. static struct platform_driver exynos_bus_platdrv = {
  468. .probe = exynos_bus_probe,
  469. .driver = {
  470. .name = "exynos-bus",
  471. .pm = &exynos_bus_pm,
  472. .of_match_table = of_match_ptr(exynos_bus_of_match),
  473. },
  474. };
  475. module_platform_driver(exynos_bus_platdrv);
  476. MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
  477. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  478. MODULE_LICENSE("GPL v2");