vpbe_display.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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. if (layer->cur_frm == layer->next_frm)
  65. return;
  66. layer->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
  67. vb2_buffer_done(&layer->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
  68. /* Make cur_frm pointing to next_frm */
  69. layer->cur_frm = layer->next_frm;
  70. }
  71. static void vpbe_isr_odd_field(struct vpbe_display *disp_obj,
  72. struct vpbe_layer *layer)
  73. {
  74. struct osd_state *osd_device = disp_obj->osd_device;
  75. unsigned long addr;
  76. spin_lock(&disp_obj->dma_queue_lock);
  77. if (list_empty(&layer->dma_queue) ||
  78. (layer->cur_frm != layer->next_frm)) {
  79. spin_unlock(&disp_obj->dma_queue_lock);
  80. return;
  81. }
  82. /*
  83. * one field is displayed configure
  84. * the next frame if it is available
  85. * otherwise hold on current frame
  86. * Get next from the buffer queue
  87. */
  88. layer->next_frm = list_entry(layer->dma_queue.next,
  89. struct vpbe_disp_buffer, list);
  90. /* Remove that from the buffer queue */
  91. list_del(&layer->next_frm->list);
  92. spin_unlock(&disp_obj->dma_queue_lock);
  93. /* Mark state of the frame to active */
  94. layer->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
  95. addr = vb2_dma_contig_plane_dma_addr(&layer->next_frm->vb.vb2_buf, 0);
  96. osd_device->ops.start_layer(osd_device,
  97. layer->layer_info.id,
  98. addr,
  99. disp_obj->cbcr_ofst);
  100. }
  101. /* interrupt service routine */
  102. static irqreturn_t venc_isr(int irq, void *arg)
  103. {
  104. struct vpbe_display *disp_dev = (struct vpbe_display *)arg;
  105. struct vpbe_layer *layer;
  106. static unsigned last_event;
  107. unsigned event = 0;
  108. int fid;
  109. int i;
  110. if ((NULL == arg) || (NULL == disp_dev->dev[0]))
  111. return IRQ_HANDLED;
  112. if (venc_is_second_field(disp_dev))
  113. event |= VENC_SECOND_FIELD;
  114. else
  115. event |= VENC_FIRST_FIELD;
  116. if (event == (last_event & ~VENC_END_OF_FRAME)) {
  117. /*
  118. * If the display is non-interlaced, then we need to flag the
  119. * end-of-frame event at every interrupt regardless of the
  120. * value of the FIDST bit. We can conclude that the display is
  121. * non-interlaced if the value of the FIDST bit is unchanged
  122. * from the previous interrupt.
  123. */
  124. event |= VENC_END_OF_FRAME;
  125. } else if (event == VENC_SECOND_FIELD) {
  126. /* end-of-frame for interlaced display */
  127. event |= VENC_END_OF_FRAME;
  128. }
  129. last_event = event;
  130. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  131. layer = disp_dev->dev[i];
  132. if (!vb2_start_streaming_called(&layer->buffer_queue))
  133. continue;
  134. if (layer->layer_first_int) {
  135. layer->layer_first_int = 0;
  136. continue;
  137. }
  138. /* Check the field format */
  139. if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
  140. (event & VENC_END_OF_FRAME)) {
  141. /* Progressive mode */
  142. vpbe_isr_even_field(disp_dev, layer);
  143. vpbe_isr_odd_field(disp_dev, layer);
  144. } else {
  145. /* Interlaced mode */
  146. layer->field_id ^= 1;
  147. if (event & VENC_FIRST_FIELD)
  148. fid = 0;
  149. else
  150. fid = 1;
  151. /*
  152. * If field id does not match with store
  153. * field id
  154. */
  155. if (fid != layer->field_id) {
  156. /* Make them in sync */
  157. layer->field_id = fid;
  158. continue;
  159. }
  160. /*
  161. * device field id and local field id are
  162. * in sync. If this is even field
  163. */
  164. if (0 == fid)
  165. vpbe_isr_even_field(disp_dev, layer);
  166. else /* odd field */
  167. vpbe_isr_odd_field(disp_dev, layer);
  168. }
  169. }
  170. return IRQ_HANDLED;
  171. }
  172. /*
  173. * vpbe_buffer_prepare()
  174. * This is the callback function called from vb2_qbuf() function
  175. * the buffer is prepared and user space virtual address is converted into
  176. * physical address
  177. */
  178. static int vpbe_buffer_prepare(struct vb2_buffer *vb)
  179. {
  180. struct vb2_queue *q = vb->vb2_queue;
  181. struct vpbe_layer *layer = vb2_get_drv_priv(q);
  182. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  183. unsigned long addr;
  184. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  185. "vpbe_buffer_prepare\n");
  186. vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
  187. if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  188. return -EINVAL;
  189. addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  190. if (!IS_ALIGNED(addr, 8)) {
  191. v4l2_err(&vpbe_dev->v4l2_dev,
  192. "buffer_prepare:offset is not aligned to 32 bytes\n");
  193. return -EINVAL;
  194. }
  195. return 0;
  196. }
  197. /*
  198. * vpbe_buffer_setup()
  199. * This function allocates memory for the buffers
  200. */
  201. static int
  202. vpbe_buffer_queue_setup(struct vb2_queue *vq,
  203. unsigned int *nbuffers, unsigned int *nplanes,
  204. unsigned int sizes[], struct device *alloc_devs[])
  205. {
  206. /* Get the file handle object and layer object */
  207. struct vpbe_layer *layer = vb2_get_drv_priv(vq);
  208. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  209. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
  210. /* Store number of buffers allocated in numbuffer member */
  211. if (vq->num_buffers + *nbuffers < VPBE_DEFAULT_NUM_BUFS)
  212. *nbuffers = VPBE_DEFAULT_NUM_BUFS - vq->num_buffers;
  213. if (*nplanes)
  214. return sizes[0] < layer->pix_fmt.sizeimage ? -EINVAL : 0;
  215. *nplanes = 1;
  216. sizes[0] = layer->pix_fmt.sizeimage;
  217. return 0;
  218. }
  219. /*
  220. * vpbe_buffer_queue()
  221. * This function adds the buffer to DMA queue
  222. */
  223. static void vpbe_buffer_queue(struct vb2_buffer *vb)
  224. {
  225. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  226. /* Get the file handle object and layer object */
  227. struct vpbe_disp_buffer *buf = container_of(vbuf,
  228. struct vpbe_disp_buffer, vb);
  229. struct vpbe_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
  230. struct vpbe_display *disp = layer->disp_dev;
  231. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  232. unsigned long flags;
  233. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  234. "vpbe_buffer_queue\n");
  235. /* add the buffer to the DMA queue */
  236. spin_lock_irqsave(&disp->dma_queue_lock, flags);
  237. list_add_tail(&buf->list, &layer->dma_queue);
  238. spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
  239. }
  240. static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
  241. {
  242. struct vpbe_layer *layer = vb2_get_drv_priv(vq);
  243. struct osd_state *osd_device = layer->disp_dev->osd_device;
  244. int ret;
  245. osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
  246. /* Get the next frame from the buffer queue */
  247. layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
  248. struct vpbe_disp_buffer, list);
  249. /* Remove buffer from the buffer queue */
  250. list_del(&layer->cur_frm->list);
  251. /* Mark state of the current frame to active */
  252. layer->cur_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
  253. /* Initialize field_id and started member */
  254. layer->field_id = 0;
  255. /* Set parameters in OSD and VENC */
  256. ret = vpbe_set_osd_display_params(layer->disp_dev, layer);
  257. if (ret < 0) {
  258. struct vpbe_disp_buffer *buf, *tmp;
  259. vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
  260. VB2_BUF_STATE_QUEUED);
  261. list_for_each_entry_safe(buf, tmp, &layer->dma_queue, list) {
  262. list_del(&buf->list);
  263. vb2_buffer_done(&buf->vb.vb2_buf,
  264. VB2_BUF_STATE_QUEUED);
  265. }
  266. return ret;
  267. }
  268. /*
  269. * if request format is yuv420 semiplanar, need to
  270. * enable both video windows
  271. */
  272. layer->layer_first_int = 1;
  273. return ret;
  274. }
  275. static void vpbe_stop_streaming(struct vb2_queue *vq)
  276. {
  277. struct vpbe_layer *layer = vb2_get_drv_priv(vq);
  278. struct osd_state *osd_device = layer->disp_dev->osd_device;
  279. struct vpbe_display *disp = layer->disp_dev;
  280. unsigned long flags;
  281. if (!vb2_is_streaming(vq))
  282. return;
  283. osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
  284. /* release all active buffers */
  285. spin_lock_irqsave(&disp->dma_queue_lock, flags);
  286. if (layer->cur_frm == layer->next_frm) {
  287. vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
  288. VB2_BUF_STATE_ERROR);
  289. } else {
  290. if (layer->cur_frm != NULL)
  291. vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
  292. VB2_BUF_STATE_ERROR);
  293. if (layer->next_frm != NULL)
  294. vb2_buffer_done(&layer->next_frm->vb.vb2_buf,
  295. VB2_BUF_STATE_ERROR);
  296. }
  297. while (!list_empty(&layer->dma_queue)) {
  298. layer->next_frm = list_entry(layer->dma_queue.next,
  299. struct vpbe_disp_buffer, list);
  300. list_del(&layer->next_frm->list);
  301. vb2_buffer_done(&layer->next_frm->vb.vb2_buf,
  302. VB2_BUF_STATE_ERROR);
  303. }
  304. spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
  305. }
  306. static struct vb2_ops video_qops = {
  307. .queue_setup = vpbe_buffer_queue_setup,
  308. .wait_prepare = vb2_ops_wait_prepare,
  309. .wait_finish = vb2_ops_wait_finish,
  310. .buf_prepare = vpbe_buffer_prepare,
  311. .start_streaming = vpbe_start_streaming,
  312. .stop_streaming = vpbe_stop_streaming,
  313. .buf_queue = vpbe_buffer_queue,
  314. };
  315. static
  316. struct vpbe_layer*
  317. _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
  318. struct vpbe_layer *layer)
  319. {
  320. enum vpbe_display_device_id thiswin, otherwin;
  321. thiswin = layer->device_id;
  322. otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
  323. VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
  324. return disp_dev->dev[otherwin];
  325. }
  326. static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
  327. struct vpbe_layer *layer)
  328. {
  329. struct osd_layer_config *cfg = &layer->layer_info.config;
  330. struct osd_state *osd_device = disp_dev->osd_device;
  331. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  332. unsigned long addr;
  333. int ret;
  334. addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb.vb2_buf, 0);
  335. /* Set address in the display registers */
  336. osd_device->ops.start_layer(osd_device,
  337. layer->layer_info.id,
  338. addr,
  339. disp_dev->cbcr_ofst);
  340. ret = osd_device->ops.enable_layer(osd_device,
  341. layer->layer_info.id, 0);
  342. if (ret < 0) {
  343. v4l2_err(&vpbe_dev->v4l2_dev,
  344. "Error in enabling osd window layer 0\n");
  345. return -1;
  346. }
  347. /* Enable the window */
  348. layer->layer_info.enable = 1;
  349. if (cfg->pixfmt == PIXFMT_NV12) {
  350. struct vpbe_layer *otherlayer =
  351. _vpbe_display_get_other_win_layer(disp_dev, layer);
  352. ret = osd_device->ops.enable_layer(osd_device,
  353. otherlayer->layer_info.id, 1);
  354. if (ret < 0) {
  355. v4l2_err(&vpbe_dev->v4l2_dev,
  356. "Error in enabling osd window layer 1\n");
  357. return -1;
  358. }
  359. otherlayer->layer_info.enable = 1;
  360. }
  361. return 0;
  362. }
  363. static void
  364. vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
  365. struct vpbe_layer *layer,
  366. int expected_xsize, int expected_ysize)
  367. {
  368. struct display_layer_info *layer_info = &layer->layer_info;
  369. struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
  370. struct osd_layer_config *cfg = &layer->layer_info.config;
  371. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  372. int calculated_xsize;
  373. int h_exp = 0;
  374. int v_exp = 0;
  375. int h_scale;
  376. int v_scale;
  377. v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
  378. /*
  379. * Application initially set the image format. Current display
  380. * size is obtained from the vpbe display controller. expected_xsize
  381. * and expected_ysize are set through S_SELECTION ioctl. Based on this,
  382. * driver will calculate the scale factors for vertical and
  383. * horizontal direction so that the image is displayed scaled
  384. * and expanded. Application uses expansion to display the image
  385. * in a square pixel. Otherwise it is displayed using displays
  386. * pixel aspect ratio.It is expected that application chooses
  387. * the crop coordinates for cropped or scaled display. if crop
  388. * size is less than the image size, it is displayed cropped or
  389. * it is displayed scaled and/or expanded.
  390. *
  391. * to begin with, set the crop window same as expected. Later we
  392. * will override with scaled window size
  393. */
  394. cfg->xsize = pixfmt->width;
  395. cfg->ysize = pixfmt->height;
  396. layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
  397. layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
  398. layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
  399. layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
  400. if (pixfmt->width < expected_xsize) {
  401. h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
  402. if (h_scale < 2)
  403. h_scale = 1;
  404. else if (h_scale >= 4)
  405. h_scale = 4;
  406. else
  407. h_scale = 2;
  408. cfg->xsize *= h_scale;
  409. if (cfg->xsize < expected_xsize) {
  410. if ((standard_id & V4L2_STD_525_60) ||
  411. (standard_id & V4L2_STD_625_50)) {
  412. calculated_xsize = (cfg->xsize *
  413. VPBE_DISPLAY_H_EXP_RATIO_N) /
  414. VPBE_DISPLAY_H_EXP_RATIO_D;
  415. if (calculated_xsize <= expected_xsize) {
  416. h_exp = 1;
  417. cfg->xsize = calculated_xsize;
  418. }
  419. }
  420. }
  421. if (h_scale == 2)
  422. layer_info->h_zoom = ZOOM_X2;
  423. else if (h_scale == 4)
  424. layer_info->h_zoom = ZOOM_X4;
  425. if (h_exp)
  426. layer_info->h_exp = H_EXP_9_OVER_8;
  427. } else {
  428. /* no scaling, only cropping. Set display area to crop area */
  429. cfg->xsize = expected_xsize;
  430. }
  431. if (pixfmt->height < expected_ysize) {
  432. v_scale = expected_ysize / pixfmt->height;
  433. if (v_scale < 2)
  434. v_scale = 1;
  435. else if (v_scale >= 4)
  436. v_scale = 4;
  437. else
  438. v_scale = 2;
  439. cfg->ysize *= v_scale;
  440. if (cfg->ysize < expected_ysize) {
  441. if ((standard_id & V4L2_STD_625_50)) {
  442. calculated_xsize = (cfg->ysize *
  443. VPBE_DISPLAY_V_EXP_RATIO_N) /
  444. VPBE_DISPLAY_V_EXP_RATIO_D;
  445. if (calculated_xsize <= expected_ysize) {
  446. v_exp = 1;
  447. cfg->ysize = calculated_xsize;
  448. }
  449. }
  450. }
  451. if (v_scale == 2)
  452. layer_info->v_zoom = ZOOM_X2;
  453. else if (v_scale == 4)
  454. layer_info->v_zoom = ZOOM_X4;
  455. if (v_exp)
  456. layer_info->h_exp = V_EXP_6_OVER_5;
  457. } else {
  458. /* no scaling, only cropping. Set display area to crop area */
  459. cfg->ysize = expected_ysize;
  460. }
  461. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  462. "crop display xsize = %d, ysize = %d\n",
  463. cfg->xsize, cfg->ysize);
  464. }
  465. static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
  466. struct vpbe_layer *layer,
  467. int top, int left)
  468. {
  469. struct osd_layer_config *cfg = &layer->layer_info.config;
  470. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  471. cfg->xpos = min((unsigned int)left,
  472. vpbe_dev->current_timings.xres - cfg->xsize);
  473. cfg->ypos = min((unsigned int)top,
  474. vpbe_dev->current_timings.yres - cfg->ysize);
  475. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  476. "new xpos = %d, ypos = %d\n",
  477. cfg->xpos, cfg->ypos);
  478. }
  479. static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
  480. struct v4l2_rect *c)
  481. {
  482. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  483. if ((c->width == 0) ||
  484. ((c->width + c->left) > vpbe_dev->current_timings.xres))
  485. c->width = vpbe_dev->current_timings.xres - c->left;
  486. if ((c->height == 0) || ((c->height + c->top) >
  487. vpbe_dev->current_timings.yres))
  488. c->height = vpbe_dev->current_timings.yres - c->top;
  489. /* window height must be even for interlaced display */
  490. if (vpbe_dev->current_timings.interlaced)
  491. c->height &= (~0x01);
  492. }
  493. /**
  494. * vpbe_try_format()
  495. * If user application provides width and height, and have bytesperline set
  496. * to zero, driver calculates bytesperline and sizeimage based on hardware
  497. * limits.
  498. */
  499. static int vpbe_try_format(struct vpbe_display *disp_dev,
  500. struct v4l2_pix_format *pixfmt, int check)
  501. {
  502. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  503. int min_height = 1;
  504. int min_width = 32;
  505. int max_height;
  506. int max_width;
  507. int bpp;
  508. if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
  509. (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
  510. /* choose default as V4L2_PIX_FMT_UYVY */
  511. pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
  512. /* Check the field format */
  513. if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
  514. (pixfmt->field != V4L2_FIELD_NONE)) {
  515. if (vpbe_dev->current_timings.interlaced)
  516. pixfmt->field = V4L2_FIELD_INTERLACED;
  517. else
  518. pixfmt->field = V4L2_FIELD_NONE;
  519. }
  520. if (pixfmt->field == V4L2_FIELD_INTERLACED)
  521. min_height = 2;
  522. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  523. bpp = 1;
  524. else
  525. bpp = 2;
  526. max_width = vpbe_dev->current_timings.xres;
  527. max_height = vpbe_dev->current_timings.yres;
  528. min_width /= bpp;
  529. if (!pixfmt->width || (pixfmt->width < min_width) ||
  530. (pixfmt->width > max_width)) {
  531. pixfmt->width = vpbe_dev->current_timings.xres;
  532. }
  533. if (!pixfmt->height || (pixfmt->height < min_height) ||
  534. (pixfmt->height > max_height)) {
  535. pixfmt->height = vpbe_dev->current_timings.yres;
  536. }
  537. if (pixfmt->bytesperline < (pixfmt->width * bpp))
  538. pixfmt->bytesperline = pixfmt->width * bpp;
  539. /* Make the bytesperline 32 byte aligned */
  540. pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
  541. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  542. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
  543. (pixfmt->bytesperline * pixfmt->height >> 1);
  544. else
  545. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  546. return 0;
  547. }
  548. static int vpbe_display_querycap(struct file *file, void *priv,
  549. struct v4l2_capability *cap)
  550. {
  551. struct vpbe_layer *layer = video_drvdata(file);
  552. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  553. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  554. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  555. snprintf(cap->driver, sizeof(cap->driver), "%s",
  556. dev_name(vpbe_dev->pdev));
  557. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  558. dev_name(vpbe_dev->pdev));
  559. strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
  560. return 0;
  561. }
  562. static int vpbe_display_s_selection(struct file *file, void *priv,
  563. struct v4l2_selection *sel)
  564. {
  565. struct vpbe_layer *layer = video_drvdata(file);
  566. struct vpbe_display *disp_dev = layer->disp_dev;
  567. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  568. struct osd_layer_config *cfg = &layer->layer_info.config;
  569. struct osd_state *osd_device = disp_dev->osd_device;
  570. struct v4l2_rect rect = sel->r;
  571. int ret;
  572. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  573. "VIDIOC_S_SELECTION, layer id = %d\n", layer->device_id);
  574. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  575. sel->target != V4L2_SEL_TGT_CROP)
  576. return -EINVAL;
  577. if (rect.top < 0)
  578. rect.top = 0;
  579. if (rect.left < 0)
  580. rect.left = 0;
  581. vpbe_disp_check_window_params(disp_dev, &rect);
  582. osd_device->ops.get_layer_config(osd_device,
  583. layer->layer_info.id, cfg);
  584. vpbe_disp_calculate_scale_factor(disp_dev, layer,
  585. rect.width,
  586. rect.height);
  587. vpbe_disp_adj_position(disp_dev, layer, rect.top,
  588. rect.left);
  589. ret = osd_device->ops.set_layer_config(osd_device,
  590. layer->layer_info.id, cfg);
  591. if (ret < 0) {
  592. v4l2_err(&vpbe_dev->v4l2_dev,
  593. "Error in set layer config:\n");
  594. return -EINVAL;
  595. }
  596. /* apply zooming and h or v expansion */
  597. osd_device->ops.set_zoom(osd_device,
  598. layer->layer_info.id,
  599. layer->layer_info.h_zoom,
  600. layer->layer_info.v_zoom);
  601. ret = osd_device->ops.set_vid_expansion(osd_device,
  602. layer->layer_info.h_exp,
  603. layer->layer_info.v_exp);
  604. if (ret < 0) {
  605. v4l2_err(&vpbe_dev->v4l2_dev,
  606. "Error in set vid expansion:\n");
  607. return -EINVAL;
  608. }
  609. if ((layer->layer_info.h_zoom != ZOOM_X1) ||
  610. (layer->layer_info.v_zoom != ZOOM_X1) ||
  611. (layer->layer_info.h_exp != H_EXP_OFF) ||
  612. (layer->layer_info.v_exp != V_EXP_OFF))
  613. /* Enable expansion filter */
  614. osd_device->ops.set_interpolation_filter(osd_device, 1);
  615. else
  616. osd_device->ops.set_interpolation_filter(osd_device, 0);
  617. sel->r = rect;
  618. return 0;
  619. }
  620. static int vpbe_display_g_selection(struct file *file, void *priv,
  621. struct v4l2_selection *sel)
  622. {
  623. struct vpbe_layer *layer = video_drvdata(file);
  624. struct osd_layer_config *cfg = &layer->layer_info.config;
  625. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  626. struct osd_state *osd_device = layer->disp_dev->osd_device;
  627. struct v4l2_rect *rect = &sel->r;
  628. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  629. "VIDIOC_G_SELECTION, layer id = %d\n",
  630. layer->device_id);
  631. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  632. return -EINVAL;
  633. switch (sel->target) {
  634. case V4L2_SEL_TGT_CROP:
  635. osd_device->ops.get_layer_config(osd_device,
  636. layer->layer_info.id, cfg);
  637. rect->top = cfg->ypos;
  638. rect->left = cfg->xpos;
  639. rect->width = cfg->xsize;
  640. rect->height = cfg->ysize;
  641. break;
  642. case V4L2_SEL_TGT_CROP_DEFAULT:
  643. case V4L2_SEL_TGT_CROP_BOUNDS:
  644. rect->left = 0;
  645. rect->top = 0;
  646. rect->width = vpbe_dev->current_timings.xres;
  647. rect->height = vpbe_dev->current_timings.yres;
  648. break;
  649. default:
  650. return -EINVAL;
  651. }
  652. return 0;
  653. }
  654. static int vpbe_display_cropcap(struct file *file, void *priv,
  655. struct v4l2_cropcap *cropcap)
  656. {
  657. struct vpbe_layer *layer = video_drvdata(file);
  658. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  659. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
  660. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  661. return -EINVAL;
  662. cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
  663. return 0;
  664. }
  665. static int vpbe_display_g_fmt(struct file *file, void *priv,
  666. struct v4l2_format *fmt)
  667. {
  668. struct vpbe_layer *layer = video_drvdata(file);
  669. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  670. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  671. "VIDIOC_G_FMT, layer id = %d\n",
  672. layer->device_id);
  673. /* If buffer type is video output */
  674. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  675. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  676. return -EINVAL;
  677. }
  678. /* Fill in the information about format */
  679. fmt->fmt.pix = layer->pix_fmt;
  680. return 0;
  681. }
  682. static int vpbe_display_enum_fmt(struct file *file, void *priv,
  683. struct v4l2_fmtdesc *fmt)
  684. {
  685. struct vpbe_layer *layer = video_drvdata(file);
  686. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  687. unsigned int index = 0;
  688. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  689. "VIDIOC_ENUM_FMT, layer id = %d\n",
  690. layer->device_id);
  691. if (fmt->index > 1) {
  692. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
  693. return -EINVAL;
  694. }
  695. /* Fill in the information about format */
  696. index = fmt->index;
  697. memset(fmt, 0, sizeof(*fmt));
  698. fmt->index = index;
  699. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  700. if (index == 0) {
  701. strcpy(fmt->description, "YUV 4:2:2 - UYVY");
  702. fmt->pixelformat = V4L2_PIX_FMT_UYVY;
  703. } else {
  704. strcpy(fmt->description, "Y/CbCr 4:2:0");
  705. fmt->pixelformat = V4L2_PIX_FMT_NV12;
  706. }
  707. return 0;
  708. }
  709. static int vpbe_display_s_fmt(struct file *file, void *priv,
  710. struct v4l2_format *fmt)
  711. {
  712. struct vpbe_layer *layer = video_drvdata(file);
  713. struct vpbe_display *disp_dev = layer->disp_dev;
  714. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  715. struct osd_layer_config *cfg = &layer->layer_info.config;
  716. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  717. struct osd_state *osd_device = disp_dev->osd_device;
  718. int ret;
  719. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  720. "VIDIOC_S_FMT, layer id = %d\n",
  721. layer->device_id);
  722. if (vb2_is_busy(&layer->buffer_queue))
  723. return -EBUSY;
  724. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  725. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
  726. return -EINVAL;
  727. }
  728. /* Check for valid pixel format */
  729. ret = vpbe_try_format(disp_dev, pixfmt, 1);
  730. if (ret)
  731. return ret;
  732. /* YUV420 is requested, check availability of the
  733. other video window */
  734. layer->pix_fmt = *pixfmt;
  735. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
  736. struct vpbe_layer *otherlayer;
  737. otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
  738. /* if other layer is available, only
  739. * claim it, do not configure it
  740. */
  741. ret = osd_device->ops.request_layer(osd_device,
  742. otherlayer->layer_info.id);
  743. if (ret < 0) {
  744. v4l2_err(&vpbe_dev->v4l2_dev,
  745. "Display Manager failed to allocate layer\n");
  746. return -EBUSY;
  747. }
  748. }
  749. /* Get osd layer config */
  750. osd_device->ops.get_layer_config(osd_device,
  751. layer->layer_info.id, cfg);
  752. /* Store the pixel format in the layer object */
  753. cfg->xsize = pixfmt->width;
  754. cfg->ysize = pixfmt->height;
  755. cfg->line_length = pixfmt->bytesperline;
  756. cfg->ypos = 0;
  757. cfg->xpos = 0;
  758. cfg->interlaced = vpbe_dev->current_timings.interlaced;
  759. if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
  760. cfg->pixfmt = PIXFMT_YCBCRI;
  761. /* Change of the default pixel format for both video windows */
  762. if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
  763. struct vpbe_layer *otherlayer;
  764. cfg->pixfmt = PIXFMT_NV12;
  765. otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
  766. layer);
  767. otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
  768. }
  769. /* Set the layer config in the osd window */
  770. ret = osd_device->ops.set_layer_config(osd_device,
  771. layer->layer_info.id, cfg);
  772. if (ret < 0) {
  773. v4l2_err(&vpbe_dev->v4l2_dev,
  774. "Error in S_FMT params:\n");
  775. return -EINVAL;
  776. }
  777. /* Readback and fill the local copy of current pix format */
  778. osd_device->ops.get_layer_config(osd_device,
  779. layer->layer_info.id, cfg);
  780. return 0;
  781. }
  782. static int vpbe_display_try_fmt(struct file *file, void *priv,
  783. struct v4l2_format *fmt)
  784. {
  785. struct vpbe_layer *layer = video_drvdata(file);
  786. struct vpbe_display *disp_dev = layer->disp_dev;
  787. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  788. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  789. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
  790. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  791. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  792. return -EINVAL;
  793. }
  794. /* Check for valid field format */
  795. return vpbe_try_format(disp_dev, pixfmt, 0);
  796. }
  797. /**
  798. * vpbe_display_s_std - Set the given standard in the encoder
  799. *
  800. * Sets the standard if supported by the current encoder. Return the status.
  801. * 0 - success & -EINVAL on error
  802. */
  803. static int vpbe_display_s_std(struct file *file, void *priv,
  804. v4l2_std_id std_id)
  805. {
  806. struct vpbe_layer *layer = video_drvdata(file);
  807. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  808. int ret;
  809. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
  810. if (vb2_is_busy(&layer->buffer_queue))
  811. return -EBUSY;
  812. if (NULL != vpbe_dev->ops.s_std) {
  813. ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
  814. if (ret) {
  815. v4l2_err(&vpbe_dev->v4l2_dev,
  816. "Failed to set standard for sub devices\n");
  817. return -EINVAL;
  818. }
  819. } else {
  820. return -EINVAL;
  821. }
  822. return 0;
  823. }
  824. /**
  825. * vpbe_display_g_std - Get the standard in the current encoder
  826. *
  827. * Get the standard in the current encoder. Return the status. 0 - success
  828. * -EINVAL on error
  829. */
  830. static int vpbe_display_g_std(struct file *file, void *priv,
  831. v4l2_std_id *std_id)
  832. {
  833. struct vpbe_layer *layer = video_drvdata(file);
  834. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  835. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
  836. /* Get the standard from the current encoder */
  837. if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
  838. *std_id = vpbe_dev->current_timings.std_id;
  839. return 0;
  840. }
  841. return -EINVAL;
  842. }
  843. /**
  844. * vpbe_display_enum_output - enumerate outputs
  845. *
  846. * Enumerates the outputs available at the vpbe display
  847. * returns the status, -EINVAL if end of output list
  848. */
  849. static int vpbe_display_enum_output(struct file *file, void *priv,
  850. struct v4l2_output *output)
  851. {
  852. struct vpbe_layer *layer = video_drvdata(file);
  853. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  854. int ret;
  855. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
  856. /* Enumerate outputs */
  857. if (NULL == vpbe_dev->ops.enum_outputs)
  858. return -EINVAL;
  859. ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
  860. if (ret) {
  861. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  862. "Failed to enumerate outputs\n");
  863. return -EINVAL;
  864. }
  865. return 0;
  866. }
  867. /**
  868. * vpbe_display_s_output - Set output to
  869. * the output specified by the index
  870. */
  871. static int vpbe_display_s_output(struct file *file, void *priv,
  872. unsigned int i)
  873. {
  874. struct vpbe_layer *layer = video_drvdata(file);
  875. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  876. int ret;
  877. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
  878. if (vb2_is_busy(&layer->buffer_queue))
  879. return -EBUSY;
  880. if (NULL == vpbe_dev->ops.set_output)
  881. return -EINVAL;
  882. ret = vpbe_dev->ops.set_output(vpbe_dev, i);
  883. if (ret) {
  884. v4l2_err(&vpbe_dev->v4l2_dev,
  885. "Failed to set output for sub devices\n");
  886. return -EINVAL;
  887. }
  888. return 0;
  889. }
  890. /**
  891. * vpbe_display_g_output - Get output from subdevice
  892. * for a given by the index
  893. */
  894. static int vpbe_display_g_output(struct file *file, void *priv,
  895. unsigned int *i)
  896. {
  897. struct vpbe_layer *layer = video_drvdata(file);
  898. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  899. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
  900. /* Get the standard from the current encoder */
  901. *i = vpbe_dev->current_out_index;
  902. return 0;
  903. }
  904. /**
  905. * vpbe_display_enum_dv_timings - Enumerate the dv timings
  906. *
  907. * enum the timings in the current encoder. Return the status. 0 - success
  908. * -EINVAL on error
  909. */
  910. static int
  911. vpbe_display_enum_dv_timings(struct file *file, void *priv,
  912. struct v4l2_enum_dv_timings *timings)
  913. {
  914. struct vpbe_layer *layer = video_drvdata(file);
  915. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  916. int ret;
  917. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
  918. /* Enumerate outputs */
  919. if (NULL == vpbe_dev->ops.enum_dv_timings)
  920. return -EINVAL;
  921. ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
  922. if (ret) {
  923. v4l2_err(&vpbe_dev->v4l2_dev,
  924. "Failed to enumerate dv timings info\n");
  925. return -EINVAL;
  926. }
  927. return 0;
  928. }
  929. /**
  930. * vpbe_display_s_dv_timings - Set the dv timings
  931. *
  932. * Set the timings in the current encoder. Return the status. 0 - success
  933. * -EINVAL on error
  934. */
  935. static int
  936. vpbe_display_s_dv_timings(struct file *file, void *priv,
  937. struct v4l2_dv_timings *timings)
  938. {
  939. struct vpbe_layer *layer = video_drvdata(file);
  940. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  941. int ret;
  942. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
  943. if (vb2_is_busy(&layer->buffer_queue))
  944. return -EBUSY;
  945. /* Set the given standard in the encoder */
  946. if (!vpbe_dev->ops.s_dv_timings)
  947. return -EINVAL;
  948. ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
  949. if (ret) {
  950. v4l2_err(&vpbe_dev->v4l2_dev,
  951. "Failed to set the dv timings info\n");
  952. return -EINVAL;
  953. }
  954. return 0;
  955. }
  956. /**
  957. * vpbe_display_g_dv_timings - Set the dv timings
  958. *
  959. * Get the timings in the current encoder. Return the status. 0 - success
  960. * -EINVAL on error
  961. */
  962. static int
  963. vpbe_display_g_dv_timings(struct file *file, void *priv,
  964. struct v4l2_dv_timings *dv_timings)
  965. {
  966. struct vpbe_layer *layer = video_drvdata(file);
  967. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  968. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
  969. /* Get the given standard in the encoder */
  970. if (vpbe_dev->current_timings.timings_type &
  971. VPBE_ENC_DV_TIMINGS) {
  972. *dv_timings = vpbe_dev->current_timings.dv_timings;
  973. } else {
  974. return -EINVAL;
  975. }
  976. return 0;
  977. }
  978. /*
  979. * vpbe_display_open()
  980. * It creates object of file handle structure and stores it in private_data
  981. * member of filepointer
  982. */
  983. static int vpbe_display_open(struct file *file)
  984. {
  985. struct vpbe_layer *layer = video_drvdata(file);
  986. struct vpbe_display *disp_dev = layer->disp_dev;
  987. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  988. struct osd_state *osd_device = disp_dev->osd_device;
  989. int err;
  990. /* creating context for file descriptor */
  991. err = v4l2_fh_open(file);
  992. if (err) {
  993. v4l2_err(&vpbe_dev->v4l2_dev, "v4l2_fh_open failed\n");
  994. return err;
  995. }
  996. /* leaving if layer is already initialized */
  997. if (!v4l2_fh_is_singular_file(file))
  998. return err;
  999. if (!layer->usrs) {
  1000. if (mutex_lock_interruptible(&layer->opslock))
  1001. return -ERESTARTSYS;
  1002. /* First claim the layer for this device */
  1003. err = osd_device->ops.request_layer(osd_device,
  1004. layer->layer_info.id);
  1005. mutex_unlock(&layer->opslock);
  1006. if (err < 0) {
  1007. /* Couldn't get layer */
  1008. v4l2_err(&vpbe_dev->v4l2_dev,
  1009. "Display Manager failed to allocate layer\n");
  1010. v4l2_fh_release(file);
  1011. return -EINVAL;
  1012. }
  1013. }
  1014. /* Increment layer usrs counter */
  1015. layer->usrs++;
  1016. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1017. "vpbe display device opened successfully\n");
  1018. return 0;
  1019. }
  1020. /*
  1021. * vpbe_display_release()
  1022. * This function deletes buffer queue, frees the buffers and the davinci
  1023. * display file * handle
  1024. */
  1025. static int vpbe_display_release(struct file *file)
  1026. {
  1027. struct vpbe_layer *layer = video_drvdata(file);
  1028. struct osd_layer_config *cfg = &layer->layer_info.config;
  1029. struct vpbe_display *disp_dev = layer->disp_dev;
  1030. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1031. struct osd_state *osd_device = disp_dev->osd_device;
  1032. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
  1033. mutex_lock(&layer->opslock);
  1034. osd_device->ops.disable_layer(osd_device,
  1035. layer->layer_info.id);
  1036. /* Decrement layer usrs counter */
  1037. layer->usrs--;
  1038. /* If this file handle has initialize encoder device, reset it */
  1039. if (!layer->usrs) {
  1040. if (cfg->pixfmt == PIXFMT_NV12) {
  1041. struct vpbe_layer *otherlayer;
  1042. otherlayer =
  1043. _vpbe_display_get_other_win_layer(disp_dev, layer);
  1044. osd_device->ops.disable_layer(osd_device,
  1045. otherlayer->layer_info.id);
  1046. osd_device->ops.release_layer(osd_device,
  1047. otherlayer->layer_info.id);
  1048. }
  1049. osd_device->ops.disable_layer(osd_device,
  1050. layer->layer_info.id);
  1051. osd_device->ops.release_layer(osd_device,
  1052. layer->layer_info.id);
  1053. }
  1054. _vb2_fop_release(file, NULL);
  1055. mutex_unlock(&layer->opslock);
  1056. disp_dev->cbcr_ofst = 0;
  1057. return 0;
  1058. }
  1059. /* vpbe capture ioctl operations */
  1060. static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
  1061. .vidioc_querycap = vpbe_display_querycap,
  1062. .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
  1063. .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
  1064. .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
  1065. .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
  1066. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1067. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1068. .vidioc_querybuf = vb2_ioctl_querybuf,
  1069. .vidioc_qbuf = vb2_ioctl_qbuf,
  1070. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1071. .vidioc_streamon = vb2_ioctl_streamon,
  1072. .vidioc_streamoff = vb2_ioctl_streamoff,
  1073. .vidioc_expbuf = vb2_ioctl_expbuf,
  1074. .vidioc_cropcap = vpbe_display_cropcap,
  1075. .vidioc_g_selection = vpbe_display_g_selection,
  1076. .vidioc_s_selection = vpbe_display_s_selection,
  1077. .vidioc_s_std = vpbe_display_s_std,
  1078. .vidioc_g_std = vpbe_display_g_std,
  1079. .vidioc_enum_output = vpbe_display_enum_output,
  1080. .vidioc_s_output = vpbe_display_s_output,
  1081. .vidioc_g_output = vpbe_display_g_output,
  1082. .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
  1083. .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
  1084. .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
  1085. };
  1086. static struct v4l2_file_operations vpbe_fops = {
  1087. .owner = THIS_MODULE,
  1088. .open = vpbe_display_open,
  1089. .release = vpbe_display_release,
  1090. .unlocked_ioctl = video_ioctl2,
  1091. .mmap = vb2_fop_mmap,
  1092. .poll = vb2_fop_poll,
  1093. };
  1094. static int vpbe_device_get(struct device *dev, void *data)
  1095. {
  1096. struct platform_device *pdev = to_platform_device(dev);
  1097. struct vpbe_display *vpbe_disp = data;
  1098. if (strcmp("vpbe_controller", pdev->name) == 0)
  1099. vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
  1100. if (strstr(pdev->name, "vpbe-osd") != NULL)
  1101. vpbe_disp->osd_device = platform_get_drvdata(pdev);
  1102. return 0;
  1103. }
  1104. static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
  1105. struct platform_device *pdev)
  1106. {
  1107. struct vpbe_layer *vpbe_display_layer = NULL;
  1108. struct video_device *vbd = NULL;
  1109. /* Allocate memory for four plane display objects */
  1110. disp_dev->dev[i] =
  1111. kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
  1112. /* If memory allocation fails, return error */
  1113. if (!disp_dev->dev[i]) {
  1114. printk(KERN_ERR "ran out of memory\n");
  1115. return -ENOMEM;
  1116. }
  1117. spin_lock_init(&disp_dev->dev[i]->irqlock);
  1118. mutex_init(&disp_dev->dev[i]->opslock);
  1119. /* Get the pointer to the layer object */
  1120. vpbe_display_layer = disp_dev->dev[i];
  1121. vbd = &vpbe_display_layer->video_dev;
  1122. /* Initialize field of video device */
  1123. vbd->release = video_device_release_empty;
  1124. vbd->fops = &vpbe_fops;
  1125. vbd->ioctl_ops = &vpbe_ioctl_ops;
  1126. vbd->minor = -1;
  1127. vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1128. vbd->lock = &vpbe_display_layer->opslock;
  1129. vbd->vfl_dir = VFL_DIR_TX;
  1130. if (disp_dev->vpbe_dev->current_timings.timings_type &
  1131. VPBE_ENC_STD)
  1132. vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
  1133. snprintf(vbd->name, sizeof(vbd->name),
  1134. "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
  1135. (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1136. (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1137. (VPBE_DISPLAY_VERSION_CODE) & 0xff);
  1138. vpbe_display_layer->device_id = i;
  1139. vpbe_display_layer->layer_info.id =
  1140. ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
  1141. return 0;
  1142. }
  1143. static int register_device(struct vpbe_layer *vpbe_display_layer,
  1144. struct vpbe_display *disp_dev,
  1145. struct platform_device *pdev)
  1146. {
  1147. int err;
  1148. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1149. "Trying to register VPBE display device.\n");
  1150. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1151. "layer=%x,layer->video_dev=%x\n",
  1152. (int)vpbe_display_layer,
  1153. (int)&vpbe_display_layer->video_dev);
  1154. vpbe_display_layer->video_dev.queue = &vpbe_display_layer->buffer_queue;
  1155. err = video_register_device(&vpbe_display_layer->video_dev,
  1156. VFL_TYPE_GRABBER,
  1157. -1);
  1158. if (err)
  1159. return -ENODEV;
  1160. vpbe_display_layer->disp_dev = disp_dev;
  1161. /* set the driver data in platform device */
  1162. platform_set_drvdata(pdev, disp_dev);
  1163. video_set_drvdata(&vpbe_display_layer->video_dev,
  1164. vpbe_display_layer);
  1165. return 0;
  1166. }
  1167. /*
  1168. * vpbe_display_probe()
  1169. * This function creates device entries by register itself to the V4L2 driver
  1170. * and initializes fields of each layer objects
  1171. */
  1172. static int vpbe_display_probe(struct platform_device *pdev)
  1173. {
  1174. struct vpbe_display *disp_dev;
  1175. struct v4l2_device *v4l2_dev;
  1176. struct resource *res = NULL;
  1177. struct vb2_queue *q;
  1178. int k;
  1179. int i;
  1180. int err;
  1181. int irq;
  1182. printk(KERN_DEBUG "vpbe_display_probe\n");
  1183. /* Allocate memory for vpbe_display */
  1184. disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
  1185. GFP_KERNEL);
  1186. if (!disp_dev)
  1187. return -ENOMEM;
  1188. spin_lock_init(&disp_dev->dma_queue_lock);
  1189. /*
  1190. * Scan all the platform devices to find the vpbe
  1191. * controller device and get the vpbe_dev object
  1192. */
  1193. err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
  1194. vpbe_device_get);
  1195. if (err < 0)
  1196. return err;
  1197. v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1198. /* Initialize the vpbe display controller */
  1199. if (NULL != disp_dev->vpbe_dev->ops.initialize) {
  1200. err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
  1201. disp_dev->vpbe_dev);
  1202. if (err) {
  1203. v4l2_err(v4l2_dev, "Error initing vpbe\n");
  1204. err = -ENOMEM;
  1205. goto probe_out;
  1206. }
  1207. }
  1208. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1209. if (init_vpbe_layer(i, disp_dev, pdev)) {
  1210. err = -ENODEV;
  1211. goto probe_out;
  1212. }
  1213. }
  1214. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1215. if (!res) {
  1216. v4l2_err(v4l2_dev, "Unable to get VENC interrupt resource\n");
  1217. err = -ENODEV;
  1218. goto probe_out;
  1219. }
  1220. irq = res->start;
  1221. err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
  1222. VPBE_DISPLAY_DRIVER, disp_dev);
  1223. if (err) {
  1224. v4l2_err(v4l2_dev, "VPBE IRQ request failed\n");
  1225. goto probe_out;
  1226. }
  1227. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1228. /* initialize vb2 queue */
  1229. q = &disp_dev->dev[i]->buffer_queue;
  1230. memset(q, 0, sizeof(*q));
  1231. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1232. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1233. q->drv_priv = disp_dev->dev[i];
  1234. q->ops = &video_qops;
  1235. q->mem_ops = &vb2_dma_contig_memops;
  1236. q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
  1237. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1238. q->min_buffers_needed = 1;
  1239. q->lock = &disp_dev->dev[i]->opslock;
  1240. q->dev = disp_dev->vpbe_dev->pdev;
  1241. err = vb2_queue_init(q);
  1242. if (err) {
  1243. v4l2_err(v4l2_dev, "vb2_queue_init() failed\n");
  1244. goto probe_out;
  1245. }
  1246. INIT_LIST_HEAD(&disp_dev->dev[i]->dma_queue);
  1247. if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
  1248. err = -ENODEV;
  1249. goto probe_out;
  1250. }
  1251. }
  1252. v4l2_dbg(1, debug, v4l2_dev,
  1253. "Successfully completed the probing of vpbe v4l2 device\n");
  1254. return 0;
  1255. probe_out:
  1256. for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
  1257. /* Unregister video device */
  1258. if (disp_dev->dev[k] != NULL) {
  1259. video_unregister_device(&disp_dev->dev[k]->video_dev);
  1260. kfree(disp_dev->dev[k]);
  1261. }
  1262. }
  1263. return err;
  1264. }
  1265. /*
  1266. * vpbe_display_remove()
  1267. * It un-register hardware layer from V4L2 driver
  1268. */
  1269. static int vpbe_display_remove(struct platform_device *pdev)
  1270. {
  1271. struct vpbe_layer *vpbe_display_layer;
  1272. struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
  1273. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1274. int i;
  1275. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
  1276. /* deinitialize the vpbe display controller */
  1277. if (NULL != vpbe_dev->ops.deinitialize)
  1278. vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
  1279. /* un-register device */
  1280. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1281. /* Get the pointer to the layer object */
  1282. vpbe_display_layer = disp_dev->dev[i];
  1283. /* Unregister video device */
  1284. video_unregister_device(&vpbe_display_layer->video_dev);
  1285. }
  1286. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1287. kfree(disp_dev->dev[i]);
  1288. disp_dev->dev[i] = NULL;
  1289. }
  1290. return 0;
  1291. }
  1292. static struct platform_driver vpbe_display_driver = {
  1293. .driver = {
  1294. .name = VPBE_DISPLAY_DRIVER,
  1295. .bus = &platform_bus_type,
  1296. },
  1297. .probe = vpbe_display_probe,
  1298. .remove = vpbe_display_remove,
  1299. };
  1300. module_platform_driver(vpbe_display_driver);
  1301. MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
  1302. MODULE_LICENSE("GPL");
  1303. MODULE_AUTHOR("Texas Instruments");