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