vpbe_display.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. /*
  2. * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/errno.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/string.h>
  19. #include <linux/wait.h>
  20. #include <linux/time.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/irq.h>
  23. #include <linux/mm.h>
  24. #include <linux/mutex.h>
  25. #include <linux/videodev2.h>
  26. #include <linux/slab.h>
  27. #include <asm/pgtable.h>
  28. #include <mach/cputype.h>
  29. #include <media/v4l2-dev.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/davinci/vpbe_display.h>
  34. #include <media/davinci/vpbe_types.h>
  35. #include <media/davinci/vpbe.h>
  36. #include <media/davinci/vpbe_venc.h>
  37. #include <media/davinci/vpbe_osd.h>
  38. #include "vpbe_venc_regs.h"
  39. #define VPBE_DISPLAY_DRIVER "vpbe-v4l2"
  40. static int debug;
  41. #define VPBE_DEFAULT_NUM_BUFS 3
  42. module_param(debug, int, 0644);
  43. static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
  44. struct vpbe_layer *layer);
  45. static int venc_is_second_field(struct vpbe_display *disp_dev)
  46. {
  47. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  48. int ret;
  49. int val;
  50. ret = v4l2_subdev_call(vpbe_dev->venc,
  51. core,
  52. ioctl,
  53. VENC_GET_FLD,
  54. &val);
  55. if (ret < 0) {
  56. v4l2_err(&vpbe_dev->v4l2_dev,
  57. "Error in getting Field ID 0\n");
  58. }
  59. return val;
  60. }
  61. static void vpbe_isr_even_field(struct vpbe_display *disp_obj,
  62. struct vpbe_layer *layer)
  63. {
  64. struct timespec timevalue;
  65. if (layer->cur_frm == layer->next_frm)
  66. return;
  67. ktime_get_ts(&timevalue);
  68. layer->cur_frm->vb.v4l2_buf.timestamp.tv_sec =
  69. timevalue.tv_sec;
  70. layer->cur_frm->vb.v4l2_buf.timestamp.tv_usec =
  71. timevalue.tv_nsec / NSEC_PER_USEC;
  72. vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_DONE);
  73. /* Make cur_frm pointing to next_frm */
  74. layer->cur_frm = layer->next_frm;
  75. }
  76. static void vpbe_isr_odd_field(struct vpbe_display *disp_obj,
  77. struct vpbe_layer *layer)
  78. {
  79. struct osd_state *osd_device = disp_obj->osd_device;
  80. unsigned long addr;
  81. spin_lock(&disp_obj->dma_queue_lock);
  82. if (list_empty(&layer->dma_queue) ||
  83. (layer->cur_frm != layer->next_frm)) {
  84. spin_unlock(&disp_obj->dma_queue_lock);
  85. return;
  86. }
  87. /*
  88. * one field is displayed configure
  89. * the next frame if it is available
  90. * otherwise hold on current frame
  91. * Get next from the buffer queue
  92. */
  93. layer->next_frm = list_entry(layer->dma_queue.next,
  94. struct vpbe_disp_buffer, list);
  95. /* Remove that from the buffer queue */
  96. list_del(&layer->next_frm->list);
  97. spin_unlock(&disp_obj->dma_queue_lock);
  98. /* Mark state of the frame to active */
  99. layer->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  100. addr = vb2_dma_contig_plane_dma_addr(&layer->next_frm->vb, 0);
  101. osd_device->ops.start_layer(osd_device,
  102. layer->layer_info.id,
  103. addr,
  104. disp_obj->cbcr_ofst);
  105. }
  106. /* interrupt service routine */
  107. static irqreturn_t venc_isr(int irq, void *arg)
  108. {
  109. struct vpbe_display *disp_dev = (struct vpbe_display *)arg;
  110. struct vpbe_layer *layer;
  111. static unsigned last_event;
  112. unsigned event = 0;
  113. int fid;
  114. int i;
  115. if ((NULL == arg) || (NULL == disp_dev->dev[0]))
  116. return IRQ_HANDLED;
  117. if (venc_is_second_field(disp_dev))
  118. event |= VENC_SECOND_FIELD;
  119. else
  120. event |= VENC_FIRST_FIELD;
  121. if (event == (last_event & ~VENC_END_OF_FRAME)) {
  122. /*
  123. * If the display is non-interlaced, then we need to flag the
  124. * end-of-frame event at every interrupt regardless of the
  125. * value of the FIDST bit. We can conclude that the display is
  126. * non-interlaced if the value of the FIDST bit is unchanged
  127. * from the previous interrupt.
  128. */
  129. event |= VENC_END_OF_FRAME;
  130. } else if (event == VENC_SECOND_FIELD) {
  131. /* end-of-frame for interlaced display */
  132. event |= VENC_END_OF_FRAME;
  133. }
  134. last_event = event;
  135. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  136. layer = disp_dev->dev[i];
  137. if (!vb2_start_streaming_called(&layer->buffer_queue))
  138. continue;
  139. if (layer->layer_first_int) {
  140. layer->layer_first_int = 0;
  141. continue;
  142. }
  143. /* Check the field format */
  144. if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
  145. (event & VENC_END_OF_FRAME)) {
  146. /* Progressive mode */
  147. vpbe_isr_even_field(disp_dev, layer);
  148. vpbe_isr_odd_field(disp_dev, layer);
  149. } else {
  150. /* Interlaced mode */
  151. layer->field_id ^= 1;
  152. if (event & VENC_FIRST_FIELD)
  153. fid = 0;
  154. else
  155. fid = 1;
  156. /*
  157. * If field id does not match with store
  158. * field id
  159. */
  160. if (fid != layer->field_id) {
  161. /* Make them in sync */
  162. layer->field_id = fid;
  163. continue;
  164. }
  165. /*
  166. * device field id and local field id are
  167. * in sync. If this is even field
  168. */
  169. if (0 == fid)
  170. vpbe_isr_even_field(disp_dev, layer);
  171. else /* odd field */
  172. vpbe_isr_odd_field(disp_dev, layer);
  173. }
  174. }
  175. return IRQ_HANDLED;
  176. }
  177. /*
  178. * vpbe_buffer_prepare()
  179. * This is the callback function called from vb2_qbuf() function
  180. * the buffer is prepared and user space virtual address is converted into
  181. * physical address
  182. */
  183. static int vpbe_buffer_prepare(struct vb2_buffer *vb)
  184. {
  185. struct vb2_queue *q = vb->vb2_queue;
  186. struct vpbe_layer *layer = vb2_get_drv_priv(q);
  187. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  188. unsigned long addr;
  189. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  190. "vpbe_buffer_prepare\n");
  191. vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
  192. if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  193. return -EINVAL;
  194. addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  195. if (!IS_ALIGNED(addr, 8)) {
  196. v4l2_err(&vpbe_dev->v4l2_dev,
  197. "buffer_prepare:offset is not aligned to 32 bytes\n");
  198. return -EINVAL;
  199. }
  200. return 0;
  201. }
  202. /*
  203. * vpbe_buffer_setup()
  204. * This function allocates memory for the buffers
  205. */
  206. static int
  207. vpbe_buffer_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  208. unsigned int *nbuffers, unsigned int *nplanes,
  209. unsigned int sizes[], void *alloc_ctxs[])
  210. {
  211. /* Get the file handle object and layer object */
  212. struct vpbe_layer *layer = vb2_get_drv_priv(vq);
  213. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  214. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
  215. if (fmt && fmt->fmt.pix.sizeimage < layer->pix_fmt.sizeimage)
  216. return -EINVAL;
  217. /* Store number of buffers allocated in numbuffer member */
  218. if (vq->num_buffers + *nbuffers < VPBE_DEFAULT_NUM_BUFS)
  219. *nbuffers = VPBE_DEFAULT_NUM_BUFS - vq->num_buffers;
  220. *nplanes = 1;
  221. sizes[0] = fmt ? fmt->fmt.pix.sizeimage : layer->pix_fmt.sizeimage;
  222. alloc_ctxs[0] = layer->alloc_ctx;
  223. return 0;
  224. }
  225. /*
  226. * vpbe_buffer_queue()
  227. * This function adds the buffer to DMA queue
  228. */
  229. static void vpbe_buffer_queue(struct vb2_buffer *vb)
  230. {
  231. /* Get the file handle object and layer object */
  232. struct vpbe_disp_buffer *buf = container_of(vb,
  233. struct vpbe_disp_buffer, vb);
  234. struct vpbe_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
  235. struct vpbe_display *disp = layer->disp_dev;
  236. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  237. unsigned long flags;
  238. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  239. "vpbe_buffer_queue\n");
  240. /* add the buffer to the DMA queue */
  241. spin_lock_irqsave(&disp->dma_queue_lock, flags);
  242. list_add_tail(&buf->list, &layer->dma_queue);
  243. spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
  244. }
  245. static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
  246. {
  247. struct vpbe_layer *layer = vb2_get_drv_priv(vq);
  248. struct osd_state *osd_device = layer->disp_dev->osd_device;
  249. int ret;
  250. osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
  251. /* Get the next frame from the buffer queue */
  252. layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
  253. struct vpbe_disp_buffer, list);
  254. /* Remove buffer from the buffer queue */
  255. list_del(&layer->cur_frm->list);
  256. /* Mark state of the current frame to active */
  257. layer->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  258. /* Initialize field_id and started member */
  259. layer->field_id = 0;
  260. /* Set parameters in OSD and VENC */
  261. ret = vpbe_set_osd_display_params(layer->disp_dev, layer);
  262. if (ret < 0) {
  263. struct vpbe_disp_buffer *buf, *tmp;
  264. vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_QUEUED);
  265. list_for_each_entry_safe(buf, tmp, &layer->dma_queue, list) {
  266. list_del(&buf->list);
  267. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
  268. }
  269. return ret;
  270. }
  271. /*
  272. * if request format is yuv420 semiplanar, need to
  273. * enable both video windows
  274. */
  275. layer->layer_first_int = 1;
  276. return ret;
  277. }
  278. static void vpbe_stop_streaming(struct vb2_queue *vq)
  279. {
  280. struct vpbe_layer *layer = vb2_get_drv_priv(vq);
  281. struct osd_state *osd_device = layer->disp_dev->osd_device;
  282. struct vpbe_display *disp = layer->disp_dev;
  283. unsigned long flags;
  284. if (!vb2_is_streaming(vq))
  285. return;
  286. osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
  287. /* release all active buffers */
  288. spin_lock_irqsave(&disp->dma_queue_lock, flags);
  289. if (layer->cur_frm == layer->next_frm) {
  290. vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_ERROR);
  291. } else {
  292. if (layer->cur_frm != NULL)
  293. vb2_buffer_done(&layer->cur_frm->vb,
  294. VB2_BUF_STATE_ERROR);
  295. if (layer->next_frm != NULL)
  296. vb2_buffer_done(&layer->next_frm->vb,
  297. VB2_BUF_STATE_ERROR);
  298. }
  299. while (!list_empty(&layer->dma_queue)) {
  300. layer->next_frm = list_entry(layer->dma_queue.next,
  301. struct vpbe_disp_buffer, list);
  302. list_del(&layer->next_frm->list);
  303. vb2_buffer_done(&layer->next_frm->vb, VB2_BUF_STATE_ERROR);
  304. }
  305. spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
  306. }
  307. static struct vb2_ops video_qops = {
  308. .queue_setup = vpbe_buffer_queue_setup,
  309. .wait_prepare = vb2_ops_wait_prepare,
  310. .wait_finish = vb2_ops_wait_finish,
  311. .buf_prepare = vpbe_buffer_prepare,
  312. .start_streaming = vpbe_start_streaming,
  313. .stop_streaming = vpbe_stop_streaming,
  314. .buf_queue = vpbe_buffer_queue,
  315. };
  316. static
  317. struct vpbe_layer*
  318. _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
  319. struct vpbe_layer *layer)
  320. {
  321. enum vpbe_display_device_id thiswin, otherwin;
  322. thiswin = layer->device_id;
  323. otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
  324. VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
  325. return disp_dev->dev[otherwin];
  326. }
  327. static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
  328. struct vpbe_layer *layer)
  329. {
  330. struct osd_layer_config *cfg = &layer->layer_info.config;
  331. struct osd_state *osd_device = disp_dev->osd_device;
  332. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  333. unsigned long addr;
  334. int ret;
  335. addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb, 0);
  336. /* Set address in the display registers */
  337. osd_device->ops.start_layer(osd_device,
  338. layer->layer_info.id,
  339. addr,
  340. disp_dev->cbcr_ofst);
  341. ret = osd_device->ops.enable_layer(osd_device,
  342. layer->layer_info.id, 0);
  343. if (ret < 0) {
  344. v4l2_err(&vpbe_dev->v4l2_dev,
  345. "Error in enabling osd window layer 0\n");
  346. return -1;
  347. }
  348. /* Enable the window */
  349. layer->layer_info.enable = 1;
  350. if (cfg->pixfmt == PIXFMT_NV12) {
  351. struct vpbe_layer *otherlayer =
  352. _vpbe_display_get_other_win_layer(disp_dev, layer);
  353. ret = osd_device->ops.enable_layer(osd_device,
  354. otherlayer->layer_info.id, 1);
  355. if (ret < 0) {
  356. v4l2_err(&vpbe_dev->v4l2_dev,
  357. "Error in enabling osd window layer 1\n");
  358. return -1;
  359. }
  360. otherlayer->layer_info.enable = 1;
  361. }
  362. return 0;
  363. }
  364. static void
  365. vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
  366. struct vpbe_layer *layer,
  367. int expected_xsize, int expected_ysize)
  368. {
  369. struct display_layer_info *layer_info = &layer->layer_info;
  370. struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
  371. struct osd_layer_config *cfg = &layer->layer_info.config;
  372. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  373. int calculated_xsize;
  374. int h_exp = 0;
  375. int v_exp = 0;
  376. int h_scale;
  377. int v_scale;
  378. v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
  379. /*
  380. * Application initially set the image format. Current display
  381. * size is obtained from the vpbe display controller. expected_xsize
  382. * and expected_ysize are set through S_CROP ioctl. Based on this,
  383. * driver will calculate the scale factors for vertical and
  384. * horizontal direction so that the image is displayed scaled
  385. * and expanded. Application uses expansion to display the image
  386. * in a square pixel. Otherwise it is displayed using displays
  387. * pixel aspect ratio.It is expected that application chooses
  388. * the crop coordinates for cropped or scaled display. if crop
  389. * size is less than the image size, it is displayed cropped or
  390. * it is displayed scaled and/or expanded.
  391. *
  392. * to begin with, set the crop window same as expected. Later we
  393. * will override with scaled window size
  394. */
  395. cfg->xsize = pixfmt->width;
  396. cfg->ysize = pixfmt->height;
  397. layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
  398. layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
  399. layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
  400. layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
  401. if (pixfmt->width < expected_xsize) {
  402. h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
  403. if (h_scale < 2)
  404. h_scale = 1;
  405. else if (h_scale >= 4)
  406. h_scale = 4;
  407. else
  408. h_scale = 2;
  409. cfg->xsize *= h_scale;
  410. if (cfg->xsize < expected_xsize) {
  411. if ((standard_id & V4L2_STD_525_60) ||
  412. (standard_id & V4L2_STD_625_50)) {
  413. calculated_xsize = (cfg->xsize *
  414. VPBE_DISPLAY_H_EXP_RATIO_N) /
  415. VPBE_DISPLAY_H_EXP_RATIO_D;
  416. if (calculated_xsize <= expected_xsize) {
  417. h_exp = 1;
  418. cfg->xsize = calculated_xsize;
  419. }
  420. }
  421. }
  422. if (h_scale == 2)
  423. layer_info->h_zoom = ZOOM_X2;
  424. else if (h_scale == 4)
  425. layer_info->h_zoom = ZOOM_X4;
  426. if (h_exp)
  427. layer_info->h_exp = H_EXP_9_OVER_8;
  428. } else {
  429. /* no scaling, only cropping. Set display area to crop area */
  430. cfg->xsize = expected_xsize;
  431. }
  432. if (pixfmt->height < expected_ysize) {
  433. v_scale = expected_ysize / pixfmt->height;
  434. if (v_scale < 2)
  435. v_scale = 1;
  436. else if (v_scale >= 4)
  437. v_scale = 4;
  438. else
  439. v_scale = 2;
  440. cfg->ysize *= v_scale;
  441. if (cfg->ysize < expected_ysize) {
  442. if ((standard_id & V4L2_STD_625_50)) {
  443. calculated_xsize = (cfg->ysize *
  444. VPBE_DISPLAY_V_EXP_RATIO_N) /
  445. VPBE_DISPLAY_V_EXP_RATIO_D;
  446. if (calculated_xsize <= expected_ysize) {
  447. v_exp = 1;
  448. cfg->ysize = calculated_xsize;
  449. }
  450. }
  451. }
  452. if (v_scale == 2)
  453. layer_info->v_zoom = ZOOM_X2;
  454. else if (v_scale == 4)
  455. layer_info->v_zoom = ZOOM_X4;
  456. if (v_exp)
  457. layer_info->h_exp = V_EXP_6_OVER_5;
  458. } else {
  459. /* no scaling, only cropping. Set display area to crop area */
  460. cfg->ysize = expected_ysize;
  461. }
  462. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  463. "crop display xsize = %d, ysize = %d\n",
  464. cfg->xsize, cfg->ysize);
  465. }
  466. static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
  467. struct vpbe_layer *layer,
  468. int top, int left)
  469. {
  470. struct osd_layer_config *cfg = &layer->layer_info.config;
  471. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  472. cfg->xpos = min((unsigned int)left,
  473. vpbe_dev->current_timings.xres - cfg->xsize);
  474. cfg->ypos = min((unsigned int)top,
  475. vpbe_dev->current_timings.yres - cfg->ysize);
  476. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  477. "new xpos = %d, ypos = %d\n",
  478. cfg->xpos, cfg->ypos);
  479. }
  480. static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
  481. struct v4l2_rect *c)
  482. {
  483. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  484. if ((c->width == 0) ||
  485. ((c->width + c->left) > vpbe_dev->current_timings.xres))
  486. c->width = vpbe_dev->current_timings.xres - c->left;
  487. if ((c->height == 0) || ((c->height + c->top) >
  488. vpbe_dev->current_timings.yres))
  489. c->height = vpbe_dev->current_timings.yres - c->top;
  490. /* window height must be even for interlaced display */
  491. if (vpbe_dev->current_timings.interlaced)
  492. c->height &= (~0x01);
  493. }
  494. /**
  495. * vpbe_try_format()
  496. * If user application provides width and height, and have bytesperline set
  497. * to zero, driver calculates bytesperline and sizeimage based on hardware
  498. * limits.
  499. */
  500. static int vpbe_try_format(struct vpbe_display *disp_dev,
  501. struct v4l2_pix_format *pixfmt, int check)
  502. {
  503. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  504. int min_height = 1;
  505. int min_width = 32;
  506. int max_height;
  507. int max_width;
  508. int bpp;
  509. if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
  510. (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
  511. /* choose default as V4L2_PIX_FMT_UYVY */
  512. pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
  513. /* Check the field format */
  514. if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
  515. (pixfmt->field != V4L2_FIELD_NONE)) {
  516. if (vpbe_dev->current_timings.interlaced)
  517. pixfmt->field = V4L2_FIELD_INTERLACED;
  518. else
  519. pixfmt->field = V4L2_FIELD_NONE;
  520. }
  521. if (pixfmt->field == V4L2_FIELD_INTERLACED)
  522. min_height = 2;
  523. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  524. bpp = 1;
  525. else
  526. bpp = 2;
  527. max_width = vpbe_dev->current_timings.xres;
  528. max_height = vpbe_dev->current_timings.yres;
  529. min_width /= bpp;
  530. if (!pixfmt->width || (pixfmt->width < min_width) ||
  531. (pixfmt->width > max_width)) {
  532. pixfmt->width = vpbe_dev->current_timings.xres;
  533. }
  534. if (!pixfmt->height || (pixfmt->height < min_height) ||
  535. (pixfmt->height > max_height)) {
  536. pixfmt->height = vpbe_dev->current_timings.yres;
  537. }
  538. if (pixfmt->bytesperline < (pixfmt->width * bpp))
  539. pixfmt->bytesperline = pixfmt->width * bpp;
  540. /* Make the bytesperline 32 byte aligned */
  541. pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
  542. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  543. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
  544. (pixfmt->bytesperline * pixfmt->height >> 1);
  545. else
  546. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  547. return 0;
  548. }
  549. static int vpbe_display_querycap(struct file *file, void *priv,
  550. struct v4l2_capability *cap)
  551. {
  552. struct vpbe_layer *layer = video_drvdata(file);
  553. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  554. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  555. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  556. snprintf(cap->driver, sizeof(cap->driver), "%s",
  557. dev_name(vpbe_dev->pdev));
  558. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  559. dev_name(vpbe_dev->pdev));
  560. strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
  561. return 0;
  562. }
  563. static int vpbe_display_s_crop(struct file *file, void *priv,
  564. const struct v4l2_crop *crop)
  565. {
  566. struct vpbe_layer *layer = video_drvdata(file);
  567. struct vpbe_display *disp_dev = layer->disp_dev;
  568. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  569. struct osd_layer_config *cfg = &layer->layer_info.config;
  570. struct osd_state *osd_device = disp_dev->osd_device;
  571. struct v4l2_rect rect = crop->c;
  572. int ret;
  573. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  574. "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
  575. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  576. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
  577. return -EINVAL;
  578. }
  579. if (rect.top < 0)
  580. rect.top = 0;
  581. if (rect.left < 0)
  582. rect.left = 0;
  583. vpbe_disp_check_window_params(disp_dev, &rect);
  584. osd_device->ops.get_layer_config(osd_device,
  585. layer->layer_info.id, cfg);
  586. vpbe_disp_calculate_scale_factor(disp_dev, layer,
  587. rect.width,
  588. rect.height);
  589. vpbe_disp_adj_position(disp_dev, layer, rect.top,
  590. rect.left);
  591. ret = osd_device->ops.set_layer_config(osd_device,
  592. layer->layer_info.id, cfg);
  593. if (ret < 0) {
  594. v4l2_err(&vpbe_dev->v4l2_dev,
  595. "Error in set layer config:\n");
  596. return -EINVAL;
  597. }
  598. /* apply zooming and h or v expansion */
  599. osd_device->ops.set_zoom(osd_device,
  600. layer->layer_info.id,
  601. layer->layer_info.h_zoom,
  602. layer->layer_info.v_zoom);
  603. ret = osd_device->ops.set_vid_expansion(osd_device,
  604. layer->layer_info.h_exp,
  605. layer->layer_info.v_exp);
  606. if (ret < 0) {
  607. v4l2_err(&vpbe_dev->v4l2_dev,
  608. "Error in set vid expansion:\n");
  609. return -EINVAL;
  610. }
  611. if ((layer->layer_info.h_zoom != ZOOM_X1) ||
  612. (layer->layer_info.v_zoom != ZOOM_X1) ||
  613. (layer->layer_info.h_exp != H_EXP_OFF) ||
  614. (layer->layer_info.v_exp != V_EXP_OFF))
  615. /* Enable expansion filter */
  616. osd_device->ops.set_interpolation_filter(osd_device, 1);
  617. else
  618. osd_device->ops.set_interpolation_filter(osd_device, 0);
  619. return 0;
  620. }
  621. static int vpbe_display_g_crop(struct file *file, void *priv,
  622. struct v4l2_crop *crop)
  623. {
  624. struct vpbe_layer *layer = video_drvdata(file);
  625. struct osd_layer_config *cfg = &layer->layer_info.config;
  626. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  627. struct osd_state *osd_device = layer->disp_dev->osd_device;
  628. struct v4l2_rect *rect = &crop->c;
  629. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  630. "VIDIOC_G_CROP, layer id = %d\n",
  631. layer->device_id);
  632. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  633. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
  634. return -EINVAL;
  635. }
  636. osd_device->ops.get_layer_config(osd_device,
  637. layer->layer_info.id, cfg);
  638. rect->top = cfg->ypos;
  639. rect->left = cfg->xpos;
  640. rect->width = cfg->xsize;
  641. rect->height = cfg->ysize;
  642. return 0;
  643. }
  644. static int vpbe_display_cropcap(struct file *file, void *priv,
  645. struct v4l2_cropcap *cropcap)
  646. {
  647. struct vpbe_layer *layer = video_drvdata(file);
  648. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  649. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
  650. cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  651. cropcap->bounds.left = 0;
  652. cropcap->bounds.top = 0;
  653. cropcap->bounds.width = vpbe_dev->current_timings.xres;
  654. cropcap->bounds.height = vpbe_dev->current_timings.yres;
  655. cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
  656. cropcap->defrect = cropcap->bounds;
  657. return 0;
  658. }
  659. static int vpbe_display_g_fmt(struct file *file, void *priv,
  660. struct v4l2_format *fmt)
  661. {
  662. struct vpbe_layer *layer = video_drvdata(file);
  663. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  664. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  665. "VIDIOC_G_FMT, layer id = %d\n",
  666. layer->device_id);
  667. /* If buffer type is video output */
  668. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  669. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  670. return -EINVAL;
  671. }
  672. /* Fill in the information about format */
  673. fmt->fmt.pix = layer->pix_fmt;
  674. return 0;
  675. }
  676. static int vpbe_display_enum_fmt(struct file *file, void *priv,
  677. struct v4l2_fmtdesc *fmt)
  678. {
  679. struct vpbe_layer *layer = video_drvdata(file);
  680. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  681. unsigned int index = 0;
  682. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  683. "VIDIOC_ENUM_FMT, layer id = %d\n",
  684. layer->device_id);
  685. if (fmt->index > 1) {
  686. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
  687. return -EINVAL;
  688. }
  689. /* Fill in the information about format */
  690. index = fmt->index;
  691. memset(fmt, 0, sizeof(*fmt));
  692. fmt->index = index;
  693. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  694. if (index == 0) {
  695. strcpy(fmt->description, "YUV 4:2:2 - UYVY");
  696. fmt->pixelformat = V4L2_PIX_FMT_UYVY;
  697. } else {
  698. strcpy(fmt->description, "Y/CbCr 4:2:0");
  699. fmt->pixelformat = V4L2_PIX_FMT_NV12;
  700. }
  701. return 0;
  702. }
  703. static int vpbe_display_s_fmt(struct file *file, void *priv,
  704. struct v4l2_format *fmt)
  705. {
  706. struct vpbe_layer *layer = video_drvdata(file);
  707. struct vpbe_display *disp_dev = layer->disp_dev;
  708. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  709. struct osd_layer_config *cfg = &layer->layer_info.config;
  710. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  711. struct osd_state *osd_device = disp_dev->osd_device;
  712. int ret;
  713. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  714. "VIDIOC_S_FMT, layer id = %d\n",
  715. layer->device_id);
  716. if (vb2_is_busy(&layer->buffer_queue))
  717. return -EBUSY;
  718. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  719. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
  720. return -EINVAL;
  721. }
  722. /* Check for valid pixel format */
  723. ret = vpbe_try_format(disp_dev, pixfmt, 1);
  724. if (ret)
  725. return ret;
  726. /* YUV420 is requested, check availability of the
  727. other video window */
  728. layer->pix_fmt = *pixfmt;
  729. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
  730. struct vpbe_layer *otherlayer;
  731. otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
  732. /* if other layer is available, only
  733. * claim it, do not configure it
  734. */
  735. ret = osd_device->ops.request_layer(osd_device,
  736. otherlayer->layer_info.id);
  737. if (ret < 0) {
  738. v4l2_err(&vpbe_dev->v4l2_dev,
  739. "Display Manager failed to allocate layer\n");
  740. return -EBUSY;
  741. }
  742. }
  743. /* Get osd layer config */
  744. osd_device->ops.get_layer_config(osd_device,
  745. layer->layer_info.id, cfg);
  746. /* Store the pixel format in the layer object */
  747. cfg->xsize = pixfmt->width;
  748. cfg->ysize = pixfmt->height;
  749. cfg->line_length = pixfmt->bytesperline;
  750. cfg->ypos = 0;
  751. cfg->xpos = 0;
  752. cfg->interlaced = vpbe_dev->current_timings.interlaced;
  753. if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
  754. cfg->pixfmt = PIXFMT_YCBCRI;
  755. /* Change of the default pixel format for both video windows */
  756. if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
  757. struct vpbe_layer *otherlayer;
  758. cfg->pixfmt = PIXFMT_NV12;
  759. otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
  760. layer);
  761. otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
  762. }
  763. /* Set the layer config in the osd window */
  764. ret = osd_device->ops.set_layer_config(osd_device,
  765. layer->layer_info.id, cfg);
  766. if (ret < 0) {
  767. v4l2_err(&vpbe_dev->v4l2_dev,
  768. "Error in S_FMT params:\n");
  769. return -EINVAL;
  770. }
  771. /* Readback and fill the local copy of current pix format */
  772. osd_device->ops.get_layer_config(osd_device,
  773. layer->layer_info.id, cfg);
  774. return 0;
  775. }
  776. static int vpbe_display_try_fmt(struct file *file, void *priv,
  777. struct v4l2_format *fmt)
  778. {
  779. struct vpbe_layer *layer = video_drvdata(file);
  780. struct vpbe_display *disp_dev = layer->disp_dev;
  781. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  782. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  783. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
  784. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  785. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  786. return -EINVAL;
  787. }
  788. /* Check for valid field format */
  789. return vpbe_try_format(disp_dev, pixfmt, 0);
  790. }
  791. /**
  792. * vpbe_display_s_std - Set the given standard in the encoder
  793. *
  794. * Sets the standard if supported by the current encoder. Return the status.
  795. * 0 - success & -EINVAL on error
  796. */
  797. static int vpbe_display_s_std(struct file *file, void *priv,
  798. v4l2_std_id std_id)
  799. {
  800. struct vpbe_layer *layer = video_drvdata(file);
  801. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  802. int ret;
  803. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
  804. if (vb2_is_busy(&layer->buffer_queue))
  805. return -EBUSY;
  806. if (NULL != vpbe_dev->ops.s_std) {
  807. ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
  808. if (ret) {
  809. v4l2_err(&vpbe_dev->v4l2_dev,
  810. "Failed to set standard for sub devices\n");
  811. return -EINVAL;
  812. }
  813. } else {
  814. return -EINVAL;
  815. }
  816. return 0;
  817. }
  818. /**
  819. * vpbe_display_g_std - Get the standard in the current encoder
  820. *
  821. * Get the standard in the current encoder. Return the status. 0 - success
  822. * -EINVAL on error
  823. */
  824. static int vpbe_display_g_std(struct file *file, void *priv,
  825. v4l2_std_id *std_id)
  826. {
  827. struct vpbe_layer *layer = video_drvdata(file);
  828. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  829. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
  830. /* Get the standard from the current encoder */
  831. if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
  832. *std_id = vpbe_dev->current_timings.std_id;
  833. return 0;
  834. }
  835. return -EINVAL;
  836. }
  837. /**
  838. * vpbe_display_enum_output - enumerate outputs
  839. *
  840. * Enumerates the outputs available at the vpbe display
  841. * returns the status, -EINVAL if end of output list
  842. */
  843. static int vpbe_display_enum_output(struct file *file, void *priv,
  844. struct v4l2_output *output)
  845. {
  846. struct vpbe_layer *layer = video_drvdata(file);
  847. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  848. int ret;
  849. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
  850. /* Enumerate outputs */
  851. if (NULL == vpbe_dev->ops.enum_outputs)
  852. return -EINVAL;
  853. ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
  854. if (ret) {
  855. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  856. "Failed to enumerate outputs\n");
  857. return -EINVAL;
  858. }
  859. return 0;
  860. }
  861. /**
  862. * vpbe_display_s_output - Set output to
  863. * the output specified by the index
  864. */
  865. static int vpbe_display_s_output(struct file *file, void *priv,
  866. unsigned int i)
  867. {
  868. struct vpbe_layer *layer = video_drvdata(file);
  869. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  870. int ret;
  871. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
  872. if (vb2_is_busy(&layer->buffer_queue))
  873. return -EBUSY;
  874. if (NULL == vpbe_dev->ops.set_output)
  875. return -EINVAL;
  876. ret = vpbe_dev->ops.set_output(vpbe_dev, i);
  877. if (ret) {
  878. v4l2_err(&vpbe_dev->v4l2_dev,
  879. "Failed to set output for sub devices\n");
  880. return -EINVAL;
  881. }
  882. return 0;
  883. }
  884. /**
  885. * vpbe_display_g_output - Get output from subdevice
  886. * for a given by the index
  887. */
  888. static int vpbe_display_g_output(struct file *file, void *priv,
  889. unsigned int *i)
  890. {
  891. struct vpbe_layer *layer = video_drvdata(file);
  892. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  893. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
  894. /* Get the standard from the current encoder */
  895. *i = vpbe_dev->current_out_index;
  896. return 0;
  897. }
  898. /**
  899. * vpbe_display_enum_dv_timings - Enumerate the dv timings
  900. *
  901. * enum the timings in the current encoder. Return the status. 0 - success
  902. * -EINVAL on error
  903. */
  904. static int
  905. vpbe_display_enum_dv_timings(struct file *file, void *priv,
  906. struct v4l2_enum_dv_timings *timings)
  907. {
  908. struct vpbe_layer *layer = video_drvdata(file);
  909. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  910. int ret;
  911. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
  912. /* Enumerate outputs */
  913. if (NULL == vpbe_dev->ops.enum_dv_timings)
  914. return -EINVAL;
  915. ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
  916. if (ret) {
  917. v4l2_err(&vpbe_dev->v4l2_dev,
  918. "Failed to enumerate dv timings info\n");
  919. return -EINVAL;
  920. }
  921. return 0;
  922. }
  923. /**
  924. * vpbe_display_s_dv_timings - Set the dv timings
  925. *
  926. * Set the timings in the current encoder. Return the status. 0 - success
  927. * -EINVAL on error
  928. */
  929. static int
  930. vpbe_display_s_dv_timings(struct file *file, void *priv,
  931. struct v4l2_dv_timings *timings)
  932. {
  933. struct vpbe_layer *layer = video_drvdata(file);
  934. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  935. int ret;
  936. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
  937. if (vb2_is_busy(&layer->buffer_queue))
  938. return -EBUSY;
  939. /* Set the given standard in the encoder */
  940. if (!vpbe_dev->ops.s_dv_timings)
  941. return -EINVAL;
  942. ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
  943. if (ret) {
  944. v4l2_err(&vpbe_dev->v4l2_dev,
  945. "Failed to set the dv timings info\n");
  946. return -EINVAL;
  947. }
  948. return 0;
  949. }
  950. /**
  951. * vpbe_display_g_dv_timings - Set the dv timings
  952. *
  953. * Get the timings in the current encoder. Return the status. 0 - success
  954. * -EINVAL on error
  955. */
  956. static int
  957. vpbe_display_g_dv_timings(struct file *file, void *priv,
  958. struct v4l2_dv_timings *dv_timings)
  959. {
  960. struct vpbe_layer *layer = video_drvdata(file);
  961. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  962. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
  963. /* Get the given standard in the encoder */
  964. if (vpbe_dev->current_timings.timings_type &
  965. VPBE_ENC_DV_TIMINGS) {
  966. *dv_timings = vpbe_dev->current_timings.dv_timings;
  967. } else {
  968. return -EINVAL;
  969. }
  970. return 0;
  971. }
  972. /*
  973. * vpbe_display_open()
  974. * It creates object of file handle structure and stores it in private_data
  975. * member of filepointer
  976. */
  977. static int vpbe_display_open(struct file *file)
  978. {
  979. struct vpbe_layer *layer = video_drvdata(file);
  980. struct vpbe_display *disp_dev = layer->disp_dev;
  981. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  982. struct osd_state *osd_device = disp_dev->osd_device;
  983. int err;
  984. /* creating context for file descriptor */
  985. err = v4l2_fh_open(file);
  986. if (err) {
  987. v4l2_err(&vpbe_dev->v4l2_dev, "v4l2_fh_open failed\n");
  988. return err;
  989. }
  990. /* leaving if layer is already initialized */
  991. if (!v4l2_fh_is_singular_file(file))
  992. return err;
  993. if (!layer->usrs) {
  994. if (mutex_lock_interruptible(&layer->opslock))
  995. return -ERESTARTSYS;
  996. /* First claim the layer for this device */
  997. err = osd_device->ops.request_layer(osd_device,
  998. layer->layer_info.id);
  999. mutex_unlock(&layer->opslock);
  1000. if (err < 0) {
  1001. /* Couldn't get layer */
  1002. v4l2_err(&vpbe_dev->v4l2_dev,
  1003. "Display Manager failed to allocate layer\n");
  1004. v4l2_fh_release(file);
  1005. return -EINVAL;
  1006. }
  1007. }
  1008. /* Increment layer usrs counter */
  1009. layer->usrs++;
  1010. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1011. "vpbe display device opened successfully\n");
  1012. return 0;
  1013. }
  1014. /*
  1015. * vpbe_display_release()
  1016. * This function deletes buffer queue, frees the buffers and the davinci
  1017. * display file * handle
  1018. */
  1019. static int vpbe_display_release(struct file *file)
  1020. {
  1021. struct vpbe_layer *layer = video_drvdata(file);
  1022. struct osd_layer_config *cfg = &layer->layer_info.config;
  1023. struct vpbe_display *disp_dev = layer->disp_dev;
  1024. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1025. struct osd_state *osd_device = disp_dev->osd_device;
  1026. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
  1027. mutex_lock(&layer->opslock);
  1028. osd_device->ops.disable_layer(osd_device,
  1029. layer->layer_info.id);
  1030. /* Decrement layer usrs counter */
  1031. layer->usrs--;
  1032. /* If this file handle has initialize encoder device, reset it */
  1033. if (!layer->usrs) {
  1034. if (cfg->pixfmt == PIXFMT_NV12) {
  1035. struct vpbe_layer *otherlayer;
  1036. otherlayer =
  1037. _vpbe_display_get_other_win_layer(disp_dev, layer);
  1038. osd_device->ops.disable_layer(osd_device,
  1039. otherlayer->layer_info.id);
  1040. osd_device->ops.release_layer(osd_device,
  1041. otherlayer->layer_info.id);
  1042. }
  1043. osd_device->ops.disable_layer(osd_device,
  1044. layer->layer_info.id);
  1045. osd_device->ops.release_layer(osd_device,
  1046. layer->layer_info.id);
  1047. }
  1048. _vb2_fop_release(file, NULL);
  1049. mutex_unlock(&layer->opslock);
  1050. disp_dev->cbcr_ofst = 0;
  1051. return 0;
  1052. }
  1053. /* vpbe capture ioctl operations */
  1054. static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
  1055. .vidioc_querycap = vpbe_display_querycap,
  1056. .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
  1057. .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
  1058. .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
  1059. .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
  1060. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1061. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1062. .vidioc_querybuf = vb2_ioctl_querybuf,
  1063. .vidioc_qbuf = vb2_ioctl_qbuf,
  1064. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1065. .vidioc_streamon = vb2_ioctl_streamon,
  1066. .vidioc_streamoff = vb2_ioctl_streamoff,
  1067. .vidioc_expbuf = vb2_ioctl_expbuf,
  1068. .vidioc_cropcap = vpbe_display_cropcap,
  1069. .vidioc_g_crop = vpbe_display_g_crop,
  1070. .vidioc_s_crop = vpbe_display_s_crop,
  1071. .vidioc_s_std = vpbe_display_s_std,
  1072. .vidioc_g_std = vpbe_display_g_std,
  1073. .vidioc_enum_output = vpbe_display_enum_output,
  1074. .vidioc_s_output = vpbe_display_s_output,
  1075. .vidioc_g_output = vpbe_display_g_output,
  1076. .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
  1077. .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
  1078. .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
  1079. };
  1080. static struct v4l2_file_operations vpbe_fops = {
  1081. .owner = THIS_MODULE,
  1082. .open = vpbe_display_open,
  1083. .release = vpbe_display_release,
  1084. .unlocked_ioctl = video_ioctl2,
  1085. .mmap = vb2_fop_mmap,
  1086. .poll = vb2_fop_poll,
  1087. };
  1088. static int vpbe_device_get(struct device *dev, void *data)
  1089. {
  1090. struct platform_device *pdev = to_platform_device(dev);
  1091. struct vpbe_display *vpbe_disp = data;
  1092. if (strcmp("vpbe_controller", pdev->name) == 0)
  1093. vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
  1094. if (strstr(pdev->name, "vpbe-osd") != NULL)
  1095. vpbe_disp->osd_device = platform_get_drvdata(pdev);
  1096. return 0;
  1097. }
  1098. static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
  1099. struct platform_device *pdev)
  1100. {
  1101. struct vpbe_layer *vpbe_display_layer = NULL;
  1102. struct video_device *vbd = NULL;
  1103. /* Allocate memory for four plane display objects */
  1104. disp_dev->dev[i] =
  1105. kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
  1106. /* If memory allocation fails, return error */
  1107. if (!disp_dev->dev[i]) {
  1108. printk(KERN_ERR "ran out of memory\n");
  1109. return -ENOMEM;
  1110. }
  1111. spin_lock_init(&disp_dev->dev[i]->irqlock);
  1112. mutex_init(&disp_dev->dev[i]->opslock);
  1113. /* Get the pointer to the layer object */
  1114. vpbe_display_layer = disp_dev->dev[i];
  1115. vbd = &vpbe_display_layer->video_dev;
  1116. /* Initialize field of video device */
  1117. vbd->release = video_device_release_empty;
  1118. vbd->fops = &vpbe_fops;
  1119. vbd->ioctl_ops = &vpbe_ioctl_ops;
  1120. vbd->minor = -1;
  1121. vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1122. vbd->lock = &vpbe_display_layer->opslock;
  1123. vbd->vfl_dir = VFL_DIR_TX;
  1124. if (disp_dev->vpbe_dev->current_timings.timings_type &
  1125. VPBE_ENC_STD)
  1126. vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
  1127. snprintf(vbd->name, sizeof(vbd->name),
  1128. "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
  1129. (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1130. (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1131. (VPBE_DISPLAY_VERSION_CODE) & 0xff);
  1132. vpbe_display_layer->device_id = i;
  1133. vpbe_display_layer->layer_info.id =
  1134. ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
  1135. return 0;
  1136. }
  1137. static int register_device(struct vpbe_layer *vpbe_display_layer,
  1138. struct vpbe_display *disp_dev,
  1139. struct platform_device *pdev)
  1140. {
  1141. int err;
  1142. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1143. "Trying to register VPBE display device.\n");
  1144. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1145. "layer=%x,layer->video_dev=%x\n",
  1146. (int)vpbe_display_layer,
  1147. (int)&vpbe_display_layer->video_dev);
  1148. vpbe_display_layer->video_dev.queue = &vpbe_display_layer->buffer_queue;
  1149. err = video_register_device(&vpbe_display_layer->video_dev,
  1150. VFL_TYPE_GRABBER,
  1151. -1);
  1152. if (err)
  1153. return -ENODEV;
  1154. vpbe_display_layer->disp_dev = disp_dev;
  1155. /* set the driver data in platform device */
  1156. platform_set_drvdata(pdev, disp_dev);
  1157. video_set_drvdata(&vpbe_display_layer->video_dev,
  1158. vpbe_display_layer);
  1159. return 0;
  1160. }
  1161. /*
  1162. * vpbe_display_probe()
  1163. * This function creates device entries by register itself to the V4L2 driver
  1164. * and initializes fields of each layer objects
  1165. */
  1166. static int vpbe_display_probe(struct platform_device *pdev)
  1167. {
  1168. struct vpbe_display *disp_dev;
  1169. struct v4l2_device *v4l2_dev;
  1170. struct resource *res = NULL;
  1171. struct vb2_queue *q;
  1172. int k;
  1173. int i;
  1174. int err;
  1175. int irq;
  1176. printk(KERN_DEBUG "vpbe_display_probe\n");
  1177. /* Allocate memory for vpbe_display */
  1178. disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
  1179. GFP_KERNEL);
  1180. if (!disp_dev)
  1181. return -ENOMEM;
  1182. spin_lock_init(&disp_dev->dma_queue_lock);
  1183. /*
  1184. * Scan all the platform devices to find the vpbe
  1185. * controller device and get the vpbe_dev object
  1186. */
  1187. err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
  1188. vpbe_device_get);
  1189. if (err < 0)
  1190. return err;
  1191. v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1192. /* Initialize the vpbe display controller */
  1193. if (NULL != disp_dev->vpbe_dev->ops.initialize) {
  1194. err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
  1195. disp_dev->vpbe_dev);
  1196. if (err) {
  1197. v4l2_err(v4l2_dev, "Error initing vpbe\n");
  1198. err = -ENOMEM;
  1199. goto probe_out;
  1200. }
  1201. }
  1202. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1203. if (init_vpbe_layer(i, disp_dev, pdev)) {
  1204. err = -ENODEV;
  1205. goto probe_out;
  1206. }
  1207. }
  1208. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1209. if (!res) {
  1210. v4l2_err(v4l2_dev, "Unable to get VENC interrupt resource\n");
  1211. err = -ENODEV;
  1212. goto probe_out;
  1213. }
  1214. irq = res->start;
  1215. err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
  1216. VPBE_DISPLAY_DRIVER, disp_dev);
  1217. if (err) {
  1218. v4l2_err(v4l2_dev, "VPBE IRQ request failed\n");
  1219. goto probe_out;
  1220. }
  1221. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1222. /* initialize vb2 queue */
  1223. q = &disp_dev->dev[i]->buffer_queue;
  1224. memset(q, 0, sizeof(*q));
  1225. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1226. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1227. q->drv_priv = disp_dev->dev[i];
  1228. q->ops = &video_qops;
  1229. q->mem_ops = &vb2_dma_contig_memops;
  1230. q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
  1231. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1232. q->min_buffers_needed = 1;
  1233. q->lock = &disp_dev->dev[i]->opslock;
  1234. err = vb2_queue_init(q);
  1235. if (err) {
  1236. v4l2_err(v4l2_dev, "vb2_queue_init() failed\n");
  1237. goto probe_out;
  1238. }
  1239. disp_dev->dev[i]->alloc_ctx =
  1240. vb2_dma_contig_init_ctx(disp_dev->vpbe_dev->pdev);
  1241. if (IS_ERR(disp_dev->dev[i]->alloc_ctx)) {
  1242. v4l2_err(v4l2_dev, "Failed to get the context\n");
  1243. err = PTR_ERR(disp_dev->dev[i]->alloc_ctx);
  1244. goto probe_out;
  1245. }
  1246. INIT_LIST_HEAD(&disp_dev->dev[i]->dma_queue);
  1247. if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
  1248. err = -ENODEV;
  1249. goto probe_out;
  1250. }
  1251. }
  1252. v4l2_dbg(1, debug, v4l2_dev,
  1253. "Successfully completed the probing of vpbe v4l2 device\n");
  1254. return 0;
  1255. probe_out:
  1256. for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
  1257. /* Unregister video device */
  1258. if (disp_dev->dev[k] != NULL) {
  1259. vb2_dma_contig_cleanup_ctx(disp_dev->dev[k]->alloc_ctx);
  1260. video_unregister_device(&disp_dev->dev[k]->video_dev);
  1261. kfree(disp_dev->dev[k]);
  1262. }
  1263. }
  1264. return err;
  1265. }
  1266. /*
  1267. * vpbe_display_remove()
  1268. * It un-register hardware layer from V4L2 driver
  1269. */
  1270. static int vpbe_display_remove(struct platform_device *pdev)
  1271. {
  1272. struct vpbe_layer *vpbe_display_layer;
  1273. struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
  1274. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1275. int i;
  1276. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
  1277. /* deinitialize the vpbe display controller */
  1278. if (NULL != vpbe_dev->ops.deinitialize)
  1279. vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
  1280. /* un-register device */
  1281. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1282. /* Get the pointer to the layer object */
  1283. vpbe_display_layer = disp_dev->dev[i];
  1284. vb2_dma_contig_cleanup_ctx(vpbe_display_layer->alloc_ctx);
  1285. /* Unregister video device */
  1286. video_unregister_device(&vpbe_display_layer->video_dev);
  1287. }
  1288. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1289. kfree(disp_dev->dev[i]);
  1290. disp_dev->dev[i] = NULL;
  1291. }
  1292. return 0;
  1293. }
  1294. static struct platform_driver vpbe_display_driver = {
  1295. .driver = {
  1296. .name = VPBE_DISPLAY_DRIVER,
  1297. .bus = &platform_bus_type,
  1298. },
  1299. .probe = vpbe_display_probe,
  1300. .remove = vpbe_display_remove,
  1301. };
  1302. module_platform_driver(vpbe_display_driver);
  1303. MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
  1304. MODULE_LICENSE("GPL");
  1305. MODULE_AUTHOR("Texas Instruments");