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