vpif_capture.c 63 KB

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