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