exynos-bus.c 14 KB

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