vpif_capture.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444
  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. vpif_dbg(2, debug, "vpif_mmap\n");
  718. return vb2_mmap(&common->buffer_queue, vma);
  719. }
  720. /**
  721. * vpif_poll: It is used for select/poll system call
  722. * @filep: file pointer
  723. * @wait: poll table to wait
  724. */
  725. static unsigned int vpif_poll(struct file *filep, poll_table * wait)
  726. {
  727. struct vpif_fh *fh = filep->private_data;
  728. struct channel_obj *channel = fh->channel;
  729. struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
  730. vpif_dbg(2, debug, "vpif_poll\n");
  731. if (common->started)
  732. return vb2_poll(&common->buffer_queue, filep, wait);
  733. return 0;
  734. }
  735. /**
  736. * vpif_open : vpif open handler
  737. * @filep: file ptr
  738. *
  739. * It creates object of file handle structure and stores it in private_data
  740. * member of filepointer
  741. */
  742. static int vpif_open(struct file *filep)
  743. {
  744. struct vpif_capture_config *config = vpif_dev->platform_data;
  745. struct video_device *vdev = video_devdata(filep);
  746. struct common_obj *common;
  747. struct video_obj *vid_ch;
  748. struct channel_obj *ch;
  749. struct vpif_fh *fh;
  750. int i;
  751. vpif_dbg(2, debug, "vpif_open\n");
  752. ch = video_get_drvdata(vdev);
  753. vid_ch = &ch->video;
  754. common = &ch->common[VPIF_VIDEO_INDEX];
  755. if (NULL == ch->curr_subdev_info) {
  756. /**
  757. * search through the sub device to see a registered
  758. * sub device and make it as current sub device
  759. */
  760. for (i = 0; i < config->subdev_count; i++) {
  761. if (vpif_obj.sd[i]) {
  762. /* the sub device is registered */
  763. ch->curr_subdev_info = &config->subdev_info[i];
  764. /* make first input as the current input */
  765. vid_ch->input_idx = 0;
  766. break;
  767. }
  768. }
  769. if (i == config->subdev_count) {
  770. vpif_err("No sub device registered\n");
  771. return -ENOENT;
  772. }
  773. }
  774. /* Allocate memory for the file handle object */
  775. fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
  776. if (NULL == fh) {
  777. vpif_err("unable to allocate memory for file handle object\n");
  778. return -ENOMEM;
  779. }
  780. /* store pointer to fh in private_data member of filep */
  781. filep->private_data = fh;
  782. fh->channel = ch;
  783. fh->initialized = 0;
  784. /* If decoder is not initialized. initialize it */
  785. if (!ch->initialized) {
  786. fh->initialized = 1;
  787. ch->initialized = 1;
  788. memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
  789. }
  790. /* Increment channel usrs counter */
  791. ch->usrs++;
  792. /* Set io_allowed member to false */
  793. fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
  794. /* Initialize priority of this instance to default priority */
  795. fh->prio = V4L2_PRIORITY_UNSET;
  796. v4l2_prio_open(&ch->prio, &fh->prio);
  797. return 0;
  798. }
  799. /**
  800. * vpif_release : function to clean up file close
  801. * @filep: file pointer
  802. *
  803. * This function deletes buffer queue, frees the buffers and the vpif file
  804. * handle
  805. */
  806. static int vpif_release(struct file *filep)
  807. {
  808. struct vpif_fh *fh = filep->private_data;
  809. struct channel_obj *ch = fh->channel;
  810. struct common_obj *common;
  811. vpif_dbg(2, debug, "vpif_release\n");
  812. common = &ch->common[VPIF_VIDEO_INDEX];
  813. /* if this instance is doing IO */
  814. if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
  815. /* Reset io_usrs member of channel object */
  816. common->io_usrs = 0;
  817. /* Disable channel as per its device type and channel id */
  818. if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
  819. enable_channel0(0);
  820. channel0_intr_enable(0);
  821. }
  822. if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
  823. (2 == common->started)) {
  824. enable_channel1(0);
  825. channel1_intr_enable(0);
  826. }
  827. common->started = 0;
  828. /* Free buffers allocated */
  829. vb2_queue_release(&common->buffer_queue);
  830. vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
  831. }
  832. /* Decrement channel usrs counter */
  833. ch->usrs--;
  834. /* Close the priority */
  835. v4l2_prio_close(&ch->prio, fh->prio);
  836. if (fh->initialized)
  837. ch->initialized = 0;
  838. filep->private_data = NULL;
  839. kfree(fh);
  840. return 0;
  841. }
  842. /**
  843. * vpif_reqbufs() - request buffer handler
  844. * @file: file ptr
  845. * @priv: file handle
  846. * @reqbuf: request buffer structure ptr
  847. */
  848. static int vpif_reqbufs(struct file *file, void *priv,
  849. struct v4l2_requestbuffers *reqbuf)
  850. {
  851. struct vpif_fh *fh = priv;
  852. struct channel_obj *ch = fh->channel;
  853. struct common_obj *common;
  854. u8 index = 0;
  855. struct vb2_queue *q;
  856. vpif_dbg(2, debug, "vpif_reqbufs\n");
  857. /**
  858. * This file handle has not initialized the channel,
  859. * It is not allowed to do settings
  860. */
  861. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
  862. || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  863. if (!fh->initialized) {
  864. vpif_dbg(1, debug, "Channel Busy\n");
  865. return -EBUSY;
  866. }
  867. }
  868. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
  869. return -EINVAL;
  870. index = VPIF_VIDEO_INDEX;
  871. common = &ch->common[index];
  872. if (0 != common->io_usrs)
  873. return -EBUSY;
  874. /* Initialize videobuf2 queue as per the buffer type */
  875. common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
  876. if (!common->alloc_ctx) {
  877. vpif_err("Failed to get the context\n");
  878. return -EINVAL;
  879. }
  880. q = &common->buffer_queue;
  881. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  882. q->io_modes = VB2_MMAP | VB2_USERPTR;
  883. q->drv_priv = fh;
  884. q->ops = &video_qops;
  885. q->mem_ops = &vb2_dma_contig_memops;
  886. q->buf_struct_size = sizeof(struct vpif_cap_buffer);
  887. vb2_queue_init(q);
  888. /* Set io allowed member of file handle to TRUE */
  889. fh->io_allowed[index] = 1;
  890. /* Increment io usrs member of channel object to 1 */
  891. common->io_usrs = 1;
  892. /* Store type of memory requested in channel object */
  893. common->memory = reqbuf->memory;
  894. INIT_LIST_HEAD(&common->dma_queue);
  895. /* Allocate buffers */
  896. return vb2_reqbufs(&common->buffer_queue, reqbuf);
  897. }
  898. /**
  899. * vpif_querybuf() - query buffer handler
  900. * @file: file ptr
  901. * @priv: file handle
  902. * @buf: v4l2 buffer structure ptr
  903. */
  904. static int vpif_querybuf(struct file *file, void *priv,
  905. struct v4l2_buffer *buf)
  906. {
  907. struct vpif_fh *fh = priv;
  908. struct channel_obj *ch = fh->channel;
  909. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  910. vpif_dbg(2, debug, "vpif_querybuf\n");
  911. if (common->fmt.type != buf->type)
  912. return -EINVAL;
  913. if (common->memory != V4L2_MEMORY_MMAP) {
  914. vpif_dbg(1, debug, "Invalid memory\n");
  915. return -EINVAL;
  916. }
  917. return vb2_querybuf(&common->buffer_queue, buf);
  918. }
  919. /**
  920. * vpif_qbuf() - query buffer handler
  921. * @file: file ptr
  922. * @priv: file handle
  923. * @buf: v4l2 buffer structure ptr
  924. */
  925. static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  926. {
  927. struct vpif_fh *fh = priv;
  928. struct channel_obj *ch = fh->channel;
  929. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  930. struct v4l2_buffer tbuf = *buf;
  931. vpif_dbg(2, debug, "vpif_qbuf\n");
  932. if (common->fmt.type != tbuf.type) {
  933. vpif_err("invalid buffer type\n");
  934. return -EINVAL;
  935. }
  936. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  937. vpif_err("fh io not allowed\n");
  938. return -EACCES;
  939. }
  940. return vb2_qbuf(&common->buffer_queue, buf);
  941. }
  942. /**
  943. * vpif_dqbuf() - query buffer handler
  944. * @file: file ptr
  945. * @priv: file handle
  946. * @buf: v4l2 buffer structure ptr
  947. */
  948. static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  949. {
  950. struct vpif_fh *fh = priv;
  951. struct channel_obj *ch = fh->channel;
  952. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  953. vpif_dbg(2, debug, "vpif_dqbuf\n");
  954. return vb2_dqbuf(&common->buffer_queue, buf,
  955. (file->f_flags & O_NONBLOCK));
  956. }
  957. /**
  958. * vpif_streamon() - streamon handler
  959. * @file: file ptr
  960. * @priv: file handle
  961. * @buftype: v4l2 buffer type
  962. */
  963. static int vpif_streamon(struct file *file, void *priv,
  964. enum v4l2_buf_type buftype)
  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. struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
  970. struct vpif_params *vpif;
  971. int ret = 0;
  972. vpif_dbg(2, debug, "vpif_streamon\n");
  973. vpif = &ch->vpifparams;
  974. if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  975. vpif_dbg(1, debug, "buffer type not supported\n");
  976. return -EINVAL;
  977. }
  978. /* If file handle is not allowed IO, return error */
  979. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  980. vpif_dbg(1, debug, "io not allowed\n");
  981. return -EACCES;
  982. }
  983. /* If Streaming is already started, return error */
  984. if (common->started) {
  985. vpif_dbg(1, debug, "channel->started\n");
  986. return -EBUSY;
  987. }
  988. if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
  989. oth_ch->common[VPIF_VIDEO_INDEX].started &&
  990. vpif->std_info.ycmux_mode == 0) ||
  991. ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
  992. (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
  993. vpif_dbg(1, debug, "other channel is being used\n");
  994. return -EBUSY;
  995. }
  996. ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
  997. if (ret)
  998. return ret;
  999. /* Enable streamon on the sub device */
  1000. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
  1001. s_stream, 1);
  1002. if (ret && (ret != -ENOIOCTLCMD)) {
  1003. vpif_dbg(1, debug, "stream on failed in subdev\n");
  1004. return ret;
  1005. }
  1006. /* Call vb2_streamon to start streaming in videobuf2 */
  1007. ret = vb2_streamon(&common->buffer_queue, buftype);
  1008. if (ret) {
  1009. vpif_dbg(1, debug, "vb2_streamon\n");
  1010. return ret;
  1011. }
  1012. return ret;
  1013. }
  1014. /**
  1015. * vpif_streamoff() - streamoff handler
  1016. * @file: file ptr
  1017. * @priv: file handle
  1018. * @buftype: v4l2 buffer type
  1019. */
  1020. static int vpif_streamoff(struct file *file, void *priv,
  1021. enum v4l2_buf_type buftype)
  1022. {
  1023. struct vpif_fh *fh = priv;
  1024. struct channel_obj *ch = fh->channel;
  1025. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1026. int ret;
  1027. vpif_dbg(2, debug, "vpif_streamoff\n");
  1028. if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1029. vpif_dbg(1, debug, "buffer type not supported\n");
  1030. return -EINVAL;
  1031. }
  1032. /* If io is allowed for this file handle, return error */
  1033. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  1034. vpif_dbg(1, debug, "io not allowed\n");
  1035. return -EACCES;
  1036. }
  1037. /* If streaming is not started, return error */
  1038. if (!common->started) {
  1039. vpif_dbg(1, debug, "channel->started\n");
  1040. return -EINVAL;
  1041. }
  1042. /* disable channel */
  1043. if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
  1044. enable_channel0(0);
  1045. channel0_intr_enable(0);
  1046. } else {
  1047. enable_channel1(0);
  1048. channel1_intr_enable(0);
  1049. }
  1050. common->started = 0;
  1051. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
  1052. s_stream, 0);
  1053. if (ret && (ret != -ENOIOCTLCMD))
  1054. vpif_dbg(1, debug, "stream off failed in subdev\n");
  1055. return vb2_streamoff(&common->buffer_queue, buftype);
  1056. }
  1057. /**
  1058. * vpif_map_sub_device_to_input() - Maps sub device to input
  1059. * @ch - ptr to channel
  1060. * @config - ptr to capture configuration
  1061. * @input_index - Given input index from application
  1062. * @sub_device_index - index into sd table
  1063. *
  1064. * lookup the sub device information for a given input index.
  1065. * we report all the inputs to application. inputs table also
  1066. * has sub device name for the each input
  1067. */
  1068. static struct vpif_subdev_info *vpif_map_sub_device_to_input(
  1069. struct channel_obj *ch,
  1070. struct vpif_capture_config *vpif_cfg,
  1071. int input_index,
  1072. int *sub_device_index)
  1073. {
  1074. struct vpif_capture_chan_config *chan_cfg;
  1075. struct vpif_subdev_info *subdev_info = NULL;
  1076. const char *subdev_name = NULL;
  1077. int i;
  1078. vpif_dbg(2, debug, "vpif_map_sub_device_to_input\n");
  1079. chan_cfg = &vpif_cfg->chan_config[ch->channel_id];
  1080. /**
  1081. * search through the inputs to find the sub device supporting
  1082. * the input
  1083. */
  1084. for (i = 0; i < chan_cfg->input_count; i++) {
  1085. /* For each sub device, loop through input */
  1086. if (i == input_index) {
  1087. subdev_name = chan_cfg->inputs[i].subdev_name;
  1088. break;
  1089. }
  1090. }
  1091. /* if reached maximum. return null */
  1092. if (i == chan_cfg->input_count || (NULL == subdev_name))
  1093. return subdev_info;
  1094. /* loop through the sub device list to get the sub device info */
  1095. for (i = 0; i < vpif_cfg->subdev_count; i++) {
  1096. subdev_info = &vpif_cfg->subdev_info[i];
  1097. if (!strcmp(subdev_info->name, subdev_name))
  1098. break;
  1099. }
  1100. if (i == vpif_cfg->subdev_count)
  1101. return subdev_info;
  1102. /* check if the sub device is registered */
  1103. if (NULL == vpif_obj.sd[i])
  1104. return NULL;
  1105. *sub_device_index = i;
  1106. return subdev_info;
  1107. }
  1108. /**
  1109. * vpif_querystd() - querystd handler
  1110. * @file: file ptr
  1111. * @priv: file handle
  1112. * @std_id: ptr to std id
  1113. *
  1114. * This function is called to detect standard at the selected input
  1115. */
  1116. static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
  1117. {
  1118. struct vpif_fh *fh = priv;
  1119. struct channel_obj *ch = fh->channel;
  1120. int ret = 0;
  1121. vpif_dbg(2, debug, "vpif_querystd\n");
  1122. /* Call querystd function of decoder device */
  1123. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
  1124. querystd, std_id);
  1125. if (ret < 0)
  1126. vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
  1127. return ret;
  1128. }
  1129. /**
  1130. * vpif_g_std() - get STD handler
  1131. * @file: file ptr
  1132. * @priv: file handle
  1133. * @std_id: ptr to std id
  1134. */
  1135. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  1136. {
  1137. struct vpif_fh *fh = priv;
  1138. struct channel_obj *ch = fh->channel;
  1139. vpif_dbg(2, debug, "vpif_g_std\n");
  1140. *std = ch->video.stdid;
  1141. return 0;
  1142. }
  1143. /**
  1144. * vpif_s_std() - set STD handler
  1145. * @file: file ptr
  1146. * @priv: file handle
  1147. * @std_id: ptr to std id
  1148. */
  1149. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  1150. {
  1151. struct vpif_fh *fh = priv;
  1152. struct channel_obj *ch = fh->channel;
  1153. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1154. int ret = 0;
  1155. vpif_dbg(2, debug, "vpif_s_std\n");
  1156. if (common->started) {
  1157. vpif_err("streaming in progress\n");
  1158. return -EBUSY;
  1159. }
  1160. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1161. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1162. if (!fh->initialized) {
  1163. vpif_dbg(1, debug, "Channel Busy\n");
  1164. return -EBUSY;
  1165. }
  1166. }
  1167. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1168. if (0 != ret)
  1169. return ret;
  1170. fh->initialized = 1;
  1171. /* Call encoder subdevice function to set the standard */
  1172. ch->video.stdid = *std_id;
  1173. ch->video.dv_preset = V4L2_DV_INVALID;
  1174. memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
  1175. /* Get the information about the standard */
  1176. if (vpif_update_std_info(ch)) {
  1177. vpif_err("Error getting the standard info\n");
  1178. return -EINVAL;
  1179. }
  1180. /* Configure the default format information */
  1181. vpif_config_format(ch);
  1182. /* set standard in the sub device */
  1183. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
  1184. s_std, *std_id);
  1185. if (ret < 0)
  1186. vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
  1187. return ret;
  1188. }
  1189. /**
  1190. * vpif_enum_input() - ENUMINPUT handler
  1191. * @file: file ptr
  1192. * @priv: file handle
  1193. * @input: ptr to input structure
  1194. */
  1195. static int vpif_enum_input(struct file *file, void *priv,
  1196. struct v4l2_input *input)
  1197. {
  1198. struct vpif_capture_config *config = vpif_dev->platform_data;
  1199. struct vpif_capture_chan_config *chan_cfg;
  1200. struct vpif_fh *fh = priv;
  1201. struct channel_obj *ch = fh->channel;
  1202. chan_cfg = &config->chan_config[ch->channel_id];
  1203. if (input->index >= chan_cfg->input_count) {
  1204. vpif_dbg(1, debug, "Invalid input index\n");
  1205. return -EINVAL;
  1206. }
  1207. memcpy(input, &chan_cfg->inputs[input->index].input,
  1208. sizeof(*input));
  1209. return 0;
  1210. }
  1211. /**
  1212. * vpif_g_input() - Get INPUT handler
  1213. * @file: file ptr
  1214. * @priv: file handle
  1215. * @index: ptr to input index
  1216. */
  1217. static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
  1218. {
  1219. struct vpif_fh *fh = priv;
  1220. struct channel_obj *ch = fh->channel;
  1221. struct video_obj *vid_ch = &ch->video;
  1222. *index = vid_ch->input_idx;
  1223. return 0;
  1224. }
  1225. /**
  1226. * vpif_s_input() - Set INPUT handler
  1227. * @file: file ptr
  1228. * @priv: file handle
  1229. * @index: input index
  1230. */
  1231. static int vpif_s_input(struct file *file, void *priv, unsigned int index)
  1232. {
  1233. struct vpif_capture_config *config = vpif_dev->platform_data;
  1234. struct vpif_capture_chan_config *chan_cfg;
  1235. struct vpif_fh *fh = priv;
  1236. struct channel_obj *ch = fh->channel;
  1237. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1238. struct video_obj *vid_ch = &ch->video;
  1239. struct vpif_subdev_info *subdev_info;
  1240. int ret = 0, sd_index = 0;
  1241. u32 input = 0, output = 0;
  1242. chan_cfg = &config->chan_config[ch->channel_id];
  1243. if (common->started) {
  1244. vpif_err("Streaming in progress\n");
  1245. return -EBUSY;
  1246. }
  1247. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1248. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1249. if (!fh->initialized) {
  1250. vpif_dbg(1, debug, "Channel Busy\n");
  1251. return -EBUSY;
  1252. }
  1253. }
  1254. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1255. if (0 != ret)
  1256. return ret;
  1257. fh->initialized = 1;
  1258. subdev_info = vpif_map_sub_device_to_input(ch, config, index,
  1259. &sd_index);
  1260. if (NULL == subdev_info) {
  1261. vpif_dbg(1, debug,
  1262. "couldn't lookup sub device for the input index\n");
  1263. return -EINVAL;
  1264. }
  1265. /* first setup input path from sub device to vpif */
  1266. if (config->setup_input_path) {
  1267. ret = config->setup_input_path(ch->channel_id,
  1268. subdev_info->name);
  1269. if (ret < 0) {
  1270. vpif_dbg(1, debug, "couldn't setup input path for the"
  1271. " sub device %s, for input index %d\n",
  1272. subdev_info->name, index);
  1273. return ret;
  1274. }
  1275. }
  1276. if (subdev_info->can_route) {
  1277. input = subdev_info->input;
  1278. output = subdev_info->output;
  1279. ret = v4l2_subdev_call(vpif_obj.sd[sd_index], video, s_routing,
  1280. input, output, 0);
  1281. if (ret < 0) {
  1282. vpif_dbg(1, debug, "Failed to set input\n");
  1283. return ret;
  1284. }
  1285. }
  1286. vid_ch->input_idx = index;
  1287. ch->curr_subdev_info = subdev_info;
  1288. ch->curr_sd_index = sd_index;
  1289. /* copy interface parameters to vpif */
  1290. ch->vpifparams.iface = subdev_info->vpif_if;
  1291. /* update tvnorms from the sub device input info */
  1292. ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
  1293. return ret;
  1294. }
  1295. /**
  1296. * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
  1297. * @file: file ptr
  1298. * @priv: file handle
  1299. * @index: input index
  1300. */
  1301. static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
  1302. struct v4l2_fmtdesc *fmt)
  1303. {
  1304. struct vpif_fh *fh = priv;
  1305. struct channel_obj *ch = fh->channel;
  1306. if (fmt->index != 0) {
  1307. vpif_dbg(1, debug, "Invalid format index\n");
  1308. return -EINVAL;
  1309. }
  1310. /* Fill in the information about format */
  1311. if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
  1312. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1313. strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
  1314. fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
  1315. } else {
  1316. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1317. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  1318. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  1319. }
  1320. return 0;
  1321. }
  1322. /**
  1323. * vpif_try_fmt_vid_cap() - TRY_FMT handler
  1324. * @file: file ptr
  1325. * @priv: file handle
  1326. * @fmt: ptr to v4l2 format structure
  1327. */
  1328. static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
  1329. struct v4l2_format *fmt)
  1330. {
  1331. struct vpif_fh *fh = priv;
  1332. struct channel_obj *ch = fh->channel;
  1333. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  1334. return vpif_check_format(ch, pixfmt, 1);
  1335. }
  1336. /**
  1337. * vpif_g_fmt_vid_cap() - Set INPUT handler
  1338. * @file: file ptr
  1339. * @priv: file handle
  1340. * @fmt: ptr to v4l2 format structure
  1341. */
  1342. static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
  1343. struct v4l2_format *fmt)
  1344. {
  1345. struct vpif_fh *fh = priv;
  1346. struct channel_obj *ch = fh->channel;
  1347. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1348. /* Check the validity of the buffer type */
  1349. if (common->fmt.type != fmt->type)
  1350. return -EINVAL;
  1351. /* Fill in the information about format */
  1352. *fmt = common->fmt;
  1353. return 0;
  1354. }
  1355. /**
  1356. * vpif_s_fmt_vid_cap() - Set FMT handler
  1357. * @file: file ptr
  1358. * @priv: file handle
  1359. * @fmt: ptr to v4l2 format structure
  1360. */
  1361. static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
  1362. struct v4l2_format *fmt)
  1363. {
  1364. struct vpif_fh *fh = priv;
  1365. struct channel_obj *ch = fh->channel;
  1366. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1367. struct v4l2_pix_format *pixfmt;
  1368. int ret = 0;
  1369. vpif_dbg(2, debug, "%s\n", __func__);
  1370. /* If streaming is started, return error */
  1371. if (common->started) {
  1372. vpif_dbg(1, debug, "Streaming is started\n");
  1373. return -EBUSY;
  1374. }
  1375. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1376. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1377. if (!fh->initialized) {
  1378. vpif_dbg(1, debug, "Channel Busy\n");
  1379. return -EBUSY;
  1380. }
  1381. }
  1382. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1383. if (0 != ret)
  1384. return ret;
  1385. fh->initialized = 1;
  1386. pixfmt = &fmt->fmt.pix;
  1387. /* Check for valid field format */
  1388. ret = vpif_check_format(ch, pixfmt, 0);
  1389. if (ret)
  1390. return ret;
  1391. /* store the format in the channel object */
  1392. common->fmt = *fmt;
  1393. return 0;
  1394. }
  1395. /**
  1396. * vpif_querycap() - QUERYCAP handler
  1397. * @file: file ptr
  1398. * @priv: file handle
  1399. * @cap: ptr to v4l2_capability structure
  1400. */
  1401. static int vpif_querycap(struct file *file, void *priv,
  1402. struct v4l2_capability *cap)
  1403. {
  1404. struct vpif_capture_config *config = vpif_dev->platform_data;
  1405. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1406. strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
  1407. strlcpy(cap->bus_info, "VPIF Platform", sizeof(cap->bus_info));
  1408. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  1409. return 0;
  1410. }
  1411. /**
  1412. * vpif_g_priority() - get priority handler
  1413. * @file: file ptr
  1414. * @priv: file handle
  1415. * @prio: ptr to v4l2_priority structure
  1416. */
  1417. static int vpif_g_priority(struct file *file, void *priv,
  1418. enum v4l2_priority *prio)
  1419. {
  1420. struct vpif_fh *fh = priv;
  1421. struct channel_obj *ch = fh->channel;
  1422. *prio = v4l2_prio_max(&ch->prio);
  1423. return 0;
  1424. }
  1425. /**
  1426. * vpif_s_priority() - set priority handler
  1427. * @file: file ptr
  1428. * @priv: file handle
  1429. * @prio: ptr to v4l2_priority structure
  1430. */
  1431. static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
  1432. {
  1433. struct vpif_fh *fh = priv;
  1434. struct channel_obj *ch = fh->channel;
  1435. return v4l2_prio_change(&ch->prio, &fh->prio, p);
  1436. }
  1437. /**
  1438. * vpif_cropcap() - cropcap handler
  1439. * @file: file ptr
  1440. * @priv: file handle
  1441. * @crop: ptr to v4l2_cropcap structure
  1442. */
  1443. static int vpif_cropcap(struct file *file, void *priv,
  1444. struct v4l2_cropcap *crop)
  1445. {
  1446. struct vpif_fh *fh = priv;
  1447. struct channel_obj *ch = fh->channel;
  1448. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1449. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
  1450. return -EINVAL;
  1451. crop->bounds.left = 0;
  1452. crop->bounds.top = 0;
  1453. crop->bounds.height = common->height;
  1454. crop->bounds.width = common->width;
  1455. crop->defrect = crop->bounds;
  1456. return 0;
  1457. }
  1458. /**
  1459. * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
  1460. * @file: file ptr
  1461. * @priv: file handle
  1462. * @preset: input preset
  1463. */
  1464. static int vpif_enum_dv_presets(struct file *file, void *priv,
  1465. struct v4l2_dv_enum_preset *preset)
  1466. {
  1467. struct vpif_fh *fh = priv;
  1468. struct channel_obj *ch = fh->channel;
  1469. return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
  1470. video, enum_dv_presets, preset);
  1471. }
  1472. /**
  1473. * vpif_query_dv_presets() - QUERY_DV_PRESET handler
  1474. * @file: file ptr
  1475. * @priv: file handle
  1476. * @preset: input preset
  1477. */
  1478. static int vpif_query_dv_preset(struct file *file, void *priv,
  1479. struct v4l2_dv_preset *preset)
  1480. {
  1481. struct vpif_fh *fh = priv;
  1482. struct channel_obj *ch = fh->channel;
  1483. return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
  1484. video, query_dv_preset, preset);
  1485. }
  1486. /**
  1487. * vpif_s_dv_presets() - S_DV_PRESETS handler
  1488. * @file: file ptr
  1489. * @priv: file handle
  1490. * @preset: input preset
  1491. */
  1492. static int vpif_s_dv_preset(struct file *file, void *priv,
  1493. struct v4l2_dv_preset *preset)
  1494. {
  1495. struct vpif_fh *fh = priv;
  1496. struct channel_obj *ch = fh->channel;
  1497. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1498. int ret = 0;
  1499. if (common->started) {
  1500. vpif_dbg(1, debug, "streaming in progress\n");
  1501. return -EBUSY;
  1502. }
  1503. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1504. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1505. if (!fh->initialized) {
  1506. vpif_dbg(1, debug, "Channel Busy\n");
  1507. return -EBUSY;
  1508. }
  1509. }
  1510. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1511. if (ret)
  1512. return ret;
  1513. fh->initialized = 1;
  1514. /* Call encoder subdevice function to set the standard */
  1515. if (mutex_lock_interruptible(&common->lock))
  1516. return -ERESTARTSYS;
  1517. ch->video.dv_preset = preset->preset;
  1518. ch->video.stdid = V4L2_STD_UNKNOWN;
  1519. memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
  1520. /* Get the information about the standard */
  1521. if (vpif_update_std_info(ch)) {
  1522. vpif_dbg(1, debug, "Error getting the standard info\n");
  1523. ret = -EINVAL;
  1524. } else {
  1525. /* Configure the default format information */
  1526. vpif_config_format(ch);
  1527. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
  1528. video, s_dv_preset, preset);
  1529. }
  1530. mutex_unlock(&common->lock);
  1531. return ret;
  1532. }
  1533. /**
  1534. * vpif_g_dv_presets() - G_DV_PRESETS handler
  1535. * @file: file ptr
  1536. * @priv: file handle
  1537. * @preset: input preset
  1538. */
  1539. static int vpif_g_dv_preset(struct file *file, void *priv,
  1540. struct v4l2_dv_preset *preset)
  1541. {
  1542. struct vpif_fh *fh = priv;
  1543. struct channel_obj *ch = fh->channel;
  1544. preset->preset = ch->video.dv_preset;
  1545. return 0;
  1546. }
  1547. /**
  1548. * vpif_s_dv_timings() - S_DV_TIMINGS handler
  1549. * @file: file ptr
  1550. * @priv: file handle
  1551. * @timings: digital video timings
  1552. */
  1553. static int vpif_s_dv_timings(struct file *file, void *priv,
  1554. struct v4l2_dv_timings *timings)
  1555. {
  1556. struct vpif_fh *fh = priv;
  1557. struct channel_obj *ch = fh->channel;
  1558. struct vpif_params *vpifparams = &ch->vpifparams;
  1559. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  1560. struct video_obj *vid_ch = &ch->video;
  1561. struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
  1562. int ret;
  1563. if (timings->type != V4L2_DV_BT_656_1120) {
  1564. vpif_dbg(2, debug, "Timing type not defined\n");
  1565. return -EINVAL;
  1566. }
  1567. /* Configure subdevice timings, if any */
  1568. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
  1569. video, s_dv_timings, timings);
  1570. if (ret == -ENOIOCTLCMD) {
  1571. vpif_dbg(2, debug, "Custom DV timings not supported by "
  1572. "subdevice\n");
  1573. return -EINVAL;
  1574. }
  1575. if (ret < 0) {
  1576. vpif_dbg(2, debug, "Error setting custom DV timings\n");
  1577. return ret;
  1578. }
  1579. if (!(timings->bt.width && timings->bt.height &&
  1580. (timings->bt.hbackporch ||
  1581. timings->bt.hfrontporch ||
  1582. timings->bt.hsync) &&
  1583. timings->bt.vfrontporch &&
  1584. (timings->bt.vbackporch ||
  1585. timings->bt.vsync))) {
  1586. vpif_dbg(2, debug, "Timings for width, height, "
  1587. "horizontal back porch, horizontal sync, "
  1588. "horizontal front porch, vertical back porch, "
  1589. "vertical sync and vertical back porch "
  1590. "must be defined\n");
  1591. return -EINVAL;
  1592. }
  1593. *bt = timings->bt;
  1594. /* Configure video port timings */
  1595. std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
  1596. bt->hsync - 8;
  1597. std_info->sav2eav = bt->width;
  1598. std_info->l1 = 1;
  1599. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  1600. if (bt->interlaced) {
  1601. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  1602. std_info->vsize = bt->height * 2 +
  1603. bt->vfrontporch + bt->vsync + bt->vbackporch +
  1604. bt->il_vfrontporch + bt->il_vsync +
  1605. bt->il_vbackporch;
  1606. std_info->l5 = std_info->vsize/2 -
  1607. (bt->vfrontporch - 1);
  1608. std_info->l7 = std_info->vsize/2 + 1;
  1609. std_info->l9 = std_info->l7 + bt->il_vsync +
  1610. bt->il_vbackporch + 1;
  1611. std_info->l11 = std_info->vsize -
  1612. (bt->il_vfrontporch - 1);
  1613. } else {
  1614. vpif_dbg(2, debug, "Required timing values for "
  1615. "interlaced BT format missing\n");
  1616. return -EINVAL;
  1617. }
  1618. } else {
  1619. std_info->vsize = bt->height + bt->vfrontporch +
  1620. bt->vsync + bt->vbackporch;
  1621. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  1622. }
  1623. strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
  1624. std_info->width = bt->width;
  1625. std_info->height = bt->height;
  1626. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  1627. std_info->ycmux_mode = 0;
  1628. std_info->capture_format = 0;
  1629. std_info->vbi_supported = 0;
  1630. std_info->hd_sd = 1;
  1631. std_info->stdid = 0;
  1632. std_info->dv_preset = V4L2_DV_INVALID;
  1633. vid_ch->stdid = 0;
  1634. vid_ch->dv_preset = V4L2_DV_INVALID;
  1635. return 0;
  1636. }
  1637. /**
  1638. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  1639. * @file: file ptr
  1640. * @priv: file handle
  1641. * @timings: digital video timings
  1642. */
  1643. static int vpif_g_dv_timings(struct file *file, void *priv,
  1644. struct v4l2_dv_timings *timings)
  1645. {
  1646. struct vpif_fh *fh = priv;
  1647. struct channel_obj *ch = fh->channel;
  1648. struct video_obj *vid_ch = &ch->video;
  1649. struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
  1650. timings->bt = *bt;
  1651. return 0;
  1652. }
  1653. /*
  1654. * vpif_g_chip_ident() - Identify the chip
  1655. * @file: file ptr
  1656. * @priv: file handle
  1657. * @chip: chip identity
  1658. *
  1659. * Returns zero or -EINVAL if read operations fails.
  1660. */
  1661. static int vpif_g_chip_ident(struct file *file, void *priv,
  1662. struct v4l2_dbg_chip_ident *chip)
  1663. {
  1664. chip->ident = V4L2_IDENT_NONE;
  1665. chip->revision = 0;
  1666. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  1667. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
  1668. vpif_dbg(2, debug, "match_type is invalid.\n");
  1669. return -EINVAL;
  1670. }
  1671. return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
  1672. g_chip_ident, chip);
  1673. }
  1674. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1675. /*
  1676. * vpif_dbg_g_register() - Read register
  1677. * @file: file ptr
  1678. * @priv: file handle
  1679. * @reg: register to be read
  1680. *
  1681. * Debugging only
  1682. * Returns zero or -EINVAL if read operations fails.
  1683. */
  1684. static int vpif_dbg_g_register(struct file *file, void *priv,
  1685. struct v4l2_dbg_register *reg){
  1686. struct vpif_fh *fh = priv;
  1687. struct channel_obj *ch = fh->channel;
  1688. return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
  1689. g_register, reg);
  1690. }
  1691. /*
  1692. * vpif_dbg_s_register() - Write to register
  1693. * @file: file ptr
  1694. * @priv: file handle
  1695. * @reg: register to be modified
  1696. *
  1697. * Debugging only
  1698. * Returns zero or -EINVAL if write operations fails.
  1699. */
  1700. static int vpif_dbg_s_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. s_register, reg);
  1706. }
  1707. #endif
  1708. /*
  1709. * vpif_log_status() - Status information
  1710. * @file: file ptr
  1711. * @priv: file handle
  1712. *
  1713. * Returns zero.
  1714. */
  1715. static int vpif_log_status(struct file *filep, void *priv)
  1716. {
  1717. /* status for sub devices */
  1718. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  1719. return 0;
  1720. }
  1721. /* vpif capture ioctl operations */
  1722. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1723. .vidioc_querycap = vpif_querycap,
  1724. .vidioc_g_priority = vpif_g_priority,
  1725. .vidioc_s_priority = vpif_s_priority,
  1726. .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
  1727. .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
  1728. .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
  1729. .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
  1730. .vidioc_enum_input = vpif_enum_input,
  1731. .vidioc_s_input = vpif_s_input,
  1732. .vidioc_g_input = vpif_g_input,
  1733. .vidioc_reqbufs = vpif_reqbufs,
  1734. .vidioc_querybuf = vpif_querybuf,
  1735. .vidioc_querystd = vpif_querystd,
  1736. .vidioc_s_std = vpif_s_std,
  1737. .vidioc_g_std = vpif_g_std,
  1738. .vidioc_qbuf = vpif_qbuf,
  1739. .vidioc_dqbuf = vpif_dqbuf,
  1740. .vidioc_streamon = vpif_streamon,
  1741. .vidioc_streamoff = vpif_streamoff,
  1742. .vidioc_cropcap = vpif_cropcap,
  1743. .vidioc_enum_dv_presets = vpif_enum_dv_presets,
  1744. .vidioc_s_dv_preset = vpif_s_dv_preset,
  1745. .vidioc_g_dv_preset = vpif_g_dv_preset,
  1746. .vidioc_query_dv_preset = vpif_query_dv_preset,
  1747. .vidioc_s_dv_timings = vpif_s_dv_timings,
  1748. .vidioc_g_dv_timings = vpif_g_dv_timings,
  1749. .vidioc_g_chip_ident = vpif_g_chip_ident,
  1750. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1751. .vidioc_g_register = vpif_dbg_g_register,
  1752. .vidioc_s_register = vpif_dbg_s_register,
  1753. #endif
  1754. .vidioc_log_status = vpif_log_status,
  1755. };
  1756. /* vpif file operations */
  1757. static struct v4l2_file_operations vpif_fops = {
  1758. .owner = THIS_MODULE,
  1759. .open = vpif_open,
  1760. .release = vpif_release,
  1761. .unlocked_ioctl = video_ioctl2,
  1762. .mmap = vpif_mmap,
  1763. .poll = vpif_poll
  1764. };
  1765. /* vpif video template */
  1766. static struct video_device vpif_video_template = {
  1767. .name = "vpif",
  1768. .fops = &vpif_fops,
  1769. .minor = -1,
  1770. .ioctl_ops = &vpif_ioctl_ops,
  1771. };
  1772. /**
  1773. * initialize_vpif() - Initialize vpif data structures
  1774. *
  1775. * Allocate memory for data structures and initialize them
  1776. */
  1777. static int initialize_vpif(void)
  1778. {
  1779. int err = 0, i, j;
  1780. int free_channel_objects_index;
  1781. /* Default number of buffers should be 3 */
  1782. if ((ch0_numbuffers > 0) &&
  1783. (ch0_numbuffers < config_params.min_numbuffers))
  1784. ch0_numbuffers = config_params.min_numbuffers;
  1785. if ((ch1_numbuffers > 0) &&
  1786. (ch1_numbuffers < config_params.min_numbuffers))
  1787. ch1_numbuffers = config_params.min_numbuffers;
  1788. /* Set buffer size to min buffers size if it is invalid */
  1789. if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
  1790. ch0_bufsize =
  1791. config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
  1792. if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
  1793. ch1_bufsize =
  1794. config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
  1795. config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
  1796. config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
  1797. if (ch0_numbuffers) {
  1798. config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
  1799. = ch0_bufsize;
  1800. }
  1801. if (ch1_numbuffers) {
  1802. config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
  1803. = ch1_bufsize;
  1804. }
  1805. /* Allocate memory for six channel objects */
  1806. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1807. vpif_obj.dev[i] =
  1808. kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
  1809. /* If memory allocation fails, return error */
  1810. if (!vpif_obj.dev[i]) {
  1811. free_channel_objects_index = i;
  1812. err = -ENOMEM;
  1813. goto vpif_init_free_channel_objects;
  1814. }
  1815. }
  1816. return 0;
  1817. vpif_init_free_channel_objects:
  1818. for (j = 0; j < free_channel_objects_index; j++)
  1819. kfree(vpif_obj.dev[j]);
  1820. return err;
  1821. }
  1822. /**
  1823. * vpif_probe : This function probes the vpif capture driver
  1824. * @pdev: platform device pointer
  1825. *
  1826. * This creates device entries by register itself to the V4L2 driver and
  1827. * initializes fields of each channel objects
  1828. */
  1829. static __init int vpif_probe(struct platform_device *pdev)
  1830. {
  1831. struct vpif_subdev_info *subdevdata;
  1832. struct vpif_capture_config *config;
  1833. int i, j, k, m, q, err;
  1834. struct i2c_adapter *i2c_adap;
  1835. struct channel_obj *ch;
  1836. struct common_obj *common;
  1837. struct video_device *vfd;
  1838. struct resource *res;
  1839. int subdev_count;
  1840. size_t size;
  1841. vpif_dev = &pdev->dev;
  1842. err = initialize_vpif();
  1843. if (err) {
  1844. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1845. return err;
  1846. }
  1847. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1848. if (err) {
  1849. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1850. return err;
  1851. }
  1852. k = 0;
  1853. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
  1854. for (i = res->start; i <= res->end; i++) {
  1855. if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
  1856. "VPIF_Capture",
  1857. (void *)(&vpif_obj.dev[k]->channel_id))) {
  1858. err = -EBUSY;
  1859. i--;
  1860. goto vpif_int_err;
  1861. }
  1862. }
  1863. k++;
  1864. }
  1865. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1866. /* Get the pointer to the channel object */
  1867. ch = vpif_obj.dev[i];
  1868. /* Allocate memory for video device */
  1869. vfd = video_device_alloc();
  1870. if (NULL == vfd) {
  1871. for (j = 0; j < i; j++) {
  1872. ch = vpif_obj.dev[j];
  1873. video_device_release(ch->video_dev);
  1874. }
  1875. err = -ENOMEM;
  1876. goto vpif_dev_alloc_err;
  1877. }
  1878. /* Initialize field of video device */
  1879. *vfd = vpif_video_template;
  1880. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1881. vfd->release = video_device_release;
  1882. snprintf(vfd->name, sizeof(vfd->name),
  1883. "VPIF_Capture_DRIVER_V%s",
  1884. VPIF_CAPTURE_VERSION);
  1885. /* Set video_dev to the video device */
  1886. ch->video_dev = vfd;
  1887. }
  1888. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1889. if (res) {
  1890. size = resource_size(res);
  1891. /* The resources are divided into two equal memory and when we
  1892. * have HD output we can add them together
  1893. */
  1894. for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
  1895. ch = vpif_obj.dev[j];
  1896. ch->channel_id = j;
  1897. /* only enabled if second resource exists */
  1898. config_params.video_limit[ch->channel_id] = 0;
  1899. if (size)
  1900. config_params.video_limit[ch->channel_id] =
  1901. size/2;
  1902. }
  1903. }
  1904. for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
  1905. ch = vpif_obj.dev[j];
  1906. ch->channel_id = j;
  1907. common = &(ch->common[VPIF_VIDEO_INDEX]);
  1908. spin_lock_init(&common->irqlock);
  1909. mutex_init(&common->lock);
  1910. /* Locking in file operations other than ioctl should be done
  1911. by the driver, not the V4L2 core.
  1912. This driver needs auditing so that this flag can be removed. */
  1913. set_bit(V4L2_FL_LOCK_ALL_FOPS, &ch->video_dev->flags);
  1914. ch->video_dev->lock = &common->lock;
  1915. /* Initialize prio member of channel object */
  1916. v4l2_prio_init(&ch->prio);
  1917. err = video_register_device(ch->video_dev,
  1918. VFL_TYPE_GRABBER, (j ? 1 : 0));
  1919. if (err)
  1920. goto probe_out;
  1921. video_set_drvdata(ch->video_dev, ch);
  1922. }
  1923. i2c_adap = i2c_get_adapter(1);
  1924. config = pdev->dev.platform_data;
  1925. subdev_count = config->subdev_count;
  1926. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1927. GFP_KERNEL);
  1928. if (vpif_obj.sd == NULL) {
  1929. vpif_err("unable to allocate memory for subdevice pointers\n");
  1930. err = -ENOMEM;
  1931. goto probe_out;
  1932. }
  1933. for (i = 0; i < subdev_count; i++) {
  1934. subdevdata = &config->subdev_info[i];
  1935. vpif_obj.sd[i] =
  1936. v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1937. i2c_adap,
  1938. &subdevdata->board_info,
  1939. NULL);
  1940. if (!vpif_obj.sd[i]) {
  1941. vpif_err("Error registering v4l2 subdevice\n");
  1942. goto probe_subdev_out;
  1943. }
  1944. v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
  1945. subdevdata->name);
  1946. if (vpif_obj.sd[i])
  1947. vpif_obj.sd[i]->grp_id = 1 << i;
  1948. }
  1949. v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
  1950. return 0;
  1951. probe_subdev_out:
  1952. /* free sub devices memory */
  1953. kfree(vpif_obj.sd);
  1954. j = VPIF_CAPTURE_MAX_DEVICES;
  1955. probe_out:
  1956. for (k = 0; k < j; k++) {
  1957. /* Get the pointer to the channel object */
  1958. ch = vpif_obj.dev[k];
  1959. /* Unregister video device */
  1960. video_unregister_device(ch->video_dev);
  1961. }
  1962. vpif_dev_alloc_err:
  1963. k = VPIF_CAPTURE_MAX_DEVICES-1;
  1964. res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
  1965. i = res->end;
  1966. vpif_int_err:
  1967. for (q = k; q >= 0; q--) {
  1968. for (m = i; m >= (int)res->start; m--)
  1969. free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
  1970. res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
  1971. if (res)
  1972. i = res->end;
  1973. }
  1974. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1975. return err;
  1976. }
  1977. /**
  1978. * vpif_remove() - driver remove handler
  1979. * @device: ptr to platform device structure
  1980. *
  1981. * The vidoe device is unregistered
  1982. */
  1983. static int vpif_remove(struct platform_device *device)
  1984. {
  1985. int i;
  1986. struct channel_obj *ch;
  1987. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1988. /* un-register device */
  1989. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1990. /* Get the pointer to the channel object */
  1991. ch = vpif_obj.dev[i];
  1992. /* Unregister video device */
  1993. video_unregister_device(ch->video_dev);
  1994. }
  1995. return 0;
  1996. }
  1997. #ifdef CONFIG_PM
  1998. /**
  1999. * vpif_suspend: vpif device suspend
  2000. */
  2001. static int vpif_suspend(struct device *dev)
  2002. {
  2003. struct common_obj *common;
  2004. struct channel_obj *ch;
  2005. int i;
  2006. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  2007. /* Get the pointer to the channel object */
  2008. ch = vpif_obj.dev[i];
  2009. common = &ch->common[VPIF_VIDEO_INDEX];
  2010. mutex_lock(&common->lock);
  2011. if (ch->usrs && common->io_usrs) {
  2012. /* Disable channel */
  2013. if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
  2014. enable_channel0(0);
  2015. channel0_intr_enable(0);
  2016. }
  2017. if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
  2018. common->started == 2) {
  2019. enable_channel1(0);
  2020. channel1_intr_enable(0);
  2021. }
  2022. }
  2023. mutex_unlock(&common->lock);
  2024. }
  2025. return 0;
  2026. }
  2027. /*
  2028. * vpif_resume: vpif device suspend
  2029. */
  2030. static int vpif_resume(struct device *dev)
  2031. {
  2032. struct common_obj *common;
  2033. struct channel_obj *ch;
  2034. int i;
  2035. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  2036. /* Get the pointer to the channel object */
  2037. ch = vpif_obj.dev[i];
  2038. common = &ch->common[VPIF_VIDEO_INDEX];
  2039. mutex_lock(&common->lock);
  2040. if (ch->usrs && common->io_usrs) {
  2041. /* Disable channel */
  2042. if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
  2043. enable_channel0(1);
  2044. channel0_intr_enable(1);
  2045. }
  2046. if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
  2047. common->started == 2) {
  2048. enable_channel1(1);
  2049. channel1_intr_enable(1);
  2050. }
  2051. }
  2052. mutex_unlock(&common->lock);
  2053. }
  2054. return 0;
  2055. }
  2056. static const struct dev_pm_ops vpif_dev_pm_ops = {
  2057. .suspend = vpif_suspend,
  2058. .resume = vpif_resume,
  2059. };
  2060. #define vpif_pm_ops (&vpif_dev_pm_ops)
  2061. #else
  2062. #define vpif_pm_ops NULL
  2063. #endif
  2064. static __refdata struct platform_driver vpif_driver = {
  2065. .driver = {
  2066. .name = "vpif_capture",
  2067. .owner = THIS_MODULE,
  2068. .pm = vpif_pm_ops,
  2069. },
  2070. .probe = vpif_probe,
  2071. .remove = vpif_remove,
  2072. };
  2073. /**
  2074. * vpif_init: initialize the vpif driver
  2075. *
  2076. * This function registers device and driver to the kernel, requests irq
  2077. * handler and allocates memory
  2078. * for channel objects
  2079. */
  2080. static __init int vpif_init(void)
  2081. {
  2082. return platform_driver_register(&vpif_driver);
  2083. }
  2084. /**
  2085. * vpif_cleanup : This function clean up the vpif capture resources
  2086. *
  2087. * This will un-registers device and driver to the kernel, frees
  2088. * requested irq handler and de-allocates memory allocated for channel
  2089. * objects.
  2090. */
  2091. static void vpif_cleanup(void)
  2092. {
  2093. struct platform_device *pdev;
  2094. struct resource *res;
  2095. int irq_num;
  2096. int i = 0;
  2097. pdev = container_of(vpif_dev, struct platform_device, dev);
  2098. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
  2099. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  2100. free_irq(irq_num,
  2101. (void *)(&vpif_obj.dev[i]->channel_id));
  2102. i++;
  2103. }
  2104. platform_driver_unregister(&vpif_driver);
  2105. kfree(vpif_obj.sd);
  2106. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
  2107. kfree(vpif_obj.dev[i]);
  2108. }
  2109. /* Function for module initialization and cleanup */
  2110. module_init(vpif_init);
  2111. module_exit(vpif_cleanup);