vpif_display.c 50 KB

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