vpif_capture.c 62 KB

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