vpbe_display.c 40 KB

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