[media] v4l: vsp1: Factorize get pad format code
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_lut.c
CommitLineData
989af883
LP
1/*
2 * vsp1_lut.c -- R-Car VSP1 Look-Up Table
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
14#include <linux/device.h>
15#include <linux/gfp.h>
16#include <linux/vsp1.h>
17
18#include <media/v4l2-subdev.h>
19
20#include "vsp1.h"
5e8dbbf3 21#include "vsp1_dl.h"
989af883
LP
22#include "vsp1_lut.h"
23
24#define LUT_MIN_SIZE 4U
25#define LUT_MAX_SIZE 8190U
26
27/* -----------------------------------------------------------------------------
28 * Device Access
29 */
30
5e8dbbf3
LP
31static inline void vsp1_lut_write(struct vsp1_lut *lut, struct vsp1_dl_list *dl,
32 u32 reg, u32 data)
989af883 33{
5e8dbbf3 34 vsp1_dl_list_write(dl, reg, data);
989af883
LP
35}
36
37/* -----------------------------------------------------------------------------
38 * V4L2 Subdevice Core Operations
39 */
40
7b905f05 41static void lut_set_table(struct vsp1_lut *lut, struct vsp1_lut_config *config)
989af883
LP
42{
43 memcpy_toio(lut->entity.vsp1->mmio + VI6_LUT_TABLE, config->lut,
44 sizeof(config->lut));
45}
46
47static long lut_ioctl(struct v4l2_subdev *subdev, unsigned int cmd, void *arg)
48{
49 struct vsp1_lut *lut = to_lut(subdev);
50
51 switch (cmd) {
52 case VIDIOC_VSP1_LUT_CONFIG:
7b905f05 53 lut_set_table(lut, arg);
989af883
LP
54 return 0;
55
56 default:
57 return -ENOIOCTLCMD;
58 }
59}
60
989af883
LP
61/* -----------------------------------------------------------------------------
62 * V4L2 Subdevice Pad Operations
63 */
64
65static int lut_enum_mbus_code(struct v4l2_subdev *subdev,
f7234138 66 struct v4l2_subdev_pad_config *cfg,
989af883
LP
67 struct v4l2_subdev_mbus_code_enum *code)
68{
69 static const unsigned int codes[] = {
27ffaeb0
BB
70 MEDIA_BUS_FMT_ARGB8888_1X32,
71 MEDIA_BUS_FMT_AHSV8888_1X32,
72 MEDIA_BUS_FMT_AYUV8_1X32,
989af883 73 };
3f1ccf16 74 struct vsp1_lut *lut = to_lut(subdev);
989af883
LP
75
76 if (code->pad == LUT_PAD_SINK) {
77 if (code->index >= ARRAY_SIZE(codes))
78 return -EINVAL;
79
80 code->code = codes[code->index];
81 } else {
e790c3cb
LP
82 struct v4l2_subdev_pad_config *config;
83 struct v4l2_mbus_framefmt *format;
84
989af883
LP
85 /* The LUT can't perform format conversion, the sink format is
86 * always identical to the source format.
87 */
88 if (code->index)
89 return -EINVAL;
90
e790c3cb
LP
91 config = vsp1_entity_get_pad_config(&lut->entity, cfg,
92 code->which);
93 if (!config)
94 return -EINVAL;
95
96 format = vsp1_entity_get_pad_format(&lut->entity, config,
97 LUT_PAD_SINK);
989af883
LP
98 code->code = format->code;
99 }
100
101 return 0;
102}
103
104static int lut_enum_frame_size(struct v4l2_subdev *subdev,
f7234138 105 struct v4l2_subdev_pad_config *cfg,
989af883
LP
106 struct v4l2_subdev_frame_size_enum *fse)
107{
5778e749 108 struct vsp1_lut *lut = to_lut(subdev);
e790c3cb 109 struct v4l2_subdev_pad_config *config;
989af883
LP
110 struct v4l2_mbus_framefmt *format;
111
e790c3cb
LP
112 config = vsp1_entity_get_pad_config(&lut->entity, cfg, fse->which);
113 if (!config)
114 return -EINVAL;
115
116 format = vsp1_entity_get_pad_format(&lut->entity, config, fse->pad);
989af883
LP
117
118 if (fse->index || fse->code != format->code)
119 return -EINVAL;
120
121 if (fse->pad == LUT_PAD_SINK) {
122 fse->min_width = LUT_MIN_SIZE;
123 fse->max_width = LUT_MAX_SIZE;
124 fse->min_height = LUT_MIN_SIZE;
125 fse->max_height = LUT_MAX_SIZE;
126 } else {
127 /* The size on the source pad are fixed and always identical to
128 * the size on the sink pad.
129 */
130 fse->min_width = format->width;
131 fse->max_width = format->width;
132 fse->min_height = format->height;
133 fse->max_height = format->height;
134 }
135
136 return 0;
137}
138
1bd0a1bd
LP
139static int lut_set_format(struct v4l2_subdev *subdev,
140 struct v4l2_subdev_pad_config *cfg,
989af883
LP
141 struct v4l2_subdev_format *fmt)
142{
143 struct vsp1_lut *lut = to_lut(subdev);
e790c3cb 144 struct v4l2_subdev_pad_config *config;
989af883
LP
145 struct v4l2_mbus_framefmt *format;
146
e790c3cb
LP
147 config = vsp1_entity_get_pad_config(&lut->entity, cfg, fmt->which);
148 if (!config)
149 return -EINVAL;
150
989af883 151 /* Default to YUV if the requested format is not supported. */
27ffaeb0
BB
152 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
153 fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
154 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
155 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
989af883 156
e790c3cb 157 format = vsp1_entity_get_pad_format(&lut->entity, config, fmt->pad);
989af883
LP
158
159 if (fmt->pad == LUT_PAD_SOURCE) {
160 /* The LUT output format can't be modified. */
161 fmt->format = *format;
162 return 0;
163 }
164
165 format->width = clamp_t(unsigned int, fmt->format.width,
166 LUT_MIN_SIZE, LUT_MAX_SIZE);
167 format->height = clamp_t(unsigned int, fmt->format.height,
168 LUT_MIN_SIZE, LUT_MAX_SIZE);
169 format->field = V4L2_FIELD_NONE;
170 format->colorspace = V4L2_COLORSPACE_SRGB;
171
172 fmt->format = *format;
173
174 /* Propagate the format to the source pad. */
e790c3cb
LP
175 format = vsp1_entity_get_pad_format(&lut->entity, config,
176 LUT_PAD_SOURCE);
989af883
LP
177 *format = fmt->format;
178
179 return 0;
180}
181
182/* -----------------------------------------------------------------------------
183 * V4L2 Subdevice Operations
184 */
185
186static struct v4l2_subdev_core_ops lut_core_ops = {
187 .ioctl = lut_ioctl,
188};
189
989af883 190static struct v4l2_subdev_pad_ops lut_pad_ops = {
0efdf0f5 191 .init_cfg = vsp1_entity_init_cfg,
989af883
LP
192 .enum_mbus_code = lut_enum_mbus_code,
193 .enum_frame_size = lut_enum_frame_size,
3f557220 194 .get_fmt = vsp1_subdev_get_pad_format,
989af883
LP
195 .set_fmt = lut_set_format,
196};
197
198static struct v4l2_subdev_ops lut_ops = {
199 .core = &lut_core_ops,
989af883
LP
200 .pad = &lut_pad_ops,
201};
202
7b905f05
LP
203/* -----------------------------------------------------------------------------
204 * VSP1 Entity Operations
205 */
206
83dd019d
LP
207static void lut_configure(struct vsp1_entity *entity,
208 struct vsp1_pipeline *pipe,
209 struct vsp1_dl_list *dl)
7b905f05
LP
210{
211 struct vsp1_lut *lut = to_lut(&entity->subdev);
212
5e8dbbf3 213 vsp1_lut_write(lut, dl, VI6_LUT_CTRL, VI6_LUT_CTRL_EN);
7b905f05
LP
214}
215
216static const struct vsp1_entity_operations lut_entity_ops = {
217 .configure = lut_configure,
218};
219
989af883
LP
220/* -----------------------------------------------------------------------------
221 * Initialization and Cleanup
222 */
223
224struct vsp1_lut *vsp1_lut_create(struct vsp1_device *vsp1)
225{
989af883
LP
226 struct vsp1_lut *lut;
227 int ret;
228
229 lut = devm_kzalloc(vsp1->dev, sizeof(*lut), GFP_KERNEL);
230 if (lut == NULL)
231 return ERR_PTR(-ENOMEM);
232
7b905f05 233 lut->entity.ops = &lut_entity_ops;
989af883 234 lut->entity.type = VSP1_ENTITY_LUT;
989af883 235
823329df 236 ret = vsp1_entity_init(vsp1, &lut->entity, "lut", 2, &lut_ops);
989af883
LP
237 if (ret < 0)
238 return ERR_PTR(ret);
239
989af883
LP
240 return lut;
241}
This page took 0.217905 seconds and 5 git commands to generate.