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