2 * vsp1_lut.c -- R-Car VSP1 Look-Up Table
4 * Copyright (C) 2013 Renesas Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
14 #include <linux/device.h>
15 #include <linux/gfp.h>
17 #include <media/v4l2-subdev.h>
23 #define LUT_MIN_SIZE 4U
24 #define LUT_MAX_SIZE 8190U
26 /* -----------------------------------------------------------------------------
30 static inline void vsp1_lut_write(struct vsp1_lut
*lut
, struct vsp1_dl_list
*dl
,
33 vsp1_dl_list_write(dl
, reg
, data
);
36 /* -----------------------------------------------------------------------------
40 #define V4L2_CID_VSP1_LUT_TABLE (V4L2_CID_USER_BASE | 0x1001)
42 static int lut_set_table(struct vsp1_lut
*lut
, struct v4l2_ctrl
*ctrl
)
44 struct vsp1_dl_body
*dlb
;
47 dlb
= vsp1_dl_fragment_alloc(lut
->entity
.vsp1
, 256);
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
]);
55 spin_lock_irq(&lut
->lock
);
57 spin_unlock_irq(&lut
->lock
);
59 vsp1_dl_fragment_free(dlb
);
63 static int lut_s_ctrl(struct v4l2_ctrl
*ctrl
)
65 struct vsp1_lut
*lut
=
66 container_of(ctrl
->handler
, struct vsp1_lut
, ctrls
);
69 case V4L2_CID_VSP1_LUT_TABLE
:
70 lut_set_table(lut
, ctrl
);
77 static const struct v4l2_ctrl_ops lut_ctrl_ops
= {
81 static const struct v4l2_ctrl_config lut_table_control
= {
83 .id
= V4L2_CID_VSP1_LUT_TABLE
,
84 .name
= "Look-Up Table",
85 .type
= V4L2_CTRL_TYPE_U32
,
93 /* -----------------------------------------------------------------------------
94 * V4L2 Subdevice Pad Operations
97 static int lut_enum_mbus_code(struct v4l2_subdev
*subdev
,
98 struct v4l2_subdev_pad_config
*cfg
,
99 struct v4l2_subdev_mbus_code_enum
*code
)
101 static const unsigned int codes
[] = {
102 MEDIA_BUS_FMT_ARGB8888_1X32
,
103 MEDIA_BUS_FMT_AHSV8888_1X32
,
104 MEDIA_BUS_FMT_AYUV8_1X32
,
107 return vsp1_subdev_enum_mbus_code(subdev
, cfg
, code
, codes
,
111 static int lut_enum_frame_size(struct v4l2_subdev
*subdev
,
112 struct v4l2_subdev_pad_config
*cfg
,
113 struct v4l2_subdev_frame_size_enum
*fse
)
115 return vsp1_subdev_enum_frame_size(subdev
, cfg
, fse
, LUT_MIN_SIZE
,
116 LUT_MIN_SIZE
, LUT_MAX_SIZE
,
120 static int lut_set_format(struct v4l2_subdev
*subdev
,
121 struct v4l2_subdev_pad_config
*cfg
,
122 struct v4l2_subdev_format
*fmt
)
124 struct vsp1_lut
*lut
= to_lut(subdev
);
125 struct v4l2_subdev_pad_config
*config
;
126 struct v4l2_mbus_framefmt
*format
;
129 mutex_lock(&lut
->entity
.lock
);
131 config
= vsp1_entity_get_pad_config(&lut
->entity
, cfg
, fmt
->which
);
137 /* Default to YUV if the requested format is not supported. */
138 if (fmt
->format
.code
!= MEDIA_BUS_FMT_ARGB8888_1X32
&&
139 fmt
->format
.code
!= MEDIA_BUS_FMT_AHSV8888_1X32
&&
140 fmt
->format
.code
!= MEDIA_BUS_FMT_AYUV8_1X32
)
141 fmt
->format
.code
= MEDIA_BUS_FMT_AYUV8_1X32
;
143 format
= vsp1_entity_get_pad_format(&lut
->entity
, config
, fmt
->pad
);
145 if (fmt
->pad
== LUT_PAD_SOURCE
) {
146 /* The LUT output format can't be modified. */
147 fmt
->format
= *format
;
151 format
->code
= fmt
->format
.code
;
152 format
->width
= clamp_t(unsigned int, fmt
->format
.width
,
153 LUT_MIN_SIZE
, LUT_MAX_SIZE
);
154 format
->height
= clamp_t(unsigned int, fmt
->format
.height
,
155 LUT_MIN_SIZE
, LUT_MAX_SIZE
);
156 format
->field
= V4L2_FIELD_NONE
;
157 format
->colorspace
= V4L2_COLORSPACE_SRGB
;
159 fmt
->format
= *format
;
161 /* Propagate the format to the source pad. */
162 format
= vsp1_entity_get_pad_format(&lut
->entity
, config
,
164 *format
= fmt
->format
;
167 mutex_unlock(&lut
->entity
.lock
);
171 /* -----------------------------------------------------------------------------
172 * V4L2 Subdevice Operations
175 static const struct v4l2_subdev_pad_ops lut_pad_ops
= {
176 .init_cfg
= vsp1_entity_init_cfg
,
177 .enum_mbus_code
= lut_enum_mbus_code
,
178 .enum_frame_size
= lut_enum_frame_size
,
179 .get_fmt
= vsp1_subdev_get_pad_format
,
180 .set_fmt
= lut_set_format
,
183 static const struct v4l2_subdev_ops lut_ops
= {
187 /* -----------------------------------------------------------------------------
188 * VSP1 Entity Operations
191 static void lut_configure(struct vsp1_entity
*entity
,
192 struct vsp1_pipeline
*pipe
,
193 struct vsp1_dl_list
*dl
,
194 enum vsp1_entity_params params
)
196 struct vsp1_lut
*lut
= to_lut(&entity
->subdev
);
197 struct vsp1_dl_body
*dlb
;
201 case VSP1_ENTITY_PARAMS_INIT
:
202 vsp1_lut_write(lut
, dl
, VI6_LUT_CTRL
, VI6_LUT_CTRL_EN
);
205 case VSP1_ENTITY_PARAMS_PARTITION
:
208 case VSP1_ENTITY_PARAMS_RUNTIME
:
209 spin_lock_irqsave(&lut
->lock
, flags
);
212 spin_unlock_irqrestore(&lut
->lock
, flags
);
215 vsp1_dl_list_add_fragment(dl
, dlb
);
220 static const struct vsp1_entity_operations lut_entity_ops
= {
221 .configure
= lut_configure
,
224 /* -----------------------------------------------------------------------------
225 * Initialization and Cleanup
228 struct vsp1_lut
*vsp1_lut_create(struct vsp1_device
*vsp1
)
230 struct vsp1_lut
*lut
;
233 lut
= devm_kzalloc(vsp1
->dev
, sizeof(*lut
), GFP_KERNEL
);
235 return ERR_PTR(-ENOMEM
);
237 spin_lock_init(&lut
->lock
);
239 lut
->entity
.ops
= &lut_entity_ops
;
240 lut
->entity
.type
= VSP1_ENTITY_LUT
;
242 ret
= vsp1_entity_init(vsp1
, &lut
->entity
, "lut", 2, &lut_ops
,
243 MEDIA_ENT_F_PROC_VIDEO_LUT
);
247 /* Initialize the control handler. */
248 v4l2_ctrl_handler_init(&lut
->ctrls
, 1);
249 v4l2_ctrl_new_custom(&lut
->ctrls
, &lut_table_control
, NULL
);
251 lut
->entity
.subdev
.ctrl_handler
= &lut
->ctrls
;
253 if (lut
->ctrls
.error
) {
254 dev_err(vsp1
->dev
, "lut: failed to initialize controls\n");
255 ret
= lut
->ctrls
.error
;
256 vsp1_entity_destroy(&lut
->entity
);
260 v4l2_ctrl_handler_setup(&lut
->ctrls
);