vpif_capture.c 61 KB

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