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