ov7675.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  1. /*
  2. *ov7675 - This code emulates a real video device with v4l2 api
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the BSD Licence, GNU General Public License
  7. * as published by the Free Software Foundation; either version 2 of the
  8. * License, or (at your option) any later version
  9. */
  10. #include <linux/module.h>
  11. #include <linux/delay.h>
  12. #include <linux/errno.h>
  13. #include <linux/fs.h>
  14. #include <linux/kernel.h>
  15. #include <linux/wait.h>
  16. #include <linux/slab.h>
  17. #include <linux/mm.h>
  18. #include <linux/ioport.h>
  19. #include <linux/init.h>
  20. #include <linux/sched.h>
  21. #include <linux/pci.h>
  22. #include <linux/random.h>
  23. #include <linux/version.h>
  24. #include <linux/mutex.h>
  25. #include <linux/videodev2.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/kthread.h>
  29. #include <linux/highmem.h>
  30. #include <linux/freezer.h>
  31. #include <media/videobuf-vmalloc.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include <linux/i2c.h>
  35. #include <media/v4l2-chip-ident.h>
  36. #include <media/v4l2-i2c-drv.h>
  37. #include <media/amlogic/aml_camera.h>
  38. #include <mach/am_regs.h>
  39. #include <mach/pinmux.h>
  40. #include <media/amlogic/656in.h>
  41. #include "common/plat_ctrl.h"
  42. #include "common/vmapi.h"
  43. #ifdef CONFIG_ARCH_MESON6
  44. #include <mach/mod_gate.h>
  45. #endif
  46. #ifdef CONFIG_HAS_EARLYSUSPEND
  47. #include <linux/earlysuspend.h>
  48. static struct early_suspend ov7675_early_suspend;
  49. #endif
  50. #define OV7675_CAMERA_MODULE_NAME "ov7675"
  51. /* Wake up at about 30 fps */
  52. #define WAKE_NUMERATOR 30
  53. #define WAKE_DENOMINATOR 1001
  54. #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
  55. #define OV7675_CAMERA_MAJOR_VERSION 0
  56. #define OV7675_CAMERA_MINOR_VERSION 7
  57. #define OV7675_CAMERA_RELEASE 0
  58. #define OV7675_CAMERA_VERSION \
  59. KERNEL_VERSION(OV7675_CAMERA_MAJOR_VERSION, OV7675_CAMERA_MINOR_VERSION, OV7675_CAMERA_RELEASE)
  60. MODULE_DESCRIPTION("ov7675 On Board");
  61. MODULE_AUTHOR("amlogic-sh");
  62. MODULE_LICENSE("GPL v2");
  63. static unsigned video_nr = -1; /* videoX start number, -1 is autodetect. */
  64. static unsigned debug;
  65. //module_param(debug, uint, 0644);
  66. //MODULE_PARM_DESC(debug, "activates debug info");
  67. static unsigned int vid_limit = 16;
  68. static unsigned int ov7675_have_opened = 0;
  69. static struct i2c_client *this_client;
  70. //module_param(vid_limit, uint, 0644);
  71. //MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  72. /* supported controls */
  73. static struct v4l2_queryctrl ov7675_qctrl[] = {
  74. /*{
  75. .id = V4L2_CID_BRIGHTNESS,
  76. .type = V4L2_CTRL_TYPE_INTEGER,
  77. .name = "Brightness",
  78. .minimum = 0,
  79. .maximum = 255,
  80. .step = 1,
  81. .default_value = 127,
  82. .flags = V4L2_CTRL_FLAG_SLIDER,
  83. }, {
  84. .id = V4L2_CID_CONTRAST,
  85. .type = V4L2_CTRL_TYPE_INTEGER,
  86. .name = "Contrast",
  87. .minimum = 0x10,
  88. .maximum = 0x60,
  89. .step = 0xa,
  90. .default_value = 0x30,
  91. .flags = V4L2_CTRL_FLAG_SLIDER,
  92. }, {
  93. .id = V4L2_CID_SATURATION,
  94. .type = V4L2_CTRL_TYPE_INTEGER,
  95. .name = "Saturation",
  96. .minimum = 0x28,
  97. .maximum = 0x60,
  98. .step = 0x8,
  99. .default_value = 0x48,
  100. .flags = V4L2_CTRL_FLAG_SLIDER,
  101. }, {
  102. .id = V4L2_CID_HFLIP,
  103. .type = V4L2_CTRL_TYPE_INTEGER,
  104. .name = "flip on horizontal",
  105. .minimum = 0,
  106. .maximum = 1,
  107. .step = 0x1,
  108. .default_value = 0,
  109. .flags = V4L2_CTRL_FLAG_SLIDER,
  110. } ,{
  111. .id = V4L2_CID_VFLIP,
  112. .type = V4L2_CTRL_TYPE_INTEGER,
  113. .name = "flip on vertical",
  114. .minimum = 0,
  115. .maximum = 1,
  116. .step = 0x1,
  117. .default_value = 0,
  118. .flags = V4L2_CTRL_FLAG_SLIDER,
  119. },*/{
  120. .id = V4L2_CID_DO_WHITE_BALANCE,
  121. .type = V4L2_CTRL_TYPE_INTEGER,
  122. .name = "white balance",
  123. .minimum = 0,
  124. .maximum = 6,
  125. .step = 0x1,
  126. .default_value = 0,
  127. .flags = V4L2_CTRL_FLAG_SLIDER,
  128. },{
  129. .id = V4L2_CID_EXPOSURE,
  130. .type = V4L2_CTRL_TYPE_INTEGER,
  131. .name = "exposure",
  132. .minimum = 0,
  133. .maximum = 8,
  134. .step = 0x1,
  135. .default_value = 4,
  136. .flags = V4L2_CTRL_FLAG_SLIDER,
  137. },{
  138. .id = V4L2_CID_COLORFX,
  139. .type = V4L2_CTRL_TYPE_INTEGER,
  140. .name = "effect",
  141. .minimum = 0,
  142. .maximum = 6,
  143. .step = 0x1,
  144. .default_value = 0,
  145. .flags = V4L2_CTRL_FLAG_SLIDER,
  146. },{
  147. .id = V4L2_CID_WHITENESS,
  148. .type = V4L2_CTRL_TYPE_INTEGER,
  149. .name = "banding",
  150. .minimum = 0,
  151. .maximum = 1,
  152. .step = 0x1,
  153. .default_value = 0,
  154. .flags = V4L2_CTRL_FLAG_SLIDER,
  155. }
  156. };
  157. #define dprintk(dev, level, fmt, arg...) \
  158. v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
  159. /* ------------------------------------------------------------------
  160. Basic structures
  161. ------------------------------------------------------------------*/
  162. struct ov7675_fmt {
  163. char *name;
  164. u32 fourcc; /* v4l2 format id */
  165. int depth;
  166. };
  167. static struct ov7675_fmt formats[] = {
  168. {
  169. .name = "RGB565 (BE)",
  170. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  171. .depth = 16,
  172. },
  173. {
  174. .name = "RGB888 (24)",
  175. .fourcc = V4L2_PIX_FMT_RGB24, /* 24 RGB-8-8-8 */
  176. .depth = 24,
  177. },
  178. {
  179. .name = "BGR888 (24)",
  180. .fourcc = V4L2_PIX_FMT_BGR24, /* 24 BGR-8-8-8 */
  181. .depth = 24,
  182. },
  183. {
  184. .name = "12 Y/CbCr 4:2:0",
  185. .fourcc = V4L2_PIX_FMT_NV12,
  186. .depth = 12,
  187. },
  188. {
  189. .name = "12 Y/CbCr 4:2:0",
  190. .fourcc = V4L2_PIX_FMT_NV21,
  191. .depth = 12,
  192. },
  193. {
  194. .name = "YUV420P",
  195. .fourcc = V4L2_PIX_FMT_YUV420,
  196. .depth = 12,
  197. }
  198. #if 0
  199. {
  200. .name = "4:2:2, packed, YUYV",
  201. .fourcc = V4L2_PIX_FMT_VYUY,
  202. .depth = 16,
  203. },
  204. {
  205. .name = "RGB565 (LE)",
  206. .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
  207. .depth = 16,
  208. },
  209. {
  210. .name = "RGB555 (LE)",
  211. .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
  212. .depth = 16,
  213. },
  214. {
  215. .name = "RGB555 (BE)",
  216. .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
  217. .depth = 16,
  218. },
  219. #endif
  220. };
  221. static struct ov7675_fmt *get_format(struct v4l2_format *f)
  222. {
  223. struct ov7675_fmt *fmt;
  224. unsigned int k;
  225. for (k = 0; k < ARRAY_SIZE(formats); k++) {
  226. fmt = &formats[k];
  227. if (fmt->fourcc == f->fmt.pix.pixelformat)
  228. break;
  229. }
  230. if (k == ARRAY_SIZE(formats))
  231. return NULL;
  232. return &formats[k];
  233. }
  234. struct sg_to_addr {
  235. int pos;
  236. struct scatterlist *sg;
  237. };
  238. /* buffer for one video frame */
  239. struct ov7675_buffer {
  240. /* common v4l buffer stuff -- must be first */
  241. struct videobuf_buffer vb;
  242. struct ov7675_fmt *fmt;
  243. };
  244. struct ov7675_dmaqueue {
  245. struct list_head active;
  246. /* thread for generating video stream*/
  247. struct task_struct *kthread;
  248. wait_queue_head_t wq;
  249. /* Counters to control fps rate */
  250. int frame;
  251. int ini_jiffies;
  252. };
  253. static LIST_HEAD(ov7675_devicelist);
  254. struct ov7675_device {
  255. struct list_head ov7675_devicelist;
  256. struct v4l2_subdev sd;
  257. struct v4l2_device v4l2_dev;
  258. spinlock_t slock;
  259. struct mutex mutex;
  260. int users;
  261. /* various device info */
  262. struct video_device *vdev;
  263. struct ov7675_dmaqueue vidq;
  264. /* Several counters */
  265. unsigned long jiffies;
  266. /* Input Number */
  267. int input;
  268. /* platform device data from board initting. */
  269. aml_plat_cam_data_t platform_dev_data;
  270. /* Control 'registers' */
  271. int qctl_regs[ARRAY_SIZE(ov7675_qctrl)];
  272. };
  273. static inline struct ov7675_device *to_dev(struct v4l2_subdev *sd)
  274. {
  275. return container_of(sd, struct ov7675_device, sd);
  276. }
  277. struct ov7675_fh {
  278. struct ov7675_device *dev;
  279. /* video capture */
  280. struct ov7675_fmt *fmt;
  281. unsigned int width, height;
  282. struct videobuf_queue vb_vidq;
  283. enum v4l2_buf_type type;
  284. int input; /* Input Number on bars */
  285. int stream_on;
  286. };
  287. static inline struct ov7675_fh *to_fh(struct ov7675_device *dev)
  288. {
  289. return container_of(dev, struct ov7675_fh, dev);
  290. }
  291. static struct v4l2_frmsize_discrete ov7675_prev_resolution[2]=
  292. {
  293. {320,240},
  294. {640,480},
  295. };
  296. static struct v4l2_frmsize_discrete ov7675_pic_resolution[1]=
  297. {
  298. {640,480},
  299. };
  300. /* ------------------------------------------------------------------
  301. reg spec of OV7675
  302. ------------------------------------------------------------------*/
  303. #if 1
  304. struct aml_camera_i2c_fig1_s OV7675_script[] = {
  305. //Input clock 24Mhz, 25fps
  306. //SCCB_salve_Address = 0x42;
  307. {0x12, 0x80},
  308. {0x11, 0x00},
  309. {0x3a, 0x04},
  310. {0x12, 0x00},
  311. {0x17, 0x13},
  312. {0x18, 0x01},
  313. {0x32, 0xb6},
  314. {0x19, 0x03},
  315. {0x1a, 0x7b},
  316. {0x03, 0x08},
  317. {0x0c, 0x00},
  318. {0x3e, 0x00},
  319. {0x70, 0x3a},
  320. {0x71, 0x35},
  321. {0x72, 0x11},
  322. {0x73, 0xf0},
  323. {0xa2, 0x02},
  324. {0x09, 0x00},
  325. {0x7a, 0x18},
  326. {0x7b, 0x04},
  327. {0x7c, 0x09},
  328. {0x7d, 0x18},
  329. {0x7e, 0x38},
  330. {0x7f, 0x47},
  331. {0x80, 0x56},
  332. {0x81, 0x66},
  333. {0x82, 0x74},
  334. {0x83, 0x7f},
  335. {0x84, 0x89},
  336. {0x85, 0x9a},
  337. {0x86, 0xA9},
  338. {0x87, 0xC4},
  339. {0x88, 0xDb},
  340. {0x89, 0xEe},
  341. {0x13, 0xe0},
  342. {0x01, 0x50},
  343. {0x02, 0x68},
  344. {0x00, 0x00},
  345. {0x10, 0x00},
  346. {0x0d, 0x40},
  347. {0x14, 0x18},
  348. {0xa5, 0x05}, // 07
  349. {0xab, 0x06}, //08
  350. {0x24, 0x40},
  351. {0x25, 0x38},
  352. {0x26, 0x81},
  353. {0x9f, 0x78},
  354. {0xa0, 0x68},
  355. {0xa1, 0x03},
  356. {0xa6, 0xd8},
  357. {0xa7, 0xd8},
  358. {0xa8, 0xf0},
  359. {0xa9, 0x90},
  360. {0xaa, 0x14},
  361. {0x13, 0xe5},
  362. {0x0e, 0x61},
  363. {0x0f, 0x4b},
  364. {0x16, 0x02},
  365. {0x1e, 0x27},
  366. {0x21, 0x02},
  367. {0x22, 0x91},
  368. {0x29, 0x07},
  369. {0x33, 0x0b},
  370. {0x35, 0x0b},
  371. {0x37, 0x1d},
  372. {0x38, 0x71},
  373. {0x39, 0x2a},
  374. {0x3c, 0x78},
  375. {0x4d, 0x40},
  376. {0x4e, 0x20},
  377. {0x69, 0x00},
  378. {0x6b, 0x0a},
  379. {0x74, 0x10},
  380. {0x8d, 0x4f},
  381. {0x8e, 0x00},
  382. {0x8f, 0x00},
  383. {0x90, 0x00},
  384. {0x91, 0x00},
  385. {0x92, 0x00}, //1a
  386. {0x2b, 0x4e},
  387. {0x96, 0x00},
  388. {0x9a, 0x80},
  389. {0xb0, 0x84},
  390. {0xb1, 0x0c},
  391. {0xb2, 0x0e},
  392. {0xb3, 0x82},
  393. {0xb8, 0x12},
  394. {0x43, 0x14},
  395. {0x44, 0xf0},
  396. {0x45, 0x41},
  397. {0x46, 0x66},
  398. {0x47, 0x2a},
  399. {0x48, 0x3e},
  400. {0x59, 0x8d},
  401. {0x5a, 0x8e},
  402. {0x5b, 0x53},
  403. {0x5c, 0x83},
  404. {0x5d, 0x4f},
  405. {0x5e, 0x0e},
  406. {0x6c, 0x0a},
  407. {0x6d, 0x55},
  408. {0x6e, 0x11},
  409. {0x6f, 0x9e},
  410. {0x62, 0x90},
  411. {0x63, 0x30},
  412. {0x64, 0x11},
  413. {0x65, 0x00},
  414. {0x66, 0x05},
  415. {0x94, 0x11},
  416. {0x95, 0x18},
  417. {0x6a, 0x40},
  418. {0x01, 0x40},
  419. {0x02, 0x40},
  420. {0x13, 0xe7},
  421. {0x4f, 0x80},
  422. {0x50, 0x80},
  423. {0x51, 0x00},
  424. {0x52, 0x22},
  425. {0x53, 0x5e},
  426. {0x54, 0x80},
  427. {0x58, 0x9e},
  428. {0x41, 0x08},
  429. {0x3f, 0x00},
  430. {0x75, 0x22},
  431. {0x76, 0xe1},
  432. {0x4c, 0x00},
  433. {0x77, 0x03},
  434. {0x3d, 0xc3},
  435. {0x55, 0x10},
  436. {0x4b, 0x09},
  437. {0xc9, 0x60},
  438. {0x41, 0x38},
  439. {0x56, 0x40},
  440. {0x34, 0x11},
  441. {0x3b, 0x0a},
  442. {0xa4, 0x88},
  443. {0x96, 0x00},
  444. {0x97, 0x30},
  445. {0x98, 0x20},
  446. {0x99, 0x30},
  447. {0x9a, 0x84},
  448. {0x9b, 0x29},
  449. {0x9c, 0x03},
  450. {0x9d, 0x3f}, //98
  451. {0x9e, 0x35},
  452. {0xa5, 0x06}, //98
  453. {0xa6, 0x08},
  454. {0x78, 0x04},
  455. {0x79, 0x01},
  456. {0xc8, 0xf0},
  457. {0x79, 0x0f},
  458. {0xc8, 0x00},
  459. {0x79, 0x10},
  460. {0xc8, 0x7e},
  461. {0x79, 0x0a},
  462. {0xc8, 0x80},
  463. {0x79, 0x0b},
  464. {0xc8, 0x01},
  465. {0x79, 0x0c},
  466. {0xc8, 0x0f},
  467. {0x79, 0x0d},
  468. {0xc8, 0x20},
  469. {0x79, 0x09},
  470. {0xc8, 0x80},
  471. {0x79, 0x02},
  472. {0xc8, 0xc0},
  473. {0x79, 0x03},
  474. {0xc8, 0x40},
  475. {0x79, 0x05},
  476. {0xc8, 0x30},
  477. {0x79, 0x26},
  478. {0x2d, 0x00},
  479. {0x2e, 0x00},
  480. {0xff,0xff},
  481. };
  482. //load GT2005 parameters
  483. void OV7675_init_regs(struct ov7675_device *dev)
  484. {
  485. int i=0;//,j;
  486. unsigned char buf[2];
  487. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  488. while(1)
  489. {
  490. buf[0] = OV7675_script[i].addr;//(unsigned char)((OV7675_script[i].addr >> 8) & 0xff);
  491. //buf[1] = (unsigned char)(OV7675_script[i].addr & 0xff);
  492. buf[1] = OV7675_script[i].val;
  493. i++;
  494. if (OV7675_script[i].val==0xff&&OV7675_script[i].addr==0xff)
  495. {
  496. printk("OV7675_write_regs success in initial OV7675.\n");
  497. break;
  498. }
  499. if((i2c_put_byte_add8(client,buf, 2)) < 0)
  500. {
  501. printk("fail in initial OV7675.i=%d \n",i);
  502. return;
  503. }
  504. if(i==1)
  505. msleep(40);
  506. }
  507. //unsigned char temp_reg;
  508. //temp_reg=ov7675_read_byte(0x22);
  509. //buf[0]=0x13;
  510. //buf[1]=0;
  511. //temp_reg=i2c_get_byte_add8(client,buf);
  512. //printk(" camera set_OV7675_param_wb=%d. \n ",temp_reg);
  513. /*msleep(40);
  514. unsigned char temp_reg;
  515. i=0;
  516. while(1)
  517. {
  518. buf[0] = OV7675_script[i].addr;//(unsigned char)((OV7675_script[i].addr >> 8) & 0xff);
  519. //buf[1] = (unsigned char)(OV7675_script[i].addr & 0xff);
  520. buf[1] = OV7675_script[i].val;
  521. i++;
  522. if (OV7675_script[i].val==0xff&&OV7675_script[i].addr==0xff)
  523. {
  524. printk("OV7675_read_regs success in initial OV7675.\n");
  525. break;
  526. }
  527. temp_reg=i2c_get_byte_add8(client,buf);
  528. if(temp_reg < 0)
  529. {
  530. printk("fail in read OV7675.i=%d \n",i);
  531. return;
  532. }
  533. else{
  534. printk("read OV7675.add=%x,val=%x \n",buf[0],temp_reg);
  535. }
  536. //if(i==1)
  537. //msleep(40);
  538. }*/
  539. return;
  540. }
  541. /*void OV7675_init_regs(struct ov7675_device *dev)
  542. {
  543. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  544. int i=0;
  545. while(1)
  546. {
  547. if (OV7675_script[i].val==0xff&&OV7675_script[i].addr==0xff)
  548. {
  549. //printk("GT2005_write_regs success in initial GT2005.\n");
  550. break;
  551. }
  552. if((i2c_put_byte(client,OV7675_script[i].addr, OV7675_script[i].val)) < 0)
  553. {
  554. printk("fail in initial OV7675. \n");
  555. break;
  556. }
  557. i++;
  558. }
  559. return;
  560. }
  561. */
  562. #endif
  563. /*************************************************************************
  564. * FUNCTION
  565. * set_OV7675_param_wb
  566. *
  567. * DESCRIPTION
  568. * OV7675 wb setting.
  569. *
  570. * PARAMETERS
  571. * none
  572. *
  573. * RETURNS
  574. * None
  575. *
  576. * GLOBALS AFFECTED 白平衡参数
  577. *
  578. *************************************************************************/
  579. void set_OV7675_param_wb(struct ov7675_device *dev,enum camera_wb_flip_e para)
  580. {
  581. // kal_uint16 rgain=0x80, ggain=0x80, bgain=0x80;
  582. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  583. unsigned char buf[4];
  584. unsigned char temp_reg;
  585. //temp_reg=ov7675_read_byte(0x22);
  586. buf[0]=0x13;
  587. buf[1]=0;
  588. temp_reg=i2c_get_byte_add8(client,buf);
  589. temp_reg=0xe7;
  590. printk(" camera set_OV7675_param_wb=%d. \n ",temp_reg);
  591. switch (para)
  592. {
  593. case CAM_WB_AUTO:
  594. buf[0]=0x13;
  595. buf[1]=temp_reg|0x2;
  596. i2c_put_byte_add8(client,buf,2); // Enable AWB
  597. break;
  598. case CAM_WB_CLOUD:
  599. buf[0]=0x6a;
  600. buf[1]=0x40;
  601. i2c_put_byte_add8(client,buf,2);
  602. buf[0]=0x13;
  603. buf[1]=temp_reg&(~0x2);
  604. i2c_put_byte_add8(client,buf,2);
  605. buf[0]=0x01;
  606. buf[1]=0x52;
  607. i2c_put_byte_add8(client,buf,2);
  608. buf[0]=0x02;
  609. buf[1]=0x6c;
  610. i2c_put_byte_add8(client,buf,2);
  611. break;
  612. case CAM_WB_DAYLIGHT: // tai yang guang
  613. buf[0]=0x6a;
  614. buf[1]=0x40;
  615. i2c_put_byte_add8(client,buf,2);
  616. buf[0]=0x13;
  617. buf[1]=temp_reg&(~0x2);
  618. i2c_put_byte_add8(client,buf,2);
  619. buf[0]=0x01;
  620. buf[1]=0x52;
  621. i2c_put_byte_add8(client,buf,2);
  622. buf[0]=0x02;
  623. buf[1]=0x66;
  624. i2c_put_byte_add8(client,buf,2);
  625. break;
  626. case CAM_WB_INCANDESCENCE: // bai re guang
  627. buf[0]=0x6a;
  628. buf[1]=0x40;
  629. i2c_put_byte_add8(client,buf,2);
  630. buf[0]=0x13;
  631. buf[1]=temp_reg&(~0x2);
  632. i2c_put_byte_add8(client,buf,2);
  633. buf[0]=0x01;
  634. buf[1]=0x8c;
  635. i2c_put_byte_add8(client,buf,2);
  636. buf[0]=0x02;
  637. buf[1]=0x59;
  638. i2c_put_byte_add8(client,buf,2);
  639. break;
  640. case CAM_WB_FLUORESCENT: //ri guang deng
  641. buf[0]=0x6a;
  642. buf[1]=0x40;
  643. i2c_put_byte_add8(client,buf,2);
  644. buf[0]=0x13;
  645. buf[1]=temp_reg&(~0x2);
  646. i2c_put_byte_add8(client,buf,2);
  647. buf[0]=0x01;
  648. buf[1]=0x7e;
  649. i2c_put_byte_add8(client,buf,2);
  650. buf[0]=0x02;
  651. buf[1]=0x49;
  652. i2c_put_byte_add8(client,buf,2);
  653. break;
  654. case CAM_WB_TUNGSTEN: // wu si deng
  655. buf[0]=0x6a;
  656. buf[1]=0x40;
  657. i2c_put_byte_add8(client,buf,2);
  658. buf[0]=0x13;
  659. buf[1]=temp_reg&(~0x2);
  660. i2c_put_byte_add8(client,buf,2);
  661. buf[0]=0x01;
  662. buf[1]=0x90;
  663. i2c_put_byte_add8(client,buf,2);
  664. buf[0]=0x02;
  665. buf[1]=0x3D;
  666. i2c_put_byte_add8(client,buf,2);
  667. break;
  668. case CAM_WB_MANUAL:
  669. // TODO
  670. break;
  671. default:
  672. break;
  673. }
  674. // kal_sleep_task(20);
  675. }
  676. /*************************************************************************
  677. * FUNCTION
  678. * OV7675_night_mode
  679. *
  680. * DESCRIPTION
  681. * This function night mode of OV7675.
  682. *
  683. * PARAMETERS
  684. * none
  685. *
  686. * RETURNS
  687. * None
  688. *
  689. * GLOBALS AFFECTED
  690. *
  691. *************************************************************************/
  692. void OV7675_night_mode(struct ov7675_device *dev,enum camera_night_mode_flip_e enable)
  693. {
  694. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  695. unsigned char buf[4];
  696. unsigned char temp_reg;
  697. //temp_reg=ov7675_read_byte(0x22);
  698. //buf[0]=0x20;
  699. temp_reg=i2c_get_byte_add8(client,0x3b);
  700. //int temp;
  701. if(enable)
  702. {
  703. temp_reg = temp_reg | 0xc0;
  704. buf[0]=0x3b;
  705. buf[1]=temp_reg;
  706. i2c_put_byte_add8(client,buf,2);
  707. buf[0]=0x14;
  708. buf[1]=0x38;
  709. i2c_put_byte_add8(client,buf,2);
  710. buf[0]=0x2d;
  711. buf[1]=0x00;
  712. i2c_put_byte_add8(client,buf,2);
  713. buf[0]=0x2e;
  714. buf[1]=0x00;
  715. i2c_put_byte_add8(client,buf,2);
  716. }
  717. else
  718. {
  719. temp_reg = temp_reg | 0xa0;
  720. buf[0]=0x3b;
  721. buf[1]=temp_reg;
  722. i2c_put_byte_add8(client,buf,2);
  723. buf[0]=0x14;
  724. buf[1]=0x28;
  725. i2c_put_byte_add8(client,buf,2);
  726. buf[0]=0x2d;
  727. buf[1]=0x00;
  728. i2c_put_byte_add8(client,buf,2);
  729. buf[0]=0x2e;
  730. buf[1]=0x00;
  731. i2c_put_byte_add8(client,buf,2);
  732. }
  733. }
  734. /*************************************************************************
  735. * FUNCTION
  736. * OV7675_night_mode
  737. *
  738. * DESCRIPTION
  739. * This function night mode of OV7675.
  740. *
  741. * PARAMETERS
  742. * none
  743. *
  744. * RETURNS
  745. * None
  746. *
  747. * GLOBALS AFFECTED
  748. *
  749. *************************************************************************/
  750. void OV7675_set_param_banding(struct ov7675_device *dev,enum camera_night_mode_flip_e banding)
  751. {
  752. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  753. unsigned char buf[4];
  754. int temp;
  755. switch(banding)
  756. {
  757. case CAM_BANDING_60HZ:
  758. /*
  759. temp = i2c_get_byte(client, 0x3b);
  760. temp = temp & 0xf7; // banding 60, bit[3] = 0
  761. i2c_put_byte(client, 0x3b, temp);
  762. i2c_put_byte_add8(client,0x9d,0x3f);
  763. i2c_put_byte_add8(client,0xa5,0x06);
  764. */
  765. break;
  766. case CAM_BANDING_50HZ:
  767. /*
  768. temp = i2c_get_byte(client, 0x3b);
  769. temp = temp | 0x08; // banding 50, bit[3] = 1
  770. i2c_put_byte(client, 0x3b, temp);
  771. i2c_put_byte_add8(client,0x9e,0x4c);
  772. i2c_put_byte_add8(client,0xab,0x05);
  773. */
  774. break;
  775. }
  776. }
  777. /*************************************************************************
  778. * FUNCTION
  779. * set_OV7675_param_exposure
  780. *
  781. * DESCRIPTION
  782. * OV7675 exposure setting.
  783. *
  784. * PARAMETERS
  785. * none
  786. *
  787. * RETURNS
  788. * None
  789. *
  790. * GLOBALS AFFECTED 亮度等级 调节参数
  791. *
  792. *************************************************************************/
  793. void set_OV7675_param_exposure(struct ov7675_device *dev,enum camera_exposure_e para)//曝光调节
  794. {
  795. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  796. unsigned char buf[4];
  797. switch (para)
  798. {
  799. case EXPOSURE_N4_STEP:
  800. buf[0]=0x55;
  801. buf[1]=0xb0;
  802. break;
  803. case EXPOSURE_N3_STEP:
  804. buf[0]=0x55;
  805. buf[1]=0xa0;
  806. break;
  807. case EXPOSURE_N2_STEP:
  808. buf[0]=0x55;
  809. buf[1]=0x90;
  810. break;
  811. case EXPOSURE_N1_STEP:
  812. buf[0]=0x55;
  813. buf[1]=0x00;
  814. break;
  815. case EXPOSURE_0_STEP:
  816. buf[0]=0x55;
  817. buf[1]=0x10;
  818. break;
  819. case EXPOSURE_P1_STEP:
  820. buf[0]=0x55;
  821. buf[1]=0x20;
  822. break;
  823. case EXPOSURE_P2_STEP:
  824. buf[0]=0x55;
  825. buf[1]=0x30;
  826. break;
  827. case EXPOSURE_P3_STEP:
  828. buf[0]=0x55;
  829. buf[1]=0x40;
  830. break;
  831. case EXPOSURE_P4_STEP:
  832. buf[0]=0x55;
  833. buf[1]=0x50;
  834. break;
  835. default:
  836. buf[0]=0x55;
  837. buf[1]=0x10;
  838. break;
  839. }
  840. i2c_put_byte_add8(client,buf,2);
  841. }
  842. /*************************************************************************
  843. * FUNCTION
  844. * set_OV7675_param_effect
  845. *
  846. * DESCRIPTION
  847. * OV7675 effect setting.
  848. *
  849. * PARAMETERS
  850. * none
  851. *
  852. * RETURNS
  853. * None
  854. *
  855. * GLOBALS AFFECTED 特效参数
  856. *
  857. *************************************************************************/
  858. void set_OV7675_param_effect(struct ov7675_device *dev,enum camera_effect_flip_e para)//特效设置
  859. {
  860. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  861. unsigned char buf[4];
  862. switch (para)
  863. {
  864. case CAM_EFFECT_ENC_NORMAL:
  865. buf[0]=0x3a;
  866. buf[1]=0x04;
  867. i2c_put_byte_add8(client,buf,2);
  868. buf[0]=0x67;
  869. buf[1]=0xc0;
  870. i2c_put_byte_add8(client,buf,2);
  871. buf[0]=0x68;
  872. buf[1]=0x80;
  873. i2c_put_byte_add8(client,buf,2);
  874. break;
  875. case CAM_EFFECT_ENC_GRAYSCALE:
  876. buf[0]=0x3a;
  877. buf[1]=0x04;
  878. i2c_put_byte_add8(client,buf,2);
  879. buf[0]=0x67;
  880. buf[1]=0xc0;
  881. i2c_put_byte_add8(client,buf,2);
  882. buf[0]=0x68;
  883. buf[1]=0x80;
  884. i2c_put_byte_add8(client,buf,2);
  885. break;
  886. case CAM_EFFECT_ENC_SEPIA:
  887. buf[0]=0x3a;
  888. buf[1]=0x14;
  889. i2c_put_byte_add8(client,buf,2);
  890. buf[0]=0x67;
  891. buf[1]=0x40;
  892. i2c_put_byte_add8(client,buf,2);
  893. buf[0]=0x68;
  894. buf[1]=0xa0;
  895. i2c_put_byte_add8(client,buf,2);
  896. break;
  897. case CAM_EFFECT_ENC_COLORINV:
  898. buf[0]=0x3a;
  899. buf[1]=0x24;
  900. i2c_put_byte_add8(client,buf,2);
  901. buf[0]=0x67;
  902. buf[1]=0x80;
  903. i2c_put_byte_add8(client,buf,2);
  904. buf[0]=0x68;
  905. buf[1]=0x80;
  906. i2c_put_byte_add8(client,buf,2);
  907. break;
  908. case CAM_EFFECT_ENC_SEPIAGREEN:
  909. buf[0]=0x3a;
  910. buf[1]=0x04;
  911. i2c_put_byte_add8(client,buf,2);
  912. buf[0]=0x67;
  913. buf[1]=0xc0;
  914. i2c_put_byte_add8(client,buf,2);
  915. buf[0]=0x68;
  916. buf[1]=0x80;
  917. i2c_put_byte_add8(client,buf,2);
  918. break;
  919. case CAM_EFFECT_ENC_SEPIABLUE:
  920. buf[0]=0x3a;
  921. buf[1]=0x04;
  922. i2c_put_byte_add8(client,buf,2);
  923. buf[0]=0x67;
  924. buf[1]=0xc0;
  925. i2c_put_byte_add8(client,buf,2);
  926. buf[0]=0x68;
  927. buf[1]=0x80;
  928. i2c_put_byte_add8(client,buf,2);
  929. break;
  930. default:
  931. break;
  932. }
  933. }
  934. unsigned char v4l_2_ov7675(int val)
  935. {
  936. int ret=val/0x20;
  937. if(ret<4) return ret*0x20+0x80;
  938. else if(ret<8) return ret*0x20+0x20;
  939. else return 0;
  940. }
  941. static int ov7675_setting(struct ov7675_device *dev,int PROP_ID,int value )
  942. {
  943. int ret=0;
  944. unsigned char cur_val;
  945. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  946. switch(PROP_ID) {
  947. case V4L2_CID_BRIGHTNESS:
  948. dprintk(dev, 1, "setting brightned:%d\n",v4l_2_ov7675(value));
  949. ret=i2c_put_byte(client,0x0201,v4l_2_ov7675(value));
  950. break;
  951. case V4L2_CID_CONTRAST:
  952. ret=i2c_put_byte(client,0x0200, value);
  953. break;
  954. case V4L2_CID_SATURATION:
  955. ret=i2c_put_byte(client,0x0202, value);
  956. break;
  957. #if 0
  958. case V4L2_CID_EXPOSURE:
  959. ret=i2c_put_byte(client,0x0201, value);
  960. break;
  961. #endif
  962. case V4L2_CID_HFLIP: /* set flip on H. */
  963. ret=i2c_get_byte(client,0x0101);
  964. if(ret>0) {
  965. cur_val=(char)ret;
  966. if(value!=0)
  967. cur_val=cur_val|0x1;
  968. else
  969. cur_val=cur_val&0xFE;
  970. ret=i2c_put_byte(client,0x0101,cur_val);
  971. if(ret<0) dprintk(dev, 1, "V4L2_CID_HFLIP setting error\n");
  972. } else {
  973. dprintk(dev, 1, "vertical read error\n");
  974. }
  975. break;
  976. case V4L2_CID_VFLIP: /* set flip on V. */
  977. ret=i2c_get_byte(client,0x0101);
  978. if(ret>0) {
  979. cur_val=(char)ret;
  980. if(value!=0)
  981. cur_val=cur_val|0x10;
  982. else
  983. cur_val=cur_val&0xFD;
  984. ret=i2c_put_byte(client,0x0101,cur_val);
  985. } else {
  986. dprintk(dev, 1, "vertical read error\n");
  987. }
  988. break;
  989. case V4L2_CID_DO_WHITE_BALANCE:
  990. if(ov7675_qctrl[0].default_value!=value){
  991. ov7675_qctrl[0].default_value=value;
  992. set_OV7675_param_wb(dev,value);
  993. printk(KERN_INFO " set camera white_balance=%d. \n ",value);
  994. }
  995. break;
  996. case V4L2_CID_EXPOSURE:
  997. if(ov7675_qctrl[1].default_value!=value){
  998. ov7675_qctrl[1].default_value=value;
  999. set_OV7675_param_exposure(dev,value);
  1000. printk(KERN_INFO " set camera exposure=%d. \n ",value);
  1001. }
  1002. break;
  1003. case V4L2_CID_COLORFX:
  1004. if(ov7675_qctrl[2].default_value!=value){
  1005. ov7675_qctrl[2].default_value=value;
  1006. set_OV7675_param_effect(dev,value);
  1007. printk(KERN_INFO " set camera effect=%d. \n ",value);
  1008. }
  1009. break;
  1010. case V4L2_CID_WHITENESS:
  1011. if(ov7675_qctrl[3].default_value!=value){
  1012. ov7675_qctrl[3].default_value=value;
  1013. OV7675_set_param_banding(dev,value);
  1014. printk(KERN_INFO " set camera banding=%d. \n ",value);
  1015. }
  1016. break;
  1017. default:
  1018. ret=-1;
  1019. break;
  1020. }
  1021. return ret;
  1022. }
  1023. static void power_down_ov7675(struct ov7675_device *dev)
  1024. {
  1025. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  1026. unsigned char buf[4];
  1027. buf[0]=0x12;
  1028. buf[1]=0x80;
  1029. i2c_put_byte_add8(client,buf,2);
  1030. msleep(5);
  1031. buf[0]=0xb8;
  1032. buf[1]=0x12;
  1033. i2c_put_byte_add8(client,buf,2);
  1034. msleep(1);
  1035. return;
  1036. }
  1037. /* ------------------------------------------------------------------
  1038. DMA and thread functions
  1039. ------------------------------------------------------------------*/
  1040. #define TSTAMP_MIN_Y 24
  1041. #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
  1042. #define TSTAMP_INPUT_X 10
  1043. #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
  1044. static void ov7675_fillbuff(struct ov7675_fh *fh, struct ov7675_buffer *buf)
  1045. {
  1046. struct ov7675_device *dev = fh->dev;
  1047. void *vbuf = videobuf_to_vmalloc(&buf->vb);
  1048. dprintk(dev,1,"%s\n", __func__);
  1049. if (!vbuf)
  1050. return;
  1051. /* 0x18221223 indicate the memory type is MAGIC_VMAL_MEM*/
  1052. vm_fill_buffer(&buf->vb,fh->fmt->fourcc ,0x18221223,vbuf);
  1053. buf->vb.state = VIDEOBUF_DONE;
  1054. }
  1055. static void ov7675_thread_tick(struct ov7675_fh *fh)
  1056. {
  1057. struct ov7675_buffer *buf;
  1058. struct ov7675_device *dev = fh->dev;
  1059. struct ov7675_dmaqueue *dma_q = &dev->vidq;
  1060. unsigned long flags = 0;
  1061. dprintk(dev, 1, "Thread tick\n");
  1062. spin_lock_irqsave(&dev->slock, flags);
  1063. if (list_empty(&dma_q->active)) {
  1064. dprintk(dev, 1, "No active queue to serve\n");
  1065. goto unlock;
  1066. }
  1067. buf = list_entry(dma_q->active.next,
  1068. struct ov7675_buffer, vb.queue);
  1069. dprintk(dev, 1, "%s\n", __func__);
  1070. dprintk(dev, 1, "list entry get buf is %x\n",(unsigned)buf);
  1071. /* Nobody is waiting on this buffer, return */
  1072. if (!waitqueue_active(&buf->vb.done))
  1073. goto unlock;
  1074. list_del(&buf->vb.queue);
  1075. do_gettimeofday(&buf->vb.ts);
  1076. /* Fill buffer */
  1077. spin_unlock_irqrestore(&dev->slock, flags);
  1078. ov7675_fillbuff(fh, buf);
  1079. dprintk(dev, 1, "filled buffer %p\n", buf);
  1080. wake_up(&buf->vb.done);
  1081. dprintk(dev, 2, "[%p/%d] wakeup\n", buf, buf->vb. i);
  1082. return;
  1083. unlock:
  1084. spin_unlock_irqrestore(&dev->slock, flags);
  1085. return;
  1086. }
  1087. #define frames_to_ms(frames) \
  1088. ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
  1089. static void ov7675_sleep(struct ov7675_fh *fh)
  1090. {
  1091. struct ov7675_device *dev = fh->dev;
  1092. struct ov7675_dmaqueue *dma_q = &dev->vidq;
  1093. DECLARE_WAITQUEUE(wait, current);
  1094. dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__,
  1095. (unsigned long)dma_q);
  1096. add_wait_queue(&dma_q->wq, &wait);
  1097. if (kthread_should_stop())
  1098. goto stop_task;
  1099. /* Calculate time to wake up */
  1100. //timeout = msecs_to_jiffies(frames_to_ms(1));
  1101. ov7675_thread_tick(fh);
  1102. schedule_timeout_interruptible(2);
  1103. stop_task:
  1104. remove_wait_queue(&dma_q->wq, &wait);
  1105. try_to_freeze();
  1106. }
  1107. static int ov7675_thread(void *data)
  1108. {
  1109. struct ov7675_fh *fh = data;
  1110. struct ov7675_device *dev = fh->dev;
  1111. dprintk(dev, 1, "thread started\n");
  1112. set_freezable();
  1113. for (;;) {
  1114. ov7675_sleep(fh);
  1115. if (kthread_should_stop())
  1116. break;
  1117. }
  1118. dprintk(dev, 1, "thread: exit\n");
  1119. return 0;
  1120. }
  1121. static int ov7675_start_thread(struct ov7675_fh *fh)
  1122. {
  1123. struct ov7675_device *dev = fh->dev;
  1124. struct ov7675_dmaqueue *dma_q = &dev->vidq;
  1125. dma_q->frame = 0;
  1126. dma_q->ini_jiffies = jiffies;
  1127. dprintk(dev, 1, "%s\n", __func__);
  1128. dma_q->kthread = kthread_run(ov7675_thread, fh, "ov7675");
  1129. if (IS_ERR(dma_q->kthread)) {
  1130. v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
  1131. return PTR_ERR(dma_q->kthread);
  1132. }
  1133. /* Wakes thread */
  1134. wake_up_interruptible(&dma_q->wq);
  1135. dprintk(dev, 1, "returning from %s\n", __func__);
  1136. return 0;
  1137. }
  1138. static void ov7675_stop_thread(struct ov7675_dmaqueue *dma_q)
  1139. {
  1140. struct ov7675_device *dev = container_of(dma_q, struct ov7675_device, vidq);
  1141. dprintk(dev, 1, "%s\n", __func__);
  1142. /* shutdown control thread */
  1143. if (dma_q->kthread) {
  1144. kthread_stop(dma_q->kthread);
  1145. dma_q->kthread = NULL;
  1146. }
  1147. }
  1148. /* ------------------------------------------------------------------
  1149. Videobuf operations
  1150. ------------------------------------------------------------------*/
  1151. static int
  1152. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  1153. {
  1154. struct ov7675_fh *fh = vq->priv_data;
  1155. struct ov7675_device *dev = fh->dev;
  1156. //int bytes = fh->fmt->depth >> 3 ;
  1157. *size = fh->width*fh->height*fh->fmt->depth >> 3;
  1158. if (0 == *count)
  1159. *count = 32;
  1160. while (*size * *count > vid_limit * 1024 * 1024)
  1161. (*count)--;
  1162. dprintk(dev, 1, "%s, count=%d, size=%d\n", __func__,
  1163. *count, *size);
  1164. return 0;
  1165. }
  1166. static void free_buffer(struct videobuf_queue *vq, struct ov7675_buffer *buf)
  1167. {
  1168. struct ov7675_fh *fh = vq->priv_data;
  1169. struct ov7675_device *dev = fh->dev;
  1170. dprintk(dev, 1, "%s, state: %i\n", __func__, buf->vb.state);
  1171. if (in_interrupt())
  1172. BUG();
  1173. videobuf_vmalloc_free(&buf->vb);
  1174. dprintk(dev, 1, "free_buffer: freed\n");
  1175. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  1176. }
  1177. #define norm_maxw() 1024
  1178. #define norm_maxh() 768
  1179. static int
  1180. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  1181. enum v4l2_field field)
  1182. {
  1183. struct ov7675_fh *fh = vq->priv_data;
  1184. struct ov7675_device *dev = fh->dev;
  1185. struct ov7675_buffer *buf = container_of(vb, struct ov7675_buffer, vb);
  1186. int rc;
  1187. //int bytes = fh->fmt->depth >> 3 ;
  1188. dprintk(dev, 1, "%s, field=%d\n", __func__, field);
  1189. BUG_ON(NULL == fh->fmt);
  1190. if (fh->width < 48 || fh->width > norm_maxw() ||
  1191. fh->height < 32 || fh->height > norm_maxh())
  1192. return -EINVAL;
  1193. buf->vb.size = fh->width*fh->height*fh->fmt->depth >> 3;
  1194. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  1195. return -EINVAL;
  1196. /* These properties only change when queue is idle, see s_fmt */
  1197. buf->fmt = fh->fmt;
  1198. buf->vb.width = fh->width;
  1199. buf->vb.height = fh->height;
  1200. buf->vb.field = field;
  1201. //precalculate_bars(fh);
  1202. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  1203. rc = videobuf_iolock(vq, &buf->vb, NULL);
  1204. if (rc < 0)
  1205. goto fail;
  1206. }
  1207. buf->vb.state = VIDEOBUF_PREPARED;
  1208. return 0;
  1209. fail:
  1210. free_buffer(vq, buf);
  1211. return rc;
  1212. }
  1213. static void
  1214. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  1215. {
  1216. struct ov7675_buffer *buf = container_of(vb, struct ov7675_buffer, vb);
  1217. struct ov7675_fh *fh = vq->priv_data;
  1218. struct ov7675_device *dev = fh->dev;
  1219. struct ov7675_dmaqueue *vidq = &dev->vidq;
  1220. dprintk(dev, 1, "%s\n", __func__);
  1221. buf->vb.state = VIDEOBUF_QUEUED;
  1222. list_add_tail(&buf->vb.queue, &vidq->active);
  1223. }
  1224. static void buffer_release(struct videobuf_queue *vq,
  1225. struct videobuf_buffer *vb)
  1226. {
  1227. struct ov7675_buffer *buf = container_of(vb, struct ov7675_buffer, vb);
  1228. struct ov7675_fh *fh = vq->priv_data;
  1229. struct ov7675_device *dev = (struct ov7675_device *)fh->dev;
  1230. dprintk(dev, 1, "%s\n", __func__);
  1231. free_buffer(vq, buf);
  1232. }
  1233. static struct videobuf_queue_ops ov7675_video_qops = {
  1234. .buf_setup = buffer_setup,
  1235. .buf_prepare = buffer_prepare,
  1236. .buf_queue = buffer_queue,
  1237. .buf_release = buffer_release,
  1238. };
  1239. /* ------------------------------------------------------------------
  1240. IOCTL vidioc handling
  1241. ------------------------------------------------------------------*/
  1242. static int vidioc_querycap(struct file *file, void *priv,
  1243. struct v4l2_capability *cap)
  1244. {
  1245. struct ov7675_fh *fh = priv;
  1246. struct ov7675_device *dev = fh->dev;
  1247. strcpy(cap->driver, "ov7675");
  1248. strcpy(cap->card, "ov7675");
  1249. strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
  1250. cap->version = OV7675_CAMERA_VERSION;
  1251. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  1252. V4L2_CAP_STREAMING |
  1253. V4L2_CAP_READWRITE;
  1254. return 0;
  1255. }
  1256. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1257. struct v4l2_fmtdesc *f)
  1258. {
  1259. struct ov7675_fmt *fmt;
  1260. if (f->index >= ARRAY_SIZE(formats))
  1261. return -EINVAL;
  1262. fmt = &formats[f->index];
  1263. strlcpy(f->description, fmt->name, sizeof(f->description));
  1264. f->pixelformat = fmt->fourcc;
  1265. return 0;
  1266. }
  1267. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  1268. struct v4l2_format *f)
  1269. {
  1270. struct ov7675_fh *fh = priv;
  1271. f->fmt.pix.width = fh->width;
  1272. f->fmt.pix.height = fh->height;
  1273. f->fmt.pix.field = fh->vb_vidq.field;
  1274. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  1275. f->fmt.pix.bytesperline =
  1276. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  1277. f->fmt.pix.sizeimage =
  1278. f->fmt.pix.height * f->fmt.pix.bytesperline;
  1279. return (0);
  1280. }
  1281. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  1282. struct v4l2_format *f)
  1283. {
  1284. struct ov7675_fh *fh = priv;
  1285. struct ov7675_device *dev = fh->dev;
  1286. struct ov7675_fmt *fmt;
  1287. enum v4l2_field field;
  1288. unsigned int maxw, maxh;
  1289. fmt = get_format(f);
  1290. if (!fmt) {
  1291. dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n",
  1292. f->fmt.pix.pixelformat);
  1293. return -EINVAL;
  1294. }
  1295. field = f->fmt.pix.field;
  1296. if (field == V4L2_FIELD_ANY) {
  1297. field = V4L2_FIELD_INTERLACED;
  1298. } else if (V4L2_FIELD_INTERLACED != field) {
  1299. dprintk(dev, 1, "Field type invalid.\n");
  1300. return -EINVAL;
  1301. }
  1302. maxw = norm_maxw();
  1303. maxh = norm_maxh();
  1304. f->fmt.pix.field = field;
  1305. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  1306. &f->fmt.pix.height, 32, maxh, 0, 0);
  1307. f->fmt.pix.bytesperline =
  1308. (f->fmt.pix.width * fmt->depth) >> 3;
  1309. f->fmt.pix.sizeimage =
  1310. f->fmt.pix.height * f->fmt.pix.bytesperline;
  1311. return 0;
  1312. }
  1313. /*FIXME: This seems to be generic enough to be at videodev2 */
  1314. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1315. struct v4l2_format *f)
  1316. {
  1317. struct ov7675_fh *fh = priv;
  1318. struct videobuf_queue *q = &fh->vb_vidq;
  1319. int ret = vidioc_try_fmt_vid_cap(file, fh, f);
  1320. if (ret < 0)
  1321. return ret;
  1322. mutex_lock(&q->vb_lock);
  1323. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  1324. dprintk(fh->dev, 1, "%s queue busy\n", __func__);
  1325. ret = -EBUSY;
  1326. goto out;
  1327. }
  1328. fh->fmt = get_format(f);
  1329. fh->width = f->fmt.pix.width;
  1330. fh->height = f->fmt.pix.height;
  1331. fh->vb_vidq.field = f->fmt.pix.field;
  1332. fh->type = f->type;
  1333. ret = 0;
  1334. out:
  1335. mutex_unlock(&q->vb_lock);
  1336. return ret;
  1337. }
  1338. static int vidioc_reqbufs(struct file *file, void *priv,
  1339. struct v4l2_requestbuffers *p)
  1340. {
  1341. struct ov7675_fh *fh = priv;
  1342. return (videobuf_reqbufs(&fh->vb_vidq, p));
  1343. }
  1344. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  1345. {
  1346. struct ov7675_fh *fh = priv;
  1347. return (videobuf_querybuf(&fh->vb_vidq, p));
  1348. }
  1349. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  1350. {
  1351. struct ov7675_fh *fh = priv;
  1352. return (videobuf_qbuf(&fh->vb_vidq, p));
  1353. }
  1354. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  1355. {
  1356. struct ov7675_fh *fh = priv;
  1357. return (videobuf_dqbuf(&fh->vb_vidq, p,
  1358. file->f_flags & O_NONBLOCK));
  1359. }
  1360. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1361. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1362. {
  1363. struct ov7675_fh *fh = priv;
  1364. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1365. }
  1366. #endif
  1367. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  1368. {
  1369. struct ov7675_fh *fh = priv;
  1370. tvin_parm_t para;
  1371. int ret = 0 ;
  1372. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1373. return -EINVAL;
  1374. if (i != fh->type)
  1375. return -EINVAL;
  1376. para.port = TVIN_PORT_CAMERA;
  1377. para.fmt_info.fmt = TVIN_SIG_FMT_MAX+1;//TVIN_SIG_FMT_MAX+1;TVIN_SIG_FMT_CAMERA_1280X720P_30Hz
  1378. para.fmt_info.frame_rate = 150;
  1379. para.fmt_info.h_active = 640;
  1380. para.fmt_info.v_active = 480;
  1381. para.fmt_info.hsync_phase = 1;
  1382. para.fmt_info.vsync_phase = 0;
  1383. ret = videobuf_streamon(&fh->vb_vidq);
  1384. if(ret == 0){
  1385. start_tvin_service(0,&para);
  1386. fh->stream_on = 1;
  1387. }
  1388. return ret;
  1389. }
  1390. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1391. {
  1392. struct ov7675_fh *fh = priv;
  1393. int ret = 0 ;
  1394. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1395. return -EINVAL;
  1396. if (i != fh->type)
  1397. return -EINVAL;
  1398. ret = videobuf_streamoff(&fh->vb_vidq);
  1399. if(ret == 0 ){
  1400. stop_tvin_service(0);
  1401. fh->stream_on = 0;
  1402. }
  1403. return ret;
  1404. }
  1405. static int vidioc_enum_framesizes(struct file *file, void *fh,struct v4l2_frmsizeenum *fsize)
  1406. {
  1407. int ret = 0,i=0;
  1408. struct ov7675_fmt *fmt = NULL;
  1409. struct v4l2_frmsize_discrete *frmsize = NULL;
  1410. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  1411. if (formats[i].fourcc == fsize->pixel_format){
  1412. fmt = &formats[i];
  1413. break;
  1414. }
  1415. }
  1416. if (fmt == NULL)
  1417. return -EINVAL;
  1418. if (fmt->fourcc == V4L2_PIX_FMT_NV21){
  1419. if (fsize->index >= ARRAY_SIZE(ov7675_prev_resolution))
  1420. return -EINVAL;
  1421. frmsize = &ov7675_prev_resolution[fsize->index];
  1422. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1423. fsize->discrete.width = frmsize->width;
  1424. fsize->discrete.height = frmsize->height;
  1425. }
  1426. else if(fmt->fourcc == V4L2_PIX_FMT_RGB24){
  1427. if (fsize->index >= ARRAY_SIZE(ov7675_pic_resolution))
  1428. return -EINVAL;
  1429. frmsize = &ov7675_pic_resolution[fsize->index];
  1430. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1431. fsize->discrete.width = frmsize->width;
  1432. fsize->discrete.height = frmsize->height;
  1433. }
  1434. return ret;
  1435. }
  1436. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
  1437. {
  1438. return 0;
  1439. }
  1440. /* only one input in this sample driver */
  1441. static int vidioc_enum_input(struct file *file, void *priv,
  1442. struct v4l2_input *inp)
  1443. {
  1444. //if (inp->index >= NUM_INPUTS)
  1445. //return -EINVAL;
  1446. inp->type = V4L2_INPUT_TYPE_CAMERA;
  1447. inp->std = V4L2_STD_525_60;
  1448. sprintf(inp->name, "Camera %u", inp->index);
  1449. return (0);
  1450. }
  1451. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1452. {
  1453. struct ov7675_fh *fh = priv;
  1454. struct ov7675_device *dev = fh->dev;
  1455. *i = dev->input;
  1456. return (0);
  1457. }
  1458. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1459. {
  1460. struct ov7675_fh *fh = priv;
  1461. struct ov7675_device *dev = fh->dev;
  1462. //if (i >= NUM_INPUTS)
  1463. //return -EINVAL;
  1464. dev->input = i;
  1465. //precalculate_bars(fh);
  1466. return (0);
  1467. }
  1468. /* --- controls ---------------------------------------------- */
  1469. static int vidioc_queryctrl(struct file *file, void *priv,
  1470. struct v4l2_queryctrl *qc)
  1471. {
  1472. int i;
  1473. for (i = 0; i < ARRAY_SIZE(ov7675_qctrl); i++)
  1474. if (qc->id && qc->id == ov7675_qctrl[i].id) {
  1475. memcpy(qc, &(ov7675_qctrl[i]),
  1476. sizeof(*qc));
  1477. return (0);
  1478. }
  1479. return -EINVAL;
  1480. }
  1481. static int vidioc_g_ctrl(struct file *file, void *priv,
  1482. struct v4l2_control *ctrl)
  1483. {
  1484. struct ov7675_fh *fh = priv;
  1485. struct ov7675_device *dev = fh->dev;
  1486. int i;
  1487. for (i = 0; i < ARRAY_SIZE(ov7675_qctrl); i++)
  1488. if (ctrl->id == ov7675_qctrl[i].id) {
  1489. ctrl->value = dev->qctl_regs[i];
  1490. return 0;
  1491. }
  1492. return -EINVAL;
  1493. }
  1494. static int vidioc_s_ctrl(struct file *file, void *priv,
  1495. struct v4l2_control *ctrl)
  1496. {
  1497. struct ov7675_fh *fh = priv;
  1498. struct ov7675_device *dev = fh->dev;
  1499. int i;
  1500. for (i = 0; i < ARRAY_SIZE(ov7675_qctrl); i++)
  1501. if (ctrl->id == ov7675_qctrl[i].id) {
  1502. if (ctrl->value < ov7675_qctrl[i].minimum ||
  1503. ctrl->value > ov7675_qctrl[i].maximum ||
  1504. ov7675_setting(dev,ctrl->id,ctrl->value)<0) {
  1505. return -ERANGE;
  1506. }
  1507. dev->qctl_regs[i] = ctrl->value;
  1508. return 0;
  1509. }
  1510. return -EINVAL;
  1511. }
  1512. /* ------------------------------------------------------------------
  1513. File operations for the device
  1514. ------------------------------------------------------------------*/
  1515. static int ov7675_open(struct file *file)
  1516. {
  1517. struct ov7675_device *dev = video_drvdata(file);
  1518. struct ov7675_fh *fh = NULL;
  1519. int retval = 0;
  1520. ov7675_have_opened=1;
  1521. #ifdef CONFIG_ARCH_MESON6
  1522. switch_mod_gate_by_name("ge2d", 1);
  1523. #endif
  1524. if(dev->platform_dev_data.device_init) {
  1525. dev->platform_dev_data.device_init();
  1526. printk("+++found a init function, and run it..\n");
  1527. }
  1528. OV7675_init_regs(dev);
  1529. msleep(40);
  1530. mutex_lock(&dev->mutex);
  1531. dev->users++;
  1532. if (dev->users > 1) {
  1533. dev->users--;
  1534. mutex_unlock(&dev->mutex);
  1535. return -EBUSY;
  1536. }
  1537. dprintk(dev, 1, "open %s type=%s users=%d\n",
  1538. video_device_node_name(dev->vdev),
  1539. v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
  1540. /* init video dma queues */
  1541. INIT_LIST_HEAD(&dev->vidq.active);
  1542. init_waitqueue_head(&dev->vidq.wq);
  1543. spin_lock_init(&dev->slock);
  1544. /* allocate + initialize per filehandle data */
  1545. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  1546. if (NULL == fh) {
  1547. dev->users--;
  1548. retval = -ENOMEM;
  1549. }
  1550. mutex_unlock(&dev->mutex);
  1551. if (retval)
  1552. return retval;
  1553. file->private_data = fh;
  1554. fh->dev = dev;
  1555. fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1556. fh->fmt = &formats[0];
  1557. fh->width = 640;
  1558. fh->height = 480;
  1559. fh->stream_on = 0 ;
  1560. /* Resets frame counters */
  1561. dev->jiffies = jiffies;
  1562. // TVIN_SIG_FMT_CAMERA_640X480P_30Hz,
  1563. // TVIN_SIG_FMT_CAMERA_800X600P_30Hz,
  1564. // TVIN_SIG_FMT_CAMERA_1024X768P_30Hz, // 190
  1565. // TVIN_SIG_FMT_CAMERA_1920X1080P_30Hz,
  1566. // TVIN_SIG_FMT_CAMERA_1280X720P_30Hz,
  1567. videobuf_queue_vmalloc_init(&fh->vb_vidq, &ov7675_video_qops,
  1568. NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
  1569. sizeof(struct ov7675_buffer), fh,NULL);
  1570. ov7675_start_thread(fh);
  1571. return 0;
  1572. }
  1573. static ssize_t
  1574. ov7675_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  1575. {
  1576. struct ov7675_fh *fh = file->private_data;
  1577. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1578. return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
  1579. file->f_flags & O_NONBLOCK);
  1580. }
  1581. return 0;
  1582. }
  1583. static unsigned int
  1584. ov7675_poll(struct file *file, struct poll_table_struct *wait)
  1585. {
  1586. struct ov7675_fh *fh = file->private_data;
  1587. struct ov7675_device *dev = fh->dev;
  1588. struct videobuf_queue *q = &fh->vb_vidq;
  1589. dprintk(dev, 1, "%s\n", __func__);
  1590. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1591. return POLLERR;
  1592. return videobuf_poll_stream(file, q, wait);
  1593. }
  1594. static int ov7675_close(struct file *file)
  1595. {
  1596. struct ov7675_fh *fh = file->private_data;
  1597. struct ov7675_device *dev = fh->dev;
  1598. struct ov7675_dmaqueue *vidq = &dev->vidq;
  1599. struct video_device *vdev = video_devdata(file);
  1600. ov7675_have_opened=0;
  1601. ov7675_stop_thread(vidq);
  1602. videobuf_stop(&fh->vb_vidq);
  1603. if(fh->stream_on){
  1604. stop_tvin_service(0);
  1605. }
  1606. videobuf_mmap_free(&fh->vb_vidq);
  1607. kfree(fh);
  1608. mutex_lock(&dev->mutex);
  1609. dev->users--;
  1610. mutex_unlock(&dev->mutex);
  1611. dprintk(dev, 1, "close called (dev=%s, users=%d)\n",
  1612. video_device_node_name(vdev), dev->users);
  1613. #if 1
  1614. ov7675_qctrl[0].default_value=0;
  1615. ov7675_qctrl[1].default_value=4;
  1616. ov7675_qctrl[2].default_value=0;
  1617. ov7675_qctrl[3].default_value=0;
  1618. power_down_ov7675(dev);
  1619. #endif
  1620. if(dev->platform_dev_data.device_uninit) {
  1621. dev->platform_dev_data.device_uninit();
  1622. printk("+++found a uninit function, and run it..\n");
  1623. }
  1624. #ifdef CONFIG_ARCH_MESON6
  1625. switch_mod_gate_by_name("ge2d", 0);
  1626. #endif
  1627. return 0;
  1628. }
  1629. static int ov7675_mmap(struct file *file, struct vm_area_struct *vma)
  1630. {
  1631. struct ov7675_fh *fh = file->private_data;
  1632. struct ov7675_device *dev = fh->dev;
  1633. int ret;
  1634. dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  1635. ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1636. dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
  1637. (unsigned long)vma->vm_start,
  1638. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1639. ret);
  1640. return ret;
  1641. }
  1642. static const struct v4l2_file_operations ov7675_fops = {
  1643. .owner = THIS_MODULE,
  1644. .open = ov7675_open,
  1645. .release = ov7675_close,
  1646. .read = ov7675_read,
  1647. .poll = ov7675_poll,
  1648. .ioctl = video_ioctl2, /* V4L2 ioctl handler */
  1649. .mmap = ov7675_mmap,
  1650. };
  1651. static const struct v4l2_ioctl_ops ov7675_ioctl_ops = {
  1652. .vidioc_querycap = vidioc_querycap,
  1653. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1654. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1655. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1656. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1657. .vidioc_reqbufs = vidioc_reqbufs,
  1658. .vidioc_querybuf = vidioc_querybuf,
  1659. .vidioc_qbuf = vidioc_qbuf,
  1660. .vidioc_dqbuf = vidioc_dqbuf,
  1661. .vidioc_s_std = vidioc_s_std,
  1662. .vidioc_enum_input = vidioc_enum_input,
  1663. .vidioc_g_input = vidioc_g_input,
  1664. .vidioc_s_input = vidioc_s_input,
  1665. .vidioc_queryctrl = vidioc_queryctrl,
  1666. .vidioc_g_ctrl = vidioc_g_ctrl,
  1667. .vidioc_s_ctrl = vidioc_s_ctrl,
  1668. .vidioc_streamon = vidioc_streamon,
  1669. .vidioc_streamoff = vidioc_streamoff,
  1670. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1671. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1672. .vidiocgmbuf = vidiocgmbuf,
  1673. #endif
  1674. };
  1675. static struct video_device ov7675_template = {
  1676. .name = "ov7675_v4l",
  1677. .fops = &ov7675_fops,
  1678. .ioctl_ops = &ov7675_ioctl_ops,
  1679. .release = video_device_release,
  1680. .tvnorms = V4L2_STD_525_60,
  1681. .current_norm = V4L2_STD_NTSC_M,
  1682. };
  1683. static int ov7675_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
  1684. {
  1685. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1686. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_OV7675, 0);
  1687. }
  1688. static const struct v4l2_subdev_core_ops ov7675_core_ops = {
  1689. .g_chip_ident = ov7675_g_chip_ident,
  1690. };
  1691. static const struct v4l2_subdev_ops ov7675_ops = {
  1692. .core = &ov7675_core_ops,
  1693. };
  1694. #ifdef CONFIG_HAS_EARLYSUSPEND
  1695. static void aml_ov7675_early_suspend(struct early_suspend *h)
  1696. {
  1697. printk("enter -----> %s \n",__FUNCTION__);
  1698. if(h && h->param) {
  1699. aml_plat_cam_data_t* plat_dat= (aml_plat_cam_data_t*)h->param;
  1700. if (plat_dat && plat_dat->early_suspend) {
  1701. plat_dat->early_suspend();
  1702. }
  1703. }
  1704. }
  1705. static void aml_ov7675_late_resume(struct early_suspend *h)
  1706. {
  1707. if(ov7675_have_opened==0){
  1708. printk("enter -----> %s \n",__FUNCTION__);
  1709. if(h && h->param) {
  1710. aml_plat_cam_data_t* plat_dat= (aml_plat_cam_data_t*)h->param;
  1711. if (plat_dat && plat_dat->late_resume) {
  1712. plat_dat->late_resume();
  1713. }
  1714. unsigned char buf[4];
  1715. buf[0]=0x12;
  1716. buf[1]=0x80;
  1717. i2c_put_byte_add8(this_client,buf,2);
  1718. msleep(5);
  1719. buf[0]=0xb8;
  1720. buf[1]=0x12;
  1721. i2c_put_byte_add8(this_client,buf,2);
  1722. msleep(1);
  1723. if (plat_dat && plat_dat->device_uninit) {
  1724. plat_dat->device_uninit();
  1725. }
  1726. }
  1727. }
  1728. }
  1729. #endif
  1730. static int ov7675_probe(struct i2c_client *client,
  1731. const struct i2c_device_id *id)
  1732. {
  1733. aml_plat_cam_data_t* plat_dat;
  1734. int err;
  1735. struct ov7675_device *t;
  1736. struct v4l2_subdev *sd;
  1737. v4l_info(client, "chip found @ 0x%x (%s)\n",
  1738. client->addr << 1, client->adapter->name);
  1739. t = kzalloc(sizeof(*t), GFP_KERNEL);
  1740. if (t == NULL)
  1741. return -ENOMEM;
  1742. sd = &t->sd;
  1743. v4l2_i2c_subdev_init(sd, client, &ov7675_ops);
  1744. mutex_init(&t->mutex);
  1745. this_client=client;
  1746. /* Now create a video4linux device */
  1747. t->vdev = video_device_alloc();
  1748. if (t->vdev == NULL) {
  1749. kfree(t);
  1750. kfree(client);
  1751. return -ENOMEM;
  1752. }
  1753. memcpy(t->vdev, &ov7675_template, sizeof(*t->vdev));
  1754. video_set_drvdata(t->vdev, t);
  1755. /* Register it */
  1756. plat_dat= (aml_plat_cam_data_t*)client->dev.platform_data;
  1757. if (plat_dat) {
  1758. t->platform_dev_data.device_init=plat_dat->device_init;
  1759. t->platform_dev_data.device_uninit=plat_dat->device_uninit;
  1760. t->platform_dev_data.device_probe=plat_dat->device_probe;
  1761. if(plat_dat->video_nr>=0) video_nr=plat_dat->video_nr;
  1762. if(t->platform_dev_data.device_probe) {
  1763. t->platform_dev_data.device_probe();
  1764. printk("+++found a device_probe function, and run it..\n");
  1765. }
  1766. power_down_ov7675(t);
  1767. if(t->platform_dev_data.device_uninit) {
  1768. t->platform_dev_data.device_uninit();
  1769. printk("+++found a uninit function, and run it..\n");
  1770. }
  1771. }
  1772. err = video_register_device(t->vdev, VFL_TYPE_GRABBER, video_nr);
  1773. if (err < 0) {
  1774. video_device_release(t->vdev);
  1775. kfree(t);
  1776. return err;
  1777. }
  1778. #ifdef CONFIG_HAS_EARLYSUSPEND
  1779. ov7675_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN;
  1780. ov7675_early_suspend.suspend = aml_ov7675_early_suspend;
  1781. ov7675_early_suspend.resume = aml_ov7675_late_resume;
  1782. ov7675_early_suspend.param = plat_dat;
  1783. register_early_suspend(&ov7675_early_suspend);
  1784. #endif
  1785. return 0;
  1786. }
  1787. static int ov7675_remove(struct i2c_client *client)
  1788. {
  1789. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1790. struct ov7675_device *t = to_dev(sd);
  1791. video_unregister_device(t->vdev);
  1792. v4l2_device_unregister_subdev(sd);
  1793. kfree(t);
  1794. return 0;
  1795. }
  1796. static int ov7675_suspend(struct i2c_client *client, pm_message_t state)
  1797. {
  1798. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1799. struct ov7675_device *t = to_dev(sd);
  1800. struct ov7675_fh *fh = to_fh(t);
  1801. if(fh->stream_on == 1){
  1802. stop_tvin_service(0);
  1803. }
  1804. power_down_ov7675(t);
  1805. return 0;
  1806. }
  1807. static int ov7675_resume(struct i2c_client *client)
  1808. {
  1809. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1810. struct ov7675_device *t = to_dev(sd);
  1811. struct ov7675_fh *fh = to_fh(t);
  1812. tvin_parm_t para;
  1813. para.port = TVIN_PORT_CAMERA;
  1814. para.fmt_info.fmt = TVIN_SIG_FMT_MAX+1;
  1815. OV7675_init_regs(t);
  1816. if(fh->stream_on == 1){
  1817. start_tvin_service(0,&para);
  1818. }
  1819. return 0;
  1820. }
  1821. static const struct i2c_device_id ov7675_id[] = {
  1822. { "ov7675_i2c", 0 },
  1823. { }
  1824. };
  1825. MODULE_DEVICE_TABLE(i2c, ov7675_id);
  1826. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  1827. .name = "ov7675",
  1828. .probe = ov7675_probe,
  1829. .remove = ov7675_remove,
  1830. .suspend = ov7675_suspend,
  1831. .resume = ov7675_resume,
  1832. .id_table = ov7675_id,
  1833. };