ov3660.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691
  1. /*
  2. *OV3660 - 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/am_eth_pinmux.h>
  40. #include <mach/pinmux.h>
  41. #include <media/amlogic/656in.h>
  42. #include "common/plat_ctrl.h"
  43. #include "common/vmapi.h"
  44. #include <mach/mod_gate.h>
  45. #ifdef CONFIG_HAS_EARLYSUSPEND
  46. #include <linux/earlysuspend.h>
  47. static struct early_suspend ov3660_early_suspend;
  48. #endif
  49. #define OV3660_CAMERA_MODULE_NAME "ov3660"
  50. #define TEST_I2C 1
  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 OV3660_CAMERA_MAJOR_VERSION 0
  56. #define OV3660_CAMERA_MINOR_VERSION 7
  57. #define OV3660_CAMERA_RELEASE 0
  58. #define OV3660_CAMERA_VERSION \
  59. KERNEL_VERSION(OV3660_CAMERA_MAJOR_VERSION, OV3660_CAMERA_MINOR_VERSION, OV3660_CAMERA_RELEASE)
  60. MODULE_DESCRIPTION("ov3660 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. //module_param(vid_limit, uint, 0644);
  69. //MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  70. static int ov3660_h_active=800;
  71. static int ov3660_v_active=596;
  72. #define EMDOOR_DEBUG_OV3660 1
  73. static struct i2c_client *ov3660_client;
  74. #ifdef CONFIG_VIDEO_AMLOGIC_FLASHLIGHT
  75. #include <media/amlogic/flashlight.h>
  76. extern aml_plat_flashlight_status_t get_flashlightflag(void);
  77. extern int set_flashlight(bool mode);
  78. #endif
  79. /* supported controls */
  80. static struct v4l2_queryctrl ov3660_qctrl[] = {
  81. {
  82. .id = V4L2_CID_BRIGHTNESS,
  83. .type = V4L2_CTRL_TYPE_INTEGER,
  84. .name = "Brightness",
  85. .minimum = -3,
  86. .maximum = 3,
  87. .step = 1,
  88. .default_value = 0,
  89. .flags = V4L2_CTRL_FLAG_SLIDER,
  90. }, {
  91. .id = V4L2_CID_CONTRAST,
  92. .type = V4L2_CTRL_TYPE_INTEGER,
  93. .name = "Contrast",
  94. .minimum = 0x10,
  95. .maximum = 0x60,
  96. .step = 0xa,
  97. .default_value = 0x30,
  98. .flags = V4L2_CTRL_FLAG_SLIDER,
  99. },{
  100. .id = V4L2_CID_HFLIP,
  101. .type = V4L2_CTRL_TYPE_INTEGER,
  102. .name = "flip on horizontal",
  103. .minimum = 0,
  104. .maximum = 1,
  105. .step = 0x1,
  106. .default_value = 0,
  107. .flags = V4L2_CTRL_FLAG_SLIDER,
  108. } ,{
  109. .id = V4L2_CID_VFLIP,
  110. .type = V4L2_CTRL_TYPE_INTEGER,
  111. .name = "flip on vertical",
  112. .minimum = 0,
  113. .maximum = 1,
  114. .step = 0x1,
  115. .default_value = 0,
  116. .flags = V4L2_CTRL_FLAG_SLIDER,
  117. },{
  118. .id = V4L2_CID_DO_WHITE_BALANCE,
  119. .type = V4L2_CTRL_TYPE_INTEGER,
  120. .name = "white balance",
  121. .minimum = 0,
  122. .maximum = 6,
  123. .step = 0x1,
  124. .default_value = 0,
  125. .flags = V4L2_CTRL_FLAG_SLIDER,
  126. },{
  127. .id = V4L2_CID_EXPOSURE,
  128. .type = V4L2_CTRL_TYPE_INTEGER,
  129. .name = "exposure",
  130. .minimum = 0,
  131. .maximum = 8,
  132. .step = 0x1,
  133. .default_value = 4,
  134. .flags = V4L2_CTRL_FLAG_SLIDER,
  135. },{
  136. .id = V4L2_CID_COLORFX,
  137. .type = V4L2_CTRL_TYPE_INTEGER,
  138. .name = "effect",
  139. .minimum = 0,
  140. .maximum = 6,
  141. .step = 0x1,
  142. .default_value = 0,
  143. .flags = V4L2_CTRL_FLAG_SLIDER,
  144. },{
  145. .id = V4L2_CID_WHITENESS,
  146. .type = V4L2_CTRL_TYPE_INTEGER,
  147. .name = "banding",
  148. .minimum = 0,
  149. .maximum = 1,
  150. .step = 0x1,
  151. .default_value = 0,
  152. .flags = V4L2_CTRL_FLAG_SLIDER,
  153. },/*{
  154. .id = V4L2_CID_SATURATION,
  155. .type = V4L2_CTRL_TYPE_INTEGER,
  156. .name = "Saturation",
  157. .minimum = -2,
  158. .maximum = 2,
  159. .step = 1,
  160. .default_value = 0,
  161. .flags = V4L2_CTRL_FLAG_SLIDER,
  162. }*/
  163. };
  164. #define dprintk(dev, level, fmt, arg...) \
  165. v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
  166. /* ------------------------------------------------------------------
  167. Basic structures
  168. ------------------------------------------------------------------*/
  169. struct ov3660_fmt {
  170. char *name;
  171. u32 fourcc; /* v4l2 format id */
  172. int depth;
  173. };
  174. static struct ov3660_fmt formats[] = {
  175. {
  176. .name = "RGB565 (BE)",
  177. .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
  178. .depth = 16,
  179. },
  180. {
  181. .name = "RGB888 (24)",
  182. .fourcc = V4L2_PIX_FMT_RGB24, /* 24 RGB-8-8-8 */
  183. .depth = 24,
  184. },
  185. {
  186. .name = "BGR888 (24)",
  187. .fourcc = V4L2_PIX_FMT_BGR24, /* 24 BGR-8-8-8 */
  188. .depth = 24,
  189. },
  190. {
  191. .name = "12 Y/CbCr 4:2:0SP",
  192. .fourcc = V4L2_PIX_FMT_NV12,
  193. .depth = 12,
  194. },
  195. {
  196. .name = "12 Y/CbCr 4:2:0SP",
  197. .fourcc = V4L2_PIX_FMT_NV21,
  198. .depth = 12,
  199. },
  200. {
  201. .name = "YUV420P",
  202. .fourcc = V4L2_PIX_FMT_YUV420,
  203. .depth = 12,
  204. }
  205. };
  206. static struct ov3660_fmt *get_format(struct v4l2_format *f)
  207. {
  208. struct ov3660_fmt *fmt;
  209. unsigned int k;
  210. for (k = 0; k < ARRAY_SIZE(formats); k++) {
  211. fmt = &formats[k];
  212. if (fmt->fourcc == f->fmt.pix.pixelformat)
  213. break;
  214. }
  215. if (k == ARRAY_SIZE(formats))
  216. return NULL;
  217. return &formats[k];
  218. }
  219. struct sg_to_addr {
  220. int pos;
  221. struct scatterlist *sg;
  222. };
  223. /* buffer for one video frame */
  224. struct ov3660_buffer {
  225. /* common v4l buffer stuff -- must be first */
  226. struct videobuf_buffer vb;
  227. struct ov3660_fmt *fmt;
  228. };
  229. struct ov3660_dmaqueue {
  230. struct list_head active;
  231. /* thread for generating video stream*/
  232. struct task_struct *kthread;
  233. wait_queue_head_t wq;
  234. /* Counters to control fps rate */
  235. int frame;
  236. int ini_jiffies;
  237. };
  238. static LIST_HEAD(ov3660_devicelist);
  239. struct ov3660_device {
  240. struct list_head ov3660_devicelist;
  241. struct v4l2_subdev sd;
  242. struct v4l2_device v4l2_dev;
  243. spinlock_t slock;
  244. struct mutex mutex;
  245. int users;
  246. /* various device info */
  247. struct video_device *vdev;
  248. struct ov3660_dmaqueue vidq;
  249. /* Several counters */
  250. unsigned long jiffies;
  251. /* Input Number */
  252. int input;
  253. /* platform device data from board initting. */
  254. aml_plat_cam_data_t platform_dev_data;
  255. /* Control 'registers' */
  256. int qctl_regs[ARRAY_SIZE(ov3660_qctrl)];
  257. };
  258. static inline struct ov3660_device *to_dev(struct v4l2_subdev *sd)
  259. {
  260. return container_of(sd, struct ov3660_device, sd);
  261. }
  262. struct ov3660_fh {
  263. struct ov3660_device *dev;
  264. /* video capture */
  265. struct ov3660_fmt *fmt;
  266. unsigned int width, height;
  267. struct videobuf_queue vb_vidq;
  268. enum v4l2_buf_type type;
  269. int input; /* Input Number on bars */
  270. int stream_on;
  271. };
  272. static inline struct ov3660_fh *to_fh(struct ov3660_device *dev)
  273. {
  274. return container_of(dev, struct ov3660_fh, dev);
  275. }
  276. static struct v4l2_frmsize_discrete ov3660_prev_resolution[]= //should include 352x288 and 640x480, those two size are used for recording
  277. {
  278. {320,240},
  279. {352,288},
  280. {640, 480},
  281. };
  282. static struct v4l2_frmsize_discrete ov3660_pic_resolution[]=
  283. {
  284. {2048, 1536},
  285. //{1600,1200},
  286. // {800,600},
  287. };
  288. /* ------------------------------------------------------------------
  289. reg spec of OV3660
  290. ------------------------------------------------------------------*/
  291. struct aml_camera_i2c_fig_s OV3660_script[] = {
  292. //{0x3008, 0x82}, //software reset
  293. //delay 3ms here
  294. //msleep(3);
  295. {0x3103, 0x13},
  296. {0x3008, 0x42}, //software power down
  297. {0x3017, 0xff}, //enable output
  298. {0x3018, 0xff}, //enable output
  299. {0x302c, 0x83}, // Driver 3x, Fsin input enable,Strobe input enable
  300. {0x3032, 0x00},
  301. {0x3901, 0x13},
  302. {0x3704, 0x80},
  303. {0x3717, 0x00},
  304. {0x371b, 0x60},
  305. {0x370b, 0x10},
  306. {0x3624, 0x03},
  307. {0x3622, 0x80},
  308. {0x3614, 0x80},
  309. {0x3630, 0x52},
  310. {0x3632, 0x07},
  311. {0x3633, 0xd2},
  312. {0x3619, 0x75},
  313. {0x371c, 0x00},
  314. {0x370b, 0x12},
  315. {0x3704, 0x80},
  316. {0x3600, 0x08},
  317. {0x3620, 0x43},
  318. {0x3702, 0x20},
  319. {0x3739, 0x48},
  320. {0x3730, 0x20},
  321. {0x370c, 0x0c},
  322. {0x3a18, 0x00}, //gain ceiling 8bit high
  323. {0x3a19, 0xf8}, //gain ceiling 8bit low
  324. {0x3000, 0x10}, //reset for blocks
  325. {0x3004, 0xef}, //clock enable for blocks
  326. //temperature sensor control registers
  327. {0x6700, 0x05},
  328. {0x6701, 0x19},
  329. {0x6702, 0xfd},
  330. {0x6703, 0xd1},
  331. {0x6704, 0xff},
  332. {0x6705, 0xff},
  333. {0x3002, 0x1c},
  334. {0x3006, 0xc3},
  335. {0x3826, 0x23},
  336. {0x3618, 0x00},
  337. {0x3623, 0x00},
  338. {0x4300, 0x31}, //YUV422,YUYV
  339. {0x440e, 0x09},
  340. {0x460b, 0x37},
  341. {0x460c, 0x20},
  342. {0x4713, 0x02},
  343. {0x471c, 0xd0},
  344. {0x5086, 0x00},
  345. {0x5000, 0x07},
  346. {0x5001, 0x03},
  347. {0x5002, 0x00},
  348. {0x501f, 0x00}, //ISP YUV422
  349. {0x302c, 0x03},
  350. {0x3a00, 0x38},
  351. {0x440e, 0x08}, //for MBIST
  352. //{0x3008, 0x02}, //WAKE UP FROM SOFTWARE POWER DOWN MODE
  353. //Reference IQ
  354. //EV
  355. {0x3a0f, 0x38},
  356. {0x3a10, 0x30},
  357. {0x3a1b, 0x38},
  358. {0x3a1e, 0x30},
  359. {0x3a11, 0x70},
  360. {0x3a1f, 0x14},
  361. // awb init
  362. {0x3406, 0x01},
  363. {0x3400, 0x06},
  364. {0x3401, 0x80},
  365. {0x3402, 0x04},
  366. {0x3403, 0x00},
  367. {0x3404, 0x06},
  368. {0x3405, 0x40},
  369. //Awb
  370. {0x5180, 0xff}, // awb
  371. {0x5181, 0xf2},
  372. {0x5182, 0x00},
  373. {0x5183, 0x14},
  374. {0x5184, 0x25},
  375. {0x5185, 0x24},
  376. {0x5186, 0x16},
  377. {0x5187, 0x16},
  378. {0x5188, 0x16},
  379. {0x5189, 0x68},
  380. {0x518a, 0x60},
  381. {0x518b, 0xe0},
  382. {0x518c, 0xb2},
  383. {0x518d, 0x42},
  384. {0x518e, 0x15},
  385. {0x518f, 0x86},
  386. {0x5190, 0x56},
  387. {0x5191, 0xf8},
  388. {0x5192, 0x04},
  389. {0x5193, 0x70},
  390. {0x5194, 0xf0},
  391. {0x5195, 0xf0},
  392. {0x5196, 0x03},
  393. {0x5197, 0x01},
  394. {0x5198, 0x04},
  395. {0x5199, 0x12},
  396. {0x519a, 0x04},
  397. {0x519b, 0x00},
  398. {0x519c, 0x06},
  399. {0x519d, 0x82},
  400. {0x519e, 0x38},
  401. //Color Matrix
  402. {0x5381, 0x1c}, // ccm
  403. {0x5382, 0x5a},
  404. {0x5383, 0x12},
  405. {0x5384, 0x07},
  406. {0x5385, 0x73},
  407. {0x5386, 0x7a},
  408. {0x5387, 0x7a},
  409. {0x5388, 0x5e},
  410. {0x5389, 0x1c},
  411. {0x538a, 0x01},
  412. {0x538b, 0x98},
  413. //CIP
  414. {0x5300, 0x0c},
  415. {0x5301, 0x20},
  416. {0x5302, 0x10},
  417. {0x5303, 0x00},
  418. {0x5304, 0x0c},
  419. {0x5305, 0x20},
  420. {0x5306, 0x0c},
  421. {0x5307, 0x20},
  422. {0x5309, 0x0c},
  423. {0x530a, 0x20},
  424. {0x530b, 0x00},
  425. {0x530c, 0x10},
  426. //Gamma
  427. {0x5481, 0x06}, //gamma
  428. {0x5482, 0x0e},
  429. {0x5483, 0x14},
  430. {0x5484, 0x3e},
  431. {0x5485, 0x4c},
  432. {0x5486, 0x5a},
  433. {0x5487, 0x68},
  434. {0x5488, 0x74},
  435. {0x5489, 0x80},
  436. {0x548a, 0x8d},
  437. {0x548b, 0x9e},
  438. {0x548c, 0xac},
  439. {0x548d, 0xc3},
  440. {0x548e, 0xd5},
  441. {0x548f, 0xe8},
  442. {0x5490, 0x20},
  443. //UV adjust
  444. {0x5001, 0xa7}, //SDE on, scale on, UV average on, color matrix on, AWB on
  445. {0x5580, 0x06},
  446. {0x5583, 0x60},
  447. {0x5584, 0x40},
  448. {0x5589, 0x10},
  449. {0x558a, 0x00},
  450. {0x558b, 0x70},
  451. //Lens Correction
  452. {0x5000, 0xa7}, //Lens on, Gamma on, BPC on, WPC on, interpolation on
  453. {0x5800, 0x0C},
  454. {0x5801, 0x09},
  455. {0x5802, 0x0C},
  456. {0x5803, 0x0C},
  457. {0x5804, 0x0D},
  458. {0x5805, 0x17},
  459. {0x5806, 0x06},
  460. {0x5807, 0x05},
  461. {0x5808, 0x04},
  462. {0x5809, 0x06},
  463. {0x580a, 0x09},
  464. {0x580b, 0x0E},
  465. {0x580c, 0x05},
  466. {0x580d, 0x01},
  467. {0x580e, 0x00},
  468. {0x580f, 0x01},
  469. {0x5810, 0x05},
  470. {0x5811, 0x0D},
  471. {0x5812, 0x05},
  472. {0x5813, 0x01},
  473. {0x5814, 0x00},
  474. {0x5815, 0x01},
  475. {0x5816, 0x05},
  476. {0x5817, 0x0D},
  477. {0x5818, 0x08},
  478. {0x5819, 0x06},
  479. {0x581a, 0x05},
  480. {0x581b, 0x07},
  481. {0x581c, 0x0B},
  482. {0x581d, 0x0D},
  483. {0x581e, 0x12},
  484. {0x581f, 0x0D},
  485. {0x5820, 0x0E},
  486. {0x5821, 0x10},
  487. {0x5822, 0x10},
  488. {0x5823, 0x1E},
  489. {0x5824, 0x53},
  490. {0x5825, 0x15},
  491. {0x5826, 0x05},
  492. {0x5827, 0x14},
  493. {0x5828, 0x54},
  494. {0x5829, 0x25},
  495. {0x582a, 0x33},
  496. {0x582b, 0x33},
  497. {0x582c, 0x34},
  498. {0x582d, 0x16},
  499. {0x582e, 0x24},
  500. {0x582f, 0x41},
  501. {0x5830, 0x50},
  502. {0x5831, 0x42},
  503. {0x5832, 0x15},
  504. {0x5833, 0x25},
  505. {0x5834, 0x34},
  506. {0x5835, 0x33},
  507. {0x5836, 0x24},
  508. {0x5837, 0x26},
  509. {0x5838, 0x54},
  510. {0x5839, 0x25},
  511. {0x583a, 0x15},
  512. {0x583b, 0x25},
  513. {0x583c, 0x53},
  514. {0x583d, 0xCF},
  515. //dns
  516. {0x5308, 0x25},
  517. {0x5304, 0x08},
  518. {0x5305, 0x30},
  519. {0x5306, 0x08},
  520. {0x5307, 0x50},
  521. //Sharpness Auto
  522. {0x5300, 0x08},
  523. {0x5301, 0x18},
  524. {0x5302, 0x30}, //0x18
  525. {0x5303, 0x20}, //0x00
  526. {0x5306, 0x18},
  527. {0x5307, 0x28},
  528. {0x5309, 0x10},
  529. {0x530a, 0x18},
  530. {0x530b, 0x04},
  531. {0x530c, 0x18},
  532. //{0x3820, 0x41},//0x40
  533. //{0x3821, 0x07},//0x04
  534. //{0x4514, 0xbb},
  535. //BANDING
  536. //Under 27M Pclk
  537. {0x3a00, 0x38}, //BIT[5]:1,banding function enable;bit[2] night mode disable
  538. /******if use 50HZ banding remove, refer to below setting********/
  539. {0x3c00, 0x04}, //BIT[2]:1,ENABLE 50HZ
  540. {0x3a14, 0x06}, //NIGHT MODE CEILING, 50HZ
  541. {0x3a15, 0x6d}, //NIGHT MODE CEILING, 50HZ
  542. {0x3a08, 0x00}, //50HZ BAND WIDTH
  543. {0x3a09, 0xeb}, //50HZ BAND WIDTH
  544. {0x3a0e, 0x06}, //50HZ MAX BAND
  545. {0x380e, 0x06},
  546. {0x380f, 0x6d}, //inset 81 dummy lines for banding filter//1c
  547. /******if use 60HZ banding remove, refer to below setting********/
  548. //{0x3c00, 0x00}, //BIT[2]:0,ENABLE 50HZ
  549. {0x3a02, 0x06}, //NIGHT MODE CEILING, 60HZ
  550. {0x3a03, 0x6d}, //NIGHT MODE CEILING, 60HZ
  551. {0x3a0a, 0x00}, //60HZ BAND WIDTH
  552. {0x3a0b, 0xc4}, //60HZ BAND WIDTH
  553. {0x3a0d, 0x08}, //60HZ MAX BAND
  554. #if 0
  555. {0x3a0f, 0x38},
  556. {0x3a10, 0x30},
  557. {0x3a1b, 0x38},
  558. {0x3a1e, 0x30},
  559. {0x3a11, 0x70},
  560. {0x3a1f, 0x14},
  561. #endif
  562. {0x3008, 0x02}, //WAKE UP FROM SOFTWARE POWER DOWN MODE
  563. {0xffff, 0xff},
  564. };
  565. //load OV3660 parameters
  566. void OV3660_init_regs(struct ov3660_device *dev)
  567. {
  568. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  569. int i=0;
  570. #ifdef TEST_I2C
  571. struct aml_camera_i2c_fig_s ov3660_id[2];
  572. unsigned int id;
  573. ov3660_id[0].addr = 0x300A;
  574. ov3660_id[1].addr = 0x300B;
  575. ov3660_id[0].val = i2c_get_byte(client, ov3660_id[0].addr);
  576. ov3660_id[1].val = i2c_get_byte(client, ov3660_id[1].addr);
  577. id = (ov3660_id[0].val << 8)|ov3660_id[1].val;
  578. printk("<=================Ov3660 ID:0x%x\n", id);
  579. #endif
  580. i2c_put_byte(client, 0x3008, 0x82); //soft reset
  581. msleep(3);
  582. while(1)
  583. {
  584. if (OV3660_script[i].val==0xff&&OV3660_script[i].addr==0xffff)
  585. {
  586. printk("OV3660_write_regs success in initial OV3660.\n");
  587. break;
  588. }
  589. if((i2c_put_byte(client,OV3660_script[i].addr, OV3660_script[i].val)) < 0)
  590. {
  591. printk("fail in initial OV3660. i=%d\n",i);
  592. // break;
  593. }
  594. i++;
  595. }
  596. aml_plat_cam_data_t* plat_dat= (aml_plat_cam_data_t*)client->dev.platform_data;
  597. if (plat_dat&&plat_dat->custom_init_script) {
  598. i=0;
  599. aml_camera_i2c_fig_t* custom_script = (aml_camera_i2c_fig_t*)plat_dat->custom_init_script;
  600. while(1)
  601. {
  602. if (custom_script[i].val==0xff&&custom_script[i].addr==0xffff){
  603. printk("OV3660_write_custom_regs success in initial OV3660.\n");
  604. break;
  605. }
  606. do {
  607. if((i2c_put_byte(client, custom_script[i].addr, custom_script[i].val)) < 0){
  608. printk("fail in initial OV3660 custom_regs. \n");
  609. return;
  610. }
  611. }while (i2c_get_byte(client, custom_script[i].addr) != custom_script[i].val);
  612. i++;
  613. }
  614. }
  615. return;
  616. }
  617. /*************************************************************************
  618. * FUNCTION
  619. * OV3660_set_param_brightness
  620. *
  621. * DESCRIPTION
  622. * brightness setting.
  623. *
  624. * PARAMETERS
  625. * none
  626. *
  627. * RETURNS
  628. * None
  629. *
  630. * GLOBALS AFFECTED
  631. *
  632. *************************************************************************/
  633. void OV3660_set_param_brightness(struct ov3660_device *dev, int value)//亮度
  634. {
  635. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  636. int temp;
  637. switch (value) {
  638. case 3:
  639. temp = i2c_get_byte(client,0x5588);
  640. i2c_put_byte(client, 0x5588, temp&0xf7);
  641. i2c_put_byte(client, 0x5587, 0x60);
  642. break;
  643. case 2:
  644. temp = i2c_get_byte(client,0x5588);
  645. i2c_put_byte(client, 0x5588, temp&0xf7);
  646. i2c_put_byte(client, 0x5587, 0x40);
  647. break;
  648. case 1:
  649. temp = i2c_get_byte(client,0x5588);
  650. i2c_put_byte(client, 0x5588, temp&0xf7);
  651. i2c_put_byte(client, 0x5587, 0x20);
  652. break;
  653. case 0:
  654. temp = i2c_get_byte(client,0x5588);
  655. i2c_put_byte(client, 0x5588, temp&0xf7);
  656. i2c_put_byte(client, 0x5587, 0x00);
  657. break;
  658. case -1:
  659. temp = i2c_get_byte(client,0x5588);
  660. i2c_put_byte(client, 0x5588, temp | 0x08);
  661. i2c_put_byte(client, 0x5587, 0x20);
  662. break;
  663. case -2:
  664. temp = i2c_get_byte(client,0x5588);
  665. i2c_put_byte(client, 0x5588, temp | 0x08);
  666. i2c_put_byte(client, 0x5587, 0x40);
  667. break;
  668. case -3:
  669. temp = i2c_get_byte(client,0x5588);
  670. i2c_put_byte(client, 0x5588, temp | 0x08);
  671. i2c_put_byte(client, 0x5587, 0x60);
  672. break;
  673. default:
  674. temp = i2c_get_byte(client,0x5588);
  675. i2c_put_byte(client, 0x5588, temp&0xf7);
  676. i2c_put_byte(client, 0x5587, 0x00);
  677. break;
  678. }
  679. } /* OV3660_set_param_brightness */
  680. /*************************************************************************
  681. * FUNCTION
  682. * OV3660_set_param_contrast
  683. *
  684. * DESCRIPTION
  685. * contrast setting.
  686. *
  687. * PARAMETERS
  688. * none
  689. *
  690. * RETURNS
  691. * None
  692. *
  693. * GLOBALS AFFECTED
  694. *
  695. *************************************************************************/
  696. void OV3660_set_param_contrast(struct ov3660_device *dev, int value)//亮度
  697. {
  698. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  699. switch (value) {
  700. case 3:
  701. i2c_put_byte(client, 0x5586, 0x38);
  702. break;
  703. case 2:
  704. i2c_put_byte(client, 0x5586, 0x30);
  705. break;
  706. case 1:
  707. i2c_put_byte(client, 0x5586, 0x28);
  708. break;
  709. case 0:
  710. i2c_put_byte(client, 0x5586, 0x20);
  711. break;
  712. case -1:
  713. i2c_put_byte(client, 0x5586, 0x18);
  714. break;
  715. case -2:
  716. i2c_put_byte(client, 0x5586, 0x10);
  717. break;
  718. case -3:
  719. i2c_put_byte(client, 0x5586, 0x08);
  720. break;
  721. default:
  722. i2c_put_byte(client, 0x5586, 0x20);
  723. break;
  724. }
  725. } /* OV3660_set_param_contrast */
  726. /*************************************************************************
  727. * FUNCTION
  728. * OV3660_set_param_saturation
  729. *
  730. * DESCRIPTION
  731. * saturation setting.
  732. *
  733. * PARAMETERS
  734. * none
  735. *
  736. * RETURNS
  737. * None
  738. *
  739. * GLOBALS AFFECTED
  740. *
  741. *************************************************************************/
  742. void OV3660_set_param_saturation(struct ov3660_device *dev, int value)//饱和度
  743. {
  744. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  745. switch (value) {
  746. case 2:
  747. i2c_put_byte(client, 0x5583, 0x70);
  748. i2c_put_byte(client, 0x5584, 0x70);
  749. break;
  750. case 1:
  751. i2c_put_byte(client, 0x5583, 0x50);
  752. i2c_put_byte(client, 0x5584, 0x50);
  753. break;
  754. case 0:
  755. i2c_put_byte(client, 0x5583, 0x60);
  756. i2c_put_byte(client, 0x5584, 0x40);
  757. break;
  758. case -1:
  759. i2c_put_byte(client, 0x5583, 0x30);
  760. i2c_put_byte(client, 0x5584, 0x30);
  761. break;
  762. case -2:
  763. i2c_put_byte(client, 0x5583, 0x10);
  764. i2c_put_byte(client, 0x5584, 0x10);
  765. break;
  766. default:
  767. i2c_put_byte(client, 0x5583, 0x60);
  768. i2c_put_byte(client, 0x5584, 0x40);
  769. break;
  770. }
  771. } /* OV3660_set_param_saturation */
  772. /*************************************************************************
  773. * FUNCTION
  774. * OV3660_set_param_wb
  775. *
  776. * DESCRIPTION
  777. * wb setting.
  778. *
  779. * PARAMETERS
  780. * none
  781. *
  782. * RETURNS
  783. * None
  784. *
  785. * GLOBALS AFFECTED
  786. *
  787. *************************************************************************/
  788. void OV3660_set_param_wb(struct ov3660_device *dev,enum camera_wb_flip_e para)//白平衡
  789. {
  790. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  791. switch (para)
  792. {
  793. case CAM_WB_AUTO://自动
  794. i2c_put_byte(client, 0x3212, 0x03);
  795. i2c_put_byte(client, 0x3406, 0x00);
  796. i2c_put_byte(client, 0x3212, 0x13);
  797. i2c_put_byte(client, 0x3212, 0xa3);
  798. break;
  799. case CAM_WB_CLOUD: //阴天
  800. i2c_put_byte(client, 0x3212, 0x03);
  801. i2c_put_byte(client, 0x3406, 0x01);
  802. i2c_put_byte(client, 0x3400, 0x07);
  803. i2c_put_byte(client, 0x3401, 0xff);
  804. i2c_put_byte(client, 0x3402, 0x04);
  805. i2c_put_byte(client, 0x3403, 0x00);
  806. i2c_put_byte(client, 0x3404, 0x04);
  807. i2c_put_byte(client, 0x3405, 0x00);
  808. i2c_put_byte(client, 0x3212, 0x13);
  809. i2c_put_byte(client, 0x3212, 0xa3);
  810. break;
  811. case CAM_WB_DAYLIGHT: //
  812. i2c_put_byte(client, 0x3212, 0x03);
  813. i2c_put_byte(client, 0x3406, 0x01);
  814. i2c_put_byte(client, 0x3400, 0x07);
  815. i2c_put_byte(client, 0x3401, 0x29);
  816. i2c_put_byte(client, 0x3402, 0x04);
  817. i2c_put_byte(client, 0x3403, 0x00);
  818. i2c_put_byte(client, 0x3404, 0x04);
  819. i2c_put_byte(client, 0x3405, 0x5d);
  820. i2c_put_byte(client, 0x3212, 0x13);
  821. i2c_put_byte(client, 0x3212, 0xa3);
  822. break;
  823. case CAM_WB_INCANDESCENCE:
  824. i2c_put_byte(client, 0x3212, 0x03);
  825. i2c_put_byte(client, 0x3406, 0x01);
  826. i2c_put_byte(client, 0x3400, 0x04);
  827. i2c_put_byte(client, 0x3401, 0xc5);
  828. i2c_put_byte(client, 0x3402, 0x04);
  829. i2c_put_byte(client, 0x3403, 0x00);
  830. i2c_put_byte(client, 0x3404, 0x08);
  831. i2c_put_byte(client, 0x3405, 0x4d);
  832. i2c_put_byte(client, 0x3212, 0x13);
  833. i2c_put_byte(client, 0x3212, 0xa3);
  834. break;
  835. case CAM_WB_TUNGSTEN:
  836. break;
  837. case CAM_WB_FLUORESCENT:
  838. i2c_put_byte(client, 0x3212, 0x03);
  839. i2c_put_byte(client, 0x3406, 0x01);
  840. i2c_put_byte(client, 0x3400, 0x06);
  841. i2c_put_byte(client, 0x3401, 0x57);
  842. i2c_put_byte(client, 0x3402, 0x04);
  843. i2c_put_byte(client, 0x3403, 0x00);
  844. i2c_put_byte(client, 0x3404, 0x07);
  845. i2c_put_byte(client, 0x3405, 0x6f);
  846. i2c_put_byte(client, 0x3212, 0x13);
  847. i2c_put_byte(client, 0x3212, 0xa3);
  848. break;
  849. case CAM_WB_MANUAL:
  850. // TODO
  851. break;
  852. }
  853. } /* OV3660_set_param_wb */
  854. /*************************************************************************
  855. * FUNCTION
  856. * OV3660_set_param_exposure
  857. *
  858. * DESCRIPTION
  859. * exposure setting.
  860. *
  861. * PARAMETERS
  862. * none
  863. *
  864. * RETURNS
  865. * None
  866. *
  867. * GLOBALS AFFECTED
  868. *
  869. *************************************************************************/
  870. void OV3660_set_param_exposure(struct ov3660_device *dev,enum camera_exposure_e para)//曝光调节
  871. {
  872. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  873. switch (para)
  874. {
  875. case EXPOSURE_N4_STEP: //负4档
  876. i2c_put_byte(client,0x3a0f , 0x18);
  877. i2c_put_byte(client,0x3a10 , 0x10);
  878. i2c_put_byte(client,0x3a1b , 0x18);
  879. i2c_put_byte(client,0x3a1e , 0x10);
  880. i2c_put_byte(client,0x3a11 , 0x30);
  881. i2c_put_byte(client,0x3a1f , 0x08);
  882. break;
  883. case EXPOSURE_N3_STEP:
  884. i2c_put_byte(client,0x3a0f , 0x20);
  885. i2c_put_byte(client,0x3a10 , 0x18);
  886. i2c_put_byte(client,0x3a1b , 0x20);
  887. i2c_put_byte(client,0x3a1e , 0x18);
  888. i2c_put_byte(client,0x3a11 , 0x40);
  889. i2c_put_byte(client,0x3a1f , 0x0c);
  890. break;
  891. case EXPOSURE_N2_STEP:
  892. i2c_put_byte(client,0x3a0f , 0x28);
  893. i2c_put_byte(client,0x3a10 , 0x20);
  894. i2c_put_byte(client,0x3a1b , 0x28);
  895. i2c_put_byte(client,0x3a1e , 0x20);
  896. i2c_put_byte(client,0x3a11 , 0x50);
  897. i2c_put_byte(client,0x3a1f , 0x10);
  898. break;
  899. case EXPOSURE_N1_STEP:
  900. i2c_put_byte(client,0x3a0f , 0x30);
  901. i2c_put_byte(client,0x3a10 , 0x28);
  902. i2c_put_byte(client,0x3a1b , 0x30);
  903. i2c_put_byte(client,0x3a1e , 0x28);
  904. i2c_put_byte(client,0x3a11 , 0x60);
  905. i2c_put_byte(client,0x3a1f , 0x14);
  906. break;
  907. case EXPOSURE_0_STEP://默认零档
  908. i2c_put_byte(client,0x3a0f , 0x38);
  909. i2c_put_byte(client,0x3a10 , 0x30);
  910. i2c_put_byte(client,0x3a1b , 0x38);
  911. i2c_put_byte(client,0x3a1e , 0x30);
  912. i2c_put_byte(client,0x3a11 , 0x70);
  913. i2c_put_byte(client,0x3a1f , 0x14);
  914. break;
  915. case EXPOSURE_P1_STEP://正一档
  916. i2c_put_byte(client,0x3a0f , 0x40);
  917. i2c_put_byte(client,0x3a10 , 0x38);
  918. i2c_put_byte(client,0x3a1b , 0x40);
  919. i2c_put_byte(client,0x3a1e , 0x38);
  920. i2c_put_byte(client,0x3a11 , 0x80);
  921. i2c_put_byte(client,0x3a1f , 0x1c);
  922. break;
  923. case EXPOSURE_P2_STEP:
  924. i2c_put_byte(client,0x3a0f , 0x48);
  925. i2c_put_byte(client,0x3a10 , 0x40);
  926. i2c_put_byte(client,0x3a1b , 0x48);
  927. i2c_put_byte(client,0x3a1e , 0x40);
  928. i2c_put_byte(client,0x3a11 , 0x90);
  929. i2c_put_byte(client,0x3a1f , 0x20);
  930. break;
  931. case EXPOSURE_P3_STEP:
  932. i2c_put_byte(client,0x3a0f , 0x50);
  933. i2c_put_byte(client,0x3a10 , 0x48);
  934. i2c_put_byte(client,0x3a1b , 0x50);
  935. i2c_put_byte(client,0x3a1e , 0x48);
  936. i2c_put_byte(client,0x3a11 , 0xa0);
  937. i2c_put_byte(client,0x3a1f , 0x24);
  938. break;
  939. case EXPOSURE_P4_STEP:
  940. i2c_put_byte(client,0x3a0f , 0x58);
  941. i2c_put_byte(client,0x3a10 , 0x50);
  942. i2c_put_byte(client,0x3a1b , 0x58);
  943. i2c_put_byte(client,0x3a1e , 0x58);
  944. i2c_put_byte(client,0x3a11 , 0xb0);
  945. i2c_put_byte(client,0x3a1f , 0x28);
  946. break;
  947. default:
  948. i2c_put_byte(client,0x3a0f , 0x38);
  949. i2c_put_byte(client,0x3a10 , 0x30);
  950. i2c_put_byte(client,0x3a1b , 0x38);
  951. i2c_put_byte(client,0x3a1e , 0x30);
  952. i2c_put_byte(client,0x3a11 , 0x70);
  953. i2c_put_byte(client,0x3a1f , 0x14);
  954. break;
  955. }
  956. } /* OV3660_set_param_exposure */
  957. /*************************************************************************
  958. * FUNCTION
  959. * OV3660_set_param_effect
  960. *
  961. * DESCRIPTION
  962. * effect setting.
  963. *
  964. * PARAMETERS
  965. * none
  966. *
  967. * RETURNS
  968. * None
  969. *
  970. * GLOBALS AFFECTED
  971. *
  972. *************************************************************************/
  973. void OV3660_set_param_effect(struct ov3660_device *dev,enum camera_effect_flip_e para)//特效设置
  974. {
  975. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  976. unsigned char temp;
  977. switch (para)
  978. {
  979. case CAM_EFFECT_ENC_NORMAL://正常
  980. i2c_put_byte(client,0x5001,0x00);//disable effect
  981. i2c_put_byte(client,0x5580,0x00);//disable effect
  982. break;
  983. case CAM_EFFECT_ENC_GRAYSCALE://灰阶
  984. i2c_put_byte(client,0x5001,0x80);
  985. i2c_put_byte(client,0x5580,0x18);
  986. i2c_put_byte(client,0x5583,0x80);
  987. i2c_put_byte(client,0x5584,0x80);
  988. break;
  989. case CAM_EFFECT_ENC_SEPIA://复古
  990. i2c_put_byte(client,0x5001,0x80);
  991. i2c_put_byte(client,0x5580,0x18);
  992. i2c_put_byte(client,0x5583,0x40);
  993. i2c_put_byte(client,0x5584,0xa0);
  994. break;
  995. case CAM_EFFECT_ENC_SEPIAGREEN://复古绿
  996. temp = i2c_get_byte(client, 0x5580);
  997. i2c_put_byte(client,0x5580,temp & 0xbf | 0x18);
  998. i2c_put_byte(client,0x5583,0x60);
  999. i2c_put_byte(client,0x5584,0x60);
  1000. break;
  1001. case CAM_EFFECT_ENC_SEPIABLUE://复古蓝
  1002. temp = i2c_get_byte(client, 0x5580);
  1003. i2c_put_byte(client,0x5580,temp & 0xbf | 0x18);
  1004. i2c_put_byte(client,0x5583,0xa0);
  1005. i2c_put_byte(client,0x5584,0x40);
  1006. break;
  1007. case CAM_EFFECT_ENC_COLORINV://底片
  1008. i2c_put_byte(client,0x5001,0x80);
  1009. i2c_put_byte(client,0x5580,0x58);
  1010. break;
  1011. default:
  1012. break;
  1013. }
  1014. } /* OV3660_set_param_effect */
  1015. /*************************************************************************
  1016. * FUNCTION
  1017. * OV3660_night_mode
  1018. *
  1019. * DESCRIPTION
  1020. * This function night mode of OV3660.
  1021. *
  1022. * PARAMETERS
  1023. * none
  1024. *
  1025. * RETURNS
  1026. * None
  1027. *
  1028. * GLOBALS AFFECTED
  1029. *
  1030. *************************************************************************/
  1031. void OV3660_set_param_banding(struct ov3660_device *dev,enum camera_night_mode_flip_e banding)
  1032. {
  1033. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  1034. unsigned char buf[4];
  1035. switch(banding){
  1036. case CAM_BANDING_60HZ:
  1037. printk("set banding 60Hz\n");
  1038. i2c_put_byte(client, 0x3c00, 0x00);
  1039. break;
  1040. case CAM_BANDING_50HZ:
  1041. printk("set banding 50Hz\n");
  1042. i2c_put_byte(client, 0x3c00, 0x04);
  1043. break;
  1044. }
  1045. }
  1046. unsigned char v4l_2_ov3660(int val)
  1047. {
  1048. int ret=val/0x20;
  1049. if(ret<4) return ret*0x20+0x80;
  1050. else if(ret<8) return ret*0x20+0x20;
  1051. else return 0;
  1052. }
  1053. static int ov3660_setting(struct ov3660_device *dev,int PROP_ID,int value )
  1054. {
  1055. int ret=0;
  1056. unsigned char cur_val;
  1057. unsigned char reg_3820, reg_3821, reg_4515;
  1058. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  1059. switch(PROP_ID) {
  1060. case V4L2_CID_BRIGHTNESS:
  1061. if(ov3660_qctrl[0].default_value!=value){
  1062. ov3660_qctrl[0].default_value=value;
  1063. OV3660_set_param_brightness(dev,value);
  1064. printk(KERN_INFO " set camera brightness=%d. \n ",value);
  1065. }
  1066. case V4L2_CID_CONTRAST:
  1067. if(ov3660_qctrl[1].default_value!=value){
  1068. ov3660_qctrl[1].default_value=value;
  1069. OV3660_set_param_contrast(dev,value);
  1070. printk(KERN_INFO " set camera contrast=%d. \n ",value);
  1071. }
  1072. break;
  1073. case V4L2_CID_HFLIP: /* set flip on H. */
  1074. reg_3820=i2c_get_byte(client,0x3820);
  1075. reg_3821=i2c_get_byte(client,0x3821);
  1076. if(reg_3820 == 0x1) {
  1077. reg_3820 = 0x7;
  1078. if (reg_3820 == reg_3821)
  1079. reg_4515 = 0xaa;
  1080. else
  1081. reg_4515 = 0xbb;
  1082. i2c_put_byte(client, 0x3820, reg_3820);
  1083. i2c_put_byte(client, 0x4515, reg_4515);
  1084. } else {
  1085. reg_3820 = 0x1;
  1086. if (reg_3820 == reg_3821)
  1087. reg_4515 = 0xaa;
  1088. else
  1089. reg_4515 = 0xbb;
  1090. i2c_put_byte(client, 0x3820, reg_3820);
  1091. i2c_put_byte(client, 0x4515, reg_4515);
  1092. }
  1093. break;
  1094. case V4L2_CID_VFLIP: /* set flip on V. */
  1095. reg_3820=i2c_get_byte(client,0x3820);
  1096. reg_3821=i2c_get_byte(client,0x3821);
  1097. if(reg_3821 == 0x1) {
  1098. reg_3821 = 0x7;
  1099. if (reg_3820 == reg_3821)
  1100. reg_4515 = 0xaa;
  1101. else
  1102. reg_4515 = 0xbb;
  1103. i2c_put_byte(client, 0x3821, reg_3821);
  1104. i2c_put_byte(client, 0x4515, reg_4515);
  1105. } else {
  1106. reg_3821 = 0x1;
  1107. if (reg_3820 == reg_3821)
  1108. reg_4515 = 0xaa;
  1109. else
  1110. reg_4515 = 0xbb;
  1111. i2c_put_byte(client, 0x3821, reg_3821);
  1112. i2c_put_byte(client, 0x4515, reg_4515);
  1113. }
  1114. break;
  1115. case V4L2_CID_DO_WHITE_BALANCE:
  1116. if(ov3660_qctrl[4].default_value!=value){
  1117. ov3660_qctrl[4].default_value=value;
  1118. OV3660_set_param_wb(dev,value);
  1119. printk(KERN_INFO " set camera white_balance=%d. \n ",value);
  1120. }
  1121. break;
  1122. case V4L2_CID_EXPOSURE:
  1123. if(ov3660_qctrl[5].default_value!=value){
  1124. ov3660_qctrl[5].default_value=value;
  1125. OV3660_set_param_exposure(dev,value);
  1126. printk(KERN_INFO " set camera exposure=%d. \n ",value);
  1127. }
  1128. break;
  1129. case V4L2_CID_COLORFX:
  1130. if(ov3660_qctrl[6].default_value!=value){
  1131. ov3660_qctrl[6].default_value=value;
  1132. OV3660_set_param_effect(dev,value);
  1133. printk(KERN_INFO " set camera effect=%d. \n ",value);
  1134. }
  1135. break;
  1136. case V4L2_CID_WHITENESS:
  1137. if(ov3660_qctrl[7].default_value!=value){
  1138. ov3660_qctrl[7].default_value=value;
  1139. OV3660_set_param_banding(dev,value);
  1140. printk(KERN_INFO " set camera banding=%d. \n ",value);
  1141. }
  1142. break;
  1143. /*case V4L2_CID_SATURATION:
  1144. if(ov3660_qctrl[8].default_value!=value){
  1145. ov3660_qctrl[8].default_value=value;
  1146. OV3660_set_param_saturation(dev,value);
  1147. printk(KERN_INFO " set camera saturation=%d. \n ",value);
  1148. }
  1149. break;
  1150. */
  1151. default:
  1152. ret=-1;
  1153. break;
  1154. }
  1155. return ret;
  1156. }
  1157. static void power_down_ov3660(struct ov3660_device *dev)
  1158. {
  1159. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  1160. /*i2c_put_byte(client,0x0104, 0x00);
  1161. i2c_put_byte(client,0x0100, 0x00);*/
  1162. }
  1163. /* ------------------------------------------------------------------
  1164. DMA and thread functions
  1165. ------------------------------------------------------------------*/
  1166. #define TSTAMP_MIN_Y 24
  1167. #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
  1168. #define TSTAMP_INPUT_X 10
  1169. #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
  1170. static void ov3660_fillbuff(struct ov3660_fh *fh, struct ov3660_buffer *buf)
  1171. {
  1172. struct ov3660_device *dev = fh->dev;
  1173. int h , pos = 0;
  1174. int hmax = buf->vb.height;
  1175. int wmax = buf->vb.width;
  1176. struct timeval ts;
  1177. char *tmpbuf;
  1178. void *vbuf = videobuf_to_vmalloc(&buf->vb);
  1179. dprintk(dev,1,"%s\n", __func__);
  1180. if (!vbuf)
  1181. return;
  1182. /* 0x18221223 indicate the memory type is MAGIC_VMAL_MEM*/
  1183. vm_fill_buffer(&buf->vb,fh->fmt->fourcc ,0x18221223,vbuf);
  1184. buf->vb.state = VIDEOBUF_DONE;
  1185. }
  1186. static void ov3660_thread_tick(struct ov3660_fh *fh)
  1187. {
  1188. struct ov3660_buffer *buf;
  1189. struct ov3660_device *dev = fh->dev;
  1190. struct ov3660_dmaqueue *dma_q = &dev->vidq;
  1191. unsigned long flags = 0;
  1192. dprintk(dev, 1, "Thread tick\n");
  1193. spin_lock_irqsave(&dev->slock, flags);
  1194. if (list_empty(&dma_q->active)) {
  1195. dprintk(dev, 1, "No active queue to serve\n");
  1196. goto unlock;
  1197. }
  1198. buf = list_entry(dma_q->active.next,
  1199. struct ov3660_buffer, vb.queue);
  1200. dprintk(dev, 1, "%s\n", __func__);
  1201. dprintk(dev, 1, "list entry get buf is %x\n",buf);
  1202. /* Nobody is waiting on this buffer, return */
  1203. if (!waitqueue_active(&buf->vb.done))
  1204. goto unlock;
  1205. list_del(&buf->vb.queue);
  1206. do_gettimeofday(&buf->vb.ts);
  1207. /* Fill buffer */
  1208. spin_unlock_irqrestore(&dev->slock, flags);
  1209. ov3660_fillbuff(fh, buf);
  1210. dprintk(dev, 1, "filled buffer %p\n", buf);
  1211. wake_up(&buf->vb.done);
  1212. dprintk(dev, 2, "[%p/%d] wakeup\n", buf, buf->vb. i);
  1213. return;
  1214. unlock:
  1215. spin_unlock_irqrestore(&dev->slock, flags);
  1216. return;
  1217. }
  1218. #define frames_to_ms(frames) \
  1219. ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
  1220. static void ov3660_sleep(struct ov3660_fh *fh)
  1221. {
  1222. struct ov3660_device *dev = fh->dev;
  1223. struct ov3660_dmaqueue *dma_q = &dev->vidq;
  1224. int timeout;
  1225. DECLARE_WAITQUEUE(wait, current);
  1226. dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__,
  1227. (unsigned long)dma_q);
  1228. add_wait_queue(&dma_q->wq, &wait);
  1229. if (kthread_should_stop())
  1230. goto stop_task;
  1231. /* Calculate time to wake up */
  1232. //timeout = msecs_to_jiffies(frames_to_ms(1));
  1233. ov3660_thread_tick(fh);
  1234. schedule_timeout_interruptible(2);
  1235. stop_task:
  1236. remove_wait_queue(&dma_q->wq, &wait);
  1237. try_to_freeze();
  1238. }
  1239. static int ov3660_thread(void *data)
  1240. {
  1241. struct ov3660_fh *fh = data;
  1242. struct ov3660_device *dev = fh->dev;
  1243. dprintk(dev, 1, "thread started\n");
  1244. set_freezable();
  1245. for (;;) {
  1246. ov3660_sleep(fh);
  1247. if (kthread_should_stop())
  1248. break;
  1249. }
  1250. dprintk(dev, 1, "thread: exit\n");
  1251. return 0;
  1252. }
  1253. static int ov3660_start_thread(struct ov3660_fh *fh)
  1254. {
  1255. struct ov3660_device *dev = fh->dev;
  1256. struct ov3660_dmaqueue *dma_q = &dev->vidq;
  1257. dma_q->frame = 0;
  1258. dma_q->ini_jiffies = jiffies;
  1259. dprintk(dev, 1, "%s\n", __func__);
  1260. dma_q->kthread = kthread_run(ov3660_thread, fh, "ov3660");
  1261. if (IS_ERR(dma_q->kthread)) {
  1262. v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
  1263. return PTR_ERR(dma_q->kthread);
  1264. }
  1265. /* Wakes thread */
  1266. wake_up_interruptible(&dma_q->wq);
  1267. dprintk(dev, 1, "returning from %s\n", __func__);
  1268. return 0;
  1269. }
  1270. static void ov3660_stop_thread(struct ov3660_dmaqueue *dma_q)
  1271. {
  1272. struct ov3660_device *dev = container_of(dma_q, struct ov3660_device, vidq);
  1273. dprintk(dev, 1, "%s\n", __func__);
  1274. /* shutdown control thread */
  1275. if (dma_q->kthread) {
  1276. kthread_stop(dma_q->kthread);
  1277. dma_q->kthread = NULL;
  1278. }
  1279. }
  1280. /* ------------------------------------------------------------------
  1281. Videobuf operations
  1282. ------------------------------------------------------------------*/
  1283. static int
  1284. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  1285. {
  1286. struct ov3660_fh *fh = vq->priv_data;
  1287. struct ov3660_device *dev = fh->dev;
  1288. //int bytes = fh->fmt->depth >> 3 ;
  1289. *size = (fh->width*fh->height*fh->fmt->depth)>>3;
  1290. if (0 == *count)
  1291. *count = 32;
  1292. while (*size * *count > vid_limit * 1024 * 1024)
  1293. (*count)--;
  1294. dprintk(dev, 1, "%s, count=%d, size=%d\n", __func__,
  1295. *count, *size);
  1296. return 0;
  1297. }
  1298. static void free_buffer(struct videobuf_queue *vq, struct ov3660_buffer *buf)
  1299. {
  1300. struct ov3660_fh *fh = vq->priv_data;
  1301. struct ov3660_device *dev = fh->dev;
  1302. dprintk(dev, 1, "%s, state: %i\n", __func__, buf->vb.state);
  1303. if (in_interrupt())
  1304. BUG();
  1305. videobuf_vmalloc_free(&buf->vb);
  1306. dprintk(dev, 1, "free_buffer: freed\n");
  1307. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  1308. }
  1309. #define norm_maxw() 3000
  1310. #define norm_maxh() 3000
  1311. static int
  1312. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  1313. enum v4l2_field field)
  1314. {
  1315. struct ov3660_fh *fh = vq->priv_data;
  1316. struct ov3660_device *dev = fh->dev;
  1317. struct ov3660_buffer *buf = container_of(vb, struct ov3660_buffer, vb);
  1318. int rc;
  1319. //int bytes = fh->fmt->depth >> 3 ;
  1320. dprintk(dev, 1, "%s, field=%d\n", __func__, field);
  1321. BUG_ON(NULL == fh->fmt);
  1322. if (fh->width < 48 || fh->width > norm_maxw() ||
  1323. fh->height < 32 || fh->height > norm_maxh())
  1324. return -EINVAL;
  1325. buf->vb.size = (fh->width*fh->height*fh->fmt->depth)>>3;
  1326. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  1327. return -EINVAL;
  1328. /* These properties only change when queue is idle, see s_fmt */
  1329. buf->fmt = fh->fmt;
  1330. buf->vb.width = fh->width;
  1331. buf->vb.height = fh->height;
  1332. buf->vb.field = field;
  1333. //precalculate_bars(fh);
  1334. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  1335. rc = videobuf_iolock(vq, &buf->vb, NULL);
  1336. if (rc < 0)
  1337. goto fail;
  1338. }
  1339. buf->vb.state = VIDEOBUF_PREPARED;
  1340. return 0;
  1341. fail:
  1342. free_buffer(vq, buf);
  1343. return rc;
  1344. }
  1345. static void
  1346. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  1347. {
  1348. struct ov3660_buffer *buf = container_of(vb, struct ov3660_buffer, vb);
  1349. struct ov3660_fh *fh = vq->priv_data;
  1350. struct ov3660_device *dev = fh->dev;
  1351. struct ov3660_dmaqueue *vidq = &dev->vidq;
  1352. dprintk(dev, 1, "%s\n", __func__);
  1353. buf->vb.state = VIDEOBUF_QUEUED;
  1354. list_add_tail(&buf->vb.queue, &vidq->active);
  1355. }
  1356. static void buffer_release(struct videobuf_queue *vq,
  1357. struct videobuf_buffer *vb)
  1358. {
  1359. struct ov3660_buffer *buf = container_of(vb, struct ov3660_buffer, vb);
  1360. struct ov3660_fh *fh = vq->priv_data;
  1361. struct ov3660_device *dev = (struct ov3660_device *)fh->dev;
  1362. dprintk(dev, 1, "%s\n", __func__);
  1363. free_buffer(vq, buf);
  1364. }
  1365. static struct videobuf_queue_ops ov3660_video_qops = {
  1366. .buf_setup = buffer_setup,
  1367. .buf_prepare = buffer_prepare,
  1368. .buf_queue = buffer_queue,
  1369. .buf_release = buffer_release,
  1370. };
  1371. /* ------------------------------------------------------------------
  1372. IOCTL vidioc handling
  1373. ------------------------------------------------------------------*/
  1374. static int vidioc_querycap(struct file *file, void *priv,
  1375. struct v4l2_capability *cap)
  1376. {
  1377. struct ov3660_fh *fh = priv;
  1378. struct ov3660_device *dev = fh->dev;
  1379. strcpy(cap->driver, "ov3660");
  1380. strcpy(cap->card, "ov3660");
  1381. strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
  1382. cap->version = OV3660_CAMERA_VERSION;
  1383. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  1384. V4L2_CAP_STREAMING |
  1385. V4L2_CAP_READWRITE;
  1386. return 0;
  1387. }
  1388. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1389. struct v4l2_fmtdesc *f)
  1390. {
  1391. struct ov3660_fmt *fmt;
  1392. if (f->index >= ARRAY_SIZE(formats))
  1393. return -EINVAL;
  1394. fmt = &formats[f->index];
  1395. strlcpy(f->description, fmt->name, sizeof(f->description));
  1396. f->pixelformat = fmt->fourcc;
  1397. return 0;
  1398. }
  1399. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  1400. struct v4l2_format *f)
  1401. {
  1402. struct ov3660_fh *fh = priv;
  1403. f->fmt.pix.width = fh->width;
  1404. f->fmt.pix.height = fh->height;
  1405. f->fmt.pix.field = fh->vb_vidq.field;
  1406. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  1407. f->fmt.pix.bytesperline =
  1408. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  1409. f->fmt.pix.sizeimage =
  1410. f->fmt.pix.height * f->fmt.pix.bytesperline;
  1411. return (0);
  1412. }
  1413. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  1414. struct v4l2_format *f)
  1415. {
  1416. struct ov3660_fh *fh = priv;
  1417. struct ov3660_device *dev = fh->dev;
  1418. struct ov3660_fmt *fmt;
  1419. enum v4l2_field field;
  1420. unsigned int maxw, maxh;
  1421. fmt = get_format(f);
  1422. if (!fmt) {
  1423. dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n",
  1424. f->fmt.pix.pixelformat);
  1425. return -EINVAL;
  1426. }
  1427. field = f->fmt.pix.field;
  1428. if (field == V4L2_FIELD_ANY) {
  1429. field = V4L2_FIELD_INTERLACED;
  1430. } else if (V4L2_FIELD_INTERLACED != field) {
  1431. dprintk(dev, 1, "Field type invalid.\n");
  1432. return -EINVAL;
  1433. }
  1434. maxw = norm_maxw();
  1435. maxh = norm_maxh();
  1436. f->fmt.pix.field = field;
  1437. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  1438. &f->fmt.pix.height, 32, maxh, 0, 0);
  1439. f->fmt.pix.bytesperline =
  1440. (f->fmt.pix.width * fmt->depth) >> 3;
  1441. f->fmt.pix.sizeimage =
  1442. f->fmt.pix.height * f->fmt.pix.bytesperline;
  1443. return 0;
  1444. }
  1445. static struct aml_camera_i2c_fig_s pic2048x1536[]={
  1446. {0x3002, 0x1c},
  1447. {0x3006, 0xc3},
  1448. {0x3800, 0x00}, //HS
  1449. {0x3801, 0x00}, //HS
  1450. {0x3802, 0x00}, //VS
  1451. {0x3803, 0x00}, //VS
  1452. {0x3804, 0x08}, //HW=1696
  1453. {0x3805, 0x1f}, //HW
  1454. {0x3806, 0x06}, //VH=1261
  1455. {0x3807, 0x0b}, //VH
  1456. {0x3808, 0x08}, //DVPHO=640
  1457. {0x3809, 0x00}, //DVPHO
  1458. {0x380a, 0x06}, //DVPVO=480
  1459. {0x380b, 0x00}, //DVPVO
  1460. {0x380c, 0x08}, //HTS=1808
  1461. {0x380d, 0xfc}, //HTS
  1462. {0x380e, 0x06}, //VTS=498
  1463. {0x380f, 0x1c}, //VTS
  1464. {0x3810, 0x00}, //HOFFSET
  1465. {0x3811, 0x10}, //HOFFSET
  1466. {0x3812, 0x00}, //VOFFSET
  1467. {0x3813, 0x06}, //VOFFSET
  1468. {0x3814, 0x11}, //X INC
  1469. {0x3815, 0x11}, //Y INC
  1470. //{0x3820, 0x40}, //FLIP
  1471. //{0x3821, 0x06}, //MIRROR
  1472. //{0x3824, 0x01}, //PCLK RATIO
  1473. {0x3826, 0x23},
  1474. {0x303a, 0x00},
  1475. {0x303b, 0x1b},//0x1b
  1476. {0x303c, 0x12},//0x11
  1477. {0x303d, 0x32},//30
  1478. {0x460c, 0x22},
  1479. {0x3824, 0x02},
  1480. {0x4005, 0x12},
  1481. {0x3618, 0x78},
  1482. {0x3708, 0x21},
  1483. {0x3709, 0x12},
  1484. {0x4300, 0x31}, //YUV422,YUYV
  1485. {0x440e, 0x09},
  1486. {0x4520, 0xb0},
  1487. {0x460b, 0x37},
  1488. {0x460c, 0x20},
  1489. {0x4713, 0x02},
  1490. {0x471c, 0xd0},
  1491. //{0x4514, 0xbb},
  1492. {0x3503, 0x03}, // turn on AGC/AEC
  1493. #if 0
  1494. //for jpeg
  1495. {0x440e, 0x08},
  1496. {0x3002, 0x00},
  1497. {0x3006, 0xff},
  1498. {0x4300, 0x30},
  1499. {0x5000, 0xa7},
  1500. {0x5001, 0xa7},
  1501. {0x501f, 0x00},
  1502. {0x3821, 0x20},
  1503. {0x4713, 0x02},
  1504. {0x460c, 0x22},
  1505. {0x3824, 0x04},
  1506. {0x460b, 0x35},
  1507. {0x471c, 0x50},
  1508. #endif
  1509. {0xffff, 0xff},
  1510. };
  1511. static struct aml_camera_i2c_fig_s pic1600x1200[]={
  1512. {0x5001,0x23},
  1513. {0x3800,0x00},
  1514. {0x3801,0x00},
  1515. {0x3802,0x00},
  1516. {0x3803,0x00},
  1517. {0x3804,0x08},
  1518. {0x3805,0x1f},
  1519. {0x3806,0x06},
  1520. {0x3807,0x0b},
  1521. {0x3808,0x06},
  1522. {0x3809,0x40},
  1523. {0x380a,0x04},
  1524. {0x380b,0xb0},
  1525. {0x380c,0x08},
  1526. {0x380d,0xfc},
  1527. {0x380e,0x06},
  1528. {0x380f,0x1c},
  1529. {0x3810,0x00},
  1530. {0x3811,0x10},
  1531. {0x3812,0x00},
  1532. {0x3813,0x06},
  1533. {0x3814,0x11}, // X inc
  1534. {0x3815,0x11}, // Y inc
  1535. //{0x3820,0x40}, // Vertical binning off
  1536. //{0x3821,0x00}, // Horizontal binning off
  1537. {0x3708,0x63},
  1538. {0x3709,0x12},
  1539. {0x370c,0x0c},
  1540. //{0x4520,0xb0},
  1541. //{0x3821,0x06}, // Mirror on,binning off
  1542. //{0x4514,0xbb},
  1543. {0x303a,0x00},
  1544. {0x303b,0x1b},
  1545. {0x303c,0x13},
  1546. {0x303d,0x32},
  1547. {0x3824,0x01},//0x02 can not work,black image
  1548. {0x460c,0x22},
  1549. //{0x3a10, 0x2d},
  1550. {0xffff, 0xff},
  1551. };
  1552. static struct aml_camera_i2c_fig_s pic800x600[]={
  1553. {0x5001,0x23},
  1554. {0x3503,0x00},
  1555. {0x303d,0x30},
  1556. {0x3800,0x00},
  1557. {0x3801,0x00},
  1558. {0x3802,0x00},
  1559. {0x3803,0x00},
  1560. {0x3804,0x08},
  1561. {0x3805,0x1f},
  1562. {0x3806,0x06},
  1563. {0x3807,0x0b},
  1564. {0x3808,0x03},
  1565. {0x3809,0x20},
  1566. {0x380a,0x02},
  1567. {0x380b,0x58},
  1568. {0x380c,0x08},
  1569. {0x380d,0xfc},
  1570. {0x380e,0x06},
  1571. {0x380f,0x6d},//1c
  1572. {0x3810,0x00},
  1573. {0x3811,0x10},
  1574. {0x3812,0x00},
  1575. {0x3813,0x06},
  1576. //{0x3820, 0x41}, //FLIP//40
  1577. //{0x3821, 0x07}, //MIRROR//04
  1578. {0x3814, 0x11},
  1579. {0x3815, 0x11},
  1580. //{0x3a10, 0x2d},
  1581. //pll
  1582. {0x303b, 0x1b},
  1583. {0x303c, 0x11},
  1584. {0x303d, 0x30},
  1585. {0x460c, 0x22},
  1586. {0x3824, 0x02},
  1587. {0xffff, 0xff},
  1588. };
  1589. static struct aml_camera_i2c_fig_s pic640x480[]={
  1590. {0x3008, 0x42},
  1591. {0x303c, 0x11},//12
  1592. {0x5001, 0xa3},
  1593. {0x3503, 0x00},
  1594. {0x3a00, 0x3c},
  1595. {0x5302, 0x30},
  1596. {0x5303, 0x10},
  1597. {0x5306, 0x18},
  1598. {0x5307, 0x28},
  1599. {0x3800, 0x00},
  1600. {0x3801, 0x00},
  1601. {0x3802, 0x00},
  1602. {0x3803, 0x00},
  1603. {0x3804, 0x08},
  1604. {0x3805, 0x1f},
  1605. {0x3806, 0x06},
  1606. {0x3807, 0x09},
  1607. {0x3808, 0x02},
  1608. {0x3809, 0x80},
  1609. {0x380a, 0x01},
  1610. {0x380b, 0xe0},
  1611. {0x3810, 0x00},
  1612. {0x3811, 0x08},
  1613. {0x3812, 0x00},
  1614. {0x3813, 0x02},
  1615. {0x3814, 0x31},
  1616. {0x3815, 0x31},
  1617. {0x3826, 0x23},
  1618. {0x303a, 0x00},
  1619. {0x303b, 0x1b},
  1620. {0x303c, 0x11},
  1621. {0x303d, 0x30},
  1622. {0x3824, 0x02},
  1623. {0x460c, 0x22},
  1624. {0x380c, 0x08},
  1625. {0x380d, 0xfc},
  1626. {0x380e, 0x03},
  1627. {0x380f, 0x10},
  1628. {0x3a08, 0x00},
  1629. {0x3a09, 0xeb},
  1630. {0x3a0e, 0x03},
  1631. {0x3a0a, 0x00},
  1632. {0x3a0b, 0xc4},
  1633. {0x3a0d, 0x04},
  1634. //{0x3820, 0x01},
  1635. //{0x3821, 0x07},
  1636. //{0x4514, 0xbb},
  1637. {0x3618, 0x00},
  1638. {0x3708, 0x66},
  1639. {0x3709, 0x12},
  1640. {0x4520, 0x0b},
  1641. {0x3008, 0x02},
  1642. //{0x3a10, 0x2d},
  1643. {0xffff, 0xff},
  1644. };
  1645. static struct aml_camera_i2c_fig_s pic320x240[]={
  1646. {0x5001,0x23},
  1647. {0x3503,0x00},
  1648. {0x303d,0x30},
  1649. {0x3800,0x00},
  1650. {0x3801,0x00},
  1651. {0x3802,0x00},
  1652. {0x3803,0x00},
  1653. {0x3804,0x08},
  1654. {0x3805,0x1f},
  1655. {0x3806,0x06},
  1656. {0x3807,0x0b},
  1657. {0x3808,0x01},
  1658. {0x3809,0x40},
  1659. {0x380a,0x00},
  1660. {0x380b,0xf0},
  1661. {0x380c,0x08},
  1662. {0x380d,0xfc},
  1663. {0x380e,0x06},
  1664. {0x380f,0x6d},
  1665. {0x3810,0x00},
  1666. {0x3811,0x10},
  1667. {0x3812,0x00},
  1668. {0x3813,0x06},
  1669. //{0x3820, 0x41}, //FLIP//40
  1670. //{0x3821, 0x07}, //MIRROR//04
  1671. {0x3814, 0x11},
  1672. {0x3815, 0x11},
  1673. //{0x3a10, 0x2d},
  1674. //pll
  1675. {0x303b, 0x1b},
  1676. {0x303c, 0x11},
  1677. {0x303d, 0x30},
  1678. {0x460c, 0x22},
  1679. {0x3824, 0x02},
  1680. {0xffff, 0xff},
  1681. };
  1682. static void pic_set_size(struct ov3660_device *dev, struct aml_camera_i2c_fig_s* size)
  1683. {
  1684. int i = 0;
  1685. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  1686. struct aml_camera_i2c_fig_s* pic = size;
  1687. i = 0;
  1688. while(1){
  1689. if (pic[i].val==0xff&&pic[i].addr==0xffff) {
  1690. //printk("OV3660 pic \n");
  1691. break;
  1692. }if((i2c_put_byte(client,pic[i].addr, pic[i].val)) < 0){
  1693. printk("fail in initial OV3660. i=%d\n",i);
  1694. // break;
  1695. }
  1696. i++;
  1697. }
  1698. }
  1699. void OV3660_set_resolution(struct ov3660_device *dev,int height,int width)
  1700. {
  1701. int ret;
  1702. //struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  1703. printk("<========%s w:%d, h:%d\n", __func__, width, height);
  1704. if((width<1600)&&(height<1200)){
  1705. //800*600
  1706. pic_set_size(dev, pic800x600);
  1707. //pic_set_size(dev, pic320x240);
  1708. mdelay(20);
  1709. ov3660_h_active=800;
  1710. ov3660_v_active=598;
  1711. //ov3660_h_active=320;
  1712. //ov3660_v_active=238;
  1713. } else if ((width>=1600&&height>=1200) && (width<2000&&height<1500)){
  1714. //2048x1536
  1715. //pic_set_size(dev, pic1600x1200);
  1716. pic_set_size(dev, pic2048x1536);
  1717. mdelay(20);
  1718. ov3660_h_active=1600;
  1719. ov3660_v_active=1200;
  1720. } else if (width>=2000&&height>=1500) {
  1721. //2048x1536
  1722. //pic_set_size(dev, pic1600x1200);
  1723. pic_set_size(dev, pic2048x1536);
  1724. mdelay(20);
  1725. ov3660_h_active=2032;
  1726. ov3660_v_active=1536;
  1727. }
  1728. } /* OV2659_set_resolution */
  1729. static unsigned long ov3660_preview_exposure;
  1730. static unsigned long ov3660_preview_extra_lines;
  1731. static unsigned long ov3660_gain;
  1732. static unsigned long ov3660_preview_maxlines;
  1733. static int Get_preview_exposure_gain(struct ov3660_device *dev)
  1734. {
  1735. int rc = 0;
  1736. unsigned int ret_l,ret_m,ret_h;
  1737. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  1738. ret_h = ret_m = ret_l = 0;
  1739. ret_h = i2c_get_byte(client, 0x350c);
  1740. ret_l = i2c_get_byte(client,0x350d);
  1741. ov3660_preview_extra_lines = ((ret_h << 8) + ret_l);
  1742. i2c_put_byte(client,0x3503, 0x03);//stop aec/agc
  1743. //get preview exp & gain
  1744. ret_h = ret_m = ret_l = 0;
  1745. ov3660_preview_exposure = 0;
  1746. ret_h = i2c_get_byte(client, 0x3500);
  1747. ret_m = i2c_get_byte(client,0x3501);
  1748. ret_l = i2c_get_byte(client,0x3502);
  1749. ov3660_preview_exposure = (ret_h << 12) + (ret_m << 4) + (ret_l >> 4);
  1750. ret_h = ret_m = ret_l = 0;
  1751. ov3660_preview_exposure = ov3660_preview_exposure + (ov3660_preview_extra_lines)/16;
  1752. printk("preview_exposure=%d\n", ov3660_preview_exposure);
  1753. ret_h = ret_m = ret_l = 0;
  1754. ov3660_preview_maxlines = 0;
  1755. ret_h = i2c_get_byte(client, 0x380e);
  1756. ret_l = i2c_get_byte(client, 0x380f);
  1757. ov3660_preview_maxlines = (ret_h << 8) + ret_l;
  1758. printk("Preview_Maxlines=%d\n", ov3660_preview_maxlines);
  1759. //Read back AGC Gain for preview
  1760. ov3660_gain = 0;
  1761. ov3660_gain = i2c_get_byte(client, 0x350b);
  1762. printk("Gain,0x350b=0x%x\n", ov3660_gain);
  1763. return rc;
  1764. }
  1765. #define CAPTURE_FRAMERATE 375
  1766. #define PREVIEW_FRAMERATE 1500
  1767. static int cal_exposure(struct ov3660_device *dev)
  1768. {
  1769. int rc = 0;
  1770. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  1771. //calculate capture exp & gain
  1772. unsigned char ExposureLow,ExposureMid,ExposureHigh,Capture_MaxLines_High,Capture_MaxLines_Low;
  1773. unsigned int ret_l,ret_m,ret_h,Lines_10ms;
  1774. unsigned short ulCapture_Exposure,iCapture_Gain;
  1775. unsigned int ulCapture_Exposure_Gain,Capture_MaxLines;
  1776. ret_h = ret_m = ret_l = 0;
  1777. ret_h = i2c_get_byte(client, 0x380e);
  1778. ret_l = i2c_get_byte(client, 0x380f);
  1779. Capture_MaxLines = (ret_h << 8) + ret_l;
  1780. Capture_MaxLines = Capture_MaxLines + (ov3660_preview_extra_lines)/16;
  1781. printk("Capture_MaxLines=%d\n", Capture_MaxLines);
  1782. if(ov3660_qctrl[7].default_value == CAM_BANDING_60HZ) //60Hz
  1783. {
  1784. Lines_10ms = CAPTURE_FRAMERATE * Capture_MaxLines/12000;
  1785. }
  1786. else
  1787. {
  1788. Lines_10ms = CAPTURE_FRAMERATE * Capture_MaxLines/10000;
  1789. }
  1790. if(ov3660_preview_maxlines == 0)
  1791. {
  1792. ov3660_preview_maxlines = 1;
  1793. }
  1794. ulCapture_Exposure = (ov3660_preview_exposure*(CAPTURE_FRAMERATE)*(Capture_MaxLines))/
  1795. (((ov3660_preview_maxlines)*(PREVIEW_FRAMERATE)));
  1796. iCapture_Gain = ov3660_gain;
  1797. ulCapture_Exposure_Gain = ulCapture_Exposure * iCapture_Gain;
  1798. if(ulCapture_Exposure_Gain < Capture_MaxLines*16)
  1799. {
  1800. ulCapture_Exposure = ulCapture_Exposure_Gain/16;
  1801. if (ulCapture_Exposure > Lines_10ms)
  1802. {
  1803. ulCapture_Exposure /= Lines_10ms;
  1804. ulCapture_Exposure *= Lines_10ms;
  1805. }
  1806. }
  1807. else
  1808. {
  1809. ulCapture_Exposure = Capture_MaxLines;
  1810. }
  1811. if(ulCapture_Exposure == 0)
  1812. {
  1813. ulCapture_Exposure = 1;
  1814. }
  1815. iCapture_Gain = (ulCapture_Exposure_Gain*2/ulCapture_Exposure + 1)/2;
  1816. ExposureLow = ((unsigned char)ulCapture_Exposure)<<4;
  1817. ExposureMid = (unsigned char)(ulCapture_Exposure >> 4) & 0xff;
  1818. ExposureHigh = (unsigned char)(ulCapture_Exposure >> 12);
  1819. Capture_MaxLines_Low = (unsigned char)(Capture_MaxLines & 0xff);
  1820. Capture_MaxLines_High = (unsigned char)(Capture_MaxLines >> 8);
  1821. i2c_put_byte(client, 0x380e, Capture_MaxLines_High);
  1822. i2c_put_byte(client, 0x380f, Capture_MaxLines_Low);
  1823. i2c_put_byte(client, 0x350b, iCapture_Gain);
  1824. i2c_put_byte(client, 0x3502, ExposureLow);
  1825. i2c_put_byte(client, 0x3501, ExposureMid);
  1826. i2c_put_byte(client, 0x3500, ExposureHigh);
  1827. printk("iCapture_Gain=%d\n", iCapture_Gain);
  1828. printk("ExposureLow=%d\n", ExposureLow);
  1829. printk("ExposureMid=%d\n", ExposureMid);
  1830. printk("ExposureHigh=%d\n", ExposureHigh);
  1831. msleep(250);
  1832. return rc;
  1833. }
  1834. /*FIXME: This seems to be generic enough to be at videodev2 */
  1835. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1836. struct v4l2_format *f)
  1837. {
  1838. struct ov3660_fh *fh = priv;
  1839. struct videobuf_queue *q = &fh->vb_vidq;
  1840. struct ov3660_device *dev = fh->dev;
  1841. struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
  1842. unsigned char Reg0x3503;
  1843. int ret = vidioc_try_fmt_vid_cap(file, fh, f);
  1844. if (ret < 0)
  1845. return ret;
  1846. mutex_lock(&q->vb_lock);
  1847. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  1848. dprintk(fh->dev, 1, "%s queue busy\n", __func__);
  1849. ret = -EBUSY;
  1850. goto out;
  1851. }
  1852. fh->fmt = get_format(f);
  1853. fh->width = f->fmt.pix.width;
  1854. fh->height = f->fmt.pix.height;
  1855. fh->vb_vidq.field = f->fmt.pix.field;
  1856. fh->type = f->type;
  1857. #if 1
  1858. if(f->fmt.pix.pixelformat==V4L2_PIX_FMT_RGB24){
  1859. Reg0x3503 = i2c_get_byte(client, 0x3503);
  1860. Reg0x3503 = Reg0x3503 | 0x03;
  1861. i2c_put_byte(client, 0x3503, Reg0x3503);
  1862. Get_preview_exposure_gain(dev);
  1863. OV3660_set_resolution(dev,fh->height,fh->width);
  1864. cal_exposure(dev);
  1865. }
  1866. else {
  1867. Reg0x3503 = i2c_get_byte(client, 0x3503);
  1868. Reg0x3503 = Reg0x3503& (~0x03);
  1869. i2c_put_byte(client, 0x3503, Reg0x3503);
  1870. OV3660_set_resolution(dev,fh->height,fh->width);
  1871. }
  1872. #endif
  1873. #ifdef CONFIG_VIDEO_AMLOGIC_FLASHLIGHT
  1874. if (dev->platform_dev_data.flash_support) {
  1875. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1876. if (get_flashlightflag() == FLASHLIGHT_ON) {
  1877. set_flashlight(true);
  1878. }
  1879. } else if(f->fmt.pix.pixelformat == V4L2_PIX_FMT_NV21){
  1880. if (get_flashlightflag() != FLASHLIGHT_TORCH) {
  1881. set_flashlight(false);
  1882. }
  1883. }
  1884. }
  1885. #endif
  1886. ret = 0;
  1887. out:
  1888. mutex_unlock(&q->vb_lock);
  1889. return ret;
  1890. }
  1891. static int vidioc_reqbufs(struct file *file, void *priv,
  1892. struct v4l2_requestbuffers *p)
  1893. {
  1894. struct ov3660_fh *fh = priv;
  1895. return (videobuf_reqbufs(&fh->vb_vidq, p));
  1896. }
  1897. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  1898. {
  1899. struct ov3660_fh *fh = priv;
  1900. return (videobuf_querybuf(&fh->vb_vidq, p));
  1901. }
  1902. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  1903. {
  1904. struct ov3660_fh *fh = priv;
  1905. return (videobuf_qbuf(&fh->vb_vidq, p));
  1906. }
  1907. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  1908. {
  1909. struct ov3660_fh *fh = priv;
  1910. return (videobuf_dqbuf(&fh->vb_vidq, p,
  1911. file->f_flags & O_NONBLOCK));
  1912. }
  1913. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1914. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1915. {
  1916. struct ov3660_fh *fh = priv;
  1917. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1918. }
  1919. #endif
  1920. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  1921. {
  1922. struct ov3660_fh *fh = priv;
  1923. tvin_parm_t para;
  1924. int ret = 0 ;
  1925. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1926. return -EINVAL;
  1927. if (i != fh->type)
  1928. return -EINVAL;
  1929. para.port = TVIN_PORT_CAMERA;
  1930. para.fmt_info.fmt = TVIN_SIG_FMT_MAX+1;//TVIN_SIG_FMT_CAMERA_1280X720P_30Hz;
  1931. para.fmt_info.frame_rate = 236;
  1932. para.fmt_info.h_active = ov3660_h_active;
  1933. para.fmt_info.v_active = ov3660_v_active;
  1934. para.fmt_info.hsync_phase = 0;//0x1
  1935. para.fmt_info.vsync_phase = 1;//0x1
  1936. ret = videobuf_streamon(&fh->vb_vidq);
  1937. if(ret == 0){
  1938. start_tvin_service(0,&para);
  1939. fh->stream_on = 1;
  1940. }
  1941. return ret;
  1942. }
  1943. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1944. {
  1945. struct ov3660_fh *fh = priv;
  1946. int ret = 0 ;
  1947. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1948. return -EINVAL;
  1949. if (i != fh->type)
  1950. return -EINVAL;
  1951. ret = videobuf_streamoff(&fh->vb_vidq);
  1952. if(ret == 0 ){
  1953. stop_tvin_service(0);
  1954. fh->stream_on = 0;
  1955. }
  1956. return ret;
  1957. }
  1958. static int vidioc_enum_framesizes(struct file *file, void *fh,struct v4l2_frmsizeenum *fsize)
  1959. {
  1960. int ret = 0,i=0;
  1961. struct ov3660_fmt *fmt = NULL;
  1962. struct v4l2_frmsize_discrete *frmsize = NULL;
  1963. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  1964. if (formats[i].fourcc == fsize->pixel_format){
  1965. fmt = &formats[i];
  1966. break;
  1967. }
  1968. }
  1969. if (fmt == NULL)
  1970. return -EINVAL;
  1971. if (fmt->fourcc == V4L2_PIX_FMT_NV21){
  1972. if (fsize->index >= ARRAY_SIZE(ov3660_prev_resolution))
  1973. return -EINVAL;
  1974. frmsize = &ov3660_prev_resolution[fsize->index];
  1975. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1976. fsize->discrete.width = frmsize->width;
  1977. fsize->discrete.height = frmsize->height;
  1978. }
  1979. else if(fmt->fourcc == V4L2_PIX_FMT_RGB24){
  1980. if (fsize->index >= ARRAY_SIZE(ov3660_pic_resolution))
  1981. return -EINVAL;
  1982. frmsize = &ov3660_pic_resolution[fsize->index];
  1983. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1984. fsize->discrete.width = frmsize->width;
  1985. fsize->discrete.height = frmsize->height;
  1986. }
  1987. return ret;
  1988. }
  1989. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
  1990. {
  1991. return 0;
  1992. }
  1993. /* only one input in this sample driver */
  1994. static int vidioc_enum_input(struct file *file, void *priv,
  1995. struct v4l2_input *inp)
  1996. {
  1997. //if (inp->index >= NUM_INPUTS)
  1998. //return -EINVAL;
  1999. inp->type = V4L2_INPUT_TYPE_CAMERA;
  2000. inp->std = V4L2_STD_525_60;
  2001. sprintf(inp->name, "Camera %u", inp->index);
  2002. return (0);
  2003. }
  2004. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  2005. {
  2006. struct ov3660_fh *fh = priv;
  2007. struct ov3660_device *dev = fh->dev;
  2008. *i = dev->input;
  2009. return (0);
  2010. }
  2011. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  2012. {
  2013. struct ov3660_fh *fh = priv;
  2014. struct ov3660_device *dev = fh->dev;
  2015. //if (i >= NUM_INPUTS)
  2016. //return -EINVAL;
  2017. dev->input = i;
  2018. //precalculate_bars(fh);
  2019. return (0);
  2020. }
  2021. /* --- controls ---------------------------------------------- */
  2022. static int vidioc_queryctrl(struct file *file, void *priv,
  2023. struct v4l2_queryctrl *qc)
  2024. {
  2025. int i;
  2026. for (i = 0; i < ARRAY_SIZE(ov3660_qctrl); i++)
  2027. if (qc->id && qc->id == ov3660_qctrl[i].id) {
  2028. memcpy(qc, &(ov3660_qctrl[i]),
  2029. sizeof(*qc));
  2030. return (0);
  2031. }
  2032. return -EINVAL;
  2033. }
  2034. static int vidioc_g_ctrl(struct file *file, void *priv,
  2035. struct v4l2_control *ctrl)
  2036. {
  2037. struct ov3660_fh *fh = priv;
  2038. struct ov3660_device *dev = fh->dev;
  2039. int i;
  2040. for (i = 0; i < ARRAY_SIZE(ov3660_qctrl); i++)
  2041. if (ctrl->id == ov3660_qctrl[i].id) {
  2042. ctrl->value = dev->qctl_regs[i];
  2043. return 0;
  2044. }
  2045. return -EINVAL;
  2046. }
  2047. static int vidioc_s_ctrl(struct file *file, void *priv,
  2048. struct v4l2_control *ctrl)
  2049. {
  2050. struct ov3660_fh *fh = priv;
  2051. struct ov3660_device *dev = fh->dev;
  2052. int i;
  2053. for (i = 0; i < ARRAY_SIZE(ov3660_qctrl); i++)
  2054. if (ctrl->id == ov3660_qctrl[i].id) {
  2055. if (ctrl->value < ov3660_qctrl[i].minimum ||
  2056. ctrl->value > ov3660_qctrl[i].maximum ||
  2057. ov3660_setting(dev,ctrl->id,ctrl->value)<0) {
  2058. return -ERANGE;
  2059. }
  2060. dev->qctl_regs[i] = ctrl->value;
  2061. return 0;
  2062. }
  2063. return -EINVAL;
  2064. }
  2065. /* ------------------------------------------------------------------
  2066. File operations for the device
  2067. ------------------------------------------------------------------*/
  2068. static int ov3660_open(struct file *file)
  2069. {
  2070. struct ov3660_device *dev = video_drvdata(file);
  2071. struct ov3660_fh *fh = NULL;
  2072. int retval = 0;
  2073. #ifdef CONFIG_ARCH_MESON6
  2074. switch_mod_gate_by_name("ge2d", 1);
  2075. #endif
  2076. if(dev->platform_dev_data.device_init) {
  2077. dev->platform_dev_data.device_init();
  2078. printk("+++found a init function, and run it..\n");
  2079. }
  2080. OV3660_init_regs(dev);
  2081. mutex_lock(&dev->mutex);
  2082. dev->users++;
  2083. if (dev->users > 1) {
  2084. dev->users--;
  2085. mutex_unlock(&dev->mutex);
  2086. return -EBUSY;
  2087. }
  2088. dprintk(dev, 1, "open %s type=%s users=%d\n",
  2089. video_device_node_name(dev->vdev),
  2090. v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
  2091. /* init video dma queues */
  2092. INIT_LIST_HEAD(&dev->vidq.active);
  2093. init_waitqueue_head(&dev->vidq.wq);
  2094. spin_lock_init(&dev->slock);
  2095. /* allocate + initialize per filehandle data */
  2096. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  2097. if (NULL == fh) {
  2098. dev->users--;
  2099. retval = -ENOMEM;
  2100. }
  2101. mutex_unlock(&dev->mutex);
  2102. if (retval)
  2103. return retval;
  2104. file->private_data = fh;
  2105. fh->dev = dev;
  2106. fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2107. fh->fmt = &formats[0];
  2108. fh->width = 640;
  2109. fh->height = 480;
  2110. fh->stream_on = 0 ;
  2111. /* Resets frame counters */
  2112. dev->jiffies = jiffies;
  2113. // TVIN_SIG_FMT_CAMERA_640X480P_30Hz,
  2114. // TVIN_SIG_FMT_CAMERA_800X600P_30Hz,
  2115. // TVIN_SIG_FMT_CAMERA_1024X768P_30Hz, // 190
  2116. // TVIN_SIG_FMT_CAMERA_1920X1080P_30Hz,
  2117. // TVIN_SIG_FMT_CAMERA_1280X720P_30Hz,
  2118. videobuf_queue_vmalloc_init(&fh->vb_vidq, &ov3660_video_qops,
  2119. NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
  2120. sizeof(struct ov3660_buffer), fh,NULL);
  2121. ov3660_start_thread(fh);
  2122. return 0;
  2123. }
  2124. static ssize_t
  2125. ov3660_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  2126. {
  2127. struct ov3660_fh *fh = file->private_data;
  2128. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2129. return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
  2130. file->f_flags & O_NONBLOCK);
  2131. }
  2132. return 0;
  2133. }
  2134. static unsigned int
  2135. ov3660_poll(struct file *file, struct poll_table_struct *wait)
  2136. {
  2137. struct ov3660_fh *fh = file->private_data;
  2138. struct ov3660_device *dev = fh->dev;
  2139. struct videobuf_queue *q = &fh->vb_vidq;
  2140. dprintk(dev, 1, "%s\n", __func__);
  2141. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  2142. return POLLERR;
  2143. return videobuf_poll_stream(file, q, wait);
  2144. }
  2145. static int ov3660_close(struct file *file)
  2146. {
  2147. struct ov3660_fh *fh = file->private_data;
  2148. struct ov3660_device *dev = fh->dev;
  2149. struct ov3660_dmaqueue *vidq = &dev->vidq;
  2150. struct video_device *vdev = video_devdata(file);
  2151. ov3660_stop_thread(vidq);
  2152. videobuf_stop(&fh->vb_vidq);
  2153. if(fh->stream_on){
  2154. stop_tvin_service(0);
  2155. }
  2156. videobuf_mmap_free(&fh->vb_vidq);
  2157. kfree(fh);
  2158. mutex_lock(&dev->mutex);
  2159. dev->users--;
  2160. mutex_unlock(&dev->mutex);
  2161. dprintk(dev, 1, "close called (dev=%s, users=%d)\n",
  2162. video_device_node_name(vdev), dev->users);
  2163. #if 1
  2164. ov3660_h_active=800;
  2165. ov3660_v_active=600;
  2166. power_down_ov3660(dev);
  2167. #endif
  2168. msleep(2);
  2169. if(dev->platform_dev_data.device_uninit) {
  2170. dev->platform_dev_data.device_uninit();
  2171. printk("+++found a uninit function, and run it..\n");
  2172. }
  2173. #ifdef CONFIG_ARCH_MESON6
  2174. switch_mod_gate_by_name("ge2d", 0);
  2175. #endif
  2176. return 0;
  2177. }
  2178. static int ov3660_mmap(struct file *file, struct vm_area_struct *vma)
  2179. {
  2180. struct ov3660_fh *fh = file->private_data;
  2181. struct ov3660_device *dev = fh->dev;
  2182. int ret;
  2183. dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
  2184. ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  2185. dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
  2186. (unsigned long)vma->vm_start,
  2187. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  2188. ret);
  2189. return ret;
  2190. }
  2191. static const struct v4l2_file_operations ov3660_fops = {
  2192. .owner = THIS_MODULE,
  2193. .open = ov3660_open,
  2194. .release = ov3660_close,
  2195. .read = ov3660_read,
  2196. .poll = ov3660_poll,
  2197. .ioctl = video_ioctl2, /* V4L2 ioctl handler */
  2198. .mmap = ov3660_mmap,
  2199. };
  2200. static const struct v4l2_ioctl_ops ov3660_ioctl_ops = {
  2201. .vidioc_querycap = vidioc_querycap,
  2202. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  2203. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  2204. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  2205. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  2206. .vidioc_reqbufs = vidioc_reqbufs,
  2207. .vidioc_querybuf = vidioc_querybuf,
  2208. .vidioc_qbuf = vidioc_qbuf,
  2209. .vidioc_dqbuf = vidioc_dqbuf,
  2210. .vidioc_s_std = vidioc_s_std,
  2211. .vidioc_enum_input = vidioc_enum_input,
  2212. .vidioc_g_input = vidioc_g_input,
  2213. .vidioc_s_input = vidioc_s_input,
  2214. .vidioc_queryctrl = vidioc_queryctrl,
  2215. .vidioc_g_ctrl = vidioc_g_ctrl,
  2216. .vidioc_s_ctrl = vidioc_s_ctrl,
  2217. .vidioc_streamon = vidioc_streamon,
  2218. .vidioc_streamoff = vidioc_streamoff,
  2219. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  2220. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  2221. .vidiocgmbuf = vidiocgmbuf,
  2222. #endif
  2223. };
  2224. static struct video_device ov3660_template = {
  2225. .name = "ov3660_v4l",
  2226. .fops = &ov3660_fops,
  2227. .ioctl_ops = &ov3660_ioctl_ops,
  2228. .release = video_device_release,
  2229. .tvnorms = V4L2_STD_525_60,
  2230. .current_norm = V4L2_STD_NTSC_M,
  2231. };
  2232. static int ov3660_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
  2233. {
  2234. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2235. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_OV3660, 0);
  2236. }
  2237. static const struct v4l2_subdev_core_ops ov3660_core_ops = {
  2238. .g_chip_ident = ov3660_g_chip_ident,
  2239. };
  2240. static const struct v4l2_subdev_ops ov3660_ops = {
  2241. .core = &ov3660_core_ops,
  2242. };
  2243. #ifdef CONFIG_HAS_EARLYSUSPEND
  2244. static void aml_ov3660_early_suspend(struct early_suspend *h)
  2245. {
  2246. printk("enter -----> %s \n",__FUNCTION__);
  2247. if(h && h->param) {
  2248. aml_plat_cam_data_t* plat_dat= (aml_plat_cam_data_t*)h->param;
  2249. if (plat_dat && plat_dat->early_suspend) {
  2250. plat_dat->early_suspend();
  2251. }
  2252. }
  2253. }
  2254. static void aml_ov3660_late_resume(struct early_suspend *h)
  2255. {
  2256. printk("enter -----> %s \n",__FUNCTION__);
  2257. if(h && h->param) {
  2258. aml_plat_cam_data_t* plat_dat= (aml_plat_cam_data_t*)h->param;
  2259. if (plat_dat && plat_dat->late_resume) {
  2260. plat_dat->late_resume();
  2261. }
  2262. }
  2263. }
  2264. #endif
  2265. #ifdef EMDOOR_DEBUG_OV3660
  2266. //add by emdoor jf.s for debug ov3660
  2267. unsigned int ov3660_reg_addr;
  2268. static ssize_t ov3660_show(struct kobject *kobj, struct kobj_attribute *attr,
  2269. char *buf)
  2270. {
  2271. unsigned char dat;
  2272. dat = i2c_get_byte(ov3660_client, ov3660_reg_addr);
  2273. return sprintf(buf, "REG[0x%x]=0x%x\n", ov3660_reg_addr, dat);
  2274. }
  2275. static ssize_t ov3660_store(struct kobject *kobj, struct kobj_attribute *attr,
  2276. const char *buf, size_t count)
  2277. {
  2278. int tmp;
  2279. unsigned short reg;
  2280. unsigned char val;
  2281. tmp = simple_strtoul(buf, NULL, 16);
  2282. //sscanf(buf, "%du", &tmp);
  2283. if(tmp < 0xffff)
  2284. ov3660_reg_addr = tmp;
  2285. else {
  2286. reg = (tmp >> 8) & 0xFFFF; //reg
  2287. ov3660_reg_addr = reg;
  2288. val = tmp & 0xFF; //val
  2289. i2c_put_byte(ov3660_client, reg, val);
  2290. }
  2291. return count;
  2292. }
  2293. static struct kobj_attribute ov3660_attribute = __ATTR(ov3660, 0666, ov3660_show, ov3660_store);
  2294. static struct attribute *ov3660_attrs[] = {
  2295. &ov3660_attribute.attr,
  2296. NULL,
  2297. };
  2298. static const struct attribute_group ov3660_group =
  2299. {
  2300. .attrs = ov3660_attrs,
  2301. };
  2302. #endif
  2303. static int ov3660_probe(struct i2c_client *client,
  2304. const struct i2c_device_id *id)
  2305. {
  2306. int pgbuf;
  2307. int err;
  2308. struct ov3660_device *t;
  2309. struct v4l2_subdev *sd;
  2310. v4l_info(client, "chip found @ 0x%x (%s)\n",
  2311. client->addr << 1, client->adapter->name);
  2312. t = kzalloc(sizeof(*t), GFP_KERNEL);
  2313. if (t == NULL)
  2314. return -ENOMEM;
  2315. sd = &t->sd;
  2316. v4l2_i2c_subdev_init(sd, client, &ov3660_ops);
  2317. mutex_init(&t->mutex);
  2318. /* Now create a video4linux device */
  2319. t->vdev = video_device_alloc();
  2320. if (t->vdev == NULL) {
  2321. kfree(t);
  2322. kfree(client);
  2323. return -ENOMEM;
  2324. }
  2325. memcpy(t->vdev, &ov3660_template, sizeof(*t->vdev));
  2326. video_set_drvdata(t->vdev, t);
  2327. /* Register it */
  2328. aml_plat_cam_data_t* plat_dat= (aml_plat_cam_data_t*)client->dev.platform_data;
  2329. if (plat_dat) {
  2330. t->platform_dev_data.device_init=plat_dat->device_init;
  2331. t->platform_dev_data.device_uninit=plat_dat->device_uninit;
  2332. t->platform_dev_data.flash_support=plat_dat->flash_support;
  2333. if(plat_dat->video_nr>=0) video_nr=plat_dat->video_nr;
  2334. }
  2335. err = video_register_device(t->vdev, VFL_TYPE_GRABBER, video_nr);
  2336. if (err < 0) {
  2337. video_device_release(t->vdev);
  2338. kfree(t);
  2339. return err;
  2340. }
  2341. #ifdef CONFIG_HAS_EARLYSUSPEND
  2342. printk("******* enter itk early suspend register *******\n");
  2343. ov3660_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN;
  2344. ov3660_early_suspend.suspend = aml_ov3660_early_suspend;
  2345. ov3660_early_suspend.resume = aml_ov3660_late_resume;
  2346. ov3660_early_suspend.param = plat_dat;
  2347. register_early_suspend(&ov3660_early_suspend);
  2348. #endif
  2349. #ifdef EMDOOR_DEBUG_OV3660
  2350. //add by emdoor jf.s for debug ov3660
  2351. ov3660_client = client;
  2352. sysfs_create_group(&client->dev.kobj, &ov3660_group);
  2353. #endif
  2354. return 0;
  2355. }
  2356. static int ov3660_remove(struct i2c_client *client)
  2357. {
  2358. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2359. struct ov3660_device *t = to_dev(sd);
  2360. video_unregister_device(t->vdev);
  2361. v4l2_device_unregister_subdev(sd);
  2362. kfree(t);
  2363. return 0;
  2364. }
  2365. static int ov3660_suspend(struct i2c_client *client, pm_message_t state)
  2366. {
  2367. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2368. struct ov3660_device *t = to_dev(sd);
  2369. struct ov3660_fh *fh = to_fh(t);
  2370. if(fh->stream_on == 1){
  2371. stop_tvin_service(0);
  2372. }
  2373. power_down_ov3660(t);
  2374. return 0;
  2375. }
  2376. static int ov3660_resume(struct i2c_client *client)
  2377. {
  2378. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2379. struct ov3660_device *t = to_dev(sd);
  2380. struct ov3660_fh *fh = to_fh(t);
  2381. tvin_parm_t para;
  2382. para.port = TVIN_PORT_CAMERA;
  2383. para.fmt_info.fmt = TVIN_SIG_FMT_CAMERA_1280X720P_30Hz;
  2384. //OV3660_init_regs(t);
  2385. if(fh->stream_on == 1){
  2386. start_tvin_service(0,&para);
  2387. }
  2388. return 0;
  2389. }
  2390. static const struct i2c_device_id ov3660_id[] = {
  2391. { "ov3660_i2c", 0 },
  2392. { }
  2393. };
  2394. MODULE_DEVICE_TABLE(i2c, ov3660_id);
  2395. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  2396. .name = "ov3660",
  2397. .probe = ov3660_probe,
  2398. .remove = ov3660_remove,
  2399. .suspend = ov3660_suspend,
  2400. .resume = ov3660_resume,
  2401. .id_table = ov3660_id,
  2402. };