vpif_capture.c 62 KB

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