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