rcar_du_kms.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * rcar_du_kms.c -- R-Car Display Unit Mode Setting
  3. *
  4. * Copyright (C) 2013-2015 Renesas Electronics Corporation
  5. *
  6. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <drm/drmP.h>
  14. #include <drm/drm_atomic.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_crtc.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_fb_cma_helper.h>
  19. #include <drm/drm_gem_cma_helper.h>
  20. #include <drm/drm_gem_framebuffer_helper.h>
  21. #include <linux/of_graph.h>
  22. #include <linux/wait.h>
  23. #include "rcar_du_crtc.h"
  24. #include "rcar_du_drv.h"
  25. #include "rcar_du_encoder.h"
  26. #include "rcar_du_kms.h"
  27. #include "rcar_du_regs.h"
  28. #include "rcar_du_vsp.h"
  29. /* -----------------------------------------------------------------------------
  30. * Format helpers
  31. */
  32. static const struct rcar_du_format_info rcar_du_format_infos[] = {
  33. {
  34. .fourcc = DRM_FORMAT_RGB565,
  35. .bpp = 16,
  36. .planes = 1,
  37. .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
  38. .edf = PnDDCR4_EDF_NONE,
  39. }, {
  40. .fourcc = DRM_FORMAT_ARGB1555,
  41. .bpp = 16,
  42. .planes = 1,
  43. .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
  44. .edf = PnDDCR4_EDF_NONE,
  45. }, {
  46. .fourcc = DRM_FORMAT_XRGB1555,
  47. .bpp = 16,
  48. .planes = 1,
  49. .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
  50. .edf = PnDDCR4_EDF_NONE,
  51. }, {
  52. .fourcc = DRM_FORMAT_XRGB8888,
  53. .bpp = 32,
  54. .planes = 1,
  55. .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
  56. .edf = PnDDCR4_EDF_RGB888,
  57. }, {
  58. .fourcc = DRM_FORMAT_ARGB8888,
  59. .bpp = 32,
  60. .planes = 1,
  61. .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP,
  62. .edf = PnDDCR4_EDF_ARGB8888,
  63. }, {
  64. .fourcc = DRM_FORMAT_UYVY,
  65. .bpp = 16,
  66. .planes = 1,
  67. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  68. .edf = PnDDCR4_EDF_NONE,
  69. }, {
  70. .fourcc = DRM_FORMAT_YUYV,
  71. .bpp = 16,
  72. .planes = 1,
  73. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  74. .edf = PnDDCR4_EDF_NONE,
  75. }, {
  76. .fourcc = DRM_FORMAT_NV12,
  77. .bpp = 12,
  78. .planes = 2,
  79. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  80. .edf = PnDDCR4_EDF_NONE,
  81. }, {
  82. .fourcc = DRM_FORMAT_NV21,
  83. .bpp = 12,
  84. .planes = 2,
  85. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  86. .edf = PnDDCR4_EDF_NONE,
  87. }, {
  88. .fourcc = DRM_FORMAT_NV16,
  89. .bpp = 16,
  90. .planes = 2,
  91. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  92. .edf = PnDDCR4_EDF_NONE,
  93. },
  94. /*
  95. * The following formats are not supported on Gen2 and thus have no
  96. * associated .pnmr or .edf settings.
  97. */
  98. {
  99. .fourcc = DRM_FORMAT_NV61,
  100. .bpp = 16,
  101. .planes = 2,
  102. }, {
  103. .fourcc = DRM_FORMAT_YUV420,
  104. .bpp = 12,
  105. .planes = 3,
  106. }, {
  107. .fourcc = DRM_FORMAT_YVU420,
  108. .bpp = 12,
  109. .planes = 3,
  110. }, {
  111. .fourcc = DRM_FORMAT_YUV422,
  112. .bpp = 16,
  113. .planes = 3,
  114. }, {
  115. .fourcc = DRM_FORMAT_YVU422,
  116. .bpp = 16,
  117. .planes = 3,
  118. }, {
  119. .fourcc = DRM_FORMAT_YUV444,
  120. .bpp = 24,
  121. .planes = 3,
  122. }, {
  123. .fourcc = DRM_FORMAT_YVU444,
  124. .bpp = 24,
  125. .planes = 3,
  126. },
  127. };
  128. const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc)
  129. {
  130. unsigned int i;
  131. for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) {
  132. if (rcar_du_format_infos[i].fourcc == fourcc)
  133. return &rcar_du_format_infos[i];
  134. }
  135. return NULL;
  136. }
  137. /* -----------------------------------------------------------------------------
  138. * Frame buffer
  139. */
  140. int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev,
  141. struct drm_mode_create_dumb *args)
  142. {
  143. struct rcar_du_device *rcdu = dev->dev_private;
  144. unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
  145. unsigned int align;
  146. /*
  147. * The R8A7779 DU requires a 16 pixels pitch alignment as documented,
  148. * but the R8A7790 DU seems to require a 128 bytes pitch alignment.
  149. */
  150. if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
  151. align = 128;
  152. else
  153. align = 16 * args->bpp / 8;
  154. args->pitch = roundup(min_pitch, align);
  155. return drm_gem_cma_dumb_create_internal(file, dev, args);
  156. }
  157. static struct drm_framebuffer *
  158. rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  159. const struct drm_mode_fb_cmd2 *mode_cmd)
  160. {
  161. struct rcar_du_device *rcdu = dev->dev_private;
  162. const struct rcar_du_format_info *format;
  163. unsigned int max_pitch;
  164. unsigned int align;
  165. unsigned int bpp;
  166. unsigned int i;
  167. format = rcar_du_format_info(mode_cmd->pixel_format);
  168. if (format == NULL) {
  169. dev_dbg(dev->dev, "unsupported pixel format %08x\n",
  170. mode_cmd->pixel_format);
  171. return ERR_PTR(-EINVAL);
  172. }
  173. /*
  174. * The pitch and alignment constraints are expressed in pixels on the
  175. * hardware side and in bytes in the DRM API.
  176. */
  177. bpp = format->planes == 1 ? format->bpp / 8 : 1;
  178. max_pitch = 4096 * bpp;
  179. if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
  180. align = 128;
  181. else
  182. align = 16 * bpp;
  183. if (mode_cmd->pitches[0] & (align - 1) ||
  184. mode_cmd->pitches[0] >= max_pitch) {
  185. dev_dbg(dev->dev, "invalid pitch value %u\n",
  186. mode_cmd->pitches[0]);
  187. return ERR_PTR(-EINVAL);
  188. }
  189. for (i = 1; i < format->planes; ++i) {
  190. if (mode_cmd->pitches[i] != mode_cmd->pitches[0]) {
  191. dev_dbg(dev->dev,
  192. "luma and chroma pitches do not match\n");
  193. return ERR_PTR(-EINVAL);
  194. }
  195. }
  196. return drm_gem_fb_create(dev, file_priv, mode_cmd);
  197. }
  198. static void rcar_du_output_poll_changed(struct drm_device *dev)
  199. {
  200. struct rcar_du_device *rcdu = dev->dev_private;
  201. drm_fbdev_cma_hotplug_event(rcdu->fbdev);
  202. }
  203. /* -----------------------------------------------------------------------------
  204. * Atomic Check and Update
  205. */
  206. static int rcar_du_atomic_check(struct drm_device *dev,
  207. struct drm_atomic_state *state)
  208. {
  209. struct rcar_du_device *rcdu = dev->dev_private;
  210. int ret;
  211. ret = drm_atomic_helper_check(dev, state);
  212. if (ret)
  213. return ret;
  214. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE))
  215. return 0;
  216. return rcar_du_atomic_check_planes(dev, state);
  217. }
  218. static void rcar_du_atomic_commit_tail(struct drm_atomic_state *old_state)
  219. {
  220. struct drm_device *dev = old_state->dev;
  221. /* Apply the atomic update. */
  222. drm_atomic_helper_commit_modeset_disables(dev, old_state);
  223. drm_atomic_helper_commit_planes(dev, old_state,
  224. DRM_PLANE_COMMIT_ACTIVE_ONLY);
  225. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  226. drm_atomic_helper_commit_hw_done(old_state);
  227. drm_atomic_helper_wait_for_flip_done(dev, old_state);
  228. drm_atomic_helper_cleanup_planes(dev, old_state);
  229. }
  230. /* -----------------------------------------------------------------------------
  231. * Initialization
  232. */
  233. static const struct drm_mode_config_helper_funcs rcar_du_mode_config_helper = {
  234. .atomic_commit_tail = rcar_du_atomic_commit_tail,
  235. };
  236. static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
  237. .fb_create = rcar_du_fb_create,
  238. .output_poll_changed = rcar_du_output_poll_changed,
  239. .atomic_check = rcar_du_atomic_check,
  240. .atomic_commit = drm_atomic_helper_commit,
  241. };
  242. static int rcar_du_encoders_init_one(struct rcar_du_device *rcdu,
  243. enum rcar_du_output output,
  244. struct of_endpoint *ep)
  245. {
  246. struct device_node *connector = NULL;
  247. struct device_node *encoder = NULL;
  248. struct device_node *ep_node = NULL;
  249. struct device_node *entity_ep_node;
  250. struct device_node *entity;
  251. int ret;
  252. /*
  253. * Locate the connected entity and infer its type from the number of
  254. * endpoints.
  255. */
  256. entity = of_graph_get_remote_port_parent(ep->local_node);
  257. if (!entity) {
  258. dev_dbg(rcdu->dev, "unconnected endpoint %pOF, skipping\n",
  259. ep->local_node);
  260. return -ENODEV;
  261. }
  262. if (!of_device_is_available(entity)) {
  263. dev_dbg(rcdu->dev,
  264. "connected entity %pOF is disabled, skipping\n",
  265. entity);
  266. of_node_put(entity);
  267. return -ENODEV;
  268. }
  269. entity_ep_node = of_graph_get_remote_endpoint(ep->local_node);
  270. for_each_endpoint_of_node(entity, ep_node) {
  271. if (ep_node == entity_ep_node)
  272. continue;
  273. /*
  274. * We've found one endpoint other than the input, this must
  275. * be an encoder. Locate the connector.
  276. */
  277. encoder = entity;
  278. connector = of_graph_get_remote_port_parent(ep_node);
  279. of_node_put(ep_node);
  280. if (!connector) {
  281. dev_warn(rcdu->dev,
  282. "no connector for encoder %pOF, skipping\n",
  283. encoder);
  284. of_node_put(entity_ep_node);
  285. of_node_put(encoder);
  286. return -ENODEV;
  287. }
  288. break;
  289. }
  290. of_node_put(entity_ep_node);
  291. if (!encoder) {
  292. dev_warn(rcdu->dev,
  293. "no encoder found for endpoint %pOF, skipping\n",
  294. ep->local_node);
  295. of_node_put(entity);
  296. return -ENODEV;
  297. }
  298. ret = rcar_du_encoder_init(rcdu, output, encoder, connector);
  299. if (ret && ret != -EPROBE_DEFER)
  300. dev_warn(rcdu->dev,
  301. "failed to initialize encoder %pOF on output %u (%d), skipping\n",
  302. encoder, output, ret);
  303. of_node_put(encoder);
  304. of_node_put(connector);
  305. return ret;
  306. }
  307. static int rcar_du_encoders_init(struct rcar_du_device *rcdu)
  308. {
  309. struct device_node *np = rcdu->dev->of_node;
  310. struct device_node *ep_node;
  311. unsigned int num_encoders = 0;
  312. /*
  313. * Iterate over the endpoints and create one encoder for each output
  314. * pipeline.
  315. */
  316. for_each_endpoint_of_node(np, ep_node) {
  317. enum rcar_du_output output;
  318. struct of_endpoint ep;
  319. unsigned int i;
  320. int ret;
  321. ret = of_graph_parse_endpoint(ep_node, &ep);
  322. if (ret < 0) {
  323. of_node_put(ep_node);
  324. return ret;
  325. }
  326. /* Find the output route corresponding to the port number. */
  327. for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) {
  328. if (rcdu->info->routes[i].possible_crtcs &&
  329. rcdu->info->routes[i].port == ep.port) {
  330. output = i;
  331. break;
  332. }
  333. }
  334. if (i == RCAR_DU_OUTPUT_MAX) {
  335. dev_warn(rcdu->dev,
  336. "port %u references unexisting output, skipping\n",
  337. ep.port);
  338. continue;
  339. }
  340. /* Process the output pipeline. */
  341. ret = rcar_du_encoders_init_one(rcdu, output, &ep);
  342. if (ret < 0) {
  343. if (ret == -EPROBE_DEFER) {
  344. of_node_put(ep_node);
  345. return ret;
  346. }
  347. continue;
  348. }
  349. num_encoders++;
  350. }
  351. return num_encoders;
  352. }
  353. static int rcar_du_properties_init(struct rcar_du_device *rcdu)
  354. {
  355. /*
  356. * The color key is expressed as an RGB888 triplet stored in a 32-bit
  357. * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
  358. * or enable source color keying (1).
  359. */
  360. rcdu->props.colorkey =
  361. drm_property_create_range(rcdu->ddev, 0, "colorkey",
  362. 0, 0x01ffffff);
  363. if (rcdu->props.colorkey == NULL)
  364. return -ENOMEM;
  365. return 0;
  366. }
  367. static int rcar_du_vsps_init(struct rcar_du_device *rcdu)
  368. {
  369. const struct device_node *np = rcdu->dev->of_node;
  370. struct of_phandle_args args;
  371. struct {
  372. struct device_node *np;
  373. unsigned int crtcs_mask;
  374. } vsps[RCAR_DU_MAX_VSPS] = { { NULL, }, };
  375. unsigned int vsps_count = 0;
  376. unsigned int cells;
  377. unsigned int i;
  378. int ret;
  379. /*
  380. * First parse the DT vsps property to populate the list of VSPs. Each
  381. * entry contains a pointer to the VSP DT node and a bitmask of the
  382. * connected DU CRTCs.
  383. */
  384. cells = of_property_count_u32_elems(np, "vsps") / rcdu->num_crtcs - 1;
  385. if (cells > 1)
  386. return -EINVAL;
  387. for (i = 0; i < rcdu->num_crtcs; ++i) {
  388. unsigned int j;
  389. ret = of_parse_phandle_with_fixed_args(np, "vsps", cells, i,
  390. &args);
  391. if (ret < 0)
  392. goto error;
  393. /*
  394. * Add the VSP to the list or update the corresponding existing
  395. * entry if the VSP has already been added.
  396. */
  397. for (j = 0; j < vsps_count; ++j) {
  398. if (vsps[j].np == args.np)
  399. break;
  400. }
  401. if (j < vsps_count)
  402. of_node_put(args.np);
  403. else
  404. vsps[vsps_count++].np = args.np;
  405. vsps[j].crtcs_mask |= BIT(i);
  406. /* Store the VSP pointer and pipe index in the CRTC. */
  407. rcdu->crtcs[i].vsp = &rcdu->vsps[j];
  408. rcdu->crtcs[i].vsp_pipe = cells >= 1 ? args.args[0] : 0;
  409. }
  410. /*
  411. * Then initialize all the VSPs from the node pointers and CRTCs bitmask
  412. * computed previously.
  413. */
  414. for (i = 0; i < vsps_count; ++i) {
  415. struct rcar_du_vsp *vsp = &rcdu->vsps[i];
  416. vsp->index = i;
  417. vsp->dev = rcdu;
  418. ret = rcar_du_vsp_init(vsp, vsps[i].np, vsps[i].crtcs_mask);
  419. if (ret < 0)
  420. goto error;
  421. }
  422. return 0;
  423. error:
  424. for (i = 0; i < ARRAY_SIZE(vsps); ++i)
  425. of_node_put(vsps[i].np);
  426. return ret;
  427. }
  428. int rcar_du_modeset_init(struct rcar_du_device *rcdu)
  429. {
  430. static const unsigned int mmio_offsets[] = {
  431. DU0_REG_OFFSET, DU2_REG_OFFSET
  432. };
  433. struct drm_device *dev = rcdu->ddev;
  434. struct drm_encoder *encoder;
  435. struct drm_fbdev_cma *fbdev;
  436. unsigned int num_encoders;
  437. unsigned int num_groups;
  438. unsigned int swindex;
  439. unsigned int hwindex;
  440. unsigned int i;
  441. int ret;
  442. drm_mode_config_init(dev);
  443. dev->mode_config.min_width = 0;
  444. dev->mode_config.min_height = 0;
  445. dev->mode_config.normalize_zpos = true;
  446. dev->mode_config.funcs = &rcar_du_mode_config_funcs;
  447. dev->mode_config.helper_private = &rcar_du_mode_config_helper;
  448. if (rcdu->info->gen < 3) {
  449. dev->mode_config.max_width = 4095;
  450. dev->mode_config.max_height = 2047;
  451. } else {
  452. /*
  453. * The Gen3 DU uses the VSP1 for memory access, and is limited
  454. * to frame sizes of 8190x8190.
  455. */
  456. dev->mode_config.max_width = 8190;
  457. dev->mode_config.max_height = 8190;
  458. }
  459. rcdu->num_crtcs = hweight8(rcdu->info->channels_mask);
  460. ret = rcar_du_properties_init(rcdu);
  461. if (ret < 0)
  462. return ret;
  463. /*
  464. * Initialize vertical blanking interrupts handling. Start with vblank
  465. * disabled for all CRTCs.
  466. */
  467. ret = drm_vblank_init(dev, rcdu->num_crtcs);
  468. if (ret < 0)
  469. return ret;
  470. /* Initialize the groups. */
  471. num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
  472. for (i = 0; i < num_groups; ++i) {
  473. struct rcar_du_group *rgrp = &rcdu->groups[i];
  474. mutex_init(&rgrp->lock);
  475. rgrp->dev = rcdu;
  476. rgrp->mmio_offset = mmio_offsets[i];
  477. rgrp->index = i;
  478. /* Extract the channel mask for this group only. */
  479. rgrp->channels_mask = (rcdu->info->channels_mask >> (2 * i))
  480. & GENMASK(1, 0);
  481. rgrp->num_crtcs = hweight8(rgrp->channels_mask);
  482. /*
  483. * If we have more than one CRTCs in this group pre-associate
  484. * the low-order planes with CRTC 0 and the high-order planes
  485. * with CRTC 1 to minimize flicker occurring when the
  486. * association is changed.
  487. */
  488. rgrp->dptsr_planes = rgrp->num_crtcs > 1
  489. ? (rcdu->info->gen >= 3 ? 0x04 : 0xf0)
  490. : 0;
  491. if (!rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
  492. ret = rcar_du_planes_init(rgrp);
  493. if (ret < 0)
  494. return ret;
  495. }
  496. }
  497. /* Initialize the compositors. */
  498. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
  499. ret = rcar_du_vsps_init(rcdu);
  500. if (ret < 0)
  501. return ret;
  502. }
  503. /* Create the CRTCs. */
  504. for (swindex = 0, hwindex = 0; swindex < rcdu->num_crtcs; ++hwindex) {
  505. struct rcar_du_group *rgrp;
  506. /* Skip unpopulated DU channels. */
  507. if (!(rcdu->info->channels_mask & BIT(hwindex)))
  508. continue;
  509. rgrp = &rcdu->groups[hwindex / 2];
  510. ret = rcar_du_crtc_create(rgrp, swindex++, hwindex);
  511. if (ret < 0)
  512. return ret;
  513. }
  514. /* Initialize the encoders. */
  515. ret = rcar_du_encoders_init(rcdu);
  516. if (ret < 0)
  517. return ret;
  518. if (ret == 0) {
  519. dev_err(rcdu->dev, "error: no encoder could be initialized\n");
  520. return -EINVAL;
  521. }
  522. num_encoders = ret;
  523. /*
  524. * Set the possible CRTCs and possible clones. There's always at least
  525. * one way for all encoders to clone each other, set all bits in the
  526. * possible clones field.
  527. */
  528. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  529. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  530. const struct rcar_du_output_routing *route =
  531. &rcdu->info->routes[renc->output];
  532. encoder->possible_crtcs = route->possible_crtcs;
  533. encoder->possible_clones = (1 << num_encoders) - 1;
  534. }
  535. drm_mode_config_reset(dev);
  536. drm_kms_helper_poll_init(dev);
  537. if (dev->mode_config.num_connector) {
  538. fbdev = drm_fbdev_cma_init(dev, 32,
  539. dev->mode_config.num_connector);
  540. if (IS_ERR(fbdev))
  541. return PTR_ERR(fbdev);
  542. rcdu->fbdev = fbdev;
  543. } else {
  544. dev_info(rcdu->dev,
  545. "no connector found, disabling fbdev emulation\n");
  546. }
  547. return 0;
  548. }