vpif_capture.c 63 KB

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