[media] v4l: vsp1: Factorize get pad format code
[deliverable/linux.git] / drivers / media / platform / vsp1 / vsp1_hsit.c
1 /*
2 * vsp1_hsit.c -- R-Car VSP1 Hue Saturation value (Inverse) Transform
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_hsit.h"
22
23 #define HSIT_MIN_SIZE 4U
24 #define HSIT_MAX_SIZE 8190U
25
26 /* -----------------------------------------------------------------------------
27 * Device Access
28 */
29
30 static inline void vsp1_hsit_write(struct vsp1_hsit *hsit,
31 struct vsp1_dl_list *dl, u32 reg, u32 data)
32 {
33 vsp1_dl_list_write(dl, reg, data);
34 }
35
36 /* -----------------------------------------------------------------------------
37 * V4L2 Subdevice Operations
38 */
39
40 static int hsit_enum_mbus_code(struct v4l2_subdev *subdev,
41 struct v4l2_subdev_pad_config *cfg,
42 struct v4l2_subdev_mbus_code_enum *code)
43 {
44 struct vsp1_hsit *hsit = to_hsit(subdev);
45
46 if (code->index > 0)
47 return -EINVAL;
48
49 if ((code->pad == HSIT_PAD_SINK && !hsit->inverse) |
50 (code->pad == HSIT_PAD_SOURCE && hsit->inverse))
51 code->code = MEDIA_BUS_FMT_ARGB8888_1X32;
52 else
53 code->code = MEDIA_BUS_FMT_AHSV8888_1X32;
54
55 return 0;
56 }
57
58 static int hsit_enum_frame_size(struct v4l2_subdev *subdev,
59 struct v4l2_subdev_pad_config *cfg,
60 struct v4l2_subdev_frame_size_enum *fse)
61 {
62 struct vsp1_hsit *hsit = to_hsit(subdev);
63 struct v4l2_subdev_pad_config *config;
64 struct v4l2_mbus_framefmt *format;
65
66 config = vsp1_entity_get_pad_config(&hsit->entity, cfg, fse->which);
67 if (!config)
68 return -EINVAL;
69
70 format = vsp1_entity_get_pad_format(&hsit->entity, config, fse->pad);
71
72 if (fse->index || fse->code != format->code)
73 return -EINVAL;
74
75 if (fse->pad == HSIT_PAD_SINK) {
76 fse->min_width = HSIT_MIN_SIZE;
77 fse->max_width = HSIT_MAX_SIZE;
78 fse->min_height = HSIT_MIN_SIZE;
79 fse->max_height = HSIT_MAX_SIZE;
80 } else {
81 /* The size on the source pad are fixed and always identical to
82 * the size on the sink pad.
83 */
84 fse->min_width = format->width;
85 fse->max_width = format->width;
86 fse->min_height = format->height;
87 fse->max_height = format->height;
88 }
89
90 return 0;
91 }
92
93 static int hsit_set_format(struct v4l2_subdev *subdev,
94 struct v4l2_subdev_pad_config *cfg,
95 struct v4l2_subdev_format *fmt)
96 {
97 struct vsp1_hsit *hsit = to_hsit(subdev);
98 struct v4l2_subdev_pad_config *config;
99 struct v4l2_mbus_framefmt *format;
100
101 config = vsp1_entity_get_pad_config(&hsit->entity, cfg, fmt->which);
102 if (!config)
103 return -EINVAL;
104
105 format = vsp1_entity_get_pad_format(&hsit->entity, config, fmt->pad);
106
107 if (fmt->pad == HSIT_PAD_SOURCE) {
108 /* The HST and HSI output format code and resolution can't be
109 * modified.
110 */
111 fmt->format = *format;
112 return 0;
113 }
114
115 format->code = hsit->inverse ? MEDIA_BUS_FMT_AHSV8888_1X32
116 : MEDIA_BUS_FMT_ARGB8888_1X32;
117 format->width = clamp_t(unsigned int, fmt->format.width,
118 HSIT_MIN_SIZE, HSIT_MAX_SIZE);
119 format->height = clamp_t(unsigned int, fmt->format.height,
120 HSIT_MIN_SIZE, HSIT_MAX_SIZE);
121 format->field = V4L2_FIELD_NONE;
122 format->colorspace = V4L2_COLORSPACE_SRGB;
123
124 fmt->format = *format;
125
126 /* Propagate the format to the source pad. */
127 format = vsp1_entity_get_pad_format(&hsit->entity, config,
128 HSIT_PAD_SOURCE);
129 *format = fmt->format;
130 format->code = hsit->inverse ? MEDIA_BUS_FMT_ARGB8888_1X32
131 : MEDIA_BUS_FMT_AHSV8888_1X32;
132
133 return 0;
134 }
135
136 static struct v4l2_subdev_pad_ops hsit_pad_ops = {
137 .init_cfg = vsp1_entity_init_cfg,
138 .enum_mbus_code = hsit_enum_mbus_code,
139 .enum_frame_size = hsit_enum_frame_size,
140 .get_fmt = vsp1_subdev_get_pad_format,
141 .set_fmt = hsit_set_format,
142 };
143
144 static struct v4l2_subdev_ops hsit_ops = {
145 .pad = &hsit_pad_ops,
146 };
147
148 /* -----------------------------------------------------------------------------
149 * VSP1 Entity Operations
150 */
151
152 static void hsit_configure(struct vsp1_entity *entity,
153 struct vsp1_pipeline *pipe,
154 struct vsp1_dl_list *dl)
155 {
156 struct vsp1_hsit *hsit = to_hsit(&entity->subdev);
157
158 if (hsit->inverse)
159 vsp1_hsit_write(hsit, dl, VI6_HSI_CTRL, VI6_HSI_CTRL_EN);
160 else
161 vsp1_hsit_write(hsit, dl, VI6_HST_CTRL, VI6_HST_CTRL_EN);
162 }
163
164 static const struct vsp1_entity_operations hsit_entity_ops = {
165 .configure = hsit_configure,
166 };
167
168 /* -----------------------------------------------------------------------------
169 * Initialization and Cleanup
170 */
171
172 struct vsp1_hsit *vsp1_hsit_create(struct vsp1_device *vsp1, bool inverse)
173 {
174 struct vsp1_hsit *hsit;
175 int ret;
176
177 hsit = devm_kzalloc(vsp1->dev, sizeof(*hsit), GFP_KERNEL);
178 if (hsit == NULL)
179 return ERR_PTR(-ENOMEM);
180
181 hsit->inverse = inverse;
182
183 hsit->entity.ops = &hsit_entity_ops;
184
185 if (inverse)
186 hsit->entity.type = VSP1_ENTITY_HSI;
187 else
188 hsit->entity.type = VSP1_ENTITY_HST;
189
190 ret = vsp1_entity_init(vsp1, &hsit->entity, inverse ? "hsi" : "hst", 2,
191 &hsit_ops);
192 if (ret < 0)
193 return ERR_PTR(ret);
194
195 return hsit;
196 }
This page took 0.035018 seconds and 5 git commands to generate.