vpfe_capture.c 55 KB

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