vpif_display.c 37 KB

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