rcar_du_kms.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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(max(args->pitch, min_pitch), align);
  118. return drm_gem_cma_dumb_create(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_pdata(struct rcar_du_device *rcdu)
  170. {
  171. unsigned int num_encoders = 0;
  172. unsigned int i;
  173. int ret;
  174. for (i = 0; i < rcdu->pdata->num_encoders; ++i) {
  175. const struct rcar_du_encoder_data *pdata =
  176. &rcdu->pdata->encoders[i];
  177. const struct rcar_du_output_routing *route =
  178. &rcdu->info->routes[pdata->output];
  179. if (pdata->type == RCAR_DU_ENCODER_UNUSED)
  180. continue;
  181. if (pdata->output >= RCAR_DU_OUTPUT_MAX ||
  182. route->possible_crtcs == 0) {
  183. dev_warn(rcdu->dev,
  184. "encoder %u references unexisting output %u, skipping\n",
  185. i, pdata->output);
  186. continue;
  187. }
  188. ret = rcar_du_encoder_init(rcdu, pdata->type, pdata->output,
  189. pdata, NULL);
  190. if (ret < 0)
  191. return ret;
  192. num_encoders++;
  193. }
  194. return num_encoders;
  195. }
  196. static int rcar_du_encoders_init_dt_one(struct rcar_du_device *rcdu,
  197. enum rcar_du_output output,
  198. struct of_endpoint *ep)
  199. {
  200. static const struct {
  201. const char *compatible;
  202. enum rcar_du_encoder_type type;
  203. } encoders[] = {
  204. { "adi,adv7123", RCAR_DU_ENCODER_VGA },
  205. { "thine,thc63lvdm83d", RCAR_DU_ENCODER_LVDS },
  206. };
  207. enum rcar_du_encoder_type enc_type = RCAR_DU_ENCODER_NONE;
  208. struct device_node *connector = NULL;
  209. struct device_node *encoder = NULL;
  210. struct device_node *prev = NULL;
  211. struct device_node *entity_ep_node;
  212. struct device_node *entity;
  213. int ret;
  214. /*
  215. * Locate the connected entity and infer its type from the number of
  216. * endpoints.
  217. */
  218. entity = of_graph_get_remote_port_parent(ep->local_node);
  219. if (!entity) {
  220. dev_dbg(rcdu->dev, "unconnected endpoint %s, skipping\n",
  221. ep->local_node->full_name);
  222. return 0;
  223. }
  224. entity_ep_node = of_parse_phandle(ep->local_node, "remote-endpoint", 0);
  225. while (1) {
  226. struct device_node *ep_node;
  227. ep_node = of_graph_get_next_endpoint(entity, prev);
  228. of_node_put(prev);
  229. prev = ep_node;
  230. if (!ep_node)
  231. break;
  232. if (ep_node == entity_ep_node)
  233. continue;
  234. /*
  235. * We've found one endpoint other than the input, this must
  236. * be an encoder. Locate the connector.
  237. */
  238. encoder = entity;
  239. connector = of_graph_get_remote_port_parent(ep_node);
  240. of_node_put(ep_node);
  241. if (!connector) {
  242. dev_warn(rcdu->dev,
  243. "no connector for encoder %s, skipping\n",
  244. encoder->full_name);
  245. of_node_put(entity_ep_node);
  246. of_node_put(encoder);
  247. return 0;
  248. }
  249. break;
  250. }
  251. of_node_put(entity_ep_node);
  252. if (encoder) {
  253. /*
  254. * If an encoder has been found, get its type based on its
  255. * compatible string.
  256. */
  257. unsigned int i;
  258. for (i = 0; i < ARRAY_SIZE(encoders); ++i) {
  259. if (of_device_is_compatible(encoder,
  260. encoders[i].compatible)) {
  261. enc_type = encoders[i].type;
  262. break;
  263. }
  264. }
  265. if (i == ARRAY_SIZE(encoders)) {
  266. dev_warn(rcdu->dev,
  267. "unknown encoder type for %s, skipping\n",
  268. encoder->full_name);
  269. of_node_put(encoder);
  270. of_node_put(connector);
  271. return 0;
  272. }
  273. } else {
  274. /*
  275. * If no encoder has been found the entity must be the
  276. * connector.
  277. */
  278. connector = entity;
  279. }
  280. ret = rcar_du_encoder_init(rcdu, enc_type, output, NULL, connector);
  281. of_node_put(encoder);
  282. of_node_put(connector);
  283. return ret < 0 ? ret : 1;
  284. }
  285. static int rcar_du_encoders_init_dt(struct rcar_du_device *rcdu)
  286. {
  287. struct device_node *np = rcdu->dev->of_node;
  288. struct device_node *prev = NULL;
  289. unsigned int num_encoders = 0;
  290. /*
  291. * Iterate over the endpoints and create one encoder for each output
  292. * pipeline.
  293. */
  294. while (1) {
  295. struct device_node *ep_node;
  296. enum rcar_du_output output;
  297. struct of_endpoint ep;
  298. unsigned int i;
  299. int ret;
  300. ep_node = of_graph_get_next_endpoint(np, prev);
  301. of_node_put(prev);
  302. prev = ep_node;
  303. if (ep_node == NULL)
  304. break;
  305. ret = of_graph_parse_endpoint(ep_node, &ep);
  306. if (ret < 0) {
  307. of_node_put(ep_node);
  308. return ret;
  309. }
  310. /* Find the output route corresponding to the port number. */
  311. for (i = 0; i < RCAR_DU_OUTPUT_MAX; ++i) {
  312. if (rcdu->info->routes[i].possible_crtcs &&
  313. rcdu->info->routes[i].port == ep.port) {
  314. output = i;
  315. break;
  316. }
  317. }
  318. if (i == RCAR_DU_OUTPUT_MAX) {
  319. dev_warn(rcdu->dev,
  320. "port %u references unexisting output, skipping\n",
  321. ep.port);
  322. continue;
  323. }
  324. /* Process the output pipeline. */
  325. ret = rcar_du_encoders_init_dt_one(rcdu, output, &ep);
  326. if (ret < 0) {
  327. of_node_put(ep_node);
  328. return ret;
  329. }
  330. num_encoders += ret;
  331. }
  332. return num_encoders;
  333. }
  334. int rcar_du_modeset_init(struct rcar_du_device *rcdu)
  335. {
  336. static const unsigned int mmio_offsets[] = {
  337. DU0_REG_OFFSET, DU2_REG_OFFSET
  338. };
  339. struct drm_device *dev = rcdu->ddev;
  340. struct drm_encoder *encoder;
  341. struct drm_fbdev_cma *fbdev;
  342. unsigned int num_encoders;
  343. unsigned int num_groups;
  344. unsigned int i;
  345. int ret;
  346. drm_mode_config_init(dev);
  347. dev->mode_config.min_width = 0;
  348. dev->mode_config.min_height = 0;
  349. dev->mode_config.max_width = 4095;
  350. dev->mode_config.max_height = 2047;
  351. dev->mode_config.funcs = &rcar_du_mode_config_funcs;
  352. rcdu->num_crtcs = rcdu->info->num_crtcs;
  353. /* Initialize the groups. */
  354. num_groups = DIV_ROUND_UP(rcdu->num_crtcs, 2);
  355. for (i = 0; i < num_groups; ++i) {
  356. struct rcar_du_group *rgrp = &rcdu->groups[i];
  357. rgrp->dev = rcdu;
  358. rgrp->mmio_offset = mmio_offsets[i];
  359. rgrp->index = i;
  360. ret = rcar_du_planes_init(rgrp);
  361. if (ret < 0)
  362. return ret;
  363. }
  364. /* Create the CRTCs. */
  365. for (i = 0; i < rcdu->num_crtcs; ++i) {
  366. struct rcar_du_group *rgrp = &rcdu->groups[i / 2];
  367. ret = rcar_du_crtc_create(rgrp, i);
  368. if (ret < 0)
  369. return ret;
  370. }
  371. /* Initialize the encoders. */
  372. ret = rcar_du_lvdsenc_init(rcdu);
  373. if (ret < 0)
  374. return ret;
  375. if (rcdu->pdata)
  376. ret = rcar_du_encoders_init_pdata(rcdu);
  377. else
  378. ret = rcar_du_encoders_init_dt(rcdu);
  379. if (ret < 0)
  380. return ret;
  381. num_encoders = ret;
  382. /* Set the possible CRTCs and possible clones. There's always at least
  383. * one way for all encoders to clone each other, set all bits in the
  384. * possible clones field.
  385. */
  386. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  387. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  388. const struct rcar_du_output_routing *route =
  389. &rcdu->info->routes[renc->output];
  390. encoder->possible_crtcs = route->possible_crtcs;
  391. encoder->possible_clones = (1 << num_encoders) - 1;
  392. }
  393. /* Now that the CRTCs have been initialized register the planes. */
  394. for (i = 0; i < num_groups; ++i) {
  395. ret = rcar_du_planes_register(&rcdu->groups[i]);
  396. if (ret < 0)
  397. return ret;
  398. }
  399. drm_kms_helper_poll_init(dev);
  400. drm_helper_disable_unused_functions(dev);
  401. fbdev = drm_fbdev_cma_init(dev, 32, dev->mode_config.num_crtc,
  402. dev->mode_config.num_connector);
  403. if (IS_ERR(fbdev))
  404. return PTR_ERR(fbdev);
  405. #ifndef CONFIG_FRAMEBUFFER_CONSOLE
  406. drm_fbdev_cma_restore_mode(fbdev);
  407. #endif
  408. rcdu->fbdev = fbdev;
  409. return 0;
  410. }