vpfe_capture.c 56 KB

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