vpfe_capture.c 58 KB

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