[media] v4l: vsp1: Store active selection rectangles in a pad config structure
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_lif.c
CommitLineData
26e0ca22
LP
1/*
2 * vsp1_lif.c -- R-Car VSP1 LCD Controller Interface
3 *
8a1edc55 4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
26e0ca22
LP
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
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
26e0ca22
LP
29static inline void vsp1_lif_write(struct vsp1_lif *lif, u32 reg, u32 data)
30{
1517b039 31 vsp1_mod_write(&lif->entity, reg, data);
26e0ca22
LP
32}
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) {
1517b039 47 vsp1_write(lif->entity.vsp1, VI6_LIF_CTRL, 0);
26e0ca22
LP
48 return 0;
49 }
50
e790c3cb
LP
51 format = vsp1_entity_get_pad_format(&lif->entity, lif->entity.config,
52 LIF_PAD_SOURCE);
26e0ca22
LP
53
54 obth = min(obth, (format->width + 1) / 2 * format->height - 4);
55
56 vsp1_lif_write(lif, VI6_LIF_CSBTH,
57 (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
58 (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
59
60 vsp1_lif_write(lif, VI6_LIF_CTRL,
61 (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
62 (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
63 VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
64
65 return 0;
66}
67
68/* -----------------------------------------------------------------------------
69 * V4L2 Subdevice Pad Operations
70 */
71
72static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
f7234138 73 struct v4l2_subdev_pad_config *cfg,
26e0ca22
LP
74 struct v4l2_subdev_mbus_code_enum *code)
75{
76 static const unsigned int codes[] = {
27ffaeb0
BB
77 MEDIA_BUS_FMT_ARGB8888_1X32,
78 MEDIA_BUS_FMT_AYUV8_1X32,
26e0ca22 79 };
3f1ccf16 80 struct vsp1_lif *lif = to_lif(subdev);
26e0ca22
LP
81
82 if (code->pad == LIF_PAD_SINK) {
83 if (code->index >= ARRAY_SIZE(codes))
84 return -EINVAL;
85
86 code->code = codes[code->index];
87 } else {
e790c3cb 88 struct v4l2_subdev_pad_config *config;
26e0ca22
LP
89 struct v4l2_mbus_framefmt *format;
90
91 /* The LIF can't perform format conversion, the sink format is
92 * always identical to the source format.
93 */
94 if (code->index)
95 return -EINVAL;
96
e790c3cb
LP
97 config = vsp1_entity_get_pad_config(&lif->entity, cfg,
98 code->which);
99 if (!config)
100 return -EINVAL;
101
102 format = vsp1_entity_get_pad_format(&lif->entity, config,
103 LIF_PAD_SINK);
26e0ca22
LP
104 code->code = format->code;
105 }
106
107 return 0;
108}
109
110static int lif_enum_frame_size(struct v4l2_subdev *subdev,
f7234138 111 struct v4l2_subdev_pad_config *cfg,
26e0ca22
LP
112 struct v4l2_subdev_frame_size_enum *fse)
113{
5778e749 114 struct vsp1_lif *lif = to_lif(subdev);
e790c3cb 115 struct v4l2_subdev_pad_config *config;
26e0ca22
LP
116 struct v4l2_mbus_framefmt *format;
117
e790c3cb
LP
118 config = vsp1_entity_get_pad_config(&lif->entity, cfg, fse->which);
119 if (!config)
120 return -EINVAL;
121
122 format = vsp1_entity_get_pad_format(&lif->entity, config, LIF_PAD_SINK);
26e0ca22
LP
123
124 if (fse->index || fse->code != format->code)
125 return -EINVAL;
126
127 if (fse->pad == LIF_PAD_SINK) {
128 fse->min_width = LIF_MIN_SIZE;
129 fse->max_width = LIF_MAX_SIZE;
130 fse->min_height = LIF_MIN_SIZE;
131 fse->max_height = LIF_MAX_SIZE;
132 } else {
133 fse->min_width = format->width;
134 fse->max_width = format->width;
135 fse->min_height = format->height;
136 fse->max_height = format->height;
137 }
138
139 return 0;
140}
141
1bd0a1bd
LP
142static int lif_get_format(struct v4l2_subdev *subdev,
143 struct v4l2_subdev_pad_config *cfg,
26e0ca22
LP
144 struct v4l2_subdev_format *fmt)
145{
146 struct vsp1_lif *lif = to_lif(subdev);
e790c3cb 147 struct v4l2_subdev_pad_config *config;
26e0ca22 148
e790c3cb
LP
149 config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which);
150 if (!config)
151 return -EINVAL;
152
153 fmt->format = *vsp1_entity_get_pad_format(&lif->entity, config,
154 fmt->pad);
26e0ca22
LP
155
156 return 0;
157}
158
1bd0a1bd
LP
159static int lif_set_format(struct v4l2_subdev *subdev,
160 struct v4l2_subdev_pad_config *cfg,
26e0ca22
LP
161 struct v4l2_subdev_format *fmt)
162{
163 struct vsp1_lif *lif = to_lif(subdev);
e790c3cb 164 struct v4l2_subdev_pad_config *config;
26e0ca22
LP
165 struct v4l2_mbus_framefmt *format;
166
e790c3cb
LP
167 config = vsp1_entity_get_pad_config(&lif->entity, cfg, fmt->which);
168 if (!config)
169 return -EINVAL;
170
26e0ca22 171 /* Default to YUV if the requested format is not supported. */
27ffaeb0
BB
172 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
173 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
174 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
26e0ca22 175
e790c3cb 176 format = vsp1_entity_get_pad_format(&lif->entity, config, fmt->pad);
26e0ca22
LP
177
178 if (fmt->pad == LIF_PAD_SOURCE) {
179 /* The LIF source format is always identical to its sink
180 * format.
181 */
182 fmt->format = *format;
183 return 0;
184 }
185
186 format->code = fmt->format.code;
187 format->width = clamp_t(unsigned int, fmt->format.width,
188 LIF_MIN_SIZE, LIF_MAX_SIZE);
189 format->height = clamp_t(unsigned int, fmt->format.height,
190 LIF_MIN_SIZE, LIF_MAX_SIZE);
191 format->field = V4L2_FIELD_NONE;
192 format->colorspace = V4L2_COLORSPACE_SRGB;
193
194 fmt->format = *format;
195
196 /* Propagate the format to the source pad. */
e790c3cb
LP
197 format = vsp1_entity_get_pad_format(&lif->entity, config,
198 LIF_PAD_SOURCE);
26e0ca22
LP
199 *format = fmt->format;
200
201 return 0;
202}
203
204/* -----------------------------------------------------------------------------
205 * V4L2 Subdevice Operations
206 */
207
208static struct v4l2_subdev_video_ops lif_video_ops = {
209 .s_stream = lif_s_stream,
210};
211
212static struct v4l2_subdev_pad_ops lif_pad_ops = {
0efdf0f5 213 .init_cfg = vsp1_entity_init_cfg,
26e0ca22
LP
214 .enum_mbus_code = lif_enum_mbus_code,
215 .enum_frame_size = lif_enum_frame_size,
216 .get_fmt = lif_get_format,
217 .set_fmt = lif_set_format,
218};
219
220static struct v4l2_subdev_ops lif_ops = {
221 .video = &lif_video_ops,
222 .pad = &lif_pad_ops,
223};
224
225/* -----------------------------------------------------------------------------
226 * Initialization and Cleanup
227 */
228
229struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
230{
26e0ca22
LP
231 struct vsp1_lif *lif;
232 int ret;
233
234 lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
235 if (lif == NULL)
236 return ERR_PTR(-ENOMEM);
237
238 lif->entity.type = VSP1_ENTITY_LIF;
26e0ca22 239
823329df 240 ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops);
26e0ca22
LP
241 if (ret < 0)
242 return ERR_PTR(ret);
243
26e0ca22
LP
244 return lif;
245}
This page took 0.146434 seconds and 5 git commands to generate.