vpfe_capture.c 56 KB

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