vpbe_display.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  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 streaming is started in this layer */
  138. if (!layer->started)
  139. continue;
  140. if (layer->layer_first_int) {
  141. layer->layer_first_int = 0;
  142. continue;
  143. }
  144. /* Check the field format */
  145. if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
  146. (event & VENC_END_OF_FRAME)) {
  147. /* Progressive mode */
  148. vpbe_isr_even_field(disp_dev, layer);
  149. vpbe_isr_odd_field(disp_dev, layer);
  150. } else {
  151. /* Interlaced mode */
  152. layer->field_id ^= 1;
  153. if (event & VENC_FIRST_FIELD)
  154. fid = 0;
  155. else
  156. fid = 1;
  157. /*
  158. * If field id does not match with store
  159. * field id
  160. */
  161. if (fid != layer->field_id) {
  162. /* Make them in sync */
  163. layer->field_id = fid;
  164. continue;
  165. }
  166. /*
  167. * device field id and local field id are
  168. * in sync. If this is even field
  169. */
  170. if (0 == fid)
  171. vpbe_isr_even_field(disp_dev, layer);
  172. else /* odd field */
  173. vpbe_isr_odd_field(disp_dev, layer);
  174. }
  175. }
  176. return IRQ_HANDLED;
  177. }
  178. /*
  179. * vpbe_buffer_prepare()
  180. * This is the callback function called from vb2_qbuf() function
  181. * the buffer is prepared and user space virtual address is converted into
  182. * physical address
  183. */
  184. static int vpbe_buffer_prepare(struct vb2_buffer *vb)
  185. {
  186. struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  187. struct vb2_queue *q = vb->vb2_queue;
  188. struct vpbe_layer *layer = fh->layer;
  189. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  190. unsigned long addr;
  191. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  192. "vpbe_buffer_prepare\n");
  193. if (vb->state != VB2_BUF_STATE_ACTIVE &&
  194. vb->state != VB2_BUF_STATE_PREPARED) {
  195. vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
  196. if (vb2_plane_vaddr(vb, 0) &&
  197. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  198. return -EINVAL;
  199. addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  200. if (q->streaming) {
  201. if (!IS_ALIGNED(addr, 8)) {
  202. v4l2_err(&vpbe_dev->v4l2_dev,
  203. "buffer_prepare:offset is \
  204. not aligned to 32 bytes\n");
  205. return -EINVAL;
  206. }
  207. }
  208. }
  209. return 0;
  210. }
  211. /*
  212. * vpbe_buffer_setup()
  213. * This function allocates memory for the buffers
  214. */
  215. static int
  216. vpbe_buffer_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  217. unsigned int *nbuffers, unsigned int *nplanes,
  218. unsigned int sizes[], void *alloc_ctxs[])
  219. {
  220. /* Get the file handle object and layer object */
  221. struct vpbe_fh *fh = vb2_get_drv_priv(vq);
  222. struct vpbe_layer *layer = fh->layer;
  223. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  224. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
  225. /* Store number of buffers allocated in numbuffer member */
  226. if (*nbuffers < VPBE_DEFAULT_NUM_BUFS)
  227. *nbuffers = layer->numbuffers = VPBE_DEFAULT_NUM_BUFS;
  228. *nplanes = 1;
  229. sizes[0] = layer->pix_fmt.sizeimage;
  230. alloc_ctxs[0] = layer->alloc_ctx;
  231. return 0;
  232. }
  233. /*
  234. * vpbe_buffer_queue()
  235. * This function adds the buffer to DMA queue
  236. */
  237. static void vpbe_buffer_queue(struct vb2_buffer *vb)
  238. {
  239. /* Get the file handle object and layer object */
  240. struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  241. struct vpbe_disp_buffer *buf = container_of(vb,
  242. struct vpbe_disp_buffer, vb);
  243. struct vpbe_layer *layer = fh->layer;
  244. struct vpbe_display *disp = fh->disp_dev;
  245. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  246. unsigned long flags;
  247. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  248. "vpbe_buffer_queue\n");
  249. /* add the buffer to the DMA queue */
  250. spin_lock_irqsave(&disp->dma_queue_lock, flags);
  251. list_add_tail(&buf->list, &layer->dma_queue);
  252. spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
  253. }
  254. /*
  255. * vpbe_buf_cleanup()
  256. * This function is called from the vb2 layer to free memory allocated to
  257. * the buffers
  258. */
  259. static void vpbe_buf_cleanup(struct vb2_buffer *vb)
  260. {
  261. /* Get the file handle object and layer object */
  262. struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  263. struct vpbe_layer *layer = fh->layer;
  264. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  265. struct vpbe_disp_buffer *buf = container_of(vb,
  266. struct vpbe_disp_buffer, vb);
  267. unsigned long flags;
  268. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  269. "vpbe_buf_cleanup\n");
  270. spin_lock_irqsave(&layer->irqlock, flags);
  271. if (vb->state == VB2_BUF_STATE_ACTIVE)
  272. list_del_init(&buf->list);
  273. spin_unlock_irqrestore(&layer->irqlock, flags);
  274. }
  275. static void vpbe_wait_prepare(struct vb2_queue *vq)
  276. {
  277. struct vpbe_fh *fh = vb2_get_drv_priv(vq);
  278. struct vpbe_layer *layer = fh->layer;
  279. mutex_unlock(&layer->opslock);
  280. }
  281. static void vpbe_wait_finish(struct vb2_queue *vq)
  282. {
  283. struct vpbe_fh *fh = vb2_get_drv_priv(vq);
  284. struct vpbe_layer *layer = fh->layer;
  285. mutex_lock(&layer->opslock);
  286. }
  287. static int vpbe_buffer_init(struct vb2_buffer *vb)
  288. {
  289. struct vpbe_disp_buffer *buf = container_of(vb,
  290. struct vpbe_disp_buffer, vb);
  291. INIT_LIST_HEAD(&buf->list);
  292. return 0;
  293. }
  294. static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
  295. {
  296. struct vpbe_fh *fh = vb2_get_drv_priv(vq);
  297. struct vpbe_layer *layer = fh->layer;
  298. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  299. int ret;
  300. /* If buffer queue is empty, return error */
  301. if (list_empty(&layer->dma_queue)) {
  302. v4l2_err(&vpbe_dev->v4l2_dev, "buffer queue is empty\n");
  303. return -EINVAL;
  304. }
  305. /* Get the next frame from the buffer queue */
  306. layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
  307. struct vpbe_disp_buffer, list);
  308. /* Remove buffer from the buffer queue */
  309. list_del(&layer->cur_frm->list);
  310. /* Mark state of the current frame to active */
  311. layer->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  312. /* Initialize field_id and started member */
  313. layer->field_id = 0;
  314. /* Set parameters in OSD and VENC */
  315. ret = vpbe_set_osd_display_params(fh->disp_dev, layer);
  316. if (ret < 0)
  317. return ret;
  318. /*
  319. * if request format is yuv420 semiplanar, need to
  320. * enable both video windows
  321. */
  322. layer->started = 1;
  323. layer->layer_first_int = 1;
  324. return ret;
  325. }
  326. static int vpbe_stop_streaming(struct vb2_queue *vq)
  327. {
  328. struct vpbe_fh *fh = vb2_get_drv_priv(vq);
  329. struct vpbe_layer *layer = fh->layer;
  330. if (!vb2_is_streaming(vq))
  331. return 0;
  332. /* release all active buffers */
  333. while (!list_empty(&layer->dma_queue)) {
  334. layer->next_frm = list_entry(layer->dma_queue.next,
  335. struct vpbe_disp_buffer, list);
  336. list_del(&layer->next_frm->list);
  337. vb2_buffer_done(&layer->next_frm->vb, VB2_BUF_STATE_ERROR);
  338. }
  339. return 0;
  340. }
  341. static struct vb2_ops video_qops = {
  342. .queue_setup = vpbe_buffer_queue_setup,
  343. .wait_prepare = vpbe_wait_prepare,
  344. .wait_finish = vpbe_wait_finish,
  345. .buf_init = vpbe_buffer_init,
  346. .buf_prepare = vpbe_buffer_prepare,
  347. .start_streaming = vpbe_start_streaming,
  348. .stop_streaming = vpbe_stop_streaming,
  349. .buf_cleanup = vpbe_buf_cleanup,
  350. .buf_queue = vpbe_buffer_queue,
  351. };
  352. static
  353. struct vpbe_layer*
  354. _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
  355. struct vpbe_layer *layer)
  356. {
  357. enum vpbe_display_device_id thiswin, otherwin;
  358. thiswin = layer->device_id;
  359. otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
  360. VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
  361. return disp_dev->dev[otherwin];
  362. }
  363. static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
  364. struct vpbe_layer *layer)
  365. {
  366. struct osd_layer_config *cfg = &layer->layer_info.config;
  367. struct osd_state *osd_device = disp_dev->osd_device;
  368. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  369. unsigned long addr;
  370. int ret;
  371. addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb, 0);
  372. /* Set address in the display registers */
  373. osd_device->ops.start_layer(osd_device,
  374. layer->layer_info.id,
  375. addr,
  376. disp_dev->cbcr_ofst);
  377. ret = osd_device->ops.enable_layer(osd_device,
  378. layer->layer_info.id, 0);
  379. if (ret < 0) {
  380. v4l2_err(&vpbe_dev->v4l2_dev,
  381. "Error in enabling osd window layer 0\n");
  382. return -1;
  383. }
  384. /* Enable the window */
  385. layer->layer_info.enable = 1;
  386. if (cfg->pixfmt == PIXFMT_NV12) {
  387. struct vpbe_layer *otherlayer =
  388. _vpbe_display_get_other_win_layer(disp_dev, layer);
  389. ret = osd_device->ops.enable_layer(osd_device,
  390. otherlayer->layer_info.id, 1);
  391. if (ret < 0) {
  392. v4l2_err(&vpbe_dev->v4l2_dev,
  393. "Error in enabling osd window layer 1\n");
  394. return -1;
  395. }
  396. otherlayer->layer_info.enable = 1;
  397. }
  398. return 0;
  399. }
  400. static void
  401. vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
  402. struct vpbe_layer *layer,
  403. int expected_xsize, int expected_ysize)
  404. {
  405. struct display_layer_info *layer_info = &layer->layer_info;
  406. struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
  407. struct osd_layer_config *cfg = &layer->layer_info.config;
  408. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  409. int calculated_xsize;
  410. int h_exp = 0;
  411. int v_exp = 0;
  412. int h_scale;
  413. int v_scale;
  414. v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
  415. /*
  416. * Application initially set the image format. Current display
  417. * size is obtained from the vpbe display controller. expected_xsize
  418. * and expected_ysize are set through S_CROP ioctl. Based on this,
  419. * driver will calculate the scale factors for vertical and
  420. * horizontal direction so that the image is displayed scaled
  421. * and expanded. Application uses expansion to display the image
  422. * in a square pixel. Otherwise it is displayed using displays
  423. * pixel aspect ratio.It is expected that application chooses
  424. * the crop coordinates for cropped or scaled display. if crop
  425. * size is less than the image size, it is displayed cropped or
  426. * it is displayed scaled and/or expanded.
  427. *
  428. * to begin with, set the crop window same as expected. Later we
  429. * will override with scaled window size
  430. */
  431. cfg->xsize = pixfmt->width;
  432. cfg->ysize = pixfmt->height;
  433. layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
  434. layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
  435. layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
  436. layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
  437. if (pixfmt->width < expected_xsize) {
  438. h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
  439. if (h_scale < 2)
  440. h_scale = 1;
  441. else if (h_scale >= 4)
  442. h_scale = 4;
  443. else
  444. h_scale = 2;
  445. cfg->xsize *= h_scale;
  446. if (cfg->xsize < expected_xsize) {
  447. if ((standard_id & V4L2_STD_525_60) ||
  448. (standard_id & V4L2_STD_625_50)) {
  449. calculated_xsize = (cfg->xsize *
  450. VPBE_DISPLAY_H_EXP_RATIO_N) /
  451. VPBE_DISPLAY_H_EXP_RATIO_D;
  452. if (calculated_xsize <= expected_xsize) {
  453. h_exp = 1;
  454. cfg->xsize = calculated_xsize;
  455. }
  456. }
  457. }
  458. if (h_scale == 2)
  459. layer_info->h_zoom = ZOOM_X2;
  460. else if (h_scale == 4)
  461. layer_info->h_zoom = ZOOM_X4;
  462. if (h_exp)
  463. layer_info->h_exp = H_EXP_9_OVER_8;
  464. } else {
  465. /* no scaling, only cropping. Set display area to crop area */
  466. cfg->xsize = expected_xsize;
  467. }
  468. if (pixfmt->height < expected_ysize) {
  469. v_scale = expected_ysize / pixfmt->height;
  470. if (v_scale < 2)
  471. v_scale = 1;
  472. else if (v_scale >= 4)
  473. v_scale = 4;
  474. else
  475. v_scale = 2;
  476. cfg->ysize *= v_scale;
  477. if (cfg->ysize < expected_ysize) {
  478. if ((standard_id & V4L2_STD_625_50)) {
  479. calculated_xsize = (cfg->ysize *
  480. VPBE_DISPLAY_V_EXP_RATIO_N) /
  481. VPBE_DISPLAY_V_EXP_RATIO_D;
  482. if (calculated_xsize <= expected_ysize) {
  483. v_exp = 1;
  484. cfg->ysize = calculated_xsize;
  485. }
  486. }
  487. }
  488. if (v_scale == 2)
  489. layer_info->v_zoom = ZOOM_X2;
  490. else if (v_scale == 4)
  491. layer_info->v_zoom = ZOOM_X4;
  492. if (v_exp)
  493. layer_info->h_exp = V_EXP_6_OVER_5;
  494. } else {
  495. /* no scaling, only cropping. Set display area to crop area */
  496. cfg->ysize = expected_ysize;
  497. }
  498. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  499. "crop display xsize = %d, ysize = %d\n",
  500. cfg->xsize, cfg->ysize);
  501. }
  502. static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
  503. struct vpbe_layer *layer,
  504. int top, int left)
  505. {
  506. struct osd_layer_config *cfg = &layer->layer_info.config;
  507. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  508. cfg->xpos = min((unsigned int)left,
  509. vpbe_dev->current_timings.xres - cfg->xsize);
  510. cfg->ypos = min((unsigned int)top,
  511. vpbe_dev->current_timings.yres - cfg->ysize);
  512. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  513. "new xpos = %d, ypos = %d\n",
  514. cfg->xpos, cfg->ypos);
  515. }
  516. static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
  517. struct v4l2_rect *c)
  518. {
  519. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  520. if ((c->width == 0) ||
  521. ((c->width + c->left) > vpbe_dev->current_timings.xres))
  522. c->width = vpbe_dev->current_timings.xres - c->left;
  523. if ((c->height == 0) || ((c->height + c->top) >
  524. vpbe_dev->current_timings.yres))
  525. c->height = vpbe_dev->current_timings.yres - c->top;
  526. /* window height must be even for interlaced display */
  527. if (vpbe_dev->current_timings.interlaced)
  528. c->height &= (~0x01);
  529. }
  530. /**
  531. * vpbe_try_format()
  532. * If user application provides width and height, and have bytesperline set
  533. * to zero, driver calculates bytesperline and sizeimage based on hardware
  534. * limits.
  535. */
  536. static int vpbe_try_format(struct vpbe_display *disp_dev,
  537. struct v4l2_pix_format *pixfmt, int check)
  538. {
  539. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  540. int min_height = 1;
  541. int min_width = 32;
  542. int max_height;
  543. int max_width;
  544. int bpp;
  545. if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
  546. (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
  547. /* choose default as V4L2_PIX_FMT_UYVY */
  548. pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
  549. /* Check the field format */
  550. if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
  551. (pixfmt->field != V4L2_FIELD_NONE)) {
  552. if (vpbe_dev->current_timings.interlaced)
  553. pixfmt->field = V4L2_FIELD_INTERLACED;
  554. else
  555. pixfmt->field = V4L2_FIELD_NONE;
  556. }
  557. if (pixfmt->field == V4L2_FIELD_INTERLACED)
  558. min_height = 2;
  559. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  560. bpp = 1;
  561. else
  562. bpp = 2;
  563. max_width = vpbe_dev->current_timings.xres;
  564. max_height = vpbe_dev->current_timings.yres;
  565. min_width /= bpp;
  566. if (!pixfmt->width || (pixfmt->width < min_width) ||
  567. (pixfmt->width > max_width)) {
  568. pixfmt->width = vpbe_dev->current_timings.xres;
  569. }
  570. if (!pixfmt->height || (pixfmt->height < min_height) ||
  571. (pixfmt->height > max_height)) {
  572. pixfmt->height = vpbe_dev->current_timings.yres;
  573. }
  574. if (pixfmt->bytesperline < (pixfmt->width * bpp))
  575. pixfmt->bytesperline = pixfmt->width * bpp;
  576. /* Make the bytesperline 32 byte aligned */
  577. pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
  578. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  579. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
  580. (pixfmt->bytesperline * pixfmt->height >> 1);
  581. else
  582. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  583. return 0;
  584. }
  585. static int vpbe_display_g_priority(struct file *file, void *priv,
  586. enum v4l2_priority *p)
  587. {
  588. struct vpbe_fh *fh = file->private_data;
  589. struct vpbe_layer *layer = fh->layer;
  590. *p = v4l2_prio_max(&layer->prio);
  591. return 0;
  592. }
  593. static int vpbe_display_s_priority(struct file *file, void *priv,
  594. enum v4l2_priority p)
  595. {
  596. struct vpbe_fh *fh = file->private_data;
  597. struct vpbe_layer *layer = fh->layer;
  598. int ret;
  599. ret = v4l2_prio_change(&layer->prio, &fh->prio, p);
  600. return ret;
  601. }
  602. static int vpbe_display_querycap(struct file *file, void *priv,
  603. struct v4l2_capability *cap)
  604. {
  605. struct vpbe_fh *fh = file->private_data;
  606. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  607. cap->version = VPBE_DISPLAY_VERSION_CODE;
  608. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  609. strlcpy(cap->driver, VPBE_DISPLAY_DRIVER, sizeof(cap->driver));
  610. strlcpy(cap->bus_info, "platform", sizeof(cap->bus_info));
  611. strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
  612. return 0;
  613. }
  614. static int vpbe_display_s_crop(struct file *file, void *priv,
  615. const struct v4l2_crop *crop)
  616. {
  617. struct vpbe_fh *fh = file->private_data;
  618. struct vpbe_layer *layer = fh->layer;
  619. struct vpbe_display *disp_dev = fh->disp_dev;
  620. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  621. struct osd_layer_config *cfg = &layer->layer_info.config;
  622. struct osd_state *osd_device = disp_dev->osd_device;
  623. struct v4l2_rect rect = crop->c;
  624. int ret;
  625. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  626. "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
  627. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  628. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
  629. return -EINVAL;
  630. }
  631. if (rect.top < 0)
  632. rect.top = 0;
  633. if (rect.left < 0)
  634. rect.left = 0;
  635. vpbe_disp_check_window_params(disp_dev, &rect);
  636. osd_device->ops.get_layer_config(osd_device,
  637. layer->layer_info.id, cfg);
  638. vpbe_disp_calculate_scale_factor(disp_dev, layer,
  639. rect.width,
  640. rect.height);
  641. vpbe_disp_adj_position(disp_dev, layer, rect.top,
  642. rect.left);
  643. ret = osd_device->ops.set_layer_config(osd_device,
  644. layer->layer_info.id, cfg);
  645. if (ret < 0) {
  646. v4l2_err(&vpbe_dev->v4l2_dev,
  647. "Error in set layer config:\n");
  648. return -EINVAL;
  649. }
  650. /* apply zooming and h or v expansion */
  651. osd_device->ops.set_zoom(osd_device,
  652. layer->layer_info.id,
  653. layer->layer_info.h_zoom,
  654. layer->layer_info.v_zoom);
  655. ret = osd_device->ops.set_vid_expansion(osd_device,
  656. layer->layer_info.h_exp,
  657. layer->layer_info.v_exp);
  658. if (ret < 0) {
  659. v4l2_err(&vpbe_dev->v4l2_dev,
  660. "Error in set vid expansion:\n");
  661. return -EINVAL;
  662. }
  663. if ((layer->layer_info.h_zoom != ZOOM_X1) ||
  664. (layer->layer_info.v_zoom != ZOOM_X1) ||
  665. (layer->layer_info.h_exp != H_EXP_OFF) ||
  666. (layer->layer_info.v_exp != V_EXP_OFF))
  667. /* Enable expansion filter */
  668. osd_device->ops.set_interpolation_filter(osd_device, 1);
  669. else
  670. osd_device->ops.set_interpolation_filter(osd_device, 0);
  671. return 0;
  672. }
  673. static int vpbe_display_g_crop(struct file *file, void *priv,
  674. struct v4l2_crop *crop)
  675. {
  676. struct vpbe_fh *fh = file->private_data;
  677. struct vpbe_layer *layer = fh->layer;
  678. struct osd_layer_config *cfg = &layer->layer_info.config;
  679. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  680. struct osd_state *osd_device = fh->disp_dev->osd_device;
  681. struct v4l2_rect *rect = &crop->c;
  682. int ret;
  683. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  684. "VIDIOC_G_CROP, layer id = %d\n",
  685. layer->device_id);
  686. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  687. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
  688. ret = -EINVAL;
  689. }
  690. osd_device->ops.get_layer_config(osd_device,
  691. layer->layer_info.id, cfg);
  692. rect->top = cfg->ypos;
  693. rect->left = cfg->xpos;
  694. rect->width = cfg->xsize;
  695. rect->height = cfg->ysize;
  696. return 0;
  697. }
  698. static int vpbe_display_cropcap(struct file *file, void *priv,
  699. struct v4l2_cropcap *cropcap)
  700. {
  701. struct vpbe_fh *fh = file->private_data;
  702. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  703. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
  704. cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  705. cropcap->bounds.left = 0;
  706. cropcap->bounds.top = 0;
  707. cropcap->bounds.width = vpbe_dev->current_timings.xres;
  708. cropcap->bounds.height = vpbe_dev->current_timings.yres;
  709. cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
  710. cropcap->defrect = cropcap->bounds;
  711. return 0;
  712. }
  713. static int vpbe_display_g_fmt(struct file *file, void *priv,
  714. struct v4l2_format *fmt)
  715. {
  716. struct vpbe_fh *fh = file->private_data;
  717. struct vpbe_layer *layer = fh->layer;
  718. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  719. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  720. "VIDIOC_G_FMT, layer id = %d\n",
  721. layer->device_id);
  722. /* If buffer type is video output */
  723. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  724. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  725. return -EINVAL;
  726. }
  727. /* Fill in the information about format */
  728. fmt->fmt.pix = layer->pix_fmt;
  729. return 0;
  730. }
  731. static int vpbe_display_enum_fmt(struct file *file, void *priv,
  732. struct v4l2_fmtdesc *fmt)
  733. {
  734. struct vpbe_fh *fh = file->private_data;
  735. struct vpbe_layer *layer = fh->layer;
  736. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  737. unsigned int index = 0;
  738. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  739. "VIDIOC_ENUM_FMT, layer id = %d\n",
  740. layer->device_id);
  741. if (fmt->index > 1) {
  742. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
  743. return -EINVAL;
  744. }
  745. /* Fill in the information about format */
  746. index = fmt->index;
  747. memset(fmt, 0, sizeof(*fmt));
  748. fmt->index = index;
  749. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  750. if (index == 0) {
  751. strcpy(fmt->description, "YUV 4:2:2 - UYVY");
  752. fmt->pixelformat = V4L2_PIX_FMT_UYVY;
  753. } else {
  754. strcpy(fmt->description, "Y/CbCr 4:2:0");
  755. fmt->pixelformat = V4L2_PIX_FMT_NV12;
  756. }
  757. return 0;
  758. }
  759. static int vpbe_display_s_fmt(struct file *file, void *priv,
  760. struct v4l2_format *fmt)
  761. {
  762. struct vpbe_fh *fh = file->private_data;
  763. struct vpbe_layer *layer = fh->layer;
  764. struct vpbe_display *disp_dev = fh->disp_dev;
  765. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  766. struct osd_layer_config *cfg = &layer->layer_info.config;
  767. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  768. struct osd_state *osd_device = disp_dev->osd_device;
  769. int ret;
  770. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  771. "VIDIOC_S_FMT, layer id = %d\n",
  772. layer->device_id);
  773. /* If streaming is started, return error */
  774. if (layer->started) {
  775. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  776. return -EBUSY;
  777. }
  778. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  779. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
  780. return -EINVAL;
  781. }
  782. /* Check for valid pixel format */
  783. ret = vpbe_try_format(disp_dev, pixfmt, 1);
  784. if (ret)
  785. return ret;
  786. /* YUV420 is requested, check availability of the
  787. other video window */
  788. layer->pix_fmt = *pixfmt;
  789. /* Get osd layer config */
  790. osd_device->ops.get_layer_config(osd_device,
  791. layer->layer_info.id, cfg);
  792. /* Store the pixel format in the layer object */
  793. cfg->xsize = pixfmt->width;
  794. cfg->ysize = pixfmt->height;
  795. cfg->line_length = pixfmt->bytesperline;
  796. cfg->ypos = 0;
  797. cfg->xpos = 0;
  798. cfg->interlaced = vpbe_dev->current_timings.interlaced;
  799. if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
  800. cfg->pixfmt = PIXFMT_YCbCrI;
  801. /* Change of the default pixel format for both video windows */
  802. if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
  803. struct vpbe_layer *otherlayer;
  804. cfg->pixfmt = PIXFMT_NV12;
  805. otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
  806. layer);
  807. otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
  808. }
  809. /* Set the layer config in the osd window */
  810. ret = osd_device->ops.set_layer_config(osd_device,
  811. layer->layer_info.id, cfg);
  812. if (ret < 0) {
  813. v4l2_err(&vpbe_dev->v4l2_dev,
  814. "Error in S_FMT params:\n");
  815. return -EINVAL;
  816. }
  817. /* Readback and fill the local copy of current pix format */
  818. osd_device->ops.get_layer_config(osd_device,
  819. layer->layer_info.id, cfg);
  820. return 0;
  821. }
  822. static int vpbe_display_try_fmt(struct file *file, void *priv,
  823. struct v4l2_format *fmt)
  824. {
  825. struct vpbe_fh *fh = file->private_data;
  826. struct vpbe_display *disp_dev = fh->disp_dev;
  827. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  828. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  829. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
  830. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  831. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  832. return -EINVAL;
  833. }
  834. /* Check for valid field format */
  835. return vpbe_try_format(disp_dev, pixfmt, 0);
  836. }
  837. /**
  838. * vpbe_display_s_std - Set the given standard in the encoder
  839. *
  840. * Sets the standard if supported by the current encoder. Return the status.
  841. * 0 - success & -EINVAL on error
  842. */
  843. static int vpbe_display_s_std(struct file *file, void *priv,
  844. v4l2_std_id *std_id)
  845. {
  846. struct vpbe_fh *fh = priv;
  847. struct vpbe_layer *layer = fh->layer;
  848. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  849. int ret;
  850. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
  851. /* If streaming is started, return error */
  852. if (layer->started) {
  853. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  854. return -EBUSY;
  855. }
  856. if (NULL != vpbe_dev->ops.s_std) {
  857. ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
  858. if (ret) {
  859. v4l2_err(&vpbe_dev->v4l2_dev,
  860. "Failed to set standard for sub devices\n");
  861. return -EINVAL;
  862. }
  863. } else {
  864. return -EINVAL;
  865. }
  866. return 0;
  867. }
  868. /**
  869. * vpbe_display_g_std - Get the standard in the current encoder
  870. *
  871. * Get the standard in the current encoder. Return the status. 0 - success
  872. * -EINVAL on error
  873. */
  874. static int vpbe_display_g_std(struct file *file, void *priv,
  875. v4l2_std_id *std_id)
  876. {
  877. struct vpbe_fh *fh = priv;
  878. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  879. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
  880. /* Get the standard from the current encoder */
  881. if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
  882. *std_id = vpbe_dev->current_timings.std_id;
  883. return 0;
  884. }
  885. return -EINVAL;
  886. }
  887. /**
  888. * vpbe_display_enum_output - enumerate outputs
  889. *
  890. * Enumerates the outputs available at the vpbe display
  891. * returns the status, -EINVAL if end of output list
  892. */
  893. static int vpbe_display_enum_output(struct file *file, void *priv,
  894. struct v4l2_output *output)
  895. {
  896. struct vpbe_fh *fh = priv;
  897. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  898. int ret;
  899. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
  900. /* Enumerate outputs */
  901. if (NULL == vpbe_dev->ops.enum_outputs)
  902. return -EINVAL;
  903. ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
  904. if (ret) {
  905. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  906. "Failed to enumerate outputs\n");
  907. return -EINVAL;
  908. }
  909. return 0;
  910. }
  911. /**
  912. * vpbe_display_s_output - Set output to
  913. * the output specified by the index
  914. */
  915. static int vpbe_display_s_output(struct file *file, void *priv,
  916. unsigned int i)
  917. {
  918. struct vpbe_fh *fh = priv;
  919. struct vpbe_layer *layer = fh->layer;
  920. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  921. int ret;
  922. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
  923. /* If streaming is started, return error */
  924. if (layer->started) {
  925. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  926. return -EBUSY;
  927. }
  928. if (NULL == vpbe_dev->ops.set_output)
  929. return -EINVAL;
  930. ret = vpbe_dev->ops.set_output(vpbe_dev, i);
  931. if (ret) {
  932. v4l2_err(&vpbe_dev->v4l2_dev,
  933. "Failed to set output for sub devices\n");
  934. return -EINVAL;
  935. }
  936. return 0;
  937. }
  938. /**
  939. * vpbe_display_g_output - Get output from subdevice
  940. * for a given by the index
  941. */
  942. static int vpbe_display_g_output(struct file *file, void *priv,
  943. unsigned int *i)
  944. {
  945. struct vpbe_fh *fh = priv;
  946. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  947. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
  948. /* Get the standard from the current encoder */
  949. *i = vpbe_dev->current_out_index;
  950. return 0;
  951. }
  952. /**
  953. * vpbe_display_enum_dv_timings - Enumerate the dv timings
  954. *
  955. * enum the timings in the current encoder. Return the status. 0 - success
  956. * -EINVAL on error
  957. */
  958. static int
  959. vpbe_display_enum_dv_timings(struct file *file, void *priv,
  960. struct v4l2_enum_dv_timings *timings)
  961. {
  962. struct vpbe_fh *fh = priv;
  963. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  964. int ret;
  965. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
  966. /* Enumerate outputs */
  967. if (NULL == vpbe_dev->ops.enum_dv_timings)
  968. return -EINVAL;
  969. ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
  970. if (ret) {
  971. v4l2_err(&vpbe_dev->v4l2_dev,
  972. "Failed to enumerate dv timings info\n");
  973. return -EINVAL;
  974. }
  975. return 0;
  976. }
  977. /**
  978. * vpbe_display_s_dv_timings - Set the dv timings
  979. *
  980. * Set the timings in the current encoder. Return the status. 0 - success
  981. * -EINVAL on error
  982. */
  983. static int
  984. vpbe_display_s_dv_timings(struct file *file, void *priv,
  985. struct v4l2_dv_timings *timings)
  986. {
  987. struct vpbe_fh *fh = priv;
  988. struct vpbe_layer *layer = fh->layer;
  989. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  990. int ret;
  991. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
  992. /* If streaming is started, return error */
  993. if (layer->started) {
  994. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  995. return -EBUSY;
  996. }
  997. /* Set the given standard in the encoder */
  998. if (!vpbe_dev->ops.s_dv_timings)
  999. return -EINVAL;
  1000. ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
  1001. if (ret) {
  1002. v4l2_err(&vpbe_dev->v4l2_dev,
  1003. "Failed to set the dv timings info\n");
  1004. return -EINVAL;
  1005. }
  1006. /* set the current norm to zero to be consistent. If STD is used
  1007. * v4l2 layer will set the norm properly on successful s_std call
  1008. */
  1009. layer->video_dev.current_norm = 0;
  1010. return 0;
  1011. }
  1012. /**
  1013. * vpbe_display_g_dv_timings - Set the dv timings
  1014. *
  1015. * Get the timings in the current encoder. Return the status. 0 - success
  1016. * -EINVAL on error
  1017. */
  1018. static int
  1019. vpbe_display_g_dv_timings(struct file *file, void *priv,
  1020. struct v4l2_dv_timings *dv_timings)
  1021. {
  1022. struct vpbe_fh *fh = priv;
  1023. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1024. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
  1025. /* Get the given standard in the encoder */
  1026. if (vpbe_dev->current_timings.timings_type &
  1027. VPBE_ENC_CUSTOM_TIMINGS) {
  1028. *dv_timings = vpbe_dev->current_timings.dv_timings;
  1029. } else {
  1030. return -EINVAL;
  1031. }
  1032. return 0;
  1033. }
  1034. static int vpbe_display_streamoff(struct file *file, void *priv,
  1035. enum v4l2_buf_type buf_type)
  1036. {
  1037. struct vpbe_fh *fh = file->private_data;
  1038. struct vpbe_layer *layer = fh->layer;
  1039. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1040. struct osd_state *osd_device = fh->disp_dev->osd_device;
  1041. int ret;
  1042. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1043. "VIDIOC_STREAMOFF,layer id = %d\n",
  1044. layer->device_id);
  1045. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
  1046. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1047. return -EINVAL;
  1048. }
  1049. /* If io is allowed for this file handle, return error */
  1050. if (!fh->io_allowed) {
  1051. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1052. return -EACCES;
  1053. }
  1054. /* If streaming is not started, return error */
  1055. if (!layer->started) {
  1056. v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
  1057. " id = %d\n", layer->device_id);
  1058. return -EINVAL;
  1059. }
  1060. osd_device->ops.disable_layer(osd_device,
  1061. layer->layer_info.id);
  1062. layer->started = 0;
  1063. ret = vb2_streamoff(&layer->buffer_queue, buf_type);
  1064. return ret;
  1065. }
  1066. static int vpbe_display_streamon(struct file *file, void *priv,
  1067. enum v4l2_buf_type buf_type)
  1068. {
  1069. struct vpbe_fh *fh = file->private_data;
  1070. struct vpbe_layer *layer = fh->layer;
  1071. struct vpbe_display *disp_dev = fh->disp_dev;
  1072. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1073. struct osd_state *osd_device = disp_dev->osd_device;
  1074. int ret;
  1075. osd_device->ops.disable_layer(osd_device,
  1076. layer->layer_info.id);
  1077. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
  1078. layer->device_id);
  1079. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
  1080. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1081. return -EINVAL;
  1082. }
  1083. /* If file handle is not allowed IO, return error */
  1084. if (!fh->io_allowed) {
  1085. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1086. return -EACCES;
  1087. }
  1088. /* If Streaming is already started, return error */
  1089. if (layer->started) {
  1090. v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
  1091. return -EBUSY;
  1092. }
  1093. /*
  1094. * Call vb2_streamon to start streaming
  1095. * in videobuf
  1096. */
  1097. ret = vb2_streamon(&layer->buffer_queue, buf_type);
  1098. if (ret) {
  1099. v4l2_err(&vpbe_dev->v4l2_dev,
  1100. "error in vb2_streamon\n");
  1101. return ret;
  1102. }
  1103. return ret;
  1104. }
  1105. static int vpbe_display_dqbuf(struct file *file, void *priv,
  1106. struct v4l2_buffer *buf)
  1107. {
  1108. struct vpbe_fh *fh = file->private_data;
  1109. struct vpbe_layer *layer = fh->layer;
  1110. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1111. int ret;
  1112. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1113. "VIDIOC_DQBUF, layer id = %d\n",
  1114. layer->device_id);
  1115. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
  1116. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1117. return -EINVAL;
  1118. }
  1119. /* If this file handle is not allowed to do IO, return error */
  1120. if (!fh->io_allowed) {
  1121. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1122. return -EACCES;
  1123. }
  1124. if (file->f_flags & O_NONBLOCK)
  1125. /* Call videobuf_dqbuf for non blocking mode */
  1126. ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
  1127. else
  1128. /* Call videobuf_dqbuf for blocking mode */
  1129. ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
  1130. return ret;
  1131. }
  1132. static int vpbe_display_qbuf(struct file *file, void *priv,
  1133. struct v4l2_buffer *p)
  1134. {
  1135. struct vpbe_fh *fh = file->private_data;
  1136. struct vpbe_layer *layer = fh->layer;
  1137. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1138. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1139. "VIDIOC_QBUF, layer id = %d\n",
  1140. layer->device_id);
  1141. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
  1142. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1143. return -EINVAL;
  1144. }
  1145. /* If this file handle is not allowed to do IO, return error */
  1146. if (!fh->io_allowed) {
  1147. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1148. return -EACCES;
  1149. }
  1150. return vb2_qbuf(&layer->buffer_queue, p);
  1151. }
  1152. static int vpbe_display_querybuf(struct file *file, void *priv,
  1153. struct v4l2_buffer *buf)
  1154. {
  1155. struct vpbe_fh *fh = file->private_data;
  1156. struct vpbe_layer *layer = fh->layer;
  1157. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1158. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1159. "VIDIOC_QUERYBUF, layer id = %d\n",
  1160. layer->device_id);
  1161. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
  1162. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1163. return -EINVAL;
  1164. }
  1165. /* Call vb2_querybuf to get information */
  1166. return vb2_querybuf(&layer->buffer_queue, buf);
  1167. }
  1168. static int vpbe_display_reqbufs(struct file *file, void *priv,
  1169. struct v4l2_requestbuffers *req_buf)
  1170. {
  1171. struct vpbe_fh *fh = file->private_data;
  1172. struct vpbe_layer *layer = fh->layer;
  1173. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1174. struct vb2_queue *q;
  1175. int ret;
  1176. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
  1177. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
  1178. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1179. return -EINVAL;
  1180. }
  1181. /* If io users of the layer is not zero, return error */
  1182. if (0 != layer->io_usrs) {
  1183. v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
  1184. return -EBUSY;
  1185. }
  1186. /* Initialize videobuf queue as per the buffer type */
  1187. layer->alloc_ctx = vb2_dma_contig_init_ctx(vpbe_dev->pdev);
  1188. if (!layer->alloc_ctx) {
  1189. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to get the context\n");
  1190. return -EINVAL;
  1191. }
  1192. q = &layer->buffer_queue;
  1193. memset(q, 0, sizeof(*q));
  1194. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1195. q->io_modes = VB2_MMAP | VB2_USERPTR;
  1196. q->drv_priv = fh;
  1197. q->ops = &video_qops;
  1198. q->mem_ops = &vb2_dma_contig_memops;
  1199. q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
  1200. ret = vb2_queue_init(q);
  1201. if (ret) {
  1202. v4l2_err(&vpbe_dev->v4l2_dev, "vb2_queue_init() failed\n");
  1203. vb2_dma_contig_cleanup_ctx(layer->alloc_ctx);
  1204. return ret;
  1205. }
  1206. /* Set io allowed member of file handle to TRUE */
  1207. fh->io_allowed = 1;
  1208. /* Increment io usrs member of layer object to 1 */
  1209. layer->io_usrs = 1;
  1210. /* Store type of memory requested in layer object */
  1211. layer->memory = req_buf->memory;
  1212. /* Initialize buffer queue */
  1213. INIT_LIST_HEAD(&layer->dma_queue);
  1214. /* Allocate buffers */
  1215. return vb2_reqbufs(q, req_buf);
  1216. }
  1217. /*
  1218. * vpbe_display_mmap()
  1219. * It is used to map kernel space buffers into user spaces
  1220. */
  1221. static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
  1222. {
  1223. /* Get the layer object and file handle object */
  1224. struct vpbe_fh *fh = filep->private_data;
  1225. struct vpbe_layer *layer = fh->layer;
  1226. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1227. int ret;
  1228. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
  1229. if (mutex_lock_interruptible(&layer->opslock))
  1230. return -ERESTARTSYS;
  1231. ret = vb2_mmap(&layer->buffer_queue, vma);
  1232. mutex_unlock(&layer->opslock);
  1233. return ret;
  1234. }
  1235. /* vpbe_display_poll(): It is used for select/poll system call
  1236. */
  1237. static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
  1238. {
  1239. struct vpbe_fh *fh = filep->private_data;
  1240. struct vpbe_layer *layer = fh->layer;
  1241. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1242. unsigned int err = 0;
  1243. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
  1244. if (layer->started) {
  1245. mutex_lock(&layer->opslock);
  1246. err = vb2_poll(&layer->buffer_queue, filep, wait);
  1247. mutex_unlock(&layer->opslock);
  1248. }
  1249. return err;
  1250. }
  1251. /*
  1252. * vpbe_display_open()
  1253. * It creates object of file handle structure and stores it in private_data
  1254. * member of filepointer
  1255. */
  1256. static int vpbe_display_open(struct file *file)
  1257. {
  1258. struct vpbe_fh *fh = NULL;
  1259. struct vpbe_layer *layer = video_drvdata(file);
  1260. struct vpbe_display *disp_dev = layer->disp_dev;
  1261. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1262. struct osd_state *osd_device = disp_dev->osd_device;
  1263. int err;
  1264. /* Allocate memory for the file handle object */
  1265. fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
  1266. if (fh == NULL) {
  1267. v4l2_err(&vpbe_dev->v4l2_dev,
  1268. "unable to allocate memory for file handle object\n");
  1269. return -ENOMEM;
  1270. }
  1271. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1272. "vpbe display open plane = %d\n",
  1273. layer->device_id);
  1274. /* store pointer to fh in private_data member of filep */
  1275. file->private_data = fh;
  1276. fh->layer = layer;
  1277. fh->disp_dev = disp_dev;
  1278. if (!layer->usrs) {
  1279. if (mutex_lock_interruptible(&layer->opslock))
  1280. return -ERESTARTSYS;
  1281. /* First claim the layer for this device */
  1282. err = osd_device->ops.request_layer(osd_device,
  1283. layer->layer_info.id);
  1284. mutex_unlock(&layer->opslock);
  1285. if (err < 0) {
  1286. /* Couldn't get layer */
  1287. v4l2_err(&vpbe_dev->v4l2_dev,
  1288. "Display Manager failed to allocate layer\n");
  1289. kfree(fh);
  1290. return -EINVAL;
  1291. }
  1292. }
  1293. /* Increment layer usrs counter */
  1294. layer->usrs++;
  1295. /* Set io_allowed member to false */
  1296. fh->io_allowed = 0;
  1297. /* Initialize priority of this instance to default priority */
  1298. fh->prio = V4L2_PRIORITY_UNSET;
  1299. v4l2_prio_open(&layer->prio, &fh->prio);
  1300. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1301. "vpbe display device opened successfully\n");
  1302. return 0;
  1303. }
  1304. /*
  1305. * vpbe_display_release()
  1306. * This function deletes buffer queue, frees the buffers and the davinci
  1307. * display file * handle
  1308. */
  1309. static int vpbe_display_release(struct file *file)
  1310. {
  1311. /* Get the layer object and file handle object */
  1312. struct vpbe_fh *fh = file->private_data;
  1313. struct vpbe_layer *layer = fh->layer;
  1314. struct osd_layer_config *cfg = &layer->layer_info.config;
  1315. struct vpbe_display *disp_dev = fh->disp_dev;
  1316. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1317. struct osd_state *osd_device = disp_dev->osd_device;
  1318. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
  1319. mutex_lock(&layer->opslock);
  1320. /* if this instance is doing IO */
  1321. if (fh->io_allowed) {
  1322. /* Reset io_usrs member of layer object */
  1323. layer->io_usrs = 0;
  1324. osd_device->ops.disable_layer(osd_device,
  1325. layer->layer_info.id);
  1326. layer->started = 0;
  1327. /* Free buffers allocated */
  1328. vb2_queue_release(&layer->buffer_queue);
  1329. vb2_dma_contig_cleanup_ctx(&layer->buffer_queue);
  1330. }
  1331. /* Decrement layer usrs counter */
  1332. layer->usrs--;
  1333. /* If this file handle has initialize encoder device, reset it */
  1334. if (!layer->usrs) {
  1335. if (cfg->pixfmt == PIXFMT_NV12) {
  1336. struct vpbe_layer *otherlayer;
  1337. otherlayer =
  1338. _vpbe_display_get_other_win_layer(disp_dev, layer);
  1339. osd_device->ops.disable_layer(osd_device,
  1340. otherlayer->layer_info.id);
  1341. osd_device->ops.release_layer(osd_device,
  1342. otherlayer->layer_info.id);
  1343. }
  1344. osd_device->ops.disable_layer(osd_device,
  1345. layer->layer_info.id);
  1346. osd_device->ops.release_layer(osd_device,
  1347. layer->layer_info.id);
  1348. }
  1349. /* Close the priority */
  1350. v4l2_prio_close(&layer->prio, fh->prio);
  1351. file->private_data = NULL;
  1352. mutex_unlock(&layer->opslock);
  1353. /* Free memory allocated to file handle object */
  1354. kfree(fh);
  1355. disp_dev->cbcr_ofst = 0;
  1356. return 0;
  1357. }
  1358. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1359. static int vpbe_display_g_register(struct file *file, void *priv,
  1360. struct v4l2_dbg_register *reg)
  1361. {
  1362. struct v4l2_dbg_match *match = &reg->match;
  1363. struct vpbe_fh *fh = file->private_data;
  1364. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1365. if (match->type >= 2) {
  1366. v4l2_subdev_call(vpbe_dev->venc,
  1367. core,
  1368. g_register,
  1369. reg);
  1370. }
  1371. return 0;
  1372. }
  1373. static int vpbe_display_s_register(struct file *file, void *priv,
  1374. struct v4l2_dbg_register *reg)
  1375. {
  1376. return 0;
  1377. }
  1378. #endif
  1379. /* vpbe capture ioctl operations */
  1380. static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
  1381. .vidioc_querycap = vpbe_display_querycap,
  1382. .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
  1383. .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
  1384. .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
  1385. .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
  1386. .vidioc_reqbufs = vpbe_display_reqbufs,
  1387. .vidioc_querybuf = vpbe_display_querybuf,
  1388. .vidioc_qbuf = vpbe_display_qbuf,
  1389. .vidioc_dqbuf = vpbe_display_dqbuf,
  1390. .vidioc_streamon = vpbe_display_streamon,
  1391. .vidioc_streamoff = vpbe_display_streamoff,
  1392. .vidioc_cropcap = vpbe_display_cropcap,
  1393. .vidioc_g_crop = vpbe_display_g_crop,
  1394. .vidioc_s_crop = vpbe_display_s_crop,
  1395. .vidioc_g_priority = vpbe_display_g_priority,
  1396. .vidioc_s_priority = vpbe_display_s_priority,
  1397. .vidioc_s_std = vpbe_display_s_std,
  1398. .vidioc_g_std = vpbe_display_g_std,
  1399. .vidioc_enum_output = vpbe_display_enum_output,
  1400. .vidioc_s_output = vpbe_display_s_output,
  1401. .vidioc_g_output = vpbe_display_g_output,
  1402. .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
  1403. .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
  1404. .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
  1405. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1406. .vidioc_g_register = vpbe_display_g_register,
  1407. .vidioc_s_register = vpbe_display_s_register,
  1408. #endif
  1409. };
  1410. static struct v4l2_file_operations vpbe_fops = {
  1411. .owner = THIS_MODULE,
  1412. .open = vpbe_display_open,
  1413. .release = vpbe_display_release,
  1414. .unlocked_ioctl = video_ioctl2,
  1415. .mmap = vpbe_display_mmap,
  1416. .poll = vpbe_display_poll
  1417. };
  1418. static int vpbe_device_get(struct device *dev, void *data)
  1419. {
  1420. struct platform_device *pdev = to_platform_device(dev);
  1421. struct vpbe_display *vpbe_disp = data;
  1422. if (strcmp("vpbe_controller", pdev->name) == 0)
  1423. vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
  1424. if (strcmp("vpbe-osd", pdev->name) == 0)
  1425. vpbe_disp->osd_device = platform_get_drvdata(pdev);
  1426. return 0;
  1427. }
  1428. static __devinit int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
  1429. struct platform_device *pdev)
  1430. {
  1431. struct vpbe_layer *vpbe_display_layer = NULL;
  1432. struct video_device *vbd = NULL;
  1433. /* Allocate memory for four plane display objects */
  1434. disp_dev->dev[i] =
  1435. kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
  1436. /* If memory allocation fails, return error */
  1437. if (!disp_dev->dev[i]) {
  1438. printk(KERN_ERR "ran out of memory\n");
  1439. return -ENOMEM;
  1440. }
  1441. spin_lock_init(&disp_dev->dev[i]->irqlock);
  1442. mutex_init(&disp_dev->dev[i]->opslock);
  1443. /* Get the pointer to the layer object */
  1444. vpbe_display_layer = disp_dev->dev[i];
  1445. vbd = &vpbe_display_layer->video_dev;
  1446. /* Initialize field of video device */
  1447. vbd->release = video_device_release_empty;
  1448. vbd->fops = &vpbe_fops;
  1449. vbd->ioctl_ops = &vpbe_ioctl_ops;
  1450. vbd->minor = -1;
  1451. vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1452. vbd->lock = &vpbe_display_layer->opslock;
  1453. vbd->vfl_dir = VFL_DIR_TX;
  1454. if (disp_dev->vpbe_dev->current_timings.timings_type &
  1455. VPBE_ENC_STD) {
  1456. vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
  1457. vbd->current_norm =
  1458. disp_dev->vpbe_dev->current_timings.std_id;
  1459. } else
  1460. vbd->current_norm = 0;
  1461. snprintf(vbd->name, sizeof(vbd->name),
  1462. "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
  1463. (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1464. (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1465. (VPBE_DISPLAY_VERSION_CODE) & 0xff);
  1466. vpbe_display_layer->device_id = i;
  1467. vpbe_display_layer->layer_info.id =
  1468. ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
  1469. /* Initialize prio member of layer object */
  1470. v4l2_prio_init(&vpbe_display_layer->prio);
  1471. return 0;
  1472. }
  1473. static __devinit int register_device(struct vpbe_layer *vpbe_display_layer,
  1474. struct vpbe_display *disp_dev,
  1475. struct platform_device *pdev) {
  1476. int err;
  1477. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1478. "Trying to register VPBE display device.\n");
  1479. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1480. "layer=%x,layer->video_dev=%x\n",
  1481. (int)vpbe_display_layer,
  1482. (int)&vpbe_display_layer->video_dev);
  1483. err = video_register_device(&vpbe_display_layer->video_dev,
  1484. VFL_TYPE_GRABBER,
  1485. -1);
  1486. if (err)
  1487. return -ENODEV;
  1488. vpbe_display_layer->disp_dev = disp_dev;
  1489. /* set the driver data in platform device */
  1490. platform_set_drvdata(pdev, disp_dev);
  1491. video_set_drvdata(&vpbe_display_layer->video_dev,
  1492. vpbe_display_layer);
  1493. return 0;
  1494. }
  1495. /*
  1496. * vpbe_display_probe()
  1497. * This function creates device entries by register itself to the V4L2 driver
  1498. * and initializes fields of each layer objects
  1499. */
  1500. static __devinit int vpbe_display_probe(struct platform_device *pdev)
  1501. {
  1502. struct vpbe_layer *vpbe_display_layer;
  1503. struct vpbe_display *disp_dev;
  1504. struct resource *res = NULL;
  1505. int k;
  1506. int i;
  1507. int err;
  1508. int irq;
  1509. printk(KERN_DEBUG "vpbe_display_probe\n");
  1510. /* Allocate memory for vpbe_display */
  1511. disp_dev = kzalloc(sizeof(struct vpbe_display), GFP_KERNEL);
  1512. if (!disp_dev) {
  1513. printk(KERN_ERR "ran out of memory\n");
  1514. return -ENOMEM;
  1515. }
  1516. spin_lock_init(&disp_dev->dma_queue_lock);
  1517. /*
  1518. * Scan all the platform devices to find the vpbe
  1519. * controller device and get the vpbe_dev object
  1520. */
  1521. err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
  1522. vpbe_device_get);
  1523. if (err < 0)
  1524. return err;
  1525. /* Initialize the vpbe display controller */
  1526. if (NULL != disp_dev->vpbe_dev->ops.initialize) {
  1527. err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
  1528. disp_dev->vpbe_dev);
  1529. if (err) {
  1530. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1531. "Error initing vpbe\n");
  1532. err = -ENOMEM;
  1533. goto probe_out;
  1534. }
  1535. }
  1536. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1537. if (init_vpbe_layer(i, disp_dev, pdev)) {
  1538. err = -ENODEV;
  1539. goto probe_out;
  1540. }
  1541. }
  1542. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1543. if (!res) {
  1544. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1545. "Unable to get VENC interrupt resource\n");
  1546. err = -ENODEV;
  1547. goto probe_out;
  1548. }
  1549. irq = res->start;
  1550. if (request_irq(irq, venc_isr, IRQF_DISABLED, VPBE_DISPLAY_DRIVER,
  1551. disp_dev)) {
  1552. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1553. "Unable to request interrupt\n");
  1554. err = -ENODEV;
  1555. goto probe_out;
  1556. }
  1557. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1558. if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
  1559. err = -ENODEV;
  1560. goto probe_out_irq;
  1561. }
  1562. }
  1563. printk(KERN_DEBUG "Successfully completed the probing of vpbe v4l2 device\n");
  1564. return 0;
  1565. probe_out_irq:
  1566. free_irq(res->start, disp_dev);
  1567. probe_out:
  1568. for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
  1569. /* Get the pointer to the layer object */
  1570. vpbe_display_layer = disp_dev->dev[k];
  1571. /* Unregister video device */
  1572. if (vpbe_display_layer) {
  1573. video_unregister_device(
  1574. &vpbe_display_layer->video_dev);
  1575. kfree(disp_dev->dev[k]);
  1576. }
  1577. }
  1578. kfree(disp_dev);
  1579. return err;
  1580. }
  1581. /*
  1582. * vpbe_display_remove()
  1583. * It un-register hardware layer from V4L2 driver
  1584. */
  1585. static int vpbe_display_remove(struct platform_device *pdev)
  1586. {
  1587. struct vpbe_layer *vpbe_display_layer;
  1588. struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
  1589. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1590. struct resource *res;
  1591. int i;
  1592. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
  1593. /* unregister irq */
  1594. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1595. free_irq(res->start, disp_dev);
  1596. /* deinitialize the vpbe display controller */
  1597. if (NULL != vpbe_dev->ops.deinitialize)
  1598. vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
  1599. /* un-register device */
  1600. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1601. /* Get the pointer to the layer object */
  1602. vpbe_display_layer = disp_dev->dev[i];
  1603. /* Unregister video device */
  1604. video_unregister_device(&vpbe_display_layer->video_dev);
  1605. }
  1606. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1607. kfree(disp_dev->dev[i]);
  1608. disp_dev->dev[i] = NULL;
  1609. }
  1610. return 0;
  1611. }
  1612. static struct platform_driver vpbe_display_driver = {
  1613. .driver = {
  1614. .name = VPBE_DISPLAY_DRIVER,
  1615. .owner = THIS_MODULE,
  1616. .bus = &platform_bus_type,
  1617. },
  1618. .probe = vpbe_display_probe,
  1619. .remove = __devexit_p(vpbe_display_remove),
  1620. };
  1621. module_platform_driver(vpbe_display_driver);
  1622. MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
  1623. MODULE_LICENSE("GPL");
  1624. MODULE_AUTHOR("Texas Instruments");