vpif_display.c 45 KB

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