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