vpif_display.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. /*
  2. * vpif-display - VPIF display driver
  3. * Display driver for TI DaVinci VPIF
  4. *
  5. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  10. *
  11. * This program is distributed .as is. WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/mm.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/string.h>
  25. #include <linux/videodev2.h>
  26. #include <linux/wait.h>
  27. #include <linux/time.h>
  28. #include <linux/i2c.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/io.h>
  31. #include <linux/version.h>
  32. #include <linux/slab.h>
  33. #include <asm/irq.h>
  34. #include <asm/page.h>
  35. #include <media/adv7343.h>
  36. #include <media/v4l2-device.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <mach/dm646x.h>
  39. #include "vpif_display.h"
  40. #include "vpif.h"
  41. MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
  42. MODULE_LICENSE("GPL");
  43. #define DM646X_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
  44. #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
  45. #define vpif_dbg(level, debug, fmt, arg...) \
  46. v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
  47. static int debug = 1;
  48. static u32 ch2_numbuffers = 3;
  49. static u32 ch3_numbuffers = 3;
  50. static u32 ch2_bufsize = 1920 * 1080 * 2;
  51. static u32 ch3_bufsize = 720 * 576 * 2;
  52. module_param(debug, int, 0644);
  53. module_param(ch2_numbuffers, uint, S_IRUGO);
  54. module_param(ch3_numbuffers, uint, S_IRUGO);
  55. module_param(ch2_bufsize, uint, S_IRUGO);
  56. module_param(ch3_bufsize, uint, S_IRUGO);
  57. MODULE_PARM_DESC(debug, "Debug level 0-1");
  58. MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
  59. MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
  60. MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
  61. MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
  62. static struct vpif_config_params config_params = {
  63. .min_numbuffers = 3,
  64. .numbuffers[0] = 3,
  65. .numbuffers[1] = 3,
  66. .min_bufsize[0] = 720 * 480 * 2,
  67. .min_bufsize[1] = 720 * 480 * 2,
  68. .channel_bufsize[0] = 1920 * 1080 * 2,
  69. .channel_bufsize[1] = 720 * 576 * 2,
  70. };
  71. static struct vpif_device vpif_obj = { {NULL} };
  72. static struct device *vpif_dev;
  73. static const struct vpif_channel_config_params ch_params[] = {
  74. {
  75. "NTSC", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
  76. 286, 525, 525, 0, 1, 0, V4L2_STD_525_60,
  77. },
  78. {
  79. "PAL", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
  80. 336, 624, 625, 0, 1, 0, V4L2_STD_625_50,
  81. },
  82. };
  83. /*
  84. * vpif_uservirt_to_phys: This function is used to convert user
  85. * space virtual address to physical address.
  86. */
  87. static u32 vpif_uservirt_to_phys(u32 virtp)
  88. {
  89. struct mm_struct *mm = current->mm;
  90. unsigned long physp = 0;
  91. struct vm_area_struct *vma;
  92. vma = find_vma(mm, virtp);
  93. /* For kernel direct-mapped memory, take the easy way */
  94. if (virtp >= PAGE_OFFSET) {
  95. physp = virt_to_phys((void *)virtp);
  96. } else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
  97. /* this will catch, kernel-allocated, mmaped-to-usermode addr */
  98. physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
  99. } else {
  100. /* otherwise, use get_user_pages() for general userland pages */
  101. int res, nr_pages = 1;
  102. struct page *pages;
  103. down_read(&current->mm->mmap_sem);
  104. res = get_user_pages(current, current->mm,
  105. virtp, nr_pages, 1, 0, &pages, NULL);
  106. up_read(&current->mm->mmap_sem);
  107. if (res == nr_pages) {
  108. physp = __pa(page_address(&pages[0]) +
  109. (virtp & ~PAGE_MASK));
  110. } else {
  111. vpif_err("get_user_pages failed\n");
  112. return 0;
  113. }
  114. }
  115. return physp;
  116. }
  117. /*
  118. * buffer_prepare: This is the callback function called from videobuf_qbuf()
  119. * function the buffer is prepared and user space virtual address is converted
  120. * into physical address
  121. */
  122. static int vpif_buffer_prepare(struct videobuf_queue *q,
  123. struct videobuf_buffer *vb,
  124. enum v4l2_field field)
  125. {
  126. struct vpif_fh *fh = q->priv_data;
  127. struct common_obj *common;
  128. unsigned long addr;
  129. common = &fh->channel->common[VPIF_VIDEO_INDEX];
  130. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  131. vb->width = common->width;
  132. vb->height = common->height;
  133. vb->size = vb->width * vb->height;
  134. vb->field = field;
  135. }
  136. vb->state = VIDEOBUF_PREPARED;
  137. /* if user pointer memory mechanism is used, get the physical
  138. * address of the buffer */
  139. if (V4L2_MEMORY_USERPTR == common->memory) {
  140. if (!vb->baddr) {
  141. vpif_err("buffer_address is 0\n");
  142. return -EINVAL;
  143. }
  144. vb->boff = vpif_uservirt_to_phys(vb->baddr);
  145. if (!ISALIGNED(vb->boff))
  146. goto buf_align_exit;
  147. }
  148. addr = vb->boff;
  149. if (q->streaming && (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
  150. if (!ISALIGNED(addr + common->ytop_off) ||
  151. !ISALIGNED(addr + common->ybtm_off) ||
  152. !ISALIGNED(addr + common->ctop_off) ||
  153. !ISALIGNED(addr + common->cbtm_off))
  154. goto buf_align_exit;
  155. }
  156. return 0;
  157. buf_align_exit:
  158. vpif_err("buffer offset not aligned to 8 bytes\n");
  159. return -EINVAL;
  160. }
  161. /*
  162. * vpif_buffer_setup: This function allocates memory for the buffers
  163. */
  164. static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  165. unsigned int *size)
  166. {
  167. struct vpif_fh *fh = q->priv_data;
  168. struct channel_obj *ch = fh->channel;
  169. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  170. if (V4L2_MEMORY_MMAP != common->memory)
  171. return 0;
  172. *size = config_params.channel_bufsize[ch->channel_id];
  173. if (*count < config_params.min_numbuffers)
  174. *count = config_params.min_numbuffers;
  175. return 0;
  176. }
  177. /*
  178. * vpif_buffer_queue: This function adds the buffer to DMA queue
  179. */
  180. static void vpif_buffer_queue(struct videobuf_queue *q,
  181. struct videobuf_buffer *vb)
  182. {
  183. struct vpif_fh *fh = q->priv_data;
  184. struct common_obj *common;
  185. common = &fh->channel->common[VPIF_VIDEO_INDEX];
  186. /* add the buffer to the DMA queue */
  187. list_add_tail(&vb->queue, &common->dma_queue);
  188. vb->state = VIDEOBUF_QUEUED;
  189. }
  190. /*
  191. * vpif_buffer_release: This function is called from the videobuf layer to
  192. * free memory allocated to the buffers
  193. */
  194. static void vpif_buffer_release(struct videobuf_queue *q,
  195. struct videobuf_buffer *vb)
  196. {
  197. struct vpif_fh *fh = q->priv_data;
  198. struct channel_obj *ch = fh->channel;
  199. struct common_obj *common;
  200. unsigned int buf_size = 0;
  201. common = &ch->common[VPIF_VIDEO_INDEX];
  202. videobuf_dma_contig_free(q, vb);
  203. vb->state = VIDEOBUF_NEEDS_INIT;
  204. if (V4L2_MEMORY_MMAP != common->memory)
  205. return;
  206. buf_size = config_params.channel_bufsize[ch->channel_id];
  207. }
  208. static struct videobuf_queue_ops video_qops = {
  209. .buf_setup = vpif_buffer_setup,
  210. .buf_prepare = vpif_buffer_prepare,
  211. .buf_queue = vpif_buffer_queue,
  212. .buf_release = vpif_buffer_release,
  213. };
  214. static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
  215. static void process_progressive_mode(struct common_obj *common)
  216. {
  217. unsigned long addr = 0;
  218. /* Get the next buffer from buffer queue */
  219. common->next_frm = list_entry(common->dma_queue.next,
  220. struct videobuf_buffer, queue);
  221. /* Remove that buffer from the buffer queue */
  222. list_del(&common->next_frm->queue);
  223. /* Mark status of the buffer as active */
  224. common->next_frm->state = VIDEOBUF_ACTIVE;
  225. /* Set top and bottom field addrs in VPIF registers */
  226. addr = videobuf_to_dma_contig(common->next_frm);
  227. common->set_addr(addr + common->ytop_off,
  228. addr + common->ybtm_off,
  229. addr + common->ctop_off,
  230. addr + common->cbtm_off);
  231. }
  232. static void process_interlaced_mode(int fid, struct common_obj *common)
  233. {
  234. /* device field id and local field id are in sync */
  235. /* If this is even field */
  236. if (0 == fid) {
  237. if (common->cur_frm == common->next_frm)
  238. return;
  239. /* one frame is displayed If next frame is
  240. * available, release cur_frm and move on */
  241. /* Copy frame display time */
  242. do_gettimeofday(&common->cur_frm->ts);
  243. /* Change status of the cur_frm */
  244. common->cur_frm->state = VIDEOBUF_DONE;
  245. /* unlock semaphore on cur_frm */
  246. wake_up_interruptible(&common->cur_frm->done);
  247. /* Make cur_frm pointing to next_frm */
  248. common->cur_frm = common->next_frm;
  249. } else if (1 == fid) { /* odd field */
  250. if (list_empty(&common->dma_queue)
  251. || (common->cur_frm != common->next_frm)) {
  252. return;
  253. }
  254. /* one field is displayed configure the next
  255. * frame if it is available else hold on current
  256. * frame */
  257. /* Get next from the buffer queue */
  258. process_progressive_mode(common);
  259. }
  260. }
  261. /*
  262. * vpif_channel_isr: It changes status of the displayed buffer, takes next
  263. * buffer from the queue and sets its address in VPIF registers
  264. */
  265. static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
  266. {
  267. struct vpif_device *dev = &vpif_obj;
  268. struct channel_obj *ch;
  269. struct common_obj *common;
  270. enum v4l2_field field;
  271. int fid = -1, i;
  272. int channel_id = 0;
  273. channel_id = *(int *)(dev_id);
  274. ch = dev->dev[channel_id];
  275. field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
  276. for (i = 0; i < VPIF_NUMOBJECTS; i++) {
  277. common = &ch->common[i];
  278. /* If streaming is started in this channel */
  279. if (0 == common->started)
  280. continue;
  281. if (1 == ch->vpifparams.std_info.frm_fmt) {
  282. if (list_empty(&common->dma_queue))
  283. continue;
  284. /* Progressive mode */
  285. if (!channel_first_int[i][channel_id]) {
  286. /* Mark status of the cur_frm to
  287. * done and unlock semaphore on it */
  288. do_gettimeofday(&common->cur_frm->ts);
  289. common->cur_frm->state = VIDEOBUF_DONE;
  290. wake_up_interruptible(&common->cur_frm->done);
  291. /* Make cur_frm pointing to next_frm */
  292. common->cur_frm = common->next_frm;
  293. }
  294. channel_first_int[i][channel_id] = 0;
  295. process_progressive_mode(common);
  296. } else {
  297. /* Interlaced mode */
  298. /* If it is first interrupt, ignore it */
  299. if (channel_first_int[i][channel_id]) {
  300. channel_first_int[i][channel_id] = 0;
  301. continue;
  302. }
  303. if (0 == i) {
  304. ch->field_id ^= 1;
  305. /* Get field id from VPIF registers */
  306. fid = vpif_channel_getfid(ch->channel_id + 2);
  307. /* If fid does not match with stored field id */
  308. if (fid != ch->field_id) {
  309. /* Make them in sync */
  310. if (0 == fid)
  311. ch->field_id = fid;
  312. return IRQ_HANDLED;
  313. }
  314. }
  315. process_interlaced_mode(fid, common);
  316. }
  317. }
  318. return IRQ_HANDLED;
  319. }
  320. static int vpif_get_std_info(struct channel_obj *ch)
  321. {
  322. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  323. struct video_obj *vid_ch = &ch->video;
  324. struct vpif_params *vpifparams = &ch->vpifparams;
  325. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  326. const struct vpif_channel_config_params *config;
  327. int index;
  328. std_info->stdid = vid_ch->stdid;
  329. if (!std_info->stdid)
  330. return -1;
  331. for (index = 0; index < ARRAY_SIZE(ch_params); index++) {
  332. config = &ch_params[index];
  333. if (config->stdid & std_info->stdid) {
  334. memcpy(std_info, config, sizeof(*config));
  335. break;
  336. }
  337. }
  338. if (index == ARRAY_SIZE(ch_params))
  339. return -1;
  340. common->fmt.fmt.pix.width = std_info->width;
  341. common->fmt.fmt.pix.height = std_info->height;
  342. vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
  343. common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
  344. /* Set height and width paramateres */
  345. ch->common[VPIF_VIDEO_INDEX].height = std_info->height;
  346. ch->common[VPIF_VIDEO_INDEX].width = std_info->width;
  347. return 0;
  348. }
  349. /*
  350. * vpif_calculate_offsets: This function calculates buffers offset for Y and C
  351. * in the top and bottom field
  352. */
  353. static void vpif_calculate_offsets(struct channel_obj *ch)
  354. {
  355. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  356. struct vpif_params *vpifparams = &ch->vpifparams;
  357. enum v4l2_field field = common->fmt.fmt.pix.field;
  358. struct video_obj *vid_ch = &ch->video;
  359. unsigned int hpitch, vpitch, sizeimage;
  360. if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
  361. if (ch->vpifparams.std_info.frm_fmt)
  362. vid_ch->buf_field = V4L2_FIELD_NONE;
  363. else
  364. vid_ch->buf_field = V4L2_FIELD_INTERLACED;
  365. } else {
  366. vid_ch->buf_field = common->fmt.fmt.pix.field;
  367. }
  368. if (V4L2_MEMORY_USERPTR == common->memory)
  369. sizeimage = common->fmt.fmt.pix.sizeimage;
  370. else
  371. sizeimage = config_params.channel_bufsize[ch->channel_id];
  372. hpitch = common->fmt.fmt.pix.bytesperline;
  373. vpitch = sizeimage / (hpitch * 2);
  374. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  375. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  376. common->ytop_off = 0;
  377. common->ybtm_off = hpitch;
  378. common->ctop_off = sizeimage / 2;
  379. common->cbtm_off = sizeimage / 2 + hpitch;
  380. } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
  381. common->ytop_off = 0;
  382. common->ybtm_off = sizeimage / 4;
  383. common->ctop_off = sizeimage / 2;
  384. common->cbtm_off = common->ctop_off + sizeimage / 4;
  385. } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
  386. common->ybtm_off = 0;
  387. common->ytop_off = sizeimage / 4;
  388. common->cbtm_off = sizeimage / 2;
  389. common->ctop_off = common->cbtm_off + sizeimage / 4;
  390. }
  391. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  392. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  393. vpifparams->video_params.storage_mode = 1;
  394. } else {
  395. vpifparams->video_params.storage_mode = 0;
  396. }
  397. if (ch->vpifparams.std_info.frm_fmt == 1) {
  398. vpifparams->video_params.hpitch =
  399. common->fmt.fmt.pix.bytesperline;
  400. } else {
  401. if ((field == V4L2_FIELD_ANY) ||
  402. (field == V4L2_FIELD_INTERLACED))
  403. vpifparams->video_params.hpitch =
  404. common->fmt.fmt.pix.bytesperline * 2;
  405. else
  406. vpifparams->video_params.hpitch =
  407. common->fmt.fmt.pix.bytesperline;
  408. }
  409. ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
  410. }
  411. static void vpif_config_format(struct channel_obj *ch)
  412. {
  413. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  414. common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
  415. if (config_params.numbuffers[ch->channel_id] == 0)
  416. common->memory = V4L2_MEMORY_USERPTR;
  417. else
  418. common->memory = V4L2_MEMORY_MMAP;
  419. common->fmt.fmt.pix.sizeimage =
  420. config_params.channel_bufsize[ch->channel_id];
  421. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  422. common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  423. }
  424. static int vpif_check_format(struct channel_obj *ch,
  425. struct v4l2_pix_format *pixfmt)
  426. {
  427. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  428. enum v4l2_field field = pixfmt->field;
  429. u32 sizeimage, hpitch, vpitch;
  430. if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
  431. goto invalid_fmt_exit;
  432. if (!(VPIF_VALID_FIELD(field)))
  433. goto invalid_fmt_exit;
  434. if (pixfmt->bytesperline <= 0)
  435. goto invalid_pitch_exit;
  436. if (V4L2_MEMORY_USERPTR == common->memory)
  437. sizeimage = pixfmt->sizeimage;
  438. else
  439. sizeimage = config_params.channel_bufsize[ch->channel_id];
  440. if (vpif_get_std_info(ch)) {
  441. vpif_err("Error getting the standard info\n");
  442. return -EINVAL;
  443. }
  444. hpitch = pixfmt->bytesperline;
  445. vpitch = sizeimage / (hpitch * 2);
  446. /* Check for valid value of pitch */
  447. if ((hpitch < ch->vpifparams.std_info.width) ||
  448. (vpitch < ch->vpifparams.std_info.height))
  449. goto invalid_pitch_exit;
  450. /* Check for 8 byte alignment */
  451. if (!ISALIGNED(hpitch)) {
  452. vpif_err("invalid pitch alignment\n");
  453. return -EINVAL;
  454. }
  455. pixfmt->width = common->fmt.fmt.pix.width;
  456. pixfmt->height = common->fmt.fmt.pix.height;
  457. return 0;
  458. invalid_fmt_exit:
  459. vpif_err("invalid field format\n");
  460. return -EINVAL;
  461. invalid_pitch_exit:
  462. vpif_err("invalid pitch\n");
  463. return -EINVAL;
  464. }
  465. static void vpif_config_addr(struct channel_obj *ch, int muxmode)
  466. {
  467. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  468. if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
  469. common->set_addr = ch3_set_videobuf_addr;
  470. } else {
  471. if (2 == muxmode)
  472. common->set_addr = ch2_set_videobuf_addr_yc_nmux;
  473. else
  474. common->set_addr = ch2_set_videobuf_addr;
  475. }
  476. }
  477. /*
  478. * vpif_mmap: It is used to map kernel space buffers into user spaces
  479. */
  480. static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
  481. {
  482. struct vpif_fh *fh = filep->private_data;
  483. struct common_obj *common = &fh->channel->common[VPIF_VIDEO_INDEX];
  484. return videobuf_mmap_mapper(&common->buffer_queue, vma);
  485. }
  486. /*
  487. * vpif_poll: It is used for select/poll system call
  488. */
  489. static unsigned int vpif_poll(struct file *filep, poll_table *wait)
  490. {
  491. struct vpif_fh *fh = filep->private_data;
  492. struct channel_obj *ch = fh->channel;
  493. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  494. if (common->started)
  495. return videobuf_poll_stream(filep, &common->buffer_queue, wait);
  496. return 0;
  497. }
  498. /*
  499. * vpif_open: It creates object of file handle structure and stores it in
  500. * private_data member of filepointer
  501. */
  502. static int vpif_open(struct file *filep)
  503. {
  504. struct video_device *vdev = video_devdata(filep);
  505. struct channel_obj *ch = NULL;
  506. struct vpif_fh *fh = NULL;
  507. ch = video_get_drvdata(vdev);
  508. /* Allocate memory for the file handle object */
  509. fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
  510. if (fh == NULL) {
  511. vpif_err("unable to allocate memory for file handle object\n");
  512. return -ENOMEM;
  513. }
  514. /* store pointer to fh in private_data member of filep */
  515. filep->private_data = fh;
  516. fh->channel = ch;
  517. fh->initialized = 0;
  518. if (!ch->initialized) {
  519. fh->initialized = 1;
  520. ch->initialized = 1;
  521. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  522. }
  523. /* Increment channel usrs counter */
  524. atomic_inc(&ch->usrs);
  525. /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
  526. fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
  527. /* Initialize priority of this instance to default priority */
  528. fh->prio = V4L2_PRIORITY_UNSET;
  529. v4l2_prio_open(&ch->prio, &fh->prio);
  530. return 0;
  531. }
  532. /*
  533. * vpif_release: This function deletes buffer queue, frees the buffers and
  534. * the vpif file handle
  535. */
  536. static int vpif_release(struct file *filep)
  537. {
  538. struct vpif_fh *fh = filep->private_data;
  539. struct channel_obj *ch = fh->channel;
  540. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  541. if (mutex_lock_interruptible(&common->lock))
  542. return -ERESTARTSYS;
  543. /* if this instance is doing IO */
  544. if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
  545. /* Reset io_usrs member of channel object */
  546. common->io_usrs = 0;
  547. /* Disable channel */
  548. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  549. enable_channel2(0);
  550. channel2_intr_enable(0);
  551. }
  552. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  553. (2 == common->started)) {
  554. enable_channel3(0);
  555. channel3_intr_enable(0);
  556. }
  557. common->started = 0;
  558. /* Free buffers allocated */
  559. videobuf_queue_cancel(&common->buffer_queue);
  560. videobuf_mmap_free(&common->buffer_queue);
  561. common->numbuffers =
  562. config_params.numbuffers[ch->channel_id];
  563. }
  564. mutex_unlock(&common->lock);
  565. /* Decrement channel usrs counter */
  566. atomic_dec(&ch->usrs);
  567. /* If this file handle has initialize encoder device, reset it */
  568. if (fh->initialized)
  569. ch->initialized = 0;
  570. /* Close the priority */
  571. v4l2_prio_close(&ch->prio, fh->prio);
  572. filep->private_data = NULL;
  573. fh->initialized = 0;
  574. kfree(fh);
  575. return 0;
  576. }
  577. /* functions implementing ioctls */
  578. static int vpif_querycap(struct file *file, void *priv,
  579. struct v4l2_capability *cap)
  580. {
  581. struct vpif_display_config *config = vpif_dev->platform_data;
  582. cap->version = VPIF_DISPLAY_VERSION_CODE;
  583. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  584. strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
  585. strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
  586. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  587. return 0;
  588. }
  589. static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
  590. struct v4l2_fmtdesc *fmt)
  591. {
  592. if (fmt->index != 0) {
  593. vpif_err("Invalid format index\n");
  594. return -EINVAL;
  595. }
  596. /* Fill in the information about format */
  597. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  598. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  599. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  600. return 0;
  601. }
  602. static int vpif_g_fmt_vid_out(struct file *file, void *priv,
  603. struct v4l2_format *fmt)
  604. {
  605. struct vpif_fh *fh = priv;
  606. struct channel_obj *ch = fh->channel;
  607. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  608. /* Check the validity of the buffer type */
  609. if (common->fmt.type != fmt->type)
  610. return -EINVAL;
  611. /* Fill in the information about format */
  612. if (mutex_lock_interruptible(&common->lock))
  613. return -ERESTARTSYS;
  614. if (vpif_get_std_info(ch)) {
  615. vpif_err("Error getting the standard info\n");
  616. return -EINVAL;
  617. }
  618. *fmt = common->fmt;
  619. mutex_unlock(&common->lock);
  620. return 0;
  621. }
  622. static int vpif_s_fmt_vid_out(struct file *file, void *priv,
  623. struct v4l2_format *fmt)
  624. {
  625. struct vpif_fh *fh = priv;
  626. struct v4l2_pix_format *pixfmt;
  627. struct channel_obj *ch = fh->channel;
  628. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  629. int ret = 0;
  630. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  631. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  632. if (!fh->initialized) {
  633. vpif_dbg(1, debug, "Channel Busy\n");
  634. return -EBUSY;
  635. }
  636. /* Check for the priority */
  637. ret = v4l2_prio_check(&ch->prio, fh->prio);
  638. if (0 != ret)
  639. return ret;
  640. fh->initialized = 1;
  641. }
  642. if (common->started) {
  643. vpif_dbg(1, debug, "Streaming in progress\n");
  644. return -EBUSY;
  645. }
  646. pixfmt = &fmt->fmt.pix;
  647. /* Check for valid field format */
  648. ret = vpif_check_format(ch, pixfmt);
  649. if (ret)
  650. return ret;
  651. /* store the pix format in the channel object */
  652. common->fmt.fmt.pix = *pixfmt;
  653. /* store the format in the channel object */
  654. if (mutex_lock_interruptible(&common->lock))
  655. return -ERESTARTSYS;
  656. common->fmt = *fmt;
  657. mutex_unlock(&common->lock);
  658. return 0;
  659. }
  660. static int vpif_try_fmt_vid_out(struct file *file, void *priv,
  661. struct v4l2_format *fmt)
  662. {
  663. struct vpif_fh *fh = priv;
  664. struct channel_obj *ch = fh->channel;
  665. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  666. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  667. int ret = 0;
  668. ret = vpif_check_format(ch, pixfmt);
  669. if (ret) {
  670. *pixfmt = common->fmt.fmt.pix;
  671. pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
  672. }
  673. return ret;
  674. }
  675. static int vpif_reqbufs(struct file *file, void *priv,
  676. struct v4l2_requestbuffers *reqbuf)
  677. {
  678. struct vpif_fh *fh = priv;
  679. struct channel_obj *ch = fh->channel;
  680. struct common_obj *common;
  681. enum v4l2_field field;
  682. u8 index = 0;
  683. int ret = 0;
  684. /* This file handle has not initialized the channel,
  685. It is not allowed to do settings */
  686. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  687. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  688. if (!fh->initialized) {
  689. vpif_err("Channel Busy\n");
  690. return -EBUSY;
  691. }
  692. }
  693. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
  694. return -EINVAL;
  695. index = VPIF_VIDEO_INDEX;
  696. common = &ch->common[index];
  697. if (mutex_lock_interruptible(&common->lock))
  698. return -ERESTARTSYS;
  699. if (common->fmt.type != reqbuf->type) {
  700. ret = -EINVAL;
  701. goto reqbuf_exit;
  702. }
  703. if (0 != common->io_usrs) {
  704. ret = -EBUSY;
  705. goto reqbuf_exit;
  706. }
  707. if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  708. if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
  709. field = V4L2_FIELD_INTERLACED;
  710. else
  711. field = common->fmt.fmt.pix.field;
  712. } else {
  713. field = V4L2_VBI_INTERLACED;
  714. }
  715. /* Initialize videobuf queue as per the buffer type */
  716. videobuf_queue_dma_contig_init(&common->buffer_queue,
  717. &video_qops, NULL,
  718. &common->irqlock,
  719. reqbuf->type, field,
  720. sizeof(struct videobuf_buffer), fh,
  721. NULL);
  722. /* Set io allowed member of file handle to TRUE */
  723. fh->io_allowed[index] = 1;
  724. /* Increment io usrs member of channel object to 1 */
  725. common->io_usrs = 1;
  726. /* Store type of memory requested in channel object */
  727. common->memory = reqbuf->memory;
  728. INIT_LIST_HEAD(&common->dma_queue);
  729. /* Allocate buffers */
  730. ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
  731. reqbuf_exit:
  732. mutex_unlock(&common->lock);
  733. return ret;
  734. }
  735. static int vpif_querybuf(struct file *file, void *priv,
  736. struct v4l2_buffer *tbuf)
  737. {
  738. struct vpif_fh *fh = priv;
  739. struct channel_obj *ch = fh->channel;
  740. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  741. if (common->fmt.type != tbuf->type)
  742. return -EINVAL;
  743. return videobuf_querybuf(&common->buffer_queue, tbuf);
  744. }
  745. static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  746. {
  747. struct vpif_fh *fh = priv;
  748. struct channel_obj *ch = fh->channel;
  749. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  750. struct v4l2_buffer tbuf = *buf;
  751. struct videobuf_buffer *buf1;
  752. unsigned long addr = 0;
  753. unsigned long flags;
  754. int ret = 0;
  755. if (common->fmt.type != tbuf.type)
  756. return -EINVAL;
  757. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  758. vpif_err("fh->io_allowed\n");
  759. return -EACCES;
  760. }
  761. if (!(list_empty(&common->dma_queue)) ||
  762. (common->cur_frm != common->next_frm) ||
  763. !(common->started) ||
  764. (common->started && (0 == ch->field_id)))
  765. return videobuf_qbuf(&common->buffer_queue, buf);
  766. /* bufferqueue is empty store buffer address in VPIF registers */
  767. mutex_lock(&common->buffer_queue.vb_lock);
  768. buf1 = common->buffer_queue.bufs[tbuf.index];
  769. if (buf1->memory != tbuf.memory) {
  770. vpif_err("invalid buffer type\n");
  771. goto qbuf_exit;
  772. }
  773. if ((buf1->state == VIDEOBUF_QUEUED) ||
  774. (buf1->state == VIDEOBUF_ACTIVE)) {
  775. vpif_err("invalid state\n");
  776. goto qbuf_exit;
  777. }
  778. switch (buf1->memory) {
  779. case V4L2_MEMORY_MMAP:
  780. if (buf1->baddr == 0)
  781. goto qbuf_exit;
  782. break;
  783. case V4L2_MEMORY_USERPTR:
  784. if (tbuf.length < buf1->bsize)
  785. goto qbuf_exit;
  786. if ((VIDEOBUF_NEEDS_INIT != buf1->state)
  787. && (buf1->baddr != tbuf.m.userptr)) {
  788. vpif_buffer_release(&common->buffer_queue, buf1);
  789. buf1->baddr = tbuf.m.userptr;
  790. }
  791. break;
  792. default:
  793. goto qbuf_exit;
  794. }
  795. local_irq_save(flags);
  796. ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
  797. common->buffer_queue.field);
  798. if (ret < 0) {
  799. local_irq_restore(flags);
  800. goto qbuf_exit;
  801. }
  802. buf1->state = VIDEOBUF_ACTIVE;
  803. addr = buf1->boff;
  804. common->next_frm = buf1;
  805. if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
  806. common->set_addr((addr + common->ytop_off),
  807. (addr + common->ybtm_off),
  808. (addr + common->ctop_off),
  809. (addr + common->cbtm_off));
  810. }
  811. local_irq_restore(flags);
  812. list_add_tail(&buf1->stream, &common->buffer_queue.stream);
  813. mutex_unlock(&common->buffer_queue.vb_lock);
  814. return 0;
  815. qbuf_exit:
  816. mutex_unlock(&common->buffer_queue.vb_lock);
  817. return -EINVAL;
  818. }
  819. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  820. {
  821. struct vpif_fh *fh = priv;
  822. struct channel_obj *ch = fh->channel;
  823. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  824. int ret = 0;
  825. if (!(*std_id & DM646X_V4L2_STD))
  826. return -EINVAL;
  827. if (common->started) {
  828. vpif_err("streaming in progress\n");
  829. return -EBUSY;
  830. }
  831. /* Call encoder subdevice function to set the standard */
  832. if (mutex_lock_interruptible(&common->lock))
  833. return -ERESTARTSYS;
  834. ch->video.stdid = *std_id;
  835. /* Get the information about the standard */
  836. if (vpif_get_std_info(ch)) {
  837. vpif_err("Error getting the standard info\n");
  838. return -EINVAL;
  839. }
  840. if ((ch->vpifparams.std_info.width *
  841. ch->vpifparams.std_info.height * 2) >
  842. config_params.channel_bufsize[ch->channel_id]) {
  843. vpif_err("invalid std for this size\n");
  844. ret = -EINVAL;
  845. goto s_std_exit;
  846. }
  847. common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
  848. /* Configure the default format information */
  849. vpif_config_format(ch);
  850. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  851. s_std_output, *std_id);
  852. if (ret < 0) {
  853. vpif_err("Failed to set output standard\n");
  854. goto s_std_exit;
  855. }
  856. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
  857. s_std, *std_id);
  858. if (ret < 0)
  859. vpif_err("Failed to set standard for sub devices\n");
  860. s_std_exit:
  861. mutex_unlock(&common->lock);
  862. return ret;
  863. }
  864. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  865. {
  866. struct vpif_fh *fh = priv;
  867. struct channel_obj *ch = fh->channel;
  868. *std = ch->video.stdid;
  869. return 0;
  870. }
  871. static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  872. {
  873. struct vpif_fh *fh = priv;
  874. struct channel_obj *ch = fh->channel;
  875. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  876. return videobuf_dqbuf(&common->buffer_queue, p,
  877. (file->f_flags & O_NONBLOCK));
  878. }
  879. static int vpif_streamon(struct file *file, void *priv,
  880. enum v4l2_buf_type buftype)
  881. {
  882. struct vpif_fh *fh = priv;
  883. struct channel_obj *ch = fh->channel;
  884. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  885. struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
  886. struct vpif_params *vpif = &ch->vpifparams;
  887. struct vpif_display_config *vpif_config_data =
  888. vpif_dev->platform_data;
  889. unsigned long addr = 0;
  890. int ret = 0;
  891. if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  892. vpif_err("buffer type not supported\n");
  893. return -EINVAL;
  894. }
  895. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  896. vpif_err("fh->io_allowed\n");
  897. return -EACCES;
  898. }
  899. /* If Streaming is already started, return error */
  900. if (common->started) {
  901. vpif_err("channel->started\n");
  902. return -EBUSY;
  903. }
  904. if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
  905. && oth_ch->common[VPIF_VIDEO_INDEX].started &&
  906. ch->vpifparams.std_info.ycmux_mode == 0)
  907. || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
  908. && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
  909. vpif_err("other channel is using\n");
  910. return -EBUSY;
  911. }
  912. ret = vpif_check_format(ch, &common->fmt.fmt.pix);
  913. if (ret < 0)
  914. return ret;
  915. /* Call videobuf_streamon to start streaming in videobuf */
  916. ret = videobuf_streamon(&common->buffer_queue);
  917. if (ret < 0) {
  918. vpif_err("videobuf_streamon\n");
  919. return ret;
  920. }
  921. if (mutex_lock_interruptible(&common->lock))
  922. return -ERESTARTSYS;
  923. /* If buffer queue is empty, return error */
  924. if (list_empty(&common->dma_queue)) {
  925. vpif_err("buffer queue is empty\n");
  926. ret = -EIO;
  927. goto streamon_exit;
  928. }
  929. /* Get the next frame from the buffer queue */
  930. common->next_frm = common->cur_frm =
  931. list_entry(common->dma_queue.next,
  932. struct videobuf_buffer, queue);
  933. list_del(&common->cur_frm->queue);
  934. /* Mark state of the current frame to active */
  935. common->cur_frm->state = VIDEOBUF_ACTIVE;
  936. /* Initialize field_id and started member */
  937. ch->field_id = 0;
  938. common->started = 1;
  939. if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  940. addr = common->cur_frm->boff;
  941. /* Calculate the offset for Y and C data in the buffer */
  942. vpif_calculate_offsets(ch);
  943. if ((ch->vpifparams.std_info.frm_fmt &&
  944. ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
  945. && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
  946. || (!ch->vpifparams.std_info.frm_fmt
  947. && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
  948. vpif_err("conflict in field format and std format\n");
  949. ret = -EINVAL;
  950. goto streamon_exit;
  951. }
  952. /* clock settings */
  953. ret =
  954. vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
  955. ch->vpifparams.std_info.hd_sd);
  956. if (ret < 0) {
  957. vpif_err("can't set clock\n");
  958. goto streamon_exit;
  959. }
  960. /* set the parameters and addresses */
  961. ret = vpif_set_video_params(vpif, ch->channel_id + 2);
  962. if (ret < 0)
  963. goto streamon_exit;
  964. common->started = ret;
  965. vpif_config_addr(ch, ret);
  966. common->set_addr((addr + common->ytop_off),
  967. (addr + common->ybtm_off),
  968. (addr + common->ctop_off),
  969. (addr + common->cbtm_off));
  970. /* Set interrupt for both the fields in VPIF
  971. Register enable channel in VPIF register */
  972. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  973. channel2_intr_assert();
  974. channel2_intr_enable(1);
  975. enable_channel2(1);
  976. }
  977. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
  978. || (common->started == 2)) {
  979. channel3_intr_assert();
  980. channel3_intr_enable(1);
  981. enable_channel3(1);
  982. }
  983. channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
  984. }
  985. streamon_exit:
  986. mutex_unlock(&common->lock);
  987. return ret;
  988. }
  989. static int vpif_streamoff(struct file *file, void *priv,
  990. enum v4l2_buf_type buftype)
  991. {
  992. struct vpif_fh *fh = priv;
  993. struct channel_obj *ch = fh->channel;
  994. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  995. if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  996. vpif_err("buffer type not supported\n");
  997. return -EINVAL;
  998. }
  999. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  1000. vpif_err("fh->io_allowed\n");
  1001. return -EACCES;
  1002. }
  1003. if (!common->started) {
  1004. vpif_err("channel->started\n");
  1005. return -EINVAL;
  1006. }
  1007. if (mutex_lock_interruptible(&common->lock))
  1008. return -ERESTARTSYS;
  1009. if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1010. /* disable channel */
  1011. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  1012. enable_channel2(0);
  1013. channel2_intr_enable(0);
  1014. }
  1015. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  1016. (2 == common->started)) {
  1017. enable_channel3(0);
  1018. channel3_intr_enable(0);
  1019. }
  1020. }
  1021. common->started = 0;
  1022. mutex_unlock(&common->lock);
  1023. return videobuf_streamoff(&common->buffer_queue);
  1024. }
  1025. static int vpif_cropcap(struct file *file, void *priv,
  1026. struct v4l2_cropcap *crop)
  1027. {
  1028. struct vpif_fh *fh = priv;
  1029. struct channel_obj *ch = fh->channel;
  1030. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1031. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
  1032. return -EINVAL;
  1033. crop->bounds.left = crop->bounds.top = 0;
  1034. crop->defrect.left = crop->defrect.top = 0;
  1035. crop->defrect.height = crop->bounds.height = common->height;
  1036. crop->defrect.width = crop->bounds.width = common->width;
  1037. return 0;
  1038. }
  1039. static int vpif_enum_output(struct file *file, void *fh,
  1040. struct v4l2_output *output)
  1041. {
  1042. struct vpif_display_config *config = vpif_dev->platform_data;
  1043. if (output->index >= config->output_count) {
  1044. vpif_dbg(1, debug, "Invalid output index\n");
  1045. return -EINVAL;
  1046. }
  1047. strcpy(output->name, config->output[output->index]);
  1048. output->type = V4L2_OUTPUT_TYPE_ANALOG;
  1049. output->std = DM646X_V4L2_STD;
  1050. return 0;
  1051. }
  1052. static int vpif_s_output(struct file *file, void *priv, unsigned int i)
  1053. {
  1054. struct vpif_fh *fh = priv;
  1055. struct channel_obj *ch = fh->channel;
  1056. struct video_obj *vid_ch = &ch->video;
  1057. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1058. int ret = 0;
  1059. if (mutex_lock_interruptible(&common->lock))
  1060. return -ERESTARTSYS;
  1061. if (common->started) {
  1062. vpif_err("Streaming in progress\n");
  1063. ret = -EBUSY;
  1064. goto s_output_exit;
  1065. }
  1066. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  1067. s_routing, 0, i, 0);
  1068. if (ret < 0)
  1069. vpif_err("Failed to set output standard\n");
  1070. vid_ch->output_id = i;
  1071. s_output_exit:
  1072. mutex_unlock(&common->lock);
  1073. return ret;
  1074. }
  1075. static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
  1076. {
  1077. struct vpif_fh *fh = priv;
  1078. struct channel_obj *ch = fh->channel;
  1079. struct video_obj *vid_ch = &ch->video;
  1080. *i = vid_ch->output_id;
  1081. return 0;
  1082. }
  1083. static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
  1084. {
  1085. struct vpif_fh *fh = priv;
  1086. struct channel_obj *ch = fh->channel;
  1087. *p = v4l2_prio_max(&ch->prio);
  1088. return 0;
  1089. }
  1090. static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
  1091. {
  1092. struct vpif_fh *fh = priv;
  1093. struct channel_obj *ch = fh->channel;
  1094. return v4l2_prio_change(&ch->prio, &fh->prio, p);
  1095. }
  1096. /* vpif display ioctl operations */
  1097. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1098. .vidioc_querycap = vpif_querycap,
  1099. .vidioc_g_priority = vpif_g_priority,
  1100. .vidioc_s_priority = vpif_s_priority,
  1101. .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
  1102. .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
  1103. .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
  1104. .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
  1105. .vidioc_reqbufs = vpif_reqbufs,
  1106. .vidioc_querybuf = vpif_querybuf,
  1107. .vidioc_qbuf = vpif_qbuf,
  1108. .vidioc_dqbuf = vpif_dqbuf,
  1109. .vidioc_streamon = vpif_streamon,
  1110. .vidioc_streamoff = vpif_streamoff,
  1111. .vidioc_s_std = vpif_s_std,
  1112. .vidioc_g_std = vpif_g_std,
  1113. .vidioc_enum_output = vpif_enum_output,
  1114. .vidioc_s_output = vpif_s_output,
  1115. .vidioc_g_output = vpif_g_output,
  1116. .vidioc_cropcap = vpif_cropcap,
  1117. };
  1118. static const struct v4l2_file_operations vpif_fops = {
  1119. .owner = THIS_MODULE,
  1120. .open = vpif_open,
  1121. .release = vpif_release,
  1122. .ioctl = video_ioctl2,
  1123. .mmap = vpif_mmap,
  1124. .poll = vpif_poll
  1125. };
  1126. static struct video_device vpif_video_template = {
  1127. .name = "vpif",
  1128. .fops = &vpif_fops,
  1129. .ioctl_ops = &vpif_ioctl_ops,
  1130. .tvnorms = DM646X_V4L2_STD,
  1131. .current_norm = V4L2_STD_625_50,
  1132. };
  1133. /*Configure the channels, buffer sizei, request irq */
  1134. static int initialize_vpif(void)
  1135. {
  1136. int free_channel_objects_index;
  1137. int free_buffer_channel_index;
  1138. int free_buffer_index;
  1139. int err = 0, i, j;
  1140. /* Default number of buffers should be 3 */
  1141. if ((ch2_numbuffers > 0) &&
  1142. (ch2_numbuffers < config_params.min_numbuffers))
  1143. ch2_numbuffers = config_params.min_numbuffers;
  1144. if ((ch3_numbuffers > 0) &&
  1145. (ch3_numbuffers < config_params.min_numbuffers))
  1146. ch3_numbuffers = config_params.min_numbuffers;
  1147. /* Set buffer size to min buffers size if invalid buffer size is
  1148. * given */
  1149. if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
  1150. ch2_bufsize =
  1151. config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
  1152. if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
  1153. ch3_bufsize =
  1154. config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
  1155. config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
  1156. if (ch2_numbuffers) {
  1157. config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
  1158. ch2_bufsize;
  1159. }
  1160. config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
  1161. if (ch3_numbuffers) {
  1162. config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
  1163. ch3_bufsize;
  1164. }
  1165. /* Allocate memory for six channel objects */
  1166. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1167. vpif_obj.dev[i] =
  1168. kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
  1169. /* If memory allocation fails, return error */
  1170. if (!vpif_obj.dev[i]) {
  1171. free_channel_objects_index = i;
  1172. err = -ENOMEM;
  1173. goto vpif_init_free_channel_objects;
  1174. }
  1175. }
  1176. free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
  1177. free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
  1178. free_buffer_index = config_params.numbuffers[i - 1];
  1179. return 0;
  1180. vpif_init_free_channel_objects:
  1181. for (j = 0; j < free_channel_objects_index; j++)
  1182. kfree(vpif_obj.dev[j]);
  1183. return err;
  1184. }
  1185. /*
  1186. * vpif_probe: This function creates device entries by register itself to the
  1187. * V4L2 driver and initializes fields of each channel objects
  1188. */
  1189. static __init int vpif_probe(struct platform_device *pdev)
  1190. {
  1191. struct vpif_subdev_info *subdevdata;
  1192. struct vpif_display_config *config;
  1193. int i, j = 0, k, q, m, err = 0;
  1194. struct i2c_adapter *i2c_adap;
  1195. struct common_obj *common;
  1196. struct channel_obj *ch;
  1197. struct video_device *vfd;
  1198. struct resource *res;
  1199. int subdev_count;
  1200. vpif_dev = &pdev->dev;
  1201. err = initialize_vpif();
  1202. if (err) {
  1203. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1204. return err;
  1205. }
  1206. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1207. if (err) {
  1208. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1209. return err;
  1210. }
  1211. k = 0;
  1212. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
  1213. for (i = res->start; i <= res->end; i++) {
  1214. if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
  1215. "DM646x_Display",
  1216. (void *)(&vpif_obj.dev[k]->channel_id))) {
  1217. err = -EBUSY;
  1218. goto vpif_int_err;
  1219. }
  1220. }
  1221. k++;
  1222. }
  1223. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1224. /* Get the pointer to the channel object */
  1225. ch = vpif_obj.dev[i];
  1226. /* Allocate memory for video device */
  1227. vfd = video_device_alloc();
  1228. if (vfd == NULL) {
  1229. for (j = 0; j < i; j++) {
  1230. ch = vpif_obj.dev[j];
  1231. video_device_release(ch->video_dev);
  1232. }
  1233. err = -ENOMEM;
  1234. goto vpif_int_err;
  1235. }
  1236. /* Initialize field of video device */
  1237. *vfd = vpif_video_template;
  1238. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1239. vfd->release = video_device_release;
  1240. snprintf(vfd->name, sizeof(vfd->name),
  1241. "DM646x_VPIFDisplay_DRIVER_V%d.%d.%d",
  1242. (VPIF_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1243. (VPIF_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1244. (VPIF_DISPLAY_VERSION_CODE) & 0xff);
  1245. /* Set video_dev to the video device */
  1246. ch->video_dev = vfd;
  1247. }
  1248. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1249. ch = vpif_obj.dev[j];
  1250. /* Initialize field of the channel objects */
  1251. atomic_set(&ch->usrs, 0);
  1252. for (k = 0; k < VPIF_NUMOBJECTS; k++) {
  1253. ch->common[k].numbuffers = 0;
  1254. common = &ch->common[k];
  1255. common->io_usrs = 0;
  1256. common->started = 0;
  1257. spin_lock_init(&common->irqlock);
  1258. mutex_init(&common->lock);
  1259. common->numbuffers = 0;
  1260. common->set_addr = NULL;
  1261. common->ytop_off = common->ybtm_off = 0;
  1262. common->ctop_off = common->cbtm_off = 0;
  1263. common->cur_frm = common->next_frm = NULL;
  1264. memset(&common->fmt, 0, sizeof(common->fmt));
  1265. common->numbuffers = config_params.numbuffers[k];
  1266. }
  1267. ch->initialized = 0;
  1268. ch->channel_id = j;
  1269. if (j < 2)
  1270. ch->common[VPIF_VIDEO_INDEX].numbuffers =
  1271. config_params.numbuffers[ch->channel_id];
  1272. else
  1273. ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
  1274. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  1275. /* Initialize prio member of channel object */
  1276. v4l2_prio_init(&ch->prio);
  1277. ch->common[VPIF_VIDEO_INDEX].fmt.type =
  1278. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1279. /* register video device */
  1280. vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
  1281. (int)ch, (int)&ch->video_dev);
  1282. err = video_register_device(ch->video_dev,
  1283. VFL_TYPE_GRABBER, (j ? 3 : 2));
  1284. if (err < 0)
  1285. goto probe_out;
  1286. video_set_drvdata(ch->video_dev, ch);
  1287. }
  1288. i2c_adap = i2c_get_adapter(1);
  1289. config = pdev->dev.platform_data;
  1290. subdev_count = config->subdev_count;
  1291. subdevdata = config->subdevinfo;
  1292. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1293. GFP_KERNEL);
  1294. if (vpif_obj.sd == NULL) {
  1295. vpif_err("unable to allocate memory for subdevice pointers\n");
  1296. err = -ENOMEM;
  1297. goto probe_out;
  1298. }
  1299. for (i = 0; i < subdev_count; i++) {
  1300. vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1301. i2c_adap,
  1302. &subdevdata[i].board_info,
  1303. NULL);
  1304. if (!vpif_obj.sd[i]) {
  1305. vpif_err("Error registering v4l2 subdevice\n");
  1306. goto probe_subdev_out;
  1307. }
  1308. if (vpif_obj.sd[i])
  1309. vpif_obj.sd[i]->grp_id = 1 << i;
  1310. }
  1311. return 0;
  1312. probe_subdev_out:
  1313. kfree(vpif_obj.sd);
  1314. probe_out:
  1315. for (k = 0; k < j; k++) {
  1316. ch = vpif_obj.dev[k];
  1317. video_unregister_device(ch->video_dev);
  1318. video_device_release(ch->video_dev);
  1319. ch->video_dev = NULL;
  1320. }
  1321. vpif_int_err:
  1322. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1323. vpif_err("VPIF IRQ request failed\n");
  1324. for (q = k; k >= 0; k--) {
  1325. for (m = i; m >= res->start; m--)
  1326. free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
  1327. res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
  1328. m = res->end;
  1329. }
  1330. return err;
  1331. }
  1332. /*
  1333. * vpif_remove: It un-register channels from V4L2 driver
  1334. */
  1335. static int vpif_remove(struct platform_device *device)
  1336. {
  1337. struct channel_obj *ch;
  1338. int i;
  1339. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1340. /* un-register device */
  1341. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1342. /* Get the pointer to the channel object */
  1343. ch = vpif_obj.dev[i];
  1344. /* Unregister video device */
  1345. video_unregister_device(ch->video_dev);
  1346. ch->video_dev = NULL;
  1347. }
  1348. return 0;
  1349. }
  1350. static __refdata struct platform_driver vpif_driver = {
  1351. .driver = {
  1352. .name = "vpif_display",
  1353. .owner = THIS_MODULE,
  1354. },
  1355. .probe = vpif_probe,
  1356. .remove = vpif_remove,
  1357. };
  1358. static __init int vpif_init(void)
  1359. {
  1360. return platform_driver_register(&vpif_driver);
  1361. }
  1362. /*
  1363. * vpif_cleanup: This function un-registers device and driver to the kernel,
  1364. * frees requested irq handler and de-allocates memory allocated for channel
  1365. * objects.
  1366. */
  1367. static void vpif_cleanup(void)
  1368. {
  1369. struct platform_device *pdev;
  1370. struct resource *res;
  1371. int irq_num;
  1372. int i = 0;
  1373. pdev = container_of(vpif_dev, struct platform_device, dev);
  1374. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
  1375. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1376. free_irq(irq_num,
  1377. (void *)(&vpif_obj.dev[i]->channel_id));
  1378. i++;
  1379. }
  1380. platform_driver_unregister(&vpif_driver);
  1381. kfree(vpif_obj.sd);
  1382. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
  1383. kfree(vpif_obj.dev[i]);
  1384. }
  1385. module_init(vpif_init);
  1386. module_exit(vpif_cleanup);