vpif_capture.c 61 KB

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