[media] v4l: vsp1: Support runtime modification of controls
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_lut.c
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
17 #include <media/v4l2-subdev.h>
18
19 #include "vsp1.h"
20 #include "vsp1_dl.h"
21 #include "vsp1_lut.h"
22
23 #define LUT_MIN_SIZE 4U
24 #define LUT_MAX_SIZE 8190U
25
26 /* -----------------------------------------------------------------------------
27 * Device Access
28 */
29
30 static inline void vsp1_lut_write(struct vsp1_lut *lut, struct vsp1_dl_list *dl,
31 u32 reg, u32 data)
32 {
33 vsp1_dl_list_write(dl, reg, data);
34 }
35
36 /* -----------------------------------------------------------------------------
37 * Controls
38 */
39
40 #define V4L2_CID_VSP1_LUT_TABLE (V4L2_CID_USER_BASE | 0x1001)
41
42 static int lut_set_table(struct vsp1_lut *lut, struct v4l2_ctrl *ctrl)
43 {
44 struct vsp1_dl_body *dlb;
45 unsigned int i;
46
47 dlb = vsp1_dl_fragment_alloc(lut->entity.vsp1, 256);
48 if (!dlb)
49 return -ENOMEM;
50
51 for (i = 0; i < 256; ++i)
52 vsp1_dl_fragment_write(dlb, VI6_LUT_TABLE + 4 * i,
53 ctrl->p_new.p_u32[i]);
54
55 swap(lut->lut, dlb);
56
57 vsp1_dl_fragment_free(dlb);
58 return 0;
59 }
60
61 static int lut_s_ctrl(struct v4l2_ctrl *ctrl)
62 {
63 struct vsp1_lut *lut =
64 container_of(ctrl->handler, struct vsp1_lut, ctrls);
65
66 switch (ctrl->id) {
67 case V4L2_CID_VSP1_LUT_TABLE:
68 lut_set_table(lut, ctrl);
69 break;
70 }
71
72 return 0;
73 }
74
75 static const struct v4l2_ctrl_ops lut_ctrl_ops = {
76 .s_ctrl = lut_s_ctrl,
77 };
78
79 static const struct v4l2_ctrl_config lut_table_control = {
80 .ops = &lut_ctrl_ops,
81 .id = V4L2_CID_VSP1_LUT_TABLE,
82 .name = "Look-Up Table",
83 .type = V4L2_CTRL_TYPE_U32,
84 .min = 0x00000000,
85 .max = 0x00ffffff,
86 .step = 1,
87 .def = 0,
88 .dims = { 256},
89 };
90
91 /* -----------------------------------------------------------------------------
92 * V4L2 Subdevice Pad Operations
93 */
94
95 static int lut_enum_mbus_code(struct v4l2_subdev *subdev,
96 struct v4l2_subdev_pad_config *cfg,
97 struct v4l2_subdev_mbus_code_enum *code)
98 {
99 static const unsigned int codes[] = {
100 MEDIA_BUS_FMT_ARGB8888_1X32,
101 MEDIA_BUS_FMT_AHSV8888_1X32,
102 MEDIA_BUS_FMT_AYUV8_1X32,
103 };
104
105 return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
106 ARRAY_SIZE(codes));
107 }
108
109 static int lut_enum_frame_size(struct v4l2_subdev *subdev,
110 struct v4l2_subdev_pad_config *cfg,
111 struct v4l2_subdev_frame_size_enum *fse)
112 {
113 return vsp1_subdev_enum_frame_size(subdev, cfg, fse, LUT_MIN_SIZE,
114 LUT_MIN_SIZE, LUT_MAX_SIZE,
115 LUT_MAX_SIZE);
116 }
117
118 static int lut_set_format(struct v4l2_subdev *subdev,
119 struct v4l2_subdev_pad_config *cfg,
120 struct v4l2_subdev_format *fmt)
121 {
122 struct vsp1_lut *lut = to_lut(subdev);
123 struct v4l2_subdev_pad_config *config;
124 struct v4l2_mbus_framefmt *format;
125
126 config = vsp1_entity_get_pad_config(&lut->entity, cfg, fmt->which);
127 if (!config)
128 return -EINVAL;
129
130 /* Default to YUV if the requested format is not supported. */
131 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
132 fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
133 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
134 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
135
136 format = vsp1_entity_get_pad_format(&lut->entity, config, fmt->pad);
137
138 if (fmt->pad == LUT_PAD_SOURCE) {
139 /* The LUT output format can't be modified. */
140 fmt->format = *format;
141 return 0;
142 }
143
144 format->code = fmt->format.code;
145 format->width = clamp_t(unsigned int, fmt->format.width,
146 LUT_MIN_SIZE, LUT_MAX_SIZE);
147 format->height = clamp_t(unsigned int, fmt->format.height,
148 LUT_MIN_SIZE, LUT_MAX_SIZE);
149 format->field = V4L2_FIELD_NONE;
150 format->colorspace = V4L2_COLORSPACE_SRGB;
151
152 fmt->format = *format;
153
154 /* Propagate the format to the source pad. */
155 format = vsp1_entity_get_pad_format(&lut->entity, config,
156 LUT_PAD_SOURCE);
157 *format = fmt->format;
158
159 return 0;
160 }
161
162 /* -----------------------------------------------------------------------------
163 * V4L2 Subdevice Operations
164 */
165
166 static const struct v4l2_subdev_pad_ops lut_pad_ops = {
167 .init_cfg = vsp1_entity_init_cfg,
168 .enum_mbus_code = lut_enum_mbus_code,
169 .enum_frame_size = lut_enum_frame_size,
170 .get_fmt = vsp1_subdev_get_pad_format,
171 .set_fmt = lut_set_format,
172 };
173
174 static const struct v4l2_subdev_ops lut_ops = {
175 .pad = &lut_pad_ops,
176 };
177
178 /* -----------------------------------------------------------------------------
179 * VSP1 Entity Operations
180 */
181
182 static void lut_configure(struct vsp1_entity *entity,
183 struct vsp1_pipeline *pipe,
184 struct vsp1_dl_list *dl, bool full)
185 {
186 struct vsp1_lut *lut = to_lut(&entity->subdev);
187
188 if (!full)
189 return;
190
191 vsp1_lut_write(lut, dl, VI6_LUT_CTRL, VI6_LUT_CTRL_EN);
192
193 mutex_lock(lut->ctrls.lock);
194
195 if (lut->lut) {
196 vsp1_dl_list_add_fragment(dl, lut->lut);
197 lut->lut = NULL;
198 }
199
200 mutex_unlock(lut->ctrls.lock);
201 }
202
203 static const struct vsp1_entity_operations lut_entity_ops = {
204 .configure = lut_configure,
205 };
206
207 /* -----------------------------------------------------------------------------
208 * Initialization and Cleanup
209 */
210
211 struct vsp1_lut *vsp1_lut_create(struct vsp1_device *vsp1)
212 {
213 struct vsp1_lut *lut;
214 int ret;
215
216 lut = devm_kzalloc(vsp1->dev, sizeof(*lut), GFP_KERNEL);
217 if (lut == NULL)
218 return ERR_PTR(-ENOMEM);
219
220 lut->entity.ops = &lut_entity_ops;
221 lut->entity.type = VSP1_ENTITY_LUT;
222
223 ret = vsp1_entity_init(vsp1, &lut->entity, "lut", 2, &lut_ops,
224 MEDIA_ENT_F_PROC_VIDEO_LUT);
225 if (ret < 0)
226 return ERR_PTR(ret);
227
228 /* Initialize the control handler. */
229 v4l2_ctrl_handler_init(&lut->ctrls, 1);
230 v4l2_ctrl_new_custom(&lut->ctrls, &lut_table_control, NULL);
231
232 lut->entity.subdev.ctrl_handler = &lut->ctrls;
233
234 if (lut->ctrls.error) {
235 dev_err(vsp1->dev, "lut: failed to initialize controls\n");
236 ret = lut->ctrls.error;
237 vsp1_entity_destroy(&lut->entity);
238 return ERR_PTR(ret);
239 }
240
241 v4l2_ctrl_handler_setup(&lut->ctrls);
242
243 return lut;
244 }
This page took 0.039404 seconds and 5 git commands to generate.