vpfe_capture.c 59 KB

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