uniphier-regulator.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Regulator controller driver for UniPhier SoC
  4. // Copyright 2018 Socionext Inc.
  5. // Author: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
  6. #include <linux/clk.h>
  7. #include <linux/io.h>
  8. #include <linux/module.h>
  9. #include <linux/of_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/regmap.h>
  12. #include <linux/regulator/driver.h>
  13. #include <linux/regulator/of_regulator.h>
  14. #include <linux/reset.h>
  15. #define MAX_CLKS 2
  16. #define MAX_RSTS 2
  17. struct uniphier_regulator_soc_data {
  18. int nclks;
  19. const char * const *clock_names;
  20. int nrsts;
  21. const char * const *reset_names;
  22. const struct regulator_desc *desc;
  23. const struct regmap_config *regconf;
  24. };
  25. struct uniphier_regulator_priv {
  26. struct clk_bulk_data clk[MAX_CLKS];
  27. struct reset_control *rst[MAX_RSTS];
  28. const struct uniphier_regulator_soc_data *data;
  29. };
  30. static const struct regulator_ops uniphier_regulator_ops = {
  31. .enable = regulator_enable_regmap,
  32. .disable = regulator_disable_regmap,
  33. .is_enabled = regulator_is_enabled_regmap,
  34. };
  35. static int uniphier_regulator_probe(struct platform_device *pdev)
  36. {
  37. struct device *dev = &pdev->dev;
  38. struct uniphier_regulator_priv *priv;
  39. struct regulator_config config = { };
  40. struct regulator_dev *rdev;
  41. struct regmap *regmap;
  42. void __iomem *base;
  43. const char *name;
  44. int i, ret, nr;
  45. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  46. if (!priv)
  47. return -ENOMEM;
  48. priv->data = of_device_get_match_data(dev);
  49. if (WARN_ON(!priv->data))
  50. return -EINVAL;
  51. base = devm_platform_ioremap_resource(pdev, 0);
  52. if (IS_ERR(base))
  53. return PTR_ERR(base);
  54. for (i = 0; i < priv->data->nclks; i++)
  55. priv->clk[i].id = priv->data->clock_names[i];
  56. ret = devm_clk_bulk_get(dev, priv->data->nclks, priv->clk);
  57. if (ret)
  58. return ret;
  59. for (i = 0; i < priv->data->nrsts; i++) {
  60. name = priv->data->reset_names[i];
  61. priv->rst[i] = devm_reset_control_get_shared(dev, name);
  62. if (IS_ERR(priv->rst[i]))
  63. return PTR_ERR(priv->rst[i]);
  64. }
  65. ret = clk_bulk_prepare_enable(priv->data->nclks, priv->clk);
  66. if (ret)
  67. return ret;
  68. for (nr = 0; nr < priv->data->nrsts; nr++) {
  69. ret = reset_control_deassert(priv->rst[nr]);
  70. if (ret)
  71. goto out_rst_assert;
  72. }
  73. regmap = devm_regmap_init_mmio(dev, base, priv->data->regconf);
  74. if (IS_ERR(regmap)) {
  75. ret = PTR_ERR(regmap);
  76. goto out_rst_assert;
  77. }
  78. config.dev = dev;
  79. config.driver_data = priv;
  80. config.of_node = dev->of_node;
  81. config.regmap = regmap;
  82. config.init_data = of_get_regulator_init_data(dev, dev->of_node,
  83. priv->data->desc);
  84. rdev = devm_regulator_register(dev, priv->data->desc, &config);
  85. if (IS_ERR(rdev)) {
  86. ret = PTR_ERR(rdev);
  87. goto out_rst_assert;
  88. }
  89. platform_set_drvdata(pdev, priv);
  90. return 0;
  91. out_rst_assert:
  92. while (nr--)
  93. reset_control_assert(priv->rst[nr]);
  94. clk_bulk_disable_unprepare(priv->data->nclks, priv->clk);
  95. return ret;
  96. }
  97. static int uniphier_regulator_remove(struct platform_device *pdev)
  98. {
  99. struct uniphier_regulator_priv *priv = platform_get_drvdata(pdev);
  100. int i;
  101. for (i = 0; i < priv->data->nrsts; i++)
  102. reset_control_assert(priv->rst[i]);
  103. clk_bulk_disable_unprepare(priv->data->nclks, priv->clk);
  104. return 0;
  105. }
  106. /* USB3 controller data */
  107. #define USB3VBUS_OFFSET 0x0
  108. #define USB3VBUS_REG BIT(4)
  109. #define USB3VBUS_REG_EN BIT(3)
  110. static const struct regulator_desc uniphier_usb3_regulator_desc = {
  111. .name = "vbus",
  112. .of_match = of_match_ptr("vbus"),
  113. .ops = &uniphier_regulator_ops,
  114. .type = REGULATOR_VOLTAGE,
  115. .owner = THIS_MODULE,
  116. .enable_reg = USB3VBUS_OFFSET,
  117. .enable_mask = USB3VBUS_REG_EN | USB3VBUS_REG,
  118. .enable_val = USB3VBUS_REG_EN | USB3VBUS_REG,
  119. .disable_val = USB3VBUS_REG_EN,
  120. };
  121. static const struct regmap_config uniphier_usb3_regulator_regconf = {
  122. .reg_bits = 32,
  123. .val_bits = 32,
  124. .reg_stride = 4,
  125. .max_register = 1,
  126. };
  127. static const char * const uniphier_pro4_clock_reset_names[] = {
  128. "gio", "link",
  129. };
  130. static const struct uniphier_regulator_soc_data uniphier_pro4_usb3_data = {
  131. .nclks = ARRAY_SIZE(uniphier_pro4_clock_reset_names),
  132. .clock_names = uniphier_pro4_clock_reset_names,
  133. .nrsts = ARRAY_SIZE(uniphier_pro4_clock_reset_names),
  134. .reset_names = uniphier_pro4_clock_reset_names,
  135. .desc = &uniphier_usb3_regulator_desc,
  136. .regconf = &uniphier_usb3_regulator_regconf,
  137. };
  138. static const char * const uniphier_pxs2_clock_reset_names[] = {
  139. "link",
  140. };
  141. static const struct uniphier_regulator_soc_data uniphier_pxs2_usb3_data = {
  142. .nclks = ARRAY_SIZE(uniphier_pxs2_clock_reset_names),
  143. .clock_names = uniphier_pxs2_clock_reset_names,
  144. .nrsts = ARRAY_SIZE(uniphier_pxs2_clock_reset_names),
  145. .reset_names = uniphier_pxs2_clock_reset_names,
  146. .desc = &uniphier_usb3_regulator_desc,
  147. .regconf = &uniphier_usb3_regulator_regconf,
  148. };
  149. static const struct of_device_id uniphier_regulator_match[] = {
  150. /* USB VBUS */
  151. {
  152. .compatible = "socionext,uniphier-pro4-usb3-regulator",
  153. .data = &uniphier_pro4_usb3_data,
  154. },
  155. {
  156. .compatible = "socionext,uniphier-pro5-usb3-regulator",
  157. .data = &uniphier_pro4_usb3_data,
  158. },
  159. {
  160. .compatible = "socionext,uniphier-pxs2-usb3-regulator",
  161. .data = &uniphier_pxs2_usb3_data,
  162. },
  163. {
  164. .compatible = "socionext,uniphier-ld20-usb3-regulator",
  165. .data = &uniphier_pxs2_usb3_data,
  166. },
  167. {
  168. .compatible = "socionext,uniphier-pxs3-usb3-regulator",
  169. .data = &uniphier_pxs2_usb3_data,
  170. },
  171. { /* Sentinel */ },
  172. };
  173. MODULE_DEVICE_TABLE(of, uniphier_regulator_match);
  174. static struct platform_driver uniphier_regulator_driver = {
  175. .probe = uniphier_regulator_probe,
  176. .remove = uniphier_regulator_remove,
  177. .driver = {
  178. .name = "uniphier-regulator",
  179. .of_match_table = uniphier_regulator_match,
  180. },
  181. };
  182. module_platform_driver(uniphier_regulator_driver);
  183. MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
  184. MODULE_DESCRIPTION("UniPhier Regulator Controller Driver");
  185. MODULE_LICENSE("GPL");