exynos-bus.c 14 KB

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