vpif_display.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  1. /*
  2. * vpif-display - VPIF display driver
  3. * Display driver for TI DaVinci VPIF
  4. *
  5. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  10. *
  11. * This program is distributed .as is. WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/mm.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/string.h>
  25. #include <linux/videodev2.h>
  26. #include <linux/wait.h>
  27. #include <linux/time.h>
  28. #include <linux/i2c.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/io.h>
  31. #include <linux/version.h>
  32. #include <linux/slab.h>
  33. #include <asm/irq.h>
  34. #include <asm/page.h>
  35. #include <media/adv7343.h>
  36. #include <media/v4l2-device.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <media/v4l2-chip-ident.h>
  39. #include <mach/dm646x.h>
  40. #include "vpif_display.h"
  41. #include "vpif.h"
  42. MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
  43. MODULE_LICENSE("GPL");
  44. #define DM646X_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
  45. #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
  46. #define vpif_dbg(level, debug, fmt, arg...) \
  47. v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
  48. static int debug = 1;
  49. static u32 ch2_numbuffers = 3;
  50. static u32 ch3_numbuffers = 3;
  51. static u32 ch2_bufsize = 1920 * 1080 * 2;
  52. static u32 ch3_bufsize = 720 * 576 * 2;
  53. module_param(debug, int, 0644);
  54. module_param(ch2_numbuffers, uint, S_IRUGO);
  55. module_param(ch3_numbuffers, uint, S_IRUGO);
  56. module_param(ch2_bufsize, uint, S_IRUGO);
  57. module_param(ch3_bufsize, uint, S_IRUGO);
  58. MODULE_PARM_DESC(debug, "Debug level 0-1");
  59. MODULE_PARM_DESC(ch2_numbuffers, "Channel2 buffer count (default:3)");
  60. MODULE_PARM_DESC(ch3_numbuffers, "Channel3 buffer count (default:3)");
  61. MODULE_PARM_DESC(ch2_bufsize, "Channel2 buffer size (default:1920 x 1080 x 2)");
  62. MODULE_PARM_DESC(ch3_bufsize, "Channel3 buffer size (default:720 x 576 x 2)");
  63. static struct vpif_config_params config_params = {
  64. .min_numbuffers = 3,
  65. .numbuffers[0] = 3,
  66. .numbuffers[1] = 3,
  67. .min_bufsize[0] = 720 * 480 * 2,
  68. .min_bufsize[1] = 720 * 480 * 2,
  69. .channel_bufsize[0] = 1920 * 1080 * 2,
  70. .channel_bufsize[1] = 720 * 576 * 2,
  71. };
  72. static struct vpif_device vpif_obj = { {NULL} };
  73. static struct device *vpif_dev;
  74. /*
  75. * vpif_uservirt_to_phys: This function is used to convert user
  76. * space virtual address to physical address.
  77. */
  78. static u32 vpif_uservirt_to_phys(u32 virtp)
  79. {
  80. struct mm_struct *mm = current->mm;
  81. unsigned long physp = 0;
  82. struct vm_area_struct *vma;
  83. vma = find_vma(mm, virtp);
  84. /* For kernel direct-mapped memory, take the easy way */
  85. if (virtp >= PAGE_OFFSET) {
  86. physp = virt_to_phys((void *)virtp);
  87. } else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff)) {
  88. /* this will catch, kernel-allocated, mmaped-to-usermode addr */
  89. physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
  90. } else {
  91. /* otherwise, use get_user_pages() for general userland pages */
  92. int res, nr_pages = 1;
  93. struct page *pages;
  94. down_read(&current->mm->mmap_sem);
  95. res = get_user_pages(current, current->mm,
  96. virtp, nr_pages, 1, 0, &pages, NULL);
  97. up_read(&current->mm->mmap_sem);
  98. if (res == nr_pages) {
  99. physp = __pa(page_address(&pages[0]) +
  100. (virtp & ~PAGE_MASK));
  101. } else {
  102. vpif_err("get_user_pages failed\n");
  103. return 0;
  104. }
  105. }
  106. return physp;
  107. }
  108. /*
  109. * buffer_prepare: This is the callback function called from videobuf_qbuf()
  110. * function the buffer is prepared and user space virtual address is converted
  111. * into physical address
  112. */
  113. static int vpif_buffer_prepare(struct videobuf_queue *q,
  114. struct videobuf_buffer *vb,
  115. enum v4l2_field field)
  116. {
  117. struct vpif_fh *fh = q->priv_data;
  118. struct common_obj *common;
  119. unsigned long addr;
  120. common = &fh->channel->common[VPIF_VIDEO_INDEX];
  121. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  122. vb->width = common->width;
  123. vb->height = common->height;
  124. vb->size = vb->width * vb->height;
  125. vb->field = field;
  126. }
  127. vb->state = VIDEOBUF_PREPARED;
  128. /* if user pointer memory mechanism is used, get the physical
  129. * address of the buffer */
  130. if (V4L2_MEMORY_USERPTR == common->memory) {
  131. if (!vb->baddr) {
  132. vpif_err("buffer_address is 0\n");
  133. return -EINVAL;
  134. }
  135. vb->boff = vpif_uservirt_to_phys(vb->baddr);
  136. if (!ISALIGNED(vb->boff))
  137. goto buf_align_exit;
  138. }
  139. addr = vb->boff;
  140. if (q->streaming && (V4L2_BUF_TYPE_SLICED_VBI_OUTPUT != q->type)) {
  141. if (!ISALIGNED(addr + common->ytop_off) ||
  142. !ISALIGNED(addr + common->ybtm_off) ||
  143. !ISALIGNED(addr + common->ctop_off) ||
  144. !ISALIGNED(addr + common->cbtm_off))
  145. goto buf_align_exit;
  146. }
  147. return 0;
  148. buf_align_exit:
  149. vpif_err("buffer offset not aligned to 8 bytes\n");
  150. return -EINVAL;
  151. }
  152. /*
  153. * vpif_buffer_setup: This function allocates memory for the buffers
  154. */
  155. static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  156. unsigned int *size)
  157. {
  158. struct vpif_fh *fh = q->priv_data;
  159. struct channel_obj *ch = fh->channel;
  160. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  161. if (V4L2_MEMORY_MMAP != common->memory)
  162. return 0;
  163. *size = config_params.channel_bufsize[ch->channel_id];
  164. if (*count < config_params.min_numbuffers)
  165. *count = config_params.min_numbuffers;
  166. return 0;
  167. }
  168. /*
  169. * vpif_buffer_queue: This function adds the buffer to DMA queue
  170. */
  171. static void vpif_buffer_queue(struct videobuf_queue *q,
  172. struct videobuf_buffer *vb)
  173. {
  174. struct vpif_fh *fh = q->priv_data;
  175. struct common_obj *common;
  176. common = &fh->channel->common[VPIF_VIDEO_INDEX];
  177. /* add the buffer to the DMA queue */
  178. list_add_tail(&vb->queue, &common->dma_queue);
  179. vb->state = VIDEOBUF_QUEUED;
  180. }
  181. /*
  182. * vpif_buffer_release: This function is called from the videobuf layer to
  183. * free memory allocated to the buffers
  184. */
  185. static void vpif_buffer_release(struct videobuf_queue *q,
  186. struct videobuf_buffer *vb)
  187. {
  188. struct vpif_fh *fh = q->priv_data;
  189. struct channel_obj *ch = fh->channel;
  190. struct common_obj *common;
  191. unsigned int buf_size = 0;
  192. common = &ch->common[VPIF_VIDEO_INDEX];
  193. videobuf_dma_contig_free(q, vb);
  194. vb->state = VIDEOBUF_NEEDS_INIT;
  195. if (V4L2_MEMORY_MMAP != common->memory)
  196. return;
  197. buf_size = config_params.channel_bufsize[ch->channel_id];
  198. }
  199. static struct videobuf_queue_ops video_qops = {
  200. .buf_setup = vpif_buffer_setup,
  201. .buf_prepare = vpif_buffer_prepare,
  202. .buf_queue = vpif_buffer_queue,
  203. .buf_release = vpif_buffer_release,
  204. };
  205. static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
  206. static void process_progressive_mode(struct common_obj *common)
  207. {
  208. unsigned long addr = 0;
  209. /* Get the next buffer from buffer queue */
  210. common->next_frm = list_entry(common->dma_queue.next,
  211. struct videobuf_buffer, queue);
  212. /* Remove that buffer from the buffer queue */
  213. list_del(&common->next_frm->queue);
  214. /* Mark status of the buffer as active */
  215. common->next_frm->state = VIDEOBUF_ACTIVE;
  216. /* Set top and bottom field addrs in VPIF registers */
  217. addr = videobuf_to_dma_contig(common->next_frm);
  218. common->set_addr(addr + common->ytop_off,
  219. addr + common->ybtm_off,
  220. addr + common->ctop_off,
  221. addr + common->cbtm_off);
  222. }
  223. static void process_interlaced_mode(int fid, struct common_obj *common)
  224. {
  225. /* device field id and local field id are in sync */
  226. /* If this is even field */
  227. if (0 == fid) {
  228. if (common->cur_frm == common->next_frm)
  229. return;
  230. /* one frame is displayed If next frame is
  231. * available, release cur_frm and move on */
  232. /* Copy frame display time */
  233. do_gettimeofday(&common->cur_frm->ts);
  234. /* Change status of the cur_frm */
  235. common->cur_frm->state = VIDEOBUF_DONE;
  236. /* unlock semaphore on cur_frm */
  237. wake_up_interruptible(&common->cur_frm->done);
  238. /* Make cur_frm pointing to next_frm */
  239. common->cur_frm = common->next_frm;
  240. } else if (1 == fid) { /* odd field */
  241. if (list_empty(&common->dma_queue)
  242. || (common->cur_frm != common->next_frm)) {
  243. return;
  244. }
  245. /* one field is displayed configure the next
  246. * frame if it is available else hold on current
  247. * frame */
  248. /* Get next from the buffer queue */
  249. process_progressive_mode(common);
  250. }
  251. }
  252. /*
  253. * vpif_channel_isr: It changes status of the displayed buffer, takes next
  254. * buffer from the queue and sets its address in VPIF registers
  255. */
  256. static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
  257. {
  258. struct vpif_device *dev = &vpif_obj;
  259. struct channel_obj *ch;
  260. struct common_obj *common;
  261. enum v4l2_field field;
  262. int fid = -1, i;
  263. int channel_id = 0;
  264. channel_id = *(int *)(dev_id);
  265. ch = dev->dev[channel_id];
  266. field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
  267. for (i = 0; i < VPIF_NUMOBJECTS; i++) {
  268. common = &ch->common[i];
  269. /* If streaming is started in this channel */
  270. if (0 == common->started)
  271. continue;
  272. if (1 == ch->vpifparams.std_info.frm_fmt) {
  273. if (list_empty(&common->dma_queue))
  274. continue;
  275. /* Progressive mode */
  276. if (!channel_first_int[i][channel_id]) {
  277. /* Mark status of the cur_frm to
  278. * done and unlock semaphore on it */
  279. do_gettimeofday(&common->cur_frm->ts);
  280. common->cur_frm->state = VIDEOBUF_DONE;
  281. wake_up_interruptible(&common->cur_frm->done);
  282. /* Make cur_frm pointing to next_frm */
  283. common->cur_frm = common->next_frm;
  284. }
  285. channel_first_int[i][channel_id] = 0;
  286. process_progressive_mode(common);
  287. } else {
  288. /* Interlaced mode */
  289. /* If it is first interrupt, ignore it */
  290. if (channel_first_int[i][channel_id]) {
  291. channel_first_int[i][channel_id] = 0;
  292. continue;
  293. }
  294. if (0 == i) {
  295. ch->field_id ^= 1;
  296. /* Get field id from VPIF registers */
  297. fid = vpif_channel_getfid(ch->channel_id + 2);
  298. /* If fid does not match with stored field id */
  299. if (fid != ch->field_id) {
  300. /* Make them in sync */
  301. if (0 == fid)
  302. ch->field_id = fid;
  303. return IRQ_HANDLED;
  304. }
  305. }
  306. process_interlaced_mode(fid, common);
  307. }
  308. }
  309. return IRQ_HANDLED;
  310. }
  311. static int vpif_update_std_info(struct channel_obj *ch)
  312. {
  313. struct video_obj *vid_ch = &ch->video;
  314. struct vpif_params *vpifparams = &ch->vpifparams;
  315. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  316. const struct vpif_channel_config_params *config;
  317. int i;
  318. for (i = 0; i < vpif_ch_params_count; i++) {
  319. config = &ch_params[i];
  320. if (config->hd_sd == 0) {
  321. vpif_dbg(2, debug, "SD format\n");
  322. if (config->stdid & vid_ch->stdid) {
  323. memcpy(std_info, config, sizeof(*config));
  324. break;
  325. }
  326. } else {
  327. vpif_dbg(2, debug, "HD format\n");
  328. if (config->dv_preset == vid_ch->dv_preset) {
  329. memcpy(std_info, config, sizeof(*config));
  330. break;
  331. }
  332. }
  333. }
  334. if (i == vpif_ch_params_count) {
  335. vpif_dbg(1, debug, "Format not found\n");
  336. return -EINVAL;
  337. }
  338. return 0;
  339. }
  340. static int vpif_update_resolution(struct channel_obj *ch)
  341. {
  342. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  343. struct video_obj *vid_ch = &ch->video;
  344. struct vpif_params *vpifparams = &ch->vpifparams;
  345. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  346. if (!vid_ch->stdid && !vid_ch->dv_preset && !vid_ch->bt_timings.height)
  347. return -EINVAL;
  348. if (vid_ch->stdid || vid_ch->dv_preset) {
  349. if (vpif_update_std_info(ch))
  350. return -EINVAL;
  351. }
  352. common->fmt.fmt.pix.width = std_info->width;
  353. common->fmt.fmt.pix.height = std_info->height;
  354. vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
  355. common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
  356. /* Set height and width paramateres */
  357. common->height = std_info->height;
  358. common->width = std_info->width;
  359. return 0;
  360. }
  361. /*
  362. * vpif_calculate_offsets: This function calculates buffers offset for Y and C
  363. * in the top and bottom field
  364. */
  365. static void vpif_calculate_offsets(struct channel_obj *ch)
  366. {
  367. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  368. struct vpif_params *vpifparams = &ch->vpifparams;
  369. enum v4l2_field field = common->fmt.fmt.pix.field;
  370. struct video_obj *vid_ch = &ch->video;
  371. unsigned int hpitch, vpitch, sizeimage;
  372. if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
  373. if (ch->vpifparams.std_info.frm_fmt)
  374. vid_ch->buf_field = V4L2_FIELD_NONE;
  375. else
  376. vid_ch->buf_field = V4L2_FIELD_INTERLACED;
  377. } else {
  378. vid_ch->buf_field = common->fmt.fmt.pix.field;
  379. }
  380. if (V4L2_MEMORY_USERPTR == common->memory)
  381. sizeimage = common->fmt.fmt.pix.sizeimage;
  382. else
  383. sizeimage = config_params.channel_bufsize[ch->channel_id];
  384. hpitch = common->fmt.fmt.pix.bytesperline;
  385. vpitch = sizeimage / (hpitch * 2);
  386. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  387. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  388. common->ytop_off = 0;
  389. common->ybtm_off = hpitch;
  390. common->ctop_off = sizeimage / 2;
  391. common->cbtm_off = sizeimage / 2 + hpitch;
  392. } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
  393. common->ytop_off = 0;
  394. common->ybtm_off = sizeimage / 4;
  395. common->ctop_off = sizeimage / 2;
  396. common->cbtm_off = common->ctop_off + sizeimage / 4;
  397. } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
  398. common->ybtm_off = 0;
  399. common->ytop_off = sizeimage / 4;
  400. common->cbtm_off = sizeimage / 2;
  401. common->ctop_off = common->cbtm_off + sizeimage / 4;
  402. }
  403. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  404. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  405. vpifparams->video_params.storage_mode = 1;
  406. } else {
  407. vpifparams->video_params.storage_mode = 0;
  408. }
  409. if (ch->vpifparams.std_info.frm_fmt == 1) {
  410. vpifparams->video_params.hpitch =
  411. common->fmt.fmt.pix.bytesperline;
  412. } else {
  413. if ((field == V4L2_FIELD_ANY) ||
  414. (field == V4L2_FIELD_INTERLACED))
  415. vpifparams->video_params.hpitch =
  416. common->fmt.fmt.pix.bytesperline * 2;
  417. else
  418. vpifparams->video_params.hpitch =
  419. common->fmt.fmt.pix.bytesperline;
  420. }
  421. ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
  422. }
  423. static void vpif_config_format(struct channel_obj *ch)
  424. {
  425. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  426. common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
  427. if (config_params.numbuffers[ch->channel_id] == 0)
  428. common->memory = V4L2_MEMORY_USERPTR;
  429. else
  430. common->memory = V4L2_MEMORY_MMAP;
  431. common->fmt.fmt.pix.sizeimage =
  432. config_params.channel_bufsize[ch->channel_id];
  433. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  434. common->fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  435. }
  436. static int vpif_check_format(struct channel_obj *ch,
  437. struct v4l2_pix_format *pixfmt)
  438. {
  439. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  440. enum v4l2_field field = pixfmt->field;
  441. u32 sizeimage, hpitch, vpitch;
  442. if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
  443. goto invalid_fmt_exit;
  444. if (!(VPIF_VALID_FIELD(field)))
  445. goto invalid_fmt_exit;
  446. if (pixfmt->bytesperline <= 0)
  447. goto invalid_pitch_exit;
  448. if (V4L2_MEMORY_USERPTR == common->memory)
  449. sizeimage = pixfmt->sizeimage;
  450. else
  451. sizeimage = config_params.channel_bufsize[ch->channel_id];
  452. if (vpif_update_resolution(ch))
  453. return -EINVAL;
  454. hpitch = pixfmt->bytesperline;
  455. vpitch = sizeimage / (hpitch * 2);
  456. /* Check for valid value of pitch */
  457. if ((hpitch < ch->vpifparams.std_info.width) ||
  458. (vpitch < ch->vpifparams.std_info.height))
  459. goto invalid_pitch_exit;
  460. /* Check for 8 byte alignment */
  461. if (!ISALIGNED(hpitch)) {
  462. vpif_err("invalid pitch alignment\n");
  463. return -EINVAL;
  464. }
  465. pixfmt->width = common->fmt.fmt.pix.width;
  466. pixfmt->height = common->fmt.fmt.pix.height;
  467. return 0;
  468. invalid_fmt_exit:
  469. vpif_err("invalid field format\n");
  470. return -EINVAL;
  471. invalid_pitch_exit:
  472. vpif_err("invalid pitch\n");
  473. return -EINVAL;
  474. }
  475. static void vpif_config_addr(struct channel_obj *ch, int muxmode)
  476. {
  477. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  478. if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
  479. common->set_addr = ch3_set_videobuf_addr;
  480. } else {
  481. if (2 == muxmode)
  482. common->set_addr = ch2_set_videobuf_addr_yc_nmux;
  483. else
  484. common->set_addr = ch2_set_videobuf_addr;
  485. }
  486. }
  487. /*
  488. * vpif_mmap: It is used to map kernel space buffers into user spaces
  489. */
  490. static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
  491. {
  492. struct vpif_fh *fh = filep->private_data;
  493. struct channel_obj *ch = fh->channel;
  494. struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
  495. vpif_dbg(2, debug, "vpif_mmap\n");
  496. return videobuf_mmap_mapper(&common->buffer_queue, vma);
  497. }
  498. /*
  499. * vpif_poll: It is used for select/poll system call
  500. */
  501. static unsigned int vpif_poll(struct file *filep, poll_table *wait)
  502. {
  503. struct vpif_fh *fh = filep->private_data;
  504. struct channel_obj *ch = fh->channel;
  505. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  506. if (common->started)
  507. return videobuf_poll_stream(filep, &common->buffer_queue, wait);
  508. return 0;
  509. }
  510. /*
  511. * vpif_open: It creates object of file handle structure and stores it in
  512. * private_data member of filepointer
  513. */
  514. static int vpif_open(struct file *filep)
  515. {
  516. struct video_device *vdev = video_devdata(filep);
  517. struct channel_obj *ch = NULL;
  518. struct vpif_fh *fh = NULL;
  519. ch = video_get_drvdata(vdev);
  520. /* Allocate memory for the file handle object */
  521. fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
  522. if (fh == NULL) {
  523. vpif_err("unable to allocate memory for file handle object\n");
  524. return -ENOMEM;
  525. }
  526. /* store pointer to fh in private_data member of filep */
  527. filep->private_data = fh;
  528. fh->channel = ch;
  529. fh->initialized = 0;
  530. if (!ch->initialized) {
  531. fh->initialized = 1;
  532. ch->initialized = 1;
  533. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  534. }
  535. /* Increment channel usrs counter */
  536. atomic_inc(&ch->usrs);
  537. /* Set io_allowed[VPIF_VIDEO_INDEX] member to false */
  538. fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
  539. /* Initialize priority of this instance to default priority */
  540. fh->prio = V4L2_PRIORITY_UNSET;
  541. v4l2_prio_open(&ch->prio, &fh->prio);
  542. return 0;
  543. }
  544. /*
  545. * vpif_release: This function deletes buffer queue, frees the buffers and
  546. * the vpif file handle
  547. */
  548. static int vpif_release(struct file *filep)
  549. {
  550. struct vpif_fh *fh = filep->private_data;
  551. struct channel_obj *ch = fh->channel;
  552. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  553. /* if this instance is doing IO */
  554. if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
  555. /* Reset io_usrs member of channel object */
  556. common->io_usrs = 0;
  557. /* Disable channel */
  558. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  559. enable_channel2(0);
  560. channel2_intr_enable(0);
  561. }
  562. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  563. (2 == common->started)) {
  564. enable_channel3(0);
  565. channel3_intr_enable(0);
  566. }
  567. common->started = 0;
  568. /* Free buffers allocated */
  569. videobuf_queue_cancel(&common->buffer_queue);
  570. videobuf_mmap_free(&common->buffer_queue);
  571. common->numbuffers =
  572. config_params.numbuffers[ch->channel_id];
  573. }
  574. /* Decrement channel usrs counter */
  575. atomic_dec(&ch->usrs);
  576. /* If this file handle has initialize encoder device, reset it */
  577. if (fh->initialized)
  578. ch->initialized = 0;
  579. /* Close the priority */
  580. v4l2_prio_close(&ch->prio, fh->prio);
  581. filep->private_data = NULL;
  582. fh->initialized = 0;
  583. kfree(fh);
  584. return 0;
  585. }
  586. /* functions implementing ioctls */
  587. /**
  588. * vpif_querycap() - QUERYCAP handler
  589. * @file: file ptr
  590. * @priv: file handle
  591. * @cap: ptr to v4l2_capability structure
  592. */
  593. static int vpif_querycap(struct file *file, void *priv,
  594. struct v4l2_capability *cap)
  595. {
  596. struct vpif_display_config *config = vpif_dev->platform_data;
  597. cap->version = VPIF_DISPLAY_VERSION_CODE;
  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 & DM646X_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 = DM646X_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 = DM646X_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_DISABLED,
  1449. "DM646x_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. "DM646x_VPIFDisplay_DRIVER_V%d.%d.%d",
  1476. (VPIF_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1477. (VPIF_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1478. (VPIF_DISPLAY_VERSION_CODE) & 0xff);
  1479. /* Set video_dev to the video device */
  1480. ch->video_dev = vfd;
  1481. }
  1482. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1483. ch = vpif_obj.dev[j];
  1484. /* Initialize field of the channel objects */
  1485. atomic_set(&ch->usrs, 0);
  1486. for (k = 0; k < VPIF_NUMOBJECTS; k++) {
  1487. ch->common[k].numbuffers = 0;
  1488. common = &ch->common[k];
  1489. common->io_usrs = 0;
  1490. common->started = 0;
  1491. spin_lock_init(&common->irqlock);
  1492. mutex_init(&common->lock);
  1493. common->numbuffers = 0;
  1494. common->set_addr = NULL;
  1495. common->ytop_off = common->ybtm_off = 0;
  1496. common->ctop_off = common->cbtm_off = 0;
  1497. common->cur_frm = common->next_frm = NULL;
  1498. memset(&common->fmt, 0, sizeof(common->fmt));
  1499. common->numbuffers = config_params.numbuffers[k];
  1500. }
  1501. ch->initialized = 0;
  1502. ch->channel_id = j;
  1503. if (j < 2)
  1504. ch->common[VPIF_VIDEO_INDEX].numbuffers =
  1505. config_params.numbuffers[ch->channel_id];
  1506. else
  1507. ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
  1508. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  1509. /* Initialize prio member of channel object */
  1510. v4l2_prio_init(&ch->prio);
  1511. ch->common[VPIF_VIDEO_INDEX].fmt.type =
  1512. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1513. ch->video_dev->lock = &common->lock;
  1514. /* register video device */
  1515. vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
  1516. (int)ch, (int)&ch->video_dev);
  1517. err = video_register_device(ch->video_dev,
  1518. VFL_TYPE_GRABBER, (j ? 3 : 2));
  1519. if (err < 0)
  1520. goto probe_out;
  1521. video_set_drvdata(ch->video_dev, ch);
  1522. }
  1523. i2c_adap = i2c_get_adapter(1);
  1524. config = pdev->dev.platform_data;
  1525. subdev_count = config->subdev_count;
  1526. subdevdata = config->subdevinfo;
  1527. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1528. GFP_KERNEL);
  1529. if (vpif_obj.sd == NULL) {
  1530. vpif_err("unable to allocate memory for subdevice pointers\n");
  1531. err = -ENOMEM;
  1532. goto probe_out;
  1533. }
  1534. for (i = 0; i < subdev_count; i++) {
  1535. vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1536. i2c_adap,
  1537. &subdevdata[i].board_info,
  1538. NULL);
  1539. if (!vpif_obj.sd[i]) {
  1540. vpif_err("Error registering v4l2 subdevice\n");
  1541. goto probe_subdev_out;
  1542. }
  1543. if (vpif_obj.sd[i])
  1544. vpif_obj.sd[i]->grp_id = 1 << i;
  1545. }
  1546. v4l2_info(&vpif_obj.v4l2_dev,
  1547. "DM646x VPIF display driver initialized\n");
  1548. return 0;
  1549. probe_subdev_out:
  1550. kfree(vpif_obj.sd);
  1551. probe_out:
  1552. for (k = 0; k < j; k++) {
  1553. ch = vpif_obj.dev[k];
  1554. video_unregister_device(ch->video_dev);
  1555. video_device_release(ch->video_dev);
  1556. ch->video_dev = NULL;
  1557. }
  1558. vpif_int_err:
  1559. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1560. vpif_err("VPIF IRQ request failed\n");
  1561. for (q = k; k >= 0; k--) {
  1562. for (m = i; m >= res->start; m--)
  1563. free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
  1564. res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
  1565. m = res->end;
  1566. }
  1567. return err;
  1568. }
  1569. /*
  1570. * vpif_remove: It un-register channels from V4L2 driver
  1571. */
  1572. static int vpif_remove(struct platform_device *device)
  1573. {
  1574. struct channel_obj *ch;
  1575. int i;
  1576. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1577. /* un-register device */
  1578. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1579. /* Get the pointer to the channel object */
  1580. ch = vpif_obj.dev[i];
  1581. /* Unregister video device */
  1582. video_unregister_device(ch->video_dev);
  1583. ch->video_dev = NULL;
  1584. }
  1585. return 0;
  1586. }
  1587. static __refdata struct platform_driver vpif_driver = {
  1588. .driver = {
  1589. .name = "vpif_display",
  1590. .owner = THIS_MODULE,
  1591. },
  1592. .probe = vpif_probe,
  1593. .remove = vpif_remove,
  1594. };
  1595. static __init int vpif_init(void)
  1596. {
  1597. return platform_driver_register(&vpif_driver);
  1598. }
  1599. /*
  1600. * vpif_cleanup: This function un-registers device and driver to the kernel,
  1601. * frees requested irq handler and de-allocates memory allocated for channel
  1602. * objects.
  1603. */
  1604. static void vpif_cleanup(void)
  1605. {
  1606. struct platform_device *pdev;
  1607. struct resource *res;
  1608. int irq_num;
  1609. int i = 0;
  1610. pdev = container_of(vpif_dev, struct platform_device, dev);
  1611. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
  1612. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1613. free_irq(irq_num,
  1614. (void *)(&vpif_obj.dev[i]->channel_id));
  1615. i++;
  1616. }
  1617. platform_driver_unregister(&vpif_driver);
  1618. kfree(vpif_obj.sd);
  1619. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
  1620. kfree(vpif_obj.dev[i]);
  1621. }
  1622. module_init(vpif_init);
  1623. module_exit(vpif_cleanup);