vpif_capture.c 62 KB

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