vpif_display.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  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. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  10. *
  11. * This program is distributed .as is. WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/mm.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/string.h>
  25. #include <linux/videodev2.h>
  26. #include <linux/wait.h>
  27. #include <linux/time.h>
  28. #include <linux/i2c.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/io.h>
  31. #include <linux/version.h>
  32. #include <asm/irq.h>
  33. #include <asm/page.h>
  34. #include <media/adv7343.h>
  35. #include <media/v4l2-device.h>
  36. #include <media/v4l2-ioctl.h>
  37. #include <mach/dm646x.h>
  38. #include "vpif_display.h"
  39. #include "vpif.h"
  40. MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
  41. MODULE_LICENSE("GPL");
  42. #define DM646X_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
  43. #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
  44. #define vpif_dbg(level, debug, fmt, arg...) \
  45. v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
  46. static int debug = 1;
  47. static u32 ch2_numbuffers = 3;
  48. static u32 ch3_numbuffers = 3;
  49. static u32 ch2_bufsize = 1920 * 1080 * 2;
  50. static u32 ch3_bufsize = 720 * 576 * 2;
  51. module_param(debug, int, 0644);
  52. module_param(ch2_numbuffers, uint, S_IRUGO);
  53. module_param(ch3_numbuffers, uint, S_IRUGO);
  54. module_param(ch2_bufsize, uint, S_IRUGO);
  55. module_param(ch3_bufsize, uint, S_IRUGO);
  56. MODULE_PARM_DESC(debug, "Debug level 0-1");
  57. MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
  58. MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
  59. MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
  60. MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
  61. static struct vpif_config_params config_params = {
  62. .min_numbuffers = 3,
  63. .numbuffers[0] = 3,
  64. .numbuffers[1] = 3,
  65. .min_bufsize[0] = 720 * 480 * 2,
  66. .min_bufsize[1] = 720 * 480 * 2,
  67. .channel_bufsize[0] = 1920 * 1080 * 2,
  68. .channel_bufsize[1] = 720 * 576 * 2,
  69. };
  70. static struct vpif_device vpif_obj = { {NULL} };
  71. static struct device *vpif_dev;
  72. static const struct vpif_channel_config_params ch_params[] = {
  73. {
  74. "NTSC", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
  75. 286, 525, 525, 0, 1, 0, V4L2_STD_525_60,
  76. },
  77. {
  78. "PAL", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
  79. 336, 624, 625, 0, 1, 0, V4L2_STD_625_50,
  80. },
  81. };
  82. /*
  83. * vpif_uservirt_to_phys: This function is used to convert user
  84. * space virtual address to physical address.
  85. */
  86. static u32 vpif_uservirt_to_phys(u32 virtp)
  87. {
  88. struct mm_struct *mm = current->mm;
  89. unsigned long physp = 0;
  90. struct vm_area_struct *vma;
  91. vma = find_vma(mm, virtp);
  92. /* For kernel direct-mapped memory, take the easy way */
  93. if (virtp >= PAGE_OFFSET) {
  94. physp = virt_to_phys((void *)virtp);
  95. } else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
  96. /* this will catch, kernel-allocated, mmaped-to-usermode addr */
  97. physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
  98. } else {
  99. /* otherwise, use get_user_pages() for general userland pages */
  100. int res, nr_pages = 1;
  101. struct page *pages;
  102. down_read(&current->mm->mmap_sem);
  103. res = get_user_pages(current, current->mm,
  104. virtp, nr_pages, 1, 0, &pages, NULL);
  105. up_read(&current->mm->mmap_sem);
  106. if (res == nr_pages) {
  107. physp = __pa(page_address(&pages[0]) +
  108. (virtp & ~PAGE_MASK));
  109. } else {
  110. vpif_err("get_user_pages failed\n");
  111. return 0;
  112. }
  113. }
  114. return physp;
  115. }
  116. /*
  117. * buffer_prepare: This is the callback function called from videobuf_qbuf()
  118. * function the buffer is prepared and user space virtual address is converted
  119. * into physical address
  120. */
  121. static int vpif_buffer_prepare(struct videobuf_queue *q,
  122. struct videobuf_buffer *vb,
  123. enum v4l2_field field)
  124. {
  125. struct vpif_fh *fh = q->priv_data;
  126. struct common_obj *common;
  127. unsigned long addr;
  128. common = &fh->channel->common[VPIF_VIDEO_INDEX];
  129. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  130. vb->width = common->width;
  131. vb->height = common->height;
  132. vb->size = vb->width * vb->height;
  133. vb->field = field;
  134. }
  135. vb->state = VIDEOBUF_PREPARED;
  136. /* if user pointer memory mechanism is used, get the physical
  137. * address of the buffer */
  138. if (V4L2_MEMORY_USERPTR == common->memory) {
  139. if (!vb->baddr) {
  140. vpif_err("buffer_address is 0\n");
  141. return -EINVAL;
  142. }
  143. vb->boff = vpif_uservirt_to_phys(vb->baddr);
  144. if (!ISALIGNED(vb->boff))
  145. goto buf_align_exit;
  146. }
  147. addr = vb->boff;
  148. if (q->streaming && (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
  149. if (!ISALIGNED(addr + common->ytop_off) ||
  150. !ISALIGNED(addr + common->ybtm_off) ||
  151. !ISALIGNED(addr + common->ctop_off) ||
  152. !ISALIGNED(addr + common->cbtm_off))
  153. goto buf_align_exit;
  154. }
  155. return 0;
  156. buf_align_exit:
  157. vpif_err("buffer offset not aligned to 8 bytes\n");
  158. return -EINVAL;
  159. }
  160. /*
  161. * vpif_buffer_setup: This function allocates memory for the buffers
  162. */
  163. static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  164. unsigned int *size)
  165. {
  166. struct vpif_fh *fh = q->priv_data;
  167. struct channel_obj *ch = fh->channel;
  168. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  169. if (V4L2_MEMORY_MMAP != common->memory)
  170. return 0;
  171. *size = config_params.channel_bufsize[ch->channel_id];
  172. if (*count < config_params.min_numbuffers)
  173. *count = config_params.min_numbuffers;
  174. return 0;
  175. }
  176. /*
  177. * vpif_buffer_queue: This function adds the buffer to DMA queue
  178. */
  179. static void vpif_buffer_queue(struct videobuf_queue *q,
  180. struct videobuf_buffer *vb)
  181. {
  182. struct vpif_fh *fh = q->priv_data;
  183. struct common_obj *common;
  184. common = &fh->channel->common[VPIF_VIDEO_INDEX];
  185. /* add the buffer to the DMA queue */
  186. list_add_tail(&vb->queue, &common->dma_queue);
  187. vb->state = VIDEOBUF_QUEUED;
  188. }
  189. /*
  190. * vpif_buffer_release: This function is called from the videobuf layer to
  191. * free memory allocated to the buffers
  192. */
  193. static void vpif_buffer_release(struct videobuf_queue *q,
  194. struct videobuf_buffer *vb)
  195. {
  196. struct vpif_fh *fh = q->priv_data;
  197. struct channel_obj *ch = fh->channel;
  198. struct common_obj *common;
  199. unsigned int buf_size = 0;
  200. common = &ch->common[VPIF_VIDEO_INDEX];
  201. videobuf_dma_contig_free(q, vb);
  202. vb->state = VIDEOBUF_NEEDS_INIT;
  203. if (V4L2_MEMORY_MMAP != common->memory)
  204. return;
  205. buf_size = config_params.channel_bufsize[ch->channel_id];
  206. }
  207. static struct videobuf_queue_ops video_qops = {
  208. .buf_setup = vpif_buffer_setup,
  209. .buf_prepare = vpif_buffer_prepare,
  210. .buf_queue = vpif_buffer_queue,
  211. .buf_release = vpif_buffer_release,
  212. };
  213. static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
  214. static void process_progressive_mode(struct common_obj *common)
  215. {
  216. unsigned long addr = 0;
  217. /* Get the next buffer from buffer queue */
  218. common->next_frm = list_entry(common->dma_queue.next,
  219. struct videobuf_buffer, queue);
  220. /* Remove that buffer from the buffer queue */
  221. list_del(&common->next_frm->queue);
  222. /* Mark status of the buffer as active */
  223. common->next_frm->state = VIDEOBUF_ACTIVE;
  224. /* Set top and bottom field addrs in VPIF registers */
  225. addr = videobuf_to_dma_contig(common->next_frm);
  226. common->set_addr(addr + common->ytop_off,
  227. addr + common->ybtm_off,
  228. addr + common->ctop_off,
  229. addr + common->cbtm_off);
  230. }
  231. static void process_interlaced_mode(int fid, struct common_obj *common)
  232. {
  233. /* device field id and local field id are in sync */
  234. /* If this is even field */
  235. if (0 == fid) {
  236. if (common->cur_frm == common->next_frm)
  237. return;
  238. /* one frame is displayed If next frame is
  239. * available, release cur_frm and move on */
  240. /* Copy frame display time */
  241. do_gettimeofday(&common->cur_frm->ts);
  242. /* Change status of the cur_frm */
  243. common->cur_frm->state = VIDEOBUF_DONE;
  244. /* unlock semaphore on cur_frm */
  245. wake_up_interruptible(&common->cur_frm->done);
  246. /* Make cur_frm pointing to next_frm */
  247. common->cur_frm = common->next_frm;
  248. } else if (1 == fid) { /* odd field */
  249. if (list_empty(&common->dma_queue)
  250. || (common->cur_frm != common->next_frm)) {
  251. return;
  252. }
  253. /* one field is displayed configure the next
  254. * frame if it is available else hold on current
  255. * frame */
  256. /* Get next from the buffer queue */
  257. process_progressive_mode(common);
  258. }
  259. }
  260. /*
  261. * vpif_channel_isr: It changes status of the displayed buffer, takes next
  262. * buffer from the queue and sets its address in VPIF registers
  263. */
  264. static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
  265. {
  266. struct vpif_device *dev = &vpif_obj;
  267. struct channel_obj *ch;
  268. struct common_obj *common;
  269. enum v4l2_field field;
  270. int fid = -1, i;
  271. int channel_id = 0;
  272. channel_id = *(int *)(dev_id);
  273. ch = dev->dev[channel_id];
  274. field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
  275. for (i = 0; i < VPIF_NUMOBJECTS; i++) {
  276. common = &ch->common[i];
  277. /* If streaming is started in this channel */
  278. if (0 == common->started)
  279. continue;
  280. if (1 == ch->vpifparams.std_info.frm_fmt) {
  281. if (list_empty(&common->dma_queue))
  282. continue;
  283. /* Progressive mode */
  284. if (!channel_first_int[i][channel_id]) {
  285. /* Mark status of the cur_frm to
  286. * done and unlock semaphore on it */
  287. do_gettimeofday(&common->cur_frm->ts);
  288. common->cur_frm->state = VIDEOBUF_DONE;
  289. wake_up_interruptible(&common->cur_frm->done);
  290. /* Make cur_frm pointing to next_frm */
  291. common->cur_frm = common->next_frm;
  292. }
  293. channel_first_int[i][channel_id] = 0;
  294. process_progressive_mode(common);
  295. } else {
  296. /* Interlaced mode */
  297. /* If it is first interrupt, ignore it */
  298. if (channel_first_int[i][channel_id]) {
  299. channel_first_int[i][channel_id] = 0;
  300. continue;
  301. }
  302. if (0 == i) {
  303. ch->field_id ^= 1;
  304. /* Get field id from VPIF registers */
  305. fid = vpif_channel_getfid(ch->channel_id + 2);
  306. /* If fid does not match with stored field id */
  307. if (fid != ch->field_id) {
  308. /* Make them in sync */
  309. if (0 == fid)
  310. ch->field_id = fid;
  311. return IRQ_HANDLED;
  312. }
  313. }
  314. process_interlaced_mode(fid, common);
  315. }
  316. }
  317. return IRQ_HANDLED;
  318. }
  319. static int vpif_get_std_info(struct channel_obj *ch)
  320. {
  321. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  322. struct video_obj *vid_ch = &ch->video;
  323. struct vpif_params *vpifparams = &ch->vpifparams;
  324. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  325. const struct vpif_channel_config_params *config;
  326. int index;
  327. std_info->stdid = vid_ch->stdid;
  328. if (!std_info)
  329. return -1;
  330. for (index = 0; index < ARRAY_SIZE(ch_params); index++) {
  331. config = &ch_params[index];
  332. if (config->stdid & std_info->stdid) {
  333. memcpy(std_info, config, sizeof(*config));
  334. break;
  335. }
  336. }
  337. if (index == ARRAY_SIZE(ch_params))
  338. return -1;
  339. common->fmt.fmt.pix.width = std_info->width;
  340. common->fmt.fmt.pix.height = std_info->height;
  341. vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
  342. common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
  343. /* Set height and width paramateres */
  344. ch->common[VPIF_VIDEO_INDEX].height = std_info->height;
  345. ch->common[VPIF_VIDEO_INDEX].width = std_info->width;
  346. return 0;
  347. }
  348. /*
  349. * vpif_calculate_offsets: This function calculates buffers offset for Y and C
  350. * in the top and bottom field
  351. */
  352. static void vpif_calculate_offsets(struct channel_obj *ch)
  353. {
  354. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  355. struct vpif_params *vpifparams = &ch->vpifparams;
  356. enum v4l2_field field = common->fmt.fmt.pix.field;
  357. struct video_obj *vid_ch = &ch->video;
  358. unsigned int hpitch, vpitch, sizeimage;
  359. if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
  360. if (ch->vpifparams.std_info.frm_fmt)
  361. vid_ch->buf_field = V4L2_FIELD_NONE;
  362. else
  363. vid_ch->buf_field = V4L2_FIELD_INTERLACED;
  364. } else {
  365. vid_ch->buf_field = common->fmt.fmt.pix.field;
  366. }
  367. if (V4L2_MEMORY_USERPTR == common->memory)
  368. sizeimage = common->fmt.fmt.pix.sizeimage;
  369. else
  370. sizeimage = config_params.channel_bufsize[ch->channel_id];
  371. hpitch = common->fmt.fmt.pix.bytesperline;
  372. vpitch = sizeimage / (hpitch * 2);
  373. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  374. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  375. common->ytop_off = 0;
  376. common->ybtm_off = hpitch;
  377. common->ctop_off = sizeimage / 2;
  378. common->cbtm_off = sizeimage / 2 + hpitch;
  379. } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
  380. common->ytop_off = 0;
  381. common->ybtm_off = sizeimage / 4;
  382. common->ctop_off = sizeimage / 2;
  383. common->cbtm_off = common->ctop_off + sizeimage / 4;
  384. } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
  385. common->ybtm_off = 0;
  386. common->ytop_off = sizeimage / 4;
  387. common->cbtm_off = sizeimage / 2;
  388. common->ctop_off = common->cbtm_off + sizeimage / 4;
  389. }
  390. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  391. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  392. vpifparams->video_params.storage_mode = 1;
  393. } else {
  394. vpifparams->video_params.storage_mode = 0;
  395. }
  396. if (ch->vpifparams.std_info.frm_fmt == 1) {
  397. vpifparams->video_params.hpitch =
  398. common->fmt.fmt.pix.bytesperline;
  399. } else {
  400. if ((field == V4L2_FIELD_ANY) ||
  401. (field == V4L2_FIELD_INTERLACED))
  402. vpifparams->video_params.hpitch =
  403. common->fmt.fmt.pix.bytesperline * 2;
  404. else
  405. vpifparams->video_params.hpitch =
  406. common->fmt.fmt.pix.bytesperline;
  407. }
  408. ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
  409. }
  410. static void vpif_config_format(struct channel_obj *ch)
  411. {
  412. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  413. common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
  414. if (config_params.numbuffers[ch->channel_id] == 0)
  415. common->memory = V4L2_MEMORY_USERPTR;
  416. else
  417. common->memory = V4L2_MEMORY_MMAP;
  418. common->fmt.fmt.pix.sizeimage =
  419. config_params.channel_bufsize[ch->channel_id];
  420. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  421. common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  422. }
  423. static int vpif_check_format(struct channel_obj *ch,
  424. struct v4l2_pix_format *pixfmt)
  425. {
  426. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  427. enum v4l2_field field = pixfmt->field;
  428. u32 sizeimage, hpitch, vpitch;
  429. if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
  430. goto invalid_fmt_exit;
  431. if (!(VPIF_VALID_FIELD(field)))
  432. goto invalid_fmt_exit;
  433. if (pixfmt->bytesperline <= 0)
  434. goto invalid_pitch_exit;
  435. if (V4L2_MEMORY_USERPTR == common->memory)
  436. sizeimage = pixfmt->sizeimage;
  437. else
  438. sizeimage = config_params.channel_bufsize[ch->channel_id];
  439. if (vpif_get_std_info(ch)) {
  440. vpif_err("Error getting the standard info\n");
  441. return -EINVAL;
  442. }
  443. hpitch = pixfmt->bytesperline;
  444. vpitch = sizeimage / (hpitch * 2);
  445. /* Check for valid value of pitch */
  446. if ((hpitch < ch->vpifparams.std_info.width) ||
  447. (vpitch < ch->vpifparams.std_info.height))
  448. goto invalid_pitch_exit;
  449. /* Check for 8 byte alignment */
  450. if (!ISALIGNED(hpitch)) {
  451. vpif_err("invalid pitch alignment\n");
  452. return -EINVAL;
  453. }
  454. pixfmt->width = common->fmt.fmt.pix.width;
  455. pixfmt->height = common->fmt.fmt.pix.height;
  456. return 0;
  457. invalid_fmt_exit:
  458. vpif_err("invalid field format\n");
  459. return -EINVAL;
  460. invalid_pitch_exit:
  461. vpif_err("invalid pitch\n");
  462. return -EINVAL;
  463. }
  464. static void vpif_config_addr(struct channel_obj *ch, int muxmode)
  465. {
  466. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  467. if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
  468. common->set_addr = ch3_set_videobuf_addr;
  469. } else {
  470. if (2 == muxmode)
  471. common->set_addr = ch2_set_videobuf_addr_yc_nmux;
  472. else
  473. common->set_addr = ch2_set_videobuf_addr;
  474. }
  475. }
  476. /*
  477. * vpif_mmap: It is used to map kernel space buffers into user spaces
  478. */
  479. static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
  480. {
  481. struct vpif_fh *fh = filep->private_data;
  482. struct common_obj *common = &fh->channel->common[VPIF_VIDEO_INDEX];
  483. return videobuf_mmap_mapper(&common->buffer_queue, vma);
  484. }
  485. /*
  486. * vpif_poll: It is used for select/poll system call
  487. */
  488. static unsigned int vpif_poll(struct file *filep, poll_table *wait)
  489. {
  490. struct vpif_fh *fh = filep->private_data;
  491. struct channel_obj *ch = fh->channel;
  492. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  493. if (common->started)
  494. return videobuf_poll_stream(filep, &common->buffer_queue, wait);
  495. return 0;
  496. }
  497. /*
  498. * vpif_open: It creates object of file handle structure and stores it in
  499. * private_data member of filepointer
  500. */
  501. static int vpif_open(struct file *filep)
  502. {
  503. struct video_device *vdev = video_devdata(filep);
  504. struct channel_obj *ch = NULL;
  505. struct vpif_fh *fh = NULL;
  506. ch = video_get_drvdata(vdev);
  507. /* Allocate memory for the file handle object */
  508. fh = kmalloc(sizeof(struct vpif_fh), GFP_KERNEL);
  509. if (fh == NULL) {
  510. vpif_err("unable to allocate memory for file handle object\n");
  511. return -ENOMEM;
  512. }
  513. /* store pointer to fh in private_data member of filep */
  514. filep->private_data = fh;
  515. fh->channel = ch;
  516. fh->initialized = 0;
  517. if (!ch->initialized) {
  518. fh->initialized = 1;
  519. ch->initialized = 1;
  520. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  521. }
  522. /* Increment channel usrs counter */
  523. atomic_inc(&ch->usrs);
  524. /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
  525. fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
  526. /* Initialize priority of this instance to default priority */
  527. fh->prio = V4L2_PRIORITY_UNSET;
  528. v4l2_prio_open(&ch->prio, &fh->prio);
  529. return 0;
  530. }
  531. /*
  532. * vpif_release: This function deletes buffer queue, frees the buffers and
  533. * the vpif file handle
  534. */
  535. static int vpif_release(struct file *filep)
  536. {
  537. struct vpif_fh *fh = filep->private_data;
  538. struct channel_obj *ch = fh->channel;
  539. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  540. if (mutex_lock_interruptible(&common->lock))
  541. return -ERESTARTSYS;
  542. /* if this instance is doing IO */
  543. if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
  544. /* Reset io_usrs member of channel object */
  545. common->io_usrs = 0;
  546. /* Disable channel */
  547. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  548. enable_channel2(0);
  549. channel2_intr_enable(0);
  550. }
  551. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  552. (2 == common->started)) {
  553. enable_channel3(0);
  554. channel3_intr_enable(0);
  555. }
  556. common->started = 0;
  557. /* Free buffers allocated */
  558. videobuf_queue_cancel(&common->buffer_queue);
  559. videobuf_mmap_free(&common->buffer_queue);
  560. common->numbuffers =
  561. config_params.numbuffers[ch->channel_id];
  562. }
  563. mutex_unlock(&common->lock);
  564. /* Decrement channel usrs counter */
  565. atomic_dec(&ch->usrs);
  566. /* If this file handle has initialize encoder device, reset it */
  567. if (fh->initialized)
  568. ch->initialized = 0;
  569. /* Close the priority */
  570. v4l2_prio_close(&ch->prio, &fh->prio);
  571. filep->private_data = NULL;
  572. fh->initialized = 0;
  573. kfree(fh);
  574. return 0;
  575. }
  576. /* functions implementing ioctls */
  577. static int vpif_querycap(struct file *file, void *priv,
  578. struct v4l2_capability *cap)
  579. {
  580. struct vpif_display_config *config = vpif_dev->platform_data;
  581. cap->version = VPIF_DISPLAY_VERSION_CODE;
  582. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  583. strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
  584. strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
  585. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  586. return 0;
  587. }
  588. static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
  589. struct v4l2_fmtdesc *fmt)
  590. {
  591. if (fmt->index != 0) {
  592. vpif_err("Invalid format index\n");
  593. return -EINVAL;
  594. }
  595. /* Fill in the information about format */
  596. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  597. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  598. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  599. return 0;
  600. }
  601. static int vpif_g_fmt_vid_out(struct file *file, void *priv,
  602. struct v4l2_format *fmt)
  603. {
  604. struct vpif_fh *fh = priv;
  605. struct channel_obj *ch = fh->channel;
  606. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  607. /* Check the validity of the buffer type */
  608. if (common->fmt.type != fmt->type)
  609. return -EINVAL;
  610. /* Fill in the information about format */
  611. if (mutex_lock_interruptible(&common->lock))
  612. return -ERESTARTSYS;
  613. if (vpif_get_std_info(ch)) {
  614. vpif_err("Error getting the standard info\n");
  615. return -EINVAL;
  616. }
  617. *fmt = common->fmt;
  618. mutex_unlock(&common->lock);
  619. return 0;
  620. }
  621. static int vpif_s_fmt_vid_out(struct file *file, void *priv,
  622. struct v4l2_format *fmt)
  623. {
  624. struct vpif_fh *fh = priv;
  625. struct v4l2_pix_format *pixfmt;
  626. struct channel_obj *ch = fh->channel;
  627. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  628. int ret = 0;
  629. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  630. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  631. if (!fh->initialized) {
  632. vpif_dbg(1, debug, "Channel Busy\n");
  633. return -EBUSY;
  634. }
  635. /* Check for the priority */
  636. ret = v4l2_prio_check(&ch->prio, &fh->prio);
  637. if (0 != ret)
  638. return ret;
  639. fh->initialized = 1;
  640. }
  641. if (common->started) {
  642. vpif_dbg(1, debug, "Streaming in progress\n");
  643. return -EBUSY;
  644. }
  645. pixfmt = &fmt->fmt.pix;
  646. /* Check for valid field format */
  647. ret = vpif_check_format(ch, pixfmt);
  648. if (ret)
  649. return ret;
  650. /* store the pix format in the channel object */
  651. common->fmt.fmt.pix = *pixfmt;
  652. /* store the format in the channel object */
  653. if (mutex_lock_interruptible(&common->lock))
  654. return -ERESTARTSYS;
  655. common->fmt = *fmt;
  656. mutex_unlock(&common->lock);
  657. return 0;
  658. }
  659. static int vpif_try_fmt_vid_out(struct file *file, void *priv,
  660. struct v4l2_format *fmt)
  661. {
  662. struct vpif_fh *fh = priv;
  663. struct channel_obj *ch = fh->channel;
  664. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  665. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  666. int ret = 0;
  667. ret = vpif_check_format(ch, pixfmt);
  668. if (ret) {
  669. *pixfmt = common->fmt.fmt.pix;
  670. pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
  671. }
  672. return ret;
  673. }
  674. static int vpif_reqbufs(struct file *file, void *priv,
  675. struct v4l2_requestbuffers *reqbuf)
  676. {
  677. struct vpif_fh *fh = priv;
  678. struct channel_obj *ch = fh->channel;
  679. struct common_obj *common;
  680. enum v4l2_field field;
  681. u8 index = 0;
  682. int ret = 0;
  683. /* This file handle has not initialized the channel,
  684. It is not allowed to do settings */
  685. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  686. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  687. if (!fh->initialized) {
  688. vpif_err("Channel Busy\n");
  689. return -EBUSY;
  690. }
  691. }
  692. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
  693. return -EINVAL;
  694. index = VPIF_VIDEO_INDEX;
  695. common = &ch->common[index];
  696. if (mutex_lock_interruptible(&common->lock))
  697. return -ERESTARTSYS;
  698. if (common->fmt.type != reqbuf->type) {
  699. ret = -EINVAL;
  700. goto reqbuf_exit;
  701. }
  702. if (0 != common->io_usrs) {
  703. ret = -EBUSY;
  704. goto reqbuf_exit;
  705. }
  706. if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  707. if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
  708. field = V4L2_FIELD_INTERLACED;
  709. else
  710. field = common->fmt.fmt.pix.field;
  711. } else {
  712. field = V4L2_VBI_INTERLACED;
  713. }
  714. /* Initialize videobuf queue as per the buffer type */
  715. videobuf_queue_dma_contig_init(&common->buffer_queue,
  716. &video_qops, NULL,
  717. &common->irqlock,
  718. reqbuf->type, field,
  719. sizeof(struct videobuf_buffer), fh);
  720. /* Set io allowed member of file handle to TRUE */
  721. fh->io_allowed[index] = 1;
  722. /* Increment io usrs member of channel object to 1 */
  723. common->io_usrs = 1;
  724. /* Store type of memory requested in channel object */
  725. common->memory = reqbuf->memory;
  726. INIT_LIST_HEAD(&common->dma_queue);
  727. /* Allocate buffers */
  728. ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
  729. reqbuf_exit:
  730. mutex_unlock(&common->lock);
  731. return ret;
  732. }
  733. static int vpif_querybuf(struct file *file, void *priv,
  734. struct v4l2_buffer *tbuf)
  735. {
  736. struct vpif_fh *fh = priv;
  737. struct channel_obj *ch = fh->channel;
  738. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  739. if (common->fmt.type != tbuf->type)
  740. return -EINVAL;
  741. return videobuf_querybuf(&common->buffer_queue, tbuf);
  742. }
  743. static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  744. {
  745. struct vpif_fh *fh = priv;
  746. struct channel_obj *ch = fh->channel;
  747. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  748. struct v4l2_buffer tbuf = *buf;
  749. struct videobuf_buffer *buf1;
  750. unsigned long addr = 0;
  751. unsigned long flags;
  752. int ret = 0;
  753. if (common->fmt.type != tbuf.type)
  754. return -EINVAL;
  755. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  756. vpif_err("fh->io_allowed\n");
  757. return -EACCES;
  758. }
  759. if (!(list_empty(&common->dma_queue)) ||
  760. (common->cur_frm != common->next_frm) ||
  761. !(common->started) ||
  762. (common->started && (0 == ch->field_id)))
  763. return videobuf_qbuf(&common->buffer_queue, buf);
  764. /* bufferqueue is empty store buffer address in VPIF registers */
  765. mutex_lock(&common->buffer_queue.vb_lock);
  766. buf1 = common->buffer_queue.bufs[tbuf.index];
  767. if (buf1->memory != tbuf.memory) {
  768. vpif_err("invalid buffer type\n");
  769. goto qbuf_exit;
  770. }
  771. if ((buf1->state == VIDEOBUF_QUEUED) ||
  772. (buf1->state == VIDEOBUF_ACTIVE)) {
  773. vpif_err("invalid state\n");
  774. goto qbuf_exit;
  775. }
  776. switch (buf1->memory) {
  777. case V4L2_MEMORY_MMAP:
  778. if (buf1->baddr == 0)
  779. goto qbuf_exit;
  780. break;
  781. case V4L2_MEMORY_USERPTR:
  782. if (tbuf.length < buf1->bsize)
  783. goto qbuf_exit;
  784. if ((VIDEOBUF_NEEDS_INIT != buf1->state)
  785. && (buf1->baddr != tbuf.m.userptr))
  786. vpif_buffer_release(&common->buffer_queue, buf1);
  787. buf1->baddr = tbuf.m.userptr;
  788. break;
  789. default:
  790. goto qbuf_exit;
  791. }
  792. local_irq_save(flags);
  793. ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
  794. common->buffer_queue.field);
  795. if (ret < 0) {
  796. local_irq_restore(flags);
  797. goto qbuf_exit;
  798. }
  799. buf1->state = VIDEOBUF_ACTIVE;
  800. addr = buf1->boff;
  801. common->next_frm = buf1;
  802. if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
  803. common->set_addr((addr + common->ytop_off),
  804. (addr + common->ybtm_off),
  805. (addr + common->ctop_off),
  806. (addr + common->cbtm_off));
  807. }
  808. local_irq_restore(flags);
  809. list_add_tail(&buf1->stream, &common->buffer_queue.stream);
  810. mutex_unlock(&common->buffer_queue.vb_lock);
  811. return 0;
  812. qbuf_exit:
  813. mutex_unlock(&common->buffer_queue.vb_lock);
  814. return -EINVAL;
  815. }
  816. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  817. {
  818. struct vpif_fh *fh = priv;
  819. struct channel_obj *ch = fh->channel;
  820. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  821. int ret = 0;
  822. if (!(*std_id & DM646X_V4L2_STD))
  823. return -EINVAL;
  824. if (common->started) {
  825. vpif_err("streaming in progress\n");
  826. return -EBUSY;
  827. }
  828. /* Call encoder subdevice function to set the standard */
  829. if (mutex_lock_interruptible(&common->lock))
  830. return -ERESTARTSYS;
  831. ch->video.stdid = *std_id;
  832. /* Get the information about the standard */
  833. if (vpif_get_std_info(ch)) {
  834. vpif_err("Error getting the standard info\n");
  835. return -EINVAL;
  836. }
  837. if ((ch->vpifparams.std_info.width *
  838. ch->vpifparams.std_info.height * 2) >
  839. config_params.channel_bufsize[ch->channel_id]) {
  840. vpif_err("invalid std for this size\n");
  841. ret = -EINVAL;
  842. goto s_std_exit;
  843. }
  844. common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
  845. /* Configure the default format information */
  846. vpif_config_format(ch);
  847. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  848. s_std_output, *std_id);
  849. if (ret < 0) {
  850. vpif_err("Failed to set output standard\n");
  851. goto s_std_exit;
  852. }
  853. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
  854. s_std, *std_id);
  855. if (ret < 0)
  856. vpif_err("Failed to set standard for sub devices\n");
  857. s_std_exit:
  858. mutex_unlock(&common->lock);
  859. return ret;
  860. }
  861. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  862. {
  863. struct vpif_fh *fh = priv;
  864. struct channel_obj *ch = fh->channel;
  865. *std = ch->video.stdid;
  866. return 0;
  867. }
  868. static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  869. {
  870. struct vpif_fh *fh = priv;
  871. struct channel_obj *ch = fh->channel;
  872. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  873. return videobuf_dqbuf(&common->buffer_queue, p,
  874. (file->f_flags & O_NONBLOCK));
  875. }
  876. static int vpif_streamon(struct file *file, void *priv,
  877. enum v4l2_buf_type buftype)
  878. {
  879. struct vpif_fh *fh = priv;
  880. struct channel_obj *ch = fh->channel;
  881. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  882. struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
  883. struct vpif_params *vpif = &ch->vpifparams;
  884. struct vpif_display_config *vpif_config_data =
  885. vpif_dev->platform_data;
  886. unsigned long addr = 0;
  887. int ret = 0;
  888. if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  889. vpif_err("buffer type not supported\n");
  890. return -EINVAL;
  891. }
  892. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  893. vpif_err("fh->io_allowed\n");
  894. return -EACCES;
  895. }
  896. /* If Streaming is already started, return error */
  897. if (common->started) {
  898. vpif_err("channel->started\n");
  899. return -EBUSY;
  900. }
  901. if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
  902. && oth_ch->common[VPIF_VIDEO_INDEX].started &&
  903. ch->vpifparams.std_info.ycmux_mode == 0)
  904. || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
  905. && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
  906. vpif_err("other channel is using\n");
  907. return -EBUSY;
  908. }
  909. ret = vpif_check_format(ch, &common->fmt.fmt.pix);
  910. if (ret < 0)
  911. return ret;
  912. /* Call videobuf_streamon to start streaming in videobuf */
  913. ret = videobuf_streamon(&common->buffer_queue);
  914. if (ret < 0) {
  915. vpif_err("videobuf_streamon\n");
  916. return ret;
  917. }
  918. if (mutex_lock_interruptible(&common->lock))
  919. return -ERESTARTSYS;
  920. /* If buffer queue is empty, return error */
  921. if (list_empty(&common->dma_queue)) {
  922. vpif_err("buffer queue is empty\n");
  923. ret = -EIO;
  924. goto streamon_exit;
  925. }
  926. /* Get the next frame from the buffer queue */
  927. common->next_frm = common->cur_frm =
  928. list_entry(common->dma_queue.next,
  929. struct videobuf_buffer, queue);
  930. list_del(&common->cur_frm->queue);
  931. /* Mark state of the current frame to active */
  932. common->cur_frm->state = VIDEOBUF_ACTIVE;
  933. /* Initialize field_id and started member */
  934. ch->field_id = 0;
  935. common->started = 1;
  936. if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  937. addr = common->cur_frm->boff;
  938. /* Calculate the offset for Y and C data in the buffer */
  939. vpif_calculate_offsets(ch);
  940. if ((ch->vpifparams.std_info.frm_fmt &&
  941. ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
  942. && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
  943. || (!ch->vpifparams.std_info.frm_fmt
  944. && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
  945. vpif_err("conflict in field format and std format\n");
  946. ret = -EINVAL;
  947. goto streamon_exit;
  948. }
  949. /* clock settings */
  950. ret =
  951. vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
  952. ch->vpifparams.std_info.hd_sd);
  953. if (ret < 0) {
  954. vpif_err("can't set clock\n");
  955. goto streamon_exit;
  956. }
  957. /* set the parameters and addresses */
  958. ret = vpif_set_video_params(vpif, ch->channel_id + 2);
  959. if (ret < 0)
  960. goto streamon_exit;
  961. common->started = ret;
  962. vpif_config_addr(ch, ret);
  963. common->set_addr((addr + common->ytop_off),
  964. (addr + common->ybtm_off),
  965. (addr + common->ctop_off),
  966. (addr + common->cbtm_off));
  967. /* Set interrupt for both the fields in VPIF
  968. Register enable channel in VPIF register */
  969. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  970. channel2_intr_assert();
  971. channel2_intr_enable(1);
  972. enable_channel2(1);
  973. }
  974. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
  975. || (common->started == 2)) {
  976. channel3_intr_assert();
  977. channel3_intr_enable(1);
  978. enable_channel3(1);
  979. }
  980. channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
  981. }
  982. streamon_exit:
  983. mutex_unlock(&common->lock);
  984. return ret;
  985. }
  986. static int vpif_streamoff(struct file *file, void *priv,
  987. enum v4l2_buf_type buftype)
  988. {
  989. struct vpif_fh *fh = priv;
  990. struct channel_obj *ch = fh->channel;
  991. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  992. if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  993. vpif_err("buffer type not supported\n");
  994. return -EINVAL;
  995. }
  996. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  997. vpif_err("fh->io_allowed\n");
  998. return -EACCES;
  999. }
  1000. if (!common->started) {
  1001. vpif_err("channel->started\n");
  1002. return -EINVAL;
  1003. }
  1004. if (mutex_lock_interruptible(&common->lock))
  1005. return -ERESTARTSYS;
  1006. if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1007. /* disable channel */
  1008. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  1009. enable_channel2(0);
  1010. channel2_intr_enable(0);
  1011. }
  1012. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  1013. (2 == common->started)) {
  1014. enable_channel3(0);
  1015. channel3_intr_enable(0);
  1016. }
  1017. }
  1018. common->started = 0;
  1019. mutex_unlock(&common->lock);
  1020. return videobuf_streamoff(&common->buffer_queue);
  1021. }
  1022. static int vpif_cropcap(struct file *file, void *priv,
  1023. struct v4l2_cropcap *crop)
  1024. {
  1025. struct vpif_fh *fh = priv;
  1026. struct channel_obj *ch = fh->channel;
  1027. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1028. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
  1029. return -EINVAL;
  1030. crop->bounds.left = crop->bounds.top = 0;
  1031. crop->defrect.left = crop->defrect.top = 0;
  1032. crop->defrect.height = crop->bounds.height = common->height;
  1033. crop->defrect.width = crop->bounds.width = common->width;
  1034. return 0;
  1035. }
  1036. static int vpif_enum_output(struct file *file, void *fh,
  1037. struct v4l2_output *output)
  1038. {
  1039. struct vpif_display_config *config = vpif_dev->platform_data;
  1040. if (output->index >= config->output_count) {
  1041. vpif_dbg(1, debug, "Invalid output index\n");
  1042. return -EINVAL;
  1043. }
  1044. strcpy(output->name, config->output[output->index]);
  1045. output->type = V4L2_OUTPUT_TYPE_ANALOG;
  1046. output->std = DM646X_V4L2_STD;
  1047. return 0;
  1048. }
  1049. static int vpif_s_output(struct file *file, void *priv, unsigned int i)
  1050. {
  1051. struct vpif_fh *fh = priv;
  1052. struct channel_obj *ch = fh->channel;
  1053. struct video_obj *vid_ch = &ch->video;
  1054. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1055. int ret = 0;
  1056. if (mutex_lock_interruptible(&common->lock))
  1057. return -ERESTARTSYS;
  1058. if (common->started) {
  1059. vpif_err("Streaming in progress\n");
  1060. ret = -EBUSY;
  1061. goto s_output_exit;
  1062. }
  1063. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  1064. s_routing, 0, i, 0);
  1065. if (ret < 0)
  1066. vpif_err("Failed to set output standard\n");
  1067. vid_ch->output_id = i;
  1068. s_output_exit:
  1069. mutex_unlock(&common->lock);
  1070. return ret;
  1071. }
  1072. static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
  1073. {
  1074. struct vpif_fh *fh = priv;
  1075. struct channel_obj *ch = fh->channel;
  1076. struct video_obj *vid_ch = &ch->video;
  1077. *i = vid_ch->output_id;
  1078. return 0;
  1079. }
  1080. static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
  1081. {
  1082. struct vpif_fh *fh = priv;
  1083. struct channel_obj *ch = fh->channel;
  1084. *p = v4l2_prio_max(&ch->prio);
  1085. return 0;
  1086. }
  1087. static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
  1088. {
  1089. struct vpif_fh *fh = priv;
  1090. struct channel_obj *ch = fh->channel;
  1091. return v4l2_prio_change(&ch->prio, &fh->prio, p);
  1092. }
  1093. /* vpif display ioctl operations */
  1094. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1095. .vidioc_querycap = vpif_querycap,
  1096. .vidioc_g_priority = vpif_g_priority,
  1097. .vidioc_s_priority = vpif_s_priority,
  1098. .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
  1099. .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
  1100. .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
  1101. .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
  1102. .vidioc_reqbufs = vpif_reqbufs,
  1103. .vidioc_querybuf = vpif_querybuf,
  1104. .vidioc_qbuf = vpif_qbuf,
  1105. .vidioc_dqbuf = vpif_dqbuf,
  1106. .vidioc_streamon = vpif_streamon,
  1107. .vidioc_streamoff = vpif_streamoff,
  1108. .vidioc_s_std = vpif_s_std,
  1109. .vidioc_g_std = vpif_g_std,
  1110. .vidioc_enum_output = vpif_enum_output,
  1111. .vidioc_s_output = vpif_s_output,
  1112. .vidioc_g_output = vpif_g_output,
  1113. .vidioc_cropcap = vpif_cropcap,
  1114. };
  1115. static const struct v4l2_file_operations vpif_fops = {
  1116. .owner = THIS_MODULE,
  1117. .open = vpif_open,
  1118. .release = vpif_release,
  1119. .ioctl = video_ioctl2,
  1120. .mmap = vpif_mmap,
  1121. .poll = vpif_poll
  1122. };
  1123. static struct video_device vpif_video_template = {
  1124. .name = "vpif",
  1125. .fops = &vpif_fops,
  1126. .minor = -1,
  1127. .ioctl_ops = &vpif_ioctl_ops,
  1128. .tvnorms = DM646X_V4L2_STD,
  1129. .current_norm = V4L2_STD_625_50,
  1130. };
  1131. /*Configure the channels, buffer sizei, request irq */
  1132. static int initialize_vpif(void)
  1133. {
  1134. int free_channel_objects_index;
  1135. int free_buffer_channel_index;
  1136. int free_buffer_index;
  1137. int err = 0, i, j;
  1138. /* Default number of buffers should be 3 */
  1139. if ((ch2_numbuffers > 0) &&
  1140. (ch2_numbuffers < config_params.min_numbuffers))
  1141. ch2_numbuffers = config_params.min_numbuffers;
  1142. if ((ch3_numbuffers > 0) &&
  1143. (ch3_numbuffers < config_params.min_numbuffers))
  1144. ch3_numbuffers = config_params.min_numbuffers;
  1145. /* Set buffer size to min buffers size if invalid buffer size is
  1146. * given */
  1147. if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
  1148. ch2_bufsize =
  1149. config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
  1150. if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
  1151. ch3_bufsize =
  1152. config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
  1153. config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
  1154. if (ch2_numbuffers) {
  1155. config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
  1156. ch2_bufsize;
  1157. }
  1158. config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
  1159. if (ch3_numbuffers) {
  1160. config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
  1161. ch3_bufsize;
  1162. }
  1163. /* Allocate memory for six channel objects */
  1164. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1165. vpif_obj.dev[i] =
  1166. kmalloc(sizeof(struct channel_obj), GFP_KERNEL);
  1167. /* If memory allocation fails, return error */
  1168. if (!vpif_obj.dev[i]) {
  1169. free_channel_objects_index = i;
  1170. err = -ENOMEM;
  1171. goto vpif_init_free_channel_objects;
  1172. }
  1173. }
  1174. free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
  1175. free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
  1176. free_buffer_index = config_params.numbuffers[i - 1];
  1177. return 0;
  1178. vpif_init_free_channel_objects:
  1179. for (j = 0; j < free_channel_objects_index; j++)
  1180. kfree(vpif_obj.dev[j]);
  1181. return err;
  1182. }
  1183. /*
  1184. * vpif_probe: This function creates device entries by register itself to the
  1185. * V4L2 driver and initializes fields of each channel objects
  1186. */
  1187. static __init int vpif_probe(struct platform_device *pdev)
  1188. {
  1189. struct vpif_subdev_info *subdevdata;
  1190. struct vpif_display_config *config;
  1191. int i, j = 0, k, q, m, err = 0;
  1192. struct i2c_adapter *i2c_adap;
  1193. struct common_obj *common;
  1194. struct channel_obj *ch;
  1195. struct video_device *vfd;
  1196. struct resource *res;
  1197. int subdev_count;
  1198. vpif_dev = &pdev->dev;
  1199. err = initialize_vpif();
  1200. if (err) {
  1201. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1202. return err;
  1203. }
  1204. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1205. if (err) {
  1206. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1207. return err;
  1208. }
  1209. k = 0;
  1210. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
  1211. for (i = res->start; i <= res->end; i++) {
  1212. if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
  1213. "DM646x_Display",
  1214. (void *)(&vpif_obj.dev[k]->channel_id))) {
  1215. err = -EBUSY;
  1216. goto vpif_int_err;
  1217. }
  1218. }
  1219. k++;
  1220. }
  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. /* Allocate memory for video device */
  1225. vfd = video_device_alloc();
  1226. if (vfd == NULL) {
  1227. for (j = 0; j < i; j++) {
  1228. ch = vpif_obj.dev[j];
  1229. video_device_release(ch->video_dev);
  1230. }
  1231. err = -ENOMEM;
  1232. goto vpif_int_err;
  1233. }
  1234. /* Initialize field of video device */
  1235. *vfd = vpif_video_template;
  1236. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1237. vfd->release = video_device_release;
  1238. snprintf(vfd->name, sizeof(vfd->name),
  1239. "DM646x_VPIFDisplay_DRIVER_V%d.%d.%d",
  1240. (VPIF_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1241. (VPIF_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1242. (VPIF_DISPLAY_VERSION_CODE) & 0xff);
  1243. /* Set video_dev to the video device */
  1244. ch->video_dev = vfd;
  1245. }
  1246. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1247. ch = vpif_obj.dev[j];
  1248. /* Initialize field of the channel objects */
  1249. atomic_set(&ch->usrs, 0);
  1250. for (k = 0; k < VPIF_NUMOBJECTS; k++) {
  1251. ch->common[k].numbuffers = 0;
  1252. common = &ch->common[k];
  1253. common->io_usrs = 0;
  1254. common->started = 0;
  1255. spin_lock_init(&common->irqlock);
  1256. mutex_init(&common->lock);
  1257. common->numbuffers = 0;
  1258. common->set_addr = NULL;
  1259. common->ytop_off = common->ybtm_off = 0;
  1260. common->ctop_off = common->cbtm_off = 0;
  1261. common->cur_frm = common->next_frm = NULL;
  1262. memset(&common->fmt, 0, sizeof(common->fmt));
  1263. common->numbuffers = config_params.numbuffers[k];
  1264. }
  1265. ch->initialized = 0;
  1266. ch->channel_id = j;
  1267. if (j < 2)
  1268. ch->common[VPIF_VIDEO_INDEX].numbuffers =
  1269. config_params.numbuffers[ch->channel_id];
  1270. else
  1271. ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
  1272. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  1273. /* Initialize prio member of channel object */
  1274. v4l2_prio_init(&ch->prio);
  1275. ch->common[VPIF_VIDEO_INDEX].fmt.type =
  1276. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1277. /* register video device */
  1278. vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
  1279. (int)ch, (int)&ch->video_dev);
  1280. err = video_register_device(ch->video_dev,
  1281. VFL_TYPE_GRABBER, (j ? 3 : 2));
  1282. if (err < 0)
  1283. goto probe_out;
  1284. video_set_drvdata(ch->video_dev, ch);
  1285. }
  1286. i2c_adap = i2c_get_adapter(1);
  1287. config = pdev->dev.platform_data;
  1288. subdev_count = config->subdev_count;
  1289. subdevdata = config->subdevinfo;
  1290. vpif_obj.sd = kmalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1291. GFP_KERNEL);
  1292. if (vpif_obj.sd == NULL) {
  1293. vpif_err("unable to allocate memory for subdevice pointers\n");
  1294. err = -ENOMEM;
  1295. goto probe_out;
  1296. }
  1297. for (i = 0; i < subdev_count; i++) {
  1298. vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1299. i2c_adap, subdevdata[i].name,
  1300. &subdevdata[i].board_info,
  1301. NULL);
  1302. if (!vpif_obj.sd[i]) {
  1303. vpif_err("Error registering v4l2 subdevice\n");
  1304. goto probe_subdev_out;
  1305. }
  1306. if (vpif_obj.sd[i])
  1307. vpif_obj.sd[i]->grp_id = 1 << i;
  1308. }
  1309. return 0;
  1310. probe_subdev_out:
  1311. kfree(vpif_obj.sd);
  1312. probe_out:
  1313. for (k = 0; k < j; k++) {
  1314. ch = vpif_obj.dev[k];
  1315. video_unregister_device(ch->video_dev);
  1316. video_device_release(ch->video_dev);
  1317. ch->video_dev = NULL;
  1318. }
  1319. vpif_int_err:
  1320. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1321. vpif_err("VPIF IRQ request failed\n");
  1322. for (q = k; k >= 0; k--) {
  1323. for (m = i; m >= res->start; m--)
  1324. free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
  1325. res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
  1326. m = res->end;
  1327. }
  1328. return err;
  1329. }
  1330. /*
  1331. * vpif_remove: It un-register channels from V4L2 driver
  1332. */
  1333. static int vpif_remove(struct platform_device *device)
  1334. {
  1335. struct channel_obj *ch;
  1336. int i;
  1337. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1338. /* un-register device */
  1339. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1340. /* Get the pointer to the channel object */
  1341. ch = vpif_obj.dev[i];
  1342. /* Unregister video device */
  1343. video_unregister_device(ch->video_dev);
  1344. ch->video_dev = NULL;
  1345. }
  1346. return 0;
  1347. }
  1348. static struct platform_driver vpif_driver = {
  1349. .driver = {
  1350. .name = "vpif_display",
  1351. .owner = THIS_MODULE,
  1352. },
  1353. .probe = vpif_probe,
  1354. .remove = vpif_remove,
  1355. };
  1356. static __init int vpif_init(void)
  1357. {
  1358. return platform_driver_register(&vpif_driver);
  1359. }
  1360. /*
  1361. * vpif_cleanup: This function un-registers device and driver to the kernel,
  1362. * frees requested irq handler and de-allocates memory allocated for channel
  1363. * objects.
  1364. */
  1365. static void vpif_cleanup(void)
  1366. {
  1367. struct platform_device *pdev;
  1368. struct resource *res;
  1369. int irq_num;
  1370. int i = 0;
  1371. pdev = container_of(vpif_dev, struct platform_device, dev);
  1372. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
  1373. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1374. free_irq(irq_num,
  1375. (void *)(&vpif_obj.dev[i]->channel_id));
  1376. i++;
  1377. }
  1378. platform_driver_unregister(&vpif_driver);
  1379. kfree(vpif_obj.sd);
  1380. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
  1381. kfree(vpif_obj.dev[i]);
  1382. }
  1383. module_init(vpif_init);
  1384. module_exit(vpif_cleanup);