vpss.c 13 KB

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