vpif_display.c 51 KB

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