vpbe_display.c 50 KB

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