clk-sun9i-mmc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright 2015 Chen-Yu Tsai
  3. *
  4. * Chen-Yu Tsai <wens@csie.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/reset.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/reset-controller.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #define SUN9I_MMC_WIDTH 4
  27. #define SUN9I_MMC_GATE_BIT 16
  28. #define SUN9I_MMC_RESET_BIT 18
  29. struct sun9i_mmc_clk_data {
  30. spinlock_t lock;
  31. void __iomem *membase;
  32. struct clk *clk;
  33. struct reset_control *reset;
  34. struct clk_onecell_data clk_data;
  35. struct reset_controller_dev rcdev;
  36. };
  37. static int sun9i_mmc_reset_assert(struct reset_controller_dev *rcdev,
  38. unsigned long id)
  39. {
  40. struct sun9i_mmc_clk_data *data = container_of(rcdev,
  41. struct sun9i_mmc_clk_data,
  42. rcdev);
  43. unsigned long flags;
  44. void __iomem *reg = data->membase + SUN9I_MMC_WIDTH * id;
  45. u32 val;
  46. clk_prepare_enable(data->clk);
  47. spin_lock_irqsave(&data->lock, flags);
  48. val = readl(reg);
  49. writel(val & ~BIT(SUN9I_MMC_RESET_BIT), reg);
  50. spin_unlock_irqrestore(&data->lock, flags);
  51. clk_disable_unprepare(data->clk);
  52. return 0;
  53. }
  54. static int sun9i_mmc_reset_deassert(struct reset_controller_dev *rcdev,
  55. unsigned long id)
  56. {
  57. struct sun9i_mmc_clk_data *data = container_of(rcdev,
  58. struct sun9i_mmc_clk_data,
  59. rcdev);
  60. unsigned long flags;
  61. void __iomem *reg = data->membase + SUN9I_MMC_WIDTH * id;
  62. u32 val;
  63. clk_prepare_enable(data->clk);
  64. spin_lock_irqsave(&data->lock, flags);
  65. val = readl(reg);
  66. writel(val | BIT(SUN9I_MMC_RESET_BIT), reg);
  67. spin_unlock_irqrestore(&data->lock, flags);
  68. clk_disable_unprepare(data->clk);
  69. return 0;
  70. }
  71. static struct reset_control_ops sun9i_mmc_reset_ops = {
  72. .assert = sun9i_mmc_reset_assert,
  73. .deassert = sun9i_mmc_reset_deassert,
  74. };
  75. static int sun9i_a80_mmc_config_clk_probe(struct platform_device *pdev)
  76. {
  77. struct device_node *np = pdev->dev.of_node;
  78. struct sun9i_mmc_clk_data *data;
  79. struct clk_onecell_data *clk_data;
  80. const char *clk_name = np->name;
  81. const char *clk_parent;
  82. struct resource *r;
  83. int count, i, ret;
  84. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  85. if (!data)
  86. return -ENOMEM;
  87. spin_lock_init(&data->lock);
  88. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  89. /* one clock/reset pair per word */
  90. count = DIV_ROUND_UP((r->end - r->start + 1), SUN9I_MMC_WIDTH);
  91. data->membase = devm_ioremap_resource(&pdev->dev, r);
  92. if (IS_ERR(data->membase))
  93. return PTR_ERR(data->membase);
  94. clk_data = &data->clk_data;
  95. clk_data->clk_num = count;
  96. clk_data->clks = devm_kcalloc(&pdev->dev, count, sizeof(struct clk *),
  97. GFP_KERNEL);
  98. if (!clk_data->clks)
  99. return -ENOMEM;
  100. data->clk = devm_clk_get(&pdev->dev, NULL);
  101. if (IS_ERR(data->clk)) {
  102. dev_err(&pdev->dev, "Could not get clock\n");
  103. return PTR_ERR(data->clk);
  104. }
  105. data->reset = devm_reset_control_get(&pdev->dev, NULL);
  106. if (IS_ERR(data->reset)) {
  107. dev_err(&pdev->dev, "Could not get reset control\n");
  108. return PTR_ERR(data->reset);
  109. }
  110. ret = reset_control_deassert(data->reset);
  111. if (ret) {
  112. dev_err(&pdev->dev, "Reset deassert err %d\n", ret);
  113. return ret;
  114. }
  115. clk_parent = __clk_get_name(data->clk);
  116. for (i = 0; i < count; i++) {
  117. of_property_read_string_index(np, "clock-output-names",
  118. i, &clk_name);
  119. clk_data->clks[i] = clk_register_gate(&pdev->dev, clk_name,
  120. clk_parent, 0,
  121. data->membase + SUN9I_MMC_WIDTH * i,
  122. SUN9I_MMC_GATE_BIT, 0,
  123. &data->lock);
  124. if (IS_ERR(clk_data->clks[i])) {
  125. ret = PTR_ERR(clk_data->clks[i]);
  126. goto err_clk_register;
  127. }
  128. }
  129. ret = of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  130. if (ret)
  131. goto err_clk_provider;
  132. data->rcdev.owner = THIS_MODULE;
  133. data->rcdev.nr_resets = count;
  134. data->rcdev.ops = &sun9i_mmc_reset_ops;
  135. data->rcdev.of_node = pdev->dev.of_node;
  136. ret = reset_controller_register(&data->rcdev);
  137. if (ret)
  138. goto err_rc_reg;
  139. platform_set_drvdata(pdev, data);
  140. return 0;
  141. err_rc_reg:
  142. of_clk_del_provider(np);
  143. err_clk_provider:
  144. for (i = 0; i < count; i++)
  145. clk_unregister(clk_data->clks[i]);
  146. err_clk_register:
  147. reset_control_assert(data->reset);
  148. return ret;
  149. }
  150. static int sun9i_a80_mmc_config_clk_remove(struct platform_device *pdev)
  151. {
  152. struct device_node *np = pdev->dev.of_node;
  153. struct sun9i_mmc_clk_data *data = platform_get_drvdata(pdev);
  154. struct clk_onecell_data *clk_data = &data->clk_data;
  155. int i;
  156. reset_controller_unregister(&data->rcdev);
  157. of_clk_del_provider(np);
  158. for (i = 0; i < clk_data->clk_num; i++)
  159. clk_unregister(clk_data->clks[i]);
  160. reset_control_assert(data->reset);
  161. return 0;
  162. }
  163. static const struct of_device_id sun9i_a80_mmc_config_clk_dt_ids[] = {
  164. { .compatible = "allwinner,sun9i-a80-mmc-config-clk" },
  165. { /* sentinel */ }
  166. };
  167. MODULE_DEVICE_TABLE(of, sun9i_a80_mmc_config_clk_dt_ids);
  168. static struct platform_driver sun9i_a80_mmc_config_clk_driver = {
  169. .driver = {
  170. .name = "sun9i-a80-mmc-config-clk",
  171. .of_match_table = sun9i_a80_mmc_config_clk_dt_ids,
  172. },
  173. .probe = sun9i_a80_mmc_config_clk_probe,
  174. .remove = sun9i_a80_mmc_config_clk_remove,
  175. };
  176. module_platform_driver(sun9i_a80_mmc_config_clk_driver);
  177. MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
  178. MODULE_DESCRIPTION("Allwinner A80 MMC clock/reset Driver");
  179. MODULE_LICENSE("GPL v2");