vc4_dpi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. dpi_connector = devm_kzalloc(dev->dev, sizeof(*dpi_connector),
  195. GFP_KERNEL);
  196. if (!dpi_connector)
  197. return ERR_PTR(-ENOMEM);
  198. connector = &dpi_connector->base;
  199. dpi_connector->encoder = dpi->encoder;
  200. dpi_connector->dpi = dpi;
  201. drm_connector_init(dev, connector, &vc4_dpi_connector_funcs,
  202. DRM_MODE_CONNECTOR_DPI);
  203. drm_connector_helper_add(connector, &vc4_dpi_connector_helper_funcs);
  204. connector->polled = 0;
  205. connector->interlace_allowed = 0;
  206. connector->doublescan_allowed = 0;
  207. drm_mode_connector_attach_encoder(connector, dpi->encoder);
  208. return connector;
  209. }
  210. static const struct drm_encoder_funcs vc4_dpi_encoder_funcs = {
  211. .destroy = drm_encoder_cleanup,
  212. };
  213. static void vc4_dpi_encoder_disable(struct drm_encoder *encoder)
  214. {
  215. struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
  216. struct vc4_dpi *dpi = vc4_encoder->dpi;
  217. drm_panel_disable(dpi->panel);
  218. clk_disable_unprepare(dpi->pixel_clock);
  219. drm_panel_unprepare(dpi->panel);
  220. }
  221. static void vc4_dpi_encoder_enable(struct drm_encoder *encoder)
  222. {
  223. struct drm_display_mode *mode = &encoder->crtc->mode;
  224. struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
  225. struct vc4_dpi *dpi = vc4_encoder->dpi;
  226. u32 dpi_c = DPI_ENABLE | DPI_OUTPUT_ENABLE_MODE;
  227. int ret;
  228. ret = drm_panel_prepare(dpi->panel);
  229. if (ret) {
  230. DRM_ERROR("Panel failed to prepare\n");
  231. return;
  232. }
  233. if (dpi->connector->display_info.num_bus_formats) {
  234. u32 bus_format = dpi->connector->display_info.bus_formats[0];
  235. switch (bus_format) {
  236. case MEDIA_BUS_FMT_RGB888_1X24:
  237. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
  238. DPI_FORMAT);
  239. break;
  240. case MEDIA_BUS_FMT_BGR888_1X24:
  241. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
  242. DPI_FORMAT);
  243. dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR, DPI_ORDER);
  244. break;
  245. case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
  246. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2,
  247. DPI_FORMAT);
  248. break;
  249. case MEDIA_BUS_FMT_RGB666_1X18:
  250. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
  251. DPI_FORMAT);
  252. break;
  253. case MEDIA_BUS_FMT_RGB565_1X16:
  254. dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_3,
  255. DPI_FORMAT);
  256. break;
  257. default:
  258. DRM_ERROR("Unknown media bus format %d\n", bus_format);
  259. break;
  260. }
  261. }
  262. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  263. dpi_c |= DPI_HSYNC_INVERT;
  264. else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
  265. dpi_c |= DPI_HSYNC_DISABLE;
  266. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  267. dpi_c |= DPI_VSYNC_INVERT;
  268. else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
  269. dpi_c |= DPI_VSYNC_DISABLE;
  270. DPI_WRITE(DPI_C, dpi_c);
  271. ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000);
  272. if (ret)
  273. DRM_ERROR("Failed to set clock rate: %d\n", ret);
  274. ret = clk_prepare_enable(dpi->pixel_clock);
  275. if (ret)
  276. DRM_ERROR("Failed to set clock rate: %d\n", ret);
  277. ret = drm_panel_enable(dpi->panel);
  278. if (ret) {
  279. DRM_ERROR("Panel failed to enable\n");
  280. drm_panel_unprepare(dpi->panel);
  281. return;
  282. }
  283. }
  284. static bool vc4_dpi_encoder_mode_fixup(struct drm_encoder *encoder,
  285. const struct drm_display_mode *mode,
  286. struct drm_display_mode *adjusted_mode)
  287. {
  288. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  289. return false;
  290. return true;
  291. }
  292. static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = {
  293. .disable = vc4_dpi_encoder_disable,
  294. .enable = vc4_dpi_encoder_enable,
  295. .mode_fixup = vc4_dpi_encoder_mode_fixup,
  296. };
  297. static const struct of_device_id vc4_dpi_dt_match[] = {
  298. { .compatible = "brcm,bcm2835-dpi", .data = NULL },
  299. {}
  300. };
  301. /* Walks the OF graph to find the panel node and then asks DRM to look
  302. * up the panel.
  303. */
  304. static struct drm_panel *vc4_dpi_get_panel(struct device *dev)
  305. {
  306. struct device_node *endpoint, *panel_node;
  307. struct device_node *np = dev->of_node;
  308. struct drm_panel *panel;
  309. endpoint = of_graph_get_next_endpoint(np, NULL);
  310. if (!endpoint) {
  311. dev_err(dev, "no endpoint to fetch DPI panel\n");
  312. return NULL;
  313. }
  314. /* don't proceed if we have an endpoint but no panel_node tied to it */
  315. panel_node = of_graph_get_remote_port_parent(endpoint);
  316. of_node_put(endpoint);
  317. if (!panel_node) {
  318. dev_err(dev, "no valid panel node\n");
  319. return NULL;
  320. }
  321. panel = of_drm_find_panel(panel_node);
  322. of_node_put(panel_node);
  323. return panel;
  324. }
  325. static int vc4_dpi_bind(struct device *dev, struct device *master, void *data)
  326. {
  327. struct platform_device *pdev = to_platform_device(dev);
  328. struct drm_device *drm = dev_get_drvdata(master);
  329. struct vc4_dev *vc4 = to_vc4_dev(drm);
  330. struct vc4_dpi *dpi;
  331. struct vc4_dpi_encoder *vc4_dpi_encoder;
  332. int ret;
  333. dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL);
  334. if (!dpi)
  335. return -ENOMEM;
  336. vc4_dpi_encoder = devm_kzalloc(dev, sizeof(*vc4_dpi_encoder),
  337. GFP_KERNEL);
  338. if (!vc4_dpi_encoder)
  339. return -ENOMEM;
  340. vc4_dpi_encoder->base.type = VC4_ENCODER_TYPE_DPI;
  341. vc4_dpi_encoder->dpi = dpi;
  342. dpi->encoder = &vc4_dpi_encoder->base.base;
  343. dpi->pdev = pdev;
  344. dpi->regs = vc4_ioremap_regs(pdev, 0);
  345. if (IS_ERR(dpi->regs))
  346. return PTR_ERR(dpi->regs);
  347. vc4_dpi_dump_regs(dpi);
  348. if (DPI_READ(DPI_ID) != DPI_ID_VALUE) {
  349. dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n",
  350. DPI_READ(DPI_ID), DPI_ID_VALUE);
  351. return -ENODEV;
  352. }
  353. dpi->core_clock = devm_clk_get(dev, "core");
  354. if (IS_ERR(dpi->core_clock)) {
  355. ret = PTR_ERR(dpi->core_clock);
  356. if (ret != -EPROBE_DEFER)
  357. DRM_ERROR("Failed to get core clock: %d\n", ret);
  358. return ret;
  359. }
  360. dpi->pixel_clock = devm_clk_get(dev, "pixel");
  361. if (IS_ERR(dpi->pixel_clock)) {
  362. ret = PTR_ERR(dpi->pixel_clock);
  363. if (ret != -EPROBE_DEFER)
  364. DRM_ERROR("Failed to get pixel clock: %d\n", ret);
  365. return ret;
  366. }
  367. ret = clk_prepare_enable(dpi->core_clock);
  368. if (ret)
  369. DRM_ERROR("Failed to turn on core clock: %d\n", ret);
  370. dpi->panel = vc4_dpi_get_panel(dev);
  371. drm_encoder_init(drm, dpi->encoder, &vc4_dpi_encoder_funcs,
  372. DRM_MODE_ENCODER_DPI, NULL);
  373. drm_encoder_helper_add(dpi->encoder, &vc4_dpi_encoder_helper_funcs);
  374. dpi->connector = vc4_dpi_connector_init(drm, dpi);
  375. if (IS_ERR(dpi->connector)) {
  376. ret = PTR_ERR(dpi->connector);
  377. goto err_destroy_encoder;
  378. }
  379. if (dpi->panel)
  380. drm_panel_attach(dpi->panel, dpi->connector);
  381. dev_set_drvdata(dev, dpi);
  382. vc4->dpi = dpi;
  383. return 0;
  384. err_destroy_encoder:
  385. drm_encoder_cleanup(dpi->encoder);
  386. clk_disable_unprepare(dpi->core_clock);
  387. return ret;
  388. }
  389. static void vc4_dpi_unbind(struct device *dev, struct device *master,
  390. void *data)
  391. {
  392. struct drm_device *drm = dev_get_drvdata(master);
  393. struct vc4_dev *vc4 = to_vc4_dev(drm);
  394. struct vc4_dpi *dpi = dev_get_drvdata(dev);
  395. if (dpi->panel)
  396. drm_panel_detach(dpi->panel);
  397. vc4_dpi_connector_destroy(dpi->connector);
  398. drm_encoder_cleanup(dpi->encoder);
  399. clk_disable_unprepare(dpi->core_clock);
  400. vc4->dpi = NULL;
  401. }
  402. static const struct component_ops vc4_dpi_ops = {
  403. .bind = vc4_dpi_bind,
  404. .unbind = vc4_dpi_unbind,
  405. };
  406. static int vc4_dpi_dev_probe(struct platform_device *pdev)
  407. {
  408. return component_add(&pdev->dev, &vc4_dpi_ops);
  409. }
  410. static int vc4_dpi_dev_remove(struct platform_device *pdev)
  411. {
  412. component_del(&pdev->dev, &vc4_dpi_ops);
  413. return 0;
  414. }
  415. struct platform_driver vc4_dpi_driver = {
  416. .probe = vc4_dpi_dev_probe,
  417. .remove = vc4_dpi_dev_remove,
  418. .driver = {
  419. .name = "vc4_dpi",
  420. .of_match_table = vc4_dpi_dt_match,
  421. },
  422. };