phy-exynos-mipi-video.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Samsung S5P/EXYNOS SoC series MIPI CSIS/DSIM DPHY driver
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  5. * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mfd/syscon/exynos4-pmu.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/mfd/syscon.h>
  23. /* MIPI_PHYn_CONTROL reg. offset (for base address from ioremap): n = 0..1 */
  24. #define EXYNOS_MIPI_PHY_CONTROL(n) ((n) * 4)
  25. enum exynos_mipi_phy_id {
  26. EXYNOS_MIPI_PHY_ID_CSIS0,
  27. EXYNOS_MIPI_PHY_ID_DSIM0,
  28. EXYNOS_MIPI_PHY_ID_CSIS1,
  29. EXYNOS_MIPI_PHY_ID_DSIM1,
  30. EXYNOS_MIPI_PHYS_NUM
  31. };
  32. #define is_mipi_dsim_phy_id(id) \
  33. ((id) == EXYNOS_MIPI_PHY_ID_DSIM0 || (id) == EXYNOS_MIPI_PHY_ID_DSIM1)
  34. struct exynos_mipi_video_phy {
  35. struct video_phy_desc {
  36. struct phy *phy;
  37. unsigned int index;
  38. } phys[EXYNOS_MIPI_PHYS_NUM];
  39. spinlock_t slock;
  40. void __iomem *regs;
  41. struct regmap *regmap;
  42. };
  43. static int __set_phy_state(struct exynos_mipi_video_phy *state,
  44. enum exynos_mipi_phy_id id, unsigned int on)
  45. {
  46. const unsigned int offset = EXYNOS4_MIPI_PHY_CONTROL(id / 2);
  47. void __iomem *addr;
  48. u32 val, reset;
  49. if (is_mipi_dsim_phy_id(id))
  50. reset = EXYNOS4_MIPI_PHY_MRESETN;
  51. else
  52. reset = EXYNOS4_MIPI_PHY_SRESETN;
  53. spin_lock(&state->slock);
  54. if (!IS_ERR(state->regmap)) {
  55. regmap_read(state->regmap, offset, &val);
  56. if (on)
  57. val |= reset;
  58. else
  59. val &= ~reset;
  60. regmap_write(state->regmap, offset, val);
  61. if (on)
  62. val |= EXYNOS4_MIPI_PHY_ENABLE;
  63. else if (!(val & EXYNOS4_MIPI_PHY_RESET_MASK))
  64. val &= ~EXYNOS4_MIPI_PHY_ENABLE;
  65. regmap_write(state->regmap, offset, val);
  66. } else {
  67. addr = state->regs + EXYNOS_MIPI_PHY_CONTROL(id / 2);
  68. val = readl(addr);
  69. if (on)
  70. val |= reset;
  71. else
  72. val &= ~reset;
  73. writel(val, addr);
  74. /* Clear ENABLE bit only if MRESETN, SRESETN bits are not set */
  75. if (on)
  76. val |= EXYNOS4_MIPI_PHY_ENABLE;
  77. else if (!(val & EXYNOS4_MIPI_PHY_RESET_MASK))
  78. val &= ~EXYNOS4_MIPI_PHY_ENABLE;
  79. writel(val, addr);
  80. }
  81. spin_unlock(&state->slock);
  82. return 0;
  83. }
  84. #define to_mipi_video_phy(desc) \
  85. container_of((desc), struct exynos_mipi_video_phy, phys[(desc)->index]);
  86. static int exynos_mipi_video_phy_power_on(struct phy *phy)
  87. {
  88. struct video_phy_desc *phy_desc = phy_get_drvdata(phy);
  89. struct exynos_mipi_video_phy *state = to_mipi_video_phy(phy_desc);
  90. return __set_phy_state(state, phy_desc->index, 1);
  91. }
  92. static int exynos_mipi_video_phy_power_off(struct phy *phy)
  93. {
  94. struct video_phy_desc *phy_desc = phy_get_drvdata(phy);
  95. struct exynos_mipi_video_phy *state = to_mipi_video_phy(phy_desc);
  96. return __set_phy_state(state, phy_desc->index, 0);
  97. }
  98. static struct phy *exynos_mipi_video_phy_xlate(struct device *dev,
  99. struct of_phandle_args *args)
  100. {
  101. struct exynos_mipi_video_phy *state = dev_get_drvdata(dev);
  102. if (WARN_ON(args->args[0] >= EXYNOS_MIPI_PHYS_NUM))
  103. return ERR_PTR(-ENODEV);
  104. return state->phys[args->args[0]].phy;
  105. }
  106. static const struct phy_ops exynos_mipi_video_phy_ops = {
  107. .power_on = exynos_mipi_video_phy_power_on,
  108. .power_off = exynos_mipi_video_phy_power_off,
  109. .owner = THIS_MODULE,
  110. };
  111. static int exynos_mipi_video_phy_probe(struct platform_device *pdev)
  112. {
  113. struct exynos_mipi_video_phy *state;
  114. struct device *dev = &pdev->dev;
  115. struct phy_provider *phy_provider;
  116. unsigned int i;
  117. state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
  118. if (!state)
  119. return -ENOMEM;
  120. state->regmap = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
  121. if (IS_ERR(state->regmap)) {
  122. struct resource *res;
  123. dev_info(dev, "regmap lookup failed: %ld\n",
  124. PTR_ERR(state->regmap));
  125. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  126. state->regs = devm_ioremap_resource(dev, res);
  127. if (IS_ERR(state->regs))
  128. return PTR_ERR(state->regs);
  129. }
  130. dev_set_drvdata(dev, state);
  131. spin_lock_init(&state->slock);
  132. for (i = 0; i < EXYNOS_MIPI_PHYS_NUM; i++) {
  133. struct phy *phy = devm_phy_create(dev, NULL,
  134. &exynos_mipi_video_phy_ops);
  135. if (IS_ERR(phy)) {
  136. dev_err(dev, "failed to create PHY %d\n", i);
  137. return PTR_ERR(phy);
  138. }
  139. state->phys[i].phy = phy;
  140. state->phys[i].index = i;
  141. phy_set_drvdata(phy, &state->phys[i]);
  142. }
  143. phy_provider = devm_of_phy_provider_register(dev,
  144. exynos_mipi_video_phy_xlate);
  145. return PTR_ERR_OR_ZERO(phy_provider);
  146. }
  147. static const struct of_device_id exynos_mipi_video_phy_of_match[] = {
  148. { .compatible = "samsung,s5pv210-mipi-video-phy" },
  149. { },
  150. };
  151. MODULE_DEVICE_TABLE(of, exynos_mipi_video_phy_of_match);
  152. static struct platform_driver exynos_mipi_video_phy_driver = {
  153. .probe = exynos_mipi_video_phy_probe,
  154. .driver = {
  155. .of_match_table = exynos_mipi_video_phy_of_match,
  156. .name = "exynos-mipi-video-phy",
  157. }
  158. };
  159. module_platform_driver(exynos_mipi_video_phy_driver);
  160. MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC MIPI CSI-2/DSI PHY driver");
  161. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  162. MODULE_LICENSE("GPL v2");