vpfe_capture.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. /*
  2. * Copyright (C) 2008-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. * Driver name : VPFE Capture driver
  15. * VPFE Capture driver allows applications to capture and stream video
  16. * frames on DaVinci SoCs (DM6446, DM355 etc) from a YUV source such as
  17. * TVP5146 or Raw Bayer RGB image data from an image sensor
  18. * such as Microns' MT9T001, MT9T031 etc.
  19. *
  20. * These SoCs have, in common, a Video Processing Subsystem (VPSS) that
  21. * consists of a Video Processing Front End (VPFE) for capturing
  22. * video/raw image data and Video Processing Back End (VPBE) for displaying
  23. * YUV data through an in-built analog encoder or Digital LCD port. This
  24. * driver is for capture through VPFE. A typical EVM using these SoCs have
  25. * following high level configuration.
  26. *
  27. *
  28. * decoder(TVP5146/ YUV/
  29. * MT9T001) --> Raw Bayer RGB ---> MUX -> VPFE (CCDC/ISIF)
  30. * data input | |
  31. * V |
  32. * SDRAM |
  33. * V
  34. * Image Processor
  35. * |
  36. * V
  37. * SDRAM
  38. * The data flow happens from a decoder connected to the VPFE over a
  39. * YUV embedded (BT.656/BT.1120) or separate sync or raw bayer rgb interface
  40. * and to the input of VPFE through an optional MUX (if more inputs are
  41. * to be interfaced on the EVM). The input data is first passed through
  42. * CCDC (CCD Controller, a.k.a Image Sensor Interface, ISIF). The CCDC
  43. * does very little or no processing on YUV data and does pre-process Raw
  44. * Bayer RGB data through modules such as Defect Pixel Correction (DFC)
  45. * Color Space Conversion (CSC), data gain/offset etc. After this, data
  46. * can be written to SDRAM or can be connected to the image processing
  47. * block such as IPIPE (on DM355 only).
  48. *
  49. * Features supported
  50. * - MMAP IO
  51. * - Capture using TVP5146 over BT.656
  52. * - support for interfacing decoders using sub device model
  53. * - Work with DM355 or DM6446 CCDC to do Raw Bayer RGB/YUV
  54. * data capture to SDRAM.
  55. * TODO list
  56. * - Support multiple REQBUF after open
  57. * - Support for de-allocating buffers through REQBUF
  58. * - Support for Raw Bayer RGB capture
  59. * - Support for chaining Image Processor
  60. * - Support for static allocation of buffers
  61. * - Support for USERPTR IO
  62. * - Support for STREAMON before QBUF
  63. * - Support for control ioctls
  64. */
  65. #include <linux/module.h>
  66. #include <linux/slab.h>
  67. #include <linux/init.h>
  68. #include <linux/platform_device.h>
  69. #include <linux/interrupt.h>
  70. #include <media/v4l2-common.h>
  71. #include <linux/io.h>
  72. #include <media/davinci/vpfe_capture.h>
  73. #include "ccdc_hw_device.h"
  74. static int debug;
  75. static u32 numbuffers = 3;
  76. static u32 bufsize = (720 * 576 * 2);
  77. module_param(numbuffers, uint, S_IRUGO);
  78. module_param(bufsize, uint, S_IRUGO);
  79. module_param(debug, int, 0644);
  80. MODULE_PARM_DESC(numbuffers, "buffer count (default:3)");
  81. MODULE_PARM_DESC(bufsize, "buffer size in bytes (default:720 x 576 x 2)");
  82. MODULE_PARM_DESC(debug, "Debug level 0-1");
  83. MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
  84. MODULE_LICENSE("GPL");
  85. MODULE_AUTHOR("Texas Instruments");
  86. /* standard information */
  87. struct vpfe_standard {
  88. v4l2_std_id std_id;
  89. unsigned int width;
  90. unsigned int height;
  91. struct v4l2_fract pixelaspect;
  92. /* 0 - progressive, 1 - interlaced */
  93. int frame_format;
  94. };
  95. /* ccdc configuration */
  96. struct ccdc_config {
  97. /* This make sure vpfe is probed and ready to go */
  98. int vpfe_probed;
  99. /* name of ccdc device */
  100. char name[32];
  101. };
  102. /* data structures */
  103. static struct vpfe_config_params config_params = {
  104. .min_numbuffers = 3,
  105. .numbuffers = 3,
  106. .min_bufsize = 720 * 480 * 2,
  107. .device_bufsize = 720 * 576 * 2,
  108. };
  109. /* ccdc device registered */
  110. static const struct ccdc_hw_device *ccdc_dev;
  111. /* lock for accessing ccdc information */
  112. static DEFINE_MUTEX(ccdc_lock);
  113. /* ccdc configuration */
  114. static struct ccdc_config *ccdc_cfg;
  115. static const struct vpfe_standard vpfe_standards[] = {
  116. {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
  117. {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
  118. };
  119. /* Used when raw Bayer image from ccdc is directly captured to SDRAM */
  120. static const struct vpfe_pixel_format vpfe_pix_fmts[] = {
  121. {
  122. .fmtdesc = {
  123. .index = 0,
  124. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  125. .description = "Bayer GrRBGb 8bit A-Law compr.",
  126. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  127. },
  128. .bpp = 1,
  129. },
  130. {
  131. .fmtdesc = {
  132. .index = 1,
  133. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  134. .description = "Bayer GrRBGb - 16bit",
  135. .pixelformat = V4L2_PIX_FMT_SBGGR16,
  136. },
  137. .bpp = 2,
  138. },
  139. {
  140. .fmtdesc = {
  141. .index = 2,
  142. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  143. .description = "Bayer GrRBGb 8bit DPCM compr.",
  144. .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
  145. },
  146. .bpp = 1,
  147. },
  148. {
  149. .fmtdesc = {
  150. .index = 3,
  151. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  152. .description = "YCbCr 4:2:2 Interleaved UYVY",
  153. .pixelformat = V4L2_PIX_FMT_UYVY,
  154. },
  155. .bpp = 2,
  156. },
  157. {
  158. .fmtdesc = {
  159. .index = 4,
  160. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  161. .description = "YCbCr 4:2:2 Interleaved YUYV",
  162. .pixelformat = V4L2_PIX_FMT_YUYV,
  163. },
  164. .bpp = 2,
  165. },
  166. {
  167. .fmtdesc = {
  168. .index = 5,
  169. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  170. .description = "Y/CbCr 4:2:0 - Semi planar",
  171. .pixelformat = V4L2_PIX_FMT_NV12,
  172. },
  173. .bpp = 1,
  174. },
  175. };
  176. /*
  177. * vpfe_lookup_pix_format()
  178. * lookup an entry in the vpfe pix format table based on pix_format
  179. */
  180. static const struct vpfe_pixel_format *vpfe_lookup_pix_format(u32 pix_format)
  181. {
  182. int i;
  183. for (i = 0; i < ARRAY_SIZE(vpfe_pix_fmts); i++) {
  184. if (pix_format == vpfe_pix_fmts[i].fmtdesc.pixelformat)
  185. return &vpfe_pix_fmts[i];
  186. }
  187. return NULL;
  188. }
  189. /*
  190. * vpfe_register_ccdc_device. CCDC module calls this to
  191. * register with vpfe capture
  192. */
  193. int vpfe_register_ccdc_device(const struct ccdc_hw_device *dev)
  194. {
  195. int ret = 0;
  196. printk(KERN_NOTICE "vpfe_register_ccdc_device: %s\n", dev->name);
  197. BUG_ON(!dev->hw_ops.open);
  198. BUG_ON(!dev->hw_ops.enable);
  199. BUG_ON(!dev->hw_ops.set_hw_if_params);
  200. BUG_ON(!dev->hw_ops.configure);
  201. BUG_ON(!dev->hw_ops.set_buftype);
  202. BUG_ON(!dev->hw_ops.get_buftype);
  203. BUG_ON(!dev->hw_ops.enum_pix);
  204. BUG_ON(!dev->hw_ops.set_frame_format);
  205. BUG_ON(!dev->hw_ops.get_frame_format);
  206. BUG_ON(!dev->hw_ops.get_pixel_format);
  207. BUG_ON(!dev->hw_ops.set_pixel_format);
  208. BUG_ON(!dev->hw_ops.set_image_window);
  209. BUG_ON(!dev->hw_ops.get_image_window);
  210. BUG_ON(!dev->hw_ops.get_line_length);
  211. BUG_ON(!dev->hw_ops.getfid);
  212. mutex_lock(&ccdc_lock);
  213. if (!ccdc_cfg) {
  214. /*
  215. * TODO. Will this ever happen? if so, we need to fix it.
  216. * Proabably we need to add the request to a linked list and
  217. * walk through it during vpfe probe
  218. */
  219. printk(KERN_ERR "vpfe capture not initialized\n");
  220. ret = -EFAULT;
  221. goto unlock;
  222. }
  223. if (strcmp(dev->name, ccdc_cfg->name)) {
  224. /* ignore this ccdc */
  225. ret = -EINVAL;
  226. goto unlock;
  227. }
  228. if (ccdc_dev) {
  229. printk(KERN_ERR "ccdc already registered\n");
  230. ret = -EINVAL;
  231. goto unlock;
  232. }
  233. ccdc_dev = dev;
  234. unlock:
  235. mutex_unlock(&ccdc_lock);
  236. return ret;
  237. }
  238. EXPORT_SYMBOL(vpfe_register_ccdc_device);
  239. /*
  240. * vpfe_unregister_ccdc_device. CCDC module calls this to
  241. * unregister with vpfe capture
  242. */
  243. void vpfe_unregister_ccdc_device(const struct ccdc_hw_device *dev)
  244. {
  245. if (!dev) {
  246. printk(KERN_ERR "invalid ccdc device ptr\n");
  247. return;
  248. }
  249. printk(KERN_NOTICE "vpfe_unregister_ccdc_device, dev->name = %s\n",
  250. dev->name);
  251. if (strcmp(dev->name, ccdc_cfg->name)) {
  252. /* ignore this ccdc */
  253. return;
  254. }
  255. mutex_lock(&ccdc_lock);
  256. ccdc_dev = NULL;
  257. mutex_unlock(&ccdc_lock);
  258. }
  259. EXPORT_SYMBOL(vpfe_unregister_ccdc_device);
  260. /*
  261. * vpfe_config_ccdc_image_format()
  262. * For a pix format, configure ccdc to setup the capture
  263. */
  264. static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe_dev)
  265. {
  266. enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
  267. int ret = 0;
  268. if (ccdc_dev->hw_ops.set_pixel_format(
  269. vpfe_dev->fmt.fmt.pix.pixelformat) < 0) {
  270. v4l2_err(&vpfe_dev->v4l2_dev,
  271. "couldn't set pix format in ccdc\n");
  272. return -EINVAL;
  273. }
  274. /* configure the image window */
  275. ccdc_dev->hw_ops.set_image_window(&vpfe_dev->crop);
  276. switch (vpfe_dev->fmt.fmt.pix.field) {
  277. case V4L2_FIELD_INTERLACED:
  278. /* do nothing, since it is default */
  279. ret = ccdc_dev->hw_ops.set_buftype(
  280. CCDC_BUFTYPE_FLD_INTERLEAVED);
  281. break;
  282. case V4L2_FIELD_NONE:
  283. frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
  284. /* buffer type only applicable for interlaced scan */
  285. break;
  286. case V4L2_FIELD_SEQ_TB:
  287. ret = ccdc_dev->hw_ops.set_buftype(
  288. CCDC_BUFTYPE_FLD_SEPARATED);
  289. break;
  290. default:
  291. return -EINVAL;
  292. }
  293. /* set the frame format */
  294. if (!ret)
  295. ret = ccdc_dev->hw_ops.set_frame_format(frm_fmt);
  296. return ret;
  297. }
  298. /*
  299. * vpfe_config_image_format()
  300. * For a given standard, this functions sets up the default
  301. * pix format & crop values in the vpfe device and ccdc. It first
  302. * starts with defaults based values from the standard table.
  303. * It then checks if sub device supports get_fmt and then override the
  304. * values based on that.Sets crop values to match with scan resolution
  305. * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
  306. * values in ccdc
  307. */
  308. static int vpfe_config_image_format(struct vpfe_device *vpfe_dev,
  309. v4l2_std_id std_id)
  310. {
  311. struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
  312. struct v4l2_subdev_format fmt = {
  313. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  314. };
  315. struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
  316. struct v4l2_pix_format *pix = &vpfe_dev->fmt.fmt.pix;
  317. int i, ret;
  318. for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
  319. if (vpfe_standards[i].std_id & std_id) {
  320. vpfe_dev->std_info.active_pixels =
  321. vpfe_standards[i].width;
  322. vpfe_dev->std_info.active_lines =
  323. vpfe_standards[i].height;
  324. vpfe_dev->std_info.frame_format =
  325. vpfe_standards[i].frame_format;
  326. vpfe_dev->std_index = i;
  327. break;
  328. }
  329. }
  330. if (i == ARRAY_SIZE(vpfe_standards)) {
  331. v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
  332. return -EINVAL;
  333. }
  334. vpfe_dev->crop.top = 0;
  335. vpfe_dev->crop.left = 0;
  336. vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
  337. vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
  338. pix->width = vpfe_dev->crop.width;
  339. pix->height = vpfe_dev->crop.height;
  340. /* first field and frame format based on standard frame format */
  341. if (vpfe_dev->std_info.frame_format) {
  342. pix->field = V4L2_FIELD_INTERLACED;
  343. /* assume V4L2_PIX_FMT_UYVY as default */
  344. pix->pixelformat = V4L2_PIX_FMT_UYVY;
  345. v4l2_fill_mbus_format(mbus_fmt, pix,
  346. MEDIA_BUS_FMT_YUYV10_2X10);
  347. } else {
  348. pix->field = V4L2_FIELD_NONE;
  349. /* assume V4L2_PIX_FMT_SBGGR8 */
  350. pix->pixelformat = V4L2_PIX_FMT_SBGGR8;
  351. v4l2_fill_mbus_format(mbus_fmt, pix,
  352. MEDIA_BUS_FMT_SBGGR8_1X8);
  353. }
  354. /* if sub device supports get_fmt, override the defaults */
  355. ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
  356. sdinfo->grp_id, pad, get_fmt, NULL, &fmt);
  357. if (ret && ret != -ENOIOCTLCMD) {
  358. v4l2_err(&vpfe_dev->v4l2_dev,
  359. "error in getting get_fmt from sub device\n");
  360. return ret;
  361. }
  362. v4l2_fill_pix_format(pix, mbus_fmt);
  363. pix->bytesperline = pix->width * 2;
  364. pix->sizeimage = pix->bytesperline * pix->height;
  365. /* Sets the values in CCDC */
  366. ret = vpfe_config_ccdc_image_format(vpfe_dev);
  367. if (ret)
  368. return ret;
  369. /* Update the values of sizeimage and bytesperline */
  370. pix->bytesperline = ccdc_dev->hw_ops.get_line_length();
  371. pix->sizeimage = pix->bytesperline * pix->height;
  372. return 0;
  373. }
  374. static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
  375. {
  376. int ret;
  377. /* set first input of current subdevice as the current input */
  378. vpfe_dev->current_input = 0;
  379. /* set default standard */
  380. vpfe_dev->std_index = 0;
  381. /* Configure the default format information */
  382. ret = vpfe_config_image_format(vpfe_dev,
  383. vpfe_standards[vpfe_dev->std_index].std_id);
  384. if (ret)
  385. return ret;
  386. /* now open the ccdc device to initialize it */
  387. mutex_lock(&ccdc_lock);
  388. if (!ccdc_dev) {
  389. v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
  390. ret = -ENODEV;
  391. goto unlock;
  392. }
  393. if (!try_module_get(ccdc_dev->owner)) {
  394. v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
  395. ret = -ENODEV;
  396. goto unlock;
  397. }
  398. ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
  399. if (!ret)
  400. vpfe_dev->initialized = 1;
  401. /* Clear all VPFE/CCDC interrupts */
  402. if (vpfe_dev->cfg->clr_intr)
  403. vpfe_dev->cfg->clr_intr(-1);
  404. unlock:
  405. mutex_unlock(&ccdc_lock);
  406. return ret;
  407. }
  408. /*
  409. * vpfe_open : It creates object of file handle structure and
  410. * stores it in private_data member of filepointer
  411. */
  412. static int vpfe_open(struct file *file)
  413. {
  414. struct vpfe_device *vpfe_dev = video_drvdata(file);
  415. struct video_device *vdev = video_devdata(file);
  416. struct vpfe_fh *fh;
  417. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
  418. if (!vpfe_dev->cfg->num_subdevs) {
  419. v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
  420. return -ENODEV;
  421. }
  422. /* Allocate memory for the file handle object */
  423. fh = kmalloc(sizeof(*fh), GFP_KERNEL);
  424. if (!fh)
  425. return -ENOMEM;
  426. /* store pointer to fh in private_data member of file */
  427. file->private_data = fh;
  428. fh->vpfe_dev = vpfe_dev;
  429. v4l2_fh_init(&fh->fh, vdev);
  430. mutex_lock(&vpfe_dev->lock);
  431. /* If decoder is not initialized. initialize it */
  432. if (!vpfe_dev->initialized) {
  433. if (vpfe_initialize_device(vpfe_dev)) {
  434. mutex_unlock(&vpfe_dev->lock);
  435. v4l2_fh_exit(&fh->fh);
  436. kfree(fh);
  437. return -ENODEV;
  438. }
  439. }
  440. /* Increment device usrs counter */
  441. vpfe_dev->usrs++;
  442. /* Set io_allowed member to false */
  443. fh->io_allowed = 0;
  444. v4l2_fh_add(&fh->fh);
  445. mutex_unlock(&vpfe_dev->lock);
  446. return 0;
  447. }
  448. static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
  449. {
  450. unsigned long addr;
  451. vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
  452. struct videobuf_buffer, queue);
  453. list_del(&vpfe_dev->next_frm->queue);
  454. vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
  455. addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
  456. ccdc_dev->hw_ops.setfbaddr(addr);
  457. }
  458. static void vpfe_schedule_bottom_field(struct vpfe_device *vpfe_dev)
  459. {
  460. unsigned long addr;
  461. addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
  462. addr += vpfe_dev->field_off;
  463. ccdc_dev->hw_ops.setfbaddr(addr);
  464. }
  465. static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
  466. {
  467. v4l2_get_timestamp(&vpfe_dev->cur_frm->ts);
  468. vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
  469. vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
  470. wake_up_interruptible(&vpfe_dev->cur_frm->done);
  471. vpfe_dev->cur_frm = vpfe_dev->next_frm;
  472. }
  473. /* ISR for VINT0*/
  474. static irqreturn_t vpfe_isr(int irq, void *dev_id)
  475. {
  476. struct vpfe_device *vpfe_dev = dev_id;
  477. enum v4l2_field field;
  478. int fid;
  479. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
  480. field = vpfe_dev->fmt.fmt.pix.field;
  481. /* if streaming not started, don't do anything */
  482. if (!vpfe_dev->started)
  483. goto clear_intr;
  484. /* only for 6446 this will be applicable */
  485. if (ccdc_dev->hw_ops.reset)
  486. ccdc_dev->hw_ops.reset();
  487. if (field == V4L2_FIELD_NONE) {
  488. /* handle progressive frame capture */
  489. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
  490. "frame format is progressive...\n");
  491. if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
  492. vpfe_process_buffer_complete(vpfe_dev);
  493. goto clear_intr;
  494. }
  495. /* interlaced or TB capture check which field we are in hardware */
  496. fid = ccdc_dev->hw_ops.getfid();
  497. /* switch the software maintained field id */
  498. vpfe_dev->field_id ^= 1;
  499. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
  500. fid, vpfe_dev->field_id);
  501. if (fid == vpfe_dev->field_id) {
  502. /* we are in-sync here,continue */
  503. if (fid == 0) {
  504. /*
  505. * One frame is just being captured. If the next frame
  506. * is available, release the current frame and move on
  507. */
  508. if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
  509. vpfe_process_buffer_complete(vpfe_dev);
  510. /*
  511. * based on whether the two fields are stored
  512. * interleavely or separately in memory, reconfigure
  513. * the CCDC memory address
  514. */
  515. if (field == V4L2_FIELD_SEQ_TB)
  516. vpfe_schedule_bottom_field(vpfe_dev);
  517. goto clear_intr;
  518. }
  519. /*
  520. * if one field is just being captured configure
  521. * the next frame get the next frame from the empty
  522. * queue if no frame is available hold on to the
  523. * current buffer
  524. */
  525. spin_lock(&vpfe_dev->dma_queue_lock);
  526. if (!list_empty(&vpfe_dev->dma_queue) &&
  527. vpfe_dev->cur_frm == vpfe_dev->next_frm)
  528. vpfe_schedule_next_buffer(vpfe_dev);
  529. spin_unlock(&vpfe_dev->dma_queue_lock);
  530. } else if (fid == 0) {
  531. /*
  532. * out of sync. Recover from any hardware out-of-sync.
  533. * May loose one frame
  534. */
  535. vpfe_dev->field_id = fid;
  536. }
  537. clear_intr:
  538. if (vpfe_dev->cfg->clr_intr)
  539. vpfe_dev->cfg->clr_intr(irq);
  540. return IRQ_HANDLED;
  541. }
  542. /* vdint1_isr - isr handler for VINT1 interrupt */
  543. static irqreturn_t vdint1_isr(int irq, void *dev_id)
  544. {
  545. struct vpfe_device *vpfe_dev = dev_id;
  546. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
  547. /* if streaming not started, don't do anything */
  548. if (!vpfe_dev->started) {
  549. if (vpfe_dev->cfg->clr_intr)
  550. vpfe_dev->cfg->clr_intr(irq);
  551. return IRQ_HANDLED;
  552. }
  553. spin_lock(&vpfe_dev->dma_queue_lock);
  554. if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
  555. !list_empty(&vpfe_dev->dma_queue) &&
  556. vpfe_dev->cur_frm == vpfe_dev->next_frm)
  557. vpfe_schedule_next_buffer(vpfe_dev);
  558. spin_unlock(&vpfe_dev->dma_queue_lock);
  559. if (vpfe_dev->cfg->clr_intr)
  560. vpfe_dev->cfg->clr_intr(irq);
  561. return IRQ_HANDLED;
  562. }
  563. static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
  564. {
  565. enum ccdc_frmfmt frame_format;
  566. frame_format = ccdc_dev->hw_ops.get_frame_format();
  567. if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
  568. free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
  569. }
  570. static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
  571. {
  572. enum ccdc_frmfmt frame_format;
  573. frame_format = ccdc_dev->hw_ops.get_frame_format();
  574. if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
  575. return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
  576. 0, "vpfe_capture1",
  577. vpfe_dev);
  578. }
  579. return 0;
  580. }
  581. /* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
  582. static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
  583. {
  584. vpfe_dev->started = 0;
  585. ccdc_dev->hw_ops.enable(0);
  586. if (ccdc_dev->hw_ops.enable_out_to_sdram)
  587. ccdc_dev->hw_ops.enable_out_to_sdram(0);
  588. }
  589. /*
  590. * vpfe_release : This function deletes buffer queue, frees the
  591. * buffers and the vpfe file handle
  592. */
  593. static int vpfe_release(struct file *file)
  594. {
  595. struct vpfe_device *vpfe_dev = video_drvdata(file);
  596. struct vpfe_fh *fh = file->private_data;
  597. struct vpfe_subdev_info *sdinfo;
  598. int ret;
  599. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
  600. /* Get the device lock */
  601. mutex_lock(&vpfe_dev->lock);
  602. /* if this instance is doing IO */
  603. if (fh->io_allowed) {
  604. if (vpfe_dev->started) {
  605. sdinfo = vpfe_dev->current_subdev;
  606. ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
  607. sdinfo->grp_id,
  608. video, s_stream, 0);
  609. if (ret && (ret != -ENOIOCTLCMD))
  610. v4l2_err(&vpfe_dev->v4l2_dev,
  611. "stream off failed in subdev\n");
  612. vpfe_stop_ccdc_capture(vpfe_dev);
  613. vpfe_detach_irq(vpfe_dev);
  614. videobuf_streamoff(&vpfe_dev->buffer_queue);
  615. }
  616. vpfe_dev->io_usrs = 0;
  617. vpfe_dev->numbuffers = config_params.numbuffers;
  618. videobuf_stop(&vpfe_dev->buffer_queue);
  619. videobuf_mmap_free(&vpfe_dev->buffer_queue);
  620. }
  621. /* Decrement device usrs counter */
  622. vpfe_dev->usrs--;
  623. v4l2_fh_del(&fh->fh);
  624. v4l2_fh_exit(&fh->fh);
  625. /* If this is the last file handle */
  626. if (!vpfe_dev->usrs) {
  627. vpfe_dev->initialized = 0;
  628. if (ccdc_dev->hw_ops.close)
  629. ccdc_dev->hw_ops.close(vpfe_dev->pdev);
  630. module_put(ccdc_dev->owner);
  631. }
  632. mutex_unlock(&vpfe_dev->lock);
  633. file->private_data = NULL;
  634. /* Free memory allocated to file handle object */
  635. kfree(fh);
  636. return 0;
  637. }
  638. /*
  639. * vpfe_mmap : It is used to map kernel space buffers
  640. * into user spaces
  641. */
  642. static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
  643. {
  644. /* Get the device object and file handle object */
  645. struct vpfe_device *vpfe_dev = video_drvdata(file);
  646. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
  647. return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
  648. }
  649. /*
  650. * vpfe_poll: It is used for select/poll system call
  651. */
  652. static __poll_t vpfe_poll(struct file *file, poll_table *wait)
  653. {
  654. struct vpfe_device *vpfe_dev = video_drvdata(file);
  655. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
  656. if (vpfe_dev->started)
  657. return videobuf_poll_stream(file,
  658. &vpfe_dev->buffer_queue, wait);
  659. return 0;
  660. }
  661. /* vpfe capture driver file operations */
  662. static const struct v4l2_file_operations vpfe_fops = {
  663. .owner = THIS_MODULE,
  664. .open = vpfe_open,
  665. .release = vpfe_release,
  666. .unlocked_ioctl = video_ioctl2,
  667. .mmap = vpfe_mmap,
  668. .poll = vpfe_poll
  669. };
  670. /*
  671. * vpfe_check_format()
  672. * This function adjust the input pixel format as per hardware
  673. * capabilities and update the same in pixfmt.
  674. * Following algorithm used :-
  675. *
  676. * If given pixformat is not in the vpfe list of pix formats or not
  677. * supported by the hardware, current value of pixformat in the device
  678. * is used
  679. * If given field is not supported, then current field is used. If field
  680. * is different from current, then it is matched with that from sub device.
  681. * Minimum height is 2 lines for interlaced or tb field and 1 line for
  682. * progressive. Maximum height is clamped to active active lines of scan
  683. * Minimum width is 32 bytes in memory and width is clamped to active
  684. * pixels of scan.
  685. * bytesperline is a multiple of 32.
  686. */
  687. static const struct vpfe_pixel_format *
  688. vpfe_check_format(struct vpfe_device *vpfe_dev,
  689. struct v4l2_pix_format *pixfmt)
  690. {
  691. u32 min_height = 1, min_width = 32, max_width, max_height;
  692. const struct vpfe_pixel_format *vpfe_pix_fmt;
  693. u32 pix;
  694. int temp, found;
  695. vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
  696. if (!vpfe_pix_fmt) {
  697. /*
  698. * use current pixel format in the vpfe device. We
  699. * will find this pix format in the table
  700. */
  701. pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
  702. vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
  703. }
  704. /* check if hw supports it */
  705. temp = 0;
  706. found = 0;
  707. while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
  708. if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
  709. found = 1;
  710. break;
  711. }
  712. temp++;
  713. }
  714. if (!found) {
  715. /* use current pixel format */
  716. pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
  717. /*
  718. * Since this is currently used in the vpfe device, we
  719. * will find this pix format in the table
  720. */
  721. vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
  722. }
  723. /* check what field format is supported */
  724. if (pixfmt->field == V4L2_FIELD_ANY) {
  725. /* if field is any, use current value as default */
  726. pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
  727. }
  728. /*
  729. * if field is not same as current field in the vpfe device
  730. * try matching the field with the sub device field
  731. */
  732. if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
  733. /*
  734. * If field value is not in the supported fields, use current
  735. * field used in the device as default
  736. */
  737. switch (pixfmt->field) {
  738. case V4L2_FIELD_INTERLACED:
  739. case V4L2_FIELD_SEQ_TB:
  740. /* if sub device is supporting progressive, use that */
  741. if (!vpfe_dev->std_info.frame_format)
  742. pixfmt->field = V4L2_FIELD_NONE;
  743. break;
  744. case V4L2_FIELD_NONE:
  745. if (vpfe_dev->std_info.frame_format)
  746. pixfmt->field = V4L2_FIELD_INTERLACED;
  747. break;
  748. default:
  749. /* use current field as default */
  750. pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
  751. break;
  752. }
  753. }
  754. /* Now adjust image resolutions supported */
  755. if (pixfmt->field == V4L2_FIELD_INTERLACED ||
  756. pixfmt->field == V4L2_FIELD_SEQ_TB)
  757. min_height = 2;
  758. max_width = vpfe_dev->std_info.active_pixels;
  759. max_height = vpfe_dev->std_info.active_lines;
  760. min_width /= vpfe_pix_fmt->bpp;
  761. v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
  762. pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
  763. pixfmt->width = clamp((pixfmt->width), min_width, max_width);
  764. pixfmt->height = clamp((pixfmt->height), min_height, max_height);
  765. /* If interlaced, adjust height to be a multiple of 2 */
  766. if (pixfmt->field == V4L2_FIELD_INTERLACED)
  767. pixfmt->height &= (~1);
  768. /*
  769. * recalculate bytesperline and sizeimage since width
  770. * and height might have changed
  771. */
  772. pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
  773. & ~31);
  774. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  775. pixfmt->sizeimage =
  776. pixfmt->bytesperline * pixfmt->height +
  777. ((pixfmt->bytesperline * pixfmt->height) >> 1);
  778. else
  779. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  780. v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height = %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
  781. pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
  782. pixfmt->bytesperline, pixfmt->sizeimage);
  783. return vpfe_pix_fmt;
  784. }
  785. static int vpfe_querycap(struct file *file, void *priv,
  786. struct v4l2_capability *cap)
  787. {
  788. struct vpfe_device *vpfe_dev = video_drvdata(file);
  789. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
  790. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  791. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  792. strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
  793. strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
  794. strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
  795. return 0;
  796. }
  797. static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
  798. struct v4l2_format *fmt)
  799. {
  800. struct vpfe_device *vpfe_dev = video_drvdata(file);
  801. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
  802. /* Fill in the information about format */
  803. *fmt = vpfe_dev->fmt;
  804. return 0;
  805. }
  806. static int vpfe_enum_fmt_vid_cap(struct file *file, void *priv,
  807. struct v4l2_fmtdesc *fmt)
  808. {
  809. struct vpfe_device *vpfe_dev = video_drvdata(file);
  810. const struct vpfe_pixel_format *pix_fmt;
  811. int temp_index;
  812. u32 pix;
  813. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
  814. if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
  815. return -EINVAL;
  816. /* Fill in the information about format */
  817. pix_fmt = vpfe_lookup_pix_format(pix);
  818. if (pix_fmt) {
  819. temp_index = fmt->index;
  820. *fmt = pix_fmt->fmtdesc;
  821. fmt->index = temp_index;
  822. return 0;
  823. }
  824. return -EINVAL;
  825. }
  826. static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
  827. struct v4l2_format *fmt)
  828. {
  829. struct vpfe_device *vpfe_dev = video_drvdata(file);
  830. const struct vpfe_pixel_format *pix_fmts;
  831. int ret;
  832. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
  833. /* If streaming is started, return error */
  834. if (vpfe_dev->started) {
  835. v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
  836. return -EBUSY;
  837. }
  838. /* Check for valid frame format */
  839. pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
  840. if (!pix_fmts)
  841. return -EINVAL;
  842. /* store the pixel format in the device object */
  843. ret = mutex_lock_interruptible(&vpfe_dev->lock);
  844. if (ret)
  845. return ret;
  846. /* First detach any IRQ if currently attached */
  847. vpfe_detach_irq(vpfe_dev);
  848. vpfe_dev->fmt = *fmt;
  849. /* set image capture parameters in the ccdc */
  850. ret = vpfe_config_ccdc_image_format(vpfe_dev);
  851. mutex_unlock(&vpfe_dev->lock);
  852. return ret;
  853. }
  854. static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
  855. struct v4l2_format *f)
  856. {
  857. struct vpfe_device *vpfe_dev = video_drvdata(file);
  858. const struct vpfe_pixel_format *pix_fmts;
  859. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
  860. pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
  861. if (!pix_fmts)
  862. return -EINVAL;
  863. return 0;
  864. }
  865. /*
  866. * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
  867. * given app input index
  868. */
  869. static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
  870. int *subdev_index,
  871. int *subdev_input_index,
  872. int app_input_index)
  873. {
  874. struct vpfe_config *cfg = vpfe_dev->cfg;
  875. struct vpfe_subdev_info *sdinfo;
  876. int i, j = 0;
  877. for (i = 0; i < cfg->num_subdevs; i++) {
  878. sdinfo = &cfg->sub_devs[i];
  879. if (app_input_index < (j + sdinfo->num_inputs)) {
  880. *subdev_index = i;
  881. *subdev_input_index = app_input_index - j;
  882. return 0;
  883. }
  884. j += sdinfo->num_inputs;
  885. }
  886. return -EINVAL;
  887. }
  888. /*
  889. * vpfe_get_app_input - Get app input index for a given subdev input index
  890. * driver stores the input index of the current sub device and translate it
  891. * when application request the current input
  892. */
  893. static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
  894. int *app_input_index)
  895. {
  896. struct vpfe_config *cfg = vpfe_dev->cfg;
  897. struct vpfe_subdev_info *sdinfo;
  898. int i, j = 0;
  899. for (i = 0; i < cfg->num_subdevs; i++) {
  900. sdinfo = &cfg->sub_devs[i];
  901. if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
  902. if (vpfe_dev->current_input >= sdinfo->num_inputs)
  903. return -1;
  904. *app_input_index = j + vpfe_dev->current_input;
  905. return 0;
  906. }
  907. j += sdinfo->num_inputs;
  908. }
  909. return -EINVAL;
  910. }
  911. static int vpfe_enum_input(struct file *file, void *priv,
  912. struct v4l2_input *inp)
  913. {
  914. struct vpfe_device *vpfe_dev = video_drvdata(file);
  915. struct vpfe_subdev_info *sdinfo;
  916. int subdev, index ;
  917. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
  918. if (vpfe_get_subdev_input_index(vpfe_dev,
  919. &subdev,
  920. &index,
  921. inp->index) < 0) {
  922. v4l2_err(&vpfe_dev->v4l2_dev, "input information not found for the subdev\n");
  923. return -EINVAL;
  924. }
  925. sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
  926. *inp = sdinfo->inputs[index];
  927. return 0;
  928. }
  929. static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
  930. {
  931. struct vpfe_device *vpfe_dev = video_drvdata(file);
  932. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
  933. return vpfe_get_app_input_index(vpfe_dev, index);
  934. }
  935. static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
  936. {
  937. struct vpfe_device *vpfe_dev = video_drvdata(file);
  938. struct v4l2_subdev *sd;
  939. struct vpfe_subdev_info *sdinfo;
  940. int subdev_index, inp_index;
  941. struct vpfe_route *route;
  942. u32 input, output;
  943. int ret;
  944. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
  945. ret = mutex_lock_interruptible(&vpfe_dev->lock);
  946. if (ret)
  947. return ret;
  948. /*
  949. * If streaming is started return device busy
  950. * error
  951. */
  952. if (vpfe_dev->started) {
  953. v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
  954. ret = -EBUSY;
  955. goto unlock_out;
  956. }
  957. ret = vpfe_get_subdev_input_index(vpfe_dev,
  958. &subdev_index,
  959. &inp_index,
  960. index);
  961. if (ret < 0) {
  962. v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
  963. goto unlock_out;
  964. }
  965. sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
  966. sd = vpfe_dev->sd[subdev_index];
  967. route = &sdinfo->routes[inp_index];
  968. if (route && sdinfo->can_route) {
  969. input = route->input;
  970. output = route->output;
  971. } else {
  972. input = 0;
  973. output = 0;
  974. }
  975. if (sd)
  976. ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
  977. if (ret) {
  978. v4l2_err(&vpfe_dev->v4l2_dev,
  979. "vpfe_doioctl:error in setting input in decoder\n");
  980. ret = -EINVAL;
  981. goto unlock_out;
  982. }
  983. vpfe_dev->current_subdev = sdinfo;
  984. if (sd)
  985. vpfe_dev->v4l2_dev.ctrl_handler = sd->ctrl_handler;
  986. vpfe_dev->current_input = index;
  987. vpfe_dev->std_index = 0;
  988. /* set the bus/interface parameter for the sub device in ccdc */
  989. ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
  990. if (ret)
  991. goto unlock_out;
  992. /* set the default image parameters in the device */
  993. ret = vpfe_config_image_format(vpfe_dev,
  994. vpfe_standards[vpfe_dev->std_index].std_id);
  995. unlock_out:
  996. mutex_unlock(&vpfe_dev->lock);
  997. return ret;
  998. }
  999. static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
  1000. {
  1001. struct vpfe_device *vpfe_dev = video_drvdata(file);
  1002. struct vpfe_subdev_info *sdinfo;
  1003. int ret;
  1004. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
  1005. ret = mutex_lock_interruptible(&vpfe_dev->lock);
  1006. sdinfo = vpfe_dev->current_subdev;
  1007. if (ret)
  1008. return ret;
  1009. /* Call querystd function of decoder device */
  1010. ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
  1011. video, querystd, std_id);
  1012. mutex_unlock(&vpfe_dev->lock);
  1013. return ret;
  1014. }
  1015. static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
  1016. {
  1017. struct vpfe_device *vpfe_dev = video_drvdata(file);
  1018. struct vpfe_subdev_info *sdinfo;
  1019. int ret;
  1020. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
  1021. /* Call decoder driver function to set the standard */
  1022. ret = mutex_lock_interruptible(&vpfe_dev->lock);
  1023. if (ret)
  1024. return ret;
  1025. sdinfo = vpfe_dev->current_subdev;
  1026. /* If streaming is started, return device busy error */
  1027. if (vpfe_dev->started) {
  1028. v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
  1029. ret = -EBUSY;
  1030. goto unlock_out;
  1031. }
  1032. ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
  1033. video, s_std, std_id);
  1034. if (ret < 0) {
  1035. v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
  1036. goto unlock_out;
  1037. }
  1038. ret = vpfe_config_image_format(vpfe_dev, std_id);
  1039. unlock_out:
  1040. mutex_unlock(&vpfe_dev->lock);
  1041. return ret;
  1042. }
  1043. static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
  1044. {
  1045. struct vpfe_device *vpfe_dev = video_drvdata(file);
  1046. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
  1047. *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
  1048. return 0;
  1049. }
  1050. /*
  1051. * Videobuf operations
  1052. */
  1053. static int vpfe_videobuf_setup(struct videobuf_queue *vq,
  1054. unsigned int *count,
  1055. unsigned int *size)
  1056. {
  1057. struct vpfe_fh *fh = vq->priv_data;
  1058. struct vpfe_device *vpfe_dev = fh->vpfe_dev;
  1059. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
  1060. *size = vpfe_dev->fmt.fmt.pix.sizeimage;
  1061. if (vpfe_dev->memory == V4L2_MEMORY_MMAP &&
  1062. vpfe_dev->fmt.fmt.pix.sizeimage > config_params.device_bufsize)
  1063. *size = config_params.device_bufsize;
  1064. if (*count < config_params.min_numbuffers)
  1065. *count = config_params.min_numbuffers;
  1066. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
  1067. "count=%d, size=%d\n", *count, *size);
  1068. return 0;
  1069. }
  1070. static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
  1071. struct videobuf_buffer *vb,
  1072. enum v4l2_field field)
  1073. {
  1074. struct vpfe_fh *fh = vq->priv_data;
  1075. struct vpfe_device *vpfe_dev = fh->vpfe_dev;
  1076. unsigned long addr;
  1077. int ret;
  1078. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
  1079. /* If buffer is not initialized, initialize it */
  1080. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  1081. vb->width = vpfe_dev->fmt.fmt.pix.width;
  1082. vb->height = vpfe_dev->fmt.fmt.pix.height;
  1083. vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
  1084. vb->field = field;
  1085. ret = videobuf_iolock(vq, vb, NULL);
  1086. if (ret < 0)
  1087. return ret;
  1088. addr = videobuf_to_dma_contig(vb);
  1089. /* Make sure user addresses are aligned to 32 bytes */
  1090. if (!ALIGN(addr, 32))
  1091. return -EINVAL;
  1092. vb->state = VIDEOBUF_PREPARED;
  1093. }
  1094. return 0;
  1095. }
  1096. static void vpfe_videobuf_queue(struct videobuf_queue *vq,
  1097. struct videobuf_buffer *vb)
  1098. {
  1099. /* Get the file handle object and device object */
  1100. struct vpfe_fh *fh = vq->priv_data;
  1101. struct vpfe_device *vpfe_dev = fh->vpfe_dev;
  1102. unsigned long flags;
  1103. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
  1104. /* add the buffer to the DMA queue */
  1105. spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
  1106. list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
  1107. spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
  1108. /* Change state of the buffer */
  1109. vb->state = VIDEOBUF_QUEUED;
  1110. }
  1111. static void vpfe_videobuf_release(struct videobuf_queue *vq,
  1112. struct videobuf_buffer *vb)
  1113. {
  1114. struct vpfe_fh *fh = vq->priv_data;
  1115. struct vpfe_device *vpfe_dev = fh->vpfe_dev;
  1116. unsigned long flags;
  1117. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
  1118. /*
  1119. * We need to flush the buffer from the dma queue since
  1120. * they are de-allocated
  1121. */
  1122. spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
  1123. INIT_LIST_HEAD(&vpfe_dev->dma_queue);
  1124. spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
  1125. videobuf_dma_contig_free(vq, vb);
  1126. vb->state = VIDEOBUF_NEEDS_INIT;
  1127. }
  1128. static const struct videobuf_queue_ops vpfe_videobuf_qops = {
  1129. .buf_setup = vpfe_videobuf_setup,
  1130. .buf_prepare = vpfe_videobuf_prepare,
  1131. .buf_queue = vpfe_videobuf_queue,
  1132. .buf_release = vpfe_videobuf_release,
  1133. };
  1134. /*
  1135. * vpfe_reqbufs. currently support REQBUF only once opening
  1136. * the device.
  1137. */
  1138. static int vpfe_reqbufs(struct file *file, void *priv,
  1139. struct v4l2_requestbuffers *req_buf)
  1140. {
  1141. struct vpfe_device *vpfe_dev = video_drvdata(file);
  1142. struct vpfe_fh *fh = file->private_data;
  1143. int ret;
  1144. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
  1145. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
  1146. v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
  1147. return -EINVAL;
  1148. }
  1149. ret = mutex_lock_interruptible(&vpfe_dev->lock);
  1150. if (ret)
  1151. return ret;
  1152. if (vpfe_dev->io_usrs != 0) {
  1153. v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
  1154. ret = -EBUSY;
  1155. goto unlock_out;
  1156. }
  1157. vpfe_dev->memory = req_buf->memory;
  1158. videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
  1159. &vpfe_videobuf_qops,
  1160. vpfe_dev->pdev,
  1161. &vpfe_dev->irqlock,
  1162. req_buf->type,
  1163. vpfe_dev->fmt.fmt.pix.field,
  1164. sizeof(struct videobuf_buffer),
  1165. fh, NULL);
  1166. fh->io_allowed = 1;
  1167. vpfe_dev->io_usrs = 1;
  1168. INIT_LIST_HEAD(&vpfe_dev->dma_queue);
  1169. ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
  1170. unlock_out:
  1171. mutex_unlock(&vpfe_dev->lock);
  1172. return ret;
  1173. }
  1174. static int vpfe_querybuf(struct file *file, void *priv,
  1175. struct v4l2_buffer *buf)
  1176. {
  1177. struct vpfe_device *vpfe_dev = video_drvdata(file);
  1178. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
  1179. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
  1180. v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
  1181. return -EINVAL;
  1182. }
  1183. if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
  1184. v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
  1185. return -EINVAL;
  1186. }
  1187. /* Call videobuf_querybuf to get information */
  1188. return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
  1189. }
  1190. static int vpfe_qbuf(struct file *file, void *priv,
  1191. struct v4l2_buffer *p)
  1192. {
  1193. struct vpfe_device *vpfe_dev = video_drvdata(file);
  1194. struct vpfe_fh *fh = file->private_data;
  1195. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
  1196. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
  1197. v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
  1198. return -EINVAL;
  1199. }
  1200. /*
  1201. * If this file handle is not allowed to do IO,
  1202. * return error
  1203. */
  1204. if (!fh->io_allowed) {
  1205. v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
  1206. return -EACCES;
  1207. }
  1208. return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
  1209. }
  1210. static int vpfe_dqbuf(struct file *file, void *priv,
  1211. struct v4l2_buffer *buf)
  1212. {
  1213. struct vpfe_device *vpfe_dev = video_drvdata(file);
  1214. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
  1215. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
  1216. v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
  1217. return -EINVAL;
  1218. }
  1219. return videobuf_dqbuf(&vpfe_dev->buffer_queue,
  1220. buf, file->f_flags & O_NONBLOCK);
  1221. }
  1222. /*
  1223. * vpfe_calculate_offsets : This function calculates buffers offset
  1224. * for top and bottom field
  1225. */
  1226. static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
  1227. {
  1228. struct v4l2_rect image_win;
  1229. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
  1230. ccdc_dev->hw_ops.get_image_window(&image_win);
  1231. vpfe_dev->field_off = image_win.height * image_win.width;
  1232. }
  1233. /* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
  1234. static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
  1235. {
  1236. ccdc_dev->hw_ops.enable(1);
  1237. if (ccdc_dev->hw_ops.enable_out_to_sdram)
  1238. ccdc_dev->hw_ops.enable_out_to_sdram(1);
  1239. vpfe_dev->started = 1;
  1240. }
  1241. /*
  1242. * vpfe_streamon. Assume the DMA queue is not empty.
  1243. * application is expected to call QBUF before calling
  1244. * this ioctl. If not, driver returns error
  1245. */
  1246. static int vpfe_streamon(struct file *file, void *priv,
  1247. enum v4l2_buf_type buf_type)
  1248. {
  1249. struct vpfe_device *vpfe_dev = video_drvdata(file);
  1250. struct vpfe_fh *fh = file->private_data;
  1251. struct vpfe_subdev_info *sdinfo;
  1252. unsigned long addr;
  1253. int ret;
  1254. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
  1255. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
  1256. v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
  1257. return -EINVAL;
  1258. }
  1259. /* If file handle is not allowed IO, return error */
  1260. if (!fh->io_allowed) {
  1261. v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
  1262. return -EACCES;
  1263. }
  1264. sdinfo = vpfe_dev->current_subdev;
  1265. ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
  1266. video, s_stream, 1);
  1267. if (ret && (ret != -ENOIOCTLCMD)) {
  1268. v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
  1269. return -EINVAL;
  1270. }
  1271. /* If buffer queue is empty, return error */
  1272. if (list_empty(&vpfe_dev->buffer_queue.stream)) {
  1273. v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
  1274. return -EIO;
  1275. }
  1276. /* Call videobuf_streamon to start streaming * in videobuf */
  1277. ret = videobuf_streamon(&vpfe_dev->buffer_queue);
  1278. if (ret)
  1279. return ret;
  1280. ret = mutex_lock_interruptible(&vpfe_dev->lock);
  1281. if (ret)
  1282. goto streamoff;
  1283. /* Get the next frame from the buffer queue */
  1284. vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
  1285. struct videobuf_buffer, queue);
  1286. vpfe_dev->cur_frm = vpfe_dev->next_frm;
  1287. /* Remove buffer from the buffer queue */
  1288. list_del(&vpfe_dev->cur_frm->queue);
  1289. /* Mark state of the current frame to active */
  1290. vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
  1291. /* Initialize field_id and started member */
  1292. vpfe_dev->field_id = 0;
  1293. addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
  1294. /* Calculate field offset */
  1295. vpfe_calculate_offsets(vpfe_dev);
  1296. if (vpfe_attach_irq(vpfe_dev) < 0) {
  1297. v4l2_err(&vpfe_dev->v4l2_dev,
  1298. "Error in attaching interrupt handle\n");
  1299. ret = -EFAULT;
  1300. goto unlock_out;
  1301. }
  1302. if (ccdc_dev->hw_ops.configure() < 0) {
  1303. v4l2_err(&vpfe_dev->v4l2_dev,
  1304. "Error in configuring ccdc\n");
  1305. ret = -EINVAL;
  1306. goto unlock_out;
  1307. }
  1308. ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
  1309. vpfe_start_ccdc_capture(vpfe_dev);
  1310. mutex_unlock(&vpfe_dev->lock);
  1311. return ret;
  1312. unlock_out:
  1313. mutex_unlock(&vpfe_dev->lock);
  1314. streamoff:
  1315. videobuf_streamoff(&vpfe_dev->buffer_queue);
  1316. return ret;
  1317. }
  1318. static int vpfe_streamoff(struct file *file, void *priv,
  1319. enum v4l2_buf_type buf_type)
  1320. {
  1321. struct vpfe_device *vpfe_dev = video_drvdata(file);
  1322. struct vpfe_fh *fh = file->private_data;
  1323. struct vpfe_subdev_info *sdinfo;
  1324. int ret;
  1325. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
  1326. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
  1327. v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
  1328. return -EINVAL;
  1329. }
  1330. /* If io is allowed for this file handle, return error */
  1331. if (!fh->io_allowed) {
  1332. v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
  1333. return -EACCES;
  1334. }
  1335. /* If streaming is not started, return error */
  1336. if (!vpfe_dev->started) {
  1337. v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
  1338. return -EINVAL;
  1339. }
  1340. ret = mutex_lock_interruptible(&vpfe_dev->lock);
  1341. if (ret)
  1342. return ret;
  1343. vpfe_stop_ccdc_capture(vpfe_dev);
  1344. vpfe_detach_irq(vpfe_dev);
  1345. sdinfo = vpfe_dev->current_subdev;
  1346. ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
  1347. video, s_stream, 0);
  1348. if (ret && (ret != -ENOIOCTLCMD))
  1349. v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
  1350. ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
  1351. mutex_unlock(&vpfe_dev->lock);
  1352. return ret;
  1353. }
  1354. static int vpfe_cropcap(struct file *file, void *priv,
  1355. struct v4l2_cropcap *crop)
  1356. {
  1357. struct vpfe_device *vpfe_dev = video_drvdata(file);
  1358. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
  1359. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1360. return -EINVAL;
  1361. /* If std_index is invalid, then just return (== 1:1 aspect) */
  1362. if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
  1363. return 0;
  1364. crop->pixelaspect = vpfe_standards[vpfe_dev->std_index].pixelaspect;
  1365. return 0;
  1366. }
  1367. static int vpfe_g_selection(struct file *file, void *priv,
  1368. struct v4l2_selection *sel)
  1369. {
  1370. struct vpfe_device *vpfe_dev = video_drvdata(file);
  1371. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_selection\n");
  1372. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1373. return -EINVAL;
  1374. switch (sel->target) {
  1375. case V4L2_SEL_TGT_CROP:
  1376. sel->r = vpfe_dev->crop;
  1377. break;
  1378. case V4L2_SEL_TGT_CROP_DEFAULT:
  1379. case V4L2_SEL_TGT_CROP_BOUNDS:
  1380. sel->r.width = vpfe_standards[vpfe_dev->std_index].width;
  1381. sel->r.height = vpfe_standards[vpfe_dev->std_index].height;
  1382. break;
  1383. default:
  1384. return -EINVAL;
  1385. }
  1386. return 0;
  1387. }
  1388. static int vpfe_s_selection(struct file *file, void *priv,
  1389. struct v4l2_selection *sel)
  1390. {
  1391. struct vpfe_device *vpfe_dev = video_drvdata(file);
  1392. struct v4l2_rect rect = sel->r;
  1393. int ret;
  1394. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_selection\n");
  1395. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1396. sel->target != V4L2_SEL_TGT_CROP)
  1397. return -EINVAL;
  1398. if (vpfe_dev->started) {
  1399. /* make sure streaming is not started */
  1400. v4l2_err(&vpfe_dev->v4l2_dev,
  1401. "Cannot change crop when streaming is ON\n");
  1402. return -EBUSY;
  1403. }
  1404. ret = mutex_lock_interruptible(&vpfe_dev->lock);
  1405. if (ret)
  1406. return ret;
  1407. if (rect.top < 0 || rect.left < 0) {
  1408. v4l2_err(&vpfe_dev->v4l2_dev,
  1409. "doesn't support negative values for top & left\n");
  1410. ret = -EINVAL;
  1411. goto unlock_out;
  1412. }
  1413. /* adjust the width to 16 pixel boundary */
  1414. rect.width = ((rect.width + 15) & ~0xf);
  1415. /* make sure parameters are valid */
  1416. if ((rect.left + rect.width >
  1417. vpfe_dev->std_info.active_pixels) ||
  1418. (rect.top + rect.height >
  1419. vpfe_dev->std_info.active_lines)) {
  1420. v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_SELECTION params\n");
  1421. ret = -EINVAL;
  1422. goto unlock_out;
  1423. }
  1424. ccdc_dev->hw_ops.set_image_window(&rect);
  1425. vpfe_dev->fmt.fmt.pix.width = rect.width;
  1426. vpfe_dev->fmt.fmt.pix.height = rect.height;
  1427. vpfe_dev->fmt.fmt.pix.bytesperline =
  1428. ccdc_dev->hw_ops.get_line_length();
  1429. vpfe_dev->fmt.fmt.pix.sizeimage =
  1430. vpfe_dev->fmt.fmt.pix.bytesperline *
  1431. vpfe_dev->fmt.fmt.pix.height;
  1432. vpfe_dev->crop = rect;
  1433. sel->r = rect;
  1434. unlock_out:
  1435. mutex_unlock(&vpfe_dev->lock);
  1436. return ret;
  1437. }
  1438. /* vpfe capture ioctl operations */
  1439. static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
  1440. .vidioc_querycap = vpfe_querycap,
  1441. .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
  1442. .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
  1443. .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
  1444. .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
  1445. .vidioc_enum_input = vpfe_enum_input,
  1446. .vidioc_g_input = vpfe_g_input,
  1447. .vidioc_s_input = vpfe_s_input,
  1448. .vidioc_querystd = vpfe_querystd,
  1449. .vidioc_s_std = vpfe_s_std,
  1450. .vidioc_g_std = vpfe_g_std,
  1451. .vidioc_reqbufs = vpfe_reqbufs,
  1452. .vidioc_querybuf = vpfe_querybuf,
  1453. .vidioc_qbuf = vpfe_qbuf,
  1454. .vidioc_dqbuf = vpfe_dqbuf,
  1455. .vidioc_streamon = vpfe_streamon,
  1456. .vidioc_streamoff = vpfe_streamoff,
  1457. .vidioc_cropcap = vpfe_cropcap,
  1458. .vidioc_g_selection = vpfe_g_selection,
  1459. .vidioc_s_selection = vpfe_s_selection,
  1460. };
  1461. static struct vpfe_device *vpfe_initialize(void)
  1462. {
  1463. struct vpfe_device *vpfe_dev;
  1464. /* Default number of buffers should be 3 */
  1465. if ((numbuffers > 0) &&
  1466. (numbuffers < config_params.min_numbuffers))
  1467. numbuffers = config_params.min_numbuffers;
  1468. /*
  1469. * Set buffer size to min buffers size if invalid buffer size is
  1470. * given
  1471. */
  1472. if (bufsize < config_params.min_bufsize)
  1473. bufsize = config_params.min_bufsize;
  1474. config_params.numbuffers = numbuffers;
  1475. if (numbuffers)
  1476. config_params.device_bufsize = bufsize;
  1477. /* Allocate memory for device objects */
  1478. vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
  1479. return vpfe_dev;
  1480. }
  1481. /*
  1482. * vpfe_probe : This function creates device entries by register
  1483. * itself to the V4L2 driver and initializes fields of each
  1484. * device objects
  1485. */
  1486. static int vpfe_probe(struct platform_device *pdev)
  1487. {
  1488. struct vpfe_subdev_info *sdinfo;
  1489. struct vpfe_config *vpfe_cfg;
  1490. struct resource *res1;
  1491. struct vpfe_device *vpfe_dev;
  1492. struct i2c_adapter *i2c_adap;
  1493. struct video_device *vfd;
  1494. int ret, i, j;
  1495. int num_subdevs = 0;
  1496. /* Get the pointer to the device object */
  1497. vpfe_dev = vpfe_initialize();
  1498. if (!vpfe_dev) {
  1499. v4l2_err(pdev->dev.driver,
  1500. "Failed to allocate memory for vpfe_dev\n");
  1501. return -ENOMEM;
  1502. }
  1503. vpfe_dev->pdev = &pdev->dev;
  1504. if (!pdev->dev.platform_data) {
  1505. v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
  1506. ret = -ENODEV;
  1507. goto probe_free_dev_mem;
  1508. }
  1509. vpfe_cfg = pdev->dev.platform_data;
  1510. vpfe_dev->cfg = vpfe_cfg;
  1511. if (!vpfe_cfg->ccdc || !vpfe_cfg->card_name || !vpfe_cfg->sub_devs) {
  1512. v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
  1513. ret = -ENOENT;
  1514. goto probe_free_dev_mem;
  1515. }
  1516. /* Allocate memory for ccdc configuration */
  1517. ccdc_cfg = kmalloc(sizeof(*ccdc_cfg), GFP_KERNEL);
  1518. if (!ccdc_cfg) {
  1519. ret = -ENOMEM;
  1520. goto probe_free_dev_mem;
  1521. }
  1522. mutex_lock(&ccdc_lock);
  1523. strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
  1524. /* Get VINT0 irq resource */
  1525. res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1526. if (!res1) {
  1527. v4l2_err(pdev->dev.driver,
  1528. "Unable to get interrupt for VINT0\n");
  1529. ret = -ENODEV;
  1530. goto probe_free_ccdc_cfg_mem;
  1531. }
  1532. vpfe_dev->ccdc_irq0 = res1->start;
  1533. /* Get VINT1 irq resource */
  1534. res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  1535. if (!res1) {
  1536. v4l2_err(pdev->dev.driver,
  1537. "Unable to get interrupt for VINT1\n");
  1538. ret = -ENODEV;
  1539. goto probe_free_ccdc_cfg_mem;
  1540. }
  1541. vpfe_dev->ccdc_irq1 = res1->start;
  1542. ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, 0,
  1543. "vpfe_capture0", vpfe_dev);
  1544. if (0 != ret) {
  1545. v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
  1546. goto probe_free_ccdc_cfg_mem;
  1547. }
  1548. vfd = &vpfe_dev->video_dev;
  1549. /* Initialize field of video device */
  1550. vfd->release = video_device_release_empty;
  1551. vfd->fops = &vpfe_fops;
  1552. vfd->ioctl_ops = &vpfe_ioctl_ops;
  1553. vfd->tvnorms = 0;
  1554. vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
  1555. snprintf(vfd->name, sizeof(vfd->name),
  1556. "%s_V%d.%d.%d",
  1557. CAPTURE_DRV_NAME,
  1558. (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
  1559. (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
  1560. (VPFE_CAPTURE_VERSION_CODE) & 0xff);
  1561. ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
  1562. if (ret) {
  1563. v4l2_err(pdev->dev.driver,
  1564. "Unable to register v4l2 device.\n");
  1565. goto probe_out_release_irq;
  1566. }
  1567. v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
  1568. spin_lock_init(&vpfe_dev->irqlock);
  1569. spin_lock_init(&vpfe_dev->dma_queue_lock);
  1570. mutex_init(&vpfe_dev->lock);
  1571. /* Initialize field of the device objects */
  1572. vpfe_dev->numbuffers = config_params.numbuffers;
  1573. /* register video device */
  1574. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
  1575. "trying to register vpfe device.\n");
  1576. v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
  1577. "video_dev=%p\n", &vpfe_dev->video_dev);
  1578. vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1579. ret = video_register_device(&vpfe_dev->video_dev,
  1580. VFL_TYPE_GRABBER, -1);
  1581. if (ret) {
  1582. v4l2_err(pdev->dev.driver,
  1583. "Unable to register video device.\n");
  1584. goto probe_out_v4l2_unregister;
  1585. }
  1586. v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
  1587. /* set the driver data in platform device */
  1588. platform_set_drvdata(pdev, vpfe_dev);
  1589. /* set driver private data */
  1590. video_set_drvdata(&vpfe_dev->video_dev, vpfe_dev);
  1591. i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
  1592. num_subdevs = vpfe_cfg->num_subdevs;
  1593. vpfe_dev->sd = kmalloc_array(num_subdevs,
  1594. sizeof(*vpfe_dev->sd),
  1595. GFP_KERNEL);
  1596. if (!vpfe_dev->sd) {
  1597. ret = -ENOMEM;
  1598. goto probe_out_video_unregister;
  1599. }
  1600. for (i = 0; i < num_subdevs; i++) {
  1601. struct v4l2_input *inps;
  1602. sdinfo = &vpfe_cfg->sub_devs[i];
  1603. /* Load up the subdevice */
  1604. vpfe_dev->sd[i] =
  1605. v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
  1606. i2c_adap,
  1607. &sdinfo->board_info,
  1608. NULL);
  1609. if (vpfe_dev->sd[i]) {
  1610. v4l2_info(&vpfe_dev->v4l2_dev,
  1611. "v4l2 sub device %s registered\n",
  1612. sdinfo->name);
  1613. vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
  1614. /* update tvnorms from the sub devices */
  1615. for (j = 0; j < sdinfo->num_inputs; j++) {
  1616. inps = &sdinfo->inputs[j];
  1617. vfd->tvnorms |= inps->std;
  1618. }
  1619. } else {
  1620. v4l2_info(&vpfe_dev->v4l2_dev,
  1621. "v4l2 sub device %s register fails\n",
  1622. sdinfo->name);
  1623. ret = -ENXIO;
  1624. goto probe_sd_out;
  1625. }
  1626. }
  1627. /* set first sub device as current one */
  1628. vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
  1629. vpfe_dev->v4l2_dev.ctrl_handler = vpfe_dev->sd[0]->ctrl_handler;
  1630. /* We have at least one sub device to work with */
  1631. mutex_unlock(&ccdc_lock);
  1632. return 0;
  1633. probe_sd_out:
  1634. kfree(vpfe_dev->sd);
  1635. probe_out_video_unregister:
  1636. video_unregister_device(&vpfe_dev->video_dev);
  1637. probe_out_v4l2_unregister:
  1638. v4l2_device_unregister(&vpfe_dev->v4l2_dev);
  1639. probe_out_release_irq:
  1640. free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
  1641. probe_free_ccdc_cfg_mem:
  1642. kfree(ccdc_cfg);
  1643. mutex_unlock(&ccdc_lock);
  1644. probe_free_dev_mem:
  1645. kfree(vpfe_dev);
  1646. return ret;
  1647. }
  1648. /*
  1649. * vpfe_remove : It un-register device from V4L2 driver
  1650. */
  1651. static int vpfe_remove(struct platform_device *pdev)
  1652. {
  1653. struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
  1654. v4l2_info(pdev->dev.driver, "vpfe_remove\n");
  1655. free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
  1656. kfree(vpfe_dev->sd);
  1657. v4l2_device_unregister(&vpfe_dev->v4l2_dev);
  1658. video_unregister_device(&vpfe_dev->video_dev);
  1659. kfree(vpfe_dev);
  1660. kfree(ccdc_cfg);
  1661. return 0;
  1662. }
  1663. static int vpfe_suspend(struct device *dev)
  1664. {
  1665. return 0;
  1666. }
  1667. static int vpfe_resume(struct device *dev)
  1668. {
  1669. return 0;
  1670. }
  1671. static const struct dev_pm_ops vpfe_dev_pm_ops = {
  1672. .suspend = vpfe_suspend,
  1673. .resume = vpfe_resume,
  1674. };
  1675. static struct platform_driver vpfe_driver = {
  1676. .driver = {
  1677. .name = CAPTURE_DRV_NAME,
  1678. .pm = &vpfe_dev_pm_ops,
  1679. },
  1680. .probe = vpfe_probe,
  1681. .remove = vpfe_remove,
  1682. };
  1683. module_platform_driver(vpfe_driver);