vpif.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * vpif - Video Port Interface driver
  3. * 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. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/kernel.h>
  24. #include <linux/io.h>
  25. #include <linux/err.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/v4l2-dv-timings.h>
  28. #include <mach/hardware.h>
  29. #include "vpif.h"
  30. MODULE_DESCRIPTION("TI DaVinci Video Port Interface driver");
  31. MODULE_LICENSE("GPL");
  32. #define VPIF_CH0_MAX_MODES (22)
  33. #define VPIF_CH1_MAX_MODES (02)
  34. #define VPIF_CH2_MAX_MODES (15)
  35. #define VPIF_CH3_MAX_MODES (02)
  36. static resource_size_t res_len;
  37. static struct resource *res;
  38. spinlock_t vpif_lock;
  39. void __iomem *vpif_base;
  40. EXPORT_SYMBOL_GPL(vpif_base);
  41. /**
  42. * vpif_ch_params: video standard configuration parameters for vpif
  43. * The table must include all presets from supported subdevices.
  44. */
  45. const struct vpif_channel_config_params vpif_ch_params[] = {
  46. /* HDTV formats */
  47. {
  48. .name = "480p59_94",
  49. .width = 720,
  50. .height = 480,
  51. .frm_fmt = 1,
  52. .ycmux_mode = 0,
  53. .eav2sav = 138-8,
  54. .sav2eav = 720,
  55. .l1 = 1,
  56. .l3 = 43,
  57. .l5 = 523,
  58. .vsize = 525,
  59. .capture_format = 0,
  60. .vbi_supported = 0,
  61. .hd_sd = 1,
  62. .dv_timings = V4L2_DV_BT_CEA_720X480P59_94,
  63. },
  64. {
  65. .name = "576p50",
  66. .width = 720,
  67. .height = 576,
  68. .frm_fmt = 1,
  69. .ycmux_mode = 0,
  70. .eav2sav = 144-8,
  71. .sav2eav = 720,
  72. .l1 = 1,
  73. .l3 = 45,
  74. .l5 = 621,
  75. .vsize = 625,
  76. .capture_format = 0,
  77. .vbi_supported = 0,
  78. .hd_sd = 1,
  79. .dv_timings = V4L2_DV_BT_CEA_720X576P50,
  80. },
  81. {
  82. .name = "720p50",
  83. .width = 1280,
  84. .height = 720,
  85. .frm_fmt = 1,
  86. .ycmux_mode = 0,
  87. .eav2sav = 700-8,
  88. .sav2eav = 1280,
  89. .l1 = 1,
  90. .l3 = 26,
  91. .l5 = 746,
  92. .vsize = 750,
  93. .capture_format = 0,
  94. .vbi_supported = 0,
  95. .hd_sd = 1,
  96. .dv_timings = V4L2_DV_BT_CEA_1280X720P50,
  97. },
  98. {
  99. .name = "720p60",
  100. .width = 1280,
  101. .height = 720,
  102. .frm_fmt = 1,
  103. .ycmux_mode = 0,
  104. .eav2sav = 370 - 8,
  105. .sav2eav = 1280,
  106. .l1 = 1,
  107. .l3 = 26,
  108. .l5 = 746,
  109. .vsize = 750,
  110. .capture_format = 0,
  111. .vbi_supported = 0,
  112. .hd_sd = 1,
  113. .dv_timings = V4L2_DV_BT_CEA_1280X720P60,
  114. },
  115. {
  116. .name = "1080I50",
  117. .width = 1920,
  118. .height = 1080,
  119. .frm_fmt = 0,
  120. .ycmux_mode = 0,
  121. .eav2sav = 720 - 8,
  122. .sav2eav = 1920,
  123. .l1 = 1,
  124. .l3 = 21,
  125. .l5 = 561,
  126. .l7 = 563,
  127. .l9 = 584,
  128. .l11 = 1124,
  129. .vsize = 1125,
  130. .capture_format = 0,
  131. .vbi_supported = 0,
  132. .hd_sd = 1,
  133. .dv_timings = V4L2_DV_BT_CEA_1920X1080I50,
  134. },
  135. {
  136. .name = "1080I60",
  137. .width = 1920,
  138. .height = 1080,
  139. .frm_fmt = 0,
  140. .ycmux_mode = 0,
  141. .eav2sav = 280 - 8,
  142. .sav2eav = 1920,
  143. .l1 = 1,
  144. .l3 = 21,
  145. .l5 = 561,
  146. .l7 = 563,
  147. .l9 = 584,
  148. .l11 = 1124,
  149. .vsize = 1125,
  150. .capture_format = 0,
  151. .vbi_supported = 0,
  152. .hd_sd = 1,
  153. .dv_timings = V4L2_DV_BT_CEA_1920X1080I60,
  154. },
  155. {
  156. .name = "1080p60",
  157. .width = 1920,
  158. .height = 1080,
  159. .frm_fmt = 1,
  160. .ycmux_mode = 0,
  161. .eav2sav = 280 - 8,
  162. .sav2eav = 1920,
  163. .l1 = 1,
  164. .l3 = 42,
  165. .l5 = 1122,
  166. .vsize = 1125,
  167. .capture_format = 0,
  168. .vbi_supported = 0,
  169. .hd_sd = 1,
  170. .dv_timings = V4L2_DV_BT_CEA_1920X1080P60,
  171. },
  172. /* SDTV formats */
  173. {
  174. .name = "NTSC_M",
  175. .width = 720,
  176. .height = 480,
  177. .frm_fmt = 0,
  178. .ycmux_mode = 1,
  179. .eav2sav = 268,
  180. .sav2eav = 1440,
  181. .l1 = 1,
  182. .l3 = 23,
  183. .l5 = 263,
  184. .l7 = 266,
  185. .l9 = 286,
  186. .l11 = 525,
  187. .vsize = 525,
  188. .capture_format = 0,
  189. .vbi_supported = 1,
  190. .hd_sd = 0,
  191. .stdid = V4L2_STD_525_60,
  192. },
  193. {
  194. .name = "PAL_BDGHIK",
  195. .width = 720,
  196. .height = 576,
  197. .frm_fmt = 0,
  198. .ycmux_mode = 1,
  199. .eav2sav = 280,
  200. .sav2eav = 1440,
  201. .l1 = 1,
  202. .l3 = 23,
  203. .l5 = 311,
  204. .l7 = 313,
  205. .l9 = 336,
  206. .l11 = 624,
  207. .vsize = 625,
  208. .capture_format = 0,
  209. .vbi_supported = 1,
  210. .hd_sd = 0,
  211. .stdid = V4L2_STD_625_50,
  212. },
  213. };
  214. EXPORT_SYMBOL_GPL(vpif_ch_params);
  215. const unsigned int vpif_ch_params_count = ARRAY_SIZE(vpif_ch_params);
  216. EXPORT_SYMBOL_GPL(vpif_ch_params_count);
  217. static inline void vpif_wr_bit(u32 reg, u32 bit, u32 val)
  218. {
  219. if (val)
  220. vpif_set_bit(reg, bit);
  221. else
  222. vpif_clr_bit(reg, bit);
  223. }
  224. /* This structure is used to keep track of VPIF size register's offsets */
  225. struct vpif_registers {
  226. u32 h_cfg, v_cfg_00, v_cfg_01, v_cfg_02, v_cfg, ch_ctrl;
  227. u32 line_offset, vanc0_strt, vanc0_size, vanc1_strt;
  228. u32 vanc1_size, width_mask, len_mask;
  229. u8 max_modes;
  230. };
  231. static const struct vpif_registers vpifregs[VPIF_NUM_CHANNELS] = {
  232. /* Channel0 */
  233. {
  234. VPIF_CH0_H_CFG, VPIF_CH0_V_CFG_00, VPIF_CH0_V_CFG_01,
  235. VPIF_CH0_V_CFG_02, VPIF_CH0_V_CFG_03, VPIF_CH0_CTRL,
  236. VPIF_CH0_IMG_ADD_OFST, 0, 0, 0, 0, 0x1FFF, 0xFFF,
  237. VPIF_CH0_MAX_MODES,
  238. },
  239. /* Channel1 */
  240. {
  241. VPIF_CH1_H_CFG, VPIF_CH1_V_CFG_00, VPIF_CH1_V_CFG_01,
  242. VPIF_CH1_V_CFG_02, VPIF_CH1_V_CFG_03, VPIF_CH1_CTRL,
  243. VPIF_CH1_IMG_ADD_OFST, 0, 0, 0, 0, 0x1FFF, 0xFFF,
  244. VPIF_CH1_MAX_MODES,
  245. },
  246. /* Channel2 */
  247. {
  248. VPIF_CH2_H_CFG, VPIF_CH2_V_CFG_00, VPIF_CH2_V_CFG_01,
  249. VPIF_CH2_V_CFG_02, VPIF_CH2_V_CFG_03, VPIF_CH2_CTRL,
  250. VPIF_CH2_IMG_ADD_OFST, VPIF_CH2_VANC0_STRT, VPIF_CH2_VANC0_SIZE,
  251. VPIF_CH2_VANC1_STRT, VPIF_CH2_VANC1_SIZE, 0x7FF, 0x7FF,
  252. VPIF_CH2_MAX_MODES
  253. },
  254. /* Channel3 */
  255. {
  256. VPIF_CH3_H_CFG, VPIF_CH3_V_CFG_00, VPIF_CH3_V_CFG_01,
  257. VPIF_CH3_V_CFG_02, VPIF_CH3_V_CFG_03, VPIF_CH3_CTRL,
  258. VPIF_CH3_IMG_ADD_OFST, VPIF_CH3_VANC0_STRT, VPIF_CH3_VANC0_SIZE,
  259. VPIF_CH3_VANC1_STRT, VPIF_CH3_VANC1_SIZE, 0x7FF, 0x7FF,
  260. VPIF_CH3_MAX_MODES
  261. },
  262. };
  263. /* vpif_set_mode_info:
  264. * This function is used to set horizontal and vertical config parameters
  265. * As per the standard in the channel, configure the values of L1, L3,
  266. * L5, L7 L9, L11 in VPIF Register , also write width and height
  267. */
  268. static void vpif_set_mode_info(const struct vpif_channel_config_params *config,
  269. u8 channel_id, u8 config_channel_id)
  270. {
  271. u32 value;
  272. value = (config->eav2sav & vpifregs[config_channel_id].width_mask);
  273. value <<= VPIF_CH_LEN_SHIFT;
  274. value |= (config->sav2eav & vpifregs[config_channel_id].width_mask);
  275. regw(value, vpifregs[channel_id].h_cfg);
  276. value = (config->l1 & vpifregs[config_channel_id].len_mask);
  277. value <<= VPIF_CH_LEN_SHIFT;
  278. value |= (config->l3 & vpifregs[config_channel_id].len_mask);
  279. regw(value, vpifregs[channel_id].v_cfg_00);
  280. value = (config->l5 & vpifregs[config_channel_id].len_mask);
  281. value <<= VPIF_CH_LEN_SHIFT;
  282. value |= (config->l7 & vpifregs[config_channel_id].len_mask);
  283. regw(value, vpifregs[channel_id].v_cfg_01);
  284. value = (config->l9 & vpifregs[config_channel_id].len_mask);
  285. value <<= VPIF_CH_LEN_SHIFT;
  286. value |= (config->l11 & vpifregs[config_channel_id].len_mask);
  287. regw(value, vpifregs[channel_id].v_cfg_02);
  288. value = (config->vsize & vpifregs[config_channel_id].len_mask);
  289. regw(value, vpifregs[channel_id].v_cfg);
  290. }
  291. /* config_vpif_params
  292. * Function to set the parameters of a channel
  293. * Mainly modifies the channel ciontrol register
  294. * It sets frame format, yc mux mode
  295. */
  296. static void config_vpif_params(struct vpif_params *vpifparams,
  297. u8 channel_id, u8 found)
  298. {
  299. const struct vpif_channel_config_params *config = &vpifparams->std_info;
  300. u32 value, ch_nip, reg;
  301. u8 start, end;
  302. int i;
  303. start = channel_id;
  304. end = channel_id + found;
  305. for (i = start; i < end; i++) {
  306. reg = vpifregs[i].ch_ctrl;
  307. if (channel_id < 2)
  308. ch_nip = VPIF_CAPTURE_CH_NIP;
  309. else
  310. ch_nip = VPIF_DISPLAY_CH_NIP;
  311. vpif_wr_bit(reg, ch_nip, config->frm_fmt);
  312. vpif_wr_bit(reg, VPIF_CH_YC_MUX_BIT, config->ycmux_mode);
  313. vpif_wr_bit(reg, VPIF_CH_INPUT_FIELD_FRAME_BIT,
  314. vpifparams->video_params.storage_mode);
  315. /* Set raster scanning SDR Format */
  316. vpif_clr_bit(reg, VPIF_CH_SDR_FMT_BIT);
  317. vpif_wr_bit(reg, VPIF_CH_DATA_MODE_BIT, config->capture_format);
  318. if (channel_id > 1) /* Set the Pixel enable bit */
  319. vpif_set_bit(reg, VPIF_DISPLAY_PIX_EN_BIT);
  320. else if (config->capture_format) {
  321. /* Set the polarity of various pins */
  322. vpif_wr_bit(reg, VPIF_CH_FID_POLARITY_BIT,
  323. vpifparams->iface.fid_pol);
  324. vpif_wr_bit(reg, VPIF_CH_V_VALID_POLARITY_BIT,
  325. vpifparams->iface.vd_pol);
  326. vpif_wr_bit(reg, VPIF_CH_H_VALID_POLARITY_BIT,
  327. vpifparams->iface.hd_pol);
  328. value = regr(reg);
  329. /* Set data width */
  330. value &= ~(0x3u <<
  331. VPIF_CH_DATA_WIDTH_BIT);
  332. value |= ((vpifparams->params.data_sz) <<
  333. VPIF_CH_DATA_WIDTH_BIT);
  334. regw(value, reg);
  335. }
  336. /* Write the pitch in the driver */
  337. regw((vpifparams->video_params.hpitch),
  338. vpifregs[i].line_offset);
  339. }
  340. }
  341. /* vpif_set_video_params
  342. * This function is used to set video parameters in VPIF register
  343. */
  344. int vpif_set_video_params(struct vpif_params *vpifparams, u8 channel_id)
  345. {
  346. const struct vpif_channel_config_params *config = &vpifparams->std_info;
  347. int found = 1;
  348. vpif_set_mode_info(config, channel_id, channel_id);
  349. if (!config->ycmux_mode) {
  350. /* YC are on separate channels (HDTV formats) */
  351. vpif_set_mode_info(config, channel_id + 1, channel_id);
  352. found = 2;
  353. }
  354. config_vpif_params(vpifparams, channel_id, found);
  355. regw(0x80, VPIF_REQ_SIZE);
  356. regw(0x01, VPIF_EMULATION_CTRL);
  357. return found;
  358. }
  359. EXPORT_SYMBOL(vpif_set_video_params);
  360. void vpif_set_vbi_display_params(struct vpif_vbi_params *vbiparams,
  361. u8 channel_id)
  362. {
  363. u32 value;
  364. value = 0x3F8 & (vbiparams->hstart0);
  365. value |= 0x3FFFFFF & ((vbiparams->vstart0) << 16);
  366. regw(value, vpifregs[channel_id].vanc0_strt);
  367. value = 0x3F8 & (vbiparams->hstart1);
  368. value |= 0x3FFFFFF & ((vbiparams->vstart1) << 16);
  369. regw(value, vpifregs[channel_id].vanc1_strt);
  370. value = 0x3F8 & (vbiparams->hsize0);
  371. value |= 0x3FFFFFF & ((vbiparams->vsize0) << 16);
  372. regw(value, vpifregs[channel_id].vanc0_size);
  373. value = 0x3F8 & (vbiparams->hsize1);
  374. value |= 0x3FFFFFF & ((vbiparams->vsize1) << 16);
  375. regw(value, vpifregs[channel_id].vanc1_size);
  376. }
  377. EXPORT_SYMBOL(vpif_set_vbi_display_params);
  378. int vpif_channel_getfid(u8 channel_id)
  379. {
  380. return (regr(vpifregs[channel_id].ch_ctrl) & VPIF_CH_FID_MASK)
  381. >> VPIF_CH_FID_SHIFT;
  382. }
  383. EXPORT_SYMBOL(vpif_channel_getfid);
  384. static int vpif_probe(struct platform_device *pdev)
  385. {
  386. int status = 0;
  387. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  388. if (!res)
  389. return -ENOENT;
  390. res_len = resource_size(res);
  391. res = request_mem_region(res->start, res_len, res->name);
  392. if (!res)
  393. return -EBUSY;
  394. vpif_base = ioremap(res->start, res_len);
  395. if (!vpif_base) {
  396. status = -EBUSY;
  397. goto fail;
  398. }
  399. pm_runtime_enable(&pdev->dev);
  400. pm_runtime_get(&pdev->dev);
  401. spin_lock_init(&vpif_lock);
  402. dev_info(&pdev->dev, "vpif probe success\n");
  403. return 0;
  404. fail:
  405. release_mem_region(res->start, res_len);
  406. return status;
  407. }
  408. static int vpif_remove(struct platform_device *pdev)
  409. {
  410. pm_runtime_disable(&pdev->dev);
  411. iounmap(vpif_base);
  412. release_mem_region(res->start, res_len);
  413. return 0;
  414. }
  415. #ifdef CONFIG_PM
  416. static int vpif_suspend(struct device *dev)
  417. {
  418. pm_runtime_put(dev);
  419. return 0;
  420. }
  421. static int vpif_resume(struct device *dev)
  422. {
  423. pm_runtime_get(dev);
  424. return 0;
  425. }
  426. static const struct dev_pm_ops vpif_pm = {
  427. .suspend = vpif_suspend,
  428. .resume = vpif_resume,
  429. };
  430. #define vpif_pm_ops (&vpif_pm)
  431. #else
  432. #define vpif_pm_ops NULL
  433. #endif
  434. static struct platform_driver vpif_driver = {
  435. .driver = {
  436. .name = "vpif",
  437. .owner = THIS_MODULE,
  438. .pm = vpif_pm_ops,
  439. },
  440. .remove = vpif_remove,
  441. .probe = vpif_probe,
  442. };
  443. static void vpif_exit(void)
  444. {
  445. platform_driver_unregister(&vpif_driver);
  446. }
  447. static int __init vpif_init(void)
  448. {
  449. return platform_driver_register(&vpif_driver);
  450. }
  451. subsys_initcall(vpif_init);
  452. module_exit(vpif_exit);