uniphier-regulator.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. struct resource *res;
  43. void __iomem *base;
  44. const char *name;
  45. int i, ret, nr;
  46. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  47. if (!priv)
  48. return -ENOMEM;
  49. priv->data = of_device_get_match_data(dev);
  50. if (WARN_ON(!priv->data))
  51. return -EINVAL;
  52. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  53. base = devm_ioremap_resource(dev, res);
  54. if (IS_ERR(base))
  55. return PTR_ERR(base);
  56. for (i = 0; i < priv->data->nclks; i++)
  57. priv->clk[i].id = priv->data->clock_names[i];
  58. ret = devm_clk_bulk_get(dev, priv->data->nclks, priv->clk);
  59. if (ret)
  60. return ret;
  61. for (i = 0; i < priv->data->nrsts; i++) {
  62. name = priv->data->reset_names[i];
  63. priv->rst[i] = devm_reset_control_get_shared(dev, name);
  64. if (IS_ERR(priv->rst[i]))
  65. return PTR_ERR(priv->rst[i]);
  66. }
  67. ret = clk_bulk_prepare_enable(priv->data->nclks, priv->clk);
  68. if (ret)
  69. return ret;
  70. for (nr = 0; nr < priv->data->nrsts; nr++) {
  71. ret = reset_control_deassert(priv->rst[nr]);
  72. if (ret)
  73. goto out_rst_assert;
  74. }
  75. regmap = devm_regmap_init_mmio(dev, base, priv->data->regconf);
  76. if (IS_ERR(regmap)) {
  77. ret = PTR_ERR(regmap);
  78. goto out_rst_assert;
  79. }
  80. config.dev = dev;
  81. config.driver_data = priv;
  82. config.of_node = dev->of_node;
  83. config.regmap = regmap;
  84. config.init_data = of_get_regulator_init_data(dev, dev->of_node,
  85. priv->data->desc);
  86. rdev = devm_regulator_register(dev, priv->data->desc, &config);
  87. if (IS_ERR(rdev)) {
  88. ret = PTR_ERR(rdev);
  89. goto out_rst_assert;
  90. }
  91. platform_set_drvdata(pdev, priv);
  92. return 0;
  93. out_rst_assert:
  94. while (nr--)
  95. reset_control_assert(priv->rst[nr]);
  96. clk_bulk_disable_unprepare(priv->data->nclks, priv->clk);
  97. return ret;
  98. }
  99. static int uniphier_regulator_remove(struct platform_device *pdev)
  100. {
  101. struct uniphier_regulator_priv *priv = platform_get_drvdata(pdev);
  102. int i;
  103. for (i = 0; i < priv->data->nrsts; i++)
  104. reset_control_assert(priv->rst[i]);
  105. clk_bulk_disable_unprepare(priv->data->nclks, priv->clk);
  106. return 0;
  107. }
  108. /* USB3 controller data */
  109. #define USB3VBUS_OFFSET 0x0
  110. #define USB3VBUS_REG BIT(4)
  111. #define USB3VBUS_REG_EN BIT(3)
  112. static const struct regulator_desc uniphier_usb3_regulator_desc = {
  113. .name = "vbus",
  114. .of_match = of_match_ptr("vbus"),
  115. .ops = &uniphier_regulator_ops,
  116. .type = REGULATOR_VOLTAGE,
  117. .owner = THIS_MODULE,
  118. .enable_reg = USB3VBUS_OFFSET,
  119. .enable_mask = USB3VBUS_REG_EN | USB3VBUS_REG,
  120. .enable_val = USB3VBUS_REG_EN | USB3VBUS_REG,
  121. .disable_val = USB3VBUS_REG_EN,
  122. };
  123. static const struct regmap_config uniphier_usb3_regulator_regconf = {
  124. .reg_bits = 32,
  125. .val_bits = 32,
  126. .reg_stride = 4,
  127. .max_register = 1,
  128. };
  129. static const char * const uniphier_pro4_clock_reset_names[] = {
  130. "gio", "link",
  131. };
  132. static const struct uniphier_regulator_soc_data uniphier_pro4_usb3_data = {
  133. .nclks = ARRAY_SIZE(uniphier_pro4_clock_reset_names),
  134. .clock_names = uniphier_pro4_clock_reset_names,
  135. .nrsts = ARRAY_SIZE(uniphier_pro4_clock_reset_names),
  136. .reset_names = uniphier_pro4_clock_reset_names,
  137. .desc = &uniphier_usb3_regulator_desc,
  138. .regconf = &uniphier_usb3_regulator_regconf,
  139. };
  140. static const char * const uniphier_pxs2_clock_reset_names[] = {
  141. "link",
  142. };
  143. static const struct uniphier_regulator_soc_data uniphier_pxs2_usb3_data = {
  144. .nclks = ARRAY_SIZE(uniphier_pxs2_clock_reset_names),
  145. .clock_names = uniphier_pxs2_clock_reset_names,
  146. .nrsts = ARRAY_SIZE(uniphier_pxs2_clock_reset_names),
  147. .reset_names = uniphier_pxs2_clock_reset_names,
  148. .desc = &uniphier_usb3_regulator_desc,
  149. .regconf = &uniphier_usb3_regulator_regconf,
  150. };
  151. static const struct of_device_id uniphier_regulator_match[] = {
  152. /* USB VBUS */
  153. {
  154. .compatible = "socionext,uniphier-pro4-usb3-regulator",
  155. .data = &uniphier_pro4_usb3_data,
  156. },
  157. {
  158. .compatible = "socionext,uniphier-pro5-usb3-regulator",
  159. .data = &uniphier_pro4_usb3_data,
  160. },
  161. {
  162. .compatible = "socionext,uniphier-pxs2-usb3-regulator",
  163. .data = &uniphier_pxs2_usb3_data,
  164. },
  165. {
  166. .compatible = "socionext,uniphier-ld20-usb3-regulator",
  167. .data = &uniphier_pxs2_usb3_data,
  168. },
  169. {
  170. .compatible = "socionext,uniphier-pxs3-usb3-regulator",
  171. .data = &uniphier_pxs2_usb3_data,
  172. },
  173. { /* Sentinel */ },
  174. };
  175. MODULE_DEVICE_TABLE(of, uniphier_regulator_match);
  176. static struct platform_driver uniphier_regulator_driver = {
  177. .probe = uniphier_regulator_probe,
  178. .remove = uniphier_regulator_remove,
  179. .driver = {
  180. .name = "uniphier-regulator",
  181. .of_match_table = uniphier_regulator_match,
  182. },
  183. };
  184. module_platform_driver(uniphier_regulator_driver);
  185. MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
  186. MODULE_DESCRIPTION("UniPhier Regulator Controller Driver");
  187. MODULE_LICENSE("GPL");