ov5640.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790
  1. /*
  2. * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
  3. * Copyright (C) 2014-2017 Mentor Graphics Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/clkdev.h>
  13. #include <linux/ctype.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/i2c.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/of_device.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <media/v4l2-async.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-fwnode.h>
  28. #include <media/v4l2-subdev.h>
  29. /* min/typical/max system clock (xclk) frequencies */
  30. #define OV5640_XCLK_MIN 6000000
  31. #define OV5640_XCLK_MAX 54000000
  32. #define OV5640_DEFAULT_SLAVE_ID 0x3c
  33. #define OV5640_REG_SYS_RESET02 0x3002
  34. #define OV5640_REG_SYS_CLOCK_ENABLE02 0x3006
  35. #define OV5640_REG_SYS_CTRL0 0x3008
  36. #define OV5640_REG_CHIP_ID 0x300a
  37. #define OV5640_REG_IO_MIPI_CTRL00 0x300e
  38. #define OV5640_REG_PAD_OUTPUT_ENABLE01 0x3017
  39. #define OV5640_REG_PAD_OUTPUT_ENABLE02 0x3018
  40. #define OV5640_REG_PAD_OUTPUT00 0x3019
  41. #define OV5640_REG_SYSTEM_CONTROL1 0x302e
  42. #define OV5640_REG_SC_PLL_CTRL0 0x3034
  43. #define OV5640_REG_SC_PLL_CTRL1 0x3035
  44. #define OV5640_REG_SC_PLL_CTRL2 0x3036
  45. #define OV5640_REG_SC_PLL_CTRL3 0x3037
  46. #define OV5640_REG_SLAVE_ID 0x3100
  47. #define OV5640_REG_SCCB_SYS_CTRL1 0x3103
  48. #define OV5640_REG_SYS_ROOT_DIVIDER 0x3108
  49. #define OV5640_REG_AWB_R_GAIN 0x3400
  50. #define OV5640_REG_AWB_G_GAIN 0x3402
  51. #define OV5640_REG_AWB_B_GAIN 0x3404
  52. #define OV5640_REG_AWB_MANUAL_CTRL 0x3406
  53. #define OV5640_REG_AEC_PK_EXPOSURE_HI 0x3500
  54. #define OV5640_REG_AEC_PK_EXPOSURE_MED 0x3501
  55. #define OV5640_REG_AEC_PK_EXPOSURE_LO 0x3502
  56. #define OV5640_REG_AEC_PK_MANUAL 0x3503
  57. #define OV5640_REG_AEC_PK_REAL_GAIN 0x350a
  58. #define OV5640_REG_AEC_PK_VTS 0x350c
  59. #define OV5640_REG_TIMING_DVPHO 0x3808
  60. #define OV5640_REG_TIMING_DVPVO 0x380a
  61. #define OV5640_REG_TIMING_HTS 0x380c
  62. #define OV5640_REG_TIMING_VTS 0x380e
  63. #define OV5640_REG_TIMING_TC_REG20 0x3820
  64. #define OV5640_REG_TIMING_TC_REG21 0x3821
  65. #define OV5640_REG_AEC_CTRL00 0x3a00
  66. #define OV5640_REG_AEC_B50_STEP 0x3a08
  67. #define OV5640_REG_AEC_B60_STEP 0x3a0a
  68. #define OV5640_REG_AEC_CTRL0D 0x3a0d
  69. #define OV5640_REG_AEC_CTRL0E 0x3a0e
  70. #define OV5640_REG_AEC_CTRL0F 0x3a0f
  71. #define OV5640_REG_AEC_CTRL10 0x3a10
  72. #define OV5640_REG_AEC_CTRL11 0x3a11
  73. #define OV5640_REG_AEC_CTRL1B 0x3a1b
  74. #define OV5640_REG_AEC_CTRL1E 0x3a1e
  75. #define OV5640_REG_AEC_CTRL1F 0x3a1f
  76. #define OV5640_REG_HZ5060_CTRL00 0x3c00
  77. #define OV5640_REG_HZ5060_CTRL01 0x3c01
  78. #define OV5640_REG_SIGMADELTA_CTRL0C 0x3c0c
  79. #define OV5640_REG_FRAME_CTRL01 0x4202
  80. #define OV5640_REG_FORMAT_CONTROL00 0x4300
  81. #define OV5640_REG_POLARITY_CTRL00 0x4740
  82. #define OV5640_REG_MIPI_CTRL00 0x4800
  83. #define OV5640_REG_DEBUG_MODE 0x4814
  84. #define OV5640_REG_ISP_FORMAT_MUX_CTRL 0x501f
  85. #define OV5640_REG_PRE_ISP_TEST_SET1 0x503d
  86. #define OV5640_REG_SDE_CTRL0 0x5580
  87. #define OV5640_REG_SDE_CTRL1 0x5581
  88. #define OV5640_REG_SDE_CTRL3 0x5583
  89. #define OV5640_REG_SDE_CTRL4 0x5584
  90. #define OV5640_REG_SDE_CTRL5 0x5585
  91. #define OV5640_REG_AVG_READOUT 0x56a1
  92. #define OV5640_SCLK2X_ROOT_DIVIDER_DEFAULT 1
  93. #define OV5640_SCLK_ROOT_DIVIDER_DEFAULT 2
  94. enum ov5640_mode_id {
  95. OV5640_MODE_QCIF_176_144 = 0,
  96. OV5640_MODE_QVGA_320_240,
  97. OV5640_MODE_VGA_640_480,
  98. OV5640_MODE_NTSC_720_480,
  99. OV5640_MODE_PAL_720_576,
  100. OV5640_MODE_XGA_1024_768,
  101. OV5640_MODE_720P_1280_720,
  102. OV5640_MODE_1080P_1920_1080,
  103. OV5640_MODE_QSXGA_2592_1944,
  104. OV5640_NUM_MODES,
  105. };
  106. enum ov5640_frame_rate {
  107. OV5640_15_FPS = 0,
  108. OV5640_30_FPS,
  109. OV5640_NUM_FRAMERATES,
  110. };
  111. struct ov5640_pixfmt {
  112. u32 code;
  113. u32 colorspace;
  114. };
  115. static const struct ov5640_pixfmt ov5640_formats[] = {
  116. { MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, },
  117. { MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, },
  118. { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, },
  119. { MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, },
  120. { MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, },
  121. };
  122. /*
  123. * FIXME: remove this when a subdev API becomes available
  124. * to set the MIPI CSI-2 virtual channel.
  125. */
  126. static unsigned int virtual_channel;
  127. module_param(virtual_channel, uint, 0444);
  128. MODULE_PARM_DESC(virtual_channel,
  129. "MIPI CSI-2 virtual channel (0..3), default 0");
  130. static const int ov5640_framerates[] = {
  131. [OV5640_15_FPS] = 15,
  132. [OV5640_30_FPS] = 30,
  133. };
  134. /* regulator supplies */
  135. static const char * const ov5640_supply_name[] = {
  136. "DOVDD", /* Digital I/O (1.8V) supply */
  137. "DVDD", /* Digital Core (1.5V) supply */
  138. "AVDD", /* Analog (2.8V) supply */
  139. };
  140. #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
  141. /*
  142. * Image size under 1280 * 960 are SUBSAMPLING
  143. * Image size upper 1280 * 960 are SCALING
  144. */
  145. enum ov5640_downsize_mode {
  146. SUBSAMPLING,
  147. SCALING,
  148. };
  149. struct reg_value {
  150. u16 reg_addr;
  151. u8 val;
  152. u8 mask;
  153. u32 delay_ms;
  154. };
  155. struct ov5640_mode_info {
  156. enum ov5640_mode_id id;
  157. enum ov5640_downsize_mode dn_mode;
  158. u32 hact;
  159. u32 htot;
  160. u32 vact;
  161. u32 vtot;
  162. const struct reg_value *reg_data;
  163. u32 reg_data_size;
  164. };
  165. struct ov5640_ctrls {
  166. struct v4l2_ctrl_handler handler;
  167. struct {
  168. struct v4l2_ctrl *auto_exp;
  169. struct v4l2_ctrl *exposure;
  170. };
  171. struct {
  172. struct v4l2_ctrl *auto_wb;
  173. struct v4l2_ctrl *blue_balance;
  174. struct v4l2_ctrl *red_balance;
  175. };
  176. struct {
  177. struct v4l2_ctrl *auto_gain;
  178. struct v4l2_ctrl *gain;
  179. };
  180. struct v4l2_ctrl *brightness;
  181. struct v4l2_ctrl *light_freq;
  182. struct v4l2_ctrl *saturation;
  183. struct v4l2_ctrl *contrast;
  184. struct v4l2_ctrl *hue;
  185. struct v4l2_ctrl *test_pattern;
  186. struct v4l2_ctrl *hflip;
  187. struct v4l2_ctrl *vflip;
  188. };
  189. struct ov5640_dev {
  190. struct i2c_client *i2c_client;
  191. struct v4l2_subdev sd;
  192. struct media_pad pad;
  193. struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
  194. struct clk *xclk; /* system clock to OV5640 */
  195. u32 xclk_freq;
  196. struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
  197. struct gpio_desc *reset_gpio;
  198. struct gpio_desc *pwdn_gpio;
  199. bool upside_down;
  200. /* lock to protect all members below */
  201. struct mutex lock;
  202. int power_count;
  203. struct v4l2_mbus_framefmt fmt;
  204. const struct ov5640_mode_info *current_mode;
  205. enum ov5640_frame_rate current_fr;
  206. struct v4l2_fract frame_interval;
  207. struct ov5640_ctrls ctrls;
  208. u32 prev_sysclk, prev_hts;
  209. u32 ae_low, ae_high, ae_target;
  210. bool pending_mode_change;
  211. bool streaming;
  212. };
  213. static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
  214. {
  215. return container_of(sd, struct ov5640_dev, sd);
  216. }
  217. static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
  218. {
  219. return &container_of(ctrl->handler, struct ov5640_dev,
  220. ctrls.handler)->sd;
  221. }
  222. /*
  223. * FIXME: all of these register tables are likely filled with
  224. * entries that set the register to their power-on default values,
  225. * and which are otherwise not touched by this driver. Those entries
  226. * should be identified and removed to speed register load time
  227. * over i2c.
  228. */
  229. static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
  230. {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
  231. {0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0},
  232. {0x3034, 0x18, 0, 0}, {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0},
  233. {0x3037, 0x13, 0, 0}, {0x3630, 0x36, 0, 0},
  234. {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
  235. {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
  236. {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
  237. {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
  238. {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
  239. {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
  240. {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
  241. {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
  242. {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
  243. {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
  244. {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
  245. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  246. {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
  247. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  248. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  249. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  250. {0x3810, 0x00, 0, 0},
  251. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
  252. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  253. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  254. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  255. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  256. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  257. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
  258. {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
  259. {0x300e, 0x45, 0, 0}, {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
  260. {0x501f, 0x00, 0, 0}, {0x4713, 0x03, 0, 0}, {0x4407, 0x04, 0, 0},
  261. {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  262. {0x4837, 0x0a, 0, 0}, {0x4800, 0x04, 0, 0}, {0x3824, 0x02, 0, 0},
  263. {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
  264. {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
  265. {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
  266. {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
  267. {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
  268. {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
  269. {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
  270. {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
  271. {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
  272. {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
  273. {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
  274. {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
  275. {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
  276. {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
  277. {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
  278. {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
  279. {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
  280. {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
  281. {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
  282. {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
  283. {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
  284. {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
  285. {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
  286. {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
  287. {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
  288. {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
  289. {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
  290. {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
  291. {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
  292. {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
  293. {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
  294. {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
  295. {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
  296. {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
  297. {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
  298. {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
  299. {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
  300. {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
  301. {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
  302. {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
  303. {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
  304. {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
  305. {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
  306. {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
  307. {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
  308. {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
  309. {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
  310. {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
  311. {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
  312. {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
  313. };
  314. static const struct reg_value ov5640_setting_30fps_VGA_640_480[] = {
  315. {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
  316. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  317. {0x3814, 0x31, 0, 0},
  318. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  319. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  320. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  321. {0x3810, 0x00, 0, 0},
  322. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
  323. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  324. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  325. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
  326. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  327. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  328. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
  329. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  330. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
  331. };
  332. static const struct reg_value ov5640_setting_15fps_VGA_640_480[] = {
  333. {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
  334. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  335. {0x3814, 0x31, 0, 0},
  336. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  337. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  338. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  339. {0x3810, 0x00, 0, 0},
  340. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
  341. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  342. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  343. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  344. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  345. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  346. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
  347. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  348. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
  349. };
  350. static const struct reg_value ov5640_setting_30fps_XGA_1024_768[] = {
  351. {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
  352. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  353. {0x3814, 0x31, 0, 0},
  354. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  355. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  356. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  357. {0x3810, 0x00, 0, 0},
  358. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
  359. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  360. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  361. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
  362. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  363. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  364. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
  365. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  366. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
  367. {0x3035, 0x12, 0, 0},
  368. };
  369. static const struct reg_value ov5640_setting_15fps_XGA_1024_768[] = {
  370. {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
  371. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  372. {0x3814, 0x31, 0, 0},
  373. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  374. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  375. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  376. {0x3810, 0x00, 0, 0},
  377. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
  378. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  379. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  380. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  381. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  382. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  383. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
  384. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  385. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
  386. };
  387. static const struct reg_value ov5640_setting_30fps_QVGA_320_240[] = {
  388. {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
  389. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  390. {0x3814, 0x31, 0, 0},
  391. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  392. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  393. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  394. {0x3810, 0x00, 0, 0},
  395. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
  396. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  397. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  398. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  399. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  400. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  401. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
  402. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  403. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
  404. };
  405. static const struct reg_value ov5640_setting_15fps_QVGA_320_240[] = {
  406. {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
  407. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  408. {0x3814, 0x31, 0, 0},
  409. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  410. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  411. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  412. {0x3810, 0x00, 0, 0},
  413. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
  414. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  415. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  416. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  417. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  418. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  419. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
  420. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  421. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
  422. };
  423. static const struct reg_value ov5640_setting_30fps_QCIF_176_144[] = {
  424. {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
  425. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  426. {0x3814, 0x31, 0, 0},
  427. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  428. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  429. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  430. {0x3810, 0x00, 0, 0},
  431. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
  432. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  433. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  434. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  435. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  436. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  437. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
  438. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  439. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
  440. };
  441. static const struct reg_value ov5640_setting_15fps_QCIF_176_144[] = {
  442. {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
  443. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  444. {0x3814, 0x31, 0, 0},
  445. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  446. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  447. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  448. {0x3810, 0x00, 0, 0},
  449. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
  450. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  451. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  452. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  453. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  454. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  455. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
  456. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  457. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
  458. };
  459. static const struct reg_value ov5640_setting_30fps_NTSC_720_480[] = {
  460. {0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
  461. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  462. {0x3814, 0x31, 0, 0},
  463. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  464. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  465. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  466. {0x3810, 0x00, 0, 0},
  467. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
  468. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  469. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  470. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  471. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  472. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  473. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
  474. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  475. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
  476. };
  477. static const struct reg_value ov5640_setting_15fps_NTSC_720_480[] = {
  478. {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
  479. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  480. {0x3814, 0x31, 0, 0},
  481. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  482. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  483. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  484. {0x3810, 0x00, 0, 0},
  485. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
  486. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  487. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  488. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  489. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  490. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  491. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
  492. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  493. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
  494. };
  495. static const struct reg_value ov5640_setting_30fps_PAL_720_576[] = {
  496. {0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
  497. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  498. {0x3814, 0x31, 0, 0},
  499. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  500. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  501. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  502. {0x3810, 0x00, 0, 0},
  503. {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
  504. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  505. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  506. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  507. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  508. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  509. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
  510. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  511. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
  512. };
  513. static const struct reg_value ov5640_setting_15fps_PAL_720_576[] = {
  514. {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0},
  515. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  516. {0x3814, 0x31, 0, 0},
  517. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  518. {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
  519. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
  520. {0x3810, 0x00, 0, 0},
  521. {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
  522. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  523. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  524. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  525. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  526. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  527. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
  528. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  529. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
  530. };
  531. static const struct reg_value ov5640_setting_30fps_720P_1280_720[] = {
  532. {0x3008, 0x42, 0, 0},
  533. {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
  534. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  535. {0x3814, 0x31, 0, 0},
  536. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  537. {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
  538. {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
  539. {0x3810, 0x00, 0, 0},
  540. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
  541. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  542. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
  543. {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
  544. {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
  545. {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
  546. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
  547. {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
  548. {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, {0x4005, 0x1a, 0, 0},
  549. {0x3008, 0x02, 0, 0}, {0x3503, 0, 0, 0},
  550. };
  551. static const struct reg_value ov5640_setting_15fps_720P_1280_720[] = {
  552. {0x3035, 0x41, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0},
  553. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  554. {0x3814, 0x31, 0, 0},
  555. {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  556. {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
  557. {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
  558. {0x3810, 0x00, 0, 0},
  559. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
  560. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  561. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
  562. {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
  563. {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
  564. {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
  565. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0},
  566. {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
  567. {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
  568. };
  569. static const struct reg_value ov5640_setting_30fps_1080P_1920_1080[] = {
  570. {0x3008, 0x42, 0, 0},
  571. {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
  572. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  573. {0x3814, 0x11, 0, 0},
  574. {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  575. {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
  576. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
  577. {0x3810, 0x00, 0, 0},
  578. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
  579. {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
  580. {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
  581. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  582. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  583. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  584. {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
  585. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  586. {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x11, 0, 0},
  587. {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
  588. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  589. {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
  590. {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
  591. {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0},
  592. {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
  593. {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
  594. {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
  595. {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
  596. {0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
  597. {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
  598. {0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0},
  599. {0x3503, 0, 0, 0},
  600. };
  601. static const struct reg_value ov5640_setting_15fps_1080P_1920_1080[] = {
  602. {0x3008, 0x42, 0, 0},
  603. {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
  604. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  605. {0x3814, 0x11, 0, 0},
  606. {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  607. {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
  608. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
  609. {0x3810, 0x00, 0, 0},
  610. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
  611. {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
  612. {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
  613. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  614. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  615. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  616. {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
  617. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  618. {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x21, 0, 0},
  619. {0x3036, 0x54, 0, 1}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
  620. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  621. {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
  622. {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
  623. {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0},
  624. {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
  625. {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
  626. {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
  627. {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
  628. {0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
  629. {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
  630. {0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3503, 0, 0, 0},
  631. };
  632. static const struct reg_value ov5640_setting_15fps_QSXGA_2592_1944[] = {
  633. {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0},
  634. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  635. {0x3814, 0x11, 0, 0},
  636. {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
  637. {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
  638. {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
  639. {0x3810, 0x00, 0, 0},
  640. {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
  641. {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
  642. {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
  643. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  644. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  645. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  646. {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
  647. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  648. {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
  649. };
  650. /* power-on sensor init reg table */
  651. static const struct ov5640_mode_info ov5640_mode_init_data = {
  652. 0, SUBSAMPLING, 640, 1896, 480, 984,
  653. ov5640_init_setting_30fps_VGA,
  654. ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
  655. };
  656. static const struct ov5640_mode_info
  657. ov5640_mode_data[OV5640_NUM_FRAMERATES][OV5640_NUM_MODES] = {
  658. {
  659. {OV5640_MODE_QCIF_176_144, SUBSAMPLING,
  660. 176, 1896, 144, 984,
  661. ov5640_setting_15fps_QCIF_176_144,
  662. ARRAY_SIZE(ov5640_setting_15fps_QCIF_176_144)},
  663. {OV5640_MODE_QVGA_320_240, SUBSAMPLING,
  664. 320, 1896, 240, 984,
  665. ov5640_setting_15fps_QVGA_320_240,
  666. ARRAY_SIZE(ov5640_setting_15fps_QVGA_320_240)},
  667. {OV5640_MODE_VGA_640_480, SUBSAMPLING,
  668. 640, 1896, 480, 1080,
  669. ov5640_setting_15fps_VGA_640_480,
  670. ARRAY_SIZE(ov5640_setting_15fps_VGA_640_480)},
  671. {OV5640_MODE_NTSC_720_480, SUBSAMPLING,
  672. 720, 1896, 480, 984,
  673. ov5640_setting_15fps_NTSC_720_480,
  674. ARRAY_SIZE(ov5640_setting_15fps_NTSC_720_480)},
  675. {OV5640_MODE_PAL_720_576, SUBSAMPLING,
  676. 720, 1896, 576, 984,
  677. ov5640_setting_15fps_PAL_720_576,
  678. ARRAY_SIZE(ov5640_setting_15fps_PAL_720_576)},
  679. {OV5640_MODE_XGA_1024_768, SUBSAMPLING,
  680. 1024, 1896, 768, 1080,
  681. ov5640_setting_15fps_XGA_1024_768,
  682. ARRAY_SIZE(ov5640_setting_15fps_XGA_1024_768)},
  683. {OV5640_MODE_720P_1280_720, SUBSAMPLING,
  684. 1280, 1892, 720, 740,
  685. ov5640_setting_15fps_720P_1280_720,
  686. ARRAY_SIZE(ov5640_setting_15fps_720P_1280_720)},
  687. {OV5640_MODE_1080P_1920_1080, SCALING,
  688. 1920, 2500, 1080, 1120,
  689. ov5640_setting_15fps_1080P_1920_1080,
  690. ARRAY_SIZE(ov5640_setting_15fps_1080P_1920_1080)},
  691. {OV5640_MODE_QSXGA_2592_1944, SCALING,
  692. 2592, 2844, 1944, 1968,
  693. ov5640_setting_15fps_QSXGA_2592_1944,
  694. ARRAY_SIZE(ov5640_setting_15fps_QSXGA_2592_1944)},
  695. }, {
  696. {OV5640_MODE_QCIF_176_144, SUBSAMPLING,
  697. 176, 1896, 144, 984,
  698. ov5640_setting_30fps_QCIF_176_144,
  699. ARRAY_SIZE(ov5640_setting_30fps_QCIF_176_144)},
  700. {OV5640_MODE_QVGA_320_240, SUBSAMPLING,
  701. 320, 1896, 240, 984,
  702. ov5640_setting_30fps_QVGA_320_240,
  703. ARRAY_SIZE(ov5640_setting_30fps_QVGA_320_240)},
  704. {OV5640_MODE_VGA_640_480, SUBSAMPLING,
  705. 640, 1896, 480, 1080,
  706. ov5640_setting_30fps_VGA_640_480,
  707. ARRAY_SIZE(ov5640_setting_30fps_VGA_640_480)},
  708. {OV5640_MODE_NTSC_720_480, SUBSAMPLING,
  709. 720, 1896, 480, 984,
  710. ov5640_setting_30fps_NTSC_720_480,
  711. ARRAY_SIZE(ov5640_setting_30fps_NTSC_720_480)},
  712. {OV5640_MODE_PAL_720_576, SUBSAMPLING,
  713. 720, 1896, 576, 984,
  714. ov5640_setting_30fps_PAL_720_576,
  715. ARRAY_SIZE(ov5640_setting_30fps_PAL_720_576)},
  716. {OV5640_MODE_XGA_1024_768, SUBSAMPLING,
  717. 1024, 1896, 768, 1080,
  718. ov5640_setting_30fps_XGA_1024_768,
  719. ARRAY_SIZE(ov5640_setting_30fps_XGA_1024_768)},
  720. {OV5640_MODE_720P_1280_720, SUBSAMPLING,
  721. 1280, 1892, 720, 740,
  722. ov5640_setting_30fps_720P_1280_720,
  723. ARRAY_SIZE(ov5640_setting_30fps_720P_1280_720)},
  724. {OV5640_MODE_1080P_1920_1080, SCALING,
  725. 1920, 2500, 1080, 1120,
  726. ov5640_setting_30fps_1080P_1920_1080,
  727. ARRAY_SIZE(ov5640_setting_30fps_1080P_1920_1080)},
  728. {OV5640_MODE_QSXGA_2592_1944, -1, 0, 0, 0, 0, NULL, 0},
  729. },
  730. };
  731. static int ov5640_init_slave_id(struct ov5640_dev *sensor)
  732. {
  733. struct i2c_client *client = sensor->i2c_client;
  734. struct i2c_msg msg;
  735. u8 buf[3];
  736. int ret;
  737. if (client->addr == OV5640_DEFAULT_SLAVE_ID)
  738. return 0;
  739. buf[0] = OV5640_REG_SLAVE_ID >> 8;
  740. buf[1] = OV5640_REG_SLAVE_ID & 0xff;
  741. buf[2] = client->addr << 1;
  742. msg.addr = OV5640_DEFAULT_SLAVE_ID;
  743. msg.flags = 0;
  744. msg.buf = buf;
  745. msg.len = sizeof(buf);
  746. ret = i2c_transfer(client->adapter, &msg, 1);
  747. if (ret < 0) {
  748. dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
  749. return ret;
  750. }
  751. return 0;
  752. }
  753. static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
  754. {
  755. struct i2c_client *client = sensor->i2c_client;
  756. struct i2c_msg msg;
  757. u8 buf[3];
  758. int ret;
  759. buf[0] = reg >> 8;
  760. buf[1] = reg & 0xff;
  761. buf[2] = val;
  762. msg.addr = client->addr;
  763. msg.flags = client->flags;
  764. msg.buf = buf;
  765. msg.len = sizeof(buf);
  766. ret = i2c_transfer(client->adapter, &msg, 1);
  767. if (ret < 0) {
  768. dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
  769. __func__, reg, val);
  770. return ret;
  771. }
  772. return 0;
  773. }
  774. static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
  775. {
  776. struct i2c_client *client = sensor->i2c_client;
  777. struct i2c_msg msg[2];
  778. u8 buf[2];
  779. int ret;
  780. buf[0] = reg >> 8;
  781. buf[1] = reg & 0xff;
  782. msg[0].addr = client->addr;
  783. msg[0].flags = client->flags;
  784. msg[0].buf = buf;
  785. msg[0].len = sizeof(buf);
  786. msg[1].addr = client->addr;
  787. msg[1].flags = client->flags | I2C_M_RD;
  788. msg[1].buf = buf;
  789. msg[1].len = 1;
  790. ret = i2c_transfer(client->adapter, msg, 2);
  791. if (ret < 0) {
  792. dev_err(&client->dev, "%s: error: reg=%x\n",
  793. __func__, reg);
  794. return ret;
  795. }
  796. *val = buf[0];
  797. return 0;
  798. }
  799. static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
  800. {
  801. u8 hi, lo;
  802. int ret;
  803. ret = ov5640_read_reg(sensor, reg, &hi);
  804. if (ret)
  805. return ret;
  806. ret = ov5640_read_reg(sensor, reg + 1, &lo);
  807. if (ret)
  808. return ret;
  809. *val = ((u16)hi << 8) | (u16)lo;
  810. return 0;
  811. }
  812. static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
  813. {
  814. int ret;
  815. ret = ov5640_write_reg(sensor, reg, val >> 8);
  816. if (ret)
  817. return ret;
  818. return ov5640_write_reg(sensor, reg + 1, val & 0xff);
  819. }
  820. static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
  821. u8 mask, u8 val)
  822. {
  823. u8 readval;
  824. int ret;
  825. ret = ov5640_read_reg(sensor, reg, &readval);
  826. if (ret)
  827. return ret;
  828. readval &= ~mask;
  829. val &= mask;
  830. val |= readval;
  831. return ov5640_write_reg(sensor, reg, val);
  832. }
  833. /* download ov5640 settings to sensor through i2c */
  834. static int ov5640_load_regs(struct ov5640_dev *sensor,
  835. const struct ov5640_mode_info *mode)
  836. {
  837. const struct reg_value *regs = mode->reg_data;
  838. unsigned int i;
  839. u32 delay_ms;
  840. u16 reg_addr;
  841. u8 mask, val;
  842. int ret = 0;
  843. for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
  844. delay_ms = regs->delay_ms;
  845. reg_addr = regs->reg_addr;
  846. val = regs->val;
  847. mask = regs->mask;
  848. if (mask)
  849. ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
  850. else
  851. ret = ov5640_write_reg(sensor, reg_addr, val);
  852. if (ret)
  853. break;
  854. if (delay_ms)
  855. usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
  856. }
  857. return ret;
  858. }
  859. /* read exposure, in number of line periods */
  860. static int ov5640_get_exposure(struct ov5640_dev *sensor)
  861. {
  862. int exp, ret;
  863. u8 temp;
  864. ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
  865. if (ret)
  866. return ret;
  867. exp = ((int)temp & 0x0f) << 16;
  868. ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
  869. if (ret)
  870. return ret;
  871. exp |= ((int)temp << 8);
  872. ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
  873. if (ret)
  874. return ret;
  875. exp |= (int)temp;
  876. return exp >> 4;
  877. }
  878. /* write exposure, given number of line periods */
  879. static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
  880. {
  881. int ret;
  882. exposure <<= 4;
  883. ret = ov5640_write_reg(sensor,
  884. OV5640_REG_AEC_PK_EXPOSURE_LO,
  885. exposure & 0xff);
  886. if (ret)
  887. return ret;
  888. ret = ov5640_write_reg(sensor,
  889. OV5640_REG_AEC_PK_EXPOSURE_MED,
  890. (exposure >> 8) & 0xff);
  891. if (ret)
  892. return ret;
  893. return ov5640_write_reg(sensor,
  894. OV5640_REG_AEC_PK_EXPOSURE_HI,
  895. (exposure >> 16) & 0x0f);
  896. }
  897. static int ov5640_get_gain(struct ov5640_dev *sensor)
  898. {
  899. u16 gain;
  900. int ret;
  901. ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
  902. if (ret)
  903. return ret;
  904. return gain & 0x3ff;
  905. }
  906. static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
  907. {
  908. int ret;
  909. unsigned int flags = sensor->ep.bus.parallel.flags;
  910. u8 pclk_pol = 0;
  911. u8 hsync_pol = 0;
  912. u8 vsync_pol = 0;
  913. /*
  914. * Note about parallel port configuration.
  915. *
  916. * When configured in parallel mode, the OV5640 will
  917. * output 10 bits data on DVP data lines [9:0].
  918. * If only 8 bits data are wanted, the 8 bits data lines
  919. * of the camera interface must be physically connected
  920. * on the DVP data lines [9:2].
  921. *
  922. * Control lines polarity can be configured through
  923. * devicetree endpoint control lines properties.
  924. * If no endpoint control lines properties are set,
  925. * polarity will be as below:
  926. * - VSYNC: active high
  927. * - HREF: active low
  928. * - PCLK: active low
  929. */
  930. if (on) {
  931. /*
  932. * reset MIPI PCLK/SERCLK divider
  933. *
  934. * SC PLL CONTRL1 0
  935. * - [3..0]: MIPI PCLK/SERCLK divider
  936. */
  937. ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0x0f, 0);
  938. if (ret)
  939. return ret;
  940. /*
  941. * configure parallel port control lines polarity
  942. *
  943. * POLARITY CTRL0
  944. * - [5]: PCLK polarity (0: active low, 1: active high)
  945. * - [1]: HREF polarity (0: active low, 1: active high)
  946. * - [0]: VSYNC polarity (mismatch here between
  947. * datasheet and hardware, 0 is active high
  948. * and 1 is active low...)
  949. */
  950. if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  951. pclk_pol = 1;
  952. if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  953. hsync_pol = 1;
  954. if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  955. vsync_pol = 1;
  956. ret = ov5640_write_reg(sensor,
  957. OV5640_REG_POLARITY_CTRL00,
  958. (pclk_pol << 5) |
  959. (hsync_pol << 1) |
  960. vsync_pol);
  961. if (ret)
  962. return ret;
  963. }
  964. /*
  965. * powerdown MIPI TX/RX PHY & disable MIPI
  966. *
  967. * MIPI CONTROL 00
  968. * 4: PWDN PHY TX
  969. * 3: PWDN PHY RX
  970. * 2: MIPI enable
  971. */
  972. ret = ov5640_write_reg(sensor,
  973. OV5640_REG_IO_MIPI_CTRL00, on ? 0x18 : 0);
  974. if (ret)
  975. return ret;
  976. /*
  977. * enable VSYNC/HREF/PCLK DVP control lines
  978. * & D[9:6] DVP data lines
  979. *
  980. * PAD OUTPUT ENABLE 01
  981. * - 6: VSYNC output enable
  982. * - 5: HREF output enable
  983. * - 4: PCLK output enable
  984. * - [3:0]: D[9:6] output enable
  985. */
  986. ret = ov5640_write_reg(sensor,
  987. OV5640_REG_PAD_OUTPUT_ENABLE01,
  988. on ? 0x7f : 0);
  989. if (ret)
  990. return ret;
  991. /*
  992. * enable D[5:0] DVP data lines
  993. *
  994. * PAD OUTPUT ENABLE 02
  995. * - [7:2]: D[5:0] output enable
  996. */
  997. return ov5640_write_reg(sensor,
  998. OV5640_REG_PAD_OUTPUT_ENABLE02,
  999. on ? 0xfc : 0);
  1000. }
  1001. static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
  1002. {
  1003. int ret;
  1004. ret = ov5640_mod_reg(sensor, OV5640_REG_MIPI_CTRL00, BIT(5),
  1005. on ? 0 : BIT(5));
  1006. if (ret)
  1007. return ret;
  1008. ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00,
  1009. on ? 0x00 : 0x70);
  1010. if (ret)
  1011. return ret;
  1012. return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
  1013. on ? 0x00 : 0x0f);
  1014. }
  1015. static int ov5640_get_sysclk(struct ov5640_dev *sensor)
  1016. {
  1017. /* calculate sysclk */
  1018. u32 xvclk = sensor->xclk_freq / 10000;
  1019. u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
  1020. u32 sclk_rdiv_map[] = {1, 2, 4, 8};
  1021. u32 bit_div2x = 1, sclk_rdiv, sysclk;
  1022. u8 temp1, temp2;
  1023. int ret;
  1024. ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
  1025. if (ret)
  1026. return ret;
  1027. temp2 = temp1 & 0x0f;
  1028. if (temp2 == 8 || temp2 == 10)
  1029. bit_div2x = temp2 / 2;
  1030. ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
  1031. if (ret)
  1032. return ret;
  1033. sysdiv = temp1 >> 4;
  1034. if (sysdiv == 0)
  1035. sysdiv = 16;
  1036. ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
  1037. if (ret)
  1038. return ret;
  1039. multiplier = temp1;
  1040. ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
  1041. if (ret)
  1042. return ret;
  1043. prediv = temp1 & 0x0f;
  1044. pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
  1045. ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
  1046. if (ret)
  1047. return ret;
  1048. temp2 = temp1 & 0x03;
  1049. sclk_rdiv = sclk_rdiv_map[temp2];
  1050. if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
  1051. return -EINVAL;
  1052. VCO = xvclk * multiplier / prediv;
  1053. sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
  1054. return sysclk;
  1055. }
  1056. static int ov5640_set_night_mode(struct ov5640_dev *sensor)
  1057. {
  1058. /* read HTS from register settings */
  1059. u8 mode;
  1060. int ret;
  1061. ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
  1062. if (ret)
  1063. return ret;
  1064. mode &= 0xfb;
  1065. return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
  1066. }
  1067. static int ov5640_get_hts(struct ov5640_dev *sensor)
  1068. {
  1069. /* read HTS from register settings */
  1070. u16 hts;
  1071. int ret;
  1072. ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
  1073. if (ret)
  1074. return ret;
  1075. return hts;
  1076. }
  1077. static int ov5640_get_vts(struct ov5640_dev *sensor)
  1078. {
  1079. u16 vts;
  1080. int ret;
  1081. ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
  1082. if (ret)
  1083. return ret;
  1084. return vts;
  1085. }
  1086. static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
  1087. {
  1088. return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
  1089. }
  1090. static int ov5640_get_light_freq(struct ov5640_dev *sensor)
  1091. {
  1092. /* get banding filter value */
  1093. int ret, light_freq = 0;
  1094. u8 temp, temp1;
  1095. ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
  1096. if (ret)
  1097. return ret;
  1098. if (temp & 0x80) {
  1099. /* manual */
  1100. ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
  1101. &temp1);
  1102. if (ret)
  1103. return ret;
  1104. if (temp1 & 0x04) {
  1105. /* 50Hz */
  1106. light_freq = 50;
  1107. } else {
  1108. /* 60Hz */
  1109. light_freq = 60;
  1110. }
  1111. } else {
  1112. /* auto */
  1113. ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
  1114. &temp1);
  1115. if (ret)
  1116. return ret;
  1117. if (temp1 & 0x01) {
  1118. /* 50Hz */
  1119. light_freq = 50;
  1120. } else {
  1121. /* 60Hz */
  1122. }
  1123. }
  1124. return light_freq;
  1125. }
  1126. static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
  1127. {
  1128. u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
  1129. int ret;
  1130. /* read preview PCLK */
  1131. ret = ov5640_get_sysclk(sensor);
  1132. if (ret < 0)
  1133. return ret;
  1134. if (ret == 0)
  1135. return -EINVAL;
  1136. sensor->prev_sysclk = ret;
  1137. /* read preview HTS */
  1138. ret = ov5640_get_hts(sensor);
  1139. if (ret < 0)
  1140. return ret;
  1141. if (ret == 0)
  1142. return -EINVAL;
  1143. sensor->prev_hts = ret;
  1144. /* read preview VTS */
  1145. ret = ov5640_get_vts(sensor);
  1146. if (ret < 0)
  1147. return ret;
  1148. prev_vts = ret;
  1149. /* calculate banding filter */
  1150. /* 60Hz */
  1151. band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
  1152. ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
  1153. if (ret)
  1154. return ret;
  1155. if (!band_step60)
  1156. return -EINVAL;
  1157. max_band60 = (int)((prev_vts - 4) / band_step60);
  1158. ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
  1159. if (ret)
  1160. return ret;
  1161. /* 50Hz */
  1162. band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
  1163. ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
  1164. if (ret)
  1165. return ret;
  1166. if (!band_step50)
  1167. return -EINVAL;
  1168. max_band50 = (int)((prev_vts - 4) / band_step50);
  1169. return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
  1170. }
  1171. static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
  1172. {
  1173. /* stable in high */
  1174. u32 fast_high, fast_low;
  1175. int ret;
  1176. sensor->ae_low = target * 23 / 25; /* 0.92 */
  1177. sensor->ae_high = target * 27 / 25; /* 1.08 */
  1178. fast_high = sensor->ae_high << 1;
  1179. if (fast_high > 255)
  1180. fast_high = 255;
  1181. fast_low = sensor->ae_low >> 1;
  1182. ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
  1183. if (ret)
  1184. return ret;
  1185. ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
  1186. if (ret)
  1187. return ret;
  1188. ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
  1189. if (ret)
  1190. return ret;
  1191. ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
  1192. if (ret)
  1193. return ret;
  1194. ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
  1195. if (ret)
  1196. return ret;
  1197. return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
  1198. }
  1199. static int ov5640_binning_on(struct ov5640_dev *sensor)
  1200. {
  1201. u8 temp;
  1202. int ret;
  1203. ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
  1204. if (ret)
  1205. return ret;
  1206. temp &= 0xfe;
  1207. return temp ? 1 : 0;
  1208. }
  1209. static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
  1210. {
  1211. int ret;
  1212. /*
  1213. * TIMING TC REG21:
  1214. * - [0]: Horizontal binning enable
  1215. */
  1216. ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
  1217. BIT(0), enable ? BIT(0) : 0);
  1218. if (ret)
  1219. return ret;
  1220. /*
  1221. * TIMING TC REG20:
  1222. * - [0]: Undocumented, but hardcoded init sequences
  1223. * are always setting REG21/REG20 bit 0 to same value...
  1224. */
  1225. return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
  1226. BIT(0), enable ? BIT(0) : 0);
  1227. }
  1228. static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
  1229. {
  1230. struct i2c_client *client = sensor->i2c_client;
  1231. u8 temp, channel = virtual_channel;
  1232. int ret;
  1233. if (channel > 3) {
  1234. dev_err(&client->dev,
  1235. "%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
  1236. __func__, channel);
  1237. return -EINVAL;
  1238. }
  1239. ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
  1240. if (ret)
  1241. return ret;
  1242. temp &= ~(3 << 6);
  1243. temp |= (channel << 6);
  1244. return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
  1245. }
  1246. static int ov5640_set_timings(struct ov5640_dev *sensor,
  1247. const struct ov5640_mode_info *mode)
  1248. {
  1249. int ret;
  1250. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact);
  1251. if (ret < 0)
  1252. return ret;
  1253. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact);
  1254. if (ret < 0)
  1255. return ret;
  1256. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot);
  1257. if (ret < 0)
  1258. return ret;
  1259. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot);
  1260. if (ret < 0)
  1261. return ret;
  1262. return 0;
  1263. }
  1264. static const struct ov5640_mode_info *
  1265. ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
  1266. int width, int height, bool nearest)
  1267. {
  1268. const struct ov5640_mode_info *mode;
  1269. mode = v4l2_find_nearest_size(ov5640_mode_data[fr],
  1270. ARRAY_SIZE(ov5640_mode_data[fr]),
  1271. hact, vact,
  1272. width, height);
  1273. if (!mode ||
  1274. (!nearest && (mode->hact != width || mode->vact != height)))
  1275. return NULL;
  1276. return mode;
  1277. }
  1278. /*
  1279. * sensor changes between scaling and subsampling, go through
  1280. * exposure calculation
  1281. */
  1282. static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
  1283. const struct ov5640_mode_info *mode)
  1284. {
  1285. u32 prev_shutter, prev_gain16;
  1286. u32 cap_shutter, cap_gain16;
  1287. u32 cap_sysclk, cap_hts, cap_vts;
  1288. u32 light_freq, cap_bandfilt, cap_maxband;
  1289. u32 cap_gain16_shutter;
  1290. u8 average;
  1291. int ret;
  1292. if (!mode->reg_data)
  1293. return -EINVAL;
  1294. /* read preview shutter */
  1295. ret = ov5640_get_exposure(sensor);
  1296. if (ret < 0)
  1297. return ret;
  1298. prev_shutter = ret;
  1299. ret = ov5640_binning_on(sensor);
  1300. if (ret < 0)
  1301. return ret;
  1302. if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
  1303. mode->id != OV5640_MODE_1080P_1920_1080)
  1304. prev_shutter *= 2;
  1305. /* read preview gain */
  1306. ret = ov5640_get_gain(sensor);
  1307. if (ret < 0)
  1308. return ret;
  1309. prev_gain16 = ret;
  1310. /* get average */
  1311. ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
  1312. if (ret)
  1313. return ret;
  1314. /* turn off night mode for capture */
  1315. ret = ov5640_set_night_mode(sensor);
  1316. if (ret < 0)
  1317. return ret;
  1318. /* Write capture setting */
  1319. ret = ov5640_load_regs(sensor, mode);
  1320. if (ret < 0)
  1321. return ret;
  1322. /* read capture VTS */
  1323. ret = ov5640_get_vts(sensor);
  1324. if (ret < 0)
  1325. return ret;
  1326. cap_vts = ret;
  1327. ret = ov5640_get_hts(sensor);
  1328. if (ret < 0)
  1329. return ret;
  1330. if (ret == 0)
  1331. return -EINVAL;
  1332. cap_hts = ret;
  1333. ret = ov5640_get_sysclk(sensor);
  1334. if (ret < 0)
  1335. return ret;
  1336. if (ret == 0)
  1337. return -EINVAL;
  1338. cap_sysclk = ret;
  1339. /* calculate capture banding filter */
  1340. ret = ov5640_get_light_freq(sensor);
  1341. if (ret < 0)
  1342. return ret;
  1343. light_freq = ret;
  1344. if (light_freq == 60) {
  1345. /* 60Hz */
  1346. cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
  1347. } else {
  1348. /* 50Hz */
  1349. cap_bandfilt = cap_sysclk * 100 / cap_hts;
  1350. }
  1351. if (!sensor->prev_sysclk) {
  1352. ret = ov5640_get_sysclk(sensor);
  1353. if (ret < 0)
  1354. return ret;
  1355. if (ret == 0)
  1356. return -EINVAL;
  1357. sensor->prev_sysclk = ret;
  1358. }
  1359. if (!cap_bandfilt)
  1360. return -EINVAL;
  1361. cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
  1362. /* calculate capture shutter/gain16 */
  1363. if (average > sensor->ae_low && average < sensor->ae_high) {
  1364. /* in stable range */
  1365. cap_gain16_shutter =
  1366. prev_gain16 * prev_shutter *
  1367. cap_sysclk / sensor->prev_sysclk *
  1368. sensor->prev_hts / cap_hts *
  1369. sensor->ae_target / average;
  1370. } else {
  1371. cap_gain16_shutter =
  1372. prev_gain16 * prev_shutter *
  1373. cap_sysclk / sensor->prev_sysclk *
  1374. sensor->prev_hts / cap_hts;
  1375. }
  1376. /* gain to shutter */
  1377. if (cap_gain16_shutter < (cap_bandfilt * 16)) {
  1378. /* shutter < 1/100 */
  1379. cap_shutter = cap_gain16_shutter / 16;
  1380. if (cap_shutter < 1)
  1381. cap_shutter = 1;
  1382. cap_gain16 = cap_gain16_shutter / cap_shutter;
  1383. if (cap_gain16 < 16)
  1384. cap_gain16 = 16;
  1385. } else {
  1386. if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
  1387. /* exposure reach max */
  1388. cap_shutter = cap_bandfilt * cap_maxband;
  1389. if (!cap_shutter)
  1390. return -EINVAL;
  1391. cap_gain16 = cap_gain16_shutter / cap_shutter;
  1392. } else {
  1393. /* 1/100 < (cap_shutter = n/100) =< max */
  1394. cap_shutter =
  1395. ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
  1396. * cap_bandfilt;
  1397. if (!cap_shutter)
  1398. return -EINVAL;
  1399. cap_gain16 = cap_gain16_shutter / cap_shutter;
  1400. }
  1401. }
  1402. /* set capture gain */
  1403. ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.gain, cap_gain16);
  1404. if (ret)
  1405. return ret;
  1406. /* write capture shutter */
  1407. if (cap_shutter > (cap_vts - 4)) {
  1408. cap_vts = cap_shutter + 4;
  1409. ret = ov5640_set_vts(sensor, cap_vts);
  1410. if (ret < 0)
  1411. return ret;
  1412. }
  1413. /* set exposure */
  1414. return __v4l2_ctrl_s_ctrl(sensor->ctrls.exposure, cap_shutter);
  1415. }
  1416. /*
  1417. * if sensor changes inside scaling or subsampling
  1418. * change mode directly
  1419. */
  1420. static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
  1421. const struct ov5640_mode_info *mode,
  1422. s32 exposure)
  1423. {
  1424. int ret;
  1425. if (!mode->reg_data)
  1426. return -EINVAL;
  1427. /* Write capture setting */
  1428. ret = ov5640_load_regs(sensor, mode);
  1429. if (ret < 0)
  1430. return ret;
  1431. /* turn auto gain/exposure back on for direct mode */
  1432. ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 1);
  1433. if (ret)
  1434. return ret;
  1435. return __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, exposure);
  1436. }
  1437. static int ov5640_set_mode(struct ov5640_dev *sensor,
  1438. const struct ov5640_mode_info *orig_mode)
  1439. {
  1440. const struct ov5640_mode_info *mode = sensor->current_mode;
  1441. enum ov5640_downsize_mode dn_mode, orig_dn_mode;
  1442. s32 exposure;
  1443. int ret;
  1444. dn_mode = mode->dn_mode;
  1445. orig_dn_mode = orig_mode->dn_mode;
  1446. /* auto gain and exposure must be turned off when changing modes */
  1447. ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 0);
  1448. if (ret)
  1449. return ret;
  1450. exposure = sensor->ctrls.auto_exp->val;
  1451. ret = ov5640_set_exposure(sensor, V4L2_EXPOSURE_MANUAL);
  1452. if (ret)
  1453. return ret;
  1454. if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
  1455. (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
  1456. /*
  1457. * change between subsampling and scaling
  1458. * go through exposure calucation
  1459. */
  1460. ret = ov5640_set_mode_exposure_calc(sensor, mode);
  1461. } else {
  1462. /*
  1463. * change inside subsampling or scaling
  1464. * download firmware directly
  1465. */
  1466. ret = ov5640_set_mode_direct(sensor, mode, exposure);
  1467. }
  1468. if (ret < 0)
  1469. return ret;
  1470. ret = ov5640_set_timings(sensor, mode);
  1471. if (ret < 0)
  1472. return ret;
  1473. ret = ov5640_set_binning(sensor, dn_mode != SCALING);
  1474. if (ret < 0)
  1475. return ret;
  1476. ret = ov5640_set_ae_target(sensor, sensor->ae_target);
  1477. if (ret < 0)
  1478. return ret;
  1479. ret = ov5640_get_light_freq(sensor);
  1480. if (ret < 0)
  1481. return ret;
  1482. ret = ov5640_set_bandingfilter(sensor);
  1483. if (ret < 0)
  1484. return ret;
  1485. ret = ov5640_set_virtual_channel(sensor);
  1486. if (ret < 0)
  1487. return ret;
  1488. sensor->pending_mode_change = false;
  1489. return 0;
  1490. }
  1491. static int ov5640_set_framefmt(struct ov5640_dev *sensor,
  1492. struct v4l2_mbus_framefmt *format);
  1493. /* restore the last set video mode after chip power-on */
  1494. static int ov5640_restore_mode(struct ov5640_dev *sensor)
  1495. {
  1496. int ret;
  1497. /* first load the initial register values */
  1498. ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
  1499. if (ret < 0)
  1500. return ret;
  1501. ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
  1502. (ilog2(OV5640_SCLK2X_ROOT_DIVIDER_DEFAULT) << 2) |
  1503. ilog2(OV5640_SCLK_ROOT_DIVIDER_DEFAULT));
  1504. if (ret)
  1505. return ret;
  1506. /* now restore the last capture mode */
  1507. ret = ov5640_set_mode(sensor, &ov5640_mode_init_data);
  1508. if (ret < 0)
  1509. return ret;
  1510. return ov5640_set_framefmt(sensor, &sensor->fmt);
  1511. }
  1512. static void ov5640_power(struct ov5640_dev *sensor, bool enable)
  1513. {
  1514. gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
  1515. }
  1516. static void ov5640_reset(struct ov5640_dev *sensor)
  1517. {
  1518. if (!sensor->reset_gpio)
  1519. return;
  1520. gpiod_set_value_cansleep(sensor->reset_gpio, 0);
  1521. /* camera power cycle */
  1522. ov5640_power(sensor, false);
  1523. usleep_range(5000, 10000);
  1524. ov5640_power(sensor, true);
  1525. usleep_range(5000, 10000);
  1526. gpiod_set_value_cansleep(sensor->reset_gpio, 1);
  1527. usleep_range(1000, 2000);
  1528. gpiod_set_value_cansleep(sensor->reset_gpio, 0);
  1529. usleep_range(5000, 10000);
  1530. }
  1531. static int ov5640_set_power_on(struct ov5640_dev *sensor)
  1532. {
  1533. struct i2c_client *client = sensor->i2c_client;
  1534. int ret;
  1535. ret = clk_prepare_enable(sensor->xclk);
  1536. if (ret) {
  1537. dev_err(&client->dev, "%s: failed to enable clock\n",
  1538. __func__);
  1539. return ret;
  1540. }
  1541. ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
  1542. sensor->supplies);
  1543. if (ret) {
  1544. dev_err(&client->dev, "%s: failed to enable regulators\n",
  1545. __func__);
  1546. goto xclk_off;
  1547. }
  1548. ov5640_reset(sensor);
  1549. ov5640_power(sensor, true);
  1550. ret = ov5640_init_slave_id(sensor);
  1551. if (ret)
  1552. goto power_off;
  1553. return 0;
  1554. power_off:
  1555. ov5640_power(sensor, false);
  1556. regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
  1557. xclk_off:
  1558. clk_disable_unprepare(sensor->xclk);
  1559. return ret;
  1560. }
  1561. static void ov5640_set_power_off(struct ov5640_dev *sensor)
  1562. {
  1563. ov5640_power(sensor, false);
  1564. regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
  1565. clk_disable_unprepare(sensor->xclk);
  1566. }
  1567. static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
  1568. {
  1569. int ret = 0;
  1570. if (on) {
  1571. ret = ov5640_set_power_on(sensor);
  1572. if (ret)
  1573. return ret;
  1574. ret = ov5640_restore_mode(sensor);
  1575. if (ret)
  1576. goto power_off;
  1577. if (sensor->ep.bus_type == V4L2_MBUS_CSI2) {
  1578. /*
  1579. * start streaming briefly followed by stream off in
  1580. * order to coax the clock lane into LP-11 state.
  1581. */
  1582. ret = ov5640_set_stream_mipi(sensor, true);
  1583. if (ret)
  1584. goto power_off;
  1585. usleep_range(1000, 2000);
  1586. ret = ov5640_set_stream_mipi(sensor, false);
  1587. if (ret)
  1588. goto power_off;
  1589. }
  1590. return 0;
  1591. }
  1592. power_off:
  1593. ov5640_set_power_off(sensor);
  1594. return ret;
  1595. }
  1596. /* --------------- Subdev Operations --------------- */
  1597. static int ov5640_s_power(struct v4l2_subdev *sd, int on)
  1598. {
  1599. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  1600. int ret = 0;
  1601. mutex_lock(&sensor->lock);
  1602. /*
  1603. * If the power count is modified from 0 to != 0 or from != 0 to 0,
  1604. * update the power state.
  1605. */
  1606. if (sensor->power_count == !on) {
  1607. ret = ov5640_set_power(sensor, !!on);
  1608. if (ret)
  1609. goto out;
  1610. }
  1611. /* Update the power count. */
  1612. sensor->power_count += on ? 1 : -1;
  1613. WARN_ON(sensor->power_count < 0);
  1614. out:
  1615. mutex_unlock(&sensor->lock);
  1616. if (on && !ret && sensor->power_count == 1) {
  1617. /* restore controls */
  1618. ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
  1619. }
  1620. return ret;
  1621. }
  1622. static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
  1623. struct v4l2_fract *fi,
  1624. u32 width, u32 height)
  1625. {
  1626. const struct ov5640_mode_info *mode;
  1627. u32 minfps, maxfps, fps;
  1628. int ret;
  1629. minfps = ov5640_framerates[OV5640_15_FPS];
  1630. maxfps = ov5640_framerates[OV5640_30_FPS];
  1631. if (fi->numerator == 0) {
  1632. fi->denominator = maxfps;
  1633. fi->numerator = 1;
  1634. return OV5640_30_FPS;
  1635. }
  1636. fps = DIV_ROUND_CLOSEST(fi->denominator, fi->numerator);
  1637. fi->numerator = 1;
  1638. if (fps > maxfps)
  1639. fi->denominator = maxfps;
  1640. else if (fps < minfps)
  1641. fi->denominator = minfps;
  1642. else if (2 * fps >= 2 * minfps + (maxfps - minfps))
  1643. fi->denominator = maxfps;
  1644. else
  1645. fi->denominator = minfps;
  1646. ret = (fi->denominator == minfps) ? OV5640_15_FPS : OV5640_30_FPS;
  1647. mode = ov5640_find_mode(sensor, ret, width, height, false);
  1648. return mode ? ret : -EINVAL;
  1649. }
  1650. static int ov5640_get_fmt(struct v4l2_subdev *sd,
  1651. struct v4l2_subdev_pad_config *cfg,
  1652. struct v4l2_subdev_format *format)
  1653. {
  1654. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  1655. struct v4l2_mbus_framefmt *fmt;
  1656. if (format->pad != 0)
  1657. return -EINVAL;
  1658. mutex_lock(&sensor->lock);
  1659. if (format->which == V4L2_SUBDEV_FORMAT_TRY)
  1660. fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
  1661. format->pad);
  1662. else
  1663. fmt = &sensor->fmt;
  1664. format->format = *fmt;
  1665. mutex_unlock(&sensor->lock);
  1666. return 0;
  1667. }
  1668. static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
  1669. struct v4l2_mbus_framefmt *fmt,
  1670. enum ov5640_frame_rate fr,
  1671. const struct ov5640_mode_info **new_mode)
  1672. {
  1673. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  1674. const struct ov5640_mode_info *mode;
  1675. int i;
  1676. mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
  1677. if (!mode)
  1678. return -EINVAL;
  1679. fmt->width = mode->hact;
  1680. fmt->height = mode->vact;
  1681. if (new_mode)
  1682. *new_mode = mode;
  1683. for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
  1684. if (ov5640_formats[i].code == fmt->code)
  1685. break;
  1686. if (i >= ARRAY_SIZE(ov5640_formats))
  1687. i = 0;
  1688. fmt->code = ov5640_formats[i].code;
  1689. fmt->colorspace = ov5640_formats[i].colorspace;
  1690. fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
  1691. fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
  1692. fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
  1693. return 0;
  1694. }
  1695. static int ov5640_set_fmt(struct v4l2_subdev *sd,
  1696. struct v4l2_subdev_pad_config *cfg,
  1697. struct v4l2_subdev_format *format)
  1698. {
  1699. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  1700. const struct ov5640_mode_info *new_mode;
  1701. struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
  1702. int ret;
  1703. if (format->pad != 0)
  1704. return -EINVAL;
  1705. mutex_lock(&sensor->lock);
  1706. if (sensor->streaming) {
  1707. ret = -EBUSY;
  1708. goto out;
  1709. }
  1710. ret = ov5640_try_fmt_internal(sd, mbus_fmt,
  1711. sensor->current_fr, &new_mode);
  1712. if (ret)
  1713. goto out;
  1714. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1715. struct v4l2_mbus_framefmt *fmt =
  1716. v4l2_subdev_get_try_format(sd, cfg, 0);
  1717. *fmt = *mbus_fmt;
  1718. goto out;
  1719. }
  1720. if (new_mode != sensor->current_mode) {
  1721. sensor->current_mode = new_mode;
  1722. sensor->fmt = *mbus_fmt;
  1723. sensor->pending_mode_change = true;
  1724. }
  1725. out:
  1726. mutex_unlock(&sensor->lock);
  1727. return ret;
  1728. }
  1729. static int ov5640_set_framefmt(struct ov5640_dev *sensor,
  1730. struct v4l2_mbus_framefmt *format)
  1731. {
  1732. int ret = 0;
  1733. bool is_rgb = false;
  1734. bool is_jpeg = false;
  1735. u8 val;
  1736. switch (format->code) {
  1737. case MEDIA_BUS_FMT_UYVY8_2X8:
  1738. /* YUV422, UYVY */
  1739. val = 0x3f;
  1740. break;
  1741. case MEDIA_BUS_FMT_YUYV8_2X8:
  1742. /* YUV422, YUYV */
  1743. val = 0x30;
  1744. break;
  1745. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  1746. /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
  1747. val = 0x6F;
  1748. is_rgb = true;
  1749. break;
  1750. case MEDIA_BUS_FMT_RGB565_2X8_BE:
  1751. /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
  1752. val = 0x61;
  1753. is_rgb = true;
  1754. break;
  1755. case MEDIA_BUS_FMT_JPEG_1X8:
  1756. /* YUV422, YUYV */
  1757. val = 0x30;
  1758. is_jpeg = true;
  1759. break;
  1760. default:
  1761. return -EINVAL;
  1762. }
  1763. /* FORMAT CONTROL00: YUV and RGB formatting */
  1764. ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, val);
  1765. if (ret)
  1766. return ret;
  1767. /* FORMAT MUX CONTROL: ISP YUV or RGB */
  1768. ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
  1769. is_rgb ? 0x01 : 0x00);
  1770. if (ret)
  1771. return ret;
  1772. /*
  1773. * TIMING TC REG21:
  1774. * - [5]: JPEG enable
  1775. */
  1776. ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
  1777. BIT(5), is_jpeg ? BIT(5) : 0);
  1778. if (ret)
  1779. return ret;
  1780. /*
  1781. * SYSTEM RESET02:
  1782. * - [4]: Reset JFIFO
  1783. * - [3]: Reset SFIFO
  1784. * - [2]: Reset JPEG
  1785. */
  1786. ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
  1787. BIT(4) | BIT(3) | BIT(2),
  1788. is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
  1789. if (ret)
  1790. return ret;
  1791. /*
  1792. * CLOCK ENABLE02:
  1793. * - [5]: Enable JPEG 2x clock
  1794. * - [3]: Enable JPEG clock
  1795. */
  1796. return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
  1797. BIT(5) | BIT(3),
  1798. is_jpeg ? (BIT(5) | BIT(3)) : 0);
  1799. }
  1800. /*
  1801. * Sensor Controls.
  1802. */
  1803. static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
  1804. {
  1805. int ret;
  1806. if (value) {
  1807. ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
  1808. BIT(0), BIT(0));
  1809. if (ret)
  1810. return ret;
  1811. ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
  1812. } else {
  1813. ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
  1814. }
  1815. return ret;
  1816. }
  1817. static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
  1818. {
  1819. int ret;
  1820. if (value) {
  1821. ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
  1822. BIT(2), BIT(2));
  1823. if (ret)
  1824. return ret;
  1825. ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
  1826. value & 0xff);
  1827. } else {
  1828. ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
  1829. }
  1830. return ret;
  1831. }
  1832. static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
  1833. {
  1834. int ret;
  1835. if (value) {
  1836. ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
  1837. BIT(1), BIT(1));
  1838. if (ret)
  1839. return ret;
  1840. ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
  1841. value & 0xff);
  1842. if (ret)
  1843. return ret;
  1844. ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
  1845. value & 0xff);
  1846. } else {
  1847. ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
  1848. }
  1849. return ret;
  1850. }
  1851. static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
  1852. {
  1853. int ret;
  1854. ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
  1855. BIT(0), awb ? 0 : 1);
  1856. if (ret)
  1857. return ret;
  1858. if (!awb) {
  1859. u16 red = (u16)sensor->ctrls.red_balance->val;
  1860. u16 blue = (u16)sensor->ctrls.blue_balance->val;
  1861. ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
  1862. if (ret)
  1863. return ret;
  1864. ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
  1865. }
  1866. return ret;
  1867. }
  1868. static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, int exp)
  1869. {
  1870. struct ov5640_ctrls *ctrls = &sensor->ctrls;
  1871. bool auto_exposure = (exp == V4L2_EXPOSURE_AUTO);
  1872. int ret = 0;
  1873. if (ctrls->auto_exp->is_new) {
  1874. ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
  1875. BIT(0), auto_exposure ? 0 : BIT(0));
  1876. if (ret)
  1877. return ret;
  1878. }
  1879. if (!auto_exposure && ctrls->exposure->is_new) {
  1880. u16 max_exp;
  1881. ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
  1882. &max_exp);
  1883. if (ret)
  1884. return ret;
  1885. ret = ov5640_get_vts(sensor);
  1886. if (ret < 0)
  1887. return ret;
  1888. max_exp += ret;
  1889. ret = 0;
  1890. if (ctrls->exposure->val < max_exp)
  1891. ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
  1892. }
  1893. return ret;
  1894. }
  1895. static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, int auto_gain)
  1896. {
  1897. struct ov5640_ctrls *ctrls = &sensor->ctrls;
  1898. int ret = 0;
  1899. if (ctrls->auto_gain->is_new) {
  1900. ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
  1901. BIT(1),
  1902. ctrls->auto_gain->val ? 0 : BIT(1));
  1903. if (ret)
  1904. return ret;
  1905. }
  1906. if (!auto_gain && ctrls->gain->is_new) {
  1907. u16 gain = (u16)ctrls->gain->val;
  1908. ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
  1909. gain & 0x3ff);
  1910. }
  1911. return ret;
  1912. }
  1913. static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
  1914. {
  1915. return ov5640_mod_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
  1916. 0xa4, value ? 0xa4 : 0);
  1917. }
  1918. static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
  1919. {
  1920. int ret;
  1921. ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
  1922. (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
  1923. 0 : BIT(7));
  1924. if (ret)
  1925. return ret;
  1926. return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
  1927. (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
  1928. BIT(2) : 0);
  1929. }
  1930. static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
  1931. {
  1932. /*
  1933. * If sensor is mounted upside down, mirror logic is inversed.
  1934. *
  1935. * Sensor is a BSI (Back Side Illuminated) one,
  1936. * so image captured is physically mirrored.
  1937. * This is why mirror logic is inversed in
  1938. * order to cancel this mirror effect.
  1939. */
  1940. /*
  1941. * TIMING TC REG21:
  1942. * - [2]: ISP mirror
  1943. * - [1]: Sensor mirror
  1944. */
  1945. return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
  1946. BIT(2) | BIT(1),
  1947. (!(value ^ sensor->upside_down)) ?
  1948. (BIT(2) | BIT(1)) : 0);
  1949. }
  1950. static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
  1951. {
  1952. /* If sensor is mounted upside down, flip logic is inversed */
  1953. /*
  1954. * TIMING TC REG20:
  1955. * - [2]: ISP vflip
  1956. * - [1]: Sensor vflip
  1957. */
  1958. return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
  1959. BIT(2) | BIT(1),
  1960. (value ^ sensor->upside_down) ?
  1961. (BIT(2) | BIT(1)) : 0);
  1962. }
  1963. static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  1964. {
  1965. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  1966. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  1967. int val;
  1968. /* v4l2_ctrl_lock() locks our own mutex */
  1969. switch (ctrl->id) {
  1970. case V4L2_CID_AUTOGAIN:
  1971. if (!ctrl->val)
  1972. return 0;
  1973. val = ov5640_get_gain(sensor);
  1974. if (val < 0)
  1975. return val;
  1976. sensor->ctrls.gain->val = val;
  1977. break;
  1978. case V4L2_CID_EXPOSURE_AUTO:
  1979. if (ctrl->val == V4L2_EXPOSURE_MANUAL)
  1980. return 0;
  1981. val = ov5640_get_exposure(sensor);
  1982. if (val < 0)
  1983. return val;
  1984. sensor->ctrls.exposure->val = val;
  1985. break;
  1986. }
  1987. return 0;
  1988. }
  1989. static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
  1990. {
  1991. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  1992. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  1993. int ret;
  1994. /* v4l2_ctrl_lock() locks our own mutex */
  1995. /*
  1996. * If the device is not powered up by the host driver do
  1997. * not apply any controls to H/W at this time. Instead
  1998. * the controls will be restored right after power-up.
  1999. */
  2000. if (sensor->power_count == 0)
  2001. return 0;
  2002. switch (ctrl->id) {
  2003. case V4L2_CID_AUTOGAIN:
  2004. ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
  2005. break;
  2006. case V4L2_CID_EXPOSURE_AUTO:
  2007. ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
  2008. break;
  2009. case V4L2_CID_AUTO_WHITE_BALANCE:
  2010. ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
  2011. break;
  2012. case V4L2_CID_HUE:
  2013. ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
  2014. break;
  2015. case V4L2_CID_CONTRAST:
  2016. ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
  2017. break;
  2018. case V4L2_CID_SATURATION:
  2019. ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
  2020. break;
  2021. case V4L2_CID_TEST_PATTERN:
  2022. ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
  2023. break;
  2024. case V4L2_CID_POWER_LINE_FREQUENCY:
  2025. ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
  2026. break;
  2027. case V4L2_CID_HFLIP:
  2028. ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
  2029. break;
  2030. case V4L2_CID_VFLIP:
  2031. ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
  2032. break;
  2033. default:
  2034. ret = -EINVAL;
  2035. break;
  2036. }
  2037. return ret;
  2038. }
  2039. static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
  2040. .g_volatile_ctrl = ov5640_g_volatile_ctrl,
  2041. .s_ctrl = ov5640_s_ctrl,
  2042. };
  2043. static const char * const test_pattern_menu[] = {
  2044. "Disabled",
  2045. "Color bars",
  2046. };
  2047. static int ov5640_init_controls(struct ov5640_dev *sensor)
  2048. {
  2049. const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
  2050. struct ov5640_ctrls *ctrls = &sensor->ctrls;
  2051. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  2052. int ret;
  2053. v4l2_ctrl_handler_init(hdl, 32);
  2054. /* we can use our own mutex for the ctrl lock */
  2055. hdl->lock = &sensor->lock;
  2056. /* Auto/manual white balance */
  2057. ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
  2058. V4L2_CID_AUTO_WHITE_BALANCE,
  2059. 0, 1, 1, 1);
  2060. ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
  2061. 0, 4095, 1, 0);
  2062. ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
  2063. 0, 4095, 1, 0);
  2064. /* Auto/manual exposure */
  2065. ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
  2066. V4L2_CID_EXPOSURE_AUTO,
  2067. V4L2_EXPOSURE_MANUAL, 0,
  2068. V4L2_EXPOSURE_AUTO);
  2069. ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
  2070. 0, 65535, 1, 0);
  2071. /* Auto/manual gain */
  2072. ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
  2073. 0, 1, 1, 1);
  2074. ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
  2075. 0, 1023, 1, 0);
  2076. ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
  2077. 0, 255, 1, 64);
  2078. ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
  2079. 0, 359, 1, 0);
  2080. ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
  2081. 0, 255, 1, 0);
  2082. ctrls->test_pattern =
  2083. v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
  2084. ARRAY_SIZE(test_pattern_menu) - 1,
  2085. 0, 0, test_pattern_menu);
  2086. ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
  2087. 0, 1, 1, 0);
  2088. ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
  2089. 0, 1, 1, 0);
  2090. ctrls->light_freq =
  2091. v4l2_ctrl_new_std_menu(hdl, ops,
  2092. V4L2_CID_POWER_LINE_FREQUENCY,
  2093. V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
  2094. V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
  2095. if (hdl->error) {
  2096. ret = hdl->error;
  2097. goto free_ctrls;
  2098. }
  2099. ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
  2100. ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
  2101. v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
  2102. v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
  2103. v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
  2104. sensor->sd.ctrl_handler = hdl;
  2105. return 0;
  2106. free_ctrls:
  2107. v4l2_ctrl_handler_free(hdl);
  2108. return ret;
  2109. }
  2110. static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
  2111. struct v4l2_subdev_pad_config *cfg,
  2112. struct v4l2_subdev_frame_size_enum *fse)
  2113. {
  2114. if (fse->pad != 0)
  2115. return -EINVAL;
  2116. if (fse->index >= OV5640_NUM_MODES)
  2117. return -EINVAL;
  2118. fse->min_width =
  2119. ov5640_mode_data[0][fse->index].hact;
  2120. fse->max_width = fse->min_width;
  2121. fse->min_height =
  2122. ov5640_mode_data[0][fse->index].vact;
  2123. fse->max_height = fse->min_height;
  2124. return 0;
  2125. }
  2126. static int ov5640_enum_frame_interval(
  2127. struct v4l2_subdev *sd,
  2128. struct v4l2_subdev_pad_config *cfg,
  2129. struct v4l2_subdev_frame_interval_enum *fie)
  2130. {
  2131. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  2132. struct v4l2_fract tpf;
  2133. int ret;
  2134. if (fie->pad != 0)
  2135. return -EINVAL;
  2136. if (fie->index >= OV5640_NUM_FRAMERATES)
  2137. return -EINVAL;
  2138. tpf.numerator = 1;
  2139. tpf.denominator = ov5640_framerates[fie->index];
  2140. ret = ov5640_try_frame_interval(sensor, &tpf,
  2141. fie->width, fie->height);
  2142. if (ret < 0)
  2143. return -EINVAL;
  2144. fie->interval = tpf;
  2145. return 0;
  2146. }
  2147. static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
  2148. struct v4l2_subdev_frame_interval *fi)
  2149. {
  2150. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  2151. mutex_lock(&sensor->lock);
  2152. fi->interval = sensor->frame_interval;
  2153. mutex_unlock(&sensor->lock);
  2154. return 0;
  2155. }
  2156. static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
  2157. struct v4l2_subdev_frame_interval *fi)
  2158. {
  2159. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  2160. const struct ov5640_mode_info *mode;
  2161. int frame_rate, ret = 0;
  2162. if (fi->pad != 0)
  2163. return -EINVAL;
  2164. mutex_lock(&sensor->lock);
  2165. if (sensor->streaming) {
  2166. ret = -EBUSY;
  2167. goto out;
  2168. }
  2169. mode = sensor->current_mode;
  2170. frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
  2171. mode->hact, mode->vact);
  2172. if (frame_rate < 0)
  2173. frame_rate = OV5640_15_FPS;
  2174. sensor->current_fr = frame_rate;
  2175. sensor->frame_interval = fi->interval;
  2176. mode = ov5640_find_mode(sensor, frame_rate, mode->hact,
  2177. mode->vact, true);
  2178. if (!mode) {
  2179. ret = -EINVAL;
  2180. goto out;
  2181. }
  2182. if (mode != sensor->current_mode) {
  2183. sensor->current_mode = mode;
  2184. sensor->pending_mode_change = true;
  2185. }
  2186. out:
  2187. mutex_unlock(&sensor->lock);
  2188. return ret;
  2189. }
  2190. static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
  2191. struct v4l2_subdev_pad_config *cfg,
  2192. struct v4l2_subdev_mbus_code_enum *code)
  2193. {
  2194. if (code->pad != 0)
  2195. return -EINVAL;
  2196. if (code->index >= ARRAY_SIZE(ov5640_formats))
  2197. return -EINVAL;
  2198. code->code = ov5640_formats[code->index].code;
  2199. return 0;
  2200. }
  2201. static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
  2202. {
  2203. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  2204. int ret = 0;
  2205. mutex_lock(&sensor->lock);
  2206. if (sensor->streaming == !enable) {
  2207. if (enable && sensor->pending_mode_change) {
  2208. ret = ov5640_set_mode(sensor, sensor->current_mode);
  2209. if (ret)
  2210. goto out;
  2211. ret = ov5640_set_framefmt(sensor, &sensor->fmt);
  2212. if (ret)
  2213. goto out;
  2214. }
  2215. if (sensor->ep.bus_type == V4L2_MBUS_CSI2)
  2216. ret = ov5640_set_stream_mipi(sensor, enable);
  2217. else
  2218. ret = ov5640_set_stream_dvp(sensor, enable);
  2219. if (!ret)
  2220. sensor->streaming = enable;
  2221. }
  2222. out:
  2223. mutex_unlock(&sensor->lock);
  2224. return ret;
  2225. }
  2226. static const struct v4l2_subdev_core_ops ov5640_core_ops = {
  2227. .s_power = ov5640_s_power,
  2228. };
  2229. static const struct v4l2_subdev_video_ops ov5640_video_ops = {
  2230. .g_frame_interval = ov5640_g_frame_interval,
  2231. .s_frame_interval = ov5640_s_frame_interval,
  2232. .s_stream = ov5640_s_stream,
  2233. };
  2234. static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
  2235. .enum_mbus_code = ov5640_enum_mbus_code,
  2236. .get_fmt = ov5640_get_fmt,
  2237. .set_fmt = ov5640_set_fmt,
  2238. .enum_frame_size = ov5640_enum_frame_size,
  2239. .enum_frame_interval = ov5640_enum_frame_interval,
  2240. };
  2241. static const struct v4l2_subdev_ops ov5640_subdev_ops = {
  2242. .core = &ov5640_core_ops,
  2243. .video = &ov5640_video_ops,
  2244. .pad = &ov5640_pad_ops,
  2245. };
  2246. static int ov5640_get_regulators(struct ov5640_dev *sensor)
  2247. {
  2248. int i;
  2249. for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
  2250. sensor->supplies[i].supply = ov5640_supply_name[i];
  2251. return devm_regulator_bulk_get(&sensor->i2c_client->dev,
  2252. OV5640_NUM_SUPPLIES,
  2253. sensor->supplies);
  2254. }
  2255. static int ov5640_check_chip_id(struct ov5640_dev *sensor)
  2256. {
  2257. struct i2c_client *client = sensor->i2c_client;
  2258. int ret = 0;
  2259. u16 chip_id;
  2260. ret = ov5640_set_power_on(sensor);
  2261. if (ret)
  2262. return ret;
  2263. ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
  2264. if (ret) {
  2265. dev_err(&client->dev, "%s: failed to read chip identifier\n",
  2266. __func__);
  2267. goto power_off;
  2268. }
  2269. if (chip_id != 0x5640) {
  2270. dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
  2271. __func__, chip_id);
  2272. ret = -ENXIO;
  2273. }
  2274. power_off:
  2275. ov5640_set_power_off(sensor);
  2276. return ret;
  2277. }
  2278. static int ov5640_probe(struct i2c_client *client,
  2279. const struct i2c_device_id *id)
  2280. {
  2281. struct device *dev = &client->dev;
  2282. struct fwnode_handle *endpoint;
  2283. struct ov5640_dev *sensor;
  2284. struct v4l2_mbus_framefmt *fmt;
  2285. u32 rotation;
  2286. int ret;
  2287. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  2288. if (!sensor)
  2289. return -ENOMEM;
  2290. sensor->i2c_client = client;
  2291. fmt = &sensor->fmt;
  2292. fmt->code = ov5640_formats[0].code;
  2293. fmt->colorspace = ov5640_formats[0].colorspace;
  2294. fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
  2295. fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
  2296. fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
  2297. fmt->width = 640;
  2298. fmt->height = 480;
  2299. fmt->field = V4L2_FIELD_NONE;
  2300. sensor->frame_interval.numerator = 1;
  2301. sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
  2302. sensor->current_fr = OV5640_30_FPS;
  2303. sensor->current_mode =
  2304. &ov5640_mode_data[OV5640_30_FPS][OV5640_MODE_VGA_640_480];
  2305. sensor->pending_mode_change = true;
  2306. sensor->ae_target = 52;
  2307. /* optional indication of physical rotation of sensor */
  2308. ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
  2309. &rotation);
  2310. if (!ret) {
  2311. switch (rotation) {
  2312. case 180:
  2313. sensor->upside_down = true;
  2314. /* fall through */
  2315. case 0:
  2316. break;
  2317. default:
  2318. dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
  2319. rotation);
  2320. }
  2321. }
  2322. endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
  2323. NULL);
  2324. if (!endpoint) {
  2325. dev_err(dev, "endpoint node not found\n");
  2326. return -EINVAL;
  2327. }
  2328. ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
  2329. fwnode_handle_put(endpoint);
  2330. if (ret) {
  2331. dev_err(dev, "Could not parse endpoint\n");
  2332. return ret;
  2333. }
  2334. /* get system clock (xclk) */
  2335. sensor->xclk = devm_clk_get(dev, "xclk");
  2336. if (IS_ERR(sensor->xclk)) {
  2337. dev_err(dev, "failed to get xclk\n");
  2338. return PTR_ERR(sensor->xclk);
  2339. }
  2340. sensor->xclk_freq = clk_get_rate(sensor->xclk);
  2341. if (sensor->xclk_freq < OV5640_XCLK_MIN ||
  2342. sensor->xclk_freq > OV5640_XCLK_MAX) {
  2343. dev_err(dev, "xclk frequency out of range: %d Hz\n",
  2344. sensor->xclk_freq);
  2345. return -EINVAL;
  2346. }
  2347. /* request optional power down pin */
  2348. sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
  2349. GPIOD_OUT_HIGH);
  2350. /* request optional reset pin */
  2351. sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  2352. GPIOD_OUT_HIGH);
  2353. v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
  2354. sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  2355. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  2356. sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  2357. ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
  2358. if (ret)
  2359. return ret;
  2360. ret = ov5640_get_regulators(sensor);
  2361. if (ret)
  2362. return ret;
  2363. mutex_init(&sensor->lock);
  2364. ret = ov5640_check_chip_id(sensor);
  2365. if (ret)
  2366. goto entity_cleanup;
  2367. ret = ov5640_init_controls(sensor);
  2368. if (ret)
  2369. goto entity_cleanup;
  2370. ret = v4l2_async_register_subdev(&sensor->sd);
  2371. if (ret)
  2372. goto free_ctrls;
  2373. return 0;
  2374. free_ctrls:
  2375. v4l2_ctrl_handler_free(&sensor->ctrls.handler);
  2376. entity_cleanup:
  2377. mutex_destroy(&sensor->lock);
  2378. media_entity_cleanup(&sensor->sd.entity);
  2379. return ret;
  2380. }
  2381. static int ov5640_remove(struct i2c_client *client)
  2382. {
  2383. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2384. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  2385. v4l2_async_unregister_subdev(&sensor->sd);
  2386. mutex_destroy(&sensor->lock);
  2387. media_entity_cleanup(&sensor->sd.entity);
  2388. v4l2_ctrl_handler_free(&sensor->ctrls.handler);
  2389. return 0;
  2390. }
  2391. static const struct i2c_device_id ov5640_id[] = {
  2392. {"ov5640", 0},
  2393. {},
  2394. };
  2395. MODULE_DEVICE_TABLE(i2c, ov5640_id);
  2396. static const struct of_device_id ov5640_dt_ids[] = {
  2397. { .compatible = "ovti,ov5640" },
  2398. { /* sentinel */ }
  2399. };
  2400. MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
  2401. static struct i2c_driver ov5640_i2c_driver = {
  2402. .driver = {
  2403. .name = "ov5640",
  2404. .of_match_table = ov5640_dt_ids,
  2405. },
  2406. .id_table = ov5640_id,
  2407. .probe = ov5640_probe,
  2408. .remove = ov5640_remove,
  2409. };
  2410. module_i2c_driver(ov5640_i2c_driver);
  2411. MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
  2412. MODULE_LICENSE("GPL");