vpif_display.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. /*
  2. * vpif-display - VPIF display driver
  3. * Display driver for TI DaVinci VPIF
  4. *
  5. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  6. * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed .as is. WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <media/v4l2-ioctl.h>
  22. #include "vpif.h"
  23. #include "vpif_display.h"
  24. MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
  25. MODULE_LICENSE("GPL");
  26. MODULE_VERSION(VPIF_DISPLAY_VERSION);
  27. #define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
  28. #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
  29. #define vpif_dbg(level, debug, fmt, arg...) \
  30. v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
  31. static int debug = 1;
  32. module_param(debug, int, 0644);
  33. MODULE_PARM_DESC(debug, "Debug level 0-1");
  34. #define VPIF_DRIVER_NAME "vpif_display"
  35. /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
  36. static int ycmux_mode;
  37. static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
  38. static struct vpif_device vpif_obj = { {NULL} };
  39. static struct device *vpif_dev;
  40. static void vpif_calculate_offsets(struct channel_obj *ch);
  41. static void vpif_config_addr(struct channel_obj *ch, int muxmode);
  42. static inline struct vpif_disp_buffer *to_vpif_buffer(struct vb2_buffer *vb)
  43. {
  44. return container_of(vb, struct vpif_disp_buffer, vb);
  45. }
  46. /**
  47. * vpif_buffer_prepare : callback function for buffer prepare
  48. * @vb: ptr to vb2_buffer
  49. *
  50. * This is the callback function for buffer prepare when vb2_qbuf()
  51. * function is called. The buffer is prepared and user space virtual address
  52. * or user address is converted into physical address
  53. */
  54. static int vpif_buffer_prepare(struct vb2_buffer *vb)
  55. {
  56. struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
  57. struct common_obj *common;
  58. common = &ch->common[VPIF_VIDEO_INDEX];
  59. vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
  60. if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  61. return -EINVAL;
  62. vb->v4l2_buf.field = common->fmt.fmt.pix.field;
  63. if (vb->vb2_queue->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
  64. unsigned long addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  65. if (!ISALIGNED(addr + common->ytop_off) ||
  66. !ISALIGNED(addr + common->ybtm_off) ||
  67. !ISALIGNED(addr + common->ctop_off) ||
  68. !ISALIGNED(addr + common->cbtm_off)) {
  69. vpif_err("buffer offset not aligned to 8 bytes\n");
  70. return -EINVAL;
  71. }
  72. }
  73. return 0;
  74. }
  75. /**
  76. * vpif_buffer_queue_setup : Callback function for buffer setup.
  77. * @vq: vb2_queue ptr
  78. * @fmt: v4l2 format
  79. * @nbuffers: ptr to number of buffers requested by application
  80. * @nplanes:: contains number of distinct video planes needed to hold a frame
  81. * @sizes[]: contains the size (in bytes) of each plane.
  82. * @alloc_ctxs: ptr to allocation context
  83. *
  84. * This callback function is called when reqbuf() is called to adjust
  85. * the buffer count and buffer size
  86. */
  87. static int vpif_buffer_queue_setup(struct vb2_queue *vq,
  88. const struct v4l2_format *fmt,
  89. unsigned int *nbuffers, unsigned int *nplanes,
  90. unsigned int sizes[], void *alloc_ctxs[])
  91. {
  92. struct channel_obj *ch = vb2_get_drv_priv(vq);
  93. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  94. if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
  95. return -EINVAL;
  96. if (vq->num_buffers + *nbuffers < 3)
  97. *nbuffers = 3 - vq->num_buffers;
  98. *nplanes = 1;
  99. sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
  100. alloc_ctxs[0] = common->alloc_ctx;
  101. /* Calculate the offset for Y and C data in the buffer */
  102. vpif_calculate_offsets(ch);
  103. return 0;
  104. }
  105. /**
  106. * vpif_buffer_queue : Callback function to add buffer to DMA queue
  107. * @vb: ptr to vb2_buffer
  108. *
  109. * This callback fucntion queues the buffer to DMA engine
  110. */
  111. static void vpif_buffer_queue(struct vb2_buffer *vb)
  112. {
  113. struct vpif_disp_buffer *buf = to_vpif_buffer(vb);
  114. struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
  115. struct common_obj *common;
  116. unsigned long flags;
  117. common = &ch->common[VPIF_VIDEO_INDEX];
  118. /* add the buffer to the DMA queue */
  119. spin_lock_irqsave(&common->irqlock, flags);
  120. list_add_tail(&buf->list, &common->dma_queue);
  121. spin_unlock_irqrestore(&common->irqlock, flags);
  122. }
  123. /**
  124. * vpif_start_streaming : Starts the DMA engine for streaming
  125. * @vb: ptr to vb2_buffer
  126. * @count: number of buffers
  127. */
  128. static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
  129. {
  130. struct vpif_display_config *vpif_config_data =
  131. vpif_dev->platform_data;
  132. struct channel_obj *ch = vb2_get_drv_priv(vq);
  133. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  134. struct vpif_params *vpif = &ch->vpifparams;
  135. struct vpif_disp_buffer *buf, *tmp;
  136. unsigned long addr, flags;
  137. int ret;
  138. spin_lock_irqsave(&common->irqlock, flags);
  139. /* Initialize field_id */
  140. ch->field_id = 0;
  141. /* clock settings */
  142. if (vpif_config_data->set_clock) {
  143. ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
  144. ycmux_mode, ch->vpifparams.std_info.hd_sd);
  145. if (ret < 0) {
  146. vpif_err("can't set clock\n");
  147. goto err;
  148. }
  149. }
  150. /* set the parameters and addresses */
  151. ret = vpif_set_video_params(vpif, ch->channel_id + 2);
  152. if (ret < 0)
  153. goto err;
  154. ycmux_mode = ret;
  155. vpif_config_addr(ch, ret);
  156. /* Get the next frame from the buffer queue */
  157. common->next_frm = common->cur_frm =
  158. list_entry(common->dma_queue.next,
  159. struct vpif_disp_buffer, list);
  160. list_del(&common->cur_frm->list);
  161. spin_unlock_irqrestore(&common->irqlock, flags);
  162. /* Mark state of the current frame to active */
  163. common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  164. addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
  165. common->set_addr((addr + common->ytop_off),
  166. (addr + common->ybtm_off),
  167. (addr + common->ctop_off),
  168. (addr + common->cbtm_off));
  169. /*
  170. * Set interrupt for both the fields in VPIF
  171. * Register enable channel in VPIF register
  172. */
  173. channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
  174. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  175. channel2_intr_assert();
  176. channel2_intr_enable(1);
  177. enable_channel2(1);
  178. if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
  179. channel2_clipping_enable(1);
  180. }
  181. if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
  182. channel3_intr_assert();
  183. channel3_intr_enable(1);
  184. enable_channel3(1);
  185. if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
  186. channel3_clipping_enable(1);
  187. }
  188. return 0;
  189. err:
  190. list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
  191. list_del(&buf->list);
  192. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
  193. }
  194. spin_unlock_irqrestore(&common->irqlock, flags);
  195. return ret;
  196. }
  197. /**
  198. * vpif_stop_streaming : Stop the DMA engine
  199. * @vq: ptr to vb2_queue
  200. *
  201. * This callback stops the DMA engine and any remaining buffers
  202. * in the DMA queue are released.
  203. */
  204. static void vpif_stop_streaming(struct vb2_queue *vq)
  205. {
  206. struct channel_obj *ch = vb2_get_drv_priv(vq);
  207. struct common_obj *common;
  208. unsigned long flags;
  209. common = &ch->common[VPIF_VIDEO_INDEX];
  210. /* Disable channel */
  211. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  212. enable_channel2(0);
  213. channel2_intr_enable(0);
  214. }
  215. if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
  216. enable_channel3(0);
  217. channel3_intr_enable(0);
  218. }
  219. /* release all active buffers */
  220. spin_lock_irqsave(&common->irqlock, flags);
  221. if (common->cur_frm == common->next_frm) {
  222. vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
  223. } else {
  224. if (common->cur_frm != NULL)
  225. vb2_buffer_done(&common->cur_frm->vb,
  226. VB2_BUF_STATE_ERROR);
  227. if (common->next_frm != NULL)
  228. vb2_buffer_done(&common->next_frm->vb,
  229. VB2_BUF_STATE_ERROR);
  230. }
  231. while (!list_empty(&common->dma_queue)) {
  232. common->next_frm = list_entry(common->dma_queue.next,
  233. struct vpif_disp_buffer, list);
  234. list_del(&common->next_frm->list);
  235. vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
  236. }
  237. spin_unlock_irqrestore(&common->irqlock, flags);
  238. }
  239. static struct vb2_ops video_qops = {
  240. .queue_setup = vpif_buffer_queue_setup,
  241. .wait_prepare = vb2_ops_wait_prepare,
  242. .wait_finish = vb2_ops_wait_finish,
  243. .buf_prepare = vpif_buffer_prepare,
  244. .start_streaming = vpif_start_streaming,
  245. .stop_streaming = vpif_stop_streaming,
  246. .buf_queue = vpif_buffer_queue,
  247. };
  248. static void process_progressive_mode(struct common_obj *common)
  249. {
  250. unsigned long addr = 0;
  251. spin_lock(&common->irqlock);
  252. /* Get the next buffer from buffer queue */
  253. common->next_frm = list_entry(common->dma_queue.next,
  254. struct vpif_disp_buffer, list);
  255. /* Remove that buffer from the buffer queue */
  256. list_del(&common->next_frm->list);
  257. spin_unlock(&common->irqlock);
  258. /* Mark status of the buffer as active */
  259. common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  260. /* Set top and bottom field addrs in VPIF registers */
  261. addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
  262. common->set_addr(addr + common->ytop_off,
  263. addr + common->ybtm_off,
  264. addr + common->ctop_off,
  265. addr + common->cbtm_off);
  266. }
  267. static void process_interlaced_mode(int fid, struct common_obj *common)
  268. {
  269. /* device field id and local field id are in sync */
  270. /* If this is even field */
  271. if (0 == fid) {
  272. if (common->cur_frm == common->next_frm)
  273. return;
  274. /* one frame is displayed If next frame is
  275. * available, release cur_frm and move on */
  276. /* Copy frame display time */
  277. v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
  278. /* Change status of the cur_frm */
  279. vb2_buffer_done(&common->cur_frm->vb,
  280. VB2_BUF_STATE_DONE);
  281. /* Make cur_frm pointing to next_frm */
  282. common->cur_frm = common->next_frm;
  283. } else if (1 == fid) { /* odd field */
  284. spin_lock(&common->irqlock);
  285. if (list_empty(&common->dma_queue)
  286. || (common->cur_frm != common->next_frm)) {
  287. spin_unlock(&common->irqlock);
  288. return;
  289. }
  290. spin_unlock(&common->irqlock);
  291. /* one field is displayed configure the next
  292. * frame if it is available else hold on current
  293. * frame */
  294. /* Get next from the buffer queue */
  295. process_progressive_mode(common);
  296. }
  297. }
  298. /*
  299. * vpif_channel_isr: It changes status of the displayed buffer, takes next
  300. * buffer from the queue and sets its address in VPIF registers
  301. */
  302. static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
  303. {
  304. struct vpif_device *dev = &vpif_obj;
  305. struct channel_obj *ch;
  306. struct common_obj *common;
  307. enum v4l2_field field;
  308. int fid = -1, i;
  309. int channel_id = 0;
  310. channel_id = *(int *)(dev_id);
  311. if (!vpif_intr_status(channel_id + 2))
  312. return IRQ_NONE;
  313. ch = dev->dev[channel_id];
  314. field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
  315. for (i = 0; i < VPIF_NUMOBJECTS; i++) {
  316. common = &ch->common[i];
  317. /* If streaming is started in this channel */
  318. if (1 == ch->vpifparams.std_info.frm_fmt) {
  319. spin_lock(&common->irqlock);
  320. if (list_empty(&common->dma_queue)) {
  321. spin_unlock(&common->irqlock);
  322. continue;
  323. }
  324. spin_unlock(&common->irqlock);
  325. /* Progressive mode */
  326. if (!channel_first_int[i][channel_id]) {
  327. /* Mark status of the cur_frm to
  328. * done and unlock semaphore on it */
  329. v4l2_get_timestamp(&common->cur_frm->vb.
  330. v4l2_buf.timestamp);
  331. vb2_buffer_done(&common->cur_frm->vb,
  332. VB2_BUF_STATE_DONE);
  333. /* Make cur_frm pointing to next_frm */
  334. common->cur_frm = common->next_frm;
  335. }
  336. channel_first_int[i][channel_id] = 0;
  337. process_progressive_mode(common);
  338. } else {
  339. /* Interlaced mode */
  340. /* If it is first interrupt, ignore it */
  341. if (channel_first_int[i][channel_id]) {
  342. channel_first_int[i][channel_id] = 0;
  343. continue;
  344. }
  345. if (0 == i) {
  346. ch->field_id ^= 1;
  347. /* Get field id from VPIF registers */
  348. fid = vpif_channel_getfid(ch->channel_id + 2);
  349. /* If fid does not match with stored field id */
  350. if (fid != ch->field_id) {
  351. /* Make them in sync */
  352. if (0 == fid)
  353. ch->field_id = fid;
  354. return IRQ_HANDLED;
  355. }
  356. }
  357. process_interlaced_mode(fid, common);
  358. }
  359. }
  360. return IRQ_HANDLED;
  361. }
  362. static int vpif_update_std_info(struct channel_obj *ch)
  363. {
  364. struct video_obj *vid_ch = &ch->video;
  365. struct vpif_params *vpifparams = &ch->vpifparams;
  366. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  367. const struct vpif_channel_config_params *config;
  368. int i;
  369. for (i = 0; i < vpif_ch_params_count; i++) {
  370. config = &vpif_ch_params[i];
  371. if (config->hd_sd == 0) {
  372. vpif_dbg(2, debug, "SD format\n");
  373. if (config->stdid & vid_ch->stdid) {
  374. memcpy(std_info, config, sizeof(*config));
  375. break;
  376. }
  377. }
  378. }
  379. if (i == vpif_ch_params_count) {
  380. vpif_dbg(1, debug, "Format not found\n");
  381. return -EINVAL;
  382. }
  383. return 0;
  384. }
  385. static int vpif_update_resolution(struct channel_obj *ch)
  386. {
  387. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  388. struct video_obj *vid_ch = &ch->video;
  389. struct vpif_params *vpifparams = &ch->vpifparams;
  390. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  391. if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
  392. return -EINVAL;
  393. if (vid_ch->stdid) {
  394. if (vpif_update_std_info(ch))
  395. return -EINVAL;
  396. }
  397. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  398. common->fmt.fmt.pix.width = std_info->width;
  399. common->fmt.fmt.pix.height = std_info->height;
  400. vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
  401. common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
  402. /* Set height and width paramateres */
  403. common->height = std_info->height;
  404. common->width = std_info->width;
  405. common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
  406. if (vid_ch->stdid)
  407. common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  408. else
  409. common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
  410. if (ch->vpifparams.std_info.frm_fmt)
  411. common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
  412. else
  413. common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  414. return 0;
  415. }
  416. /*
  417. * vpif_calculate_offsets: This function calculates buffers offset for Y and C
  418. * in the top and bottom field
  419. */
  420. static void vpif_calculate_offsets(struct channel_obj *ch)
  421. {
  422. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  423. struct vpif_params *vpifparams = &ch->vpifparams;
  424. enum v4l2_field field = common->fmt.fmt.pix.field;
  425. struct video_obj *vid_ch = &ch->video;
  426. unsigned int hpitch, vpitch, sizeimage;
  427. if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
  428. if (ch->vpifparams.std_info.frm_fmt)
  429. vid_ch->buf_field = V4L2_FIELD_NONE;
  430. else
  431. vid_ch->buf_field = V4L2_FIELD_INTERLACED;
  432. } else {
  433. vid_ch->buf_field = common->fmt.fmt.pix.field;
  434. }
  435. sizeimage = common->fmt.fmt.pix.sizeimage;
  436. hpitch = common->fmt.fmt.pix.bytesperline;
  437. vpitch = sizeimage / (hpitch * 2);
  438. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  439. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  440. common->ytop_off = 0;
  441. common->ybtm_off = hpitch;
  442. common->ctop_off = sizeimage / 2;
  443. common->cbtm_off = sizeimage / 2 + hpitch;
  444. } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
  445. common->ytop_off = 0;
  446. common->ybtm_off = sizeimage / 4;
  447. common->ctop_off = sizeimage / 2;
  448. common->cbtm_off = common->ctop_off + sizeimage / 4;
  449. } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
  450. common->ybtm_off = 0;
  451. common->ytop_off = sizeimage / 4;
  452. common->cbtm_off = sizeimage / 2;
  453. common->ctop_off = common->cbtm_off + sizeimage / 4;
  454. }
  455. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  456. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  457. vpifparams->video_params.storage_mode = 1;
  458. } else {
  459. vpifparams->video_params.storage_mode = 0;
  460. }
  461. if (ch->vpifparams.std_info.frm_fmt == 1) {
  462. vpifparams->video_params.hpitch =
  463. common->fmt.fmt.pix.bytesperline;
  464. } else {
  465. if ((field == V4L2_FIELD_ANY) ||
  466. (field == V4L2_FIELD_INTERLACED))
  467. vpifparams->video_params.hpitch =
  468. common->fmt.fmt.pix.bytesperline * 2;
  469. else
  470. vpifparams->video_params.hpitch =
  471. common->fmt.fmt.pix.bytesperline;
  472. }
  473. ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
  474. }
  475. static void vpif_config_addr(struct channel_obj *ch, int muxmode)
  476. {
  477. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  478. if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
  479. common->set_addr = ch3_set_videobuf_addr;
  480. } else {
  481. if (2 == muxmode)
  482. common->set_addr = ch2_set_videobuf_addr_yc_nmux;
  483. else
  484. common->set_addr = ch2_set_videobuf_addr;
  485. }
  486. }
  487. /* functions implementing ioctls */
  488. /**
  489. * vpif_querycap() - QUERYCAP handler
  490. * @file: file ptr
  491. * @priv: file handle
  492. * @cap: ptr to v4l2_capability structure
  493. */
  494. static int vpif_querycap(struct file *file, void *priv,
  495. struct v4l2_capability *cap)
  496. {
  497. struct vpif_display_config *config = vpif_dev->platform_data;
  498. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  499. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  500. strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
  501. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  502. dev_name(vpif_dev));
  503. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  504. return 0;
  505. }
  506. static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
  507. struct v4l2_fmtdesc *fmt)
  508. {
  509. if (fmt->index != 0)
  510. return -EINVAL;
  511. /* Fill in the information about format */
  512. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  513. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  514. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  515. fmt->flags = 0;
  516. return 0;
  517. }
  518. static int vpif_g_fmt_vid_out(struct file *file, void *priv,
  519. struct v4l2_format *fmt)
  520. {
  521. struct video_device *vdev = video_devdata(file);
  522. struct channel_obj *ch = video_get_drvdata(vdev);
  523. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  524. /* Check the validity of the buffer type */
  525. if (common->fmt.type != fmt->type)
  526. return -EINVAL;
  527. if (vpif_update_resolution(ch))
  528. return -EINVAL;
  529. *fmt = common->fmt;
  530. return 0;
  531. }
  532. static int vpif_try_fmt_vid_out(struct file *file, void *priv,
  533. struct v4l2_format *fmt)
  534. {
  535. struct video_device *vdev = video_devdata(file);
  536. struct channel_obj *ch = video_get_drvdata(vdev);
  537. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  538. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  539. /*
  540. * to supress v4l-compliance warnings silently correct
  541. * the pixelformat
  542. */
  543. if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
  544. pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
  545. if (vpif_update_resolution(ch))
  546. return -EINVAL;
  547. pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
  548. pixfmt->field = common->fmt.fmt.pix.field;
  549. pixfmt->bytesperline = common->fmt.fmt.pix.width;
  550. pixfmt->width = common->fmt.fmt.pix.width;
  551. pixfmt->height = common->fmt.fmt.pix.height;
  552. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
  553. pixfmt->priv = 0;
  554. return 0;
  555. }
  556. static int vpif_s_fmt_vid_out(struct file *file, void *priv,
  557. struct v4l2_format *fmt)
  558. {
  559. struct video_device *vdev = video_devdata(file);
  560. struct channel_obj *ch = video_get_drvdata(vdev);
  561. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  562. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  563. int ret;
  564. if (vb2_is_busy(&common->buffer_queue))
  565. return -EBUSY;
  566. ret = vpif_try_fmt_vid_out(file, priv, fmt);
  567. if (ret)
  568. return ret;
  569. /* store the pix format in the channel object */
  570. common->fmt.fmt.pix = *pixfmt;
  571. /* store the format in the channel object */
  572. common->fmt = *fmt;
  573. return 0;
  574. }
  575. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
  576. {
  577. struct vpif_display_config *config = vpif_dev->platform_data;
  578. struct video_device *vdev = video_devdata(file);
  579. struct channel_obj *ch = video_get_drvdata(vdev);
  580. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  581. struct vpif_display_chan_config *chan_cfg;
  582. struct v4l2_output output;
  583. int ret;
  584. if (config->chan_config[ch->channel_id].outputs == NULL)
  585. return -ENODATA;
  586. chan_cfg = &config->chan_config[ch->channel_id];
  587. output = chan_cfg->outputs[ch->output_idx].output;
  588. if (output.capabilities != V4L2_OUT_CAP_STD)
  589. return -ENODATA;
  590. if (vb2_is_busy(&common->buffer_queue))
  591. return -EBUSY;
  592. if (!(std_id & VPIF_V4L2_STD))
  593. return -EINVAL;
  594. /* Call encoder subdevice function to set the standard */
  595. ch->video.stdid = std_id;
  596. memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
  597. /* Get the information about the standard */
  598. if (vpif_update_resolution(ch))
  599. return -EINVAL;
  600. common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
  601. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  602. s_std_output, std_id);
  603. if (ret < 0) {
  604. vpif_err("Failed to set output standard\n");
  605. return ret;
  606. }
  607. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  608. s_std, std_id);
  609. if (ret < 0)
  610. vpif_err("Failed to set standard for sub devices\n");
  611. return ret;
  612. }
  613. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  614. {
  615. struct vpif_display_config *config = vpif_dev->platform_data;
  616. struct video_device *vdev = video_devdata(file);
  617. struct channel_obj *ch = video_get_drvdata(vdev);
  618. struct vpif_display_chan_config *chan_cfg;
  619. struct v4l2_output output;
  620. if (config->chan_config[ch->channel_id].outputs == NULL)
  621. return -ENODATA;
  622. chan_cfg = &config->chan_config[ch->channel_id];
  623. output = chan_cfg->outputs[ch->output_idx].output;
  624. if (output.capabilities != V4L2_OUT_CAP_STD)
  625. return -ENODATA;
  626. *std = ch->video.stdid;
  627. return 0;
  628. }
  629. static int vpif_enum_output(struct file *file, void *fh,
  630. struct v4l2_output *output)
  631. {
  632. struct vpif_display_config *config = vpif_dev->platform_data;
  633. struct video_device *vdev = video_devdata(file);
  634. struct channel_obj *ch = video_get_drvdata(vdev);
  635. struct vpif_display_chan_config *chan_cfg;
  636. chan_cfg = &config->chan_config[ch->channel_id];
  637. if (output->index >= chan_cfg->output_count) {
  638. vpif_dbg(1, debug, "Invalid output index\n");
  639. return -EINVAL;
  640. }
  641. *output = chan_cfg->outputs[output->index].output;
  642. return 0;
  643. }
  644. /**
  645. * vpif_output_to_subdev() - Maps output to sub device
  646. * @vpif_cfg - global config ptr
  647. * @chan_cfg - channel config ptr
  648. * @index - Given output index from application
  649. *
  650. * lookup the sub device information for a given output index.
  651. * we report all the output to application. output table also
  652. * has sub device name for the each output
  653. */
  654. static int
  655. vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
  656. struct vpif_display_chan_config *chan_cfg, int index)
  657. {
  658. struct vpif_subdev_info *subdev_info;
  659. const char *subdev_name;
  660. int i;
  661. vpif_dbg(2, debug, "vpif_output_to_subdev\n");
  662. if (chan_cfg->outputs == NULL)
  663. return -1;
  664. subdev_name = chan_cfg->outputs[index].subdev_name;
  665. if (subdev_name == NULL)
  666. return -1;
  667. /* loop through the sub device list to get the sub device info */
  668. for (i = 0; i < vpif_cfg->subdev_count; i++) {
  669. subdev_info = &vpif_cfg->subdevinfo[i];
  670. if (!strcmp(subdev_info->name, subdev_name))
  671. return i;
  672. }
  673. return -1;
  674. }
  675. /**
  676. * vpif_set_output() - Select an output
  677. * @vpif_cfg - global config ptr
  678. * @ch - channel
  679. * @index - Given output index from application
  680. *
  681. * Select the given output.
  682. */
  683. static int vpif_set_output(struct vpif_display_config *vpif_cfg,
  684. struct channel_obj *ch, int index)
  685. {
  686. struct vpif_display_chan_config *chan_cfg =
  687. &vpif_cfg->chan_config[ch->channel_id];
  688. struct vpif_subdev_info *subdev_info = NULL;
  689. struct v4l2_subdev *sd = NULL;
  690. u32 input = 0, output = 0;
  691. int sd_index;
  692. int ret;
  693. sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
  694. if (sd_index >= 0) {
  695. sd = vpif_obj.sd[sd_index];
  696. subdev_info = &vpif_cfg->subdevinfo[sd_index];
  697. }
  698. if (sd) {
  699. input = chan_cfg->outputs[index].input_route;
  700. output = chan_cfg->outputs[index].output_route;
  701. ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
  702. if (ret < 0 && ret != -ENOIOCTLCMD) {
  703. vpif_err("Failed to set output\n");
  704. return ret;
  705. }
  706. }
  707. ch->output_idx = index;
  708. ch->sd = sd;
  709. if (chan_cfg->outputs != NULL)
  710. /* update tvnorms from the sub device output info */
  711. ch->video_dev->tvnorms = chan_cfg->outputs[index].output.std;
  712. return 0;
  713. }
  714. static int vpif_s_output(struct file *file, void *priv, unsigned int i)
  715. {
  716. struct vpif_display_config *config = vpif_dev->platform_data;
  717. struct video_device *vdev = video_devdata(file);
  718. struct channel_obj *ch = video_get_drvdata(vdev);
  719. struct vpif_display_chan_config *chan_cfg;
  720. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  721. if (vb2_is_busy(&common->buffer_queue))
  722. return -EBUSY;
  723. chan_cfg = &config->chan_config[ch->channel_id];
  724. if (i >= chan_cfg->output_count)
  725. return -EINVAL;
  726. return vpif_set_output(config, ch, i);
  727. }
  728. static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
  729. {
  730. struct video_device *vdev = video_devdata(file);
  731. struct channel_obj *ch = video_get_drvdata(vdev);
  732. *i = ch->output_idx;
  733. return 0;
  734. }
  735. /**
  736. * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
  737. * @file: file ptr
  738. * @priv: file handle
  739. * @timings: input timings
  740. */
  741. static int
  742. vpif_enum_dv_timings(struct file *file, void *priv,
  743. struct v4l2_enum_dv_timings *timings)
  744. {
  745. struct vpif_display_config *config = vpif_dev->platform_data;
  746. struct video_device *vdev = video_devdata(file);
  747. struct channel_obj *ch = video_get_drvdata(vdev);
  748. struct vpif_display_chan_config *chan_cfg;
  749. struct v4l2_output output;
  750. int ret;
  751. if (config->chan_config[ch->channel_id].outputs == NULL)
  752. return -ENODATA;
  753. chan_cfg = &config->chan_config[ch->channel_id];
  754. output = chan_cfg->outputs[ch->output_idx].output;
  755. if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
  756. return -ENODATA;
  757. timings->pad = 0;
  758. ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
  759. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  760. return -EINVAL;
  761. return ret;
  762. }
  763. /**
  764. * vpif_s_dv_timings() - S_DV_TIMINGS handler
  765. * @file: file ptr
  766. * @priv: file handle
  767. * @timings: digital video timings
  768. */
  769. static int vpif_s_dv_timings(struct file *file, void *priv,
  770. struct v4l2_dv_timings *timings)
  771. {
  772. struct vpif_display_config *config = vpif_dev->platform_data;
  773. struct video_device *vdev = video_devdata(file);
  774. struct channel_obj *ch = video_get_drvdata(vdev);
  775. struct vpif_params *vpifparams = &ch->vpifparams;
  776. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  777. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  778. struct video_obj *vid_ch = &ch->video;
  779. struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
  780. struct vpif_display_chan_config *chan_cfg;
  781. struct v4l2_output output;
  782. int ret;
  783. if (config->chan_config[ch->channel_id].outputs == NULL)
  784. return -ENODATA;
  785. chan_cfg = &config->chan_config[ch->channel_id];
  786. output = chan_cfg->outputs[ch->output_idx].output;
  787. if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
  788. return -ENODATA;
  789. if (vb2_is_busy(&common->buffer_queue))
  790. return -EBUSY;
  791. if (timings->type != V4L2_DV_BT_656_1120) {
  792. vpif_dbg(2, debug, "Timing type not defined\n");
  793. return -EINVAL;
  794. }
  795. /* Configure subdevice timings, if any */
  796. ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
  797. if (ret == -ENOIOCTLCMD || ret == -ENODEV)
  798. ret = 0;
  799. if (ret < 0) {
  800. vpif_dbg(2, debug, "Error setting custom DV timings\n");
  801. return ret;
  802. }
  803. if (!(timings->bt.width && timings->bt.height &&
  804. (timings->bt.hbackporch ||
  805. timings->bt.hfrontporch ||
  806. timings->bt.hsync) &&
  807. timings->bt.vfrontporch &&
  808. (timings->bt.vbackporch ||
  809. timings->bt.vsync))) {
  810. vpif_dbg(2, debug, "Timings for width, height, "
  811. "horizontal back porch, horizontal sync, "
  812. "horizontal front porch, vertical back porch, "
  813. "vertical sync and vertical back porch "
  814. "must be defined\n");
  815. return -EINVAL;
  816. }
  817. vid_ch->dv_timings = *timings;
  818. /* Configure video port timings */
  819. std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
  820. std_info->sav2eav = bt->width;
  821. std_info->l1 = 1;
  822. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  823. std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
  824. if (bt->interlaced) {
  825. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  826. std_info->l5 = std_info->vsize/2 -
  827. (bt->vfrontporch - 1);
  828. std_info->l7 = std_info->vsize/2 + 1;
  829. std_info->l9 = std_info->l7 + bt->il_vsync +
  830. bt->il_vbackporch + 1;
  831. std_info->l11 = std_info->vsize -
  832. (bt->il_vfrontporch - 1);
  833. } else {
  834. vpif_dbg(2, debug, "Required timing values for "
  835. "interlaced BT format missing\n");
  836. return -EINVAL;
  837. }
  838. } else {
  839. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  840. }
  841. strncpy(std_info->name, "Custom timings BT656/1120",
  842. VPIF_MAX_NAME);
  843. std_info->width = bt->width;
  844. std_info->height = bt->height;
  845. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  846. std_info->ycmux_mode = 0;
  847. std_info->capture_format = 0;
  848. std_info->vbi_supported = 0;
  849. std_info->hd_sd = 1;
  850. std_info->stdid = 0;
  851. vid_ch->stdid = 0;
  852. return 0;
  853. }
  854. /**
  855. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  856. * @file: file ptr
  857. * @priv: file handle
  858. * @timings: digital video timings
  859. */
  860. static int vpif_g_dv_timings(struct file *file, void *priv,
  861. struct v4l2_dv_timings *timings)
  862. {
  863. struct vpif_display_config *config = vpif_dev->platform_data;
  864. struct video_device *vdev = video_devdata(file);
  865. struct channel_obj *ch = video_get_drvdata(vdev);
  866. struct vpif_display_chan_config *chan_cfg;
  867. struct video_obj *vid_ch = &ch->video;
  868. struct v4l2_output output;
  869. if (config->chan_config[ch->channel_id].outputs == NULL)
  870. goto error;
  871. chan_cfg = &config->chan_config[ch->channel_id];
  872. output = chan_cfg->outputs[ch->output_idx].output;
  873. if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
  874. goto error;
  875. *timings = vid_ch->dv_timings;
  876. return 0;
  877. error:
  878. return -ENODATA;
  879. }
  880. /*
  881. * vpif_log_status() - Status information
  882. * @file: file ptr
  883. * @priv: file handle
  884. *
  885. * Returns zero.
  886. */
  887. static int vpif_log_status(struct file *filep, void *priv)
  888. {
  889. /* status for sub devices */
  890. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  891. return 0;
  892. }
  893. /* vpif display ioctl operations */
  894. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  895. .vidioc_querycap = vpif_querycap,
  896. .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
  897. .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
  898. .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
  899. .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
  900. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  901. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  902. .vidioc_querybuf = vb2_ioctl_querybuf,
  903. .vidioc_qbuf = vb2_ioctl_qbuf,
  904. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  905. .vidioc_expbuf = vb2_ioctl_expbuf,
  906. .vidioc_streamon = vb2_ioctl_streamon,
  907. .vidioc_streamoff = vb2_ioctl_streamoff,
  908. .vidioc_s_std = vpif_s_std,
  909. .vidioc_g_std = vpif_g_std,
  910. .vidioc_enum_output = vpif_enum_output,
  911. .vidioc_s_output = vpif_s_output,
  912. .vidioc_g_output = vpif_g_output,
  913. .vidioc_enum_dv_timings = vpif_enum_dv_timings,
  914. .vidioc_s_dv_timings = vpif_s_dv_timings,
  915. .vidioc_g_dv_timings = vpif_g_dv_timings,
  916. .vidioc_log_status = vpif_log_status,
  917. };
  918. static const struct v4l2_file_operations vpif_fops = {
  919. .owner = THIS_MODULE,
  920. .open = v4l2_fh_open,
  921. .release = vb2_fop_release,
  922. .unlocked_ioctl = video_ioctl2,
  923. .mmap = vb2_fop_mmap,
  924. .poll = vb2_fop_poll
  925. };
  926. /*Configure the channels, buffer sizei, request irq */
  927. static int initialize_vpif(void)
  928. {
  929. int free_channel_objects_index;
  930. int err, i, j;
  931. /* Allocate memory for six channel objects */
  932. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  933. vpif_obj.dev[i] =
  934. kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
  935. /* If memory allocation fails, return error */
  936. if (!vpif_obj.dev[i]) {
  937. free_channel_objects_index = i;
  938. err = -ENOMEM;
  939. goto vpif_init_free_channel_objects;
  940. }
  941. }
  942. return 0;
  943. vpif_init_free_channel_objects:
  944. for (j = 0; j < free_channel_objects_index; j++)
  945. kfree(vpif_obj.dev[j]);
  946. return err;
  947. }
  948. static int vpif_async_bound(struct v4l2_async_notifier *notifier,
  949. struct v4l2_subdev *subdev,
  950. struct v4l2_async_subdev *asd)
  951. {
  952. int i;
  953. for (i = 0; i < vpif_obj.config->subdev_count; i++)
  954. if (!strcmp(vpif_obj.config->subdevinfo[i].name,
  955. subdev->name)) {
  956. vpif_obj.sd[i] = subdev;
  957. vpif_obj.sd[i]->grp_id = 1 << i;
  958. return 0;
  959. }
  960. return -EINVAL;
  961. }
  962. static int vpif_probe_complete(void)
  963. {
  964. struct common_obj *common;
  965. struct video_device *vdev;
  966. struct channel_obj *ch;
  967. struct vb2_queue *q;
  968. int j, err, k;
  969. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  970. ch = vpif_obj.dev[j];
  971. /* Initialize field of the channel objects */
  972. for (k = 0; k < VPIF_NUMOBJECTS; k++) {
  973. common = &ch->common[k];
  974. spin_lock_init(&common->irqlock);
  975. mutex_init(&common->lock);
  976. common->set_addr = NULL;
  977. common->ytop_off = 0;
  978. common->ybtm_off = 0;
  979. common->ctop_off = 0;
  980. common->cbtm_off = 0;
  981. common->cur_frm = NULL;
  982. common->next_frm = NULL;
  983. memset(&common->fmt, 0, sizeof(common->fmt));
  984. }
  985. ch->initialized = 0;
  986. if (vpif_obj.config->subdev_count)
  987. ch->sd = vpif_obj.sd[0];
  988. ch->channel_id = j;
  989. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  990. ch->common[VPIF_VIDEO_INDEX].fmt.type =
  991. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  992. /* select output 0 */
  993. err = vpif_set_output(vpif_obj.config, ch, 0);
  994. if (err)
  995. goto probe_out;
  996. /* set initial format */
  997. ch->video.stdid = V4L2_STD_525_60;
  998. memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
  999. vpif_update_resolution(ch);
  1000. /* Initialize vb2 queue */
  1001. q = &common->buffer_queue;
  1002. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1003. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1004. q->drv_priv = ch;
  1005. q->ops = &video_qops;
  1006. q->mem_ops = &vb2_dma_contig_memops;
  1007. q->buf_struct_size = sizeof(struct vpif_disp_buffer);
  1008. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1009. q->min_buffers_needed = 1;
  1010. q->lock = &common->lock;
  1011. err = vb2_queue_init(q);
  1012. if (err) {
  1013. vpif_err("vpif_display: vb2_queue_init() failed\n");
  1014. goto probe_out;
  1015. }
  1016. common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
  1017. if (IS_ERR(common->alloc_ctx)) {
  1018. vpif_err("Failed to get the context\n");
  1019. err = PTR_ERR(common->alloc_ctx);
  1020. goto probe_out;
  1021. }
  1022. INIT_LIST_HEAD(&common->dma_queue);
  1023. /* register video device */
  1024. vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
  1025. (int)ch, (int)&ch->video_dev);
  1026. /* Initialize the video_device structure */
  1027. vdev = ch->video_dev;
  1028. strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
  1029. vdev->release = video_device_release;
  1030. vdev->fops = &vpif_fops;
  1031. vdev->ioctl_ops = &vpif_ioctl_ops;
  1032. vdev->v4l2_dev = &vpif_obj.v4l2_dev;
  1033. vdev->vfl_dir = VFL_DIR_TX;
  1034. vdev->queue = q;
  1035. vdev->lock = &common->lock;
  1036. set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
  1037. video_set_drvdata(ch->video_dev, ch);
  1038. err = video_register_device(vdev, VFL_TYPE_GRABBER,
  1039. (j ? 3 : 2));
  1040. if (err < 0)
  1041. goto probe_out;
  1042. }
  1043. return 0;
  1044. probe_out:
  1045. for (k = 0; k < j; k++) {
  1046. ch = vpif_obj.dev[k];
  1047. common = &ch->common[k];
  1048. vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
  1049. video_unregister_device(ch->video_dev);
  1050. video_device_release(ch->video_dev);
  1051. ch->video_dev = NULL;
  1052. }
  1053. return err;
  1054. }
  1055. static int vpif_async_complete(struct v4l2_async_notifier *notifier)
  1056. {
  1057. return vpif_probe_complete();
  1058. }
  1059. /*
  1060. * vpif_probe: This function creates device entries by register itself to the
  1061. * V4L2 driver and initializes fields of each channel objects
  1062. */
  1063. static __init int vpif_probe(struct platform_device *pdev)
  1064. {
  1065. struct vpif_subdev_info *subdevdata;
  1066. int i, j = 0, err = 0;
  1067. int res_idx = 0;
  1068. struct i2c_adapter *i2c_adap;
  1069. struct channel_obj *ch;
  1070. struct video_device *vfd;
  1071. struct resource *res;
  1072. int subdev_count;
  1073. vpif_dev = &pdev->dev;
  1074. err = initialize_vpif();
  1075. if (err) {
  1076. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1077. return err;
  1078. }
  1079. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1080. if (err) {
  1081. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1082. return err;
  1083. }
  1084. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
  1085. err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
  1086. IRQF_SHARED, VPIF_DRIVER_NAME,
  1087. (void *)(&vpif_obj.dev[res_idx]->
  1088. channel_id));
  1089. if (err) {
  1090. err = -EINVAL;
  1091. vpif_err("VPIF IRQ request failed\n");
  1092. goto vpif_unregister;
  1093. }
  1094. res_idx++;
  1095. }
  1096. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1097. /* Get the pointer to the channel object */
  1098. ch = vpif_obj.dev[i];
  1099. /* Allocate memory for video device */
  1100. vfd = video_device_alloc();
  1101. if (vfd == NULL) {
  1102. for (j = 0; j < i; j++) {
  1103. ch = vpif_obj.dev[j];
  1104. video_device_release(ch->video_dev);
  1105. }
  1106. err = -ENOMEM;
  1107. goto vpif_unregister;
  1108. }
  1109. /* Set video_dev to the video device */
  1110. ch->video_dev = vfd;
  1111. }
  1112. vpif_obj.config = pdev->dev.platform_data;
  1113. subdev_count = vpif_obj.config->subdev_count;
  1114. subdevdata = vpif_obj.config->subdevinfo;
  1115. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1116. GFP_KERNEL);
  1117. if (vpif_obj.sd == NULL) {
  1118. vpif_err("unable to allocate memory for subdevice pointers\n");
  1119. err = -ENOMEM;
  1120. goto vpif_sd_error;
  1121. }
  1122. if (!vpif_obj.config->asd_sizes) {
  1123. i2c_adap = i2c_get_adapter(1);
  1124. for (i = 0; i < subdev_count; i++) {
  1125. vpif_obj.sd[i] =
  1126. v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1127. i2c_adap,
  1128. &subdevdata[i].
  1129. board_info,
  1130. NULL);
  1131. if (!vpif_obj.sd[i]) {
  1132. vpif_err("Error registering v4l2 subdevice\n");
  1133. err = -ENODEV;
  1134. goto probe_subdev_out;
  1135. }
  1136. if (vpif_obj.sd[i])
  1137. vpif_obj.sd[i]->grp_id = 1 << i;
  1138. }
  1139. vpif_probe_complete();
  1140. } else {
  1141. vpif_obj.notifier.subdevs = vpif_obj.config->asd;
  1142. vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
  1143. vpif_obj.notifier.bound = vpif_async_bound;
  1144. vpif_obj.notifier.complete = vpif_async_complete;
  1145. err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
  1146. &vpif_obj.notifier);
  1147. if (err) {
  1148. vpif_err("Error registering async notifier\n");
  1149. err = -EINVAL;
  1150. goto probe_subdev_out;
  1151. }
  1152. }
  1153. return 0;
  1154. probe_subdev_out:
  1155. kfree(vpif_obj.sd);
  1156. vpif_sd_error:
  1157. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1158. ch = vpif_obj.dev[i];
  1159. /* Note: does nothing if ch->video_dev == NULL */
  1160. video_device_release(ch->video_dev);
  1161. }
  1162. vpif_unregister:
  1163. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1164. return err;
  1165. }
  1166. /*
  1167. * vpif_remove: It un-register channels from V4L2 driver
  1168. */
  1169. static int vpif_remove(struct platform_device *device)
  1170. {
  1171. struct common_obj *common;
  1172. struct channel_obj *ch;
  1173. int i;
  1174. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1175. kfree(vpif_obj.sd);
  1176. /* un-register device */
  1177. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1178. /* Get the pointer to the channel object */
  1179. ch = vpif_obj.dev[i];
  1180. common = &ch->common[i];
  1181. vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
  1182. /* Unregister video device */
  1183. video_unregister_device(ch->video_dev);
  1184. ch->video_dev = NULL;
  1185. kfree(vpif_obj.dev[i]);
  1186. }
  1187. return 0;
  1188. }
  1189. #ifdef CONFIG_PM_SLEEP
  1190. static int vpif_suspend(struct device *dev)
  1191. {
  1192. struct common_obj *common;
  1193. struct channel_obj *ch;
  1194. int i;
  1195. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1196. /* Get the pointer to the channel object */
  1197. ch = vpif_obj.dev[i];
  1198. common = &ch->common[VPIF_VIDEO_INDEX];
  1199. if (!vb2_is_streaming(&common->buffer_queue))
  1200. continue;
  1201. mutex_lock(&common->lock);
  1202. /* Disable channel */
  1203. if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
  1204. enable_channel2(0);
  1205. channel2_intr_enable(0);
  1206. }
  1207. if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
  1208. ycmux_mode == 2) {
  1209. enable_channel3(0);
  1210. channel3_intr_enable(0);
  1211. }
  1212. mutex_unlock(&common->lock);
  1213. }
  1214. return 0;
  1215. }
  1216. static int vpif_resume(struct device *dev)
  1217. {
  1218. struct common_obj *common;
  1219. struct channel_obj *ch;
  1220. int i;
  1221. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1222. /* Get the pointer to the channel object */
  1223. ch = vpif_obj.dev[i];
  1224. common = &ch->common[VPIF_VIDEO_INDEX];
  1225. if (!vb2_is_streaming(&common->buffer_queue))
  1226. continue;
  1227. mutex_lock(&common->lock);
  1228. /* Enable channel */
  1229. if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
  1230. enable_channel2(1);
  1231. channel2_intr_enable(1);
  1232. }
  1233. if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
  1234. ycmux_mode == 2) {
  1235. enable_channel3(1);
  1236. channel3_intr_enable(1);
  1237. }
  1238. mutex_unlock(&common->lock);
  1239. }
  1240. return 0;
  1241. }
  1242. #endif
  1243. static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
  1244. static __refdata struct platform_driver vpif_driver = {
  1245. .driver = {
  1246. .name = VPIF_DRIVER_NAME,
  1247. .owner = THIS_MODULE,
  1248. .pm = &vpif_pm_ops,
  1249. },
  1250. .probe = vpif_probe,
  1251. .remove = vpif_remove,
  1252. };
  1253. module_platform_driver(vpif_driver);