blob: be0ea166016c40fc3d83c47e88eb76d0c5ca3f7a [file] [log] [blame]
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03001/*
2 * vsp1_lif.c -- R-Car VSP1 LCD Controller Interface
3 *
Laurent Pinchart8a1edc52014-02-06 14:42:31 -03004 * Copyright (C) 2013-2014 Renesas Electronics Corporation
Laurent Pinchart26e0ca22013-06-04 11:22:30 -03005 *
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
14#include <linux/device.h>
15#include <linux/gfp.h>
16
17#include <media/v4l2-subdev.h>
18
19#include "vsp1.h"
20#include "vsp1_lif.h"
21
22#define LIF_MIN_SIZE 2U
23#define LIF_MAX_SIZE 2048U
24
25/* -----------------------------------------------------------------------------
26 * Device Access
27 */
28
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030029static inline void vsp1_lif_write(struct vsp1_lif *lif, u32 reg, u32 data)
30{
Takashi Saito1517b032015-09-07 01:40:25 -030031 vsp1_mod_write(&lif->entity, reg, data);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030032}
33
34/* -----------------------------------------------------------------------------
35 * V4L2 Subdevice Core Operations
36 */
37
38static int lif_s_stream(struct v4l2_subdev *subdev, int enable)
39{
40 const struct v4l2_mbus_framefmt *format;
41 struct vsp1_lif *lif = to_lif(subdev);
42 unsigned int hbth = 1300;
43 unsigned int obth = 400;
44 unsigned int lbth = 200;
45
46 if (!enable) {
Takashi Saito1517b032015-09-07 01:40:25 -030047 vsp1_write(lif->entity.vsp1, VI6_LIF_CTRL, 0);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030048 return 0;
49 }
50
51 format = &lif->entity.formats[LIF_PAD_SOURCE];
52
53 obth = min(obth, (format->width + 1) / 2 * format->height - 4);
54
55 vsp1_lif_write(lif, VI6_LIF_CSBTH,
56 (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
57 (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
58
59 vsp1_lif_write(lif, VI6_LIF_CTRL,
60 (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
61 (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
62 VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
63
64 return 0;
65}
66
67/* -----------------------------------------------------------------------------
68 * V4L2 Subdevice Pad Operations
69 */
70
71static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080072 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030073 struct v4l2_subdev_mbus_code_enum *code)
74{
75 static const unsigned int codes[] = {
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030076 MEDIA_BUS_FMT_ARGB8888_1X32,
77 MEDIA_BUS_FMT_AYUV8_1X32,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030078 };
Hans Verkuil3f1ccf12015-03-04 01:47:57 -080079 struct vsp1_lif *lif = to_lif(subdev);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030080
81 if (code->pad == LIF_PAD_SINK) {
82 if (code->index >= ARRAY_SIZE(codes))
83 return -EINVAL;
84
85 code->code = codes[code->index];
86 } else {
87 struct v4l2_mbus_framefmt *format;
88
89 /* The LIF can't perform format conversion, the sink format is
90 * always identical to the source format.
91 */
92 if (code->index)
93 return -EINVAL;
94
Hans Verkuil3f1ccf12015-03-04 01:47:57 -080095 format = vsp1_entity_get_pad_format(&lif->entity, cfg,
96 LIF_PAD_SINK, code->which);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -030097 code->code = format->code;
98 }
99
100 return 0;
101}
102
103static int lif_enum_frame_size(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800104 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300105 struct v4l2_subdev_frame_size_enum *fse)
106{
Hans Verkuil5778e742015-03-04 01:47:58 -0800107 struct vsp1_lif *lif = to_lif(subdev);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300108 struct v4l2_mbus_framefmt *format;
109
Hans Verkuil5778e742015-03-04 01:47:58 -0800110 format = vsp1_entity_get_pad_format(&lif->entity, cfg, LIF_PAD_SINK,
111 fse->which);
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300112
113 if (fse->index || fse->code != format->code)
114 return -EINVAL;
115
116 if (fse->pad == LIF_PAD_SINK) {
117 fse->min_width = LIF_MIN_SIZE;
118 fse->max_width = LIF_MAX_SIZE;
119 fse->min_height = LIF_MIN_SIZE;
120 fse->max_height = LIF_MAX_SIZE;
121 } else {
122 fse->min_width = format->width;
123 fse->max_width = format->width;
124 fse->min_height = format->height;
125 fse->max_height = format->height;
126 }
127
128 return 0;
129}
130
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200131static int lif_get_format(struct v4l2_subdev *subdev,
132 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300133 struct v4l2_subdev_format *fmt)
134{
135 struct vsp1_lif *lif = to_lif(subdev);
136
Hans Verkuilf7234132015-03-04 01:47:54 -0800137 fmt->format = *vsp1_entity_get_pad_format(&lif->entity, cfg, fmt->pad,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300138 fmt->which);
139
140 return 0;
141}
142
Laurent Pinchart1bd0a1b2015-11-01 15:18:32 -0200143static int lif_set_format(struct v4l2_subdev *subdev,
144 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300145 struct v4l2_subdev_format *fmt)
146{
147 struct vsp1_lif *lif = to_lif(subdev);
148 struct v4l2_mbus_framefmt *format;
149
150 /* Default to YUV if the requested format is not supported. */
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300151 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
152 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
153 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300154
Hans Verkuilf7234132015-03-04 01:47:54 -0800155 format = vsp1_entity_get_pad_format(&lif->entity, cfg, fmt->pad,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300156 fmt->which);
157
158 if (fmt->pad == LIF_PAD_SOURCE) {
159 /* The LIF source format is always identical to its sink
160 * format.
161 */
162 fmt->format = *format;
163 return 0;
164 }
165
166 format->code = fmt->format.code;
167 format->width = clamp_t(unsigned int, fmt->format.width,
168 LIF_MIN_SIZE, LIF_MAX_SIZE);
169 format->height = clamp_t(unsigned int, fmt->format.height,
170 LIF_MIN_SIZE, LIF_MAX_SIZE);
171 format->field = V4L2_FIELD_NONE;
172 format->colorspace = V4L2_COLORSPACE_SRGB;
173
174 fmt->format = *format;
175
176 /* Propagate the format to the source pad. */
Hans Verkuilf7234132015-03-04 01:47:54 -0800177 format = vsp1_entity_get_pad_format(&lif->entity, cfg, LIF_PAD_SOURCE,
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300178 fmt->which);
179 *format = fmt->format;
180
181 return 0;
182}
183
184/* -----------------------------------------------------------------------------
185 * V4L2 Subdevice Operations
186 */
187
188static struct v4l2_subdev_video_ops lif_video_ops = {
189 .s_stream = lif_s_stream,
190};
191
192static struct v4l2_subdev_pad_ops lif_pad_ops = {
193 .enum_mbus_code = lif_enum_mbus_code,
194 .enum_frame_size = lif_enum_frame_size,
195 .get_fmt = lif_get_format,
196 .set_fmt = lif_set_format,
197};
198
199static struct v4l2_subdev_ops lif_ops = {
200 .video = &lif_video_ops,
201 .pad = &lif_pad_ops,
202};
203
204/* -----------------------------------------------------------------------------
205 * Initialization and Cleanup
206 */
207
208struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
209{
210 struct v4l2_subdev *subdev;
211 struct vsp1_lif *lif;
212 int ret;
213
214 lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
215 if (lif == NULL)
216 return ERR_PTR(-ENOMEM);
217
218 lif->entity.type = VSP1_ENTITY_LIF;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300219
220 ret = vsp1_entity_init(vsp1, &lif->entity, 2);
221 if (ret < 0)
222 return ERR_PTR(ret);
223
224 /* Initialize the V4L2 subdev. */
225 subdev = &lif->entity.subdev;
226 v4l2_subdev_init(subdev, &lif_ops);
227
Laurent Pinchartbabca002015-08-05 17:14:41 -0300228 subdev->entity.ops = &vsp1->media_ops;
Laurent Pinchart26e0ca22013-06-04 11:22:30 -0300229 subdev->internal_ops = &vsp1_subdev_internal_ops;
230 snprintf(subdev->name, sizeof(subdev->name), "%s lif",
231 dev_name(vsp1->dev));
232 v4l2_set_subdevdata(subdev, lif);
233 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
234
235 vsp1_entity_init_formats(subdev, NULL);
236
237 return lif;
238}