vpif_display.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  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/slab.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 <media/v4l2-chip-ident.h>
  38. #include "vpif_display.h"
  39. #include "vpif.h"
  40. MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
  41. MODULE_LICENSE("GPL");
  42. MODULE_VERSION(VPIF_DISPLAY_VERSION);
  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. /*
  74. * vpif_uservirt_to_phys: This function is used to convert user
  75. * space virtual address to physical address.
  76. */
  77. static u32 vpif_uservirt_to_phys(u32 virtp)
  78. {
  79. struct mm_struct *mm = current->mm;
  80. unsigned long physp = 0;
  81. struct vm_area_struct *vma;
  82. vma = find_vma(mm, virtp);
  83. /* For kernel direct-mapped memory, take the easy way */
  84. if (virtp >= PAGE_OFFSET) {
  85. physp = virt_to_phys((void *)virtp);
  86. } else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
  87. /* this will catch, kernel-allocated, mmaped-to-usermode addr */
  88. physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
  89. } else {
  90. /* otherwise, use get_user_pages() for general userland pages */
  91. int res, nr_pages = 1;
  92. struct page *pages;
  93. down_read(&current->mm->mmap_sem);
  94. res = get_user_pages(current, current->mm,
  95. virtp, nr_pages, 1, 0, &pages, NULL);
  96. up_read(&current->mm->mmap_sem);
  97. if (res == nr_pages) {
  98. physp = __pa(page_address(&pages[0]) +
  99. (virtp & ~PAGE_MASK));
  100. } else {
  101. vpif_err("get_user_pages failed\n");
  102. return 0;
  103. }
  104. }
  105. return physp;
  106. }
  107. /*
  108. * buffer_prepare: This is the callback function called from videobuf_qbuf()
  109. * function the buffer is prepared and user space virtual address is converted
  110. * into physical address
  111. */
  112. static int vpif_buffer_prepare(struct videobuf_queue *q,
  113. struct videobuf_buffer *vb,
  114. enum v4l2_field field)
  115. {
  116. struct vpif_fh *fh = q->priv_data;
  117. struct common_obj *common;
  118. unsigned long addr;
  119. common = &fh->channel->common[VPIF_VIDEO_INDEX];
  120. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  121. vb->width = common->width;
  122. vb->height = common->height;
  123. vb->size = vb->width * vb->height;
  124. vb->field = field;
  125. }
  126. vb->state = VIDEOBUF_PREPARED;
  127. /* if user pointer memory mechanism is used, get the physical
  128. * address of the buffer */
  129. if (V4L2_MEMORY_USERPTR == common->memory) {
  130. if (!vb->baddr) {
  131. vpif_err("buffer_address is 0\n");
  132. return -EINVAL;
  133. }
  134. vb->boff = vpif_uservirt_to_phys(vb->baddr);
  135. if (!ISALIGNED(vb->boff))
  136. goto buf_align_exit;
  137. }
  138. addr = vb->boff;
  139. if (q->streaming && (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
  140. if (!ISALIGNED(addr + common->ytop_off) ||
  141. !ISALIGNED(addr + common->ybtm_off) ||
  142. !ISALIGNED(addr + common->ctop_off) ||
  143. !ISALIGNED(addr + common->cbtm_off))
  144. goto buf_align_exit;
  145. }
  146. return 0;
  147. buf_align_exit:
  148. vpif_err("buffer offset not aligned to 8 bytes\n");
  149. return -EINVAL;
  150. }
  151. /*
  152. * vpif_buffer_setup: This function allocates memory for the buffers
  153. */
  154. static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  155. unsigned int *size)
  156. {
  157. struct vpif_fh *fh = q->priv_data;
  158. struct channel_obj *ch = fh->channel;
  159. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  160. if (V4L2_MEMORY_MMAP != common->memory)
  161. return 0;
  162. *size = config_params.channel_bufsize[ch->channel_id];
  163. if (*count < config_params.min_numbuffers)
  164. *count = config_params.min_numbuffers;
  165. return 0;
  166. }
  167. /*
  168. * vpif_buffer_queue: This function adds the buffer to DMA queue
  169. */
  170. static void vpif_buffer_queue(struct videobuf_queue *q,
  171. struct videobuf_buffer *vb)
  172. {
  173. struct vpif_fh *fh = q->priv_data;
  174. struct common_obj *common;
  175. common = &fh->channel->common[VPIF_VIDEO_INDEX];
  176. /* add the buffer to the DMA queue */
  177. list_add_tail(&vb->queue, &common->dma_queue);
  178. vb->state = VIDEOBUF_QUEUED;
  179. }
  180. /*
  181. * vpif_buffer_release: This function is called from the videobuf layer to
  182. * free memory allocated to the buffers
  183. */
  184. static void vpif_buffer_release(struct videobuf_queue *q,
  185. struct videobuf_buffer *vb)
  186. {
  187. struct vpif_fh *fh = q->priv_data;
  188. struct channel_obj *ch = fh->channel;
  189. struct common_obj *common;
  190. unsigned int buf_size = 0;
  191. common = &ch->common[VPIF_VIDEO_INDEX];
  192. videobuf_dma_contig_free(q, vb);
  193. vb->state = VIDEOBUF_NEEDS_INIT;
  194. if (V4L2_MEMORY_MMAP != common->memory)
  195. return;
  196. buf_size = config_params.channel_bufsize[ch->channel_id];
  197. }
  198. static struct videobuf_queue_ops video_qops = {
  199. .buf_setup = vpif_buffer_setup,
  200. .buf_prepare = vpif_buffer_prepare,
  201. .buf_queue = vpif_buffer_queue,
  202. .buf_release = vpif_buffer_release,
  203. };
  204. static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
  205. static void process_progressive_mode(struct common_obj *common)
  206. {
  207. unsigned long addr = 0;
  208. /* Get the next buffer from buffer queue */
  209. common->next_frm = list_entry(common->dma_queue.next,
  210. struct videobuf_buffer, queue);
  211. /* Remove that buffer from the buffer queue */
  212. list_del(&common->next_frm->queue);
  213. /* Mark status of the buffer as active */
  214. common->next_frm->state = VIDEOBUF_ACTIVE;
  215. /* Set top and bottom field addrs in VPIF registers */
  216. addr = videobuf_to_dma_contig(common->next_frm);
  217. common->set_addr(addr + common->ytop_off,
  218. addr + common->ybtm_off,
  219. addr + common->ctop_off,
  220. addr + common->cbtm_off);
  221. }
  222. static void process_interlaced_mode(int fid, struct common_obj *common)
  223. {
  224. /* device field id and local field id are in sync */
  225. /* If this is even field */
  226. if (0 == fid) {
  227. if (common->cur_frm == common->next_frm)
  228. return;
  229. /* one frame is displayed If next frame is
  230. * available, release cur_frm and move on */
  231. /* Copy frame display time */
  232. do_gettimeofday(&common->cur_frm->ts);
  233. /* Change status of the cur_frm */
  234. common->cur_frm->state = VIDEOBUF_DONE;
  235. /* unlock semaphore on cur_frm */
  236. wake_up_interruptible(&common->cur_frm->done);
  237. /* Make cur_frm pointing to next_frm */
  238. common->cur_frm = common->next_frm;
  239. } else if (1 == fid) { /* odd field */
  240. if (list_empty(&common->dma_queue)
  241. || (common->cur_frm != common->next_frm)) {
  242. return;
  243. }
  244. /* one field is displayed configure the next
  245. * frame if it is available else hold on current
  246. * frame */
  247. /* Get next from the buffer queue */
  248. process_progressive_mode(common);
  249. }
  250. }
  251. /*
  252. * vpif_channel_isr: It changes status of the displayed buffer, takes next
  253. * buffer from the queue and sets its address in VPIF registers
  254. */
  255. static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
  256. {
  257. struct vpif_device *dev = &vpif_obj;
  258. struct channel_obj *ch;
  259. struct common_obj *common;
  260. enum v4l2_field field;
  261. int fid = -1, i;
  262. int channel_id = 0;
  263. channel_id = *(int *)(dev_id);
  264. ch = dev->dev[channel_id];
  265. field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
  266. for (i = 0; i < VPIF_NUMOBJECTS; i++) {
  267. common = &ch->common[i];
  268. /* If streaming is started in this channel */
  269. if (0 == common->started)
  270. continue;
  271. if (1 == ch->vpifparams.std_info.frm_fmt) {
  272. if (list_empty(&common->dma_queue))
  273. continue;
  274. /* Progressive mode */
  275. if (!channel_first_int[i][channel_id]) {
  276. /* Mark status of the cur_frm to
  277. * done and unlock semaphore on it */
  278. do_gettimeofday(&common->cur_frm->ts);
  279. common->cur_frm->state = VIDEOBUF_DONE;
  280. wake_up_interruptible(&common->cur_frm->done);
  281. /* Make cur_frm pointing to next_frm */
  282. common->cur_frm = common->next_frm;
  283. }
  284. channel_first_int[i][channel_id] = 0;
  285. process_progressive_mode(common);
  286. } else {
  287. /* Interlaced mode */
  288. /* If it is first interrupt, ignore it */
  289. if (channel_first_int[i][channel_id]) {
  290. channel_first_int[i][channel_id] = 0;
  291. continue;
  292. }
  293. if (0 == i) {
  294. ch->field_id ^= 1;
  295. /* Get field id from VPIF registers */
  296. fid = vpif_channel_getfid(ch->channel_id + 2);
  297. /* If fid does not match with stored field id */
  298. if (fid != ch->field_id) {
  299. /* Make them in sync */
  300. if (0 == fid)
  301. ch->field_id = fid;
  302. return IRQ_HANDLED;
  303. }
  304. }
  305. process_interlaced_mode(fid, common);
  306. }
  307. }
  308. return IRQ_HANDLED;
  309. }
  310. static int vpif_update_std_info(struct channel_obj *ch)
  311. {
  312. struct video_obj *vid_ch = &ch->video;
  313. struct vpif_params *vpifparams = &ch->vpifparams;
  314. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  315. const struct vpif_channel_config_params *config;
  316. int i;
  317. for (i = 0; i < vpif_ch_params_count; i++) {
  318. config = &ch_params[i];
  319. if (config->hd_sd == 0) {
  320. vpif_dbg(2, debug, "SD format\n");
  321. if (config->stdid & vid_ch->stdid) {
  322. memcpy(std_info, config, sizeof(*config));
  323. break;
  324. }
  325. } else {
  326. vpif_dbg(2, debug, "HD format\n");
  327. if (config->dv_preset == vid_ch->dv_preset) {
  328. memcpy(std_info, config, sizeof(*config));
  329. break;
  330. }
  331. }
  332. }
  333. if (i == vpif_ch_params_count) {
  334. vpif_dbg(1, debug, "Format not found\n");
  335. return -EINVAL;
  336. }
  337. return 0;
  338. }
  339. static int vpif_update_resolution(struct channel_obj *ch)
  340. {
  341. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  342. struct video_obj *vid_ch = &ch->video;
  343. struct vpif_params *vpifparams = &ch->vpifparams;
  344. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  345. if (!vid_ch->stdid && !vid_ch->dv_preset && !vid_ch->bt_timings.height)
  346. return -EINVAL;
  347. if (vid_ch->stdid || vid_ch->dv_preset) {
  348. if (vpif_update_std_info(ch))
  349. return -EINVAL;
  350. }
  351. common->fmt.fmt.pix.width = std_info->width;
  352. common->fmt.fmt.pix.height = std_info->height;
  353. vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
  354. common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
  355. /* Set height and width paramateres */
  356. common->height = std_info->height;
  357. common->width = std_info->width;
  358. return 0;
  359. }
  360. /*
  361. * vpif_calculate_offsets: This function calculates buffers offset for Y and C
  362. * in the top and bottom field
  363. */
  364. static void vpif_calculate_offsets(struct channel_obj *ch)
  365. {
  366. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  367. struct vpif_params *vpifparams = &ch->vpifparams;
  368. enum v4l2_field field = common->fmt.fmt.pix.field;
  369. struct video_obj *vid_ch = &ch->video;
  370. unsigned int hpitch, vpitch, sizeimage;
  371. if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
  372. if (ch->vpifparams.std_info.frm_fmt)
  373. vid_ch->buf_field = V4L2_FIELD_NONE;
  374. else
  375. vid_ch->buf_field = V4L2_FIELD_INTERLACED;
  376. } else {
  377. vid_ch->buf_field = common->fmt.fmt.pix.field;
  378. }
  379. if (V4L2_MEMORY_USERPTR == common->memory)
  380. sizeimage = common->fmt.fmt.pix.sizeimage;
  381. else
  382. sizeimage = config_params.channel_bufsize[ch->channel_id];
  383. hpitch = common->fmt.fmt.pix.bytesperline;
  384. vpitch = sizeimage / (hpitch * 2);
  385. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  386. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  387. common->ytop_off = 0;
  388. common->ybtm_off = hpitch;
  389. common->ctop_off = sizeimage / 2;
  390. common->cbtm_off = sizeimage / 2 + hpitch;
  391. } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
  392. common->ytop_off = 0;
  393. common->ybtm_off = sizeimage / 4;
  394. common->ctop_off = sizeimage / 2;
  395. common->cbtm_off = common->ctop_off + sizeimage / 4;
  396. } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
  397. common->ybtm_off = 0;
  398. common->ytop_off = sizeimage / 4;
  399. common->cbtm_off = sizeimage / 2;
  400. common->ctop_off = common->cbtm_off + sizeimage / 4;
  401. }
  402. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  403. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  404. vpifparams->video_params.storage_mode = 1;
  405. } else {
  406. vpifparams->video_params.storage_mode = 0;
  407. }
  408. if (ch->vpifparams.std_info.frm_fmt == 1) {
  409. vpifparams->video_params.hpitch =
  410. common->fmt.fmt.pix.bytesperline;
  411. } else {
  412. if ((field == V4L2_FIELD_ANY) ||
  413. (field == V4L2_FIELD_INTERLACED))
  414. vpifparams->video_params.hpitch =
  415. common->fmt.fmt.pix.bytesperline * 2;
  416. else
  417. vpifparams->video_params.hpitch =
  418. common->fmt.fmt.pix.bytesperline;
  419. }
  420. ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
  421. }
  422. static void vpif_config_format(struct channel_obj *ch)
  423. {
  424. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  425. common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
  426. if (config_params.numbuffers[ch->channel_id] == 0)
  427. common->memory = V4L2_MEMORY_USERPTR;
  428. else
  429. common->memory = V4L2_MEMORY_MMAP;
  430. common->fmt.fmt.pix.sizeimage =
  431. config_params.channel_bufsize[ch->channel_id];
  432. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  433. common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  434. }
  435. static int vpif_check_format(struct channel_obj *ch,
  436. struct v4l2_pix_format *pixfmt)
  437. {
  438. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  439. enum v4l2_field field = pixfmt->field;
  440. u32 sizeimage, hpitch, vpitch;
  441. if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
  442. goto invalid_fmt_exit;
  443. if (!(VPIF_VALID_FIELD(field)))
  444. goto invalid_fmt_exit;
  445. if (pixfmt->bytesperline <= 0)
  446. goto invalid_pitch_exit;
  447. if (V4L2_MEMORY_USERPTR == common->memory)
  448. sizeimage = pixfmt->sizeimage;
  449. else
  450. sizeimage = config_params.channel_bufsize[ch->channel_id];
  451. if (vpif_update_resolution(ch))
  452. return -EINVAL;
  453. hpitch = pixfmt->bytesperline;
  454. vpitch = sizeimage / (hpitch * 2);
  455. /* Check for valid value of pitch */
  456. if ((hpitch < ch->vpifparams.std_info.width) ||
  457. (vpitch < ch->vpifparams.std_info.height))
  458. goto invalid_pitch_exit;
  459. /* Check for 8 byte alignment */
  460. if (!ISALIGNED(hpitch)) {
  461. vpif_err("invalid pitch alignment\n");
  462. return -EINVAL;
  463. }
  464. pixfmt->width = common->fmt.fmt.pix.width;
  465. pixfmt->height = common->fmt.fmt.pix.height;
  466. return 0;
  467. invalid_fmt_exit:
  468. vpif_err("invalid field format\n");
  469. return -EINVAL;
  470. invalid_pitch_exit:
  471. vpif_err("invalid pitch\n");
  472. return -EINVAL;
  473. }
  474. static void vpif_config_addr(struct channel_obj *ch, int muxmode)
  475. {
  476. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  477. if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
  478. common->set_addr = ch3_set_videobuf_addr;
  479. } else {
  480. if (2 == muxmode)
  481. common->set_addr = ch2_set_videobuf_addr_yc_nmux;
  482. else
  483. common->set_addr = ch2_set_videobuf_addr;
  484. }
  485. }
  486. /*
  487. * vpif_mmap: It is used to map kernel space buffers into user spaces
  488. */
  489. static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
  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. vpif_dbg(2, debug, "vpif_mmap\n");
  495. return videobuf_mmap_mapper(&common->buffer_queue, vma);
  496. }
  497. /*
  498. * vpif_poll: It is used for select/poll system call
  499. */
  500. static unsigned int vpif_poll(struct file *filep, poll_table *wait)
  501. {
  502. struct vpif_fh *fh = filep->private_data;
  503. struct channel_obj *ch = fh->channel;
  504. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  505. if (common->started)
  506. return videobuf_poll_stream(filep, &common->buffer_queue, wait);
  507. return 0;
  508. }
  509. /*
  510. * vpif_open: It creates object of file handle structure and stores it in
  511. * private_data member of filepointer
  512. */
  513. static int vpif_open(struct file *filep)
  514. {
  515. struct video_device *vdev = video_devdata(filep);
  516. struct channel_obj *ch = NULL;
  517. struct vpif_fh *fh = NULL;
  518. ch = video_get_drvdata(vdev);
  519. /* Allocate memory for the file handle object */
  520. fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
  521. if (fh == NULL) {
  522. vpif_err("unable to allocate memory for file handle object\n");
  523. return -ENOMEM;
  524. }
  525. /* store pointer to fh in private_data member of filep */
  526. filep->private_data = fh;
  527. fh->channel = ch;
  528. fh->initialized = 0;
  529. if (!ch->initialized) {
  530. fh->initialized = 1;
  531. ch->initialized = 1;
  532. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  533. }
  534. /* Increment channel usrs counter */
  535. atomic_inc(&ch->usrs);
  536. /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
  537. fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
  538. /* Initialize priority of this instance to default priority */
  539. fh->prio = V4L2_PRIORITY_UNSET;
  540. v4l2_prio_open(&ch->prio, &fh->prio);
  541. return 0;
  542. }
  543. /*
  544. * vpif_release: This function deletes buffer queue, frees the buffers and
  545. * the vpif file handle
  546. */
  547. static int vpif_release(struct file *filep)
  548. {
  549. struct vpif_fh *fh = filep->private_data;
  550. struct channel_obj *ch = fh->channel;
  551. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  552. /* if this instance is doing IO */
  553. if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
  554. /* Reset io_usrs member of channel object */
  555. common->io_usrs = 0;
  556. /* Disable channel */
  557. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  558. enable_channel2(0);
  559. channel2_intr_enable(0);
  560. }
  561. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  562. (2 == common->started)) {
  563. enable_channel3(0);
  564. channel3_intr_enable(0);
  565. }
  566. common->started = 0;
  567. /* Free buffers allocated */
  568. videobuf_queue_cancel(&common->buffer_queue);
  569. videobuf_mmap_free(&common->buffer_queue);
  570. common->numbuffers =
  571. config_params.numbuffers[ch->channel_id];
  572. }
  573. /* Decrement channel usrs counter */
  574. atomic_dec(&ch->usrs);
  575. /* If this file handle has initialize encoder device, reset it */
  576. if (fh->initialized)
  577. ch->initialized = 0;
  578. /* Close the priority */
  579. v4l2_prio_close(&ch->prio, fh->prio);
  580. filep->private_data = NULL;
  581. fh->initialized = 0;
  582. kfree(fh);
  583. return 0;
  584. }
  585. /* functions implementing ioctls */
  586. /**
  587. * vpif_querycap() - QUERYCAP handler
  588. * @file: file ptr
  589. * @priv: file handle
  590. * @cap: ptr to v4l2_capability structure
  591. */
  592. static int vpif_querycap(struct file *file, void *priv,
  593. struct v4l2_capability *cap)
  594. {
  595. struct vpif_display_config *config = vpif_dev->platform_data;
  596. cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  597. strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
  598. strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
  599. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  600. return 0;
  601. }
  602. static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
  603. struct v4l2_fmtdesc *fmt)
  604. {
  605. if (fmt->index != 0) {
  606. vpif_err("Invalid format index\n");
  607. return -EINVAL;
  608. }
  609. /* Fill in the information about format */
  610. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  611. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  612. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  613. return 0;
  614. }
  615. static int vpif_g_fmt_vid_out(struct file *file, void *priv,
  616. struct v4l2_format *fmt)
  617. {
  618. struct vpif_fh *fh = priv;
  619. struct channel_obj *ch = fh->channel;
  620. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  621. /* Check the validity of the buffer type */
  622. if (common->fmt.type != fmt->type)
  623. return -EINVAL;
  624. if (vpif_update_resolution(ch))
  625. return -EINVAL;
  626. *fmt = common->fmt;
  627. return 0;
  628. }
  629. static int vpif_s_fmt_vid_out(struct file *file, void *priv,
  630. struct v4l2_format *fmt)
  631. {
  632. struct vpif_fh *fh = priv;
  633. struct v4l2_pix_format *pixfmt;
  634. struct channel_obj *ch = fh->channel;
  635. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  636. int ret = 0;
  637. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  638. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  639. if (!fh->initialized) {
  640. vpif_dbg(1, debug, "Channel Busy\n");
  641. return -EBUSY;
  642. }
  643. /* Check for the priority */
  644. ret = v4l2_prio_check(&ch->prio, fh->prio);
  645. if (0 != ret)
  646. return ret;
  647. fh->initialized = 1;
  648. }
  649. if (common->started) {
  650. vpif_dbg(1, debug, "Streaming in progress\n");
  651. return -EBUSY;
  652. }
  653. pixfmt = &fmt->fmt.pix;
  654. /* Check for valid field format */
  655. ret = vpif_check_format(ch, pixfmt);
  656. if (ret)
  657. return ret;
  658. /* store the pix format in the channel object */
  659. common->fmt.fmt.pix = *pixfmt;
  660. /* store the format in the channel object */
  661. common->fmt = *fmt;
  662. return 0;
  663. }
  664. static int vpif_try_fmt_vid_out(struct file *file, void *priv,
  665. struct v4l2_format *fmt)
  666. {
  667. struct vpif_fh *fh = priv;
  668. struct channel_obj *ch = fh->channel;
  669. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  670. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  671. int ret = 0;
  672. ret = vpif_check_format(ch, pixfmt);
  673. if (ret) {
  674. *pixfmt = common->fmt.fmt.pix;
  675. pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
  676. }
  677. return ret;
  678. }
  679. static int vpif_reqbufs(struct file *file, void *priv,
  680. struct v4l2_requestbuffers *reqbuf)
  681. {
  682. struct vpif_fh *fh = priv;
  683. struct channel_obj *ch = fh->channel;
  684. struct common_obj *common;
  685. enum v4l2_field field;
  686. u8 index = 0;
  687. /* This file handle has not initialized the channel,
  688. It is not allowed to do settings */
  689. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  690. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  691. if (!fh->initialized) {
  692. vpif_err("Channel Busy\n");
  693. return -EBUSY;
  694. }
  695. }
  696. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
  697. return -EINVAL;
  698. index = VPIF_VIDEO_INDEX;
  699. common = &ch->common[index];
  700. if (common->fmt.type != reqbuf->type)
  701. return -EINVAL;
  702. if (0 != common->io_usrs)
  703. return -EBUSY;
  704. if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  705. if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
  706. field = V4L2_FIELD_INTERLACED;
  707. else
  708. field = common->fmt.fmt.pix.field;
  709. } else {
  710. field = V4L2_VBI_INTERLACED;
  711. }
  712. /* Initialize videobuf queue as per the buffer type */
  713. videobuf_queue_dma_contig_init(&common->buffer_queue,
  714. &video_qops, NULL,
  715. &common->irqlock,
  716. reqbuf->type, field,
  717. sizeof(struct videobuf_buffer), fh,
  718. &common->lock);
  719. /* Set io allowed member of file handle to TRUE */
  720. fh->io_allowed[index] = 1;
  721. /* Increment io usrs member of channel object to 1 */
  722. common->io_usrs = 1;
  723. /* Store type of memory requested in channel object */
  724. common->memory = reqbuf->memory;
  725. INIT_LIST_HEAD(&common->dma_queue);
  726. /* Allocate buffers */
  727. return videobuf_reqbufs(&common->buffer_queue, reqbuf);
  728. }
  729. static int vpif_querybuf(struct file *file, void *priv,
  730. struct v4l2_buffer *tbuf)
  731. {
  732. struct vpif_fh *fh = priv;
  733. struct channel_obj *ch = fh->channel;
  734. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  735. if (common->fmt.type != tbuf->type)
  736. return -EINVAL;
  737. return videobuf_querybuf(&common->buffer_queue, tbuf);
  738. }
  739. static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  740. {
  741. struct vpif_fh *fh = priv;
  742. struct channel_obj *ch = fh->channel;
  743. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  744. struct v4l2_buffer tbuf = *buf;
  745. struct videobuf_buffer *buf1;
  746. unsigned long addr = 0;
  747. unsigned long flags;
  748. int ret = 0;
  749. if (common->fmt.type != tbuf.type)
  750. return -EINVAL;
  751. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  752. vpif_err("fh->io_allowed\n");
  753. return -EACCES;
  754. }
  755. if (!(list_empty(&common->dma_queue)) ||
  756. (common->cur_frm != common->next_frm) ||
  757. !(common->started) ||
  758. (common->started && (0 == ch->field_id)))
  759. return videobuf_qbuf(&common->buffer_queue, buf);
  760. /* bufferqueue is empty store buffer address in VPIF registers */
  761. mutex_lock(&common->buffer_queue.vb_lock);
  762. buf1 = common->buffer_queue.bufs[tbuf.index];
  763. if (buf1->memory != tbuf.memory) {
  764. vpif_err("invalid buffer type\n");
  765. goto qbuf_exit;
  766. }
  767. if ((buf1->state == VIDEOBUF_QUEUED) ||
  768. (buf1->state == VIDEOBUF_ACTIVE)) {
  769. vpif_err("invalid state\n");
  770. goto qbuf_exit;
  771. }
  772. switch (buf1->memory) {
  773. case V4L2_MEMORY_MMAP:
  774. if (buf1->baddr == 0)
  775. goto qbuf_exit;
  776. break;
  777. case V4L2_MEMORY_USERPTR:
  778. if (tbuf.length < buf1->bsize)
  779. goto qbuf_exit;
  780. if ((VIDEOBUF_NEEDS_INIT != buf1->state)
  781. && (buf1->baddr != tbuf.m.userptr)) {
  782. vpif_buffer_release(&common->buffer_queue, buf1);
  783. buf1->baddr = tbuf.m.userptr;
  784. }
  785. break;
  786. default:
  787. goto qbuf_exit;
  788. }
  789. local_irq_save(flags);
  790. ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
  791. common->buffer_queue.field);
  792. if (ret < 0) {
  793. local_irq_restore(flags);
  794. goto qbuf_exit;
  795. }
  796. buf1->state = VIDEOBUF_ACTIVE;
  797. addr = buf1->boff;
  798. common->next_frm = buf1;
  799. if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
  800. common->set_addr((addr + common->ytop_off),
  801. (addr + common->ybtm_off),
  802. (addr + common->ctop_off),
  803. (addr + common->cbtm_off));
  804. }
  805. local_irq_restore(flags);
  806. list_add_tail(&buf1->stream, &common->buffer_queue.stream);
  807. mutex_unlock(&common->buffer_queue.vb_lock);
  808. return 0;
  809. qbuf_exit:
  810. mutex_unlock(&common->buffer_queue.vb_lock);
  811. return -EINVAL;
  812. }
  813. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  814. {
  815. struct vpif_fh *fh = priv;
  816. struct channel_obj *ch = fh->channel;
  817. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  818. int ret = 0;
  819. if (!(*std_id & DM646X_V4L2_STD))
  820. return -EINVAL;
  821. if (common->started) {
  822. vpif_err("streaming in progress\n");
  823. return -EBUSY;
  824. }
  825. /* Call encoder subdevice function to set the standard */
  826. ch->video.stdid = *std_id;
  827. ch->video.dv_preset = V4L2_DV_INVALID;
  828. memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
  829. /* Get the information about the standard */
  830. if (vpif_update_resolution(ch))
  831. return -EINVAL;
  832. if ((ch->vpifparams.std_info.width *
  833. ch->vpifparams.std_info.height * 2) >
  834. config_params.channel_bufsize[ch->channel_id]) {
  835. vpif_err("invalid std for this size\n");
  836. return -EINVAL;
  837. }
  838. common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
  839. /* Configure the default format information */
  840. vpif_config_format(ch);
  841. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  842. s_std_output, *std_id);
  843. if (ret < 0) {
  844. vpif_err("Failed to set output standard\n");
  845. return ret;
  846. }
  847. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
  848. s_std, *std_id);
  849. if (ret < 0)
  850. vpif_err("Failed to set standard for sub devices\n");
  851. return ret;
  852. }
  853. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  854. {
  855. struct vpif_fh *fh = priv;
  856. struct channel_obj *ch = fh->channel;
  857. *std = ch->video.stdid;
  858. return 0;
  859. }
  860. static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  861. {
  862. struct vpif_fh *fh = priv;
  863. struct channel_obj *ch = fh->channel;
  864. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  865. return videobuf_dqbuf(&common->buffer_queue, p,
  866. (file->f_flags & O_NONBLOCK));
  867. }
  868. static int vpif_streamon(struct file *file, void *priv,
  869. enum v4l2_buf_type buftype)
  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. struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
  875. struct vpif_params *vpif = &ch->vpifparams;
  876. struct vpif_display_config *vpif_config_data =
  877. vpif_dev->platform_data;
  878. unsigned long addr = 0;
  879. int ret = 0;
  880. if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  881. vpif_err("buffer type not supported\n");
  882. return -EINVAL;
  883. }
  884. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  885. vpif_err("fh->io_allowed\n");
  886. return -EACCES;
  887. }
  888. /* If Streaming is already started, return error */
  889. if (common->started) {
  890. vpif_err("channel->started\n");
  891. return -EBUSY;
  892. }
  893. if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
  894. && oth_ch->common[VPIF_VIDEO_INDEX].started &&
  895. ch->vpifparams.std_info.ycmux_mode == 0)
  896. || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
  897. && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
  898. vpif_err("other channel is using\n");
  899. return -EBUSY;
  900. }
  901. ret = vpif_check_format(ch, &common->fmt.fmt.pix);
  902. if (ret < 0)
  903. return ret;
  904. /* Call videobuf_streamon to start streaming in videobuf */
  905. ret = videobuf_streamon(&common->buffer_queue);
  906. if (ret < 0) {
  907. vpif_err("videobuf_streamon\n");
  908. return ret;
  909. }
  910. /* If buffer queue is empty, return error */
  911. if (list_empty(&common->dma_queue)) {
  912. vpif_err("buffer queue is empty\n");
  913. return -EIO;
  914. }
  915. /* Get the next frame from the buffer queue */
  916. common->next_frm = common->cur_frm =
  917. list_entry(common->dma_queue.next,
  918. struct videobuf_buffer, queue);
  919. list_del(&common->cur_frm->queue);
  920. /* Mark state of the current frame to active */
  921. common->cur_frm->state = VIDEOBUF_ACTIVE;
  922. /* Initialize field_id and started member */
  923. ch->field_id = 0;
  924. common->started = 1;
  925. if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  926. addr = common->cur_frm->boff;
  927. /* Calculate the offset for Y and C data in the buffer */
  928. vpif_calculate_offsets(ch);
  929. if ((ch->vpifparams.std_info.frm_fmt &&
  930. ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
  931. && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
  932. || (!ch->vpifparams.std_info.frm_fmt
  933. && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
  934. vpif_err("conflict in field format and std format\n");
  935. return -EINVAL;
  936. }
  937. /* clock settings */
  938. ret =
  939. vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
  940. ch->vpifparams.std_info.hd_sd);
  941. if (ret < 0) {
  942. vpif_err("can't set clock\n");
  943. return ret;
  944. }
  945. /* set the parameters and addresses */
  946. ret = vpif_set_video_params(vpif, ch->channel_id + 2);
  947. if (ret < 0)
  948. return ret;
  949. common->started = ret;
  950. vpif_config_addr(ch, ret);
  951. common->set_addr((addr + common->ytop_off),
  952. (addr + common->ybtm_off),
  953. (addr + common->ctop_off),
  954. (addr + common->cbtm_off));
  955. /* Set interrupt for both the fields in VPIF
  956. Register enable channel in VPIF register */
  957. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  958. channel2_intr_assert();
  959. channel2_intr_enable(1);
  960. enable_channel2(1);
  961. }
  962. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
  963. || (common->started == 2)) {
  964. channel3_intr_assert();
  965. channel3_intr_enable(1);
  966. enable_channel3(1);
  967. }
  968. channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
  969. }
  970. return ret;
  971. }
  972. static int vpif_streamoff(struct file *file, void *priv,
  973. enum v4l2_buf_type buftype)
  974. {
  975. struct vpif_fh *fh = priv;
  976. struct channel_obj *ch = fh->channel;
  977. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  978. if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  979. vpif_err("buffer type not supported\n");
  980. return -EINVAL;
  981. }
  982. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  983. vpif_err("fh->io_allowed\n");
  984. return -EACCES;
  985. }
  986. if (!common->started) {
  987. vpif_err("channel->started\n");
  988. return -EINVAL;
  989. }
  990. if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  991. /* disable channel */
  992. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  993. enable_channel2(0);
  994. channel2_intr_enable(0);
  995. }
  996. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  997. (2 == common->started)) {
  998. enable_channel3(0);
  999. channel3_intr_enable(0);
  1000. }
  1001. }
  1002. common->started = 0;
  1003. return videobuf_streamoff(&common->buffer_queue);
  1004. }
  1005. static int vpif_cropcap(struct file *file, void *priv,
  1006. struct v4l2_cropcap *crop)
  1007. {
  1008. struct vpif_fh *fh = priv;
  1009. struct channel_obj *ch = fh->channel;
  1010. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1011. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
  1012. return -EINVAL;
  1013. crop->bounds.left = crop->bounds.top = 0;
  1014. crop->defrect.left = crop->defrect.top = 0;
  1015. crop->defrect.height = crop->bounds.height = common->height;
  1016. crop->defrect.width = crop->bounds.width = common->width;
  1017. return 0;
  1018. }
  1019. static int vpif_enum_output(struct file *file, void *fh,
  1020. struct v4l2_output *output)
  1021. {
  1022. struct vpif_display_config *config = vpif_dev->platform_data;
  1023. if (output->index >= config->output_count) {
  1024. vpif_dbg(1, debug, "Invalid output index\n");
  1025. return -EINVAL;
  1026. }
  1027. strcpy(output->name, config->output[output->index]);
  1028. output->type = V4L2_OUTPUT_TYPE_ANALOG;
  1029. output->std = DM646X_V4L2_STD;
  1030. return 0;
  1031. }
  1032. static int vpif_s_output(struct file *file, void *priv, unsigned int i)
  1033. {
  1034. struct vpif_fh *fh = priv;
  1035. struct channel_obj *ch = fh->channel;
  1036. struct video_obj *vid_ch = &ch->video;
  1037. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1038. int ret = 0;
  1039. if (common->started) {
  1040. vpif_err("Streaming in progress\n");
  1041. return -EBUSY;
  1042. }
  1043. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  1044. s_routing, 0, i, 0);
  1045. if (ret < 0)
  1046. vpif_err("Failed to set output standard\n");
  1047. vid_ch->output_id = i;
  1048. return ret;
  1049. }
  1050. static int vpif_g_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. *i = vid_ch->output_id;
  1056. return 0;
  1057. }
  1058. static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
  1059. {
  1060. struct vpif_fh *fh = priv;
  1061. struct channel_obj *ch = fh->channel;
  1062. *p = v4l2_prio_max(&ch->prio);
  1063. return 0;
  1064. }
  1065. static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
  1066. {
  1067. struct vpif_fh *fh = priv;
  1068. struct channel_obj *ch = fh->channel;
  1069. return v4l2_prio_change(&ch->prio, &fh->prio, p);
  1070. }
  1071. /**
  1072. * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
  1073. * @file: file ptr
  1074. * @priv: file handle
  1075. * @preset: input preset
  1076. */
  1077. static int vpif_enum_dv_presets(struct file *file, void *priv,
  1078. struct v4l2_dv_enum_preset *preset)
  1079. {
  1080. struct vpif_fh *fh = priv;
  1081. struct channel_obj *ch = fh->channel;
  1082. struct video_obj *vid_ch = &ch->video;
  1083. return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
  1084. video, enum_dv_presets, preset);
  1085. }
  1086. /**
  1087. * vpif_s_dv_presets() - S_DV_PRESETS handler
  1088. * @file: file ptr
  1089. * @priv: file handle
  1090. * @preset: input preset
  1091. */
  1092. static int vpif_s_dv_preset(struct file *file, void *priv,
  1093. struct v4l2_dv_preset *preset)
  1094. {
  1095. struct vpif_fh *fh = priv;
  1096. struct channel_obj *ch = fh->channel;
  1097. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1098. struct video_obj *vid_ch = &ch->video;
  1099. int ret = 0;
  1100. if (common->started) {
  1101. vpif_dbg(1, debug, "streaming in progress\n");
  1102. return -EBUSY;
  1103. }
  1104. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1105. if (ret != 0)
  1106. return ret;
  1107. fh->initialized = 1;
  1108. /* Call encoder subdevice function to set the standard */
  1109. if (mutex_lock_interruptible(&common->lock))
  1110. return -ERESTARTSYS;
  1111. ch->video.dv_preset = preset->preset;
  1112. ch->video.stdid = V4L2_STD_UNKNOWN;
  1113. memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
  1114. /* Get the information about the standard */
  1115. if (vpif_update_resolution(ch)) {
  1116. ret = -EINVAL;
  1117. } else {
  1118. /* Configure the default format information */
  1119. vpif_config_format(ch);
  1120. ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
  1121. video, s_dv_preset, preset);
  1122. }
  1123. mutex_unlock(&common->lock);
  1124. return ret;
  1125. }
  1126. /**
  1127. * vpif_g_dv_presets() - G_DV_PRESETS handler
  1128. * @file: file ptr
  1129. * @priv: file handle
  1130. * @preset: input preset
  1131. */
  1132. static int vpif_g_dv_preset(struct file *file, void *priv,
  1133. struct v4l2_dv_preset *preset)
  1134. {
  1135. struct vpif_fh *fh = priv;
  1136. struct channel_obj *ch = fh->channel;
  1137. preset->preset = ch->video.dv_preset;
  1138. return 0;
  1139. }
  1140. /**
  1141. * vpif_s_dv_timings() - S_DV_TIMINGS handler
  1142. * @file: file ptr
  1143. * @priv: file handle
  1144. * @timings: digital video timings
  1145. */
  1146. static int vpif_s_dv_timings(struct file *file, void *priv,
  1147. struct v4l2_dv_timings *timings)
  1148. {
  1149. struct vpif_fh *fh = priv;
  1150. struct channel_obj *ch = fh->channel;
  1151. struct vpif_params *vpifparams = &ch->vpifparams;
  1152. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  1153. struct video_obj *vid_ch = &ch->video;
  1154. struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
  1155. int ret;
  1156. if (timings->type != V4L2_DV_BT_656_1120) {
  1157. vpif_dbg(2, debug, "Timing type not defined\n");
  1158. return -EINVAL;
  1159. }
  1160. /* Configure subdevice timings, if any */
  1161. ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
  1162. video, s_dv_timings, timings);
  1163. if (ret == -ENOIOCTLCMD) {
  1164. vpif_dbg(2, debug, "Custom DV timings not supported by "
  1165. "subdevice\n");
  1166. return -EINVAL;
  1167. }
  1168. if (ret < 0) {
  1169. vpif_dbg(2, debug, "Error setting custom DV timings\n");
  1170. return ret;
  1171. }
  1172. if (!(timings->bt.width && timings->bt.height &&
  1173. (timings->bt.hbackporch ||
  1174. timings->bt.hfrontporch ||
  1175. timings->bt.hsync) &&
  1176. timings->bt.vfrontporch &&
  1177. (timings->bt.vbackporch ||
  1178. timings->bt.vsync))) {
  1179. vpif_dbg(2, debug, "Timings for width, height, "
  1180. "horizontal back porch, horizontal sync, "
  1181. "horizontal front porch, vertical back porch, "
  1182. "vertical sync and vertical back porch "
  1183. "must be defined\n");
  1184. return -EINVAL;
  1185. }
  1186. *bt = timings->bt;
  1187. /* Configure video port timings */
  1188. std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
  1189. bt->hsync - 8;
  1190. std_info->sav2eav = bt->width;
  1191. std_info->l1 = 1;
  1192. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  1193. if (bt->interlaced) {
  1194. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  1195. std_info->vsize = bt->height * 2 +
  1196. bt->vfrontporch + bt->vsync + bt->vbackporch +
  1197. bt->il_vfrontporch + bt->il_vsync +
  1198. bt->il_vbackporch;
  1199. std_info->l5 = std_info->vsize/2 -
  1200. (bt->vfrontporch - 1);
  1201. std_info->l7 = std_info->vsize/2 + 1;
  1202. std_info->l9 = std_info->l7 + bt->il_vsync +
  1203. bt->il_vbackporch + 1;
  1204. std_info->l11 = std_info->vsize -
  1205. (bt->il_vfrontporch - 1);
  1206. } else {
  1207. vpif_dbg(2, debug, "Required timing values for "
  1208. "interlaced BT format missing\n");
  1209. return -EINVAL;
  1210. }
  1211. } else {
  1212. std_info->vsize = bt->height + bt->vfrontporch +
  1213. bt->vsync + bt->vbackporch;
  1214. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  1215. }
  1216. strncpy(std_info->name, "Custom timings BT656/1120",
  1217. VPIF_MAX_NAME);
  1218. std_info->width = bt->width;
  1219. std_info->height = bt->height;
  1220. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  1221. std_info->ycmux_mode = 0;
  1222. std_info->capture_format = 0;
  1223. std_info->vbi_supported = 0;
  1224. std_info->hd_sd = 1;
  1225. std_info->stdid = 0;
  1226. std_info->dv_preset = V4L2_DV_INVALID;
  1227. vid_ch->stdid = 0;
  1228. vid_ch->dv_preset = V4L2_DV_INVALID;
  1229. return 0;
  1230. }
  1231. /**
  1232. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  1233. * @file: file ptr
  1234. * @priv: file handle
  1235. * @timings: digital video timings
  1236. */
  1237. static int vpif_g_dv_timings(struct file *file, void *priv,
  1238. struct v4l2_dv_timings *timings)
  1239. {
  1240. struct vpif_fh *fh = priv;
  1241. struct channel_obj *ch = fh->channel;
  1242. struct video_obj *vid_ch = &ch->video;
  1243. struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
  1244. timings->bt = *bt;
  1245. return 0;
  1246. }
  1247. /*
  1248. * vpif_g_chip_ident() - Identify the chip
  1249. * @file: file ptr
  1250. * @priv: file handle
  1251. * @chip: chip identity
  1252. *
  1253. * Returns zero or -EINVAL if read operations fails.
  1254. */
  1255. static int vpif_g_chip_ident(struct file *file, void *priv,
  1256. struct v4l2_dbg_chip_ident *chip)
  1257. {
  1258. chip->ident = V4L2_IDENT_NONE;
  1259. chip->revision = 0;
  1260. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  1261. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
  1262. vpif_dbg(2, debug, "match_type is invalid.\n");
  1263. return -EINVAL;
  1264. }
  1265. return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
  1266. g_chip_ident, chip);
  1267. }
  1268. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1269. /*
  1270. * vpif_dbg_g_register() - Read register
  1271. * @file: file ptr
  1272. * @priv: file handle
  1273. * @reg: register to be read
  1274. *
  1275. * Debugging only
  1276. * Returns zero or -EINVAL if read operations fails.
  1277. */
  1278. static int vpif_dbg_g_register(struct file *file, void *priv,
  1279. struct v4l2_dbg_register *reg){
  1280. struct vpif_fh *fh = priv;
  1281. struct channel_obj *ch = fh->channel;
  1282. struct video_obj *vid_ch = &ch->video;
  1283. return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
  1284. g_register, reg);
  1285. }
  1286. /*
  1287. * vpif_dbg_s_register() - Write to register
  1288. * @file: file ptr
  1289. * @priv: file handle
  1290. * @reg: register to be modified
  1291. *
  1292. * Debugging only
  1293. * Returns zero or -EINVAL if write operations fails.
  1294. */
  1295. static int vpif_dbg_s_register(struct file *file, void *priv,
  1296. struct v4l2_dbg_register *reg){
  1297. struct vpif_fh *fh = priv;
  1298. struct channel_obj *ch = fh->channel;
  1299. struct video_obj *vid_ch = &ch->video;
  1300. return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
  1301. s_register, reg);
  1302. }
  1303. #endif
  1304. /*
  1305. * vpif_log_status() - Status information
  1306. * @file: file ptr
  1307. * @priv: file handle
  1308. *
  1309. * Returns zero.
  1310. */
  1311. static int vpif_log_status(struct file *filep, void *priv)
  1312. {
  1313. /* status for sub devices */
  1314. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  1315. return 0;
  1316. }
  1317. /* vpif display ioctl operations */
  1318. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1319. .vidioc_querycap = vpif_querycap,
  1320. .vidioc_g_priority = vpif_g_priority,
  1321. .vidioc_s_priority = vpif_s_priority,
  1322. .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
  1323. .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
  1324. .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
  1325. .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
  1326. .vidioc_reqbufs = vpif_reqbufs,
  1327. .vidioc_querybuf = vpif_querybuf,
  1328. .vidioc_qbuf = vpif_qbuf,
  1329. .vidioc_dqbuf = vpif_dqbuf,
  1330. .vidioc_streamon = vpif_streamon,
  1331. .vidioc_streamoff = vpif_streamoff,
  1332. .vidioc_s_std = vpif_s_std,
  1333. .vidioc_g_std = vpif_g_std,
  1334. .vidioc_enum_output = vpif_enum_output,
  1335. .vidioc_s_output = vpif_s_output,
  1336. .vidioc_g_output = vpif_g_output,
  1337. .vidioc_cropcap = vpif_cropcap,
  1338. .vidioc_enum_dv_presets = vpif_enum_dv_presets,
  1339. .vidioc_s_dv_preset = vpif_s_dv_preset,
  1340. .vidioc_g_dv_preset = vpif_g_dv_preset,
  1341. .vidioc_s_dv_timings = vpif_s_dv_timings,
  1342. .vidioc_g_dv_timings = vpif_g_dv_timings,
  1343. .vidioc_g_chip_ident = vpif_g_chip_ident,
  1344. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1345. .vidioc_g_register = vpif_dbg_g_register,
  1346. .vidioc_s_register = vpif_dbg_s_register,
  1347. #endif
  1348. .vidioc_log_status = vpif_log_status,
  1349. };
  1350. static const struct v4l2_file_operations vpif_fops = {
  1351. .owner = THIS_MODULE,
  1352. .open = vpif_open,
  1353. .release = vpif_release,
  1354. .unlocked_ioctl = video_ioctl2,
  1355. .mmap = vpif_mmap,
  1356. .poll = vpif_poll
  1357. };
  1358. static struct video_device vpif_video_template = {
  1359. .name = "vpif",
  1360. .fops = &vpif_fops,
  1361. .ioctl_ops = &vpif_ioctl_ops,
  1362. .tvnorms = DM646X_V4L2_STD,
  1363. .current_norm = V4L2_STD_625_50,
  1364. };
  1365. /*Configure the channels, buffer sizei, request irq */
  1366. static int initialize_vpif(void)
  1367. {
  1368. int free_channel_objects_index;
  1369. int free_buffer_channel_index;
  1370. int free_buffer_index;
  1371. int err = 0, i, j;
  1372. /* Default number of buffers should be 3 */
  1373. if ((ch2_numbuffers > 0) &&
  1374. (ch2_numbuffers < config_params.min_numbuffers))
  1375. ch2_numbuffers = config_params.min_numbuffers;
  1376. if ((ch3_numbuffers > 0) &&
  1377. (ch3_numbuffers < config_params.min_numbuffers))
  1378. ch3_numbuffers = config_params.min_numbuffers;
  1379. /* Set buffer size to min buffers size if invalid buffer size is
  1380. * given */
  1381. if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
  1382. ch2_bufsize =
  1383. config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
  1384. if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
  1385. ch3_bufsize =
  1386. config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
  1387. config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
  1388. if (ch2_numbuffers) {
  1389. config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
  1390. ch2_bufsize;
  1391. }
  1392. config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
  1393. if (ch3_numbuffers) {
  1394. config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
  1395. ch3_bufsize;
  1396. }
  1397. /* Allocate memory for six channel objects */
  1398. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1399. vpif_obj.dev[i] =
  1400. kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
  1401. /* If memory allocation fails, return error */
  1402. if (!vpif_obj.dev[i]) {
  1403. free_channel_objects_index = i;
  1404. err = -ENOMEM;
  1405. goto vpif_init_free_channel_objects;
  1406. }
  1407. }
  1408. free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
  1409. free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
  1410. free_buffer_index = config_params.numbuffers[i - 1];
  1411. return 0;
  1412. vpif_init_free_channel_objects:
  1413. for (j = 0; j < free_channel_objects_index; j++)
  1414. kfree(vpif_obj.dev[j]);
  1415. return err;
  1416. }
  1417. /*
  1418. * vpif_probe: This function creates device entries by register itself to the
  1419. * V4L2 driver and initializes fields of each channel objects
  1420. */
  1421. static __init int vpif_probe(struct platform_device *pdev)
  1422. {
  1423. struct vpif_subdev_info *subdevdata;
  1424. struct vpif_display_config *config;
  1425. int i, j = 0, k, q, m, err = 0;
  1426. struct i2c_adapter *i2c_adap;
  1427. struct common_obj *common;
  1428. struct channel_obj *ch;
  1429. struct video_device *vfd;
  1430. struct resource *res;
  1431. int subdev_count;
  1432. vpif_dev = &pdev->dev;
  1433. err = initialize_vpif();
  1434. if (err) {
  1435. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1436. return err;
  1437. }
  1438. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1439. if (err) {
  1440. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1441. return err;
  1442. }
  1443. k = 0;
  1444. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
  1445. for (i = res->start; i <= res->end; i++) {
  1446. if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
  1447. "DM646x_Display",
  1448. (void *)(&vpif_obj.dev[k]->channel_id))) {
  1449. err = -EBUSY;
  1450. goto vpif_int_err;
  1451. }
  1452. }
  1453. k++;
  1454. }
  1455. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1456. /* Get the pointer to the channel object */
  1457. ch = vpif_obj.dev[i];
  1458. /* Allocate memory for video device */
  1459. vfd = video_device_alloc();
  1460. if (vfd == NULL) {
  1461. for (j = 0; j < i; j++) {
  1462. ch = vpif_obj.dev[j];
  1463. video_device_release(ch->video_dev);
  1464. }
  1465. err = -ENOMEM;
  1466. goto vpif_int_err;
  1467. }
  1468. /* Initialize field of video device */
  1469. *vfd = vpif_video_template;
  1470. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1471. vfd->release = video_device_release;
  1472. snprintf(vfd->name, sizeof(vfd->name),
  1473. "DM646x_VPIFDisplay_DRIVER_V%s",
  1474. VPIF_DISPLAY_VERSION);
  1475. /* Set video_dev to the video device */
  1476. ch->video_dev = vfd;
  1477. }
  1478. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1479. ch = vpif_obj.dev[j];
  1480. /* Initialize field of the channel objects */
  1481. atomic_set(&ch->usrs, 0);
  1482. for (k = 0; k < VPIF_NUMOBJECTS; k++) {
  1483. ch->common[k].numbuffers = 0;
  1484. common = &ch->common[k];
  1485. common->io_usrs = 0;
  1486. common->started = 0;
  1487. spin_lock_init(&common->irqlock);
  1488. mutex_init(&common->lock);
  1489. common->numbuffers = 0;
  1490. common->set_addr = NULL;
  1491. common->ytop_off = common->ybtm_off = 0;
  1492. common->ctop_off = common->cbtm_off = 0;
  1493. common->cur_frm = common->next_frm = NULL;
  1494. memset(&common->fmt, 0, sizeof(common->fmt));
  1495. common->numbuffers = config_params.numbuffers[k];
  1496. }
  1497. ch->initialized = 0;
  1498. ch->channel_id = j;
  1499. if (j < 2)
  1500. ch->common[VPIF_VIDEO_INDEX].numbuffers =
  1501. config_params.numbuffers[ch->channel_id];
  1502. else
  1503. ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
  1504. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  1505. /* Initialize prio member of channel object */
  1506. v4l2_prio_init(&ch->prio);
  1507. ch->common[VPIF_VIDEO_INDEX].fmt.type =
  1508. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1509. ch->video_dev->lock = &common->lock;
  1510. /* register video device */
  1511. vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
  1512. (int)ch, (int)&ch->video_dev);
  1513. err = video_register_device(ch->video_dev,
  1514. VFL_TYPE_GRABBER, (j ? 3 : 2));
  1515. if (err < 0)
  1516. goto probe_out;
  1517. video_set_drvdata(ch->video_dev, ch);
  1518. }
  1519. i2c_adap = i2c_get_adapter(1);
  1520. config = pdev->dev.platform_data;
  1521. subdev_count = config->subdev_count;
  1522. subdevdata = config->subdevinfo;
  1523. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1524. GFP_KERNEL);
  1525. if (vpif_obj.sd == NULL) {
  1526. vpif_err("unable to allocate memory for subdevice pointers\n");
  1527. err = -ENOMEM;
  1528. goto probe_out;
  1529. }
  1530. for (i = 0; i < subdev_count; i++) {
  1531. vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1532. i2c_adap,
  1533. &subdevdata[i].board_info,
  1534. NULL);
  1535. if (!vpif_obj.sd[i]) {
  1536. vpif_err("Error registering v4l2 subdevice\n");
  1537. goto probe_subdev_out;
  1538. }
  1539. if (vpif_obj.sd[i])
  1540. vpif_obj.sd[i]->grp_id = 1 << i;
  1541. }
  1542. v4l2_info(&vpif_obj.v4l2_dev,
  1543. "DM646x VPIF display driver initialized\n");
  1544. return 0;
  1545. probe_subdev_out:
  1546. kfree(vpif_obj.sd);
  1547. probe_out:
  1548. for (k = 0; k < j; k++) {
  1549. ch = vpif_obj.dev[k];
  1550. video_unregister_device(ch->video_dev);
  1551. video_device_release(ch->video_dev);
  1552. ch->video_dev = NULL;
  1553. }
  1554. vpif_int_err:
  1555. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1556. vpif_err("VPIF IRQ request failed\n");
  1557. for (q = k; k >= 0; k--) {
  1558. for (m = i; m >= res->start; m--)
  1559. free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
  1560. res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
  1561. m = res->end;
  1562. }
  1563. return err;
  1564. }
  1565. /*
  1566. * vpif_remove: It un-register channels from V4L2 driver
  1567. */
  1568. static int vpif_remove(struct platform_device *device)
  1569. {
  1570. struct channel_obj *ch;
  1571. int i;
  1572. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1573. /* un-register device */
  1574. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1575. /* Get the pointer to the channel object */
  1576. ch = vpif_obj.dev[i];
  1577. /* Unregister video device */
  1578. video_unregister_device(ch->video_dev);
  1579. ch->video_dev = NULL;
  1580. }
  1581. return 0;
  1582. }
  1583. static __refdata struct platform_driver vpif_driver = {
  1584. .driver = {
  1585. .name = "vpif_display",
  1586. .owner = THIS_MODULE,
  1587. },
  1588. .probe = vpif_probe,
  1589. .remove = vpif_remove,
  1590. };
  1591. static __init int vpif_init(void)
  1592. {
  1593. return platform_driver_register(&vpif_driver);
  1594. }
  1595. /*
  1596. * vpif_cleanup: This function un-registers device and driver to the kernel,
  1597. * frees requested irq handler and de-allocates memory allocated for channel
  1598. * objects.
  1599. */
  1600. static void vpif_cleanup(void)
  1601. {
  1602. struct platform_device *pdev;
  1603. struct resource *res;
  1604. int irq_num;
  1605. int i = 0;
  1606. pdev = container_of(vpif_dev, struct platform_device, dev);
  1607. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
  1608. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1609. free_irq(irq_num,
  1610. (void *)(&vpif_obj.dev[i]->channel_id));
  1611. i++;
  1612. }
  1613. platform_driver_unregister(&vpif_driver);
  1614. kfree(vpif_obj.sd);
  1615. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
  1616. kfree(vpif_obj.dev[i]);
  1617. }
  1618. module_init(vpif_init);
  1619. module_exit(vpif_cleanup);