vc4_dpi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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_atomic_helper.h"
  24. #include "drm_crtc_helper.h"
  25. #include "drm_edid.h"
  26. #include "drm_panel.h"
  27. #include "linux/clk.h"
  28. #include "linux/component.h"
  29. #include "linux/of_graph.h"
  30. #include "linux/of_platform.h"
  31. #include "vc4_drv.h"
  32. #include "vc4_regs.h"
  33. #define DPI_C 0x00
  34. # define DPI_OUTPUT_ENABLE_MODE BIT(16)
  35. /* The order field takes the incoming 24 bit RGB from the pixel valve
  36. * and shuffles the 3 channels.
  37. */
  38. # define DPI_ORDER_MASK VC4_MASK(15, 14)
  39. # define DPI_ORDER_SHIFT 14
  40. # define DPI_ORDER_RGB 0
  41. # define DPI_ORDER_BGR 1
  42. # define DPI_ORDER_GRB 2
  43. # define DPI_ORDER_BRG 3
  44. /* The format field takes the ORDER-shuffled pixel valve data and
  45. * formats it onto the output lines.
  46. */
  47. # define DPI_FORMAT_MASK VC4_MASK(13, 11)
  48. # define DPI_FORMAT_SHIFT 11
  49. /* This define is named in the hardware, but actually just outputs 0. */
  50. # define DPI_FORMAT_9BIT_666_RGB 0
  51. /* Outputs 00000000rrrrrggggggbbbbb */
  52. # define DPI_FORMAT_16BIT_565_RGB_1 1
  53. /* Outputs 000rrrrr00gggggg000bbbbb */
  54. # define DPI_FORMAT_16BIT_565_RGB_2 2
  55. /* Outputs 00rrrrr000gggggg00bbbbb0 */
  56. # define DPI_FORMAT_16BIT_565_RGB_3 3
  57. /* Outputs 000000rrrrrrggggggbbbbbb */
  58. # define DPI_FORMAT_18BIT_666_RGB_1 4
  59. /* Outputs 00rrrrrr00gggggg00bbbbbb */
  60. # define DPI_FORMAT_18BIT_666_RGB_2 5
  61. /* Outputs rrrrrrrrggggggggbbbbbbbb */
  62. # define DPI_FORMAT_24BIT_888_RGB 6
  63. /* Reverses the polarity of the corresponding signal */
  64. # define DPI_PIXEL_CLK_INVERT BIT(10)
  65. # define DPI_HSYNC_INVERT BIT(9)
  66. # define DPI_VSYNC_INVERT BIT(8)
  67. # define DPI_OUTPUT_ENABLE_INVERT BIT(7)
  68. /* Outputs the signal the falling clock edge instead of rising. */
  69. # define DPI_HSYNC_NEGATE BIT(6)
  70. # define DPI_VSYNC_NEGATE BIT(5)
  71. # define DPI_OUTPUT_ENABLE_NEGATE BIT(4)
  72. /* Disables the signal */
  73. # define DPI_HSYNC_DISABLE BIT(3)
  74. # define DPI_VSYNC_DISABLE BIT(2)
  75. # define DPI_OUTPUT_ENABLE_DISABLE BIT(1)
  76. /* Power gate to the device, full reset at 0 -> 1 transition */
  77. # define DPI_ENABLE BIT(0)
  78. /* All other registers besides DPI_C return the ID */
  79. #define DPI_ID 0x04
  80. # define DPI_ID_VALUE 0x00647069
  81. /* General DPI hardware state. */
  82. struct vc4_dpi {
  83. struct platform_device *pdev;
  84. struct drm_encoder *encoder;
  85. struct drm_connector *connector;
  86. struct drm_panel *panel;
  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. /* VC4 DPI connector KMS struct */
  104. struct vc4_dpi_connector {
  105. struct drm_connector base;
  106. struct vc4_dpi *dpi;
  107. /* Since the connector is attached to just the one encoder,
  108. * this is the reference to it so we can do the best_encoder()
  109. * hook.
  110. */
  111. struct drm_encoder *encoder;
  112. };
  113. static inline struct vc4_dpi_connector *
  114. to_vc4_dpi_connector(struct drm_connector *connector)
  115. {
  116. return container_of(connector, struct vc4_dpi_connector, base);
  117. }
  118. #define DPI_REG(reg) { reg, #reg }
  119. static const struct {
  120. u32 reg;
  121. const char *name;
  122. } dpi_regs[] = {
  123. DPI_REG(DPI_C),
  124. DPI_REG(DPI_ID),
  125. };
  126. #ifdef CONFIG_DEBUG_FS
  127. int vc4_dpi_debugfs_regs(struct seq_file *m, void *unused)
  128. {
  129. struct drm_info_node *node = (struct drm_info_node *)m->private;
  130. struct drm_device *dev = node->minor->dev;
  131. struct vc4_dev *vc4 = to_vc4_dev(dev);
  132. struct vc4_dpi *dpi = vc4->dpi;
  133. int i;
  134. if (!dpi)
  135. return 0;
  136. for (i = 0; i < ARRAY_SIZE(dpi_regs); i++) {
  137. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  138. dpi_regs[i].name, dpi_regs[i].reg,
  139. DPI_READ(dpi_regs[i].reg));
  140. }
  141. return 0;
  142. }
  143. #endif
  144. static enum drm_connector_status
  145. vc4_dpi_connector_detect(struct drm_connector *connector, bool force)
  146. {
  147. struct vc4_dpi_connector *vc4_connector =
  148. to_vc4_dpi_connector(connector);
  149. struct vc4_dpi *dpi = vc4_connector->dpi;
  150. if (dpi->panel)
  151. return connector_status_connected;
  152. else
  153. return connector_status_disconnected;
  154. }
  155. static void vc4_dpi_connector_destroy(struct drm_connector *connector)
  156. {
  157. drm_connector_unregister(connector);
  158. drm_connector_cleanup(connector);
  159. }
  160. static int vc4_dpi_connector_get_modes(struct drm_connector *connector)
  161. {
  162. struct vc4_dpi_connector *vc4_connector =
  163. to_vc4_dpi_connector(connector);
  164. struct vc4_dpi *dpi = vc4_connector->dpi;
  165. if (dpi->panel)
  166. return drm_panel_get_modes(dpi->panel);
  167. return 0;
  168. }
  169. static const struct drm_connector_funcs vc4_dpi_connector_funcs = {
  170. .dpms = drm_atomic_helper_connector_dpms,
  171. .detect = vc4_dpi_connector_detect,
  172. .fill_modes = drm_helper_probe_single_connector_modes,
  173. .destroy = vc4_dpi_connector_destroy,
  174. .reset = drm_atomic_helper_connector_reset,
  175. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  176. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  177. };
  178. static const struct drm_connector_helper_funcs vc4_dpi_connector_helper_funcs = {
  179. .get_modes = vc4_dpi_connector_get_modes,
  180. };
  181. static struct drm_connector *vc4_dpi_connector_init(struct drm_device *dev,
  182. struct vc4_dpi *dpi)
  183. {
  184. struct drm_connector *connector = NULL;
  185. struct vc4_dpi_connector *dpi_connector;
  186. dpi_connector = devm_kzalloc(dev->dev, sizeof(*dpi_connector),
  187. GFP_KERNEL);
  188. if (!dpi_connector)
  189. return ERR_PTR(-ENOMEM);
  190. connector = &dpi_connector->base;
  191. dpi_connector->encoder = dpi->encoder;
  192. dpi_connector->dpi = dpi;
  193. drm_connector_init(dev, connector, &vc4_dpi_connector_funcs,
  194. DRM_MODE_CONNECTOR_DPI);
  195. drm_connector_helper_add(connector, &vc4_dpi_connector_helper_funcs);
  196. connector->polled = 0;
  197. connector->interlace_allowed = 0;
  198. connector->doublescan_allowed = 0;
  199. drm_mode_connector_attach_encoder(connector, dpi->encoder);
  200. return connector;
  201. }
  202. static const struct drm_encoder_funcs vc4_dpi_encoder_funcs = {
  203. .destroy = drm_encoder_cleanup,
  204. };
  205. static void vc4_dpi_encoder_disable(struct drm_encoder *encoder)
  206. {
  207. struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
  208. struct vc4_dpi *dpi = vc4_encoder->dpi;
  209. drm_panel_disable(dpi->panel);
  210. clk_disable_unprepare(dpi->pixel_clock);
  211. drm_panel_unprepare(dpi->panel);
  212. }
  213. static void vc4_dpi_encoder_enable(struct drm_encoder *encoder)
  214. {
  215. struct drm_display_mode *mode = &encoder->crtc->mode;
  216. struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
  217. struct vc4_dpi *dpi = vc4_encoder->dpi;
  218. u32 dpi_c = DPI_ENABLE | DPI_OUTPUT_ENABLE_MODE;
  219. int ret;
  220. ret = drm_panel_prepare(dpi->panel);
  221. if (ret) {
  222. DRM_ERROR("Panel failed to prepare\n");
  223. return;
  224. }
  225. if (dpi->connector->display_info.num_bus_formats) {
  226. u32 bus_format = dpi->connector->display_info.bus_formats[0];
  227. switch (bus_format) {
  228. case MEDIA_BUS_FMT_RGB888_1X24:
  229. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
  230. DPI_FORMAT);
  231. break;
  232. case MEDIA_BUS_FMT_BGR888_1X24:
  233. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
  234. DPI_FORMAT);
  235. dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR, DPI_ORDER);
  236. break;
  237. case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
  238. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2,
  239. DPI_FORMAT);
  240. break;
  241. case MEDIA_BUS_FMT_RGB666_1X18:
  242. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
  243. DPI_FORMAT);
  244. break;
  245. case MEDIA_BUS_FMT_RGB565_1X16:
  246. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_3,
  247. DPI_FORMAT);
  248. break;
  249. default:
  250. DRM_ERROR("Unknown media bus format %d\n", bus_format);
  251. break;
  252. }
  253. }
  254. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  255. dpi_c |= DPI_HSYNC_INVERT;
  256. else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
  257. dpi_c |= DPI_HSYNC_DISABLE;
  258. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  259. dpi_c |= DPI_VSYNC_INVERT;
  260. else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
  261. dpi_c |= DPI_VSYNC_DISABLE;
  262. DPI_WRITE(DPI_C, dpi_c);
  263. ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000);
  264. if (ret)
  265. DRM_ERROR("Failed to set clock rate: %d\n", ret);
  266. ret = clk_prepare_enable(dpi->pixel_clock);
  267. if (ret)
  268. DRM_ERROR("Failed to set clock rate: %d\n", ret);
  269. ret = drm_panel_enable(dpi->panel);
  270. if (ret) {
  271. DRM_ERROR("Panel failed to enable\n");
  272. drm_panel_unprepare(dpi->panel);
  273. return;
  274. }
  275. }
  276. static bool vc4_dpi_encoder_mode_fixup(struct drm_encoder *encoder,
  277. const struct drm_display_mode *mode,
  278. struct drm_display_mode *adjusted_mode)
  279. {
  280. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  281. return false;
  282. return true;
  283. }
  284. static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = {
  285. .disable = vc4_dpi_encoder_disable,
  286. .enable = vc4_dpi_encoder_enable,
  287. .mode_fixup = vc4_dpi_encoder_mode_fixup,
  288. };
  289. static const struct of_device_id vc4_dpi_dt_match[] = {
  290. { .compatible = "brcm,bcm2835-dpi", .data = NULL },
  291. {}
  292. };
  293. /* Walks the OF graph to find the panel node and then asks DRM to look
  294. * up the panel.
  295. */
  296. static struct drm_panel *vc4_dpi_get_panel(struct device *dev)
  297. {
  298. struct device_node *panel_node;
  299. struct device_node *np = dev->of_node;
  300. struct drm_panel *panel;
  301. /* don't proceed if we have an endpoint but no panel_node tied to it */
  302. panel_node = of_graph_get_remote_node(np, 0, 0);
  303. if (!panel_node)
  304. return NULL;
  305. panel = of_drm_find_panel(panel_node);
  306. of_node_put(panel_node);
  307. return panel;
  308. }
  309. static int vc4_dpi_bind(struct device *dev, struct device *master, void *data)
  310. {
  311. struct platform_device *pdev = to_platform_device(dev);
  312. struct drm_device *drm = dev_get_drvdata(master);
  313. struct vc4_dev *vc4 = to_vc4_dev(drm);
  314. struct vc4_dpi *dpi;
  315. struct vc4_dpi_encoder *vc4_dpi_encoder;
  316. int ret;
  317. dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL);
  318. if (!dpi)
  319. return -ENOMEM;
  320. vc4_dpi_encoder = devm_kzalloc(dev, sizeof(*vc4_dpi_encoder),
  321. GFP_KERNEL);
  322. if (!vc4_dpi_encoder)
  323. return -ENOMEM;
  324. vc4_dpi_encoder->base.type = VC4_ENCODER_TYPE_DPI;
  325. vc4_dpi_encoder->dpi = dpi;
  326. dpi->encoder = &vc4_dpi_encoder->base.base;
  327. dpi->pdev = pdev;
  328. dpi->regs = vc4_ioremap_regs(pdev, 0);
  329. if (IS_ERR(dpi->regs))
  330. return PTR_ERR(dpi->regs);
  331. if (DPI_READ(DPI_ID) != DPI_ID_VALUE) {
  332. dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n",
  333. DPI_READ(DPI_ID), DPI_ID_VALUE);
  334. return -ENODEV;
  335. }
  336. dpi->core_clock = devm_clk_get(dev, "core");
  337. if (IS_ERR(dpi->core_clock)) {
  338. ret = PTR_ERR(dpi->core_clock);
  339. if (ret != -EPROBE_DEFER)
  340. DRM_ERROR("Failed to get core clock: %d\n", ret);
  341. return ret;
  342. }
  343. dpi->pixel_clock = devm_clk_get(dev, "pixel");
  344. if (IS_ERR(dpi->pixel_clock)) {
  345. ret = PTR_ERR(dpi->pixel_clock);
  346. if (ret != -EPROBE_DEFER)
  347. DRM_ERROR("Failed to get pixel clock: %d\n", ret);
  348. return ret;
  349. }
  350. ret = clk_prepare_enable(dpi->core_clock);
  351. if (ret)
  352. DRM_ERROR("Failed to turn on core clock: %d\n", ret);
  353. dpi->panel = vc4_dpi_get_panel(dev);
  354. drm_encoder_init(drm, dpi->encoder, &vc4_dpi_encoder_funcs,
  355. DRM_MODE_ENCODER_DPI, NULL);
  356. drm_encoder_helper_add(dpi->encoder, &vc4_dpi_encoder_helper_funcs);
  357. dpi->connector = vc4_dpi_connector_init(drm, dpi);
  358. if (IS_ERR(dpi->connector)) {
  359. ret = PTR_ERR(dpi->connector);
  360. goto err_destroy_encoder;
  361. }
  362. if (dpi->panel)
  363. drm_panel_attach(dpi->panel, dpi->connector);
  364. dev_set_drvdata(dev, dpi);
  365. vc4->dpi = dpi;
  366. return 0;
  367. err_destroy_encoder:
  368. drm_encoder_cleanup(dpi->encoder);
  369. clk_disable_unprepare(dpi->core_clock);
  370. return ret;
  371. }
  372. static void vc4_dpi_unbind(struct device *dev, struct device *master,
  373. void *data)
  374. {
  375. struct drm_device *drm = dev_get_drvdata(master);
  376. struct vc4_dev *vc4 = to_vc4_dev(drm);
  377. struct vc4_dpi *dpi = dev_get_drvdata(dev);
  378. if (dpi->panel)
  379. drm_panel_detach(dpi->panel);
  380. vc4_dpi_connector_destroy(dpi->connector);
  381. drm_encoder_cleanup(dpi->encoder);
  382. clk_disable_unprepare(dpi->core_clock);
  383. vc4->dpi = NULL;
  384. }
  385. static const struct component_ops vc4_dpi_ops = {
  386. .bind = vc4_dpi_bind,
  387. .unbind = vc4_dpi_unbind,
  388. };
  389. static int vc4_dpi_dev_probe(struct platform_device *pdev)
  390. {
  391. return component_add(&pdev->dev, &vc4_dpi_ops);
  392. }
  393. static int vc4_dpi_dev_remove(struct platform_device *pdev)
  394. {
  395. component_del(&pdev->dev, &vc4_dpi_ops);
  396. return 0;
  397. }
  398. struct platform_driver vc4_dpi_driver = {
  399. .probe = vc4_dpi_dev_probe,
  400. .remove = vc4_dpi_dev_remove,
  401. .driver = {
  402. .name = "vc4_dpi",
  403. .of_match_table = vc4_dpi_dt_match,
  404. },
  405. };