rcar_du_kms.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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_modeset(dev, state);
  212. if (ret)
  213. return ret;
  214. ret = drm_atomic_normalize_zpos(dev, state);
  215. if (ret)
  216. return ret;
  217. ret = drm_atomic_helper_check_planes(dev, state);
  218. if (ret)
  219. return ret;
  220. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE))
  221. return 0;
  222. return rcar_du_atomic_check_planes(dev, state);
  223. }
  224. static void rcar_du_atomic_commit_tail(struct drm_atomic_state *old_state)
  225. {
  226. struct drm_device *dev = old_state->dev;
  227. /* Apply the atomic update. */
  228. drm_atomic_helper_commit_modeset_disables(dev, old_state);
  229. drm_atomic_helper_commit_planes(dev, old_state,
  230. DRM_PLANE_COMMIT_ACTIVE_ONLY);
  231. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  232. drm_atomic_helper_commit_hw_done(old_state);
  233. drm_atomic_helper_wait_for_flip_done(dev, old_state);
  234. drm_atomic_helper_cleanup_planes(dev, old_state);
  235. }
  236. /* -----------------------------------------------------------------------------
  237. * Initialization
  238. */
  239. static const struct drm_mode_config_helper_funcs rcar_du_mode_config_helper = {
  240. .atomic_commit_tail = rcar_du_atomic_commit_tail,
  241. };
  242. static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
  243. .fb_create = rcar_du_fb_create,
  244. .output_poll_changed = rcar_du_output_poll_changed,
  245. .atomic_check = rcar_du_atomic_check,
  246. .atomic_commit = drm_atomic_helper_commit,
  247. };
  248. static int rcar_du_encoders_init_one(struct rcar_du_device *rcdu,
  249. enum rcar_du_output output,
  250. struct of_endpoint *ep)
  251. {
  252. struct device_node *connector = NULL;
  253. struct device_node *encoder = NULL;
  254. struct device_node *ep_node = NULL;
  255. struct device_node *entity_ep_node;
  256. struct device_node *entity;
  257. int ret;
  258. /*
  259. * Locate the connected entity and infer its type from the number of
  260. * endpoints.
  261. */
  262. entity = of_graph_get_remote_port_parent(ep->local_node);
  263. if (!entity) {
  264. dev_dbg(rcdu->dev, "unconnected endpoint %pOF, skipping\n",
  265. ep->local_node);
  266. return -ENODEV;
  267. }
  268. if (!of_device_is_available(entity)) {
  269. dev_dbg(rcdu->dev,
  270. "connected entity %pOF is disabled, skipping\n",
  271. entity);
  272. return -ENODEV;
  273. }
  274. entity_ep_node = of_graph_get_remote_endpoint(ep->local_node);
  275. for_each_endpoint_of_node(entity, ep_node) {
  276. if (ep_node == entity_ep_node)
  277. continue;
  278. /*
  279. * We've found one endpoint other than the input, this must
  280. * be an encoder. Locate the connector.
  281. */
  282. encoder = entity;
  283. connector = of_graph_get_remote_port_parent(ep_node);
  284. of_node_put(ep_node);
  285. if (!connector) {
  286. dev_warn(rcdu->dev,
  287. "no connector for encoder %pOF, skipping\n",
  288. encoder);
  289. of_node_put(entity_ep_node);
  290. of_node_put(encoder);
  291. return -ENODEV;
  292. }
  293. break;
  294. }
  295. of_node_put(entity_ep_node);
  296. if (!encoder) {
  297. dev_warn(rcdu->dev,
  298. "no encoder found for endpoint %pOF, skipping\n",
  299. ep->local_node);
  300. return -ENODEV;
  301. }
  302. ret = rcar_du_encoder_init(rcdu, output, encoder, connector);
  303. if (ret && ret != -EPROBE_DEFER)
  304. dev_warn(rcdu->dev,
  305. "failed to initialize encoder %pOF on output %u (%d), skipping\n",
  306. encoder, output, ret);
  307. of_node_put(encoder);
  308. of_node_put(connector);
  309. return ret;
  310. }
  311. static int rcar_du_encoders_init(struct rcar_du_device *rcdu)
  312. {
  313. struct device_node *np = rcdu->dev->of_node;
  314. struct device_node *ep_node;
  315. unsigned int num_encoders = 0;
  316. /*
  317. * Iterate over the endpoints and create one encoder for each output
  318. * pipeline.
  319. */
  320. for_each_endpoint_of_node(np, ep_node) {
  321. enum rcar_du_output output;
  322. struct of_endpoint ep;
  323. unsigned int i;
  324. int ret;
  325. ret = of_graph_parse_endpoint(ep_node, &ep);
  326. if (ret < 0) {
  327. of_node_put(ep_node);
  328. return ret;
  329. }
  330. /* Find the output route corresponding to the port number. */
  331. for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) {
  332. if (rcdu->info->routes[i].possible_crtcs &&
  333. rcdu->info->routes[i].port == ep.port) {
  334. output = i;
  335. break;
  336. }
  337. }
  338. if (i == RCAR_DU_OUTPUT_MAX) {
  339. dev_warn(rcdu->dev,
  340. "port %u references unexisting output, skipping\n",
  341. ep.port);
  342. continue;
  343. }
  344. /* Process the output pipeline. */
  345. ret = rcar_du_encoders_init_one(rcdu, output, &ep);
  346. if (ret < 0) {
  347. if (ret == -EPROBE_DEFER) {
  348. of_node_put(ep_node);
  349. return ret;
  350. }
  351. continue;
  352. }
  353. num_encoders++;
  354. }
  355. return num_encoders;
  356. }
  357. static int rcar_du_properties_init(struct rcar_du_device *rcdu)
  358. {
  359. rcdu->props.alpha =
  360. drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
  361. if (rcdu->props.alpha == NULL)
  362. return -ENOMEM;
  363. /*
  364. * The color key is expressed as an RGB888 triplet stored in a 32-bit
  365. * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
  366. * or enable source color keying (1).
  367. */
  368. rcdu->props.colorkey =
  369. drm_property_create_range(rcdu->ddev, 0, "colorkey",
  370. 0, 0x01ffffff);
  371. if (rcdu->props.colorkey == NULL)
  372. return -ENOMEM;
  373. return 0;
  374. }
  375. static int rcar_du_vsps_init(struct rcar_du_device *rcdu)
  376. {
  377. const struct device_node *np = rcdu->dev->of_node;
  378. struct of_phandle_args args;
  379. struct {
  380. struct device_node *np;
  381. unsigned int crtcs_mask;
  382. } vsps[RCAR_DU_MAX_VSPS] = { { 0, }, };
  383. unsigned int vsps_count = 0;
  384. unsigned int cells;
  385. unsigned int i;
  386. int ret;
  387. /*
  388. * First parse the DT vsps property to populate the list of VSPs. Each
  389. * entry contains a pointer to the VSP DT node and a bitmask of the
  390. * connected DU CRTCs.
  391. */
  392. cells = of_property_count_u32_elems(np, "vsps") / rcdu->num_crtcs - 1;
  393. if (cells > 1)
  394. return -EINVAL;
  395. for (i = 0; i < rcdu->num_crtcs; ++i) {
  396. unsigned int j;
  397. ret = of_parse_phandle_with_fixed_args(np, "vsps", cells, i,
  398. &args);
  399. if (ret < 0)
  400. goto error;
  401. /*
  402. * Add the VSP to the list or update the corresponding existing
  403. * entry if the VSP has already been added.
  404. */
  405. for (j = 0; j < vsps_count; ++j) {
  406. if (vsps[j].np == args.np)
  407. break;
  408. }
  409. if (j < vsps_count)
  410. of_node_put(args.np);
  411. else
  412. vsps[vsps_count++].np = args.np;
  413. vsps[j].crtcs_mask |= BIT(i);
  414. /* Store the VSP pointer and pipe index in the CRTC. */
  415. rcdu->crtcs[i].vsp = &rcdu->vsps[j];
  416. rcdu->crtcs[i].vsp_pipe = cells >= 1 ? args.args[0] : 0;
  417. }
  418. /*
  419. * Then initialize all the VSPs from the node pointers and CRTCs bitmask
  420. * computed previously.
  421. */
  422. for (i = 0; i < vsps_count; ++i) {
  423. struct rcar_du_vsp *vsp = &rcdu->vsps[i];
  424. vsp->index = i;
  425. vsp->dev = rcdu;
  426. ret = rcar_du_vsp_init(vsp, vsps[i].np, vsps[i].crtcs_mask);
  427. if (ret < 0)
  428. goto error;
  429. }
  430. return 0;
  431. error:
  432. for (i = 0; i < ARRAY_SIZE(vsps); ++i)
  433. of_node_put(vsps[i].np);
  434. return ret;
  435. }
  436. int rcar_du_modeset_init(struct rcar_du_device *rcdu)
  437. {
  438. static const unsigned int mmio_offsets[] = {
  439. DU0_REG_OFFSET, DU2_REG_OFFSET
  440. };
  441. struct drm_device *dev = rcdu->ddev;
  442. struct drm_encoder *encoder;
  443. struct drm_fbdev_cma *fbdev;
  444. unsigned int num_encoders;
  445. unsigned int num_groups;
  446. unsigned int i;
  447. int ret;
  448. drm_mode_config_init(dev);
  449. dev->mode_config.min_width = 0;
  450. dev->mode_config.min_height = 0;
  451. dev->mode_config.max_width = 4095;
  452. dev->mode_config.max_height = 2047;
  453. dev->mode_config.funcs = &rcar_du_mode_config_funcs;
  454. dev->mode_config.helper_private = &rcar_du_mode_config_helper;
  455. rcdu->num_crtcs = rcdu->info->num_crtcs;
  456. ret = rcar_du_properties_init(rcdu);
  457. if (ret < 0)
  458. return ret;
  459. /*
  460. * Initialize vertical blanking interrupts handling. Start with vblank
  461. * disabled for all CRTCs.
  462. */
  463. ret = drm_vblank_init(dev, (1 << rcdu->info->num_crtcs) - 1);
  464. if (ret < 0)
  465. return ret;
  466. /* Initialize the groups. */
  467. num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
  468. for (i = 0; i < num_groups; ++i) {
  469. struct rcar_du_group *rgrp = &rcdu->groups[i];
  470. mutex_init(&rgrp->lock);
  471. rgrp->dev = rcdu;
  472. rgrp->mmio_offset = mmio_offsets[i];
  473. rgrp->index = i;
  474. rgrp->num_crtcs = min(rcdu->num_crtcs - 2 * i, 2U);
  475. /*
  476. * If we have more than one CRTCs in this group pre-associate
  477. * the low-order planes with CRTC 0 and the high-order planes
  478. * with CRTC 1 to minimize flicker occurring when the
  479. * association is changed.
  480. */
  481. rgrp->dptsr_planes = rgrp->num_crtcs > 1
  482. ? (rcdu->info->gen >= 3 ? 0x04 : 0xf0)
  483. : 0;
  484. if (!rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
  485. ret = rcar_du_planes_init(rgrp);
  486. if (ret < 0)
  487. return ret;
  488. }
  489. }
  490. /* Initialize the compositors. */
  491. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
  492. ret = rcar_du_vsps_init(rcdu);
  493. if (ret < 0)
  494. return ret;
  495. }
  496. /* Create the CRTCs. */
  497. for (i = 0; i < rcdu->num_crtcs; ++i) {
  498. struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
  499. ret = rcar_du_crtc_create(rgrp, i);
  500. if (ret < 0)
  501. return ret;
  502. }
  503. /* Initialize the encoders. */
  504. ret = rcar_du_encoders_init(rcdu);
  505. if (ret < 0)
  506. return ret;
  507. if (ret == 0) {
  508. dev_err(rcdu->dev, "error: no encoder could be initialized\n");
  509. return -EINVAL;
  510. }
  511. num_encoders = ret;
  512. /*
  513. * Set the possible CRTCs and possible clones. There's always at least
  514. * one way for all encoders to clone each other, set all bits in the
  515. * possible clones field.
  516. */
  517. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  518. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  519. const struct rcar_du_output_routing *route =
  520. &rcdu->info->routes[renc->output];
  521. encoder->possible_crtcs = route->possible_crtcs;
  522. encoder->possible_clones = (1 << num_encoders) - 1;
  523. }
  524. drm_mode_config_reset(dev);
  525. drm_kms_helper_poll_init(dev);
  526. if (dev->mode_config.num_connector) {
  527. fbdev = drm_fbdev_cma_init(dev, 32,
  528. dev->mode_config.num_connector);
  529. if (IS_ERR(fbdev))
  530. return PTR_ERR(fbdev);
  531. rcdu->fbdev = fbdev;
  532. } else {
  533. dev_info(rcdu->dev,
  534. "no connector found, disabling fbdev emulation\n");
  535. }
  536. return 0;
  537. }