vpif_capture.c 62 KB

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