vpif_display.c 50 KB

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