vpif_display.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  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 <mach/dm646x.h>
  39. #include "vpif_display.h"
  40. #include "vpif.h"
  41. MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
  42. MODULE_LICENSE("GPL");
  43. MODULE_VERSION(VPIF_DISPLAY_VERSION);
  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->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  598. strlcpy(cap->driver, "vpif display", sizeof(cap->driver));
  599. strlcpy(cap->bus_info, "Platform", sizeof(cap->bus_info));
  600. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  601. return 0;
  602. }
  603. static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
  604. struct v4l2_fmtdesc *fmt)
  605. {
  606. if (fmt->index != 0) {
  607. vpif_err("Invalid format index\n");
  608. return -EINVAL;
  609. }
  610. /* Fill in the information about format */
  611. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  612. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  613. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  614. return 0;
  615. }
  616. static int vpif_g_fmt_vid_out(struct file *file, void *priv,
  617. struct v4l2_format *fmt)
  618. {
  619. struct vpif_fh *fh = priv;
  620. struct channel_obj *ch = fh->channel;
  621. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  622. /* Check the validity of the buffer type */
  623. if (common->fmt.type != fmt->type)
  624. return -EINVAL;
  625. if (vpif_update_resolution(ch))
  626. return -EINVAL;
  627. *fmt = common->fmt;
  628. return 0;
  629. }
  630. static int vpif_s_fmt_vid_out(struct file *file, void *priv,
  631. struct v4l2_format *fmt)
  632. {
  633. struct vpif_fh *fh = priv;
  634. struct v4l2_pix_format *pixfmt;
  635. struct channel_obj *ch = fh->channel;
  636. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  637. int ret = 0;
  638. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  639. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  640. if (!fh->initialized) {
  641. vpif_dbg(1, debug, "Channel Busy\n");
  642. return -EBUSY;
  643. }
  644. /* Check for the priority */
  645. ret = v4l2_prio_check(&ch->prio, fh->prio);
  646. if (0 != ret)
  647. return ret;
  648. fh->initialized = 1;
  649. }
  650. if (common->started) {
  651. vpif_dbg(1, debug, "Streaming in progress\n");
  652. return -EBUSY;
  653. }
  654. pixfmt = &fmt->fmt.pix;
  655. /* Check for valid field format */
  656. ret = vpif_check_format(ch, pixfmt);
  657. if (ret)
  658. return ret;
  659. /* store the pix format in the channel object */
  660. common->fmt.fmt.pix = *pixfmt;
  661. /* store the format in the channel object */
  662. common->fmt = *fmt;
  663. return 0;
  664. }
  665. static int vpif_try_fmt_vid_out(struct file *file, void *priv,
  666. struct v4l2_format *fmt)
  667. {
  668. struct vpif_fh *fh = priv;
  669. struct channel_obj *ch = fh->channel;
  670. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  671. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  672. int ret = 0;
  673. ret = vpif_check_format(ch, pixfmt);
  674. if (ret) {
  675. *pixfmt = common->fmt.fmt.pix;
  676. pixfmt->sizeimage = pixfmt->width * pixfmt->height * 2;
  677. }
  678. return ret;
  679. }
  680. static int vpif_reqbufs(struct file *file, void *priv,
  681. struct v4l2_requestbuffers *reqbuf)
  682. {
  683. struct vpif_fh *fh = priv;
  684. struct channel_obj *ch = fh->channel;
  685. struct common_obj *common;
  686. enum v4l2_field field;
  687. u8 index = 0;
  688. /* This file handle has not initialized the channel,
  689. It is not allowed to do settings */
  690. if ((VPIF_CHANNEL2_VIDEO == ch->channel_id)
  691. || (VPIF_CHANNEL3_VIDEO == ch->channel_id)) {
  692. if (!fh->initialized) {
  693. vpif_err("Channel Busy\n");
  694. return -EBUSY;
  695. }
  696. }
  697. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != reqbuf->type)
  698. return -EINVAL;
  699. index = VPIF_VIDEO_INDEX;
  700. common = &ch->common[index];
  701. if (common->fmt.type != reqbuf->type)
  702. return -EINVAL;
  703. if (0 != common->io_usrs)
  704. return -EBUSY;
  705. if (reqbuf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  706. if (common->fmt.fmt.pix.field == V4L2_FIELD_ANY)
  707. field = V4L2_FIELD_INTERLACED;
  708. else
  709. field = common->fmt.fmt.pix.field;
  710. } else {
  711. field = V4L2_VBI_INTERLACED;
  712. }
  713. /* Initialize videobuf queue as per the buffer type */
  714. videobuf_queue_dma_contig_init(&common->buffer_queue,
  715. &video_qops, NULL,
  716. &common->irqlock,
  717. reqbuf->type, field,
  718. sizeof(struct videobuf_buffer), fh,
  719. &common->lock);
  720. /* Set io allowed member of file handle to TRUE */
  721. fh->io_allowed[index] = 1;
  722. /* Increment io usrs member of channel object to 1 */
  723. common->io_usrs = 1;
  724. /* Store type of memory requested in channel object */
  725. common->memory = reqbuf->memory;
  726. INIT_LIST_HEAD(&common->dma_queue);
  727. /* Allocate buffers */
  728. return videobuf_reqbufs(&common->buffer_queue, reqbuf);
  729. }
  730. static int vpif_querybuf(struct file *file, void *priv,
  731. struct v4l2_buffer *tbuf)
  732. {
  733. struct vpif_fh *fh = priv;
  734. struct channel_obj *ch = fh->channel;
  735. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  736. if (common->fmt.type != tbuf->type)
  737. return -EINVAL;
  738. return videobuf_querybuf(&common->buffer_queue, tbuf);
  739. }
  740. static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  741. {
  742. struct vpif_fh *fh = priv;
  743. struct channel_obj *ch = fh->channel;
  744. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  745. struct v4l2_buffer tbuf = *buf;
  746. struct videobuf_buffer *buf1;
  747. unsigned long addr = 0;
  748. unsigned long flags;
  749. int ret = 0;
  750. if (common->fmt.type != tbuf.type)
  751. return -EINVAL;
  752. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  753. vpif_err("fh->io_allowed\n");
  754. return -EACCES;
  755. }
  756. if (!(list_empty(&common->dma_queue)) ||
  757. (common->cur_frm != common->next_frm) ||
  758. !(common->started) ||
  759. (common->started && (0 == ch->field_id)))
  760. return videobuf_qbuf(&common->buffer_queue, buf);
  761. /* bufferqueue is empty store buffer address in VPIF registers */
  762. mutex_lock(&common->buffer_queue.vb_lock);
  763. buf1 = common->buffer_queue.bufs[tbuf.index];
  764. if (buf1->memory != tbuf.memory) {
  765. vpif_err("invalid buffer type\n");
  766. goto qbuf_exit;
  767. }
  768. if ((buf1->state == VIDEOBUF_QUEUED) ||
  769. (buf1->state == VIDEOBUF_ACTIVE)) {
  770. vpif_err("invalid state\n");
  771. goto qbuf_exit;
  772. }
  773. switch (buf1->memory) {
  774. case V4L2_MEMORY_MMAP:
  775. if (buf1->baddr == 0)
  776. goto qbuf_exit;
  777. break;
  778. case V4L2_MEMORY_USERPTR:
  779. if (tbuf.length < buf1->bsize)
  780. goto qbuf_exit;
  781. if ((VIDEOBUF_NEEDS_INIT != buf1->state)
  782. && (buf1->baddr != tbuf.m.userptr)) {
  783. vpif_buffer_release(&common->buffer_queue, buf1);
  784. buf1->baddr = tbuf.m.userptr;
  785. }
  786. break;
  787. default:
  788. goto qbuf_exit;
  789. }
  790. local_irq_save(flags);
  791. ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
  792. common->buffer_queue.field);
  793. if (ret < 0) {
  794. local_irq_restore(flags);
  795. goto qbuf_exit;
  796. }
  797. buf1->state = VIDEOBUF_ACTIVE;
  798. addr = buf1->boff;
  799. common->next_frm = buf1;
  800. if (tbuf.type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
  801. common->set_addr((addr + common->ytop_off),
  802. (addr + common->ybtm_off),
  803. (addr + common->ctop_off),
  804. (addr + common->cbtm_off));
  805. }
  806. local_irq_restore(flags);
  807. list_add_tail(&buf1->stream, &common->buffer_queue.stream);
  808. mutex_unlock(&common->buffer_queue.vb_lock);
  809. return 0;
  810. qbuf_exit:
  811. mutex_unlock(&common->buffer_queue.vb_lock);
  812. return -EINVAL;
  813. }
  814. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  815. {
  816. struct vpif_fh *fh = priv;
  817. struct channel_obj *ch = fh->channel;
  818. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  819. int ret = 0;
  820. if (!(*std_id & DM646X_V4L2_STD))
  821. return -EINVAL;
  822. if (common->started) {
  823. vpif_err("streaming in progress\n");
  824. return -EBUSY;
  825. }
  826. /* Call encoder subdevice function to set the standard */
  827. ch->video.stdid = *std_id;
  828. ch->video.dv_preset = V4L2_DV_INVALID;
  829. memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
  830. /* Get the information about the standard */
  831. if (vpif_update_resolution(ch))
  832. return -EINVAL;
  833. if ((ch->vpifparams.std_info.width *
  834. ch->vpifparams.std_info.height * 2) >
  835. config_params.channel_bufsize[ch->channel_id]) {
  836. vpif_err("invalid std for this size\n");
  837. return -EINVAL;
  838. }
  839. common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
  840. /* Configure the default format information */
  841. vpif_config_format(ch);
  842. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  843. s_std_output, *std_id);
  844. if (ret < 0) {
  845. vpif_err("Failed to set output standard\n");
  846. return ret;
  847. }
  848. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, core,
  849. s_std, *std_id);
  850. if (ret < 0)
  851. vpif_err("Failed to set standard for sub devices\n");
  852. return ret;
  853. }
  854. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  855. {
  856. struct vpif_fh *fh = priv;
  857. struct channel_obj *ch = fh->channel;
  858. *std = ch->video.stdid;
  859. return 0;
  860. }
  861. static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  862. {
  863. struct vpif_fh *fh = priv;
  864. struct channel_obj *ch = fh->channel;
  865. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  866. return videobuf_dqbuf(&common->buffer_queue, p,
  867. (file->f_flags & O_NONBLOCK));
  868. }
  869. static int vpif_streamon(struct file *file, void *priv,
  870. enum v4l2_buf_type buftype)
  871. {
  872. struct vpif_fh *fh = priv;
  873. struct channel_obj *ch = fh->channel;
  874. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  875. struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
  876. struct vpif_params *vpif = &ch->vpifparams;
  877. struct vpif_display_config *vpif_config_data =
  878. vpif_dev->platform_data;
  879. unsigned long addr = 0;
  880. int ret = 0;
  881. if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  882. vpif_err("buffer type not supported\n");
  883. return -EINVAL;
  884. }
  885. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  886. vpif_err("fh->io_allowed\n");
  887. return -EACCES;
  888. }
  889. /* If Streaming is already started, return error */
  890. if (common->started) {
  891. vpif_err("channel->started\n");
  892. return -EBUSY;
  893. }
  894. if ((ch->channel_id == VPIF_CHANNEL2_VIDEO
  895. && oth_ch->common[VPIF_VIDEO_INDEX].started &&
  896. ch->vpifparams.std_info.ycmux_mode == 0)
  897. || ((ch->channel_id == VPIF_CHANNEL3_VIDEO)
  898. && (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
  899. vpif_err("other channel is using\n");
  900. return -EBUSY;
  901. }
  902. ret = vpif_check_format(ch, &common->fmt.fmt.pix);
  903. if (ret < 0)
  904. return ret;
  905. /* Call videobuf_streamon to start streaming in videobuf */
  906. ret = videobuf_streamon(&common->buffer_queue);
  907. if (ret < 0) {
  908. vpif_err("videobuf_streamon\n");
  909. return ret;
  910. }
  911. /* If buffer queue is empty, return error */
  912. if (list_empty(&common->dma_queue)) {
  913. vpif_err("buffer queue is empty\n");
  914. return -EIO;
  915. }
  916. /* Get the next frame from the buffer queue */
  917. common->next_frm = common->cur_frm =
  918. list_entry(common->dma_queue.next,
  919. struct videobuf_buffer, queue);
  920. list_del(&common->cur_frm->queue);
  921. /* Mark state of the current frame to active */
  922. common->cur_frm->state = VIDEOBUF_ACTIVE;
  923. /* Initialize field_id and started member */
  924. ch->field_id = 0;
  925. common->started = 1;
  926. if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  927. addr = common->cur_frm->boff;
  928. /* Calculate the offset for Y and C data in the buffer */
  929. vpif_calculate_offsets(ch);
  930. if ((ch->vpifparams.std_info.frm_fmt &&
  931. ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE)
  932. && (common->fmt.fmt.pix.field != V4L2_FIELD_ANY)))
  933. || (!ch->vpifparams.std_info.frm_fmt
  934. && (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
  935. vpif_err("conflict in field format and std format\n");
  936. return -EINVAL;
  937. }
  938. /* clock settings */
  939. ret =
  940. vpif_config_data->set_clock(ch->vpifparams.std_info.ycmux_mode,
  941. ch->vpifparams.std_info.hd_sd);
  942. if (ret < 0) {
  943. vpif_err("can't set clock\n");
  944. return ret;
  945. }
  946. /* set the parameters and addresses */
  947. ret = vpif_set_video_params(vpif, ch->channel_id + 2);
  948. if (ret < 0)
  949. return ret;
  950. common->started = ret;
  951. vpif_config_addr(ch, ret);
  952. common->set_addr((addr + common->ytop_off),
  953. (addr + common->ybtm_off),
  954. (addr + common->ctop_off),
  955. (addr + common->cbtm_off));
  956. /* Set interrupt for both the fields in VPIF
  957. Register enable channel in VPIF register */
  958. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  959. channel2_intr_assert();
  960. channel2_intr_enable(1);
  961. enable_channel2(1);
  962. }
  963. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id)
  964. || (common->started == 2)) {
  965. channel3_intr_assert();
  966. channel3_intr_enable(1);
  967. enable_channel3(1);
  968. }
  969. channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
  970. }
  971. return ret;
  972. }
  973. static int vpif_streamoff(struct file *file, void *priv,
  974. enum v4l2_buf_type buftype)
  975. {
  976. struct vpif_fh *fh = priv;
  977. struct channel_obj *ch = fh->channel;
  978. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  979. if (buftype != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  980. vpif_err("buffer type not supported\n");
  981. return -EINVAL;
  982. }
  983. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  984. vpif_err("fh->io_allowed\n");
  985. return -EACCES;
  986. }
  987. if (!common->started) {
  988. vpif_err("channel->started\n");
  989. return -EINVAL;
  990. }
  991. if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  992. /* disable channel */
  993. if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
  994. enable_channel2(0);
  995. channel2_intr_enable(0);
  996. }
  997. if ((VPIF_CHANNEL3_VIDEO == ch->channel_id) ||
  998. (2 == common->started)) {
  999. enable_channel3(0);
  1000. channel3_intr_enable(0);
  1001. }
  1002. }
  1003. common->started = 0;
  1004. return videobuf_streamoff(&common->buffer_queue);
  1005. }
  1006. static int vpif_cropcap(struct file *file, void *priv,
  1007. struct v4l2_cropcap *crop)
  1008. {
  1009. struct vpif_fh *fh = priv;
  1010. struct channel_obj *ch = fh->channel;
  1011. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1012. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != crop->type)
  1013. return -EINVAL;
  1014. crop->bounds.left = crop->bounds.top = 0;
  1015. crop->defrect.left = crop->defrect.top = 0;
  1016. crop->defrect.height = crop->bounds.height = common->height;
  1017. crop->defrect.width = crop->bounds.width = common->width;
  1018. return 0;
  1019. }
  1020. static int vpif_enum_output(struct file *file, void *fh,
  1021. struct v4l2_output *output)
  1022. {
  1023. struct vpif_display_config *config = vpif_dev->platform_data;
  1024. if (output->index >= config->output_count) {
  1025. vpif_dbg(1, debug, "Invalid output index\n");
  1026. return -EINVAL;
  1027. }
  1028. strcpy(output->name, config->output[output->index]);
  1029. output->type = V4L2_OUTPUT_TYPE_ANALOG;
  1030. output->std = DM646X_V4L2_STD;
  1031. return 0;
  1032. }
  1033. static int vpif_s_output(struct file *file, void *priv, unsigned int i)
  1034. {
  1035. struct vpif_fh *fh = priv;
  1036. struct channel_obj *ch = fh->channel;
  1037. struct video_obj *vid_ch = &ch->video;
  1038. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1039. int ret = 0;
  1040. if (common->started) {
  1041. vpif_err("Streaming in progress\n");
  1042. return -EBUSY;
  1043. }
  1044. ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
  1045. s_routing, 0, i, 0);
  1046. if (ret < 0)
  1047. vpif_err("Failed to set output standard\n");
  1048. vid_ch->output_id = i;
  1049. return ret;
  1050. }
  1051. static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
  1052. {
  1053. struct vpif_fh *fh = priv;
  1054. struct channel_obj *ch = fh->channel;
  1055. struct video_obj *vid_ch = &ch->video;
  1056. *i = vid_ch->output_id;
  1057. return 0;
  1058. }
  1059. static int vpif_g_priority(struct file *file, void *priv, enum v4l2_priority *p)
  1060. {
  1061. struct vpif_fh *fh = priv;
  1062. struct channel_obj *ch = fh->channel;
  1063. *p = v4l2_prio_max(&ch->prio);
  1064. return 0;
  1065. }
  1066. static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
  1067. {
  1068. struct vpif_fh *fh = priv;
  1069. struct channel_obj *ch = fh->channel;
  1070. return v4l2_prio_change(&ch->prio, &fh->prio, p);
  1071. }
  1072. /**
  1073. * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
  1074. * @file: file ptr
  1075. * @priv: file handle
  1076. * @preset: input preset
  1077. */
  1078. static int vpif_enum_dv_presets(struct file *file, void *priv,
  1079. struct v4l2_dv_enum_preset *preset)
  1080. {
  1081. struct vpif_fh *fh = priv;
  1082. struct channel_obj *ch = fh->channel;
  1083. struct video_obj *vid_ch = &ch->video;
  1084. return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
  1085. video, enum_dv_presets, preset);
  1086. }
  1087. /**
  1088. * vpif_s_dv_presets() - S_DV_PRESETS handler
  1089. * @file: file ptr
  1090. * @priv: file handle
  1091. * @preset: input preset
  1092. */
  1093. static int vpif_s_dv_preset(struct file *file, void *priv,
  1094. struct v4l2_dv_preset *preset)
  1095. {
  1096. struct vpif_fh *fh = priv;
  1097. struct channel_obj *ch = fh->channel;
  1098. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1099. struct video_obj *vid_ch = &ch->video;
  1100. int ret = 0;
  1101. if (common->started) {
  1102. vpif_dbg(1, debug, "streaming in progress\n");
  1103. return -EBUSY;
  1104. }
  1105. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1106. if (ret != 0)
  1107. return ret;
  1108. fh->initialized = 1;
  1109. /* Call encoder subdevice function to set the standard */
  1110. if (mutex_lock_interruptible(&common->lock))
  1111. return -ERESTARTSYS;
  1112. ch->video.dv_preset = preset->preset;
  1113. ch->video.stdid = V4L2_STD_UNKNOWN;
  1114. memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
  1115. /* Get the information about the standard */
  1116. if (vpif_update_resolution(ch)) {
  1117. ret = -EINVAL;
  1118. } else {
  1119. /* Configure the default format information */
  1120. vpif_config_format(ch);
  1121. ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
  1122. video, s_dv_preset, preset);
  1123. }
  1124. mutex_unlock(&common->lock);
  1125. return ret;
  1126. }
  1127. /**
  1128. * vpif_g_dv_presets() - G_DV_PRESETS handler
  1129. * @file: file ptr
  1130. * @priv: file handle
  1131. * @preset: input preset
  1132. */
  1133. static int vpif_g_dv_preset(struct file *file, void *priv,
  1134. struct v4l2_dv_preset *preset)
  1135. {
  1136. struct vpif_fh *fh = priv;
  1137. struct channel_obj *ch = fh->channel;
  1138. preset->preset = ch->video.dv_preset;
  1139. return 0;
  1140. }
  1141. /**
  1142. * vpif_s_dv_timings() - S_DV_TIMINGS handler
  1143. * @file: file ptr
  1144. * @priv: file handle
  1145. * @timings: digital video timings
  1146. */
  1147. static int vpif_s_dv_timings(struct file *file, void *priv,
  1148. struct v4l2_dv_timings *timings)
  1149. {
  1150. struct vpif_fh *fh = priv;
  1151. struct channel_obj *ch = fh->channel;
  1152. struct vpif_params *vpifparams = &ch->vpifparams;
  1153. struct vpif_channel_config_params *std_info = &vpifparams->std_info;
  1154. struct video_obj *vid_ch = &ch->video;
  1155. struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
  1156. int ret;
  1157. if (timings->type != V4L2_DV_BT_656_1120) {
  1158. vpif_dbg(2, debug, "Timing type not defined\n");
  1159. return -EINVAL;
  1160. }
  1161. /* Configure subdevice timings, if any */
  1162. ret = v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id],
  1163. video, s_dv_timings, timings);
  1164. if (ret == -ENOIOCTLCMD) {
  1165. vpif_dbg(2, debug, "Custom DV timings not supported by "
  1166. "subdevice\n");
  1167. return -EINVAL;
  1168. }
  1169. if (ret < 0) {
  1170. vpif_dbg(2, debug, "Error setting custom DV timings\n");
  1171. return ret;
  1172. }
  1173. if (!(timings->bt.width && timings->bt.height &&
  1174. (timings->bt.hbackporch ||
  1175. timings->bt.hfrontporch ||
  1176. timings->bt.hsync) &&
  1177. timings->bt.vfrontporch &&
  1178. (timings->bt.vbackporch ||
  1179. timings->bt.vsync))) {
  1180. vpif_dbg(2, debug, "Timings for width, height, "
  1181. "horizontal back porch, horizontal sync, "
  1182. "horizontal front porch, vertical back porch, "
  1183. "vertical sync and vertical back porch "
  1184. "must be defined\n");
  1185. return -EINVAL;
  1186. }
  1187. *bt = timings->bt;
  1188. /* Configure video port timings */
  1189. std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
  1190. bt->hsync - 8;
  1191. std_info->sav2eav = bt->width;
  1192. std_info->l1 = 1;
  1193. std_info->l3 = bt->vsync + bt->vbackporch + 1;
  1194. if (bt->interlaced) {
  1195. if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
  1196. std_info->vsize = bt->height * 2 +
  1197. bt->vfrontporch + bt->vsync + bt->vbackporch +
  1198. bt->il_vfrontporch + bt->il_vsync +
  1199. bt->il_vbackporch;
  1200. std_info->l5 = std_info->vsize/2 -
  1201. (bt->vfrontporch - 1);
  1202. std_info->l7 = std_info->vsize/2 + 1;
  1203. std_info->l9 = std_info->l7 + bt->il_vsync +
  1204. bt->il_vbackporch + 1;
  1205. std_info->l11 = std_info->vsize -
  1206. (bt->il_vfrontporch - 1);
  1207. } else {
  1208. vpif_dbg(2, debug, "Required timing values for "
  1209. "interlaced BT format missing\n");
  1210. return -EINVAL;
  1211. }
  1212. } else {
  1213. std_info->vsize = bt->height + bt->vfrontporch +
  1214. bt->vsync + bt->vbackporch;
  1215. std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
  1216. }
  1217. strncpy(std_info->name, "Custom timings BT656/1120",
  1218. VPIF_MAX_NAME);
  1219. std_info->width = bt->width;
  1220. std_info->height = bt->height;
  1221. std_info->frm_fmt = bt->interlaced ? 0 : 1;
  1222. std_info->ycmux_mode = 0;
  1223. std_info->capture_format = 0;
  1224. std_info->vbi_supported = 0;
  1225. std_info->hd_sd = 1;
  1226. std_info->stdid = 0;
  1227. std_info->dv_preset = V4L2_DV_INVALID;
  1228. vid_ch->stdid = 0;
  1229. vid_ch->dv_preset = V4L2_DV_INVALID;
  1230. return 0;
  1231. }
  1232. /**
  1233. * vpif_g_dv_timings() - G_DV_TIMINGS handler
  1234. * @file: file ptr
  1235. * @priv: file handle
  1236. * @timings: digital video timings
  1237. */
  1238. static int vpif_g_dv_timings(struct file *file, void *priv,
  1239. struct v4l2_dv_timings *timings)
  1240. {
  1241. struct vpif_fh *fh = priv;
  1242. struct channel_obj *ch = fh->channel;
  1243. struct video_obj *vid_ch = &ch->video;
  1244. struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
  1245. timings->bt = *bt;
  1246. return 0;
  1247. }
  1248. /*
  1249. * vpif_g_chip_ident() - Identify the chip
  1250. * @file: file ptr
  1251. * @priv: file handle
  1252. * @chip: chip identity
  1253. *
  1254. * Returns zero or -EINVAL if read operations fails.
  1255. */
  1256. static int vpif_g_chip_ident(struct file *file, void *priv,
  1257. struct v4l2_dbg_chip_ident *chip)
  1258. {
  1259. chip->ident = V4L2_IDENT_NONE;
  1260. chip->revision = 0;
  1261. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  1262. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
  1263. vpif_dbg(2, debug, "match_type is invalid.\n");
  1264. return -EINVAL;
  1265. }
  1266. return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
  1267. g_chip_ident, chip);
  1268. }
  1269. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1270. /*
  1271. * vpif_dbg_g_register() - Read register
  1272. * @file: file ptr
  1273. * @priv: file handle
  1274. * @reg: register to be read
  1275. *
  1276. * Debugging only
  1277. * Returns zero or -EINVAL if read operations fails.
  1278. */
  1279. static int vpif_dbg_g_register(struct file *file, void *priv,
  1280. struct v4l2_dbg_register *reg){
  1281. struct vpif_fh *fh = priv;
  1282. struct channel_obj *ch = fh->channel;
  1283. struct video_obj *vid_ch = &ch->video;
  1284. return v4l2_subdev_call(vpif_obj.sd[vid_ch->output_id], core,
  1285. g_register, reg);
  1286. }
  1287. /*
  1288. * vpif_dbg_s_register() - Write to register
  1289. * @file: file ptr
  1290. * @priv: file handle
  1291. * @reg: register to be modified
  1292. *
  1293. * Debugging only
  1294. * Returns zero or -EINVAL if write operations fails.
  1295. */
  1296. static int vpif_dbg_s_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. s_register, reg);
  1303. }
  1304. #endif
  1305. /*
  1306. * vpif_log_status() - Status information
  1307. * @file: file ptr
  1308. * @priv: file handle
  1309. *
  1310. * Returns zero.
  1311. */
  1312. static int vpif_log_status(struct file *filep, void *priv)
  1313. {
  1314. /* status for sub devices */
  1315. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  1316. return 0;
  1317. }
  1318. /* vpif display ioctl operations */
  1319. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1320. .vidioc_querycap = vpif_querycap,
  1321. .vidioc_g_priority = vpif_g_priority,
  1322. .vidioc_s_priority = vpif_s_priority,
  1323. .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
  1324. .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
  1325. .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
  1326. .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
  1327. .vidioc_reqbufs = vpif_reqbufs,
  1328. .vidioc_querybuf = vpif_querybuf,
  1329. .vidioc_qbuf = vpif_qbuf,
  1330. .vidioc_dqbuf = vpif_dqbuf,
  1331. .vidioc_streamon = vpif_streamon,
  1332. .vidioc_streamoff = vpif_streamoff,
  1333. .vidioc_s_std = vpif_s_std,
  1334. .vidioc_g_std = vpif_g_std,
  1335. .vidioc_enum_output = vpif_enum_output,
  1336. .vidioc_s_output = vpif_s_output,
  1337. .vidioc_g_output = vpif_g_output,
  1338. .vidioc_cropcap = vpif_cropcap,
  1339. .vidioc_enum_dv_presets = vpif_enum_dv_presets,
  1340. .vidioc_s_dv_preset = vpif_s_dv_preset,
  1341. .vidioc_g_dv_preset = vpif_g_dv_preset,
  1342. .vidioc_s_dv_timings = vpif_s_dv_timings,
  1343. .vidioc_g_dv_timings = vpif_g_dv_timings,
  1344. .vidioc_g_chip_ident = vpif_g_chip_ident,
  1345. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1346. .vidioc_g_register = vpif_dbg_g_register,
  1347. .vidioc_s_register = vpif_dbg_s_register,
  1348. #endif
  1349. .vidioc_log_status = vpif_log_status,
  1350. };
  1351. static const struct v4l2_file_operations vpif_fops = {
  1352. .owner = THIS_MODULE,
  1353. .open = vpif_open,
  1354. .release = vpif_release,
  1355. .unlocked_ioctl = video_ioctl2,
  1356. .mmap = vpif_mmap,
  1357. .poll = vpif_poll
  1358. };
  1359. static struct video_device vpif_video_template = {
  1360. .name = "vpif",
  1361. .fops = &vpif_fops,
  1362. .ioctl_ops = &vpif_ioctl_ops,
  1363. .tvnorms = DM646X_V4L2_STD,
  1364. .current_norm = V4L2_STD_625_50,
  1365. };
  1366. /*Configure the channels, buffer sizei, request irq */
  1367. static int initialize_vpif(void)
  1368. {
  1369. int free_channel_objects_index;
  1370. int free_buffer_channel_index;
  1371. int free_buffer_index;
  1372. int err = 0, i, j;
  1373. /* Default number of buffers should be 3 */
  1374. if ((ch2_numbuffers > 0) &&
  1375. (ch2_numbuffers < config_params.min_numbuffers))
  1376. ch2_numbuffers = config_params.min_numbuffers;
  1377. if ((ch3_numbuffers > 0) &&
  1378. (ch3_numbuffers < config_params.min_numbuffers))
  1379. ch3_numbuffers = config_params.min_numbuffers;
  1380. /* Set buffer size to min buffers size if invalid buffer size is
  1381. * given */
  1382. if (ch2_bufsize < config_params.min_bufsize[VPIF_CHANNEL2_VIDEO])
  1383. ch2_bufsize =
  1384. config_params.min_bufsize[VPIF_CHANNEL2_VIDEO];
  1385. if (ch3_bufsize < config_params.min_bufsize[VPIF_CHANNEL3_VIDEO])
  1386. ch3_bufsize =
  1387. config_params.min_bufsize[VPIF_CHANNEL3_VIDEO];
  1388. config_params.numbuffers[VPIF_CHANNEL2_VIDEO] = ch2_numbuffers;
  1389. if (ch2_numbuffers) {
  1390. config_params.channel_bufsize[VPIF_CHANNEL2_VIDEO] =
  1391. ch2_bufsize;
  1392. }
  1393. config_params.numbuffers[VPIF_CHANNEL3_VIDEO] = ch3_numbuffers;
  1394. if (ch3_numbuffers) {
  1395. config_params.channel_bufsize[VPIF_CHANNEL3_VIDEO] =
  1396. ch3_bufsize;
  1397. }
  1398. /* Allocate memory for six channel objects */
  1399. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1400. vpif_obj.dev[i] =
  1401. kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
  1402. /* If memory allocation fails, return error */
  1403. if (!vpif_obj.dev[i]) {
  1404. free_channel_objects_index = i;
  1405. err = -ENOMEM;
  1406. goto vpif_init_free_channel_objects;
  1407. }
  1408. }
  1409. free_channel_objects_index = VPIF_DISPLAY_MAX_DEVICES;
  1410. free_buffer_channel_index = VPIF_DISPLAY_NUM_CHANNELS;
  1411. free_buffer_index = config_params.numbuffers[i - 1];
  1412. return 0;
  1413. vpif_init_free_channel_objects:
  1414. for (j = 0; j < free_channel_objects_index; j++)
  1415. kfree(vpif_obj.dev[j]);
  1416. return err;
  1417. }
  1418. /*
  1419. * vpif_probe: This function creates device entries by register itself to the
  1420. * V4L2 driver and initializes fields of each channel objects
  1421. */
  1422. static __init int vpif_probe(struct platform_device *pdev)
  1423. {
  1424. struct vpif_subdev_info *subdevdata;
  1425. struct vpif_display_config *config;
  1426. int i, j = 0, k, q, m, err = 0;
  1427. struct i2c_adapter *i2c_adap;
  1428. struct common_obj *common;
  1429. struct channel_obj *ch;
  1430. struct video_device *vfd;
  1431. struct resource *res;
  1432. int subdev_count;
  1433. vpif_dev = &pdev->dev;
  1434. err = initialize_vpif();
  1435. if (err) {
  1436. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1437. return err;
  1438. }
  1439. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1440. if (err) {
  1441. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1442. return err;
  1443. }
  1444. k = 0;
  1445. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
  1446. for (i = res->start; i <= res->end; i++) {
  1447. if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
  1448. "DM646x_Display",
  1449. (void *)(&vpif_obj.dev[k]->channel_id))) {
  1450. err = -EBUSY;
  1451. goto vpif_int_err;
  1452. }
  1453. }
  1454. k++;
  1455. }
  1456. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1457. /* Get the pointer to the channel object */
  1458. ch = vpif_obj.dev[i];
  1459. /* Allocate memory for video device */
  1460. vfd = video_device_alloc();
  1461. if (vfd == NULL) {
  1462. for (j = 0; j < i; j++) {
  1463. ch = vpif_obj.dev[j];
  1464. video_device_release(ch->video_dev);
  1465. }
  1466. err = -ENOMEM;
  1467. goto vpif_int_err;
  1468. }
  1469. /* Initialize field of video device */
  1470. *vfd = vpif_video_template;
  1471. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1472. vfd->release = video_device_release;
  1473. snprintf(vfd->name, sizeof(vfd->name),
  1474. "DM646x_VPIFDisplay_DRIVER_V%s",
  1475. VPIF_DISPLAY_VERSION);
  1476. /* Set video_dev to the video device */
  1477. ch->video_dev = vfd;
  1478. }
  1479. for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
  1480. ch = vpif_obj.dev[j];
  1481. /* Initialize field of the channel objects */
  1482. atomic_set(&ch->usrs, 0);
  1483. for (k = 0; k < VPIF_NUMOBJECTS; k++) {
  1484. ch->common[k].numbuffers = 0;
  1485. common = &ch->common[k];
  1486. common->io_usrs = 0;
  1487. common->started = 0;
  1488. spin_lock_init(&common->irqlock);
  1489. mutex_init(&common->lock);
  1490. common->numbuffers = 0;
  1491. common->set_addr = NULL;
  1492. common->ytop_off = common->ybtm_off = 0;
  1493. common->ctop_off = common->cbtm_off = 0;
  1494. common->cur_frm = common->next_frm = NULL;
  1495. memset(&common->fmt, 0, sizeof(common->fmt));
  1496. common->numbuffers = config_params.numbuffers[k];
  1497. }
  1498. ch->initialized = 0;
  1499. ch->channel_id = j;
  1500. if (j < 2)
  1501. ch->common[VPIF_VIDEO_INDEX].numbuffers =
  1502. config_params.numbuffers[ch->channel_id];
  1503. else
  1504. ch->common[VPIF_VIDEO_INDEX].numbuffers = 0;
  1505. memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
  1506. /* Initialize prio member of channel object */
  1507. v4l2_prio_init(&ch->prio);
  1508. ch->common[VPIF_VIDEO_INDEX].fmt.type =
  1509. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1510. ch->video_dev->lock = &common->lock;
  1511. /* register video device */
  1512. vpif_dbg(1, debug, "channel=%x,channel->video_dev=%x\n",
  1513. (int)ch, (int)&ch->video_dev);
  1514. err = video_register_device(ch->video_dev,
  1515. VFL_TYPE_GRABBER, (j ? 3 : 2));
  1516. if (err < 0)
  1517. goto probe_out;
  1518. video_set_drvdata(ch->video_dev, ch);
  1519. }
  1520. i2c_adap = i2c_get_adapter(1);
  1521. config = pdev->dev.platform_data;
  1522. subdev_count = config->subdev_count;
  1523. subdevdata = config->subdevinfo;
  1524. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1525. GFP_KERNEL);
  1526. if (vpif_obj.sd == NULL) {
  1527. vpif_err("unable to allocate memory for subdevice pointers\n");
  1528. err = -ENOMEM;
  1529. goto probe_out;
  1530. }
  1531. for (i = 0; i < subdev_count; i++) {
  1532. vpif_obj.sd[i] = v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1533. i2c_adap,
  1534. &subdevdata[i].board_info,
  1535. NULL);
  1536. if (!vpif_obj.sd[i]) {
  1537. vpif_err("Error registering v4l2 subdevice\n");
  1538. goto probe_subdev_out;
  1539. }
  1540. if (vpif_obj.sd[i])
  1541. vpif_obj.sd[i]->grp_id = 1 << i;
  1542. }
  1543. v4l2_info(&vpif_obj.v4l2_dev,
  1544. "DM646x VPIF display driver initialized\n");
  1545. return 0;
  1546. probe_subdev_out:
  1547. kfree(vpif_obj.sd);
  1548. probe_out:
  1549. for (k = 0; k < j; k++) {
  1550. ch = vpif_obj.dev[k];
  1551. video_unregister_device(ch->video_dev);
  1552. video_device_release(ch->video_dev);
  1553. ch->video_dev = NULL;
  1554. }
  1555. vpif_int_err:
  1556. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1557. vpif_err("VPIF IRQ request failed\n");
  1558. for (q = k; k >= 0; k--) {
  1559. for (m = i; m >= res->start; m--)
  1560. free_irq(m, (void *)(&vpif_obj.dev[k]->channel_id));
  1561. res = platform_get_resource(pdev, IORESOURCE_IRQ, k-1);
  1562. m = res->end;
  1563. }
  1564. return err;
  1565. }
  1566. /*
  1567. * vpif_remove: It un-register channels from V4L2 driver
  1568. */
  1569. static int vpif_remove(struct platform_device *device)
  1570. {
  1571. struct channel_obj *ch;
  1572. int i;
  1573. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1574. /* un-register device */
  1575. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
  1576. /* Get the pointer to the channel object */
  1577. ch = vpif_obj.dev[i];
  1578. /* Unregister video device */
  1579. video_unregister_device(ch->video_dev);
  1580. ch->video_dev = NULL;
  1581. }
  1582. return 0;
  1583. }
  1584. static __refdata struct platform_driver vpif_driver = {
  1585. .driver = {
  1586. .name = "vpif_display",
  1587. .owner = THIS_MODULE,
  1588. },
  1589. .probe = vpif_probe,
  1590. .remove = vpif_remove,
  1591. };
  1592. static __init int vpif_init(void)
  1593. {
  1594. return platform_driver_register(&vpif_driver);
  1595. }
  1596. /*
  1597. * vpif_cleanup: This function un-registers device and driver to the kernel,
  1598. * frees requested irq handler and de-allocates memory allocated for channel
  1599. * objects.
  1600. */
  1601. static void vpif_cleanup(void)
  1602. {
  1603. struct platform_device *pdev;
  1604. struct resource *res;
  1605. int irq_num;
  1606. int i = 0;
  1607. pdev = container_of(vpif_dev, struct platform_device, dev);
  1608. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
  1609. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1610. free_irq(irq_num,
  1611. (void *)(&vpif_obj.dev[i]->channel_id));
  1612. i++;
  1613. }
  1614. platform_driver_unregister(&vpif_driver);
  1615. kfree(vpif_obj.sd);
  1616. for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++)
  1617. kfree(vpif_obj.dev[i]);
  1618. }
  1619. module_init(vpif_init);
  1620. module_exit(vpif_cleanup);