vsp1_lif.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * vsp1_lif.c -- R-Car VSP1 LCD Controller Interface
  3. *
  4. * Copyright (C) 2013-2014 Renesas Electronics Corporation
  5. *
  6. * Contact: Laurent Pinchart ([email protected])
  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 <linux/device.h>
  14. #include <linux/gfp.h>
  15. #include <media/v4l2-subdev.h>
  16. #include "vsp1.h"
  17. #include "vsp1_dl.h"
  18. #include "vsp1_lif.h"
  19. #define LIF_MIN_SIZE 2U
  20. #define LIF_MAX_SIZE 8190U
  21. /* -----------------------------------------------------------------------------
  22. * Device Access
  23. */
  24. static inline void vsp1_lif_write(struct vsp1_lif *lif, struct vsp1_dl_list *dl,
  25. u32 reg, u32 data)
  26. {
  27. vsp1_dl_list_write(dl, reg, data);
  28. }
  29. /* -----------------------------------------------------------------------------
  30. * V4L2 Subdevice Operations
  31. */
  32. static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
  33. struct v4l2_subdev_pad_config *cfg,
  34. struct v4l2_subdev_mbus_code_enum *code)
  35. {
  36. static const unsigned int codes[] = {
  37. MEDIA_BUS_FMT_ARGB8888_1X32,
  38. MEDIA_BUS_FMT_AYUV8_1X32,
  39. };
  40. return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
  41. ARRAY_SIZE(codes));
  42. }
  43. static int lif_enum_frame_size(struct v4l2_subdev *subdev,
  44. struct v4l2_subdev_pad_config *cfg,
  45. struct v4l2_subdev_frame_size_enum *fse)
  46. {
  47. return vsp1_subdev_enum_frame_size(subdev, cfg, fse, LIF_MIN_SIZE,
  48. LIF_MIN_SIZE, LIF_MAX_SIZE,
  49. LIF_MAX_SIZE);
  50. }
  51. static int lif_set_format(struct v4l2_subdev *subdev,
  52. struct v4l2_subdev_pad_config *cfg,
  53. struct v4l2_subdev_format *fmt)
  54. {
  55. struct vsp1_lif *lif = to_lif(subdev);
  56. struct v4l2_subdev_pad_config *config;
  57. struct v4l2_mbus_framefmt *format;
  58. int ret = 0;
  59. mutex_lock(&lif->entity.lock);
  60. config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which);
  61. if (!config) {
  62. ret = -EINVAL;
  63. goto done;
  64. }
  65. /* Default to YUV if the requested format is not supported. */
  66. if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  67. fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
  68. fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
  69. format = vsp1_entity_get_pad_format(&lif->entity, config, fmt->pad);
  70. if (fmt->pad == LIF_PAD_SOURCE) {
  71. /* The LIF source format is always identical to its sink
  72. * format.
  73. */
  74. fmt->format = *format;
  75. goto done;
  76. }
  77. format->code = fmt->format.code;
  78. format->width = clamp_t(unsigned int, fmt->format.width,
  79. LIF_MIN_SIZE, LIF_MAX_SIZE);
  80. format->height = clamp_t(unsigned int, fmt->format.height,
  81. LIF_MIN_SIZE, LIF_MAX_SIZE);
  82. format->field = V4L2_FIELD_NONE;
  83. format->colorspace = V4L2_COLORSPACE_SRGB;
  84. fmt->format = *format;
  85. /* Propagate the format to the source pad. */
  86. format = vsp1_entity_get_pad_format(&lif->entity, config,
  87. LIF_PAD_SOURCE);
  88. *format = fmt->format;
  89. done:
  90. mutex_unlock(&lif->entity.lock);
  91. return ret;
  92. }
  93. static const struct v4l2_subdev_pad_ops lif_pad_ops = {
  94. .init_cfg = vsp1_entity_init_cfg,
  95. .enum_mbus_code = lif_enum_mbus_code,
  96. .enum_frame_size = lif_enum_frame_size,
  97. .get_fmt = vsp1_subdev_get_pad_format,
  98. .set_fmt = lif_set_format,
  99. };
  100. static const struct v4l2_subdev_ops lif_ops = {
  101. .pad = &lif_pad_ops,
  102. };
  103. /* -----------------------------------------------------------------------------
  104. * VSP1 Entity Operations
  105. */
  106. static void lif_configure(struct vsp1_entity *entity,
  107. struct vsp1_pipeline *pipe,
  108. struct vsp1_dl_list *dl,
  109. enum vsp1_entity_params params)
  110. {
  111. const struct v4l2_mbus_framefmt *format;
  112. struct vsp1_lif *lif = to_lif(&entity->subdev);
  113. unsigned int hbth = 1300;
  114. unsigned int obth = 400;
  115. unsigned int lbth = 200;
  116. if (params != VSP1_ENTITY_PARAMS_INIT)
  117. return;
  118. format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
  119. LIF_PAD_SOURCE);
  120. obth = min(obth, (format->width + 1) / 2 * format->height - 4);
  121. vsp1_lif_write(lif, dl, VI6_LIF_CSBTH,
  122. (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
  123. (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
  124. vsp1_lif_write(lif, dl, VI6_LIF_CTRL,
  125. (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
  126. (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
  127. VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
  128. }
  129. static const struct vsp1_entity_operations lif_entity_ops = {
  130. .configure = lif_configure,
  131. };
  132. /* -----------------------------------------------------------------------------
  133. * Initialization and Cleanup
  134. */
  135. struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
  136. {
  137. struct vsp1_lif *lif;
  138. int ret;
  139. lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
  140. if (lif == NULL)
  141. return ERR_PTR(-ENOMEM);
  142. lif->entity.ops = &lif_entity_ops;
  143. lif->entity.type = VSP1_ENTITY_LIF;
  144. /* The LIF is never exposed to userspace, but media entity registration
  145. * requires a function to be set. Use PROC_VIDEO_PIXEL_FORMATTER just to
  146. * avoid triggering a WARN_ON(), the value won't be seen anywhere.
  147. */
  148. ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops,
  149. MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
  150. if (ret < 0)
  151. return ERR_PTR(ret);
  152. return lif;
  153. }