vpif_display.c 53 KB

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