vsp1_lif.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * vsp1_lif.c -- R-Car VSP1 LCD Controller Interface
  3. *
  4. * Copyright (C) 2013 Renesas 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 <linux/device.h>
  14. #include <linux/gfp.h>
  15. #include <media/v4l2-subdev.h>
  16. #include "vsp1.h"
  17. #include "vsp1_lif.h"
  18. #define LIF_MIN_SIZE 2U
  19. #define LIF_MAX_SIZE 2048U
  20. /* -----------------------------------------------------------------------------
  21. * Device Access
  22. */
  23. static inline u32 vsp1_lif_read(struct vsp1_lif *lif, u32 reg)
  24. {
  25. return vsp1_read(lif->entity.vsp1, reg);
  26. }
  27. static inline void vsp1_lif_write(struct vsp1_lif *lif, u32 reg, u32 data)
  28. {
  29. vsp1_write(lif->entity.vsp1, reg, data);
  30. }
  31. /* -----------------------------------------------------------------------------
  32. * V4L2 Subdevice Core Operations
  33. */
  34. static int lif_s_stream(struct v4l2_subdev *subdev, int enable)
  35. {
  36. const struct v4l2_mbus_framefmt *format;
  37. struct vsp1_lif *lif = to_lif(subdev);
  38. unsigned int hbth = 1300;
  39. unsigned int obth = 400;
  40. unsigned int lbth = 200;
  41. if (!enable) {
  42. vsp1_lif_write(lif, VI6_LIF_CTRL, 0);
  43. return 0;
  44. }
  45. format = &lif->entity.formats[LIF_PAD_SOURCE];
  46. obth = min(obth, (format->width + 1) / 2 * format->height - 4);
  47. vsp1_lif_write(lif, VI6_LIF_CSBTH,
  48. (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
  49. (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
  50. vsp1_lif_write(lif, VI6_LIF_CTRL,
  51. (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
  52. (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
  53. VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
  54. return 0;
  55. }
  56. /* -----------------------------------------------------------------------------
  57. * V4L2 Subdevice Pad Operations
  58. */
  59. static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
  60. struct v4l2_subdev_fh *fh,
  61. struct v4l2_subdev_mbus_code_enum *code)
  62. {
  63. static const unsigned int codes[] = {
  64. V4L2_MBUS_FMT_ARGB8888_1X32,
  65. V4L2_MBUS_FMT_AYUV8_1X32,
  66. };
  67. if (code->pad == LIF_PAD_SINK) {
  68. if (code->index >= ARRAY_SIZE(codes))
  69. return -EINVAL;
  70. code->code = codes[code->index];
  71. } else {
  72. struct v4l2_mbus_framefmt *format;
  73. /* The LIF can't perform format conversion, the sink format is
  74. * always identical to the source format.
  75. */
  76. if (code->index)
  77. return -EINVAL;
  78. format = v4l2_subdev_get_try_format(fh, LIF_PAD_SINK);
  79. code->code = format->code;
  80. }
  81. return 0;
  82. }
  83. static int lif_enum_frame_size(struct v4l2_subdev *subdev,
  84. struct v4l2_subdev_fh *fh,
  85. struct v4l2_subdev_frame_size_enum *fse)
  86. {
  87. struct v4l2_mbus_framefmt *format;
  88. format = v4l2_subdev_get_try_format(fh, LIF_PAD_SINK);
  89. if (fse->index || fse->code != format->code)
  90. return -EINVAL;
  91. if (fse->pad == LIF_PAD_SINK) {
  92. fse->min_width = LIF_MIN_SIZE;
  93. fse->max_width = LIF_MAX_SIZE;
  94. fse->min_height = LIF_MIN_SIZE;
  95. fse->max_height = LIF_MAX_SIZE;
  96. } else {
  97. fse->min_width = format->width;
  98. fse->max_width = format->width;
  99. fse->min_height = format->height;
  100. fse->max_height = format->height;
  101. }
  102. return 0;
  103. }
  104. static int lif_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
  105. struct v4l2_subdev_format *fmt)
  106. {
  107. struct vsp1_lif *lif = to_lif(subdev);
  108. fmt->format = *vsp1_entity_get_pad_format(&lif->entity, fh, fmt->pad,
  109. fmt->which);
  110. return 0;
  111. }
  112. static int lif_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh,
  113. struct v4l2_subdev_format *fmt)
  114. {
  115. struct vsp1_lif *lif = to_lif(subdev);
  116. struct v4l2_mbus_framefmt *format;
  117. /* Default to YUV if the requested format is not supported. */
  118. if (fmt->format.code != V4L2_MBUS_FMT_ARGB8888_1X32 &&
  119. fmt->format.code != V4L2_MBUS_FMT_AYUV8_1X32)
  120. fmt->format.code = V4L2_MBUS_FMT_AYUV8_1X32;
  121. format = vsp1_entity_get_pad_format(&lif->entity, fh, fmt->pad,
  122. fmt->which);
  123. if (fmt->pad == LIF_PAD_SOURCE) {
  124. /* The LIF source format is always identical to its sink
  125. * format.
  126. */
  127. fmt->format = *format;
  128. return 0;
  129. }
  130. format->code = fmt->format.code;
  131. format->width = clamp_t(unsigned int, fmt->format.width,
  132. LIF_MIN_SIZE, LIF_MAX_SIZE);
  133. format->height = clamp_t(unsigned int, fmt->format.height,
  134. LIF_MIN_SIZE, LIF_MAX_SIZE);
  135. format->field = V4L2_FIELD_NONE;
  136. format->colorspace = V4L2_COLORSPACE_SRGB;
  137. fmt->format = *format;
  138. /* Propagate the format to the source pad. */
  139. format = vsp1_entity_get_pad_format(&lif->entity, fh, LIF_PAD_SOURCE,
  140. fmt->which);
  141. *format = fmt->format;
  142. return 0;
  143. }
  144. /* -----------------------------------------------------------------------------
  145. * V4L2 Subdevice Operations
  146. */
  147. static struct v4l2_subdev_video_ops lif_video_ops = {
  148. .s_stream = lif_s_stream,
  149. };
  150. static struct v4l2_subdev_pad_ops lif_pad_ops = {
  151. .enum_mbus_code = lif_enum_mbus_code,
  152. .enum_frame_size = lif_enum_frame_size,
  153. .get_fmt = lif_get_format,
  154. .set_fmt = lif_set_format,
  155. };
  156. static struct v4l2_subdev_ops lif_ops = {
  157. .video = &lif_video_ops,
  158. .pad = &lif_pad_ops,
  159. };
  160. /* -----------------------------------------------------------------------------
  161. * Initialization and Cleanup
  162. */
  163. struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
  164. {
  165. struct v4l2_subdev *subdev;
  166. struct vsp1_lif *lif;
  167. int ret;
  168. lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
  169. if (lif == NULL)
  170. return ERR_PTR(-ENOMEM);
  171. lif->entity.type = VSP1_ENTITY_LIF;
  172. lif->entity.id = VI6_DPR_NODE_LIF;
  173. ret = vsp1_entity_init(vsp1, &lif->entity, 2);
  174. if (ret < 0)
  175. return ERR_PTR(ret);
  176. /* Initialize the V4L2 subdev. */
  177. subdev = &lif->entity.subdev;
  178. v4l2_subdev_init(subdev, &lif_ops);
  179. subdev->entity.ops = &vsp1_media_ops;
  180. subdev->internal_ops = &vsp1_subdev_internal_ops;
  181. snprintf(subdev->name, sizeof(subdev->name), "%s lif",
  182. dev_name(vsp1->dev));
  183. v4l2_set_subdevdata(subdev, lif);
  184. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  185. vsp1_entity_init_formats(subdev, NULL);
  186. return lif;
  187. }