vpif_display.c 43 KB

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