vpif_capture.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  1. /*
  2. * Copyright (C) 2009 Texas Instruments Inc
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * TODO : add support for VBI & HBI data service
  19. * add static buffer allocation
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/errno.h>
  25. #include <linux/fs.h>
  26. #include <linux/mm.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/string.h>
  30. #include <linux/videodev2.h>
  31. #include <linux/wait.h>
  32. #include <linux/time.h>
  33. #include <linux/i2c.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/io.h>
  36. #include <linux/version.h>
  37. #include <linux/slab.h>
  38. #include <media/v4l2-device.h>
  39. #include <media/v4l2-ioctl.h>
  40. #include <media/v4l2-chip-ident.h>
  41. #include "vpif_capture.h"
  42. #include "vpif.h"
  43. MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
  44. MODULE_LICENSE("GPL");
  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 ch0_numbuffers = 3;
  50. static u32 ch1_numbuffers = 3;
  51. static u32 ch0_bufsize = 1920 * 1080 * 2;
  52. static u32 ch1_bufsize = 720 * 576 * 2;
  53. module_param(debug, int, 0644);
  54. module_param(ch0_numbuffers, uint, S_IRUGO);
  55. module_param(ch1_numbuffers, uint, S_IRUGO);
  56. module_param(ch0_bufsize, uint, S_IRUGO);
  57. module_param(ch1_bufsize, uint, S_IRUGO);
  58. MODULE_PARM_DESC(debug, "Debug level 0-1");
  59. MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
  60. MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
  61. MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
  62. MODULE_PARM_DESC(ch3_bufsize, "Channel1 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. /* global variables */
  73. static struct vpif_device vpif_obj = { {NULL} };
  74. static struct device *vpif_dev;
  75. /**
  76. * ch_params: video standard configuration parameters for vpif
  77. */
  78. static const struct vpif_channel_config_params ch_params[] = {
  79. {
  80. "NTSC_M", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
  81. 286, 525, 525, 0, 1, 0, V4L2_STD_525_60,
  82. },
  83. {
  84. "PAL_BDGHIK", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
  85. 336, 624, 625, 0, 1, 0, V4L2_STD_625_50,
  86. },
  87. };
  88. /**
  89. * vpif_uservirt_to_phys : translate user/virtual address to phy address
  90. * @virtp: user/virtual address
  91. *
  92. * This inline function is used to convert user space virtual address to
  93. * physical address.
  94. */
  95. static inline u32 vpif_uservirt_to_phys(u32 virtp)
  96. {
  97. unsigned long physp = 0;
  98. struct mm_struct *mm = current->mm;
  99. struct vm_area_struct *vma;
  100. vma = find_vma(mm, virtp);
  101. /* For kernel direct-mapped memory, take the easy way */
  102. if (virtp >= PAGE_OFFSET)
  103. physp = virt_to_phys((void *)virtp);
  104. else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff))
  105. /**
  106. * this will catch, kernel-allocated, mmaped-to-usermode
  107. * addresses
  108. */
  109. physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
  110. else {
  111. /* otherwise, use get_user_pages() for general userland pages */
  112. int res, nr_pages = 1;
  113. struct page *pages;
  114. down_read(&current->mm->mmap_sem);
  115. res = get_user_pages(current, current->mm,
  116. virtp, nr_pages, 1, 0, &pages, NULL);
  117. up_read(&current->mm->mmap_sem);
  118. if (res == nr_pages)
  119. physp = __pa(page_address(&pages[0]) +
  120. (virtp & ~PAGE_MASK));
  121. else {
  122. vpif_err("get_user_pages failed\n");
  123. return 0;
  124. }
  125. }
  126. return physp;
  127. }
  128. /**
  129. * buffer_prepare : callback function for buffer prepare
  130. * @q : buffer queue ptr
  131. * @vb: ptr to video buffer
  132. * @field: field info
  133. *
  134. * This is the callback function for buffer prepare when videobuf_qbuf()
  135. * function is called. The buffer is prepared and user space virtual address
  136. * or user address is converted into physical address
  137. */
  138. static int vpif_buffer_prepare(struct videobuf_queue *q,
  139. struct videobuf_buffer *vb,
  140. enum v4l2_field field)
  141. {
  142. /* Get the file handle object and channel object */
  143. struct vpif_fh *fh = q->priv_data;
  144. struct channel_obj *ch = fh->channel;
  145. struct common_obj *common;
  146. unsigned long addr;
  147. vpif_dbg(2, debug, "vpif_buffer_prepare\n");
  148. common = &ch->common[VPIF_VIDEO_INDEX];
  149. /* If buffer is not initialized, initialize it */
  150. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  151. vb->width = common->width;
  152. vb->height = common->height;
  153. vb->size = vb->width * vb->height;
  154. vb->field = field;
  155. }
  156. vb->state = VIDEOBUF_PREPARED;
  157. /**
  158. * if user pointer memory mechanism is used, get the physical
  159. * address of the buffer
  160. */
  161. if (V4L2_MEMORY_USERPTR == common->memory) {
  162. if (0 == vb->baddr) {
  163. vpif_dbg(1, debug, "buffer address is 0\n");
  164. return -EINVAL;
  165. }
  166. vb->boff = vpif_uservirt_to_phys(vb->baddr);
  167. if (!IS_ALIGNED(vb->boff, 8))
  168. goto exit;
  169. }
  170. addr = vb->boff;
  171. if (q->streaming) {
  172. if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
  173. !IS_ALIGNED((addr + common->ybtm_off), 8) ||
  174. !IS_ALIGNED((addr + common->ctop_off), 8) ||
  175. !IS_ALIGNED((addr + common->cbtm_off), 8))
  176. goto exit;
  177. }
  178. return 0;
  179. exit:
  180. vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
  181. return -EINVAL;
  182. }
  183. /**
  184. * vpif_buffer_setup : Callback function for buffer setup.
  185. * @q: buffer queue ptr
  186. * @count: number of buffers
  187. * @size: size of the buffer
  188. *
  189. * This callback function is called when reqbuf() is called to adjust
  190. * the buffer count and buffer size
  191. */
  192. static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  193. unsigned int *size)
  194. {
  195. /* Get the file handle object and channel object */
  196. struct vpif_fh *fh = q->priv_data;
  197. struct channel_obj *ch = fh->channel;
  198. struct common_obj *common;
  199. common = &ch->common[VPIF_VIDEO_INDEX];
  200. vpif_dbg(2, debug, "vpif_buffer_setup\n");
  201. /* If memory type is not mmap, return */
  202. if (V4L2_MEMORY_MMAP != common->memory)
  203. return 0;
  204. /* Calculate the size of the buffer */
  205. *size = config_params.channel_bufsize[ch->channel_id];
  206. if (*count < config_params.min_numbuffers)
  207. *count = config_params.min_numbuffers;
  208. return 0;
  209. }
  210. /**
  211. * vpif_buffer_queue : Callback function to add buffer to DMA queue
  212. * @q: ptr to videobuf_queue
  213. * @vb: ptr to videobuf_buffer
  214. */
  215. static void vpif_buffer_queue(struct videobuf_queue *q,
  216. struct videobuf_buffer *vb)
  217. {
  218. /* Get the file handle object and channel object */
  219. struct vpif_fh *fh = q->priv_data;
  220. struct channel_obj *ch = fh->channel;
  221. struct common_obj *common;
  222. common = &ch->common[VPIF_VIDEO_INDEX];
  223. vpif_dbg(2, debug, "vpif_buffer_queue\n");
  224. /* add the buffer to the DMA queue */
  225. list_add_tail(&vb->queue, &common->dma_queue);
  226. /* Change state of the buffer */
  227. vb->state = VIDEOBUF_QUEUED;
  228. }
  229. /**
  230. * vpif_buffer_release : Callback function to free buffer
  231. * @q: buffer queue ptr
  232. * @vb: ptr to video buffer
  233. *
  234. * This function is called from the videobuf layer to free memory
  235. * allocated to the buffers
  236. */
  237. static void vpif_buffer_release(struct videobuf_queue *q,
  238. struct videobuf_buffer *vb)
  239. {
  240. /* Get the file handle object and channel object */
  241. struct vpif_fh *fh = q->priv_data;
  242. struct channel_obj *ch = fh->channel;
  243. struct common_obj *common;
  244. common = &ch->common[VPIF_VIDEO_INDEX];
  245. videobuf_dma_contig_free(q, vb);
  246. vb->state = VIDEOBUF_NEEDS_INIT;
  247. }
  248. static struct videobuf_queue_ops video_qops = {
  249. .buf_setup = vpif_buffer_setup,
  250. .buf_prepare = vpif_buffer_prepare,
  251. .buf_queue = vpif_buffer_queue,
  252. .buf_release = vpif_buffer_release,
  253. };
  254. static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
  255. { {1, 1} };
  256. /**
  257. * vpif_process_buffer_complete: process a completed buffer
  258. * @common: ptr to common channel object
  259. *
  260. * This function time stamp the buffer and mark it as DONE. It also
  261. * wake up any process waiting on the QUEUE and set the next buffer
  262. * as current
  263. */
  264. static void vpif_process_buffer_complete(struct common_obj *common)
  265. {
  266. do_gettimeofday(&common->cur_frm->ts);
  267. common->cur_frm->state = VIDEOBUF_DONE;
  268. wake_up_interruptible(&common->cur_frm->done);
  269. /* Make curFrm pointing to nextFrm */
  270. common->cur_frm = common->next_frm;
  271. }
  272. /**
  273. * vpif_schedule_next_buffer: set next buffer address for capture
  274. * @common : ptr to common channel object
  275. *
  276. * This function will get next buffer from the dma queue and
  277. * set the buffer address in the vpif register for capture.
  278. * the buffer is marked active
  279. */
  280. static void vpif_schedule_next_buffer(struct common_obj *common)
  281. {
  282. unsigned long addr = 0;
  283. common->next_frm = list_entry(common->dma_queue.next,
  284. struct videobuf_buffer, queue);
  285. /* Remove that buffer from the buffer queue */
  286. list_del(&common->next_frm->queue);
  287. common->next_frm->state = VIDEOBUF_ACTIVE;
  288. if (V4L2_MEMORY_USERPTR == common->memory)
  289. addr = common->next_frm->boff;
  290. else
  291. addr = videobuf_to_dma_contig(common->next_frm);
  292. /* Set top and bottom field addresses in VPIF registers */
  293. common->set_addr(addr + common->ytop_off,
  294. addr + common->ybtm_off,
  295. addr + common->ctop_off,
  296. addr + common->cbtm_off);
  297. }
  298. /**
  299. * vpif_channel_isr : ISR handler for vpif capture
  300. * @irq: irq number
  301. * @dev_id: dev_id ptr
  302. *
  303. * It changes status of the captured buffer, takes next buffer from the queue
  304. * and sets its address in VPIF registers
  305. */
  306. static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
  307. {
  308. struct vpif_device *dev = &vpif_obj;
  309. struct common_obj *common;
  310. struct channel_obj *ch;
  311. enum v4l2_field field;
  312. int channel_id = 0;
  313. int fid = -1, i;
  314. channel_id = *(int *)(dev_id);
  315. ch = dev->dev[channel_id];
  316. field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
  317. for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
  318. common = &ch->common[i];
  319. /* skip If streaming is not started in this channel */
  320. if (0 == common->started)
  321. continue;
  322. /* Check the field format */
  323. if (1 == ch->vpifparams.std_info.frm_fmt) {
  324. /* Progressive mode */
  325. if (list_empty(&common->dma_queue))
  326. continue;
  327. if (!channel_first_int[i][channel_id])
  328. vpif_process_buffer_complete(common);
  329. channel_first_int[i][channel_id] = 0;
  330. vpif_schedule_next_buffer(common);
  331. channel_first_int[i][channel_id] = 0;
  332. } else {
  333. /**
  334. * Interlaced mode. If it is first interrupt, ignore
  335. * it
  336. */
  337. if (channel_first_int[i][channel_id]) {
  338. channel_first_int[i][channel_id] = 0;
  339. continue;
  340. }
  341. if (0 == i) {
  342. ch->field_id ^= 1;
  343. /* Get field id from VPIF registers */
  344. fid = vpif_channel_getfid(ch->channel_id);
  345. if (fid != ch->field_id) {
  346. /**
  347. * If field id does not match stored
  348. * field id, make them in sync
  349. */
  350. if (0 == fid)
  351. ch->field_id = fid;
  352. return IRQ_HANDLED;
  353. }
  354. }
  355. /* device field id and local field id are in sync */
  356. if (0 == fid) {
  357. /* this is even field */
  358. if (common->cur_frm == common->next_frm)
  359. continue;
  360. /* mark the current buffer as done */
  361. vpif_process_buffer_complete(common);
  362. } else if (1 == fid) {
  363. /* odd field */
  364. if (list_empty(&common->dma_queue) ||
  365. (common->cur_frm != common->next_frm))
  366. continue;
  367. vpif_schedule_next_buffer(common);
  368. }
  369. }
  370. }
  371. return IRQ_HANDLED;
  372. }
  373. /**
  374. * vpif_update_std_info() - update standard related info
  375. * @ch: ptr to channel object
  376. *
  377. * For a given standard selected by application, update values
  378. * in the device data structures
  379. */
  380. static int vpif_update_std_info(struct channel_obj *ch)
  381. {
  382. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  383. struct vpif_params *vpifparams = &ch->vpifparams;
  384. const struct vpif_channel_config_params *config;
  385. struct vpif_channel_config_params *std_info;
  386. struct video_obj *vid_ch = &ch->video;
  387. int index;
  388. vpif_dbg(2, debug, "vpif_update_std_info\n");
  389. std_info = &vpifparams->std_info;
  390. for (index = 0; index < ARRAY_SIZE(ch_params); index++) {
  391. config = &ch_params[index];
  392. if (config->stdid & vid_ch->stdid) {
  393. memcpy(std_info, config, sizeof(*config));
  394. break;
  395. }
  396. }
  397. /* standard not found */
  398. if (index == ARRAY_SIZE(ch_params))
  399. return -EINVAL;
  400. common->fmt.fmt.pix.width = std_info->width;
  401. common->width = std_info->width;
  402. common->fmt.fmt.pix.height = std_info->height;
  403. common->height = std_info->height;
  404. common->fmt.fmt.pix.bytesperline = std_info->width;
  405. vpifparams->video_params.hpitch = std_info->width;
  406. vpifparams->video_params.storage_mode = std_info->frm_fmt;
  407. return 0;
  408. }
  409. /**
  410. * vpif_calculate_offsets : This function calculates buffers offsets
  411. * @ch : ptr to channel object
  412. *
  413. * This function calculates buffer offsets for Y and C in the top and
  414. * bottom field
  415. */
  416. static void vpif_calculate_offsets(struct channel_obj *ch)
  417. {
  418. unsigned int hpitch, vpitch, sizeimage;
  419. struct video_obj *vid_ch = &(ch->video);
  420. struct vpif_params *vpifparams = &ch->vpifparams;
  421. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  422. enum v4l2_field field = common->fmt.fmt.pix.field;
  423. vpif_dbg(2, debug, "vpif_calculate_offsets\n");
  424. if (V4L2_FIELD_ANY == field) {
  425. if (vpifparams->std_info.frm_fmt)
  426. vid_ch->buf_field = V4L2_FIELD_NONE;
  427. else
  428. vid_ch->buf_field = V4L2_FIELD_INTERLACED;
  429. } else
  430. vid_ch->buf_field = common->fmt.fmt.pix.field;
  431. if (V4L2_MEMORY_USERPTR == common->memory)
  432. sizeimage = common->fmt.fmt.pix.sizeimage;
  433. else
  434. sizeimage = config_params.channel_bufsize[ch->channel_id];
  435. hpitch = common->fmt.fmt.pix.bytesperline;
  436. vpitch = sizeimage / (hpitch * 2);
  437. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  438. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  439. /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
  440. common->ytop_off = 0;
  441. common->ybtm_off = hpitch;
  442. common->ctop_off = sizeimage / 2;
  443. common->cbtm_off = sizeimage / 2 + hpitch;
  444. } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
  445. /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
  446. common->ytop_off = 0;
  447. common->ybtm_off = sizeimage / 4;
  448. common->ctop_off = sizeimage / 2;
  449. common->cbtm_off = common->ctop_off + sizeimage / 4;
  450. } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
  451. /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
  452. common->ybtm_off = 0;
  453. common->ytop_off = sizeimage / 4;
  454. common->cbtm_off = sizeimage / 2;
  455. common->ctop_off = common->cbtm_off + sizeimage / 4;
  456. }
  457. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  458. (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
  459. vpifparams->video_params.storage_mode = 1;
  460. else
  461. vpifparams->video_params.storage_mode = 0;
  462. if (1 == vpifparams->std_info.frm_fmt)
  463. vpifparams->video_params.hpitch =
  464. common->fmt.fmt.pix.bytesperline;
  465. else {
  466. if ((field == V4L2_FIELD_ANY)
  467. || (field == V4L2_FIELD_INTERLACED))
  468. vpifparams->video_params.hpitch =
  469. common->fmt.fmt.pix.bytesperline * 2;
  470. else
  471. vpifparams->video_params.hpitch =
  472. common->fmt.fmt.pix.bytesperline;
  473. }
  474. ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
  475. }
  476. /**
  477. * vpif_config_format: configure default frame format in the device
  478. * ch : ptr to channel object
  479. */
  480. static void vpif_config_format(struct channel_obj *ch)
  481. {
  482. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  483. vpif_dbg(2, debug, "vpif_config_format\n");
  484. common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
  485. if (config_params.numbuffers[ch->channel_id] == 0)
  486. common->memory = V4L2_MEMORY_USERPTR;
  487. else
  488. common->memory = V4L2_MEMORY_MMAP;
  489. common->fmt.fmt.pix.sizeimage
  490. = config_params.channel_bufsize[ch->channel_id];
  491. if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
  492. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
  493. else
  494. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  495. common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  496. }
  497. /**
  498. * vpif_get_default_field() - Get default field type based on interface
  499. * @vpif_params - ptr to vpif params
  500. */
  501. static inline enum v4l2_field vpif_get_default_field(
  502. struct vpif_interface *iface)
  503. {
  504. return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
  505. V4L2_FIELD_INTERLACED;
  506. }
  507. /**
  508. * vpif_check_format() - check given pixel format for compatibility
  509. * @ch - channel ptr
  510. * @pixfmt - Given pixel format
  511. * @update - update the values as per hardware requirement
  512. *
  513. * Check the application pixel format for S_FMT and update the input
  514. * values as per hardware limits for TRY_FMT. The default pixel and
  515. * field format is selected based on interface type.
  516. */
  517. static int vpif_check_format(struct channel_obj *ch,
  518. struct v4l2_pix_format *pixfmt,
  519. int update)
  520. {
  521. struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
  522. struct vpif_params *vpif_params = &ch->vpifparams;
  523. enum v4l2_field field = pixfmt->field;
  524. u32 sizeimage, hpitch, vpitch;
  525. int ret = -EINVAL;
  526. vpif_dbg(2, debug, "vpif_check_format\n");
  527. /**
  528. * first check for the pixel format. If if_type is Raw bayer,
  529. * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
  530. * V4L2_PIX_FMT_YUV422P is supported
  531. */
  532. if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
  533. if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
  534. if (!update) {
  535. vpif_dbg(2, debug, "invalid pix format\n");
  536. goto exit;
  537. }
  538. pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
  539. }
  540. } else {
  541. if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
  542. if (!update) {
  543. vpif_dbg(2, debug, "invalid pixel format\n");
  544. goto exit;
  545. }
  546. pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  547. }
  548. }
  549. if (!(VPIF_VALID_FIELD(field))) {
  550. if (!update) {
  551. vpif_dbg(2, debug, "invalid field format\n");
  552. goto exit;
  553. }
  554. /**
  555. * By default use FIELD_NONE for RAW Bayer capture
  556. * and FIELD_INTERLACED for other interfaces
  557. */
  558. field = vpif_get_default_field(&vpif_params->iface);
  559. } else if (field == V4L2_FIELD_ANY)
  560. /* unsupported field. Use default */
  561. field = vpif_get_default_field(&vpif_params->iface);
  562. /* validate the hpitch */
  563. hpitch = pixfmt->bytesperline;
  564. if (hpitch < vpif_params->std_info.width) {
  565. if (!update) {
  566. vpif_dbg(2, debug, "invalid hpitch\n");
  567. goto exit;
  568. }
  569. hpitch = vpif_params->std_info.width;
  570. }
  571. if (V4L2_MEMORY_USERPTR == common->memory)
  572. sizeimage = pixfmt->sizeimage;
  573. else
  574. sizeimage = config_params.channel_bufsize[ch->channel_id];
  575. vpitch = sizeimage / (hpitch * 2);
  576. /* validate the vpitch */
  577. if (vpitch < vpif_params->std_info.height) {
  578. if (!update) {
  579. vpif_dbg(2, debug, "Invalid vpitch\n");
  580. goto exit;
  581. }
  582. vpitch = vpif_params->std_info.height;
  583. }
  584. /* Check for 8 byte alignment */
  585. if (!ALIGN(hpitch, 8)) {
  586. if (!update) {
  587. vpif_dbg(2, debug, "invalid pitch alignment\n");
  588. goto exit;
  589. }
  590. /* adjust to next 8 byte boundary */
  591. hpitch = (((hpitch + 7) / 8) * 8);
  592. }
  593. /* if update is set, modify the bytesperline and sizeimage */
  594. if (update) {
  595. pixfmt->bytesperline = hpitch;
  596. pixfmt->sizeimage = hpitch * vpitch * 2;
  597. }
  598. /**
  599. * Image width and height is always based on current standard width and
  600. * height
  601. */
  602. pixfmt->width = common->fmt.fmt.pix.width;
  603. pixfmt->height = common->fmt.fmt.pix.height;
  604. return 0;
  605. exit:
  606. return ret;
  607. }
  608. /**
  609. * vpif_config_addr() - function to configure buffer address in vpif
  610. * @ch - channel ptr
  611. * @muxmode - channel mux mode
  612. */
  613. static void vpif_config_addr(struct channel_obj *ch, int muxmode)
  614. {
  615. struct common_obj *common;
  616. vpif_dbg(2, debug, "vpif_config_addr\n");
  617. common = &(ch->common[VPIF_VIDEO_INDEX]);
  618. if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
  619. common->set_addr = ch1_set_videobuf_addr;
  620. else if (2 == muxmode)
  621. common->set_addr = ch0_set_videobuf_addr_yc_nmux;
  622. else
  623. common->set_addr = ch0_set_videobuf_addr;
  624. }
  625. /**
  626. * vpfe_mmap : It is used to map kernel space buffers into user spaces
  627. * @filep: file pointer
  628. * @vma: ptr to vm_area_struct
  629. */
  630. static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
  631. {
  632. /* Get the channel object and file handle object */
  633. struct vpif_fh *fh = filep->private_data;
  634. struct channel_obj *ch = fh->channel;
  635. struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
  636. vpif_dbg(2, debug, "vpif_mmap\n");
  637. return videobuf_mmap_mapper(&common->buffer_queue, vma);
  638. }
  639. /**
  640. * vpif_poll: It is used for select/poll system call
  641. * @filep: file pointer
  642. * @wait: poll table to wait
  643. */
  644. static unsigned int vpif_poll(struct file *filep, poll_table * wait)
  645. {
  646. struct vpif_fh *fh = filep->private_data;
  647. struct channel_obj *channel = fh->channel;
  648. struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
  649. vpif_dbg(2, debug, "vpif_poll\n");
  650. if (common->started)
  651. return videobuf_poll_stream(filep, &common->buffer_queue, wait);
  652. return 0;
  653. }
  654. /**
  655. * vpif_open : vpif open handler
  656. * @filep: file ptr
  657. *
  658. * It creates object of file handle structure and stores it in private_data
  659. * member of filepointer
  660. */
  661. static int vpif_open(struct file *filep)
  662. {
  663. struct vpif_capture_config *config = vpif_dev->platform_data;
  664. struct video_device *vdev = video_devdata(filep);
  665. struct common_obj *common;
  666. struct video_obj *vid_ch;
  667. struct channel_obj *ch;
  668. struct vpif_fh *fh;
  669. int i, ret = 0;
  670. vpif_dbg(2, debug, "vpif_open\n");
  671. ch = video_get_drvdata(vdev);
  672. vid_ch = &ch->video;
  673. common = &ch->common[VPIF_VIDEO_INDEX];
  674. if (mutex_lock_interruptible(&common->lock))
  675. return -ERESTARTSYS;
  676. if (NULL == ch->curr_subdev_info) {
  677. /**
  678. * search through the sub device to see a registered
  679. * sub device and make it as current sub device
  680. */
  681. for (i = 0; i < config->subdev_count; i++) {
  682. if (vpif_obj.sd[i]) {
  683. /* the sub device is registered */
  684. ch->curr_subdev_info = &config->subdev_info[i];
  685. /* make first input as the current input */
  686. vid_ch->input_idx = 0;
  687. break;
  688. }
  689. }
  690. if (i == config->subdev_count) {
  691. vpif_err("No sub device registered\n");
  692. ret = -ENOENT;
  693. goto exit;
  694. }
  695. }
  696. /* Allocate memory for the file handle object */
  697. fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
  698. if (NULL == fh) {
  699. vpif_err("unable to allocate memory for file handle object\n");
  700. ret = -ENOMEM;
  701. goto exit;
  702. }
  703. /* store pointer to fh in private_data member of filep */
  704. filep->private_data = fh;
  705. fh->channel = ch;
  706. fh->initialized = 0;
  707. /* If decoder is not initialized. initialize it */
  708. if (!ch->initialized) {
  709. fh->initialized = 1;
  710. ch->initialized = 1;
  711. memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
  712. }
  713. /* Increment channel usrs counter */
  714. ch->usrs++;
  715. /* Set io_allowed member to false */
  716. fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
  717. /* Initialize priority of this instance to default priority */
  718. fh->prio = V4L2_PRIORITY_UNSET;
  719. v4l2_prio_open(&ch->prio, &fh->prio);
  720. exit:
  721. mutex_unlock(&common->lock);
  722. return ret;
  723. }
  724. /**
  725. * vpif_release : function to clean up file close
  726. * @filep: file pointer
  727. *
  728. * This function deletes buffer queue, frees the buffers and the vpfe file
  729. * handle
  730. */
  731. static int vpif_release(struct file *filep)
  732. {
  733. struct vpif_fh *fh = filep->private_data;
  734. struct channel_obj *ch = fh->channel;
  735. struct common_obj *common;
  736. vpif_dbg(2, debug, "vpif_release\n");
  737. common = &ch->common[VPIF_VIDEO_INDEX];
  738. if (mutex_lock_interruptible(&common->lock))
  739. return -ERESTARTSYS;
  740. /* if this instance is doing IO */
  741. if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
  742. /* Reset io_usrs member of channel object */
  743. common->io_usrs = 0;
  744. /* Disable channel as per its device type and channel id */
  745. if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
  746. enable_channel0(0);
  747. channel0_intr_enable(0);
  748. }
  749. if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
  750. (2 == common->started)) {
  751. enable_channel1(0);
  752. channel1_intr_enable(0);
  753. }
  754. common->started = 0;
  755. /* Free buffers allocated */
  756. videobuf_queue_cancel(&common->buffer_queue);
  757. videobuf_mmap_free(&common->buffer_queue);
  758. }
  759. /* Decrement channel usrs counter */
  760. ch->usrs--;
  761. /* unlock mutex on channel object */
  762. mutex_unlock(&common->lock);
  763. /* Close the priority */
  764. v4l2_prio_close(&ch->prio, fh->prio);
  765. if (fh->initialized)
  766. ch->initialized = 0;
  767. filep->private_data = NULL;
  768. kfree(fh);
  769. return 0;
  770. }
  771. /**
  772. * vpif_reqbufs() - request buffer handler
  773. * @file: file ptr
  774. * @priv: file handle
  775. * @reqbuf: request buffer structure ptr
  776. */
  777. static int vpif_reqbufs(struct file *file, void *priv,
  778. struct v4l2_requestbuffers *reqbuf)
  779. {
  780. struct vpif_fh *fh = priv;
  781. struct channel_obj *ch = fh->channel;
  782. struct common_obj *common;
  783. u8 index = 0;
  784. int ret = 0;
  785. vpif_dbg(2, debug, "vpif_reqbufs\n");
  786. /**
  787. * This file handle has not initialized the channel,
  788. * It is not allowed to do settings
  789. */
  790. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
  791. || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  792. if (!fh->initialized) {
  793. vpif_dbg(1, debug, "Channel Busy\n");
  794. return -EBUSY;
  795. }
  796. }
  797. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type)
  798. return -EINVAL;
  799. index = VPIF_VIDEO_INDEX;
  800. common = &ch->common[index];
  801. if (mutex_lock_interruptible(&common->lock))
  802. return -ERESTARTSYS;
  803. if (0 != common->io_usrs) {
  804. ret = -EBUSY;
  805. goto reqbuf_exit;
  806. }
  807. /* Initialize videobuf queue as per the buffer type */
  808. videobuf_queue_dma_contig_init(&common->buffer_queue,
  809. &video_qops, NULL,
  810. &common->irqlock,
  811. reqbuf->type,
  812. common->fmt.fmt.pix.field,
  813. sizeof(struct videobuf_buffer), fh,
  814. NULL);
  815. /* Set io allowed member of file handle to TRUE */
  816. fh->io_allowed[index] = 1;
  817. /* Increment io usrs member of channel object to 1 */
  818. common->io_usrs = 1;
  819. /* Store type of memory requested in channel object */
  820. common->memory = reqbuf->memory;
  821. INIT_LIST_HEAD(&common->dma_queue);
  822. /* Allocate buffers */
  823. ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
  824. reqbuf_exit:
  825. mutex_unlock(&common->lock);
  826. return ret;
  827. }
  828. /**
  829. * vpif_querybuf() - query buffer handler
  830. * @file: file ptr
  831. * @priv: file handle
  832. * @buf: v4l2 buffer structure ptr
  833. */
  834. static int vpif_querybuf(struct file *file, void *priv,
  835. struct v4l2_buffer *buf)
  836. {
  837. struct vpif_fh *fh = priv;
  838. struct channel_obj *ch = fh->channel;
  839. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  840. vpif_dbg(2, debug, "vpif_querybuf\n");
  841. if (common->fmt.type != buf->type)
  842. return -EINVAL;
  843. if (common->memory != V4L2_MEMORY_MMAP) {
  844. vpif_dbg(1, debug, "Invalid memory\n");
  845. return -EINVAL;
  846. }
  847. return videobuf_querybuf(&common->buffer_queue, buf);
  848. }
  849. /**
  850. * vpif_qbuf() - query buffer handler
  851. * @file: file ptr
  852. * @priv: file handle
  853. * @buf: v4l2 buffer structure ptr
  854. */
  855. static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  856. {
  857. struct vpif_fh *fh = priv;
  858. struct channel_obj *ch = fh->channel;
  859. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  860. struct v4l2_buffer tbuf = *buf;
  861. struct videobuf_buffer *buf1;
  862. unsigned long addr = 0;
  863. unsigned long flags;
  864. int ret = 0;
  865. vpif_dbg(2, debug, "vpif_qbuf\n");
  866. if (common->fmt.type != tbuf.type) {
  867. vpif_err("invalid buffer type\n");
  868. return -EINVAL;
  869. }
  870. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  871. vpif_err("fh io not allowed \n");
  872. return -EACCES;
  873. }
  874. if (!(list_empty(&common->dma_queue)) ||
  875. (common->cur_frm != common->next_frm) ||
  876. !common->started ||
  877. (common->started && (0 == ch->field_id)))
  878. return videobuf_qbuf(&common->buffer_queue, buf);
  879. /* bufferqueue is empty store buffer address in VPIF registers */
  880. mutex_lock(&common->buffer_queue.vb_lock);
  881. buf1 = common->buffer_queue.bufs[tbuf.index];
  882. if ((buf1->state == VIDEOBUF_QUEUED) ||
  883. (buf1->state == VIDEOBUF_ACTIVE)) {
  884. vpif_err("invalid state\n");
  885. goto qbuf_exit;
  886. }
  887. switch (buf1->memory) {
  888. case V4L2_MEMORY_MMAP:
  889. if (buf1->baddr == 0)
  890. goto qbuf_exit;
  891. break;
  892. case V4L2_MEMORY_USERPTR:
  893. if (tbuf.length < buf1->bsize)
  894. goto qbuf_exit;
  895. if ((VIDEOBUF_NEEDS_INIT != buf1->state)
  896. && (buf1->baddr != tbuf.m.userptr)) {
  897. vpif_buffer_release(&common->buffer_queue, buf1);
  898. buf1->baddr = tbuf.m.userptr;
  899. }
  900. break;
  901. default:
  902. goto qbuf_exit;
  903. }
  904. local_irq_save(flags);
  905. ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
  906. common->buffer_queue.field);
  907. if (ret < 0) {
  908. local_irq_restore(flags);
  909. goto qbuf_exit;
  910. }
  911. buf1->state = VIDEOBUF_ACTIVE;
  912. if (V4L2_MEMORY_USERPTR == common->memory)
  913. addr = buf1->boff;
  914. else
  915. addr = videobuf_to_dma_contig(buf1);
  916. common->next_frm = buf1;
  917. common->set_addr(addr + common->ytop_off,
  918. addr + common->ybtm_off,
  919. addr + common->ctop_off,
  920. addr + common->cbtm_off);
  921. local_irq_restore(flags);
  922. list_add_tail(&buf1->stream, &common->buffer_queue.stream);
  923. mutex_unlock(&common->buffer_queue.vb_lock);
  924. return 0;
  925. qbuf_exit:
  926. mutex_unlock(&common->buffer_queue.vb_lock);
  927. return -EINVAL;
  928. }
  929. /**
  930. * vpif_dqbuf() - query buffer handler
  931. * @file: file ptr
  932. * @priv: file handle
  933. * @buf: v4l2 buffer structure ptr
  934. */
  935. static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  936. {
  937. struct vpif_fh *fh = priv;
  938. struct channel_obj *ch = fh->channel;
  939. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  940. vpif_dbg(2, debug, "vpif_dqbuf\n");
  941. return videobuf_dqbuf(&common->buffer_queue, buf,
  942. file->f_flags & O_NONBLOCK);
  943. }
  944. /**
  945. * vpif_streamon() - streamon handler
  946. * @file: file ptr
  947. * @priv: file handle
  948. * @buftype: v4l2 buffer type
  949. */
  950. static int vpif_streamon(struct file *file, void *priv,
  951. enum v4l2_buf_type buftype)
  952. {
  953. struct vpif_capture_config *config = vpif_dev->platform_data;
  954. struct vpif_fh *fh = priv;
  955. struct channel_obj *ch = fh->channel;
  956. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  957. struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
  958. struct vpif_params *vpif;
  959. unsigned long addr = 0;
  960. int ret = 0;
  961. vpif_dbg(2, debug, "vpif_streamon\n");
  962. vpif = &ch->vpifparams;
  963. if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  964. vpif_dbg(1, debug, "buffer type not supported\n");
  965. return -EINVAL;
  966. }
  967. /* If file handle is not allowed IO, return error */
  968. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  969. vpif_dbg(1, debug, "io not allowed\n");
  970. return -EACCES;
  971. }
  972. /* If Streaming is already started, return error */
  973. if (common->started) {
  974. vpif_dbg(1, debug, "channel->started\n");
  975. return -EBUSY;
  976. }
  977. if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
  978. oth_ch->common[VPIF_VIDEO_INDEX].started &&
  979. vpif->std_info.ycmux_mode == 0) ||
  980. ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
  981. (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
  982. vpif_dbg(1, debug, "other channel is being used\n");
  983. return -EBUSY;
  984. }
  985. ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
  986. if (ret)
  987. return ret;
  988. /* Enable streamon on the sub device */
  989. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
  990. s_stream, 1);
  991. if (ret && (ret != -ENOIOCTLCMD)) {
  992. vpif_dbg(1, debug, "stream on failed in subdev\n");
  993. return ret;
  994. }
  995. /* Call videobuf_streamon to start streaming in videobuf */
  996. ret = videobuf_streamon(&common->buffer_queue);
  997. if (ret) {
  998. vpif_dbg(1, debug, "videobuf_streamon\n");
  999. return ret;
  1000. }
  1001. if (mutex_lock_interruptible(&common->lock)) {
  1002. ret = -ERESTARTSYS;
  1003. goto streamoff_exit;
  1004. }
  1005. /* If buffer queue is empty, return error */
  1006. if (list_empty(&common->dma_queue)) {
  1007. vpif_dbg(1, debug, "buffer queue is empty\n");
  1008. ret = -EIO;
  1009. goto exit;
  1010. }
  1011. /* Get the next frame from the buffer queue */
  1012. common->cur_frm = list_entry(common->dma_queue.next,
  1013. struct videobuf_buffer, queue);
  1014. common->next_frm = common->cur_frm;
  1015. /* Remove buffer from the buffer queue */
  1016. list_del(&common->cur_frm->queue);
  1017. /* Mark state of the current frame to active */
  1018. common->cur_frm->state = VIDEOBUF_ACTIVE;
  1019. /* Initialize field_id and started member */
  1020. ch->field_id = 0;
  1021. common->started = 1;
  1022. if (V4L2_MEMORY_USERPTR == common->memory)
  1023. addr = common->cur_frm->boff;
  1024. else
  1025. addr = videobuf_to_dma_contig(common->cur_frm);
  1026. /* Calculate the offset for Y and C data in the buffer */
  1027. vpif_calculate_offsets(ch);
  1028. if ((vpif->std_info.frm_fmt &&
  1029. ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
  1030. (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
  1031. (!vpif->std_info.frm_fmt &&
  1032. (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
  1033. vpif_dbg(1, debug, "conflict in field format and std format\n");
  1034. ret = -EINVAL;
  1035. goto exit;
  1036. }
  1037. /* configure 1 or 2 channel mode */
  1038. ret = config->setup_input_channel_mode(vpif->std_info.ycmux_mode);
  1039. if (ret < 0) {
  1040. vpif_dbg(1, debug, "can't set vpif channel mode\n");
  1041. goto exit;
  1042. }
  1043. /* Call vpif_set_params function to set the parameters and addresses */
  1044. ret = vpif_set_video_params(vpif, ch->channel_id);
  1045. if (ret < 0) {
  1046. vpif_dbg(1, debug, "can't set video params\n");
  1047. goto exit;
  1048. }
  1049. common->started = ret;
  1050. vpif_config_addr(ch, ret);
  1051. common->set_addr(addr + common->ytop_off,
  1052. addr + common->ybtm_off,
  1053. addr + common->ctop_off,
  1054. addr + common->cbtm_off);
  1055. /**
  1056. * Set interrupt for both the fields in VPIF Register enable channel in
  1057. * VPIF register
  1058. */
  1059. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
  1060. channel0_intr_assert();
  1061. channel0_intr_enable(1);
  1062. enable_channel0(1);
  1063. }
  1064. if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
  1065. (common->started == 2)) {
  1066. channel1_intr_assert();
  1067. channel1_intr_enable(1);
  1068. enable_channel1(1);
  1069. }
  1070. channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
  1071. mutex_unlock(&common->lock);
  1072. return ret;
  1073. exit:
  1074. mutex_unlock(&common->lock);
  1075. streamoff_exit:
  1076. ret = videobuf_streamoff(&common->buffer_queue);
  1077. return ret;
  1078. }
  1079. /**
  1080. * vpif_streamoff() - streamoff handler
  1081. * @file: file ptr
  1082. * @priv: file handle
  1083. * @buftype: v4l2 buffer type
  1084. */
  1085. static int vpif_streamoff(struct file *file, void *priv,
  1086. enum v4l2_buf_type buftype)
  1087. {
  1088. struct vpif_fh *fh = priv;
  1089. struct channel_obj *ch = fh->channel;
  1090. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1091. int ret;
  1092. vpif_dbg(2, debug, "vpif_streamoff\n");
  1093. if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1094. vpif_dbg(1, debug, "buffer type not supported\n");
  1095. return -EINVAL;
  1096. }
  1097. /* If io is allowed for this file handle, return error */
  1098. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  1099. vpif_dbg(1, debug, "io not allowed\n");
  1100. return -EACCES;
  1101. }
  1102. /* If streaming is not started, return error */
  1103. if (!common->started) {
  1104. vpif_dbg(1, debug, "channel->started\n");
  1105. return -EINVAL;
  1106. }
  1107. if (mutex_lock_interruptible(&common->lock))
  1108. return -ERESTARTSYS;
  1109. /* disable channel */
  1110. if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
  1111. enable_channel0(0);
  1112. channel0_intr_enable(0);
  1113. } else {
  1114. enable_channel1(0);
  1115. channel1_intr_enable(0);
  1116. }
  1117. common->started = 0;
  1118. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
  1119. s_stream, 0);
  1120. if (ret && (ret != -ENOIOCTLCMD))
  1121. vpif_dbg(1, debug, "stream off failed in subdev\n");
  1122. mutex_unlock(&common->lock);
  1123. return videobuf_streamoff(&common->buffer_queue);
  1124. }
  1125. /**
  1126. * vpif_map_sub_device_to_input() - Maps sub device to input
  1127. * @ch - ptr to channel
  1128. * @config - ptr to capture configuration
  1129. * @input_index - Given input index from application
  1130. * @sub_device_index - index into sd table
  1131. *
  1132. * lookup the sub device information for a given input index.
  1133. * we report all the inputs to application. inputs table also
  1134. * has sub device name for the each input
  1135. */
  1136. static struct vpif_subdev_info *vpif_map_sub_device_to_input(
  1137. struct channel_obj *ch,
  1138. struct vpif_capture_config *vpif_cfg,
  1139. int input_index,
  1140. int *sub_device_index)
  1141. {
  1142. struct vpif_capture_chan_config *chan_cfg;
  1143. struct vpif_subdev_info *subdev_info = NULL;
  1144. const char *subdev_name = NULL;
  1145. int i;
  1146. vpif_dbg(2, debug, "vpif_map_sub_device_to_input\n");
  1147. chan_cfg = &vpif_cfg->chan_config[ch->channel_id];
  1148. /**
  1149. * search through the inputs to find the sub device supporting
  1150. * the input
  1151. */
  1152. for (i = 0; i < chan_cfg->input_count; i++) {
  1153. /* For each sub device, loop through input */
  1154. if (i == input_index) {
  1155. subdev_name = chan_cfg->inputs[i].subdev_name;
  1156. break;
  1157. }
  1158. }
  1159. /* if reached maximum. return null */
  1160. if (i == chan_cfg->input_count || (NULL == subdev_name))
  1161. return subdev_info;
  1162. /* loop through the sub device list to get the sub device info */
  1163. for (i = 0; i < vpif_cfg->subdev_count; i++) {
  1164. subdev_info = &vpif_cfg->subdev_info[i];
  1165. if (!strcmp(subdev_info->name, subdev_name))
  1166. break;
  1167. }
  1168. if (i == vpif_cfg->subdev_count)
  1169. return subdev_info;
  1170. /* check if the sub device is registered */
  1171. if (NULL == vpif_obj.sd[i])
  1172. return NULL;
  1173. *sub_device_index = i;
  1174. return subdev_info;
  1175. }
  1176. /**
  1177. * vpif_querystd() - querystd handler
  1178. * @file: file ptr
  1179. * @priv: file handle
  1180. * @std_id: ptr to std id
  1181. *
  1182. * This function is called to detect standard at the selected input
  1183. */
  1184. static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
  1185. {
  1186. struct vpif_fh *fh = priv;
  1187. struct channel_obj *ch = fh->channel;
  1188. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1189. int ret = 0;
  1190. vpif_dbg(2, debug, "vpif_querystd\n");
  1191. if (mutex_lock_interruptible(&common->lock))
  1192. return -ERESTARTSYS;
  1193. /* Call querystd function of decoder device */
  1194. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
  1195. querystd, std_id);
  1196. if (ret < 0)
  1197. vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
  1198. mutex_unlock(&common->lock);
  1199. return ret;
  1200. }
  1201. /**
  1202. * vpif_g_std() - get STD handler
  1203. * @file: file ptr
  1204. * @priv: file handle
  1205. * @std_id: ptr to std id
  1206. */
  1207. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  1208. {
  1209. struct vpif_fh *fh = priv;
  1210. struct channel_obj *ch = fh->channel;
  1211. vpif_dbg(2, debug, "vpif_g_std\n");
  1212. *std = ch->video.stdid;
  1213. return 0;
  1214. }
  1215. /**
  1216. * vpif_s_std() - set STD handler
  1217. * @file: file ptr
  1218. * @priv: file handle
  1219. * @std_id: ptr to std id
  1220. */
  1221. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  1222. {
  1223. struct vpif_fh *fh = priv;
  1224. struct channel_obj *ch = fh->channel;
  1225. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1226. int ret = 0;
  1227. vpif_dbg(2, debug, "vpif_s_std\n");
  1228. if (common->started) {
  1229. vpif_err("streaming in progress\n");
  1230. return -EBUSY;
  1231. }
  1232. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1233. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1234. if (!fh->initialized) {
  1235. vpif_dbg(1, debug, "Channel Busy\n");
  1236. return -EBUSY;
  1237. }
  1238. }
  1239. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1240. if (0 != ret)
  1241. return ret;
  1242. fh->initialized = 1;
  1243. /* Call encoder subdevice function to set the standard */
  1244. if (mutex_lock_interruptible(&common->lock))
  1245. return -ERESTARTSYS;
  1246. ch->video.stdid = *std_id;
  1247. /* Get the information about the standard */
  1248. if (vpif_update_std_info(ch)) {
  1249. ret = -EINVAL;
  1250. vpif_err("Error getting the standard info\n");
  1251. goto s_std_exit;
  1252. }
  1253. /* Configure the default format information */
  1254. vpif_config_format(ch);
  1255. /* set standard in the sub device */
  1256. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
  1257. s_std, *std_id);
  1258. if (ret < 0)
  1259. vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
  1260. s_std_exit:
  1261. mutex_unlock(&common->lock);
  1262. return ret;
  1263. }
  1264. /**
  1265. * vpif_enum_input() - ENUMINPUT handler
  1266. * @file: file ptr
  1267. * @priv: file handle
  1268. * @input: ptr to input structure
  1269. */
  1270. static int vpif_enum_input(struct file *file, void *priv,
  1271. struct v4l2_input *input)
  1272. {
  1273. struct vpif_capture_config *config = vpif_dev->platform_data;
  1274. struct vpif_capture_chan_config *chan_cfg;
  1275. struct vpif_fh *fh = priv;
  1276. struct channel_obj *ch = fh->channel;
  1277. chan_cfg = &config->chan_config[ch->channel_id];
  1278. if (input->index >= chan_cfg->input_count) {
  1279. vpif_dbg(1, debug, "Invalid input index\n");
  1280. return -EINVAL;
  1281. }
  1282. memcpy(input, &chan_cfg->inputs[input->index].input,
  1283. sizeof(*input));
  1284. return 0;
  1285. }
  1286. /**
  1287. * vpif_g_input() - Get INPUT handler
  1288. * @file: file ptr
  1289. * @priv: file handle
  1290. * @index: ptr to input index
  1291. */
  1292. static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
  1293. {
  1294. struct vpif_fh *fh = priv;
  1295. struct channel_obj *ch = fh->channel;
  1296. struct video_obj *vid_ch = &ch->video;
  1297. *index = vid_ch->input_idx;
  1298. return 0;
  1299. }
  1300. /**
  1301. * vpif_s_input() - Set INPUT handler
  1302. * @file: file ptr
  1303. * @priv: file handle
  1304. * @index: input index
  1305. */
  1306. static int vpif_s_input(struct file *file, void *priv, unsigned int index)
  1307. {
  1308. struct vpif_capture_config *config = vpif_dev->platform_data;
  1309. struct vpif_capture_chan_config *chan_cfg;
  1310. struct vpif_fh *fh = priv;
  1311. struct channel_obj *ch = fh->channel;
  1312. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1313. struct video_obj *vid_ch = &ch->video;
  1314. struct vpif_subdev_info *subdev_info;
  1315. int ret = 0, sd_index = 0;
  1316. u32 input = 0, output = 0;
  1317. chan_cfg = &config->chan_config[ch->channel_id];
  1318. if (common->started) {
  1319. vpif_err("Streaming in progress\n");
  1320. return -EBUSY;
  1321. }
  1322. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1323. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1324. if (!fh->initialized) {
  1325. vpif_dbg(1, debug, "Channel Busy\n");
  1326. return -EBUSY;
  1327. }
  1328. }
  1329. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1330. if (0 != ret)
  1331. return ret;
  1332. fh->initialized = 1;
  1333. subdev_info = vpif_map_sub_device_to_input(ch, config, index,
  1334. &sd_index);
  1335. if (NULL == subdev_info) {
  1336. vpif_dbg(1, debug,
  1337. "couldn't lookup sub device for the input index\n");
  1338. return -EINVAL;
  1339. }
  1340. if (mutex_lock_interruptible(&common->lock))
  1341. return -ERESTARTSYS;
  1342. /* first setup input path from sub device to vpif */
  1343. if (config->setup_input_path) {
  1344. ret = config->setup_input_path(ch->channel_id,
  1345. subdev_info->name);
  1346. if (ret < 0) {
  1347. vpif_dbg(1, debug, "couldn't setup input path for the"
  1348. " sub device %s, for input index %d\n",
  1349. subdev_info->name, index);
  1350. goto exit;
  1351. }
  1352. }
  1353. if (subdev_info->can_route) {
  1354. input = subdev_info->input;
  1355. output = subdev_info->output;
  1356. ret = v4l2_subdev_call(vpif_obj.sd[sd_index], video, s_routing,
  1357. input, output, 0);
  1358. if (ret < 0) {
  1359. vpif_dbg(1, debug, "Failed to set input\n");
  1360. goto exit;
  1361. }
  1362. }
  1363. vid_ch->input_idx = index;
  1364. ch->curr_subdev_info = subdev_info;
  1365. ch->curr_sd_index = sd_index;
  1366. /* copy interface parameters to vpif */
  1367. ch->vpifparams.iface = subdev_info->vpif_if;
  1368. /* update tvnorms from the sub device input info */
  1369. ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
  1370. exit:
  1371. mutex_unlock(&common->lock);
  1372. return ret;
  1373. }
  1374. /**
  1375. * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
  1376. * @file: file ptr
  1377. * @priv: file handle
  1378. * @index: input index
  1379. */
  1380. static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
  1381. struct v4l2_fmtdesc *fmt)
  1382. {
  1383. struct vpif_fh *fh = priv;
  1384. struct channel_obj *ch = fh->channel;
  1385. if (fmt->index != 0) {
  1386. vpif_dbg(1, debug, "Invalid format index\n");
  1387. return -EINVAL;
  1388. }
  1389. /* Fill in the information about format */
  1390. if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
  1391. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1392. strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
  1393. fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
  1394. } else {
  1395. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1396. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  1397. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  1398. }
  1399. return 0;
  1400. }
  1401. /**
  1402. * vpif_try_fmt_vid_cap() - TRY_FMT handler
  1403. * @file: file ptr
  1404. * @priv: file handle
  1405. * @fmt: ptr to v4l2 format structure
  1406. */
  1407. static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
  1408. struct v4l2_format *fmt)
  1409. {
  1410. struct vpif_fh *fh = priv;
  1411. struct channel_obj *ch = fh->channel;
  1412. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  1413. return vpif_check_format(ch, pixfmt, 1);
  1414. }
  1415. /**
  1416. * vpif_g_fmt_vid_cap() - Set INPUT handler
  1417. * @file: file ptr
  1418. * @priv: file handle
  1419. * @fmt: ptr to v4l2 format structure
  1420. */
  1421. static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
  1422. struct v4l2_format *fmt)
  1423. {
  1424. struct vpif_fh *fh = priv;
  1425. struct channel_obj *ch = fh->channel;
  1426. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1427. /* Check the validity of the buffer type */
  1428. if (common->fmt.type != fmt->type)
  1429. return -EINVAL;
  1430. /* Fill in the information about format */
  1431. if (mutex_lock_interruptible(&common->lock))
  1432. return -ERESTARTSYS;
  1433. *fmt = common->fmt;
  1434. mutex_unlock(&common->lock);
  1435. return 0;
  1436. }
  1437. /**
  1438. * vpif_s_fmt_vid_cap() - Set FMT handler
  1439. * @file: file ptr
  1440. * @priv: file handle
  1441. * @fmt: ptr to v4l2 format structure
  1442. */
  1443. static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
  1444. struct v4l2_format *fmt)
  1445. {
  1446. struct vpif_fh *fh = priv;
  1447. struct channel_obj *ch = fh->channel;
  1448. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1449. struct v4l2_pix_format *pixfmt;
  1450. int ret = 0;
  1451. vpif_dbg(2, debug, "VIDIOC_S_FMT\n");
  1452. /* If streaming is started, return error */
  1453. if (common->started) {
  1454. vpif_dbg(1, debug, "Streaming is started\n");
  1455. return -EBUSY;
  1456. }
  1457. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1458. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1459. if (!fh->initialized) {
  1460. vpif_dbg(1, debug, "Channel Busy\n");
  1461. return -EBUSY;
  1462. }
  1463. }
  1464. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1465. if (0 != ret)
  1466. return ret;
  1467. fh->initialized = 1;
  1468. pixfmt = &fmt->fmt.pix;
  1469. /* Check for valid field format */
  1470. ret = vpif_check_format(ch, pixfmt, 0);
  1471. if (ret)
  1472. return ret;
  1473. /* store the format in the channel object */
  1474. if (mutex_lock_interruptible(&common->lock))
  1475. return -ERESTARTSYS;
  1476. common->fmt = *fmt;
  1477. mutex_unlock(&common->lock);
  1478. return 0;
  1479. }
  1480. /**
  1481. * vpif_querycap() - QUERYCAP handler
  1482. * @file: file ptr
  1483. * @priv: file handle
  1484. * @cap: ptr to v4l2_capability structure
  1485. */
  1486. static int vpif_querycap(struct file *file, void *priv,
  1487. struct v4l2_capability *cap)
  1488. {
  1489. struct vpif_capture_config *config = vpif_dev->platform_data;
  1490. cap->version = VPIF_CAPTURE_VERSION_CODE;
  1491. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1492. strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
  1493. strlcpy(cap->bus_info, "DM646x Platform", sizeof(cap->bus_info));
  1494. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  1495. return 0;
  1496. }
  1497. /**
  1498. * vpif_g_priority() - get priority handler
  1499. * @file: file ptr
  1500. * @priv: file handle
  1501. * @prio: ptr to v4l2_priority structure
  1502. */
  1503. static int vpif_g_priority(struct file *file, void *priv,
  1504. enum v4l2_priority *prio)
  1505. {
  1506. struct vpif_fh *fh = priv;
  1507. struct channel_obj *ch = fh->channel;
  1508. *prio = v4l2_prio_max(&ch->prio);
  1509. return 0;
  1510. }
  1511. /**
  1512. * vpif_s_priority() - set priority handler
  1513. * @file: file ptr
  1514. * @priv: file handle
  1515. * @prio: ptr to v4l2_priority structure
  1516. */
  1517. static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
  1518. {
  1519. struct vpif_fh *fh = priv;
  1520. struct channel_obj *ch = fh->channel;
  1521. return v4l2_prio_change(&ch->prio, &fh->prio, p);
  1522. }
  1523. /**
  1524. * vpif_cropcap() - cropcap handler
  1525. * @file: file ptr
  1526. * @priv: file handle
  1527. * @crop: ptr to v4l2_cropcap structure
  1528. */
  1529. static int vpif_cropcap(struct file *file, void *priv,
  1530. struct v4l2_cropcap *crop)
  1531. {
  1532. struct vpif_fh *fh = priv;
  1533. struct channel_obj *ch = fh->channel;
  1534. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1535. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
  1536. return -EINVAL;
  1537. crop->bounds.left = 0;
  1538. crop->bounds.top = 0;
  1539. crop->bounds.height = common->height;
  1540. crop->bounds.width = common->width;
  1541. crop->defrect = crop->bounds;
  1542. return 0;
  1543. }
  1544. /*
  1545. * vpif_g_chip_ident() - Identify the chip
  1546. * @file: file ptr
  1547. * @priv: file handle
  1548. * @chip: chip identity
  1549. *
  1550. * Returns zero or -EINVAL if read operations fails.
  1551. */
  1552. static int vpif_g_chip_ident(struct file *file, void *priv,
  1553. struct v4l2_dbg_chip_ident *chip)
  1554. {
  1555. chip->ident = V4L2_IDENT_NONE;
  1556. chip->revision = 0;
  1557. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  1558. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
  1559. vpif_dbg(2, debug, "match_type is invalid.\n");
  1560. return -EINVAL;
  1561. }
  1562. return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
  1563. g_chip_ident, chip);
  1564. }
  1565. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1566. /*
  1567. * vpif_dbg_g_register() - Read register
  1568. * @file: file ptr
  1569. * @priv: file handle
  1570. * @reg: register to be read
  1571. *
  1572. * Debugging only
  1573. * Returns zero or -EINVAL if read operations fails.
  1574. */
  1575. static int vpif_dbg_g_register(struct file *file, void *priv,
  1576. struct v4l2_dbg_register *reg){
  1577. struct vpif_fh *fh = priv;
  1578. struct channel_obj *ch = fh->channel;
  1579. return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
  1580. g_register, reg);
  1581. }
  1582. /*
  1583. * vpif_dbg_s_register() - Write to register
  1584. * @file: file ptr
  1585. * @priv: file handle
  1586. * @reg: register to be modified
  1587. *
  1588. * Debugging only
  1589. * Returns zero or -EINVAL if write operations fails.
  1590. */
  1591. static int vpif_dbg_s_register(struct file *file, void *priv,
  1592. struct v4l2_dbg_register *reg){
  1593. struct vpif_fh *fh = priv;
  1594. struct channel_obj *ch = fh->channel;
  1595. return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
  1596. s_register, reg);
  1597. }
  1598. #endif
  1599. /*
  1600. * vpif_log_status() - Status information
  1601. * @file: file ptr
  1602. * @priv: file handle
  1603. *
  1604. * Returns zero.
  1605. */
  1606. static int vpif_log_status(struct file *filep, void *priv)
  1607. {
  1608. /* status for sub devices */
  1609. v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
  1610. return 0;
  1611. }
  1612. /* vpif capture ioctl operations */
  1613. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1614. .vidioc_querycap = vpif_querycap,
  1615. .vidioc_g_priority = vpif_g_priority,
  1616. .vidioc_s_priority = vpif_s_priority,
  1617. .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
  1618. .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
  1619. .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
  1620. .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
  1621. .vidioc_enum_input = vpif_enum_input,
  1622. .vidioc_s_input = vpif_s_input,
  1623. .vidioc_g_input = vpif_g_input,
  1624. .vidioc_reqbufs = vpif_reqbufs,
  1625. .vidioc_querybuf = vpif_querybuf,
  1626. .vidioc_querystd = vpif_querystd,
  1627. .vidioc_s_std = vpif_s_std,
  1628. .vidioc_g_std = vpif_g_std,
  1629. .vidioc_qbuf = vpif_qbuf,
  1630. .vidioc_dqbuf = vpif_dqbuf,
  1631. .vidioc_streamon = vpif_streamon,
  1632. .vidioc_streamoff = vpif_streamoff,
  1633. .vidioc_cropcap = vpif_cropcap,
  1634. .vidioc_g_chip_ident = vpif_g_chip_ident,
  1635. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1636. .vidioc_g_register = vpif_dbg_g_register,
  1637. .vidioc_s_register = vpif_dbg_s_register,
  1638. #endif
  1639. .vidioc_log_status = vpif_log_status,
  1640. };
  1641. /* vpif file operations */
  1642. static struct v4l2_file_operations vpif_fops = {
  1643. .owner = THIS_MODULE,
  1644. .open = vpif_open,
  1645. .release = vpif_release,
  1646. .ioctl = video_ioctl2,
  1647. .mmap = vpif_mmap,
  1648. .poll = vpif_poll
  1649. };
  1650. /* vpif video template */
  1651. static struct video_device vpif_video_template = {
  1652. .name = "vpif",
  1653. .fops = &vpif_fops,
  1654. .minor = -1,
  1655. .ioctl_ops = &vpif_ioctl_ops,
  1656. };
  1657. /**
  1658. * initialize_vpif() - Initialize vpif data structures
  1659. *
  1660. * Allocate memory for data structures and initialize them
  1661. */
  1662. static int initialize_vpif(void)
  1663. {
  1664. int err = 0, i, j;
  1665. int free_channel_objects_index;
  1666. /* Default number of buffers should be 3 */
  1667. if ((ch0_numbuffers > 0) &&
  1668. (ch0_numbuffers < config_params.min_numbuffers))
  1669. ch0_numbuffers = config_params.min_numbuffers;
  1670. if ((ch1_numbuffers > 0) &&
  1671. (ch1_numbuffers < config_params.min_numbuffers))
  1672. ch1_numbuffers = config_params.min_numbuffers;
  1673. /* Set buffer size to min buffers size if it is invalid */
  1674. if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
  1675. ch0_bufsize =
  1676. config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
  1677. if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
  1678. ch1_bufsize =
  1679. config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
  1680. config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
  1681. config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
  1682. if (ch0_numbuffers) {
  1683. config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
  1684. = ch0_bufsize;
  1685. }
  1686. if (ch1_numbuffers) {
  1687. config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
  1688. = ch1_bufsize;
  1689. }
  1690. /* Allocate memory for six channel objects */
  1691. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1692. vpif_obj.dev[i] =
  1693. kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
  1694. /* If memory allocation fails, return error */
  1695. if (!vpif_obj.dev[i]) {
  1696. free_channel_objects_index = i;
  1697. err = -ENOMEM;
  1698. goto vpif_init_free_channel_objects;
  1699. }
  1700. }
  1701. return 0;
  1702. vpif_init_free_channel_objects:
  1703. for (j = 0; j < free_channel_objects_index; j++)
  1704. kfree(vpif_obj.dev[j]);
  1705. return err;
  1706. }
  1707. /**
  1708. * vpif_probe : This function probes the vpif capture driver
  1709. * @pdev: platform device pointer
  1710. *
  1711. * This creates device entries by register itself to the V4L2 driver and
  1712. * initializes fields of each channel objects
  1713. */
  1714. static __init int vpif_probe(struct platform_device *pdev)
  1715. {
  1716. struct vpif_subdev_info *subdevdata;
  1717. struct vpif_capture_config *config;
  1718. int i, j, k, m, q, err;
  1719. struct i2c_adapter *i2c_adap;
  1720. struct channel_obj *ch;
  1721. struct common_obj *common;
  1722. struct video_device *vfd;
  1723. struct resource *res;
  1724. int subdev_count;
  1725. vpif_dev = &pdev->dev;
  1726. err = initialize_vpif();
  1727. if (err) {
  1728. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1729. return err;
  1730. }
  1731. k = 0;
  1732. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
  1733. for (i = res->start; i <= res->end; i++) {
  1734. if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
  1735. "DM646x_Capture",
  1736. (void *)(&vpif_obj.dev[k]->channel_id))) {
  1737. err = -EBUSY;
  1738. i--;
  1739. goto vpif_int_err;
  1740. }
  1741. }
  1742. k++;
  1743. }
  1744. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1745. /* Get the pointer to the channel object */
  1746. ch = vpif_obj.dev[i];
  1747. /* Allocate memory for video device */
  1748. vfd = video_device_alloc();
  1749. if (NULL == vfd) {
  1750. for (j = 0; j < i; j++) {
  1751. ch = vpif_obj.dev[j];
  1752. video_device_release(ch->video_dev);
  1753. }
  1754. err = -ENOMEM;
  1755. goto vpif_dev_alloc_err;
  1756. }
  1757. /* Initialize field of video device */
  1758. *vfd = vpif_video_template;
  1759. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1760. vfd->release = video_device_release;
  1761. snprintf(vfd->name, sizeof(vfd->name),
  1762. "DM646x_VPIFCapture_DRIVER_V%d.%d.%d",
  1763. (VPIF_CAPTURE_VERSION_CODE >> 16) & 0xff,
  1764. (VPIF_CAPTURE_VERSION_CODE >> 8) & 0xff,
  1765. (VPIF_CAPTURE_VERSION_CODE) & 0xff);
  1766. /* Set video_dev to the video device */
  1767. ch->video_dev = vfd;
  1768. }
  1769. for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
  1770. ch = vpif_obj.dev[j];
  1771. ch->channel_id = j;
  1772. common = &(ch->common[VPIF_VIDEO_INDEX]);
  1773. spin_lock_init(&common->irqlock);
  1774. mutex_init(&common->lock);
  1775. /* Initialize prio member of channel object */
  1776. v4l2_prio_init(&ch->prio);
  1777. err = video_register_device(ch->video_dev,
  1778. VFL_TYPE_GRABBER, (j ? 1 : 0));
  1779. if (err)
  1780. goto probe_out;
  1781. video_set_drvdata(ch->video_dev, ch);
  1782. }
  1783. i2c_adap = i2c_get_adapter(1);
  1784. config = pdev->dev.platform_data;
  1785. subdev_count = config->subdev_count;
  1786. vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1787. GFP_KERNEL);
  1788. if (vpif_obj.sd == NULL) {
  1789. vpif_err("unable to allocate memory for subdevice pointers\n");
  1790. err = -ENOMEM;
  1791. goto probe_out;
  1792. }
  1793. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1794. if (err) {
  1795. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1796. goto probe_subdev_out;
  1797. }
  1798. for (i = 0; i < subdev_count; i++) {
  1799. subdevdata = &config->subdev_info[i];
  1800. vpif_obj.sd[i] =
  1801. v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1802. i2c_adap,
  1803. &subdevdata->board_info,
  1804. NULL);
  1805. if (!vpif_obj.sd[i]) {
  1806. vpif_err("Error registering v4l2 subdevice\n");
  1807. goto probe_subdev_out;
  1808. }
  1809. v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
  1810. subdevdata->name);
  1811. if (vpif_obj.sd[i])
  1812. vpif_obj.sd[i]->grp_id = 1 << i;
  1813. }
  1814. v4l2_info(&vpif_obj.v4l2_dev, "DM646x VPIF Capture driver"
  1815. " initialized\n");
  1816. return 0;
  1817. probe_subdev_out:
  1818. /* free sub devices memory */
  1819. kfree(vpif_obj.sd);
  1820. j = VPIF_CAPTURE_MAX_DEVICES;
  1821. probe_out:
  1822. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1823. for (k = 0; k < j; k++) {
  1824. /* Get the pointer to the channel object */
  1825. ch = vpif_obj.dev[k];
  1826. /* Unregister video device */
  1827. video_unregister_device(ch->video_dev);
  1828. }
  1829. vpif_dev_alloc_err:
  1830. k = VPIF_CAPTURE_MAX_DEVICES-1;
  1831. res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
  1832. i = res->end;
  1833. vpif_int_err:
  1834. for (q = k; q >= 0; q--) {
  1835. for (m = i; m >= (int)res->start; m--)
  1836. free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
  1837. res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
  1838. if (res)
  1839. i = res->end;
  1840. }
  1841. return err;
  1842. }
  1843. /**
  1844. * vpif_remove() - driver remove handler
  1845. * @device: ptr to platform device structure
  1846. *
  1847. * The vidoe device is unregistered
  1848. */
  1849. static int vpif_remove(struct platform_device *device)
  1850. {
  1851. int i;
  1852. struct channel_obj *ch;
  1853. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1854. /* un-register device */
  1855. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1856. /* Get the pointer to the channel object */
  1857. ch = vpif_obj.dev[i];
  1858. /* Unregister video device */
  1859. video_unregister_device(ch->video_dev);
  1860. }
  1861. return 0;
  1862. }
  1863. /**
  1864. * vpif_suspend: vpif device suspend
  1865. *
  1866. * TODO: Add suspend code here
  1867. */
  1868. static int
  1869. vpif_suspend(struct device *dev)
  1870. {
  1871. return -1;
  1872. }
  1873. /**
  1874. * vpif_resume: vpif device suspend
  1875. *
  1876. * TODO: Add resume code here
  1877. */
  1878. static int
  1879. vpif_resume(struct device *dev)
  1880. {
  1881. return -1;
  1882. }
  1883. static const struct dev_pm_ops vpif_dev_pm_ops = {
  1884. .suspend = vpif_suspend,
  1885. .resume = vpif_resume,
  1886. };
  1887. static __refdata struct platform_driver vpif_driver = {
  1888. .driver = {
  1889. .name = "vpif_capture",
  1890. .owner = THIS_MODULE,
  1891. .pm = &vpif_dev_pm_ops,
  1892. },
  1893. .probe = vpif_probe,
  1894. .remove = vpif_remove,
  1895. };
  1896. /**
  1897. * vpif_init: initialize the vpif driver
  1898. *
  1899. * This function registers device and driver to the kernel, requests irq
  1900. * handler and allocates memory
  1901. * for channel objects
  1902. */
  1903. static __init int vpif_init(void)
  1904. {
  1905. return platform_driver_register(&vpif_driver);
  1906. }
  1907. /**
  1908. * vpif_cleanup : This function clean up the vpif capture resources
  1909. *
  1910. * This will un-registers device and driver to the kernel, frees
  1911. * requested irq handler and de-allocates memory allocated for channel
  1912. * objects.
  1913. */
  1914. static void vpif_cleanup(void)
  1915. {
  1916. struct platform_device *pdev;
  1917. struct resource *res;
  1918. int irq_num;
  1919. int i = 0;
  1920. pdev = container_of(vpif_dev, struct platform_device, dev);
  1921. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
  1922. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1923. free_irq(irq_num,
  1924. (void *)(&vpif_obj.dev[i]->channel_id));
  1925. i++;
  1926. }
  1927. platform_driver_unregister(&vpif_driver);
  1928. kfree(vpif_obj.sd);
  1929. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
  1930. kfree(vpif_obj.dev[i]);
  1931. }
  1932. /* Function for module initialization and cleanup */
  1933. module_init(vpif_init);
  1934. module_exit(vpif_cleanup);