vc4_dpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. void __iomem *regs;
  88. struct clk *pixel_clock;
  89. struct clk *core_clock;
  90. };
  91. #define DPI_READ(offset) readl(dpi->regs + (offset))
  92. #define DPI_WRITE(offset, val) writel(val, dpi->regs + (offset))
  93. /* VC4 DPI encoder KMS struct */
  94. struct vc4_dpi_encoder {
  95. struct vc4_encoder base;
  96. struct vc4_dpi *dpi;
  97. };
  98. static inline struct vc4_dpi_encoder *
  99. to_vc4_dpi_encoder(struct drm_encoder *encoder)
  100. {
  101. return container_of(encoder, struct vc4_dpi_encoder, base.base);
  102. }
  103. #define DPI_REG(reg) { reg, #reg }
  104. static const struct {
  105. u32 reg;
  106. const char *name;
  107. } dpi_regs[] = {
  108. DPI_REG(DPI_C),
  109. DPI_REG(DPI_ID),
  110. };
  111. #ifdef CONFIG_DEBUG_FS
  112. int vc4_dpi_debugfs_regs(struct seq_file *m, void *unused)
  113. {
  114. struct drm_info_node *node = (struct drm_info_node *)m->private;
  115. struct drm_device *dev = node->minor->dev;
  116. struct vc4_dev *vc4 = to_vc4_dev(dev);
  117. struct vc4_dpi *dpi = vc4->dpi;
  118. int i;
  119. if (!dpi)
  120. return 0;
  121. for (i = 0; i < ARRAY_SIZE(dpi_regs); i++) {
  122. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  123. dpi_regs[i].name, dpi_regs[i].reg,
  124. DPI_READ(dpi_regs[i].reg));
  125. }
  126. return 0;
  127. }
  128. #endif
  129. static const struct drm_encoder_funcs vc4_dpi_encoder_funcs = {
  130. .destroy = drm_encoder_cleanup,
  131. };
  132. static void vc4_dpi_encoder_disable(struct drm_encoder *encoder)
  133. {
  134. struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
  135. struct vc4_dpi *dpi = vc4_encoder->dpi;
  136. clk_disable_unprepare(dpi->pixel_clock);
  137. }
  138. static void vc4_dpi_encoder_enable(struct drm_encoder *encoder)
  139. {
  140. struct drm_device *dev = encoder->dev;
  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. struct drm_connector_list_iter conn_iter;
  145. struct drm_connector *connector = NULL, *connector_scan;
  146. u32 dpi_c = DPI_ENABLE | DPI_OUTPUT_ENABLE_MODE;
  147. int ret;
  148. /* Look up the connector attached to DPI so we can get the
  149. * bus_format. Ideally the bridge would tell us the
  150. * bus_format we want, but it doesn't yet, so assume that it's
  151. * uniform throughout the bridge chain.
  152. */
  153. drm_connector_list_iter_begin(dev, &conn_iter);
  154. drm_for_each_connector_iter(connector_scan, &conn_iter) {
  155. if (connector_scan->encoder == encoder) {
  156. connector = connector_scan;
  157. break;
  158. }
  159. }
  160. drm_connector_list_iter_end(&conn_iter);
  161. if (connector && connector->display_info.num_bus_formats) {
  162. u32 bus_format = connector->display_info.bus_formats[0];
  163. switch (bus_format) {
  164. case MEDIA_BUS_FMT_RGB888_1X24:
  165. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
  166. DPI_FORMAT);
  167. break;
  168. case MEDIA_BUS_FMT_BGR888_1X24:
  169. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
  170. DPI_FORMAT);
  171. dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR, DPI_ORDER);
  172. break;
  173. case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
  174. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2,
  175. DPI_FORMAT);
  176. break;
  177. case MEDIA_BUS_FMT_RGB666_1X18:
  178. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
  179. DPI_FORMAT);
  180. break;
  181. case MEDIA_BUS_FMT_RGB565_1X16:
  182. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_3,
  183. DPI_FORMAT);
  184. break;
  185. default:
  186. DRM_ERROR("Unknown media bus format %d\n", bus_format);
  187. break;
  188. }
  189. } else {
  190. /* Default to 24bit if no connector found. */
  191. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB, DPI_FORMAT);
  192. }
  193. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  194. dpi_c |= DPI_HSYNC_INVERT;
  195. else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
  196. dpi_c |= DPI_HSYNC_DISABLE;
  197. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  198. dpi_c |= DPI_VSYNC_INVERT;
  199. else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
  200. dpi_c |= DPI_VSYNC_DISABLE;
  201. DPI_WRITE(DPI_C, dpi_c);
  202. ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000);
  203. if (ret)
  204. DRM_ERROR("Failed to set clock rate: %d\n", ret);
  205. ret = clk_prepare_enable(dpi->pixel_clock);
  206. if (ret)
  207. DRM_ERROR("Failed to set clock rate: %d\n", ret);
  208. }
  209. static enum drm_mode_status vc4_dpi_encoder_mode_valid(struct drm_encoder *encoder,
  210. const struct drm_display_mode *mode)
  211. {
  212. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  213. return MODE_NO_INTERLACE;
  214. return MODE_OK;
  215. }
  216. static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = {
  217. .disable = vc4_dpi_encoder_disable,
  218. .enable = vc4_dpi_encoder_enable,
  219. .mode_valid = vc4_dpi_encoder_mode_valid,
  220. };
  221. static const struct of_device_id vc4_dpi_dt_match[] = {
  222. { .compatible = "brcm,bcm2835-dpi", .data = NULL },
  223. {}
  224. };
  225. /* Sets up the next link in the display chain, whether it's a panel or
  226. * a bridge.
  227. */
  228. static int vc4_dpi_init_bridge(struct vc4_dpi *dpi)
  229. {
  230. struct device *dev = &dpi->pdev->dev;
  231. struct drm_panel *panel;
  232. struct drm_bridge *bridge;
  233. int ret;
  234. ret = drm_of_find_panel_or_bridge(dev->of_node, 0, 0,
  235. &panel, &bridge);
  236. if (ret) {
  237. /* If nothing was connected in the DT, that's not an
  238. * error.
  239. */
  240. if (ret == -ENODEV)
  241. return 0;
  242. else
  243. return ret;
  244. }
  245. if (panel)
  246. bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_DPI);
  247. return drm_bridge_attach(dpi->encoder, bridge, NULL);
  248. }
  249. static int vc4_dpi_bind(struct device *dev, struct device *master, void *data)
  250. {
  251. struct platform_device *pdev = to_platform_device(dev);
  252. struct drm_device *drm = dev_get_drvdata(master);
  253. struct vc4_dev *vc4 = to_vc4_dev(drm);
  254. struct vc4_dpi *dpi;
  255. struct vc4_dpi_encoder *vc4_dpi_encoder;
  256. int ret;
  257. dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL);
  258. if (!dpi)
  259. return -ENOMEM;
  260. vc4_dpi_encoder = devm_kzalloc(dev, sizeof(*vc4_dpi_encoder),
  261. GFP_KERNEL);
  262. if (!vc4_dpi_encoder)
  263. return -ENOMEM;
  264. vc4_dpi_encoder->base.type = VC4_ENCODER_TYPE_DPI;
  265. vc4_dpi_encoder->dpi = dpi;
  266. dpi->encoder = &vc4_dpi_encoder->base.base;
  267. dpi->pdev = pdev;
  268. dpi->regs = vc4_ioremap_regs(pdev, 0);
  269. if (IS_ERR(dpi->regs))
  270. return PTR_ERR(dpi->regs);
  271. if (DPI_READ(DPI_ID) != DPI_ID_VALUE) {
  272. dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n",
  273. DPI_READ(DPI_ID), DPI_ID_VALUE);
  274. return -ENODEV;
  275. }
  276. dpi->core_clock = devm_clk_get(dev, "core");
  277. if (IS_ERR(dpi->core_clock)) {
  278. ret = PTR_ERR(dpi->core_clock);
  279. if (ret != -EPROBE_DEFER)
  280. DRM_ERROR("Failed to get core clock: %d\n", ret);
  281. return ret;
  282. }
  283. dpi->pixel_clock = devm_clk_get(dev, "pixel");
  284. if (IS_ERR(dpi->pixel_clock)) {
  285. ret = PTR_ERR(dpi->pixel_clock);
  286. if (ret != -EPROBE_DEFER)
  287. DRM_ERROR("Failed to get pixel clock: %d\n", ret);
  288. return ret;
  289. }
  290. ret = clk_prepare_enable(dpi->core_clock);
  291. if (ret)
  292. DRM_ERROR("Failed to turn on core clock: %d\n", ret);
  293. drm_encoder_init(drm, dpi->encoder, &vc4_dpi_encoder_funcs,
  294. DRM_MODE_ENCODER_DPI, NULL);
  295. drm_encoder_helper_add(dpi->encoder, &vc4_dpi_encoder_helper_funcs);
  296. ret = vc4_dpi_init_bridge(dpi);
  297. if (ret)
  298. goto err_destroy_encoder;
  299. dev_set_drvdata(dev, dpi);
  300. vc4->dpi = dpi;
  301. return 0;
  302. err_destroy_encoder:
  303. drm_encoder_cleanup(dpi->encoder);
  304. clk_disable_unprepare(dpi->core_clock);
  305. return ret;
  306. }
  307. static void vc4_dpi_unbind(struct device *dev, struct device *master,
  308. void *data)
  309. {
  310. struct drm_device *drm = dev_get_drvdata(master);
  311. struct vc4_dev *vc4 = to_vc4_dev(drm);
  312. struct vc4_dpi *dpi = dev_get_drvdata(dev);
  313. drm_of_panel_bridge_remove(dev->of_node, 0, 0);
  314. drm_encoder_cleanup(dpi->encoder);
  315. clk_disable_unprepare(dpi->core_clock);
  316. vc4->dpi = NULL;
  317. }
  318. static const struct component_ops vc4_dpi_ops = {
  319. .bind = vc4_dpi_bind,
  320. .unbind = vc4_dpi_unbind,
  321. };
  322. static int vc4_dpi_dev_probe(struct platform_device *pdev)
  323. {
  324. return component_add(&pdev->dev, &vc4_dpi_ops);
  325. }
  326. static int vc4_dpi_dev_remove(struct platform_device *pdev)
  327. {
  328. component_del(&pdev->dev, &vc4_dpi_ops);
  329. return 0;
  330. }
  331. struct platform_driver vc4_dpi_driver = {
  332. .probe = vc4_dpi_dev_probe,
  333. .remove = vc4_dpi_dev_remove,
  334. .driver = {
  335. .name = "vc4_dpi",
  336. .of_match_table = vc4_dpi_dt_match,
  337. },
  338. };