vpif_capture.c 62 KB

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