vpif_display.c 50 KB

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