vpss.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2009 Texas Instruments.
  4. *
  5. * common vpss system module platform driver for all video drivers.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/io.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/err.h>
  12. #include <media/davinci/vpss.h>
  13. MODULE_LICENSE("GPL");
  14. MODULE_DESCRIPTION("VPSS Driver");
  15. MODULE_AUTHOR("Texas Instruments");
  16. /* DM644x defines */
  17. #define DM644X_SBL_PCR_VPSS (4)
  18. #define DM355_VPSSBL_INTSEL 0x10
  19. #define DM355_VPSSBL_EVTSEL 0x14
  20. /* vpss BL register offsets */
  21. #define DM355_VPSSBL_CCDCMUX 0x1c
  22. /* vpss CLK register offsets */
  23. #define DM355_VPSSCLK_CLKCTRL 0x04
  24. /* masks and shifts */
  25. #define VPSS_HSSISEL_SHIFT 4
  26. /*
  27. * VDINT0 - vpss_int0, VDINT1 - vpss_int1, H3A - vpss_int4,
  28. * IPIPE_INT1_SDR - vpss_int5
  29. */
  30. #define DM355_VPSSBL_INTSEL_DEFAULT 0xff83ff10
  31. /* VENCINT - vpss_int8 */
  32. #define DM355_VPSSBL_EVTSEL_DEFAULT 0x4
  33. #define DM365_ISP5_PCCR 0x04
  34. #define DM365_ISP5_PCCR_BL_CLK_ENABLE BIT(0)
  35. #define DM365_ISP5_PCCR_ISIF_CLK_ENABLE BIT(1)
  36. #define DM365_ISP5_PCCR_H3A_CLK_ENABLE BIT(2)
  37. #define DM365_ISP5_PCCR_RSZ_CLK_ENABLE BIT(3)
  38. #define DM365_ISP5_PCCR_IPIPE_CLK_ENABLE BIT(4)
  39. #define DM365_ISP5_PCCR_IPIPEIF_CLK_ENABLE BIT(5)
  40. #define DM365_ISP5_PCCR_RSV BIT(6)
  41. #define DM365_ISP5_BCR 0x08
  42. #define DM365_ISP5_BCR_ISIF_OUT_ENABLE BIT(1)
  43. #define DM365_ISP5_INTSEL1 0x10
  44. #define DM365_ISP5_INTSEL2 0x14
  45. #define DM365_ISP5_INTSEL3 0x18
  46. #define DM365_ISP5_CCDCMUX 0x20
  47. #define DM365_ISP5_PG_FRAME_SIZE 0x28
  48. #define DM365_VPBE_CLK_CTRL 0x00
  49. #define VPSS_CLK_CTRL 0x01c40044
  50. #define VPSS_CLK_CTRL_VENCCLKEN BIT(3)
  51. #define VPSS_CLK_CTRL_DACCLKEN BIT(4)
  52. /*
  53. * vpss interrupts. VDINT0 - vpss_int0, VDINT1 - vpss_int1,
  54. * AF - vpss_int3
  55. */
  56. #define DM365_ISP5_INTSEL1_DEFAULT 0x0b1f0100
  57. /* AEW - vpss_int6, RSZ_INT_DMA - vpss_int5 */
  58. #define DM365_ISP5_INTSEL2_DEFAULT 0x1f0a0f1f
  59. /* VENC - vpss_int8 */
  60. #define DM365_ISP5_INTSEL3_DEFAULT 0x00000015
  61. /* masks and shifts for DM365*/
  62. #define DM365_CCDC_PG_VD_POL_SHIFT 0
  63. #define DM365_CCDC_PG_HD_POL_SHIFT 1
  64. #define CCD_SRC_SEL_MASK (BIT_MASK(5) | BIT_MASK(4))
  65. #define CCD_SRC_SEL_SHIFT 4
  66. /* Different SoC platforms supported by this driver */
  67. enum vpss_platform_type {
  68. DM644X,
  69. DM355,
  70. DM365,
  71. };
  72. /*
  73. * vpss operations. Depends on platform. Not all functions are available
  74. * on all platforms. The api, first check if a function is available before
  75. * invoking it. In the probe, the function ptrs are initialized based on
  76. * vpss name. vpss name can be "dm355_vpss", "dm644x_vpss" etc.
  77. */
  78. struct vpss_hw_ops {
  79. /* enable clock */
  80. int (*enable_clock)(enum vpss_clock_sel clock_sel, int en);
  81. /* select input to ccdc */
  82. void (*select_ccdc_source)(enum vpss_ccdc_source_sel src_sel);
  83. /* clear wbl overflow bit */
  84. int (*clear_wbl_overflow)(enum vpss_wbl_sel wbl_sel);
  85. /* set sync polarity */
  86. void (*set_sync_pol)(struct vpss_sync_pol);
  87. /* set the PG_FRAME_SIZE register*/
  88. void (*set_pg_frame_size)(struct vpss_pg_frame_size);
  89. /* check and clear interrupt if occurred */
  90. int (*dma_complete_interrupt)(void);
  91. };
  92. /* vpss configuration */
  93. struct vpss_oper_config {
  94. __iomem void *vpss_regs_base0;
  95. __iomem void *vpss_regs_base1;
  96. __iomem void *vpss_regs_base2;
  97. enum vpss_platform_type platform;
  98. spinlock_t vpss_lock;
  99. struct vpss_hw_ops hw_ops;
  100. };
  101. static struct vpss_oper_config oper_cfg;
  102. /* register access routines */
  103. static inline u32 bl_regr(u32 offset)
  104. {
  105. return __raw_readl(oper_cfg.vpss_regs_base0 + offset);
  106. }
  107. static inline void bl_regw(u32 val, u32 offset)
  108. {
  109. __raw_writel(val, oper_cfg.vpss_regs_base0 + offset);
  110. }
  111. static inline u32 vpss_regr(u32 offset)
  112. {
  113. return __raw_readl(oper_cfg.vpss_regs_base1 + offset);
  114. }
  115. static inline void vpss_regw(u32 val, u32 offset)
  116. {
  117. __raw_writel(val, oper_cfg.vpss_regs_base1 + offset);
  118. }
  119. /* For DM365 only */
  120. static inline u32 isp5_read(u32 offset)
  121. {
  122. return __raw_readl(oper_cfg.vpss_regs_base0 + offset);
  123. }
  124. /* For DM365 only */
  125. static inline void isp5_write(u32 val, u32 offset)
  126. {
  127. __raw_writel(val, oper_cfg.vpss_regs_base0 + offset);
  128. }
  129. static void dm365_select_ccdc_source(enum vpss_ccdc_source_sel src_sel)
  130. {
  131. u32 temp = isp5_read(DM365_ISP5_CCDCMUX) & ~CCD_SRC_SEL_MASK;
  132. /* if we are using pattern generator, enable it */
  133. if (src_sel == VPSS_PGLPBK || src_sel == VPSS_CCDCPG)
  134. temp |= 0x08;
  135. temp |= (src_sel << CCD_SRC_SEL_SHIFT);
  136. isp5_write(temp, DM365_ISP5_CCDCMUX);
  137. }
  138. static void dm355_select_ccdc_source(enum vpss_ccdc_source_sel src_sel)
  139. {
  140. bl_regw(src_sel << VPSS_HSSISEL_SHIFT, DM355_VPSSBL_CCDCMUX);
  141. }
  142. int vpss_dma_complete_interrupt(void)
  143. {
  144. if (!oper_cfg.hw_ops.dma_complete_interrupt)
  145. return 2;
  146. return oper_cfg.hw_ops.dma_complete_interrupt();
  147. }
  148. EXPORT_SYMBOL(vpss_dma_complete_interrupt);
  149. int vpss_select_ccdc_source(enum vpss_ccdc_source_sel src_sel)
  150. {
  151. if (!oper_cfg.hw_ops.select_ccdc_source)
  152. return -EINVAL;
  153. oper_cfg.hw_ops.select_ccdc_source(src_sel);
  154. return 0;
  155. }
  156. EXPORT_SYMBOL(vpss_select_ccdc_source);
  157. static int dm644x_clear_wbl_overflow(enum vpss_wbl_sel wbl_sel)
  158. {
  159. u32 mask = 1, val;
  160. if (wbl_sel < VPSS_PCR_AEW_WBL_0 ||
  161. wbl_sel > VPSS_PCR_CCDC_WBL_O)
  162. return -EINVAL;
  163. /* writing a 0 clear the overflow */
  164. mask = ~(mask << wbl_sel);
  165. val = bl_regr(DM644X_SBL_PCR_VPSS) & mask;
  166. bl_regw(val, DM644X_SBL_PCR_VPSS);
  167. return 0;
  168. }
  169. void vpss_set_sync_pol(struct vpss_sync_pol sync)
  170. {
  171. if (!oper_cfg.hw_ops.set_sync_pol)
  172. return;
  173. oper_cfg.hw_ops.set_sync_pol(sync);
  174. }
  175. EXPORT_SYMBOL(vpss_set_sync_pol);
  176. int vpss_clear_wbl_overflow(enum vpss_wbl_sel wbl_sel)
  177. {
  178. if (!oper_cfg.hw_ops.clear_wbl_overflow)
  179. return -EINVAL;
  180. return oper_cfg.hw_ops.clear_wbl_overflow(wbl_sel);
  181. }
  182. EXPORT_SYMBOL(vpss_clear_wbl_overflow);
  183. /*
  184. * dm355_enable_clock - Enable VPSS Clock
  185. * @clock_sel: Clock to be enabled/disabled
  186. * @en: enable/disable flag
  187. *
  188. * This is called to enable or disable a vpss clock
  189. */
  190. static int dm355_enable_clock(enum vpss_clock_sel clock_sel, int en)
  191. {
  192. unsigned long flags;
  193. u32 utemp, mask = 0x1, shift = 0;
  194. switch (clock_sel) {
  195. case VPSS_VPBE_CLOCK:
  196. /* nothing since lsb */
  197. break;
  198. case VPSS_VENC_CLOCK_SEL:
  199. shift = 2;
  200. break;
  201. case VPSS_CFALD_CLOCK:
  202. shift = 3;
  203. break;
  204. case VPSS_H3A_CLOCK:
  205. shift = 4;
  206. break;
  207. case VPSS_IPIPE_CLOCK:
  208. shift = 5;
  209. break;
  210. case VPSS_CCDC_CLOCK:
  211. shift = 6;
  212. break;
  213. default:
  214. printk(KERN_ERR "dm355_enable_clock: Invalid selector: %d\n",
  215. clock_sel);
  216. return -EINVAL;
  217. }
  218. spin_lock_irqsave(&oper_cfg.vpss_lock, flags);
  219. utemp = vpss_regr(DM355_VPSSCLK_CLKCTRL);
  220. if (!en)
  221. utemp &= ~(mask << shift);
  222. else
  223. utemp |= (mask << shift);
  224. vpss_regw(utemp, DM355_VPSSCLK_CLKCTRL);
  225. spin_unlock_irqrestore(&oper_cfg.vpss_lock, flags);
  226. return 0;
  227. }
  228. static int dm365_enable_clock(enum vpss_clock_sel clock_sel, int en)
  229. {
  230. unsigned long flags;
  231. u32 utemp, mask = 0x1, shift = 0, offset = DM365_ISP5_PCCR;
  232. u32 (*read)(u32 offset) = isp5_read;
  233. void(*write)(u32 val, u32 offset) = isp5_write;
  234. switch (clock_sel) {
  235. case VPSS_BL_CLOCK:
  236. break;
  237. case VPSS_CCDC_CLOCK:
  238. shift = 1;
  239. break;
  240. case VPSS_H3A_CLOCK:
  241. shift = 2;
  242. break;
  243. case VPSS_RSZ_CLOCK:
  244. shift = 3;
  245. break;
  246. case VPSS_IPIPE_CLOCK:
  247. shift = 4;
  248. break;
  249. case VPSS_IPIPEIF_CLOCK:
  250. shift = 5;
  251. break;
  252. case VPSS_PCLK_INTERNAL:
  253. shift = 6;
  254. break;
  255. case VPSS_PSYNC_CLOCK_SEL:
  256. shift = 7;
  257. break;
  258. case VPSS_VPBE_CLOCK:
  259. read = vpss_regr;
  260. write = vpss_regw;
  261. offset = DM365_VPBE_CLK_CTRL;
  262. break;
  263. case VPSS_VENC_CLOCK_SEL:
  264. shift = 2;
  265. read = vpss_regr;
  266. write = vpss_regw;
  267. offset = DM365_VPBE_CLK_CTRL;
  268. break;
  269. case VPSS_LDC_CLOCK:
  270. shift = 3;
  271. read = vpss_regr;
  272. write = vpss_regw;
  273. offset = DM365_VPBE_CLK_CTRL;
  274. break;
  275. case VPSS_FDIF_CLOCK:
  276. shift = 4;
  277. read = vpss_regr;
  278. write = vpss_regw;
  279. offset = DM365_VPBE_CLK_CTRL;
  280. break;
  281. case VPSS_OSD_CLOCK_SEL:
  282. shift = 6;
  283. read = vpss_regr;
  284. write = vpss_regw;
  285. offset = DM365_VPBE_CLK_CTRL;
  286. break;
  287. case VPSS_LDC_CLOCK_SEL:
  288. shift = 7;
  289. read = vpss_regr;
  290. write = vpss_regw;
  291. offset = DM365_VPBE_CLK_CTRL;
  292. break;
  293. default:
  294. printk(KERN_ERR "dm365_enable_clock: Invalid selector: %d\n",
  295. clock_sel);
  296. return -1;
  297. }
  298. spin_lock_irqsave(&oper_cfg.vpss_lock, flags);
  299. utemp = read(offset);
  300. if (!en) {
  301. mask = ~mask;
  302. utemp &= (mask << shift);
  303. } else
  304. utemp |= (mask << shift);
  305. write(utemp, offset);
  306. spin_unlock_irqrestore(&oper_cfg.vpss_lock, flags);
  307. return 0;
  308. }
  309. int vpss_enable_clock(enum vpss_clock_sel clock_sel, int en)
  310. {
  311. if (!oper_cfg.hw_ops.enable_clock)
  312. return -EINVAL;
  313. return oper_cfg.hw_ops.enable_clock(clock_sel, en);
  314. }
  315. EXPORT_SYMBOL(vpss_enable_clock);
  316. void dm365_vpss_set_sync_pol(struct vpss_sync_pol sync)
  317. {
  318. int val = 0;
  319. val = isp5_read(DM365_ISP5_CCDCMUX);
  320. val |= (sync.ccdpg_hdpol << DM365_CCDC_PG_HD_POL_SHIFT);
  321. val |= (sync.ccdpg_vdpol << DM365_CCDC_PG_VD_POL_SHIFT);
  322. isp5_write(val, DM365_ISP5_CCDCMUX);
  323. }
  324. EXPORT_SYMBOL(dm365_vpss_set_sync_pol);
  325. void vpss_set_pg_frame_size(struct vpss_pg_frame_size frame_size)
  326. {
  327. if (!oper_cfg.hw_ops.set_pg_frame_size)
  328. return;
  329. oper_cfg.hw_ops.set_pg_frame_size(frame_size);
  330. }
  331. EXPORT_SYMBOL(vpss_set_pg_frame_size);
  332. void dm365_vpss_set_pg_frame_size(struct vpss_pg_frame_size frame_size)
  333. {
  334. int current_reg = ((frame_size.hlpfr >> 1) - 1) << 16;
  335. current_reg |= (frame_size.pplen - 1);
  336. isp5_write(current_reg, DM365_ISP5_PG_FRAME_SIZE);
  337. }
  338. EXPORT_SYMBOL(dm365_vpss_set_pg_frame_size);
  339. static int vpss_probe(struct platform_device *pdev)
  340. {
  341. struct resource *res;
  342. char *platform_name;
  343. if (!pdev->dev.platform_data) {
  344. dev_err(&pdev->dev, "no platform data\n");
  345. return -ENOENT;
  346. }
  347. platform_name = pdev->dev.platform_data;
  348. if (!strcmp(platform_name, "dm355_vpss"))
  349. oper_cfg.platform = DM355;
  350. else if (!strcmp(platform_name, "dm365_vpss"))
  351. oper_cfg.platform = DM365;
  352. else if (!strcmp(platform_name, "dm644x_vpss"))
  353. oper_cfg.platform = DM644X;
  354. else {
  355. dev_err(&pdev->dev, "vpss driver not supported on this platform\n");
  356. return -ENODEV;
  357. }
  358. dev_info(&pdev->dev, "%s vpss probed\n", platform_name);
  359. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  360. oper_cfg.vpss_regs_base0 = devm_ioremap_resource(&pdev->dev, res);
  361. if (IS_ERR(oper_cfg.vpss_regs_base0))
  362. return PTR_ERR(oper_cfg.vpss_regs_base0);
  363. if (oper_cfg.platform == DM355 || oper_cfg.platform == DM365) {
  364. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  365. oper_cfg.vpss_regs_base1 = devm_ioremap_resource(&pdev->dev,
  366. res);
  367. if (IS_ERR(oper_cfg.vpss_regs_base1))
  368. return PTR_ERR(oper_cfg.vpss_regs_base1);
  369. }
  370. if (oper_cfg.platform == DM355) {
  371. oper_cfg.hw_ops.enable_clock = dm355_enable_clock;
  372. oper_cfg.hw_ops.select_ccdc_source = dm355_select_ccdc_source;
  373. /* Setup vpss interrupts */
  374. bl_regw(DM355_VPSSBL_INTSEL_DEFAULT, DM355_VPSSBL_INTSEL);
  375. bl_regw(DM355_VPSSBL_EVTSEL_DEFAULT, DM355_VPSSBL_EVTSEL);
  376. } else if (oper_cfg.platform == DM365) {
  377. oper_cfg.hw_ops.enable_clock = dm365_enable_clock;
  378. oper_cfg.hw_ops.select_ccdc_source = dm365_select_ccdc_source;
  379. /* Setup vpss interrupts */
  380. isp5_write((isp5_read(DM365_ISP5_PCCR) |
  381. DM365_ISP5_PCCR_BL_CLK_ENABLE |
  382. DM365_ISP5_PCCR_ISIF_CLK_ENABLE |
  383. DM365_ISP5_PCCR_H3A_CLK_ENABLE |
  384. DM365_ISP5_PCCR_RSZ_CLK_ENABLE |
  385. DM365_ISP5_PCCR_IPIPE_CLK_ENABLE |
  386. DM365_ISP5_PCCR_IPIPEIF_CLK_ENABLE |
  387. DM365_ISP5_PCCR_RSV), DM365_ISP5_PCCR);
  388. isp5_write((isp5_read(DM365_ISP5_BCR) |
  389. DM365_ISP5_BCR_ISIF_OUT_ENABLE), DM365_ISP5_BCR);
  390. isp5_write(DM365_ISP5_INTSEL1_DEFAULT, DM365_ISP5_INTSEL1);
  391. isp5_write(DM365_ISP5_INTSEL2_DEFAULT, DM365_ISP5_INTSEL2);
  392. isp5_write(DM365_ISP5_INTSEL3_DEFAULT, DM365_ISP5_INTSEL3);
  393. } else
  394. oper_cfg.hw_ops.clear_wbl_overflow = dm644x_clear_wbl_overflow;
  395. pm_runtime_enable(&pdev->dev);
  396. pm_runtime_get(&pdev->dev);
  397. spin_lock_init(&oper_cfg.vpss_lock);
  398. dev_info(&pdev->dev, "%s vpss probe success\n", platform_name);
  399. return 0;
  400. }
  401. static int vpss_remove(struct platform_device *pdev)
  402. {
  403. pm_runtime_disable(&pdev->dev);
  404. return 0;
  405. }
  406. static int vpss_suspend(struct device *dev)
  407. {
  408. pm_runtime_put(dev);
  409. return 0;
  410. }
  411. static int vpss_resume(struct device *dev)
  412. {
  413. pm_runtime_get(dev);
  414. return 0;
  415. }
  416. static const struct dev_pm_ops vpss_pm_ops = {
  417. .suspend = vpss_suspend,
  418. .resume = vpss_resume,
  419. };
  420. static struct platform_driver vpss_driver = {
  421. .driver = {
  422. .name = "vpss",
  423. .pm = &vpss_pm_ops,
  424. },
  425. .remove = vpss_remove,
  426. .probe = vpss_probe,
  427. };
  428. static void vpss_exit(void)
  429. {
  430. platform_driver_unregister(&vpss_driver);
  431. iounmap(oper_cfg.vpss_regs_base2);
  432. release_mem_region(VPSS_CLK_CTRL, 4);
  433. }
  434. static int __init vpss_init(void)
  435. {
  436. int ret;
  437. if (!request_mem_region(VPSS_CLK_CTRL, 4, "vpss_clock_control"))
  438. return -EBUSY;
  439. oper_cfg.vpss_regs_base2 = ioremap(VPSS_CLK_CTRL, 4);
  440. if (unlikely(!oper_cfg.vpss_regs_base2)) {
  441. ret = -ENOMEM;
  442. goto err_ioremap;
  443. }
  444. writel(VPSS_CLK_CTRL_VENCCLKEN |
  445. VPSS_CLK_CTRL_DACCLKEN, oper_cfg.vpss_regs_base2);
  446. ret = platform_driver_register(&vpss_driver);
  447. if (ret)
  448. goto err_pd_register;
  449. return 0;
  450. err_pd_register:
  451. iounmap(oper_cfg.vpss_regs_base2);
  452. err_ioremap:
  453. release_mem_region(VPSS_CLK_CTRL, 4);
  454. return ret;
  455. }
  456. subsys_initcall(vpss_init);
  457. module_exit(vpss_exit);