vpbe_display.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  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->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  609. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  610. snprintf(cap->driver, sizeof(cap->driver), "%s",
  611. dev_name(vpbe_dev->pdev));
  612. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  613. dev_name(vpbe_dev->pdev));
  614. strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
  615. return 0;
  616. }
  617. static int vpbe_display_s_crop(struct file *file, void *priv,
  618. const struct v4l2_crop *crop)
  619. {
  620. struct vpbe_fh *fh = file->private_data;
  621. struct vpbe_layer *layer = fh->layer;
  622. struct vpbe_display *disp_dev = fh->disp_dev;
  623. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  624. struct osd_layer_config *cfg = &layer->layer_info.config;
  625. struct osd_state *osd_device = disp_dev->osd_device;
  626. struct v4l2_rect rect = crop->c;
  627. int ret;
  628. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  629. "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
  630. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  631. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
  632. return -EINVAL;
  633. }
  634. if (rect.top < 0)
  635. rect.top = 0;
  636. if (rect.left < 0)
  637. rect.left = 0;
  638. vpbe_disp_check_window_params(disp_dev, &rect);
  639. osd_device->ops.get_layer_config(osd_device,
  640. layer->layer_info.id, cfg);
  641. vpbe_disp_calculate_scale_factor(disp_dev, layer,
  642. rect.width,
  643. rect.height);
  644. vpbe_disp_adj_position(disp_dev, layer, rect.top,
  645. rect.left);
  646. ret = osd_device->ops.set_layer_config(osd_device,
  647. layer->layer_info.id, cfg);
  648. if (ret < 0) {
  649. v4l2_err(&vpbe_dev->v4l2_dev,
  650. "Error in set layer config:\n");
  651. return -EINVAL;
  652. }
  653. /* apply zooming and h or v expansion */
  654. osd_device->ops.set_zoom(osd_device,
  655. layer->layer_info.id,
  656. layer->layer_info.h_zoom,
  657. layer->layer_info.v_zoom);
  658. ret = osd_device->ops.set_vid_expansion(osd_device,
  659. layer->layer_info.h_exp,
  660. layer->layer_info.v_exp);
  661. if (ret < 0) {
  662. v4l2_err(&vpbe_dev->v4l2_dev,
  663. "Error in set vid expansion:\n");
  664. return -EINVAL;
  665. }
  666. if ((layer->layer_info.h_zoom != ZOOM_X1) ||
  667. (layer->layer_info.v_zoom != ZOOM_X1) ||
  668. (layer->layer_info.h_exp != H_EXP_OFF) ||
  669. (layer->layer_info.v_exp != V_EXP_OFF))
  670. /* Enable expansion filter */
  671. osd_device->ops.set_interpolation_filter(osd_device, 1);
  672. else
  673. osd_device->ops.set_interpolation_filter(osd_device, 0);
  674. return 0;
  675. }
  676. static int vpbe_display_g_crop(struct file *file, void *priv,
  677. struct v4l2_crop *crop)
  678. {
  679. struct vpbe_fh *fh = file->private_data;
  680. struct vpbe_layer *layer = fh->layer;
  681. struct osd_layer_config *cfg = &layer->layer_info.config;
  682. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  683. struct osd_state *osd_device = fh->disp_dev->osd_device;
  684. struct v4l2_rect *rect = &crop->c;
  685. int ret;
  686. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  687. "VIDIOC_G_CROP, layer id = %d\n",
  688. layer->device_id);
  689. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  690. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
  691. ret = -EINVAL;
  692. }
  693. osd_device->ops.get_layer_config(osd_device,
  694. layer->layer_info.id, cfg);
  695. rect->top = cfg->ypos;
  696. rect->left = cfg->xpos;
  697. rect->width = cfg->xsize;
  698. rect->height = cfg->ysize;
  699. return 0;
  700. }
  701. static int vpbe_display_cropcap(struct file *file, void *priv,
  702. struct v4l2_cropcap *cropcap)
  703. {
  704. struct vpbe_fh *fh = file->private_data;
  705. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  706. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
  707. cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  708. cropcap->bounds.left = 0;
  709. cropcap->bounds.top = 0;
  710. cropcap->bounds.width = vpbe_dev->current_timings.xres;
  711. cropcap->bounds.height = vpbe_dev->current_timings.yres;
  712. cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
  713. cropcap->defrect = cropcap->bounds;
  714. return 0;
  715. }
  716. static int vpbe_display_g_fmt(struct file *file, void *priv,
  717. struct v4l2_format *fmt)
  718. {
  719. struct vpbe_fh *fh = file->private_data;
  720. struct vpbe_layer *layer = fh->layer;
  721. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  722. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  723. "VIDIOC_G_FMT, layer id = %d\n",
  724. layer->device_id);
  725. /* If buffer type is video output */
  726. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  727. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  728. return -EINVAL;
  729. }
  730. /* Fill in the information about format */
  731. fmt->fmt.pix = layer->pix_fmt;
  732. return 0;
  733. }
  734. static int vpbe_display_enum_fmt(struct file *file, void *priv,
  735. struct v4l2_fmtdesc *fmt)
  736. {
  737. struct vpbe_fh *fh = file->private_data;
  738. struct vpbe_layer *layer = fh->layer;
  739. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  740. unsigned int index = 0;
  741. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  742. "VIDIOC_ENUM_FMT, layer id = %d\n",
  743. layer->device_id);
  744. if (fmt->index > 1) {
  745. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
  746. return -EINVAL;
  747. }
  748. /* Fill in the information about format */
  749. index = fmt->index;
  750. memset(fmt, 0, sizeof(*fmt));
  751. fmt->index = index;
  752. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  753. if (index == 0) {
  754. strcpy(fmt->description, "YUV 4:2:2 - UYVY");
  755. fmt->pixelformat = V4L2_PIX_FMT_UYVY;
  756. } else {
  757. strcpy(fmt->description, "Y/CbCr 4:2:0");
  758. fmt->pixelformat = V4L2_PIX_FMT_NV12;
  759. }
  760. return 0;
  761. }
  762. static int vpbe_display_s_fmt(struct file *file, void *priv,
  763. struct v4l2_format *fmt)
  764. {
  765. struct vpbe_fh *fh = file->private_data;
  766. struct vpbe_layer *layer = fh->layer;
  767. struct vpbe_display *disp_dev = fh->disp_dev;
  768. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  769. struct osd_layer_config *cfg = &layer->layer_info.config;
  770. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  771. struct osd_state *osd_device = disp_dev->osd_device;
  772. int ret;
  773. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  774. "VIDIOC_S_FMT, layer id = %d\n",
  775. layer->device_id);
  776. /* If streaming is started, return error */
  777. if (layer->started) {
  778. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  779. return -EBUSY;
  780. }
  781. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  782. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
  783. return -EINVAL;
  784. }
  785. /* Check for valid pixel format */
  786. ret = vpbe_try_format(disp_dev, pixfmt, 1);
  787. if (ret)
  788. return ret;
  789. /* YUV420 is requested, check availability of the
  790. other video window */
  791. layer->pix_fmt = *pixfmt;
  792. /* Get osd layer config */
  793. osd_device->ops.get_layer_config(osd_device,
  794. layer->layer_info.id, cfg);
  795. /* Store the pixel format in the layer object */
  796. cfg->xsize = pixfmt->width;
  797. cfg->ysize = pixfmt->height;
  798. cfg->line_length = pixfmt->bytesperline;
  799. cfg->ypos = 0;
  800. cfg->xpos = 0;
  801. cfg->interlaced = vpbe_dev->current_timings.interlaced;
  802. if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
  803. cfg->pixfmt = PIXFMT_YCbCrI;
  804. /* Change of the default pixel format for both video windows */
  805. if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
  806. struct vpbe_layer *otherlayer;
  807. cfg->pixfmt = PIXFMT_NV12;
  808. otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
  809. layer);
  810. otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
  811. }
  812. /* Set the layer config in the osd window */
  813. ret = osd_device->ops.set_layer_config(osd_device,
  814. layer->layer_info.id, cfg);
  815. if (ret < 0) {
  816. v4l2_err(&vpbe_dev->v4l2_dev,
  817. "Error in S_FMT params:\n");
  818. return -EINVAL;
  819. }
  820. /* Readback and fill the local copy of current pix format */
  821. osd_device->ops.get_layer_config(osd_device,
  822. layer->layer_info.id, cfg);
  823. return 0;
  824. }
  825. static int vpbe_display_try_fmt(struct file *file, void *priv,
  826. struct v4l2_format *fmt)
  827. {
  828. struct vpbe_fh *fh = file->private_data;
  829. struct vpbe_display *disp_dev = fh->disp_dev;
  830. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  831. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  832. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
  833. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  834. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  835. return -EINVAL;
  836. }
  837. /* Check for valid field format */
  838. return vpbe_try_format(disp_dev, pixfmt, 0);
  839. }
  840. /**
  841. * vpbe_display_s_std - Set the given standard in the encoder
  842. *
  843. * Sets the standard if supported by the current encoder. Return the status.
  844. * 0 - success & -EINVAL on error
  845. */
  846. static int vpbe_display_s_std(struct file *file, void *priv,
  847. v4l2_std_id *std_id)
  848. {
  849. struct vpbe_fh *fh = priv;
  850. struct vpbe_layer *layer = fh->layer;
  851. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  852. int ret;
  853. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
  854. /* If streaming is started, return error */
  855. if (layer->started) {
  856. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  857. return -EBUSY;
  858. }
  859. if (NULL != vpbe_dev->ops.s_std) {
  860. ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
  861. if (ret) {
  862. v4l2_err(&vpbe_dev->v4l2_dev,
  863. "Failed to set standard for sub devices\n");
  864. return -EINVAL;
  865. }
  866. } else {
  867. return -EINVAL;
  868. }
  869. return 0;
  870. }
  871. /**
  872. * vpbe_display_g_std - Get the standard in the current encoder
  873. *
  874. * Get the standard in the current encoder. Return the status. 0 - success
  875. * -EINVAL on error
  876. */
  877. static int vpbe_display_g_std(struct file *file, void *priv,
  878. v4l2_std_id *std_id)
  879. {
  880. struct vpbe_fh *fh = priv;
  881. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  882. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
  883. /* Get the standard from the current encoder */
  884. if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
  885. *std_id = vpbe_dev->current_timings.std_id;
  886. return 0;
  887. }
  888. return -EINVAL;
  889. }
  890. /**
  891. * vpbe_display_enum_output - enumerate outputs
  892. *
  893. * Enumerates the outputs available at the vpbe display
  894. * returns the status, -EINVAL if end of output list
  895. */
  896. static int vpbe_display_enum_output(struct file *file, void *priv,
  897. struct v4l2_output *output)
  898. {
  899. struct vpbe_fh *fh = priv;
  900. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  901. int ret;
  902. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
  903. /* Enumerate outputs */
  904. if (NULL == vpbe_dev->ops.enum_outputs)
  905. return -EINVAL;
  906. ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
  907. if (ret) {
  908. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  909. "Failed to enumerate outputs\n");
  910. return -EINVAL;
  911. }
  912. return 0;
  913. }
  914. /**
  915. * vpbe_display_s_output - Set output to
  916. * the output specified by the index
  917. */
  918. static int vpbe_display_s_output(struct file *file, void *priv,
  919. unsigned int i)
  920. {
  921. struct vpbe_fh *fh = priv;
  922. struct vpbe_layer *layer = fh->layer;
  923. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  924. int ret;
  925. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
  926. /* If streaming is started, return error */
  927. if (layer->started) {
  928. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  929. return -EBUSY;
  930. }
  931. if (NULL == vpbe_dev->ops.set_output)
  932. return -EINVAL;
  933. ret = vpbe_dev->ops.set_output(vpbe_dev, i);
  934. if (ret) {
  935. v4l2_err(&vpbe_dev->v4l2_dev,
  936. "Failed to set output for sub devices\n");
  937. return -EINVAL;
  938. }
  939. return 0;
  940. }
  941. /**
  942. * vpbe_display_g_output - Get output from subdevice
  943. * for a given by the index
  944. */
  945. static int vpbe_display_g_output(struct file *file, void *priv,
  946. unsigned int *i)
  947. {
  948. struct vpbe_fh *fh = priv;
  949. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  950. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
  951. /* Get the standard from the current encoder */
  952. *i = vpbe_dev->current_out_index;
  953. return 0;
  954. }
  955. /**
  956. * vpbe_display_enum_dv_timings - Enumerate the dv timings
  957. *
  958. * enum the timings in the current encoder. Return the status. 0 - success
  959. * -EINVAL on error
  960. */
  961. static int
  962. vpbe_display_enum_dv_timings(struct file *file, void *priv,
  963. struct v4l2_enum_dv_timings *timings)
  964. {
  965. struct vpbe_fh *fh = priv;
  966. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  967. int ret;
  968. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
  969. /* Enumerate outputs */
  970. if (NULL == vpbe_dev->ops.enum_dv_timings)
  971. return -EINVAL;
  972. ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
  973. if (ret) {
  974. v4l2_err(&vpbe_dev->v4l2_dev,
  975. "Failed to enumerate dv timings info\n");
  976. return -EINVAL;
  977. }
  978. return 0;
  979. }
  980. /**
  981. * vpbe_display_s_dv_timings - Set the dv timings
  982. *
  983. * Set the timings in the current encoder. Return the status. 0 - success
  984. * -EINVAL on error
  985. */
  986. static int
  987. vpbe_display_s_dv_timings(struct file *file, void *priv,
  988. struct v4l2_dv_timings *timings)
  989. {
  990. struct vpbe_fh *fh = priv;
  991. struct vpbe_layer *layer = fh->layer;
  992. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  993. int ret;
  994. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
  995. /* If streaming is started, return error */
  996. if (layer->started) {
  997. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  998. return -EBUSY;
  999. }
  1000. /* Set the given standard in the encoder */
  1001. if (!vpbe_dev->ops.s_dv_timings)
  1002. return -EINVAL;
  1003. ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
  1004. if (ret) {
  1005. v4l2_err(&vpbe_dev->v4l2_dev,
  1006. "Failed to set the dv timings info\n");
  1007. return -EINVAL;
  1008. }
  1009. /* set the current norm to zero to be consistent. If STD is used
  1010. * v4l2 layer will set the norm properly on successful s_std call
  1011. */
  1012. layer->video_dev.current_norm = 0;
  1013. return 0;
  1014. }
  1015. /**
  1016. * vpbe_display_g_dv_timings - Set the dv timings
  1017. *
  1018. * Get the timings in the current encoder. Return the status. 0 - success
  1019. * -EINVAL on error
  1020. */
  1021. static int
  1022. vpbe_display_g_dv_timings(struct file *file, void *priv,
  1023. struct v4l2_dv_timings *dv_timings)
  1024. {
  1025. struct vpbe_fh *fh = priv;
  1026. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1027. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
  1028. /* Get the given standard in the encoder */
  1029. if (vpbe_dev->current_timings.timings_type &
  1030. VPBE_ENC_CUSTOM_TIMINGS) {
  1031. *dv_timings = vpbe_dev->current_timings.dv_timings;
  1032. } else {
  1033. return -EINVAL;
  1034. }
  1035. return 0;
  1036. }
  1037. static int vpbe_display_streamoff(struct file *file, void *priv,
  1038. enum v4l2_buf_type buf_type)
  1039. {
  1040. struct vpbe_fh *fh = file->private_data;
  1041. struct vpbe_layer *layer = fh->layer;
  1042. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1043. struct osd_state *osd_device = fh->disp_dev->osd_device;
  1044. int ret;
  1045. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1046. "VIDIOC_STREAMOFF,layer id = %d\n",
  1047. layer->device_id);
  1048. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
  1049. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1050. return -EINVAL;
  1051. }
  1052. /* If io is allowed for this file handle, return error */
  1053. if (!fh->io_allowed) {
  1054. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1055. return -EACCES;
  1056. }
  1057. /* If streaming is not started, return error */
  1058. if (!layer->started) {
  1059. v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
  1060. " id = %d\n", layer->device_id);
  1061. return -EINVAL;
  1062. }
  1063. osd_device->ops.disable_layer(osd_device,
  1064. layer->layer_info.id);
  1065. layer->started = 0;
  1066. ret = vb2_streamoff(&layer->buffer_queue, buf_type);
  1067. return ret;
  1068. }
  1069. static int vpbe_display_streamon(struct file *file, void *priv,
  1070. enum v4l2_buf_type buf_type)
  1071. {
  1072. struct vpbe_fh *fh = file->private_data;
  1073. struct vpbe_layer *layer = fh->layer;
  1074. struct vpbe_display *disp_dev = fh->disp_dev;
  1075. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1076. struct osd_state *osd_device = disp_dev->osd_device;
  1077. int ret;
  1078. osd_device->ops.disable_layer(osd_device,
  1079. layer->layer_info.id);
  1080. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
  1081. layer->device_id);
  1082. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
  1083. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1084. return -EINVAL;
  1085. }
  1086. /* If file handle is not allowed IO, return error */
  1087. if (!fh->io_allowed) {
  1088. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1089. return -EACCES;
  1090. }
  1091. /* If Streaming is already started, return error */
  1092. if (layer->started) {
  1093. v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
  1094. return -EBUSY;
  1095. }
  1096. /*
  1097. * Call vb2_streamon to start streaming
  1098. * in videobuf
  1099. */
  1100. ret = vb2_streamon(&layer->buffer_queue, buf_type);
  1101. if (ret) {
  1102. v4l2_err(&vpbe_dev->v4l2_dev,
  1103. "error in vb2_streamon\n");
  1104. return ret;
  1105. }
  1106. return ret;
  1107. }
  1108. static int vpbe_display_dqbuf(struct file *file, void *priv,
  1109. struct v4l2_buffer *buf)
  1110. {
  1111. struct vpbe_fh *fh = file->private_data;
  1112. struct vpbe_layer *layer = fh->layer;
  1113. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1114. int ret;
  1115. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1116. "VIDIOC_DQBUF, layer id = %d\n",
  1117. layer->device_id);
  1118. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
  1119. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1120. return -EINVAL;
  1121. }
  1122. /* If this file handle is not allowed to do IO, return error */
  1123. if (!fh->io_allowed) {
  1124. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1125. return -EACCES;
  1126. }
  1127. if (file->f_flags & O_NONBLOCK)
  1128. /* Call videobuf_dqbuf for non blocking mode */
  1129. ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
  1130. else
  1131. /* Call videobuf_dqbuf for blocking mode */
  1132. ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
  1133. return ret;
  1134. }
  1135. static int vpbe_display_qbuf(struct file *file, void *priv,
  1136. struct v4l2_buffer *p)
  1137. {
  1138. struct vpbe_fh *fh = file->private_data;
  1139. struct vpbe_layer *layer = fh->layer;
  1140. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1141. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1142. "VIDIOC_QBUF, layer id = %d\n",
  1143. layer->device_id);
  1144. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
  1145. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1146. return -EINVAL;
  1147. }
  1148. /* If this file handle is not allowed to do IO, return error */
  1149. if (!fh->io_allowed) {
  1150. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1151. return -EACCES;
  1152. }
  1153. return vb2_qbuf(&layer->buffer_queue, p);
  1154. }
  1155. static int vpbe_display_querybuf(struct file *file, void *priv,
  1156. struct v4l2_buffer *buf)
  1157. {
  1158. struct vpbe_fh *fh = file->private_data;
  1159. struct vpbe_layer *layer = fh->layer;
  1160. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1161. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1162. "VIDIOC_QUERYBUF, layer id = %d\n",
  1163. layer->device_id);
  1164. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
  1165. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1166. return -EINVAL;
  1167. }
  1168. /* Call vb2_querybuf to get information */
  1169. return vb2_querybuf(&layer->buffer_queue, buf);
  1170. }
  1171. static int vpbe_display_reqbufs(struct file *file, void *priv,
  1172. struct v4l2_requestbuffers *req_buf)
  1173. {
  1174. struct vpbe_fh *fh = file->private_data;
  1175. struct vpbe_layer *layer = fh->layer;
  1176. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1177. struct vb2_queue *q;
  1178. int ret;
  1179. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
  1180. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
  1181. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1182. return -EINVAL;
  1183. }
  1184. /* If io users of the layer is not zero, return error */
  1185. if (0 != layer->io_usrs) {
  1186. v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
  1187. return -EBUSY;
  1188. }
  1189. /* Initialize videobuf queue as per the buffer type */
  1190. layer->alloc_ctx = vb2_dma_contig_init_ctx(vpbe_dev->pdev);
  1191. if (!layer->alloc_ctx) {
  1192. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to get the context\n");
  1193. return -EINVAL;
  1194. }
  1195. q = &layer->buffer_queue;
  1196. memset(q, 0, sizeof(*q));
  1197. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1198. q->io_modes = VB2_MMAP | VB2_USERPTR;
  1199. q->drv_priv = fh;
  1200. q->ops = &video_qops;
  1201. q->mem_ops = &vb2_dma_contig_memops;
  1202. q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
  1203. ret = vb2_queue_init(q);
  1204. if (ret) {
  1205. v4l2_err(&vpbe_dev->v4l2_dev, "vb2_queue_init() failed\n");
  1206. vb2_dma_contig_cleanup_ctx(layer->alloc_ctx);
  1207. return ret;
  1208. }
  1209. /* Set io allowed member of file handle to TRUE */
  1210. fh->io_allowed = 1;
  1211. /* Increment io usrs member of layer object to 1 */
  1212. layer->io_usrs = 1;
  1213. /* Store type of memory requested in layer object */
  1214. layer->memory = req_buf->memory;
  1215. /* Initialize buffer queue */
  1216. INIT_LIST_HEAD(&layer->dma_queue);
  1217. /* Allocate buffers */
  1218. return vb2_reqbufs(q, req_buf);
  1219. }
  1220. /*
  1221. * vpbe_display_mmap()
  1222. * It is used to map kernel space buffers into user spaces
  1223. */
  1224. static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
  1225. {
  1226. /* Get the layer object and file handle object */
  1227. struct vpbe_fh *fh = filep->private_data;
  1228. struct vpbe_layer *layer = fh->layer;
  1229. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1230. int ret;
  1231. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
  1232. if (mutex_lock_interruptible(&layer->opslock))
  1233. return -ERESTARTSYS;
  1234. ret = vb2_mmap(&layer->buffer_queue, vma);
  1235. mutex_unlock(&layer->opslock);
  1236. return ret;
  1237. }
  1238. /* vpbe_display_poll(): It is used for select/poll system call
  1239. */
  1240. static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
  1241. {
  1242. struct vpbe_fh *fh = filep->private_data;
  1243. struct vpbe_layer *layer = fh->layer;
  1244. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1245. unsigned int err = 0;
  1246. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
  1247. if (layer->started) {
  1248. mutex_lock(&layer->opslock);
  1249. err = vb2_poll(&layer->buffer_queue, filep, wait);
  1250. mutex_unlock(&layer->opslock);
  1251. }
  1252. return err;
  1253. }
  1254. /*
  1255. * vpbe_display_open()
  1256. * It creates object of file handle structure and stores it in private_data
  1257. * member of filepointer
  1258. */
  1259. static int vpbe_display_open(struct file *file)
  1260. {
  1261. struct vpbe_fh *fh = NULL;
  1262. struct vpbe_layer *layer = video_drvdata(file);
  1263. struct vpbe_display *disp_dev = layer->disp_dev;
  1264. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1265. struct osd_state *osd_device = disp_dev->osd_device;
  1266. int err;
  1267. /* Allocate memory for the file handle object */
  1268. fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
  1269. if (fh == NULL) {
  1270. v4l2_err(&vpbe_dev->v4l2_dev,
  1271. "unable to allocate memory for file handle object\n");
  1272. return -ENOMEM;
  1273. }
  1274. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1275. "vpbe display open plane = %d\n",
  1276. layer->device_id);
  1277. /* store pointer to fh in private_data member of filep */
  1278. file->private_data = fh;
  1279. fh->layer = layer;
  1280. fh->disp_dev = disp_dev;
  1281. if (!layer->usrs) {
  1282. if (mutex_lock_interruptible(&layer->opslock))
  1283. return -ERESTARTSYS;
  1284. /* First claim the layer for this device */
  1285. err = osd_device->ops.request_layer(osd_device,
  1286. layer->layer_info.id);
  1287. mutex_unlock(&layer->opslock);
  1288. if (err < 0) {
  1289. /* Couldn't get layer */
  1290. v4l2_err(&vpbe_dev->v4l2_dev,
  1291. "Display Manager failed to allocate layer\n");
  1292. kfree(fh);
  1293. return -EINVAL;
  1294. }
  1295. }
  1296. /* Increment layer usrs counter */
  1297. layer->usrs++;
  1298. /* Set io_allowed member to false */
  1299. fh->io_allowed = 0;
  1300. /* Initialize priority of this instance to default priority */
  1301. fh->prio = V4L2_PRIORITY_UNSET;
  1302. v4l2_prio_open(&layer->prio, &fh->prio);
  1303. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1304. "vpbe display device opened successfully\n");
  1305. return 0;
  1306. }
  1307. /*
  1308. * vpbe_display_release()
  1309. * This function deletes buffer queue, frees the buffers and the davinci
  1310. * display file * handle
  1311. */
  1312. static int vpbe_display_release(struct file *file)
  1313. {
  1314. /* Get the layer object and file handle object */
  1315. struct vpbe_fh *fh = file->private_data;
  1316. struct vpbe_layer *layer = fh->layer;
  1317. struct osd_layer_config *cfg = &layer->layer_info.config;
  1318. struct vpbe_display *disp_dev = fh->disp_dev;
  1319. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1320. struct osd_state *osd_device = disp_dev->osd_device;
  1321. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
  1322. mutex_lock(&layer->opslock);
  1323. /* if this instance is doing IO */
  1324. if (fh->io_allowed) {
  1325. /* Reset io_usrs member of layer object */
  1326. layer->io_usrs = 0;
  1327. osd_device->ops.disable_layer(osd_device,
  1328. layer->layer_info.id);
  1329. layer->started = 0;
  1330. /* Free buffers allocated */
  1331. vb2_queue_release(&layer->buffer_queue);
  1332. vb2_dma_contig_cleanup_ctx(&layer->buffer_queue);
  1333. }
  1334. /* Decrement layer usrs counter */
  1335. layer->usrs--;
  1336. /* If this file handle has initialize encoder device, reset it */
  1337. if (!layer->usrs) {
  1338. if (cfg->pixfmt == PIXFMT_NV12) {
  1339. struct vpbe_layer *otherlayer;
  1340. otherlayer =
  1341. _vpbe_display_get_other_win_layer(disp_dev, layer);
  1342. osd_device->ops.disable_layer(osd_device,
  1343. otherlayer->layer_info.id);
  1344. osd_device->ops.release_layer(osd_device,
  1345. otherlayer->layer_info.id);
  1346. }
  1347. osd_device->ops.disable_layer(osd_device,
  1348. layer->layer_info.id);
  1349. osd_device->ops.release_layer(osd_device,
  1350. layer->layer_info.id);
  1351. }
  1352. /* Close the priority */
  1353. v4l2_prio_close(&layer->prio, fh->prio);
  1354. file->private_data = NULL;
  1355. mutex_unlock(&layer->opslock);
  1356. /* Free memory allocated to file handle object */
  1357. kfree(fh);
  1358. disp_dev->cbcr_ofst = 0;
  1359. return 0;
  1360. }
  1361. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1362. static int vpbe_display_g_register(struct file *file, void *priv,
  1363. struct v4l2_dbg_register *reg)
  1364. {
  1365. struct v4l2_dbg_match *match = &reg->match;
  1366. struct vpbe_fh *fh = file->private_data;
  1367. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1368. if (match->type >= 2) {
  1369. v4l2_subdev_call(vpbe_dev->venc,
  1370. core,
  1371. g_register,
  1372. reg);
  1373. }
  1374. return 0;
  1375. }
  1376. static int vpbe_display_s_register(struct file *file, void *priv,
  1377. struct v4l2_dbg_register *reg)
  1378. {
  1379. return 0;
  1380. }
  1381. #endif
  1382. /* vpbe capture ioctl operations */
  1383. static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
  1384. .vidioc_querycap = vpbe_display_querycap,
  1385. .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
  1386. .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
  1387. .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
  1388. .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
  1389. .vidioc_reqbufs = vpbe_display_reqbufs,
  1390. .vidioc_querybuf = vpbe_display_querybuf,
  1391. .vidioc_qbuf = vpbe_display_qbuf,
  1392. .vidioc_dqbuf = vpbe_display_dqbuf,
  1393. .vidioc_streamon = vpbe_display_streamon,
  1394. .vidioc_streamoff = vpbe_display_streamoff,
  1395. .vidioc_cropcap = vpbe_display_cropcap,
  1396. .vidioc_g_crop = vpbe_display_g_crop,
  1397. .vidioc_s_crop = vpbe_display_s_crop,
  1398. .vidioc_g_priority = vpbe_display_g_priority,
  1399. .vidioc_s_priority = vpbe_display_s_priority,
  1400. .vidioc_s_std = vpbe_display_s_std,
  1401. .vidioc_g_std = vpbe_display_g_std,
  1402. .vidioc_enum_output = vpbe_display_enum_output,
  1403. .vidioc_s_output = vpbe_display_s_output,
  1404. .vidioc_g_output = vpbe_display_g_output,
  1405. .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
  1406. .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
  1407. .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
  1408. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1409. .vidioc_g_register = vpbe_display_g_register,
  1410. .vidioc_s_register = vpbe_display_s_register,
  1411. #endif
  1412. };
  1413. static struct v4l2_file_operations vpbe_fops = {
  1414. .owner = THIS_MODULE,
  1415. .open = vpbe_display_open,
  1416. .release = vpbe_display_release,
  1417. .unlocked_ioctl = video_ioctl2,
  1418. .mmap = vpbe_display_mmap,
  1419. .poll = vpbe_display_poll
  1420. };
  1421. static int vpbe_device_get(struct device *dev, void *data)
  1422. {
  1423. struct platform_device *pdev = to_platform_device(dev);
  1424. struct vpbe_display *vpbe_disp = data;
  1425. if (strcmp("vpbe_controller", pdev->name) == 0)
  1426. vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
  1427. if (strstr(pdev->name, "vpbe-osd") != NULL)
  1428. vpbe_disp->osd_device = platform_get_drvdata(pdev);
  1429. return 0;
  1430. }
  1431. static __devinit int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
  1432. struct platform_device *pdev)
  1433. {
  1434. struct vpbe_layer *vpbe_display_layer = NULL;
  1435. struct video_device *vbd = NULL;
  1436. /* Allocate memory for four plane display objects */
  1437. disp_dev->dev[i] =
  1438. kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
  1439. /* If memory allocation fails, return error */
  1440. if (!disp_dev->dev[i]) {
  1441. printk(KERN_ERR "ran out of memory\n");
  1442. return -ENOMEM;
  1443. }
  1444. spin_lock_init(&disp_dev->dev[i]->irqlock);
  1445. mutex_init(&disp_dev->dev[i]->opslock);
  1446. /* Get the pointer to the layer object */
  1447. vpbe_display_layer = disp_dev->dev[i];
  1448. vbd = &vpbe_display_layer->video_dev;
  1449. /* Initialize field of video device */
  1450. vbd->release = video_device_release_empty;
  1451. vbd->fops = &vpbe_fops;
  1452. vbd->ioctl_ops = &vpbe_ioctl_ops;
  1453. vbd->minor = -1;
  1454. vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1455. vbd->lock = &vpbe_display_layer->opslock;
  1456. vbd->vfl_dir = VFL_DIR_TX;
  1457. if (disp_dev->vpbe_dev->current_timings.timings_type &
  1458. VPBE_ENC_STD) {
  1459. vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
  1460. vbd->current_norm =
  1461. disp_dev->vpbe_dev->current_timings.std_id;
  1462. } else
  1463. vbd->current_norm = 0;
  1464. snprintf(vbd->name, sizeof(vbd->name),
  1465. "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
  1466. (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1467. (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1468. (VPBE_DISPLAY_VERSION_CODE) & 0xff);
  1469. vpbe_display_layer->device_id = i;
  1470. vpbe_display_layer->layer_info.id =
  1471. ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
  1472. /* Initialize prio member of layer object */
  1473. v4l2_prio_init(&vpbe_display_layer->prio);
  1474. return 0;
  1475. }
  1476. static __devinit int register_device(struct vpbe_layer *vpbe_display_layer,
  1477. struct vpbe_display *disp_dev,
  1478. struct platform_device *pdev) {
  1479. int err;
  1480. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1481. "Trying to register VPBE display device.\n");
  1482. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1483. "layer=%x,layer->video_dev=%x\n",
  1484. (int)vpbe_display_layer,
  1485. (int)&vpbe_display_layer->video_dev);
  1486. err = video_register_device(&vpbe_display_layer->video_dev,
  1487. VFL_TYPE_GRABBER,
  1488. -1);
  1489. if (err)
  1490. return -ENODEV;
  1491. vpbe_display_layer->disp_dev = disp_dev;
  1492. /* set the driver data in platform device */
  1493. platform_set_drvdata(pdev, disp_dev);
  1494. video_set_drvdata(&vpbe_display_layer->video_dev,
  1495. vpbe_display_layer);
  1496. return 0;
  1497. }
  1498. /*
  1499. * vpbe_display_probe()
  1500. * This function creates device entries by register itself to the V4L2 driver
  1501. * and initializes fields of each layer objects
  1502. */
  1503. static __devinit int vpbe_display_probe(struct platform_device *pdev)
  1504. {
  1505. struct vpbe_layer *vpbe_display_layer;
  1506. struct vpbe_display *disp_dev;
  1507. struct resource *res = NULL;
  1508. int k;
  1509. int i;
  1510. int err;
  1511. int irq;
  1512. printk(KERN_DEBUG "vpbe_display_probe\n");
  1513. /* Allocate memory for vpbe_display */
  1514. disp_dev = kzalloc(sizeof(struct vpbe_display), GFP_KERNEL);
  1515. if (!disp_dev) {
  1516. printk(KERN_ERR "ran out of memory\n");
  1517. return -ENOMEM;
  1518. }
  1519. spin_lock_init(&disp_dev->dma_queue_lock);
  1520. /*
  1521. * Scan all the platform devices to find the vpbe
  1522. * controller device and get the vpbe_dev object
  1523. */
  1524. err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
  1525. vpbe_device_get);
  1526. if (err < 0)
  1527. return err;
  1528. /* Initialize the vpbe display controller */
  1529. if (NULL != disp_dev->vpbe_dev->ops.initialize) {
  1530. err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
  1531. disp_dev->vpbe_dev);
  1532. if (err) {
  1533. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1534. "Error initing vpbe\n");
  1535. err = -ENOMEM;
  1536. goto probe_out;
  1537. }
  1538. }
  1539. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1540. if (init_vpbe_layer(i, disp_dev, pdev)) {
  1541. err = -ENODEV;
  1542. goto probe_out;
  1543. }
  1544. }
  1545. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1546. if (!res) {
  1547. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1548. "Unable to get VENC interrupt resource\n");
  1549. err = -ENODEV;
  1550. goto probe_out;
  1551. }
  1552. irq = res->start;
  1553. if (request_irq(irq, venc_isr, IRQF_DISABLED, VPBE_DISPLAY_DRIVER,
  1554. disp_dev)) {
  1555. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1556. "Unable to request interrupt\n");
  1557. err = -ENODEV;
  1558. goto probe_out;
  1559. }
  1560. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1561. if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
  1562. err = -ENODEV;
  1563. goto probe_out_irq;
  1564. }
  1565. }
  1566. printk(KERN_DEBUG "Successfully completed the probing of vpbe v4l2 device\n");
  1567. return 0;
  1568. probe_out_irq:
  1569. free_irq(res->start, disp_dev);
  1570. probe_out:
  1571. for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
  1572. /* Get the pointer to the layer object */
  1573. vpbe_display_layer = disp_dev->dev[k];
  1574. /* Unregister video device */
  1575. if (vpbe_display_layer) {
  1576. video_unregister_device(
  1577. &vpbe_display_layer->video_dev);
  1578. kfree(disp_dev->dev[k]);
  1579. }
  1580. }
  1581. kfree(disp_dev);
  1582. return err;
  1583. }
  1584. /*
  1585. * vpbe_display_remove()
  1586. * It un-register hardware layer from V4L2 driver
  1587. */
  1588. static int vpbe_display_remove(struct platform_device *pdev)
  1589. {
  1590. struct vpbe_layer *vpbe_display_layer;
  1591. struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
  1592. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1593. struct resource *res;
  1594. int i;
  1595. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
  1596. /* unregister irq */
  1597. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1598. free_irq(res->start, disp_dev);
  1599. /* deinitialize the vpbe display controller */
  1600. if (NULL != vpbe_dev->ops.deinitialize)
  1601. vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
  1602. /* un-register device */
  1603. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1604. /* Get the pointer to the layer object */
  1605. vpbe_display_layer = disp_dev->dev[i];
  1606. /* Unregister video device */
  1607. video_unregister_device(&vpbe_display_layer->video_dev);
  1608. }
  1609. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1610. kfree(disp_dev->dev[i]);
  1611. disp_dev->dev[i] = NULL;
  1612. }
  1613. return 0;
  1614. }
  1615. static struct platform_driver vpbe_display_driver = {
  1616. .driver = {
  1617. .name = VPBE_DISPLAY_DRIVER,
  1618. .owner = THIS_MODULE,
  1619. .bus = &platform_bus_type,
  1620. },
  1621. .probe = vpbe_display_probe,
  1622. .remove = __devexit_p(vpbe_display_remove),
  1623. };
  1624. module_platform_driver(vpbe_display_driver);
  1625. MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
  1626. MODULE_LICENSE("GPL");
  1627. MODULE_AUTHOR("Texas Instruments");