[media] v4l: vsp1: Fix LUT format setting
[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 #include <linux/vsp1.h>
17
18 #include <media/v4l2-subdev.h>
19
20 #include "vsp1.h"
21 #include "vsp1_dl.h"
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
31 static inline void vsp1_lut_write(struct vsp1_lut *lut, struct vsp1_dl_list *dl,
32 u32 reg, u32 data)
33 {
34 vsp1_dl_list_write(dl, reg, data);
35 }
36
37 /* -----------------------------------------------------------------------------
38 * V4L2 Subdevice Core Operations
39 */
40
41 static void lut_set_table(struct vsp1_lut *lut, struct vsp1_lut_config *config)
42 {
43 memcpy_toio(lut->entity.vsp1->mmio + VI6_LUT_TABLE, config->lut,
44 sizeof(config->lut));
45 }
46
47 static 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:
53 lut_set_table(lut, arg);
54 return 0;
55
56 default:
57 return -ENOIOCTLCMD;
58 }
59 }
60
61 /* -----------------------------------------------------------------------------
62 * V4L2 Subdevice Pad Operations
63 */
64
65 static int lut_enum_mbus_code(struct v4l2_subdev *subdev,
66 struct v4l2_subdev_pad_config *cfg,
67 struct v4l2_subdev_mbus_code_enum *code)
68 {
69 static const unsigned int codes[] = {
70 MEDIA_BUS_FMT_ARGB8888_1X32,
71 MEDIA_BUS_FMT_AHSV8888_1X32,
72 MEDIA_BUS_FMT_AYUV8_1X32,
73 };
74
75 return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
76 ARRAY_SIZE(codes));
77 }
78
79 static int lut_enum_frame_size(struct v4l2_subdev *subdev,
80 struct v4l2_subdev_pad_config *cfg,
81 struct v4l2_subdev_frame_size_enum *fse)
82 {
83 return vsp1_subdev_enum_frame_size(subdev, cfg, fse, LUT_MIN_SIZE,
84 LUT_MIN_SIZE, LUT_MAX_SIZE,
85 LUT_MAX_SIZE);
86 }
87
88 static int lut_set_format(struct v4l2_subdev *subdev,
89 struct v4l2_subdev_pad_config *cfg,
90 struct v4l2_subdev_format *fmt)
91 {
92 struct vsp1_lut *lut = to_lut(subdev);
93 struct v4l2_subdev_pad_config *config;
94 struct v4l2_mbus_framefmt *format;
95
96 config = vsp1_entity_get_pad_config(&lut->entity, cfg, fmt->which);
97 if (!config)
98 return -EINVAL;
99
100 /* Default to YUV if the requested format is not supported. */
101 if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
102 fmt->format.code != MEDIA_BUS_FMT_AHSV8888_1X32 &&
103 fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
104 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
105
106 format = vsp1_entity_get_pad_format(&lut->entity, config, fmt->pad);
107
108 if (fmt->pad == LUT_PAD_SOURCE) {
109 /* The LUT output format can't be modified. */
110 fmt->format = *format;
111 return 0;
112 }
113
114 format->code = fmt->format.code;
115 format->width = clamp_t(unsigned int, fmt->format.width,
116 LUT_MIN_SIZE, LUT_MAX_SIZE);
117 format->height = clamp_t(unsigned int, fmt->format.height,
118 LUT_MIN_SIZE, LUT_MAX_SIZE);
119 format->field = V4L2_FIELD_NONE;
120 format->colorspace = V4L2_COLORSPACE_SRGB;
121
122 fmt->format = *format;
123
124 /* Propagate the format to the source pad. */
125 format = vsp1_entity_get_pad_format(&lut->entity, config,
126 LUT_PAD_SOURCE);
127 *format = fmt->format;
128
129 return 0;
130 }
131
132 /* -----------------------------------------------------------------------------
133 * V4L2 Subdevice Operations
134 */
135
136 static struct v4l2_subdev_core_ops lut_core_ops = {
137 .ioctl = lut_ioctl,
138 };
139
140 static struct v4l2_subdev_pad_ops lut_pad_ops = {
141 .init_cfg = vsp1_entity_init_cfg,
142 .enum_mbus_code = lut_enum_mbus_code,
143 .enum_frame_size = lut_enum_frame_size,
144 .get_fmt = vsp1_subdev_get_pad_format,
145 .set_fmt = lut_set_format,
146 };
147
148 static struct v4l2_subdev_ops lut_ops = {
149 .core = &lut_core_ops,
150 .pad = &lut_pad_ops,
151 };
152
153 /* -----------------------------------------------------------------------------
154 * VSP1 Entity Operations
155 */
156
157 static void lut_configure(struct vsp1_entity *entity,
158 struct vsp1_pipeline *pipe,
159 struct vsp1_dl_list *dl)
160 {
161 struct vsp1_lut *lut = to_lut(&entity->subdev);
162
163 vsp1_lut_write(lut, dl, VI6_LUT_CTRL, VI6_LUT_CTRL_EN);
164 }
165
166 static const struct vsp1_entity_operations lut_entity_ops = {
167 .configure = lut_configure,
168 };
169
170 /* -----------------------------------------------------------------------------
171 * Initialization and Cleanup
172 */
173
174 struct vsp1_lut *vsp1_lut_create(struct vsp1_device *vsp1)
175 {
176 struct vsp1_lut *lut;
177 int ret;
178
179 lut = devm_kzalloc(vsp1->dev, sizeof(*lut), GFP_KERNEL);
180 if (lut == NULL)
181 return ERR_PTR(-ENOMEM);
182
183 lut->entity.ops = &lut_entity_ops;
184 lut->entity.type = VSP1_ENTITY_LUT;
185
186 ret = vsp1_entity_init(vsp1, &lut->entity, "lut", 2, &lut_ops);
187 if (ret < 0)
188 return ERR_PTR(ret);
189
190 return lut;
191 }
This page took 0.035195 seconds and 5 git commands to generate.