vpif.c 14 KB

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