vpif.c 11 KB

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