vpfe_capture.c 56 KB

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