phy-exynos-mipi-video.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Samsung S5P/EXYNOS SoC series MIPI CSIS/DSIM DPHY driver
  3. *
  4. * Copyright (C) 2013,2016 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/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_device.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/regmap.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/soc/samsung/exynos-regs-pmu.h>
  22. #include <linux/mfd/syscon.h>
  23. enum exynos_mipi_phy_id {
  24. EXYNOS_MIPI_PHY_ID_NONE = -1,
  25. EXYNOS_MIPI_PHY_ID_CSIS0,
  26. EXYNOS_MIPI_PHY_ID_DSIM0,
  27. EXYNOS_MIPI_PHY_ID_CSIS1,
  28. EXYNOS_MIPI_PHY_ID_DSIM1,
  29. EXYNOS_MIPI_PHY_ID_CSIS2,
  30. EXYNOS_MIPI_PHYS_NUM
  31. };
  32. enum exynos_mipi_phy_regmap_id {
  33. EXYNOS_MIPI_REGMAP_PMU,
  34. EXYNOS_MIPI_REGMAP_DISP,
  35. EXYNOS_MIPI_REGMAP_CAM0,
  36. EXYNOS_MIPI_REGMAP_CAM1,
  37. EXYNOS_MIPI_REGMAPS_NUM
  38. };
  39. struct mipi_phy_device_desc {
  40. int num_phys;
  41. int num_regmaps;
  42. const char *regmap_names[EXYNOS_MIPI_REGMAPS_NUM];
  43. struct exynos_mipi_phy_desc {
  44. enum exynos_mipi_phy_id coupled_phy_id;
  45. u32 enable_val;
  46. unsigned int enable_reg;
  47. enum exynos_mipi_phy_regmap_id enable_map;
  48. u32 resetn_val;
  49. unsigned int resetn_reg;
  50. enum exynos_mipi_phy_regmap_id resetn_map;
  51. } phys[EXYNOS_MIPI_PHYS_NUM];
  52. };
  53. static const struct mipi_phy_device_desc s5pv210_mipi_phy = {
  54. .num_regmaps = 1,
  55. .regmap_names = {"syscon"},
  56. .num_phys = 4,
  57. .phys = {
  58. {
  59. /* EXYNOS_MIPI_PHY_ID_CSIS0 */
  60. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_DSIM0,
  61. .enable_val = EXYNOS4_PHY_ENABLE,
  62. .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(0),
  63. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  64. .resetn_val = EXYNOS4_MIPI_PHY_SRESETN,
  65. .resetn_reg = EXYNOS4_MIPI_PHY_CONTROL(0),
  66. .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
  67. }, {
  68. /* EXYNOS_MIPI_PHY_ID_DSIM0 */
  69. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_CSIS0,
  70. .enable_val = EXYNOS4_PHY_ENABLE,
  71. .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(0),
  72. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  73. .resetn_val = EXYNOS4_MIPI_PHY_MRESETN,
  74. .resetn_reg = EXYNOS4_MIPI_PHY_CONTROL(0),
  75. .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
  76. }, {
  77. /* EXYNOS_MIPI_PHY_ID_CSIS1 */
  78. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_DSIM1,
  79. .enable_val = EXYNOS4_PHY_ENABLE,
  80. .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(1),
  81. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  82. .resetn_val = EXYNOS4_MIPI_PHY_SRESETN,
  83. .resetn_reg = EXYNOS4_MIPI_PHY_CONTROL(1),
  84. .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
  85. }, {
  86. /* EXYNOS_MIPI_PHY_ID_DSIM1 */
  87. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_CSIS1,
  88. .enable_val = EXYNOS4_PHY_ENABLE,
  89. .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(1),
  90. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  91. .resetn_val = EXYNOS4_MIPI_PHY_MRESETN,
  92. .resetn_reg = EXYNOS4_MIPI_PHY_CONTROL(1),
  93. .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
  94. },
  95. },
  96. };
  97. static const struct mipi_phy_device_desc exynos5420_mipi_phy = {
  98. .num_regmaps = 1,
  99. .regmap_names = {"syscon"},
  100. .num_phys = 5,
  101. .phys = {
  102. {
  103. /* EXYNOS_MIPI_PHY_ID_CSIS0 */
  104. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_DSIM0,
  105. .enable_val = EXYNOS4_PHY_ENABLE,
  106. .enable_reg = EXYNOS5420_MIPI_PHY_CONTROL(0),
  107. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  108. .resetn_val = EXYNOS4_MIPI_PHY_SRESETN,
  109. .resetn_reg = EXYNOS5420_MIPI_PHY_CONTROL(0),
  110. .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
  111. }, {
  112. /* EXYNOS_MIPI_PHY_ID_DSIM0 */
  113. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_CSIS0,
  114. .enable_val = EXYNOS4_PHY_ENABLE,
  115. .enable_reg = EXYNOS5420_MIPI_PHY_CONTROL(0),
  116. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  117. .resetn_val = EXYNOS4_MIPI_PHY_MRESETN,
  118. .resetn_reg = EXYNOS5420_MIPI_PHY_CONTROL(0),
  119. .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
  120. }, {
  121. /* EXYNOS_MIPI_PHY_ID_CSIS1 */
  122. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_DSIM1,
  123. .enable_val = EXYNOS4_PHY_ENABLE,
  124. .enable_reg = EXYNOS5420_MIPI_PHY_CONTROL(1),
  125. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  126. .resetn_val = EXYNOS4_MIPI_PHY_SRESETN,
  127. .resetn_reg = EXYNOS5420_MIPI_PHY_CONTROL(1),
  128. .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
  129. }, {
  130. /* EXYNOS_MIPI_PHY_ID_DSIM1 */
  131. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_CSIS1,
  132. .enable_val = EXYNOS4_PHY_ENABLE,
  133. .enable_reg = EXYNOS5420_MIPI_PHY_CONTROL(1),
  134. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  135. .resetn_val = EXYNOS4_MIPI_PHY_MRESETN,
  136. .resetn_reg = EXYNOS5420_MIPI_PHY_CONTROL(1),
  137. .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
  138. }, {
  139. /* EXYNOS_MIPI_PHY_ID_CSIS2 */
  140. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_NONE,
  141. .enable_val = EXYNOS4_PHY_ENABLE,
  142. .enable_reg = EXYNOS5420_MIPI_PHY_CONTROL(2),
  143. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  144. .resetn_val = EXYNOS4_MIPI_PHY_SRESETN,
  145. .resetn_reg = EXYNOS5420_MIPI_PHY_CONTROL(2),
  146. .resetn_map = EXYNOS_MIPI_REGMAP_PMU,
  147. },
  148. },
  149. };
  150. #define EXYNOS5433_SYSREG_DISP_MIPI_PHY 0x100C
  151. #define EXYNOS5433_SYSREG_CAM0_MIPI_DPHY_CON 0x1014
  152. #define EXYNOS5433_SYSREG_CAM1_MIPI_DPHY_CON 0x1020
  153. static const struct mipi_phy_device_desc exynos5433_mipi_phy = {
  154. .num_regmaps = 4,
  155. .regmap_names = {
  156. "samsung,pmu-syscon",
  157. "samsung,disp-sysreg",
  158. "samsung,cam0-sysreg",
  159. "samsung,cam1-sysreg"
  160. },
  161. .num_phys = 5,
  162. .phys = {
  163. {
  164. /* EXYNOS_MIPI_PHY_ID_CSIS0 */
  165. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_DSIM0,
  166. .enable_val = EXYNOS4_PHY_ENABLE,
  167. .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(0),
  168. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  169. .resetn_val = BIT(0),
  170. .resetn_reg = EXYNOS5433_SYSREG_CAM0_MIPI_DPHY_CON,
  171. .resetn_map = EXYNOS_MIPI_REGMAP_CAM0,
  172. }, {
  173. /* EXYNOS_MIPI_PHY_ID_DSIM0 */
  174. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_CSIS0,
  175. .enable_val = EXYNOS4_PHY_ENABLE,
  176. .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(0),
  177. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  178. .resetn_val = BIT(0),
  179. .resetn_reg = EXYNOS5433_SYSREG_DISP_MIPI_PHY,
  180. .resetn_map = EXYNOS_MIPI_REGMAP_DISP,
  181. }, {
  182. /* EXYNOS_MIPI_PHY_ID_CSIS1 */
  183. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_NONE,
  184. .enable_val = EXYNOS4_PHY_ENABLE,
  185. .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(1),
  186. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  187. .resetn_val = BIT(1),
  188. .resetn_reg = EXYNOS5433_SYSREG_CAM0_MIPI_DPHY_CON,
  189. .resetn_map = EXYNOS_MIPI_REGMAP_CAM0,
  190. }, {
  191. /* EXYNOS_MIPI_PHY_ID_DSIM1 */
  192. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_NONE,
  193. .enable_val = EXYNOS4_PHY_ENABLE,
  194. .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(1),
  195. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  196. .resetn_val = BIT(1),
  197. .resetn_reg = EXYNOS5433_SYSREG_DISP_MIPI_PHY,
  198. .resetn_map = EXYNOS_MIPI_REGMAP_DISP,
  199. }, {
  200. /* EXYNOS_MIPI_PHY_ID_CSIS2 */
  201. .coupled_phy_id = EXYNOS_MIPI_PHY_ID_NONE,
  202. .enable_val = EXYNOS4_PHY_ENABLE,
  203. .enable_reg = EXYNOS4_MIPI_PHY_CONTROL(2),
  204. .enable_map = EXYNOS_MIPI_REGMAP_PMU,
  205. .resetn_val = BIT(0),
  206. .resetn_reg = EXYNOS5433_SYSREG_CAM1_MIPI_DPHY_CON,
  207. .resetn_map = EXYNOS_MIPI_REGMAP_CAM1,
  208. },
  209. },
  210. };
  211. struct exynos_mipi_video_phy {
  212. struct regmap *regmaps[EXYNOS_MIPI_REGMAPS_NUM];
  213. int num_phys;
  214. struct video_phy_desc {
  215. struct phy *phy;
  216. unsigned int index;
  217. const struct exynos_mipi_phy_desc *data;
  218. } phys[EXYNOS_MIPI_PHYS_NUM];
  219. spinlock_t slock;
  220. };
  221. static int __set_phy_state(const struct exynos_mipi_phy_desc *data,
  222. struct exynos_mipi_video_phy *state, unsigned int on)
  223. {
  224. u32 val;
  225. spin_lock(&state->slock);
  226. /* disable in PMU sysreg */
  227. if (!on && data->coupled_phy_id >= 0 &&
  228. state->phys[data->coupled_phy_id].phy->power_count == 0) {
  229. regmap_read(state->regmaps[data->enable_map], data->enable_reg,
  230. &val);
  231. val &= ~data->enable_val;
  232. regmap_write(state->regmaps[data->enable_map], data->enable_reg,
  233. val);
  234. }
  235. /* PHY reset */
  236. regmap_read(state->regmaps[data->resetn_map], data->resetn_reg, &val);
  237. val = on ? (val | data->resetn_val) : (val & ~data->resetn_val);
  238. regmap_write(state->regmaps[data->resetn_map], data->resetn_reg, val);
  239. /* enable in PMU sysreg */
  240. if (on) {
  241. regmap_read(state->regmaps[data->enable_map], data->enable_reg,
  242. &val);
  243. val |= data->enable_val;
  244. regmap_write(state->regmaps[data->enable_map], data->enable_reg,
  245. val);
  246. }
  247. spin_unlock(&state->slock);
  248. return 0;
  249. }
  250. #define to_mipi_video_phy(desc) \
  251. container_of((desc), struct exynos_mipi_video_phy, phys[(desc)->index])
  252. static int exynos_mipi_video_phy_power_on(struct phy *phy)
  253. {
  254. struct video_phy_desc *phy_desc = phy_get_drvdata(phy);
  255. struct exynos_mipi_video_phy *state = to_mipi_video_phy(phy_desc);
  256. return __set_phy_state(phy_desc->data, state, 1);
  257. }
  258. static int exynos_mipi_video_phy_power_off(struct phy *phy)
  259. {
  260. struct video_phy_desc *phy_desc = phy_get_drvdata(phy);
  261. struct exynos_mipi_video_phy *state = to_mipi_video_phy(phy_desc);
  262. return __set_phy_state(phy_desc->data, state, 0);
  263. }
  264. static struct phy *exynos_mipi_video_phy_xlate(struct device *dev,
  265. struct of_phandle_args *args)
  266. {
  267. struct exynos_mipi_video_phy *state = dev_get_drvdata(dev);
  268. if (WARN_ON(args->args[0] >= state->num_phys))
  269. return ERR_PTR(-ENODEV);
  270. return state->phys[args->args[0]].phy;
  271. }
  272. static const struct phy_ops exynos_mipi_video_phy_ops = {
  273. .power_on = exynos_mipi_video_phy_power_on,
  274. .power_off = exynos_mipi_video_phy_power_off,
  275. .owner = THIS_MODULE,
  276. };
  277. static int exynos_mipi_video_phy_probe(struct platform_device *pdev)
  278. {
  279. const struct mipi_phy_device_desc *phy_dev;
  280. struct exynos_mipi_video_phy *state;
  281. struct device *dev = &pdev->dev;
  282. struct device_node *np = dev->of_node;
  283. struct phy_provider *phy_provider;
  284. unsigned int i;
  285. phy_dev = of_device_get_match_data(dev);
  286. if (!phy_dev)
  287. return -ENODEV;
  288. state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
  289. if (!state)
  290. return -ENOMEM;
  291. for (i = 0; i < phy_dev->num_regmaps; i++) {
  292. state->regmaps[i] = syscon_regmap_lookup_by_phandle(np,
  293. phy_dev->regmap_names[i]);
  294. if (IS_ERR(state->regmaps[i]))
  295. return PTR_ERR(state->regmaps[i]);
  296. }
  297. state->num_phys = phy_dev->num_phys;
  298. spin_lock_init(&state->slock);
  299. dev_set_drvdata(dev, state);
  300. for (i = 0; i < state->num_phys; i++) {
  301. struct phy *phy = devm_phy_create(dev, NULL,
  302. &exynos_mipi_video_phy_ops);
  303. if (IS_ERR(phy)) {
  304. dev_err(dev, "failed to create PHY %d\n", i);
  305. return PTR_ERR(phy);
  306. }
  307. state->phys[i].phy = phy;
  308. state->phys[i].index = i;
  309. state->phys[i].data = &phy_dev->phys[i];
  310. phy_set_drvdata(phy, &state->phys[i]);
  311. }
  312. phy_provider = devm_of_phy_provider_register(dev,
  313. exynos_mipi_video_phy_xlate);
  314. return PTR_ERR_OR_ZERO(phy_provider);
  315. }
  316. static const struct of_device_id exynos_mipi_video_phy_of_match[] = {
  317. {
  318. .compatible = "samsung,s5pv210-mipi-video-phy",
  319. .data = &s5pv210_mipi_phy,
  320. }, {
  321. .compatible = "samsung,exynos5420-mipi-video-phy",
  322. .data = &exynos5420_mipi_phy,
  323. }, {
  324. .compatible = "samsung,exynos5433-mipi-video-phy",
  325. .data = &exynos5433_mipi_phy,
  326. },
  327. { /* sentinel */ },
  328. };
  329. MODULE_DEVICE_TABLE(of, exynos_mipi_video_phy_of_match);
  330. static struct platform_driver exynos_mipi_video_phy_driver = {
  331. .probe = exynos_mipi_video_phy_probe,
  332. .driver = {
  333. .of_match_table = exynos_mipi_video_phy_of_match,
  334. .name = "exynos-mipi-video-phy",
  335. }
  336. };
  337. module_platform_driver(exynos_mipi_video_phy_driver);
  338. MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC MIPI CSI-2/DSI PHY driver");
  339. MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
  340. MODULE_LICENSE("GPL v2");