vpif_display.c 52 KB

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