vpif_display.c 53 KB

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