vpfe_capture.c 57 KB

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