blob: 7360586c902ac0dce55d83abe564fc84e517a7f9 [file] [log] [blame]
Laurent Pinchart5cdf5742013-07-10 17:30:14 -03001/*
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_hsit.h"
21
22#define HSIT_MIN_SIZE 4U
23#define HSIT_MAX_SIZE 8190U
24
25/* -----------------------------------------------------------------------------
26 * Device Access
27 */
28
Laurent Pinchart5cdf5742013-07-10 17:30:14 -030029static inline void vsp1_hsit_write(struct vsp1_hsit *hsit, u32 reg, u32 data)
30{
Laurent Pinchart773abaf2015-11-01 12:26:14 -020031 vsp1_mod_write(&hsit->entity, reg, data);
Laurent Pinchart5cdf5742013-07-10 17:30:14 -030032}
33
34/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -020035 * V4L2 Subdevice Operations
Laurent Pinchart5cdf5742013-07-10 17:30:14 -030036 */
37
38static int hsit_enum_mbus_code(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080039 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart5cdf5742013-07-10 17:30:14 -030040 struct v4l2_subdev_mbus_code_enum *code)
41{
42 struct vsp1_hsit *hsit = to_hsit(subdev);
43
44 if (code->index > 0)
45 return -EINVAL;
46
47 if ((code->pad == HSIT_PAD_SINK && !hsit->inverse) |
48 (code->pad == HSIT_PAD_SOURCE && hsit->inverse))
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030049 code->code = MEDIA_BUS_FMT_ARGB8888_1X32;
Laurent Pinchart5cdf5742013-07-10 17:30:14 -030050 else
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -030051 code->code = MEDIA_BUS_FMT_AHSV8888_1X32;
Laurent Pinchart5cdf5742013-07-10 17:30:14 -030052
53 return 0;
54}
55
56static int hsit_enum_frame_size(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080057 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart5cdf5742013-07-10 17:30:14 -030058 struct v4l2_subdev_frame_size_enum *fse)
59{
Hans Verkuil5778e742015-03-04 01:47:58 -080060 struct vsp1_hsit *hsit = to_hsit(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -020061 struct v4l2_subdev_pad_config *config;
Laurent Pinchart5cdf5742013-07-10 17:30:14 -030062 struct v4l2_mbus_framefmt *format;
63
Laurent Pincharte790c3c2015-11-15 19:14:22 -020064 config = vsp1_entity_get_pad_config(&hsit->entity, cfg, fse->which);
65 if (!config)
66 return -EINVAL;
67
68 format = vsp1_entity_get_pad_format(&hsit->entity, config, fse->pad);
Laurent Pinchart5cdf5742013-07-10 17:30:14 -030069
70 if (fse->index || fse->code != format->code)
71 return -EINVAL;
72
73 if (fse->pad == HSIT_PAD_SINK) {
74 fse->min_width = HSIT_MIN_SIZE;
75 fse->max_width = HSIT_MAX_SIZE;
76 fse->min_height = HSIT_MIN_SIZE;
77 fse->max_height = HSIT_MAX_SIZE;
78 } else {
79 /* The size on the source pad are fixed and always identical to
80 * the size on the sink pad.
81 */
82 fse->min_width = format->width;
83 fse->max_width = format->width;
84 fse->min_height = format->height;
85 fse->max_height = format->height;
86 }
87
88 return 0;
89}
90
91static int hsit_get_format(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -080092 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart5cdf5742013-07-10 17:30:14 -030093 struct v4l2_subdev_format *fmt)
94{
95 struct vsp1_hsit *hsit = to_hsit(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -020096 struct v4l2_subdev_pad_config *config;
Laurent Pinchart5cdf5742013-07-10 17:30:14 -030097
Laurent Pincharte790c3c2015-11-15 19:14:22 -020098 config = vsp1_entity_get_pad_config(&hsit->entity, cfg, fmt->which);
99 if (!config)
100 return -EINVAL;
101
102 fmt->format = *vsp1_entity_get_pad_format(&hsit->entity, config,
103 fmt->pad);
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300104
105 return 0;
106}
107
108static int hsit_set_format(struct v4l2_subdev *subdev,
Hans Verkuilf7234132015-03-04 01:47:54 -0800109 struct v4l2_subdev_pad_config *cfg,
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300110 struct v4l2_subdev_format *fmt)
111{
112 struct vsp1_hsit *hsit = to_hsit(subdev);
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200113 struct v4l2_subdev_pad_config *config;
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300114 struct v4l2_mbus_framefmt *format;
115
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200116 config = vsp1_entity_get_pad_config(&hsit->entity, cfg, fmt->which);
117 if (!config)
118 return -EINVAL;
119
120 format = vsp1_entity_get_pad_format(&hsit->entity, config, fmt->pad);
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300121
122 if (fmt->pad == HSIT_PAD_SOURCE) {
123 /* The HST and HSI output format code and resolution can't be
124 * modified.
125 */
126 fmt->format = *format;
127 return 0;
128 }
129
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300130 format->code = hsit->inverse ? MEDIA_BUS_FMT_AHSV8888_1X32
131 : MEDIA_BUS_FMT_ARGB8888_1X32;
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300132 format->width = clamp_t(unsigned int, fmt->format.width,
133 HSIT_MIN_SIZE, HSIT_MAX_SIZE);
134 format->height = clamp_t(unsigned int, fmt->format.height,
135 HSIT_MIN_SIZE, HSIT_MAX_SIZE);
136 format->field = V4L2_FIELD_NONE;
137 format->colorspace = V4L2_COLORSPACE_SRGB;
138
139 fmt->format = *format;
140
141 /* Propagate the format to the source pad. */
Laurent Pincharte790c3c2015-11-15 19:14:22 -0200142 format = vsp1_entity_get_pad_format(&hsit->entity, config,
143 HSIT_PAD_SOURCE);
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300144 *format = fmt->format;
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300145 format->code = hsit->inverse ? MEDIA_BUS_FMT_ARGB8888_1X32
146 : MEDIA_BUS_FMT_AHSV8888_1X32;
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300147
148 return 0;
149}
150
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300151static struct v4l2_subdev_pad_ops hsit_pad_ops = {
Laurent Pinchart0efdf0f2015-11-15 20:09:08 -0200152 .init_cfg = vsp1_entity_init_cfg,
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300153 .enum_mbus_code = hsit_enum_mbus_code,
154 .enum_frame_size = hsit_enum_frame_size,
155 .get_fmt = hsit_get_format,
156 .set_fmt = hsit_set_format,
157};
158
159static struct v4l2_subdev_ops hsit_ops = {
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300160 .pad = &hsit_pad_ops,
161};
162
163/* -----------------------------------------------------------------------------
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200164 * VSP1 Entity Operations
165 */
166
167static void hsit_configure(struct vsp1_entity *entity)
168{
169 struct vsp1_hsit *hsit = to_hsit(&entity->subdev);
170
171 if (hsit->inverse)
172 vsp1_hsit_write(hsit, VI6_HSI_CTRL, VI6_HSI_CTRL_EN);
173 else
174 vsp1_hsit_write(hsit, VI6_HST_CTRL, VI6_HST_CTRL_EN);
175}
176
177static const struct vsp1_entity_operations hsit_entity_ops = {
178 .configure = hsit_configure,
179};
180
181/* -----------------------------------------------------------------------------
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300182 * Initialization and Cleanup
183 */
184
185struct vsp1_hsit *vsp1_hsit_create(struct vsp1_device *vsp1, bool inverse)
186{
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300187 struct vsp1_hsit *hsit;
188 int ret;
189
190 hsit = devm_kzalloc(vsp1->dev, sizeof(*hsit), GFP_KERNEL);
191 if (hsit == NULL)
192 return ERR_PTR(-ENOMEM);
193
194 hsit->inverse = inverse;
195
Laurent Pinchart7b905f02015-11-17 13:10:26 -0200196 hsit->entity.ops = &hsit_entity_ops;
197
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300198 if (inverse)
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300199 hsit->entity.type = VSP1_ENTITY_HSI;
Laurent Pinchartd9b45ed2013-07-10 18:37:27 -0300200 else
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300201 hsit->entity.type = VSP1_ENTITY_HST;
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300202
Laurent Pinchart823329d2015-11-15 19:42:01 -0200203 ret = vsp1_entity_init(vsp1, &hsit->entity, inverse ? "hsi" : "hst", 2,
204 &hsit_ops);
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300205 if (ret < 0)
206 return ERR_PTR(ret);
207
Laurent Pinchart5cdf5742013-07-10 17:30:14 -0300208 return hsit;
209}