cea86e77f7f1107703e8bbfb3348156ad0fb3104
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_clu.c
1 /*
2 * vsp1_clu.c -- R-Car VSP1 Cubic Look-Up Table
3 *
4 * Copyright (C) 2015-2016 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
14 #include <linux/device.h>
15 #include <linux/slab.h>
16
17 #include <media/v4l2-subdev.h>
18
19 #include "vsp1.h"
20 #include "vsp1_clu.h"
21 #include "vsp1_dl.h"
22
23 #define CLU_MIN_SIZE 4U
24 #define CLU_MAX_SIZE 8190U
25
26 /* -----------------------------------------------------------------------------
27 * Device Access
28 */
29
30 static inline void vsp1_clu_write(struct vsp1_clu *clu, 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_CLU_TABLE (V4L2_CID_USER_BASE | 0x1001)
41 #define V4L2_CID_VSP1_CLU_MODE (V4L2_CID_USER_BASE | 0x1002)
42 #define V4L2_CID_VSP1_CLU_MODE_2D 0
43 #define V4L2_CID_VSP1_CLU_MODE_3D 1
44
45 static int clu_set_table(struct vsp1_clu *clu, struct v4l2_ctrl *ctrl)
46 {
47 struct vsp1_dl_body *dlb;
48 unsigned int i;
49
50 dlb = vsp1_dl_fragment_alloc(clu->entity.vsp1, 1 + 17 * 17 * 17);
51 if (!dlb)
52 return -ENOMEM;
53
54 vsp1_dl_fragment_write(dlb, VI6_CLU_ADDR, 0);
55 for (i = 0; i < 17 * 17 * 17; ++i)
56 vsp1_dl_fragment_write(dlb, VI6_CLU_DATA, ctrl->p_new.p_u32[i]);
57
58 swap(clu->clu, dlb);
59
60 vsp1_dl_fragment_free(dlb);
61 return 0;
62 }
63
64 static int clu_s_ctrl(struct v4l2_ctrl *ctrl)
65 {
66 struct vsp1_clu *clu =
67 container_of(ctrl->handler, struct vsp1_clu, ctrls);
68
69 switch (ctrl->id) {
70 case V4L2_CID_VSP1_CLU_TABLE:
71 clu_set_table(clu, ctrl);
72 break;
73
74 case V4L2_CID_VSP1_CLU_MODE:
75 clu->mode = ctrl->val;
76 break;
77 }
78
79 return 0;
80 }
81
82 static const struct v4l2_ctrl_ops clu_ctrl_ops = {
83 .s_ctrl = clu_s_ctrl,
84 };
85
86 static const struct v4l2_ctrl_config clu_table_control = {
87 .ops = &clu_ctrl_ops,
88 .id = V4L2_CID_VSP1_CLU_TABLE,
89 .name = "Look-Up Table",
90 .type = V4L2_CTRL_TYPE_U32,
91 .min = 0x00000000,
92 .max = 0x00ffffff,
93 .step = 1,
94 .def = 0,
95 .dims = { 17, 17, 17 },
96 };
97
98 static const char * const clu_mode_menu[] = {
99 "2D",
100 "3D",
101 NULL,
102 };
103
104 static const struct v4l2_ctrl_config clu_mode_control = {
105 .ops = &clu_ctrl_ops,
106 .id = V4L2_CID_VSP1_CLU_MODE,
107 .name = "Mode",
108 .type = V4L2_CTRL_TYPE_MENU,
109 .min = 0,
110 .max = 1,
111 .def = 1,
112 .qmenu = clu_mode_menu,
113 };
114
115 /* -----------------------------------------------------------------------------
116 * V4L2 Subdevice Pad Operations
117 */
118
119 static int clu_enum_mbus_code(struct v4l2_subdev *subdev,
120 struct v4l2_subdev_pad_config *cfg,
121 struct v4l2_subdev_mbus_code_enum *code)
122 {
123 static const unsigned int codes[] = {
124 MEDIA_BUS_FMT_ARGB8888_1X32,
125 MEDIA_BUS_FMT_AHSV8888_1X32,
126 MEDIA_BUS_FMT_AYUV8_1X32,
127 };
128
129 return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
130 ARRAY_SIZE(codes));
131 }
132
133 static int clu_enum_frame_size(struct v4l2_subdev *subdev,
134 struct v4l2_subdev_pad_config *cfg,
135 struct v4l2_subdev_frame_size_enum *fse)
136 {
137 return vsp1_subdev_enum_frame_size(subdev, cfg, fse, CLU_MIN_SIZE,
138 CLU_MIN_SIZE, CLU_MAX_SIZE,
139 CLU_MAX_SIZE);
140 }
141
142 static int clu_set_format(struct v4l2_subdev *subdev,
143 struct v4l2_subdev_pad_config *cfg,
144 struct v4l2_subdev_format *fmt)
145 {
146 struct vsp1_clu *clu = to_clu(subdev);
147 struct v4l2_subdev_pad_config *config;
148 struct v4l2_mbus_framefmt *format;
149
150 config = vsp1_entity_get_pad_config(&clu->entity, cfg, fmt->which);
151 if (!config)
152 return -EINVAL;
153
154 /* Default to YUV if the requested format is not supported. */
155 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
156 fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
157 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
158 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
159
160 format = vsp1_entity_get_pad_format(&clu->entity, config, fmt->pad);
161
162 if (fmt->pad == CLU_PAD_SOURCE) {
163 /* The CLU output format can't be modified. */
164 fmt->format = *format;
165 return 0;
166 }
167
168 format->code = fmt->format.code;
169 format->width = clamp_t(unsigned int, fmt->format.width,
170 CLU_MIN_SIZE, CLU_MAX_SIZE);
171 format->height = clamp_t(unsigned int, fmt->format.height,
172 CLU_MIN_SIZE, CLU_MAX_SIZE);
173 format->field = V4L2_FIELD_NONE;
174 format->colorspace = V4L2_COLORSPACE_SRGB;
175
176 fmt->format = *format;
177
178 /* Propagate the format to the source pad. */
179 format = vsp1_entity_get_pad_format(&clu->entity, config,
180 CLU_PAD_SOURCE);
181 *format = fmt->format;
182
183 return 0;
184 }
185
186 /* -----------------------------------------------------------------------------
187 * V4L2 Subdevice Operations
188 */
189
190 static const struct v4l2_subdev_pad_ops clu_pad_ops = {
191 .init_cfg = vsp1_entity_init_cfg,
192 .enum_mbus_code = clu_enum_mbus_code,
193 .enum_frame_size = clu_enum_frame_size,
194 .get_fmt = vsp1_subdev_get_pad_format,
195 .set_fmt = clu_set_format,
196 };
197
198 static const struct v4l2_subdev_ops clu_ops = {
199 .pad = &clu_pad_ops,
200 };
201
202 /* -----------------------------------------------------------------------------
203 * VSP1 Entity Operations
204 */
205
206 static void clu_configure(struct vsp1_entity *entity,
207 struct vsp1_pipeline *pipe,
208 struct vsp1_dl_list *dl)
209 {
210 struct vsp1_clu *clu = to_clu(&entity->subdev);
211 struct v4l2_mbus_framefmt *format;
212 u32 ctrl = VI6_CLU_CTRL_AAI | VI6_CLU_CTRL_MVS | VI6_CLU_CTRL_EN;
213
214 format = vsp1_entity_get_pad_format(&clu->entity, clu->entity.config,
215 CLU_PAD_SINK);
216
217 mutex_lock(clu->ctrls.lock);
218
219 /* 2D mode can only be used with the YCbCr pixel encoding. */
220 if (clu->mode == V4L2_CID_VSP1_CLU_MODE_2D &&
221 format->code == MEDIA_BUS_FMT_AYUV8_1X32)
222 ctrl |= VI6_CLU_CTRL_AX1I_2D | VI6_CLU_CTRL_AX2I_2D
223 | VI6_CLU_CTRL_OS0_2D | VI6_CLU_CTRL_OS1_2D
224 | VI6_CLU_CTRL_OS2_2D | VI6_CLU_CTRL_M2D;
225
226 if (clu->clu) {
227 vsp1_dl_list_add_fragment(dl, clu->clu);
228 clu->clu = NULL;
229 }
230
231 mutex_unlock(clu->ctrls.lock);
232
233 vsp1_clu_write(clu, dl, VI6_CLU_CTRL, ctrl);
234 }
235
236 static const struct vsp1_entity_operations clu_entity_ops = {
237 .configure = clu_configure,
238 };
239
240 /* -----------------------------------------------------------------------------
241 * Initialization and Cleanup
242 */
243
244 struct vsp1_clu *vsp1_clu_create(struct vsp1_device *vsp1)
245 {
246 struct vsp1_clu *clu;
247 int ret;
248
249 clu = devm_kzalloc(vsp1->dev, sizeof(*clu), GFP_KERNEL);
250 if (clu == NULL)
251 return ERR_PTR(-ENOMEM);
252
253 clu->entity.ops = &clu_entity_ops;
254 clu->entity.type = VSP1_ENTITY_CLU;
255
256 ret = vsp1_entity_init(vsp1, &clu->entity, "clu", 2, &clu_ops,
257 MEDIA_ENT_F_PROC_VIDEO_LUT);
258 if (ret < 0)
259 return ERR_PTR(ret);
260
261 /* Initialize the control handler. */
262 v4l2_ctrl_handler_init(&clu->ctrls, 2);
263 v4l2_ctrl_new_custom(&clu->ctrls, &clu_table_control, NULL);
264 v4l2_ctrl_new_custom(&clu->ctrls, &clu_mode_control, NULL);
265
266 clu->entity.subdev.ctrl_handler = &clu->ctrls;
267
268 if (clu->ctrls.error) {
269 dev_err(vsp1->dev, "clu: failed to initialize controls\n");
270 ret = clu->ctrls.error;
271 vsp1_entity_destroy(&clu->entity);
272 return ERR_PTR(ret);
273 }
274
275 v4l2_ctrl_handler_setup(&clu->ctrls);
276
277 return clu;
278 }
This page took 0.037067 seconds and 4 git commands to generate.