vpif_display.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  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 = bt->hbackporch + bt->hfrontporch +
  1221. bt->hsync - 8;
  1222. std_info->sav2eav = bt->width;
  1223. std_info->l1 = 1;
  1224. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  1225. if (bt->interlaced) {
  1226. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  1227. std_info->vsize = bt->height * 2 +
  1228. bt->vfrontporch + bt->vsync + bt->vbackporch +
  1229. bt->il_vfrontporch + bt->il_vsync +
  1230. bt->il_vbackporch;
  1231. std_info->l5 = std_info->vsize/2 -
  1232. (bt->vfrontporch - 1);
  1233. std_info->l7 = std_info->vsize/2 + 1;
  1234. std_info->l9 = std_info->l7 + bt->il_vsync +
  1235. bt->il_vbackporch + 1;
  1236. std_info->l11 = std_info->vsize -
  1237. (bt->il_vfrontporch - 1);
  1238. } else {
  1239. vpif_dbg(2, debug, "Required timing values for "
  1240. "interlaced BT format missing\n");
  1241. return -EINVAL;
  1242. }
  1243. } else {
  1244. std_info->vsize = bt->height + bt->vfrontporch +
  1245. bt->vsync + bt->vbackporch;
  1246. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  1247. }
  1248. strncpy(std_info->name, "Custom timings BT656/1120",
  1249. VPIF_MAX_NAME);
  1250. std_info->width = bt->width;
  1251. std_info->height = bt->height;
  1252. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  1253. std_info->ycmux_mode = 0;
  1254. std_info->capture_format = 0;
  1255. std_info->vbi_supported = 0;
  1256. std_info->hd_sd = 1;
  1257. std_info->stdid = 0;
  1258. vid_ch->stdid = 0;
  1259. return 0;
  1260. }
  1261. /**
  1262. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  1263. * @file: file ptr
  1264. * @priv: file handle
  1265. * @timings: digital video timings
  1266. */
  1267. static int vpif_g_dv_timings(struct file *file, void *priv,
  1268. struct v4l2_dv_timings *timings)
  1269. {
  1270. struct vpif_fh *fh = priv;
  1271. struct channel_obj *ch = fh->channel;
  1272. struct video_obj *vid_ch = &ch->video;
  1273. *timings = vid_ch->dv_timings;
  1274. return 0;
  1275. }
  1276. /*
  1277. * vpif_log_status() - Status information
  1278. * @file: file ptr
  1279. * @priv: file handle
  1280. *
  1281. * Returns zero.
  1282. */
  1283. static int vpif_log_status(struct file *filep, void *priv)
  1284. {
  1285. /* status for sub devices */
  1286. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  1287. return 0;
  1288. }
  1289. /* vpif display ioctl operations */
  1290. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1291. .vidioc_querycap = vpif_querycap,
  1292. .vidioc_g_priority = vpif_g_priority,
  1293. .vidioc_s_priority = vpif_s_priority,
  1294. .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
  1295. .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
  1296. .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
  1297. .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
  1298. .vidioc_reqbufs = vpif_reqbufs,
  1299. .vidioc_querybuf = vpif_querybuf,
  1300. .vidioc_qbuf = vpif_qbuf,
  1301. .vidioc_dqbuf = vpif_dqbuf,
  1302. .vidioc_streamon = vpif_streamon,
  1303. .vidioc_streamoff = vpif_streamoff,
  1304. .vidioc_s_std = vpif_s_std,
  1305. .vidioc_g_std = vpif_g_std,
  1306. .vidioc_enum_output = vpif_enum_output,
  1307. .vidioc_s_output = vpif_s_output,
  1308. .vidioc_g_output = vpif_g_output,
  1309. .vidioc_cropcap = vpif_cropcap,
  1310. .vidioc_enum_dv_timings = vpif_enum_dv_timings,
  1311. .vidioc_s_dv_timings = vpif_s_dv_timings,
  1312. .vidioc_g_dv_timings = vpif_g_dv_timings,
  1313. .vidioc_log_status = vpif_log_status,
  1314. };
  1315. static const struct v4l2_file_operations vpif_fops = {
  1316. .owner = THIS_MODULE,
  1317. .open = vpif_open,
  1318. .release = vpif_release,
  1319. .unlocked_ioctl = video_ioctl2,
  1320. .mmap = vpif_mmap,
  1321. .poll = vpif_poll
  1322. };
  1323. static struct video_device vpif_video_template = {
  1324. .name = "vpif",
  1325. .fops = &vpif_fops,
  1326. .ioctl_ops = &vpif_ioctl_ops,
  1327. };
  1328. /*Configure the channels, buffer sizei, request irq */
  1329. static int initialize_vpif(void)
  1330. {
  1331. int free_channel_objects_index;
  1332. int free_buffer_channel_index;
  1333. int free_buffer_index;
  1334. int err = 0, i, j;
  1335. /* Default number of buffers should be 3 */
  1336. if ((ch2_numbuffers > 0) &&
  1337. (ch2_numbuffers < config_params.min_numbuffers))
  1338. ch2_numbuffers = config_params.min_numbuffers;
  1339. if ((ch3_numbuffers > 0) &&
  1340. (ch3_numbuffers < config_params.min_numbuffers))
  1341. ch3_numbuffers = config_params.min_numbuffers;
  1342. /* Set buffer size to min buffers size if invalid buffer size is
  1343. * given */
  1344. if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
  1345. ch2_bufsize =
  1346. config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
  1347. if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
  1348. ch3_bufsize =
  1349. config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
  1350. config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
  1351. if (ch2_numbuffers) {
  1352. config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
  1353. ch2_bufsize;
  1354. }
  1355. config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
  1356. if (ch3_numbuffers) {
  1357. config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
  1358. ch3_bufsize;
  1359. }
  1360. /* Allocate memory for six channel objects */
  1361. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1362. vpif_obj.dev[i] =
  1363. kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
  1364. /* If memory allocation fails, return error */
  1365. if (!vpif_obj.dev[i]) {
  1366. free_channel_objects_index = i;
  1367. err = -ENOMEM;
  1368. goto vpif_init_free_channel_objects;
  1369. }
  1370. }
  1371. free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
  1372. free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
  1373. free_buffer_index = config_params.numbuffers[i - 1];
  1374. return 0;
  1375. vpif_init_free_channel_objects:
  1376. for (j = 0; j < free_channel_objects_index; j++)
  1377. kfree(vpif_obj.dev[j]);
  1378. return err;
  1379. }
  1380. /*
  1381. * vpif_probe: This function creates device entries by register itself to the
  1382. * V4L2 driver and initializes fields of each channel objects
  1383. */
  1384. static __init int vpif_probe(struct platform_device *pdev)
  1385. {
  1386. struct vpif_subdev_info *subdevdata;
  1387. struct vpif_display_config *config;
  1388. int i, j = 0, k, err = 0;
  1389. int res_idx = 0;
  1390. struct i2c_adapter *i2c_adap;
  1391. struct common_obj *common;
  1392. struct channel_obj *ch;
  1393. struct video_device *vfd;
  1394. struct resource *res;
  1395. int subdev_count;
  1396. size_t size;
  1397. vpif_dev = &pdev->dev;
  1398. err = initialize_vpif();
  1399. if (err) {
  1400. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1401. return err;
  1402. }
  1403. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1404. if (err) {
  1405. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1406. return err;
  1407. }
  1408. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
  1409. err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
  1410. IRQF_SHARED, "VPIF_Display",
  1411. (void *)(&vpif_obj.dev[res_idx]->
  1412. channel_id));
  1413. if (err) {
  1414. err = -EINVAL;
  1415. vpif_err("VPIF IRQ request failed\n");
  1416. goto vpif_unregister;
  1417. }
  1418. res_idx++;
  1419. }
  1420. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1421. /* Get the pointer to the channel object */
  1422. ch = vpif_obj.dev[i];
  1423. /* Allocate memory for video device */
  1424. vfd = video_device_alloc();
  1425. if (vfd == NULL) {
  1426. for (j = 0; j < i; j++) {
  1427. ch = vpif_obj.dev[j];
  1428. video_device_release(ch->video_dev);
  1429. }
  1430. err = -ENOMEM;
  1431. goto vpif_unregister;
  1432. }
  1433. /* Initialize field of video device */
  1434. *vfd = vpif_video_template;
  1435. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1436. vfd->release = video_device_release;
  1437. vfd->vfl_dir = VFL_DIR_TX;
  1438. snprintf(vfd->name, sizeof(vfd->name),
  1439. "VPIF_Display_DRIVER_V%s",
  1440. VPIF_DISPLAY_VERSION);
  1441. /* Set video_dev to the video device */
  1442. ch->video_dev = vfd;
  1443. }
  1444. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1445. if (res) {
  1446. size = resource_size(res);
  1447. /* The resources are divided into two equal memory and when
  1448. * we have HD output we can add them together
  1449. */
  1450. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1451. ch = vpif_obj.dev[j];
  1452. ch->channel_id = j;
  1453. /* only enabled if second resource exists */
  1454. config_params.video_limit[ch->channel_id] = 0;
  1455. if (size)
  1456. config_params.video_limit[ch->channel_id] =
  1457. size/2;
  1458. }
  1459. }
  1460. i2c_adap = i2c_get_adapter(1);
  1461. config = pdev->dev.platform_data;
  1462. subdev_count = config->subdev_count;
  1463. subdevdata = config->subdevinfo;
  1464. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1465. GFP_KERNEL);
  1466. if (vpif_obj.sd == NULL) {
  1467. vpif_err("unable to allocate memory for subdevice pointers\n");
  1468. err = -ENOMEM;
  1469. goto vpif_sd_error;
  1470. }
  1471. for (i = 0; i < subdev_count; i++) {
  1472. vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1473. i2c_adap,
  1474. &subdevdata[i].board_info,
  1475. NULL);
  1476. if (!vpif_obj.sd[i]) {
  1477. vpif_err("Error registering v4l2 subdevice\n");
  1478. err = -ENODEV;
  1479. goto probe_subdev_out;
  1480. }
  1481. if (vpif_obj.sd[i])
  1482. vpif_obj.sd[i]->grp_id = 1 << i;
  1483. }
  1484. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1485. ch = vpif_obj.dev[j];
  1486. /* Initialize field of the channel objects */
  1487. atomic_set(&ch->usrs, 0);
  1488. for (k = 0; k < VPIF_NUMOBJECTS; k++) {
  1489. ch->common[k].numbuffers = 0;
  1490. common = &ch->common[k];
  1491. common->io_usrs = 0;
  1492. common->started = 0;
  1493. spin_lock_init(&common->irqlock);
  1494. mutex_init(&common->lock);
  1495. common->numbuffers = 0;
  1496. common->set_addr = NULL;
  1497. common->ytop_off = common->ybtm_off = 0;
  1498. common->ctop_off = common->cbtm_off = 0;
  1499. common->cur_frm = common->next_frm = NULL;
  1500. memset(&common->fmt, 0, sizeof(common->fmt));
  1501. common->numbuffers = config_params.numbuffers[k];
  1502. }
  1503. ch->initialized = 0;
  1504. if (subdev_count)
  1505. ch->sd = vpif_obj.sd[0];
  1506. ch->channel_id = j;
  1507. if (j < 2)
  1508. ch->common[VPIF_VIDEO_INDEX].numbuffers =
  1509. config_params.numbuffers[ch->channel_id];
  1510. else
  1511. ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
  1512. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  1513. /* Initialize prio member of channel object */
  1514. v4l2_prio_init(&ch->prio);
  1515. ch->common[VPIF_VIDEO_INDEX].fmt.type =
  1516. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1517. ch->video_dev->lock = &common->lock;
  1518. video_set_drvdata(ch->video_dev, ch);
  1519. /* select output 0 */
  1520. err = vpif_set_output(config, ch, 0);
  1521. if (err)
  1522. goto probe_out;
  1523. /* register video device */
  1524. vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
  1525. (int)ch, (int)&ch->video_dev);
  1526. err = video_register_device(ch->video_dev,
  1527. VFL_TYPE_GRABBER, (j ? 3 : 2));
  1528. if (err < 0)
  1529. goto probe_out;
  1530. }
  1531. v4l2_info(&vpif_obj.v4l2_dev,
  1532. " VPIF display driver initialized\n");
  1533. return 0;
  1534. probe_out:
  1535. for (k = 0; k < j; k++) {
  1536. ch = vpif_obj.dev[k];
  1537. video_unregister_device(ch->video_dev);
  1538. video_device_release(ch->video_dev);
  1539. ch->video_dev = NULL;
  1540. }
  1541. probe_subdev_out:
  1542. kfree(vpif_obj.sd);
  1543. vpif_sd_error:
  1544. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1545. ch = vpif_obj.dev[i];
  1546. /* Note: does nothing if ch->video_dev == NULL */
  1547. video_device_release(ch->video_dev);
  1548. }
  1549. vpif_unregister:
  1550. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1551. return err;
  1552. }
  1553. /*
  1554. * vpif_remove: It un-register channels from V4L2 driver
  1555. */
  1556. static int vpif_remove(struct platform_device *device)
  1557. {
  1558. struct channel_obj *ch;
  1559. int i;
  1560. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1561. kfree(vpif_obj.sd);
  1562. /* un-register device */
  1563. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1564. /* Get the pointer to the channel object */
  1565. ch = vpif_obj.dev[i];
  1566. /* Unregister video device */
  1567. video_unregister_device(ch->video_dev);
  1568. ch->video_dev = NULL;
  1569. kfree(vpif_obj.dev[i]);
  1570. }
  1571. return 0;
  1572. }
  1573. #ifdef CONFIG_PM
  1574. static int vpif_suspend(struct device *dev)
  1575. {
  1576. struct common_obj *common;
  1577. struct channel_obj *ch;
  1578. int i;
  1579. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1580. /* Get the pointer to the channel object */
  1581. ch = vpif_obj.dev[i];
  1582. common = &ch->common[VPIF_VIDEO_INDEX];
  1583. mutex_lock(&common->lock);
  1584. if (atomic_read(&ch->usrs) && common->io_usrs) {
  1585. /* Disable channel */
  1586. if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
  1587. enable_channel2(0);
  1588. channel2_intr_enable(0);
  1589. }
  1590. if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
  1591. common->started == 2) {
  1592. enable_channel3(0);
  1593. channel3_intr_enable(0);
  1594. }
  1595. }
  1596. mutex_unlock(&common->lock);
  1597. }
  1598. return 0;
  1599. }
  1600. static int vpif_resume(struct device *dev)
  1601. {
  1602. struct common_obj *common;
  1603. struct channel_obj *ch;
  1604. int i;
  1605. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1606. /* Get the pointer to the channel object */
  1607. ch = vpif_obj.dev[i];
  1608. common = &ch->common[VPIF_VIDEO_INDEX];
  1609. mutex_lock(&common->lock);
  1610. if (atomic_read(&ch->usrs) && common->io_usrs) {
  1611. /* Enable channel */
  1612. if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
  1613. enable_channel2(1);
  1614. channel2_intr_enable(1);
  1615. }
  1616. if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
  1617. common->started == 2) {
  1618. enable_channel3(1);
  1619. channel3_intr_enable(1);
  1620. }
  1621. }
  1622. mutex_unlock(&common->lock);
  1623. }
  1624. return 0;
  1625. }
  1626. static const struct dev_pm_ops vpif_pm = {
  1627. .suspend = vpif_suspend,
  1628. .resume = vpif_resume,
  1629. };
  1630. #define vpif_pm_ops (&vpif_pm)
  1631. #else
  1632. #define vpif_pm_ops NULL
  1633. #endif
  1634. static __refdata struct platform_driver vpif_driver = {
  1635. .driver = {
  1636. .name = "vpif_display",
  1637. .owner = THIS_MODULE,
  1638. .pm = vpif_pm_ops,
  1639. },
  1640. .probe = vpif_probe,
  1641. .remove = vpif_remove,
  1642. };
  1643. module_platform_driver(vpif_driver);