vsp1_lif.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 (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_pad_config *cfg,
  61. struct v4l2_subdev_mbus_code_enum *code)
  62. {
  63. static const unsigned int codes[] = {
  64. MEDIA_BUS_FMT_ARGB8888_1X32,
  65. MEDIA_BUS_FMT_AYUV8_1X32,
  66. };
  67. struct vsp1_lif *lif = to_lif(subdev);
  68. if (code->pad == LIF_PAD_SINK) {
  69. if (code->index >= ARRAY_SIZE(codes))
  70. return -EINVAL;
  71. code->code = codes[code->index];
  72. } else {
  73. struct v4l2_mbus_framefmt *format;
  74. /* The LIF can't perform format conversion, the sink format is
  75. * always identical to the source format.
  76. */
  77. if (code->index)
  78. return -EINVAL;
  79. format = vsp1_entity_get_pad_format(&lif->entity, cfg,
  80. LIF_PAD_SINK, code->which);
  81. code->code = format->code;
  82. }
  83. return 0;
  84. }
  85. static int lif_enum_frame_size(struct v4l2_subdev *subdev,
  86. struct v4l2_subdev_pad_config *cfg,
  87. struct v4l2_subdev_frame_size_enum *fse)
  88. {
  89. struct vsp1_lif *lif = to_lif(subdev);
  90. struct v4l2_mbus_framefmt *format;
  91. format = vsp1_entity_get_pad_format(&lif->entity, cfg, LIF_PAD_SINK,
  92. fse->which);
  93. if (fse->index || fse->code != format->code)
  94. return -EINVAL;
  95. if (fse->pad == LIF_PAD_SINK) {
  96. fse->min_width = LIF_MIN_SIZE;
  97. fse->max_width = LIF_MAX_SIZE;
  98. fse->min_height = LIF_MIN_SIZE;
  99. fse->max_height = LIF_MAX_SIZE;
  100. } else {
  101. fse->min_width = format->width;
  102. fse->max_width = format->width;
  103. fse->min_height = format->height;
  104. fse->max_height = format->height;
  105. }
  106. return 0;
  107. }
  108. static int lif_get_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
  109. struct v4l2_subdev_format *fmt)
  110. {
  111. struct vsp1_lif *lif = to_lif(subdev);
  112. fmt->format = *vsp1_entity_get_pad_format(&lif->entity, cfg, fmt->pad,
  113. fmt->which);
  114. return 0;
  115. }
  116. static int lif_set_format(struct v4l2_subdev *subdev, struct v4l2_subdev_pad_config *cfg,
  117. struct v4l2_subdev_format *fmt)
  118. {
  119. struct vsp1_lif *lif = to_lif(subdev);
  120. struct v4l2_mbus_framefmt *format;
  121. /* Default to YUV if the requested format is not supported. */
  122. if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
  123. fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
  124. fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
  125. format = vsp1_entity_get_pad_format(&lif->entity, cfg, fmt->pad,
  126. fmt->which);
  127. if (fmt->pad == LIF_PAD_SOURCE) {
  128. /* The LIF source format is always identical to its sink
  129. * format.
  130. */
  131. fmt->format = *format;
  132. return 0;
  133. }
  134. format->code = fmt->format.code;
  135. format->width = clamp_t(unsigned int, fmt->format.width,
  136. LIF_MIN_SIZE, LIF_MAX_SIZE);
  137. format->height = clamp_t(unsigned int, fmt->format.height,
  138. LIF_MIN_SIZE, LIF_MAX_SIZE);
  139. format->field = V4L2_FIELD_NONE;
  140. format->colorspace = V4L2_COLORSPACE_SRGB;
  141. fmt->format = *format;
  142. /* Propagate the format to the source pad. */
  143. format = vsp1_entity_get_pad_format(&lif->entity, cfg, LIF_PAD_SOURCE,
  144. fmt->which);
  145. *format = fmt->format;
  146. return 0;
  147. }
  148. /* -----------------------------------------------------------------------------
  149. * V4L2 Subdevice Operations
  150. */
  151. static struct v4l2_subdev_video_ops lif_video_ops = {
  152. .s_stream = lif_s_stream,
  153. };
  154. static struct v4l2_subdev_pad_ops lif_pad_ops = {
  155. .enum_mbus_code = lif_enum_mbus_code,
  156. .enum_frame_size = lif_enum_frame_size,
  157. .get_fmt = lif_get_format,
  158. .set_fmt = lif_set_format,
  159. };
  160. static struct v4l2_subdev_ops lif_ops = {
  161. .video = &lif_video_ops,
  162. .pad = &lif_pad_ops,
  163. };
  164. /* -----------------------------------------------------------------------------
  165. * Initialization and Cleanup
  166. */
  167. struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
  168. {
  169. struct v4l2_subdev *subdev;
  170. struct vsp1_lif *lif;
  171. int ret;
  172. lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
  173. if (lif == NULL)
  174. return ERR_PTR(-ENOMEM);
  175. lif->entity.type = VSP1_ENTITY_LIF;
  176. ret = vsp1_entity_init(vsp1, &lif->entity, 2);
  177. if (ret < 0)
  178. return ERR_PTR(ret);
  179. /* Initialize the V4L2 subdev. */
  180. subdev = &lif->entity.subdev;
  181. v4l2_subdev_init(subdev, &lif_ops);
  182. subdev->entity.ops = &vsp1_media_ops;
  183. subdev->internal_ops = &vsp1_subdev_internal_ops;
  184. snprintf(subdev->name, sizeof(subdev->name), "%s lif",
  185. dev_name(vsp1->dev));
  186. v4l2_set_subdevdata(subdev, lif);
  187. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  188. vsp1_entity_init_formats(subdev, NULL);
  189. return lif;
  190. }