vpbe_display.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  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. return 0;
  1009. }
  1010. /**
  1011. * vpbe_display_g_dv_timings - Set the dv timings
  1012. *
  1013. * Get the timings in the current encoder. Return the status. 0 - success
  1014. * -EINVAL on error
  1015. */
  1016. static int
  1017. vpbe_display_g_dv_timings(struct file *file, void *priv,
  1018. struct v4l2_dv_timings *dv_timings)
  1019. {
  1020. struct vpbe_fh *fh = priv;
  1021. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1022. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
  1023. /* Get the given standard in the encoder */
  1024. if (vpbe_dev->current_timings.timings_type &
  1025. VPBE_ENC_DV_TIMINGS) {
  1026. *dv_timings = vpbe_dev->current_timings.dv_timings;
  1027. } else {
  1028. return -EINVAL;
  1029. }
  1030. return 0;
  1031. }
  1032. static int vpbe_display_streamoff(struct file *file, void *priv,
  1033. enum v4l2_buf_type buf_type)
  1034. {
  1035. struct vpbe_fh *fh = file->private_data;
  1036. struct vpbe_layer *layer = fh->layer;
  1037. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1038. struct osd_state *osd_device = fh->disp_dev->osd_device;
  1039. int ret;
  1040. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1041. "VIDIOC_STREAMOFF,layer id = %d\n",
  1042. layer->device_id);
  1043. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
  1044. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1045. return -EINVAL;
  1046. }
  1047. /* If io is allowed for this file handle, return error */
  1048. if (!fh->io_allowed) {
  1049. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1050. return -EACCES;
  1051. }
  1052. /* If streaming is not started, return error */
  1053. if (!layer->started) {
  1054. v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
  1055. " id = %d\n", layer->device_id);
  1056. return -EINVAL;
  1057. }
  1058. osd_device->ops.disable_layer(osd_device,
  1059. layer->layer_info.id);
  1060. layer->started = 0;
  1061. ret = vb2_streamoff(&layer->buffer_queue, buf_type);
  1062. return ret;
  1063. }
  1064. static int vpbe_display_streamon(struct file *file, void *priv,
  1065. enum v4l2_buf_type buf_type)
  1066. {
  1067. struct vpbe_fh *fh = file->private_data;
  1068. struct vpbe_layer *layer = fh->layer;
  1069. struct vpbe_display *disp_dev = fh->disp_dev;
  1070. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1071. struct osd_state *osd_device = disp_dev->osd_device;
  1072. int ret;
  1073. osd_device->ops.disable_layer(osd_device,
  1074. layer->layer_info.id);
  1075. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
  1076. layer->device_id);
  1077. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
  1078. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1079. return -EINVAL;
  1080. }
  1081. /* If file handle is not allowed IO, return error */
  1082. if (!fh->io_allowed) {
  1083. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1084. return -EACCES;
  1085. }
  1086. /* If Streaming is already started, return error */
  1087. if (layer->started) {
  1088. v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
  1089. return -EBUSY;
  1090. }
  1091. /*
  1092. * Call vb2_streamon to start streaming
  1093. * in videobuf
  1094. */
  1095. ret = vb2_streamon(&layer->buffer_queue, buf_type);
  1096. if (ret) {
  1097. v4l2_err(&vpbe_dev->v4l2_dev,
  1098. "error in vb2_streamon\n");
  1099. return ret;
  1100. }
  1101. return ret;
  1102. }
  1103. static int vpbe_display_dqbuf(struct file *file, void *priv,
  1104. struct v4l2_buffer *buf)
  1105. {
  1106. struct vpbe_fh *fh = file->private_data;
  1107. struct vpbe_layer *layer = fh->layer;
  1108. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1109. int ret;
  1110. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1111. "VIDIOC_DQBUF, layer id = %d\n",
  1112. layer->device_id);
  1113. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
  1114. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1115. return -EINVAL;
  1116. }
  1117. /* If this file handle is not allowed to do IO, return error */
  1118. if (!fh->io_allowed) {
  1119. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1120. return -EACCES;
  1121. }
  1122. if (file->f_flags & O_NONBLOCK)
  1123. /* Call videobuf_dqbuf for non blocking mode */
  1124. ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
  1125. else
  1126. /* Call videobuf_dqbuf for blocking mode */
  1127. ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
  1128. return ret;
  1129. }
  1130. static int vpbe_display_qbuf(struct file *file, void *priv,
  1131. struct v4l2_buffer *p)
  1132. {
  1133. struct vpbe_fh *fh = file->private_data;
  1134. struct vpbe_layer *layer = fh->layer;
  1135. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1136. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1137. "VIDIOC_QBUF, layer id = %d\n",
  1138. layer->device_id);
  1139. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
  1140. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1141. return -EINVAL;
  1142. }
  1143. /* If this file handle is not allowed to do IO, return error */
  1144. if (!fh->io_allowed) {
  1145. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1146. return -EACCES;
  1147. }
  1148. return vb2_qbuf(&layer->buffer_queue, p);
  1149. }
  1150. static int vpbe_display_querybuf(struct file *file, void *priv,
  1151. struct v4l2_buffer *buf)
  1152. {
  1153. struct vpbe_fh *fh = file->private_data;
  1154. struct vpbe_layer *layer = fh->layer;
  1155. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1156. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1157. "VIDIOC_QUERYBUF, layer id = %d\n",
  1158. layer->device_id);
  1159. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
  1160. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1161. return -EINVAL;
  1162. }
  1163. /* Call vb2_querybuf to get information */
  1164. return vb2_querybuf(&layer->buffer_queue, buf);
  1165. }
  1166. static int vpbe_display_reqbufs(struct file *file, void *priv,
  1167. struct v4l2_requestbuffers *req_buf)
  1168. {
  1169. struct vpbe_fh *fh = file->private_data;
  1170. struct vpbe_layer *layer = fh->layer;
  1171. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1172. struct vb2_queue *q;
  1173. int ret;
  1174. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
  1175. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
  1176. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1177. return -EINVAL;
  1178. }
  1179. /* If io users of the layer is not zero, return error */
  1180. if (0 != layer->io_usrs) {
  1181. v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
  1182. return -EBUSY;
  1183. }
  1184. /* Initialize videobuf queue as per the buffer type */
  1185. layer->alloc_ctx = vb2_dma_contig_init_ctx(vpbe_dev->pdev);
  1186. if (IS_ERR(layer->alloc_ctx)) {
  1187. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to get the context\n");
  1188. return PTR_ERR(layer->alloc_ctx);
  1189. }
  1190. q = &layer->buffer_queue;
  1191. memset(q, 0, sizeof(*q));
  1192. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1193. q->io_modes = VB2_MMAP | VB2_USERPTR;
  1194. q->drv_priv = fh;
  1195. q->ops = &video_qops;
  1196. q->mem_ops = &vb2_dma_contig_memops;
  1197. q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
  1198. q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1199. ret = vb2_queue_init(q);
  1200. if (ret) {
  1201. v4l2_err(&vpbe_dev->v4l2_dev, "vb2_queue_init() failed\n");
  1202. vb2_dma_contig_cleanup_ctx(layer->alloc_ctx);
  1203. return ret;
  1204. }
  1205. /* Set io allowed member of file handle to TRUE */
  1206. fh->io_allowed = 1;
  1207. /* Increment io usrs member of layer object to 1 */
  1208. layer->io_usrs = 1;
  1209. /* Store type of memory requested in layer object */
  1210. layer->memory = req_buf->memory;
  1211. /* Initialize buffer queue */
  1212. INIT_LIST_HEAD(&layer->dma_queue);
  1213. /* Allocate buffers */
  1214. return vb2_reqbufs(q, req_buf);
  1215. }
  1216. /*
  1217. * vpbe_display_mmap()
  1218. * It is used to map kernel space buffers into user spaces
  1219. */
  1220. static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
  1221. {
  1222. /* Get the layer object and file handle object */
  1223. struct vpbe_fh *fh = filep->private_data;
  1224. struct vpbe_layer *layer = fh->layer;
  1225. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1226. int ret;
  1227. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
  1228. if (mutex_lock_interruptible(&layer->opslock))
  1229. return -ERESTARTSYS;
  1230. ret = vb2_mmap(&layer->buffer_queue, vma);
  1231. mutex_unlock(&layer->opslock);
  1232. return ret;
  1233. }
  1234. /* vpbe_display_poll(): It is used for select/poll system call
  1235. */
  1236. static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
  1237. {
  1238. struct vpbe_fh *fh = filep->private_data;
  1239. struct vpbe_layer *layer = fh->layer;
  1240. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1241. unsigned int err = 0;
  1242. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
  1243. if (layer->started) {
  1244. mutex_lock(&layer->opslock);
  1245. err = vb2_poll(&layer->buffer_queue, filep, wait);
  1246. mutex_unlock(&layer->opslock);
  1247. }
  1248. return err;
  1249. }
  1250. /*
  1251. * vpbe_display_open()
  1252. * It creates object of file handle structure and stores it in private_data
  1253. * member of filepointer
  1254. */
  1255. static int vpbe_display_open(struct file *file)
  1256. {
  1257. struct vpbe_fh *fh = NULL;
  1258. struct vpbe_layer *layer = video_drvdata(file);
  1259. struct vpbe_display *disp_dev = layer->disp_dev;
  1260. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1261. struct osd_state *osd_device = disp_dev->osd_device;
  1262. int err;
  1263. /* Allocate memory for the file handle object */
  1264. fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
  1265. if (fh == NULL) {
  1266. v4l2_err(&vpbe_dev->v4l2_dev,
  1267. "unable to allocate memory for file handle object\n");
  1268. return -ENOMEM;
  1269. }
  1270. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1271. "vpbe display open plane = %d\n",
  1272. layer->device_id);
  1273. /* store pointer to fh in private_data member of filep */
  1274. file->private_data = fh;
  1275. fh->layer = layer;
  1276. fh->disp_dev = disp_dev;
  1277. if (!layer->usrs) {
  1278. if (mutex_lock_interruptible(&layer->opslock))
  1279. return -ERESTARTSYS;
  1280. /* First claim the layer for this device */
  1281. err = osd_device->ops.request_layer(osd_device,
  1282. layer->layer_info.id);
  1283. mutex_unlock(&layer->opslock);
  1284. if (err < 0) {
  1285. /* Couldn't get layer */
  1286. v4l2_err(&vpbe_dev->v4l2_dev,
  1287. "Display Manager failed to allocate layer\n");
  1288. kfree(fh);
  1289. return -EINVAL;
  1290. }
  1291. }
  1292. /* Increment layer usrs counter */
  1293. layer->usrs++;
  1294. /* Set io_allowed member to false */
  1295. fh->io_allowed = 0;
  1296. /* Initialize priority of this instance to default priority */
  1297. fh->prio = V4L2_PRIORITY_UNSET;
  1298. v4l2_prio_open(&layer->prio, &fh->prio);
  1299. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1300. "vpbe display device opened successfully\n");
  1301. return 0;
  1302. }
  1303. /*
  1304. * vpbe_display_release()
  1305. * This function deletes buffer queue, frees the buffers and the davinci
  1306. * display file * handle
  1307. */
  1308. static int vpbe_display_release(struct file *file)
  1309. {
  1310. /* Get the layer object and file handle object */
  1311. struct vpbe_fh *fh = file->private_data;
  1312. struct vpbe_layer *layer = fh->layer;
  1313. struct osd_layer_config *cfg = &layer->layer_info.config;
  1314. struct vpbe_display *disp_dev = fh->disp_dev;
  1315. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1316. struct osd_state *osd_device = disp_dev->osd_device;
  1317. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
  1318. mutex_lock(&layer->opslock);
  1319. /* if this instance is doing IO */
  1320. if (fh->io_allowed) {
  1321. /* Reset io_usrs member of layer object */
  1322. layer->io_usrs = 0;
  1323. osd_device->ops.disable_layer(osd_device,
  1324. layer->layer_info.id);
  1325. layer->started = 0;
  1326. /* Free buffers allocated */
  1327. vb2_queue_release(&layer->buffer_queue);
  1328. vb2_dma_contig_cleanup_ctx(&layer->buffer_queue);
  1329. }
  1330. /* Decrement layer usrs counter */
  1331. layer->usrs--;
  1332. /* If this file handle has initialize encoder device, reset it */
  1333. if (!layer->usrs) {
  1334. if (cfg->pixfmt == PIXFMT_NV12) {
  1335. struct vpbe_layer *otherlayer;
  1336. otherlayer =
  1337. _vpbe_display_get_other_win_layer(disp_dev, layer);
  1338. osd_device->ops.disable_layer(osd_device,
  1339. otherlayer->layer_info.id);
  1340. osd_device->ops.release_layer(osd_device,
  1341. otherlayer->layer_info.id);
  1342. }
  1343. osd_device->ops.disable_layer(osd_device,
  1344. layer->layer_info.id);
  1345. osd_device->ops.release_layer(osd_device,
  1346. layer->layer_info.id);
  1347. }
  1348. /* Close the priority */
  1349. v4l2_prio_close(&layer->prio, fh->prio);
  1350. file->private_data = NULL;
  1351. mutex_unlock(&layer->opslock);
  1352. /* Free memory allocated to file handle object */
  1353. kfree(fh);
  1354. disp_dev->cbcr_ofst = 0;
  1355. return 0;
  1356. }
  1357. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1358. static int vpbe_display_g_register(struct file *file, void *priv,
  1359. struct v4l2_dbg_register *reg)
  1360. {
  1361. struct v4l2_dbg_match *match = &reg->match;
  1362. struct vpbe_fh *fh = file->private_data;
  1363. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1364. if (match->type >= 2) {
  1365. v4l2_subdev_call(vpbe_dev->venc,
  1366. core,
  1367. g_register,
  1368. reg);
  1369. }
  1370. return 0;
  1371. }
  1372. static int vpbe_display_s_register(struct file *file, void *priv,
  1373. const struct v4l2_dbg_register *reg)
  1374. {
  1375. return 0;
  1376. }
  1377. #endif
  1378. /* vpbe capture ioctl operations */
  1379. static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
  1380. .vidioc_querycap = vpbe_display_querycap,
  1381. .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
  1382. .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
  1383. .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
  1384. .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
  1385. .vidioc_reqbufs = vpbe_display_reqbufs,
  1386. .vidioc_querybuf = vpbe_display_querybuf,
  1387. .vidioc_qbuf = vpbe_display_qbuf,
  1388. .vidioc_dqbuf = vpbe_display_dqbuf,
  1389. .vidioc_streamon = vpbe_display_streamon,
  1390. .vidioc_streamoff = vpbe_display_streamoff,
  1391. .vidioc_cropcap = vpbe_display_cropcap,
  1392. .vidioc_g_crop = vpbe_display_g_crop,
  1393. .vidioc_s_crop = vpbe_display_s_crop,
  1394. .vidioc_g_priority = vpbe_display_g_priority,
  1395. .vidioc_s_priority = vpbe_display_s_priority,
  1396. .vidioc_s_std = vpbe_display_s_std,
  1397. .vidioc_g_std = vpbe_display_g_std,
  1398. .vidioc_enum_output = vpbe_display_enum_output,
  1399. .vidioc_s_output = vpbe_display_s_output,
  1400. .vidioc_g_output = vpbe_display_g_output,
  1401. .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
  1402. .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
  1403. .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
  1404. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1405. .vidioc_g_register = vpbe_display_g_register,
  1406. .vidioc_s_register = vpbe_display_s_register,
  1407. #endif
  1408. };
  1409. static struct v4l2_file_operations vpbe_fops = {
  1410. .owner = THIS_MODULE,
  1411. .open = vpbe_display_open,
  1412. .release = vpbe_display_release,
  1413. .unlocked_ioctl = video_ioctl2,
  1414. .mmap = vpbe_display_mmap,
  1415. .poll = vpbe_display_poll
  1416. };
  1417. static int vpbe_device_get(struct device *dev, void *data)
  1418. {
  1419. struct platform_device *pdev = to_platform_device(dev);
  1420. struct vpbe_display *vpbe_disp = data;
  1421. if (strcmp("vpbe_controller", pdev->name) == 0)
  1422. vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
  1423. if (strstr(pdev->name, "vpbe-osd") != NULL)
  1424. vpbe_disp->osd_device = platform_get_drvdata(pdev);
  1425. return 0;
  1426. }
  1427. static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
  1428. struct platform_device *pdev)
  1429. {
  1430. struct vpbe_layer *vpbe_display_layer = NULL;
  1431. struct video_device *vbd = NULL;
  1432. /* Allocate memory for four plane display objects */
  1433. disp_dev->dev[i] =
  1434. kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
  1435. /* If memory allocation fails, return error */
  1436. if (!disp_dev->dev[i]) {
  1437. printk(KERN_ERR "ran out of memory\n");
  1438. return -ENOMEM;
  1439. }
  1440. spin_lock_init(&disp_dev->dev[i]->irqlock);
  1441. mutex_init(&disp_dev->dev[i]->opslock);
  1442. /* Get the pointer to the layer object */
  1443. vpbe_display_layer = disp_dev->dev[i];
  1444. vbd = &vpbe_display_layer->video_dev;
  1445. /* Initialize field of video device */
  1446. vbd->release = video_device_release_empty;
  1447. vbd->fops = &vpbe_fops;
  1448. vbd->ioctl_ops = &vpbe_ioctl_ops;
  1449. vbd->minor = -1;
  1450. vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1451. vbd->lock = &vpbe_display_layer->opslock;
  1452. vbd->vfl_dir = VFL_DIR_TX;
  1453. if (disp_dev->vpbe_dev->current_timings.timings_type &
  1454. VPBE_ENC_STD)
  1455. vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
  1456. snprintf(vbd->name, sizeof(vbd->name),
  1457. "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
  1458. (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1459. (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1460. (VPBE_DISPLAY_VERSION_CODE) & 0xff);
  1461. vpbe_display_layer->device_id = i;
  1462. vpbe_display_layer->layer_info.id =
  1463. ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
  1464. /* Initialize prio member of layer object */
  1465. v4l2_prio_init(&vpbe_display_layer->prio);
  1466. return 0;
  1467. }
  1468. static int register_device(struct vpbe_layer *vpbe_display_layer,
  1469. struct vpbe_display *disp_dev,
  1470. struct platform_device *pdev)
  1471. {
  1472. int err;
  1473. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1474. "Trying to register VPBE display device.\n");
  1475. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1476. "layer=%x,layer->video_dev=%x\n",
  1477. (int)vpbe_display_layer,
  1478. (int)&vpbe_display_layer->video_dev);
  1479. err = video_register_device(&vpbe_display_layer->video_dev,
  1480. VFL_TYPE_GRABBER,
  1481. -1);
  1482. if (err)
  1483. return -ENODEV;
  1484. vpbe_display_layer->disp_dev = disp_dev;
  1485. /* set the driver data in platform device */
  1486. platform_set_drvdata(pdev, disp_dev);
  1487. video_set_drvdata(&vpbe_display_layer->video_dev,
  1488. vpbe_display_layer);
  1489. return 0;
  1490. }
  1491. /*
  1492. * vpbe_display_probe()
  1493. * This function creates device entries by register itself to the V4L2 driver
  1494. * and initializes fields of each layer objects
  1495. */
  1496. static int vpbe_display_probe(struct platform_device *pdev)
  1497. {
  1498. struct vpbe_layer *vpbe_display_layer;
  1499. struct vpbe_display *disp_dev;
  1500. struct resource *res = NULL;
  1501. int k;
  1502. int i;
  1503. int err;
  1504. int irq;
  1505. printk(KERN_DEBUG "vpbe_display_probe\n");
  1506. /* Allocate memory for vpbe_display */
  1507. disp_dev = kzalloc(sizeof(struct vpbe_display), GFP_KERNEL);
  1508. if (!disp_dev) {
  1509. printk(KERN_ERR "ran out of memory\n");
  1510. return -ENOMEM;
  1511. }
  1512. spin_lock_init(&disp_dev->dma_queue_lock);
  1513. /*
  1514. * Scan all the platform devices to find the vpbe
  1515. * controller device and get the vpbe_dev object
  1516. */
  1517. err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
  1518. vpbe_device_get);
  1519. if (err < 0)
  1520. return err;
  1521. /* Initialize the vpbe display controller */
  1522. if (NULL != disp_dev->vpbe_dev->ops.initialize) {
  1523. err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
  1524. disp_dev->vpbe_dev);
  1525. if (err) {
  1526. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1527. "Error initing vpbe\n");
  1528. err = -ENOMEM;
  1529. goto probe_out;
  1530. }
  1531. }
  1532. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1533. if (init_vpbe_layer(i, disp_dev, pdev)) {
  1534. err = -ENODEV;
  1535. goto probe_out;
  1536. }
  1537. }
  1538. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1539. if (!res) {
  1540. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1541. "Unable to get VENC interrupt resource\n");
  1542. err = -ENODEV;
  1543. goto probe_out;
  1544. }
  1545. irq = res->start;
  1546. if (request_irq(irq, venc_isr, IRQF_DISABLED, VPBE_DISPLAY_DRIVER,
  1547. disp_dev)) {
  1548. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1549. "Unable to request interrupt\n");
  1550. err = -ENODEV;
  1551. goto probe_out;
  1552. }
  1553. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1554. if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
  1555. err = -ENODEV;
  1556. goto probe_out_irq;
  1557. }
  1558. }
  1559. printk(KERN_DEBUG "Successfully completed the probing of vpbe v4l2 device\n");
  1560. return 0;
  1561. probe_out_irq:
  1562. free_irq(res->start, disp_dev);
  1563. probe_out:
  1564. for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
  1565. /* Get the pointer to the layer object */
  1566. vpbe_display_layer = disp_dev->dev[k];
  1567. /* Unregister video device */
  1568. if (vpbe_display_layer) {
  1569. video_unregister_device(
  1570. &vpbe_display_layer->video_dev);
  1571. kfree(disp_dev->dev[k]);
  1572. }
  1573. }
  1574. kfree(disp_dev);
  1575. return err;
  1576. }
  1577. /*
  1578. * vpbe_display_remove()
  1579. * It un-register hardware layer from V4L2 driver
  1580. */
  1581. static int vpbe_display_remove(struct platform_device *pdev)
  1582. {
  1583. struct vpbe_layer *vpbe_display_layer;
  1584. struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
  1585. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1586. struct resource *res;
  1587. int i;
  1588. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
  1589. /* unregister irq */
  1590. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1591. free_irq(res->start, disp_dev);
  1592. /* deinitialize the vpbe display controller */
  1593. if (NULL != vpbe_dev->ops.deinitialize)
  1594. vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
  1595. /* un-register device */
  1596. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1597. /* Get the pointer to the layer object */
  1598. vpbe_display_layer = disp_dev->dev[i];
  1599. /* Unregister video device */
  1600. video_unregister_device(&vpbe_display_layer->video_dev);
  1601. }
  1602. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1603. kfree(disp_dev->dev[i]);
  1604. disp_dev->dev[i] = NULL;
  1605. }
  1606. return 0;
  1607. }
  1608. static struct platform_driver vpbe_display_driver = {
  1609. .driver = {
  1610. .name = VPBE_DISPLAY_DRIVER,
  1611. .owner = THIS_MODULE,
  1612. .bus = &platform_bus_type,
  1613. },
  1614. .probe = vpbe_display_probe,
  1615. .remove = vpbe_display_remove,
  1616. };
  1617. module_platform_driver(vpbe_display_driver);
  1618. MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
  1619. MODULE_LICENSE("GPL");
  1620. MODULE_AUTHOR("Texas Instruments");