vpif_capture.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  1. /*
  2. * Copyright (C) 2009 Texas Instruments Inc
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * TODO : add support for VBI & HBI data service
  19. * add static buffer allocation
  20. */
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include "vpif.h"
  27. #include "vpif_capture.h"
  28. MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
  29. MODULE_LICENSE("GPL");
  30. MODULE_VERSION(VPIF_CAPTURE_VERSION);
  31. #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
  32. #define vpif_dbg(level, debug, fmt, arg...) \
  33. v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
  34. static int debug = 1;
  35. static u32 ch0_numbuffers = 3;
  36. static u32 ch1_numbuffers = 3;
  37. static u32 ch0_bufsize = 1920 * 1080 * 2;
  38. static u32 ch1_bufsize = 720 * 576 * 2;
  39. module_param(debug, int, 0644);
  40. module_param(ch0_numbuffers, uint, S_IRUGO);
  41. module_param(ch1_numbuffers, uint, S_IRUGO);
  42. module_param(ch0_bufsize, uint, S_IRUGO);
  43. module_param(ch1_bufsize, uint, S_IRUGO);
  44. MODULE_PARM_DESC(debug, "Debug level 0-1");
  45. MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
  46. MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
  47. MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
  48. MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
  49. static struct vpif_config_params config_params = {
  50. .min_numbuffers = 3,
  51. .numbuffers[0] = 3,
  52. .numbuffers[1] = 3,
  53. .min_bufsize[0] = 720 * 480 * 2,
  54. .min_bufsize[1] = 720 * 480 * 2,
  55. .channel_bufsize[0] = 1920 * 1080 * 2,
  56. .channel_bufsize[1] = 720 * 576 * 2,
  57. };
  58. /* global variables */
  59. static struct vpif_device vpif_obj = { {NULL} };
  60. static struct device *vpif_dev;
  61. static void vpif_calculate_offsets(struct channel_obj *ch);
  62. static void vpif_config_addr(struct channel_obj *ch, int muxmode);
  63. /**
  64. * buffer_prepare : callback function for buffer prepare
  65. * @vb: ptr to vb2_buffer
  66. *
  67. * This is the callback function for buffer prepare when vb2_qbuf()
  68. * function is called. The buffer is prepared and user space virtual address
  69. * or user address is converted into physical address
  70. */
  71. static int vpif_buffer_prepare(struct vb2_buffer *vb)
  72. {
  73. /* Get the file handle object and channel object */
  74. struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  75. struct vb2_queue *q = vb->vb2_queue;
  76. struct channel_obj *ch = fh->channel;
  77. struct common_obj *common;
  78. unsigned long addr;
  79. vpif_dbg(2, debug, "vpif_buffer_prepare\n");
  80. common = &ch->common[VPIF_VIDEO_INDEX];
  81. if (vb->state != VB2_BUF_STATE_ACTIVE &&
  82. vb->state != VB2_BUF_STATE_PREPARED) {
  83. vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
  84. if (vb2_plane_vaddr(vb, 0) &&
  85. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  86. goto exit;
  87. addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  88. if (q->streaming) {
  89. if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
  90. !IS_ALIGNED((addr + common->ybtm_off), 8) ||
  91. !IS_ALIGNED((addr + common->ctop_off), 8) ||
  92. !IS_ALIGNED((addr + common->cbtm_off), 8))
  93. goto exit;
  94. }
  95. }
  96. return 0;
  97. exit:
  98. vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
  99. return -EINVAL;
  100. }
  101. /**
  102. * vpif_buffer_queue_setup : Callback function for buffer setup.
  103. * @vq: vb2_queue ptr
  104. * @fmt: v4l2 format
  105. * @nbuffers: ptr to number of buffers requested by application
  106. * @nplanes:: contains number of distinct video planes needed to hold a frame
  107. * @sizes[]: contains the size (in bytes) of each plane.
  108. * @alloc_ctxs: ptr to allocation context
  109. *
  110. * This callback function is called when reqbuf() is called to adjust
  111. * the buffer count and buffer size
  112. */
  113. static int vpif_buffer_queue_setup(struct vb2_queue *vq,
  114. const struct v4l2_format *fmt,
  115. unsigned int *nbuffers, unsigned int *nplanes,
  116. unsigned int sizes[], void *alloc_ctxs[])
  117. {
  118. /* Get the file handle object and channel object */
  119. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  120. struct channel_obj *ch = fh->channel;
  121. struct common_obj *common;
  122. unsigned long size;
  123. common = &ch->common[VPIF_VIDEO_INDEX];
  124. vpif_dbg(2, debug, "vpif_buffer_setup\n");
  125. /* If memory type is not mmap, return */
  126. if (V4L2_MEMORY_MMAP == common->memory) {
  127. /* Calculate the size of the buffer */
  128. size = config_params.channel_bufsize[ch->channel_id];
  129. /*
  130. * Checking if the buffer size exceeds the available buffer
  131. * ycmux_mode = 0 means 1 channel mode HD and
  132. * ycmux_mode = 1 means 2 channels mode SD
  133. */
  134. if (ch->vpifparams.std_info.ycmux_mode == 0) {
  135. if (config_params.video_limit[ch->channel_id])
  136. while (size * *nbuffers >
  137. (config_params.video_limit[0]
  138. + config_params.video_limit[1]))
  139. (*nbuffers)--;
  140. } else {
  141. if (config_params.video_limit[ch->channel_id])
  142. while (size * *nbuffers >
  143. config_params.video_limit[ch->channel_id])
  144. (*nbuffers)--;
  145. }
  146. } else {
  147. size = common->fmt.fmt.pix.sizeimage;
  148. }
  149. if (*nbuffers < config_params.min_numbuffers)
  150. *nbuffers = config_params.min_numbuffers;
  151. *nplanes = 1;
  152. sizes[0] = size;
  153. alloc_ctxs[0] = common->alloc_ctx;
  154. return 0;
  155. }
  156. /**
  157. * vpif_buffer_queue : Callback function to add buffer to DMA queue
  158. * @vb: ptr to vb2_buffer
  159. */
  160. static void vpif_buffer_queue(struct vb2_buffer *vb)
  161. {
  162. /* Get the file handle object and channel object */
  163. struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  164. struct channel_obj *ch = fh->channel;
  165. struct vpif_cap_buffer *buf = container_of(vb,
  166. struct vpif_cap_buffer, vb);
  167. struct common_obj *common;
  168. unsigned long flags;
  169. common = &ch->common[VPIF_VIDEO_INDEX];
  170. vpif_dbg(2, debug, "vpif_buffer_queue\n");
  171. spin_lock_irqsave(&common->irqlock, flags);
  172. /* add the buffer to the DMA queue */
  173. list_add_tail(&buf->list, &common->dma_queue);
  174. spin_unlock_irqrestore(&common->irqlock, flags);
  175. }
  176. /**
  177. * vpif_buf_cleanup : Callback function to free buffer
  178. * @vb: ptr to vb2_buffer
  179. *
  180. * This function is called from the videobuf2 layer to free memory
  181. * allocated to the buffers
  182. */
  183. static void vpif_buf_cleanup(struct vb2_buffer *vb)
  184. {
  185. /* Get the file handle object and channel object */
  186. struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  187. struct vpif_cap_buffer *buf = container_of(vb,
  188. struct vpif_cap_buffer, vb);
  189. struct channel_obj *ch = fh->channel;
  190. struct common_obj *common;
  191. unsigned long flags;
  192. common = &ch->common[VPIF_VIDEO_INDEX];
  193. spin_lock_irqsave(&common->irqlock, flags);
  194. if (vb->state == VB2_BUF_STATE_ACTIVE)
  195. list_del_init(&buf->list);
  196. spin_unlock_irqrestore(&common->irqlock, flags);
  197. }
  198. static void vpif_wait_prepare(struct vb2_queue *vq)
  199. {
  200. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  201. struct channel_obj *ch = fh->channel;
  202. struct common_obj *common;
  203. common = &ch->common[VPIF_VIDEO_INDEX];
  204. mutex_unlock(&common->lock);
  205. }
  206. static void vpif_wait_finish(struct vb2_queue *vq)
  207. {
  208. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  209. struct channel_obj *ch = fh->channel;
  210. struct common_obj *common;
  211. common = &ch->common[VPIF_VIDEO_INDEX];
  212. mutex_lock(&common->lock);
  213. }
  214. static int vpif_buffer_init(struct vb2_buffer *vb)
  215. {
  216. struct vpif_cap_buffer *buf = container_of(vb,
  217. struct vpif_cap_buffer, vb);
  218. INIT_LIST_HEAD(&buf->list);
  219. return 0;
  220. }
  221. static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
  222. { {1, 1} };
  223. static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
  224. {
  225. struct vpif_capture_config *vpif_config_data =
  226. vpif_dev->platform_data;
  227. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  228. struct channel_obj *ch = fh->channel;
  229. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  230. struct vpif_params *vpif = &ch->vpifparams;
  231. unsigned long addr = 0;
  232. unsigned long flags;
  233. int ret;
  234. /* If buffer queue is empty, return error */
  235. spin_lock_irqsave(&common->irqlock, flags);
  236. if (list_empty(&common->dma_queue)) {
  237. spin_unlock_irqrestore(&common->irqlock, flags);
  238. vpif_dbg(1, debug, "buffer queue is empty\n");
  239. return -EIO;
  240. }
  241. /* Get the next frame from the buffer queue */
  242. common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
  243. struct vpif_cap_buffer, list);
  244. /* Remove buffer from the buffer queue */
  245. list_del(&common->cur_frm->list);
  246. spin_unlock_irqrestore(&common->irqlock, flags);
  247. /* Mark state of the current frame to active */
  248. common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  249. /* Initialize field_id and started member */
  250. ch->field_id = 0;
  251. common->started = 1;
  252. addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
  253. /* Calculate the offset for Y and C data in the buffer */
  254. vpif_calculate_offsets(ch);
  255. if ((vpif->std_info.frm_fmt &&
  256. ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
  257. (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
  258. (!vpif->std_info.frm_fmt &&
  259. (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
  260. vpif_dbg(1, debug, "conflict in field format and std format\n");
  261. return -EINVAL;
  262. }
  263. /* configure 1 or 2 channel mode */
  264. if (vpif_config_data->setup_input_channel_mode) {
  265. ret = vpif_config_data->
  266. setup_input_channel_mode(vpif->std_info.ycmux_mode);
  267. if (ret < 0) {
  268. vpif_dbg(1, debug, "can't set vpif channel mode\n");
  269. return ret;
  270. }
  271. }
  272. /* Call vpif_set_params function to set the parameters and addresses */
  273. ret = vpif_set_video_params(vpif, ch->channel_id);
  274. if (ret < 0) {
  275. vpif_dbg(1, debug, "can't set video params\n");
  276. return ret;
  277. }
  278. common->started = ret;
  279. vpif_config_addr(ch, ret);
  280. common->set_addr(addr + common->ytop_off,
  281. addr + common->ybtm_off,
  282. addr + common->ctop_off,
  283. addr + common->cbtm_off);
  284. /**
  285. * Set interrupt for both the fields in VPIF Register enable channel in
  286. * VPIF register
  287. */
  288. channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
  289. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
  290. channel0_intr_assert();
  291. channel0_intr_enable(1);
  292. enable_channel0(1);
  293. }
  294. if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
  295. (common->started == 2)) {
  296. channel1_intr_assert();
  297. channel1_intr_enable(1);
  298. enable_channel1(1);
  299. }
  300. return 0;
  301. }
  302. /* abort streaming and wait for last buffer */
  303. static int vpif_stop_streaming(struct vb2_queue *vq)
  304. {
  305. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  306. struct channel_obj *ch = fh->channel;
  307. struct common_obj *common;
  308. unsigned long flags;
  309. if (!vb2_is_streaming(vq))
  310. return 0;
  311. common = &ch->common[VPIF_VIDEO_INDEX];
  312. /* release all active buffers */
  313. spin_lock_irqsave(&common->irqlock, flags);
  314. while (!list_empty(&common->dma_queue)) {
  315. common->next_frm = list_entry(common->dma_queue.next,
  316. struct vpif_cap_buffer, list);
  317. list_del(&common->next_frm->list);
  318. vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
  319. }
  320. spin_unlock_irqrestore(&common->irqlock, flags);
  321. return 0;
  322. }
  323. static struct vb2_ops video_qops = {
  324. .queue_setup = vpif_buffer_queue_setup,
  325. .wait_prepare = vpif_wait_prepare,
  326. .wait_finish = vpif_wait_finish,
  327. .buf_init = vpif_buffer_init,
  328. .buf_prepare = vpif_buffer_prepare,
  329. .start_streaming = vpif_start_streaming,
  330. .stop_streaming = vpif_stop_streaming,
  331. .buf_cleanup = vpif_buf_cleanup,
  332. .buf_queue = vpif_buffer_queue,
  333. };
  334. /**
  335. * vpif_process_buffer_complete: process a completed buffer
  336. * @common: ptr to common channel object
  337. *
  338. * This function time stamp the buffer and mark it as DONE. It also
  339. * wake up any process waiting on the QUEUE and set the next buffer
  340. * as current
  341. */
  342. static void vpif_process_buffer_complete(struct common_obj *common)
  343. {
  344. v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
  345. vb2_buffer_done(&common->cur_frm->vb,
  346. VB2_BUF_STATE_DONE);
  347. /* Make curFrm pointing to nextFrm */
  348. common->cur_frm = common->next_frm;
  349. }
  350. /**
  351. * vpif_schedule_next_buffer: set next buffer address for capture
  352. * @common : ptr to common channel object
  353. *
  354. * This function will get next buffer from the dma queue and
  355. * set the buffer address in the vpif register for capture.
  356. * the buffer is marked active
  357. */
  358. static void vpif_schedule_next_buffer(struct common_obj *common)
  359. {
  360. unsigned long addr = 0;
  361. spin_lock(&common->irqlock);
  362. common->next_frm = list_entry(common->dma_queue.next,
  363. struct vpif_cap_buffer, list);
  364. /* Remove that buffer from the buffer queue */
  365. list_del(&common->next_frm->list);
  366. spin_unlock(&common->irqlock);
  367. common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  368. addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
  369. /* Set top and bottom field addresses in VPIF registers */
  370. common->set_addr(addr + common->ytop_off,
  371. addr + common->ybtm_off,
  372. addr + common->ctop_off,
  373. addr + common->cbtm_off);
  374. }
  375. /**
  376. * vpif_channel_isr : ISR handler for vpif capture
  377. * @irq: irq number
  378. * @dev_id: dev_id ptr
  379. *
  380. * It changes status of the captured buffer, takes next buffer from the queue
  381. * and sets its address in VPIF registers
  382. */
  383. static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
  384. {
  385. struct vpif_device *dev = &vpif_obj;
  386. struct common_obj *common;
  387. struct channel_obj *ch;
  388. enum v4l2_field field;
  389. int channel_id = 0;
  390. int fid = -1, i;
  391. channel_id = *(int *)(dev_id);
  392. if (!vpif_intr_status(channel_id))
  393. return IRQ_NONE;
  394. ch = dev->dev[channel_id];
  395. field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
  396. for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
  397. common = &ch->common[i];
  398. /* skip If streaming is not started in this channel */
  399. if (0 == common->started)
  400. continue;
  401. /* Check the field format */
  402. if (1 == ch->vpifparams.std_info.frm_fmt) {
  403. /* Progressive mode */
  404. spin_lock(&common->irqlock);
  405. if (list_empty(&common->dma_queue)) {
  406. spin_unlock(&common->irqlock);
  407. continue;
  408. }
  409. spin_unlock(&common->irqlock);
  410. if (!channel_first_int[i][channel_id])
  411. vpif_process_buffer_complete(common);
  412. channel_first_int[i][channel_id] = 0;
  413. vpif_schedule_next_buffer(common);
  414. channel_first_int[i][channel_id] = 0;
  415. } else {
  416. /**
  417. * Interlaced mode. If it is first interrupt, ignore
  418. * it
  419. */
  420. if (channel_first_int[i][channel_id]) {
  421. channel_first_int[i][channel_id] = 0;
  422. continue;
  423. }
  424. if (0 == i) {
  425. ch->field_id ^= 1;
  426. /* Get field id from VPIF registers */
  427. fid = vpif_channel_getfid(ch->channel_id);
  428. if (fid != ch->field_id) {
  429. /**
  430. * If field id does not match stored
  431. * field id, make them in sync
  432. */
  433. if (0 == fid)
  434. ch->field_id = fid;
  435. return IRQ_HANDLED;
  436. }
  437. }
  438. /* device field id and local field id are in sync */
  439. if (0 == fid) {
  440. /* this is even field */
  441. if (common->cur_frm == common->next_frm)
  442. continue;
  443. /* mark the current buffer as done */
  444. vpif_process_buffer_complete(common);
  445. } else if (1 == fid) {
  446. /* odd field */
  447. spin_lock(&common->irqlock);
  448. if (list_empty(&common->dma_queue) ||
  449. (common->cur_frm != common->next_frm)) {
  450. spin_unlock(&common->irqlock);
  451. continue;
  452. }
  453. spin_unlock(&common->irqlock);
  454. vpif_schedule_next_buffer(common);
  455. }
  456. }
  457. }
  458. return IRQ_HANDLED;
  459. }
  460. /**
  461. * vpif_update_std_info() - update standard related info
  462. * @ch: ptr to channel object
  463. *
  464. * For a given standard selected by application, update values
  465. * in the device data structures
  466. */
  467. static int vpif_update_std_info(struct channel_obj *ch)
  468. {
  469. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  470. struct vpif_params *vpifparams = &ch->vpifparams;
  471. const struct vpif_channel_config_params *config;
  472. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  473. struct video_obj *vid_ch = &ch->video;
  474. int index;
  475. vpif_dbg(2, debug, "vpif_update_std_info\n");
  476. for (index = 0; index < vpif_ch_params_count; index++) {
  477. config = &vpif_ch_params[index];
  478. if (config->hd_sd == 0) {
  479. vpif_dbg(2, debug, "SD format\n");
  480. if (config->stdid & vid_ch->stdid) {
  481. memcpy(std_info, config, sizeof(*config));
  482. break;
  483. }
  484. } else {
  485. vpif_dbg(2, debug, "HD format\n");
  486. if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
  487. sizeof(vid_ch->dv_timings))) {
  488. memcpy(std_info, config, sizeof(*config));
  489. break;
  490. }
  491. }
  492. }
  493. /* standard not found */
  494. if (index == vpif_ch_params_count)
  495. return -EINVAL;
  496. common->fmt.fmt.pix.width = std_info->width;
  497. common->width = std_info->width;
  498. common->fmt.fmt.pix.height = std_info->height;
  499. common->height = std_info->height;
  500. common->fmt.fmt.pix.bytesperline = std_info->width;
  501. vpifparams->video_params.hpitch = std_info->width;
  502. vpifparams->video_params.storage_mode = std_info->frm_fmt;
  503. return 0;
  504. }
  505. /**
  506. * vpif_calculate_offsets : This function calculates buffers offsets
  507. * @ch : ptr to channel object
  508. *
  509. * This function calculates buffer offsets for Y and C in the top and
  510. * bottom field
  511. */
  512. static void vpif_calculate_offsets(struct channel_obj *ch)
  513. {
  514. unsigned int hpitch, vpitch, sizeimage;
  515. struct video_obj *vid_ch = &(ch->video);
  516. struct vpif_params *vpifparams = &ch->vpifparams;
  517. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  518. enum v4l2_field field = common->fmt.fmt.pix.field;
  519. vpif_dbg(2, debug, "vpif_calculate_offsets\n");
  520. if (V4L2_FIELD_ANY == field) {
  521. if (vpifparams->std_info.frm_fmt)
  522. vid_ch->buf_field = V4L2_FIELD_NONE;
  523. else
  524. vid_ch->buf_field = V4L2_FIELD_INTERLACED;
  525. } else
  526. vid_ch->buf_field = common->fmt.fmt.pix.field;
  527. sizeimage = common->fmt.fmt.pix.sizeimage;
  528. hpitch = common->fmt.fmt.pix.bytesperline;
  529. vpitch = sizeimage / (hpitch * 2);
  530. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  531. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  532. /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
  533. common->ytop_off = 0;
  534. common->ybtm_off = hpitch;
  535. common->ctop_off = sizeimage / 2;
  536. common->cbtm_off = sizeimage / 2 + hpitch;
  537. } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
  538. /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
  539. common->ytop_off = 0;
  540. common->ybtm_off = sizeimage / 4;
  541. common->ctop_off = sizeimage / 2;
  542. common->cbtm_off = common->ctop_off + sizeimage / 4;
  543. } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
  544. /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
  545. common->ybtm_off = 0;
  546. common->ytop_off = sizeimage / 4;
  547. common->cbtm_off = sizeimage / 2;
  548. common->ctop_off = common->cbtm_off + sizeimage / 4;
  549. }
  550. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  551. (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
  552. vpifparams->video_params.storage_mode = 1;
  553. else
  554. vpifparams->video_params.storage_mode = 0;
  555. if (1 == vpifparams->std_info.frm_fmt)
  556. vpifparams->video_params.hpitch =
  557. common->fmt.fmt.pix.bytesperline;
  558. else {
  559. if ((field == V4L2_FIELD_ANY)
  560. || (field == V4L2_FIELD_INTERLACED))
  561. vpifparams->video_params.hpitch =
  562. common->fmt.fmt.pix.bytesperline * 2;
  563. else
  564. vpifparams->video_params.hpitch =
  565. common->fmt.fmt.pix.bytesperline;
  566. }
  567. ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
  568. }
  569. /**
  570. * vpif_config_format: configure default frame format in the device
  571. * ch : ptr to channel object
  572. */
  573. static void vpif_config_format(struct channel_obj *ch)
  574. {
  575. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  576. vpif_dbg(2, debug, "vpif_config_format\n");
  577. common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
  578. if (config_params.numbuffers[ch->channel_id] == 0)
  579. common->memory = V4L2_MEMORY_USERPTR;
  580. else
  581. common->memory = V4L2_MEMORY_MMAP;
  582. common->fmt.fmt.pix.sizeimage
  583. = config_params.channel_bufsize[ch->channel_id];
  584. if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
  585. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
  586. else
  587. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  588. common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  589. }
  590. /**
  591. * vpif_get_default_field() - Get default field type based on interface
  592. * @vpif_params - ptr to vpif params
  593. */
  594. static inline enum v4l2_field vpif_get_default_field(
  595. struct vpif_interface *iface)
  596. {
  597. return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
  598. V4L2_FIELD_INTERLACED;
  599. }
  600. /**
  601. * vpif_check_format() - check given pixel format for compatibility
  602. * @ch - channel ptr
  603. * @pixfmt - Given pixel format
  604. * @update - update the values as per hardware requirement
  605. *
  606. * Check the application pixel format for S_FMT and update the input
  607. * values as per hardware limits for TRY_FMT. The default pixel and
  608. * field format is selected based on interface type.
  609. */
  610. static int vpif_check_format(struct channel_obj *ch,
  611. struct v4l2_pix_format *pixfmt,
  612. int update)
  613. {
  614. struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
  615. struct vpif_params *vpif_params = &ch->vpifparams;
  616. enum v4l2_field field = pixfmt->field;
  617. u32 sizeimage, hpitch, vpitch;
  618. int ret = -EINVAL;
  619. vpif_dbg(2, debug, "vpif_check_format\n");
  620. /**
  621. * first check for the pixel format. If if_type is Raw bayer,
  622. * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
  623. * V4L2_PIX_FMT_YUV422P is supported
  624. */
  625. if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
  626. if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
  627. if (!update) {
  628. vpif_dbg(2, debug, "invalid pix format\n");
  629. goto exit;
  630. }
  631. pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
  632. }
  633. } else {
  634. if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
  635. if (!update) {
  636. vpif_dbg(2, debug, "invalid pixel format\n");
  637. goto exit;
  638. }
  639. pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  640. }
  641. }
  642. if (!(VPIF_VALID_FIELD(field))) {
  643. if (!update) {
  644. vpif_dbg(2, debug, "invalid field format\n");
  645. goto exit;
  646. }
  647. /**
  648. * By default use FIELD_NONE for RAW Bayer capture
  649. * and FIELD_INTERLACED for other interfaces
  650. */
  651. field = vpif_get_default_field(&vpif_params->iface);
  652. } else if (field == V4L2_FIELD_ANY)
  653. /* unsupported field. Use default */
  654. field = vpif_get_default_field(&vpif_params->iface);
  655. /* validate the hpitch */
  656. hpitch = pixfmt->bytesperline;
  657. if (hpitch < vpif_params->std_info.width) {
  658. if (!update) {
  659. vpif_dbg(2, debug, "invalid hpitch\n");
  660. goto exit;
  661. }
  662. hpitch = vpif_params->std_info.width;
  663. }
  664. sizeimage = pixfmt->sizeimage;
  665. vpitch = sizeimage / (hpitch * 2);
  666. /* validate the vpitch */
  667. if (vpitch < vpif_params->std_info.height) {
  668. if (!update) {
  669. vpif_dbg(2, debug, "Invalid vpitch\n");
  670. goto exit;
  671. }
  672. vpitch = vpif_params->std_info.height;
  673. }
  674. /* Check for 8 byte alignment */
  675. if (!ALIGN(hpitch, 8)) {
  676. if (!update) {
  677. vpif_dbg(2, debug, "invalid pitch alignment\n");
  678. goto exit;
  679. }
  680. /* adjust to next 8 byte boundary */
  681. hpitch = (((hpitch + 7) / 8) * 8);
  682. }
  683. /* if update is set, modify the bytesperline and sizeimage */
  684. if (update) {
  685. pixfmt->bytesperline = hpitch;
  686. pixfmt->sizeimage = hpitch * vpitch * 2;
  687. }
  688. /**
  689. * Image width and height is always based on current standard width and
  690. * height
  691. */
  692. pixfmt->width = common->fmt.fmt.pix.width;
  693. pixfmt->height = common->fmt.fmt.pix.height;
  694. return 0;
  695. exit:
  696. return ret;
  697. }
  698. /**
  699. * vpif_config_addr() - function to configure buffer address in vpif
  700. * @ch - channel ptr
  701. * @muxmode - channel mux mode
  702. */
  703. static void vpif_config_addr(struct channel_obj *ch, int muxmode)
  704. {
  705. struct common_obj *common;
  706. vpif_dbg(2, debug, "vpif_config_addr\n");
  707. common = &(ch->common[VPIF_VIDEO_INDEX]);
  708. if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
  709. common->set_addr = ch1_set_videobuf_addr;
  710. else if (2 == muxmode)
  711. common->set_addr = ch0_set_videobuf_addr_yc_nmux;
  712. else
  713. common->set_addr = ch0_set_videobuf_addr;
  714. }
  715. /**
  716. * vpif_mmap : It is used to map kernel space buffers into user spaces
  717. * @filep: file pointer
  718. * @vma: ptr to vm_area_struct
  719. */
  720. static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
  721. {
  722. /* Get the channel object and file handle object */
  723. struct vpif_fh *fh = filep->private_data;
  724. struct channel_obj *ch = fh->channel;
  725. struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
  726. int ret;
  727. vpif_dbg(2, debug, "vpif_mmap\n");
  728. if (mutex_lock_interruptible(&common->lock))
  729. return -ERESTARTSYS;
  730. ret = vb2_mmap(&common->buffer_queue, vma);
  731. mutex_unlock(&common->lock);
  732. return ret;
  733. }
  734. /**
  735. * vpif_poll: It is used for select/poll system call
  736. * @filep: file pointer
  737. * @wait: poll table to wait
  738. */
  739. static unsigned int vpif_poll(struct file *filep, poll_table * wait)
  740. {
  741. struct vpif_fh *fh = filep->private_data;
  742. struct channel_obj *channel = fh->channel;
  743. struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
  744. unsigned int res = 0;
  745. vpif_dbg(2, debug, "vpif_poll\n");
  746. if (common->started) {
  747. mutex_lock(&common->lock);
  748. res = vb2_poll(&common->buffer_queue, filep, wait);
  749. mutex_unlock(&common->lock);
  750. }
  751. return res;
  752. }
  753. /**
  754. * vpif_open : vpif open handler
  755. * @filep: file ptr
  756. *
  757. * It creates object of file handle structure and stores it in private_data
  758. * member of filepointer
  759. */
  760. static int vpif_open(struct file *filep)
  761. {
  762. struct video_device *vdev = video_devdata(filep);
  763. struct common_obj *common;
  764. struct video_obj *vid_ch;
  765. struct channel_obj *ch;
  766. struct vpif_fh *fh;
  767. vpif_dbg(2, debug, "vpif_open\n");
  768. ch = video_get_drvdata(vdev);
  769. vid_ch = &ch->video;
  770. common = &ch->common[VPIF_VIDEO_INDEX];
  771. /* Allocate memory for the file handle object */
  772. fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
  773. if (NULL == fh) {
  774. vpif_err("unable to allocate memory for file handle object\n");
  775. return -ENOMEM;
  776. }
  777. if (mutex_lock_interruptible(&common->lock)) {
  778. kfree(fh);
  779. return -ERESTARTSYS;
  780. }
  781. /* store pointer to fh in private_data member of filep */
  782. filep->private_data = fh;
  783. fh->channel = ch;
  784. fh->initialized = 0;
  785. /* If decoder is not initialized. initialize it */
  786. if (!ch->initialized) {
  787. fh->initialized = 1;
  788. ch->initialized = 1;
  789. memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
  790. }
  791. /* Increment channel usrs counter */
  792. ch->usrs++;
  793. /* Set io_allowed member to false */
  794. fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
  795. /* Initialize priority of this instance to default priority */
  796. fh->prio = V4L2_PRIORITY_UNSET;
  797. v4l2_prio_open(&ch->prio, &fh->prio);
  798. mutex_unlock(&common->lock);
  799. return 0;
  800. }
  801. /**
  802. * vpif_release : function to clean up file close
  803. * @filep: file pointer
  804. *
  805. * This function deletes buffer queue, frees the buffers and the vpif file
  806. * handle
  807. */
  808. static int vpif_release(struct file *filep)
  809. {
  810. struct vpif_fh *fh = filep->private_data;
  811. struct channel_obj *ch = fh->channel;
  812. struct common_obj *common;
  813. vpif_dbg(2, debug, "vpif_release\n");
  814. common = &ch->common[VPIF_VIDEO_INDEX];
  815. mutex_lock(&common->lock);
  816. /* if this instance is doing IO */
  817. if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
  818. /* Reset io_usrs member of channel object */
  819. common->io_usrs = 0;
  820. /* Disable channel as per its device type and channel id */
  821. if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
  822. enable_channel0(0);
  823. channel0_intr_enable(0);
  824. }
  825. if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
  826. (2 == common->started)) {
  827. enable_channel1(0);
  828. channel1_intr_enable(0);
  829. }
  830. common->started = 0;
  831. /* Free buffers allocated */
  832. vb2_queue_release(&common->buffer_queue);
  833. vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
  834. }
  835. /* Decrement channel usrs counter */
  836. ch->usrs--;
  837. /* Close the priority */
  838. v4l2_prio_close(&ch->prio, fh->prio);
  839. if (fh->initialized)
  840. ch->initialized = 0;
  841. mutex_unlock(&common->lock);
  842. filep->private_data = NULL;
  843. kfree(fh);
  844. return 0;
  845. }
  846. /**
  847. * vpif_reqbufs() - request buffer handler
  848. * @file: file ptr
  849. * @priv: file handle
  850. * @reqbuf: request buffer structure ptr
  851. */
  852. static int vpif_reqbufs(struct file *file, void *priv,
  853. struct v4l2_requestbuffers *reqbuf)
  854. {
  855. struct vpif_fh *fh = priv;
  856. struct channel_obj *ch = fh->channel;
  857. struct common_obj *common;
  858. u8 index = 0;
  859. struct vb2_queue *q;
  860. int ret;
  861. vpif_dbg(2, debug, "vpif_reqbufs\n");
  862. /**
  863. * This file handle has not initialized the channel,
  864. * It is not allowed to do settings
  865. */
  866. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
  867. || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  868. if (!fh->initialized) {
  869. vpif_dbg(1, debug, "Channel Busy\n");
  870. return -EBUSY;
  871. }
  872. }
  873. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
  874. return -EINVAL;
  875. index = VPIF_VIDEO_INDEX;
  876. common = &ch->common[index];
  877. if (0 != common->io_usrs)
  878. return -EBUSY;
  879. /* Initialize videobuf2 queue as per the buffer type */
  880. common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
  881. if (IS_ERR(common->alloc_ctx)) {
  882. vpif_err("Failed to get the context\n");
  883. return PTR_ERR(common->alloc_ctx);
  884. }
  885. q = &common->buffer_queue;
  886. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  887. q->io_modes = VB2_MMAP | VB2_USERPTR;
  888. q->drv_priv = fh;
  889. q->ops = &video_qops;
  890. q->mem_ops = &vb2_dma_contig_memops;
  891. q->buf_struct_size = sizeof(struct vpif_cap_buffer);
  892. q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  893. ret = vb2_queue_init(q);
  894. if (ret) {
  895. vpif_err("vpif_capture: vb2_queue_init() failed\n");
  896. vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
  897. return ret;
  898. }
  899. /* Set io allowed member of file handle to TRUE */
  900. fh->io_allowed[index] = 1;
  901. /* Increment io usrs member of channel object to 1 */
  902. common->io_usrs = 1;
  903. /* Store type of memory requested in channel object */
  904. common->memory = reqbuf->memory;
  905. INIT_LIST_HEAD(&common->dma_queue);
  906. /* Allocate buffers */
  907. return vb2_reqbufs(&common->buffer_queue, reqbuf);
  908. }
  909. /**
  910. * vpif_querybuf() - query buffer handler
  911. * @file: file ptr
  912. * @priv: file handle
  913. * @buf: v4l2 buffer structure ptr
  914. */
  915. static int vpif_querybuf(struct file *file, void *priv,
  916. struct v4l2_buffer *buf)
  917. {
  918. struct vpif_fh *fh = priv;
  919. struct channel_obj *ch = fh->channel;
  920. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  921. vpif_dbg(2, debug, "vpif_querybuf\n");
  922. if (common->fmt.type != buf->type)
  923. return -EINVAL;
  924. if (common->memory != V4L2_MEMORY_MMAP) {
  925. vpif_dbg(1, debug, "Invalid memory\n");
  926. return -EINVAL;
  927. }
  928. return vb2_querybuf(&common->buffer_queue, buf);
  929. }
  930. /**
  931. * vpif_qbuf() - query buffer handler
  932. * @file: file ptr
  933. * @priv: file handle
  934. * @buf: v4l2 buffer structure ptr
  935. */
  936. static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  937. {
  938. struct vpif_fh *fh = priv;
  939. struct channel_obj *ch = fh->channel;
  940. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  941. struct v4l2_buffer tbuf = *buf;
  942. vpif_dbg(2, debug, "vpif_qbuf\n");
  943. if (common->fmt.type != tbuf.type) {
  944. vpif_err("invalid buffer type\n");
  945. return -EINVAL;
  946. }
  947. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  948. vpif_err("fh io not allowed\n");
  949. return -EACCES;
  950. }
  951. return vb2_qbuf(&common->buffer_queue, buf);
  952. }
  953. /**
  954. * vpif_dqbuf() - query buffer handler
  955. * @file: file ptr
  956. * @priv: file handle
  957. * @buf: v4l2 buffer structure ptr
  958. */
  959. static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  960. {
  961. struct vpif_fh *fh = priv;
  962. struct channel_obj *ch = fh->channel;
  963. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  964. vpif_dbg(2, debug, "vpif_dqbuf\n");
  965. return vb2_dqbuf(&common->buffer_queue, buf,
  966. (file->f_flags & O_NONBLOCK));
  967. }
  968. /**
  969. * vpif_streamon() - streamon handler
  970. * @file: file ptr
  971. * @priv: file handle
  972. * @buftype: v4l2 buffer type
  973. */
  974. static int vpif_streamon(struct file *file, void *priv,
  975. enum v4l2_buf_type buftype)
  976. {
  977. struct vpif_fh *fh = priv;
  978. struct channel_obj *ch = fh->channel;
  979. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  980. struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
  981. struct vpif_params *vpif;
  982. int ret = 0;
  983. vpif_dbg(2, debug, "vpif_streamon\n");
  984. vpif = &ch->vpifparams;
  985. if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  986. vpif_dbg(1, debug, "buffer type not supported\n");
  987. return -EINVAL;
  988. }
  989. /* If file handle is not allowed IO, return error */
  990. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  991. vpif_dbg(1, debug, "io not allowed\n");
  992. return -EACCES;
  993. }
  994. /* If Streaming is already started, return error */
  995. if (common->started) {
  996. vpif_dbg(1, debug, "channel->started\n");
  997. return -EBUSY;
  998. }
  999. if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
  1000. oth_ch->common[VPIF_VIDEO_INDEX].started &&
  1001. vpif->std_info.ycmux_mode == 0) ||
  1002. ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
  1003. (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
  1004. vpif_dbg(1, debug, "other channel is being used\n");
  1005. return -EBUSY;
  1006. }
  1007. ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
  1008. if (ret)
  1009. return ret;
  1010. /* Enable streamon on the sub device */
  1011. ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
  1012. if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
  1013. vpif_dbg(1, debug, "stream on failed in subdev\n");
  1014. return ret;
  1015. }
  1016. /* Call vb2_streamon to start streaming in videobuf2 */
  1017. ret = vb2_streamon(&common->buffer_queue, buftype);
  1018. if (ret) {
  1019. vpif_dbg(1, debug, "vb2_streamon\n");
  1020. return ret;
  1021. }
  1022. return ret;
  1023. }
  1024. /**
  1025. * vpif_streamoff() - streamoff handler
  1026. * @file: file ptr
  1027. * @priv: file handle
  1028. * @buftype: v4l2 buffer type
  1029. */
  1030. static int vpif_streamoff(struct file *file, void *priv,
  1031. enum v4l2_buf_type buftype)
  1032. {
  1033. struct vpif_fh *fh = priv;
  1034. struct channel_obj *ch = fh->channel;
  1035. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1036. int ret;
  1037. vpif_dbg(2, debug, "vpif_streamoff\n");
  1038. if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1039. vpif_dbg(1, debug, "buffer type not supported\n");
  1040. return -EINVAL;
  1041. }
  1042. /* If io is allowed for this file handle, return error */
  1043. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  1044. vpif_dbg(1, debug, "io not allowed\n");
  1045. return -EACCES;
  1046. }
  1047. /* If streaming is not started, return error */
  1048. if (!common->started) {
  1049. vpif_dbg(1, debug, "channel->started\n");
  1050. return -EINVAL;
  1051. }
  1052. /* disable channel */
  1053. if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
  1054. enable_channel0(0);
  1055. channel0_intr_enable(0);
  1056. } else {
  1057. enable_channel1(0);
  1058. channel1_intr_enable(0);
  1059. }
  1060. common->started = 0;
  1061. ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
  1062. if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
  1063. vpif_dbg(1, debug, "stream off failed in subdev\n");
  1064. return vb2_streamoff(&common->buffer_queue, buftype);
  1065. }
  1066. /**
  1067. * vpif_input_to_subdev() - Maps input to sub device
  1068. * @vpif_cfg - global config ptr
  1069. * @chan_cfg - channel config ptr
  1070. * @input_index - Given input index from application
  1071. *
  1072. * lookup the sub device information for a given input index.
  1073. * we report all the inputs to application. inputs table also
  1074. * has sub device name for the each input
  1075. */
  1076. static int vpif_input_to_subdev(
  1077. struct vpif_capture_config *vpif_cfg,
  1078. struct vpif_capture_chan_config *chan_cfg,
  1079. int input_index)
  1080. {
  1081. struct vpif_subdev_info *subdev_info;
  1082. const char *subdev_name;
  1083. int i;
  1084. vpif_dbg(2, debug, "vpif_input_to_subdev\n");
  1085. subdev_name = chan_cfg->inputs[input_index].subdev_name;
  1086. if (subdev_name == NULL)
  1087. return -1;
  1088. /* loop through the sub device list to get the sub device info */
  1089. for (i = 0; i < vpif_cfg->subdev_count; i++) {
  1090. subdev_info = &vpif_cfg->subdev_info[i];
  1091. if (!strcmp(subdev_info->name, subdev_name))
  1092. return i;
  1093. }
  1094. return -1;
  1095. }
  1096. /**
  1097. * vpif_set_input() - Select an input
  1098. * @vpif_cfg - global config ptr
  1099. * @ch - channel
  1100. * @_index - Given input index from application
  1101. *
  1102. * Select the given input.
  1103. */
  1104. static int vpif_set_input(
  1105. struct vpif_capture_config *vpif_cfg,
  1106. struct channel_obj *ch,
  1107. int index)
  1108. {
  1109. struct vpif_capture_chan_config *chan_cfg =
  1110. &vpif_cfg->chan_config[ch->channel_id];
  1111. struct vpif_subdev_info *subdev_info = NULL;
  1112. struct v4l2_subdev *sd = NULL;
  1113. u32 input = 0, output = 0;
  1114. int sd_index;
  1115. int ret;
  1116. sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
  1117. if (sd_index >= 0) {
  1118. sd = vpif_obj.sd[sd_index];
  1119. subdev_info = &vpif_cfg->subdev_info[sd_index];
  1120. }
  1121. /* first setup input path from sub device to vpif */
  1122. if (sd && vpif_cfg->setup_input_path) {
  1123. ret = vpif_cfg->setup_input_path(ch->channel_id,
  1124. subdev_info->name);
  1125. if (ret < 0) {
  1126. vpif_dbg(1, debug, "couldn't setup input path for the" \
  1127. " sub device %s, for input index %d\n",
  1128. subdev_info->name, index);
  1129. return ret;
  1130. }
  1131. }
  1132. if (sd) {
  1133. input = chan_cfg->inputs[index].input_route;
  1134. output = chan_cfg->inputs[index].output_route;
  1135. ret = v4l2_subdev_call(sd, video, s_routing,
  1136. input, output, 0);
  1137. if (ret < 0 && ret != -ENOIOCTLCMD) {
  1138. vpif_dbg(1, debug, "Failed to set input\n");
  1139. return ret;
  1140. }
  1141. }
  1142. ch->input_idx = index;
  1143. ch->sd = sd;
  1144. /* copy interface parameters to vpif */
  1145. ch->vpifparams.iface = chan_cfg->vpif_if;
  1146. /* update tvnorms from the sub device input info */
  1147. ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
  1148. return 0;
  1149. }
  1150. /**
  1151. * vpif_querystd() - querystd handler
  1152. * @file: file ptr
  1153. * @priv: file handle
  1154. * @std_id: ptr to std id
  1155. *
  1156. * This function is called to detect standard at the selected input
  1157. */
  1158. static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
  1159. {
  1160. struct vpif_fh *fh = priv;
  1161. struct channel_obj *ch = fh->channel;
  1162. int ret = 0;
  1163. vpif_dbg(2, debug, "vpif_querystd\n");
  1164. /* Call querystd function of decoder device */
  1165. ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
  1166. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  1167. return -ENODATA;
  1168. if (ret) {
  1169. vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
  1170. return ret;
  1171. }
  1172. return 0;
  1173. }
  1174. /**
  1175. * vpif_g_std() - get STD handler
  1176. * @file: file ptr
  1177. * @priv: file handle
  1178. * @std_id: ptr to std id
  1179. */
  1180. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  1181. {
  1182. struct vpif_fh *fh = priv;
  1183. struct channel_obj *ch = fh->channel;
  1184. vpif_dbg(2, debug, "vpif_g_std\n");
  1185. *std = ch->video.stdid;
  1186. return 0;
  1187. }
  1188. /**
  1189. * vpif_s_std() - set STD handler
  1190. * @file: file ptr
  1191. * @priv: file handle
  1192. * @std_id: ptr to std id
  1193. */
  1194. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
  1195. {
  1196. struct vpif_fh *fh = priv;
  1197. struct channel_obj *ch = fh->channel;
  1198. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1199. int ret = 0;
  1200. vpif_dbg(2, debug, "vpif_s_std\n");
  1201. if (common->started) {
  1202. vpif_err("streaming in progress\n");
  1203. return -EBUSY;
  1204. }
  1205. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1206. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1207. if (!fh->initialized) {
  1208. vpif_dbg(1, debug, "Channel Busy\n");
  1209. return -EBUSY;
  1210. }
  1211. }
  1212. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1213. if (0 != ret)
  1214. return ret;
  1215. fh->initialized = 1;
  1216. /* Call encoder subdevice function to set the standard */
  1217. ch->video.stdid = std_id;
  1218. memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
  1219. /* Get the information about the standard */
  1220. if (vpif_update_std_info(ch)) {
  1221. vpif_err("Error getting the standard info\n");
  1222. return -EINVAL;
  1223. }
  1224. /* Configure the default format information */
  1225. vpif_config_format(ch);
  1226. /* set standard in the sub device */
  1227. ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
  1228. if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
  1229. vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
  1230. return ret;
  1231. }
  1232. return 0;
  1233. }
  1234. /**
  1235. * vpif_enum_input() - ENUMINPUT handler
  1236. * @file: file ptr
  1237. * @priv: file handle
  1238. * @input: ptr to input structure
  1239. */
  1240. static int vpif_enum_input(struct file *file, void *priv,
  1241. struct v4l2_input *input)
  1242. {
  1243. struct vpif_capture_config *config = vpif_dev->platform_data;
  1244. struct vpif_capture_chan_config *chan_cfg;
  1245. struct vpif_fh *fh = priv;
  1246. struct channel_obj *ch = fh->channel;
  1247. chan_cfg = &config->chan_config[ch->channel_id];
  1248. if (input->index >= chan_cfg->input_count) {
  1249. vpif_dbg(1, debug, "Invalid input index\n");
  1250. return -EINVAL;
  1251. }
  1252. memcpy(input, &chan_cfg->inputs[input->index].input,
  1253. sizeof(*input));
  1254. return 0;
  1255. }
  1256. /**
  1257. * vpif_g_input() - Get INPUT handler
  1258. * @file: file ptr
  1259. * @priv: file handle
  1260. * @index: ptr to input index
  1261. */
  1262. static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
  1263. {
  1264. struct vpif_fh *fh = priv;
  1265. struct channel_obj *ch = fh->channel;
  1266. *index = ch->input_idx;
  1267. return 0;
  1268. }
  1269. /**
  1270. * vpif_s_input() - Set INPUT handler
  1271. * @file: file ptr
  1272. * @priv: file handle
  1273. * @index: input index
  1274. */
  1275. static int vpif_s_input(struct file *file, void *priv, unsigned int index)
  1276. {
  1277. struct vpif_capture_config *config = vpif_dev->platform_data;
  1278. struct vpif_capture_chan_config *chan_cfg;
  1279. struct vpif_fh *fh = priv;
  1280. struct channel_obj *ch = fh->channel;
  1281. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1282. int ret;
  1283. chan_cfg = &config->chan_config[ch->channel_id];
  1284. if (index >= chan_cfg->input_count)
  1285. return -EINVAL;
  1286. if (common->started) {
  1287. vpif_err("Streaming in progress\n");
  1288. return -EBUSY;
  1289. }
  1290. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1291. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1292. if (!fh->initialized) {
  1293. vpif_dbg(1, debug, "Channel Busy\n");
  1294. return -EBUSY;
  1295. }
  1296. }
  1297. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1298. if (0 != ret)
  1299. return ret;
  1300. fh->initialized = 1;
  1301. return vpif_set_input(config, ch, index);
  1302. }
  1303. /**
  1304. * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
  1305. * @file: file ptr
  1306. * @priv: file handle
  1307. * @index: input index
  1308. */
  1309. static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
  1310. struct v4l2_fmtdesc *fmt)
  1311. {
  1312. struct vpif_fh *fh = priv;
  1313. struct channel_obj *ch = fh->channel;
  1314. if (fmt->index != 0) {
  1315. vpif_dbg(1, debug, "Invalid format index\n");
  1316. return -EINVAL;
  1317. }
  1318. /* Fill in the information about format */
  1319. if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
  1320. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1321. strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
  1322. fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
  1323. } else {
  1324. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1325. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  1326. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  1327. }
  1328. return 0;
  1329. }
  1330. /**
  1331. * vpif_try_fmt_vid_cap() - TRY_FMT handler
  1332. * @file: file ptr
  1333. * @priv: file handle
  1334. * @fmt: ptr to v4l2 format structure
  1335. */
  1336. static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
  1337. struct v4l2_format *fmt)
  1338. {
  1339. struct vpif_fh *fh = priv;
  1340. struct channel_obj *ch = fh->channel;
  1341. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  1342. return vpif_check_format(ch, pixfmt, 1);
  1343. }
  1344. /**
  1345. * vpif_g_fmt_vid_cap() - Set INPUT handler
  1346. * @file: file ptr
  1347. * @priv: file handle
  1348. * @fmt: ptr to v4l2 format structure
  1349. */
  1350. static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
  1351. struct v4l2_format *fmt)
  1352. {
  1353. struct vpif_fh *fh = priv;
  1354. struct channel_obj *ch = fh->channel;
  1355. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1356. /* Check the validity of the buffer type */
  1357. if (common->fmt.type != fmt->type)
  1358. return -EINVAL;
  1359. /* Fill in the information about format */
  1360. *fmt = common->fmt;
  1361. return 0;
  1362. }
  1363. /**
  1364. * vpif_s_fmt_vid_cap() - Set FMT handler
  1365. * @file: file ptr
  1366. * @priv: file handle
  1367. * @fmt: ptr to v4l2 format structure
  1368. */
  1369. static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
  1370. struct v4l2_format *fmt)
  1371. {
  1372. struct vpif_fh *fh = priv;
  1373. struct channel_obj *ch = fh->channel;
  1374. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1375. struct v4l2_pix_format *pixfmt;
  1376. int ret = 0;
  1377. vpif_dbg(2, debug, "%s\n", __func__);
  1378. /* If streaming is started, return error */
  1379. if (common->started) {
  1380. vpif_dbg(1, debug, "Streaming is started\n");
  1381. return -EBUSY;
  1382. }
  1383. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1384. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1385. if (!fh->initialized) {
  1386. vpif_dbg(1, debug, "Channel Busy\n");
  1387. return -EBUSY;
  1388. }
  1389. }
  1390. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1391. if (0 != ret)
  1392. return ret;
  1393. fh->initialized = 1;
  1394. pixfmt = &fmt->fmt.pix;
  1395. /* Check for valid field format */
  1396. ret = vpif_check_format(ch, pixfmt, 0);
  1397. if (ret)
  1398. return ret;
  1399. /* store the format in the channel object */
  1400. common->fmt = *fmt;
  1401. return 0;
  1402. }
  1403. /**
  1404. * vpif_querycap() - QUERYCAP handler
  1405. * @file: file ptr
  1406. * @priv: file handle
  1407. * @cap: ptr to v4l2_capability structure
  1408. */
  1409. static int vpif_querycap(struct file *file, void *priv,
  1410. struct v4l2_capability *cap)
  1411. {
  1412. struct vpif_capture_config *config = vpif_dev->platform_data;
  1413. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1414. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1415. snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
  1416. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  1417. dev_name(vpif_dev));
  1418. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  1419. return 0;
  1420. }
  1421. /**
  1422. * vpif_g_priority() - get priority handler
  1423. * @file: file ptr
  1424. * @priv: file handle
  1425. * @prio: ptr to v4l2_priority structure
  1426. */
  1427. static int vpif_g_priority(struct file *file, void *priv,
  1428. enum v4l2_priority *prio)
  1429. {
  1430. struct vpif_fh *fh = priv;
  1431. struct channel_obj *ch = fh->channel;
  1432. *prio = v4l2_prio_max(&ch->prio);
  1433. return 0;
  1434. }
  1435. /**
  1436. * vpif_s_priority() - set priority handler
  1437. * @file: file ptr
  1438. * @priv: file handle
  1439. * @prio: ptr to v4l2_priority structure
  1440. */
  1441. static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
  1442. {
  1443. struct vpif_fh *fh = priv;
  1444. struct channel_obj *ch = fh->channel;
  1445. return v4l2_prio_change(&ch->prio, &fh->prio, p);
  1446. }
  1447. /**
  1448. * vpif_cropcap() - cropcap handler
  1449. * @file: file ptr
  1450. * @priv: file handle
  1451. * @crop: ptr to v4l2_cropcap structure
  1452. */
  1453. static int vpif_cropcap(struct file *file, void *priv,
  1454. struct v4l2_cropcap *crop)
  1455. {
  1456. struct vpif_fh *fh = priv;
  1457. struct channel_obj *ch = fh->channel;
  1458. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1459. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
  1460. return -EINVAL;
  1461. crop->bounds.left = 0;
  1462. crop->bounds.top = 0;
  1463. crop->bounds.height = common->height;
  1464. crop->bounds.width = common->width;
  1465. crop->defrect = crop->bounds;
  1466. return 0;
  1467. }
  1468. /**
  1469. * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
  1470. * @file: file ptr
  1471. * @priv: file handle
  1472. * @timings: input timings
  1473. */
  1474. static int
  1475. vpif_enum_dv_timings(struct file *file, void *priv,
  1476. struct v4l2_enum_dv_timings *timings)
  1477. {
  1478. struct vpif_fh *fh = priv;
  1479. struct channel_obj *ch = fh->channel;
  1480. int ret;
  1481. ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
  1482. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  1483. return -EINVAL;
  1484. return ret;
  1485. }
  1486. /**
  1487. * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
  1488. * @file: file ptr
  1489. * @priv: file handle
  1490. * @timings: input timings
  1491. */
  1492. static int
  1493. vpif_query_dv_timings(struct file *file, void *priv,
  1494. struct v4l2_dv_timings *timings)
  1495. {
  1496. struct vpif_fh *fh = priv;
  1497. struct channel_obj *ch = fh->channel;
  1498. int ret;
  1499. ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
  1500. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  1501. return -ENODATA;
  1502. return ret;
  1503. }
  1504. /**
  1505. * vpif_s_dv_timings() - S_DV_TIMINGS handler
  1506. * @file: file ptr
  1507. * @priv: file handle
  1508. * @timings: digital video timings
  1509. */
  1510. static int vpif_s_dv_timings(struct file *file, void *priv,
  1511. struct v4l2_dv_timings *timings)
  1512. {
  1513. struct vpif_fh *fh = priv;
  1514. struct channel_obj *ch = fh->channel;
  1515. struct vpif_params *vpifparams = &ch->vpifparams;
  1516. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  1517. struct video_obj *vid_ch = &ch->video;
  1518. struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
  1519. int ret;
  1520. if (timings->type != V4L2_DV_BT_656_1120) {
  1521. vpif_dbg(2, debug, "Timing type not defined\n");
  1522. return -EINVAL;
  1523. }
  1524. /* Configure subdevice timings, if any */
  1525. ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
  1526. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  1527. ret = 0;
  1528. if (ret < 0) {
  1529. vpif_dbg(2, debug, "Error setting custom DV timings\n");
  1530. return ret;
  1531. }
  1532. if (!(timings->bt.width && timings->bt.height &&
  1533. (timings->bt.hbackporch ||
  1534. timings->bt.hfrontporch ||
  1535. timings->bt.hsync) &&
  1536. timings->bt.vfrontporch &&
  1537. (timings->bt.vbackporch ||
  1538. timings->bt.vsync))) {
  1539. vpif_dbg(2, debug, "Timings for width, height, "
  1540. "horizontal back porch, horizontal sync, "
  1541. "horizontal front porch, vertical back porch, "
  1542. "vertical sync and vertical back porch "
  1543. "must be defined\n");
  1544. return -EINVAL;
  1545. }
  1546. vid_ch->dv_timings = *timings;
  1547. /* Configure video port timings */
  1548. std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
  1549. std_info->sav2eav = bt->width;
  1550. std_info->l1 = 1;
  1551. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  1552. std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
  1553. if (bt->interlaced) {
  1554. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  1555. std_info->l5 = std_info->vsize/2 -
  1556. (bt->vfrontporch - 1);
  1557. std_info->l7 = std_info->vsize/2 + 1;
  1558. std_info->l9 = std_info->l7 + bt->il_vsync +
  1559. bt->il_vbackporch + 1;
  1560. std_info->l11 = std_info->vsize -
  1561. (bt->il_vfrontporch - 1);
  1562. } else {
  1563. vpif_dbg(2, debug, "Required timing values for "
  1564. "interlaced BT format missing\n");
  1565. return -EINVAL;
  1566. }
  1567. } else {
  1568. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  1569. }
  1570. strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
  1571. std_info->width = bt->width;
  1572. std_info->height = bt->height;
  1573. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  1574. std_info->ycmux_mode = 0;
  1575. std_info->capture_format = 0;
  1576. std_info->vbi_supported = 0;
  1577. std_info->hd_sd = 1;
  1578. std_info->stdid = 0;
  1579. vid_ch->stdid = 0;
  1580. return 0;
  1581. }
  1582. /**
  1583. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  1584. * @file: file ptr
  1585. * @priv: file handle
  1586. * @timings: digital video timings
  1587. */
  1588. static int vpif_g_dv_timings(struct file *file, void *priv,
  1589. struct v4l2_dv_timings *timings)
  1590. {
  1591. struct vpif_fh *fh = priv;
  1592. struct channel_obj *ch = fh->channel;
  1593. struct video_obj *vid_ch = &ch->video;
  1594. *timings = vid_ch->dv_timings;
  1595. return 0;
  1596. }
  1597. /*
  1598. * vpif_log_status() - Status information
  1599. * @file: file ptr
  1600. * @priv: file handle
  1601. *
  1602. * Returns zero.
  1603. */
  1604. static int vpif_log_status(struct file *filep, void *priv)
  1605. {
  1606. /* status for sub devices */
  1607. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  1608. return 0;
  1609. }
  1610. /* vpif capture ioctl operations */
  1611. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1612. .vidioc_querycap = vpif_querycap,
  1613. .vidioc_g_priority = vpif_g_priority,
  1614. .vidioc_s_priority = vpif_s_priority,
  1615. .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
  1616. .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
  1617. .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
  1618. .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
  1619. .vidioc_enum_input = vpif_enum_input,
  1620. .vidioc_s_input = vpif_s_input,
  1621. .vidioc_g_input = vpif_g_input,
  1622. .vidioc_reqbufs = vpif_reqbufs,
  1623. .vidioc_querybuf = vpif_querybuf,
  1624. .vidioc_querystd = vpif_querystd,
  1625. .vidioc_s_std = vpif_s_std,
  1626. .vidioc_g_std = vpif_g_std,
  1627. .vidioc_qbuf = vpif_qbuf,
  1628. .vidioc_dqbuf = vpif_dqbuf,
  1629. .vidioc_streamon = vpif_streamon,
  1630. .vidioc_streamoff = vpif_streamoff,
  1631. .vidioc_cropcap = vpif_cropcap,
  1632. .vidioc_enum_dv_timings = vpif_enum_dv_timings,
  1633. .vidioc_query_dv_timings = vpif_query_dv_timings,
  1634. .vidioc_s_dv_timings = vpif_s_dv_timings,
  1635. .vidioc_g_dv_timings = vpif_g_dv_timings,
  1636. .vidioc_log_status = vpif_log_status,
  1637. };
  1638. /* vpif file operations */
  1639. static struct v4l2_file_operations vpif_fops = {
  1640. .owner = THIS_MODULE,
  1641. .open = vpif_open,
  1642. .release = vpif_release,
  1643. .unlocked_ioctl = video_ioctl2,
  1644. .mmap = vpif_mmap,
  1645. .poll = vpif_poll
  1646. };
  1647. /* vpif video template */
  1648. static struct video_device vpif_video_template = {
  1649. .name = "vpif",
  1650. .fops = &vpif_fops,
  1651. .minor = -1,
  1652. .ioctl_ops = &vpif_ioctl_ops,
  1653. };
  1654. /**
  1655. * initialize_vpif() - Initialize vpif data structures
  1656. *
  1657. * Allocate memory for data structures and initialize them
  1658. */
  1659. static int initialize_vpif(void)
  1660. {
  1661. int err = 0, i, j;
  1662. int free_channel_objects_index;
  1663. /* Default number of buffers should be 3 */
  1664. if ((ch0_numbuffers > 0) &&
  1665. (ch0_numbuffers < config_params.min_numbuffers))
  1666. ch0_numbuffers = config_params.min_numbuffers;
  1667. if ((ch1_numbuffers > 0) &&
  1668. (ch1_numbuffers < config_params.min_numbuffers))
  1669. ch1_numbuffers = config_params.min_numbuffers;
  1670. /* Set buffer size to min buffers size if it is invalid */
  1671. if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
  1672. ch0_bufsize =
  1673. config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
  1674. if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
  1675. ch1_bufsize =
  1676. config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
  1677. config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
  1678. config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
  1679. if (ch0_numbuffers) {
  1680. config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
  1681. = ch0_bufsize;
  1682. }
  1683. if (ch1_numbuffers) {
  1684. config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
  1685. = ch1_bufsize;
  1686. }
  1687. /* Allocate memory for six channel objects */
  1688. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1689. vpif_obj.dev[i] =
  1690. kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
  1691. /* If memory allocation fails, return error */
  1692. if (!vpif_obj.dev[i]) {
  1693. free_channel_objects_index = i;
  1694. err = -ENOMEM;
  1695. goto vpif_init_free_channel_objects;
  1696. }
  1697. }
  1698. return 0;
  1699. vpif_init_free_channel_objects:
  1700. for (j = 0; j < free_channel_objects_index; j++)
  1701. kfree(vpif_obj.dev[j]);
  1702. return err;
  1703. }
  1704. static int vpif_async_bound(struct v4l2_async_notifier *notifier,
  1705. struct v4l2_subdev *subdev,
  1706. struct v4l2_async_subdev *asd)
  1707. {
  1708. int i;
  1709. for (i = 0; i < vpif_obj.config->subdev_count; i++)
  1710. if (!strcmp(vpif_obj.config->subdev_info[i].name,
  1711. subdev->name)) {
  1712. vpif_obj.sd[i] = subdev;
  1713. return 0;
  1714. }
  1715. return -EINVAL;
  1716. }
  1717. static int vpif_probe_complete(void)
  1718. {
  1719. struct common_obj *common;
  1720. struct channel_obj *ch;
  1721. int i, j, err, k;
  1722. for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
  1723. ch = vpif_obj.dev[j];
  1724. ch->channel_id = j;
  1725. common = &(ch->common[VPIF_VIDEO_INDEX]);
  1726. spin_lock_init(&common->irqlock);
  1727. mutex_init(&common->lock);
  1728. ch->video_dev->lock = &common->lock;
  1729. /* Initialize prio member of channel object */
  1730. v4l2_prio_init(&ch->prio);
  1731. video_set_drvdata(ch->video_dev, ch);
  1732. /* select input 0 */
  1733. err = vpif_set_input(vpif_obj.config, ch, 0);
  1734. if (err)
  1735. goto probe_out;
  1736. err = video_register_device(ch->video_dev,
  1737. VFL_TYPE_GRABBER, (j ? 1 : 0));
  1738. if (err)
  1739. goto probe_out;
  1740. }
  1741. v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
  1742. return 0;
  1743. probe_out:
  1744. for (k = 0; k < j; k++) {
  1745. /* Get the pointer to the channel object */
  1746. ch = vpif_obj.dev[k];
  1747. /* Unregister video device */
  1748. video_unregister_device(ch->video_dev);
  1749. }
  1750. kfree(vpif_obj.sd);
  1751. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1752. ch = vpif_obj.dev[i];
  1753. /* Note: does nothing if ch->video_dev == NULL */
  1754. video_device_release(ch->video_dev);
  1755. }
  1756. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1757. return err;
  1758. }
  1759. static int vpif_async_complete(struct v4l2_async_notifier *notifier)
  1760. {
  1761. return vpif_probe_complete();
  1762. }
  1763. /**
  1764. * vpif_probe : This function probes the vpif capture driver
  1765. * @pdev: platform device pointer
  1766. *
  1767. * This creates device entries by register itself to the V4L2 driver and
  1768. * initializes fields of each channel objects
  1769. */
  1770. static __init int vpif_probe(struct platform_device *pdev)
  1771. {
  1772. struct vpif_subdev_info *subdevdata;
  1773. int i, j, err;
  1774. int res_idx = 0;
  1775. struct i2c_adapter *i2c_adap;
  1776. struct channel_obj *ch;
  1777. struct video_device *vfd;
  1778. struct resource *res;
  1779. int subdev_count;
  1780. size_t size;
  1781. vpif_dev = &pdev->dev;
  1782. err = initialize_vpif();
  1783. if (err) {
  1784. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1785. return err;
  1786. }
  1787. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1788. if (err) {
  1789. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1790. return err;
  1791. }
  1792. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
  1793. err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
  1794. IRQF_SHARED, "VPIF_Capture",
  1795. (void *)(&vpif_obj.dev[res_idx]->
  1796. channel_id));
  1797. if (err) {
  1798. err = -EINVAL;
  1799. goto vpif_unregister;
  1800. }
  1801. res_idx++;
  1802. }
  1803. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1804. /* Get the pointer to the channel object */
  1805. ch = vpif_obj.dev[i];
  1806. /* Allocate memory for video device */
  1807. vfd = video_device_alloc();
  1808. if (NULL == vfd) {
  1809. for (j = 0; j < i; j++) {
  1810. ch = vpif_obj.dev[j];
  1811. video_device_release(ch->video_dev);
  1812. }
  1813. err = -ENOMEM;
  1814. goto vpif_unregister;
  1815. }
  1816. /* Initialize field of video device */
  1817. *vfd = vpif_video_template;
  1818. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1819. vfd->release = video_device_release;
  1820. snprintf(vfd->name, sizeof(vfd->name),
  1821. "VPIF_Capture_DRIVER_V%s",
  1822. VPIF_CAPTURE_VERSION);
  1823. /* Set video_dev to the video device */
  1824. ch->video_dev = vfd;
  1825. }
  1826. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1827. if (res) {
  1828. size = resource_size(res);
  1829. /* The resources are divided into two equal memory and when we
  1830. * have HD output we can add them together
  1831. */
  1832. for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
  1833. ch = vpif_obj.dev[j];
  1834. ch->channel_id = j;
  1835. /* only enabled if second resource exists */
  1836. config_params.video_limit[ch->channel_id] = 0;
  1837. if (size)
  1838. config_params.video_limit[ch->channel_id] =
  1839. size/2;
  1840. }
  1841. }
  1842. vpif_obj.config = pdev->dev.platform_data;
  1843. subdev_count = vpif_obj.config->subdev_count;
  1844. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1845. GFP_KERNEL);
  1846. if (vpif_obj.sd == NULL) {
  1847. vpif_err("unable to allocate memory for subdevice pointers\n");
  1848. err = -ENOMEM;
  1849. goto vpif_sd_error;
  1850. }
  1851. if (!vpif_obj.config->asd_sizes) {
  1852. i2c_adap = i2c_get_adapter(1);
  1853. for (i = 0; i < subdev_count; i++) {
  1854. subdevdata = &vpif_obj.config->subdev_info[i];
  1855. vpif_obj.sd[i] =
  1856. v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1857. i2c_adap,
  1858. &subdevdata->
  1859. board_info,
  1860. NULL);
  1861. if (!vpif_obj.sd[i]) {
  1862. vpif_err("Error registering v4l2 subdevice\n");
  1863. err = -ENOMEM;
  1864. goto probe_subdev_out;
  1865. }
  1866. v4l2_info(&vpif_obj.v4l2_dev,
  1867. "registered sub device %s\n",
  1868. subdevdata->name);
  1869. }
  1870. vpif_probe_complete();
  1871. } else {
  1872. vpif_obj.notifier.subdevs = vpif_obj.config->asd;
  1873. vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
  1874. vpif_obj.notifier.bound = vpif_async_bound;
  1875. vpif_obj.notifier.complete = vpif_async_complete;
  1876. err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
  1877. &vpif_obj.notifier);
  1878. if (err) {
  1879. vpif_err("Error registering async notifier\n");
  1880. err = -EINVAL;
  1881. goto probe_subdev_out;
  1882. }
  1883. }
  1884. return 0;
  1885. probe_subdev_out:
  1886. /* free sub devices memory */
  1887. kfree(vpif_obj.sd);
  1888. vpif_sd_error:
  1889. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1890. ch = vpif_obj.dev[i];
  1891. /* Note: does nothing if ch->video_dev == NULL */
  1892. video_device_release(ch->video_dev);
  1893. }
  1894. vpif_unregister:
  1895. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1896. return err;
  1897. }
  1898. /**
  1899. * vpif_remove() - driver remove handler
  1900. * @device: ptr to platform device structure
  1901. *
  1902. * The vidoe device is unregistered
  1903. */
  1904. static int vpif_remove(struct platform_device *device)
  1905. {
  1906. struct channel_obj *ch;
  1907. int i;
  1908. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1909. kfree(vpif_obj.sd);
  1910. /* un-register device */
  1911. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1912. /* Get the pointer to the channel object */
  1913. ch = vpif_obj.dev[i];
  1914. /* Unregister video device */
  1915. video_unregister_device(ch->video_dev);
  1916. kfree(vpif_obj.dev[i]);
  1917. }
  1918. return 0;
  1919. }
  1920. #ifdef CONFIG_PM
  1921. /**
  1922. * vpif_suspend: vpif device suspend
  1923. */
  1924. static int vpif_suspend(struct device *dev)
  1925. {
  1926. struct common_obj *common;
  1927. struct channel_obj *ch;
  1928. int i;
  1929. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1930. /* Get the pointer to the channel object */
  1931. ch = vpif_obj.dev[i];
  1932. common = &ch->common[VPIF_VIDEO_INDEX];
  1933. mutex_lock(&common->lock);
  1934. if (ch->usrs && common->io_usrs) {
  1935. /* Disable channel */
  1936. if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
  1937. enable_channel0(0);
  1938. channel0_intr_enable(0);
  1939. }
  1940. if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
  1941. common->started == 2) {
  1942. enable_channel1(0);
  1943. channel1_intr_enable(0);
  1944. }
  1945. }
  1946. mutex_unlock(&common->lock);
  1947. }
  1948. return 0;
  1949. }
  1950. /*
  1951. * vpif_resume: vpif device suspend
  1952. */
  1953. static int vpif_resume(struct device *dev)
  1954. {
  1955. struct common_obj *common;
  1956. struct channel_obj *ch;
  1957. int i;
  1958. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1959. /* Get the pointer to the channel object */
  1960. ch = vpif_obj.dev[i];
  1961. common = &ch->common[VPIF_VIDEO_INDEX];
  1962. mutex_lock(&common->lock);
  1963. if (ch->usrs && common->io_usrs) {
  1964. /* Disable channel */
  1965. if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
  1966. enable_channel0(1);
  1967. channel0_intr_enable(1);
  1968. }
  1969. if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
  1970. common->started == 2) {
  1971. enable_channel1(1);
  1972. channel1_intr_enable(1);
  1973. }
  1974. }
  1975. mutex_unlock(&common->lock);
  1976. }
  1977. return 0;
  1978. }
  1979. static const struct dev_pm_ops vpif_dev_pm_ops = {
  1980. .suspend = vpif_suspend,
  1981. .resume = vpif_resume,
  1982. };
  1983. #define vpif_pm_ops (&vpif_dev_pm_ops)
  1984. #else
  1985. #define vpif_pm_ops NULL
  1986. #endif
  1987. static __refdata struct platform_driver vpif_driver = {
  1988. .driver = {
  1989. .name = "vpif_capture",
  1990. .owner = THIS_MODULE,
  1991. .pm = vpif_pm_ops,
  1992. },
  1993. .probe = vpif_probe,
  1994. .remove = vpif_remove,
  1995. };
  1996. module_platform_driver(vpif_driver);