ov5640.c 82 KB

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