vpif_display.c 45 KB

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