2 * vpif - Video Port Interface driver
3 * VPIF is a receiver and transmitter for video data. It has two channels(0, 1)
4 * that receiving video byte stream and two channels(2, 3) for video output.
5 * The hardware supports SDTV, HDTV formats, raw data capture.
6 * Currently, the driver supports NTSC and PAL standards.
8 * Copyright (C) 2009 Texas Instruments Incorporated - https://www.ti.com/
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation version 2.
14 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
15 * kind, whether express or implied; without even the implied warranty
16 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/err.h>
21 #include <linux/init.h>
23 #include <linux/irq.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/spinlock.h>
30 #include <linux/v4l2-dv-timings.h>
31 #include <linux/of_graph.h>
35 MODULE_DESCRIPTION("TI DaVinci Video Port Interface driver");
36 MODULE_LICENSE("GPL");
38 #define VPIF_DRIVER_NAME "vpif"
39 MODULE_ALIAS("platform:" VPIF_DRIVER_NAME);
41 #define VPIF_CH0_MAX_MODES 22
42 #define VPIF_CH1_MAX_MODES 2
43 #define VPIF_CH2_MAX_MODES 15
44 #define VPIF_CH3_MAX_MODES 2
47 struct platform_device *capture;
48 struct platform_device *display;
51 DEFINE_SPINLOCK(vpif_lock);
52 EXPORT_SYMBOL_GPL(vpif_lock);
54 void __iomem *vpif_base;
55 EXPORT_SYMBOL_GPL(vpif_base);
58 * vpif_ch_params: video standard configuration parameters for vpif
60 * The table must include all presets from supported subdevices.
62 const struct vpif_channel_config_params vpif_ch_params[] = {
79 .dv_timings = V4L2_DV_BT_CEA_720X480P59_94,
96 .dv_timings = V4L2_DV_BT_CEA_720X576P50,
113 .dv_timings = V4L2_DV_BT_CEA_1280X720P50,
130 .dv_timings = V4L2_DV_BT_CEA_1280X720P60,
150 .dv_timings = V4L2_DV_BT_CEA_1920X1080I50,
170 .dv_timings = V4L2_DV_BT_CEA_1920X1080I60,
187 .dv_timings = V4L2_DV_BT_CEA_1920X1080P60,
209 .stdid = V4L2_STD_525_60,
212 .name = "PAL_BDGHIK",
229 .stdid = V4L2_STD_625_50,
232 EXPORT_SYMBOL_GPL(vpif_ch_params);
234 const unsigned int vpif_ch_params_count = ARRAY_SIZE(vpif_ch_params);
235 EXPORT_SYMBOL_GPL(vpif_ch_params_count);
237 static inline void vpif_wr_bit(u32 reg, u32 bit, u32 val)
240 vpif_set_bit(reg, bit);
242 vpif_clr_bit(reg, bit);
245 /* This structure is used to keep track of VPIF size register's offsets */
246 struct vpif_registers {
247 u32 h_cfg, v_cfg_00, v_cfg_01, v_cfg_02, v_cfg, ch_ctrl;
248 u32 line_offset, vanc0_strt, vanc0_size, vanc1_strt;
249 u32 vanc1_size, width_mask, len_mask;
253 static const struct vpif_registers vpifregs[VPIF_NUM_CHANNELS] = {
256 VPIF_CH0_H_CFG, VPIF_CH0_V_CFG_00, VPIF_CH0_V_CFG_01,
257 VPIF_CH0_V_CFG_02, VPIF_CH0_V_CFG_03, VPIF_CH0_CTRL,
258 VPIF_CH0_IMG_ADD_OFST, 0, 0, 0, 0, 0x1FFF, 0xFFF,
263 VPIF_CH1_H_CFG, VPIF_CH1_V_CFG_00, VPIF_CH1_V_CFG_01,
264 VPIF_CH1_V_CFG_02, VPIF_CH1_V_CFG_03, VPIF_CH1_CTRL,
265 VPIF_CH1_IMG_ADD_OFST, 0, 0, 0, 0, 0x1FFF, 0xFFF,
270 VPIF_CH2_H_CFG, VPIF_CH2_V_CFG_00, VPIF_CH2_V_CFG_01,
271 VPIF_CH2_V_CFG_02, VPIF_CH2_V_CFG_03, VPIF_CH2_CTRL,
272 VPIF_CH2_IMG_ADD_OFST, VPIF_CH2_VANC0_STRT, VPIF_CH2_VANC0_SIZE,
273 VPIF_CH2_VANC1_STRT, VPIF_CH2_VANC1_SIZE, 0x7FF, 0x7FF,
278 VPIF_CH3_H_CFG, VPIF_CH3_V_CFG_00, VPIF_CH3_V_CFG_01,
279 VPIF_CH3_V_CFG_02, VPIF_CH3_V_CFG_03, VPIF_CH3_CTRL,
280 VPIF_CH3_IMG_ADD_OFST, VPIF_CH3_VANC0_STRT, VPIF_CH3_VANC0_SIZE,
281 VPIF_CH3_VANC1_STRT, VPIF_CH3_VANC1_SIZE, 0x7FF, 0x7FF,
286 /* vpif_set_mode_info:
287 * This function is used to set horizontal and vertical config parameters
288 * As per the standard in the channel, configure the values of L1, L3,
289 * L5, L7 L9, L11 in VPIF Register , also write width and height
291 static void vpif_set_mode_info(const struct vpif_channel_config_params *config,
292 u8 channel_id, u8 config_channel_id)
296 value = (config->eav2sav & vpifregs[config_channel_id].width_mask);
297 value <<= VPIF_CH_LEN_SHIFT;
298 value |= (config->sav2eav & vpifregs[config_channel_id].width_mask);
299 regw(value, vpifregs[channel_id].h_cfg);
301 value = (config->l1 & vpifregs[config_channel_id].len_mask);
302 value <<= VPIF_CH_LEN_SHIFT;
303 value |= (config->l3 & vpifregs[config_channel_id].len_mask);
304 regw(value, vpifregs[channel_id].v_cfg_00);
306 value = (config->l5 & vpifregs[config_channel_id].len_mask);
307 value <<= VPIF_CH_LEN_SHIFT;
308 value |= (config->l7 & vpifregs[config_channel_id].len_mask);
309 regw(value, vpifregs[channel_id].v_cfg_01);
311 value = (config->l9 & vpifregs[config_channel_id].len_mask);
312 value <<= VPIF_CH_LEN_SHIFT;
313 value |= (config->l11 & vpifregs[config_channel_id].len_mask);
314 regw(value, vpifregs[channel_id].v_cfg_02);
316 value = (config->vsize & vpifregs[config_channel_id].len_mask);
317 regw(value, vpifregs[channel_id].v_cfg);
320 /* config_vpif_params
321 * Function to set the parameters of a channel
322 * Mainly modifies the channel ciontrol register
323 * It sets frame format, yc mux mode
325 static void config_vpif_params(struct vpif_params *vpifparams,
326 u8 channel_id, u8 found)
328 const struct vpif_channel_config_params *config = &vpifparams->std_info;
329 u32 value, ch_nip, reg;
334 end = channel_id + found;
336 for (i = start; i < end; i++) {
337 reg = vpifregs[i].ch_ctrl;
339 ch_nip = VPIF_CAPTURE_CH_NIP;
341 ch_nip = VPIF_DISPLAY_CH_NIP;
343 vpif_wr_bit(reg, ch_nip, config->frm_fmt);
344 vpif_wr_bit(reg, VPIF_CH_YC_MUX_BIT, config->ycmux_mode);
345 vpif_wr_bit(reg, VPIF_CH_INPUT_FIELD_FRAME_BIT,
346 vpifparams->video_params.storage_mode);
348 /* Set raster scanning SDR Format */
349 vpif_clr_bit(reg, VPIF_CH_SDR_FMT_BIT);
350 vpif_wr_bit(reg, VPIF_CH_DATA_MODE_BIT, config->capture_format);
352 if (channel_id > 1) /* Set the Pixel enable bit */
353 vpif_set_bit(reg, VPIF_DISPLAY_PIX_EN_BIT);
354 else if (config->capture_format) {
355 /* Set the polarity of various pins */
356 vpif_wr_bit(reg, VPIF_CH_FID_POLARITY_BIT,
357 vpifparams->iface.fid_pol);
358 vpif_wr_bit(reg, VPIF_CH_V_VALID_POLARITY_BIT,
359 vpifparams->iface.vd_pol);
360 vpif_wr_bit(reg, VPIF_CH_H_VALID_POLARITY_BIT,
361 vpifparams->iface.hd_pol);
366 VPIF_CH_DATA_WIDTH_BIT);
367 value |= ((vpifparams->params.data_sz) <<
368 VPIF_CH_DATA_WIDTH_BIT);
372 /* Write the pitch in the driver */
373 regw((vpifparams->video_params.hpitch),
374 vpifregs[i].line_offset);
378 /* vpif_set_video_params
379 * This function is used to set video parameters in VPIF register
381 int vpif_set_video_params(struct vpif_params *vpifparams, u8 channel_id)
383 const struct vpif_channel_config_params *config = &vpifparams->std_info;
386 vpif_set_mode_info(config, channel_id, channel_id);
387 if (!config->ycmux_mode) {
388 /* YC are on separate channels (HDTV formats) */
389 vpif_set_mode_info(config, channel_id + 1, channel_id);
393 config_vpif_params(vpifparams, channel_id, found);
395 regw(0x80, VPIF_REQ_SIZE);
396 regw(0x01, VPIF_EMULATION_CTRL);
400 EXPORT_SYMBOL(vpif_set_video_params);
402 void vpif_set_vbi_display_params(struct vpif_vbi_params *vbiparams,
407 value = 0x3F8 & (vbiparams->hstart0);
408 value |= 0x3FFFFFF & ((vbiparams->vstart0) << 16);
409 regw(value, vpifregs[channel_id].vanc0_strt);
411 value = 0x3F8 & (vbiparams->hstart1);
412 value |= 0x3FFFFFF & ((vbiparams->vstart1) << 16);
413 regw(value, vpifregs[channel_id].vanc1_strt);
415 value = 0x3F8 & (vbiparams->hsize0);
416 value |= 0x3FFFFFF & ((vbiparams->vsize0) << 16);
417 regw(value, vpifregs[channel_id].vanc0_size);
419 value = 0x3F8 & (vbiparams->hsize1);
420 value |= 0x3FFFFFF & ((vbiparams->vsize1) << 16);
421 regw(value, vpifregs[channel_id].vanc1_size);
424 EXPORT_SYMBOL(vpif_set_vbi_display_params);
426 int vpif_channel_getfid(u8 channel_id)
428 return (regr(vpifregs[channel_id].ch_ctrl) & VPIF_CH_FID_MASK)
429 >> VPIF_CH_FID_SHIFT;
431 EXPORT_SYMBOL(vpif_channel_getfid);
433 static void vpif_pdev_release(struct device *dev)
435 struct platform_device *pdev = to_platform_device(dev);
440 static int vpif_probe(struct platform_device *pdev)
442 static struct resource res_irq;
443 struct platform_device *pdev_capture, *pdev_display;
444 struct device_node *endpoint = NULL;
445 struct vpif_data *data;
449 vpif_base = devm_platform_ioremap_resource(pdev, 0);
450 if (IS_ERR(vpif_base))
451 return PTR_ERR(vpif_base);
453 data = kzalloc(sizeof(*data), GFP_KERNEL);
457 platform_set_drvdata(pdev, data);
459 pm_runtime_enable(&pdev->dev);
460 pm_runtime_get(&pdev->dev);
463 * If VPIF Node has endpoints, assume "new" DT support,
464 * where capture and display drivers don't have DT nodes
465 * so their devices need to be registered manually here
466 * for their legacy platform_drivers to work.
468 endpoint = of_graph_get_next_endpoint(pdev->dev.of_node,
474 * For DT platforms, manually create platform_devices for
475 * capture/display drivers.
477 irq = platform_get_irq(pdev, 0);
482 res_irq = (struct resource)DEFINE_RES_IRQ_NAMED(irq, of_node_full_name(pdev->dev.of_node));
483 res_irq.flags |= irq_get_trigger_type(irq);
485 pdev_capture = kzalloc(sizeof(*pdev_capture), GFP_KERNEL);
491 pdev_capture->name = "vpif_capture";
492 pdev_capture->id = -1;
493 pdev_capture->resource = &res_irq;
494 pdev_capture->num_resources = 1;
495 pdev_capture->dev.dma_mask = pdev->dev.dma_mask;
496 pdev_capture->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
497 pdev_capture->dev.parent = &pdev->dev;
498 pdev_capture->dev.release = vpif_pdev_release;
500 ret = platform_device_register(pdev_capture);
502 goto err_put_pdev_capture;
504 pdev_display = kzalloc(sizeof(*pdev_display), GFP_KERNEL);
507 goto err_put_pdev_capture;
510 pdev_display->name = "vpif_display";
511 pdev_display->id = -1;
512 pdev_display->resource = &res_irq;
513 pdev_display->num_resources = 1;
514 pdev_display->dev.dma_mask = pdev->dev.dma_mask;
515 pdev_display->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
516 pdev_display->dev.parent = &pdev->dev;
517 pdev_display->dev.release = vpif_pdev_release;
519 ret = platform_device_register(pdev_display);
521 goto err_put_pdev_display;
523 data->capture = pdev_capture;
524 data->display = pdev_display;
528 err_put_pdev_display:
529 platform_device_put(pdev_display);
530 err_put_pdev_capture:
531 platform_device_put(pdev_capture);
533 pm_runtime_put(&pdev->dev);
534 pm_runtime_disable(&pdev->dev);
540 static int vpif_remove(struct platform_device *pdev)
542 struct vpif_data *data = platform_get_drvdata(pdev);
545 platform_device_unregister(data->capture);
547 platform_device_unregister(data->display);
549 pm_runtime_put(&pdev->dev);
550 pm_runtime_disable(&pdev->dev);
558 static int vpif_suspend(struct device *dev)
564 static int vpif_resume(struct device *dev)
570 static const struct dev_pm_ops vpif_pm = {
571 .suspend = vpif_suspend,
572 .resume = vpif_resume,
575 #define vpif_pm_ops (&vpif_pm)
577 #define vpif_pm_ops NULL
580 #if IS_ENABLED(CONFIG_OF)
581 static const struct of_device_id vpif_of_match[] = {
582 { .compatible = "ti,da850-vpif", },
585 MODULE_DEVICE_TABLE(of, vpif_of_match);
588 static struct platform_driver vpif_driver = {
590 .of_match_table = of_match_ptr(vpif_of_match),
591 .name = VPIF_DRIVER_NAME,
594 .remove = vpif_remove,
598 static void vpif_exit(void)
600 platform_driver_unregister(&vpif_driver);
603 static int __init vpif_init(void)
605 return platform_driver_register(&vpif_driver);
607 subsys_initcall(vpif_init);
608 module_exit(vpif_exit);