rcar_du_kms.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * rcar_du_kms.c -- R-Car Display Unit Mode Setting
  3. *
  4. * Copyright (C) 2013-2014 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_crtc.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include <drm/drm_fb_cma_helper.h>
  17. #include <drm/drm_gem_cma_helper.h>
  18. #include <linux/of_graph.h>
  19. #include "rcar_du_crtc.h"
  20. #include "rcar_du_drv.h"
  21. #include "rcar_du_encoder.h"
  22. #include "rcar_du_kms.h"
  23. #include "rcar_du_lvdsenc.h"
  24. #include "rcar_du_regs.h"
  25. /* -----------------------------------------------------------------------------
  26. * Format helpers
  27. */
  28. static const struct rcar_du_format_info rcar_du_format_infos[] = {
  29. {
  30. .fourcc = DRM_FORMAT_RGB565,
  31. .bpp = 16,
  32. .planes = 1,
  33. .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
  34. .edf = PnDDCR4_EDF_NONE,
  35. }, {
  36. .fourcc = DRM_FORMAT_ARGB1555,
  37. .bpp = 16,
  38. .planes = 1,
  39. .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
  40. .edf = PnDDCR4_EDF_NONE,
  41. }, {
  42. .fourcc = DRM_FORMAT_XRGB1555,
  43. .bpp = 16,
  44. .planes = 1,
  45. .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_ARGB,
  46. .edf = PnDDCR4_EDF_NONE,
  47. }, {
  48. .fourcc = DRM_FORMAT_XRGB8888,
  49. .bpp = 32,
  50. .planes = 1,
  51. .pnmr = PnMR_SPIM_TP | PnMR_DDDF_16BPP,
  52. .edf = PnDDCR4_EDF_RGB888,
  53. }, {
  54. .fourcc = DRM_FORMAT_ARGB8888,
  55. .bpp = 32,
  56. .planes = 1,
  57. .pnmr = PnMR_SPIM_ALP | PnMR_DDDF_16BPP,
  58. .edf = PnDDCR4_EDF_ARGB8888,
  59. }, {
  60. .fourcc = DRM_FORMAT_UYVY,
  61. .bpp = 16,
  62. .planes = 1,
  63. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  64. .edf = PnDDCR4_EDF_NONE,
  65. }, {
  66. .fourcc = DRM_FORMAT_YUYV,
  67. .bpp = 16,
  68. .planes = 1,
  69. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  70. .edf = PnDDCR4_EDF_NONE,
  71. }, {
  72. .fourcc = DRM_FORMAT_NV12,
  73. .bpp = 12,
  74. .planes = 2,
  75. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  76. .edf = PnDDCR4_EDF_NONE,
  77. }, {
  78. .fourcc = DRM_FORMAT_NV21,
  79. .bpp = 12,
  80. .planes = 2,
  81. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  82. .edf = PnDDCR4_EDF_NONE,
  83. }, {
  84. /* In YUV 4:2:2, only NV16 is supported (NV61 isn't) */
  85. .fourcc = DRM_FORMAT_NV16,
  86. .bpp = 16,
  87. .planes = 2,
  88. .pnmr = PnMR_SPIM_TP_OFF | PnMR_DDDF_YC,
  89. .edf = PnDDCR4_EDF_NONE,
  90. },
  91. };
  92. const struct rcar_du_format_info *rcar_du_format_info(u32 fourcc)
  93. {
  94. unsigned int i;
  95. for (i = 0; i < ARRAY_SIZE(rcar_du_format_infos); ++i) {
  96. if (rcar_du_format_infos[i].fourcc == fourcc)
  97. return &rcar_du_format_infos[i];
  98. }
  99. return NULL;
  100. }
  101. /* -----------------------------------------------------------------------------
  102. * Frame buffer
  103. */
  104. int rcar_du_dumb_create(struct drm_file *file, struct drm_device *dev,
  105. struct drm_mode_create_dumb *args)
  106. {
  107. struct rcar_du_device *rcdu = dev->dev_private;
  108. unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
  109. unsigned int align;
  110. /* The R8A7779 DU requires a 16 pixels pitch alignment as documented,
  111. * but the R8A7790 DU seems to require a 128 bytes pitch alignment.
  112. */
  113. if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
  114. align = 128;
  115. else
  116. align = 16 * args->bpp / 8;
  117. args->pitch = roundup(min_pitch, align);
  118. return drm_gem_cma_dumb_create_internal(file, dev, args);
  119. }
  120. static struct drm_framebuffer *
  121. rcar_du_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  122. struct drm_mode_fb_cmd2 *mode_cmd)
  123. {
  124. struct rcar_du_device *rcdu = dev->dev_private;
  125. const struct rcar_du_format_info *format;
  126. unsigned int max_pitch;
  127. unsigned int align;
  128. unsigned int bpp;
  129. format = rcar_du_format_info(mode_cmd->pixel_format);
  130. if (format == NULL) {
  131. dev_dbg(dev->dev, "unsupported pixel format %08x\n",
  132. mode_cmd->pixel_format);
  133. return ERR_PTR(-EINVAL);
  134. }
  135. /*
  136. * The pitch and alignment constraints are expressed in pixels on the
  137. * hardware side and in bytes in the DRM API.
  138. */
  139. bpp = format->planes == 2 ? 1 : format->bpp / 8;
  140. max_pitch = 4096 * bpp;
  141. if (rcar_du_needs(rcdu, RCAR_DU_QUIRK_ALIGN_128B))
  142. align = 128;
  143. else
  144. align = 16 * bpp;
  145. if (mode_cmd->pitches[0] & (align - 1) ||
  146. mode_cmd->pitches[0] >= max_pitch) {
  147. dev_dbg(dev->dev, "invalid pitch value %u\n",
  148. mode_cmd->pitches[0]);
  149. return ERR_PTR(-EINVAL);
  150. }
  151. if (format->planes == 2) {
  152. if (mode_cmd->pitches[1] != mode_cmd->pitches[0]) {
  153. dev_dbg(dev->dev,
  154. "luma and chroma pitches do not match\n");
  155. return ERR_PTR(-EINVAL);
  156. }
  157. }
  158. return drm_fb_cma_create(dev, file_priv, mode_cmd);
  159. }
  160. static void rcar_du_output_poll_changed(struct drm_device *dev)
  161. {
  162. struct rcar_du_device *rcdu = dev->dev_private;
  163. drm_fbdev_cma_hotplug_event(rcdu->fbdev);
  164. }
  165. static const struct drm_mode_config_funcs rcar_du_mode_config_funcs = {
  166. .fb_create = rcar_du_fb_create,
  167. .output_poll_changed = rcar_du_output_poll_changed,
  168. };
  169. static int rcar_du_encoders_init_one(struct rcar_du_device *rcdu,
  170. enum rcar_du_output output,
  171. struct of_endpoint *ep)
  172. {
  173. static const struct {
  174. const char *compatible;
  175. enum rcar_du_encoder_type type;
  176. } encoders[] = {
  177. { "adi,adv7123", RCAR_DU_ENCODER_VGA },
  178. { "adi,adv7511w", RCAR_DU_ENCODER_HDMI },
  179. { "thine,thc63lvdm83d", RCAR_DU_ENCODER_LVDS },
  180. };
  181. enum rcar_du_encoder_type enc_type = RCAR_DU_ENCODER_NONE;
  182. struct device_node *connector = NULL;
  183. struct device_node *encoder = NULL;
  184. struct device_node *prev = NULL;
  185. struct device_node *entity_ep_node;
  186. struct device_node *entity;
  187. int ret;
  188. /*
  189. * Locate the connected entity and infer its type from the number of
  190. * endpoints.
  191. */
  192. entity = of_graph_get_remote_port_parent(ep->local_node);
  193. if (!entity) {
  194. dev_dbg(rcdu->dev, "unconnected endpoint %s, skipping\n",
  195. ep->local_node->full_name);
  196. return 0;
  197. }
  198. entity_ep_node = of_parse_phandle(ep->local_node, "remote-endpoint", 0);
  199. while (1) {
  200. struct device_node *ep_node;
  201. ep_node = of_graph_get_next_endpoint(entity, prev);
  202. of_node_put(prev);
  203. prev = ep_node;
  204. if (!ep_node)
  205. break;
  206. if (ep_node == entity_ep_node)
  207. continue;
  208. /*
  209. * We've found one endpoint other than the input, this must
  210. * be an encoder. Locate the connector.
  211. */
  212. encoder = entity;
  213. connector = of_graph_get_remote_port_parent(ep_node);
  214. of_node_put(ep_node);
  215. if (!connector) {
  216. dev_warn(rcdu->dev,
  217. "no connector for encoder %s, skipping\n",
  218. encoder->full_name);
  219. of_node_put(entity_ep_node);
  220. of_node_put(encoder);
  221. return 0;
  222. }
  223. break;
  224. }
  225. of_node_put(entity_ep_node);
  226. if (encoder) {
  227. /*
  228. * If an encoder has been found, get its type based on its
  229. * compatible string.
  230. */
  231. unsigned int i;
  232. for (i = 0; i < ARRAY_SIZE(encoders); ++i) {
  233. if (of_device_is_compatible(encoder,
  234. encoders[i].compatible)) {
  235. enc_type = encoders[i].type;
  236. break;
  237. }
  238. }
  239. if (i == ARRAY_SIZE(encoders)) {
  240. dev_warn(rcdu->dev,
  241. "unknown encoder type for %s, skipping\n",
  242. encoder->full_name);
  243. of_node_put(encoder);
  244. of_node_put(connector);
  245. return 0;
  246. }
  247. } else {
  248. /*
  249. * If no encoder has been found the entity must be the
  250. * connector.
  251. */
  252. connector = entity;
  253. }
  254. ret = rcar_du_encoder_init(rcdu, enc_type, output, encoder, connector);
  255. of_node_put(encoder);
  256. of_node_put(connector);
  257. return ret < 0 ? ret : 1;
  258. }
  259. static int rcar_du_encoders_init(struct rcar_du_device *rcdu)
  260. {
  261. struct device_node *np = rcdu->dev->of_node;
  262. struct device_node *prev = NULL;
  263. unsigned int num_encoders = 0;
  264. /*
  265. * Iterate over the endpoints and create one encoder for each output
  266. * pipeline.
  267. */
  268. while (1) {
  269. struct device_node *ep_node;
  270. enum rcar_du_output output;
  271. struct of_endpoint ep;
  272. unsigned int i;
  273. int ret;
  274. ep_node = of_graph_get_next_endpoint(np, prev);
  275. of_node_put(prev);
  276. prev = ep_node;
  277. if (ep_node == NULL)
  278. break;
  279. ret = of_graph_parse_endpoint(ep_node, &ep);
  280. if (ret < 0) {
  281. of_node_put(ep_node);
  282. return ret;
  283. }
  284. /* Find the output route corresponding to the port number. */
  285. for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) {
  286. if (rcdu->info->routes[i].possible_crtcs &&
  287. rcdu->info->routes[i].port == ep.port) {
  288. output = i;
  289. break;
  290. }
  291. }
  292. if (i == RCAR_DU_OUTPUT_MAX) {
  293. dev_warn(rcdu->dev,
  294. "port %u references unexisting output, skipping\n",
  295. ep.port);
  296. continue;
  297. }
  298. /* Process the output pipeline. */
  299. ret = rcar_du_encoders_init_one(rcdu, output, &ep);
  300. if (ret < 0) {
  301. of_node_put(ep_node);
  302. return ret;
  303. }
  304. num_encoders += ret;
  305. }
  306. return num_encoders;
  307. }
  308. int rcar_du_modeset_init(struct rcar_du_device *rcdu)
  309. {
  310. static const unsigned int mmio_offsets[] = {
  311. DU0_REG_OFFSET, DU2_REG_OFFSET
  312. };
  313. struct drm_device *dev = rcdu->ddev;
  314. struct drm_encoder *encoder;
  315. struct drm_fbdev_cma *fbdev;
  316. unsigned int num_encoders;
  317. unsigned int num_groups;
  318. unsigned int i;
  319. int ret;
  320. drm_mode_config_init(dev);
  321. dev->mode_config.min_width = 0;
  322. dev->mode_config.min_height = 0;
  323. dev->mode_config.max_width = 4095;
  324. dev->mode_config.max_height = 2047;
  325. dev->mode_config.funcs = &rcar_du_mode_config_funcs;
  326. rcdu->num_crtcs = rcdu->info->num_crtcs;
  327. /* Initialize the groups. */
  328. num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
  329. for (i = 0; i < num_groups; ++i) {
  330. struct rcar_du_group *rgrp = &rcdu->groups[i];
  331. rgrp->dev = rcdu;
  332. rgrp->mmio_offset = mmio_offsets[i];
  333. rgrp->index = i;
  334. ret = rcar_du_planes_init(rgrp);
  335. if (ret < 0)
  336. return ret;
  337. }
  338. /* Create the CRTCs. */
  339. for (i = 0; i < rcdu->num_crtcs; ++i) {
  340. struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
  341. ret = rcar_du_crtc_create(rgrp, i);
  342. if (ret < 0)
  343. return ret;
  344. }
  345. /* Initialize the encoders. */
  346. ret = rcar_du_lvdsenc_init(rcdu);
  347. if (ret < 0)
  348. return ret;
  349. ret = rcar_du_encoders_init(rcdu);
  350. if (ret < 0)
  351. return ret;
  352. num_encoders = ret;
  353. /* Set the possible CRTCs and possible clones. There's always at least
  354. * one way for all encoders to clone each other, set all bits in the
  355. * possible clones field.
  356. */
  357. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  358. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  359. const struct rcar_du_output_routing *route =
  360. &rcdu->info->routes[renc->output];
  361. encoder->possible_crtcs = route->possible_crtcs;
  362. encoder->possible_clones = (1 << num_encoders) - 1;
  363. }
  364. /* Now that the CRTCs have been initialized register the planes. */
  365. for (i = 0; i < num_groups; ++i) {
  366. ret = rcar_du_planes_register(&rcdu->groups[i]);
  367. if (ret < 0)
  368. return ret;
  369. }
  370. drm_kms_helper_poll_init(dev);
  371. drm_helper_disable_unused_functions(dev);
  372. fbdev = drm_fbdev_cma_init(dev, 32, dev->mode_config.num_crtc,
  373. dev->mode_config.num_connector);
  374. if (IS_ERR(fbdev))
  375. return PTR_ERR(fbdev);
  376. #ifndef CONFIG_FRAMEBUFFER_CONSOLE
  377. drm_fbdev_cma_restore_mode(fbdev);
  378. #endif
  379. rcdu->fbdev = fbdev;
  380. return 0;
  381. }