sun6i_drc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Free Electrons
  4. *
  5. * Maxime Ripard <maxime.ripard@free-electrons.com>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/component.h>
  9. #include <linux/module.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/reset.h>
  14. struct sun6i_drc {
  15. struct clk *bus_clk;
  16. struct clk *mod_clk;
  17. struct reset_control *reset;
  18. };
  19. static int sun6i_drc_bind(struct device *dev, struct device *master,
  20. void *data)
  21. {
  22. struct sun6i_drc *drc;
  23. int ret;
  24. drc = devm_kzalloc(dev, sizeof(*drc), GFP_KERNEL);
  25. if (!drc)
  26. return -ENOMEM;
  27. dev_set_drvdata(dev, drc);
  28. drc->reset = devm_reset_control_get(dev, NULL);
  29. if (IS_ERR(drc->reset)) {
  30. dev_err(dev, "Couldn't get our reset line\n");
  31. return PTR_ERR(drc->reset);
  32. }
  33. ret = reset_control_deassert(drc->reset);
  34. if (ret) {
  35. dev_err(dev, "Couldn't deassert our reset line\n");
  36. return ret;
  37. }
  38. drc->bus_clk = devm_clk_get(dev, "ahb");
  39. if (IS_ERR(drc->bus_clk)) {
  40. dev_err(dev, "Couldn't get our bus clock\n");
  41. ret = PTR_ERR(drc->bus_clk);
  42. goto err_assert_reset;
  43. }
  44. clk_prepare_enable(drc->bus_clk);
  45. drc->mod_clk = devm_clk_get(dev, "mod");
  46. if (IS_ERR(drc->mod_clk)) {
  47. dev_err(dev, "Couldn't get our mod clock\n");
  48. ret = PTR_ERR(drc->mod_clk);
  49. goto err_disable_bus_clk;
  50. }
  51. ret = clk_set_rate_exclusive(drc->mod_clk, 300000000);
  52. if (ret) {
  53. dev_err(dev, "Couldn't set the module clock frequency\n");
  54. goto err_disable_bus_clk;
  55. }
  56. clk_prepare_enable(drc->mod_clk);
  57. return 0;
  58. err_disable_bus_clk:
  59. clk_disable_unprepare(drc->bus_clk);
  60. err_assert_reset:
  61. reset_control_assert(drc->reset);
  62. return ret;
  63. }
  64. static void sun6i_drc_unbind(struct device *dev, struct device *master,
  65. void *data)
  66. {
  67. struct sun6i_drc *drc = dev_get_drvdata(dev);
  68. clk_rate_exclusive_put(drc->mod_clk);
  69. clk_disable_unprepare(drc->mod_clk);
  70. clk_disable_unprepare(drc->bus_clk);
  71. reset_control_assert(drc->reset);
  72. }
  73. static const struct component_ops sun6i_drc_ops = {
  74. .bind = sun6i_drc_bind,
  75. .unbind = sun6i_drc_unbind,
  76. };
  77. static int sun6i_drc_probe(struct platform_device *pdev)
  78. {
  79. return component_add(&pdev->dev, &sun6i_drc_ops);
  80. }
  81. static int sun6i_drc_remove(struct platform_device *pdev)
  82. {
  83. component_del(&pdev->dev, &sun6i_drc_ops);
  84. return 0;
  85. }
  86. static const struct of_device_id sun6i_drc_of_table[] = {
  87. { .compatible = "allwinner,sun6i-a31-drc" },
  88. { .compatible = "allwinner,sun6i-a31s-drc" },
  89. { .compatible = "allwinner,sun8i-a23-drc" },
  90. { .compatible = "allwinner,sun8i-a33-drc" },
  91. { .compatible = "allwinner,sun9i-a80-drc" },
  92. { }
  93. };
  94. MODULE_DEVICE_TABLE(of, sun6i_drc_of_table);
  95. static struct platform_driver sun6i_drc_platform_driver = {
  96. .probe = sun6i_drc_probe,
  97. .remove = sun6i_drc_remove,
  98. .driver = {
  99. .name = "sun6i-drc",
  100. .of_match_table = sun6i_drc_of_table,
  101. },
  102. };
  103. module_platform_driver(sun6i_drc_platform_driver);
  104. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  105. MODULE_DESCRIPTION("Allwinner A31 Dynamic Range Control (DRC) Driver");
  106. MODULE_LICENSE("GPL");