Linux-libre 5.4.48-gnu
[librecmc/linux-libre.git] / drivers / media / platform / davinci / vpbe.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2010 Texas Instruments Inc
4  */
5 #include <linux/kernel.h>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/errno.h>
9 #include <linux/fs.h>
10 #include <linux/string.h>
11 #include <linux/wait.h>
12 #include <linux/time.h>
13 #include <linux/platform_device.h>
14 #include <linux/io.h>
15 #include <linux/slab.h>
16 #include <linux/clk.h>
17 #include <linux/err.h>
18
19 #include <media/v4l2-device.h>
20 #include <media/davinci/vpbe_types.h>
21 #include <media/davinci/vpbe.h>
22 #include <media/davinci/vpss.h>
23 #include <media/davinci/vpbe_venc.h>
24
25 #define VPBE_DEFAULT_OUTPUT     "Composite"
26 #define VPBE_DEFAULT_MODE       "ntsc"
27
28 static char *def_output = VPBE_DEFAULT_OUTPUT;
29 static char *def_mode = VPBE_DEFAULT_MODE;
30 static int debug;
31
32 module_param(def_output, charp, S_IRUGO);
33 module_param(def_mode, charp, S_IRUGO);
34 module_param(debug, int, 0644);
35
36 MODULE_PARM_DESC(def_output, "vpbe output name (default:Composite)");
37 MODULE_PARM_DESC(def_mode, "vpbe output mode name (default:ntsc");
38 MODULE_PARM_DESC(debug, "Debug level 0-1");
39
40 MODULE_DESCRIPTION("TI DMXXX VPBE Display controller");
41 MODULE_LICENSE("GPL");
42 MODULE_AUTHOR("Texas Instruments");
43
44 /**
45  * vpbe_current_encoder_info - Get config info for current encoder
46  * @vpbe_dev: vpbe device ptr
47  *
48  * Return ptr to current encoder config info
49  */
50 static struct encoder_config_info*
51 vpbe_current_encoder_info(struct vpbe_device *vpbe_dev)
52 {
53         struct vpbe_config *cfg = vpbe_dev->cfg;
54         int index = vpbe_dev->current_sd_index;
55
56         return ((index == 0) ? &cfg->venc :
57                                 &cfg->ext_encoders[index-1]);
58 }
59
60 /**
61  * vpbe_find_encoder_sd_index - Given a name find encoder sd index
62  *
63  * @cfg: ptr to vpbe cfg
64  * @index: index used by application
65  *
66  * Return sd index of the encoder
67  */
68 static int vpbe_find_encoder_sd_index(struct vpbe_config *cfg,
69                              int index)
70 {
71         char *encoder_name = cfg->outputs[index].subdev_name;
72         int i;
73
74         /* Venc is always first */
75         if (!strcmp(encoder_name, cfg->venc.module_name))
76                 return 0;
77
78         for (i = 0; i < cfg->num_ext_encoders; i++) {
79                 if (!strcmp(encoder_name,
80                      cfg->ext_encoders[i].module_name))
81                         return i+1;
82         }
83
84         return -EINVAL;
85 }
86
87 /**
88  * vpbe_enum_outputs - enumerate outputs
89  * @vpbe_dev: vpbe device ptr
90  * @output: ptr to v4l2_output structure
91  *
92  * Enumerates the outputs available at the vpbe display
93  * returns the status, -EINVAL if end of output list
94  */
95 static int vpbe_enum_outputs(struct vpbe_device *vpbe_dev,
96                              struct v4l2_output *output)
97 {
98         struct vpbe_config *cfg = vpbe_dev->cfg;
99         unsigned int temp_index = output->index;
100
101         if (temp_index >= cfg->num_outputs)
102                 return -EINVAL;
103
104         *output = cfg->outputs[temp_index].output;
105         output->index = temp_index;
106
107         return 0;
108 }
109
110 static int vpbe_get_mode_info(struct vpbe_device *vpbe_dev, char *mode,
111                               int output_index)
112 {
113         struct vpbe_config *cfg = vpbe_dev->cfg;
114         struct vpbe_enc_mode_info var;
115         int curr_output = output_index;
116         int i;
117
118         if (!mode)
119                 return -EINVAL;
120
121         for (i = 0; i < cfg->outputs[curr_output].num_modes; i++) {
122                 var = cfg->outputs[curr_output].modes[i];
123                 if (!strcmp(mode, var.name)) {
124                         vpbe_dev->current_timings = var;
125                         return 0;
126                 }
127         }
128
129         return -EINVAL;
130 }
131
132 static int vpbe_get_current_mode_info(struct vpbe_device *vpbe_dev,
133                                       struct vpbe_enc_mode_info *mode_info)
134 {
135         if (!mode_info)
136                 return -EINVAL;
137
138         *mode_info = vpbe_dev->current_timings;
139
140         return 0;
141 }
142
143 /* Get std by std id */
144 static int vpbe_get_std_info(struct vpbe_device *vpbe_dev,
145                              v4l2_std_id std_id)
146 {
147         struct vpbe_config *cfg = vpbe_dev->cfg;
148         struct vpbe_enc_mode_info var;
149         int curr_output = vpbe_dev->current_out_index;
150         int i;
151
152         for (i = 0; i < vpbe_dev->cfg->outputs[curr_output].num_modes; i++) {
153                 var = cfg->outputs[curr_output].modes[i];
154                 if ((var.timings_type & VPBE_ENC_STD) &&
155                   (var.std_id & std_id)) {
156                         vpbe_dev->current_timings = var;
157                         return 0;
158                 }
159         }
160
161         return -EINVAL;
162 }
163
164 static int vpbe_get_std_info_by_name(struct vpbe_device *vpbe_dev,
165                                 char *std_name)
166 {
167         struct vpbe_config *cfg = vpbe_dev->cfg;
168         struct vpbe_enc_mode_info var;
169         int curr_output = vpbe_dev->current_out_index;
170         int i;
171
172         for (i = 0; i < vpbe_dev->cfg->outputs[curr_output].num_modes; i++) {
173                 var = cfg->outputs[curr_output].modes[i];
174                 if (!strcmp(var.name, std_name)) {
175                         vpbe_dev->current_timings = var;
176                         return 0;
177                 }
178         }
179
180         return -EINVAL;
181 }
182
183 /**
184  * vpbe_set_output - Set output
185  * @vpbe_dev: vpbe device ptr
186  * @index: index of output
187  *
188  * Set vpbe output to the output specified by the index
189  */
190 static int vpbe_set_output(struct vpbe_device *vpbe_dev, int index)
191 {
192         struct encoder_config_info *curr_enc_info =
193                         vpbe_current_encoder_info(vpbe_dev);
194         struct vpbe_config *cfg = vpbe_dev->cfg;
195         struct venc_platform_data *venc_device = vpbe_dev->venc_device;
196         int enc_out_index;
197         int sd_index;
198         int ret;
199
200         if (index >= cfg->num_outputs)
201                 return -EINVAL;
202
203         mutex_lock(&vpbe_dev->lock);
204
205         sd_index = vpbe_dev->current_sd_index;
206         enc_out_index = cfg->outputs[index].output.index;
207         /*
208          * Currently we switch the encoder based on output selected
209          * by the application. If media controller is implemented later
210          * there is will be an API added to setup_link between venc
211          * and external encoder. So in that case below comparison always
212          * match and encoder will not be switched. But if application
213          * chose not to use media controller, then this provides current
214          * way of switching encoder at the venc output.
215          */
216         if (strcmp(curr_enc_info->module_name,
217                    cfg->outputs[index].subdev_name)) {
218                 /* Need to switch the encoder at the output */
219                 sd_index = vpbe_find_encoder_sd_index(cfg, index);
220                 if (sd_index < 0) {
221                         ret = -EINVAL;
222                         goto unlock;
223                 }
224
225                 ret = venc_device->setup_if_config(cfg->outputs[index].if_params);
226                 if (ret)
227                         goto unlock;
228         }
229
230         /* Set output at the encoder */
231         ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
232                                        s_routing, 0, enc_out_index, 0);
233         if (ret)
234                 goto unlock;
235
236         /*
237          * It is assumed that venc or external encoder will set a default
238          * mode in the sub device. For external encoder or LCD pannel output,
239          * we also need to set up the lcd port for the required mode. So setup
240          * the lcd port for the default mode that is configured in the board
241          * arch/arm/mach-davinci/board-dm355-evm.setup file for the external
242          * encoder.
243          */
244         ret = vpbe_get_mode_info(vpbe_dev,
245                                  cfg->outputs[index].default_mode, index);
246         if (!ret) {
247                 struct osd_state *osd_device = vpbe_dev->osd_device;
248
249                 osd_device->ops.set_left_margin(osd_device,
250                         vpbe_dev->current_timings.left_margin);
251                 osd_device->ops.set_top_margin(osd_device,
252                 vpbe_dev->current_timings.upper_margin);
253                 vpbe_dev->current_sd_index = sd_index;
254                 vpbe_dev->current_out_index = index;
255         }
256 unlock:
257         mutex_unlock(&vpbe_dev->lock);
258         return ret;
259 }
260
261 static int vpbe_set_default_output(struct vpbe_device *vpbe_dev)
262 {
263         struct vpbe_config *cfg = vpbe_dev->cfg;
264         int i;
265
266         for (i = 0; i < cfg->num_outputs; i++) {
267                 if (!strcmp(def_output,
268                             cfg->outputs[i].output.name)) {
269                         int ret = vpbe_set_output(vpbe_dev, i);
270
271                         if (!ret)
272                                 vpbe_dev->current_out_index = i;
273                         return ret;
274                 }
275         }
276         return 0;
277 }
278
279 /**
280  * vpbe_get_output - Get output
281  * @vpbe_dev: vpbe device ptr
282  *
283  * return current vpbe output to the the index
284  */
285 static unsigned int vpbe_get_output(struct vpbe_device *vpbe_dev)
286 {
287         return vpbe_dev->current_out_index;
288 }
289
290 /*
291  * vpbe_s_dv_timings - Set the given preset timings in the encoder
292  *
293  * Sets the timings if supported by the current encoder. Return the status.
294  * 0 - success & -EINVAL on error
295  */
296 static int vpbe_s_dv_timings(struct vpbe_device *vpbe_dev,
297                     struct v4l2_dv_timings *dv_timings)
298 {
299         struct vpbe_config *cfg = vpbe_dev->cfg;
300         int out_index = vpbe_dev->current_out_index;
301         struct vpbe_output *output = &cfg->outputs[out_index];
302         int sd_index = vpbe_dev->current_sd_index;
303         int ret, i;
304
305
306         if (!(cfg->outputs[out_index].output.capabilities &
307             V4L2_OUT_CAP_DV_TIMINGS))
308                 return -ENODATA;
309
310         for (i = 0; i < output->num_modes; i++) {
311                 if (output->modes[i].timings_type == VPBE_ENC_DV_TIMINGS &&
312                     !memcmp(&output->modes[i].dv_timings,
313                                 dv_timings, sizeof(*dv_timings)))
314                         break;
315         }
316         if (i >= output->num_modes)
317                 return -EINVAL;
318         vpbe_dev->current_timings = output->modes[i];
319         mutex_lock(&vpbe_dev->lock);
320
321         ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
322                                         s_dv_timings, dv_timings);
323         if (!ret && vpbe_dev->amp) {
324                 /* Call amplifier subdevice */
325                 ret = v4l2_subdev_call(vpbe_dev->amp, video,
326                                 s_dv_timings, dv_timings);
327         }
328         /* set the lcd controller output for the given mode */
329         if (!ret) {
330                 struct osd_state *osd_device = vpbe_dev->osd_device;
331
332                 osd_device->ops.set_left_margin(osd_device,
333                 vpbe_dev->current_timings.left_margin);
334                 osd_device->ops.set_top_margin(osd_device,
335                 vpbe_dev->current_timings.upper_margin);
336         }
337         mutex_unlock(&vpbe_dev->lock);
338
339         return ret;
340 }
341
342 /*
343  * vpbe_g_dv_timings - Get the timings in the current encoder
344  *
345  * Get the timings in the current encoder. Return the status. 0 - success
346  * -EINVAL on error
347  */
348 static int vpbe_g_dv_timings(struct vpbe_device *vpbe_dev,
349                      struct v4l2_dv_timings *dv_timings)
350 {
351         struct vpbe_config *cfg = vpbe_dev->cfg;
352         int out_index = vpbe_dev->current_out_index;
353
354         if (!(cfg->outputs[out_index].output.capabilities &
355                 V4L2_OUT_CAP_DV_TIMINGS))
356                 return -ENODATA;
357
358         if (vpbe_dev->current_timings.timings_type &
359           VPBE_ENC_DV_TIMINGS) {
360                 *dv_timings = vpbe_dev->current_timings.dv_timings;
361                 return 0;
362         }
363
364         return -EINVAL;
365 }
366
367 /*
368  * vpbe_enum_dv_timings - Enumerate the dv timings in the current encoder
369  *
370  * Get the timings in the current encoder. Return the status. 0 - success
371  * -EINVAL on error
372  */
373 static int vpbe_enum_dv_timings(struct vpbe_device *vpbe_dev,
374                          struct v4l2_enum_dv_timings *timings)
375 {
376         struct vpbe_config *cfg = vpbe_dev->cfg;
377         int out_index = vpbe_dev->current_out_index;
378         struct vpbe_output *output = &cfg->outputs[out_index];
379         int j = 0;
380         int i;
381
382         if (!(output->output.capabilities & V4L2_OUT_CAP_DV_TIMINGS))
383                 return -ENODATA;
384
385         for (i = 0; i < output->num_modes; i++) {
386                 if (output->modes[i].timings_type == VPBE_ENC_DV_TIMINGS) {
387                         if (j == timings->index)
388                                 break;
389                         j++;
390                 }
391         }
392
393         if (i == output->num_modes)
394                 return -EINVAL;
395         timings->timings = output->modes[i].dv_timings;
396         return 0;
397 }
398
399 /*
400  * vpbe_s_std - Set the given standard in the encoder
401  *
402  * Sets the standard if supported by the current encoder. Return the status.
403  * 0 - success & -EINVAL on error
404  */
405 static int vpbe_s_std(struct vpbe_device *vpbe_dev, v4l2_std_id std_id)
406 {
407         struct vpbe_config *cfg = vpbe_dev->cfg;
408         int out_index = vpbe_dev->current_out_index;
409         int sd_index = vpbe_dev->current_sd_index;
410         int ret;
411
412         if (!(cfg->outputs[out_index].output.capabilities &
413                 V4L2_OUT_CAP_STD))
414                 return -ENODATA;
415
416         ret = vpbe_get_std_info(vpbe_dev, std_id);
417         if (ret)
418                 return ret;
419
420         mutex_lock(&vpbe_dev->lock);
421
422         ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
423                                s_std_output, std_id);
424         /* set the lcd controller output for the given mode */
425         if (!ret) {
426                 struct osd_state *osd_device = vpbe_dev->osd_device;
427
428                 osd_device->ops.set_left_margin(osd_device,
429                 vpbe_dev->current_timings.left_margin);
430                 osd_device->ops.set_top_margin(osd_device,
431                 vpbe_dev->current_timings.upper_margin);
432         }
433         mutex_unlock(&vpbe_dev->lock);
434
435         return ret;
436 }
437
438 /*
439  * vpbe_g_std - Get the standard in the current encoder
440  *
441  * Get the standard in the current encoder. Return the status. 0 - success
442  * -EINVAL on error
443  */
444 static int vpbe_g_std(struct vpbe_device *vpbe_dev, v4l2_std_id *std_id)
445 {
446         struct vpbe_enc_mode_info *cur_timings = &vpbe_dev->current_timings;
447         struct vpbe_config *cfg = vpbe_dev->cfg;
448         int out_index = vpbe_dev->current_out_index;
449
450         if (!(cfg->outputs[out_index].output.capabilities & V4L2_OUT_CAP_STD))
451                 return -ENODATA;
452
453         if (cur_timings->timings_type & VPBE_ENC_STD) {
454                 *std_id = cur_timings->std_id;
455                 return 0;
456         }
457
458         return -EINVAL;
459 }
460
461 /*
462  * vpbe_set_mode - Set mode in the current encoder using mode info
463  *
464  * Use the mode string to decide what timings to set in the encoder
465  * This is typically useful when fbset command is used to change the current
466  * timings by specifying a string to indicate the timings.
467  */
468 static int vpbe_set_mode(struct vpbe_device *vpbe_dev,
469                          struct vpbe_enc_mode_info *mode_info)
470 {
471         struct vpbe_enc_mode_info *preset_mode = NULL;
472         struct vpbe_config *cfg = vpbe_dev->cfg;
473         struct v4l2_dv_timings dv_timings;
474         struct osd_state *osd_device;
475         int out_index = vpbe_dev->current_out_index;
476         int i;
477
478         if (!mode_info || !mode_info->name)
479                 return -EINVAL;
480
481         for (i = 0; i < cfg->outputs[out_index].num_modes; i++) {
482                 if (!strcmp(mode_info->name,
483                      cfg->outputs[out_index].modes[i].name)) {
484                         preset_mode = &cfg->outputs[out_index].modes[i];
485                         /*
486                          * it may be one of the 3 timings type. Check and
487                          * invoke right API
488                          */
489                         if (preset_mode->timings_type & VPBE_ENC_STD)
490                                 return vpbe_s_std(vpbe_dev,
491                                                  preset_mode->std_id);
492                         if (preset_mode->timings_type &
493                                                 VPBE_ENC_DV_TIMINGS) {
494                                 dv_timings =
495                                         preset_mode->dv_timings;
496                                 return vpbe_s_dv_timings(vpbe_dev, &dv_timings);
497                         }
498                 }
499         }
500
501         /* Only custom timing should reach here */
502         if (!preset_mode)
503                 return -EINVAL;
504
505         mutex_lock(&vpbe_dev->lock);
506
507         osd_device = vpbe_dev->osd_device;
508         vpbe_dev->current_timings = *preset_mode;
509         osd_device->ops.set_left_margin(osd_device,
510                 vpbe_dev->current_timings.left_margin);
511         osd_device->ops.set_top_margin(osd_device,
512                 vpbe_dev->current_timings.upper_margin);
513
514         mutex_unlock(&vpbe_dev->lock);
515         return 0;
516 }
517
518 static int vpbe_set_default_mode(struct vpbe_device *vpbe_dev)
519 {
520         int ret;
521
522         ret = vpbe_get_std_info_by_name(vpbe_dev, def_mode);
523         if (ret)
524                 return ret;
525
526         /* set the default mode in the encoder */
527         return vpbe_set_mode(vpbe_dev, &vpbe_dev->current_timings);
528 }
529
530 static int platform_device_get(struct device *dev, void *data)
531 {
532         struct platform_device *pdev = to_platform_device(dev);
533         struct vpbe_device *vpbe_dev = data;
534
535         if (strstr(pdev->name, "vpbe-osd"))
536                 vpbe_dev->osd_device = platform_get_drvdata(pdev);
537         if (strstr(pdev->name, "vpbe-venc"))
538                 vpbe_dev->venc_device = dev_get_platdata(&pdev->dev);
539
540         return 0;
541 }
542
543 /**
544  * vpbe_initialize() - Initialize the vpbe display controller
545  * @dev: Master and slave device ptr
546  * @vpbe_dev: vpbe device ptr
547  *
548  * Master frame buffer device drivers calls this to initialize vpbe
549  * display controller. This will then registers v4l2 device and the sub
550  * devices and sets a current encoder sub device for display. v4l2 display
551  * device driver is the master and frame buffer display device driver is
552  * the slave. Frame buffer display driver checks the initialized during
553  * probe and exit if not initialized. Returns status.
554  */
555 static int vpbe_initialize(struct device *dev, struct vpbe_device *vpbe_dev)
556 {
557         struct encoder_config_info *enc_info;
558         struct amp_config_info *amp_info;
559         struct v4l2_subdev **enc_subdev;
560         struct osd_state *osd_device;
561         struct i2c_adapter *i2c_adap;
562         int num_encoders;
563         int ret = 0;
564         int err;
565         int i;
566
567         /*
568          * v4l2 abd FBDev frame buffer devices will get the vpbe_dev pointer
569          * from the platform device by iteration of platform drivers and
570          * matching with device name
571          */
572         if (!vpbe_dev || !dev) {
573                 printk(KERN_ERR "Null device pointers.\n");
574                 return -ENODEV;
575         }
576
577         if (vpbe_dev->initialized)
578                 return 0;
579
580         mutex_lock(&vpbe_dev->lock);
581
582         if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
583                 /* We have dac clock available for platform */
584                 vpbe_dev->dac_clk = clk_get(vpbe_dev->pdev, "vpss_dac");
585                 if (IS_ERR(vpbe_dev->dac_clk)) {
586                         ret =  PTR_ERR(vpbe_dev->dac_clk);
587                         goto fail_mutex_unlock;
588                 }
589                 if (clk_prepare_enable(vpbe_dev->dac_clk)) {
590                         ret =  -ENODEV;
591                         clk_put(vpbe_dev->dac_clk);
592                         goto fail_mutex_unlock;
593                 }
594         }
595
596         /* first enable vpss clocks */
597         vpss_enable_clock(VPSS_VPBE_CLOCK, 1);
598
599         /* First register a v4l2 device */
600         ret = v4l2_device_register(dev, &vpbe_dev->v4l2_dev);
601         if (ret) {
602                 v4l2_err(dev->driver,
603                         "Unable to register v4l2 device.\n");
604                 goto fail_clk_put;
605         }
606         v4l2_info(&vpbe_dev->v4l2_dev, "vpbe v4l2 device registered\n");
607
608         err = bus_for_each_dev(&platform_bus_type, NULL, vpbe_dev,
609                                platform_device_get);
610         if (err < 0) {
611                 ret = err;
612                 goto fail_dev_unregister;
613         }
614
615         vpbe_dev->venc = venc_sub_dev_init(&vpbe_dev->v4l2_dev,
616                                            vpbe_dev->cfg->venc.module_name);
617         /* register venc sub device */
618         if (!vpbe_dev->venc) {
619                 v4l2_err(&vpbe_dev->v4l2_dev,
620                         "vpbe unable to init venc sub device\n");
621                 ret = -ENODEV;
622                 goto fail_dev_unregister;
623         }
624         /* initialize osd device */
625         osd_device = vpbe_dev->osd_device;
626         if (osd_device->ops.initialize) {
627                 err = osd_device->ops.initialize(osd_device);
628                 if (err) {
629                         v4l2_err(&vpbe_dev->v4l2_dev,
630                                  "unable to initialize the OSD device");
631                         err = -ENOMEM;
632                         goto fail_dev_unregister;
633                 }
634         }
635
636         /*
637          * Register any external encoders that are configured. At index 0 we
638          * store venc sd index.
639          */
640         num_encoders = vpbe_dev->cfg->num_ext_encoders + 1;
641         vpbe_dev->encoders = kmalloc_array(num_encoders,
642                                            sizeof(*vpbe_dev->encoders),
643                                            GFP_KERNEL);
644         if (!vpbe_dev->encoders) {
645                 ret = -ENOMEM;
646                 goto fail_dev_unregister;
647         }
648
649         i2c_adap = i2c_get_adapter(vpbe_dev->cfg->i2c_adapter_id);
650         for (i = 0; i < (vpbe_dev->cfg->num_ext_encoders + 1); i++) {
651                 if (i == 0) {
652                         /* venc is at index 0 */
653                         enc_subdev = &vpbe_dev->encoders[i];
654                         *enc_subdev = vpbe_dev->venc;
655                         continue;
656                 }
657                 enc_info = &vpbe_dev->cfg->ext_encoders[i];
658                 if (enc_info->is_i2c) {
659                         enc_subdev = &vpbe_dev->encoders[i];
660                         *enc_subdev = v4l2_i2c_new_subdev_board(
661                                                 &vpbe_dev->v4l2_dev, i2c_adap,
662                                                 &enc_info->board_info, NULL);
663                         if (*enc_subdev)
664                                 v4l2_info(&vpbe_dev->v4l2_dev,
665                                           "v4l2 sub device %s registered\n",
666                                           enc_info->module_name);
667                         else {
668                                 v4l2_err(&vpbe_dev->v4l2_dev, "encoder %s failed to register",
669                                          enc_info->module_name);
670                                 ret = -ENODEV;
671                                 goto fail_kfree_encoders;
672                         }
673                 } else
674                         v4l2_warn(&vpbe_dev->v4l2_dev, "non-i2c encoders currently not supported");
675         }
676         /* Add amplifier subdevice for dm365 */
677         if ((strcmp(vpbe_dev->cfg->module_name, "dm365-vpbe-display") == 0) &&
678            vpbe_dev->cfg->amp) {
679                 amp_info = vpbe_dev->cfg->amp;
680                 if (amp_info->is_i2c) {
681                         vpbe_dev->amp = v4l2_i2c_new_subdev_board(
682                         &vpbe_dev->v4l2_dev, i2c_adap,
683                         &amp_info->board_info, NULL);
684                         if (!vpbe_dev->amp) {
685                                 v4l2_err(&vpbe_dev->v4l2_dev,
686                                          "amplifier %s failed to register",
687                                          amp_info->module_name);
688                                 ret = -ENODEV;
689                                 goto fail_kfree_encoders;
690                         }
691                         v4l2_info(&vpbe_dev->v4l2_dev,
692                                           "v4l2 sub device %s registered\n",
693                                           amp_info->module_name);
694                 } else {
695                             vpbe_dev->amp = NULL;
696                             v4l2_warn(&vpbe_dev->v4l2_dev, "non-i2c amplifiers currently not supported");
697                 }
698         } else {
699             vpbe_dev->amp = NULL;
700         }
701
702         /* set the current encoder and output to that of venc by default */
703         vpbe_dev->current_sd_index = 0;
704         vpbe_dev->current_out_index = 0;
705
706         mutex_unlock(&vpbe_dev->lock);
707
708         printk(KERN_NOTICE "Setting default output to %s\n", def_output);
709         ret = vpbe_set_default_output(vpbe_dev);
710         if (ret) {
711                 v4l2_err(&vpbe_dev->v4l2_dev, "Failed to set default output %s",
712                          def_output);
713                 goto fail_kfree_amp;
714         }
715
716         printk(KERN_NOTICE "Setting default mode to %s\n", def_mode);
717         ret = vpbe_set_default_mode(vpbe_dev);
718         if (ret) {
719                 v4l2_err(&vpbe_dev->v4l2_dev, "Failed to set default mode %s",
720                          def_mode);
721                 goto fail_kfree_amp;
722         }
723         vpbe_dev->initialized = 1;
724         /* TBD handling of bootargs for default output and mode */
725         return 0;
726
727 fail_kfree_amp:
728         mutex_lock(&vpbe_dev->lock);
729         kfree(vpbe_dev->amp);
730 fail_kfree_encoders:
731         kfree(vpbe_dev->encoders);
732 fail_dev_unregister:
733         v4l2_device_unregister(&vpbe_dev->v4l2_dev);
734 fail_clk_put:
735         if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
736                 clk_disable_unprepare(vpbe_dev->dac_clk);
737                 clk_put(vpbe_dev->dac_clk);
738         }
739 fail_mutex_unlock:
740         mutex_unlock(&vpbe_dev->lock);
741         return ret;
742 }
743
744 /**
745  * vpbe_deinitialize() - de-initialize the vpbe display controller
746  * @dev: Master and slave device ptr
747  * @vpbe_dev: vpbe device ptr
748  *
749  * vpbe_master and slave frame buffer devices calls this to de-initialize
750  * the display controller. It is called when master and slave device
751  * driver modules are removed and no longer requires the display controller.
752  */
753 static void vpbe_deinitialize(struct device *dev, struct vpbe_device *vpbe_dev)
754 {
755         v4l2_device_unregister(&vpbe_dev->v4l2_dev);
756         if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
757                 clk_disable_unprepare(vpbe_dev->dac_clk);
758                 clk_put(vpbe_dev->dac_clk);
759         }
760
761         kfree(vpbe_dev->amp);
762         kfree(vpbe_dev->encoders);
763         vpbe_dev->initialized = 0;
764         /* disable vpss clocks */
765         vpss_enable_clock(VPSS_VPBE_CLOCK, 0);
766 }
767
768 static const struct vpbe_device_ops vpbe_dev_ops = {
769         .enum_outputs = vpbe_enum_outputs,
770         .set_output = vpbe_set_output,
771         .get_output = vpbe_get_output,
772         .s_dv_timings = vpbe_s_dv_timings,
773         .g_dv_timings = vpbe_g_dv_timings,
774         .enum_dv_timings = vpbe_enum_dv_timings,
775         .s_std = vpbe_s_std,
776         .g_std = vpbe_g_std,
777         .initialize = vpbe_initialize,
778         .deinitialize = vpbe_deinitialize,
779         .get_mode_info = vpbe_get_current_mode_info,
780         .set_mode = vpbe_set_mode,
781 };
782
783 static int vpbe_probe(struct platform_device *pdev)
784 {
785         struct vpbe_device *vpbe_dev;
786         struct vpbe_config *cfg;
787
788         if (!pdev->dev.platform_data) {
789                 v4l2_err(pdev->dev.driver, "No platform data\n");
790                 return -ENODEV;
791         }
792         cfg = pdev->dev.platform_data;
793
794         if (!cfg->module_name[0] ||
795             !cfg->osd.module_name[0] ||
796             !cfg->venc.module_name[0]) {
797                 v4l2_err(pdev->dev.driver, "vpbe display module names not defined\n");
798                 return -EINVAL;
799         }
800
801         vpbe_dev = kzalloc(sizeof(*vpbe_dev), GFP_KERNEL);
802         if (!vpbe_dev)
803                 return -ENOMEM;
804
805         vpbe_dev->cfg = cfg;
806         vpbe_dev->ops = vpbe_dev_ops;
807         vpbe_dev->pdev = &pdev->dev;
808
809         if (cfg->outputs->num_modes > 0)
810                 vpbe_dev->current_timings = vpbe_dev->cfg->outputs[0].modes[0];
811         else {
812                 kfree(vpbe_dev);
813                 return -ENODEV;
814         }
815
816         /* set the driver data in platform device */
817         platform_set_drvdata(pdev, vpbe_dev);
818         mutex_init(&vpbe_dev->lock);
819
820         return 0;
821 }
822
823 static int vpbe_remove(struct platform_device *device)
824 {
825         struct vpbe_device *vpbe_dev = platform_get_drvdata(device);
826
827         kfree(vpbe_dev);
828
829         return 0;
830 }
831
832 static struct platform_driver vpbe_driver = {
833         .driver = {
834                 .name   = "vpbe_controller",
835         },
836         .probe = vpbe_probe,
837         .remove = vpbe_remove,
838 };
839
840 module_platform_driver(vpbe_driver);