]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/platform/vsp1/vsp1_lif.c
[media] v4l: vsp1: Implement and use the subdev pad::init_cfg configuration
[karo-tx-linux.git] / drivers / media / platform / vsp1 / vsp1_lif.c
1 /*
2  * vsp1_lif.c  --  R-Car VSP1 LCD Controller Interface
3  *
4  * Copyright (C) 2013-2014 Renesas Electronics 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_lif.h"
21
22 #define LIF_MIN_SIZE                            2U
23 #define LIF_MAX_SIZE                            2048U
24
25 /* -----------------------------------------------------------------------------
26  * Device Access
27  */
28
29 static inline void vsp1_lif_write(struct vsp1_lif *lif, u32 reg, u32 data)
30 {
31         vsp1_mod_write(&lif->entity, reg, data);
32 }
33
34 /* -----------------------------------------------------------------------------
35  * V4L2 Subdevice Core Operations
36  */
37
38 static int lif_s_stream(struct v4l2_subdev *subdev, int enable)
39 {
40         const struct v4l2_mbus_framefmt *format;
41         struct vsp1_lif *lif = to_lif(subdev);
42         unsigned int hbth = 1300;
43         unsigned int obth = 400;
44         unsigned int lbth = 200;
45
46         if (!enable) {
47                 vsp1_write(lif->entity.vsp1, VI6_LIF_CTRL, 0);
48                 return 0;
49         }
50
51         format = &lif->entity.formats[LIF_PAD_SOURCE];
52
53         obth = min(obth, (format->width + 1) / 2 * format->height - 4);
54
55         vsp1_lif_write(lif, VI6_LIF_CSBTH,
56                         (hbth << VI6_LIF_CSBTH_HBTH_SHIFT) |
57                         (lbth << VI6_LIF_CSBTH_LBTH_SHIFT));
58
59         vsp1_lif_write(lif, VI6_LIF_CTRL,
60                         (obth << VI6_LIF_CTRL_OBTH_SHIFT) |
61                         (format->code == 0 ? VI6_LIF_CTRL_CFMT : 0) |
62                         VI6_LIF_CTRL_REQSEL | VI6_LIF_CTRL_LIF_EN);
63
64         return 0;
65 }
66
67 /* -----------------------------------------------------------------------------
68  * V4L2 Subdevice Pad Operations
69  */
70
71 static int lif_enum_mbus_code(struct v4l2_subdev *subdev,
72                               struct v4l2_subdev_pad_config *cfg,
73                               struct v4l2_subdev_mbus_code_enum *code)
74 {
75         static const unsigned int codes[] = {
76                 MEDIA_BUS_FMT_ARGB8888_1X32,
77                 MEDIA_BUS_FMT_AYUV8_1X32,
78         };
79         struct vsp1_lif *lif = to_lif(subdev);
80
81         if (code->pad == LIF_PAD_SINK) {
82                 if (code->index >= ARRAY_SIZE(codes))
83                         return -EINVAL;
84
85                 code->code = codes[code->index];
86         } else {
87                 struct v4l2_mbus_framefmt *format;
88
89                 /* The LIF can't perform format conversion, the sink format is
90                  * always identical to the source format.
91                  */
92                 if (code->index)
93                         return -EINVAL;
94
95                 format = vsp1_entity_get_pad_format(&lif->entity, cfg,
96                                                     LIF_PAD_SINK, code->which);
97                 code->code = format->code;
98         }
99
100         return 0;
101 }
102
103 static int lif_enum_frame_size(struct v4l2_subdev *subdev,
104                                struct v4l2_subdev_pad_config *cfg,
105                                struct v4l2_subdev_frame_size_enum *fse)
106 {
107         struct vsp1_lif *lif = to_lif(subdev);
108         struct v4l2_mbus_framefmt *format;
109
110         format = vsp1_entity_get_pad_format(&lif->entity, cfg, LIF_PAD_SINK,
111                                             fse->which);
112
113         if (fse->index || fse->code != format->code)
114                 return -EINVAL;
115
116         if (fse->pad == LIF_PAD_SINK) {
117                 fse->min_width = LIF_MIN_SIZE;
118                 fse->max_width = LIF_MAX_SIZE;
119                 fse->min_height = LIF_MIN_SIZE;
120                 fse->max_height = LIF_MAX_SIZE;
121         } else {
122                 fse->min_width = format->width;
123                 fse->max_width = format->width;
124                 fse->min_height = format->height;
125                 fse->max_height = format->height;
126         }
127
128         return 0;
129 }
130
131 static int lif_get_format(struct v4l2_subdev *subdev,
132                           struct v4l2_subdev_pad_config *cfg,
133                           struct v4l2_subdev_format *fmt)
134 {
135         struct vsp1_lif *lif = to_lif(subdev);
136
137         fmt->format = *vsp1_entity_get_pad_format(&lif->entity, cfg, fmt->pad,
138                                                   fmt->which);
139
140         return 0;
141 }
142
143 static int lif_set_format(struct v4l2_subdev *subdev,
144                           struct v4l2_subdev_pad_config *cfg,
145                           struct v4l2_subdev_format *fmt)
146 {
147         struct vsp1_lif *lif = to_lif(subdev);
148         struct v4l2_mbus_framefmt *format;
149
150         /* Default to YUV if the requested format is not supported. */
151         if (fmt->format.code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
152             fmt->format.code != MEDIA_BUS_FMT_AYUV8_1X32)
153                 fmt->format.code = MEDIA_BUS_FMT_AYUV8_1X32;
154
155         format = vsp1_entity_get_pad_format(&lif->entity, cfg, fmt->pad,
156                                             fmt->which);
157
158         if (fmt->pad == LIF_PAD_SOURCE) {
159                 /* The LIF source format is always identical to its sink
160                  * format.
161                  */
162                 fmt->format = *format;
163                 return 0;
164         }
165
166         format->code = fmt->format.code;
167         format->width = clamp_t(unsigned int, fmt->format.width,
168                                 LIF_MIN_SIZE, LIF_MAX_SIZE);
169         format->height = clamp_t(unsigned int, fmt->format.height,
170                                  LIF_MIN_SIZE, LIF_MAX_SIZE);
171         format->field = V4L2_FIELD_NONE;
172         format->colorspace = V4L2_COLORSPACE_SRGB;
173
174         fmt->format = *format;
175
176         /* Propagate the format to the source pad. */
177         format = vsp1_entity_get_pad_format(&lif->entity, cfg, LIF_PAD_SOURCE,
178                                             fmt->which);
179         *format = fmt->format;
180
181         return 0;
182 }
183
184 /* -----------------------------------------------------------------------------
185  * V4L2 Subdevice Operations
186  */
187
188 static struct v4l2_subdev_video_ops lif_video_ops = {
189         .s_stream = lif_s_stream,
190 };
191
192 static struct v4l2_subdev_pad_ops lif_pad_ops = {
193         .init_cfg = vsp1_entity_init_cfg,
194         .enum_mbus_code = lif_enum_mbus_code,
195         .enum_frame_size = lif_enum_frame_size,
196         .get_fmt = lif_get_format,
197         .set_fmt = lif_set_format,
198 };
199
200 static struct v4l2_subdev_ops lif_ops = {
201         .video  = &lif_video_ops,
202         .pad    = &lif_pad_ops,
203 };
204
205 /* -----------------------------------------------------------------------------
206  * Initialization and Cleanup
207  */
208
209 struct vsp1_lif *vsp1_lif_create(struct vsp1_device *vsp1)
210 {
211         struct vsp1_lif *lif;
212         int ret;
213
214         lif = devm_kzalloc(vsp1->dev, sizeof(*lif), GFP_KERNEL);
215         if (lif == NULL)
216                 return ERR_PTR(-ENOMEM);
217
218         lif->entity.type = VSP1_ENTITY_LIF;
219
220         ret = vsp1_entity_init(vsp1, &lif->entity, "lif", 2, &lif_ops);
221         if (ret < 0)
222                 return ERR_PTR(ret);
223
224         return lif;
225 }