vpif_display.c 39 KB

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