vc4_dpi.c 13 KB

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