vpif_display.c 51 KB

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