rcar_du_kms.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 <linux/of_graph.h>
  21. #include <linux/wait.h>
  22. #include "rcar_du_crtc.h"
  23. #include "rcar_du_drv.h"
  24. #include "rcar_du_encoder.h"
  25. #include "rcar_du_kms.h"
  26. #include "rcar_du_lvdsenc.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_fb_cma_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. /*
  298. * If no encoder has been found the entity must be the
  299. * connector.
  300. */
  301. connector = entity;
  302. }
  303. ret = rcar_du_encoder_init(rcdu, output, encoder, connector);
  304. if (ret && ret != -EPROBE_DEFER)
  305. dev_warn(rcdu->dev,
  306. "failed to initialize encoder %pOF on output %u (%d), skipping\n",
  307. encoder, output, ret);
  308. of_node_put(encoder);
  309. of_node_put(connector);
  310. return ret;
  311. }
  312. static int rcar_du_encoders_init(struct rcar_du_device *rcdu)
  313. {
  314. struct device_node *np = rcdu->dev->of_node;
  315. struct device_node *ep_node;
  316. unsigned int num_encoders = 0;
  317. /*
  318. * Iterate over the endpoints and create one encoder for each output
  319. * pipeline.
  320. */
  321. for_each_endpoint_of_node(np, ep_node) {
  322. enum rcar_du_output output;
  323. struct of_endpoint ep;
  324. unsigned int i;
  325. int ret;
  326. ret = of_graph_parse_endpoint(ep_node, &ep);
  327. if (ret < 0) {
  328. of_node_put(ep_node);
  329. return ret;
  330. }
  331. /* Find the output route corresponding to the port number. */
  332. for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) {
  333. if (rcdu->info->routes[i].possible_crtcs &&
  334. rcdu->info->routes[i].port == ep.port) {
  335. output = i;
  336. break;
  337. }
  338. }
  339. if (i == RCAR_DU_OUTPUT_MAX) {
  340. dev_warn(rcdu->dev,
  341. "port %u references unexisting output, skipping\n",
  342. ep.port);
  343. continue;
  344. }
  345. /* Process the output pipeline. */
  346. ret = rcar_du_encoders_init_one(rcdu, output, &ep);
  347. if (ret < 0) {
  348. if (ret == -EPROBE_DEFER) {
  349. of_node_put(ep_node);
  350. return ret;
  351. }
  352. continue;
  353. }
  354. num_encoders++;
  355. }
  356. return num_encoders;
  357. }
  358. static int rcar_du_properties_init(struct rcar_du_device *rcdu)
  359. {
  360. rcdu->props.alpha =
  361. drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
  362. if (rcdu->props.alpha == NULL)
  363. return -ENOMEM;
  364. /*
  365. * The color key is expressed as an RGB888 triplet stored in a 32-bit
  366. * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
  367. * or enable source color keying (1).
  368. */
  369. rcdu->props.colorkey =
  370. drm_property_create_range(rcdu->ddev, 0, "colorkey",
  371. 0, 0x01ffffff);
  372. if (rcdu->props.colorkey == NULL)
  373. return -ENOMEM;
  374. return 0;
  375. }
  376. static int rcar_du_vsps_init(struct rcar_du_device *rcdu)
  377. {
  378. const struct device_node *np = rcdu->dev->of_node;
  379. struct of_phandle_args args;
  380. struct {
  381. struct device_node *np;
  382. unsigned int crtcs_mask;
  383. } vsps[RCAR_DU_MAX_VSPS] = { { 0, }, };
  384. unsigned int vsps_count = 0;
  385. unsigned int cells;
  386. unsigned int i;
  387. int ret;
  388. /*
  389. * First parse the DT vsps property to populate the list of VSPs. Each
  390. * entry contains a pointer to the VSP DT node and a bitmask of the
  391. * connected DU CRTCs.
  392. */
  393. cells = of_property_count_u32_elems(np, "vsps") / rcdu->num_crtcs - 1;
  394. if (cells > 1)
  395. return -EINVAL;
  396. for (i = 0; i < rcdu->num_crtcs; ++i) {
  397. unsigned int j;
  398. ret = of_parse_phandle_with_fixed_args(np, "vsps", cells, i,
  399. &args);
  400. if (ret < 0)
  401. goto error;
  402. /*
  403. * Add the VSP to the list or update the corresponding existing
  404. * entry if the VSP has already been added.
  405. */
  406. for (j = 0; j < vsps_count; ++j) {
  407. if (vsps[j].np == args.np)
  408. break;
  409. }
  410. if (j < vsps_count)
  411. of_node_put(args.np);
  412. else
  413. vsps[vsps_count++].np = args.np;
  414. vsps[j].crtcs_mask |= BIT(i);
  415. /* Store the VSP pointer and pipe index in the CRTC. */
  416. rcdu->crtcs[i].vsp = &rcdu->vsps[j];
  417. rcdu->crtcs[i].vsp_pipe = cells >= 1 ? args.args[0] : 0;
  418. }
  419. /*
  420. * Then initialize all the VSPs from the node pointers and CRTCs bitmask
  421. * computed previously.
  422. */
  423. for (i = 0; i < vsps_count; ++i) {
  424. struct rcar_du_vsp *vsp = &rcdu->vsps[i];
  425. vsp->index = i;
  426. vsp->dev = rcdu;
  427. ret = rcar_du_vsp_init(vsp, vsps[i].np, vsps[i].crtcs_mask);
  428. if (ret < 0)
  429. goto error;
  430. }
  431. return 0;
  432. error:
  433. for (i = 0; i < ARRAY_SIZE(vsps); ++i)
  434. of_node_put(vsps[i].np);
  435. return ret;
  436. }
  437. int rcar_du_modeset_init(struct rcar_du_device *rcdu)
  438. {
  439. static const unsigned int mmio_offsets[] = {
  440. DU0_REG_OFFSET, DU2_REG_OFFSET
  441. };
  442. struct drm_device *dev = rcdu->ddev;
  443. struct drm_encoder *encoder;
  444. struct drm_fbdev_cma *fbdev;
  445. unsigned int num_encoders;
  446. unsigned int num_groups;
  447. unsigned int i;
  448. int ret;
  449. drm_mode_config_init(dev);
  450. dev->mode_config.min_width = 0;
  451. dev->mode_config.min_height = 0;
  452. dev->mode_config.max_width = 4095;
  453. dev->mode_config.max_height = 2047;
  454. dev->mode_config.funcs = &rcar_du_mode_config_funcs;
  455. dev->mode_config.helper_private = &rcar_du_mode_config_helper;
  456. rcdu->num_crtcs = rcdu->info->num_crtcs;
  457. ret = rcar_du_properties_init(rcdu);
  458. if (ret < 0)
  459. return ret;
  460. /*
  461. * Initialize vertical blanking interrupts handling. Start with vblank
  462. * disabled for all CRTCs.
  463. */
  464. ret = drm_vblank_init(dev, (1 << rcdu->info->num_crtcs) - 1);
  465. if (ret < 0)
  466. return ret;
  467. /* Initialize the groups. */
  468. num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
  469. for (i = 0; i < num_groups; ++i) {
  470. struct rcar_du_group *rgrp = &rcdu->groups[i];
  471. mutex_init(&rgrp->lock);
  472. rgrp->dev = rcdu;
  473. rgrp->mmio_offset = mmio_offsets[i];
  474. rgrp->index = i;
  475. rgrp->num_crtcs = min(rcdu->num_crtcs - 2 * i, 2U);
  476. /*
  477. * If we have more than one CRTCs in this group pre-associate
  478. * the low-order planes with CRTC 0 and the high-order planes
  479. * with CRTC 1 to minimize flicker occurring when the
  480. * association is changed.
  481. */
  482. rgrp->dptsr_planes = rgrp->num_crtcs > 1
  483. ? (rcdu->info->gen >= 3 ? 0x04 : 0xf0)
  484. : 0;
  485. if (!rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
  486. ret = rcar_du_planes_init(rgrp);
  487. if (ret < 0)
  488. return ret;
  489. }
  490. }
  491. /* Initialize the compositors. */
  492. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
  493. ret = rcar_du_vsps_init(rcdu);
  494. if (ret < 0)
  495. return ret;
  496. }
  497. /* Create the CRTCs. */
  498. for (i = 0; i < rcdu->num_crtcs; ++i) {
  499. struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
  500. ret = rcar_du_crtc_create(rgrp, i);
  501. if (ret < 0)
  502. return ret;
  503. }
  504. /* Initialize the encoders. */
  505. ret = rcar_du_lvdsenc_init(rcdu);
  506. if (ret < 0)
  507. return ret;
  508. ret = rcar_du_encoders_init(rcdu);
  509. if (ret < 0)
  510. return ret;
  511. if (ret == 0) {
  512. dev_err(rcdu->dev, "error: no encoder could be initialized\n");
  513. return -EINVAL;
  514. }
  515. num_encoders = ret;
  516. /*
  517. * Set the possible CRTCs and possible clones. There's always at least
  518. * one way for all encoders to clone each other, set all bits in the
  519. * possible clones field.
  520. */
  521. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  522. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  523. const struct rcar_du_output_routing *route =
  524. &rcdu->info->routes[renc->output];
  525. encoder->possible_crtcs = route->possible_crtcs;
  526. encoder->possible_clones = (1 << num_encoders) - 1;
  527. }
  528. drm_mode_config_reset(dev);
  529. drm_kms_helper_poll_init(dev);
  530. if (dev->mode_config.num_connector) {
  531. fbdev = drm_fbdev_cma_init(dev, 32,
  532. dev->mode_config.num_connector);
  533. if (IS_ERR(fbdev))
  534. return PTR_ERR(fbdev);
  535. rcdu->fbdev = fbdev;
  536. } else {
  537. dev_info(rcdu->dev,
  538. "no connector found, disabling fbdev emulation\n");
  539. }
  540. return 0;
  541. }