vpfe_capture.c 56 KB

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