vpif_display.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  1. /*
  2. * vpif-display - VPIF display driver
  3. * Display driver for TI DaVinci VPIF
  4. *
  5. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  10. *
  11. * This program is distributed .as is. WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/mm.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/string.h>
  25. #include <linux/videodev2.h>
  26. #include <linux/wait.h>
  27. #include <linux/time.h>
  28. #include <linux/i2c.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/io.h>
  31. #include <linux/slab.h>
  32. #include <asm/irq.h>
  33. #include <asm/page.h>
  34. #include <media/adv7343.h>
  35. #include <media/v4l2-device.h>
  36. #include <media/v4l2-ioctl.h>
  37. #include <media/v4l2-chip-ident.h>
  38. #include "vpif_display.h"
  39. #include "vpif.h"
  40. MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
  41. MODULE_LICENSE("GPL");
  42. MODULE_VERSION(VPIF_DISPLAY_VERSION);
  43. #define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
  44. #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
  45. #define vpif_dbg(level, debug, fmt, arg...) \
  46. v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
  47. static int debug = 1;
  48. static u32 ch2_numbuffers = 3;
  49. static u32 ch3_numbuffers = 3;
  50. static u32 ch2_bufsize = 1920 * 1080 * 2;
  51. static u32 ch3_bufsize = 720 * 576 * 2;
  52. module_param(debug, int, 0644);
  53. module_param(ch2_numbuffers, uint, S_IRUGO);
  54. module_param(ch3_numbuffers, uint, S_IRUGO);
  55. module_param(ch2_bufsize, uint, S_IRUGO);
  56. module_param(ch3_bufsize, uint, S_IRUGO);
  57. MODULE_PARM_DESC(debug, "Debug level 0-1");
  58. MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
  59. MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
  60. MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
  61. MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
  62. static struct vpif_config_params config_params = {
  63. .min_numbuffers = 3,
  64. .numbuffers[0] = 3,
  65. .numbuffers[1] = 3,
  66. .min_bufsize[0] = 720 * 480 * 2,
  67. .min_bufsize[1] = 720 * 480 * 2,
  68. .channel_bufsize[0] = 1920 * 1080 * 2,
  69. .channel_bufsize[1] = 720 * 576 * 2,
  70. };
  71. static struct vpif_device vpif_obj = { {NULL} };
  72. static struct device *vpif_dev;
  73. static void vpif_calculate_offsets(struct channel_obj *ch);
  74. static void vpif_config_addr(struct channel_obj *ch, int muxmode);
  75. /*
  76. * buffer_prepare: This is the callback function called from vb2_qbuf()
  77. * function the buffer is prepared and user space virtual address is converted
  78. * into physical address
  79. */
  80. static int vpif_buffer_prepare(struct vb2_buffer *vb)
  81. {
  82. struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  83. struct vb2_queue *q = vb->vb2_queue;
  84. struct common_obj *common;
  85. unsigned long addr;
  86. common = &fh->channel->common[VPIF_VIDEO_INDEX];
  87. if (vb->state != VB2_BUF_STATE_ACTIVE &&
  88. vb->state != VB2_BUF_STATE_PREPARED) {
  89. vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
  90. if (vb2_plane_vaddr(vb, 0) &&
  91. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  92. goto buf_align_exit;
  93. addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  94. if (q->streaming &&
  95. (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
  96. if (!ISALIGNED(addr + common->ytop_off) ||
  97. !ISALIGNED(addr + common->ybtm_off) ||
  98. !ISALIGNED(addr + common->ctop_off) ||
  99. !ISALIGNED(addr + common->cbtm_off))
  100. goto buf_align_exit;
  101. }
  102. }
  103. return 0;
  104. buf_align_exit:
  105. vpif_err("buffer offset not aligned to 8 bytes\n");
  106. return -EINVAL;
  107. }
  108. /*
  109. * vpif_buffer_queue_setup: This function allocates memory for the buffers
  110. */
  111. static int vpif_buffer_queue_setup(struct vb2_queue *vq,
  112. const struct v4l2_format *fmt,
  113. unsigned int *nbuffers, unsigned int *nplanes,
  114. unsigned int sizes[], void *alloc_ctxs[])
  115. {
  116. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  117. struct channel_obj *ch = fh->channel;
  118. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  119. unsigned long size;
  120. if (V4L2_MEMORY_MMAP == common->memory) {
  121. size = config_params.channel_bufsize[ch->channel_id];
  122. /*
  123. * Checking if the buffer size exceeds the available buffer
  124. * ycmux_mode = 0 means 1 channel mode HD and
  125. * ycmux_mode = 1 means 2 channels mode SD
  126. */
  127. if (ch->vpifparams.std_info.ycmux_mode == 0) {
  128. if (config_params.video_limit[ch->channel_id])
  129. while (size * *nbuffers >
  130. (config_params.video_limit[0]
  131. + config_params.video_limit[1]))
  132. (*nbuffers)--;
  133. } else {
  134. if (config_params.video_limit[ch->channel_id])
  135. while (size * *nbuffers >
  136. config_params.video_limit[ch->channel_id])
  137. (*nbuffers)--;
  138. }
  139. } else {
  140. size = common->fmt.fmt.pix.sizeimage;
  141. }
  142. if (*nbuffers < config_params.min_numbuffers)
  143. *nbuffers = config_params.min_numbuffers;
  144. *nplanes = 1;
  145. sizes[0] = size;
  146. alloc_ctxs[0] = common->alloc_ctx;
  147. return 0;
  148. }
  149. /*
  150. * vpif_buffer_queue: This function adds the buffer to DMA queue
  151. */
  152. static void vpif_buffer_queue(struct vb2_buffer *vb)
  153. {
  154. struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  155. struct vpif_disp_buffer *buf = container_of(vb,
  156. struct vpif_disp_buffer, vb);
  157. struct channel_obj *ch = fh->channel;
  158. struct common_obj *common;
  159. unsigned long flags;
  160. common = &ch->common[VPIF_VIDEO_INDEX];
  161. /* add the buffer to the DMA queue */
  162. spin_lock_irqsave(&common->irqlock, flags);
  163. list_add_tail(&buf->list, &common->dma_queue);
  164. spin_unlock_irqrestore(&common->irqlock, flags);
  165. }
  166. /*
  167. * vpif_buf_cleanup: This function is called from the videobuf2 layer to
  168. * free memory allocated to the buffers
  169. */
  170. static void vpif_buf_cleanup(struct vb2_buffer *vb)
  171. {
  172. struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  173. struct vpif_disp_buffer *buf = container_of(vb,
  174. struct vpif_disp_buffer, vb);
  175. struct channel_obj *ch = fh->channel;
  176. struct common_obj *common;
  177. unsigned long flags;
  178. common = &ch->common[VPIF_VIDEO_INDEX];
  179. spin_lock_irqsave(&common->irqlock, flags);
  180. if (vb->state == VB2_BUF_STATE_ACTIVE)
  181. list_del_init(&buf->list);
  182. spin_unlock_irqrestore(&common->irqlock, flags);
  183. }
  184. static void vpif_wait_prepare(struct vb2_queue *vq)
  185. {
  186. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  187. struct channel_obj *ch = fh->channel;
  188. struct common_obj *common;
  189. common = &ch->common[VPIF_VIDEO_INDEX];
  190. mutex_unlock(&common->lock);
  191. }
  192. static void vpif_wait_finish(struct vb2_queue *vq)
  193. {
  194. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  195. struct channel_obj *ch = fh->channel;
  196. struct common_obj *common;
  197. common = &ch->common[VPIF_VIDEO_INDEX];
  198. mutex_lock(&common->lock);
  199. }
  200. static int vpif_buffer_init(struct vb2_buffer *vb)
  201. {
  202. struct vpif_disp_buffer *buf = container_of(vb,
  203. struct vpif_disp_buffer, vb);
  204. INIT_LIST_HEAD(&buf->list);
  205. return 0;
  206. }
  207. static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
  208. static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
  209. {
  210. struct vpif_display_config *vpif_config_data =
  211. vpif_dev->platform_data;
  212. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  213. struct channel_obj *ch = fh->channel;
  214. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  215. struct vpif_params *vpif = &ch->vpifparams;
  216. unsigned long addr = 0;
  217. unsigned long flags;
  218. int ret;
  219. /* If buffer queue is empty, return error */
  220. spin_lock_irqsave(&common->irqlock, flags);
  221. if (list_empty(&common->dma_queue)) {
  222. spin_unlock_irqrestore(&common->irqlock, flags);
  223. vpif_err("buffer queue is empty\n");
  224. return -EIO;
  225. }
  226. /* Get the next frame from the buffer queue */
  227. common->next_frm = common->cur_frm =
  228. list_entry(common->dma_queue.next,
  229. struct vpif_disp_buffer, list);
  230. list_del(&common->cur_frm->list);
  231. spin_unlock_irqrestore(&common->irqlock, flags);
  232. /* Mark state of the current frame to active */
  233. common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  234. /* Initialize field_id and started member */
  235. ch->field_id = 0;
  236. common->started = 1;
  237. addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
  238. /* Calculate the offset for Y and C data in the buffer */
  239. vpif_calculate_offsets(ch);
  240. if ((ch->vpifparams.std_info.frm_fmt &&
  241. ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
  242. && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
  243. || (!ch->vpifparams.std_info.frm_fmt
  244. && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
  245. vpif_err("conflict in field format and std format\n");
  246. return -EINVAL;
  247. }
  248. /* clock settings */
  249. if (vpif_config_data->set_clock) {
  250. ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
  251. ycmux_mode, ch->vpifparams.std_info.hd_sd);
  252. if (ret < 0) {
  253. vpif_err("can't set clock\n");
  254. return ret;
  255. }
  256. }
  257. /* set the parameters and addresses */
  258. ret = vpif_set_video_params(vpif, ch->channel_id + 2);
  259. if (ret < 0)
  260. return ret;
  261. common->started = ret;
  262. vpif_config_addr(ch, ret);
  263. common->set_addr((addr + common->ytop_off),
  264. (addr + common->ybtm_off),
  265. (addr + common->ctop_off),
  266. (addr + common->cbtm_off));
  267. /* Set interrupt for both the fields in VPIF
  268. Register enable channel in VPIF register */
  269. channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
  270. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  271. channel2_intr_assert();
  272. channel2_intr_enable(1);
  273. enable_channel2(1);
  274. if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
  275. channel2_clipping_enable(1);
  276. }
  277. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
  278. || (common->started == 2)) {
  279. channel3_intr_assert();
  280. channel3_intr_enable(1);
  281. enable_channel3(1);
  282. if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
  283. channel3_clipping_enable(1);
  284. }
  285. return 0;
  286. }
  287. /* abort streaming and wait for last buffer */
  288. static int vpif_stop_streaming(struct vb2_queue *vq)
  289. {
  290. struct vpif_fh *fh = vb2_get_drv_priv(vq);
  291. struct channel_obj *ch = fh->channel;
  292. struct common_obj *common;
  293. unsigned long flags;
  294. if (!vb2_is_streaming(vq))
  295. return 0;
  296. common = &ch->common[VPIF_VIDEO_INDEX];
  297. /* release all active buffers */
  298. spin_lock_irqsave(&common->irqlock, flags);
  299. while (!list_empty(&common->dma_queue)) {
  300. common->next_frm = list_entry(common->dma_queue.next,
  301. struct vpif_disp_buffer, list);
  302. list_del(&common->next_frm->list);
  303. vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
  304. }
  305. spin_unlock_irqrestore(&common->irqlock, flags);
  306. return 0;
  307. }
  308. static struct vb2_ops video_qops = {
  309. .queue_setup = vpif_buffer_queue_setup,
  310. .wait_prepare = vpif_wait_prepare,
  311. .wait_finish = vpif_wait_finish,
  312. .buf_init = vpif_buffer_init,
  313. .buf_prepare = vpif_buffer_prepare,
  314. .start_streaming = vpif_start_streaming,
  315. .stop_streaming = vpif_stop_streaming,
  316. .buf_cleanup = vpif_buf_cleanup,
  317. .buf_queue = vpif_buffer_queue,
  318. };
  319. static void process_progressive_mode(struct common_obj *common)
  320. {
  321. unsigned long addr = 0;
  322. spin_lock(&common->irqlock);
  323. /* Get the next buffer from buffer queue */
  324. common->next_frm = list_entry(common->dma_queue.next,
  325. struct vpif_disp_buffer, list);
  326. /* Remove that buffer from the buffer queue */
  327. list_del(&common->next_frm->list);
  328. spin_unlock(&common->irqlock);
  329. /* Mark status of the buffer as active */
  330. common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  331. /* Set top and bottom field addrs in VPIF registers */
  332. addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
  333. common->set_addr(addr + common->ytop_off,
  334. addr + common->ybtm_off,
  335. addr + common->ctop_off,
  336. addr + common->cbtm_off);
  337. }
  338. static void process_interlaced_mode(int fid, struct common_obj *common)
  339. {
  340. /* device field id and local field id are in sync */
  341. /* If this is even field */
  342. if (0 == fid) {
  343. if (common->cur_frm == common->next_frm)
  344. return;
  345. /* one frame is displayed If next frame is
  346. * available, release cur_frm and move on */
  347. /* Copy frame display time */
  348. v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
  349. /* Change status of the cur_frm */
  350. vb2_buffer_done(&common->cur_frm->vb,
  351. VB2_BUF_STATE_DONE);
  352. /* Make cur_frm pointing to next_frm */
  353. common->cur_frm = common->next_frm;
  354. } else if (1 == fid) { /* odd field */
  355. spin_lock(&common->irqlock);
  356. if (list_empty(&common->dma_queue)
  357. || (common->cur_frm != common->next_frm)) {
  358. spin_unlock(&common->irqlock);
  359. return;
  360. }
  361. spin_unlock(&common->irqlock);
  362. /* one field is displayed configure the next
  363. * frame if it is available else hold on current
  364. * frame */
  365. /* Get next from the buffer queue */
  366. process_progressive_mode(common);
  367. }
  368. }
  369. /*
  370. * vpif_channel_isr: It changes status of the displayed buffer, takes next
  371. * buffer from the queue and sets its address in VPIF registers
  372. */
  373. static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
  374. {
  375. struct vpif_device *dev = &vpif_obj;
  376. struct channel_obj *ch;
  377. struct common_obj *common;
  378. enum v4l2_field field;
  379. int fid = -1, i;
  380. int channel_id = 0;
  381. channel_id = *(int *)(dev_id);
  382. if (!vpif_intr_status(channel_id + 2))
  383. return IRQ_NONE;
  384. ch = dev->dev[channel_id];
  385. field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
  386. for (i = 0; i < VPIF_NUMOBJECTS; i++) {
  387. common = &ch->common[i];
  388. /* If streaming is started in this channel */
  389. if (0 == common->started)
  390. continue;
  391. if (1 == ch->vpifparams.std_info.frm_fmt) {
  392. spin_lock(&common->irqlock);
  393. if (list_empty(&common->dma_queue)) {
  394. spin_unlock(&common->irqlock);
  395. continue;
  396. }
  397. spin_unlock(&common->irqlock);
  398. /* Progressive mode */
  399. if (!channel_first_int[i][channel_id]) {
  400. /* Mark status of the cur_frm to
  401. * done and unlock semaphore on it */
  402. v4l2_get_timestamp(&common->cur_frm->vb.
  403. v4l2_buf.timestamp);
  404. vb2_buffer_done(&common->cur_frm->vb,
  405. VB2_BUF_STATE_DONE);
  406. /* Make cur_frm pointing to next_frm */
  407. common->cur_frm = common->next_frm;
  408. }
  409. channel_first_int[i][channel_id] = 0;
  410. process_progressive_mode(common);
  411. } else {
  412. /* Interlaced mode */
  413. /* If it is first interrupt, ignore it */
  414. if (channel_first_int[i][channel_id]) {
  415. channel_first_int[i][channel_id] = 0;
  416. continue;
  417. }
  418. if (0 == i) {
  419. ch->field_id ^= 1;
  420. /* Get field id from VPIF registers */
  421. fid = vpif_channel_getfid(ch->channel_id + 2);
  422. /* If fid does not match with stored field id */
  423. if (fid != ch->field_id) {
  424. /* Make them in sync */
  425. if (0 == fid)
  426. ch->field_id = fid;
  427. return IRQ_HANDLED;
  428. }
  429. }
  430. process_interlaced_mode(fid, common);
  431. }
  432. }
  433. return IRQ_HANDLED;
  434. }
  435. static int vpif_update_std_info(struct channel_obj *ch)
  436. {
  437. struct video_obj *vid_ch = &ch->video;
  438. struct vpif_params *vpifparams = &ch->vpifparams;
  439. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  440. const struct vpif_channel_config_params *config;
  441. int i;
  442. for (i = 0; i < vpif_ch_params_count; i++) {
  443. config = &ch_params[i];
  444. if (config->hd_sd == 0) {
  445. vpif_dbg(2, debug, "SD format\n");
  446. if (config->stdid & vid_ch->stdid) {
  447. memcpy(std_info, config, sizeof(*config));
  448. break;
  449. }
  450. }
  451. }
  452. if (i == vpif_ch_params_count) {
  453. vpif_dbg(1, debug, "Format not found\n");
  454. return -EINVAL;
  455. }
  456. return 0;
  457. }
  458. static int vpif_update_resolution(struct channel_obj *ch)
  459. {
  460. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  461. struct video_obj *vid_ch = &ch->video;
  462. struct vpif_params *vpifparams = &ch->vpifparams;
  463. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  464. if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
  465. return -EINVAL;
  466. if (vid_ch->stdid) {
  467. if (vpif_update_std_info(ch))
  468. return -EINVAL;
  469. }
  470. common->fmt.fmt.pix.width = std_info->width;
  471. common->fmt.fmt.pix.height = std_info->height;
  472. vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
  473. common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
  474. /* Set height and width paramateres */
  475. common->height = std_info->height;
  476. common->width = std_info->width;
  477. return 0;
  478. }
  479. /*
  480. * vpif_calculate_offsets: This function calculates buffers offset for Y and C
  481. * in the top and bottom field
  482. */
  483. static void vpif_calculate_offsets(struct channel_obj *ch)
  484. {
  485. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  486. struct vpif_params *vpifparams = &ch->vpifparams;
  487. enum v4l2_field field = common->fmt.fmt.pix.field;
  488. struct video_obj *vid_ch = &ch->video;
  489. unsigned int hpitch, vpitch, sizeimage;
  490. if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
  491. if (ch->vpifparams.std_info.frm_fmt)
  492. vid_ch->buf_field = V4L2_FIELD_NONE;
  493. else
  494. vid_ch->buf_field = V4L2_FIELD_INTERLACED;
  495. } else {
  496. vid_ch->buf_field = common->fmt.fmt.pix.field;
  497. }
  498. sizeimage = common->fmt.fmt.pix.sizeimage;
  499. hpitch = common->fmt.fmt.pix.bytesperline;
  500. vpitch = sizeimage / (hpitch * 2);
  501. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  502. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  503. common->ytop_off = 0;
  504. common->ybtm_off = hpitch;
  505. common->ctop_off = sizeimage / 2;
  506. common->cbtm_off = sizeimage / 2 + hpitch;
  507. } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
  508. common->ytop_off = 0;
  509. common->ybtm_off = sizeimage / 4;
  510. common->ctop_off = sizeimage / 2;
  511. common->cbtm_off = common->ctop_off + sizeimage / 4;
  512. } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
  513. common->ybtm_off = 0;
  514. common->ytop_off = sizeimage / 4;
  515. common->cbtm_off = sizeimage / 2;
  516. common->ctop_off = common->cbtm_off + sizeimage / 4;
  517. }
  518. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  519. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  520. vpifparams->video_params.storage_mode = 1;
  521. } else {
  522. vpifparams->video_params.storage_mode = 0;
  523. }
  524. if (ch->vpifparams.std_info.frm_fmt == 1) {
  525. vpifparams->video_params.hpitch =
  526. common->fmt.fmt.pix.bytesperline;
  527. } else {
  528. if ((field == V4L2_FIELD_ANY) ||
  529. (field == V4L2_FIELD_INTERLACED))
  530. vpifparams->video_params.hpitch =
  531. common->fmt.fmt.pix.bytesperline * 2;
  532. else
  533. vpifparams->video_params.hpitch =
  534. common->fmt.fmt.pix.bytesperline;
  535. }
  536. ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
  537. }
  538. static void vpif_config_format(struct channel_obj *ch)
  539. {
  540. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  541. common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
  542. if (config_params.numbuffers[ch->channel_id] == 0)
  543. common->memory = V4L2_MEMORY_USERPTR;
  544. else
  545. common->memory = V4L2_MEMORY_MMAP;
  546. common->fmt.fmt.pix.sizeimage =
  547. config_params.channel_bufsize[ch->channel_id];
  548. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  549. common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  550. }
  551. static int vpif_check_format(struct channel_obj *ch,
  552. struct v4l2_pix_format *pixfmt)
  553. {
  554. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  555. enum v4l2_field field = pixfmt->field;
  556. u32 sizeimage, hpitch, vpitch;
  557. if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
  558. goto invalid_fmt_exit;
  559. if (!(VPIF_VALID_FIELD(field)))
  560. goto invalid_fmt_exit;
  561. if (pixfmt->bytesperline <= 0)
  562. goto invalid_pitch_exit;
  563. sizeimage = pixfmt->sizeimage;
  564. if (vpif_update_resolution(ch))
  565. return -EINVAL;
  566. hpitch = pixfmt->bytesperline;
  567. vpitch = sizeimage / (hpitch * 2);
  568. /* Check for valid value of pitch */
  569. if ((hpitch < ch->vpifparams.std_info.width) ||
  570. (vpitch < ch->vpifparams.std_info.height))
  571. goto invalid_pitch_exit;
  572. /* Check for 8 byte alignment */
  573. if (!ISALIGNED(hpitch)) {
  574. vpif_err("invalid pitch alignment\n");
  575. return -EINVAL;
  576. }
  577. pixfmt->width = common->fmt.fmt.pix.width;
  578. pixfmt->height = common->fmt.fmt.pix.height;
  579. return 0;
  580. invalid_fmt_exit:
  581. vpif_err("invalid field format\n");
  582. return -EINVAL;
  583. invalid_pitch_exit:
  584. vpif_err("invalid pitch\n");
  585. return -EINVAL;
  586. }
  587. static void vpif_config_addr(struct channel_obj *ch, int muxmode)
  588. {
  589. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  590. if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
  591. common->set_addr = ch3_set_videobuf_addr;
  592. } else {
  593. if (2 == muxmode)
  594. common->set_addr = ch2_set_videobuf_addr_yc_nmux;
  595. else
  596. common->set_addr = ch2_set_videobuf_addr;
  597. }
  598. }
  599. /*
  600. * vpif_mmap: It is used to map kernel space buffers into user spaces
  601. */
  602. static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
  603. {
  604. struct vpif_fh *fh = filep->private_data;
  605. struct channel_obj *ch = fh->channel;
  606. struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
  607. int ret;
  608. vpif_dbg(2, debug, "vpif_mmap\n");
  609. if (mutex_lock_interruptible(&common->lock))
  610. return -ERESTARTSYS;
  611. ret = vb2_mmap(&common->buffer_queue, vma);
  612. mutex_unlock(&common->lock);
  613. return ret;
  614. }
  615. /*
  616. * vpif_poll: It is used for select/poll system call
  617. */
  618. static unsigned int vpif_poll(struct file *filep, poll_table *wait)
  619. {
  620. struct vpif_fh *fh = filep->private_data;
  621. struct channel_obj *ch = fh->channel;
  622. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  623. unsigned int res = 0;
  624. if (common->started) {
  625. mutex_lock(&common->lock);
  626. res = vb2_poll(&common->buffer_queue, filep, wait);
  627. mutex_unlock(&common->lock);
  628. }
  629. return res;
  630. }
  631. /*
  632. * vpif_open: It creates object of file handle structure and stores it in
  633. * private_data member of filepointer
  634. */
  635. static int vpif_open(struct file *filep)
  636. {
  637. struct video_device *vdev = video_devdata(filep);
  638. struct channel_obj *ch = video_get_drvdata(vdev);
  639. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  640. struct vpif_fh *fh;
  641. /* Allocate memory for the file handle object */
  642. fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
  643. if (fh == NULL) {
  644. vpif_err("unable to allocate memory for file handle object\n");
  645. return -ENOMEM;
  646. }
  647. if (mutex_lock_interruptible(&common->lock)) {
  648. kfree(fh);
  649. return -ERESTARTSYS;
  650. }
  651. /* store pointer to fh in private_data member of filep */
  652. filep->private_data = fh;
  653. fh->channel = ch;
  654. fh->initialized = 0;
  655. if (!ch->initialized) {
  656. fh->initialized = 1;
  657. ch->initialized = 1;
  658. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  659. }
  660. /* Increment channel usrs counter */
  661. atomic_inc(&ch->usrs);
  662. /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
  663. fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
  664. /* Initialize priority of this instance to default priority */
  665. fh->prio = V4L2_PRIORITY_UNSET;
  666. v4l2_prio_open(&ch->prio, &fh->prio);
  667. mutex_unlock(&common->lock);
  668. return 0;
  669. }
  670. /*
  671. * vpif_release: This function deletes buffer queue, frees the buffers and
  672. * the vpif file handle
  673. */
  674. static int vpif_release(struct file *filep)
  675. {
  676. struct vpif_fh *fh = filep->private_data;
  677. struct channel_obj *ch = fh->channel;
  678. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  679. mutex_lock(&common->lock);
  680. /* if this instance is doing IO */
  681. if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
  682. /* Reset io_usrs member of channel object */
  683. common->io_usrs = 0;
  684. /* Disable channel */
  685. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  686. enable_channel2(0);
  687. channel2_intr_enable(0);
  688. }
  689. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  690. (2 == common->started)) {
  691. enable_channel3(0);
  692. channel3_intr_enable(0);
  693. }
  694. common->started = 0;
  695. /* Free buffers allocated */
  696. vb2_queue_release(&common->buffer_queue);
  697. vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
  698. common->numbuffers =
  699. config_params.numbuffers[ch->channel_id];
  700. }
  701. /* Decrement channel usrs counter */
  702. atomic_dec(&ch->usrs);
  703. /* If this file handle has initialize encoder device, reset it */
  704. if (fh->initialized)
  705. ch->initialized = 0;
  706. /* Close the priority */
  707. v4l2_prio_close(&ch->prio, fh->prio);
  708. filep->private_data = NULL;
  709. fh->initialized = 0;
  710. mutex_unlock(&common->lock);
  711. kfree(fh);
  712. return 0;
  713. }
  714. /* functions implementing ioctls */
  715. /**
  716. * vpif_querycap() - QUERYCAP handler
  717. * @file: file ptr
  718. * @priv: file handle
  719. * @cap: ptr to v4l2_capability structure
  720. */
  721. static int vpif_querycap(struct file *file, void *priv,
  722. struct v4l2_capability *cap)
  723. {
  724. struct vpif_display_config *config = vpif_dev->platform_data;
  725. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  726. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  727. snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
  728. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  729. dev_name(vpif_dev));
  730. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  731. return 0;
  732. }
  733. static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
  734. struct v4l2_fmtdesc *fmt)
  735. {
  736. if (fmt->index != 0) {
  737. vpif_err("Invalid format index\n");
  738. return -EINVAL;
  739. }
  740. /* Fill in the information about format */
  741. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  742. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  743. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  744. return 0;
  745. }
  746. static int vpif_g_fmt_vid_out(struct file *file, void *priv,
  747. struct v4l2_format *fmt)
  748. {
  749. struct vpif_fh *fh = priv;
  750. struct channel_obj *ch = fh->channel;
  751. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  752. /* Check the validity of the buffer type */
  753. if (common->fmt.type != fmt->type)
  754. return -EINVAL;
  755. if (vpif_update_resolution(ch))
  756. return -EINVAL;
  757. *fmt = common->fmt;
  758. return 0;
  759. }
  760. static int vpif_s_fmt_vid_out(struct file *file, void *priv,
  761. struct v4l2_format *fmt)
  762. {
  763. struct vpif_fh *fh = priv;
  764. struct v4l2_pix_format *pixfmt;
  765. struct channel_obj *ch = fh->channel;
  766. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  767. int ret = 0;
  768. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  769. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  770. if (!fh->initialized) {
  771. vpif_dbg(1, debug, "Channel Busy\n");
  772. return -EBUSY;
  773. }
  774. /* Check for the priority */
  775. ret = v4l2_prio_check(&ch->prio, fh->prio);
  776. if (0 != ret)
  777. return ret;
  778. fh->initialized = 1;
  779. }
  780. if (common->started) {
  781. vpif_dbg(1, debug, "Streaming in progress\n");
  782. return -EBUSY;
  783. }
  784. pixfmt = &fmt->fmt.pix;
  785. /* Check for valid field format */
  786. ret = vpif_check_format(ch, pixfmt);
  787. if (ret)
  788. return ret;
  789. /* store the pix format in the channel object */
  790. common->fmt.fmt.pix = *pixfmt;
  791. /* store the format in the channel object */
  792. common->fmt = *fmt;
  793. return 0;
  794. }
  795. static int vpif_try_fmt_vid_out(struct file *file, void *priv,
  796. struct v4l2_format *fmt)
  797. {
  798. struct vpif_fh *fh = priv;
  799. struct channel_obj *ch = fh->channel;
  800. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  801. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  802. int ret = 0;
  803. ret = vpif_check_format(ch, pixfmt);
  804. if (ret) {
  805. *pixfmt = common->fmt.fmt.pix;
  806. pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
  807. }
  808. return ret;
  809. }
  810. static int vpif_reqbufs(struct file *file, void *priv,
  811. struct v4l2_requestbuffers *reqbuf)
  812. {
  813. struct vpif_fh *fh = priv;
  814. struct channel_obj *ch = fh->channel;
  815. struct common_obj *common;
  816. enum v4l2_field field;
  817. struct vb2_queue *q;
  818. u8 index = 0;
  819. int ret;
  820. /* This file handle has not initialized the channel,
  821. It is not allowed to do settings */
  822. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  823. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  824. if (!fh->initialized) {
  825. vpif_err("Channel Busy\n");
  826. return -EBUSY;
  827. }
  828. }
  829. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
  830. return -EINVAL;
  831. index = VPIF_VIDEO_INDEX;
  832. common = &ch->common[index];
  833. if (common->fmt.type != reqbuf->type || !vpif_dev)
  834. return -EINVAL;
  835. if (0 != common->io_usrs)
  836. return -EBUSY;
  837. if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  838. if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
  839. field = V4L2_FIELD_INTERLACED;
  840. else
  841. field = common->fmt.fmt.pix.field;
  842. } else {
  843. field = V4L2_VBI_INTERLACED;
  844. }
  845. /* Initialize videobuf2 queue as per the buffer type */
  846. common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
  847. if (IS_ERR(common->alloc_ctx)) {
  848. vpif_err("Failed to get the context\n");
  849. return PTR_ERR(common->alloc_ctx);
  850. }
  851. q = &common->buffer_queue;
  852. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  853. q->io_modes = VB2_MMAP | VB2_USERPTR;
  854. q->drv_priv = fh;
  855. q->ops = &video_qops;
  856. q->mem_ops = &vb2_dma_contig_memops;
  857. q->buf_struct_size = sizeof(struct vpif_disp_buffer);
  858. ret = vb2_queue_init(q);
  859. if (ret) {
  860. vpif_err("vpif_display: vb2_queue_init() failed\n");
  861. vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
  862. return ret;
  863. }
  864. /* Set io allowed member of file handle to TRUE */
  865. fh->io_allowed[index] = 1;
  866. /* Increment io usrs member of channel object to 1 */
  867. common->io_usrs = 1;
  868. /* Store type of memory requested in channel object */
  869. common->memory = reqbuf->memory;
  870. INIT_LIST_HEAD(&common->dma_queue);
  871. /* Allocate buffers */
  872. return vb2_reqbufs(&common->buffer_queue, reqbuf);
  873. }
  874. static int vpif_querybuf(struct file *file, void *priv,
  875. struct v4l2_buffer *tbuf)
  876. {
  877. struct vpif_fh *fh = priv;
  878. struct channel_obj *ch = fh->channel;
  879. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  880. if (common->fmt.type != tbuf->type)
  881. return -EINVAL;
  882. return vb2_querybuf(&common->buffer_queue, tbuf);
  883. }
  884. static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  885. {
  886. struct vpif_fh *fh = NULL;
  887. struct channel_obj *ch = NULL;
  888. struct common_obj *common = NULL;
  889. if (!buf || !priv)
  890. return -EINVAL;
  891. fh = priv;
  892. ch = fh->channel;
  893. if (!ch)
  894. return -EINVAL;
  895. common = &(ch->common[VPIF_VIDEO_INDEX]);
  896. if (common->fmt.type != buf->type)
  897. return -EINVAL;
  898. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  899. vpif_err("fh->io_allowed\n");
  900. return -EACCES;
  901. }
  902. return vb2_qbuf(&common->buffer_queue, buf);
  903. }
  904. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  905. {
  906. struct vpif_fh *fh = priv;
  907. struct channel_obj *ch = fh->channel;
  908. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  909. int ret = 0;
  910. if (!(*std_id & VPIF_V4L2_STD))
  911. return -EINVAL;
  912. if (common->started) {
  913. vpif_err("streaming in progress\n");
  914. return -EBUSY;
  915. }
  916. /* Call encoder subdevice function to set the standard */
  917. ch->video.stdid = *std_id;
  918. memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
  919. /* Get the information about the standard */
  920. if (vpif_update_resolution(ch))
  921. return -EINVAL;
  922. if ((ch->vpifparams.std_info.width *
  923. ch->vpifparams.std_info.height * 2) >
  924. config_params.channel_bufsize[ch->channel_id]) {
  925. vpif_err("invalid std for this size\n");
  926. return -EINVAL;
  927. }
  928. common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
  929. /* Configure the default format information */
  930. vpif_config_format(ch);
  931. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  932. s_std_output, *std_id);
  933. if (ret < 0) {
  934. vpif_err("Failed to set output standard\n");
  935. return ret;
  936. }
  937. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
  938. s_std, *std_id);
  939. if (ret < 0)
  940. vpif_err("Failed to set standard for sub devices\n");
  941. return ret;
  942. }
  943. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  944. {
  945. struct vpif_fh *fh = priv;
  946. struct channel_obj *ch = fh->channel;
  947. *std = ch->video.stdid;
  948. return 0;
  949. }
  950. static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  951. {
  952. struct vpif_fh *fh = priv;
  953. struct channel_obj *ch = fh->channel;
  954. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  955. return vb2_dqbuf(&common->buffer_queue, p,
  956. (file->f_flags & O_NONBLOCK));
  957. }
  958. static int vpif_streamon(struct file *file, void *priv,
  959. enum v4l2_buf_type buftype)
  960. {
  961. struct vpif_fh *fh = priv;
  962. struct channel_obj *ch = fh->channel;
  963. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  964. struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
  965. int ret = 0;
  966. if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  967. vpif_err("buffer type not supported\n");
  968. return -EINVAL;
  969. }
  970. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  971. vpif_err("fh->io_allowed\n");
  972. return -EACCES;
  973. }
  974. /* If Streaming is already started, return error */
  975. if (common->started) {
  976. vpif_err("channel->started\n");
  977. return -EBUSY;
  978. }
  979. if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
  980. && oth_ch->common[VPIF_VIDEO_INDEX].started &&
  981. ch->vpifparams.std_info.ycmux_mode == 0)
  982. || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
  983. && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
  984. vpif_err("other channel is using\n");
  985. return -EBUSY;
  986. }
  987. ret = vpif_check_format(ch, &common->fmt.fmt.pix);
  988. if (ret < 0)
  989. return ret;
  990. /* Call vb2_streamon to start streaming in videobuf2 */
  991. ret = vb2_streamon(&common->buffer_queue, buftype);
  992. if (ret < 0) {
  993. vpif_err("vb2_streamon\n");
  994. return ret;
  995. }
  996. return ret;
  997. }
  998. static int vpif_streamoff(struct file *file, void *priv,
  999. enum v4l2_buf_type buftype)
  1000. {
  1001. struct vpif_fh *fh = priv;
  1002. struct channel_obj *ch = fh->channel;
  1003. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1004. struct vpif_display_config *vpif_config_data =
  1005. vpif_dev->platform_data;
  1006. if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1007. vpif_err("buffer type not supported\n");
  1008. return -EINVAL;
  1009. }
  1010. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  1011. vpif_err("fh->io_allowed\n");
  1012. return -EACCES;
  1013. }
  1014. if (!common->started) {
  1015. vpif_err("channel->started\n");
  1016. return -EINVAL;
  1017. }
  1018. if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1019. /* disable channel */
  1020. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  1021. if (vpif_config_data->
  1022. chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
  1023. channel2_clipping_enable(0);
  1024. enable_channel2(0);
  1025. channel2_intr_enable(0);
  1026. }
  1027. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  1028. (2 == common->started)) {
  1029. if (vpif_config_data->
  1030. chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
  1031. channel3_clipping_enable(0);
  1032. enable_channel3(0);
  1033. channel3_intr_enable(0);
  1034. }
  1035. }
  1036. common->started = 0;
  1037. return vb2_streamoff(&common->buffer_queue, buftype);
  1038. }
  1039. static int vpif_cropcap(struct file *file, void *priv,
  1040. struct v4l2_cropcap *crop)
  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. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
  1046. return -EINVAL;
  1047. crop->bounds.left = crop->bounds.top = 0;
  1048. crop->defrect.left = crop->defrect.top = 0;
  1049. crop->defrect.height = crop->bounds.height = common->height;
  1050. crop->defrect.width = crop->bounds.width = common->width;
  1051. return 0;
  1052. }
  1053. static int vpif_enum_output(struct file *file, void *fh,
  1054. struct v4l2_output *output)
  1055. {
  1056. struct vpif_display_config *config = vpif_dev->platform_data;
  1057. struct vpif_display_chan_config *chan_cfg;
  1058. struct vpif_fh *vpif_handler = fh;
  1059. struct channel_obj *ch = vpif_handler->channel;
  1060. chan_cfg = &config->chan_config[ch->channel_id];
  1061. if (output->index >= chan_cfg->output_count) {
  1062. vpif_dbg(1, debug, "Invalid output index\n");
  1063. return -EINVAL;
  1064. }
  1065. *output = chan_cfg->outputs[output->index].output;
  1066. return 0;
  1067. }
  1068. /**
  1069. * vpif_output_to_subdev() - Maps output to sub device
  1070. * @vpif_cfg - global config ptr
  1071. * @chan_cfg - channel config ptr
  1072. * @index - Given output index from application
  1073. *
  1074. * lookup the sub device information for a given output index.
  1075. * we report all the output to application. output table also
  1076. * has sub device name for the each output
  1077. */
  1078. static int
  1079. vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
  1080. struct vpif_display_chan_config *chan_cfg, int index)
  1081. {
  1082. struct vpif_subdev_info *subdev_info;
  1083. const char *subdev_name;
  1084. int i;
  1085. vpif_dbg(2, debug, "vpif_output_to_subdev\n");
  1086. if (chan_cfg->outputs == NULL)
  1087. return -1;
  1088. subdev_name = chan_cfg->outputs[index].subdev_name;
  1089. if (subdev_name == NULL)
  1090. return -1;
  1091. /* loop through the sub device list to get the sub device info */
  1092. for (i = 0; i < vpif_cfg->subdev_count; i++) {
  1093. subdev_info = &vpif_cfg->subdevinfo[i];
  1094. if (!strcmp(subdev_info->name, subdev_name))
  1095. return i;
  1096. }
  1097. return -1;
  1098. }
  1099. /**
  1100. * vpif_set_output() - Select an output
  1101. * @vpif_cfg - global config ptr
  1102. * @ch - channel
  1103. * @index - Given output index from application
  1104. *
  1105. * Select the given output.
  1106. */
  1107. static int vpif_set_output(struct vpif_display_config *vpif_cfg,
  1108. struct channel_obj *ch, int index)
  1109. {
  1110. struct vpif_display_chan_config *chan_cfg =
  1111. &vpif_cfg->chan_config[ch->channel_id];
  1112. struct vpif_subdev_info *subdev_info = NULL;
  1113. struct v4l2_subdev *sd = NULL;
  1114. u32 input = 0, output = 0;
  1115. int sd_index;
  1116. int ret;
  1117. sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
  1118. if (sd_index >= 0) {
  1119. sd = vpif_obj.sd[sd_index];
  1120. subdev_info = &vpif_cfg->subdevinfo[sd_index];
  1121. }
  1122. if (sd) {
  1123. input = chan_cfg->outputs[index].input_route;
  1124. output = chan_cfg->outputs[index].output_route;
  1125. ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
  1126. if (ret < 0 && ret != -ENOIOCTLCMD) {
  1127. vpif_err("Failed to set output\n");
  1128. return ret;
  1129. }
  1130. }
  1131. ch->output_idx = index;
  1132. ch->sd = sd;
  1133. if (chan_cfg->outputs != NULL)
  1134. /* update tvnorms from the sub device output info */
  1135. ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
  1136. return 0;
  1137. }
  1138. static int vpif_s_output(struct file *file, void *priv, unsigned int i)
  1139. {
  1140. struct vpif_display_config *config = vpif_dev->platform_data;
  1141. struct vpif_display_chan_config *chan_cfg;
  1142. struct vpif_fh *fh = priv;
  1143. struct channel_obj *ch = fh->channel;
  1144. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1145. chan_cfg = &config->chan_config[ch->channel_id];
  1146. if (i >= chan_cfg->output_count)
  1147. return -EINVAL;
  1148. if (common->started) {
  1149. vpif_err("Streaming in progress\n");
  1150. return -EBUSY;
  1151. }
  1152. return vpif_set_output(config, ch, i);
  1153. }
  1154. static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
  1155. {
  1156. struct vpif_fh *fh = priv;
  1157. struct channel_obj *ch = fh->channel;
  1158. *i = ch->output_idx;
  1159. return 0;
  1160. }
  1161. static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
  1162. {
  1163. struct vpif_fh *fh = priv;
  1164. struct channel_obj *ch = fh->channel;
  1165. *p = v4l2_prio_max(&ch->prio);
  1166. return 0;
  1167. }
  1168. static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
  1169. {
  1170. struct vpif_fh *fh = priv;
  1171. struct channel_obj *ch = fh->channel;
  1172. return v4l2_prio_change(&ch->prio, &fh->prio, p);
  1173. }
  1174. /**
  1175. * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
  1176. * @file: file ptr
  1177. * @priv: file handle
  1178. * @timings: input timings
  1179. */
  1180. static int
  1181. vpif_enum_dv_timings(struct file *file, void *priv,
  1182. struct v4l2_enum_dv_timings *timings)
  1183. {
  1184. struct vpif_fh *fh = priv;
  1185. struct channel_obj *ch = fh->channel;
  1186. int ret;
  1187. ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
  1188. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  1189. return -EINVAL;
  1190. return ret;
  1191. }
  1192. /**
  1193. * vpif_s_dv_timings() - S_DV_TIMINGS handler
  1194. * @file: file ptr
  1195. * @priv: file handle
  1196. * @timings: digital video timings
  1197. */
  1198. static int vpif_s_dv_timings(struct file *file, void *priv,
  1199. struct v4l2_dv_timings *timings)
  1200. {
  1201. struct vpif_fh *fh = priv;
  1202. struct channel_obj *ch = fh->channel;
  1203. struct vpif_params *vpifparams = &ch->vpifparams;
  1204. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  1205. struct video_obj *vid_ch = &ch->video;
  1206. struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
  1207. int ret;
  1208. if (timings->type != V4L2_DV_BT_656_1120) {
  1209. vpif_dbg(2, debug, "Timing type not defined\n");
  1210. return -EINVAL;
  1211. }
  1212. /* Configure subdevice timings, if any */
  1213. ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
  1214. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  1215. ret = 0;
  1216. if (ret < 0) {
  1217. vpif_dbg(2, debug, "Error setting custom DV timings\n");
  1218. return ret;
  1219. }
  1220. if (!(timings->bt.width && timings->bt.height &&
  1221. (timings->bt.hbackporch ||
  1222. timings->bt.hfrontporch ||
  1223. timings->bt.hsync) &&
  1224. timings->bt.vfrontporch &&
  1225. (timings->bt.vbackporch ||
  1226. timings->bt.vsync))) {
  1227. vpif_dbg(2, debug, "Timings for width, height, "
  1228. "horizontal back porch, horizontal sync, "
  1229. "horizontal front porch, vertical back porch, "
  1230. "vertical sync and vertical back porch "
  1231. "must be defined\n");
  1232. return -EINVAL;
  1233. }
  1234. vid_ch->dv_timings = *timings;
  1235. /* Configure video port timings */
  1236. std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
  1237. bt->hsync - 8;
  1238. std_info->sav2eav = bt->width;
  1239. std_info->l1 = 1;
  1240. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  1241. if (bt->interlaced) {
  1242. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  1243. std_info->vsize = bt->height * 2 +
  1244. bt->vfrontporch + bt->vsync + bt->vbackporch +
  1245. bt->il_vfrontporch + bt->il_vsync +
  1246. bt->il_vbackporch;
  1247. std_info->l5 = std_info->vsize/2 -
  1248. (bt->vfrontporch - 1);
  1249. std_info->l7 = std_info->vsize/2 + 1;
  1250. std_info->l9 = std_info->l7 + bt->il_vsync +
  1251. bt->il_vbackporch + 1;
  1252. std_info->l11 = std_info->vsize -
  1253. (bt->il_vfrontporch - 1);
  1254. } else {
  1255. vpif_dbg(2, debug, "Required timing values for "
  1256. "interlaced BT format missing\n");
  1257. return -EINVAL;
  1258. }
  1259. } else {
  1260. std_info->vsize = bt->height + bt->vfrontporch +
  1261. bt->vsync + bt->vbackporch;
  1262. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  1263. }
  1264. strncpy(std_info->name, "Custom timings BT656/1120",
  1265. VPIF_MAX_NAME);
  1266. std_info->width = bt->width;
  1267. std_info->height = bt->height;
  1268. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  1269. std_info->ycmux_mode = 0;
  1270. std_info->capture_format = 0;
  1271. std_info->vbi_supported = 0;
  1272. std_info->hd_sd = 1;
  1273. std_info->stdid = 0;
  1274. vid_ch->stdid = 0;
  1275. return 0;
  1276. }
  1277. /**
  1278. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  1279. * @file: file ptr
  1280. * @priv: file handle
  1281. * @timings: digital video timings
  1282. */
  1283. static int vpif_g_dv_timings(struct file *file, void *priv,
  1284. struct v4l2_dv_timings *timings)
  1285. {
  1286. struct vpif_fh *fh = priv;
  1287. struct channel_obj *ch = fh->channel;
  1288. struct video_obj *vid_ch = &ch->video;
  1289. *timings = vid_ch->dv_timings;
  1290. return 0;
  1291. }
  1292. /*
  1293. * vpif_g_chip_ident() - Identify the chip
  1294. * @file: file ptr
  1295. * @priv: file handle
  1296. * @chip: chip identity
  1297. *
  1298. * Returns zero or -EINVAL if read operations fails.
  1299. */
  1300. static int vpif_g_chip_ident(struct file *file, void *priv,
  1301. struct v4l2_dbg_chip_ident *chip)
  1302. {
  1303. chip->ident = V4L2_IDENT_NONE;
  1304. chip->revision = 0;
  1305. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  1306. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
  1307. vpif_dbg(2, debug, "match_type is invalid.\n");
  1308. return -EINVAL;
  1309. }
  1310. return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
  1311. g_chip_ident, chip);
  1312. }
  1313. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1314. /*
  1315. * vpif_dbg_g_register() - Read register
  1316. * @file: file ptr
  1317. * @priv: file handle
  1318. * @reg: register to be read
  1319. *
  1320. * Debugging only
  1321. * Returns zero or -EINVAL if read operations fails.
  1322. */
  1323. static int vpif_dbg_g_register(struct file *file, void *priv,
  1324. struct v4l2_dbg_register *reg){
  1325. struct vpif_fh *fh = priv;
  1326. struct channel_obj *ch = fh->channel;
  1327. return v4l2_subdev_call(ch->sd, core, g_register, reg);
  1328. }
  1329. /*
  1330. * vpif_dbg_s_register() - Write to register
  1331. * @file: file ptr
  1332. * @priv: file handle
  1333. * @reg: register to be modified
  1334. *
  1335. * Debugging only
  1336. * Returns zero or -EINVAL if write operations fails.
  1337. */
  1338. static int vpif_dbg_s_register(struct file *file, void *priv,
  1339. struct v4l2_dbg_register *reg){
  1340. struct vpif_fh *fh = priv;
  1341. struct channel_obj *ch = fh->channel;
  1342. return v4l2_subdev_call(ch->sd, core, s_register, reg);
  1343. }
  1344. #endif
  1345. /*
  1346. * vpif_log_status() - Status information
  1347. * @file: file ptr
  1348. * @priv: file handle
  1349. *
  1350. * Returns zero.
  1351. */
  1352. static int vpif_log_status(struct file *filep, void *priv)
  1353. {
  1354. /* status for sub devices */
  1355. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  1356. return 0;
  1357. }
  1358. /* vpif display ioctl operations */
  1359. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1360. .vidioc_querycap = vpif_querycap,
  1361. .vidioc_g_priority = vpif_g_priority,
  1362. .vidioc_s_priority = vpif_s_priority,
  1363. .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
  1364. .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
  1365. .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
  1366. .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
  1367. .vidioc_reqbufs = vpif_reqbufs,
  1368. .vidioc_querybuf = vpif_querybuf,
  1369. .vidioc_qbuf = vpif_qbuf,
  1370. .vidioc_dqbuf = vpif_dqbuf,
  1371. .vidioc_streamon = vpif_streamon,
  1372. .vidioc_streamoff = vpif_streamoff,
  1373. .vidioc_s_std = vpif_s_std,
  1374. .vidioc_g_std = vpif_g_std,
  1375. .vidioc_enum_output = vpif_enum_output,
  1376. .vidioc_s_output = vpif_s_output,
  1377. .vidioc_g_output = vpif_g_output,
  1378. .vidioc_cropcap = vpif_cropcap,
  1379. .vidioc_enum_dv_timings = vpif_enum_dv_timings,
  1380. .vidioc_s_dv_timings = vpif_s_dv_timings,
  1381. .vidioc_g_dv_timings = vpif_g_dv_timings,
  1382. .vidioc_g_chip_ident = vpif_g_chip_ident,
  1383. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1384. .vidioc_g_register = vpif_dbg_g_register,
  1385. .vidioc_s_register = vpif_dbg_s_register,
  1386. #endif
  1387. .vidioc_log_status = vpif_log_status,
  1388. };
  1389. static const struct v4l2_file_operations vpif_fops = {
  1390. .owner = THIS_MODULE,
  1391. .open = vpif_open,
  1392. .release = vpif_release,
  1393. .unlocked_ioctl = video_ioctl2,
  1394. .mmap = vpif_mmap,
  1395. .poll = vpif_poll
  1396. };
  1397. static struct video_device vpif_video_template = {
  1398. .name = "vpif",
  1399. .fops = &vpif_fops,
  1400. .ioctl_ops = &vpif_ioctl_ops,
  1401. };
  1402. /*Configure the channels, buffer sizei, request irq */
  1403. static int initialize_vpif(void)
  1404. {
  1405. int free_channel_objects_index;
  1406. int free_buffer_channel_index;
  1407. int free_buffer_index;
  1408. int err = 0, i, j;
  1409. /* Default number of buffers should be 3 */
  1410. if ((ch2_numbuffers > 0) &&
  1411. (ch2_numbuffers < config_params.min_numbuffers))
  1412. ch2_numbuffers = config_params.min_numbuffers;
  1413. if ((ch3_numbuffers > 0) &&
  1414. (ch3_numbuffers < config_params.min_numbuffers))
  1415. ch3_numbuffers = config_params.min_numbuffers;
  1416. /* Set buffer size to min buffers size if invalid buffer size is
  1417. * given */
  1418. if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
  1419. ch2_bufsize =
  1420. config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
  1421. if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
  1422. ch3_bufsize =
  1423. config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
  1424. config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
  1425. if (ch2_numbuffers) {
  1426. config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
  1427. ch2_bufsize;
  1428. }
  1429. config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
  1430. if (ch3_numbuffers) {
  1431. config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
  1432. ch3_bufsize;
  1433. }
  1434. /* Allocate memory for six channel objects */
  1435. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1436. vpif_obj.dev[i] =
  1437. kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
  1438. /* If memory allocation fails, return error */
  1439. if (!vpif_obj.dev[i]) {
  1440. free_channel_objects_index = i;
  1441. err = -ENOMEM;
  1442. goto vpif_init_free_channel_objects;
  1443. }
  1444. }
  1445. free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
  1446. free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
  1447. free_buffer_index = config_params.numbuffers[i - 1];
  1448. return 0;
  1449. vpif_init_free_channel_objects:
  1450. for (j = 0; j < free_channel_objects_index; j++)
  1451. kfree(vpif_obj.dev[j]);
  1452. return err;
  1453. }
  1454. /*
  1455. * vpif_probe: This function creates device entries by register itself to the
  1456. * V4L2 driver and initializes fields of each channel objects
  1457. */
  1458. static __init int vpif_probe(struct platform_device *pdev)
  1459. {
  1460. struct vpif_subdev_info *subdevdata;
  1461. struct vpif_display_config *config;
  1462. int i, j = 0, k, err = 0;
  1463. int res_idx = 0;
  1464. struct i2c_adapter *i2c_adap;
  1465. struct common_obj *common;
  1466. struct channel_obj *ch;
  1467. struct video_device *vfd;
  1468. struct resource *res;
  1469. int subdev_count;
  1470. size_t size;
  1471. vpif_dev = &pdev->dev;
  1472. err = initialize_vpif();
  1473. if (err) {
  1474. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1475. return err;
  1476. }
  1477. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1478. if (err) {
  1479. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1480. return err;
  1481. }
  1482. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
  1483. for (i = res->start; i <= res->end; i++) {
  1484. if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
  1485. "VPIF_Display", (void *)
  1486. (&vpif_obj.dev[res_idx]->channel_id))) {
  1487. err = -EBUSY;
  1488. for (j = 0; j < i; j++)
  1489. free_irq(j, (void *)
  1490. (&vpif_obj.dev[res_idx]->channel_id));
  1491. goto vpif_int_err;
  1492. }
  1493. }
  1494. res_idx++;
  1495. }
  1496. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1497. /* Get the pointer to the channel object */
  1498. ch = vpif_obj.dev[i];
  1499. /* Allocate memory for video device */
  1500. vfd = video_device_alloc();
  1501. if (vfd == NULL) {
  1502. for (j = 0; j < i; j++) {
  1503. ch = vpif_obj.dev[j];
  1504. video_device_release(ch->video_dev);
  1505. }
  1506. err = -ENOMEM;
  1507. goto vpif_int_err;
  1508. }
  1509. /* Initialize field of video device */
  1510. *vfd = vpif_video_template;
  1511. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1512. vfd->release = video_device_release;
  1513. vfd->vfl_dir = VFL_DIR_TX;
  1514. snprintf(vfd->name, sizeof(vfd->name),
  1515. "VPIF_Display_DRIVER_V%s",
  1516. VPIF_DISPLAY_VERSION);
  1517. /* Set video_dev to the video device */
  1518. ch->video_dev = vfd;
  1519. }
  1520. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1521. if (res) {
  1522. size = resource_size(res);
  1523. /* The resources are divided into two equal memory and when
  1524. * we have HD output we can add them together
  1525. */
  1526. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1527. ch = vpif_obj.dev[j];
  1528. ch->channel_id = j;
  1529. /* only enabled if second resource exists */
  1530. config_params.video_limit[ch->channel_id] = 0;
  1531. if (size)
  1532. config_params.video_limit[ch->channel_id] =
  1533. size/2;
  1534. }
  1535. }
  1536. i2c_adap = i2c_get_adapter(1);
  1537. config = pdev->dev.platform_data;
  1538. subdev_count = config->subdev_count;
  1539. subdevdata = config->subdevinfo;
  1540. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1541. GFP_KERNEL);
  1542. if (vpif_obj.sd == NULL) {
  1543. vpif_err("unable to allocate memory for subdevice pointers\n");
  1544. err = -ENOMEM;
  1545. goto vpif_sd_error;
  1546. }
  1547. for (i = 0; i < subdev_count; i++) {
  1548. vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1549. i2c_adap,
  1550. &subdevdata[i].board_info,
  1551. NULL);
  1552. if (!vpif_obj.sd[i]) {
  1553. vpif_err("Error registering v4l2 subdevice\n");
  1554. goto probe_subdev_out;
  1555. }
  1556. if (vpif_obj.sd[i])
  1557. vpif_obj.sd[i]->grp_id = 1 << i;
  1558. }
  1559. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1560. ch = vpif_obj.dev[j];
  1561. /* Initialize field of the channel objects */
  1562. atomic_set(&ch->usrs, 0);
  1563. for (k = 0; k < VPIF_NUMOBJECTS; k++) {
  1564. ch->common[k].numbuffers = 0;
  1565. common = &ch->common[k];
  1566. common->io_usrs = 0;
  1567. common->started = 0;
  1568. spin_lock_init(&common->irqlock);
  1569. mutex_init(&common->lock);
  1570. common->numbuffers = 0;
  1571. common->set_addr = NULL;
  1572. common->ytop_off = common->ybtm_off = 0;
  1573. common->ctop_off = common->cbtm_off = 0;
  1574. common->cur_frm = common->next_frm = NULL;
  1575. memset(&common->fmt, 0, sizeof(common->fmt));
  1576. common->numbuffers = config_params.numbuffers[k];
  1577. }
  1578. ch->initialized = 0;
  1579. if (subdev_count)
  1580. ch->sd = vpif_obj.sd[0];
  1581. ch->channel_id = j;
  1582. if (j < 2)
  1583. ch->common[VPIF_VIDEO_INDEX].numbuffers =
  1584. config_params.numbuffers[ch->channel_id];
  1585. else
  1586. ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
  1587. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  1588. /* Initialize prio member of channel object */
  1589. v4l2_prio_init(&ch->prio);
  1590. ch->common[VPIF_VIDEO_INDEX].fmt.type =
  1591. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1592. ch->video_dev->lock = &common->lock;
  1593. video_set_drvdata(ch->video_dev, ch);
  1594. /* select output 0 */
  1595. err = vpif_set_output(config, ch, 0);
  1596. if (err)
  1597. goto probe_out;
  1598. /* register video device */
  1599. vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
  1600. (int)ch, (int)&ch->video_dev);
  1601. err = video_register_device(ch->video_dev,
  1602. VFL_TYPE_GRABBER, (j ? 3 : 2));
  1603. if (err < 0)
  1604. goto probe_out;
  1605. }
  1606. v4l2_info(&vpif_obj.v4l2_dev,
  1607. " VPIF display driver initialized\n");
  1608. return 0;
  1609. probe_out:
  1610. for (k = 0; k < j; k++) {
  1611. ch = vpif_obj.dev[k];
  1612. video_unregister_device(ch->video_dev);
  1613. video_device_release(ch->video_dev);
  1614. ch->video_dev = NULL;
  1615. }
  1616. probe_subdev_out:
  1617. kfree(vpif_obj.sd);
  1618. vpif_sd_error:
  1619. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1620. ch = vpif_obj.dev[i];
  1621. /* Note: does nothing if ch->video_dev == NULL */
  1622. video_device_release(ch->video_dev);
  1623. }
  1624. vpif_int_err:
  1625. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1626. vpif_err("VPIF IRQ request failed\n");
  1627. for (i = 0; i < res_idx; i++) {
  1628. res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
  1629. for (j = res->start; j <= res->end; j++)
  1630. free_irq(j, (void *)(&vpif_obj.dev[i]->channel_id));
  1631. }
  1632. return err;
  1633. }
  1634. /*
  1635. * vpif_remove: It un-register channels from V4L2 driver
  1636. */
  1637. static int vpif_remove(struct platform_device *device)
  1638. {
  1639. struct channel_obj *ch;
  1640. int i;
  1641. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1642. /* un-register device */
  1643. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1644. /* Get the pointer to the channel object */
  1645. ch = vpif_obj.dev[i];
  1646. /* Unregister video device */
  1647. video_unregister_device(ch->video_dev);
  1648. ch->video_dev = NULL;
  1649. }
  1650. return 0;
  1651. }
  1652. #ifdef CONFIG_PM
  1653. static int vpif_suspend(struct device *dev)
  1654. {
  1655. struct common_obj *common;
  1656. struct channel_obj *ch;
  1657. int i;
  1658. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1659. /* Get the pointer to the channel object */
  1660. ch = vpif_obj.dev[i];
  1661. common = &ch->common[VPIF_VIDEO_INDEX];
  1662. mutex_lock(&common->lock);
  1663. if (atomic_read(&ch->usrs) && common->io_usrs) {
  1664. /* Disable channel */
  1665. if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
  1666. enable_channel2(0);
  1667. channel2_intr_enable(0);
  1668. }
  1669. if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
  1670. common->started == 2) {
  1671. enable_channel3(0);
  1672. channel3_intr_enable(0);
  1673. }
  1674. }
  1675. mutex_unlock(&common->lock);
  1676. }
  1677. return 0;
  1678. }
  1679. static int vpif_resume(struct device *dev)
  1680. {
  1681. struct common_obj *common;
  1682. struct channel_obj *ch;
  1683. int i;
  1684. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1685. /* Get the pointer to the channel object */
  1686. ch = vpif_obj.dev[i];
  1687. common = &ch->common[VPIF_VIDEO_INDEX];
  1688. mutex_lock(&common->lock);
  1689. if (atomic_read(&ch->usrs) && common->io_usrs) {
  1690. /* Enable channel */
  1691. if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
  1692. enable_channel2(1);
  1693. channel2_intr_enable(1);
  1694. }
  1695. if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
  1696. common->started == 2) {
  1697. enable_channel3(1);
  1698. channel3_intr_enable(1);
  1699. }
  1700. }
  1701. mutex_unlock(&common->lock);
  1702. }
  1703. return 0;
  1704. }
  1705. static const struct dev_pm_ops vpif_pm = {
  1706. .suspend = vpif_suspend,
  1707. .resume = vpif_resume,
  1708. };
  1709. #define vpif_pm_ops (&vpif_pm)
  1710. #else
  1711. #define vpif_pm_ops NULL
  1712. #endif
  1713. static __refdata struct platform_driver vpif_driver = {
  1714. .driver = {
  1715. .name = "vpif_display",
  1716. .owner = THIS_MODULE,
  1717. .pm = vpif_pm_ops,
  1718. },
  1719. .probe = vpif_probe,
  1720. .remove = vpif_remove,
  1721. };
  1722. static __init int vpif_init(void)
  1723. {
  1724. return platform_driver_register(&vpif_driver);
  1725. }
  1726. /*
  1727. * vpif_cleanup: This function un-registers device and driver to the kernel,
  1728. * frees requested irq handler and de-allocates memory allocated for channel
  1729. * objects.
  1730. */
  1731. static void vpif_cleanup(void)
  1732. {
  1733. struct platform_device *pdev;
  1734. struct resource *res;
  1735. int irq_num;
  1736. int i = 0;
  1737. pdev = container_of(vpif_dev, struct platform_device, dev);
  1738. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
  1739. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1740. free_irq(irq_num,
  1741. (void *)(&vpif_obj.dev[i]->channel_id));
  1742. i++;
  1743. }
  1744. platform_driver_unregister(&vpif_driver);
  1745. kfree(vpif_obj.sd);
  1746. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
  1747. kfree(vpif_obj.dev[i]);
  1748. }
  1749. module_init(vpif_init);
  1750. module_exit(vpif_cleanup);