vpif_capture.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  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 vpif_capture_config *config = vpif_dev->platform_data;
  757. struct video_device *vdev = video_devdata(filep);
  758. struct common_obj *common;
  759. struct video_obj *vid_ch;
  760. struct channel_obj *ch;
  761. struct vpif_fh *fh;
  762. int i;
  763. vpif_dbg(2, debug, "vpif_open\n");
  764. ch = video_get_drvdata(vdev);
  765. vid_ch = &ch->video;
  766. common = &ch->common[VPIF_VIDEO_INDEX];
  767. if (NULL == ch->curr_subdev_info) {
  768. /**
  769. * search through the sub device to see a registered
  770. * sub device and make it as current sub device
  771. */
  772. for (i = 0; i < config->subdev_count; i++) {
  773. if (vpif_obj.sd[i]) {
  774. /* the sub device is registered */
  775. ch->curr_subdev_info = &config->subdev_info[i];
  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. *index = ch->input_idx;
  1238. return 0;
  1239. }
  1240. /**
  1241. * vpif_s_input() - Set INPUT handler
  1242. * @file: file ptr
  1243. * @priv: file handle
  1244. * @index: input index
  1245. */
  1246. static int vpif_s_input(struct file *file, void *priv, unsigned int index)
  1247. {
  1248. struct vpif_capture_config *config = vpif_dev->platform_data;
  1249. struct vpif_capture_chan_config *chan_cfg;
  1250. struct vpif_fh *fh = priv;
  1251. struct channel_obj *ch = fh->channel;
  1252. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1253. struct vpif_subdev_info *subdev_info;
  1254. int ret = 0, sd_index = 0;
  1255. u32 input = 0, output = 0;
  1256. chan_cfg = &config->chan_config[ch->channel_id];
  1257. if (index >= chan_cfg->input_count)
  1258. return -EINVAL;
  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. input = chan_cfg->inputs[index].input_route;
  1293. output = chan_cfg->inputs[index].output_route;
  1294. ret = v4l2_subdev_call(vpif_obj.sd[sd_index], video, s_routing,
  1295. input, output, 0);
  1296. if (ret < 0 && ret != -ENOIOCTLCMD) {
  1297. vpif_dbg(1, debug, "Failed to set input\n");
  1298. return ret;
  1299. }
  1300. ch->input_idx = index;
  1301. ch->curr_subdev_info = subdev_info;
  1302. ch->curr_sd_index = sd_index;
  1303. /* copy interface parameters to vpif */
  1304. ch->vpifparams.iface = subdev_info->vpif_if;
  1305. /* update tvnorms from the sub device input info */
  1306. ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
  1307. return 0;
  1308. }
  1309. /**
  1310. * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
  1311. * @file: file ptr
  1312. * @priv: file handle
  1313. * @index: input index
  1314. */
  1315. static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
  1316. struct v4l2_fmtdesc *fmt)
  1317. {
  1318. struct vpif_fh *fh = priv;
  1319. struct channel_obj *ch = fh->channel;
  1320. if (fmt->index != 0) {
  1321. vpif_dbg(1, debug, "Invalid format index\n");
  1322. return -EINVAL;
  1323. }
  1324. /* Fill in the information about format */
  1325. if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
  1326. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1327. strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
  1328. fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
  1329. } else {
  1330. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1331. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  1332. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  1333. }
  1334. return 0;
  1335. }
  1336. /**
  1337. * vpif_try_fmt_vid_cap() - TRY_FMT handler
  1338. * @file: file ptr
  1339. * @priv: file handle
  1340. * @fmt: ptr to v4l2 format structure
  1341. */
  1342. static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
  1343. struct v4l2_format *fmt)
  1344. {
  1345. struct vpif_fh *fh = priv;
  1346. struct channel_obj *ch = fh->channel;
  1347. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  1348. return vpif_check_format(ch, pixfmt, 1);
  1349. }
  1350. /**
  1351. * vpif_g_fmt_vid_cap() - Set INPUT handler
  1352. * @file: file ptr
  1353. * @priv: file handle
  1354. * @fmt: ptr to v4l2 format structure
  1355. */
  1356. static int vpif_g_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. /* Check the validity of the buffer type */
  1363. if (common->fmt.type != fmt->type)
  1364. return -EINVAL;
  1365. /* Fill in the information about format */
  1366. *fmt = common->fmt;
  1367. return 0;
  1368. }
  1369. /**
  1370. * vpif_s_fmt_vid_cap() - Set FMT handler
  1371. * @file: file ptr
  1372. * @priv: file handle
  1373. * @fmt: ptr to v4l2 format structure
  1374. */
  1375. static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
  1376. struct v4l2_format *fmt)
  1377. {
  1378. struct vpif_fh *fh = priv;
  1379. struct channel_obj *ch = fh->channel;
  1380. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1381. struct v4l2_pix_format *pixfmt;
  1382. int ret = 0;
  1383. vpif_dbg(2, debug, "%s\n", __func__);
  1384. /* If streaming is started, return error */
  1385. if (common->started) {
  1386. vpif_dbg(1, debug, "Streaming is started\n");
  1387. return -EBUSY;
  1388. }
  1389. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1390. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1391. if (!fh->initialized) {
  1392. vpif_dbg(1, debug, "Channel Busy\n");
  1393. return -EBUSY;
  1394. }
  1395. }
  1396. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1397. if (0 != ret)
  1398. return ret;
  1399. fh->initialized = 1;
  1400. pixfmt = &fmt->fmt.pix;
  1401. /* Check for valid field format */
  1402. ret = vpif_check_format(ch, pixfmt, 0);
  1403. if (ret)
  1404. return ret;
  1405. /* store the format in the channel object */
  1406. common->fmt = *fmt;
  1407. return 0;
  1408. }
  1409. /**
  1410. * vpif_querycap() - QUERYCAP handler
  1411. * @file: file ptr
  1412. * @priv: file handle
  1413. * @cap: ptr to v4l2_capability structure
  1414. */
  1415. static int vpif_querycap(struct file *file, void *priv,
  1416. struct v4l2_capability *cap)
  1417. {
  1418. struct vpif_capture_config *config = vpif_dev->platform_data;
  1419. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1420. strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
  1421. strlcpy(cap->bus_info, "VPIF Platform", sizeof(cap->bus_info));
  1422. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  1423. return 0;
  1424. }
  1425. /**
  1426. * vpif_g_priority() - get priority handler
  1427. * @file: file ptr
  1428. * @priv: file handle
  1429. * @prio: ptr to v4l2_priority structure
  1430. */
  1431. static int vpif_g_priority(struct file *file, void *priv,
  1432. enum v4l2_priority *prio)
  1433. {
  1434. struct vpif_fh *fh = priv;
  1435. struct channel_obj *ch = fh->channel;
  1436. *prio = v4l2_prio_max(&ch->prio);
  1437. return 0;
  1438. }
  1439. /**
  1440. * vpif_s_priority() - set priority handler
  1441. * @file: file ptr
  1442. * @priv: file handle
  1443. * @prio: ptr to v4l2_priority structure
  1444. */
  1445. static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
  1446. {
  1447. struct vpif_fh *fh = priv;
  1448. struct channel_obj *ch = fh->channel;
  1449. return v4l2_prio_change(&ch->prio, &fh->prio, p);
  1450. }
  1451. /**
  1452. * vpif_cropcap() - cropcap handler
  1453. * @file: file ptr
  1454. * @priv: file handle
  1455. * @crop: ptr to v4l2_cropcap structure
  1456. */
  1457. static int vpif_cropcap(struct file *file, void *priv,
  1458. struct v4l2_cropcap *crop)
  1459. {
  1460. struct vpif_fh *fh = priv;
  1461. struct channel_obj *ch = fh->channel;
  1462. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1463. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
  1464. return -EINVAL;
  1465. crop->bounds.left = 0;
  1466. crop->bounds.top = 0;
  1467. crop->bounds.height = common->height;
  1468. crop->bounds.width = common->width;
  1469. crop->defrect = crop->bounds;
  1470. return 0;
  1471. }
  1472. /**
  1473. * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
  1474. * @file: file ptr
  1475. * @priv: file handle
  1476. * @timings: input timings
  1477. */
  1478. static int
  1479. vpif_enum_dv_timings(struct file *file, void *priv,
  1480. struct v4l2_enum_dv_timings *timings)
  1481. {
  1482. struct vpif_fh *fh = priv;
  1483. struct channel_obj *ch = fh->channel;
  1484. return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
  1485. video, enum_dv_timings, timings);
  1486. }
  1487. /**
  1488. * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
  1489. * @file: file ptr
  1490. * @priv: file handle
  1491. * @timings: input timings
  1492. */
  1493. static int
  1494. vpif_query_dv_timings(struct file *file, void *priv,
  1495. struct v4l2_dv_timings *timings)
  1496. {
  1497. struct vpif_fh *fh = priv;
  1498. struct channel_obj *ch = fh->channel;
  1499. return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
  1500. video, query_dv_timings, timings);
  1501. }
  1502. /**
  1503. * vpif_s_dv_timings() - S_DV_TIMINGS handler
  1504. * @file: file ptr
  1505. * @priv: file handle
  1506. * @timings: digital video timings
  1507. */
  1508. static int vpif_s_dv_timings(struct file *file, void *priv,
  1509. struct v4l2_dv_timings *timings)
  1510. {
  1511. struct vpif_fh *fh = priv;
  1512. struct channel_obj *ch = fh->channel;
  1513. struct vpif_params *vpifparams = &ch->vpifparams;
  1514. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  1515. struct video_obj *vid_ch = &ch->video;
  1516. struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
  1517. int ret;
  1518. if (timings->type != V4L2_DV_BT_656_1120) {
  1519. vpif_dbg(2, debug, "Timing type not defined\n");
  1520. return -EINVAL;
  1521. }
  1522. /* Configure subdevice timings, if any */
  1523. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
  1524. video, s_dv_timings, timings);
  1525. if (ret == -ENOIOCTLCMD) {
  1526. vpif_dbg(2, debug, "Custom DV timings not supported by "
  1527. "subdevice\n");
  1528. return -EINVAL;
  1529. }
  1530. if (ret < 0) {
  1531. vpif_dbg(2, debug, "Error setting custom DV timings\n");
  1532. return ret;
  1533. }
  1534. if (!(timings->bt.width && timings->bt.height &&
  1535. (timings->bt.hbackporch ||
  1536. timings->bt.hfrontporch ||
  1537. timings->bt.hsync) &&
  1538. timings->bt.vfrontporch &&
  1539. (timings->bt.vbackporch ||
  1540. timings->bt.vsync))) {
  1541. vpif_dbg(2, debug, "Timings for width, height, "
  1542. "horizontal back porch, horizontal sync, "
  1543. "horizontal front porch, vertical back porch, "
  1544. "vertical sync and vertical back porch "
  1545. "must be defined\n");
  1546. return -EINVAL;
  1547. }
  1548. vid_ch->dv_timings = *timings;
  1549. /* Configure video port timings */
  1550. std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
  1551. bt->hsync - 8;
  1552. std_info->sav2eav = bt->width;
  1553. std_info->l1 = 1;
  1554. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  1555. if (bt->interlaced) {
  1556. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  1557. std_info->vsize = bt->height * 2 +
  1558. bt->vfrontporch + bt->vsync + bt->vbackporch +
  1559. bt->il_vfrontporch + bt->il_vsync +
  1560. bt->il_vbackporch;
  1561. std_info->l5 = std_info->vsize/2 -
  1562. (bt->vfrontporch - 1);
  1563. std_info->l7 = std_info->vsize/2 + 1;
  1564. std_info->l9 = std_info->l7 + bt->il_vsync +
  1565. bt->il_vbackporch + 1;
  1566. std_info->l11 = std_info->vsize -
  1567. (bt->il_vfrontporch - 1);
  1568. } else {
  1569. vpif_dbg(2, debug, "Required timing values for "
  1570. "interlaced BT format missing\n");
  1571. return -EINVAL;
  1572. }
  1573. } else {
  1574. std_info->vsize = bt->height + bt->vfrontporch +
  1575. bt->vsync + bt->vbackporch;
  1576. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  1577. }
  1578. strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
  1579. std_info->width = bt->width;
  1580. std_info->height = bt->height;
  1581. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  1582. std_info->ycmux_mode = 0;
  1583. std_info->capture_format = 0;
  1584. std_info->vbi_supported = 0;
  1585. std_info->hd_sd = 1;
  1586. std_info->stdid = 0;
  1587. vid_ch->stdid = 0;
  1588. return 0;
  1589. }
  1590. /**
  1591. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  1592. * @file: file ptr
  1593. * @priv: file handle
  1594. * @timings: digital video timings
  1595. */
  1596. static int vpif_g_dv_timings(struct file *file, void *priv,
  1597. struct v4l2_dv_timings *timings)
  1598. {
  1599. struct vpif_fh *fh = priv;
  1600. struct channel_obj *ch = fh->channel;
  1601. struct video_obj *vid_ch = &ch->video;
  1602. *timings = vid_ch->dv_timings;
  1603. return 0;
  1604. }
  1605. /*
  1606. * vpif_g_chip_ident() - Identify the chip
  1607. * @file: file ptr
  1608. * @priv: file handle
  1609. * @chip: chip identity
  1610. *
  1611. * Returns zero or -EINVAL if read operations fails.
  1612. */
  1613. static int vpif_g_chip_ident(struct file *file, void *priv,
  1614. struct v4l2_dbg_chip_ident *chip)
  1615. {
  1616. chip->ident = V4L2_IDENT_NONE;
  1617. chip->revision = 0;
  1618. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  1619. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
  1620. vpif_dbg(2, debug, "match_type is invalid.\n");
  1621. return -EINVAL;
  1622. }
  1623. return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
  1624. g_chip_ident, chip);
  1625. }
  1626. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1627. /*
  1628. * vpif_dbg_g_register() - Read register
  1629. * @file: file ptr
  1630. * @priv: file handle
  1631. * @reg: register to be read
  1632. *
  1633. * Debugging only
  1634. * Returns zero or -EINVAL if read operations fails.
  1635. */
  1636. static int vpif_dbg_g_register(struct file *file, void *priv,
  1637. struct v4l2_dbg_register *reg){
  1638. struct vpif_fh *fh = priv;
  1639. struct channel_obj *ch = fh->channel;
  1640. return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
  1641. g_register, reg);
  1642. }
  1643. /*
  1644. * vpif_dbg_s_register() - Write to register
  1645. * @file: file ptr
  1646. * @priv: file handle
  1647. * @reg: register to be modified
  1648. *
  1649. * Debugging only
  1650. * Returns zero or -EINVAL if write operations fails.
  1651. */
  1652. static int vpif_dbg_s_register(struct file *file, void *priv,
  1653. struct v4l2_dbg_register *reg){
  1654. struct vpif_fh *fh = priv;
  1655. struct channel_obj *ch = fh->channel;
  1656. return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
  1657. s_register, reg);
  1658. }
  1659. #endif
  1660. /*
  1661. * vpif_log_status() - Status information
  1662. * @file: file ptr
  1663. * @priv: file handle
  1664. *
  1665. * Returns zero.
  1666. */
  1667. static int vpif_log_status(struct file *filep, void *priv)
  1668. {
  1669. /* status for sub devices */
  1670. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  1671. return 0;
  1672. }
  1673. /* vpif capture ioctl operations */
  1674. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1675. .vidioc_querycap = vpif_querycap,
  1676. .vidioc_g_priority = vpif_g_priority,
  1677. .vidioc_s_priority = vpif_s_priority,
  1678. .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
  1679. .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
  1680. .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
  1681. .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
  1682. .vidioc_enum_input = vpif_enum_input,
  1683. .vidioc_s_input = vpif_s_input,
  1684. .vidioc_g_input = vpif_g_input,
  1685. .vidioc_reqbufs = vpif_reqbufs,
  1686. .vidioc_querybuf = vpif_querybuf,
  1687. .vidioc_querystd = vpif_querystd,
  1688. .vidioc_s_std = vpif_s_std,
  1689. .vidioc_g_std = vpif_g_std,
  1690. .vidioc_qbuf = vpif_qbuf,
  1691. .vidioc_dqbuf = vpif_dqbuf,
  1692. .vidioc_streamon = vpif_streamon,
  1693. .vidioc_streamoff = vpif_streamoff,
  1694. .vidioc_cropcap = vpif_cropcap,
  1695. .vidioc_enum_dv_timings = vpif_enum_dv_timings,
  1696. .vidioc_query_dv_timings = vpif_query_dv_timings,
  1697. .vidioc_s_dv_timings = vpif_s_dv_timings,
  1698. .vidioc_g_dv_timings = vpif_g_dv_timings,
  1699. .vidioc_g_chip_ident = vpif_g_chip_ident,
  1700. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1701. .vidioc_g_register = vpif_dbg_g_register,
  1702. .vidioc_s_register = vpif_dbg_s_register,
  1703. #endif
  1704. .vidioc_log_status = vpif_log_status,
  1705. };
  1706. /* vpif file operations */
  1707. static struct v4l2_file_operations vpif_fops = {
  1708. .owner = THIS_MODULE,
  1709. .open = vpif_open,
  1710. .release = vpif_release,
  1711. .unlocked_ioctl = video_ioctl2,
  1712. .mmap = vpif_mmap,
  1713. .poll = vpif_poll
  1714. };
  1715. /* vpif video template */
  1716. static struct video_device vpif_video_template = {
  1717. .name = "vpif",
  1718. .fops = &vpif_fops,
  1719. .minor = -1,
  1720. .ioctl_ops = &vpif_ioctl_ops,
  1721. };
  1722. /**
  1723. * initialize_vpif() - Initialize vpif data structures
  1724. *
  1725. * Allocate memory for data structures and initialize them
  1726. */
  1727. static int initialize_vpif(void)
  1728. {
  1729. int err = 0, i, j;
  1730. int free_channel_objects_index;
  1731. /* Default number of buffers should be 3 */
  1732. if ((ch0_numbuffers > 0) &&
  1733. (ch0_numbuffers < config_params.min_numbuffers))
  1734. ch0_numbuffers = config_params.min_numbuffers;
  1735. if ((ch1_numbuffers > 0) &&
  1736. (ch1_numbuffers < config_params.min_numbuffers))
  1737. ch1_numbuffers = config_params.min_numbuffers;
  1738. /* Set buffer size to min buffers size if it is invalid */
  1739. if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
  1740. ch0_bufsize =
  1741. config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
  1742. if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
  1743. ch1_bufsize =
  1744. config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
  1745. config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
  1746. config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
  1747. if (ch0_numbuffers) {
  1748. config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
  1749. = ch0_bufsize;
  1750. }
  1751. if (ch1_numbuffers) {
  1752. config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
  1753. = ch1_bufsize;
  1754. }
  1755. /* Allocate memory for six channel objects */
  1756. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1757. vpif_obj.dev[i] =
  1758. kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
  1759. /* If memory allocation fails, return error */
  1760. if (!vpif_obj.dev[i]) {
  1761. free_channel_objects_index = i;
  1762. err = -ENOMEM;
  1763. goto vpif_init_free_channel_objects;
  1764. }
  1765. }
  1766. return 0;
  1767. vpif_init_free_channel_objects:
  1768. for (j = 0; j < free_channel_objects_index; j++)
  1769. kfree(vpif_obj.dev[j]);
  1770. return err;
  1771. }
  1772. /**
  1773. * vpif_probe : This function probes the vpif capture driver
  1774. * @pdev: platform device pointer
  1775. *
  1776. * This creates device entries by register itself to the V4L2 driver and
  1777. * initializes fields of each channel objects
  1778. */
  1779. static __init int vpif_probe(struct platform_device *pdev)
  1780. {
  1781. struct vpif_subdev_info *subdevdata;
  1782. struct vpif_capture_config *config;
  1783. int i, j, k, m, q, err;
  1784. struct i2c_adapter *i2c_adap;
  1785. struct channel_obj *ch;
  1786. struct common_obj *common;
  1787. struct video_device *vfd;
  1788. struct resource *res;
  1789. int subdev_count;
  1790. size_t size;
  1791. vpif_dev = &pdev->dev;
  1792. err = initialize_vpif();
  1793. if (err) {
  1794. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1795. return err;
  1796. }
  1797. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1798. if (err) {
  1799. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1800. return err;
  1801. }
  1802. k = 0;
  1803. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
  1804. for (i = res->start; i <= res->end; i++) {
  1805. if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
  1806. "VPIF_Capture",
  1807. (void *)(&vpif_obj.dev[k]->channel_id))) {
  1808. err = -EBUSY;
  1809. i--;
  1810. goto vpif_int_err;
  1811. }
  1812. }
  1813. k++;
  1814. }
  1815. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1816. /* Get the pointer to the channel object */
  1817. ch = vpif_obj.dev[i];
  1818. /* Allocate memory for video device */
  1819. vfd = video_device_alloc();
  1820. if (NULL == vfd) {
  1821. for (j = 0; j < i; j++) {
  1822. ch = vpif_obj.dev[j];
  1823. video_device_release(ch->video_dev);
  1824. }
  1825. err = -ENOMEM;
  1826. goto vpif_dev_alloc_err;
  1827. }
  1828. /* Initialize field of video device */
  1829. *vfd = vpif_video_template;
  1830. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1831. vfd->release = video_device_release;
  1832. snprintf(vfd->name, sizeof(vfd->name),
  1833. "VPIF_Capture_DRIVER_V%s",
  1834. VPIF_CAPTURE_VERSION);
  1835. /* Set video_dev to the video device */
  1836. ch->video_dev = vfd;
  1837. }
  1838. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1839. if (res) {
  1840. size = resource_size(res);
  1841. /* The resources are divided into two equal memory and when we
  1842. * have HD output we can add them together
  1843. */
  1844. for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
  1845. ch = vpif_obj.dev[j];
  1846. ch->channel_id = j;
  1847. /* only enabled if second resource exists */
  1848. config_params.video_limit[ch->channel_id] = 0;
  1849. if (size)
  1850. config_params.video_limit[ch->channel_id] =
  1851. size/2;
  1852. }
  1853. }
  1854. i2c_adap = i2c_get_adapter(1);
  1855. config = pdev->dev.platform_data;
  1856. subdev_count = config->subdev_count;
  1857. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1858. GFP_KERNEL);
  1859. if (vpif_obj.sd == NULL) {
  1860. vpif_err("unable to allocate memory for subdevice pointers\n");
  1861. err = -ENOMEM;
  1862. goto vpif_dev_alloc_err;
  1863. }
  1864. for (i = 0; i < subdev_count; i++) {
  1865. subdevdata = &config->subdev_info[i];
  1866. vpif_obj.sd[i] =
  1867. v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1868. i2c_adap,
  1869. &subdevdata->board_info,
  1870. NULL);
  1871. if (!vpif_obj.sd[i]) {
  1872. vpif_err("Error registering v4l2 subdevice\n");
  1873. goto probe_subdev_out;
  1874. }
  1875. v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
  1876. subdevdata->name);
  1877. }
  1878. for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
  1879. ch = vpif_obj.dev[j];
  1880. ch->channel_id = j;
  1881. common = &(ch->common[VPIF_VIDEO_INDEX]);
  1882. spin_lock_init(&common->irqlock);
  1883. mutex_init(&common->lock);
  1884. ch->video_dev->lock = &common->lock;
  1885. /* Initialize prio member of channel object */
  1886. v4l2_prio_init(&ch->prio);
  1887. video_set_drvdata(ch->video_dev, ch);
  1888. err = video_register_device(ch->video_dev,
  1889. VFL_TYPE_GRABBER, (j ? 1 : 0));
  1890. if (err)
  1891. goto probe_out;
  1892. }
  1893. v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
  1894. return 0;
  1895. probe_out:
  1896. for (k = 0; k < j; k++) {
  1897. /* Get the pointer to the channel object */
  1898. ch = vpif_obj.dev[k];
  1899. /* Unregister video device */
  1900. video_unregister_device(ch->video_dev);
  1901. }
  1902. probe_subdev_out:
  1903. /* free sub devices memory */
  1904. kfree(vpif_obj.sd);
  1905. vpif_dev_alloc_err:
  1906. k = VPIF_CAPTURE_MAX_DEVICES-1;
  1907. res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
  1908. i = res->end;
  1909. vpif_int_err:
  1910. for (q = k; q >= 0; q--) {
  1911. for (m = i; m >= (int)res->start; m--)
  1912. free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
  1913. res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
  1914. if (res)
  1915. i = res->end;
  1916. }
  1917. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1918. return err;
  1919. }
  1920. /**
  1921. * vpif_remove() - driver remove handler
  1922. * @device: ptr to platform device structure
  1923. *
  1924. * The vidoe device is unregistered
  1925. */
  1926. static int vpif_remove(struct platform_device *device)
  1927. {
  1928. int i;
  1929. struct channel_obj *ch;
  1930. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1931. /* un-register device */
  1932. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1933. /* Get the pointer to the channel object */
  1934. ch = vpif_obj.dev[i];
  1935. /* Unregister video device */
  1936. video_unregister_device(ch->video_dev);
  1937. }
  1938. return 0;
  1939. }
  1940. #ifdef CONFIG_PM
  1941. /**
  1942. * vpif_suspend: vpif device suspend
  1943. */
  1944. static int vpif_suspend(struct device *dev)
  1945. {
  1946. struct common_obj *common;
  1947. struct channel_obj *ch;
  1948. int i;
  1949. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1950. /* Get the pointer to the channel object */
  1951. ch = vpif_obj.dev[i];
  1952. common = &ch->common[VPIF_VIDEO_INDEX];
  1953. mutex_lock(&common->lock);
  1954. if (ch->usrs && common->io_usrs) {
  1955. /* Disable channel */
  1956. if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
  1957. enable_channel0(0);
  1958. channel0_intr_enable(0);
  1959. }
  1960. if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
  1961. common->started == 2) {
  1962. enable_channel1(0);
  1963. channel1_intr_enable(0);
  1964. }
  1965. }
  1966. mutex_unlock(&common->lock);
  1967. }
  1968. return 0;
  1969. }
  1970. /*
  1971. * vpif_resume: vpif device suspend
  1972. */
  1973. static int vpif_resume(struct device *dev)
  1974. {
  1975. struct common_obj *common;
  1976. struct channel_obj *ch;
  1977. int i;
  1978. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1979. /* Get the pointer to the channel object */
  1980. ch = vpif_obj.dev[i];
  1981. common = &ch->common[VPIF_VIDEO_INDEX];
  1982. mutex_lock(&common->lock);
  1983. if (ch->usrs && common->io_usrs) {
  1984. /* Disable channel */
  1985. if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
  1986. enable_channel0(1);
  1987. channel0_intr_enable(1);
  1988. }
  1989. if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
  1990. common->started == 2) {
  1991. enable_channel1(1);
  1992. channel1_intr_enable(1);
  1993. }
  1994. }
  1995. mutex_unlock(&common->lock);
  1996. }
  1997. return 0;
  1998. }
  1999. static const struct dev_pm_ops vpif_dev_pm_ops = {
  2000. .suspend = vpif_suspend,
  2001. .resume = vpif_resume,
  2002. };
  2003. #define vpif_pm_ops (&vpif_dev_pm_ops)
  2004. #else
  2005. #define vpif_pm_ops NULL
  2006. #endif
  2007. static __refdata struct platform_driver vpif_driver = {
  2008. .driver = {
  2009. .name = "vpif_capture",
  2010. .owner = THIS_MODULE,
  2011. .pm = vpif_pm_ops,
  2012. },
  2013. .probe = vpif_probe,
  2014. .remove = vpif_remove,
  2015. };
  2016. /**
  2017. * vpif_init: initialize the vpif driver
  2018. *
  2019. * This function registers device and driver to the kernel, requests irq
  2020. * handler and allocates memory
  2021. * for channel objects
  2022. */
  2023. static __init int vpif_init(void)
  2024. {
  2025. return platform_driver_register(&vpif_driver);
  2026. }
  2027. /**
  2028. * vpif_cleanup : This function clean up the vpif capture resources
  2029. *
  2030. * This will un-registers device and driver to the kernel, frees
  2031. * requested irq handler and de-allocates memory allocated for channel
  2032. * objects.
  2033. */
  2034. static void vpif_cleanup(void)
  2035. {
  2036. struct platform_device *pdev;
  2037. struct resource *res;
  2038. int irq_num;
  2039. int i = 0;
  2040. pdev = container_of(vpif_dev, struct platform_device, dev);
  2041. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
  2042. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  2043. free_irq(irq_num,
  2044. (void *)(&vpif_obj.dev[i]->channel_id));
  2045. i++;
  2046. }
  2047. platform_driver_unregister(&vpif_driver);
  2048. kfree(vpif_obj.sd);
  2049. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
  2050. kfree(vpif_obj.dev[i]);
  2051. }
  2052. /* Function for module initialization and cleanup */
  2053. module_init(vpif_init);
  2054. module_exit(vpif_cleanup);