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. 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 (%luHz -> %luHz, %luHz)\n",
  127. old_freq, new_freq, clk_get_rate(bus->clk));
  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. dev_pm_opp_of_remove_table(dev);
  164. clk_disable_unprepare(bus->clk);
  165. if (bus->regulator)
  166. regulator_disable(bus->regulator);
  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 (%luHz -> %luHz, %luHz)\n",
  199. old_freq, new_freq, clk_get_rate(bus->clk));
  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. bool passive = false;
  322. if (!np) {
  323. dev_err(dev, "failed to find devicetree node\n");
  324. return -EINVAL;
  325. }
  326. bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
  327. if (!bus)
  328. return -ENOMEM;
  329. mutex_init(&bus->lock);
  330. bus->dev = &pdev->dev;
  331. platform_set_drvdata(pdev, bus);
  332. profile = devm_kzalloc(dev, sizeof(*profile), GFP_KERNEL);
  333. if (!profile)
  334. return -ENOMEM;
  335. node = of_parse_phandle(dev->of_node, "devfreq", 0);
  336. if (node) {
  337. of_node_put(node);
  338. passive = true;
  339. } else {
  340. ret = exynos_bus_parent_parse_of(np, bus);
  341. if (ret < 0)
  342. return ret;
  343. }
  344. /* Parse the device-tree to get the resource information */
  345. ret = exynos_bus_parse_of(np, bus);
  346. if (ret < 0)
  347. goto err_reg;
  348. if (passive)
  349. goto passive;
  350. /* Initialize the struct profile and governor data for parent device */
  351. profile->polling_ms = 50;
  352. profile->target = exynos_bus_target;
  353. profile->get_dev_status = exynos_bus_get_dev_status;
  354. profile->exit = exynos_bus_exit;
  355. ondemand_data = devm_kzalloc(dev, sizeof(*ondemand_data), GFP_KERNEL);
  356. if (!ondemand_data) {
  357. ret = -ENOMEM;
  358. goto err;
  359. }
  360. ondemand_data->upthreshold = 40;
  361. ondemand_data->downdifferential = 5;
  362. /* Add devfreq device to monitor and handle the exynos bus */
  363. bus->devfreq = devm_devfreq_add_device(dev, profile, "simple_ondemand",
  364. ondemand_data);
  365. if (IS_ERR(bus->devfreq)) {
  366. dev_err(dev, "failed to add devfreq device\n");
  367. ret = PTR_ERR(bus->devfreq);
  368. goto err;
  369. }
  370. /* Register opp_notifier to catch the change of OPP */
  371. ret = devm_devfreq_register_opp_notifier(dev, bus->devfreq);
  372. if (ret < 0) {
  373. dev_err(dev, "failed to register opp notifier\n");
  374. goto err;
  375. }
  376. /*
  377. * Enable devfreq-event to get raw data which is used to determine
  378. * current bus load.
  379. */
  380. ret = exynos_bus_enable_edev(bus);
  381. if (ret < 0) {
  382. dev_err(dev, "failed to enable devfreq-event devices\n");
  383. goto err;
  384. }
  385. ret = exynos_bus_set_event(bus);
  386. if (ret < 0) {
  387. dev_err(dev, "failed to set event to devfreq-event devices\n");
  388. goto err;
  389. }
  390. goto out;
  391. passive:
  392. /* Initialize the struct profile and governor data for passive device */
  393. profile->target = exynos_bus_passive_target;
  394. profile->exit = exynos_bus_passive_exit;
  395. /* Get the instance of parent devfreq device */
  396. parent_devfreq = devfreq_get_devfreq_by_phandle(dev, 0);
  397. if (IS_ERR(parent_devfreq)) {
  398. ret = -EPROBE_DEFER;
  399. goto err;
  400. }
  401. passive_data = devm_kzalloc(dev, sizeof(*passive_data), GFP_KERNEL);
  402. if (!passive_data) {
  403. ret = -ENOMEM;
  404. goto err;
  405. }
  406. passive_data->parent = parent_devfreq;
  407. /* Add devfreq device for exynos bus with passive governor */
  408. bus->devfreq = devm_devfreq_add_device(dev, profile, "passive",
  409. passive_data);
  410. if (IS_ERR(bus->devfreq)) {
  411. dev_err(dev,
  412. "failed to add devfreq dev with passive governor\n");
  413. ret = PTR_ERR(bus->devfreq);
  414. goto err;
  415. }
  416. out:
  417. max_state = bus->devfreq->profile->max_state;
  418. min_freq = (bus->devfreq->profile->freq_table[0] / 1000);
  419. max_freq = (bus->devfreq->profile->freq_table[max_state - 1] / 1000);
  420. pr_info("exynos-bus: new bus device registered: %s (%6ld KHz ~ %6ld KHz)\n",
  421. dev_name(dev), min_freq, max_freq);
  422. return 0;
  423. err:
  424. dev_pm_opp_of_remove_table(dev);
  425. clk_disable_unprepare(bus->clk);
  426. err_reg:
  427. if (!passive)
  428. regulator_disable(bus->regulator);
  429. return ret;
  430. }
  431. #ifdef CONFIG_PM_SLEEP
  432. static int exynos_bus_resume(struct device *dev)
  433. {
  434. struct exynos_bus *bus = dev_get_drvdata(dev);
  435. int ret;
  436. ret = exynos_bus_enable_edev(bus);
  437. if (ret < 0) {
  438. dev_err(dev, "failed to enable the devfreq-event devices\n");
  439. return ret;
  440. }
  441. return 0;
  442. }
  443. static int exynos_bus_suspend(struct device *dev)
  444. {
  445. struct exynos_bus *bus = dev_get_drvdata(dev);
  446. int ret;
  447. ret = exynos_bus_disable_edev(bus);
  448. if (ret < 0) {
  449. dev_err(dev, "failed to disable the devfreq-event devices\n");
  450. return ret;
  451. }
  452. return 0;
  453. }
  454. #endif
  455. static const struct dev_pm_ops exynos_bus_pm = {
  456. SET_SYSTEM_SLEEP_PM_OPS(exynos_bus_suspend, exynos_bus_resume)
  457. };
  458. static const struct of_device_id exynos_bus_of_match[] = {
  459. { .compatible = "samsung,exynos-bus", },
  460. { /* sentinel */ },
  461. };
  462. MODULE_DEVICE_TABLE(of, exynos_bus_of_match);
  463. static struct platform_driver exynos_bus_platdrv = {
  464. .probe = exynos_bus_probe,
  465. .driver = {
  466. .name = "exynos-bus",
  467. .pm = &exynos_bus_pm,
  468. .of_match_table = of_match_ptr(exynos_bus_of_match),
  469. },
  470. };
  471. module_platform_driver(exynos_bus_platdrv);
  472. MODULE_DESCRIPTION("Generic Exynos Bus frequency driver");
  473. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  474. MODULE_LICENSE("GPL v2");