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. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  686. "VIDIOC_G_CROP, layer id = %d\n",
  687. layer->device_id);
  688. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  689. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
  690. return -EINVAL;
  691. }
  692. osd_device->ops.get_layer_config(osd_device,
  693. layer->layer_info.id, cfg);
  694. rect->top = cfg->ypos;
  695. rect->left = cfg->xpos;
  696. rect->width = cfg->xsize;
  697. rect->height = cfg->ysize;
  698. return 0;
  699. }
  700. static int vpbe_display_cropcap(struct file *file, void *priv,
  701. struct v4l2_cropcap *cropcap)
  702. {
  703. struct vpbe_fh *fh = file->private_data;
  704. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  705. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
  706. cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  707. cropcap->bounds.left = 0;
  708. cropcap->bounds.top = 0;
  709. cropcap->bounds.width = vpbe_dev->current_timings.xres;
  710. cropcap->bounds.height = vpbe_dev->current_timings.yres;
  711. cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
  712. cropcap->defrect = cropcap->bounds;
  713. return 0;
  714. }
  715. static int vpbe_display_g_fmt(struct file *file, void *priv,
  716. struct v4l2_format *fmt)
  717. {
  718. struct vpbe_fh *fh = file->private_data;
  719. struct vpbe_layer *layer = fh->layer;
  720. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  721. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  722. "VIDIOC_G_FMT, layer id = %d\n",
  723. layer->device_id);
  724. /* If buffer type is video output */
  725. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  726. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  727. return -EINVAL;
  728. }
  729. /* Fill in the information about format */
  730. fmt->fmt.pix = layer->pix_fmt;
  731. return 0;
  732. }
  733. static int vpbe_display_enum_fmt(struct file *file, void *priv,
  734. struct v4l2_fmtdesc *fmt)
  735. {
  736. struct vpbe_fh *fh = file->private_data;
  737. struct vpbe_layer *layer = fh->layer;
  738. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  739. unsigned int index = 0;
  740. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  741. "VIDIOC_ENUM_FMT, layer id = %d\n",
  742. layer->device_id);
  743. if (fmt->index > 1) {
  744. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
  745. return -EINVAL;
  746. }
  747. /* Fill in the information about format */
  748. index = fmt->index;
  749. memset(fmt, 0, sizeof(*fmt));
  750. fmt->index = index;
  751. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  752. if (index == 0) {
  753. strcpy(fmt->description, "YUV 4:2:2 - UYVY");
  754. fmt->pixelformat = V4L2_PIX_FMT_UYVY;
  755. } else {
  756. strcpy(fmt->description, "Y/CbCr 4:2:0");
  757. fmt->pixelformat = V4L2_PIX_FMT_NV12;
  758. }
  759. return 0;
  760. }
  761. static int vpbe_display_s_fmt(struct file *file, void *priv,
  762. struct v4l2_format *fmt)
  763. {
  764. struct vpbe_fh *fh = file->private_data;
  765. struct vpbe_layer *layer = fh->layer;
  766. struct vpbe_display *disp_dev = fh->disp_dev;
  767. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  768. struct osd_layer_config *cfg = &layer->layer_info.config;
  769. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  770. struct osd_state *osd_device = disp_dev->osd_device;
  771. int ret;
  772. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  773. "VIDIOC_S_FMT, layer id = %d\n",
  774. layer->device_id);
  775. /* If streaming is started, return error */
  776. if (layer->started) {
  777. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  778. return -EBUSY;
  779. }
  780. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  781. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
  782. return -EINVAL;
  783. }
  784. /* Check for valid pixel format */
  785. ret = vpbe_try_format(disp_dev, pixfmt, 1);
  786. if (ret)
  787. return ret;
  788. /* YUV420 is requested, check availability of the
  789. other video window */
  790. layer->pix_fmt = *pixfmt;
  791. /* Get osd layer config */
  792. osd_device->ops.get_layer_config(osd_device,
  793. layer->layer_info.id, cfg);
  794. /* Store the pixel format in the layer object */
  795. cfg->xsize = pixfmt->width;
  796. cfg->ysize = pixfmt->height;
  797. cfg->line_length = pixfmt->bytesperline;
  798. cfg->ypos = 0;
  799. cfg->xpos = 0;
  800. cfg->interlaced = vpbe_dev->current_timings.interlaced;
  801. if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
  802. cfg->pixfmt = PIXFMT_YCbCrI;
  803. /* Change of the default pixel format for both video windows */
  804. if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
  805. struct vpbe_layer *otherlayer;
  806. cfg->pixfmt = PIXFMT_NV12;
  807. otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
  808. layer);
  809. otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
  810. }
  811. /* Set the layer config in the osd window */
  812. ret = osd_device->ops.set_layer_config(osd_device,
  813. layer->layer_info.id, cfg);
  814. if (ret < 0) {
  815. v4l2_err(&vpbe_dev->v4l2_dev,
  816. "Error in S_FMT params:\n");
  817. return -EINVAL;
  818. }
  819. /* Readback and fill the local copy of current pix format */
  820. osd_device->ops.get_layer_config(osd_device,
  821. layer->layer_info.id, cfg);
  822. return 0;
  823. }
  824. static int vpbe_display_try_fmt(struct file *file, void *priv,
  825. struct v4l2_format *fmt)
  826. {
  827. struct vpbe_fh *fh = file->private_data;
  828. struct vpbe_display *disp_dev = fh->disp_dev;
  829. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  830. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  831. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
  832. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  833. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  834. return -EINVAL;
  835. }
  836. /* Check for valid field format */
  837. return vpbe_try_format(disp_dev, pixfmt, 0);
  838. }
  839. /**
  840. * vpbe_display_s_std - Set the given standard in the encoder
  841. *
  842. * Sets the standard if supported by the current encoder. Return the status.
  843. * 0 - success & -EINVAL on error
  844. */
  845. static int vpbe_display_s_std(struct file *file, void *priv,
  846. v4l2_std_id *std_id)
  847. {
  848. struct vpbe_fh *fh = priv;
  849. struct vpbe_layer *layer = fh->layer;
  850. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  851. int ret;
  852. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
  853. /* If streaming is started, return error */
  854. if (layer->started) {
  855. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  856. return -EBUSY;
  857. }
  858. if (NULL != vpbe_dev->ops.s_std) {
  859. ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
  860. if (ret) {
  861. v4l2_err(&vpbe_dev->v4l2_dev,
  862. "Failed to set standard for sub devices\n");
  863. return -EINVAL;
  864. }
  865. } else {
  866. return -EINVAL;
  867. }
  868. return 0;
  869. }
  870. /**
  871. * vpbe_display_g_std - Get the standard in the current encoder
  872. *
  873. * Get the standard in the current encoder. Return the status. 0 - success
  874. * -EINVAL on error
  875. */
  876. static int vpbe_display_g_std(struct file *file, void *priv,
  877. v4l2_std_id *std_id)
  878. {
  879. struct vpbe_fh *fh = priv;
  880. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  881. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
  882. /* Get the standard from the current encoder */
  883. if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
  884. *std_id = vpbe_dev->current_timings.std_id;
  885. return 0;
  886. }
  887. return -EINVAL;
  888. }
  889. /**
  890. * vpbe_display_enum_output - enumerate outputs
  891. *
  892. * Enumerates the outputs available at the vpbe display
  893. * returns the status, -EINVAL if end of output list
  894. */
  895. static int vpbe_display_enum_output(struct file *file, void *priv,
  896. struct v4l2_output *output)
  897. {
  898. struct vpbe_fh *fh = priv;
  899. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  900. int ret;
  901. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
  902. /* Enumerate outputs */
  903. if (NULL == vpbe_dev->ops.enum_outputs)
  904. return -EINVAL;
  905. ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
  906. if (ret) {
  907. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  908. "Failed to enumerate outputs\n");
  909. return -EINVAL;
  910. }
  911. return 0;
  912. }
  913. /**
  914. * vpbe_display_s_output - Set output to
  915. * the output specified by the index
  916. */
  917. static int vpbe_display_s_output(struct file *file, void *priv,
  918. unsigned int i)
  919. {
  920. struct vpbe_fh *fh = priv;
  921. struct vpbe_layer *layer = fh->layer;
  922. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  923. int ret;
  924. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
  925. /* If streaming is started, return error */
  926. if (layer->started) {
  927. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  928. return -EBUSY;
  929. }
  930. if (NULL == vpbe_dev->ops.set_output)
  931. return -EINVAL;
  932. ret = vpbe_dev->ops.set_output(vpbe_dev, i);
  933. if (ret) {
  934. v4l2_err(&vpbe_dev->v4l2_dev,
  935. "Failed to set output for sub devices\n");
  936. return -EINVAL;
  937. }
  938. return 0;
  939. }
  940. /**
  941. * vpbe_display_g_output - Get output from subdevice
  942. * for a given by the index
  943. */
  944. static int vpbe_display_g_output(struct file *file, void *priv,
  945. unsigned int *i)
  946. {
  947. struct vpbe_fh *fh = priv;
  948. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  949. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
  950. /* Get the standard from the current encoder */
  951. *i = vpbe_dev->current_out_index;
  952. return 0;
  953. }
  954. /**
  955. * vpbe_display_enum_dv_timings - Enumerate the dv timings
  956. *
  957. * enum the timings in the current encoder. Return the status. 0 - success
  958. * -EINVAL on error
  959. */
  960. static int
  961. vpbe_display_enum_dv_timings(struct file *file, void *priv,
  962. struct v4l2_enum_dv_timings *timings)
  963. {
  964. struct vpbe_fh *fh = priv;
  965. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  966. int ret;
  967. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
  968. /* Enumerate outputs */
  969. if (NULL == vpbe_dev->ops.enum_dv_timings)
  970. return -EINVAL;
  971. ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
  972. if (ret) {
  973. v4l2_err(&vpbe_dev->v4l2_dev,
  974. "Failed to enumerate dv timings info\n");
  975. return -EINVAL;
  976. }
  977. return 0;
  978. }
  979. /**
  980. * vpbe_display_s_dv_timings - Set the dv timings
  981. *
  982. * Set the timings in the current encoder. Return the status. 0 - success
  983. * -EINVAL on error
  984. */
  985. static int
  986. vpbe_display_s_dv_timings(struct file *file, void *priv,
  987. struct v4l2_dv_timings *timings)
  988. {
  989. struct vpbe_fh *fh = priv;
  990. struct vpbe_layer *layer = fh->layer;
  991. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  992. int ret;
  993. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
  994. /* If streaming is started, return error */
  995. if (layer->started) {
  996. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  997. return -EBUSY;
  998. }
  999. /* Set the given standard in the encoder */
  1000. if (!vpbe_dev->ops.s_dv_timings)
  1001. return -EINVAL;
  1002. ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
  1003. if (ret) {
  1004. v4l2_err(&vpbe_dev->v4l2_dev,
  1005. "Failed to set the dv timings info\n");
  1006. return -EINVAL;
  1007. }
  1008. /* set the current norm to zero to be consistent. If STD is used
  1009. * v4l2 layer will set the norm properly on successful s_std call
  1010. */
  1011. layer->video_dev.current_norm = 0;
  1012. return 0;
  1013. }
  1014. /**
  1015. * vpbe_display_g_dv_timings - Set the dv timings
  1016. *
  1017. * Get the timings in the current encoder. Return the status. 0 - success
  1018. * -EINVAL on error
  1019. */
  1020. static int
  1021. vpbe_display_g_dv_timings(struct file *file, void *priv,
  1022. struct v4l2_dv_timings *dv_timings)
  1023. {
  1024. struct vpbe_fh *fh = priv;
  1025. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1026. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
  1027. /* Get the given standard in the encoder */
  1028. if (vpbe_dev->current_timings.timings_type &
  1029. VPBE_ENC_CUSTOM_TIMINGS) {
  1030. *dv_timings = vpbe_dev->current_timings.dv_timings;
  1031. } else {
  1032. return -EINVAL;
  1033. }
  1034. return 0;
  1035. }
  1036. static int vpbe_display_streamoff(struct file *file, void *priv,
  1037. enum v4l2_buf_type buf_type)
  1038. {
  1039. struct vpbe_fh *fh = file->private_data;
  1040. struct vpbe_layer *layer = fh->layer;
  1041. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1042. struct osd_state *osd_device = fh->disp_dev->osd_device;
  1043. int ret;
  1044. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1045. "VIDIOC_STREAMOFF,layer id = %d\n",
  1046. layer->device_id);
  1047. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
  1048. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1049. return -EINVAL;
  1050. }
  1051. /* If io is allowed for this file handle, return error */
  1052. if (!fh->io_allowed) {
  1053. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1054. return -EACCES;
  1055. }
  1056. /* If streaming is not started, return error */
  1057. if (!layer->started) {
  1058. v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
  1059. " id = %d\n", layer->device_id);
  1060. return -EINVAL;
  1061. }
  1062. osd_device->ops.disable_layer(osd_device,
  1063. layer->layer_info.id);
  1064. layer->started = 0;
  1065. ret = vb2_streamoff(&layer->buffer_queue, buf_type);
  1066. return ret;
  1067. }
  1068. static int vpbe_display_streamon(struct file *file, void *priv,
  1069. enum v4l2_buf_type buf_type)
  1070. {
  1071. struct vpbe_fh *fh = file->private_data;
  1072. struct vpbe_layer *layer = fh->layer;
  1073. struct vpbe_display *disp_dev = fh->disp_dev;
  1074. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1075. struct osd_state *osd_device = disp_dev->osd_device;
  1076. int ret;
  1077. osd_device->ops.disable_layer(osd_device,
  1078. layer->layer_info.id);
  1079. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
  1080. layer->device_id);
  1081. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
  1082. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1083. return -EINVAL;
  1084. }
  1085. /* If file handle is not allowed IO, return error */
  1086. if (!fh->io_allowed) {
  1087. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1088. return -EACCES;
  1089. }
  1090. /* If Streaming is already started, return error */
  1091. if (layer->started) {
  1092. v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
  1093. return -EBUSY;
  1094. }
  1095. /*
  1096. * Call vb2_streamon to start streaming
  1097. * in videobuf
  1098. */
  1099. ret = vb2_streamon(&layer->buffer_queue, buf_type);
  1100. if (ret) {
  1101. v4l2_err(&vpbe_dev->v4l2_dev,
  1102. "error in vb2_streamon\n");
  1103. return ret;
  1104. }
  1105. return ret;
  1106. }
  1107. static int vpbe_display_dqbuf(struct file *file, void *priv,
  1108. struct v4l2_buffer *buf)
  1109. {
  1110. struct vpbe_fh *fh = file->private_data;
  1111. struct vpbe_layer *layer = fh->layer;
  1112. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1113. int ret;
  1114. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1115. "VIDIOC_DQBUF, layer id = %d\n",
  1116. layer->device_id);
  1117. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
  1118. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1119. return -EINVAL;
  1120. }
  1121. /* If this file handle is not allowed to do IO, return error */
  1122. if (!fh->io_allowed) {
  1123. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1124. return -EACCES;
  1125. }
  1126. if (file->f_flags & O_NONBLOCK)
  1127. /* Call videobuf_dqbuf for non blocking mode */
  1128. ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
  1129. else
  1130. /* Call videobuf_dqbuf for blocking mode */
  1131. ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
  1132. return ret;
  1133. }
  1134. static int vpbe_display_qbuf(struct file *file, void *priv,
  1135. struct v4l2_buffer *p)
  1136. {
  1137. struct vpbe_fh *fh = file->private_data;
  1138. struct vpbe_layer *layer = fh->layer;
  1139. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1140. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1141. "VIDIOC_QBUF, layer id = %d\n",
  1142. layer->device_id);
  1143. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
  1144. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1145. return -EINVAL;
  1146. }
  1147. /* If this file handle is not allowed to do IO, return error */
  1148. if (!fh->io_allowed) {
  1149. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1150. return -EACCES;
  1151. }
  1152. return vb2_qbuf(&layer->buffer_queue, p);
  1153. }
  1154. static int vpbe_display_querybuf(struct file *file, void *priv,
  1155. struct v4l2_buffer *buf)
  1156. {
  1157. struct vpbe_fh *fh = file->private_data;
  1158. struct vpbe_layer *layer = fh->layer;
  1159. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1160. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1161. "VIDIOC_QUERYBUF, layer id = %d\n",
  1162. layer->device_id);
  1163. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
  1164. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1165. return -EINVAL;
  1166. }
  1167. /* Call vb2_querybuf to get information */
  1168. return vb2_querybuf(&layer->buffer_queue, buf);
  1169. }
  1170. static int vpbe_display_reqbufs(struct file *file, void *priv,
  1171. struct v4l2_requestbuffers *req_buf)
  1172. {
  1173. struct vpbe_fh *fh = file->private_data;
  1174. struct vpbe_layer *layer = fh->layer;
  1175. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1176. struct vb2_queue *q;
  1177. int ret;
  1178. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
  1179. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
  1180. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1181. return -EINVAL;
  1182. }
  1183. /* If io users of the layer is not zero, return error */
  1184. if (0 != layer->io_usrs) {
  1185. v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
  1186. return -EBUSY;
  1187. }
  1188. /* Initialize videobuf queue as per the buffer type */
  1189. layer->alloc_ctx = vb2_dma_contig_init_ctx(vpbe_dev->pdev);
  1190. if (IS_ERR(layer->alloc_ctx)) {
  1191. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to get the context\n");
  1192. return PTR_ERR(layer->alloc_ctx);
  1193. }
  1194. q = &layer->buffer_queue;
  1195. memset(q, 0, sizeof(*q));
  1196. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1197. q->io_modes = VB2_MMAP | VB2_USERPTR;
  1198. q->drv_priv = fh;
  1199. q->ops = &video_qops;
  1200. q->mem_ops = &vb2_dma_contig_memops;
  1201. q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
  1202. ret = vb2_queue_init(q);
  1203. if (ret) {
  1204. v4l2_err(&vpbe_dev->v4l2_dev, "vb2_queue_init() failed\n");
  1205. vb2_dma_contig_cleanup_ctx(layer->alloc_ctx);
  1206. return ret;
  1207. }
  1208. /* Set io allowed member of file handle to TRUE */
  1209. fh->io_allowed = 1;
  1210. /* Increment io usrs member of layer object to 1 */
  1211. layer->io_usrs = 1;
  1212. /* Store type of memory requested in layer object */
  1213. layer->memory = req_buf->memory;
  1214. /* Initialize buffer queue */
  1215. INIT_LIST_HEAD(&layer->dma_queue);
  1216. /* Allocate buffers */
  1217. return vb2_reqbufs(q, req_buf);
  1218. }
  1219. /*
  1220. * vpbe_display_mmap()
  1221. * It is used to map kernel space buffers into user spaces
  1222. */
  1223. static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
  1224. {
  1225. /* Get the layer object and file handle object */
  1226. struct vpbe_fh *fh = filep->private_data;
  1227. struct vpbe_layer *layer = fh->layer;
  1228. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1229. int ret;
  1230. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
  1231. if (mutex_lock_interruptible(&layer->opslock))
  1232. return -ERESTARTSYS;
  1233. ret = vb2_mmap(&layer->buffer_queue, vma);
  1234. mutex_unlock(&layer->opslock);
  1235. return ret;
  1236. }
  1237. /* vpbe_display_poll(): It is used for select/poll system call
  1238. */
  1239. static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
  1240. {
  1241. struct vpbe_fh *fh = filep->private_data;
  1242. struct vpbe_layer *layer = fh->layer;
  1243. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1244. unsigned int err = 0;
  1245. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
  1246. if (layer->started) {
  1247. mutex_lock(&layer->opslock);
  1248. err = vb2_poll(&layer->buffer_queue, filep, wait);
  1249. mutex_unlock(&layer->opslock);
  1250. }
  1251. return err;
  1252. }
  1253. /*
  1254. * vpbe_display_open()
  1255. * It creates object of file handle structure and stores it in private_data
  1256. * member of filepointer
  1257. */
  1258. static int vpbe_display_open(struct file *file)
  1259. {
  1260. struct vpbe_fh *fh = NULL;
  1261. struct vpbe_layer *layer = video_drvdata(file);
  1262. struct vpbe_display *disp_dev = layer->disp_dev;
  1263. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1264. struct osd_state *osd_device = disp_dev->osd_device;
  1265. int err;
  1266. /* Allocate memory for the file handle object */
  1267. fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
  1268. if (fh == NULL) {
  1269. v4l2_err(&vpbe_dev->v4l2_dev,
  1270. "unable to allocate memory for file handle object\n");
  1271. return -ENOMEM;
  1272. }
  1273. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1274. "vpbe display open plane = %d\n",
  1275. layer->device_id);
  1276. /* store pointer to fh in private_data member of filep */
  1277. file->private_data = fh;
  1278. fh->layer = layer;
  1279. fh->disp_dev = disp_dev;
  1280. if (!layer->usrs) {
  1281. if (mutex_lock_interruptible(&layer->opslock))
  1282. return -ERESTARTSYS;
  1283. /* First claim the layer for this device */
  1284. err = osd_device->ops.request_layer(osd_device,
  1285. layer->layer_info.id);
  1286. mutex_unlock(&layer->opslock);
  1287. if (err < 0) {
  1288. /* Couldn't get layer */
  1289. v4l2_err(&vpbe_dev->v4l2_dev,
  1290. "Display Manager failed to allocate layer\n");
  1291. kfree(fh);
  1292. return -EINVAL;
  1293. }
  1294. }
  1295. /* Increment layer usrs counter */
  1296. layer->usrs++;
  1297. /* Set io_allowed member to false */
  1298. fh->io_allowed = 0;
  1299. /* Initialize priority of this instance to default priority */
  1300. fh->prio = V4L2_PRIORITY_UNSET;
  1301. v4l2_prio_open(&layer->prio, &fh->prio);
  1302. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1303. "vpbe display device opened successfully\n");
  1304. return 0;
  1305. }
  1306. /*
  1307. * vpbe_display_release()
  1308. * This function deletes buffer queue, frees the buffers and the davinci
  1309. * display file * handle
  1310. */
  1311. static int vpbe_display_release(struct file *file)
  1312. {
  1313. /* Get the layer object and file handle object */
  1314. struct vpbe_fh *fh = file->private_data;
  1315. struct vpbe_layer *layer = fh->layer;
  1316. struct osd_layer_config *cfg = &layer->layer_info.config;
  1317. struct vpbe_display *disp_dev = fh->disp_dev;
  1318. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1319. struct osd_state *osd_device = disp_dev->osd_device;
  1320. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
  1321. mutex_lock(&layer->opslock);
  1322. /* if this instance is doing IO */
  1323. if (fh->io_allowed) {
  1324. /* Reset io_usrs member of layer object */
  1325. layer->io_usrs = 0;
  1326. osd_device->ops.disable_layer(osd_device,
  1327. layer->layer_info.id);
  1328. layer->started = 0;
  1329. /* Free buffers allocated */
  1330. vb2_queue_release(&layer->buffer_queue);
  1331. vb2_dma_contig_cleanup_ctx(&layer->buffer_queue);
  1332. }
  1333. /* Decrement layer usrs counter */
  1334. layer->usrs--;
  1335. /* If this file handle has initialize encoder device, reset it */
  1336. if (!layer->usrs) {
  1337. if (cfg->pixfmt == PIXFMT_NV12) {
  1338. struct vpbe_layer *otherlayer;
  1339. otherlayer =
  1340. _vpbe_display_get_other_win_layer(disp_dev, layer);
  1341. osd_device->ops.disable_layer(osd_device,
  1342. otherlayer->layer_info.id);
  1343. osd_device->ops.release_layer(osd_device,
  1344. otherlayer->layer_info.id);
  1345. }
  1346. osd_device->ops.disable_layer(osd_device,
  1347. layer->layer_info.id);
  1348. osd_device->ops.release_layer(osd_device,
  1349. layer->layer_info.id);
  1350. }
  1351. /* Close the priority */
  1352. v4l2_prio_close(&layer->prio, fh->prio);
  1353. file->private_data = NULL;
  1354. mutex_unlock(&layer->opslock);
  1355. /* Free memory allocated to file handle object */
  1356. kfree(fh);
  1357. disp_dev->cbcr_ofst = 0;
  1358. return 0;
  1359. }
  1360. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1361. static int vpbe_display_g_register(struct file *file, void *priv,
  1362. struct v4l2_dbg_register *reg)
  1363. {
  1364. struct v4l2_dbg_match *match = &reg->match;
  1365. struct vpbe_fh *fh = file->private_data;
  1366. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1367. if (match->type >= 2) {
  1368. v4l2_subdev_call(vpbe_dev->venc,
  1369. core,
  1370. g_register,
  1371. reg);
  1372. }
  1373. return 0;
  1374. }
  1375. static int vpbe_display_s_register(struct file *file, void *priv,
  1376. struct v4l2_dbg_register *reg)
  1377. {
  1378. return 0;
  1379. }
  1380. #endif
  1381. /* vpbe capture ioctl operations */
  1382. static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
  1383. .vidioc_querycap = vpbe_display_querycap,
  1384. .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
  1385. .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
  1386. .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
  1387. .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
  1388. .vidioc_reqbufs = vpbe_display_reqbufs,
  1389. .vidioc_querybuf = vpbe_display_querybuf,
  1390. .vidioc_qbuf = vpbe_display_qbuf,
  1391. .vidioc_dqbuf = vpbe_display_dqbuf,
  1392. .vidioc_streamon = vpbe_display_streamon,
  1393. .vidioc_streamoff = vpbe_display_streamoff,
  1394. .vidioc_cropcap = vpbe_display_cropcap,
  1395. .vidioc_g_crop = vpbe_display_g_crop,
  1396. .vidioc_s_crop = vpbe_display_s_crop,
  1397. .vidioc_g_priority = vpbe_display_g_priority,
  1398. .vidioc_s_priority = vpbe_display_s_priority,
  1399. .vidioc_s_std = vpbe_display_s_std,
  1400. .vidioc_g_std = vpbe_display_g_std,
  1401. .vidioc_enum_output = vpbe_display_enum_output,
  1402. .vidioc_s_output = vpbe_display_s_output,
  1403. .vidioc_g_output = vpbe_display_g_output,
  1404. .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
  1405. .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
  1406. .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
  1407. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1408. .vidioc_g_register = vpbe_display_g_register,
  1409. .vidioc_s_register = vpbe_display_s_register,
  1410. #endif
  1411. };
  1412. static struct v4l2_file_operations vpbe_fops = {
  1413. .owner = THIS_MODULE,
  1414. .open = vpbe_display_open,
  1415. .release = vpbe_display_release,
  1416. .unlocked_ioctl = video_ioctl2,
  1417. .mmap = vpbe_display_mmap,
  1418. .poll = vpbe_display_poll
  1419. };
  1420. static int vpbe_device_get(struct device *dev, void *data)
  1421. {
  1422. struct platform_device *pdev = to_platform_device(dev);
  1423. struct vpbe_display *vpbe_disp = data;
  1424. if (strcmp("vpbe_controller", pdev->name) == 0)
  1425. vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
  1426. if (strstr(pdev->name, "vpbe-osd") != NULL)
  1427. vpbe_disp->osd_device = platform_get_drvdata(pdev);
  1428. return 0;
  1429. }
  1430. static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
  1431. struct platform_device *pdev)
  1432. {
  1433. struct vpbe_layer *vpbe_display_layer = NULL;
  1434. struct video_device *vbd = NULL;
  1435. /* Allocate memory for four plane display objects */
  1436. disp_dev->dev[i] =
  1437. kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
  1438. /* If memory allocation fails, return error */
  1439. if (!disp_dev->dev[i]) {
  1440. printk(KERN_ERR "ran out of memory\n");
  1441. return -ENOMEM;
  1442. }
  1443. spin_lock_init(&disp_dev->dev[i]->irqlock);
  1444. mutex_init(&disp_dev->dev[i]->opslock);
  1445. /* Get the pointer to the layer object */
  1446. vpbe_display_layer = disp_dev->dev[i];
  1447. vbd = &vpbe_display_layer->video_dev;
  1448. /* Initialize field of video device */
  1449. vbd->release = video_device_release_empty;
  1450. vbd->fops = &vpbe_fops;
  1451. vbd->ioctl_ops = &vpbe_ioctl_ops;
  1452. vbd->minor = -1;
  1453. vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1454. vbd->lock = &vpbe_display_layer->opslock;
  1455. vbd->vfl_dir = VFL_DIR_TX;
  1456. if (disp_dev->vpbe_dev->current_timings.timings_type &
  1457. VPBE_ENC_STD) {
  1458. vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
  1459. vbd->current_norm =
  1460. disp_dev->vpbe_dev->current_timings.std_id;
  1461. } else
  1462. vbd->current_norm = 0;
  1463. snprintf(vbd->name, sizeof(vbd->name),
  1464. "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
  1465. (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1466. (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1467. (VPBE_DISPLAY_VERSION_CODE) & 0xff);
  1468. vpbe_display_layer->device_id = i;
  1469. vpbe_display_layer->layer_info.id =
  1470. ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
  1471. /* Initialize prio member of layer object */
  1472. v4l2_prio_init(&vpbe_display_layer->prio);
  1473. return 0;
  1474. }
  1475. static int register_device(struct vpbe_layer *vpbe_display_layer,
  1476. struct vpbe_display *disp_dev,
  1477. struct platform_device *pdev)
  1478. {
  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 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 = 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");