blob: 761c8259a8afd61d4553d9c077e82ec6fe53c964 [file] [log] [blame]
Chaithrika U S63a8e712009-06-09 05:54:02 -03001/*
Manjunath Hadli0a631722012-04-13 04:44:00 -03002 * vpif - Video Port Interface driver
Chaithrika U S63a8e712009-06-09 05:54:02 -03003 * VPIF is a receiver and transmitter for video data. It has two channels(0, 1)
4 * that receiveing 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.
7 *
8 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
9 *
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.
13 *
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.
18 */
19
Lad, Prabhakar01105692013-06-17 11:20:41 -030020#include <linux/err.h>
Chaithrika U S63a8e712009-06-09 05:54:02 -030021#include <linux/init.h>
Lad, Prabhakar01105692013-06-17 11:20:41 -030022#include <linux/io.h>
23#include <linux/kernel.h>
Chaithrika U S63a8e712009-06-09 05:54:02 -030024#include <linux/module.h>
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -030025#include <linux/platform_device.h>
Lad, Prabhakar407ccc62013-04-02 09:41:30 -030026#include <linux/pm_runtime.h>
Lad, Prabhakar01105692013-06-17 11:20:41 -030027#include <linux/spinlock.h>
Hans Verkuil0598c172012-09-18 07:18:47 -030028#include <linux/v4l2-dv-timings.h>
29
Chaithrika U S63a8e712009-06-09 05:54:02 -030030#include "vpif.h"
31
32MODULE_DESCRIPTION("TI DaVinci Video Port Interface driver");
33MODULE_LICENSE("GPL");
34
35#define VPIF_CH0_MAX_MODES (22)
36#define VPIF_CH1_MAX_MODES (02)
37#define VPIF_CH2_MAX_MODES (15)
38#define VPIF_CH3_MAX_MODES (02)
39
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -030040static resource_size_t res_len;
41static struct resource *res;
42spinlock_t vpif_lock;
43
44void __iomem *vpif_base;
Lad, Prabhakarced9b212013-03-20 01:28:27 -030045EXPORT_SYMBOL_GPL(vpif_base);
46
Mats Randgaardaa444402010-12-16 12:17:42 -030047/**
Lad, Prabhakarced9b212013-03-20 01:28:27 -030048 * vpif_ch_params: video standard configuration parameters for vpif
Mats Randgaardaa444402010-12-16 12:17:42 -030049 * The table must include all presets from supported subdevices.
50 */
Lad, Prabhakarced9b212013-03-20 01:28:27 -030051const struct vpif_channel_config_params vpif_ch_params[] = {
Mats Randgaard40c8bce2010-12-16 12:17:43 -030052 /* HDTV formats */
53 {
54 .name = "480p59_94",
55 .width = 720,
56 .height = 480,
57 .frm_fmt = 1,
58 .ycmux_mode = 0,
59 .eav2sav = 138-8,
60 .sav2eav = 720,
61 .l1 = 1,
62 .l3 = 43,
63 .l5 = 523,
64 .vsize = 525,
65 .capture_format = 0,
66 .vbi_supported = 0,
67 .hd_sd = 1,
Hans Verkuil0598c172012-09-18 07:18:47 -030068 .dv_timings = V4L2_DV_BT_CEA_720X480P59_94,
Mats Randgaard40c8bce2010-12-16 12:17:43 -030069 },
70 {
71 .name = "576p50",
72 .width = 720,
73 .height = 576,
74 .frm_fmt = 1,
75 .ycmux_mode = 0,
76 .eav2sav = 144-8,
77 .sav2eav = 720,
78 .l1 = 1,
79 .l3 = 45,
80 .l5 = 621,
81 .vsize = 625,
82 .capture_format = 0,
83 .vbi_supported = 0,
84 .hd_sd = 1,
Hans Verkuil0598c172012-09-18 07:18:47 -030085 .dv_timings = V4L2_DV_BT_CEA_720X576P50,
Mats Randgaard40c8bce2010-12-16 12:17:43 -030086 },
87 {
88 .name = "720p50",
89 .width = 1280,
90 .height = 720,
91 .frm_fmt = 1,
92 .ycmux_mode = 0,
93 .eav2sav = 700-8,
94 .sav2eav = 1280,
95 .l1 = 1,
96 .l3 = 26,
97 .l5 = 746,
98 .vsize = 750,
99 .capture_format = 0,
100 .vbi_supported = 0,
101 .hd_sd = 1,
Hans Verkuil0598c172012-09-18 07:18:47 -0300102 .dv_timings = V4L2_DV_BT_CEA_1280X720P50,
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300103 },
104 {
105 .name = "720p60",
106 .width = 1280,
107 .height = 720,
108 .frm_fmt = 1,
109 .ycmux_mode = 0,
110 .eav2sav = 370 - 8,
111 .sav2eav = 1280,
112 .l1 = 1,
113 .l3 = 26,
114 .l5 = 746,
115 .vsize = 750,
116 .capture_format = 0,
117 .vbi_supported = 0,
118 .hd_sd = 1,
Hans Verkuil0598c172012-09-18 07:18:47 -0300119 .dv_timings = V4L2_DV_BT_CEA_1280X720P60,
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300120 },
121 {
122 .name = "1080I50",
123 .width = 1920,
124 .height = 1080,
125 .frm_fmt = 0,
126 .ycmux_mode = 0,
127 .eav2sav = 720 - 8,
128 .sav2eav = 1920,
129 .l1 = 1,
130 .l3 = 21,
131 .l5 = 561,
132 .l7 = 563,
133 .l9 = 584,
134 .l11 = 1124,
135 .vsize = 1125,
136 .capture_format = 0,
137 .vbi_supported = 0,
138 .hd_sd = 1,
Hans Verkuil0598c172012-09-18 07:18:47 -0300139 .dv_timings = V4L2_DV_BT_CEA_1920X1080I50,
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300140 },
141 {
142 .name = "1080I60",
143 .width = 1920,
144 .height = 1080,
145 .frm_fmt = 0,
146 .ycmux_mode = 0,
147 .eav2sav = 280 - 8,
148 .sav2eav = 1920,
149 .l1 = 1,
150 .l3 = 21,
151 .l5 = 561,
152 .l7 = 563,
153 .l9 = 584,
154 .l11 = 1124,
155 .vsize = 1125,
156 .capture_format = 0,
157 .vbi_supported = 0,
158 .hd_sd = 1,
Hans Verkuil0598c172012-09-18 07:18:47 -0300159 .dv_timings = V4L2_DV_BT_CEA_1920X1080I60,
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300160 },
161 {
162 .name = "1080p60",
163 .width = 1920,
164 .height = 1080,
165 .frm_fmt = 1,
166 .ycmux_mode = 0,
167 .eav2sav = 280 - 8,
168 .sav2eav = 1920,
169 .l1 = 1,
170 .l3 = 42,
171 .l5 = 1122,
172 .vsize = 1125,
173 .capture_format = 0,
174 .vbi_supported = 0,
175 .hd_sd = 1,
Hans Verkuil0598c172012-09-18 07:18:47 -0300176 .dv_timings = V4L2_DV_BT_CEA_1920X1080P60,
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300177 },
178
Mats Randgaardaa444402010-12-16 12:17:42 -0300179 /* SDTV formats */
180 {
181 .name = "NTSC_M",
182 .width = 720,
183 .height = 480,
184 .frm_fmt = 0,
185 .ycmux_mode = 1,
186 .eav2sav = 268,
187 .sav2eav = 1440,
188 .l1 = 1,
189 .l3 = 23,
190 .l5 = 263,
191 .l7 = 266,
192 .l9 = 286,
193 .l11 = 525,
194 .vsize = 525,
195 .capture_format = 0,
196 .vbi_supported = 1,
197 .hd_sd = 0,
198 .stdid = V4L2_STD_525_60,
199 },
200 {
201 .name = "PAL_BDGHIK",
202 .width = 720,
203 .height = 576,
204 .frm_fmt = 0,
205 .ycmux_mode = 1,
206 .eav2sav = 280,
207 .sav2eav = 1440,
208 .l1 = 1,
209 .l3 = 23,
210 .l5 = 311,
211 .l7 = 313,
212 .l9 = 336,
213 .l11 = 624,
214 .vsize = 625,
215 .capture_format = 0,
216 .vbi_supported = 1,
217 .hd_sd = 0,
218 .stdid = V4L2_STD_625_50,
219 },
220};
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300221EXPORT_SYMBOL_GPL(vpif_ch_params);
Mats Randgaardaa444402010-12-16 12:17:42 -0300222
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300223const unsigned int vpif_ch_params_count = ARRAY_SIZE(vpif_ch_params);
224EXPORT_SYMBOL_GPL(vpif_ch_params_count);
Mats Randgaardaa444402010-12-16 12:17:42 -0300225
Chaithrika U S63a8e712009-06-09 05:54:02 -0300226static inline void vpif_wr_bit(u32 reg, u32 bit, u32 val)
227{
228 if (val)
229 vpif_set_bit(reg, bit);
230 else
231 vpif_clr_bit(reg, bit);
232}
233
234/* This structure is used to keep track of VPIF size register's offsets */
235struct vpif_registers {
236 u32 h_cfg, v_cfg_00, v_cfg_01, v_cfg_02, v_cfg, ch_ctrl;
237 u32 line_offset, vanc0_strt, vanc0_size, vanc1_strt;
238 u32 vanc1_size, width_mask, len_mask;
239 u8 max_modes;
240};
241
242static const struct vpif_registers vpifregs[VPIF_NUM_CHANNELS] = {
243 /* Channel0 */
244 {
245 VPIF_CH0_H_CFG, VPIF_CH0_V_CFG_00, VPIF_CH0_V_CFG_01,
246 VPIF_CH0_V_CFG_02, VPIF_CH0_V_CFG_03, VPIF_CH0_CTRL,
247 VPIF_CH0_IMG_ADD_OFST, 0, 0, 0, 0, 0x1FFF, 0xFFF,
248 VPIF_CH0_MAX_MODES,
249 },
250 /* Channel1 */
251 {
252 VPIF_CH1_H_CFG, VPIF_CH1_V_CFG_00, VPIF_CH1_V_CFG_01,
253 VPIF_CH1_V_CFG_02, VPIF_CH1_V_CFG_03, VPIF_CH1_CTRL,
254 VPIF_CH1_IMG_ADD_OFST, 0, 0, 0, 0, 0x1FFF, 0xFFF,
255 VPIF_CH1_MAX_MODES,
256 },
257 /* Channel2 */
258 {
259 VPIF_CH2_H_CFG, VPIF_CH2_V_CFG_00, VPIF_CH2_V_CFG_01,
260 VPIF_CH2_V_CFG_02, VPIF_CH2_V_CFG_03, VPIF_CH2_CTRL,
261 VPIF_CH2_IMG_ADD_OFST, VPIF_CH2_VANC0_STRT, VPIF_CH2_VANC0_SIZE,
262 VPIF_CH2_VANC1_STRT, VPIF_CH2_VANC1_SIZE, 0x7FF, 0x7FF,
263 VPIF_CH2_MAX_MODES
264 },
265 /* Channel3 */
266 {
267 VPIF_CH3_H_CFG, VPIF_CH3_V_CFG_00, VPIF_CH3_V_CFG_01,
268 VPIF_CH3_V_CFG_02, VPIF_CH3_V_CFG_03, VPIF_CH3_CTRL,
269 VPIF_CH3_IMG_ADD_OFST, VPIF_CH3_VANC0_STRT, VPIF_CH3_VANC0_SIZE,
270 VPIF_CH3_VANC1_STRT, VPIF_CH3_VANC1_SIZE, 0x7FF, 0x7FF,
271 VPIF_CH3_MAX_MODES
272 },
273};
274
275/* vpif_set_mode_info:
276 * This function is used to set horizontal and vertical config parameters
277 * As per the standard in the channel, configure the values of L1, L3,
278 * L5, L7 L9, L11 in VPIF Register , also write width and height
279 */
280static void vpif_set_mode_info(const struct vpif_channel_config_params *config,
281 u8 channel_id, u8 config_channel_id)
282{
283 u32 value;
284
285 value = (config->eav2sav & vpifregs[config_channel_id].width_mask);
286 value <<= VPIF_CH_LEN_SHIFT;
287 value |= (config->sav2eav & vpifregs[config_channel_id].width_mask);
288 regw(value, vpifregs[channel_id].h_cfg);
289
290 value = (config->l1 & vpifregs[config_channel_id].len_mask);
291 value <<= VPIF_CH_LEN_SHIFT;
292 value |= (config->l3 & vpifregs[config_channel_id].len_mask);
293 regw(value, vpifregs[channel_id].v_cfg_00);
294
295 value = (config->l5 & vpifregs[config_channel_id].len_mask);
296 value <<= VPIF_CH_LEN_SHIFT;
297 value |= (config->l7 & vpifregs[config_channel_id].len_mask);
298 regw(value, vpifregs[channel_id].v_cfg_01);
299
300 value = (config->l9 & vpifregs[config_channel_id].len_mask);
301 value <<= VPIF_CH_LEN_SHIFT;
302 value |= (config->l11 & vpifregs[config_channel_id].len_mask);
303 regw(value, vpifregs[channel_id].v_cfg_02);
304
305 value = (config->vsize & vpifregs[config_channel_id].len_mask);
306 regw(value, vpifregs[channel_id].v_cfg);
307}
308
309/* config_vpif_params
310 * Function to set the parameters of a channel
311 * Mainly modifies the channel ciontrol register
312 * It sets frame format, yc mux mode
313 */
314static void config_vpif_params(struct vpif_params *vpifparams,
315 u8 channel_id, u8 found)
316{
317 const struct vpif_channel_config_params *config = &vpifparams->std_info;
318 u32 value, ch_nip, reg;
319 u8 start, end;
320 int i;
321
322 start = channel_id;
323 end = channel_id + found;
324
325 for (i = start; i < end; i++) {
326 reg = vpifregs[i].ch_ctrl;
327 if (channel_id < 2)
328 ch_nip = VPIF_CAPTURE_CH_NIP;
329 else
330 ch_nip = VPIF_DISPLAY_CH_NIP;
331
332 vpif_wr_bit(reg, ch_nip, config->frm_fmt);
333 vpif_wr_bit(reg, VPIF_CH_YC_MUX_BIT, config->ycmux_mode);
334 vpif_wr_bit(reg, VPIF_CH_INPUT_FIELD_FRAME_BIT,
335 vpifparams->video_params.storage_mode);
336
337 /* Set raster scanning SDR Format */
338 vpif_clr_bit(reg, VPIF_CH_SDR_FMT_BIT);
339 vpif_wr_bit(reg, VPIF_CH_DATA_MODE_BIT, config->capture_format);
340
341 if (channel_id > 1) /* Set the Pixel enable bit */
342 vpif_set_bit(reg, VPIF_DISPLAY_PIX_EN_BIT);
343 else if (config->capture_format) {
344 /* Set the polarity of various pins */
345 vpif_wr_bit(reg, VPIF_CH_FID_POLARITY_BIT,
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300346 vpifparams->iface.fid_pol);
Chaithrika U S63a8e712009-06-09 05:54:02 -0300347 vpif_wr_bit(reg, VPIF_CH_V_VALID_POLARITY_BIT,
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300348 vpifparams->iface.vd_pol);
Chaithrika U S63a8e712009-06-09 05:54:02 -0300349 vpif_wr_bit(reg, VPIF_CH_H_VALID_POLARITY_BIT,
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300350 vpifparams->iface.hd_pol);
Chaithrika U S63a8e712009-06-09 05:54:02 -0300351
352 value = regr(reg);
353 /* Set data width */
Manjunath Hadli3bc19532012-04-13 04:47:17 -0300354 value &= ~(0x3u <<
Chaithrika U S63a8e712009-06-09 05:54:02 -0300355 VPIF_CH_DATA_WIDTH_BIT);
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300356 value |= ((vpifparams->params.data_sz) <<
Chaithrika U S63a8e712009-06-09 05:54:02 -0300357 VPIF_CH_DATA_WIDTH_BIT);
358 regw(value, reg);
359 }
360
361 /* Write the pitch in the driver */
362 regw((vpifparams->video_params.hpitch),
363 vpifregs[i].line_offset);
364 }
365}
366
367/* vpif_set_video_params
368 * This function is used to set video parameters in VPIF register
369 */
370int vpif_set_video_params(struct vpif_params *vpifparams, u8 channel_id)
371{
372 const struct vpif_channel_config_params *config = &vpifparams->std_info;
373 int found = 1;
374
375 vpif_set_mode_info(config, channel_id, channel_id);
376 if (!config->ycmux_mode) {
377 /* YC are on separate channels (HDTV formats) */
378 vpif_set_mode_info(config, channel_id + 1, channel_id);
379 found = 2;
380 }
381
382 config_vpif_params(vpifparams, channel_id, found);
383
384 regw(0x80, VPIF_REQ_SIZE);
385 regw(0x01, VPIF_EMULATION_CTRL);
386
387 return found;
388}
389EXPORT_SYMBOL(vpif_set_video_params);
390
391void vpif_set_vbi_display_params(struct vpif_vbi_params *vbiparams,
392 u8 channel_id)
393{
394 u32 value;
395
396 value = 0x3F8 & (vbiparams->hstart0);
397 value |= 0x3FFFFFF & ((vbiparams->vstart0) << 16);
398 regw(value, vpifregs[channel_id].vanc0_strt);
399
400 value = 0x3F8 & (vbiparams->hstart1);
401 value |= 0x3FFFFFF & ((vbiparams->vstart1) << 16);
402 regw(value, vpifregs[channel_id].vanc1_strt);
403
404 value = 0x3F8 & (vbiparams->hsize0);
405 value |= 0x3FFFFFF & ((vbiparams->vsize0) << 16);
406 regw(value, vpifregs[channel_id].vanc0_size);
407
408 value = 0x3F8 & (vbiparams->hsize1);
409 value |= 0x3FFFFFF & ((vbiparams->vsize1) << 16);
410 regw(value, vpifregs[channel_id].vanc1_size);
411
412}
413EXPORT_SYMBOL(vpif_set_vbi_display_params);
414
415int vpif_channel_getfid(u8 channel_id)
416{
417 return (regr(vpifregs[channel_id].ch_ctrl) & VPIF_CH_FID_MASK)
418 >> VPIF_CH_FID_SHIFT;
419}
420EXPORT_SYMBOL(vpif_channel_getfid);
421
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800422static int vpif_probe(struct platform_device *pdev)
Chaithrika U S63a8e712009-06-09 05:54:02 -0300423{
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300424 int status = 0;
425
426 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
427 if (!res)
428 return -ENOENT;
429
Joe Perches28f65c112011-06-09 09:13:32 -0700430 res_len = resource_size(res);
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300431
432 res = request_mem_region(res->start, res_len, res->name);
433 if (!res)
434 return -EBUSY;
435
436 vpif_base = ioremap(res->start, res_len);
437 if (!vpif_base) {
438 status = -EBUSY;
439 goto fail;
440 }
441
Lad, Prabhakar407ccc62013-04-02 09:41:30 -0300442 pm_runtime_enable(&pdev->dev);
443 pm_runtime_get(&pdev->dev);
Manjunath Hadli31415d02012-04-13 04:51:13 -0300444
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300445 spin_lock_init(&vpif_lock);
446 dev_info(&pdev->dev, "vpif probe success\n");
447 return 0;
448
449fail:
450 release_mem_region(res->start, res_len);
451 return status;
Chaithrika U S63a8e712009-06-09 05:54:02 -0300452}
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300453
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800454static int vpif_remove(struct platform_device *pdev)
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300455{
Lad, Prabhakar407ccc62013-04-02 09:41:30 -0300456 pm_runtime_disable(&pdev->dev);
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300457 iounmap(vpif_base);
458 release_mem_region(res->start, res_len);
459 return 0;
460}
461
Manjunath Hadli31415d02012-04-13 04:51:13 -0300462#ifdef CONFIG_PM
463static int vpif_suspend(struct device *dev)
464{
Lad, Prabhakar407ccc62013-04-02 09:41:30 -0300465 pm_runtime_put(dev);
Manjunath Hadli31415d02012-04-13 04:51:13 -0300466 return 0;
467}
468
469static int vpif_resume(struct device *dev)
470{
Lad, Prabhakar407ccc62013-04-02 09:41:30 -0300471 pm_runtime_get(dev);
Manjunath Hadli31415d02012-04-13 04:51:13 -0300472 return 0;
473}
474
475static const struct dev_pm_ops vpif_pm = {
476 .suspend = vpif_suspend,
477 .resume = vpif_resume,
478};
479
480#define vpif_pm_ops (&vpif_pm)
481#else
482#define vpif_pm_ops NULL
483#endif
484
Lad, Prabhakara1b3a6c2012-08-14 01:23:09 -0300485static struct platform_driver vpif_driver = {
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300486 .driver = {
487 .name = "vpif",
488 .owner = THIS_MODULE,
Manjunath Hadli31415d02012-04-13 04:51:13 -0300489 .pm = vpif_pm_ops,
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300490 },
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800491 .remove = vpif_remove,
Muralidharan Karicherid28a6df2009-09-16 14:31:20 -0300492 .probe = vpif_probe,
493};
494
495static void vpif_exit(void)
496{
497 platform_driver_unregister(&vpif_driver);
498}
499
500static int __init vpif_init(void)
501{
502 return platform_driver_register(&vpif_driver);
503}
504subsys_initcall(vpif_init);
505module_exit(vpif_exit);
506