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