phy-mtk-mipi-dsi.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015 MediaTek Inc.
  4. */
  5. #include "phy-mtk-mipi-dsi.h"
  6. inline struct mtk_mipi_tx *mtk_mipi_tx_from_clk_hw(struct clk_hw *hw)
  7. {
  8. return container_of(hw, struct mtk_mipi_tx, pll_hw);
  9. }
  10. int mtk_mipi_tx_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  11. unsigned long parent_rate)
  12. {
  13. struct mtk_mipi_tx *mipi_tx = mtk_mipi_tx_from_clk_hw(hw);
  14. dev_dbg(mipi_tx->dev, "set rate: %lu Hz\n", rate);
  15. mipi_tx->data_rate = rate;
  16. return 0;
  17. }
  18. unsigned long mtk_mipi_tx_pll_recalc_rate(struct clk_hw *hw,
  19. unsigned long parent_rate)
  20. {
  21. struct mtk_mipi_tx *mipi_tx = mtk_mipi_tx_from_clk_hw(hw);
  22. return mipi_tx->data_rate;
  23. }
  24. static int mtk_mipi_tx_power_on(struct phy *phy)
  25. {
  26. struct mtk_mipi_tx *mipi_tx = phy_get_drvdata(phy);
  27. int ret;
  28. /* Power up core and enable PLL */
  29. ret = clk_prepare_enable(mipi_tx->pll);
  30. if (ret < 0)
  31. return ret;
  32. /* Enable DSI Lane LDO outputs, disable pad tie low */
  33. mipi_tx->driver_data->mipi_tx_enable_signal(phy);
  34. return 0;
  35. }
  36. static int mtk_mipi_tx_power_off(struct phy *phy)
  37. {
  38. struct mtk_mipi_tx *mipi_tx = phy_get_drvdata(phy);
  39. /* Enable pad tie low, disable DSI Lane LDO outputs */
  40. mipi_tx->driver_data->mipi_tx_disable_signal(phy);
  41. /* Disable PLL and power down core */
  42. clk_disable_unprepare(mipi_tx->pll);
  43. return 0;
  44. }
  45. static const struct phy_ops mtk_mipi_tx_ops = {
  46. .power_on = mtk_mipi_tx_power_on,
  47. .power_off = mtk_mipi_tx_power_off,
  48. .owner = THIS_MODULE,
  49. };
  50. static void mtk_mipi_tx_get_calibration_datal(struct mtk_mipi_tx *mipi_tx)
  51. {
  52. struct nvmem_cell *cell;
  53. size_t len;
  54. u32 *buf;
  55. cell = nvmem_cell_get(mipi_tx->dev, "calibration-data");
  56. if (IS_ERR(cell)) {
  57. dev_info(mipi_tx->dev, "can't get nvmem_cell_get, ignore it\n");
  58. return;
  59. }
  60. buf = (u32 *)nvmem_cell_read(cell, &len);
  61. nvmem_cell_put(cell);
  62. if (IS_ERR(buf)) {
  63. dev_info(mipi_tx->dev, "can't get data, ignore it\n");
  64. return;
  65. }
  66. if (len < 3 * sizeof(u32)) {
  67. dev_info(mipi_tx->dev, "invalid calibration data\n");
  68. kfree(buf);
  69. return;
  70. }
  71. mipi_tx->rt_code[0] = ((buf[0] >> 6 & 0x1f) << 5) |
  72. (buf[0] >> 11 & 0x1f);
  73. mipi_tx->rt_code[1] = ((buf[1] >> 27 & 0x1f) << 5) |
  74. (buf[0] >> 1 & 0x1f);
  75. mipi_tx->rt_code[2] = ((buf[1] >> 17 & 0x1f) << 5) |
  76. (buf[1] >> 22 & 0x1f);
  77. mipi_tx->rt_code[3] = ((buf[1] >> 7 & 0x1f) << 5) |
  78. (buf[1] >> 12 & 0x1f);
  79. mipi_tx->rt_code[4] = ((buf[2] >> 27 & 0x1f) << 5) |
  80. (buf[1] >> 2 & 0x1f);
  81. kfree(buf);
  82. }
  83. static int mtk_mipi_tx_probe(struct platform_device *pdev)
  84. {
  85. struct device *dev = &pdev->dev;
  86. struct mtk_mipi_tx *mipi_tx;
  87. const char *ref_clk_name;
  88. struct clk *ref_clk;
  89. struct clk_init_data clk_init = {
  90. .num_parents = 1,
  91. .parent_names = (const char * const *)&ref_clk_name,
  92. .flags = CLK_SET_RATE_GATE,
  93. };
  94. struct phy *phy;
  95. struct phy_provider *phy_provider;
  96. int ret;
  97. mipi_tx = devm_kzalloc(dev, sizeof(*mipi_tx), GFP_KERNEL);
  98. if (!mipi_tx)
  99. return -ENOMEM;
  100. mipi_tx->driver_data = of_device_get_match_data(dev);
  101. if (!mipi_tx->driver_data)
  102. return -ENODEV;
  103. mipi_tx->regs = devm_platform_ioremap_resource(pdev, 0);
  104. if (IS_ERR(mipi_tx->regs))
  105. return PTR_ERR(mipi_tx->regs);
  106. ref_clk = devm_clk_get(dev, NULL);
  107. if (IS_ERR(ref_clk))
  108. return dev_err_probe(dev, PTR_ERR(ref_clk),
  109. "Failed to get reference clock\n");
  110. ret = of_property_read_u32(dev->of_node, "drive-strength-microamp",
  111. &mipi_tx->mipitx_drive);
  112. /* If can't get the "mipi_tx->mipitx_drive", set it default 0x8 */
  113. if (ret < 0)
  114. mipi_tx->mipitx_drive = 4600;
  115. /* check the mipitx_drive valid */
  116. if (mipi_tx->mipitx_drive > 6000 || mipi_tx->mipitx_drive < 3000) {
  117. dev_warn(dev, "drive-strength-microamp is invalid %d, not in 3000 ~ 6000\n",
  118. mipi_tx->mipitx_drive);
  119. mipi_tx->mipitx_drive = clamp_val(mipi_tx->mipitx_drive, 3000,
  120. 6000);
  121. }
  122. ref_clk_name = __clk_get_name(ref_clk);
  123. ret = of_property_read_string(dev->of_node, "clock-output-names",
  124. &clk_init.name);
  125. if (ret < 0)
  126. return dev_err_probe(dev, ret, "Failed to read clock-output-names\n");
  127. clk_init.ops = mipi_tx->driver_data->mipi_tx_clk_ops;
  128. mipi_tx->pll_hw.init = &clk_init;
  129. mipi_tx->pll = devm_clk_register(dev, &mipi_tx->pll_hw);
  130. if (IS_ERR(mipi_tx->pll))
  131. return dev_err_probe(dev, PTR_ERR(mipi_tx->pll), "Failed to register PLL\n");
  132. phy = devm_phy_create(dev, NULL, &mtk_mipi_tx_ops);
  133. if (IS_ERR(phy))
  134. return dev_err_probe(dev, PTR_ERR(phy), "Failed to create MIPI D-PHY\n");
  135. phy_set_drvdata(phy, mipi_tx);
  136. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  137. if (IS_ERR(phy_provider))
  138. return PTR_ERR(phy_provider);
  139. mipi_tx->dev = dev;
  140. mtk_mipi_tx_get_calibration_datal(mipi_tx);
  141. return of_clk_add_provider(dev->of_node, of_clk_src_simple_get,
  142. mipi_tx->pll);
  143. }
  144. static int mtk_mipi_tx_remove(struct platform_device *pdev)
  145. {
  146. of_clk_del_provider(pdev->dev.of_node);
  147. return 0;
  148. }
  149. static const struct of_device_id mtk_mipi_tx_match[] = {
  150. { .compatible = "mediatek,mt2701-mipi-tx",
  151. .data = &mt2701_mipitx_data },
  152. { .compatible = "mediatek,mt8173-mipi-tx",
  153. .data = &mt8173_mipitx_data },
  154. { .compatible = "mediatek,mt8183-mipi-tx",
  155. .data = &mt8183_mipitx_data },
  156. { },
  157. };
  158. MODULE_DEVICE_TABLE(of, mtk_mipi_tx_match);
  159. static struct platform_driver mtk_mipi_tx_driver = {
  160. .probe = mtk_mipi_tx_probe,
  161. .remove = mtk_mipi_tx_remove,
  162. .driver = {
  163. .name = "mediatek-mipi-tx",
  164. .of_match_table = mtk_mipi_tx_match,
  165. },
  166. };
  167. module_platform_driver(mtk_mipi_tx_driver);
  168. MODULE_DESCRIPTION("MediaTek MIPI TX Driver");
  169. MODULE_LICENSE("GPL v2");