vpbe_display.c 41 KB

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