dw-csi-plat.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018-2019 Synopsys, Inc. and/or its affiliates.
  4. *
  5. * Synopsys DesignWare MIPI CSI-2 Host controller driver.
  6. * Platform driver
  7. *
  8. * Author: Luis Oliveira <luis.oliveira@synopsys.com>
  9. */
  10. #include "dw-csi-data.h"
  11. #include "dw-dphy-data.h"
  12. #include "dw-csi-plat.h"
  13. #include "bm_csi_hw.h"
  14. const struct mipi_dt csi_dt[] = {
  15. {
  16. .hex = CSI_2_YUV420_8,
  17. .name = "YUV420_8bits",
  18. }, {
  19. .hex = CSI_2_YUV420_10,
  20. .name = "YUV420_10bits",
  21. }, {
  22. .hex = CSI_2_YUV420_8_LEG,
  23. .name = "YUV420_8bits_LEGACY",
  24. }, {
  25. .hex = CSI_2_YUV420_8_SHIFT,
  26. .name = "YUV420_8bits_SHIFT",
  27. }, {
  28. .hex = CSI_2_YUV420_10_SHIFT,
  29. .name = "YUV420_10bits_SHIFT",
  30. }, {
  31. .hex = CSI_2_YUV422_8,
  32. .name = "YUV442_8bits",
  33. }, {
  34. .hex = CSI_2_YUV422_10,
  35. .name = "YUV442_10bits",
  36. }, {
  37. .hex = CSI_2_RGB444,
  38. .name = "RGB444",
  39. }, {
  40. .hex = CSI_2_RGB555,
  41. .name = "RGB555",
  42. }, {
  43. .hex = CSI_2_RGB565,
  44. .name = "RGB565",
  45. }, {
  46. .hex = CSI_2_RGB666,
  47. .name = "RGB666",
  48. }, {
  49. .hex = CSI_2_RGB888,
  50. .name = "RGB888",
  51. }, {
  52. .hex = CSI_2_RAW6,
  53. .name = "RAW6",
  54. }, {
  55. .hex = CSI_2_RAW7,
  56. .name = "RAW7",
  57. }, {
  58. .hex = CSI_2_RAW8,
  59. .name = "RAW8",
  60. }, {
  61. .hex = CSI_2_RAW10,
  62. .name = "RAW10",
  63. }, {
  64. .hex = CSI_2_RAW12,
  65. .name = "RAW12",
  66. }, {
  67. .hex = CSI_2_RAW14,
  68. .name = "RAW14",
  69. }, {
  70. .hex = CSI_2_RAW16,
  71. .name = "RAW16",
  72. },
  73. };
  74. static struct mipi_fmt *
  75. find_dw_mipi_csi_format(uint32_t mbus_code)
  76. {
  77. unsigned int i;
  78. pr_info("%s entered mbus: 0x%x\n", __func__, mbus_code);
  79. for (i = 0; i < ARRAY_SIZE(dw_mipi_csi_formats); i++)
  80. if (mbus_code == dw_mipi_csi_formats[i].mbus_code) {
  81. pr_info("Found mbus 0x%x\n", dw_mipi_csi_formats[i].mbus_code);
  82. return &dw_mipi_csi_formats[i];
  83. }
  84. return NULL;
  85. }
  86. static int dw_mipi_csi_enum_mbus_code(int index, uint32_t *code)
  87. {
  88. if (index != 0)
  89. return -EINVAL;
  90. *code = dw_mipi_csi_formats[index].mbus_code;
  91. return 0;
  92. }
  93. static struct mipi_fmt *
  94. dw_mipi_csi_try_format(uint32_t mbus_code)
  95. {
  96. struct mipi_fmt *fmt;
  97. fmt = find_dw_mipi_csi_format(mbus_code);
  98. if (!fmt)
  99. fmt = &dw_mipi_csi_formats[0];
  100. return fmt;
  101. }
  102. struct mipi_fmt *
  103. dw_mipi_csi_get_format(struct dw_csi *dev)
  104. {
  105. dev_info(dev->dev,
  106. "%s got v4l2_mbus_pixelcode. 0x%x\n", __func__,
  107. dev->fmt->mbus_code);
  108. dev_info(dev->dev,
  109. "%s got width. 0x%x\n", __func__,
  110. dev->fmt->width);
  111. dev_info(dev->dev,
  112. "%s got height. 0x%x\n", __func__,
  113. dev->fmt->height);
  114. return dev->fmt;
  115. }
  116. static int
  117. dw_mipi_csi_set_fmt(struct platform_device *pdev,
  118. uint32_t mbus_code, uint32_t width, uint32_t height)
  119. {
  120. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  121. struct dw_csi *dev = &drvdata->csi_dev;
  122. struct mipi_fmt *dev_fmt = NULL;
  123. int i;
  124. dev_info(dev->dev,
  125. "%s got mbus_pixelcode. 0x%x\n", __func__,
  126. mbus_code);
  127. dev_fmt = dw_mipi_csi_try_format(mbus_code);
  128. dev_info(dev->dev,
  129. "%s got v4l2_mbus_pixelcode. 0x%x\n", __func__,
  130. dev_fmt->mbus_code);
  131. if (!dev_fmt)
  132. return -EINVAL;
  133. if (dev_fmt) {
  134. dev->fmt->mbus_code = dev_fmt->mbus_code;
  135. dev->fmt->width = width;
  136. dev->fmt->height = height;
  137. dw_mipi_csi_set_ipi_fmt(dev);
  138. }
  139. dev_info(dev->dev, "Width: %d, Height: %d of Demo\n", width, height);
  140. if (width > 0 && height > 0) {
  141. dw_mipi_csi_fill_timings(dev, width, height);
  142. /*
  143. demo_writel(dev,
  144. DEMO_ACTIVE_WIDTH, fmt->format.width);
  145. demo_writel(dev,
  146. DEMO_ACTIVE_HEIGHT, fmt->format.height);
  147. */
  148. dev_vdbg(dev->dev,
  149. "(core/demosaic) : width=%d/%d, height=%d/%d\n",
  150. dev->hw.htotal - (dev->hw.hbp
  151. + dev->hw.hsd
  152. + dev->hw.hsa),
  153. width,
  154. dev->hw.vactive,
  155. height);
  156. dev_info(dev->dev,
  157. "(core/demosaic) : width=%d/%d, height=%d/%d\n",
  158. dev->hw.htotal - (dev->hw.hbp
  159. + dev->hw.hsd
  160. + dev->hw.hsa),
  161. width,
  162. dev->hw.vactive,
  163. height);
  164. } else {
  165. dev_vdbg(dev->dev, "%s unacceptable values 0x%x.\n",
  166. __func__, width);
  167. dev_vdbg(dev->dev, "%s unacceptable values 0x%x.\n",
  168. __func__, height);
  169. dev_info(dev->dev, "%s unacceptable values 0x%x.\n",
  170. __func__, width);
  171. dev_info(dev->dev, "%s unacceptable values 0x%x.\n",
  172. __func__, height);
  173. return -EINVAL;
  174. }
  175. for (i = 0; i < ARRAY_SIZE(csi_dt); i++)
  176. if (csi_dt[i].hex == dev->ipi_dt) {
  177. dev_vdbg(dev->dev, "Using data type %s\n",
  178. csi_dt[i].name);
  179. dev_info(dev->dev, "Using data type %s\n",
  180. csi_dt[i].name);
  181. }
  182. return 0;
  183. }
  184. static int
  185. dw_mipi_csi_get_fmt(struct platform_device *pdev,
  186. struct mipi_fmt *fmt)
  187. {
  188. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  189. struct dw_csi *dev = &drvdata->csi_dev;
  190. struct mipi_fmt *mf = dw_mipi_csi_get_format(dev);
  191. if (!mf)
  192. return -EINVAL;
  193. mutex_lock(&dev->lock);
  194. *fmt = *mf;
  195. mutex_unlock(&dev->lock);
  196. return 0;
  197. }
  198. int dw_mipi_csi_s_power(struct dw_csi *dev, int on)
  199. {
  200. dev_info(dev->dev, "%s: on=%d\n", __func__, on);
  201. if (on) {
  202. dw_mipi_csi_hw_stdby(dev);
  203. dw_mipi_csi_start(dev);
  204. } else {
  205. phy_power_off(dev->phy);
  206. dw_mipi_csi_mask_irq_power_off(dev);
  207. /* reset data type */
  208. dev->ipi_dt = 0x0;
  209. }
  210. return 0;
  211. }
  212. int dw_mipi_csi_log_status(struct dw_csi *dev)
  213. {
  214. dw_mipi_csi_dump(dev);
  215. return 0;
  216. }
  217. #if IS_ENABLED(CONFIG_VIDEO_ADV_DEBUG)
  218. int
  219. dw_mipi_csi_g_register(struct v4l2_subdev *sd, uint32_t reg)
  220. {
  221. struct bm_csi_drvdata *drvdata = platform_get_drvdata(pdev);
  222. struct dw_csi *dev = &drvdata->csi_dev;
  223. dev_vdbg(dev->dev, "%s: reg=%llu\n", __func__, reg);
  224. reg->val = dw_mipi_csi_read(dev, reg);
  225. return 0;
  226. }
  227. #endif
  228. #if 0
  229. static int dw_mipi_csi_init_cfg(struct v4l2_subdev *sd,
  230. struct v4l2_subdev_pad_config *cfg)
  231. {
  232. struct v4l2_mbus_framefmt *format =
  233. v4l2_subdev_get_try_format(sd, cfg, 0);
  234. format->colorspace = V4L2_COLORSPACE_SRGB;
  235. format->code = MEDIA_BUS_FMT_RGB888_1X24;
  236. format->field = V4L2_FIELD_NONE;
  237. return 0;
  238. }
  239. #endif
  240. static irqreturn_t dw_mipi_csi_irq1(int irq, void *dev_id)
  241. {
  242. struct dw_csi *csi_dev = dev_id;
  243. dw_mipi_csi_irq_handler(csi_dev);
  244. return IRQ_HANDLED;
  245. }
  246. void dw_csi_soc_reset(void __iomem *io_mem)
  247. {
  248. #define VISYS_SW_RST 0x100
  249. uint32_t reg_val = 0;
  250. reg_val = readl(io_mem + VISYS_SW_RST);
  251. reg_val &= ~(1 << 16);
  252. writel(reg_val, io_mem + VISYS_SW_RST);
  253. reg_val |= 1 << 16;
  254. writel(reg_val, io_mem + VISYS_SW_RST);
  255. }
  256. static const struct of_device_id dw_mipi_csi_of_match[];
  257. int dw_csi_probe(struct platform_device *pdev)
  258. {
  259. struct device *dev = &pdev->dev;
  260. struct dw_csi *csi;
  261. struct dw_dphy_rx *dphy;
  262. struct dw_csih_pdata *pdata;
  263. struct bm_csi_drvdata *drvdata;
  264. int ret;
  265. if (!IS_ENABLED(CONFIG_OF))
  266. dev_vdbg(dev, "Probing started\n");
  267. /* Resource allocation */
  268. drvdata = platform_get_drvdata(pdev);
  269. csi = &drvdata->csi_dev;
  270. dphy = drvdata->dphy;
  271. pdata = &drvdata->csi_pdata;
  272. mutex_init(&csi->lock);
  273. spin_lock_init(&csi->slock);
  274. csi->dev = dev;
  275. /*set csi phy*/
  276. csi->phy = dphy->phy;
  277. csi->base_address = drvdata->base;
  278. if (IS_ERR(csi->base_address)) {
  279. dev_err(dev, "base address not set.\n");
  280. return PTR_ERR(csi->base_address);
  281. }
  282. csi->ctrl_irq_number = drvdata->irq_num;
  283. if (csi->ctrl_irq_number < 0) {
  284. dev_err(dev, "irq number %d not set.\n", csi->ctrl_irq_number);
  285. ret = csi->ctrl_irq_number;
  286. goto end;
  287. }
  288. ret = devm_request_irq(dev, csi->ctrl_irq_number,
  289. dw_mipi_csi_irq1, IRQF_SHARED,
  290. dev_name(dev), csi);
  291. if (ret) {
  292. dev_err(dev, "irq csi %d failed\n", pdata->id);
  293. goto end;
  294. }
  295. csi->fmt = &dw_mipi_csi_formats[0];
  296. csi->fmt->mbus_code = dw_mipi_csi_formats[0].mbus_code;
  297. /*
  298. to do pad init
  299. */
  300. csi->hw.num_lanes = pdata->lanes;
  301. csi->hw.pclk = pdata->pclk;
  302. csi->hw.fps = pdata->fps;
  303. csi->hw.dphy_freq = pdata->hs_freq;
  304. //csi soc reset
  305. dw_csi_soc_reset(drvdata->reset);
  306. #if IS_ENABLED(CONFIG_DWC_MIPI_TC_DPHY_GEN3)
  307. dw_csi_create_capabilities_sysfs(pdev);
  308. #endif
  309. dw_mipi_csi_get_version(csi);
  310. dw_mipi_csi_specific_mappings(csi);
  311. dw_mipi_csi_mask_irq_power_off(csi);
  312. dev_info(dev, "DW MIPI CSI-2 Host registered successfully HW v%u.%u\n",
  313. csi->hw_version_major, csi->hw_version_minor);
  314. ret = phy_init(csi->phy);
  315. if (ret) {
  316. dev_err(&csi->phy->dev, "phy init failed --> %d\n", ret);
  317. goto end;
  318. }
  319. return 0;
  320. end:
  321. return ret;
  322. }
  323. int dw_csi_remove(struct platform_device *pdev)
  324. {
  325. struct bm_csi_drvdata *drvdata;
  326. drvdata = platform_get_drvdata(pdev);
  327. dw_csi_remove_capabilities_sysfs(pdev);
  328. //struct dw_csi *mipi_csi = &drvdata->csi_dev;
  329. //csi soc reset
  330. dw_csi_soc_reset(drvdata->reset);
  331. dev_info(&pdev->dev, "DW MIPI CSI-2 Host module removed\n");
  332. return 0;
  333. }
  334. MODULE_LICENSE("GPL v2");
  335. MODULE_AUTHOR("Luis Oliveira <luis.oliveira@synopsys.com>");
  336. MODULE_DESCRIPTION("Synopsys DesignWare MIPI CSI-2 Host Platform driver");