ov5640.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
  4. * Copyright (C) 2014-2017 Mentor Graphics Inc.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/clkdev.h>
  9. #include <linux/ctype.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/i2c.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/slab.h>
  20. #include <linux/types.h>
  21. #include <media/v4l2-async.h>
  22. #include <media/v4l2-ctrls.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-event.h>
  25. #include <media/v4l2-fwnode.h>
  26. #include <media/v4l2-subdev.h>
  27. /* min/typical/max system clock (xclk) frequencies */
  28. #define OV5640_XCLK_MIN 6000000
  29. #define OV5640_XCLK_MAX 54000000
  30. #define OV5640_NATIVE_WIDTH 2624
  31. #define OV5640_NATIVE_HEIGHT 1964
  32. #define OV5640_PIXEL_ARRAY_TOP 14
  33. #define OV5640_PIXEL_ARRAY_LEFT 16
  34. #define OV5640_PIXEL_ARRAY_WIDTH 2592
  35. #define OV5640_PIXEL_ARRAY_HEIGHT 1944
  36. /* FIXME: not documented. */
  37. #define OV5640_MIN_VBLANK 24
  38. #define OV5640_MAX_VTS 3375
  39. #define OV5640_DEFAULT_SLAVE_ID 0x3c
  40. #define OV5640_LINK_RATE_MAX 490000000U
  41. #define OV5640_REG_SYS_RESET02 0x3002
  42. #define OV5640_REG_SYS_CLOCK_ENABLE02 0x3006
  43. #define OV5640_REG_SYS_CTRL0 0x3008
  44. #define OV5640_REG_SYS_CTRL0_SW_PWDN 0x42
  45. #define OV5640_REG_SYS_CTRL0_SW_PWUP 0x02
  46. #define OV5640_REG_SYS_CTRL0_SW_RST 0x82
  47. #define OV5640_REG_CHIP_ID 0x300a
  48. #define OV5640_REG_IO_MIPI_CTRL00 0x300e
  49. #define OV5640_REG_PAD_OUTPUT_ENABLE01 0x3017
  50. #define OV5640_REG_PAD_OUTPUT_ENABLE02 0x3018
  51. #define OV5640_REG_PAD_OUTPUT00 0x3019
  52. #define OV5640_REG_SYSTEM_CONTROL1 0x302e
  53. #define OV5640_REG_SC_PLL_CTRL0 0x3034
  54. #define OV5640_REG_SC_PLL_CTRL1 0x3035
  55. #define OV5640_REG_SC_PLL_CTRL2 0x3036
  56. #define OV5640_REG_SC_PLL_CTRL3 0x3037
  57. #define OV5640_REG_SLAVE_ID 0x3100
  58. #define OV5640_REG_SCCB_SYS_CTRL1 0x3103
  59. #define OV5640_REG_SYS_ROOT_DIVIDER 0x3108
  60. #define OV5640_REG_AWB_R_GAIN 0x3400
  61. #define OV5640_REG_AWB_G_GAIN 0x3402
  62. #define OV5640_REG_AWB_B_GAIN 0x3404
  63. #define OV5640_REG_AWB_MANUAL_CTRL 0x3406
  64. #define OV5640_REG_AEC_PK_EXPOSURE_HI 0x3500
  65. #define OV5640_REG_AEC_PK_EXPOSURE_MED 0x3501
  66. #define OV5640_REG_AEC_PK_EXPOSURE_LO 0x3502
  67. #define OV5640_REG_AEC_PK_MANUAL 0x3503
  68. #define OV5640_REG_AEC_PK_REAL_GAIN 0x350a
  69. #define OV5640_REG_AEC_PK_VTS 0x350c
  70. #define OV5640_REG_TIMING_HS 0x3800
  71. #define OV5640_REG_TIMING_VS 0x3802
  72. #define OV5640_REG_TIMING_HW 0x3804
  73. #define OV5640_REG_TIMING_VH 0x3806
  74. #define OV5640_REG_TIMING_DVPHO 0x3808
  75. #define OV5640_REG_TIMING_DVPVO 0x380a
  76. #define OV5640_REG_TIMING_HTS 0x380c
  77. #define OV5640_REG_TIMING_VTS 0x380e
  78. #define OV5640_REG_TIMING_HOFFS 0x3810
  79. #define OV5640_REG_TIMING_VOFFS 0x3812
  80. #define OV5640_REG_TIMING_TC_REG20 0x3820
  81. #define OV5640_REG_TIMING_TC_REG21 0x3821
  82. #define OV5640_REG_AEC_CTRL00 0x3a00
  83. #define OV5640_REG_AEC_B50_STEP 0x3a08
  84. #define OV5640_REG_AEC_B60_STEP 0x3a0a
  85. #define OV5640_REG_AEC_CTRL0D 0x3a0d
  86. #define OV5640_REG_AEC_CTRL0E 0x3a0e
  87. #define OV5640_REG_AEC_CTRL0F 0x3a0f
  88. #define OV5640_REG_AEC_CTRL10 0x3a10
  89. #define OV5640_REG_AEC_CTRL11 0x3a11
  90. #define OV5640_REG_AEC_CTRL1B 0x3a1b
  91. #define OV5640_REG_AEC_CTRL1E 0x3a1e
  92. #define OV5640_REG_AEC_CTRL1F 0x3a1f
  93. #define OV5640_REG_HZ5060_CTRL00 0x3c00
  94. #define OV5640_REG_HZ5060_CTRL01 0x3c01
  95. #define OV5640_REG_SIGMADELTA_CTRL0C 0x3c0c
  96. #define OV5640_REG_FRAME_CTRL01 0x4202
  97. #define OV5640_REG_FORMAT_CONTROL00 0x4300
  98. #define OV5640_REG_VFIFO_HSIZE 0x4602
  99. #define OV5640_REG_VFIFO_VSIZE 0x4604
  100. #define OV5640_REG_JPG_MODE_SELECT 0x4713
  101. #define OV5640_REG_CCIR656_CTRL00 0x4730
  102. #define OV5640_REG_POLARITY_CTRL00 0x4740
  103. #define OV5640_REG_MIPI_CTRL00 0x4800
  104. #define OV5640_REG_DEBUG_MODE 0x4814
  105. #define OV5640_REG_PCLK_PERIOD 0x4837
  106. #define OV5640_REG_ISP_FORMAT_MUX_CTRL 0x501f
  107. #define OV5640_REG_PRE_ISP_TEST_SET1 0x503d
  108. #define OV5640_REG_SDE_CTRL0 0x5580
  109. #define OV5640_REG_SDE_CTRL1 0x5581
  110. #define OV5640_REG_SDE_CTRL3 0x5583
  111. #define OV5640_REG_SDE_CTRL4 0x5584
  112. #define OV5640_REG_SDE_CTRL5 0x5585
  113. #define OV5640_REG_AVG_READOUT 0x56a1
  114. enum ov5640_mode_id {
  115. OV5640_MODE_QQVGA_160_120 = 0,
  116. OV5640_MODE_QCIF_176_144,
  117. OV5640_MODE_QVGA_320_240,
  118. OV5640_MODE_VGA_640_480,
  119. OV5640_MODE_NTSC_720_480,
  120. OV5640_MODE_PAL_720_576,
  121. OV5640_MODE_XGA_1024_768,
  122. OV5640_MODE_720P_1280_720,
  123. OV5640_MODE_1080P_1920_1080,
  124. OV5640_MODE_QSXGA_2592_1944,
  125. OV5640_NUM_MODES,
  126. };
  127. enum ov5640_frame_rate {
  128. OV5640_15_FPS = 0,
  129. OV5640_30_FPS,
  130. OV5640_60_FPS,
  131. OV5640_NUM_FRAMERATES,
  132. };
  133. enum ov5640_pixel_rate_id {
  134. OV5640_PIXEL_RATE_168M,
  135. OV5640_PIXEL_RATE_148M,
  136. OV5640_PIXEL_RATE_124M,
  137. OV5640_PIXEL_RATE_96M,
  138. OV5640_PIXEL_RATE_48M,
  139. OV5640_NUM_PIXEL_RATES,
  140. };
  141. /*
  142. * The chip manual suggests 24/48/96/192 MHz pixel clocks.
  143. *
  144. * 192MHz exceeds the sysclk limits; use 168MHz as maximum pixel rate for
  145. * full resolution mode @15 FPS.
  146. */
  147. static const u32 ov5640_pixel_rates[] = {
  148. [OV5640_PIXEL_RATE_168M] = 168000000,
  149. [OV5640_PIXEL_RATE_148M] = 148000000,
  150. [OV5640_PIXEL_RATE_124M] = 124000000,
  151. [OV5640_PIXEL_RATE_96M] = 96000000,
  152. [OV5640_PIXEL_RATE_48M] = 48000000,
  153. };
  154. /*
  155. * MIPI CSI-2 link frequencies.
  156. *
  157. * Derived from the above defined pixel rate for bpp = (8, 16, 24) and
  158. * data_lanes = (1, 2)
  159. *
  160. * link_freq = (pixel_rate * bpp) / (2 * data_lanes)
  161. */
  162. static const s64 ov5640_csi2_link_freqs[] = {
  163. 992000000, 888000000, 768000000, 744000000, 672000000, 672000000,
  164. 592000000, 592000000, 576000000, 576000000, 496000000, 496000000,
  165. 384000000, 384000000, 384000000, 336000000, 296000000, 288000000,
  166. 248000000, 192000000, 192000000, 192000000, 96000000,
  167. };
  168. /* Link freq for default mode: UYVY 16 bpp, 2 data lanes. */
  169. #define OV5640_DEFAULT_LINK_FREQ 13
  170. enum ov5640_format_mux {
  171. OV5640_FMT_MUX_YUV422 = 0,
  172. OV5640_FMT_MUX_RGB,
  173. OV5640_FMT_MUX_DITHER,
  174. OV5640_FMT_MUX_RAW_DPC,
  175. OV5640_FMT_MUX_SNR_RAW,
  176. OV5640_FMT_MUX_RAW_CIP,
  177. };
  178. struct ov5640_pixfmt {
  179. u32 code;
  180. u32 colorspace;
  181. u8 bpp;
  182. u8 ctrl00;
  183. enum ov5640_format_mux mux;
  184. };
  185. static const struct ov5640_pixfmt ov5640_dvp_formats[] = {
  186. {
  187. /* YUV422, YUYV */
  188. .code = MEDIA_BUS_FMT_JPEG_1X8,
  189. .colorspace = V4L2_COLORSPACE_JPEG,
  190. .bpp = 16,
  191. .ctrl00 = 0x30,
  192. .mux = OV5640_FMT_MUX_YUV422,
  193. }, {
  194. /* YUV422, UYVY */
  195. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  196. .colorspace = V4L2_COLORSPACE_SRGB,
  197. .bpp = 16,
  198. .ctrl00 = 0x3f,
  199. .mux = OV5640_FMT_MUX_YUV422,
  200. }, {
  201. /* YUV422, YUYV */
  202. .code = MEDIA_BUS_FMT_YUYV8_2X8,
  203. .colorspace = V4L2_COLORSPACE_SRGB,
  204. .bpp = 16,
  205. .ctrl00 = 0x30,
  206. .mux = OV5640_FMT_MUX_YUV422,
  207. }, {
  208. /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
  209. .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
  210. .colorspace = V4L2_COLORSPACE_SRGB,
  211. .bpp = 16,
  212. .ctrl00 = 0x6f,
  213. .mux = OV5640_FMT_MUX_RGB,
  214. }, {
  215. /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
  216. .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
  217. .colorspace = V4L2_COLORSPACE_SRGB,
  218. .bpp = 16,
  219. .ctrl00 = 0x61,
  220. .mux = OV5640_FMT_MUX_RGB,
  221. }, {
  222. /* Raw, BGBG... / GRGR... */
  223. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  224. .colorspace = V4L2_COLORSPACE_SRGB,
  225. .bpp = 8,
  226. .ctrl00 = 0x00,
  227. .mux = OV5640_FMT_MUX_RAW_DPC,
  228. }, {
  229. /* Raw bayer, GBGB... / RGRG... */
  230. .code = MEDIA_BUS_FMT_SGBRG8_1X8,
  231. .colorspace = V4L2_COLORSPACE_SRGB,
  232. .bpp = 8,
  233. .ctrl00 = 0x01,
  234. .mux = OV5640_FMT_MUX_RAW_DPC,
  235. }, {
  236. /* Raw bayer, GRGR... / BGBG... */
  237. .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  238. .colorspace = V4L2_COLORSPACE_SRGB,
  239. .bpp = 8,
  240. .ctrl00 = 0x02,
  241. .mux = OV5640_FMT_MUX_RAW_DPC,
  242. }, {
  243. /* Raw bayer, RGRG... / GBGB... */
  244. .code = MEDIA_BUS_FMT_SRGGB8_1X8,
  245. .colorspace = V4L2_COLORSPACE_SRGB,
  246. .bpp = 8,
  247. .ctrl00 = 0x03,
  248. .mux = OV5640_FMT_MUX_RAW_DPC,
  249. },
  250. { /* sentinel */ }
  251. };
  252. static const struct ov5640_pixfmt ov5640_csi2_formats[] = {
  253. {
  254. /* YUV422, YUYV */
  255. .code = MEDIA_BUS_FMT_JPEG_1X8,
  256. .colorspace = V4L2_COLORSPACE_JPEG,
  257. .bpp = 16,
  258. .ctrl00 = 0x30,
  259. .mux = OV5640_FMT_MUX_YUV422,
  260. }, {
  261. /* YUV422, UYVY */
  262. .code = MEDIA_BUS_FMT_UYVY8_1X16,
  263. .colorspace = V4L2_COLORSPACE_SRGB,
  264. .bpp = 16,
  265. .ctrl00 = 0x3f,
  266. .mux = OV5640_FMT_MUX_YUV422,
  267. }, {
  268. /* YUV422, YUYV */
  269. .code = MEDIA_BUS_FMT_YUYV8_1X16,
  270. .colorspace = V4L2_COLORSPACE_SRGB,
  271. .bpp = 16,
  272. .ctrl00 = 0x30,
  273. .mux = OV5640_FMT_MUX_YUV422,
  274. }, {
  275. /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
  276. .code = MEDIA_BUS_FMT_RGB565_1X16,
  277. .colorspace = V4L2_COLORSPACE_SRGB,
  278. .bpp = 16,
  279. .ctrl00 = 0x6f,
  280. .mux = OV5640_FMT_MUX_RGB,
  281. }, {
  282. /* BGR888: RGB */
  283. .code = MEDIA_BUS_FMT_BGR888_1X24,
  284. .colorspace = V4L2_COLORSPACE_SRGB,
  285. .bpp = 24,
  286. .ctrl00 = 0x23,
  287. .mux = OV5640_FMT_MUX_RGB,
  288. }, {
  289. /* Raw, BGBG... / GRGR... */
  290. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  291. .colorspace = V4L2_COLORSPACE_SRGB,
  292. .bpp = 8,
  293. .ctrl00 = 0x00,
  294. .mux = OV5640_FMT_MUX_RAW_DPC,
  295. }, {
  296. /* Raw bayer, GBGB... / RGRG... */
  297. .code = MEDIA_BUS_FMT_SGBRG8_1X8,
  298. .colorspace = V4L2_COLORSPACE_SRGB,
  299. .bpp = 8,
  300. .ctrl00 = 0x01,
  301. .mux = OV5640_FMT_MUX_RAW_DPC,
  302. }, {
  303. /* Raw bayer, GRGR... / BGBG... */
  304. .code = MEDIA_BUS_FMT_SGRBG8_1X8,
  305. .colorspace = V4L2_COLORSPACE_SRGB,
  306. .bpp = 8,
  307. .ctrl00 = 0x02,
  308. .mux = OV5640_FMT_MUX_RAW_DPC,
  309. }, {
  310. /* Raw bayer, RGRG... / GBGB... */
  311. .code = MEDIA_BUS_FMT_SRGGB8_1X8,
  312. .colorspace = V4L2_COLORSPACE_SRGB,
  313. .bpp = 8,
  314. .ctrl00 = 0x03,
  315. .mux = OV5640_FMT_MUX_RAW_DPC,
  316. },
  317. { /* sentinel */ }
  318. };
  319. /*
  320. * FIXME: remove this when a subdev API becomes available
  321. * to set the MIPI CSI-2 virtual channel.
  322. */
  323. static unsigned int virtual_channel;
  324. module_param(virtual_channel, uint, 0444);
  325. MODULE_PARM_DESC(virtual_channel,
  326. "MIPI CSI-2 virtual channel (0..3), default 0");
  327. static const int ov5640_framerates[] = {
  328. [OV5640_15_FPS] = 15,
  329. [OV5640_30_FPS] = 30,
  330. [OV5640_60_FPS] = 60,
  331. };
  332. /* regulator supplies */
  333. static const char * const ov5640_supply_name[] = {
  334. "DOVDD", /* Digital I/O (1.8V) supply */
  335. "AVDD", /* Analog (2.8V) supply */
  336. "DVDD", /* Digital Core (1.5V) supply */
  337. };
  338. #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
  339. /*
  340. * Image size under 1280 * 960 are SUBSAMPLING
  341. * Image size upper 1280 * 960 are SCALING
  342. */
  343. enum ov5640_downsize_mode {
  344. SUBSAMPLING,
  345. SCALING,
  346. };
  347. struct reg_value {
  348. u16 reg_addr;
  349. u8 val;
  350. u8 mask;
  351. u32 delay_ms;
  352. };
  353. struct ov5640_timings {
  354. /* Analog crop rectangle. */
  355. struct v4l2_rect analog_crop;
  356. /* Visibile crop: from analog crop top-left corner. */
  357. struct v4l2_rect crop;
  358. /* Total pixels per line: width + fixed hblank. */
  359. u32 htot;
  360. /* Default vertical blanking: frame height = height + vblank. */
  361. u32 vblank_def;
  362. };
  363. struct ov5640_mode_info {
  364. enum ov5640_mode_id id;
  365. enum ov5640_downsize_mode dn_mode;
  366. enum ov5640_pixel_rate_id pixel_rate;
  367. unsigned int width;
  368. unsigned int height;
  369. struct ov5640_timings dvp_timings;
  370. struct ov5640_timings csi2_timings;
  371. const struct reg_value *reg_data;
  372. u32 reg_data_size;
  373. /* Used by s_frame_interval only. */
  374. u32 max_fps;
  375. u32 def_fps;
  376. };
  377. struct ov5640_ctrls {
  378. struct v4l2_ctrl_handler handler;
  379. struct v4l2_ctrl *pixel_rate;
  380. struct v4l2_ctrl *link_freq;
  381. struct v4l2_ctrl *hblank;
  382. struct v4l2_ctrl *vblank;
  383. struct {
  384. struct v4l2_ctrl *auto_exp;
  385. struct v4l2_ctrl *exposure;
  386. };
  387. struct {
  388. struct v4l2_ctrl *auto_wb;
  389. struct v4l2_ctrl *blue_balance;
  390. struct v4l2_ctrl *red_balance;
  391. };
  392. struct {
  393. struct v4l2_ctrl *auto_gain;
  394. struct v4l2_ctrl *gain;
  395. };
  396. struct v4l2_ctrl *brightness;
  397. struct v4l2_ctrl *light_freq;
  398. struct v4l2_ctrl *saturation;
  399. struct v4l2_ctrl *contrast;
  400. struct v4l2_ctrl *hue;
  401. struct v4l2_ctrl *test_pattern;
  402. struct v4l2_ctrl *hflip;
  403. struct v4l2_ctrl *vflip;
  404. };
  405. struct ov5640_dev {
  406. struct i2c_client *i2c_client;
  407. struct v4l2_subdev sd;
  408. struct media_pad pad;
  409. struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
  410. struct clk *xclk; /* system clock to OV5640 */
  411. u32 xclk_freq;
  412. struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
  413. struct gpio_desc *reset_gpio;
  414. struct gpio_desc *pwdn_gpio;
  415. bool upside_down;
  416. /* lock to protect all members below */
  417. struct mutex lock;
  418. struct v4l2_mbus_framefmt fmt;
  419. bool pending_fmt_change;
  420. const struct ov5640_mode_info *current_mode;
  421. const struct ov5640_mode_info *last_mode;
  422. enum ov5640_frame_rate current_fr;
  423. struct v4l2_fract frame_interval;
  424. s64 current_link_freq;
  425. struct ov5640_ctrls ctrls;
  426. u32 prev_sysclk, prev_hts;
  427. u32 ae_low, ae_high, ae_target;
  428. bool pending_mode_change;
  429. bool streaming;
  430. };
  431. static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
  432. {
  433. return container_of(sd, struct ov5640_dev, sd);
  434. }
  435. static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
  436. {
  437. return &container_of(ctrl->handler, struct ov5640_dev,
  438. ctrls.handler)->sd;
  439. }
  440. static inline bool ov5640_is_csi2(const struct ov5640_dev *sensor)
  441. {
  442. return sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY;
  443. }
  444. static inline const struct ov5640_pixfmt *
  445. ov5640_formats(struct ov5640_dev *sensor)
  446. {
  447. return ov5640_is_csi2(sensor) ? ov5640_csi2_formats
  448. : ov5640_dvp_formats;
  449. }
  450. static const struct ov5640_pixfmt *
  451. ov5640_code_to_pixfmt(struct ov5640_dev *sensor, u32 code)
  452. {
  453. const struct ov5640_pixfmt *formats = ov5640_formats(sensor);
  454. unsigned int i;
  455. for (i = 0; formats[i].code; ++i) {
  456. if (formats[i].code == code)
  457. return &formats[i];
  458. }
  459. return &formats[0];
  460. }
  461. static u32 ov5640_code_to_bpp(struct ov5640_dev *sensor, u32 code)
  462. {
  463. const struct ov5640_pixfmt *format = ov5640_code_to_pixfmt(sensor,
  464. code);
  465. return format->bpp;
  466. }
  467. /*
  468. * FIXME: all of these register tables are likely filled with
  469. * entries that set the register to their power-on default values,
  470. * and which are otherwise not touched by this driver. Those entries
  471. * should be identified and removed to speed register load time
  472. * over i2c.
  473. */
  474. /* YUV422 UYVY VGA@30fps */
  475. static const struct v4l2_mbus_framefmt ov5640_default_fmt = {
  476. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  477. .width = 640,
  478. .height = 480,
  479. .colorspace = V4L2_COLORSPACE_SRGB,
  480. .ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB),
  481. .quantization = V4L2_QUANTIZATION_FULL_RANGE,
  482. .xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB),
  483. .field = V4L2_FIELD_NONE,
  484. };
  485. static const struct reg_value ov5640_init_setting[] = {
  486. {0x3103, 0x11, 0, 0},
  487. {0x3103, 0x03, 0, 0}, {0x3630, 0x36, 0, 0},
  488. {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
  489. {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
  490. {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
  491. {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
  492. {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
  493. {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
  494. {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
  495. {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
  496. {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
  497. {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
  498. {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
  499. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  500. {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
  501. {0x3815, 0x31, 0, 0},
  502. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  503. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  504. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  505. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  506. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  507. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
  508. {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
  509. {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
  510. {0x501f, 0x00, 0, 0}, {0x440e, 0x00, 0, 0}, {0x4837, 0x0a, 0, 0},
  511. {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
  512. {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
  513. {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
  514. {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
  515. {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
  516. {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
  517. {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
  518. {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
  519. {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
  520. {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
  521. {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
  522. {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
  523. {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
  524. {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
  525. {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
  526. {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
  527. {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
  528. {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
  529. {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
  530. {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
  531. {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
  532. {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
  533. {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
  534. {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
  535. {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
  536. {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
  537. {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
  538. {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
  539. {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
  540. {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
  541. {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
  542. {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
  543. {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
  544. {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
  545. {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
  546. {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
  547. {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
  548. {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
  549. {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
  550. {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
  551. {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
  552. {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
  553. {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
  554. {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
  555. {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
  556. {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
  557. {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
  558. {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
  559. {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
  560. {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
  561. };
  562. static const struct reg_value ov5640_setting_low_res[] = {
  563. {0x3c07, 0x08, 0, 0},
  564. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  565. {0x3814, 0x31, 0, 0},
  566. {0x3815, 0x31, 0, 0},
  567. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  568. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
  569. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  570. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  571. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  572. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
  573. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  574. {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
  575. };
  576. static const struct reg_value ov5640_setting_720P_1280_720[] = {
  577. {0x3c07, 0x07, 0, 0},
  578. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  579. {0x3814, 0x31, 0, 0},
  580. {0x3815, 0x31, 0, 0},
  581. {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
  582. {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
  583. {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
  584. {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
  585. {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
  586. {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
  587. {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
  588. {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
  589. };
  590. static const struct reg_value ov5640_setting_1080P_1920_1080[] = {
  591. {0x3c07, 0x08, 0, 0},
  592. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  593. {0x3814, 0x11, 0, 0},
  594. {0x3815, 0x11, 0, 0},
  595. {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
  596. {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
  597. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  598. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  599. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  600. {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
  601. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  602. {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
  603. {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
  604. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  605. {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
  606. {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
  607. {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
  608. {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
  609. {0x3a15, 0x60, 0, 0}, {0x4407, 0x04, 0, 0},
  610. {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
  611. {0x4005, 0x1a, 0, 0},
  612. };
  613. static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = {
  614. {0x3c07, 0x08, 0, 0},
  615. {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
  616. {0x3814, 0x11, 0, 0},
  617. {0x3815, 0x11, 0, 0},
  618. {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
  619. {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
  620. {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
  621. {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
  622. {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
  623. {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0},
  624. {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
  625. {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
  626. };
  627. static const struct ov5640_mode_info ov5640_mode_data[OV5640_NUM_MODES] = {
  628. {
  629. /* 160x120 */
  630. .id = OV5640_MODE_QQVGA_160_120,
  631. .dn_mode = SUBSAMPLING,
  632. .pixel_rate = OV5640_PIXEL_RATE_48M,
  633. .width = 160,
  634. .height = 120,
  635. .dvp_timings = {
  636. .analog_crop = {
  637. .left = 0,
  638. .top = 4,
  639. .width = 2624,
  640. .height = 1944,
  641. },
  642. .crop = {
  643. .left = 16,
  644. .top = 6,
  645. .width = 160,
  646. .height = 120,
  647. },
  648. .htot = 1896,
  649. .vblank_def = 864,
  650. },
  651. .csi2_timings = {
  652. /* Feed the full valid pixel array to the ISP. */
  653. .analog_crop = {
  654. .left = OV5640_PIXEL_ARRAY_LEFT,
  655. .top = OV5640_PIXEL_ARRAY_TOP,
  656. .width = OV5640_PIXEL_ARRAY_WIDTH,
  657. .height = OV5640_PIXEL_ARRAY_HEIGHT,
  658. },
  659. /* Maintain a minimum processing margin. */
  660. .crop = {
  661. .left = 2,
  662. .top = 4,
  663. .width = 160,
  664. .height = 120,
  665. },
  666. .htot = 1600,
  667. .vblank_def = 878,
  668. },
  669. .reg_data = ov5640_setting_low_res,
  670. .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
  671. .max_fps = OV5640_30_FPS,
  672. .def_fps = OV5640_30_FPS
  673. }, {
  674. /* 176x144 */
  675. .id = OV5640_MODE_QCIF_176_144,
  676. .dn_mode = SUBSAMPLING,
  677. .pixel_rate = OV5640_PIXEL_RATE_48M,
  678. .width = 176,
  679. .height = 144,
  680. .dvp_timings = {
  681. .analog_crop = {
  682. .left = 0,
  683. .top = 4,
  684. .width = 2624,
  685. .height = 1944,
  686. },
  687. .crop = {
  688. .left = 16,
  689. .top = 6,
  690. .width = 176,
  691. .height = 144,
  692. },
  693. .htot = 1896,
  694. .vblank_def = 840,
  695. },
  696. .csi2_timings = {
  697. /* Feed the full valid pixel array to the ISP. */
  698. .analog_crop = {
  699. .left = OV5640_PIXEL_ARRAY_LEFT,
  700. .top = OV5640_PIXEL_ARRAY_TOP,
  701. .width = OV5640_PIXEL_ARRAY_WIDTH,
  702. .height = OV5640_PIXEL_ARRAY_HEIGHT,
  703. },
  704. /* Maintain a minimum processing margin. */
  705. .crop = {
  706. .left = 2,
  707. .top = 4,
  708. .width = 176,
  709. .height = 144,
  710. },
  711. .htot = 1600,
  712. .vblank_def = 854,
  713. },
  714. .reg_data = ov5640_setting_low_res,
  715. .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
  716. .max_fps = OV5640_30_FPS,
  717. .def_fps = OV5640_30_FPS
  718. }, {
  719. /* 320x240 */
  720. .id = OV5640_MODE_QVGA_320_240,
  721. .dn_mode = SUBSAMPLING,
  722. .width = 320,
  723. .height = 240,
  724. .pixel_rate = OV5640_PIXEL_RATE_48M,
  725. .dvp_timings = {
  726. .analog_crop = {
  727. .left = 0,
  728. .top = 4,
  729. .width = 2624,
  730. .height = 1944,
  731. },
  732. .crop = {
  733. .left = 16,
  734. .top = 6,
  735. .width = 320,
  736. .height = 240,
  737. },
  738. .htot = 1896,
  739. .vblank_def = 744,
  740. },
  741. .csi2_timings = {
  742. /* Feed the full valid pixel array to the ISP. */
  743. .analog_crop = {
  744. .left = OV5640_PIXEL_ARRAY_LEFT,
  745. .top = OV5640_PIXEL_ARRAY_TOP,
  746. .width = OV5640_PIXEL_ARRAY_WIDTH,
  747. .height = OV5640_PIXEL_ARRAY_HEIGHT,
  748. },
  749. /* Maintain a minimum processing margin. */
  750. .crop = {
  751. .left = 2,
  752. .top = 4,
  753. .width = 320,
  754. .height = 240,
  755. },
  756. .htot = 1600,
  757. .vblank_def = 760,
  758. },
  759. .reg_data = ov5640_setting_low_res,
  760. .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
  761. .max_fps = OV5640_30_FPS,
  762. .def_fps = OV5640_30_FPS
  763. }, {
  764. /* 640x480 */
  765. .id = OV5640_MODE_VGA_640_480,
  766. .dn_mode = SUBSAMPLING,
  767. .pixel_rate = OV5640_PIXEL_RATE_48M,
  768. .width = 640,
  769. .height = 480,
  770. .dvp_timings = {
  771. .analog_crop = {
  772. .left = 0,
  773. .top = 4,
  774. .width = 2624,
  775. .height = 1944,
  776. },
  777. .crop = {
  778. .left = 16,
  779. .top = 6,
  780. .width = 640,
  781. .height = 480,
  782. },
  783. .htot = 1896,
  784. .vblank_def = 600,
  785. },
  786. .csi2_timings = {
  787. /* Feed the full valid pixel array to the ISP. */
  788. .analog_crop = {
  789. .left = OV5640_PIXEL_ARRAY_LEFT,
  790. .top = OV5640_PIXEL_ARRAY_TOP,
  791. .width = OV5640_PIXEL_ARRAY_WIDTH,
  792. .height = OV5640_PIXEL_ARRAY_HEIGHT,
  793. },
  794. /* Maintain a minimum processing margin. */
  795. .crop = {
  796. .left = 2,
  797. .top = 4,
  798. .width = 640,
  799. .height = 480,
  800. },
  801. .htot = 1600,
  802. .vblank_def = 520,
  803. },
  804. .reg_data = ov5640_setting_low_res,
  805. .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
  806. .max_fps = OV5640_60_FPS,
  807. .def_fps = OV5640_30_FPS
  808. }, {
  809. /* 720x480 */
  810. .id = OV5640_MODE_NTSC_720_480,
  811. .dn_mode = SUBSAMPLING,
  812. .width = 720,
  813. .height = 480,
  814. .pixel_rate = OV5640_PIXEL_RATE_96M,
  815. .dvp_timings = {
  816. .analog_crop = {
  817. .left = 0,
  818. .top = 4,
  819. .width = 2624,
  820. .height = 1944,
  821. },
  822. .crop = {
  823. .left = 56,
  824. .top = 60,
  825. .width = 720,
  826. .height = 480,
  827. },
  828. .htot = 1896,
  829. .vblank_def = 504,
  830. },
  831. .csi2_timings = {
  832. /* Feed the full valid pixel array to the ISP. */
  833. .analog_crop = {
  834. .left = OV5640_PIXEL_ARRAY_LEFT,
  835. .top = OV5640_PIXEL_ARRAY_TOP,
  836. .width = OV5640_PIXEL_ARRAY_WIDTH,
  837. .height = OV5640_PIXEL_ARRAY_HEIGHT,
  838. },
  839. .crop = {
  840. .left = 56,
  841. .top = 60,
  842. .width = 720,
  843. .height = 480,
  844. },
  845. .htot = 1896,
  846. .vblank_def = 1206,
  847. },
  848. .reg_data = ov5640_setting_low_res,
  849. .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
  850. .max_fps = OV5640_30_FPS,
  851. .def_fps = OV5640_30_FPS
  852. }, {
  853. /* 720x576 */
  854. .id = OV5640_MODE_PAL_720_576,
  855. .dn_mode = SUBSAMPLING,
  856. .width = 720,
  857. .height = 576,
  858. .pixel_rate = OV5640_PIXEL_RATE_96M,
  859. .dvp_timings = {
  860. .analog_crop = {
  861. .left = 0,
  862. .top = 4,
  863. .width = 2624,
  864. .height = 1944,
  865. },
  866. .crop = {
  867. .left = 56,
  868. .top = 6,
  869. .width = 720,
  870. .height = 576,
  871. },
  872. .htot = 1896,
  873. .vblank_def = 408,
  874. },
  875. .csi2_timings = {
  876. /* Feed the full valid pixel array to the ISP. */
  877. .analog_crop = {
  878. .left = OV5640_PIXEL_ARRAY_LEFT,
  879. .top = OV5640_PIXEL_ARRAY_TOP,
  880. .width = OV5640_PIXEL_ARRAY_WIDTH,
  881. .height = OV5640_PIXEL_ARRAY_HEIGHT,
  882. },
  883. .crop = {
  884. .left = 56,
  885. .top = 6,
  886. .width = 720,
  887. .height = 576,
  888. },
  889. .htot = 1896,
  890. .vblank_def = 1110,
  891. },
  892. .reg_data = ov5640_setting_low_res,
  893. .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
  894. .max_fps = OV5640_30_FPS,
  895. .def_fps = OV5640_30_FPS
  896. }, {
  897. /* 1024x768 */
  898. .id = OV5640_MODE_XGA_1024_768,
  899. .dn_mode = SUBSAMPLING,
  900. .pixel_rate = OV5640_PIXEL_RATE_96M,
  901. .width = 1024,
  902. .height = 768,
  903. .dvp_timings = {
  904. .analog_crop = {
  905. .left = 0,
  906. .top = 4,
  907. .width = 2624,
  908. .height = 1944,
  909. },
  910. .crop = {
  911. .left = 16,
  912. .top = 6,
  913. .width = 1024,
  914. .height = 768,
  915. },
  916. .htot = 1896,
  917. .vblank_def = 312,
  918. },
  919. .csi2_timings = {
  920. .analog_crop = {
  921. .left = 0,
  922. .top = 4,
  923. .width = OV5640_NATIVE_WIDTH,
  924. .height = OV5640_PIXEL_ARRAY_HEIGHT,
  925. },
  926. .crop = {
  927. .left = 16,
  928. .top = 6,
  929. .width = 1024,
  930. .height = 768,
  931. },
  932. .htot = 1896,
  933. .vblank_def = 918,
  934. },
  935. .reg_data = ov5640_setting_low_res,
  936. .reg_data_size = ARRAY_SIZE(ov5640_setting_low_res),
  937. .max_fps = OV5640_30_FPS,
  938. .def_fps = OV5640_30_FPS
  939. }, {
  940. /* 1280x720 */
  941. .id = OV5640_MODE_720P_1280_720,
  942. .dn_mode = SUBSAMPLING,
  943. .pixel_rate = OV5640_PIXEL_RATE_124M,
  944. .width = 1280,
  945. .height = 720,
  946. .dvp_timings = {
  947. .analog_crop = {
  948. .left = 0,
  949. .top = 250,
  950. .width = 2624,
  951. .height = 1456,
  952. },
  953. .crop = {
  954. .left = 16,
  955. .top = 4,
  956. .width = 1280,
  957. .height = 720,
  958. },
  959. .htot = 1892,
  960. .vblank_def = 20,
  961. },
  962. .csi2_timings = {
  963. .analog_crop = {
  964. .left = 0,
  965. .top = 250,
  966. .width = 2624,
  967. .height = 1456,
  968. },
  969. .crop = {
  970. .left = 16,
  971. .top = 4,
  972. .width = 1280,
  973. .height = 720,
  974. },
  975. .htot = 1600,
  976. .vblank_def = 560,
  977. },
  978. .reg_data = ov5640_setting_720P_1280_720,
  979. .reg_data_size = ARRAY_SIZE(ov5640_setting_720P_1280_720),
  980. .max_fps = OV5640_30_FPS,
  981. .def_fps = OV5640_30_FPS
  982. }, {
  983. /* 1920x1080 */
  984. .id = OV5640_MODE_1080P_1920_1080,
  985. .dn_mode = SCALING,
  986. .pixel_rate = OV5640_PIXEL_RATE_148M,
  987. .width = 1920,
  988. .height = 1080,
  989. .dvp_timings = {
  990. .analog_crop = {
  991. .left = 336,
  992. .top = 434,
  993. .width = 1952,
  994. .height = 1088,
  995. },
  996. .crop = {
  997. .left = 16,
  998. .top = 4,
  999. .width = 1920,
  1000. .height = 1080,
  1001. },
  1002. .htot = 2500,
  1003. .vblank_def = 40,
  1004. },
  1005. .csi2_timings = {
  1006. /* Crop the full valid pixel array in the center. */
  1007. .analog_crop = {
  1008. .left = 336,
  1009. .top = 434,
  1010. .width = 1952,
  1011. .height = 1088,
  1012. },
  1013. /* Maintain a larger processing margins. */
  1014. .crop = {
  1015. .left = 16,
  1016. .top = 4,
  1017. .width = 1920,
  1018. .height = 1080,
  1019. },
  1020. .htot = 2234,
  1021. .vblank_def = 24,
  1022. },
  1023. .reg_data = ov5640_setting_1080P_1920_1080,
  1024. .reg_data_size = ARRAY_SIZE(ov5640_setting_1080P_1920_1080),
  1025. .max_fps = OV5640_30_FPS,
  1026. .def_fps = OV5640_30_FPS
  1027. }, {
  1028. /* 2592x1944 */
  1029. .id = OV5640_MODE_QSXGA_2592_1944,
  1030. .dn_mode = SCALING,
  1031. .pixel_rate = OV5640_PIXEL_RATE_168M,
  1032. .width = OV5640_PIXEL_ARRAY_WIDTH,
  1033. .height = OV5640_PIXEL_ARRAY_HEIGHT,
  1034. .dvp_timings = {
  1035. .analog_crop = {
  1036. .left = 0,
  1037. .top = 0,
  1038. .width = 2624,
  1039. .height = 1952,
  1040. },
  1041. .crop = {
  1042. .left = 16,
  1043. .top = 4,
  1044. .width = 2592,
  1045. .height = 1944,
  1046. },
  1047. .htot = 2844,
  1048. .vblank_def = 24,
  1049. },
  1050. .csi2_timings = {
  1051. /* Give more processing margin to full resolution. */
  1052. .analog_crop = {
  1053. .left = 0,
  1054. .top = 0,
  1055. .width = OV5640_NATIVE_WIDTH,
  1056. .height = 1952,
  1057. },
  1058. .crop = {
  1059. .left = 16,
  1060. .top = 4,
  1061. .width = 2592,
  1062. .height = 1944,
  1063. },
  1064. .htot = 2844,
  1065. .vblank_def = 24,
  1066. },
  1067. .reg_data = ov5640_setting_QSXGA_2592_1944,
  1068. .reg_data_size = ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944),
  1069. .max_fps = OV5640_15_FPS,
  1070. .def_fps = OV5640_15_FPS
  1071. },
  1072. };
  1073. static const struct ov5640_timings *
  1074. ov5640_timings(const struct ov5640_dev *sensor,
  1075. const struct ov5640_mode_info *mode)
  1076. {
  1077. if (ov5640_is_csi2(sensor))
  1078. return &mode->csi2_timings;
  1079. return &mode->dvp_timings;
  1080. }
  1081. static int ov5640_init_slave_id(struct ov5640_dev *sensor)
  1082. {
  1083. struct i2c_client *client = sensor->i2c_client;
  1084. struct i2c_msg msg;
  1085. u8 buf[3];
  1086. int ret;
  1087. if (client->addr == OV5640_DEFAULT_SLAVE_ID)
  1088. return 0;
  1089. buf[0] = OV5640_REG_SLAVE_ID >> 8;
  1090. buf[1] = OV5640_REG_SLAVE_ID & 0xff;
  1091. buf[2] = client->addr << 1;
  1092. msg.addr = OV5640_DEFAULT_SLAVE_ID;
  1093. msg.flags = 0;
  1094. msg.buf = buf;
  1095. msg.len = sizeof(buf);
  1096. ret = i2c_transfer(client->adapter, &msg, 1);
  1097. if (ret < 0) {
  1098. dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
  1099. return ret;
  1100. }
  1101. return 0;
  1102. }
  1103. static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
  1104. {
  1105. struct i2c_client *client = sensor->i2c_client;
  1106. struct i2c_msg msg;
  1107. u8 buf[3];
  1108. int ret;
  1109. buf[0] = reg >> 8;
  1110. buf[1] = reg & 0xff;
  1111. buf[2] = val;
  1112. msg.addr = client->addr;
  1113. msg.flags = client->flags;
  1114. msg.buf = buf;
  1115. msg.len = sizeof(buf);
  1116. ret = i2c_transfer(client->adapter, &msg, 1);
  1117. if (ret < 0) {
  1118. dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
  1119. __func__, reg, val);
  1120. return ret;
  1121. }
  1122. return 0;
  1123. }
  1124. static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
  1125. {
  1126. struct i2c_client *client = sensor->i2c_client;
  1127. struct i2c_msg msg[2];
  1128. u8 buf[2];
  1129. int ret;
  1130. buf[0] = reg >> 8;
  1131. buf[1] = reg & 0xff;
  1132. msg[0].addr = client->addr;
  1133. msg[0].flags = client->flags;
  1134. msg[0].buf = buf;
  1135. msg[0].len = sizeof(buf);
  1136. msg[1].addr = client->addr;
  1137. msg[1].flags = client->flags | I2C_M_RD;
  1138. msg[1].buf = buf;
  1139. msg[1].len = 1;
  1140. ret = i2c_transfer(client->adapter, msg, 2);
  1141. if (ret < 0) {
  1142. dev_err(&client->dev, "%s: error: reg=%x\n",
  1143. __func__, reg);
  1144. return ret;
  1145. }
  1146. *val = buf[0];
  1147. return 0;
  1148. }
  1149. static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
  1150. {
  1151. u8 hi, lo;
  1152. int ret;
  1153. ret = ov5640_read_reg(sensor, reg, &hi);
  1154. if (ret)
  1155. return ret;
  1156. ret = ov5640_read_reg(sensor, reg + 1, &lo);
  1157. if (ret)
  1158. return ret;
  1159. *val = ((u16)hi << 8) | (u16)lo;
  1160. return 0;
  1161. }
  1162. static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
  1163. {
  1164. int ret;
  1165. ret = ov5640_write_reg(sensor, reg, val >> 8);
  1166. if (ret)
  1167. return ret;
  1168. return ov5640_write_reg(sensor, reg + 1, val & 0xff);
  1169. }
  1170. static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
  1171. u8 mask, u8 val)
  1172. {
  1173. u8 readval;
  1174. int ret;
  1175. ret = ov5640_read_reg(sensor, reg, &readval);
  1176. if (ret)
  1177. return ret;
  1178. readval &= ~mask;
  1179. val &= mask;
  1180. val |= readval;
  1181. return ov5640_write_reg(sensor, reg, val);
  1182. }
  1183. /*
  1184. * After trying the various combinations, reading various
  1185. * documentations spread around the net, and from the various
  1186. * feedback, the clock tree is probably as follows:
  1187. *
  1188. * +--------------+
  1189. * | Ext. Clock |
  1190. * +-+------------+
  1191. * | +----------+
  1192. * +->| PLL1 | - reg 0x3036, for the multiplier
  1193. * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
  1194. * | +--------------+
  1195. * +->| System Clock | - reg 0x3035, bits 4-7
  1196. * +-+------------+
  1197. * | +--------------+
  1198. * +->| MIPI Divider | - reg 0x3035, bits 0-3
  1199. * | +-+------------+
  1200. * | +----------------> MIPI SCLK
  1201. * | + +-----+
  1202. * | +->| / 2 |-------> MIPI BIT CLK
  1203. * | +-----+
  1204. * | +--------------+
  1205. * +->| PLL Root Div | - reg 0x3037, bit 4
  1206. * +-+------------+
  1207. * | +---------+
  1208. * +->| Bit Div | - reg 0x3034, bits 0-3
  1209. * +-+-------+
  1210. * | +-------------+
  1211. * +->| SCLK Div | - reg 0x3108, bits 0-1
  1212. * | +-+-----------+
  1213. * | +---------------> SCLK
  1214. * | +-------------+
  1215. * +->| SCLK 2X Div | - reg 0x3108, bits 2-3
  1216. * | +-+-----------+
  1217. * | +---------------> SCLK 2X
  1218. * | +-------------+
  1219. * +->| PCLK Div | - reg 0x3108, bits 4-5
  1220. * ++------------+
  1221. * + +-----------+
  1222. * +->| P_DIV | - reg 0x3035, bits 0-3
  1223. * +-----+-----+
  1224. * +------------> PCLK
  1225. *
  1226. * There seems to be also constraints:
  1227. * - the PLL pre-divider output rate should be in the 4-27MHz range
  1228. * - the PLL multiplier output rate should be in the 500-1000MHz range
  1229. * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
  1230. */
  1231. /*
  1232. * This is supposed to be ranging from 1 to 8, but the value is always
  1233. * set to 3 in the vendor kernels.
  1234. */
  1235. #define OV5640_PLL_PREDIV 3
  1236. #define OV5640_PLL_MULT_MIN 4
  1237. #define OV5640_PLL_MULT_MAX 252
  1238. /*
  1239. * This is supposed to be ranging from 1 to 16, but the value is
  1240. * always set to either 1 or 2 in the vendor kernels.
  1241. */
  1242. #define OV5640_SYSDIV_MIN 1
  1243. #define OV5640_SYSDIV_MAX 16
  1244. /*
  1245. * This is supposed to be ranging from 1 to 2, but the value is always
  1246. * set to 2 in the vendor kernels.
  1247. */
  1248. #define OV5640_PLL_ROOT_DIV 2
  1249. #define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 BIT(4)
  1250. /*
  1251. * We only supports 8-bit formats at the moment
  1252. */
  1253. #define OV5640_BIT_DIV 2
  1254. #define OV5640_PLL_CTRL0_MIPI_MODE_8BIT 0x08
  1255. /*
  1256. * This is supposed to be ranging from 1 to 8, but the value is always
  1257. * set to 2 in the vendor kernels.
  1258. */
  1259. #define OV5640_SCLK_ROOT_DIV 2
  1260. /*
  1261. * This is hardcoded so that the consistency is maintained between SCLK and
  1262. * SCLK 2x.
  1263. */
  1264. #define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
  1265. /*
  1266. * This is supposed to be ranging from 1 to 8, but the value is always
  1267. * set to 1 in the vendor kernels.
  1268. */
  1269. #define OV5640_PCLK_ROOT_DIV 1
  1270. #define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS 0x00
  1271. static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
  1272. u8 pll_prediv, u8 pll_mult,
  1273. u8 sysdiv)
  1274. {
  1275. unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
  1276. /* PLL1 output cannot exceed 1GHz. */
  1277. if (sysclk / 1000000 > 1000)
  1278. return 0;
  1279. return sysclk / sysdiv;
  1280. }
  1281. static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
  1282. unsigned long rate,
  1283. u8 *pll_prediv, u8 *pll_mult,
  1284. u8 *sysdiv)
  1285. {
  1286. unsigned long best = ~0;
  1287. u8 best_sysdiv = 1, best_mult = 1;
  1288. u8 _sysdiv, _pll_mult;
  1289. for (_sysdiv = OV5640_SYSDIV_MIN;
  1290. _sysdiv <= OV5640_SYSDIV_MAX;
  1291. _sysdiv++) {
  1292. for (_pll_mult = OV5640_PLL_MULT_MIN;
  1293. _pll_mult <= OV5640_PLL_MULT_MAX;
  1294. _pll_mult++) {
  1295. unsigned long _rate;
  1296. /*
  1297. * The PLL multiplier cannot be odd if above
  1298. * 127.
  1299. */
  1300. if (_pll_mult > 127 && (_pll_mult % 2))
  1301. continue;
  1302. _rate = ov5640_compute_sys_clk(sensor,
  1303. OV5640_PLL_PREDIV,
  1304. _pll_mult, _sysdiv);
  1305. /*
  1306. * We have reached the maximum allowed PLL1 output,
  1307. * increase sysdiv.
  1308. */
  1309. if (!_rate)
  1310. break;
  1311. /*
  1312. * Prefer rates above the expected clock rate than
  1313. * below, even if that means being less precise.
  1314. */
  1315. if (_rate < rate)
  1316. continue;
  1317. if (abs(rate - _rate) < abs(rate - best)) {
  1318. best = _rate;
  1319. best_sysdiv = _sysdiv;
  1320. best_mult = _pll_mult;
  1321. }
  1322. if (_rate == rate)
  1323. goto out;
  1324. }
  1325. }
  1326. out:
  1327. *sysdiv = best_sysdiv;
  1328. *pll_prediv = OV5640_PLL_PREDIV;
  1329. *pll_mult = best_mult;
  1330. return best;
  1331. }
  1332. /*
  1333. * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
  1334. * for the MIPI CSI-2 output.
  1335. */
  1336. static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor)
  1337. {
  1338. u8 bit_div, mipi_div, pclk_div, sclk_div, sclk2x_div, root_div;
  1339. u8 prediv, mult, sysdiv;
  1340. unsigned long link_freq;
  1341. unsigned long sysclk;
  1342. u8 pclk_period;
  1343. u32 sample_rate;
  1344. u32 num_lanes;
  1345. int ret;
  1346. /* Use the link freq computed at ov5640_update_pixel_rate() time. */
  1347. link_freq = sensor->current_link_freq;
  1348. /*
  1349. * - mipi_div - Additional divider for the MIPI lane clock.
  1350. *
  1351. * Higher link frequencies would make sysclk > 1GHz.
  1352. * Keep the sysclk low and do not divide in the MIPI domain.
  1353. */
  1354. if (link_freq > OV5640_LINK_RATE_MAX)
  1355. mipi_div = 1;
  1356. else
  1357. mipi_div = 2;
  1358. sysclk = link_freq * mipi_div;
  1359. ov5640_calc_sys_clk(sensor, sysclk, &prediv, &mult, &sysdiv);
  1360. /*
  1361. * Adjust PLL parameters to maintain the MIPI_SCLK-to-PCLK ratio.
  1362. *
  1363. * - root_div = 2 (fixed)
  1364. * - bit_div : MIPI 8-bit = 2; MIPI 10-bit = 2.5
  1365. * - pclk_div = 1 (fixed)
  1366. * - p_div = (2 lanes ? mipi_div : 2 * mipi_div)
  1367. *
  1368. * This results in the following MIPI_SCLK depending on the number
  1369. * of lanes:
  1370. *
  1371. * - 2 lanes: MIPI_SCLK = (4 or 5) * PCLK
  1372. * - 1 lanes: MIPI_SCLK = (8 or 10) * PCLK
  1373. */
  1374. root_div = OV5640_PLL_CTRL3_PLL_ROOT_DIV_2;
  1375. bit_div = OV5640_PLL_CTRL0_MIPI_MODE_8BIT;
  1376. pclk_div = ilog2(OV5640_PCLK_ROOT_DIV);
  1377. /*
  1378. * Scaler clock:
  1379. * - YUV: PCLK >= 2 * SCLK
  1380. * - RAW or JPEG: PCLK >= SCLK
  1381. * - sclk2x_div = sclk_div / 2
  1382. */
  1383. sclk_div = ilog2(OV5640_SCLK_ROOT_DIV);
  1384. sclk2x_div = ilog2(OV5640_SCLK2X_ROOT_DIV);
  1385. /*
  1386. * Set the pixel clock period expressed in ns with 1-bit decimal
  1387. * (0x01=0.5ns).
  1388. *
  1389. * The register is very briefly documented. In the OV5645 datasheet it
  1390. * is described as (2 * pclk period), and from testing it seems the
  1391. * actual definition is 2 * 8-bit sample period.
  1392. *
  1393. * 2 * sample_period = (mipi_clk * 2 * num_lanes / bpp) * (bpp / 8) / 2
  1394. */
  1395. num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
  1396. sample_rate = (link_freq * mipi_div * num_lanes * 2) / 16;
  1397. pclk_period = 2000000000UL / sample_rate;
  1398. /* Program the clock tree registers. */
  1399. ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, 0x0f, bit_div);
  1400. if (ret)
  1401. return ret;
  1402. ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0xff,
  1403. (sysdiv << 4) | mipi_div);
  1404. if (ret)
  1405. return ret;
  1406. ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
  1407. if (ret)
  1408. return ret;
  1409. ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, 0x1f,
  1410. root_div | prediv);
  1411. if (ret)
  1412. return ret;
  1413. ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
  1414. (pclk_div << 4) | (sclk2x_div << 2) | sclk_div);
  1415. if (ret)
  1416. return ret;
  1417. return ov5640_write_reg(sensor, OV5640_REG_PCLK_PERIOD, pclk_period);
  1418. }
  1419. static u32 ov5640_calc_pixel_rate(struct ov5640_dev *sensor)
  1420. {
  1421. const struct ov5640_mode_info *mode = sensor->current_mode;
  1422. const struct ov5640_timings *timings = &mode->dvp_timings;
  1423. u32 rate;
  1424. rate = timings->htot * (timings->crop.height + timings->vblank_def);
  1425. rate *= ov5640_framerates[sensor->current_fr];
  1426. return rate;
  1427. }
  1428. static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
  1429. unsigned long rate,
  1430. u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
  1431. u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
  1432. {
  1433. unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
  1434. OV5640_PCLK_ROOT_DIV;
  1435. _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
  1436. sysdiv);
  1437. *pll_rdiv = OV5640_PLL_ROOT_DIV;
  1438. *bit_div = OV5640_BIT_DIV;
  1439. *pclk_div = OV5640_PCLK_ROOT_DIV;
  1440. return _rate / *pll_rdiv / *bit_div / *pclk_div;
  1441. }
  1442. static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor)
  1443. {
  1444. u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
  1445. u32 rate;
  1446. int ret;
  1447. rate = ov5640_calc_pixel_rate(sensor);
  1448. rate *= ov5640_code_to_bpp(sensor, sensor->fmt.code);
  1449. rate /= sensor->ep.bus.parallel.bus_width;
  1450. ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
  1451. &bit_div, &pclk_div);
  1452. if (bit_div == 2)
  1453. bit_div = 8;
  1454. ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
  1455. 0x0f, bit_div);
  1456. if (ret)
  1457. return ret;
  1458. /*
  1459. * We need to set sysdiv according to the clock, and to clear
  1460. * the MIPI divider.
  1461. */
  1462. ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
  1463. 0xff, sysdiv << 4);
  1464. if (ret)
  1465. return ret;
  1466. ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
  1467. 0xff, mult);
  1468. if (ret)
  1469. return ret;
  1470. ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
  1471. 0x1f, prediv | ((pll_rdiv - 1) << 4));
  1472. if (ret)
  1473. return ret;
  1474. return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
  1475. (ilog2(pclk_div) << 4));
  1476. }
  1477. /* set JPEG framing sizes */
  1478. static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor,
  1479. const struct ov5640_mode_info *mode)
  1480. {
  1481. int ret;
  1482. /*
  1483. * compression mode 3 timing
  1484. *
  1485. * Data is transmitted with programmable width (VFIFO_HSIZE).
  1486. * No padding done. Last line may have less data. Varying
  1487. * number of lines per frame, depending on amount of data.
  1488. */
  1489. ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3);
  1490. if (ret < 0)
  1491. return ret;
  1492. ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->width);
  1493. if (ret < 0)
  1494. return ret;
  1495. return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->height);
  1496. }
  1497. /* download ov5640 settings to sensor through i2c */
  1498. static int ov5640_set_timings(struct ov5640_dev *sensor,
  1499. const struct ov5640_mode_info *mode)
  1500. {
  1501. const struct ov5640_timings *timings;
  1502. const struct v4l2_rect *analog_crop;
  1503. const struct v4l2_rect *crop;
  1504. int ret;
  1505. if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) {
  1506. ret = ov5640_set_jpeg_timings(sensor, mode);
  1507. if (ret < 0)
  1508. return ret;
  1509. }
  1510. timings = ov5640_timings(sensor, mode);
  1511. analog_crop = &timings->analog_crop;
  1512. crop = &timings->crop;
  1513. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HS,
  1514. analog_crop->left);
  1515. if (ret < 0)
  1516. return ret;
  1517. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VS,
  1518. analog_crop->top);
  1519. if (ret < 0)
  1520. return ret;
  1521. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HW,
  1522. analog_crop->left + analog_crop->width - 1);
  1523. if (ret < 0)
  1524. return ret;
  1525. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VH,
  1526. analog_crop->top + analog_crop->height - 1);
  1527. if (ret < 0)
  1528. return ret;
  1529. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HOFFS, crop->left);
  1530. if (ret < 0)
  1531. return ret;
  1532. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VOFFS, crop->top);
  1533. if (ret < 0)
  1534. return ret;
  1535. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->width);
  1536. if (ret < 0)
  1537. return ret;
  1538. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->height);
  1539. if (ret < 0)
  1540. return ret;
  1541. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, timings->htot);
  1542. if (ret < 0)
  1543. return ret;
  1544. ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
  1545. mode->height + timings->vblank_def);
  1546. if (ret < 0)
  1547. return ret;
  1548. return 0;
  1549. }
  1550. static void ov5640_load_regs(struct ov5640_dev *sensor,
  1551. const struct reg_value *regs, unsigned int regnum)
  1552. {
  1553. unsigned int i;
  1554. u32 delay_ms;
  1555. u16 reg_addr;
  1556. u8 mask, val;
  1557. int ret = 0;
  1558. for (i = 0; i < regnum; ++i, ++regs) {
  1559. delay_ms = regs->delay_ms;
  1560. reg_addr = regs->reg_addr;
  1561. val = regs->val;
  1562. mask = regs->mask;
  1563. /* remain in power down mode for DVP */
  1564. if (regs->reg_addr == OV5640_REG_SYS_CTRL0 &&
  1565. val == OV5640_REG_SYS_CTRL0_SW_PWUP &&
  1566. !ov5640_is_csi2(sensor))
  1567. continue;
  1568. if (mask)
  1569. ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
  1570. else
  1571. ret = ov5640_write_reg(sensor, reg_addr, val);
  1572. if (ret)
  1573. break;
  1574. if (delay_ms)
  1575. usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
  1576. }
  1577. }
  1578. static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
  1579. {
  1580. return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
  1581. BIT(0), on ? 0 : BIT(0));
  1582. }
  1583. /* read exposure, in number of line periods */
  1584. static int ov5640_get_exposure(struct ov5640_dev *sensor)
  1585. {
  1586. int exp, ret;
  1587. u8 temp;
  1588. ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
  1589. if (ret)
  1590. return ret;
  1591. exp = ((int)temp & 0x0f) << 16;
  1592. ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
  1593. if (ret)
  1594. return ret;
  1595. exp |= ((int)temp << 8);
  1596. ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
  1597. if (ret)
  1598. return ret;
  1599. exp |= (int)temp;
  1600. return exp >> 4;
  1601. }
  1602. /* write exposure, given number of line periods */
  1603. static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
  1604. {
  1605. int ret;
  1606. exposure <<= 4;
  1607. ret = ov5640_write_reg(sensor,
  1608. OV5640_REG_AEC_PK_EXPOSURE_LO,
  1609. exposure & 0xff);
  1610. if (ret)
  1611. return ret;
  1612. ret = ov5640_write_reg(sensor,
  1613. OV5640_REG_AEC_PK_EXPOSURE_MED,
  1614. (exposure >> 8) & 0xff);
  1615. if (ret)
  1616. return ret;
  1617. return ov5640_write_reg(sensor,
  1618. OV5640_REG_AEC_PK_EXPOSURE_HI,
  1619. (exposure >> 16) & 0x0f);
  1620. }
  1621. static int ov5640_get_gain(struct ov5640_dev *sensor)
  1622. {
  1623. u16 gain;
  1624. int ret;
  1625. ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
  1626. if (ret)
  1627. return ret;
  1628. return gain & 0x3ff;
  1629. }
  1630. static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
  1631. {
  1632. return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
  1633. (u16)gain & 0x3ff);
  1634. }
  1635. static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
  1636. {
  1637. return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
  1638. BIT(1), on ? 0 : BIT(1));
  1639. }
  1640. static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
  1641. {
  1642. return ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0, on ?
  1643. OV5640_REG_SYS_CTRL0_SW_PWUP :
  1644. OV5640_REG_SYS_CTRL0_SW_PWDN);
  1645. }
  1646. static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
  1647. {
  1648. int ret;
  1649. /*
  1650. * Enable/disable the MIPI interface
  1651. *
  1652. * 0x300e = on ? 0x45 : 0x40
  1653. *
  1654. * FIXME: the sensor manual (version 2.03) reports
  1655. * [7:5] = 000 : 1 data lane mode
  1656. * [7:5] = 001 : 2 data lanes mode
  1657. * But this settings do not work, while the following ones
  1658. * have been validated for 2 data lanes mode.
  1659. *
  1660. * [7:5] = 010 : 2 data lanes mode
  1661. * [4] = 0 : Power up MIPI HS Tx
  1662. * [3] = 0 : Power up MIPI LS Rx
  1663. * [2] = 1/0 : MIPI interface enable/disable
  1664. * [1:0] = 01/00: FIXME: 'debug'
  1665. */
  1666. ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
  1667. on ? 0x45 : 0x40);
  1668. if (ret)
  1669. return ret;
  1670. return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
  1671. on ? 0x00 : 0x0f);
  1672. }
  1673. static int ov5640_get_sysclk(struct ov5640_dev *sensor)
  1674. {
  1675. /* calculate sysclk */
  1676. u32 xvclk = sensor->xclk_freq / 10000;
  1677. u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
  1678. u32 sclk_rdiv_map[] = {1, 2, 4, 8};
  1679. u32 bit_div2x = 1, sclk_rdiv, sysclk;
  1680. u8 temp1, temp2;
  1681. int ret;
  1682. ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
  1683. if (ret)
  1684. return ret;
  1685. temp2 = temp1 & 0x0f;
  1686. if (temp2 == 8 || temp2 == 10)
  1687. bit_div2x = temp2 / 2;
  1688. ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
  1689. if (ret)
  1690. return ret;
  1691. sysdiv = temp1 >> 4;
  1692. if (sysdiv == 0)
  1693. sysdiv = 16;
  1694. ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
  1695. if (ret)
  1696. return ret;
  1697. multiplier = temp1;
  1698. ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
  1699. if (ret)
  1700. return ret;
  1701. prediv = temp1 & 0x0f;
  1702. pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
  1703. ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
  1704. if (ret)
  1705. return ret;
  1706. temp2 = temp1 & 0x03;
  1707. sclk_rdiv = sclk_rdiv_map[temp2];
  1708. if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
  1709. return -EINVAL;
  1710. VCO = xvclk * multiplier / prediv;
  1711. sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
  1712. return sysclk;
  1713. }
  1714. static int ov5640_set_night_mode(struct ov5640_dev *sensor)
  1715. {
  1716. /* read HTS from register settings */
  1717. u8 mode;
  1718. int ret;
  1719. ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
  1720. if (ret)
  1721. return ret;
  1722. mode &= 0xfb;
  1723. return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
  1724. }
  1725. static int ov5640_get_hts(struct ov5640_dev *sensor)
  1726. {
  1727. /* read HTS from register settings */
  1728. u16 hts;
  1729. int ret;
  1730. ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
  1731. if (ret)
  1732. return ret;
  1733. return hts;
  1734. }
  1735. static int ov5640_get_vts(struct ov5640_dev *sensor)
  1736. {
  1737. u16 vts;
  1738. int ret;
  1739. ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
  1740. if (ret)
  1741. return ret;
  1742. return vts;
  1743. }
  1744. static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
  1745. {
  1746. return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
  1747. }
  1748. static int ov5640_get_light_freq(struct ov5640_dev *sensor)
  1749. {
  1750. /* get banding filter value */
  1751. int ret, light_freq = 0;
  1752. u8 temp, temp1;
  1753. ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
  1754. if (ret)
  1755. return ret;
  1756. if (temp & 0x80) {
  1757. /* manual */
  1758. ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
  1759. &temp1);
  1760. if (ret)
  1761. return ret;
  1762. if (temp1 & 0x04) {
  1763. /* 50Hz */
  1764. light_freq = 50;
  1765. } else {
  1766. /* 60Hz */
  1767. light_freq = 60;
  1768. }
  1769. } else {
  1770. /* auto */
  1771. ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
  1772. &temp1);
  1773. if (ret)
  1774. return ret;
  1775. if (temp1 & 0x01) {
  1776. /* 50Hz */
  1777. light_freq = 50;
  1778. } else {
  1779. /* 60Hz */
  1780. }
  1781. }
  1782. return light_freq;
  1783. }
  1784. static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
  1785. {
  1786. u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
  1787. int ret;
  1788. /* read preview PCLK */
  1789. ret = ov5640_get_sysclk(sensor);
  1790. if (ret < 0)
  1791. return ret;
  1792. if (ret == 0)
  1793. return -EINVAL;
  1794. sensor->prev_sysclk = ret;
  1795. /* read preview HTS */
  1796. ret = ov5640_get_hts(sensor);
  1797. if (ret < 0)
  1798. return ret;
  1799. if (ret == 0)
  1800. return -EINVAL;
  1801. sensor->prev_hts = ret;
  1802. /* read preview VTS */
  1803. ret = ov5640_get_vts(sensor);
  1804. if (ret < 0)
  1805. return ret;
  1806. prev_vts = ret;
  1807. /* calculate banding filter */
  1808. /* 60Hz */
  1809. band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
  1810. ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
  1811. if (ret)
  1812. return ret;
  1813. if (!band_step60)
  1814. return -EINVAL;
  1815. max_band60 = (int)((prev_vts - 4) / band_step60);
  1816. ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
  1817. if (ret)
  1818. return ret;
  1819. /* 50Hz */
  1820. band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
  1821. ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
  1822. if (ret)
  1823. return ret;
  1824. if (!band_step50)
  1825. return -EINVAL;
  1826. max_band50 = (int)((prev_vts - 4) / band_step50);
  1827. return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
  1828. }
  1829. static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
  1830. {
  1831. /* stable in high */
  1832. u32 fast_high, fast_low;
  1833. int ret;
  1834. sensor->ae_low = target * 23 / 25; /* 0.92 */
  1835. sensor->ae_high = target * 27 / 25; /* 1.08 */
  1836. fast_high = sensor->ae_high << 1;
  1837. if (fast_high > 255)
  1838. fast_high = 255;
  1839. fast_low = sensor->ae_low >> 1;
  1840. ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
  1841. if (ret)
  1842. return ret;
  1843. ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
  1844. if (ret)
  1845. return ret;
  1846. ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
  1847. if (ret)
  1848. return ret;
  1849. ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
  1850. if (ret)
  1851. return ret;
  1852. ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
  1853. if (ret)
  1854. return ret;
  1855. return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
  1856. }
  1857. static int ov5640_get_binning(struct ov5640_dev *sensor)
  1858. {
  1859. u8 temp;
  1860. int ret;
  1861. ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
  1862. if (ret)
  1863. return ret;
  1864. return temp & BIT(0);
  1865. }
  1866. static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
  1867. {
  1868. int ret;
  1869. /*
  1870. * TIMING TC REG21:
  1871. * - [0]: Horizontal binning enable
  1872. */
  1873. ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
  1874. BIT(0), enable ? BIT(0) : 0);
  1875. if (ret)
  1876. return ret;
  1877. /*
  1878. * TIMING TC REG20:
  1879. * - [0]: Undocumented, but hardcoded init sequences
  1880. * are always setting REG21/REG20 bit 0 to same value...
  1881. */
  1882. return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
  1883. BIT(0), enable ? BIT(0) : 0);
  1884. }
  1885. static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
  1886. {
  1887. struct i2c_client *client = sensor->i2c_client;
  1888. u8 temp, channel = virtual_channel;
  1889. int ret;
  1890. if (channel > 3) {
  1891. dev_err(&client->dev,
  1892. "%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
  1893. __func__, channel);
  1894. return -EINVAL;
  1895. }
  1896. ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
  1897. if (ret)
  1898. return ret;
  1899. temp &= ~(3 << 6);
  1900. temp |= (channel << 6);
  1901. return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
  1902. }
  1903. static const struct ov5640_mode_info *
  1904. ov5640_find_mode(struct ov5640_dev *sensor, int width, int height, bool nearest)
  1905. {
  1906. const struct ov5640_mode_info *mode;
  1907. mode = v4l2_find_nearest_size(ov5640_mode_data,
  1908. ARRAY_SIZE(ov5640_mode_data),
  1909. width, height, width, height);
  1910. if (!mode ||
  1911. (!nearest &&
  1912. (mode->width != width || mode->height != height)))
  1913. return NULL;
  1914. return mode;
  1915. }
  1916. /*
  1917. * sensor changes between scaling and subsampling, go through
  1918. * exposure calculation
  1919. */
  1920. static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
  1921. const struct ov5640_mode_info *mode)
  1922. {
  1923. u32 prev_shutter, prev_gain16;
  1924. u32 cap_shutter, cap_gain16;
  1925. u32 cap_sysclk, cap_hts, cap_vts;
  1926. u32 light_freq, cap_bandfilt, cap_maxband;
  1927. u32 cap_gain16_shutter;
  1928. u8 average;
  1929. int ret;
  1930. if (!mode->reg_data)
  1931. return -EINVAL;
  1932. /* read preview shutter */
  1933. ret = ov5640_get_exposure(sensor);
  1934. if (ret < 0)
  1935. return ret;
  1936. prev_shutter = ret;
  1937. ret = ov5640_get_binning(sensor);
  1938. if (ret < 0)
  1939. return ret;
  1940. if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
  1941. mode->id != OV5640_MODE_1080P_1920_1080)
  1942. prev_shutter *= 2;
  1943. /* read preview gain */
  1944. ret = ov5640_get_gain(sensor);
  1945. if (ret < 0)
  1946. return ret;
  1947. prev_gain16 = ret;
  1948. /* get average */
  1949. ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
  1950. if (ret)
  1951. return ret;
  1952. /* turn off night mode for capture */
  1953. ret = ov5640_set_night_mode(sensor);
  1954. if (ret < 0)
  1955. return ret;
  1956. /* Write capture setting */
  1957. ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size);
  1958. ret = ov5640_set_timings(sensor, mode);
  1959. if (ret < 0)
  1960. return ret;
  1961. /* read capture VTS */
  1962. ret = ov5640_get_vts(sensor);
  1963. if (ret < 0)
  1964. return ret;
  1965. cap_vts = ret;
  1966. ret = ov5640_get_hts(sensor);
  1967. if (ret < 0)
  1968. return ret;
  1969. if (ret == 0)
  1970. return -EINVAL;
  1971. cap_hts = ret;
  1972. ret = ov5640_get_sysclk(sensor);
  1973. if (ret < 0)
  1974. return ret;
  1975. if (ret == 0)
  1976. return -EINVAL;
  1977. cap_sysclk = ret;
  1978. /* calculate capture banding filter */
  1979. ret = ov5640_get_light_freq(sensor);
  1980. if (ret < 0)
  1981. return ret;
  1982. light_freq = ret;
  1983. if (light_freq == 60) {
  1984. /* 60Hz */
  1985. cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
  1986. } else {
  1987. /* 50Hz */
  1988. cap_bandfilt = cap_sysclk * 100 / cap_hts;
  1989. }
  1990. if (!sensor->prev_sysclk) {
  1991. ret = ov5640_get_sysclk(sensor);
  1992. if (ret < 0)
  1993. return ret;
  1994. if (ret == 0)
  1995. return -EINVAL;
  1996. sensor->prev_sysclk = ret;
  1997. }
  1998. if (!cap_bandfilt)
  1999. return -EINVAL;
  2000. cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
  2001. /* calculate capture shutter/gain16 */
  2002. if (average > sensor->ae_low && average < sensor->ae_high) {
  2003. /* in stable range */
  2004. cap_gain16_shutter =
  2005. prev_gain16 * prev_shutter *
  2006. cap_sysclk / sensor->prev_sysclk *
  2007. sensor->prev_hts / cap_hts *
  2008. sensor->ae_target / average;
  2009. } else {
  2010. cap_gain16_shutter =
  2011. prev_gain16 * prev_shutter *
  2012. cap_sysclk / sensor->prev_sysclk *
  2013. sensor->prev_hts / cap_hts;
  2014. }
  2015. /* gain to shutter */
  2016. if (cap_gain16_shutter < (cap_bandfilt * 16)) {
  2017. /* shutter < 1/100 */
  2018. cap_shutter = cap_gain16_shutter / 16;
  2019. if (cap_shutter < 1)
  2020. cap_shutter = 1;
  2021. cap_gain16 = cap_gain16_shutter / cap_shutter;
  2022. if (cap_gain16 < 16)
  2023. cap_gain16 = 16;
  2024. } else {
  2025. if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
  2026. /* exposure reach max */
  2027. cap_shutter = cap_bandfilt * cap_maxband;
  2028. if (!cap_shutter)
  2029. return -EINVAL;
  2030. cap_gain16 = cap_gain16_shutter / cap_shutter;
  2031. } else {
  2032. /* 1/100 < (cap_shutter = n/100) =< max */
  2033. cap_shutter =
  2034. ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
  2035. * cap_bandfilt;
  2036. if (!cap_shutter)
  2037. return -EINVAL;
  2038. cap_gain16 = cap_gain16_shutter / cap_shutter;
  2039. }
  2040. }
  2041. /* set capture gain */
  2042. ret = ov5640_set_gain(sensor, cap_gain16);
  2043. if (ret)
  2044. return ret;
  2045. /* write capture shutter */
  2046. if (cap_shutter > (cap_vts - 4)) {
  2047. cap_vts = cap_shutter + 4;
  2048. ret = ov5640_set_vts(sensor, cap_vts);
  2049. if (ret < 0)
  2050. return ret;
  2051. }
  2052. /* set exposure */
  2053. return ov5640_set_exposure(sensor, cap_shutter);
  2054. }
  2055. /*
  2056. * if sensor changes inside scaling or subsampling
  2057. * change mode directly
  2058. */
  2059. static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
  2060. const struct ov5640_mode_info *mode)
  2061. {
  2062. if (!mode->reg_data)
  2063. return -EINVAL;
  2064. /* Write capture setting */
  2065. ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size);
  2066. return ov5640_set_timings(sensor, mode);
  2067. }
  2068. static int ov5640_set_mode(struct ov5640_dev *sensor)
  2069. {
  2070. const struct ov5640_mode_info *mode = sensor->current_mode;
  2071. const struct ov5640_mode_info *orig_mode = sensor->last_mode;
  2072. enum ov5640_downsize_mode dn_mode, orig_dn_mode;
  2073. bool auto_gain = sensor->ctrls.auto_gain->val == 1;
  2074. bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
  2075. int ret;
  2076. dn_mode = mode->dn_mode;
  2077. orig_dn_mode = orig_mode->dn_mode;
  2078. /* auto gain and exposure must be turned off when changing modes */
  2079. if (auto_gain) {
  2080. ret = ov5640_set_autogain(sensor, false);
  2081. if (ret)
  2082. return ret;
  2083. }
  2084. if (auto_exp) {
  2085. ret = ov5640_set_autoexposure(sensor, false);
  2086. if (ret)
  2087. goto restore_auto_gain;
  2088. }
  2089. if (ov5640_is_csi2(sensor))
  2090. ret = ov5640_set_mipi_pclk(sensor);
  2091. else
  2092. ret = ov5640_set_dvp_pclk(sensor);
  2093. if (ret < 0)
  2094. return 0;
  2095. if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
  2096. (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
  2097. /*
  2098. * change between subsampling and scaling
  2099. * go through exposure calculation
  2100. */
  2101. ret = ov5640_set_mode_exposure_calc(sensor, mode);
  2102. } else {
  2103. /*
  2104. * change inside subsampling or scaling
  2105. * download firmware directly
  2106. */
  2107. ret = ov5640_set_mode_direct(sensor, mode);
  2108. }
  2109. if (ret < 0)
  2110. goto restore_auto_exp_gain;
  2111. /* restore auto gain and exposure */
  2112. if (auto_gain)
  2113. ov5640_set_autogain(sensor, true);
  2114. if (auto_exp)
  2115. ov5640_set_autoexposure(sensor, true);
  2116. ret = ov5640_set_binning(sensor, dn_mode != SCALING);
  2117. if (ret < 0)
  2118. return ret;
  2119. ret = ov5640_set_ae_target(sensor, sensor->ae_target);
  2120. if (ret < 0)
  2121. return ret;
  2122. ret = ov5640_get_light_freq(sensor);
  2123. if (ret < 0)
  2124. return ret;
  2125. ret = ov5640_set_bandingfilter(sensor);
  2126. if (ret < 0)
  2127. return ret;
  2128. ret = ov5640_set_virtual_channel(sensor);
  2129. if (ret < 0)
  2130. return ret;
  2131. sensor->pending_mode_change = false;
  2132. sensor->last_mode = mode;
  2133. return 0;
  2134. restore_auto_exp_gain:
  2135. if (auto_exp)
  2136. ov5640_set_autoexposure(sensor, true);
  2137. restore_auto_gain:
  2138. if (auto_gain)
  2139. ov5640_set_autogain(sensor, true);
  2140. return ret;
  2141. }
  2142. static int ov5640_set_framefmt(struct ov5640_dev *sensor,
  2143. struct v4l2_mbus_framefmt *format);
  2144. /* restore the last set video mode after chip power-on */
  2145. static int ov5640_restore_mode(struct ov5640_dev *sensor)
  2146. {
  2147. int ret;
  2148. /* first load the initial register values */
  2149. ov5640_load_regs(sensor, ov5640_init_setting,
  2150. ARRAY_SIZE(ov5640_init_setting));
  2151. ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
  2152. (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) |
  2153. ilog2(OV5640_SCLK_ROOT_DIV));
  2154. if (ret)
  2155. return ret;
  2156. /* now restore the last capture mode */
  2157. ret = ov5640_set_mode(sensor);
  2158. if (ret < 0)
  2159. return ret;
  2160. return ov5640_set_framefmt(sensor, &sensor->fmt);
  2161. }
  2162. static void ov5640_power(struct ov5640_dev *sensor, bool enable)
  2163. {
  2164. gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
  2165. }
  2166. /*
  2167. * From section 2.7 power up sequence:
  2168. * t0 + t1 + t2 >= 5ms Delay from DOVDD stable to PWDN pull down
  2169. * t3 >= 1ms Delay from PWDN pull down to RESETB pull up
  2170. * t4 >= 20ms Delay from RESETB pull up to SCCB (i2c) stable
  2171. *
  2172. * Some modules don't expose RESETB/PWDN pins directly, instead providing a
  2173. * "PWUP" GPIO which is wired through appropriate delays and inverters to the
  2174. * pins.
  2175. *
  2176. * In such cases, this gpio should be mapped to pwdn_gpio in the driver, and we
  2177. * should still toggle the pwdn_gpio below with the appropriate delays, while
  2178. * the calls to reset_gpio will be ignored.
  2179. */
  2180. static void ov5640_powerup_sequence(struct ov5640_dev *sensor)
  2181. {
  2182. if (sensor->pwdn_gpio) {
  2183. gpiod_set_value_cansleep(sensor->reset_gpio, 1);
  2184. /* camera power cycle */
  2185. ov5640_power(sensor, false);
  2186. usleep_range(5000, 10000); /* t2 */
  2187. ov5640_power(sensor, true);
  2188. usleep_range(1000, 2000); /* t3 */
  2189. gpiod_set_value_cansleep(sensor->reset_gpio, 0);
  2190. } else {
  2191. /* software reset */
  2192. ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0,
  2193. OV5640_REG_SYS_CTRL0_SW_RST);
  2194. }
  2195. usleep_range(20000, 25000); /* t4 */
  2196. /*
  2197. * software standby: allows registers programming;
  2198. * exit at restore_mode() for CSI, s_stream(1) for DVP
  2199. */
  2200. ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0,
  2201. OV5640_REG_SYS_CTRL0_SW_PWDN);
  2202. }
  2203. static int ov5640_set_power_on(struct ov5640_dev *sensor)
  2204. {
  2205. struct i2c_client *client = sensor->i2c_client;
  2206. int ret;
  2207. ret = clk_prepare_enable(sensor->xclk);
  2208. if (ret) {
  2209. dev_err(&client->dev, "%s: failed to enable clock\n",
  2210. __func__);
  2211. return ret;
  2212. }
  2213. ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
  2214. sensor->supplies);
  2215. if (ret) {
  2216. dev_err(&client->dev, "%s: failed to enable regulators\n",
  2217. __func__);
  2218. goto xclk_off;
  2219. }
  2220. ov5640_powerup_sequence(sensor);
  2221. ret = ov5640_init_slave_id(sensor);
  2222. if (ret)
  2223. goto power_off;
  2224. return 0;
  2225. power_off:
  2226. ov5640_power(sensor, false);
  2227. regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
  2228. xclk_off:
  2229. clk_disable_unprepare(sensor->xclk);
  2230. return ret;
  2231. }
  2232. static void ov5640_set_power_off(struct ov5640_dev *sensor)
  2233. {
  2234. ov5640_power(sensor, false);
  2235. regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
  2236. clk_disable_unprepare(sensor->xclk);
  2237. }
  2238. static int ov5640_set_power_mipi(struct ov5640_dev *sensor, bool on)
  2239. {
  2240. int ret;
  2241. if (!on) {
  2242. /* Reset MIPI bus settings to their default values. */
  2243. ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
  2244. ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x04);
  2245. ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x00);
  2246. return 0;
  2247. }
  2248. /*
  2249. * Power up MIPI HS Tx and LS Rx; 2 data lanes mode
  2250. *
  2251. * 0x300e = 0x40
  2252. * [7:5] = 010 : 2 data lanes mode (see FIXME note in
  2253. * "ov5640_set_stream_mipi()")
  2254. * [4] = 0 : Power up MIPI HS Tx
  2255. * [3] = 0 : Power up MIPI LS Rx
  2256. * [2] = 1 : MIPI interface enabled
  2257. */
  2258. ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x44);
  2259. if (ret)
  2260. return ret;
  2261. /*
  2262. * Gate clock and set LP11 in 'no packets mode' (idle)
  2263. *
  2264. * 0x4800 = 0x24
  2265. * [5] = 1 : Gate clock when 'no packets'
  2266. * [2] = 1 : MIPI bus in LP11 when 'no packets'
  2267. */
  2268. ret = ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x24);
  2269. if (ret)
  2270. return ret;
  2271. /*
  2272. * Set data lanes and clock in LP11 when 'sleeping'
  2273. *
  2274. * 0x3019 = 0x70
  2275. * [6] = 1 : MIPI data lane 2 in LP11 when 'sleeping'
  2276. * [5] = 1 : MIPI data lane 1 in LP11 when 'sleeping'
  2277. * [4] = 1 : MIPI clock lane in LP11 when 'sleeping'
  2278. */
  2279. ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x70);
  2280. if (ret)
  2281. return ret;
  2282. /* Give lanes some time to coax into LP11 state. */
  2283. usleep_range(500, 1000);
  2284. return 0;
  2285. }
  2286. static int ov5640_set_power_dvp(struct ov5640_dev *sensor, bool on)
  2287. {
  2288. unsigned int flags = sensor->ep.bus.parallel.flags;
  2289. bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656;
  2290. u8 polarities = 0;
  2291. int ret;
  2292. if (!on) {
  2293. /* Reset settings to their default values. */
  2294. ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 0x00);
  2295. ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58);
  2296. ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, 0x20);
  2297. ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 0x00);
  2298. ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0x00);
  2299. return 0;
  2300. }
  2301. /*
  2302. * Note about parallel port configuration.
  2303. *
  2304. * When configured in parallel mode, the OV5640 will
  2305. * output 10 bits data on DVP data lines [9:0].
  2306. * If only 8 bits data are wanted, the 8 bits data lines
  2307. * of the camera interface must be physically connected
  2308. * on the DVP data lines [9:2].
  2309. *
  2310. * Control lines polarity can be configured through
  2311. * devicetree endpoint control lines properties.
  2312. * If no endpoint control lines properties are set,
  2313. * polarity will be as below:
  2314. * - VSYNC: active high
  2315. * - HREF: active low
  2316. * - PCLK: active low
  2317. *
  2318. * VSYNC & HREF are not configured if BT656 bus mode is selected
  2319. */
  2320. /*
  2321. * BT656 embedded synchronization configuration
  2322. *
  2323. * CCIR656 CTRL00
  2324. * - [7]: SYNC code selection (0: auto generate sync code,
  2325. * 1: sync code from regs 0x4732-0x4735)
  2326. * - [6]: f value in CCIR656 SYNC code when fixed f value
  2327. * - [5]: Fixed f value
  2328. * - [4:3]: Blank toggle data options (00: data=1'h040/1'h200,
  2329. * 01: data from regs 0x4736-0x4738, 10: always keep 0)
  2330. * - [1]: Clip data disable
  2331. * - [0]: CCIR656 mode enable
  2332. *
  2333. * Default CCIR656 SAV/EAV mode with default codes
  2334. * SAV=0xff000080 & EAV=0xff00009d is enabled here with settings:
  2335. * - CCIR656 mode enable
  2336. * - auto generation of sync codes
  2337. * - blank toggle data 1'h040/1'h200
  2338. * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe)
  2339. */
  2340. ret = ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00,
  2341. bt656 ? 0x01 : 0x00);
  2342. if (ret)
  2343. return ret;
  2344. /*
  2345. * configure parallel port control lines polarity
  2346. *
  2347. * POLARITY CTRL0
  2348. * - [5]: PCLK polarity (0: active low, 1: active high)
  2349. * - [1]: HREF polarity (0: active low, 1: active high)
  2350. * - [0]: VSYNC polarity (mismatch here between
  2351. * datasheet and hardware, 0 is active high
  2352. * and 1 is active low...)
  2353. */
  2354. if (!bt656) {
  2355. if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  2356. polarities |= BIT(1);
  2357. if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  2358. polarities |= BIT(0);
  2359. }
  2360. if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  2361. polarities |= BIT(5);
  2362. ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities);
  2363. if (ret)
  2364. return ret;
  2365. /*
  2366. * powerdown MIPI TX/RX PHY & enable DVP
  2367. *
  2368. * MIPI CONTROL 00
  2369. * [4] = 1 : Power down MIPI HS Tx
  2370. * [3] = 1 : Power down MIPI LS Rx
  2371. * [2] = 0 : DVP enable (MIPI disable)
  2372. */
  2373. ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x18);
  2374. if (ret)
  2375. return ret;
  2376. /*
  2377. * enable VSYNC/HREF/PCLK DVP control lines
  2378. * & D[9:6] DVP data lines
  2379. *
  2380. * PAD OUTPUT ENABLE 01
  2381. * - 6: VSYNC output enable
  2382. * - 5: HREF output enable
  2383. * - 4: PCLK output enable
  2384. * - [3:0]: D[9:6] output enable
  2385. */
  2386. ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01,
  2387. bt656 ? 0x1f : 0x7f);
  2388. if (ret)
  2389. return ret;
  2390. /*
  2391. * enable D[5:0] DVP data lines
  2392. *
  2393. * PAD OUTPUT ENABLE 02
  2394. * - [7:2]: D[5:0] output enable
  2395. */
  2396. return ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0xfc);
  2397. }
  2398. static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
  2399. {
  2400. int ret = 0;
  2401. if (on) {
  2402. ret = ov5640_set_power_on(sensor);
  2403. if (ret)
  2404. return ret;
  2405. ret = ov5640_restore_mode(sensor);
  2406. if (ret)
  2407. goto power_off;
  2408. }
  2409. if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
  2410. ret = ov5640_set_power_mipi(sensor, on);
  2411. else
  2412. ret = ov5640_set_power_dvp(sensor, on);
  2413. if (ret)
  2414. goto power_off;
  2415. if (!on)
  2416. ov5640_set_power_off(sensor);
  2417. return 0;
  2418. power_off:
  2419. ov5640_set_power_off(sensor);
  2420. return ret;
  2421. }
  2422. static int ov5640_sensor_suspend(struct device *dev)
  2423. {
  2424. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  2425. struct ov5640_dev *ov5640 = to_ov5640_dev(sd);
  2426. return ov5640_set_power(ov5640, false);
  2427. }
  2428. static int ov5640_sensor_resume(struct device *dev)
  2429. {
  2430. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  2431. struct ov5640_dev *ov5640 = to_ov5640_dev(sd);
  2432. return ov5640_set_power(ov5640, true);
  2433. }
  2434. /* --------------- Subdev Operations --------------- */
  2435. static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
  2436. struct v4l2_fract *fi,
  2437. u32 width, u32 height)
  2438. {
  2439. const struct ov5640_mode_info *mode;
  2440. enum ov5640_frame_rate rate = OV5640_15_FPS;
  2441. int minfps, maxfps, best_fps, fps;
  2442. int i;
  2443. minfps = ov5640_framerates[OV5640_15_FPS];
  2444. maxfps = ov5640_framerates[OV5640_60_FPS];
  2445. if (fi->numerator == 0) {
  2446. fi->denominator = maxfps;
  2447. fi->numerator = 1;
  2448. rate = OV5640_60_FPS;
  2449. goto find_mode;
  2450. }
  2451. fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
  2452. minfps, maxfps);
  2453. best_fps = minfps;
  2454. for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) {
  2455. int curr_fps = ov5640_framerates[i];
  2456. if (abs(curr_fps - fps) < abs(best_fps - fps)) {
  2457. best_fps = curr_fps;
  2458. rate = i;
  2459. }
  2460. }
  2461. fi->numerator = 1;
  2462. fi->denominator = best_fps;
  2463. find_mode:
  2464. mode = ov5640_find_mode(sensor, width, height, false);
  2465. return mode ? rate : -EINVAL;
  2466. }
  2467. static int ov5640_get_fmt(struct v4l2_subdev *sd,
  2468. struct v4l2_subdev_state *sd_state,
  2469. struct v4l2_subdev_format *format)
  2470. {
  2471. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  2472. struct v4l2_mbus_framefmt *fmt;
  2473. if (format->pad != 0)
  2474. return -EINVAL;
  2475. mutex_lock(&sensor->lock);
  2476. if (format->which == V4L2_SUBDEV_FORMAT_TRY)
  2477. fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
  2478. format->pad);
  2479. else
  2480. fmt = &sensor->fmt;
  2481. format->format = *fmt;
  2482. mutex_unlock(&sensor->lock);
  2483. return 0;
  2484. }
  2485. static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
  2486. struct v4l2_mbus_framefmt *fmt,
  2487. enum ov5640_frame_rate fr,
  2488. const struct ov5640_mode_info **new_mode)
  2489. {
  2490. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  2491. const struct ov5640_mode_info *mode;
  2492. const struct ov5640_pixfmt *pixfmt;
  2493. unsigned int bpp;
  2494. mode = ov5640_find_mode(sensor, fmt->width, fmt->height, true);
  2495. if (!mode)
  2496. return -EINVAL;
  2497. pixfmt = ov5640_code_to_pixfmt(sensor, fmt->code);
  2498. bpp = pixfmt->bpp;
  2499. /*
  2500. * Adjust mode according to bpp:
  2501. * - 8bpp modes work for resolution >= 1280x720
  2502. * - 24bpp modes work resolution < 1280x720
  2503. */
  2504. if (bpp == 8 && mode->width < 1280)
  2505. mode = &ov5640_mode_data[OV5640_MODE_720P_1280_720];
  2506. else if (bpp == 24 && mode->width > 1024)
  2507. mode = &ov5640_mode_data[OV5640_MODE_XGA_1024_768];
  2508. fmt->width = mode->width;
  2509. fmt->height = mode->height;
  2510. if (new_mode)
  2511. *new_mode = mode;
  2512. fmt->code = pixfmt->code;
  2513. fmt->colorspace = pixfmt->colorspace;
  2514. fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
  2515. fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
  2516. fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
  2517. return 0;
  2518. }
  2519. static void __v4l2_ctrl_vblank_update(struct ov5640_dev *sensor, u32 vblank)
  2520. {
  2521. const struct ov5640_mode_info *mode = sensor->current_mode;
  2522. __v4l2_ctrl_modify_range(sensor->ctrls.vblank, OV5640_MIN_VBLANK,
  2523. OV5640_MAX_VTS - mode->height, 1, vblank);
  2524. __v4l2_ctrl_s_ctrl(sensor->ctrls.vblank, vblank);
  2525. }
  2526. static int ov5640_update_pixel_rate(struct ov5640_dev *sensor)
  2527. {
  2528. const struct ov5640_mode_info *mode = sensor->current_mode;
  2529. enum ov5640_pixel_rate_id pixel_rate_id = mode->pixel_rate;
  2530. struct v4l2_mbus_framefmt *fmt = &sensor->fmt;
  2531. const struct ov5640_timings *timings = ov5640_timings(sensor, mode);
  2532. s32 exposure_val, exposure_max;
  2533. unsigned int hblank;
  2534. unsigned int i = 0;
  2535. u32 pixel_rate;
  2536. s64 link_freq;
  2537. u32 num_lanes;
  2538. u32 vblank;
  2539. u32 bpp;
  2540. /*
  2541. * Update the pixel rate control value.
  2542. *
  2543. * For DVP mode, maintain the pixel rate calculation using fixed FPS.
  2544. */
  2545. if (!ov5640_is_csi2(sensor)) {
  2546. __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
  2547. ov5640_calc_pixel_rate(sensor));
  2548. __v4l2_ctrl_vblank_update(sensor, timings->vblank_def);
  2549. return 0;
  2550. }
  2551. /*
  2552. * The MIPI CSI-2 link frequency should comply with the CSI-2
  2553. * specification and be lower than 1GHz.
  2554. *
  2555. * Start from the suggested pixel_rate for the current mode and
  2556. * progressively slow it down if it exceeds 1GHz.
  2557. */
  2558. num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes;
  2559. bpp = ov5640_code_to_bpp(sensor, fmt->code);
  2560. do {
  2561. pixel_rate = ov5640_pixel_rates[pixel_rate_id];
  2562. link_freq = pixel_rate * bpp / (2 * num_lanes);
  2563. } while (link_freq >= 1000000000U &&
  2564. ++pixel_rate_id < OV5640_NUM_PIXEL_RATES);
  2565. sensor->current_link_freq = link_freq;
  2566. /*
  2567. * Higher link rates require the clock tree to be programmed with
  2568. * 'mipi_div' = 1; this has the effect of halving the actual output
  2569. * pixel rate in the MIPI domain.
  2570. *
  2571. * Adjust the pixel rate and link frequency control value to report it
  2572. * correctly to userspace.
  2573. */
  2574. if (link_freq > OV5640_LINK_RATE_MAX) {
  2575. pixel_rate /= 2;
  2576. link_freq /= 2;
  2577. }
  2578. for (i = 0; i < ARRAY_SIZE(ov5640_csi2_link_freqs); ++i) {
  2579. if (ov5640_csi2_link_freqs[i] == link_freq)
  2580. break;
  2581. }
  2582. WARN_ON(i == ARRAY_SIZE(ov5640_csi2_link_freqs));
  2583. __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, pixel_rate);
  2584. __v4l2_ctrl_s_ctrl(sensor->ctrls.link_freq, i);
  2585. hblank = timings->htot - mode->width;
  2586. __v4l2_ctrl_modify_range(sensor->ctrls.hblank,
  2587. hblank, hblank, 1, hblank);
  2588. vblank = timings->vblank_def;
  2589. __v4l2_ctrl_vblank_update(sensor, vblank);
  2590. exposure_max = timings->crop.height + vblank - 4;
  2591. exposure_val = clamp_t(s32, sensor->ctrls.exposure->val,
  2592. sensor->ctrls.exposure->minimum,
  2593. exposure_max);
  2594. __v4l2_ctrl_modify_range(sensor->ctrls.exposure,
  2595. sensor->ctrls.exposure->minimum,
  2596. exposure_max, 1, exposure_val);
  2597. return 0;
  2598. }
  2599. static int ov5640_set_fmt(struct v4l2_subdev *sd,
  2600. struct v4l2_subdev_state *sd_state,
  2601. struct v4l2_subdev_format *format)
  2602. {
  2603. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  2604. const struct ov5640_mode_info *new_mode;
  2605. struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
  2606. int ret;
  2607. if (format->pad != 0)
  2608. return -EINVAL;
  2609. mutex_lock(&sensor->lock);
  2610. if (sensor->streaming) {
  2611. ret = -EBUSY;
  2612. goto out;
  2613. }
  2614. ret = ov5640_try_fmt_internal(sd, mbus_fmt,
  2615. sensor->current_fr, &new_mode);
  2616. if (ret)
  2617. goto out;
  2618. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  2619. *v4l2_subdev_get_try_format(sd, sd_state, 0) = *mbus_fmt;
  2620. goto out;
  2621. }
  2622. if (new_mode != sensor->current_mode) {
  2623. sensor->current_fr = new_mode->def_fps;
  2624. sensor->current_mode = new_mode;
  2625. sensor->pending_mode_change = true;
  2626. }
  2627. if (mbus_fmt->code != sensor->fmt.code)
  2628. sensor->pending_fmt_change = true;
  2629. /* update format even if code is unchanged, resolution might change */
  2630. sensor->fmt = *mbus_fmt;
  2631. ov5640_update_pixel_rate(sensor);
  2632. out:
  2633. mutex_unlock(&sensor->lock);
  2634. return ret;
  2635. }
  2636. static int ov5640_get_selection(struct v4l2_subdev *sd,
  2637. struct v4l2_subdev_state *sd_state,
  2638. struct v4l2_subdev_selection *sel)
  2639. {
  2640. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  2641. const struct ov5640_mode_info *mode = sensor->current_mode;
  2642. const struct ov5640_timings *timings;
  2643. switch (sel->target) {
  2644. case V4L2_SEL_TGT_CROP: {
  2645. mutex_lock(&sensor->lock);
  2646. timings = ov5640_timings(sensor, mode);
  2647. sel->r = timings->analog_crop;
  2648. mutex_unlock(&sensor->lock);
  2649. return 0;
  2650. }
  2651. case V4L2_SEL_TGT_NATIVE_SIZE:
  2652. case V4L2_SEL_TGT_CROP_BOUNDS:
  2653. sel->r.top = 0;
  2654. sel->r.left = 0;
  2655. sel->r.width = OV5640_NATIVE_WIDTH;
  2656. sel->r.height = OV5640_NATIVE_HEIGHT;
  2657. return 0;
  2658. case V4L2_SEL_TGT_CROP_DEFAULT:
  2659. sel->r.top = OV5640_PIXEL_ARRAY_TOP;
  2660. sel->r.left = OV5640_PIXEL_ARRAY_LEFT;
  2661. sel->r.width = OV5640_PIXEL_ARRAY_WIDTH;
  2662. sel->r.height = OV5640_PIXEL_ARRAY_HEIGHT;
  2663. return 0;
  2664. }
  2665. return -EINVAL;
  2666. }
  2667. static int ov5640_set_framefmt(struct ov5640_dev *sensor,
  2668. struct v4l2_mbus_framefmt *format)
  2669. {
  2670. bool is_jpeg = format->code == MEDIA_BUS_FMT_JPEG_1X8;
  2671. const struct ov5640_pixfmt *pixfmt;
  2672. int ret = 0;
  2673. pixfmt = ov5640_code_to_pixfmt(sensor, format->code);
  2674. /* FORMAT CONTROL00: YUV and RGB formatting */
  2675. ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00,
  2676. pixfmt->ctrl00);
  2677. if (ret)
  2678. return ret;
  2679. /* FORMAT MUX CONTROL: ISP YUV or RGB */
  2680. ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
  2681. pixfmt->mux);
  2682. if (ret)
  2683. return ret;
  2684. /*
  2685. * TIMING TC REG21:
  2686. * - [5]: JPEG enable
  2687. */
  2688. ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
  2689. BIT(5), is_jpeg ? BIT(5) : 0);
  2690. if (ret)
  2691. return ret;
  2692. /*
  2693. * SYSTEM RESET02:
  2694. * - [4]: Reset JFIFO
  2695. * - [3]: Reset SFIFO
  2696. * - [2]: Reset JPEG
  2697. */
  2698. ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
  2699. BIT(4) | BIT(3) | BIT(2),
  2700. is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
  2701. if (ret)
  2702. return ret;
  2703. /*
  2704. * CLOCK ENABLE02:
  2705. * - [5]: Enable JPEG 2x clock
  2706. * - [3]: Enable JPEG clock
  2707. */
  2708. return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
  2709. BIT(5) | BIT(3),
  2710. is_jpeg ? (BIT(5) | BIT(3)) : 0);
  2711. }
  2712. /*
  2713. * Sensor Controls.
  2714. */
  2715. static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
  2716. {
  2717. int ret;
  2718. if (value) {
  2719. ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
  2720. BIT(0), BIT(0));
  2721. if (ret)
  2722. return ret;
  2723. ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
  2724. } else {
  2725. ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
  2726. }
  2727. return ret;
  2728. }
  2729. static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
  2730. {
  2731. int ret;
  2732. if (value) {
  2733. ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
  2734. BIT(2), BIT(2));
  2735. if (ret)
  2736. return ret;
  2737. ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
  2738. value & 0xff);
  2739. } else {
  2740. ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
  2741. }
  2742. return ret;
  2743. }
  2744. static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
  2745. {
  2746. int ret;
  2747. if (value) {
  2748. ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
  2749. BIT(1), BIT(1));
  2750. if (ret)
  2751. return ret;
  2752. ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
  2753. value & 0xff);
  2754. if (ret)
  2755. return ret;
  2756. ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
  2757. value & 0xff);
  2758. } else {
  2759. ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
  2760. }
  2761. return ret;
  2762. }
  2763. static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
  2764. {
  2765. int ret;
  2766. ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
  2767. BIT(0), awb ? 0 : 1);
  2768. if (ret)
  2769. return ret;
  2770. if (!awb) {
  2771. u16 red = (u16)sensor->ctrls.red_balance->val;
  2772. u16 blue = (u16)sensor->ctrls.blue_balance->val;
  2773. ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
  2774. if (ret)
  2775. return ret;
  2776. ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
  2777. }
  2778. return ret;
  2779. }
  2780. static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
  2781. enum v4l2_exposure_auto_type auto_exposure)
  2782. {
  2783. struct ov5640_ctrls *ctrls = &sensor->ctrls;
  2784. bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
  2785. int ret = 0;
  2786. if (ctrls->auto_exp->is_new) {
  2787. ret = ov5640_set_autoexposure(sensor, auto_exp);
  2788. if (ret)
  2789. return ret;
  2790. }
  2791. if (!auto_exp && ctrls->exposure->is_new) {
  2792. u16 max_exp;
  2793. ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
  2794. &max_exp);
  2795. if (ret)
  2796. return ret;
  2797. ret = ov5640_get_vts(sensor);
  2798. if (ret < 0)
  2799. return ret;
  2800. max_exp += ret;
  2801. ret = 0;
  2802. if (ctrls->exposure->val < max_exp)
  2803. ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
  2804. }
  2805. return ret;
  2806. }
  2807. static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
  2808. {
  2809. struct ov5640_ctrls *ctrls = &sensor->ctrls;
  2810. int ret = 0;
  2811. if (ctrls->auto_gain->is_new) {
  2812. ret = ov5640_set_autogain(sensor, auto_gain);
  2813. if (ret)
  2814. return ret;
  2815. }
  2816. if (!auto_gain && ctrls->gain->is_new)
  2817. ret = ov5640_set_gain(sensor, ctrls->gain->val);
  2818. return ret;
  2819. }
  2820. static const char * const test_pattern_menu[] = {
  2821. "Disabled",
  2822. "Color bars",
  2823. "Color bars w/ rolling bar",
  2824. "Color squares",
  2825. "Color squares w/ rolling bar",
  2826. };
  2827. #define OV5640_TEST_ENABLE BIT(7)
  2828. #define OV5640_TEST_ROLLING BIT(6) /* rolling horizontal bar */
  2829. #define OV5640_TEST_TRANSPARENT BIT(5)
  2830. #define OV5640_TEST_SQUARE_BW BIT(4) /* black & white squares */
  2831. #define OV5640_TEST_BAR_STANDARD (0 << 2)
  2832. #define OV5640_TEST_BAR_VERT_CHANGE_1 (1 << 2)
  2833. #define OV5640_TEST_BAR_HOR_CHANGE (2 << 2)
  2834. #define OV5640_TEST_BAR_VERT_CHANGE_2 (3 << 2)
  2835. #define OV5640_TEST_BAR (0 << 0)
  2836. #define OV5640_TEST_RANDOM (1 << 0)
  2837. #define OV5640_TEST_SQUARE (2 << 0)
  2838. #define OV5640_TEST_BLACK (3 << 0)
  2839. static const u8 test_pattern_val[] = {
  2840. 0,
  2841. OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 |
  2842. OV5640_TEST_BAR,
  2843. OV5640_TEST_ENABLE | OV5640_TEST_ROLLING |
  2844. OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR,
  2845. OV5640_TEST_ENABLE | OV5640_TEST_SQUARE,
  2846. OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE,
  2847. };
  2848. static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
  2849. {
  2850. return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
  2851. test_pattern_val[value]);
  2852. }
  2853. static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
  2854. {
  2855. int ret;
  2856. ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
  2857. (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
  2858. 0 : BIT(7));
  2859. if (ret)
  2860. return ret;
  2861. return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
  2862. (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
  2863. BIT(2) : 0);
  2864. }
  2865. static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
  2866. {
  2867. /*
  2868. * If sensor is mounted upside down, mirror logic is inversed.
  2869. *
  2870. * Sensor is a BSI (Back Side Illuminated) one,
  2871. * so image captured is physically mirrored.
  2872. * This is why mirror logic is inversed in
  2873. * order to cancel this mirror effect.
  2874. */
  2875. /*
  2876. * TIMING TC REG21:
  2877. * - [2]: ISP mirror
  2878. * - [1]: Sensor mirror
  2879. */
  2880. return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
  2881. BIT(2) | BIT(1),
  2882. (!(value ^ sensor->upside_down)) ?
  2883. (BIT(2) | BIT(1)) : 0);
  2884. }
  2885. static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
  2886. {
  2887. /* If sensor is mounted upside down, flip logic is inversed */
  2888. /*
  2889. * TIMING TC REG20:
  2890. * - [2]: ISP vflip
  2891. * - [1]: Sensor vflip
  2892. */
  2893. return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
  2894. BIT(2) | BIT(1),
  2895. (value ^ sensor->upside_down) ?
  2896. (BIT(2) | BIT(1)) : 0);
  2897. }
  2898. static int ov5640_set_ctrl_vblank(struct ov5640_dev *sensor, int value)
  2899. {
  2900. const struct ov5640_mode_info *mode = sensor->current_mode;
  2901. /* Update the VTOT timing register value. */
  2902. return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS,
  2903. mode->height + value);
  2904. }
  2905. static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  2906. {
  2907. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  2908. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  2909. int val;
  2910. /* v4l2_ctrl_lock() locks our own mutex */
  2911. if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
  2912. return 0;
  2913. switch (ctrl->id) {
  2914. case V4L2_CID_AUTOGAIN:
  2915. val = ov5640_get_gain(sensor);
  2916. if (val < 0)
  2917. return val;
  2918. sensor->ctrls.gain->val = val;
  2919. break;
  2920. case V4L2_CID_EXPOSURE_AUTO:
  2921. val = ov5640_get_exposure(sensor);
  2922. if (val < 0)
  2923. return val;
  2924. sensor->ctrls.exposure->val = val;
  2925. break;
  2926. }
  2927. pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
  2928. return 0;
  2929. }
  2930. static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
  2931. {
  2932. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  2933. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  2934. const struct ov5640_mode_info *mode = sensor->current_mode;
  2935. const struct ov5640_timings *timings;
  2936. unsigned int exp_max;
  2937. int ret;
  2938. /* v4l2_ctrl_lock() locks our own mutex */
  2939. switch (ctrl->id) {
  2940. case V4L2_CID_VBLANK:
  2941. /* Update the exposure range to the newly programmed vblank. */
  2942. timings = ov5640_timings(sensor, mode);
  2943. exp_max = mode->height + ctrl->val - 4;
  2944. __v4l2_ctrl_modify_range(sensor->ctrls.exposure,
  2945. sensor->ctrls.exposure->minimum,
  2946. exp_max, sensor->ctrls.exposure->step,
  2947. timings->vblank_def);
  2948. break;
  2949. }
  2950. /*
  2951. * If the device is not powered up by the host driver do
  2952. * not apply any controls to H/W at this time. Instead
  2953. * the controls will be restored at start streaming time.
  2954. */
  2955. if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev))
  2956. return 0;
  2957. switch (ctrl->id) {
  2958. case V4L2_CID_AUTOGAIN:
  2959. ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
  2960. break;
  2961. case V4L2_CID_EXPOSURE_AUTO:
  2962. ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
  2963. break;
  2964. case V4L2_CID_AUTO_WHITE_BALANCE:
  2965. ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
  2966. break;
  2967. case V4L2_CID_HUE:
  2968. ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
  2969. break;
  2970. case V4L2_CID_CONTRAST:
  2971. ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
  2972. break;
  2973. case V4L2_CID_SATURATION:
  2974. ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
  2975. break;
  2976. case V4L2_CID_TEST_PATTERN:
  2977. ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
  2978. break;
  2979. case V4L2_CID_POWER_LINE_FREQUENCY:
  2980. ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
  2981. break;
  2982. case V4L2_CID_HFLIP:
  2983. ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
  2984. break;
  2985. case V4L2_CID_VFLIP:
  2986. ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
  2987. break;
  2988. case V4L2_CID_VBLANK:
  2989. ret = ov5640_set_ctrl_vblank(sensor, ctrl->val);
  2990. break;
  2991. default:
  2992. ret = -EINVAL;
  2993. break;
  2994. }
  2995. pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
  2996. return ret;
  2997. }
  2998. static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
  2999. .g_volatile_ctrl = ov5640_g_volatile_ctrl,
  3000. .s_ctrl = ov5640_s_ctrl,
  3001. };
  3002. static int ov5640_init_controls(struct ov5640_dev *sensor)
  3003. {
  3004. const struct ov5640_mode_info *mode = sensor->current_mode;
  3005. const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
  3006. struct ov5640_ctrls *ctrls = &sensor->ctrls;
  3007. struct v4l2_ctrl_handler *hdl = &ctrls->handler;
  3008. struct v4l2_fwnode_device_properties props;
  3009. const struct ov5640_timings *timings;
  3010. unsigned int max_vblank;
  3011. unsigned int hblank;
  3012. int ret;
  3013. v4l2_ctrl_handler_init(hdl, 32);
  3014. /* we can use our own mutex for the ctrl lock */
  3015. hdl->lock = &sensor->lock;
  3016. /* Clock related controls */
  3017. ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
  3018. ov5640_pixel_rates[OV5640_NUM_PIXEL_RATES - 1],
  3019. ov5640_pixel_rates[0], 1,
  3020. ov5640_pixel_rates[mode->pixel_rate]);
  3021. ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, ops,
  3022. V4L2_CID_LINK_FREQ,
  3023. ARRAY_SIZE(ov5640_csi2_link_freqs) - 1,
  3024. OV5640_DEFAULT_LINK_FREQ,
  3025. ov5640_csi2_link_freqs);
  3026. timings = ov5640_timings(sensor, mode);
  3027. hblank = timings->htot - mode->width;
  3028. ctrls->hblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HBLANK, hblank,
  3029. hblank, 1, hblank);
  3030. max_vblank = OV5640_MAX_VTS - mode->height;
  3031. ctrls->vblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VBLANK,
  3032. OV5640_MIN_VBLANK, max_vblank,
  3033. 1, timings->vblank_def);
  3034. /* Auto/manual white balance */
  3035. ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
  3036. V4L2_CID_AUTO_WHITE_BALANCE,
  3037. 0, 1, 1, 1);
  3038. ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
  3039. 0, 4095, 1, 0);
  3040. ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
  3041. 0, 4095, 1, 0);
  3042. /* Auto/manual exposure */
  3043. ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
  3044. V4L2_CID_EXPOSURE_AUTO,
  3045. V4L2_EXPOSURE_MANUAL, 0,
  3046. V4L2_EXPOSURE_AUTO);
  3047. ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
  3048. 0, 65535, 1, 0);
  3049. /* Auto/manual gain */
  3050. ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
  3051. 0, 1, 1, 1);
  3052. ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_ANALOGUE_GAIN,
  3053. 0, 1023, 1, 0);
  3054. ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
  3055. 0, 255, 1, 64);
  3056. ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
  3057. 0, 359, 1, 0);
  3058. ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
  3059. 0, 255, 1, 0);
  3060. ctrls->test_pattern =
  3061. v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
  3062. ARRAY_SIZE(test_pattern_menu) - 1,
  3063. 0, 0, test_pattern_menu);
  3064. ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
  3065. 0, 1, 1, 0);
  3066. ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
  3067. 0, 1, 1, 0);
  3068. ctrls->light_freq =
  3069. v4l2_ctrl_new_std_menu(hdl, ops,
  3070. V4L2_CID_POWER_LINE_FREQUENCY,
  3071. V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
  3072. V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
  3073. if (hdl->error) {
  3074. ret = hdl->error;
  3075. goto free_ctrls;
  3076. }
  3077. ret = v4l2_fwnode_device_parse(&sensor->i2c_client->dev, &props);
  3078. if (ret)
  3079. goto free_ctrls;
  3080. if (props.rotation == 180)
  3081. sensor->upside_down = true;
  3082. ret = v4l2_ctrl_new_fwnode_properties(hdl, ops, &props);
  3083. if (ret)
  3084. goto free_ctrls;
  3085. ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  3086. ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  3087. ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  3088. ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
  3089. ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
  3090. v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
  3091. v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
  3092. v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
  3093. sensor->sd.ctrl_handler = hdl;
  3094. return 0;
  3095. free_ctrls:
  3096. v4l2_ctrl_handler_free(hdl);
  3097. return ret;
  3098. }
  3099. static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
  3100. struct v4l2_subdev_state *sd_state,
  3101. struct v4l2_subdev_frame_size_enum *fse)
  3102. {
  3103. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  3104. u32 bpp = ov5640_code_to_bpp(sensor, fse->code);
  3105. unsigned int index = fse->index;
  3106. if (fse->pad != 0)
  3107. return -EINVAL;
  3108. if (!bpp)
  3109. return -EINVAL;
  3110. /* Only low-resolution modes are supported for 24bpp formats. */
  3111. if (bpp == 24 && index >= OV5640_MODE_720P_1280_720)
  3112. return -EINVAL;
  3113. /* FIXME: Low resolution modes don't work in 8bpp formats. */
  3114. if (bpp == 8)
  3115. index += OV5640_MODE_720P_1280_720;
  3116. if (index >= OV5640_NUM_MODES)
  3117. return -EINVAL;
  3118. fse->min_width = ov5640_mode_data[index].width;
  3119. fse->max_width = fse->min_width;
  3120. fse->min_height = ov5640_mode_data[index].height;
  3121. fse->max_height = fse->min_height;
  3122. return 0;
  3123. }
  3124. static int ov5640_enum_frame_interval(
  3125. struct v4l2_subdev *sd,
  3126. struct v4l2_subdev_state *sd_state,
  3127. struct v4l2_subdev_frame_interval_enum *fie)
  3128. {
  3129. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  3130. struct v4l2_fract tpf;
  3131. int ret;
  3132. if (fie->pad != 0)
  3133. return -EINVAL;
  3134. if (fie->index >= OV5640_NUM_FRAMERATES)
  3135. return -EINVAL;
  3136. tpf.numerator = 1;
  3137. tpf.denominator = ov5640_framerates[fie->index];
  3138. ret = ov5640_try_frame_interval(sensor, &tpf,
  3139. fie->width, fie->height);
  3140. if (ret < 0)
  3141. return -EINVAL;
  3142. fie->interval = tpf;
  3143. return 0;
  3144. }
  3145. static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
  3146. struct v4l2_subdev_frame_interval *fi)
  3147. {
  3148. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  3149. mutex_lock(&sensor->lock);
  3150. fi->interval = sensor->frame_interval;
  3151. mutex_unlock(&sensor->lock);
  3152. return 0;
  3153. }
  3154. static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
  3155. struct v4l2_subdev_frame_interval *fi)
  3156. {
  3157. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  3158. const struct ov5640_mode_info *mode;
  3159. int frame_rate, ret = 0;
  3160. if (fi->pad != 0)
  3161. return -EINVAL;
  3162. mutex_lock(&sensor->lock);
  3163. if (sensor->streaming) {
  3164. ret = -EBUSY;
  3165. goto out;
  3166. }
  3167. mode = sensor->current_mode;
  3168. frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
  3169. mode->width,
  3170. mode->height);
  3171. if (frame_rate < 0) {
  3172. /* Always return a valid frame interval value */
  3173. fi->interval = sensor->frame_interval;
  3174. goto out;
  3175. }
  3176. mode = ov5640_find_mode(sensor, mode->width, mode->height, true);
  3177. if (!mode) {
  3178. ret = -EINVAL;
  3179. goto out;
  3180. }
  3181. if (ov5640_framerates[frame_rate] > ov5640_framerates[mode->max_fps]) {
  3182. ret = -EINVAL;
  3183. goto out;
  3184. }
  3185. if (mode != sensor->current_mode ||
  3186. frame_rate != sensor->current_fr) {
  3187. sensor->current_fr = frame_rate;
  3188. sensor->frame_interval = fi->interval;
  3189. sensor->current_mode = mode;
  3190. sensor->pending_mode_change = true;
  3191. ov5640_update_pixel_rate(sensor);
  3192. }
  3193. out:
  3194. mutex_unlock(&sensor->lock);
  3195. return ret;
  3196. }
  3197. static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
  3198. struct v4l2_subdev_state *sd_state,
  3199. struct v4l2_subdev_mbus_code_enum *code)
  3200. {
  3201. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  3202. const struct ov5640_pixfmt *formats;
  3203. unsigned int num_formats;
  3204. if (ov5640_is_csi2(sensor)) {
  3205. formats = ov5640_csi2_formats;
  3206. num_formats = ARRAY_SIZE(ov5640_csi2_formats) - 1;
  3207. } else {
  3208. formats = ov5640_dvp_formats;
  3209. num_formats = ARRAY_SIZE(ov5640_dvp_formats) - 1;
  3210. }
  3211. if (code->index >= num_formats)
  3212. return -EINVAL;
  3213. code->code = formats[code->index].code;
  3214. return 0;
  3215. }
  3216. static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
  3217. {
  3218. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  3219. int ret = 0;
  3220. if (enable) {
  3221. ret = pm_runtime_resume_and_get(&sensor->i2c_client->dev);
  3222. if (ret < 0)
  3223. return ret;
  3224. ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
  3225. if (ret) {
  3226. pm_runtime_put(&sensor->i2c_client->dev);
  3227. return ret;
  3228. }
  3229. }
  3230. mutex_lock(&sensor->lock);
  3231. if (sensor->streaming == !enable) {
  3232. if (enable && sensor->pending_mode_change) {
  3233. ret = ov5640_set_mode(sensor);
  3234. if (ret)
  3235. goto out;
  3236. }
  3237. if (enable && sensor->pending_fmt_change) {
  3238. ret = ov5640_set_framefmt(sensor, &sensor->fmt);
  3239. if (ret)
  3240. goto out;
  3241. sensor->pending_fmt_change = false;
  3242. }
  3243. if (ov5640_is_csi2(sensor))
  3244. ret = ov5640_set_stream_mipi(sensor, enable);
  3245. else
  3246. ret = ov5640_set_stream_dvp(sensor, enable);
  3247. if (!ret)
  3248. sensor->streaming = enable;
  3249. }
  3250. out:
  3251. mutex_unlock(&sensor->lock);
  3252. if (!enable || ret)
  3253. pm_runtime_put_autosuspend(&sensor->i2c_client->dev);
  3254. return ret;
  3255. }
  3256. static int ov5640_init_cfg(struct v4l2_subdev *sd,
  3257. struct v4l2_subdev_state *state)
  3258. {
  3259. struct v4l2_mbus_framefmt *fmt =
  3260. v4l2_subdev_get_try_format(sd, state, 0);
  3261. struct v4l2_rect *crop = v4l2_subdev_get_try_crop(sd, state, 0);
  3262. *fmt = ov5640_default_fmt;
  3263. crop->left = OV5640_PIXEL_ARRAY_LEFT;
  3264. crop->top = OV5640_PIXEL_ARRAY_TOP;
  3265. crop->width = OV5640_PIXEL_ARRAY_WIDTH;
  3266. crop->height = OV5640_PIXEL_ARRAY_HEIGHT;
  3267. return 0;
  3268. }
  3269. static const struct v4l2_subdev_core_ops ov5640_core_ops = {
  3270. .log_status = v4l2_ctrl_subdev_log_status,
  3271. .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
  3272. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  3273. };
  3274. static const struct v4l2_subdev_video_ops ov5640_video_ops = {
  3275. .g_frame_interval = ov5640_g_frame_interval,
  3276. .s_frame_interval = ov5640_s_frame_interval,
  3277. .s_stream = ov5640_s_stream,
  3278. };
  3279. static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
  3280. .init_cfg = ov5640_init_cfg,
  3281. .enum_mbus_code = ov5640_enum_mbus_code,
  3282. .get_fmt = ov5640_get_fmt,
  3283. .set_fmt = ov5640_set_fmt,
  3284. .get_selection = ov5640_get_selection,
  3285. .enum_frame_size = ov5640_enum_frame_size,
  3286. .enum_frame_interval = ov5640_enum_frame_interval,
  3287. };
  3288. static const struct v4l2_subdev_ops ov5640_subdev_ops = {
  3289. .core = &ov5640_core_ops,
  3290. .video = &ov5640_video_ops,
  3291. .pad = &ov5640_pad_ops,
  3292. };
  3293. static int ov5640_get_regulators(struct ov5640_dev *sensor)
  3294. {
  3295. int i;
  3296. for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
  3297. sensor->supplies[i].supply = ov5640_supply_name[i];
  3298. return devm_regulator_bulk_get(&sensor->i2c_client->dev,
  3299. OV5640_NUM_SUPPLIES,
  3300. sensor->supplies);
  3301. }
  3302. static int ov5640_check_chip_id(struct ov5640_dev *sensor)
  3303. {
  3304. struct i2c_client *client = sensor->i2c_client;
  3305. int ret = 0;
  3306. u16 chip_id;
  3307. ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
  3308. if (ret) {
  3309. dev_err(&client->dev, "%s: failed to read chip identifier\n",
  3310. __func__);
  3311. return ret;
  3312. }
  3313. if (chip_id != 0x5640) {
  3314. dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
  3315. __func__, chip_id);
  3316. return -ENXIO;
  3317. }
  3318. return 0;
  3319. }
  3320. static int ov5640_probe(struct i2c_client *client)
  3321. {
  3322. struct device *dev = &client->dev;
  3323. struct fwnode_handle *endpoint;
  3324. struct ov5640_dev *sensor;
  3325. int ret;
  3326. sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
  3327. if (!sensor)
  3328. return -ENOMEM;
  3329. sensor->i2c_client = client;
  3330. /*
  3331. * default init sequence initialize sensor to
  3332. * YUV422 UYVY VGA@30fps
  3333. */
  3334. sensor->fmt = ov5640_default_fmt;
  3335. sensor->frame_interval.numerator = 1;
  3336. sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
  3337. sensor->current_fr = OV5640_30_FPS;
  3338. sensor->current_mode =
  3339. &ov5640_mode_data[OV5640_MODE_VGA_640_480];
  3340. sensor->last_mode = sensor->current_mode;
  3341. sensor->current_link_freq =
  3342. ov5640_csi2_link_freqs[OV5640_DEFAULT_LINK_FREQ];
  3343. sensor->ae_target = 52;
  3344. endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
  3345. NULL);
  3346. if (!endpoint) {
  3347. dev_err(dev, "endpoint node not found\n");
  3348. return -EINVAL;
  3349. }
  3350. ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
  3351. fwnode_handle_put(endpoint);
  3352. if (ret) {
  3353. dev_err(dev, "Could not parse endpoint\n");
  3354. return ret;
  3355. }
  3356. if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL &&
  3357. sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY &&
  3358. sensor->ep.bus_type != V4L2_MBUS_BT656) {
  3359. dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type);
  3360. return -EINVAL;
  3361. }
  3362. /* get system clock (xclk) */
  3363. sensor->xclk = devm_clk_get(dev, "xclk");
  3364. if (IS_ERR(sensor->xclk)) {
  3365. dev_err(dev, "failed to get xclk\n");
  3366. return PTR_ERR(sensor->xclk);
  3367. }
  3368. sensor->xclk_freq = clk_get_rate(sensor->xclk);
  3369. if (sensor->xclk_freq < OV5640_XCLK_MIN ||
  3370. sensor->xclk_freq > OV5640_XCLK_MAX) {
  3371. dev_err(dev, "xclk frequency out of range: %d Hz\n",
  3372. sensor->xclk_freq);
  3373. return -EINVAL;
  3374. }
  3375. /* request optional power down pin */
  3376. sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
  3377. GPIOD_OUT_HIGH);
  3378. if (IS_ERR(sensor->pwdn_gpio))
  3379. return PTR_ERR(sensor->pwdn_gpio);
  3380. /* request optional reset pin */
  3381. sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  3382. GPIOD_OUT_HIGH);
  3383. if (IS_ERR(sensor->reset_gpio))
  3384. return PTR_ERR(sensor->reset_gpio);
  3385. v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
  3386. sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  3387. V4L2_SUBDEV_FL_HAS_EVENTS;
  3388. sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
  3389. sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  3390. ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
  3391. if (ret)
  3392. return ret;
  3393. ret = ov5640_get_regulators(sensor);
  3394. if (ret)
  3395. goto entity_cleanup;
  3396. mutex_init(&sensor->lock);
  3397. ret = ov5640_init_controls(sensor);
  3398. if (ret)
  3399. goto entity_cleanup;
  3400. ret = ov5640_sensor_resume(dev);
  3401. if (ret) {
  3402. dev_err(dev, "failed to power on\n");
  3403. goto free_ctrls;
  3404. }
  3405. pm_runtime_set_active(dev);
  3406. pm_runtime_get_noresume(dev);
  3407. pm_runtime_enable(dev);
  3408. ret = ov5640_check_chip_id(sensor);
  3409. if (ret)
  3410. goto err_pm_runtime;
  3411. ret = v4l2_async_register_subdev_sensor(&sensor->sd);
  3412. if (ret)
  3413. goto err_pm_runtime;
  3414. pm_runtime_set_autosuspend_delay(dev, 1000);
  3415. pm_runtime_use_autosuspend(dev);
  3416. pm_runtime_put_autosuspend(dev);
  3417. return 0;
  3418. err_pm_runtime:
  3419. pm_runtime_put_noidle(dev);
  3420. pm_runtime_disable(dev);
  3421. ov5640_sensor_suspend(dev);
  3422. free_ctrls:
  3423. v4l2_ctrl_handler_free(&sensor->ctrls.handler);
  3424. entity_cleanup:
  3425. media_entity_cleanup(&sensor->sd.entity);
  3426. mutex_destroy(&sensor->lock);
  3427. return ret;
  3428. }
  3429. static void ov5640_remove(struct i2c_client *client)
  3430. {
  3431. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  3432. struct ov5640_dev *sensor = to_ov5640_dev(sd);
  3433. struct device *dev = &client->dev;
  3434. pm_runtime_disable(dev);
  3435. if (!pm_runtime_status_suspended(dev))
  3436. ov5640_sensor_suspend(dev);
  3437. pm_runtime_set_suspended(dev);
  3438. v4l2_async_unregister_subdev(&sensor->sd);
  3439. media_entity_cleanup(&sensor->sd.entity);
  3440. v4l2_ctrl_handler_free(&sensor->ctrls.handler);
  3441. mutex_destroy(&sensor->lock);
  3442. }
  3443. static const struct dev_pm_ops ov5640_pm_ops = {
  3444. SET_RUNTIME_PM_OPS(ov5640_sensor_suspend, ov5640_sensor_resume, NULL)
  3445. };
  3446. static const struct i2c_device_id ov5640_id[] = {
  3447. {"ov5640", 0},
  3448. {},
  3449. };
  3450. MODULE_DEVICE_TABLE(i2c, ov5640_id);
  3451. static const struct of_device_id ov5640_dt_ids[] = {
  3452. { .compatible = "ovti,ov5640" },
  3453. { /* sentinel */ }
  3454. };
  3455. MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
  3456. static struct i2c_driver ov5640_i2c_driver = {
  3457. .driver = {
  3458. .name = "ov5640",
  3459. .of_match_table = ov5640_dt_ids,
  3460. .pm = &ov5640_pm_ops,
  3461. },
  3462. .id_table = ov5640_id,
  3463. .probe_new = ov5640_probe,
  3464. .remove = ov5640_remove,
  3465. };
  3466. module_i2c_driver(ov5640_i2c_driver);
  3467. MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
  3468. MODULE_LICENSE("GPL");