rcar_du_kms.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. /* The following formats are not supported on Gen2 and thus have no
  95. * associated .pnmr or .edf settings.
  96. */
  97. {
  98. .fourcc = DRM_FORMAT_NV61,
  99. .bpp = 16,
  100. .planes = 2,
  101. }, {
  102. .fourcc = DRM_FORMAT_YUV420,
  103. .bpp = 12,
  104. .planes = 3,
  105. }, {
  106. .fourcc = DRM_FORMAT_YVU420,
  107. .bpp = 12,
  108. .planes = 3,
  109. }, {
  110. .fourcc = DRM_FORMAT_YUV422,
  111. .bpp = 16,
  112. .planes = 3,
  113. }, {
  114. .fourcc = DRM_FORMAT_YVU422,
  115. .bpp = 16,
  116. .planes = 3,
  117. }, {
  118. .fourcc = DRM_FORMAT_YUV444,
  119. .bpp = 24,
  120. .planes = 3,
  121. }, {
  122. .fourcc = DRM_FORMAT_YVU444,
  123. .bpp = 24,
  124. .planes = 3,
  125. },
  126. };
  127. const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc)
  128. {
  129. unsigned int i;
  130. for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) {
  131. if (rcar_du_format_infos[i].fourcc == fourcc)
  132. return &rcar_du_format_infos[i];
  133. }
  134. return NULL;
  135. }
  136. /* -----------------------------------------------------------------------------
  137. * Frame buffer
  138. */
  139. int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev,
  140. struct drm_mode_create_dumb *args)
  141. {
  142. struct rcar_du_device *rcdu = dev->dev_private;
  143. unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
  144. unsigned int align;
  145. /* The R8A7779 DU requires a 16 pixels pitch alignment as documented,
  146. * but the R8A7790 DU seems to require a 128 bytes pitch alignment.
  147. */
  148. if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
  149. align = 128;
  150. else
  151. align = 16 * args->bpp / 8;
  152. args->pitch = roundup(min_pitch, align);
  153. return drm_gem_cma_dumb_create_internal(file, dev, args);
  154. }
  155. static struct drm_framebuffer *
  156. rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  157. const struct drm_mode_fb_cmd2 *mode_cmd)
  158. {
  159. struct rcar_du_device *rcdu = dev->dev_private;
  160. const struct rcar_du_format_info *format;
  161. unsigned int max_pitch;
  162. unsigned int align;
  163. unsigned int bpp;
  164. unsigned int i;
  165. format = rcar_du_format_info(mode_cmd->pixel_format);
  166. if (format == NULL) {
  167. dev_dbg(dev->dev, "unsupported pixel format %08x\n",
  168. mode_cmd->pixel_format);
  169. return ERR_PTR(-EINVAL);
  170. }
  171. /*
  172. * The pitch and alignment constraints are expressed in pixels on the
  173. * hardware side and in bytes in the DRM API.
  174. */
  175. bpp = format->planes == 1 ? format->bpp / 8 : 1;
  176. max_pitch = 4096 * bpp;
  177. if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
  178. align = 128;
  179. else
  180. align = 16 * bpp;
  181. if (mode_cmd->pitches[0] & (align - 1) ||
  182. mode_cmd->pitches[0] >= max_pitch) {
  183. dev_dbg(dev->dev, "invalid pitch value %u\n",
  184. mode_cmd->pitches[0]);
  185. return ERR_PTR(-EINVAL);
  186. }
  187. for (i = 1; i < format->planes; ++i) {
  188. if (mode_cmd->pitches[i] != mode_cmd->pitches[0]) {
  189. dev_dbg(dev->dev,
  190. "luma and chroma pitches do not match\n");
  191. return ERR_PTR(-EINVAL);
  192. }
  193. }
  194. return drm_fb_cma_create(dev, file_priv, mode_cmd);
  195. }
  196. static void rcar_du_output_poll_changed(struct drm_device *dev)
  197. {
  198. struct rcar_du_device *rcdu = dev->dev_private;
  199. drm_fbdev_cma_hotplug_event(rcdu->fbdev);
  200. }
  201. /* -----------------------------------------------------------------------------
  202. * Atomic Check and Update
  203. */
  204. static int rcar_du_atomic_check(struct drm_device *dev,
  205. struct drm_atomic_state *state)
  206. {
  207. struct rcar_du_device *rcdu = dev->dev_private;
  208. int ret;
  209. ret = drm_atomic_helper_check_modeset(dev, state);
  210. if (ret)
  211. return ret;
  212. ret = drm_atomic_normalize_zpos(dev, state);
  213. if (ret)
  214. return ret;
  215. ret = drm_atomic_helper_check_planes(dev, state);
  216. if (ret)
  217. return ret;
  218. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE))
  219. return 0;
  220. return rcar_du_atomic_check_planes(dev, state);
  221. }
  222. static void rcar_du_atomic_commit_tail(struct drm_atomic_state *old_state)
  223. {
  224. struct drm_device *dev = old_state->dev;
  225. /* Apply the atomic update. */
  226. drm_atomic_helper_commit_modeset_disables(dev, old_state);
  227. drm_atomic_helper_commit_modeset_enables(dev, old_state);
  228. drm_atomic_helper_commit_planes(dev, old_state,
  229. DRM_PLANE_COMMIT_ACTIVE_ONLY);
  230. drm_atomic_helper_commit_hw_done(old_state);
  231. drm_atomic_helper_wait_for_vblanks(dev, old_state);
  232. drm_atomic_helper_cleanup_planes(dev, old_state);
  233. }
  234. /* -----------------------------------------------------------------------------
  235. * Initialization
  236. */
  237. static const struct drm_mode_config_helper_funcs rcar_du_mode_config_helper = {
  238. .atomic_commit_tail = rcar_du_atomic_commit_tail,
  239. };
  240. static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
  241. .fb_create = rcar_du_fb_create,
  242. .output_poll_changed = rcar_du_output_poll_changed,
  243. .atomic_check = rcar_du_atomic_check,
  244. .atomic_commit = drm_atomic_helper_commit,
  245. };
  246. static int rcar_du_encoders_init_one(struct rcar_du_device *rcdu,
  247. enum rcar_du_output output,
  248. struct of_endpoint *ep)
  249. {
  250. struct device_node *connector = NULL;
  251. struct device_node *encoder = NULL;
  252. struct device_node *ep_node = NULL;
  253. struct device_node *entity_ep_node;
  254. struct device_node *entity;
  255. int ret;
  256. /*
  257. * Locate the connected entity and infer its type from the number of
  258. * endpoints.
  259. */
  260. entity = of_graph_get_remote_port_parent(ep->local_node);
  261. if (!entity) {
  262. dev_dbg(rcdu->dev, "unconnected endpoint %s, skipping\n",
  263. ep->local_node->full_name);
  264. return -ENODEV;
  265. }
  266. if (!of_device_is_available(entity)) {
  267. dev_dbg(rcdu->dev,
  268. "connected entity %s is disabled, skipping\n",
  269. entity->full_name);
  270. return -ENODEV;
  271. }
  272. entity_ep_node = of_parse_phandle(ep->local_node, "remote-endpoint", 0);
  273. for_each_endpoint_of_node(entity, ep_node) {
  274. if (ep_node == entity_ep_node)
  275. continue;
  276. /*
  277. * We've found one endpoint other than the input, this must
  278. * be an encoder. Locate the connector.
  279. */
  280. encoder = entity;
  281. connector = of_graph_get_remote_port_parent(ep_node);
  282. of_node_put(ep_node);
  283. if (!connector) {
  284. dev_warn(rcdu->dev,
  285. "no connector for encoder %s, skipping\n",
  286. encoder->full_name);
  287. of_node_put(entity_ep_node);
  288. of_node_put(encoder);
  289. return -ENODEV;
  290. }
  291. break;
  292. }
  293. of_node_put(entity_ep_node);
  294. if (!encoder) {
  295. /*
  296. * If no encoder has been found the entity must be the
  297. * connector.
  298. */
  299. connector = entity;
  300. }
  301. ret = rcar_du_encoder_init(rcdu, output, encoder, connector);
  302. if (ret && ret != -EPROBE_DEFER)
  303. dev_warn(rcdu->dev,
  304. "failed to initialize encoder %s on output %u (%d), skipping\n",
  305. of_node_full_name(encoder), output, ret);
  306. of_node_put(encoder);
  307. of_node_put(connector);
  308. return ret;
  309. }
  310. static int rcar_du_encoders_init(struct rcar_du_device *rcdu)
  311. {
  312. struct device_node *np = rcdu->dev->of_node;
  313. struct device_node *ep_node;
  314. unsigned int num_encoders = 0;
  315. /*
  316. * Iterate over the endpoints and create one encoder for each output
  317. * pipeline.
  318. */
  319. for_each_endpoint_of_node(np, ep_node) {
  320. enum rcar_du_output output;
  321. struct of_endpoint ep;
  322. unsigned int i;
  323. int ret;
  324. ret = of_graph_parse_endpoint(ep_node, &ep);
  325. if (ret < 0) {
  326. of_node_put(ep_node);
  327. return ret;
  328. }
  329. /* Find the output route corresponding to the port number. */
  330. for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) {
  331. if (rcdu->info->routes[i].possible_crtcs &&
  332. rcdu->info->routes[i].port == ep.port) {
  333. output = i;
  334. break;
  335. }
  336. }
  337. if (i == RCAR_DU_OUTPUT_MAX) {
  338. dev_warn(rcdu->dev,
  339. "port %u references unexisting output, skipping\n",
  340. ep.port);
  341. continue;
  342. }
  343. /* Process the output pipeline. */
  344. ret = rcar_du_encoders_init_one(rcdu, output, &ep);
  345. if (ret < 0) {
  346. if (ret == -EPROBE_DEFER) {
  347. of_node_put(ep_node);
  348. return ret;
  349. }
  350. continue;
  351. }
  352. num_encoders++;
  353. }
  354. return num_encoders;
  355. }
  356. static int rcar_du_properties_init(struct rcar_du_device *rcdu)
  357. {
  358. rcdu->props.alpha =
  359. drm_property_create_range(rcdu->ddev, 0, "alpha", 0, 255);
  360. if (rcdu->props.alpha == NULL)
  361. return -ENOMEM;
  362. /* The color key is expressed as an RGB888 triplet stored in a 32-bit
  363. * integer in XRGB8888 format. Bit 24 is used as a flag to disable (0)
  364. * or enable source color keying (1).
  365. */
  366. rcdu->props.colorkey =
  367. drm_property_create_range(rcdu->ddev, 0, "colorkey",
  368. 0, 0x01ffffff);
  369. if (rcdu->props.colorkey == NULL)
  370. return -ENOMEM;
  371. return 0;
  372. }
  373. int rcar_du_modeset_init(struct rcar_du_device *rcdu)
  374. {
  375. static const unsigned int mmio_offsets[] = {
  376. DU0_REG_OFFSET, DU2_REG_OFFSET
  377. };
  378. struct drm_device *dev = rcdu->ddev;
  379. struct drm_encoder *encoder;
  380. struct drm_fbdev_cma *fbdev;
  381. unsigned int num_encoders;
  382. unsigned int num_groups;
  383. unsigned int i;
  384. int ret;
  385. drm_mode_config_init(dev);
  386. dev->mode_config.min_width = 0;
  387. dev->mode_config.min_height = 0;
  388. dev->mode_config.max_width = 4095;
  389. dev->mode_config.max_height = 2047;
  390. dev->mode_config.funcs = &rcar_du_mode_config_funcs;
  391. dev->mode_config.helper_private = &rcar_du_mode_config_helper;
  392. rcdu->num_crtcs = rcdu->info->num_crtcs;
  393. ret = rcar_du_properties_init(rcdu);
  394. if (ret < 0)
  395. return ret;
  396. /* Initialize vertical blanking interrupts handling. Start with vblank
  397. * disabled for all CRTCs.
  398. */
  399. ret = drm_vblank_init(dev, (1 << rcdu->info->num_crtcs) - 1);
  400. if (ret < 0)
  401. return ret;
  402. /* Initialize the groups. */
  403. num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
  404. for (i = 0; i < num_groups; ++i) {
  405. struct rcar_du_group *rgrp = &rcdu->groups[i];
  406. mutex_init(&rgrp->lock);
  407. rgrp->dev = rcdu;
  408. rgrp->mmio_offset = mmio_offsets[i];
  409. rgrp->index = i;
  410. rgrp->num_crtcs = min(rcdu->num_crtcs - 2 * i, 2U);
  411. /* If we have more than one CRTCs in this group pre-associate
  412. * the low-order planes with CRTC 0 and the high-order planes
  413. * with CRTC 1 to minimize flicker occurring when the
  414. * association is changed.
  415. */
  416. rgrp->dptsr_planes = rgrp->num_crtcs > 1
  417. ? (rcdu->info->gen >= 3 ? 0x04 : 0xf0)
  418. : 0;
  419. if (!rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
  420. ret = rcar_du_planes_init(rgrp);
  421. if (ret < 0)
  422. return ret;
  423. }
  424. }
  425. /* Initialize the compositors. */
  426. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
  427. for (i = 0; i < rcdu->num_crtcs; ++i) {
  428. struct rcar_du_vsp *vsp = &rcdu->vsps[i];
  429. vsp->index = i;
  430. vsp->dev = rcdu;
  431. rcdu->crtcs[i].vsp = vsp;
  432. ret = rcar_du_vsp_init(vsp);
  433. if (ret < 0)
  434. return ret;
  435. }
  436. }
  437. /* Create the CRTCs. */
  438. for (i = 0; i < rcdu->num_crtcs; ++i) {
  439. struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
  440. ret = rcar_du_crtc_create(rgrp, i);
  441. if (ret < 0)
  442. return ret;
  443. }
  444. /* Initialize the encoders. */
  445. ret = rcar_du_lvdsenc_init(rcdu);
  446. if (ret < 0)
  447. return ret;
  448. ret = rcar_du_encoders_init(rcdu);
  449. if (ret < 0)
  450. return ret;
  451. if (ret == 0) {
  452. dev_err(rcdu->dev, "error: no encoder could be initialized\n");
  453. return -EINVAL;
  454. }
  455. num_encoders = ret;
  456. /* Set the possible CRTCs and possible clones. There's always at least
  457. * one way for all encoders to clone each other, set all bits in the
  458. * possible clones field.
  459. */
  460. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  461. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  462. const struct rcar_du_output_routing *route =
  463. &rcdu->info->routes[renc->output];
  464. encoder->possible_crtcs = route->possible_crtcs;
  465. encoder->possible_clones = (1 << num_encoders) - 1;
  466. }
  467. drm_mode_config_reset(dev);
  468. drm_kms_helper_poll_init(dev);
  469. if (dev->mode_config.num_connector) {
  470. fbdev = drm_fbdev_cma_init(dev, 32,
  471. dev->mode_config.num_connector);
  472. if (IS_ERR(fbdev))
  473. return PTR_ERR(fbdev);
  474. rcdu->fbdev = fbdev;
  475. } else {
  476. dev_info(rcdu->dev,
  477. "no connector found, disabling fbdev emulation\n");
  478. }
  479. return 0;
  480. }