vc4_dpi.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (C) 2016 Broadcom Limited
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /**
  17. * DOC: VC4 DPI module
  18. *
  19. * The VC4 DPI hardware supports MIPI DPI type 4 and Nokia ViSSI
  20. * signals. On BCM2835, these can be routed out to GPIO0-27 with the
  21. * ALT2 function.
  22. */
  23. #include <drm/drm_atomic_helper.h>
  24. #include <drm/drm_bridge.h>
  25. #include <drm/drm_crtc_helper.h>
  26. #include <drm/drm_edid.h>
  27. #include <drm/drm_of.h>
  28. #include <drm/drm_panel.h>
  29. #include <linux/clk.h>
  30. #include <linux/component.h>
  31. #include <linux/of_graph.h>
  32. #include <linux/of_platform.h>
  33. #include "vc4_drv.h"
  34. #include "vc4_regs.h"
  35. #define DPI_C 0x00
  36. # define DPI_OUTPUT_ENABLE_MODE BIT(16)
  37. /* The order field takes the incoming 24 bit RGB from the pixel valve
  38. * and shuffles the 3 channels.
  39. */
  40. # define DPI_ORDER_MASK VC4_MASK(15, 14)
  41. # define DPI_ORDER_SHIFT 14
  42. # define DPI_ORDER_RGB 0
  43. # define DPI_ORDER_BGR 1
  44. # define DPI_ORDER_GRB 2
  45. # define DPI_ORDER_BRG 3
  46. /* The format field takes the ORDER-shuffled pixel valve data and
  47. * formats it onto the output lines.
  48. */
  49. # define DPI_FORMAT_MASK VC4_MASK(13, 11)
  50. # define DPI_FORMAT_SHIFT 11
  51. /* This define is named in the hardware, but actually just outputs 0. */
  52. # define DPI_FORMAT_9BIT_666_RGB 0
  53. /* Outputs 00000000rrrrrggggggbbbbb */
  54. # define DPI_FORMAT_16BIT_565_RGB_1 1
  55. /* Outputs 000rrrrr00gggggg000bbbbb */
  56. # define DPI_FORMAT_16BIT_565_RGB_2 2
  57. /* Outputs 00rrrrr000gggggg00bbbbb0 */
  58. # define DPI_FORMAT_16BIT_565_RGB_3 3
  59. /* Outputs 000000rrrrrrggggggbbbbbb */
  60. # define DPI_FORMAT_18BIT_666_RGB_1 4
  61. /* Outputs 00rrrrrr00gggggg00bbbbbb */
  62. # define DPI_FORMAT_18BIT_666_RGB_2 5
  63. /* Outputs rrrrrrrrggggggggbbbbbbbb */
  64. # define DPI_FORMAT_24BIT_888_RGB 6
  65. /* Reverses the polarity of the corresponding signal */
  66. # define DPI_PIXEL_CLK_INVERT BIT(10)
  67. # define DPI_HSYNC_INVERT BIT(9)
  68. # define DPI_VSYNC_INVERT BIT(8)
  69. # define DPI_OUTPUT_ENABLE_INVERT BIT(7)
  70. /* Outputs the signal the falling clock edge instead of rising. */
  71. # define DPI_HSYNC_NEGATE BIT(6)
  72. # define DPI_VSYNC_NEGATE BIT(5)
  73. # define DPI_OUTPUT_ENABLE_NEGATE BIT(4)
  74. /* Disables the signal */
  75. # define DPI_HSYNC_DISABLE BIT(3)
  76. # define DPI_VSYNC_DISABLE BIT(2)
  77. # define DPI_OUTPUT_ENABLE_DISABLE BIT(1)
  78. /* Power gate to the device, full reset at 0 -> 1 transition */
  79. # define DPI_ENABLE BIT(0)
  80. /* All other registers besides DPI_C return the ID */
  81. #define DPI_ID 0x04
  82. # define DPI_ID_VALUE 0x00647069
  83. /* General DPI hardware state. */
  84. struct vc4_dpi {
  85. struct platform_device *pdev;
  86. struct drm_encoder *encoder;
  87. struct drm_connector *connector;
  88. void __iomem *regs;
  89. struct clk *pixel_clock;
  90. struct clk *core_clock;
  91. };
  92. #define DPI_READ(offset) readl(dpi->regs + (offset))
  93. #define DPI_WRITE(offset, val) writel(val, dpi->regs + (offset))
  94. /* VC4 DPI encoder KMS struct */
  95. struct vc4_dpi_encoder {
  96. struct vc4_encoder base;
  97. struct vc4_dpi *dpi;
  98. };
  99. static inline struct vc4_dpi_encoder *
  100. to_vc4_dpi_encoder(struct drm_encoder *encoder)
  101. {
  102. return container_of(encoder, struct vc4_dpi_encoder, base.base);
  103. }
  104. #define DPI_REG(reg) { reg, #reg }
  105. static const struct {
  106. u32 reg;
  107. const char *name;
  108. } dpi_regs[] = {
  109. DPI_REG(DPI_C),
  110. DPI_REG(DPI_ID),
  111. };
  112. #ifdef CONFIG_DEBUG_FS
  113. int vc4_dpi_debugfs_regs(struct seq_file *m, void *unused)
  114. {
  115. struct drm_info_node *node = (struct drm_info_node *)m->private;
  116. struct drm_device *dev = node->minor->dev;
  117. struct vc4_dev *vc4 = to_vc4_dev(dev);
  118. struct vc4_dpi *dpi = vc4->dpi;
  119. int i;
  120. if (!dpi)
  121. return 0;
  122. for (i = 0; i < ARRAY_SIZE(dpi_regs); i++) {
  123. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  124. dpi_regs[i].name, dpi_regs[i].reg,
  125. DPI_READ(dpi_regs[i].reg));
  126. }
  127. return 0;
  128. }
  129. #endif
  130. static const struct drm_encoder_funcs vc4_dpi_encoder_funcs = {
  131. .destroy = drm_encoder_cleanup,
  132. };
  133. static void vc4_dpi_encoder_disable(struct drm_encoder *encoder)
  134. {
  135. struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
  136. struct vc4_dpi *dpi = vc4_encoder->dpi;
  137. clk_disable_unprepare(dpi->pixel_clock);
  138. }
  139. static void vc4_dpi_encoder_enable(struct drm_encoder *encoder)
  140. {
  141. struct drm_display_mode *mode = &encoder->crtc->mode;
  142. struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
  143. struct vc4_dpi *dpi = vc4_encoder->dpi;
  144. u32 dpi_c = DPI_ENABLE | DPI_OUTPUT_ENABLE_MODE;
  145. int ret;
  146. if (dpi->connector->display_info.num_bus_formats) {
  147. u32 bus_format = dpi->connector->display_info.bus_formats[0];
  148. switch (bus_format) {
  149. case MEDIA_BUS_FMT_RGB888_1X24:
  150. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
  151. DPI_FORMAT);
  152. break;
  153. case MEDIA_BUS_FMT_BGR888_1X24:
  154. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
  155. DPI_FORMAT);
  156. dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR, DPI_ORDER);
  157. break;
  158. case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
  159. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2,
  160. DPI_FORMAT);
  161. break;
  162. case MEDIA_BUS_FMT_RGB666_1X18:
  163. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
  164. DPI_FORMAT);
  165. break;
  166. case MEDIA_BUS_FMT_RGB565_1X16:
  167. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_3,
  168. DPI_FORMAT);
  169. break;
  170. default:
  171. DRM_ERROR("Unknown media bus format %d\n", bus_format);
  172. break;
  173. }
  174. }
  175. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  176. dpi_c |= DPI_HSYNC_INVERT;
  177. else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
  178. dpi_c |= DPI_HSYNC_DISABLE;
  179. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  180. dpi_c |= DPI_VSYNC_INVERT;
  181. else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
  182. dpi_c |= DPI_VSYNC_DISABLE;
  183. DPI_WRITE(DPI_C, dpi_c);
  184. ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000);
  185. if (ret)
  186. DRM_ERROR("Failed to set clock rate: %d\n", ret);
  187. ret = clk_prepare_enable(dpi->pixel_clock);
  188. if (ret)
  189. DRM_ERROR("Failed to set clock rate: %d\n", ret);
  190. }
  191. static enum drm_mode_status vc4_dpi_encoder_mode_valid(struct drm_encoder *encoder,
  192. const struct drm_display_mode *mode)
  193. {
  194. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  195. return MODE_NO_INTERLACE;
  196. return MODE_OK;
  197. }
  198. static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = {
  199. .disable = vc4_dpi_encoder_disable,
  200. .enable = vc4_dpi_encoder_enable,
  201. .mode_valid = vc4_dpi_encoder_mode_valid,
  202. };
  203. static const struct of_device_id vc4_dpi_dt_match[] = {
  204. { .compatible = "brcm,bcm2835-dpi", .data = NULL },
  205. {}
  206. };
  207. /* Sets up the next link in the display chain, whether it's a panel or
  208. * a bridge.
  209. */
  210. static int vc4_dpi_init_bridge(struct vc4_dpi *dpi)
  211. {
  212. struct device *dev = &dpi->pdev->dev;
  213. struct drm_panel *panel;
  214. struct drm_bridge *bridge;
  215. int ret;
  216. ret = drm_of_find_panel_or_bridge(dev->of_node, 0, 0,
  217. &panel, &bridge);
  218. if (ret) {
  219. /* If nothing was connected in the DT, that's not an
  220. * error.
  221. */
  222. if (ret == -ENODEV)
  223. return 0;
  224. else
  225. return ret;
  226. }
  227. if (panel)
  228. bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_DPI);
  229. return drm_bridge_attach(dpi->encoder, bridge, NULL);
  230. }
  231. static int vc4_dpi_bind(struct device *dev, struct device *master, void *data)
  232. {
  233. struct platform_device *pdev = to_platform_device(dev);
  234. struct drm_device *drm = dev_get_drvdata(master);
  235. struct vc4_dev *vc4 = to_vc4_dev(drm);
  236. struct vc4_dpi *dpi;
  237. struct vc4_dpi_encoder *vc4_dpi_encoder;
  238. int ret;
  239. dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL);
  240. if (!dpi)
  241. return -ENOMEM;
  242. vc4_dpi_encoder = devm_kzalloc(dev, sizeof(*vc4_dpi_encoder),
  243. GFP_KERNEL);
  244. if (!vc4_dpi_encoder)
  245. return -ENOMEM;
  246. vc4_dpi_encoder->base.type = VC4_ENCODER_TYPE_DPI;
  247. vc4_dpi_encoder->dpi = dpi;
  248. dpi->encoder = &vc4_dpi_encoder->base.base;
  249. dpi->pdev = pdev;
  250. dpi->regs = vc4_ioremap_regs(pdev, 0);
  251. if (IS_ERR(dpi->regs))
  252. return PTR_ERR(dpi->regs);
  253. if (DPI_READ(DPI_ID) != DPI_ID_VALUE) {
  254. dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n",
  255. DPI_READ(DPI_ID), DPI_ID_VALUE);
  256. return -ENODEV;
  257. }
  258. dpi->core_clock = devm_clk_get(dev, "core");
  259. if (IS_ERR(dpi->core_clock)) {
  260. ret = PTR_ERR(dpi->core_clock);
  261. if (ret != -EPROBE_DEFER)
  262. DRM_ERROR("Failed to get core clock: %d\n", ret);
  263. return ret;
  264. }
  265. dpi->pixel_clock = devm_clk_get(dev, "pixel");
  266. if (IS_ERR(dpi->pixel_clock)) {
  267. ret = PTR_ERR(dpi->pixel_clock);
  268. if (ret != -EPROBE_DEFER)
  269. DRM_ERROR("Failed to get pixel clock: %d\n", ret);
  270. return ret;
  271. }
  272. ret = clk_prepare_enable(dpi->core_clock);
  273. if (ret)
  274. DRM_ERROR("Failed to turn on core clock: %d\n", ret);
  275. drm_encoder_init(drm, dpi->encoder, &vc4_dpi_encoder_funcs,
  276. DRM_MODE_ENCODER_DPI, NULL);
  277. drm_encoder_helper_add(dpi->encoder, &vc4_dpi_encoder_helper_funcs);
  278. ret = vc4_dpi_init_bridge(dpi);
  279. if (ret)
  280. goto err_destroy_encoder;
  281. dev_set_drvdata(dev, dpi);
  282. vc4->dpi = dpi;
  283. return 0;
  284. err_destroy_encoder:
  285. drm_encoder_cleanup(dpi->encoder);
  286. clk_disable_unprepare(dpi->core_clock);
  287. return ret;
  288. }
  289. static void vc4_dpi_unbind(struct device *dev, struct device *master,
  290. void *data)
  291. {
  292. struct drm_device *drm = dev_get_drvdata(master);
  293. struct vc4_dev *vc4 = to_vc4_dev(drm);
  294. struct vc4_dpi *dpi = dev_get_drvdata(dev);
  295. drm_of_panel_bridge_remove(dev->of_node, 0, 0);
  296. drm_encoder_cleanup(dpi->encoder);
  297. clk_disable_unprepare(dpi->core_clock);
  298. vc4->dpi = NULL;
  299. }
  300. static const struct component_ops vc4_dpi_ops = {
  301. .bind = vc4_dpi_bind,
  302. .unbind = vc4_dpi_unbind,
  303. };
  304. static int vc4_dpi_dev_probe(struct platform_device *pdev)
  305. {
  306. return component_add(&pdev->dev, &vc4_dpi_ops);
  307. }
  308. static int vc4_dpi_dev_remove(struct platform_device *pdev)
  309. {
  310. component_del(&pdev->dev, &vc4_dpi_ops);
  311. return 0;
  312. }
  313. struct platform_driver vc4_dpi_driver = {
  314. .probe = vc4_dpi_dev_probe,
  315. .remove = vc4_dpi_dev_remove,
  316. .driver = {
  317. .name = "vc4_dpi",
  318. .of_match_table = vc4_dpi_dt_match,
  319. },
  320. };