vpif_display.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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. * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed .as is. WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <media/v4l2-ioctl.h>
  22. #include "vpif.h"
  23. #include "vpif_display.h"
  24. MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
  25. MODULE_LICENSE("GPL");
  26. MODULE_VERSION(VPIF_DISPLAY_VERSION);
  27. #define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
  28. #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
  29. #define vpif_dbg(level, debug, fmt, arg...) \
  30. v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
  31. static int debug = 1;
  32. module_param(debug, int, 0644);
  33. MODULE_PARM_DESC(debug, "Debug level 0-1");
  34. #define VPIF_DRIVER_NAME "vpif_display"
  35. /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
  36. static int ycmux_mode;
  37. static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
  38. static struct vpif_device vpif_obj = { {NULL} };
  39. static struct device *vpif_dev;
  40. static void vpif_calculate_offsets(struct channel_obj *ch);
  41. static void vpif_config_addr(struct channel_obj *ch, int muxmode);
  42. static inline
  43. struct vpif_disp_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
  44. {
  45. return container_of(vb, struct vpif_disp_buffer, vb);
  46. }
  47. /**
  48. * vpif_buffer_prepare : callback function for buffer prepare
  49. * @vb: ptr to vb2_buffer
  50. *
  51. * This is the callback function for buffer prepare when vb2_qbuf()
  52. * function is called. The buffer is prepared and user space virtual address
  53. * or user address is converted into physical address
  54. */
  55. static int vpif_buffer_prepare(struct vb2_buffer *vb)
  56. {
  57. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  58. struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
  59. struct common_obj *common;
  60. common = &ch->common[VPIF_VIDEO_INDEX];
  61. vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
  62. if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  63. return -EINVAL;
  64. vbuf->field = common->fmt.fmt.pix.field;
  65. if (vb->vb2_queue->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
  66. unsigned long addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  67. if (!ISALIGNED(addr + common->ytop_off) ||
  68. !ISALIGNED(addr + common->ybtm_off) ||
  69. !ISALIGNED(addr + common->ctop_off) ||
  70. !ISALIGNED(addr + common->cbtm_off)) {
  71. vpif_err("buffer offset not aligned to 8 bytes\n");
  72. return -EINVAL;
  73. }
  74. }
  75. return 0;
  76. }
  77. /**
  78. * vpif_buffer_queue_setup : Callback function for buffer setup.
  79. * @vq: vb2_queue ptr
  80. * @fmt: v4l2 format
  81. * @nbuffers: ptr to number of buffers requested by application
  82. * @nplanes:: contains number of distinct video planes needed to hold a frame
  83. * @sizes[]: contains the size (in bytes) of each plane.
  84. * @alloc_ctxs: ptr to allocation context
  85. *
  86. * This callback function is called when reqbuf() is called to adjust
  87. * the buffer count and buffer size
  88. */
  89. static int vpif_buffer_queue_setup(struct vb2_queue *vq,
  90. const void *parg,
  91. unsigned int *nbuffers, unsigned int *nplanes,
  92. unsigned int sizes[], void *alloc_ctxs[])
  93. {
  94. const struct v4l2_format *fmt = parg;
  95. struct channel_obj *ch = vb2_get_drv_priv(vq);
  96. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  97. if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
  98. return -EINVAL;
  99. if (vq->num_buffers + *nbuffers < 3)
  100. *nbuffers = 3 - vq->num_buffers;
  101. *nplanes = 1;
  102. sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
  103. alloc_ctxs[0] = common->alloc_ctx;
  104. /* Calculate the offset for Y and C data in the buffer */
  105. vpif_calculate_offsets(ch);
  106. return 0;
  107. }
  108. /**
  109. * vpif_buffer_queue : Callback function to add buffer to DMA queue
  110. * @vb: ptr to vb2_buffer
  111. *
  112. * This callback fucntion queues the buffer to DMA engine
  113. */
  114. static void vpif_buffer_queue(struct vb2_buffer *vb)
  115. {
  116. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  117. struct vpif_disp_buffer *buf = to_vpif_buffer(vbuf);
  118. struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
  119. struct common_obj *common;
  120. unsigned long flags;
  121. common = &ch->common[VPIF_VIDEO_INDEX];
  122. /* add the buffer to the DMA queue */
  123. spin_lock_irqsave(&common->irqlock, flags);
  124. list_add_tail(&buf->list, &common->dma_queue);
  125. spin_unlock_irqrestore(&common->irqlock, flags);
  126. }
  127. /**
  128. * vpif_start_streaming : Starts the DMA engine for streaming
  129. * @vb: ptr to vb2_buffer
  130. * @count: number of buffers
  131. */
  132. static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
  133. {
  134. struct vpif_display_config *vpif_config_data =
  135. vpif_dev->platform_data;
  136. struct channel_obj *ch = vb2_get_drv_priv(vq);
  137. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  138. struct vpif_params *vpif = &ch->vpifparams;
  139. struct vpif_disp_buffer *buf, *tmp;
  140. unsigned long addr, flags;
  141. int ret;
  142. spin_lock_irqsave(&common->irqlock, flags);
  143. /* Initialize field_id */
  144. ch->field_id = 0;
  145. /* clock settings */
  146. if (vpif_config_data->set_clock) {
  147. ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
  148. ycmux_mode, ch->vpifparams.std_info.hd_sd);
  149. if (ret < 0) {
  150. vpif_err("can't set clock\n");
  151. goto err;
  152. }
  153. }
  154. /* set the parameters and addresses */
  155. ret = vpif_set_video_params(vpif, ch->channel_id + 2);
  156. if (ret < 0)
  157. goto err;
  158. ycmux_mode = ret;
  159. vpif_config_addr(ch, ret);
  160. /* Get the next frame from the buffer queue */
  161. common->next_frm = common->cur_frm =
  162. list_entry(common->dma_queue.next,
  163. struct vpif_disp_buffer, list);
  164. list_del(&common->cur_frm->list);
  165. spin_unlock_irqrestore(&common->irqlock, flags);
  166. addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
  167. common->set_addr((addr + common->ytop_off),
  168. (addr + common->ybtm_off),
  169. (addr + common->ctop_off),
  170. (addr + common->cbtm_off));
  171. /*
  172. * Set interrupt for both the fields in VPIF
  173. * Register enable channel in VPIF register
  174. */
  175. channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
  176. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  177. channel2_intr_assert();
  178. channel2_intr_enable(1);
  179. enable_channel2(1);
  180. if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
  181. channel2_clipping_enable(1);
  182. }
  183. if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
  184. channel3_intr_assert();
  185. channel3_intr_enable(1);
  186. enable_channel3(1);
  187. if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
  188. channel3_clipping_enable(1);
  189. }
  190. return 0;
  191. err:
  192. list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
  193. list_del(&buf->list);
  194. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
  195. }
  196. spin_unlock_irqrestore(&common->irqlock, flags);
  197. return ret;
  198. }
  199. /**
  200. * vpif_stop_streaming : Stop the DMA engine
  201. * @vq: ptr to vb2_queue
  202. *
  203. * This callback stops the DMA engine and any remaining buffers
  204. * in the DMA queue are released.
  205. */
  206. static void vpif_stop_streaming(struct vb2_queue *vq)
  207. {
  208. struct channel_obj *ch = vb2_get_drv_priv(vq);
  209. struct common_obj *common;
  210. unsigned long flags;
  211. common = &ch->common[VPIF_VIDEO_INDEX];
  212. /* Disable channel */
  213. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  214. enable_channel2(0);
  215. channel2_intr_enable(0);
  216. }
  217. if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
  218. enable_channel3(0);
  219. channel3_intr_enable(0);
  220. }
  221. /* release all active buffers */
  222. spin_lock_irqsave(&common->irqlock, flags);
  223. if (common->cur_frm == common->next_frm) {
  224. vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
  225. VB2_BUF_STATE_ERROR);
  226. } else {
  227. if (common->cur_frm != NULL)
  228. vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
  229. VB2_BUF_STATE_ERROR);
  230. if (common->next_frm != NULL)
  231. vb2_buffer_done(&common->next_frm->vb.vb2_buf,
  232. VB2_BUF_STATE_ERROR);
  233. }
  234. while (!list_empty(&common->dma_queue)) {
  235. common->next_frm = list_entry(common->dma_queue.next,
  236. struct vpif_disp_buffer, list);
  237. list_del(&common->next_frm->list);
  238. vb2_buffer_done(&common->next_frm->vb.vb2_buf,
  239. VB2_BUF_STATE_ERROR);
  240. }
  241. spin_unlock_irqrestore(&common->irqlock, flags);
  242. }
  243. static struct vb2_ops video_qops = {
  244. .queue_setup = vpif_buffer_queue_setup,
  245. .wait_prepare = vb2_ops_wait_prepare,
  246. .wait_finish = vb2_ops_wait_finish,
  247. .buf_prepare = vpif_buffer_prepare,
  248. .start_streaming = vpif_start_streaming,
  249. .stop_streaming = vpif_stop_streaming,
  250. .buf_queue = vpif_buffer_queue,
  251. };
  252. static void process_progressive_mode(struct common_obj *common)
  253. {
  254. unsigned long addr = 0;
  255. spin_lock(&common->irqlock);
  256. /* Get the next buffer from buffer queue */
  257. common->next_frm = list_entry(common->dma_queue.next,
  258. struct vpif_disp_buffer, list);
  259. /* Remove that buffer from the buffer queue */
  260. list_del(&common->next_frm->list);
  261. spin_unlock(&common->irqlock);
  262. /* Set top and bottom field addrs in VPIF registers */
  263. addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
  264. common->set_addr(addr + common->ytop_off,
  265. addr + common->ybtm_off,
  266. addr + common->ctop_off,
  267. addr + common->cbtm_off);
  268. }
  269. static void process_interlaced_mode(int fid, struct common_obj *common)
  270. {
  271. /* device field id and local field id are in sync */
  272. /* If this is even field */
  273. if (0 == fid) {
  274. if (common->cur_frm == common->next_frm)
  275. return;
  276. /* one frame is displayed If next frame is
  277. * available, release cur_frm and move on */
  278. /* Copy frame display time */
  279. v4l2_get_timestamp(&common->cur_frm->vb.timestamp);
  280. /* Change status of the cur_frm */
  281. vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
  282. VB2_BUF_STATE_DONE);
  283. /* Make cur_frm pointing to next_frm */
  284. common->cur_frm = common->next_frm;
  285. } else if (1 == fid) { /* odd field */
  286. spin_lock(&common->irqlock);
  287. if (list_empty(&common->dma_queue)
  288. || (common->cur_frm != common->next_frm)) {
  289. spin_unlock(&common->irqlock);
  290. return;
  291. }
  292. spin_unlock(&common->irqlock);
  293. /* one field is displayed configure the next
  294. * frame if it is available else hold on current
  295. * frame */
  296. /* Get next from the buffer queue */
  297. process_progressive_mode(common);
  298. }
  299. }
  300. /*
  301. * vpif_channel_isr: It changes status of the displayed buffer, takes next
  302. * buffer from the queue and sets its address in VPIF registers
  303. */
  304. static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
  305. {
  306. struct vpif_device *dev = &vpif_obj;
  307. struct channel_obj *ch;
  308. struct common_obj *common;
  309. int fid = -1, i;
  310. int channel_id = 0;
  311. channel_id = *(int *)(dev_id);
  312. if (!vpif_intr_status(channel_id + 2))
  313. return IRQ_NONE;
  314. ch = dev->dev[channel_id];
  315. for (i = 0; i < VPIF_NUMOBJECTS; i++) {
  316. common = &ch->common[i];
  317. /* If streaming is started in this channel */
  318. if (1 == ch->vpifparams.std_info.frm_fmt) {
  319. spin_lock(&common->irqlock);
  320. if (list_empty(&common->dma_queue)) {
  321. spin_unlock(&common->irqlock);
  322. continue;
  323. }
  324. spin_unlock(&common->irqlock);
  325. /* Progressive mode */
  326. if (!channel_first_int[i][channel_id]) {
  327. /* Mark status of the cur_frm to
  328. * done and unlock semaphore on it */
  329. v4l2_get_timestamp(
  330. &common->cur_frm->vb.timestamp);
  331. vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
  332. VB2_BUF_STATE_DONE);
  333. /* Make cur_frm pointing to next_frm */
  334. common->cur_frm = common->next_frm;
  335. }
  336. channel_first_int[i][channel_id] = 0;
  337. process_progressive_mode(common);
  338. } else {
  339. /* Interlaced mode */
  340. /* If it is first interrupt, ignore it */
  341. if (channel_first_int[i][channel_id]) {
  342. channel_first_int[i][channel_id] = 0;
  343. continue;
  344. }
  345. if (0 == i) {
  346. ch->field_id ^= 1;
  347. /* Get field id from VPIF registers */
  348. fid = vpif_channel_getfid(ch->channel_id + 2);
  349. /* If fid does not match with stored field id */
  350. if (fid != ch->field_id) {
  351. /* Make them in sync */
  352. if (0 == fid)
  353. ch->field_id = fid;
  354. return IRQ_HANDLED;
  355. }
  356. }
  357. process_interlaced_mode(fid, common);
  358. }
  359. }
  360. return IRQ_HANDLED;
  361. }
  362. static int vpif_update_std_info(struct channel_obj *ch)
  363. {
  364. struct video_obj *vid_ch = &ch->video;
  365. struct vpif_params *vpifparams = &ch->vpifparams;
  366. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  367. const struct vpif_channel_config_params *config;
  368. int i;
  369. for (i = 0; i < vpif_ch_params_count; i++) {
  370. config = &vpif_ch_params[i];
  371. if (config->hd_sd == 0) {
  372. vpif_dbg(2, debug, "SD format\n");
  373. if (config->stdid & vid_ch->stdid) {
  374. memcpy(std_info, config, sizeof(*config));
  375. break;
  376. }
  377. }
  378. }
  379. if (i == vpif_ch_params_count) {
  380. vpif_dbg(1, debug, "Format not found\n");
  381. return -EINVAL;
  382. }
  383. return 0;
  384. }
  385. static int vpif_update_resolution(struct channel_obj *ch)
  386. {
  387. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  388. struct video_obj *vid_ch = &ch->video;
  389. struct vpif_params *vpifparams = &ch->vpifparams;
  390. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  391. if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
  392. return -EINVAL;
  393. if (vid_ch->stdid) {
  394. if (vpif_update_std_info(ch))
  395. return -EINVAL;
  396. }
  397. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  398. common->fmt.fmt.pix.width = std_info->width;
  399. common->fmt.fmt.pix.height = std_info->height;
  400. vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
  401. common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
  402. /* Set height and width paramateres */
  403. common->height = std_info->height;
  404. common->width = std_info->width;
  405. common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
  406. if (vid_ch->stdid)
  407. common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  408. else
  409. common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  410. if (ch->vpifparams.std_info.frm_fmt)
  411. common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
  412. else
  413. common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  414. return 0;
  415. }
  416. /*
  417. * vpif_calculate_offsets: This function calculates buffers offset for Y and C
  418. * in the top and bottom field
  419. */
  420. static void vpif_calculate_offsets(struct channel_obj *ch)
  421. {
  422. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  423. struct vpif_params *vpifparams = &ch->vpifparams;
  424. enum v4l2_field field = common->fmt.fmt.pix.field;
  425. struct video_obj *vid_ch = &ch->video;
  426. unsigned int hpitch, sizeimage;
  427. if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
  428. if (ch->vpifparams.std_info.frm_fmt)
  429. vid_ch->buf_field = V4L2_FIELD_NONE;
  430. else
  431. vid_ch->buf_field = V4L2_FIELD_INTERLACED;
  432. } else {
  433. vid_ch->buf_field = common->fmt.fmt.pix.field;
  434. }
  435. sizeimage = common->fmt.fmt.pix.sizeimage;
  436. hpitch = common->fmt.fmt.pix.bytesperline;
  437. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  438. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  439. common->ytop_off = 0;
  440. common->ybtm_off = hpitch;
  441. common->ctop_off = sizeimage / 2;
  442. common->cbtm_off = sizeimage / 2 + hpitch;
  443. } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
  444. common->ytop_off = 0;
  445. common->ybtm_off = sizeimage / 4;
  446. common->ctop_off = sizeimage / 2;
  447. common->cbtm_off = common->ctop_off + sizeimage / 4;
  448. } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
  449. common->ybtm_off = 0;
  450. common->ytop_off = sizeimage / 4;
  451. common->cbtm_off = sizeimage / 2;
  452. common->ctop_off = common->cbtm_off + sizeimage / 4;
  453. }
  454. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  455. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  456. vpifparams->video_params.storage_mode = 1;
  457. } else {
  458. vpifparams->video_params.storage_mode = 0;
  459. }
  460. if (ch->vpifparams.std_info.frm_fmt == 1) {
  461. vpifparams->video_params.hpitch =
  462. common->fmt.fmt.pix.bytesperline;
  463. } else {
  464. if ((field == V4L2_FIELD_ANY) ||
  465. (field == V4L2_FIELD_INTERLACED))
  466. vpifparams->video_params.hpitch =
  467. common->fmt.fmt.pix.bytesperline * 2;
  468. else
  469. vpifparams->video_params.hpitch =
  470. common->fmt.fmt.pix.bytesperline;
  471. }
  472. ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
  473. }
  474. static void vpif_config_addr(struct channel_obj *ch, int muxmode)
  475. {
  476. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  477. if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
  478. common->set_addr = ch3_set_videobuf_addr;
  479. } else {
  480. if (2 == muxmode)
  481. common->set_addr = ch2_set_videobuf_addr_yc_nmux;
  482. else
  483. common->set_addr = ch2_set_videobuf_addr;
  484. }
  485. }
  486. /* functions implementing ioctls */
  487. /**
  488. * vpif_querycap() - QUERYCAP handler
  489. * @file: file ptr
  490. * @priv: file handle
  491. * @cap: ptr to v4l2_capability structure
  492. */
  493. static int vpif_querycap(struct file *file, void *priv,
  494. struct v4l2_capability *cap)
  495. {
  496. struct vpif_display_config *config = vpif_dev->platform_data;
  497. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  498. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  499. strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
  500. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  501. dev_name(vpif_dev));
  502. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  503. return 0;
  504. }
  505. static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
  506. struct v4l2_fmtdesc *fmt)
  507. {
  508. if (fmt->index != 0)
  509. return -EINVAL;
  510. /* Fill in the information about format */
  511. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  512. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  513. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  514. fmt->flags = 0;
  515. return 0;
  516. }
  517. static int vpif_g_fmt_vid_out(struct file *file, void *priv,
  518. struct v4l2_format *fmt)
  519. {
  520. struct video_device *vdev = video_devdata(file);
  521. struct channel_obj *ch = video_get_drvdata(vdev);
  522. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  523. /* Check the validity of the buffer type */
  524. if (common->fmt.type != fmt->type)
  525. return -EINVAL;
  526. if (vpif_update_resolution(ch))
  527. return -EINVAL;
  528. *fmt = common->fmt;
  529. return 0;
  530. }
  531. static int vpif_try_fmt_vid_out(struct file *file, void *priv,
  532. struct v4l2_format *fmt)
  533. {
  534. struct video_device *vdev = video_devdata(file);
  535. struct channel_obj *ch = video_get_drvdata(vdev);
  536. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  537. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  538. /*
  539. * to supress v4l-compliance warnings silently correct
  540. * the pixelformat
  541. */
  542. if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
  543. pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
  544. if (vpif_update_resolution(ch))
  545. return -EINVAL;
  546. pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
  547. pixfmt->field = common->fmt.fmt.pix.field;
  548. pixfmt->bytesperline = common->fmt.fmt.pix.width;
  549. pixfmt->width = common->fmt.fmt.pix.width;
  550. pixfmt->height = common->fmt.fmt.pix.height;
  551. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
  552. return 0;
  553. }
  554. static int vpif_s_fmt_vid_out(struct file *file, void *priv,
  555. struct v4l2_format *fmt)
  556. {
  557. struct video_device *vdev = video_devdata(file);
  558. struct channel_obj *ch = video_get_drvdata(vdev);
  559. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  560. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  561. int ret;
  562. if (vb2_is_busy(&common->buffer_queue))
  563. return -EBUSY;
  564. ret = vpif_try_fmt_vid_out(file, priv, fmt);
  565. if (ret)
  566. return ret;
  567. /* store the pix format in the channel object */
  568. common->fmt.fmt.pix = *pixfmt;
  569. /* store the format in the channel object */
  570. common->fmt = *fmt;
  571. return 0;
  572. }
  573. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
  574. {
  575. struct vpif_display_config *config = vpif_dev->platform_data;
  576. struct video_device *vdev = video_devdata(file);
  577. struct channel_obj *ch = video_get_drvdata(vdev);
  578. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  579. struct vpif_display_chan_config *chan_cfg;
  580. struct v4l2_output output;
  581. int ret;
  582. if (config->chan_config[ch->channel_id].outputs == NULL)
  583. return -ENODATA;
  584. chan_cfg = &config->chan_config[ch->channel_id];
  585. output = chan_cfg->outputs[ch->output_idx].output;
  586. if (output.capabilities != V4L2_OUT_CAP_STD)
  587. return -ENODATA;
  588. if (vb2_is_busy(&common->buffer_queue))
  589. return -EBUSY;
  590. if (!(std_id & VPIF_V4L2_STD))
  591. return -EINVAL;
  592. /* Call encoder subdevice function to set the standard */
  593. ch->video.stdid = std_id;
  594. memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
  595. /* Get the information about the standard */
  596. if (vpif_update_resolution(ch))
  597. return -EINVAL;
  598. common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
  599. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  600. s_std_output, std_id);
  601. if (ret < 0) {
  602. vpif_err("Failed to set output standard\n");
  603. return ret;
  604. }
  605. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  606. s_std, std_id);
  607. if (ret < 0)
  608. vpif_err("Failed to set standard for sub devices\n");
  609. return ret;
  610. }
  611. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  612. {
  613. struct vpif_display_config *config = vpif_dev->platform_data;
  614. struct video_device *vdev = video_devdata(file);
  615. struct channel_obj *ch = video_get_drvdata(vdev);
  616. struct vpif_display_chan_config *chan_cfg;
  617. struct v4l2_output output;
  618. if (config->chan_config[ch->channel_id].outputs == NULL)
  619. return -ENODATA;
  620. chan_cfg = &config->chan_config[ch->channel_id];
  621. output = chan_cfg->outputs[ch->output_idx].output;
  622. if (output.capabilities != V4L2_OUT_CAP_STD)
  623. return -ENODATA;
  624. *std = ch->video.stdid;
  625. return 0;
  626. }
  627. static int vpif_enum_output(struct file *file, void *fh,
  628. struct v4l2_output *output)
  629. {
  630. struct vpif_display_config *config = vpif_dev->platform_data;
  631. struct video_device *vdev = video_devdata(file);
  632. struct channel_obj *ch = video_get_drvdata(vdev);
  633. struct vpif_display_chan_config *chan_cfg;
  634. chan_cfg = &config->chan_config[ch->channel_id];
  635. if (output->index >= chan_cfg->output_count) {
  636. vpif_dbg(1, debug, "Invalid output index\n");
  637. return -EINVAL;
  638. }
  639. *output = chan_cfg->outputs[output->index].output;
  640. return 0;
  641. }
  642. /**
  643. * vpif_output_to_subdev() - Maps output to sub device
  644. * @vpif_cfg - global config ptr
  645. * @chan_cfg - channel config ptr
  646. * @index - Given output index from application
  647. *
  648. * lookup the sub device information for a given output index.
  649. * we report all the output to application. output table also
  650. * has sub device name for the each output
  651. */
  652. static int
  653. vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
  654. struct vpif_display_chan_config *chan_cfg, int index)
  655. {
  656. struct vpif_subdev_info *subdev_info;
  657. const char *subdev_name;
  658. int i;
  659. vpif_dbg(2, debug, "vpif_output_to_subdev\n");
  660. if (chan_cfg->outputs == NULL)
  661. return -1;
  662. subdev_name = chan_cfg->outputs[index].subdev_name;
  663. if (subdev_name == NULL)
  664. return -1;
  665. /* loop through the sub device list to get the sub device info */
  666. for (i = 0; i < vpif_cfg->subdev_count; i++) {
  667. subdev_info = &vpif_cfg->subdevinfo[i];
  668. if (!strcmp(subdev_info->name, subdev_name))
  669. return i;
  670. }
  671. return -1;
  672. }
  673. /**
  674. * vpif_set_output() - Select an output
  675. * @vpif_cfg - global config ptr
  676. * @ch - channel
  677. * @index - Given output index from application
  678. *
  679. * Select the given output.
  680. */
  681. static int vpif_set_output(struct vpif_display_config *vpif_cfg,
  682. struct channel_obj *ch, int index)
  683. {
  684. struct vpif_display_chan_config *chan_cfg =
  685. &vpif_cfg->chan_config[ch->channel_id];
  686. struct v4l2_subdev *sd = NULL;
  687. u32 input = 0, output = 0;
  688. int sd_index;
  689. int ret;
  690. sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
  691. if (sd_index >= 0)
  692. sd = vpif_obj.sd[sd_index];
  693. if (sd) {
  694. input = chan_cfg->outputs[index].input_route;
  695. output = chan_cfg->outputs[index].output_route;
  696. ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
  697. if (ret < 0 && ret != -ENOIOCTLCMD) {
  698. vpif_err("Failed to set output\n");
  699. return ret;
  700. }
  701. }
  702. ch->output_idx = index;
  703. ch->sd = sd;
  704. if (chan_cfg->outputs != NULL)
  705. /* update tvnorms from the sub device output info */
  706. ch->video_dev.tvnorms = chan_cfg->outputs[index].output.std;
  707. return 0;
  708. }
  709. static int vpif_s_output(struct file *file, void *priv, unsigned int i)
  710. {
  711. struct vpif_display_config *config = vpif_dev->platform_data;
  712. struct video_device *vdev = video_devdata(file);
  713. struct channel_obj *ch = video_get_drvdata(vdev);
  714. struct vpif_display_chan_config *chan_cfg;
  715. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  716. if (vb2_is_busy(&common->buffer_queue))
  717. return -EBUSY;
  718. chan_cfg = &config->chan_config[ch->channel_id];
  719. if (i >= chan_cfg->output_count)
  720. return -EINVAL;
  721. return vpif_set_output(config, ch, i);
  722. }
  723. static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
  724. {
  725. struct video_device *vdev = video_devdata(file);
  726. struct channel_obj *ch = video_get_drvdata(vdev);
  727. *i = ch->output_idx;
  728. return 0;
  729. }
  730. /**
  731. * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
  732. * @file: file ptr
  733. * @priv: file handle
  734. * @timings: input timings
  735. */
  736. static int
  737. vpif_enum_dv_timings(struct file *file, void *priv,
  738. struct v4l2_enum_dv_timings *timings)
  739. {
  740. struct vpif_display_config *config = vpif_dev->platform_data;
  741. struct video_device *vdev = video_devdata(file);
  742. struct channel_obj *ch = video_get_drvdata(vdev);
  743. struct vpif_display_chan_config *chan_cfg;
  744. struct v4l2_output output;
  745. int ret;
  746. if (config->chan_config[ch->channel_id].outputs == NULL)
  747. return -ENODATA;
  748. chan_cfg = &config->chan_config[ch->channel_id];
  749. output = chan_cfg->outputs[ch->output_idx].output;
  750. if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
  751. return -ENODATA;
  752. timings->pad = 0;
  753. ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
  754. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  755. return -EINVAL;
  756. return ret;
  757. }
  758. /**
  759. * vpif_s_dv_timings() - S_DV_TIMINGS handler
  760. * @file: file ptr
  761. * @priv: file handle
  762. * @timings: digital video timings
  763. */
  764. static int vpif_s_dv_timings(struct file *file, void *priv,
  765. struct v4l2_dv_timings *timings)
  766. {
  767. struct vpif_display_config *config = vpif_dev->platform_data;
  768. struct video_device *vdev = video_devdata(file);
  769. struct channel_obj *ch = video_get_drvdata(vdev);
  770. struct vpif_params *vpifparams = &ch->vpifparams;
  771. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  772. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  773. struct video_obj *vid_ch = &ch->video;
  774. struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
  775. struct vpif_display_chan_config *chan_cfg;
  776. struct v4l2_output output;
  777. int ret;
  778. if (config->chan_config[ch->channel_id].outputs == NULL)
  779. return -ENODATA;
  780. chan_cfg = &config->chan_config[ch->channel_id];
  781. output = chan_cfg->outputs[ch->output_idx].output;
  782. if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
  783. return -ENODATA;
  784. if (vb2_is_busy(&common->buffer_queue))
  785. return -EBUSY;
  786. if (timings->type != V4L2_DV_BT_656_1120) {
  787. vpif_dbg(2, debug, "Timing type not defined\n");
  788. return -EINVAL;
  789. }
  790. /* Configure subdevice timings, if any */
  791. ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
  792. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  793. ret = 0;
  794. if (ret < 0) {
  795. vpif_dbg(2, debug, "Error setting custom DV timings\n");
  796. return ret;
  797. }
  798. if (!(timings->bt.width && timings->bt.height &&
  799. (timings->bt.hbackporch ||
  800. timings->bt.hfrontporch ||
  801. timings->bt.hsync) &&
  802. timings->bt.vfrontporch &&
  803. (timings->bt.vbackporch ||
  804. timings->bt.vsync))) {
  805. vpif_dbg(2, debug, "Timings for width, height, "
  806. "horizontal back porch, horizontal sync, "
  807. "horizontal front porch, vertical back porch, "
  808. "vertical sync and vertical back porch "
  809. "must be defined\n");
  810. return -EINVAL;
  811. }
  812. vid_ch->dv_timings = *timings;
  813. /* Configure video port timings */
  814. std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
  815. std_info->sav2eav = bt->width;
  816. std_info->l1 = 1;
  817. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  818. std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
  819. if (bt->interlaced) {
  820. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  821. std_info->l5 = std_info->vsize/2 -
  822. (bt->vfrontporch - 1);
  823. std_info->l7 = std_info->vsize/2 + 1;
  824. std_info->l9 = std_info->l7 + bt->il_vsync +
  825. bt->il_vbackporch + 1;
  826. std_info->l11 = std_info->vsize -
  827. (bt->il_vfrontporch - 1);
  828. } else {
  829. vpif_dbg(2, debug, "Required timing values for "
  830. "interlaced BT format missing\n");
  831. return -EINVAL;
  832. }
  833. } else {
  834. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  835. }
  836. strncpy(std_info->name, "Custom timings BT656/1120",
  837. VPIF_MAX_NAME);
  838. std_info->width = bt->width;
  839. std_info->height = bt->height;
  840. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  841. std_info->ycmux_mode = 0;
  842. std_info->capture_format = 0;
  843. std_info->vbi_supported = 0;
  844. std_info->hd_sd = 1;
  845. std_info->stdid = 0;
  846. vid_ch->stdid = 0;
  847. return 0;
  848. }
  849. /**
  850. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  851. * @file: file ptr
  852. * @priv: file handle
  853. * @timings: digital video timings
  854. */
  855. static int vpif_g_dv_timings(struct file *file, void *priv,
  856. struct v4l2_dv_timings *timings)
  857. {
  858. struct vpif_display_config *config = vpif_dev->platform_data;
  859. struct video_device *vdev = video_devdata(file);
  860. struct channel_obj *ch = video_get_drvdata(vdev);
  861. struct vpif_display_chan_config *chan_cfg;
  862. struct video_obj *vid_ch = &ch->video;
  863. struct v4l2_output output;
  864. if (config->chan_config[ch->channel_id].outputs == NULL)
  865. goto error;
  866. chan_cfg = &config->chan_config[ch->channel_id];
  867. output = chan_cfg->outputs[ch->output_idx].output;
  868. if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
  869. goto error;
  870. *timings = vid_ch->dv_timings;
  871. return 0;
  872. error:
  873. return -ENODATA;
  874. }
  875. /*
  876. * vpif_log_status() - Status information
  877. * @file: file ptr
  878. * @priv: file handle
  879. *
  880. * Returns zero.
  881. */
  882. static int vpif_log_status(struct file *filep, void *priv)
  883. {
  884. /* status for sub devices */
  885. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  886. return 0;
  887. }
  888. /* vpif display ioctl operations */
  889. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  890. .vidioc_querycap = vpif_querycap,
  891. .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
  892. .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
  893. .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
  894. .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
  895. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  896. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  897. .vidioc_querybuf = vb2_ioctl_querybuf,
  898. .vidioc_qbuf = vb2_ioctl_qbuf,
  899. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  900. .vidioc_expbuf = vb2_ioctl_expbuf,
  901. .vidioc_streamon = vb2_ioctl_streamon,
  902. .vidioc_streamoff = vb2_ioctl_streamoff,
  903. .vidioc_s_std = vpif_s_std,
  904. .vidioc_g_std = vpif_g_std,
  905. .vidioc_enum_output = vpif_enum_output,
  906. .vidioc_s_output = vpif_s_output,
  907. .vidioc_g_output = vpif_g_output,
  908. .vidioc_enum_dv_timings = vpif_enum_dv_timings,
  909. .vidioc_s_dv_timings = vpif_s_dv_timings,
  910. .vidioc_g_dv_timings = vpif_g_dv_timings,
  911. .vidioc_log_status = vpif_log_status,
  912. };
  913. static const struct v4l2_file_operations vpif_fops = {
  914. .owner = THIS_MODULE,
  915. .open = v4l2_fh_open,
  916. .release = vb2_fop_release,
  917. .unlocked_ioctl = video_ioctl2,
  918. .mmap = vb2_fop_mmap,
  919. .poll = vb2_fop_poll
  920. };
  921. /*Configure the channels, buffer sizei, request irq */
  922. static int initialize_vpif(void)
  923. {
  924. int free_channel_objects_index;
  925. int err, i, j;
  926. /* Allocate memory for six channel objects */
  927. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  928. vpif_obj.dev[i] =
  929. kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
  930. /* If memory allocation fails, return error */
  931. if (!vpif_obj.dev[i]) {
  932. free_channel_objects_index = i;
  933. err = -ENOMEM;
  934. goto vpif_init_free_channel_objects;
  935. }
  936. }
  937. return 0;
  938. vpif_init_free_channel_objects:
  939. for (j = 0; j < free_channel_objects_index; j++)
  940. kfree(vpif_obj.dev[j]);
  941. return err;
  942. }
  943. static int vpif_async_bound(struct v4l2_async_notifier *notifier,
  944. struct v4l2_subdev *subdev,
  945. struct v4l2_async_subdev *asd)
  946. {
  947. int i;
  948. for (i = 0; i < vpif_obj.config->subdev_count; i++)
  949. if (!strcmp(vpif_obj.config->subdevinfo[i].name,
  950. subdev->name)) {
  951. vpif_obj.sd[i] = subdev;
  952. vpif_obj.sd[i]->grp_id = 1 << i;
  953. return 0;
  954. }
  955. return -EINVAL;
  956. }
  957. static int vpif_probe_complete(void)
  958. {
  959. struct common_obj *common;
  960. struct video_device *vdev;
  961. struct channel_obj *ch;
  962. struct vb2_queue *q;
  963. int j, err, k;
  964. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  965. ch = vpif_obj.dev[j];
  966. /* Initialize field of the channel objects */
  967. for (k = 0; k < VPIF_NUMOBJECTS; k++) {
  968. common = &ch->common[k];
  969. spin_lock_init(&common->irqlock);
  970. mutex_init(&common->lock);
  971. common->set_addr = NULL;
  972. common->ytop_off = 0;
  973. common->ybtm_off = 0;
  974. common->ctop_off = 0;
  975. common->cbtm_off = 0;
  976. common->cur_frm = NULL;
  977. common->next_frm = NULL;
  978. memset(&common->fmt, 0, sizeof(common->fmt));
  979. }
  980. ch->initialized = 0;
  981. if (vpif_obj.config->subdev_count)
  982. ch->sd = vpif_obj.sd[0];
  983. ch->channel_id = j;
  984. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  985. ch->common[VPIF_VIDEO_INDEX].fmt.type =
  986. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  987. /* select output 0 */
  988. err = vpif_set_output(vpif_obj.config, ch, 0);
  989. if (err)
  990. goto probe_out;
  991. /* set initial format */
  992. ch->video.stdid = V4L2_STD_525_60;
  993. memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
  994. vpif_update_resolution(ch);
  995. /* Initialize vb2 queue */
  996. q = &common->buffer_queue;
  997. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  998. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  999. q->drv_priv = ch;
  1000. q->ops = &video_qops;
  1001. q->mem_ops = &vb2_dma_contig_memops;
  1002. q->buf_struct_size = sizeof(struct vpif_disp_buffer);
  1003. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1004. q->min_buffers_needed = 1;
  1005. q->lock = &common->lock;
  1006. err = vb2_queue_init(q);
  1007. if (err) {
  1008. vpif_err("vpif_display: vb2_queue_init() failed\n");
  1009. goto probe_out;
  1010. }
  1011. common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
  1012. if (IS_ERR(common->alloc_ctx)) {
  1013. vpif_err("Failed to get the context\n");
  1014. err = PTR_ERR(common->alloc_ctx);
  1015. goto probe_out;
  1016. }
  1017. INIT_LIST_HEAD(&common->dma_queue);
  1018. /* register video device */
  1019. vpif_dbg(1, debug, "channel=%p,channel->video_dev=%p\n",
  1020. ch, &ch->video_dev);
  1021. /* Initialize the video_device structure */
  1022. vdev = &ch->video_dev;
  1023. strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
  1024. vdev->release = video_device_release_empty;
  1025. vdev->fops = &vpif_fops;
  1026. vdev->ioctl_ops = &vpif_ioctl_ops;
  1027. vdev->v4l2_dev = &vpif_obj.v4l2_dev;
  1028. vdev->vfl_dir = VFL_DIR_TX;
  1029. vdev->queue = q;
  1030. vdev->lock = &common->lock;
  1031. video_set_drvdata(&ch->video_dev, ch);
  1032. err = video_register_device(vdev, VFL_TYPE_GRABBER,
  1033. (j ? 3 : 2));
  1034. if (err < 0)
  1035. goto probe_out;
  1036. }
  1037. return 0;
  1038. probe_out:
  1039. for (k = 0; k < j; k++) {
  1040. ch = vpif_obj.dev[k];
  1041. common = &ch->common[k];
  1042. vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
  1043. video_unregister_device(&ch->video_dev);
  1044. }
  1045. return err;
  1046. }
  1047. static int vpif_async_complete(struct v4l2_async_notifier *notifier)
  1048. {
  1049. return vpif_probe_complete();
  1050. }
  1051. /*
  1052. * vpif_probe: This function creates device entries by register itself to the
  1053. * V4L2 driver and initializes fields of each channel objects
  1054. */
  1055. static __init int vpif_probe(struct platform_device *pdev)
  1056. {
  1057. struct vpif_subdev_info *subdevdata;
  1058. struct i2c_adapter *i2c_adap;
  1059. struct resource *res;
  1060. int subdev_count;
  1061. int res_idx = 0;
  1062. int i, err;
  1063. vpif_dev = &pdev->dev;
  1064. err = initialize_vpif();
  1065. if (err) {
  1066. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1067. return err;
  1068. }
  1069. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1070. if (err) {
  1071. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1072. return err;
  1073. }
  1074. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
  1075. err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
  1076. IRQF_SHARED, VPIF_DRIVER_NAME,
  1077. (void *)(&vpif_obj.dev[res_idx]->
  1078. channel_id));
  1079. if (err) {
  1080. err = -EINVAL;
  1081. vpif_err("VPIF IRQ request failed\n");
  1082. goto vpif_unregister;
  1083. }
  1084. res_idx++;
  1085. }
  1086. vpif_obj.config = pdev->dev.platform_data;
  1087. subdev_count = vpif_obj.config->subdev_count;
  1088. subdevdata = vpif_obj.config->subdevinfo;
  1089. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1090. GFP_KERNEL);
  1091. if (vpif_obj.sd == NULL) {
  1092. vpif_err("unable to allocate memory for subdevice pointers\n");
  1093. err = -ENOMEM;
  1094. goto vpif_unregister;
  1095. }
  1096. if (!vpif_obj.config->asd_sizes) {
  1097. i2c_adap = i2c_get_adapter(1);
  1098. for (i = 0; i < subdev_count; i++) {
  1099. vpif_obj.sd[i] =
  1100. v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1101. i2c_adap,
  1102. &subdevdata[i].
  1103. board_info,
  1104. NULL);
  1105. if (!vpif_obj.sd[i]) {
  1106. vpif_err("Error registering v4l2 subdevice\n");
  1107. err = -ENODEV;
  1108. goto probe_subdev_out;
  1109. }
  1110. if (vpif_obj.sd[i])
  1111. vpif_obj.sd[i]->grp_id = 1 << i;
  1112. }
  1113. vpif_probe_complete();
  1114. } else {
  1115. vpif_obj.notifier.subdevs = vpif_obj.config->asd;
  1116. vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
  1117. vpif_obj.notifier.bound = vpif_async_bound;
  1118. vpif_obj.notifier.complete = vpif_async_complete;
  1119. err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
  1120. &vpif_obj.notifier);
  1121. if (err) {
  1122. vpif_err("Error registering async notifier\n");
  1123. err = -EINVAL;
  1124. goto probe_subdev_out;
  1125. }
  1126. }
  1127. return 0;
  1128. probe_subdev_out:
  1129. kfree(vpif_obj.sd);
  1130. vpif_unregister:
  1131. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1132. return err;
  1133. }
  1134. /*
  1135. * vpif_remove: It un-register channels from V4L2 driver
  1136. */
  1137. static int vpif_remove(struct platform_device *device)
  1138. {
  1139. struct common_obj *common;
  1140. struct channel_obj *ch;
  1141. int i;
  1142. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1143. kfree(vpif_obj.sd);
  1144. /* un-register device */
  1145. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1146. /* Get the pointer to the channel object */
  1147. ch = vpif_obj.dev[i];
  1148. common = &ch->common[VPIF_VIDEO_INDEX];
  1149. vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
  1150. /* Unregister video device */
  1151. video_unregister_device(&ch->video_dev);
  1152. kfree(vpif_obj.dev[i]);
  1153. }
  1154. return 0;
  1155. }
  1156. #ifdef CONFIG_PM_SLEEP
  1157. static int vpif_suspend(struct device *dev)
  1158. {
  1159. struct common_obj *common;
  1160. struct channel_obj *ch;
  1161. int i;
  1162. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1163. /* Get the pointer to the channel object */
  1164. ch = vpif_obj.dev[i];
  1165. common = &ch->common[VPIF_VIDEO_INDEX];
  1166. if (!vb2_start_streaming_called(&common->buffer_queue))
  1167. continue;
  1168. mutex_lock(&common->lock);
  1169. /* Disable channel */
  1170. if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
  1171. enable_channel2(0);
  1172. channel2_intr_enable(0);
  1173. }
  1174. if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
  1175. ycmux_mode == 2) {
  1176. enable_channel3(0);
  1177. channel3_intr_enable(0);
  1178. }
  1179. mutex_unlock(&common->lock);
  1180. }
  1181. return 0;
  1182. }
  1183. static int vpif_resume(struct device *dev)
  1184. {
  1185. struct common_obj *common;
  1186. struct channel_obj *ch;
  1187. int i;
  1188. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1189. /* Get the pointer to the channel object */
  1190. ch = vpif_obj.dev[i];
  1191. common = &ch->common[VPIF_VIDEO_INDEX];
  1192. if (!vb2_start_streaming_called(&common->buffer_queue))
  1193. continue;
  1194. mutex_lock(&common->lock);
  1195. /* Enable channel */
  1196. if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
  1197. enable_channel2(1);
  1198. channel2_intr_enable(1);
  1199. }
  1200. if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
  1201. ycmux_mode == 2) {
  1202. enable_channel3(1);
  1203. channel3_intr_enable(1);
  1204. }
  1205. mutex_unlock(&common->lock);
  1206. }
  1207. return 0;
  1208. }
  1209. #endif
  1210. static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
  1211. static __refdata struct platform_driver vpif_driver = {
  1212. .driver = {
  1213. .name = VPIF_DRIVER_NAME,
  1214. .pm = &vpif_pm_ops,
  1215. },
  1216. .probe = vpif_probe,
  1217. .remove = vpif_remove,
  1218. };
  1219. module_platform_driver(vpif_driver);