blob: 7dcdb7f4b3237f52af1a2086fff5e1c66a006036 [file] [log] [blame]
Manjunath Hadli66715cd2011-06-17 04:01:32 -03001/*
2 * Copyright (C) 2010 Texas Instruments Inc
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation version 2.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/errno.h>
21#include <linux/fs.h>
22#include <linux/string.h>
23#include <linux/wait.h>
24#include <linux/time.h>
25#include <linux/platform_device.h>
26#include <linux/io.h>
27#include <linux/slab.h>
28#include <linux/clk.h>
29#include <linux/err.h>
30
31#include <media/v4l2-device.h>
32#include <media/davinci/vpbe_types.h>
33#include <media/davinci/vpbe.h>
34#include <media/davinci/vpss.h>
35#include <media/davinci/vpbe_venc.h>
36
37#define VPBE_DEFAULT_OUTPUT "Composite"
38#define VPBE_DEFAULT_MODE "ntsc"
39
40static char *def_output = VPBE_DEFAULT_OUTPUT;
41static char *def_mode = VPBE_DEFAULT_MODE;
42static int debug;
43
44module_param(def_output, charp, S_IRUGO);
45module_param(def_mode, charp, S_IRUGO);
46module_param(debug, int, 0644);
47
48MODULE_PARM_DESC(def_output, "vpbe output name (default:Composite)");
49MODULE_PARM_DESC(def_mode, "vpbe output mode name (default:ntsc");
50MODULE_PARM_DESC(debug, "Debug level 0-1");
51
52MODULE_DESCRIPTION("TI DMXXX VPBE Display controller");
53MODULE_LICENSE("GPL");
54MODULE_AUTHOR("Texas Instruments");
55
56/**
57 * vpbe_current_encoder_info - Get config info for current encoder
58 * @vpbe_dev - vpbe device ptr
59 *
60 * Return ptr to current encoder config info
61 */
62static struct encoder_config_info*
63vpbe_current_encoder_info(struct vpbe_device *vpbe_dev)
64{
65 struct vpbe_config *cfg = vpbe_dev->cfg;
66 int index = vpbe_dev->current_sd_index;
67
68 return ((index == 0) ? &cfg->venc :
69 &cfg->ext_encoders[index-1]);
70}
71
72/**
73 * vpbe_find_encoder_sd_index - Given a name find encoder sd index
74 *
75 * @vpbe_config - ptr to vpbe cfg
76 * @output_index - index used by application
77 *
78 * Return sd index of the encoder
79 */
80static int vpbe_find_encoder_sd_index(struct vpbe_config *cfg,
81 int index)
82{
83 char *encoder_name = cfg->outputs[index].subdev_name;
84 int i;
85
86 /* Venc is always first */
87 if (!strcmp(encoder_name, cfg->venc.module_name))
88 return 0;
89
90 for (i = 0; i < cfg->num_ext_encoders; i++) {
91 if (!strcmp(encoder_name,
92 cfg->ext_encoders[i].module_name))
93 return i+1;
94 }
95
96 return -EINVAL;
97}
98
99/**
100 * vpbe_g_cropcap - Get crop capabilities of the display
101 * @vpbe_dev - vpbe device ptr
102 * @cropcap - cropcap is a ptr to struct v4l2_cropcap
103 *
104 * Update the crop capabilities in crop cap for current
105 * mode
106 */
107static int vpbe_g_cropcap(struct vpbe_device *vpbe_dev,
108 struct v4l2_cropcap *cropcap)
109{
Markus Elfring13538752016-10-11 08:37:10 -0300110 if (!cropcap)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300111 return -EINVAL;
112 cropcap->bounds.left = 0;
113 cropcap->bounds.top = 0;
114 cropcap->bounds.width = vpbe_dev->current_timings.xres;
115 cropcap->bounds.height = vpbe_dev->current_timings.yres;
116 cropcap->defrect = cropcap->bounds;
117
118 return 0;
119}
120
121/**
122 * vpbe_enum_outputs - enumerate outputs
123 * @vpbe_dev - vpbe device ptr
124 * @output - ptr to v4l2_output structure
125 *
126 * Enumerates the outputs available at the vpbe display
127 * returns the status, -EINVAL if end of output list
128 */
129static int vpbe_enum_outputs(struct vpbe_device *vpbe_dev,
130 struct v4l2_output *output)
131{
132 struct vpbe_config *cfg = vpbe_dev->cfg;
133 int temp_index = output->index;
134
135 if (temp_index >= cfg->num_outputs)
136 return -EINVAL;
137
138 *output = cfg->outputs[temp_index].output;
139 output->index = temp_index;
140
141 return 0;
142}
143
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300144static int vpbe_get_mode_info(struct vpbe_device *vpbe_dev, char *mode,
145 int output_index)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300146{
147 struct vpbe_config *cfg = vpbe_dev->cfg;
148 struct vpbe_enc_mode_info var;
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300149 int curr_output = output_index;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300150 int i;
151
Markus Elfring13538752016-10-11 08:37:10 -0300152 if (!mode)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300153 return -EINVAL;
154
155 for (i = 0; i < cfg->outputs[curr_output].num_modes; i++) {
156 var = cfg->outputs[curr_output].modes[i];
157 if (!strcmp(mode, var.name)) {
158 vpbe_dev->current_timings = var;
159 return 0;
160 }
161 }
162
163 return -EINVAL;
164}
165
166static int vpbe_get_current_mode_info(struct vpbe_device *vpbe_dev,
167 struct vpbe_enc_mode_info *mode_info)
168{
Markus Elfring13538752016-10-11 08:37:10 -0300169 if (!mode_info)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300170 return -EINVAL;
171
172 *mode_info = vpbe_dev->current_timings;
173
174 return 0;
175}
176
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300177/* Get std by std id */
178static int vpbe_get_std_info(struct vpbe_device *vpbe_dev,
179 v4l2_std_id std_id)
180{
181 struct vpbe_config *cfg = vpbe_dev->cfg;
182 struct vpbe_enc_mode_info var;
183 int curr_output = vpbe_dev->current_out_index;
184 int i;
185
186 for (i = 0; i < vpbe_dev->cfg->outputs[curr_output].num_modes; i++) {
187 var = cfg->outputs[curr_output].modes[i];
188 if ((var.timings_type & VPBE_ENC_STD) &&
Hans Verkuil36864082012-10-01 11:39:46 -0300189 (var.std_id & std_id)) {
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300190 vpbe_dev->current_timings = var;
191 return 0;
192 }
193 }
194
195 return -EINVAL;
196}
197
198static int vpbe_get_std_info_by_name(struct vpbe_device *vpbe_dev,
199 char *std_name)
200{
201 struct vpbe_config *cfg = vpbe_dev->cfg;
202 struct vpbe_enc_mode_info var;
203 int curr_output = vpbe_dev->current_out_index;
204 int i;
205
206 for (i = 0; i < vpbe_dev->cfg->outputs[curr_output].num_modes; i++) {
207 var = cfg->outputs[curr_output].modes[i];
208 if (!strcmp(var.name, std_name)) {
209 vpbe_dev->current_timings = var;
210 return 0;
211 }
212 }
213
214 return -EINVAL;
215}
216
217/**
218 * vpbe_set_output - Set output
219 * @vpbe_dev - vpbe device ptr
220 * @index - index of output
221 *
222 * Set vpbe output to the output specified by the index
223 */
224static int vpbe_set_output(struct vpbe_device *vpbe_dev, int index)
225{
226 struct encoder_config_info *curr_enc_info =
227 vpbe_current_encoder_info(vpbe_dev);
228 struct vpbe_config *cfg = vpbe_dev->cfg;
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300229 struct venc_platform_data *venc_device = vpbe_dev->venc_device;
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300230 u32 if_params;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300231 int enc_out_index;
232 int sd_index;
Markus Elfring630bf792016-10-12 05:16:23 -0300233 int ret;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300234
235 if (index >= cfg->num_outputs)
236 return -EINVAL;
237
238 mutex_lock(&vpbe_dev->lock);
239
240 sd_index = vpbe_dev->current_sd_index;
241 enc_out_index = cfg->outputs[index].output.index;
242 /*
243 * Currently we switch the encoder based on output selected
244 * by the application. If media controller is implemented later
245 * there is will be an API added to setup_link between venc
246 * and external encoder. So in that case below comparison always
247 * match and encoder will not be switched. But if application
248 * chose not to use media controller, then this provides current
249 * way of switching encoder at the venc output.
250 */
251 if (strcmp(curr_enc_info->module_name,
252 cfg->outputs[index].subdev_name)) {
253 /* Need to switch the encoder at the output */
254 sd_index = vpbe_find_encoder_sd_index(cfg, index);
255 if (sd_index < 0) {
256 ret = -EINVAL;
Markus Elfring15a78312016-10-12 05:10:19 -0300257 goto unlock;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300258 }
259
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300260 if_params = cfg->outputs[index].if_params;
261 venc_device->setup_if_config(if_params);
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300262 if (ret)
Markus Elfring15a78312016-10-12 05:10:19 -0300263 goto unlock;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300264 }
265
266 /* Set output at the encoder */
267 ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
268 s_routing, 0, enc_out_index, 0);
269 if (ret)
Markus Elfring15a78312016-10-12 05:10:19 -0300270 goto unlock;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300271
272 /*
273 * It is assumed that venc or extenal encoder will set a default
274 * mode in the sub device. For external encoder or LCD pannel output,
275 * we also need to set up the lcd port for the required mode. So setup
276 * the lcd port for the default mode that is configured in the board
277 * arch/arm/mach-davinci/board-dm355-evm.setup file for the external
278 * encoder.
279 */
280 ret = vpbe_get_mode_info(vpbe_dev,
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300281 cfg->outputs[index].default_mode, index);
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300282 if (!ret) {
283 struct osd_state *osd_device = vpbe_dev->osd_device;
284
285 osd_device->ops.set_left_margin(osd_device,
286 vpbe_dev->current_timings.left_margin);
287 osd_device->ops.set_top_margin(osd_device,
288 vpbe_dev->current_timings.upper_margin);
289 vpbe_dev->current_sd_index = sd_index;
290 vpbe_dev->current_out_index = index;
291 }
Markus Elfring15a78312016-10-12 05:10:19 -0300292unlock:
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300293 mutex_unlock(&vpbe_dev->lock);
294 return ret;
295}
296
297static int vpbe_set_default_output(struct vpbe_device *vpbe_dev)
298{
299 struct vpbe_config *cfg = vpbe_dev->cfg;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300300 int i;
301
302 for (i = 0; i < cfg->num_outputs; i++) {
303 if (!strcmp(def_output,
304 cfg->outputs[i].output.name)) {
Markus Elfring837fdcf2016-10-12 04:54:26 -0300305 int ret = vpbe_set_output(vpbe_dev, i);
306
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300307 if (!ret)
308 vpbe_dev->current_out_index = i;
309 return ret;
310 }
311 }
Markus Elfring837fdcf2016-10-12 04:54:26 -0300312 return 0;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300313}
314
315/**
316 * vpbe_get_output - Get output
317 * @vpbe_dev - vpbe device ptr
318 *
319 * return current vpbe output to the the index
320 */
321static unsigned int vpbe_get_output(struct vpbe_device *vpbe_dev)
322{
323 return vpbe_dev->current_out_index;
324}
325
326/**
Hans Verkuil36864082012-10-01 11:39:46 -0300327 * vpbe_s_dv_timings - Set the given preset timings in the encoder
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300328 *
Hans Verkuil36864082012-10-01 11:39:46 -0300329 * Sets the timings if supported by the current encoder. Return the status.
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300330 * 0 - success & -EINVAL on error
331 */
Hans Verkuil36864082012-10-01 11:39:46 -0300332static int vpbe_s_dv_timings(struct vpbe_device *vpbe_dev,
333 struct v4l2_dv_timings *dv_timings)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300334{
335 struct vpbe_config *cfg = vpbe_dev->cfg;
336 int out_index = vpbe_dev->current_out_index;
Hans Verkuil36864082012-10-01 11:39:46 -0300337 struct vpbe_output *output = &cfg->outputs[out_index];
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300338 int sd_index = vpbe_dev->current_sd_index;
Hans Verkuil36864082012-10-01 11:39:46 -0300339 int ret, i;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300340
341
342 if (!(cfg->outputs[out_index].output.capabilities &
Lad, Prabhakare32087b2012-10-03 02:25:42 -0300343 V4L2_OUT_CAP_DV_TIMINGS))
Prabhakar Ladf9cc70b2014-10-12 17:40:45 -0300344 return -ENODATA;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300345
Hans Verkuil36864082012-10-01 11:39:46 -0300346 for (i = 0; i < output->num_modes; i++) {
Hans Verkuilef2d41b2013-02-15 15:06:28 -0300347 if (output->modes[i].timings_type == VPBE_ENC_DV_TIMINGS &&
Hans Verkuil36864082012-10-01 11:39:46 -0300348 !memcmp(&output->modes[i].dv_timings,
349 dv_timings, sizeof(*dv_timings)))
350 break;
351 }
352 if (i >= output->num_modes)
353 return -EINVAL;
354 vpbe_dev->current_timings = output->modes[i];
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300355 mutex_lock(&vpbe_dev->lock);
356
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300357 ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
Hans Verkuil36864082012-10-01 11:39:46 -0300358 s_dv_timings, dv_timings);
Markus Elfring13538752016-10-11 08:37:10 -0300359 if (!ret && vpbe_dev->amp) {
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300360 /* Call amplifier subdevice */
361 ret = v4l2_subdev_call(vpbe_dev->amp, video,
Hans Verkuil36864082012-10-01 11:39:46 -0300362 s_dv_timings, dv_timings);
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300363 }
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300364 /* set the lcd controller output for the given mode */
365 if (!ret) {
366 struct osd_state *osd_device = vpbe_dev->osd_device;
367
368 osd_device->ops.set_left_margin(osd_device,
369 vpbe_dev->current_timings.left_margin);
370 osd_device->ops.set_top_margin(osd_device,
371 vpbe_dev->current_timings.upper_margin);
372 }
373 mutex_unlock(&vpbe_dev->lock);
374
375 return ret;
376}
377
378/**
Hans Verkuil36864082012-10-01 11:39:46 -0300379 * vpbe_g_dv_timings - Get the timings in the current encoder
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300380 *
Hans Verkuil36864082012-10-01 11:39:46 -0300381 * Get the timings in the current encoder. Return the status. 0 - success
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300382 * -EINVAL on error
383 */
Hans Verkuil36864082012-10-01 11:39:46 -0300384static int vpbe_g_dv_timings(struct vpbe_device *vpbe_dev,
385 struct v4l2_dv_timings *dv_timings)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300386{
Prabhakar Ladf9cc70b2014-10-12 17:40:45 -0300387 struct vpbe_config *cfg = vpbe_dev->cfg;
388 int out_index = vpbe_dev->current_out_index;
389
390 if (!(cfg->outputs[out_index].output.capabilities &
391 V4L2_OUT_CAP_DV_TIMINGS))
392 return -ENODATA;
393
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300394 if (vpbe_dev->current_timings.timings_type &
Hans Verkuilef2d41b2013-02-15 15:06:28 -0300395 VPBE_ENC_DV_TIMINGS) {
Hans Verkuil36864082012-10-01 11:39:46 -0300396 *dv_timings = vpbe_dev->current_timings.dv_timings;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300397 return 0;
398 }
399
400 return -EINVAL;
401}
402
403/**
Hans Verkuil36864082012-10-01 11:39:46 -0300404 * vpbe_enum_dv_timings - Enumerate the dv timings in the current encoder
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300405 *
Hans Verkuil36864082012-10-01 11:39:46 -0300406 * Get the timings in the current encoder. Return the status. 0 - success
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300407 * -EINVAL on error
408 */
Hans Verkuil36864082012-10-01 11:39:46 -0300409static int vpbe_enum_dv_timings(struct vpbe_device *vpbe_dev,
410 struct v4l2_enum_dv_timings *timings)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300411{
412 struct vpbe_config *cfg = vpbe_dev->cfg;
413 int out_index = vpbe_dev->current_out_index;
414 struct vpbe_output *output = &cfg->outputs[out_index];
415 int j = 0;
416 int i;
417
Lad, Prabhakare32087b2012-10-03 02:25:42 -0300418 if (!(output->output.capabilities & V4L2_OUT_CAP_DV_TIMINGS))
Prabhakar Ladf9cc70b2014-10-12 17:40:45 -0300419 return -ENODATA;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300420
421 for (i = 0; i < output->num_modes; i++) {
Hans Verkuilef2d41b2013-02-15 15:06:28 -0300422 if (output->modes[i].timings_type == VPBE_ENC_DV_TIMINGS) {
Hans Verkuil36864082012-10-01 11:39:46 -0300423 if (j == timings->index)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300424 break;
425 j++;
426 }
427 }
428
429 if (i == output->num_modes)
430 return -EINVAL;
Hans Verkuil36864082012-10-01 11:39:46 -0300431 timings->timings = output->modes[i].dv_timings;
432 return 0;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300433}
434
435/**
436 * vpbe_s_std - Set the given standard in the encoder
437 *
438 * Sets the standard if supported by the current encoder. Return the status.
439 * 0 - success & -EINVAL on error
440 */
Hans Verkuil314527a2013-03-15 06:10:40 -0300441static int vpbe_s_std(struct vpbe_device *vpbe_dev, v4l2_std_id std_id)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300442{
443 struct vpbe_config *cfg = vpbe_dev->cfg;
444 int out_index = vpbe_dev->current_out_index;
445 int sd_index = vpbe_dev->current_sd_index;
446 int ret;
447
448 if (!(cfg->outputs[out_index].output.capabilities &
449 V4L2_OUT_CAP_STD))
Prabhakar Ladf9cc70b2014-10-12 17:40:45 -0300450 return -ENODATA;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300451
Hans Verkuil314527a2013-03-15 06:10:40 -0300452 ret = vpbe_get_std_info(vpbe_dev, std_id);
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300453 if (ret)
454 return ret;
455
456 mutex_lock(&vpbe_dev->lock);
457
458 ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
Hans Verkuil314527a2013-03-15 06:10:40 -0300459 s_std_output, std_id);
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300460 /* set the lcd controller output for the given mode */
461 if (!ret) {
462 struct osd_state *osd_device = vpbe_dev->osd_device;
463
464 osd_device->ops.set_left_margin(osd_device,
465 vpbe_dev->current_timings.left_margin);
466 osd_device->ops.set_top_margin(osd_device,
467 vpbe_dev->current_timings.upper_margin);
468 }
469 mutex_unlock(&vpbe_dev->lock);
470
471 return ret;
472}
473
474/**
475 * vpbe_g_std - Get the standard in the current encoder
476 *
477 * Get the standard in the current encoder. Return the status. 0 - success
478 * -EINVAL on error
479 */
480static int vpbe_g_std(struct vpbe_device *vpbe_dev, v4l2_std_id *std_id)
481{
Hans Verkuil36864082012-10-01 11:39:46 -0300482 struct vpbe_enc_mode_info *cur_timings = &vpbe_dev->current_timings;
Prabhakar Ladf9cc70b2014-10-12 17:40:45 -0300483 struct vpbe_config *cfg = vpbe_dev->cfg;
484 int out_index = vpbe_dev->current_out_index;
485
486 if (!(cfg->outputs[out_index].output.capabilities & V4L2_OUT_CAP_STD))
487 return -ENODATA;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300488
Hans Verkuil36864082012-10-01 11:39:46 -0300489 if (cur_timings->timings_type & VPBE_ENC_STD) {
490 *std_id = cur_timings->std_id;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300491 return 0;
492 }
493
494 return -EINVAL;
495}
496
497/**
498 * vpbe_set_mode - Set mode in the current encoder using mode info
499 *
500 * Use the mode string to decide what timings to set in the encoder
501 * This is typically useful when fbset command is used to change the current
502 * timings by specifying a string to indicate the timings.
503 */
504static int vpbe_set_mode(struct vpbe_device *vpbe_dev,
505 struct vpbe_enc_mode_info *mode_info)
506{
507 struct vpbe_enc_mode_info *preset_mode = NULL;
508 struct vpbe_config *cfg = vpbe_dev->cfg;
Hans Verkuil36864082012-10-01 11:39:46 -0300509 struct v4l2_dv_timings dv_timings;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300510 struct osd_state *osd_device;
511 int out_index = vpbe_dev->current_out_index;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300512 int i;
513
Markus Elfring13538752016-10-11 08:37:10 -0300514 if (!mode_info || !mode_info->name)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300515 return -EINVAL;
516
517 for (i = 0; i < cfg->outputs[out_index].num_modes; i++) {
518 if (!strcmp(mode_info->name,
519 cfg->outputs[out_index].modes[i].name)) {
520 preset_mode = &cfg->outputs[out_index].modes[i];
521 /*
522 * it may be one of the 3 timings type. Check and
523 * invoke right API
524 */
525 if (preset_mode->timings_type & VPBE_ENC_STD)
526 return vpbe_s_std(vpbe_dev,
Hans Verkuil314527a2013-03-15 06:10:40 -0300527 preset_mode->std_id);
Hans Verkuil36864082012-10-01 11:39:46 -0300528 if (preset_mode->timings_type &
Hans Verkuilef2d41b2013-02-15 15:06:28 -0300529 VPBE_ENC_DV_TIMINGS) {
Hans Verkuil36864082012-10-01 11:39:46 -0300530 dv_timings =
531 preset_mode->dv_timings;
532 return vpbe_s_dv_timings(vpbe_dev, &dv_timings);
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300533 }
534 }
535 }
536
537 /* Only custom timing should reach here */
Markus Elfring13538752016-10-11 08:37:10 -0300538 if (!preset_mode)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300539 return -EINVAL;
540
541 mutex_lock(&vpbe_dev->lock);
542
543 osd_device = vpbe_dev->osd_device;
544 vpbe_dev->current_timings = *preset_mode;
545 osd_device->ops.set_left_margin(osd_device,
546 vpbe_dev->current_timings.left_margin);
547 osd_device->ops.set_top_margin(osd_device,
548 vpbe_dev->current_timings.upper_margin);
549
550 mutex_unlock(&vpbe_dev->lock);
Markus Elfringca7948aa2016-10-12 04:51:29 -0300551 return 0;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300552}
553
554static int vpbe_set_default_mode(struct vpbe_device *vpbe_dev)
555{
556 int ret;
557
558 ret = vpbe_get_std_info_by_name(vpbe_dev, def_mode);
559 if (ret)
560 return ret;
561
562 /* set the default mode in the encoder */
563 return vpbe_set_mode(vpbe_dev, &vpbe_dev->current_timings);
564}
565
566static int platform_device_get(struct device *dev, void *data)
567{
568 struct platform_device *pdev = to_platform_device(dev);
569 struct vpbe_device *vpbe_dev = data;
570
Markus Elfring13538752016-10-11 08:37:10 -0300571 if (strstr(pdev->name, "vpbe-osd"))
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300572 vpbe_dev->osd_device = platform_get_drvdata(pdev);
Markus Elfring13538752016-10-11 08:37:10 -0300573 if (strstr(pdev->name, "vpbe-venc"))
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300574 vpbe_dev->venc_device = dev_get_platdata(&pdev->dev);
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300575
576 return 0;
577}
578
579/**
580 * vpbe_initialize() - Initialize the vpbe display controller
581 * @vpbe_dev - vpbe device ptr
582 *
583 * Master frame buffer device drivers calls this to initialize vpbe
584 * display controller. This will then registers v4l2 device and the sub
585 * devices and sets a current encoder sub device for display. v4l2 display
586 * device driver is the master and frame buffer display device driver is
587 * the slave. Frame buffer display driver checks the initialized during
588 * probe and exit if not initialized. Returns status.
589 */
590static int vpbe_initialize(struct device *dev, struct vpbe_device *vpbe_dev)
591{
592 struct encoder_config_info *enc_info;
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300593 struct amp_config_info *amp_info;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300594 struct v4l2_subdev **enc_subdev;
595 struct osd_state *osd_device;
596 struct i2c_adapter *i2c_adap;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300597 int num_encoders;
598 int ret = 0;
599 int err;
600 int i;
601
602 /*
603 * v4l2 abd FBDev frame buffer devices will get the vpbe_dev pointer
604 * from the platform device by iteration of platform drivers and
605 * matching with device name
606 */
Markus Elfring13538752016-10-11 08:37:10 -0300607 if (!vpbe_dev || !dev) {
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300608 printk(KERN_ERR "Null device pointers.\n");
609 return -ENODEV;
610 }
611
612 if (vpbe_dev->initialized)
613 return 0;
614
615 mutex_lock(&vpbe_dev->lock);
616
617 if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
618 /* We have dac clock available for platform */
619 vpbe_dev->dac_clk = clk_get(vpbe_dev->pdev, "vpss_dac");
620 if (IS_ERR(vpbe_dev->dac_clk)) {
621 ret = PTR_ERR(vpbe_dev->dac_clk);
Peter Senna Tschudinac0fe5b2012-09-14 12:46:52 -0300622 goto fail_mutex_unlock;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300623 }
Murali Karicheri1f5a5e62012-10-22 11:41:36 -0300624 if (clk_prepare_enable(vpbe_dev->dac_clk)) {
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300625 ret = -ENODEV;
Sudip Mukherjee47efeb52014-11-06 10:04:27 -0300626 clk_put(vpbe_dev->dac_clk);
Peter Senna Tschudinac0fe5b2012-09-14 12:46:52 -0300627 goto fail_mutex_unlock;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300628 }
629 }
630
631 /* first enable vpss clocks */
632 vpss_enable_clock(VPSS_VPBE_CLOCK, 1);
633
634 /* First register a v4l2 device */
635 ret = v4l2_device_register(dev, &vpbe_dev->v4l2_dev);
636 if (ret) {
637 v4l2_err(dev->driver,
638 "Unable to register v4l2 device.\n");
Peter Senna Tschudinac0fe5b2012-09-14 12:46:52 -0300639 goto fail_clk_put;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300640 }
641 v4l2_info(&vpbe_dev->v4l2_dev, "vpbe v4l2 device registered\n");
642
643 err = bus_for_each_dev(&platform_bus_type, NULL, vpbe_dev,
644 platform_device_get);
Wei Yongjun5d97046a32012-10-22 01:36:13 -0300645 if (err < 0) {
646 ret = err;
647 goto fail_dev_unregister;
648 }
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300649
650 vpbe_dev->venc = venc_sub_dev_init(&vpbe_dev->v4l2_dev,
651 vpbe_dev->cfg->venc.module_name);
652 /* register venc sub device */
Markus Elfring13538752016-10-11 08:37:10 -0300653 if (!vpbe_dev->venc) {
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300654 v4l2_err(&vpbe_dev->v4l2_dev,
655 "vpbe unable to init venc sub device\n");
656 ret = -ENODEV;
Peter Senna Tschudinac0fe5b2012-09-14 12:46:52 -0300657 goto fail_dev_unregister;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300658 }
659 /* initialize osd device */
660 osd_device = vpbe_dev->osd_device;
Markus Elfring13538752016-10-11 08:37:10 -0300661 if (osd_device->ops.initialize) {
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300662 err = osd_device->ops.initialize(osd_device);
663 if (err) {
664 v4l2_err(&vpbe_dev->v4l2_dev,
665 "unable to initialize the OSD device");
666 err = -ENOMEM;
Peter Senna Tschudinac0fe5b2012-09-14 12:46:52 -0300667 goto fail_dev_unregister;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300668 }
669 }
670
671 /*
672 * Register any external encoders that are configured. At index 0 we
673 * store venc sd index.
674 */
675 num_encoders = vpbe_dev->cfg->num_ext_encoders + 1;
Markus Elfringf42afd22016-10-11 04:40:41 -0300676 vpbe_dev->encoders = kmalloc_array(num_encoders,
677 sizeof(*vpbe_dev->encoders),
678 GFP_KERNEL);
Markus Elfring13538752016-10-11 08:37:10 -0300679 if (!vpbe_dev->encoders) {
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300680 ret = -ENOMEM;
Peter Senna Tschudinac0fe5b2012-09-14 12:46:52 -0300681 goto fail_dev_unregister;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300682 }
683
684 i2c_adap = i2c_get_adapter(vpbe_dev->cfg->i2c_adapter_id);
685 for (i = 0; i < (vpbe_dev->cfg->num_ext_encoders + 1); i++) {
686 if (i == 0) {
687 /* venc is at index 0 */
688 enc_subdev = &vpbe_dev->encoders[i];
689 *enc_subdev = vpbe_dev->venc;
690 continue;
691 }
692 enc_info = &vpbe_dev->cfg->ext_encoders[i];
693 if (enc_info->is_i2c) {
694 enc_subdev = &vpbe_dev->encoders[i];
695 *enc_subdev = v4l2_i2c_new_subdev_board(
696 &vpbe_dev->v4l2_dev, i2c_adap,
697 &enc_info->board_info, NULL);
698 if (*enc_subdev)
699 v4l2_info(&vpbe_dev->v4l2_dev,
700 "v4l2 sub device %s registered\n",
701 enc_info->module_name);
702 else {
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -0200703 v4l2_err(&vpbe_dev->v4l2_dev, "encoder %s failed to register",
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300704 enc_info->module_name);
705 ret = -ENODEV;
Peter Senna Tschudinac0fe5b2012-09-14 12:46:52 -0300706 goto fail_kfree_encoders;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300707 }
708 } else
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -0200709 v4l2_warn(&vpbe_dev->v4l2_dev, "non-i2c encoders currently not supported");
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300710 }
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300711 /* Add amplifier subdevice for dm365 */
712 if ((strcmp(vpbe_dev->cfg->module_name, "dm365-vpbe-display") == 0) &&
Markus Elfring13538752016-10-11 08:37:10 -0300713 vpbe_dev->cfg->amp) {
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300714 amp_info = vpbe_dev->cfg->amp;
715 if (amp_info->is_i2c) {
716 vpbe_dev->amp = v4l2_i2c_new_subdev_board(
717 &vpbe_dev->v4l2_dev, i2c_adap,
718 &amp_info->board_info, NULL);
719 if (!vpbe_dev->amp) {
720 v4l2_err(&vpbe_dev->v4l2_dev,
721 "amplifier %s failed to register",
722 amp_info->module_name);
723 ret = -ENODEV;
Peter Senna Tschudinac0fe5b2012-09-14 12:46:52 -0300724 goto fail_kfree_encoders;
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300725 }
726 v4l2_info(&vpbe_dev->v4l2_dev,
727 "v4l2 sub device %s registered\n",
728 amp_info->module_name);
729 } else {
730 vpbe_dev->amp = NULL;
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -0200731 v4l2_warn(&vpbe_dev->v4l2_dev, "non-i2c amplifiers currently not supported");
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300732 }
733 } else {
734 vpbe_dev->amp = NULL;
735 }
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300736
737 /* set the current encoder and output to that of venc by default */
738 vpbe_dev->current_sd_index = 0;
739 vpbe_dev->current_out_index = 0;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300740
741 mutex_unlock(&vpbe_dev->lock);
742
743 printk(KERN_NOTICE "Setting default output to %s\n", def_output);
744 ret = vpbe_set_default_output(vpbe_dev);
745 if (ret) {
746 v4l2_err(&vpbe_dev->v4l2_dev, "Failed to set default output %s",
747 def_output);
748 return ret;
749 }
750
751 printk(KERN_NOTICE "Setting default mode to %s\n", def_mode);
752 ret = vpbe_set_default_mode(vpbe_dev);
753 if (ret) {
754 v4l2_err(&vpbe_dev->v4l2_dev, "Failed to set default mode %s",
755 def_mode);
756 return ret;
757 }
758 vpbe_dev->initialized = 1;
759 /* TBD handling of bootargs for default output and mode */
760 return 0;
761
Peter Senna Tschudinac0fe5b2012-09-14 12:46:52 -0300762fail_kfree_encoders:
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300763 kfree(vpbe_dev->encoders);
Peter Senna Tschudinac0fe5b2012-09-14 12:46:52 -0300764fail_dev_unregister:
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300765 v4l2_device_unregister(&vpbe_dev->v4l2_dev);
Peter Senna Tschudinac0fe5b2012-09-14 12:46:52 -0300766fail_clk_put:
Murali Karicheri1f5a5e62012-10-22 11:41:36 -0300767 if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
768 clk_disable_unprepare(vpbe_dev->dac_clk);
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300769 clk_put(vpbe_dev->dac_clk);
Murali Karicheri1f5a5e62012-10-22 11:41:36 -0300770 }
Peter Senna Tschudinac0fe5b2012-09-14 12:46:52 -0300771fail_mutex_unlock:
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300772 mutex_unlock(&vpbe_dev->lock);
773 return ret;
774}
775
776/**
777 * vpbe_deinitialize() - de-initialize the vpbe display controller
778 * @dev - Master and slave device ptr
779 *
780 * vpbe_master and slave frame buffer devices calls this to de-initialize
781 * the display controller. It is called when master and slave device
782 * driver modules are removed and no longer requires the display controller.
783 */
784static void vpbe_deinitialize(struct device *dev, struct vpbe_device *vpbe_dev)
785{
786 v4l2_device_unregister(&vpbe_dev->v4l2_dev);
Murali Karicheri1f5a5e62012-10-22 11:41:36 -0300787 if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
788 clk_disable_unprepare(vpbe_dev->dac_clk);
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300789 clk_put(vpbe_dev->dac_clk);
Murali Karicheri1f5a5e62012-10-22 11:41:36 -0300790 }
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300791
Manjunath Hadli9a7f95a2011-04-30 03:01:40 -0300792 kfree(vpbe_dev->amp);
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300793 kfree(vpbe_dev->encoders);
794 vpbe_dev->initialized = 0;
795 /* disable vpss clocks */
796 vpss_enable_clock(VPSS_VPBE_CLOCK, 0);
797}
798
799static struct vpbe_device_ops vpbe_dev_ops = {
800 .g_cropcap = vpbe_g_cropcap,
801 .enum_outputs = vpbe_enum_outputs,
802 .set_output = vpbe_set_output,
803 .get_output = vpbe_get_output,
Hans Verkuil36864082012-10-01 11:39:46 -0300804 .s_dv_timings = vpbe_s_dv_timings,
805 .g_dv_timings = vpbe_g_dv_timings,
806 .enum_dv_timings = vpbe_enum_dv_timings,
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300807 .s_std = vpbe_s_std,
808 .g_std = vpbe_g_std,
809 .initialize = vpbe_initialize,
810 .deinitialize = vpbe_deinitialize,
811 .get_mode_info = vpbe_get_current_mode_info,
812 .set_mode = vpbe_set_mode,
813};
814
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800815static int vpbe_probe(struct platform_device *pdev)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300816{
817 struct vpbe_device *vpbe_dev;
818 struct vpbe_config *cfg;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300819
Markus Elfring13538752016-10-11 08:37:10 -0300820 if (!pdev->dev.platform_data) {
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300821 v4l2_err(pdev->dev.driver, "No platform data\n");
822 return -ENODEV;
823 }
824 cfg = pdev->dev.platform_data;
825
826 if (!cfg->module_name[0] ||
827 !cfg->osd.module_name[0] ||
828 !cfg->venc.module_name[0]) {
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -0200829 v4l2_err(pdev->dev.driver, "vpbe display module names not defined\n");
Markus Elfring9d2fe9a2016-10-11 08:43:25 -0300830 return -EINVAL;
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300831 }
832
833 vpbe_dev = kzalloc(sizeof(*vpbe_dev), GFP_KERNEL);
Markus Elfring2ac09892016-10-11 04:56:13 -0300834 if (!vpbe_dev)
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300835 return -ENOMEM;
Markus Elfring2ac09892016-10-11 04:56:13 -0300836
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300837 vpbe_dev->cfg = cfg;
838 vpbe_dev->ops = vpbe_dev_ops;
839 vpbe_dev->pdev = &pdev->dev;
840
841 if (cfg->outputs->num_modes > 0)
842 vpbe_dev->current_timings = vpbe_dev->cfg->outputs[0].modes[0];
Julia Lawall75e5ac72011-12-23 13:39:32 -0300843 else {
844 kfree(vpbe_dev);
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300845 return -ENODEV;
Julia Lawall75e5ac72011-12-23 13:39:32 -0300846 }
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300847
848 /* set the driver data in platform device */
849 platform_set_drvdata(pdev, vpbe_dev);
850 mutex_init(&vpbe_dev->lock);
851
852 return 0;
853}
854
855static int vpbe_remove(struct platform_device *device)
856{
857 struct vpbe_device *vpbe_dev = platform_get_drvdata(device);
858
859 kfree(vpbe_dev);
860
861 return 0;
862}
863
864static struct platform_driver vpbe_driver = {
865 .driver = {
866 .name = "vpbe_controller",
Manjunath Hadli66715cd2011-06-17 04:01:32 -0300867 },
868 .probe = vpbe_probe,
869 .remove = vpbe_remove,
870};
871
Axel Lin1d6629b2012-01-10 03:21:49 -0300872module_platform_driver(vpbe_driver);