cxd2841er.c 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872
  1. /*
  2. * cxd2841er.c
  3. *
  4. * Sony digital demodulator driver for
  5. * CXD2841ER - DVB-S/S2/T/T2/C/C2
  6. * CXD2854ER - DVB-S/S2/T/T2/C/C2, ISDB-T/S
  7. *
  8. * Copyright 2012 Sony Corporation
  9. * Copyright (C) 2014 NetUP Inc.
  10. * Copyright (C) 2014 Sergey Kozlov <serjk@netup.ru>
  11. * Copyright (C) 2014 Abylay Ospan <aospan@netup.ru>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/string.h>
  26. #include <linux/slab.h>
  27. #include <linux/bitops.h>
  28. #include <linux/math64.h>
  29. #include <linux/log2.h>
  30. #include <linux/dynamic_debug.h>
  31. #include "dvb_math.h"
  32. #include "dvb_frontend.h"
  33. #include "cxd2841er.h"
  34. #include "cxd2841er_priv.h"
  35. #define MAX_WRITE_REGSIZE 16
  36. #define LOG2_E_100X 144
  37. /* DVB-C constellation */
  38. enum sony_dvbc_constellation_t {
  39. SONY_DVBC_CONSTELLATION_16QAM,
  40. SONY_DVBC_CONSTELLATION_32QAM,
  41. SONY_DVBC_CONSTELLATION_64QAM,
  42. SONY_DVBC_CONSTELLATION_128QAM,
  43. SONY_DVBC_CONSTELLATION_256QAM
  44. };
  45. enum cxd2841er_state {
  46. STATE_SHUTDOWN = 0,
  47. STATE_SLEEP_S,
  48. STATE_ACTIVE_S,
  49. STATE_SLEEP_TC,
  50. STATE_ACTIVE_TC
  51. };
  52. struct cxd2841er_priv {
  53. struct dvb_frontend frontend;
  54. struct i2c_adapter *i2c;
  55. u8 i2c_addr_slvx;
  56. u8 i2c_addr_slvt;
  57. const struct cxd2841er_config *config;
  58. enum cxd2841er_state state;
  59. u8 system;
  60. enum cxd2841er_xtal xtal;
  61. enum fe_caps caps;
  62. };
  63. static const struct cxd2841er_cnr_data s_cn_data[] = {
  64. { 0x033e, 0 }, { 0x0339, 100 }, { 0x0333, 200 },
  65. { 0x032e, 300 }, { 0x0329, 400 }, { 0x0324, 500 },
  66. { 0x031e, 600 }, { 0x0319, 700 }, { 0x0314, 800 },
  67. { 0x030f, 900 }, { 0x030a, 1000 }, { 0x02ff, 1100 },
  68. { 0x02f4, 1200 }, { 0x02e9, 1300 }, { 0x02de, 1400 },
  69. { 0x02d4, 1500 }, { 0x02c9, 1600 }, { 0x02bf, 1700 },
  70. { 0x02b5, 1800 }, { 0x02ab, 1900 }, { 0x02a1, 2000 },
  71. { 0x029b, 2100 }, { 0x0295, 2200 }, { 0x0290, 2300 },
  72. { 0x028a, 2400 }, { 0x0284, 2500 }, { 0x027f, 2600 },
  73. { 0x0279, 2700 }, { 0x0274, 2800 }, { 0x026e, 2900 },
  74. { 0x0269, 3000 }, { 0x0262, 3100 }, { 0x025c, 3200 },
  75. { 0x0255, 3300 }, { 0x024f, 3400 }, { 0x0249, 3500 },
  76. { 0x0242, 3600 }, { 0x023c, 3700 }, { 0x0236, 3800 },
  77. { 0x0230, 3900 }, { 0x022a, 4000 }, { 0x0223, 4100 },
  78. { 0x021c, 4200 }, { 0x0215, 4300 }, { 0x020e, 4400 },
  79. { 0x0207, 4500 }, { 0x0201, 4600 }, { 0x01fa, 4700 },
  80. { 0x01f4, 4800 }, { 0x01ed, 4900 }, { 0x01e7, 5000 },
  81. { 0x01e0, 5100 }, { 0x01d9, 5200 }, { 0x01d2, 5300 },
  82. { 0x01cb, 5400 }, { 0x01c4, 5500 }, { 0x01be, 5600 },
  83. { 0x01b7, 5700 }, { 0x01b1, 5800 }, { 0x01aa, 5900 },
  84. { 0x01a4, 6000 }, { 0x019d, 6100 }, { 0x0196, 6200 },
  85. { 0x018f, 6300 }, { 0x0189, 6400 }, { 0x0182, 6500 },
  86. { 0x017c, 6600 }, { 0x0175, 6700 }, { 0x016f, 6800 },
  87. { 0x0169, 6900 }, { 0x0163, 7000 }, { 0x015c, 7100 },
  88. { 0x0156, 7200 }, { 0x0150, 7300 }, { 0x014a, 7400 },
  89. { 0x0144, 7500 }, { 0x013e, 7600 }, { 0x0138, 7700 },
  90. { 0x0132, 7800 }, { 0x012d, 7900 }, { 0x0127, 8000 },
  91. { 0x0121, 8100 }, { 0x011c, 8200 }, { 0x0116, 8300 },
  92. { 0x0111, 8400 }, { 0x010b, 8500 }, { 0x0106, 8600 },
  93. { 0x0101, 8700 }, { 0x00fc, 8800 }, { 0x00f7, 8900 },
  94. { 0x00f2, 9000 }, { 0x00ee, 9100 }, { 0x00ea, 9200 },
  95. { 0x00e6, 9300 }, { 0x00e2, 9400 }, { 0x00de, 9500 },
  96. { 0x00da, 9600 }, { 0x00d7, 9700 }, { 0x00d3, 9800 },
  97. { 0x00d0, 9900 }, { 0x00cc, 10000 }, { 0x00c7, 10100 },
  98. { 0x00c3, 10200 }, { 0x00bf, 10300 }, { 0x00ba, 10400 },
  99. { 0x00b6, 10500 }, { 0x00b2, 10600 }, { 0x00ae, 10700 },
  100. { 0x00aa, 10800 }, { 0x00a7, 10900 }, { 0x00a3, 11000 },
  101. { 0x009f, 11100 }, { 0x009c, 11200 }, { 0x0098, 11300 },
  102. { 0x0094, 11400 }, { 0x0091, 11500 }, { 0x008e, 11600 },
  103. { 0x008a, 11700 }, { 0x0087, 11800 }, { 0x0084, 11900 },
  104. { 0x0081, 12000 }, { 0x007e, 12100 }, { 0x007b, 12200 },
  105. { 0x0079, 12300 }, { 0x0076, 12400 }, { 0x0073, 12500 },
  106. { 0x0071, 12600 }, { 0x006e, 12700 }, { 0x006c, 12800 },
  107. { 0x0069, 12900 }, { 0x0067, 13000 }, { 0x0065, 13100 },
  108. { 0x0062, 13200 }, { 0x0060, 13300 }, { 0x005e, 13400 },
  109. { 0x005c, 13500 }, { 0x005a, 13600 }, { 0x0058, 13700 },
  110. { 0x0056, 13800 }, { 0x0054, 13900 }, { 0x0052, 14000 },
  111. { 0x0050, 14100 }, { 0x004e, 14200 }, { 0x004c, 14300 },
  112. { 0x004b, 14400 }, { 0x0049, 14500 }, { 0x0047, 14600 },
  113. { 0x0046, 14700 }, { 0x0044, 14800 }, { 0x0043, 14900 },
  114. { 0x0041, 15000 }, { 0x003f, 15100 }, { 0x003e, 15200 },
  115. { 0x003c, 15300 }, { 0x003b, 15400 }, { 0x003a, 15500 },
  116. { 0x0037, 15700 }, { 0x0036, 15800 }, { 0x0034, 15900 },
  117. { 0x0033, 16000 }, { 0x0032, 16100 }, { 0x0031, 16200 },
  118. { 0x0030, 16300 }, { 0x002f, 16400 }, { 0x002e, 16500 },
  119. { 0x002d, 16600 }, { 0x002c, 16700 }, { 0x002b, 16800 },
  120. { 0x002a, 16900 }, { 0x0029, 17000 }, { 0x0028, 17100 },
  121. { 0x0027, 17200 }, { 0x0026, 17300 }, { 0x0025, 17400 },
  122. { 0x0024, 17500 }, { 0x0023, 17600 }, { 0x0022, 17800 },
  123. { 0x0021, 17900 }, { 0x0020, 18000 }, { 0x001f, 18200 },
  124. { 0x001e, 18300 }, { 0x001d, 18500 }, { 0x001c, 18700 },
  125. { 0x001b, 18900 }, { 0x001a, 19000 }, { 0x0019, 19200 },
  126. { 0x0018, 19300 }, { 0x0017, 19500 }, { 0x0016, 19700 },
  127. { 0x0015, 19900 }, { 0x0014, 20000 },
  128. };
  129. static const struct cxd2841er_cnr_data s2_cn_data[] = {
  130. { 0x05af, 0 }, { 0x0597, 100 }, { 0x057e, 200 },
  131. { 0x0567, 300 }, { 0x0550, 400 }, { 0x0539, 500 },
  132. { 0x0522, 600 }, { 0x050c, 700 }, { 0x04f6, 800 },
  133. { 0x04e1, 900 }, { 0x04cc, 1000 }, { 0x04b6, 1100 },
  134. { 0x04a1, 1200 }, { 0x048c, 1300 }, { 0x0477, 1400 },
  135. { 0x0463, 1500 }, { 0x044f, 1600 }, { 0x043c, 1700 },
  136. { 0x0428, 1800 }, { 0x0416, 1900 }, { 0x0403, 2000 },
  137. { 0x03ef, 2100 }, { 0x03dc, 2200 }, { 0x03c9, 2300 },
  138. { 0x03b6, 2400 }, { 0x03a4, 2500 }, { 0x0392, 2600 },
  139. { 0x0381, 2700 }, { 0x036f, 2800 }, { 0x035f, 2900 },
  140. { 0x034e, 3000 }, { 0x033d, 3100 }, { 0x032d, 3200 },
  141. { 0x031d, 3300 }, { 0x030d, 3400 }, { 0x02fd, 3500 },
  142. { 0x02ee, 3600 }, { 0x02df, 3700 }, { 0x02d0, 3800 },
  143. { 0x02c2, 3900 }, { 0x02b4, 4000 }, { 0x02a6, 4100 },
  144. { 0x0299, 4200 }, { 0x028c, 4300 }, { 0x027f, 4400 },
  145. { 0x0272, 4500 }, { 0x0265, 4600 }, { 0x0259, 4700 },
  146. { 0x024d, 4800 }, { 0x0241, 4900 }, { 0x0236, 5000 },
  147. { 0x022b, 5100 }, { 0x0220, 5200 }, { 0x0215, 5300 },
  148. { 0x020a, 5400 }, { 0x0200, 5500 }, { 0x01f6, 5600 },
  149. { 0x01ec, 5700 }, { 0x01e2, 5800 }, { 0x01d8, 5900 },
  150. { 0x01cf, 6000 }, { 0x01c6, 6100 }, { 0x01bc, 6200 },
  151. { 0x01b3, 6300 }, { 0x01aa, 6400 }, { 0x01a2, 6500 },
  152. { 0x0199, 6600 }, { 0x0191, 6700 }, { 0x0189, 6800 },
  153. { 0x0181, 6900 }, { 0x0179, 7000 }, { 0x0171, 7100 },
  154. { 0x0169, 7200 }, { 0x0161, 7300 }, { 0x015a, 7400 },
  155. { 0x0153, 7500 }, { 0x014b, 7600 }, { 0x0144, 7700 },
  156. { 0x013d, 7800 }, { 0x0137, 7900 }, { 0x0130, 8000 },
  157. { 0x012a, 8100 }, { 0x0124, 8200 }, { 0x011e, 8300 },
  158. { 0x0118, 8400 }, { 0x0112, 8500 }, { 0x010c, 8600 },
  159. { 0x0107, 8700 }, { 0x0101, 8800 }, { 0x00fc, 8900 },
  160. { 0x00f7, 9000 }, { 0x00f2, 9100 }, { 0x00ec, 9200 },
  161. { 0x00e7, 9300 }, { 0x00e2, 9400 }, { 0x00dd, 9500 },
  162. { 0x00d8, 9600 }, { 0x00d4, 9700 }, { 0x00cf, 9800 },
  163. { 0x00ca, 9900 }, { 0x00c6, 10000 }, { 0x00c2, 10100 },
  164. { 0x00be, 10200 }, { 0x00b9, 10300 }, { 0x00b5, 10400 },
  165. { 0x00b1, 10500 }, { 0x00ae, 10600 }, { 0x00aa, 10700 },
  166. { 0x00a6, 10800 }, { 0x00a3, 10900 }, { 0x009f, 11000 },
  167. { 0x009b, 11100 }, { 0x0098, 11200 }, { 0x0095, 11300 },
  168. { 0x0091, 11400 }, { 0x008e, 11500 }, { 0x008b, 11600 },
  169. { 0x0088, 11700 }, { 0x0085, 11800 }, { 0x0082, 11900 },
  170. { 0x007f, 12000 }, { 0x007c, 12100 }, { 0x007a, 12200 },
  171. { 0x0077, 12300 }, { 0x0074, 12400 }, { 0x0072, 12500 },
  172. { 0x006f, 12600 }, { 0x006d, 12700 }, { 0x006b, 12800 },
  173. { 0x0068, 12900 }, { 0x0066, 13000 }, { 0x0064, 13100 },
  174. { 0x0061, 13200 }, { 0x005f, 13300 }, { 0x005d, 13400 },
  175. { 0x005b, 13500 }, { 0x0059, 13600 }, { 0x0057, 13700 },
  176. { 0x0055, 13800 }, { 0x0053, 13900 }, { 0x0051, 14000 },
  177. { 0x004f, 14100 }, { 0x004e, 14200 }, { 0x004c, 14300 },
  178. { 0x004a, 14400 }, { 0x0049, 14500 }, { 0x0047, 14600 },
  179. { 0x0045, 14700 }, { 0x0044, 14800 }, { 0x0042, 14900 },
  180. { 0x0041, 15000 }, { 0x003f, 15100 }, { 0x003e, 15200 },
  181. { 0x003c, 15300 }, { 0x003b, 15400 }, { 0x003a, 15500 },
  182. { 0x0038, 15600 }, { 0x0037, 15700 }, { 0x0036, 15800 },
  183. { 0x0034, 15900 }, { 0x0033, 16000 }, { 0x0032, 16100 },
  184. { 0x0031, 16200 }, { 0x0030, 16300 }, { 0x002f, 16400 },
  185. { 0x002e, 16500 }, { 0x002d, 16600 }, { 0x002c, 16700 },
  186. { 0x002b, 16800 }, { 0x002a, 16900 }, { 0x0029, 17000 },
  187. { 0x0028, 17100 }, { 0x0027, 17200 }, { 0x0026, 17300 },
  188. { 0x0025, 17400 }, { 0x0024, 17500 }, { 0x0023, 17600 },
  189. { 0x0022, 17800 }, { 0x0021, 17900 }, { 0x0020, 18000 },
  190. { 0x001f, 18200 }, { 0x001e, 18300 }, { 0x001d, 18500 },
  191. { 0x001c, 18700 }, { 0x001b, 18900 }, { 0x001a, 19000 },
  192. { 0x0019, 19200 }, { 0x0018, 19300 }, { 0x0017, 19500 },
  193. { 0x0016, 19700 }, { 0x0015, 19900 }, { 0x0014, 20000 },
  194. };
  195. #define MAKE_IFFREQ_CONFIG(iffreq) ((u32)(((iffreq)/41.0)*16777216.0 + 0.5))
  196. #define MAKE_IFFREQ_CONFIG_XTAL(xtal, iffreq) ((xtal == SONY_XTAL_24000) ? \
  197. (u32)(((iffreq)/48.0)*16777216.0 + 0.5) : \
  198. (u32)(((iffreq)/41.0)*16777216.0 + 0.5))
  199. static int cxd2841er_freeze_regs(struct cxd2841er_priv *priv);
  200. static int cxd2841er_unfreeze_regs(struct cxd2841er_priv *priv);
  201. static void cxd2841er_i2c_debug(struct cxd2841er_priv *priv,
  202. u8 addr, u8 reg, u8 write,
  203. const u8 *data, u32 len)
  204. {
  205. dev_dbg(&priv->i2c->dev,
  206. "cxd2841er: I2C %s addr %02x reg 0x%02x size %d\n",
  207. (write == 0 ? "read" : "write"), addr, reg, len);
  208. print_hex_dump_bytes("cxd2841er: I2C data: ",
  209. DUMP_PREFIX_OFFSET, data, len);
  210. }
  211. static int cxd2841er_write_regs(struct cxd2841er_priv *priv,
  212. u8 addr, u8 reg, const u8 *data, u32 len)
  213. {
  214. int ret;
  215. u8 buf[MAX_WRITE_REGSIZE + 1];
  216. u8 i2c_addr = (addr == I2C_SLVX ?
  217. priv->i2c_addr_slvx : priv->i2c_addr_slvt);
  218. struct i2c_msg msg[1] = {
  219. {
  220. .addr = i2c_addr,
  221. .flags = 0,
  222. .len = len + 1,
  223. .buf = buf,
  224. }
  225. };
  226. if (len + 1 >= sizeof(buf)) {
  227. dev_warn(&priv->i2c->dev, "wr reg=%04x: len=%d is too big!\n",
  228. reg, len + 1);
  229. return -E2BIG;
  230. }
  231. cxd2841er_i2c_debug(priv, i2c_addr, reg, 1, data, len);
  232. buf[0] = reg;
  233. memcpy(&buf[1], data, len);
  234. ret = i2c_transfer(priv->i2c, msg, 1);
  235. if (ret >= 0 && ret != 1)
  236. ret = -EIO;
  237. if (ret < 0) {
  238. dev_warn(&priv->i2c->dev,
  239. "%s: i2c wr failed=%d addr=%02x reg=%02x len=%d\n",
  240. KBUILD_MODNAME, ret, i2c_addr, reg, len);
  241. return ret;
  242. }
  243. return 0;
  244. }
  245. static int cxd2841er_write_reg(struct cxd2841er_priv *priv,
  246. u8 addr, u8 reg, u8 val)
  247. {
  248. return cxd2841er_write_regs(priv, addr, reg, &val, 1);
  249. }
  250. static int cxd2841er_read_regs(struct cxd2841er_priv *priv,
  251. u8 addr, u8 reg, u8 *val, u32 len)
  252. {
  253. int ret;
  254. u8 i2c_addr = (addr == I2C_SLVX ?
  255. priv->i2c_addr_slvx : priv->i2c_addr_slvt);
  256. struct i2c_msg msg[2] = {
  257. {
  258. .addr = i2c_addr,
  259. .flags = 0,
  260. .len = 1,
  261. .buf = &reg,
  262. }, {
  263. .addr = i2c_addr,
  264. .flags = I2C_M_RD,
  265. .len = len,
  266. .buf = val,
  267. }
  268. };
  269. ret = i2c_transfer(priv->i2c, &msg[0], 1);
  270. if (ret >= 0 && ret != 1)
  271. ret = -EIO;
  272. if (ret < 0) {
  273. dev_warn(&priv->i2c->dev,
  274. "%s: i2c rw failed=%d addr=%02x reg=%02x\n",
  275. KBUILD_MODNAME, ret, i2c_addr, reg);
  276. return ret;
  277. }
  278. ret = i2c_transfer(priv->i2c, &msg[1], 1);
  279. if (ret >= 0 && ret != 1)
  280. ret = -EIO;
  281. if (ret < 0) {
  282. dev_warn(&priv->i2c->dev,
  283. "%s: i2c rd failed=%d addr=%02x reg=%02x\n",
  284. KBUILD_MODNAME, ret, i2c_addr, reg);
  285. return ret;
  286. }
  287. cxd2841er_i2c_debug(priv, i2c_addr, reg, 0, val, len);
  288. return 0;
  289. }
  290. static int cxd2841er_read_reg(struct cxd2841er_priv *priv,
  291. u8 addr, u8 reg, u8 *val)
  292. {
  293. return cxd2841er_read_regs(priv, addr, reg, val, 1);
  294. }
  295. static int cxd2841er_set_reg_bits(struct cxd2841er_priv *priv,
  296. u8 addr, u8 reg, u8 data, u8 mask)
  297. {
  298. int res;
  299. u8 rdata;
  300. if (mask != 0xff) {
  301. res = cxd2841er_read_reg(priv, addr, reg, &rdata);
  302. if (res)
  303. return res;
  304. data = ((data & mask) | (rdata & (mask ^ 0xFF)));
  305. }
  306. return cxd2841er_write_reg(priv, addr, reg, data);
  307. }
  308. static int cxd2841er_dvbs2_set_symbol_rate(struct cxd2841er_priv *priv,
  309. u32 symbol_rate)
  310. {
  311. u32 reg_value = 0;
  312. u8 data[3] = {0, 0, 0};
  313. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  314. /*
  315. * regValue = (symbolRateKSps * 2^14 / 1000) + 0.5
  316. * = ((symbolRateKSps * 2^14) + 500) / 1000
  317. * = ((symbolRateKSps * 16384) + 500) / 1000
  318. */
  319. reg_value = DIV_ROUND_CLOSEST(symbol_rate * 16384, 1000);
  320. if ((reg_value == 0) || (reg_value > 0xFFFFF)) {
  321. dev_err(&priv->i2c->dev,
  322. "%s(): reg_value is out of range\n", __func__);
  323. return -EINVAL;
  324. }
  325. data[0] = (u8)((reg_value >> 16) & 0x0F);
  326. data[1] = (u8)((reg_value >> 8) & 0xFF);
  327. data[2] = (u8)(reg_value & 0xFF);
  328. /* Set SLV-T Bank : 0xAE */
  329. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
  330. cxd2841er_write_regs(priv, I2C_SLVT, 0x20, data, 3);
  331. return 0;
  332. }
  333. static void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
  334. u8 system);
  335. static int cxd2841er_sleep_s_to_active_s(struct cxd2841er_priv *priv,
  336. u8 system, u32 symbol_rate)
  337. {
  338. int ret;
  339. u8 data[4] = { 0, 0, 0, 0 };
  340. if (priv->state != STATE_SLEEP_S) {
  341. dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
  342. __func__, (int)priv->state);
  343. return -EINVAL;
  344. }
  345. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  346. cxd2841er_set_ts_clock_mode(priv, SYS_DVBS);
  347. /* Set demod mode */
  348. if (system == SYS_DVBS) {
  349. data[0] = 0x0A;
  350. } else if (system == SYS_DVBS2) {
  351. data[0] = 0x0B;
  352. } else {
  353. dev_err(&priv->i2c->dev, "%s(): invalid delsys %d\n",
  354. __func__, system);
  355. return -EINVAL;
  356. }
  357. /* Set SLV-X Bank : 0x00 */
  358. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  359. cxd2841er_write_reg(priv, I2C_SLVX, 0x17, data[0]);
  360. /* DVB-S/S2 */
  361. data[0] = 0x00;
  362. /* Set SLV-T Bank : 0x00 */
  363. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  364. /* Enable S/S2 auto detection 1 */
  365. cxd2841er_write_reg(priv, I2C_SLVT, 0x2d, data[0]);
  366. /* Set SLV-T Bank : 0xAE */
  367. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
  368. /* Enable S/S2 auto detection 2 */
  369. cxd2841er_write_reg(priv, I2C_SLVT, 0x30, data[0]);
  370. /* Set SLV-T Bank : 0x00 */
  371. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  372. /* Enable demod clock */
  373. cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
  374. /* Enable ADC clock */
  375. cxd2841er_write_reg(priv, I2C_SLVT, 0x31, 0x01);
  376. /* Enable ADC 1 */
  377. cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
  378. /* Enable ADC 2 */
  379. cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x3f);
  380. /* Set SLV-X Bank : 0x00 */
  381. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  382. /* Enable ADC 3 */
  383. cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
  384. /* Set SLV-T Bank : 0xA3 */
  385. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa3);
  386. cxd2841er_write_reg(priv, I2C_SLVT, 0xac, 0x00);
  387. data[0] = 0x07;
  388. data[1] = 0x3B;
  389. data[2] = 0x08;
  390. data[3] = 0xC5;
  391. /* Set SLV-T Bank : 0xAB */
  392. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xab);
  393. cxd2841er_write_regs(priv, I2C_SLVT, 0x98, data, 4);
  394. data[0] = 0x05;
  395. data[1] = 0x80;
  396. data[2] = 0x0A;
  397. data[3] = 0x80;
  398. cxd2841er_write_regs(priv, I2C_SLVT, 0xa8, data, 4);
  399. data[0] = 0x0C;
  400. data[1] = 0xCC;
  401. cxd2841er_write_regs(priv, I2C_SLVT, 0xc3, data, 2);
  402. /* Set demod parameter */
  403. ret = cxd2841er_dvbs2_set_symbol_rate(priv, symbol_rate);
  404. if (ret != 0)
  405. return ret;
  406. /* Set SLV-T Bank : 0x00 */
  407. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  408. /* disable Hi-Z setting 1 */
  409. cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x10);
  410. /* disable Hi-Z setting 2 */
  411. cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
  412. priv->state = STATE_ACTIVE_S;
  413. return 0;
  414. }
  415. static int cxd2841er_sleep_tc_to_active_t_band(struct cxd2841er_priv *priv,
  416. u32 bandwidth);
  417. static int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
  418. u32 bandwidth);
  419. static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
  420. u32 bandwidth);
  421. static int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
  422. u32 bandwidth);
  423. static int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv);
  424. static int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv);
  425. static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv);
  426. static int cxd2841er_retune_active(struct cxd2841er_priv *priv,
  427. struct dtv_frontend_properties *p)
  428. {
  429. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  430. if (priv->state != STATE_ACTIVE_S &&
  431. priv->state != STATE_ACTIVE_TC) {
  432. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  433. __func__, priv->state);
  434. return -EINVAL;
  435. }
  436. /* Set SLV-T Bank : 0x00 */
  437. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  438. /* disable TS output */
  439. cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
  440. if (priv->state == STATE_ACTIVE_S)
  441. return cxd2841er_dvbs2_set_symbol_rate(
  442. priv, p->symbol_rate / 1000);
  443. else if (priv->state == STATE_ACTIVE_TC) {
  444. switch (priv->system) {
  445. case SYS_DVBT:
  446. return cxd2841er_sleep_tc_to_active_t_band(
  447. priv, p->bandwidth_hz);
  448. case SYS_DVBT2:
  449. return cxd2841er_sleep_tc_to_active_t2_band(
  450. priv, p->bandwidth_hz);
  451. case SYS_DVBC_ANNEX_A:
  452. return cxd2841er_sleep_tc_to_active_c_band(
  453. priv, p->bandwidth_hz);
  454. case SYS_ISDBT:
  455. cxd2841er_active_i_to_sleep_tc(priv);
  456. cxd2841er_sleep_tc_to_shutdown(priv);
  457. cxd2841er_shutdown_to_sleep_tc(priv);
  458. return cxd2841er_sleep_tc_to_active_i(
  459. priv, p->bandwidth_hz);
  460. }
  461. }
  462. dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
  463. __func__, priv->system);
  464. return -EINVAL;
  465. }
  466. static int cxd2841er_active_s_to_sleep_s(struct cxd2841er_priv *priv)
  467. {
  468. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  469. if (priv->state != STATE_ACTIVE_S) {
  470. dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
  471. __func__, priv->state);
  472. return -EINVAL;
  473. }
  474. /* Set SLV-T Bank : 0x00 */
  475. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  476. /* disable TS output */
  477. cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
  478. /* enable Hi-Z setting 1 */
  479. cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x1f);
  480. /* enable Hi-Z setting 2 */
  481. cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
  482. /* Set SLV-X Bank : 0x00 */
  483. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  484. /* disable ADC 1 */
  485. cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
  486. /* Set SLV-T Bank : 0x00 */
  487. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  488. /* disable ADC clock */
  489. cxd2841er_write_reg(priv, I2C_SLVT, 0x31, 0x00);
  490. /* disable ADC 2 */
  491. cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
  492. /* disable ADC 3 */
  493. cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
  494. /* SADC Bias ON */
  495. cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
  496. /* disable demod clock */
  497. cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
  498. /* Set SLV-T Bank : 0xAE */
  499. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
  500. /* disable S/S2 auto detection1 */
  501. cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
  502. /* Set SLV-T Bank : 0x00 */
  503. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  504. /* disable S/S2 auto detection2 */
  505. cxd2841er_write_reg(priv, I2C_SLVT, 0x2d, 0x00);
  506. priv->state = STATE_SLEEP_S;
  507. return 0;
  508. }
  509. static int cxd2841er_sleep_s_to_shutdown(struct cxd2841er_priv *priv)
  510. {
  511. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  512. if (priv->state != STATE_SLEEP_S) {
  513. dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
  514. __func__, priv->state);
  515. return -EINVAL;
  516. }
  517. /* Set SLV-T Bank : 0x00 */
  518. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  519. /* Disable DSQOUT */
  520. cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
  521. /* Disable DSQIN */
  522. cxd2841er_write_reg(priv, I2C_SLVT, 0x9c, 0x00);
  523. /* Set SLV-X Bank : 0x00 */
  524. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  525. /* Disable oscillator */
  526. cxd2841er_write_reg(priv, I2C_SLVX, 0x15, 0x01);
  527. /* Set demod mode */
  528. cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
  529. priv->state = STATE_SHUTDOWN;
  530. return 0;
  531. }
  532. static int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv)
  533. {
  534. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  535. if (priv->state != STATE_SLEEP_TC) {
  536. dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
  537. __func__, priv->state);
  538. return -EINVAL;
  539. }
  540. /* Set SLV-X Bank : 0x00 */
  541. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  542. /* Disable oscillator */
  543. cxd2841er_write_reg(priv, I2C_SLVX, 0x15, 0x01);
  544. /* Set demod mode */
  545. cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
  546. priv->state = STATE_SHUTDOWN;
  547. return 0;
  548. }
  549. static int cxd2841er_active_t_to_sleep_tc(struct cxd2841er_priv *priv)
  550. {
  551. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  552. if (priv->state != STATE_ACTIVE_TC) {
  553. dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
  554. __func__, priv->state);
  555. return -EINVAL;
  556. }
  557. /* Set SLV-T Bank : 0x00 */
  558. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  559. /* disable TS output */
  560. cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
  561. /* enable Hi-Z setting 1 */
  562. cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
  563. /* enable Hi-Z setting 2 */
  564. cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
  565. /* Set SLV-X Bank : 0x00 */
  566. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  567. /* disable ADC 1 */
  568. cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
  569. /* Set SLV-T Bank : 0x00 */
  570. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  571. /* Disable ADC 2 */
  572. cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
  573. /* Disable ADC 3 */
  574. cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
  575. /* Disable ADC clock */
  576. cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
  577. /* Disable RF level monitor */
  578. cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
  579. /* Disable demod clock */
  580. cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
  581. priv->state = STATE_SLEEP_TC;
  582. return 0;
  583. }
  584. static int cxd2841er_active_t2_to_sleep_tc(struct cxd2841er_priv *priv)
  585. {
  586. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  587. if (priv->state != STATE_ACTIVE_TC) {
  588. dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
  589. __func__, priv->state);
  590. return -EINVAL;
  591. }
  592. /* Set SLV-T Bank : 0x00 */
  593. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  594. /* disable TS output */
  595. cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
  596. /* enable Hi-Z setting 1 */
  597. cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
  598. /* enable Hi-Z setting 2 */
  599. cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
  600. /* Cancel DVB-T2 setting */
  601. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
  602. cxd2841er_write_reg(priv, I2C_SLVT, 0x83, 0x40);
  603. cxd2841er_write_reg(priv, I2C_SLVT, 0x86, 0x21);
  604. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9e, 0x09, 0x0f);
  605. cxd2841er_write_reg(priv, I2C_SLVT, 0x9f, 0xfb);
  606. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2a);
  607. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x38, 0x00, 0x0f);
  608. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
  609. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x00, 0x3f);
  610. /* Set SLV-X Bank : 0x00 */
  611. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  612. /* disable ADC 1 */
  613. cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
  614. /* Set SLV-T Bank : 0x00 */
  615. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  616. /* Disable ADC 2 */
  617. cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
  618. /* Disable ADC 3 */
  619. cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
  620. /* Disable ADC clock */
  621. cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
  622. /* Disable RF level monitor */
  623. cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
  624. /* Disable demod clock */
  625. cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
  626. priv->state = STATE_SLEEP_TC;
  627. return 0;
  628. }
  629. static int cxd2841er_active_c_to_sleep_tc(struct cxd2841er_priv *priv)
  630. {
  631. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  632. if (priv->state != STATE_ACTIVE_TC) {
  633. dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
  634. __func__, priv->state);
  635. return -EINVAL;
  636. }
  637. /* Set SLV-T Bank : 0x00 */
  638. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  639. /* disable TS output */
  640. cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
  641. /* enable Hi-Z setting 1 */
  642. cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
  643. /* enable Hi-Z setting 2 */
  644. cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
  645. /* Cancel DVB-C setting */
  646. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
  647. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa3, 0x00, 0x1f);
  648. /* Set SLV-X Bank : 0x00 */
  649. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  650. /* disable ADC 1 */
  651. cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
  652. /* Set SLV-T Bank : 0x00 */
  653. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  654. /* Disable ADC 2 */
  655. cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
  656. /* Disable ADC 3 */
  657. cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
  658. /* Disable ADC clock */
  659. cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
  660. /* Disable RF level monitor */
  661. cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
  662. /* Disable demod clock */
  663. cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
  664. priv->state = STATE_SLEEP_TC;
  665. return 0;
  666. }
  667. static int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv)
  668. {
  669. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  670. if (priv->state != STATE_ACTIVE_TC) {
  671. dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
  672. __func__, priv->state);
  673. return -EINVAL;
  674. }
  675. /* Set SLV-T Bank : 0x00 */
  676. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  677. /* disable TS output */
  678. cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
  679. /* enable Hi-Z setting 1 */
  680. cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
  681. /* enable Hi-Z setting 2 */
  682. cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
  683. /* TODO: Cancel demod parameter */
  684. /* Set SLV-X Bank : 0x00 */
  685. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  686. /* disable ADC 1 */
  687. cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
  688. /* Set SLV-T Bank : 0x00 */
  689. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  690. /* Disable ADC 2 */
  691. cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
  692. /* Disable ADC 3 */
  693. cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
  694. /* Disable ADC clock */
  695. cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
  696. /* Disable RF level monitor */
  697. cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
  698. /* Disable demod clock */
  699. cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
  700. priv->state = STATE_SLEEP_TC;
  701. return 0;
  702. }
  703. static int cxd2841er_shutdown_to_sleep_s(struct cxd2841er_priv *priv)
  704. {
  705. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  706. if (priv->state != STATE_SHUTDOWN) {
  707. dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
  708. __func__, priv->state);
  709. return -EINVAL;
  710. }
  711. /* Set SLV-X Bank : 0x00 */
  712. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  713. /* Clear all demodulator registers */
  714. cxd2841er_write_reg(priv, I2C_SLVX, 0x02, 0x00);
  715. usleep_range(3000, 5000);
  716. /* Set SLV-X Bank : 0x00 */
  717. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  718. /* Set demod SW reset */
  719. cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
  720. switch (priv->xtal) {
  721. case SONY_XTAL_20500:
  722. cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x00);
  723. break;
  724. case SONY_XTAL_24000:
  725. /* Select demod frequency */
  726. cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
  727. cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x03);
  728. break;
  729. case SONY_XTAL_41000:
  730. cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x01);
  731. break;
  732. default:
  733. dev_dbg(&priv->i2c->dev, "%s(): invalid demod xtal %d\n",
  734. __func__, priv->xtal);
  735. return -EINVAL;
  736. }
  737. /* Set demod mode */
  738. cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x0a);
  739. /* Clear demod SW reset */
  740. cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
  741. usleep_range(1000, 2000);
  742. /* Set SLV-T Bank : 0x00 */
  743. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  744. /* enable DSQOUT */
  745. cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x1F);
  746. /* enable DSQIN */
  747. cxd2841er_write_reg(priv, I2C_SLVT, 0x9C, 0x40);
  748. /* TADC Bias On */
  749. cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
  750. cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
  751. /* SADC Bias On */
  752. cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
  753. cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
  754. cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
  755. priv->state = STATE_SLEEP_S;
  756. return 0;
  757. }
  758. static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv)
  759. {
  760. u8 data = 0;
  761. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  762. if (priv->state != STATE_SHUTDOWN) {
  763. dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
  764. __func__, priv->state);
  765. return -EINVAL;
  766. }
  767. /* Set SLV-X Bank : 0x00 */
  768. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  769. /* Clear all demodulator registers */
  770. cxd2841er_write_reg(priv, I2C_SLVX, 0x02, 0x00);
  771. usleep_range(3000, 5000);
  772. /* Set SLV-X Bank : 0x00 */
  773. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  774. /* Set demod SW reset */
  775. cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
  776. /* Select ADC clock mode */
  777. cxd2841er_write_reg(priv, I2C_SLVX, 0x13, 0x00);
  778. switch (priv->xtal) {
  779. case SONY_XTAL_20500:
  780. data = 0x0;
  781. break;
  782. case SONY_XTAL_24000:
  783. /* Select demod frequency */
  784. cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
  785. data = 0x3;
  786. break;
  787. case SONY_XTAL_41000:
  788. cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
  789. data = 0x1;
  790. break;
  791. }
  792. cxd2841er_write_reg(priv, I2C_SLVX, 0x14, data);
  793. /* Clear demod SW reset */
  794. cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
  795. usleep_range(1000, 2000);
  796. /* Set SLV-T Bank : 0x00 */
  797. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  798. /* TADC Bias On */
  799. cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
  800. cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
  801. /* SADC Bias On */
  802. cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
  803. cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
  804. cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
  805. priv->state = STATE_SLEEP_TC;
  806. return 0;
  807. }
  808. static int cxd2841er_tune_done(struct cxd2841er_priv *priv)
  809. {
  810. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  811. /* Set SLV-T Bank : 0x00 */
  812. cxd2841er_write_reg(priv, I2C_SLVT, 0, 0);
  813. /* SW Reset */
  814. cxd2841er_write_reg(priv, I2C_SLVT, 0xfe, 0x01);
  815. /* Enable TS output */
  816. cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x00);
  817. return 0;
  818. }
  819. /* Set TS parallel mode */
  820. static void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
  821. u8 system)
  822. {
  823. u8 serial_ts, ts_rate_ctrl_off, ts_in_off;
  824. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  825. /* Set SLV-T Bank : 0x00 */
  826. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  827. cxd2841er_read_reg(priv, I2C_SLVT, 0xc4, &serial_ts);
  828. cxd2841er_read_reg(priv, I2C_SLVT, 0xd3, &ts_rate_ctrl_off);
  829. cxd2841er_read_reg(priv, I2C_SLVT, 0xde, &ts_in_off);
  830. dev_dbg(&priv->i2c->dev, "%s(): ser_ts=0x%02x rate_ctrl_off=0x%02x in_off=0x%02x\n",
  831. __func__, serial_ts, ts_rate_ctrl_off, ts_in_off);
  832. /*
  833. * slave Bank Addr Bit default Name
  834. * <SLV-T> 00h D9h [7:0] 8'h08 OTSCKPERIOD
  835. */
  836. cxd2841er_write_reg(priv, I2C_SLVT, 0xd9, 0x08);
  837. /*
  838. * Disable TS IF Clock
  839. * slave Bank Addr Bit default Name
  840. * <SLV-T> 00h 32h [0] 1'b1 OREG_CK_TSIF_EN
  841. */
  842. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x32, 0x00, 0x01);
  843. /*
  844. * slave Bank Addr Bit default Name
  845. * <SLV-T> 00h 33h [1:0] 2'b01 OREG_CKSEL_TSIF
  846. */
  847. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x33, 0x00, 0x03);
  848. /*
  849. * Enable TS IF Clock
  850. * slave Bank Addr Bit default Name
  851. * <SLV-T> 00h 32h [0] 1'b1 OREG_CK_TSIF_EN
  852. */
  853. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x32, 0x01, 0x01);
  854. if (system == SYS_DVBT) {
  855. /* Enable parity period for DVB-T */
  856. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  857. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x66, 0x01, 0x01);
  858. } else if (system == SYS_DVBC_ANNEX_A) {
  859. /* Enable parity period for DVB-C */
  860. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
  861. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x66, 0x01, 0x01);
  862. }
  863. }
  864. static u8 cxd2841er_chip_id(struct cxd2841er_priv *priv)
  865. {
  866. u8 chip_id = 0;
  867. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  868. if (cxd2841er_write_reg(priv, I2C_SLVT, 0, 0) == 0)
  869. cxd2841er_read_reg(priv, I2C_SLVT, 0xfd, &chip_id);
  870. else if (cxd2841er_write_reg(priv, I2C_SLVX, 0, 0) == 0)
  871. cxd2841er_read_reg(priv, I2C_SLVX, 0xfd, &chip_id);
  872. return chip_id;
  873. }
  874. static int cxd2841er_read_status_s(struct dvb_frontend *fe,
  875. enum fe_status *status)
  876. {
  877. u8 reg = 0;
  878. struct cxd2841er_priv *priv = fe->demodulator_priv;
  879. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  880. *status = 0;
  881. if (priv->state != STATE_ACTIVE_S) {
  882. dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
  883. __func__, priv->state);
  884. return -EINVAL;
  885. }
  886. /* Set SLV-T Bank : 0xA0 */
  887. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
  888. /*
  889. * slave Bank Addr Bit Signal name
  890. * <SLV-T> A0h 11h [2] ITSLOCK
  891. */
  892. cxd2841er_read_reg(priv, I2C_SLVT, 0x11, &reg);
  893. if (reg & 0x04) {
  894. *status = FE_HAS_SIGNAL
  895. | FE_HAS_CARRIER
  896. | FE_HAS_VITERBI
  897. | FE_HAS_SYNC
  898. | FE_HAS_LOCK;
  899. }
  900. dev_dbg(&priv->i2c->dev, "%s(): result 0x%x\n", __func__, *status);
  901. return 0;
  902. }
  903. static int cxd2841er_read_status_t_t2(struct cxd2841er_priv *priv,
  904. u8 *sync, u8 *tslock, u8 *unlock)
  905. {
  906. u8 data = 0;
  907. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  908. if (priv->state != STATE_ACTIVE_TC)
  909. return -EINVAL;
  910. if (priv->system == SYS_DVBT) {
  911. /* Set SLV-T Bank : 0x10 */
  912. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  913. } else {
  914. /* Set SLV-T Bank : 0x20 */
  915. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
  916. }
  917. cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
  918. if ((data & 0x07) == 0x07) {
  919. dev_dbg(&priv->i2c->dev,
  920. "%s(): invalid hardware state detected\n", __func__);
  921. *sync = 0;
  922. *tslock = 0;
  923. *unlock = 0;
  924. } else {
  925. *sync = ((data & 0x07) == 0x6 ? 1 : 0);
  926. *tslock = ((data & 0x20) ? 1 : 0);
  927. *unlock = ((data & 0x10) ? 1 : 0);
  928. }
  929. return 0;
  930. }
  931. static int cxd2841er_read_status_c(struct cxd2841er_priv *priv, u8 *tslock)
  932. {
  933. u8 data;
  934. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  935. if (priv->state != STATE_ACTIVE_TC)
  936. return -EINVAL;
  937. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
  938. cxd2841er_read_reg(priv, I2C_SLVT, 0x88, &data);
  939. if ((data & 0x01) == 0) {
  940. *tslock = 0;
  941. } else {
  942. cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
  943. *tslock = ((data & 0x20) ? 1 : 0);
  944. }
  945. return 0;
  946. }
  947. static int cxd2841er_read_status_i(struct cxd2841er_priv *priv,
  948. u8 *sync, u8 *tslock, u8 *unlock)
  949. {
  950. u8 data = 0;
  951. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  952. if (priv->state != STATE_ACTIVE_TC)
  953. return -EINVAL;
  954. /* Set SLV-T Bank : 0x60 */
  955. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
  956. cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
  957. dev_dbg(&priv->i2c->dev,
  958. "%s(): lock=0x%x\n", __func__, data);
  959. *sync = ((data & 0x02) ? 1 : 0);
  960. *tslock = ((data & 0x01) ? 1 : 0);
  961. *unlock = ((data & 0x10) ? 1 : 0);
  962. return 0;
  963. }
  964. static int cxd2841er_read_status_tc(struct dvb_frontend *fe,
  965. enum fe_status *status)
  966. {
  967. int ret = 0;
  968. u8 sync = 0;
  969. u8 tslock = 0;
  970. u8 unlock = 0;
  971. struct cxd2841er_priv *priv = fe->demodulator_priv;
  972. *status = 0;
  973. if (priv->state == STATE_ACTIVE_TC) {
  974. if (priv->system == SYS_DVBT || priv->system == SYS_DVBT2) {
  975. ret = cxd2841er_read_status_t_t2(
  976. priv, &sync, &tslock, &unlock);
  977. if (ret)
  978. goto done;
  979. if (unlock)
  980. goto done;
  981. if (sync)
  982. *status = FE_HAS_SIGNAL |
  983. FE_HAS_CARRIER |
  984. FE_HAS_VITERBI |
  985. FE_HAS_SYNC;
  986. if (tslock)
  987. *status |= FE_HAS_LOCK;
  988. } else if (priv->system == SYS_ISDBT) {
  989. ret = cxd2841er_read_status_i(
  990. priv, &sync, &tslock, &unlock);
  991. if (ret)
  992. goto done;
  993. if (unlock)
  994. goto done;
  995. if (sync)
  996. *status = FE_HAS_SIGNAL |
  997. FE_HAS_CARRIER |
  998. FE_HAS_VITERBI |
  999. FE_HAS_SYNC;
  1000. if (tslock)
  1001. *status |= FE_HAS_LOCK;
  1002. } else if (priv->system == SYS_DVBC_ANNEX_A) {
  1003. ret = cxd2841er_read_status_c(priv, &tslock);
  1004. if (ret)
  1005. goto done;
  1006. if (tslock)
  1007. *status = FE_HAS_SIGNAL |
  1008. FE_HAS_CARRIER |
  1009. FE_HAS_VITERBI |
  1010. FE_HAS_SYNC |
  1011. FE_HAS_LOCK;
  1012. }
  1013. }
  1014. done:
  1015. dev_dbg(&priv->i2c->dev, "%s(): status 0x%x\n", __func__, *status);
  1016. return ret;
  1017. }
  1018. static int cxd2841er_get_carrier_offset_s_s2(struct cxd2841er_priv *priv,
  1019. int *offset)
  1020. {
  1021. u8 data[3];
  1022. u8 is_hs_mode;
  1023. s32 cfrl_ctrlval;
  1024. s32 temp_div, temp_q, temp_r;
  1025. if (priv->state != STATE_ACTIVE_S) {
  1026. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  1027. __func__, priv->state);
  1028. return -EINVAL;
  1029. }
  1030. /*
  1031. * Get High Sampling Rate mode
  1032. * slave Bank Addr Bit Signal name
  1033. * <SLV-T> A0h 10h [0] ITRL_LOCK
  1034. */
  1035. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
  1036. cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data[0]);
  1037. if (data[0] & 0x01) {
  1038. /*
  1039. * slave Bank Addr Bit Signal name
  1040. * <SLV-T> A0h 50h [4] IHSMODE
  1041. */
  1042. cxd2841er_read_reg(priv, I2C_SLVT, 0x50, &data[0]);
  1043. is_hs_mode = (data[0] & 0x10 ? 1 : 0);
  1044. } else {
  1045. dev_dbg(&priv->i2c->dev,
  1046. "%s(): unable to detect sampling rate mode\n",
  1047. __func__);
  1048. return -EINVAL;
  1049. }
  1050. /*
  1051. * slave Bank Addr Bit Signal name
  1052. * <SLV-T> A0h 45h [4:0] ICFRL_CTRLVAL[20:16]
  1053. * <SLV-T> A0h 46h [7:0] ICFRL_CTRLVAL[15:8]
  1054. * <SLV-T> A0h 47h [7:0] ICFRL_CTRLVAL[7:0]
  1055. */
  1056. cxd2841er_read_regs(priv, I2C_SLVT, 0x45, data, 3);
  1057. cfrl_ctrlval = sign_extend32((((u32)data[0] & 0x1F) << 16) |
  1058. (((u32)data[1] & 0xFF) << 8) |
  1059. ((u32)data[2] & 0xFF), 20);
  1060. temp_div = (is_hs_mode ? 1048576 : 1572864);
  1061. if (cfrl_ctrlval > 0) {
  1062. temp_q = div_s64_rem(97375LL * cfrl_ctrlval,
  1063. temp_div, &temp_r);
  1064. } else {
  1065. temp_q = div_s64_rem(-97375LL * cfrl_ctrlval,
  1066. temp_div, &temp_r);
  1067. }
  1068. if (temp_r >= temp_div / 2)
  1069. temp_q++;
  1070. if (cfrl_ctrlval > 0)
  1071. temp_q *= -1;
  1072. *offset = temp_q;
  1073. return 0;
  1074. }
  1075. static int cxd2841er_get_carrier_offset_i(struct cxd2841er_priv *priv,
  1076. u32 bandwidth, int *offset)
  1077. {
  1078. u8 data[4];
  1079. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  1080. if (priv->state != STATE_ACTIVE_TC) {
  1081. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  1082. __func__, priv->state);
  1083. return -EINVAL;
  1084. }
  1085. if (priv->system != SYS_ISDBT) {
  1086. dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
  1087. __func__, priv->system);
  1088. return -EINVAL;
  1089. }
  1090. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
  1091. cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
  1092. *offset = -1 * sign_extend32(
  1093. ((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
  1094. ((u32)data[2] << 8) | (u32)data[3], 29);
  1095. switch (bandwidth) {
  1096. case 6000000:
  1097. *offset = -1 * ((*offset) * 8/264);
  1098. break;
  1099. case 7000000:
  1100. *offset = -1 * ((*offset) * 8/231);
  1101. break;
  1102. case 8000000:
  1103. *offset = -1 * ((*offset) * 8/198);
  1104. break;
  1105. default:
  1106. dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
  1107. __func__, bandwidth);
  1108. return -EINVAL;
  1109. }
  1110. dev_dbg(&priv->i2c->dev, "%s(): bandwidth %d offset %d\n",
  1111. __func__, bandwidth, *offset);
  1112. return 0;
  1113. }
  1114. static int cxd2841er_get_carrier_offset_t(struct cxd2841er_priv *priv,
  1115. u32 bandwidth, int *offset)
  1116. {
  1117. u8 data[4];
  1118. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  1119. if (priv->state != STATE_ACTIVE_TC) {
  1120. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  1121. __func__, priv->state);
  1122. return -EINVAL;
  1123. }
  1124. if (priv->system != SYS_DVBT) {
  1125. dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
  1126. __func__, priv->system);
  1127. return -EINVAL;
  1128. }
  1129. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  1130. cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
  1131. *offset = -1 * sign_extend32(
  1132. ((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
  1133. ((u32)data[2] << 8) | (u32)data[3], 29);
  1134. *offset *= (bandwidth / 1000000);
  1135. *offset /= 235;
  1136. return 0;
  1137. }
  1138. static int cxd2841er_get_carrier_offset_t2(struct cxd2841er_priv *priv,
  1139. u32 bandwidth, int *offset)
  1140. {
  1141. u8 data[4];
  1142. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  1143. if (priv->state != STATE_ACTIVE_TC) {
  1144. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  1145. __func__, priv->state);
  1146. return -EINVAL;
  1147. }
  1148. if (priv->system != SYS_DVBT2) {
  1149. dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
  1150. __func__, priv->system);
  1151. return -EINVAL;
  1152. }
  1153. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
  1154. cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
  1155. *offset = -1 * sign_extend32(
  1156. ((u32)(data[0] & 0x0F) << 24) | ((u32)data[1] << 16) |
  1157. ((u32)data[2] << 8) | (u32)data[3], 27);
  1158. switch (bandwidth) {
  1159. case 1712000:
  1160. *offset /= 582;
  1161. break;
  1162. case 5000000:
  1163. case 6000000:
  1164. case 7000000:
  1165. case 8000000:
  1166. *offset *= (bandwidth / 1000000);
  1167. *offset /= 940;
  1168. break;
  1169. default:
  1170. dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
  1171. __func__, bandwidth);
  1172. return -EINVAL;
  1173. }
  1174. return 0;
  1175. }
  1176. static int cxd2841er_get_carrier_offset_c(struct cxd2841er_priv *priv,
  1177. int *offset)
  1178. {
  1179. u8 data[2];
  1180. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  1181. if (priv->state != STATE_ACTIVE_TC) {
  1182. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  1183. __func__, priv->state);
  1184. return -EINVAL;
  1185. }
  1186. if (priv->system != SYS_DVBC_ANNEX_A) {
  1187. dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
  1188. __func__, priv->system);
  1189. return -EINVAL;
  1190. }
  1191. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
  1192. cxd2841er_read_regs(priv, I2C_SLVT, 0x15, data, sizeof(data));
  1193. *offset = div_s64(41000LL * sign_extend32((((u32)data[0] & 0x3f) << 8)
  1194. | (u32)data[1], 13), 16384);
  1195. return 0;
  1196. }
  1197. static int cxd2841er_read_packet_errors_c(
  1198. struct cxd2841er_priv *priv, u32 *penum)
  1199. {
  1200. u8 data[3];
  1201. *penum = 0;
  1202. if (priv->state != STATE_ACTIVE_TC) {
  1203. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  1204. __func__, priv->state);
  1205. return -EINVAL;
  1206. }
  1207. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
  1208. cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
  1209. if (data[2] & 0x01)
  1210. *penum = ((u32)data[0] << 8) | (u32)data[1];
  1211. return 0;
  1212. }
  1213. static int cxd2841er_read_packet_errors_t(
  1214. struct cxd2841er_priv *priv, u32 *penum)
  1215. {
  1216. u8 data[3];
  1217. *penum = 0;
  1218. if (priv->state != STATE_ACTIVE_TC) {
  1219. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  1220. __func__, priv->state);
  1221. return -EINVAL;
  1222. }
  1223. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  1224. cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
  1225. if (data[2] & 0x01)
  1226. *penum = ((u32)data[0] << 8) | (u32)data[1];
  1227. return 0;
  1228. }
  1229. static int cxd2841er_read_packet_errors_t2(
  1230. struct cxd2841er_priv *priv, u32 *penum)
  1231. {
  1232. u8 data[3];
  1233. *penum = 0;
  1234. if (priv->state != STATE_ACTIVE_TC) {
  1235. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  1236. __func__, priv->state);
  1237. return -EINVAL;
  1238. }
  1239. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
  1240. cxd2841er_read_regs(priv, I2C_SLVT, 0xfd, data, sizeof(data));
  1241. if (data[0] & 0x01)
  1242. *penum = ((u32)data[1] << 8) | (u32)data[2];
  1243. return 0;
  1244. }
  1245. static int cxd2841er_read_packet_errors_i(
  1246. struct cxd2841er_priv *priv, u32 *penum)
  1247. {
  1248. u8 data[2];
  1249. *penum = 0;
  1250. if (priv->state != STATE_ACTIVE_TC) {
  1251. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  1252. __func__, priv->state);
  1253. return -EINVAL;
  1254. }
  1255. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
  1256. cxd2841er_read_regs(priv, I2C_SLVT, 0xA1, data, 1);
  1257. if (!(data[0] & 0x01))
  1258. return 0;
  1259. /* Layer A */
  1260. cxd2841er_read_regs(priv, I2C_SLVT, 0xA2, data, sizeof(data));
  1261. *penum = ((u32)data[0] << 8) | (u32)data[1];
  1262. /* Layer B */
  1263. cxd2841er_read_regs(priv, I2C_SLVT, 0xA4, data, sizeof(data));
  1264. *penum += ((u32)data[0] << 8) | (u32)data[1];
  1265. /* Layer C */
  1266. cxd2841er_read_regs(priv, I2C_SLVT, 0xA6, data, sizeof(data));
  1267. *penum += ((u32)data[0] << 8) | (u32)data[1];
  1268. return 0;
  1269. }
  1270. static int cxd2841er_read_ber_c(struct cxd2841er_priv *priv,
  1271. u32 *bit_error, u32 *bit_count)
  1272. {
  1273. u8 data[3];
  1274. u32 bit_err, period_exp;
  1275. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  1276. if (priv->state != STATE_ACTIVE_TC) {
  1277. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  1278. __func__, priv->state);
  1279. return -EINVAL;
  1280. }
  1281. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
  1282. cxd2841er_read_regs(priv, I2C_SLVT, 0x62, data, sizeof(data));
  1283. if (!(data[0] & 0x80)) {
  1284. dev_dbg(&priv->i2c->dev,
  1285. "%s(): no valid BER data\n", __func__);
  1286. return -EINVAL;
  1287. }
  1288. bit_err = ((u32)(data[0] & 0x3f) << 16) |
  1289. ((u32)data[1] << 8) |
  1290. (u32)data[2];
  1291. cxd2841er_read_reg(priv, I2C_SLVT, 0x60, data);
  1292. period_exp = data[0] & 0x1f;
  1293. if ((period_exp <= 11) && (bit_err > (1 << period_exp) * 204 * 8)) {
  1294. dev_dbg(&priv->i2c->dev,
  1295. "%s(): period_exp(%u) or bit_err(%u) not in range. no valid BER data\n",
  1296. __func__, period_exp, bit_err);
  1297. return -EINVAL;
  1298. }
  1299. dev_dbg(&priv->i2c->dev,
  1300. "%s(): period_exp(%u) or bit_err(%u) count=%d\n",
  1301. __func__, period_exp, bit_err,
  1302. ((1 << period_exp) * 204 * 8));
  1303. *bit_error = bit_err;
  1304. *bit_count = ((1 << period_exp) * 204 * 8);
  1305. return 0;
  1306. }
  1307. static int cxd2841er_read_ber_i(struct cxd2841er_priv *priv,
  1308. u32 *bit_error, u32 *bit_count)
  1309. {
  1310. u8 data[3];
  1311. u8 pktnum[2];
  1312. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  1313. if (priv->state != STATE_ACTIVE_TC) {
  1314. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  1315. __func__, priv->state);
  1316. return -EINVAL;
  1317. }
  1318. cxd2841er_freeze_regs(priv);
  1319. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
  1320. cxd2841er_read_regs(priv, I2C_SLVT, 0x5B, pktnum, sizeof(pktnum));
  1321. cxd2841er_read_regs(priv, I2C_SLVT, 0x16, data, sizeof(data));
  1322. if (!pktnum[0] && !pktnum[1]) {
  1323. dev_dbg(&priv->i2c->dev,
  1324. "%s(): no valid BER data\n", __func__);
  1325. cxd2841er_unfreeze_regs(priv);
  1326. return -EINVAL;
  1327. }
  1328. *bit_error = ((u32)(data[0] & 0x7F) << 16) |
  1329. ((u32)data[1] << 8) | data[2];
  1330. *bit_count = ((((u32)pktnum[0] << 8) | pktnum[1]) * 204 * 8);
  1331. dev_dbg(&priv->i2c->dev, "%s(): bit_error=%u bit_count=%u\n",
  1332. __func__, *bit_error, *bit_count);
  1333. cxd2841er_unfreeze_regs(priv);
  1334. return 0;
  1335. }
  1336. static int cxd2841er_mon_read_ber_s(struct cxd2841er_priv *priv,
  1337. u32 *bit_error, u32 *bit_count)
  1338. {
  1339. u8 data[11];
  1340. /* Set SLV-T Bank : 0xA0 */
  1341. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
  1342. /*
  1343. * slave Bank Addr Bit Signal name
  1344. * <SLV-T> A0h 35h [0] IFVBER_VALID
  1345. * <SLV-T> A0h 36h [5:0] IFVBER_BITERR[21:16]
  1346. * <SLV-T> A0h 37h [7:0] IFVBER_BITERR[15:8]
  1347. * <SLV-T> A0h 38h [7:0] IFVBER_BITERR[7:0]
  1348. * <SLV-T> A0h 3Dh [5:0] IFVBER_BITNUM[21:16]
  1349. * <SLV-T> A0h 3Eh [7:0] IFVBER_BITNUM[15:8]
  1350. * <SLV-T> A0h 3Fh [7:0] IFVBER_BITNUM[7:0]
  1351. */
  1352. cxd2841er_read_regs(priv, I2C_SLVT, 0x35, data, 11);
  1353. if (data[0] & 0x01) {
  1354. *bit_error = ((u32)(data[1] & 0x3F) << 16) |
  1355. ((u32)(data[2] & 0xFF) << 8) |
  1356. (u32)(data[3] & 0xFF);
  1357. *bit_count = ((u32)(data[8] & 0x3F) << 16) |
  1358. ((u32)(data[9] & 0xFF) << 8) |
  1359. (u32)(data[10] & 0xFF);
  1360. if ((*bit_count == 0) || (*bit_error > *bit_count)) {
  1361. dev_dbg(&priv->i2c->dev,
  1362. "%s(): invalid bit_error %d, bit_count %d\n",
  1363. __func__, *bit_error, *bit_count);
  1364. return -EINVAL;
  1365. }
  1366. return 0;
  1367. }
  1368. dev_dbg(&priv->i2c->dev, "%s(): no data available\n", __func__);
  1369. return -EINVAL;
  1370. }
  1371. static int cxd2841er_mon_read_ber_s2(struct cxd2841er_priv *priv,
  1372. u32 *bit_error, u32 *bit_count)
  1373. {
  1374. u8 data[5];
  1375. u32 period;
  1376. /* Set SLV-T Bank : 0xB2 */
  1377. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xb2);
  1378. /*
  1379. * slave Bank Addr Bit Signal name
  1380. * <SLV-T> B2h 30h [0] IFLBER_VALID
  1381. * <SLV-T> B2h 31h [3:0] IFLBER_BITERR[27:24]
  1382. * <SLV-T> B2h 32h [7:0] IFLBER_BITERR[23:16]
  1383. * <SLV-T> B2h 33h [7:0] IFLBER_BITERR[15:8]
  1384. * <SLV-T> B2h 34h [7:0] IFLBER_BITERR[7:0]
  1385. */
  1386. cxd2841er_read_regs(priv, I2C_SLVT, 0x30, data, 5);
  1387. if (data[0] & 0x01) {
  1388. /* Bit error count */
  1389. *bit_error = ((u32)(data[1] & 0x0F) << 24) |
  1390. ((u32)(data[2] & 0xFF) << 16) |
  1391. ((u32)(data[3] & 0xFF) << 8) |
  1392. (u32)(data[4] & 0xFF);
  1393. /* Set SLV-T Bank : 0xA0 */
  1394. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
  1395. cxd2841er_read_reg(priv, I2C_SLVT, 0x7a, data);
  1396. /* Measurement period */
  1397. period = (u32)(1 << (data[0] & 0x0F));
  1398. if (period == 0) {
  1399. dev_dbg(&priv->i2c->dev,
  1400. "%s(): period is 0\n", __func__);
  1401. return -EINVAL;
  1402. }
  1403. if (*bit_error > (period * 64800)) {
  1404. dev_dbg(&priv->i2c->dev,
  1405. "%s(): invalid bit_err 0x%x period 0x%x\n",
  1406. __func__, *bit_error, period);
  1407. return -EINVAL;
  1408. }
  1409. *bit_count = period * 64800;
  1410. return 0;
  1411. } else {
  1412. dev_dbg(&priv->i2c->dev,
  1413. "%s(): no data available\n", __func__);
  1414. }
  1415. return -EINVAL;
  1416. }
  1417. static int cxd2841er_read_ber_t2(struct cxd2841er_priv *priv,
  1418. u32 *bit_error, u32 *bit_count)
  1419. {
  1420. u8 data[4];
  1421. u32 period_exp, n_ldpc;
  1422. if (priv->state != STATE_ACTIVE_TC) {
  1423. dev_dbg(&priv->i2c->dev,
  1424. "%s(): invalid state %d\n", __func__, priv->state);
  1425. return -EINVAL;
  1426. }
  1427. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
  1428. cxd2841er_read_regs(priv, I2C_SLVT, 0x39, data, sizeof(data));
  1429. if (!(data[0] & 0x10)) {
  1430. dev_dbg(&priv->i2c->dev,
  1431. "%s(): no valid BER data\n", __func__);
  1432. return -EINVAL;
  1433. }
  1434. *bit_error = ((u32)(data[0] & 0x0f) << 24) |
  1435. ((u32)data[1] << 16) |
  1436. ((u32)data[2] << 8) |
  1437. (u32)data[3];
  1438. cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
  1439. period_exp = data[0] & 0x0f;
  1440. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x22);
  1441. cxd2841er_read_reg(priv, I2C_SLVT, 0x5e, data);
  1442. n_ldpc = ((data[0] & 0x03) == 0 ? 16200 : 64800);
  1443. if (*bit_error > ((1U << period_exp) * n_ldpc)) {
  1444. dev_dbg(&priv->i2c->dev,
  1445. "%s(): invalid BER value\n", __func__);
  1446. return -EINVAL;
  1447. }
  1448. /*
  1449. * FIXME: the right thing would be to return bit_error untouched,
  1450. * but, as we don't know the scale returned by the counters, let's
  1451. * at least preserver BER = bit_error/bit_count.
  1452. */
  1453. if (period_exp >= 4) {
  1454. *bit_count = (1U << (period_exp - 4)) * (n_ldpc / 200);
  1455. *bit_error *= 3125ULL;
  1456. } else {
  1457. *bit_count = (1U << period_exp) * (n_ldpc / 200);
  1458. *bit_error *= 50000ULL;
  1459. }
  1460. return 0;
  1461. }
  1462. static int cxd2841er_read_ber_t(struct cxd2841er_priv *priv,
  1463. u32 *bit_error, u32 *bit_count)
  1464. {
  1465. u8 data[2];
  1466. u32 period;
  1467. if (priv->state != STATE_ACTIVE_TC) {
  1468. dev_dbg(&priv->i2c->dev,
  1469. "%s(): invalid state %d\n", __func__, priv->state);
  1470. return -EINVAL;
  1471. }
  1472. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  1473. cxd2841er_read_reg(priv, I2C_SLVT, 0x39, data);
  1474. if (!(data[0] & 0x01)) {
  1475. dev_dbg(&priv->i2c->dev,
  1476. "%s(): no valid BER data\n", __func__);
  1477. return 0;
  1478. }
  1479. cxd2841er_read_regs(priv, I2C_SLVT, 0x22, data, sizeof(data));
  1480. *bit_error = ((u32)data[0] << 8) | (u32)data[1];
  1481. cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
  1482. period = ((data[0] & 0x07) == 0) ? 256 : (4096 << (data[0] & 0x07));
  1483. /*
  1484. * FIXME: the right thing would be to return bit_error untouched,
  1485. * but, as we don't know the scale returned by the counters, let's
  1486. * at least preserver BER = bit_error/bit_count.
  1487. */
  1488. *bit_count = period / 128;
  1489. *bit_error *= 78125ULL;
  1490. return 0;
  1491. }
  1492. static int cxd2841er_freeze_regs(struct cxd2841er_priv *priv)
  1493. {
  1494. /*
  1495. * Freeze registers: ensure multiple separate register reads
  1496. * are from the same snapshot
  1497. */
  1498. cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x01);
  1499. return 0;
  1500. }
  1501. static int cxd2841er_unfreeze_regs(struct cxd2841er_priv *priv)
  1502. {
  1503. /*
  1504. * un-freeze registers
  1505. */
  1506. cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x00);
  1507. return 0;
  1508. }
  1509. static u32 cxd2841er_dvbs_read_snr(struct cxd2841er_priv *priv,
  1510. u8 delsys, u32 *snr)
  1511. {
  1512. u8 data[3];
  1513. u32 res = 0, value;
  1514. int min_index, max_index, index;
  1515. static const struct cxd2841er_cnr_data *cn_data;
  1516. cxd2841er_freeze_regs(priv);
  1517. /* Set SLV-T Bank : 0xA1 */
  1518. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa1);
  1519. /*
  1520. * slave Bank Addr Bit Signal name
  1521. * <SLV-T> A1h 10h [0] ICPM_QUICKRDY
  1522. * <SLV-T> A1h 11h [4:0] ICPM_QUICKCNDT[12:8]
  1523. * <SLV-T> A1h 12h [7:0] ICPM_QUICKCNDT[7:0]
  1524. */
  1525. cxd2841er_read_regs(priv, I2C_SLVT, 0x10, data, 3);
  1526. if (data[0] & 0x01) {
  1527. value = ((u32)(data[1] & 0x1F) << 8) | (u32)(data[2] & 0xFF);
  1528. min_index = 0;
  1529. if (delsys == SYS_DVBS) {
  1530. cn_data = s_cn_data;
  1531. max_index = sizeof(s_cn_data) /
  1532. sizeof(s_cn_data[0]) - 1;
  1533. } else {
  1534. cn_data = s2_cn_data;
  1535. max_index = sizeof(s2_cn_data) /
  1536. sizeof(s2_cn_data[0]) - 1;
  1537. }
  1538. if (value >= cn_data[min_index].value) {
  1539. res = cn_data[min_index].cnr_x1000;
  1540. goto done;
  1541. }
  1542. if (value <= cn_data[max_index].value) {
  1543. res = cn_data[max_index].cnr_x1000;
  1544. goto done;
  1545. }
  1546. while ((max_index - min_index) > 1) {
  1547. index = (max_index + min_index) / 2;
  1548. if (value == cn_data[index].value) {
  1549. res = cn_data[index].cnr_x1000;
  1550. goto done;
  1551. } else if (value > cn_data[index].value)
  1552. max_index = index;
  1553. else
  1554. min_index = index;
  1555. if ((max_index - min_index) <= 1) {
  1556. if (value == cn_data[max_index].value) {
  1557. res = cn_data[max_index].cnr_x1000;
  1558. goto done;
  1559. } else {
  1560. res = cn_data[min_index].cnr_x1000;
  1561. goto done;
  1562. }
  1563. }
  1564. }
  1565. } else {
  1566. dev_dbg(&priv->i2c->dev,
  1567. "%s(): no data available\n", __func__);
  1568. cxd2841er_unfreeze_regs(priv);
  1569. return -EINVAL;
  1570. }
  1571. done:
  1572. cxd2841er_unfreeze_regs(priv);
  1573. *snr = res;
  1574. return 0;
  1575. }
  1576. static uint32_t sony_log(uint32_t x)
  1577. {
  1578. return (((10000>>8)*(intlog2(x)>>16) + LOG2_E_100X/2)/LOG2_E_100X);
  1579. }
  1580. static int cxd2841er_read_snr_c(struct cxd2841er_priv *priv, u32 *snr)
  1581. {
  1582. u32 reg;
  1583. u8 data[2];
  1584. enum sony_dvbc_constellation_t qam = SONY_DVBC_CONSTELLATION_16QAM;
  1585. *snr = 0;
  1586. if (priv->state != STATE_ACTIVE_TC) {
  1587. dev_dbg(&priv->i2c->dev,
  1588. "%s(): invalid state %d\n",
  1589. __func__, priv->state);
  1590. return -EINVAL;
  1591. }
  1592. cxd2841er_freeze_regs(priv);
  1593. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
  1594. cxd2841er_read_regs(priv, I2C_SLVT, 0x19, data, 1);
  1595. qam = (enum sony_dvbc_constellation_t) (data[0] & 0x07);
  1596. cxd2841er_read_regs(priv, I2C_SLVT, 0x4C, data, 2);
  1597. reg = ((u32)(data[0]&0x1f) << 8) | (u32)data[1];
  1598. if (reg == 0) {
  1599. dev_dbg(&priv->i2c->dev,
  1600. "%s(): reg value out of range\n", __func__);
  1601. cxd2841er_unfreeze_regs(priv);
  1602. return 0;
  1603. }
  1604. switch (qam) {
  1605. case SONY_DVBC_CONSTELLATION_16QAM:
  1606. case SONY_DVBC_CONSTELLATION_64QAM:
  1607. case SONY_DVBC_CONSTELLATION_256QAM:
  1608. /* SNR(dB) = -9.50 * ln(IREG_SNR_ESTIMATE / (24320)) */
  1609. if (reg < 126)
  1610. reg = 126;
  1611. *snr = -95 * (int32_t)sony_log(reg) + 95941;
  1612. break;
  1613. case SONY_DVBC_CONSTELLATION_32QAM:
  1614. case SONY_DVBC_CONSTELLATION_128QAM:
  1615. /* SNR(dB) = -8.75 * ln(IREG_SNR_ESTIMATE / (20800)) */
  1616. if (reg < 69)
  1617. reg = 69;
  1618. *snr = -88 * (int32_t)sony_log(reg) + 86999;
  1619. break;
  1620. default:
  1621. cxd2841er_unfreeze_regs(priv);
  1622. return -EINVAL;
  1623. }
  1624. cxd2841er_unfreeze_regs(priv);
  1625. return 0;
  1626. }
  1627. static int cxd2841er_read_snr_t(struct cxd2841er_priv *priv, u32 *snr)
  1628. {
  1629. u32 reg;
  1630. u8 data[2];
  1631. *snr = 0;
  1632. if (priv->state != STATE_ACTIVE_TC) {
  1633. dev_dbg(&priv->i2c->dev,
  1634. "%s(): invalid state %d\n", __func__, priv->state);
  1635. return -EINVAL;
  1636. }
  1637. cxd2841er_freeze_regs(priv);
  1638. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  1639. cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
  1640. reg = ((u32)data[0] << 8) | (u32)data[1];
  1641. if (reg == 0) {
  1642. dev_dbg(&priv->i2c->dev,
  1643. "%s(): reg value out of range\n", __func__);
  1644. cxd2841er_unfreeze_regs(priv);
  1645. return 0;
  1646. }
  1647. if (reg > 4996)
  1648. reg = 4996;
  1649. *snr = 10000 * ((intlog10(reg) - intlog10(5350 - reg)) >> 24) + 28500;
  1650. cxd2841er_unfreeze_regs(priv);
  1651. return 0;
  1652. }
  1653. static int cxd2841er_read_snr_t2(struct cxd2841er_priv *priv, u32 *snr)
  1654. {
  1655. u32 reg;
  1656. u8 data[2];
  1657. *snr = 0;
  1658. if (priv->state != STATE_ACTIVE_TC) {
  1659. dev_dbg(&priv->i2c->dev,
  1660. "%s(): invalid state %d\n", __func__, priv->state);
  1661. return -EINVAL;
  1662. }
  1663. cxd2841er_freeze_regs(priv);
  1664. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
  1665. cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
  1666. reg = ((u32)data[0] << 8) | (u32)data[1];
  1667. if (reg == 0) {
  1668. dev_dbg(&priv->i2c->dev,
  1669. "%s(): reg value out of range\n", __func__);
  1670. cxd2841er_unfreeze_regs(priv);
  1671. return 0;
  1672. }
  1673. if (reg > 10876)
  1674. reg = 10876;
  1675. *snr = 10000 * ((intlog10(reg) -
  1676. intlog10(12600 - reg)) >> 24) + 32000;
  1677. cxd2841er_unfreeze_regs(priv);
  1678. return 0;
  1679. }
  1680. static int cxd2841er_read_snr_i(struct cxd2841er_priv *priv, u32 *snr)
  1681. {
  1682. u32 reg;
  1683. u8 data[2];
  1684. *snr = 0;
  1685. if (priv->state != STATE_ACTIVE_TC) {
  1686. dev_dbg(&priv->i2c->dev,
  1687. "%s(): invalid state %d\n", __func__,
  1688. priv->state);
  1689. return -EINVAL;
  1690. }
  1691. cxd2841er_freeze_regs(priv);
  1692. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
  1693. cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
  1694. reg = ((u32)data[0] << 8) | (u32)data[1];
  1695. if (reg == 0) {
  1696. dev_dbg(&priv->i2c->dev,
  1697. "%s(): reg value out of range\n", __func__);
  1698. cxd2841er_unfreeze_regs(priv);
  1699. return 0;
  1700. }
  1701. *snr = 10000 * (intlog10(reg) >> 24) - 9031;
  1702. cxd2841er_unfreeze_regs(priv);
  1703. return 0;
  1704. }
  1705. static u16 cxd2841er_read_agc_gain_c(struct cxd2841er_priv *priv,
  1706. u8 delsys)
  1707. {
  1708. u8 data[2];
  1709. cxd2841er_write_reg(
  1710. priv, I2C_SLVT, 0x00, 0x40);
  1711. cxd2841er_read_regs(priv, I2C_SLVT, 0x49, data, 2);
  1712. dev_dbg(&priv->i2c->dev,
  1713. "%s(): AGC value=%u\n",
  1714. __func__, (((u16)data[0] & 0x0F) << 8) |
  1715. (u16)(data[1] & 0xFF));
  1716. return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
  1717. }
  1718. static u16 cxd2841er_read_agc_gain_t_t2(struct cxd2841er_priv *priv,
  1719. u8 delsys)
  1720. {
  1721. u8 data[2];
  1722. cxd2841er_write_reg(
  1723. priv, I2C_SLVT, 0x00, (delsys == SYS_DVBT ? 0x10 : 0x20));
  1724. cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
  1725. dev_dbg(&priv->i2c->dev,
  1726. "%s(): AGC value=%u\n",
  1727. __func__, (((u16)data[0] & 0x0F) << 8) |
  1728. (u16)(data[1] & 0xFF));
  1729. return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
  1730. }
  1731. static u16 cxd2841er_read_agc_gain_i(struct cxd2841er_priv *priv,
  1732. u8 delsys)
  1733. {
  1734. u8 data[2];
  1735. cxd2841er_write_reg(
  1736. priv, I2C_SLVT, 0x00, 0x60);
  1737. cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
  1738. dev_dbg(&priv->i2c->dev,
  1739. "%s(): AGC value=%u\n",
  1740. __func__, (((u16)data[0] & 0x0F) << 8) |
  1741. (u16)(data[1] & 0xFF));
  1742. return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
  1743. }
  1744. static u16 cxd2841er_read_agc_gain_s(struct cxd2841er_priv *priv)
  1745. {
  1746. u8 data[2];
  1747. /* Set SLV-T Bank : 0xA0 */
  1748. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
  1749. /*
  1750. * slave Bank Addr Bit Signal name
  1751. * <SLV-T> A0h 1Fh [4:0] IRFAGC_GAIN[12:8]
  1752. * <SLV-T> A0h 20h [7:0] IRFAGC_GAIN[7:0]
  1753. */
  1754. cxd2841er_read_regs(priv, I2C_SLVT, 0x1f, data, 2);
  1755. return ((((u16)data[0] & 0x1F) << 8) | (u16)(data[1] & 0xFF)) << 3;
  1756. }
  1757. static void cxd2841er_read_ber(struct dvb_frontend *fe)
  1758. {
  1759. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  1760. struct cxd2841er_priv *priv = fe->demodulator_priv;
  1761. u32 ret, bit_error = 0, bit_count = 0;
  1762. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  1763. switch (p->delivery_system) {
  1764. case SYS_DVBC_ANNEX_A:
  1765. case SYS_DVBC_ANNEX_B:
  1766. case SYS_DVBC_ANNEX_C:
  1767. ret = cxd2841er_read_ber_c(priv, &bit_error, &bit_count);
  1768. break;
  1769. case SYS_ISDBT:
  1770. ret = cxd2841er_read_ber_i(priv, &bit_error, &bit_count);
  1771. break;
  1772. case SYS_DVBS:
  1773. ret = cxd2841er_mon_read_ber_s(priv, &bit_error, &bit_count);
  1774. break;
  1775. case SYS_DVBS2:
  1776. ret = cxd2841er_mon_read_ber_s2(priv, &bit_error, &bit_count);
  1777. break;
  1778. case SYS_DVBT:
  1779. ret = cxd2841er_read_ber_t(priv, &bit_error, &bit_count);
  1780. break;
  1781. case SYS_DVBT2:
  1782. ret = cxd2841er_read_ber_t2(priv, &bit_error, &bit_count);
  1783. break;
  1784. default:
  1785. p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1786. p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1787. return;
  1788. }
  1789. if (!ret) {
  1790. p->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  1791. p->post_bit_error.stat[0].uvalue += bit_error;
  1792. p->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  1793. p->post_bit_count.stat[0].uvalue += bit_count;
  1794. } else {
  1795. p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1796. p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1797. }
  1798. }
  1799. static void cxd2841er_read_signal_strength(struct dvb_frontend *fe)
  1800. {
  1801. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  1802. struct cxd2841er_priv *priv = fe->demodulator_priv;
  1803. s32 strength;
  1804. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  1805. switch (p->delivery_system) {
  1806. case SYS_DVBT:
  1807. case SYS_DVBT2:
  1808. strength = cxd2841er_read_agc_gain_t_t2(priv,
  1809. p->delivery_system);
  1810. p->strength.stat[0].scale = FE_SCALE_DECIBEL;
  1811. /* Formula was empirically determinated @ 410 MHz */
  1812. p->strength.stat[0].uvalue = strength * 366 / 100 - 89520;
  1813. break; /* Code moved out of the function */
  1814. case SYS_DVBC_ANNEX_A:
  1815. case SYS_DVBC_ANNEX_B:
  1816. case SYS_DVBC_ANNEX_C:
  1817. strength = cxd2841er_read_agc_gain_c(priv,
  1818. p->delivery_system);
  1819. p->strength.stat[0].scale = FE_SCALE_DECIBEL;
  1820. /*
  1821. * Formula was empirically determinated via linear regression,
  1822. * using frequencies: 175 MHz, 410 MHz and 800 MHz, and a
  1823. * stream modulated with QAM64
  1824. */
  1825. p->strength.stat[0].uvalue = strength * 4045 / 1000 - 85224;
  1826. break;
  1827. case SYS_ISDBT:
  1828. strength = cxd2841er_read_agc_gain_i(priv, p->delivery_system);
  1829. p->strength.stat[0].scale = FE_SCALE_DECIBEL;
  1830. /*
  1831. * Formula was empirically determinated via linear regression,
  1832. * using frequencies: 175 MHz, 410 MHz and 800 MHz.
  1833. */
  1834. p->strength.stat[0].uvalue = strength * 3775 / 1000 - 90185;
  1835. break;
  1836. case SYS_DVBS:
  1837. case SYS_DVBS2:
  1838. strength = 65535 - cxd2841er_read_agc_gain_s(priv);
  1839. p->strength.stat[0].scale = FE_SCALE_RELATIVE;
  1840. p->strength.stat[0].uvalue = strength;
  1841. break;
  1842. default:
  1843. p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1844. break;
  1845. }
  1846. }
  1847. static void cxd2841er_read_snr(struct dvb_frontend *fe)
  1848. {
  1849. u32 tmp = 0;
  1850. int ret = 0;
  1851. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  1852. struct cxd2841er_priv *priv = fe->demodulator_priv;
  1853. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  1854. switch (p->delivery_system) {
  1855. case SYS_DVBC_ANNEX_A:
  1856. case SYS_DVBC_ANNEX_B:
  1857. case SYS_DVBC_ANNEX_C:
  1858. ret = cxd2841er_read_snr_c(priv, &tmp);
  1859. break;
  1860. case SYS_DVBT:
  1861. ret = cxd2841er_read_snr_t(priv, &tmp);
  1862. break;
  1863. case SYS_DVBT2:
  1864. ret = cxd2841er_read_snr_t2(priv, &tmp);
  1865. break;
  1866. case SYS_ISDBT:
  1867. ret = cxd2841er_read_snr_i(priv, &tmp);
  1868. break;
  1869. case SYS_DVBS:
  1870. case SYS_DVBS2:
  1871. ret = cxd2841er_dvbs_read_snr(priv, p->delivery_system, &tmp);
  1872. break;
  1873. default:
  1874. dev_dbg(&priv->i2c->dev, "%s(): unknown delivery system %d\n",
  1875. __func__, p->delivery_system);
  1876. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1877. return;
  1878. }
  1879. dev_dbg(&priv->i2c->dev, "%s(): snr=%d\n",
  1880. __func__, (int32_t)tmp);
  1881. if (!ret) {
  1882. p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  1883. p->cnr.stat[0].svalue = tmp;
  1884. } else {
  1885. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1886. }
  1887. }
  1888. static void cxd2841er_read_ucblocks(struct dvb_frontend *fe)
  1889. {
  1890. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  1891. struct cxd2841er_priv *priv = fe->demodulator_priv;
  1892. u32 ucblocks = 0;
  1893. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  1894. switch (p->delivery_system) {
  1895. case SYS_DVBC_ANNEX_A:
  1896. case SYS_DVBC_ANNEX_B:
  1897. case SYS_DVBC_ANNEX_C:
  1898. cxd2841er_read_packet_errors_c(priv, &ucblocks);
  1899. break;
  1900. case SYS_DVBT:
  1901. cxd2841er_read_packet_errors_t(priv, &ucblocks);
  1902. break;
  1903. case SYS_DVBT2:
  1904. cxd2841er_read_packet_errors_t2(priv, &ucblocks);
  1905. break;
  1906. case SYS_ISDBT:
  1907. cxd2841er_read_packet_errors_i(priv, &ucblocks);
  1908. break;
  1909. default:
  1910. p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  1911. return;
  1912. }
  1913. dev_dbg(&priv->i2c->dev, "%s() ucblocks=%u\n", __func__, ucblocks);
  1914. p->block_error.stat[0].scale = FE_SCALE_COUNTER;
  1915. p->block_error.stat[0].uvalue = ucblocks;
  1916. }
  1917. static int cxd2841er_dvbt2_set_profile(
  1918. struct cxd2841er_priv *priv, enum cxd2841er_dvbt2_profile_t profile)
  1919. {
  1920. u8 tune_mode;
  1921. u8 seq_not2d_time;
  1922. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  1923. switch (profile) {
  1924. case DVBT2_PROFILE_BASE:
  1925. tune_mode = 0x01;
  1926. /* Set early unlock time */
  1927. seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x0E:0x0C;
  1928. break;
  1929. case DVBT2_PROFILE_LITE:
  1930. tune_mode = 0x05;
  1931. /* Set early unlock time */
  1932. seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
  1933. break;
  1934. case DVBT2_PROFILE_ANY:
  1935. tune_mode = 0x00;
  1936. /* Set early unlock time */
  1937. seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
  1938. break;
  1939. default:
  1940. return -EINVAL;
  1941. }
  1942. /* Set SLV-T Bank : 0x2E */
  1943. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2e);
  1944. /* Set profile and tune mode */
  1945. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x10, tune_mode, 0x07);
  1946. /* Set SLV-T Bank : 0x2B */
  1947. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
  1948. /* Set early unlock detection time */
  1949. cxd2841er_write_reg(priv, I2C_SLVT, 0x9d, seq_not2d_time);
  1950. return 0;
  1951. }
  1952. static int cxd2841er_dvbt2_set_plp_config(struct cxd2841er_priv *priv,
  1953. u8 is_auto, u8 plp_id)
  1954. {
  1955. if (is_auto) {
  1956. dev_dbg(&priv->i2c->dev,
  1957. "%s() using auto PLP selection\n", __func__);
  1958. } else {
  1959. dev_dbg(&priv->i2c->dev,
  1960. "%s() using manual PLP selection, ID %d\n",
  1961. __func__, plp_id);
  1962. }
  1963. /* Set SLV-T Bank : 0x23 */
  1964. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
  1965. if (!is_auto) {
  1966. /* Manual PLP selection mode. Set the data PLP Id. */
  1967. cxd2841er_write_reg(priv, I2C_SLVT, 0xaf, plp_id);
  1968. }
  1969. /* Auto PLP select (Scanning mode = 0x00). Data PLP select = 0x01. */
  1970. cxd2841er_write_reg(priv, I2C_SLVT, 0xad, (is_auto ? 0x00 : 0x01));
  1971. return 0;
  1972. }
  1973. static int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
  1974. u32 bandwidth)
  1975. {
  1976. u32 iffreq;
  1977. u8 data[MAX_WRITE_REGSIZE];
  1978. const uint8_t nominalRate8bw[3][5] = {
  1979. /* TRCG Nominal Rate [37:0] */
  1980. {0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
  1981. {0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
  1982. {0x11, 0xF0, 0x00, 0x00, 0x00} /* 41MHz XTal */
  1983. };
  1984. const uint8_t nominalRate7bw[3][5] = {
  1985. /* TRCG Nominal Rate [37:0] */
  1986. {0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
  1987. {0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
  1988. {0x14, 0x80, 0x00, 0x00, 0x00} /* 41MHz XTal */
  1989. };
  1990. const uint8_t nominalRate6bw[3][5] = {
  1991. /* TRCG Nominal Rate [37:0] */
  1992. {0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
  1993. {0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
  1994. {0x17, 0xEA, 0xAA, 0xAA, 0xAA} /* 41MHz XTal */
  1995. };
  1996. const uint8_t nominalRate5bw[3][5] = {
  1997. /* TRCG Nominal Rate [37:0] */
  1998. {0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
  1999. {0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
  2000. {0x1C, 0xB3, 0x33, 0x33, 0x33} /* 41MHz XTal */
  2001. };
  2002. const uint8_t nominalRate17bw[3][5] = {
  2003. /* TRCG Nominal Rate [37:0] */
  2004. {0x58, 0xE2, 0xAF, 0xE0, 0xBC}, /* 20.5MHz XTal */
  2005. {0x68, 0x0F, 0xA2, 0x32, 0xD0}, /* 24MHz XTal */
  2006. {0x58, 0xE2, 0xAF, 0xE0, 0xBC} /* 41MHz XTal */
  2007. };
  2008. const uint8_t itbCoef8bw[3][14] = {
  2009. {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
  2010. 0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
  2011. {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1,
  2012. 0x29, 0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal */
  2013. {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
  2014. 0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8} /* 41MHz XTal */
  2015. };
  2016. const uint8_t itbCoef7bw[3][14] = {
  2017. {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
  2018. 0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
  2019. {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0,
  2020. 0x29, 0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal */
  2021. {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
  2022. 0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5} /* 41MHz XTal */
  2023. };
  2024. const uint8_t itbCoef6bw[3][14] = {
  2025. {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
  2026. 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
  2027. {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
  2028. 0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
  2029. {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
  2030. 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
  2031. };
  2032. const uint8_t itbCoef5bw[3][14] = {
  2033. {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
  2034. 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
  2035. {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
  2036. 0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
  2037. {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
  2038. 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
  2039. };
  2040. const uint8_t itbCoef17bw[3][14] = {
  2041. {0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
  2042. 0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99}, /* 20.5MHz XTal */
  2043. {0x33, 0x8E, 0x2B, 0x97, 0x2D, 0x95, 0x37, 0x8B,
  2044. 0x30, 0x97, 0x2D, 0x9A, 0x21, 0xA4}, /* 24MHz XTal */
  2045. {0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
  2046. 0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99} /* 41MHz XTal */
  2047. };
  2048. /* Set SLV-T Bank : 0x20 */
  2049. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
  2050. switch (bandwidth) {
  2051. case 8000000:
  2052. /* <Timing Recovery setting> */
  2053. cxd2841er_write_regs(priv, I2C_SLVT,
  2054. 0x9F, nominalRate8bw[priv->xtal], 5);
  2055. /* Set SLV-T Bank : 0x27 */
  2056. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
  2057. cxd2841er_set_reg_bits(priv, I2C_SLVT,
  2058. 0x7a, 0x00, 0x0f);
  2059. /* Set SLV-T Bank : 0x10 */
  2060. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2061. /* Group delay equaliser settings for
  2062. * ASCOT2D, ASCOT2E and ASCOT3 tuners
  2063. */
  2064. cxd2841er_write_regs(priv, I2C_SLVT,
  2065. 0xA6, itbCoef8bw[priv->xtal], 14);
  2066. /* <IF freq setting> */
  2067. iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.80);
  2068. data[0] = (u8) ((iffreq >> 16) & 0xff);
  2069. data[1] = (u8)((iffreq >> 8) & 0xff);
  2070. data[2] = (u8)(iffreq & 0xff);
  2071. cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
  2072. /* System bandwidth setting */
  2073. cxd2841er_set_reg_bits(
  2074. priv, I2C_SLVT, 0xD7, 0x00, 0x07);
  2075. break;
  2076. case 7000000:
  2077. /* <Timing Recovery setting> */
  2078. cxd2841er_write_regs(priv, I2C_SLVT,
  2079. 0x9F, nominalRate7bw[priv->xtal], 5);
  2080. /* Set SLV-T Bank : 0x27 */
  2081. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
  2082. cxd2841er_set_reg_bits(priv, I2C_SLVT,
  2083. 0x7a, 0x00, 0x0f);
  2084. /* Set SLV-T Bank : 0x10 */
  2085. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2086. /* Group delay equaliser settings for
  2087. * ASCOT2D, ASCOT2E and ASCOT3 tuners
  2088. */
  2089. cxd2841er_write_regs(priv, I2C_SLVT,
  2090. 0xA6, itbCoef7bw[priv->xtal], 14);
  2091. /* <IF freq setting> */
  2092. iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.20);
  2093. data[0] = (u8) ((iffreq >> 16) & 0xff);
  2094. data[1] = (u8)((iffreq >> 8) & 0xff);
  2095. data[2] = (u8)(iffreq & 0xff);
  2096. cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
  2097. /* System bandwidth setting */
  2098. cxd2841er_set_reg_bits(
  2099. priv, I2C_SLVT, 0xD7, 0x02, 0x07);
  2100. break;
  2101. case 6000000:
  2102. /* <Timing Recovery setting> */
  2103. cxd2841er_write_regs(priv, I2C_SLVT,
  2104. 0x9F, nominalRate6bw[priv->xtal], 5);
  2105. /* Set SLV-T Bank : 0x27 */
  2106. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
  2107. cxd2841er_set_reg_bits(priv, I2C_SLVT,
  2108. 0x7a, 0x00, 0x0f);
  2109. /* Set SLV-T Bank : 0x10 */
  2110. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2111. /* Group delay equaliser settings for
  2112. * ASCOT2D, ASCOT2E and ASCOT3 tuners
  2113. */
  2114. cxd2841er_write_regs(priv, I2C_SLVT,
  2115. 0xA6, itbCoef6bw[priv->xtal], 14);
  2116. /* <IF freq setting> */
  2117. iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.60);
  2118. data[0] = (u8) ((iffreq >> 16) & 0xff);
  2119. data[1] = (u8)((iffreq >> 8) & 0xff);
  2120. data[2] = (u8)(iffreq & 0xff);
  2121. cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
  2122. /* System bandwidth setting */
  2123. cxd2841er_set_reg_bits(
  2124. priv, I2C_SLVT, 0xD7, 0x04, 0x07);
  2125. break;
  2126. case 5000000:
  2127. /* <Timing Recovery setting> */
  2128. cxd2841er_write_regs(priv, I2C_SLVT,
  2129. 0x9F, nominalRate5bw[priv->xtal], 5);
  2130. /* Set SLV-T Bank : 0x27 */
  2131. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
  2132. cxd2841er_set_reg_bits(priv, I2C_SLVT,
  2133. 0x7a, 0x00, 0x0f);
  2134. /* Set SLV-T Bank : 0x10 */
  2135. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2136. /* Group delay equaliser settings for
  2137. * ASCOT2D, ASCOT2E and ASCOT3 tuners
  2138. */
  2139. cxd2841er_write_regs(priv, I2C_SLVT,
  2140. 0xA6, itbCoef5bw[priv->xtal], 14);
  2141. /* <IF freq setting> */
  2142. iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.60);
  2143. data[0] = (u8) ((iffreq >> 16) & 0xff);
  2144. data[1] = (u8)((iffreq >> 8) & 0xff);
  2145. data[2] = (u8)(iffreq & 0xff);
  2146. cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
  2147. /* System bandwidth setting */
  2148. cxd2841er_set_reg_bits(
  2149. priv, I2C_SLVT, 0xD7, 0x06, 0x07);
  2150. break;
  2151. case 1712000:
  2152. /* <Timing Recovery setting> */
  2153. cxd2841er_write_regs(priv, I2C_SLVT,
  2154. 0x9F, nominalRate17bw[priv->xtal], 5);
  2155. /* Set SLV-T Bank : 0x27 */
  2156. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
  2157. cxd2841er_set_reg_bits(priv, I2C_SLVT,
  2158. 0x7a, 0x03, 0x0f);
  2159. /* Set SLV-T Bank : 0x10 */
  2160. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2161. /* Group delay equaliser settings for
  2162. * ASCOT2D, ASCOT2E and ASCOT3 tuners
  2163. */
  2164. cxd2841er_write_regs(priv, I2C_SLVT,
  2165. 0xA6, itbCoef17bw[priv->xtal], 14);
  2166. /* <IF freq setting> */
  2167. iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.50);
  2168. data[0] = (u8) ((iffreq >> 16) & 0xff);
  2169. data[1] = (u8)((iffreq >> 8) & 0xff);
  2170. data[2] = (u8)(iffreq & 0xff);
  2171. cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
  2172. /* System bandwidth setting */
  2173. cxd2841er_set_reg_bits(
  2174. priv, I2C_SLVT, 0xD7, 0x03, 0x07);
  2175. break;
  2176. default:
  2177. return -EINVAL;
  2178. }
  2179. return 0;
  2180. }
  2181. static int cxd2841er_sleep_tc_to_active_t_band(
  2182. struct cxd2841er_priv *priv, u32 bandwidth)
  2183. {
  2184. u8 data[MAX_WRITE_REGSIZE];
  2185. u32 iffreq;
  2186. u8 nominalRate8bw[3][5] = {
  2187. /* TRCG Nominal Rate [37:0] */
  2188. {0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
  2189. {0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
  2190. {0x11, 0xF0, 0x00, 0x00, 0x00} /* 41MHz XTal */
  2191. };
  2192. u8 nominalRate7bw[3][5] = {
  2193. /* TRCG Nominal Rate [37:0] */
  2194. {0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
  2195. {0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
  2196. {0x14, 0x80, 0x00, 0x00, 0x00} /* 41MHz XTal */
  2197. };
  2198. u8 nominalRate6bw[3][5] = {
  2199. /* TRCG Nominal Rate [37:0] */
  2200. {0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
  2201. {0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
  2202. {0x17, 0xEA, 0xAA, 0xAA, 0xAA} /* 41MHz XTal */
  2203. };
  2204. u8 nominalRate5bw[3][5] = {
  2205. /* TRCG Nominal Rate [37:0] */
  2206. {0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
  2207. {0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
  2208. {0x1C, 0xB3, 0x33, 0x33, 0x33} /* 41MHz XTal */
  2209. };
  2210. u8 itbCoef8bw[3][14] = {
  2211. {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
  2212. 0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
  2213. {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29, 0xA5,
  2214. 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal */
  2215. {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
  2216. 0x1F, 0xA8, 0x2C, 0xC8} /* 41MHz XTal */
  2217. };
  2218. u8 itbCoef7bw[3][14] = {
  2219. {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
  2220. 0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
  2221. {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29, 0xA2,
  2222. 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal */
  2223. {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
  2224. 0x26, 0xA9, 0x21, 0xA5} /* 41MHz XTal */
  2225. };
  2226. u8 itbCoef6bw[3][14] = {
  2227. {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
  2228. 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
  2229. {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
  2230. 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
  2231. {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
  2232. 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
  2233. };
  2234. u8 itbCoef5bw[3][14] = {
  2235. {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
  2236. 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
  2237. {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
  2238. 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
  2239. {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
  2240. 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
  2241. };
  2242. /* Set SLV-T Bank : 0x13 */
  2243. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
  2244. /* Echo performance optimization setting */
  2245. data[0] = 0x01;
  2246. data[1] = 0x14;
  2247. cxd2841er_write_regs(priv, I2C_SLVT, 0x9C, data, 2);
  2248. /* Set SLV-T Bank : 0x10 */
  2249. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2250. switch (bandwidth) {
  2251. case 8000000:
  2252. /* <Timing Recovery setting> */
  2253. cxd2841er_write_regs(priv, I2C_SLVT,
  2254. 0x9F, nominalRate8bw[priv->xtal], 5);
  2255. /* Group delay equaliser settings for
  2256. * ASCOT2D, ASCOT2E and ASCOT3 tuners
  2257. */
  2258. cxd2841er_write_regs(priv, I2C_SLVT,
  2259. 0xA6, itbCoef8bw[priv->xtal], 14);
  2260. /* <IF freq setting> */
  2261. iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.80);
  2262. data[0] = (u8) ((iffreq >> 16) & 0xff);
  2263. data[1] = (u8)((iffreq >> 8) & 0xff);
  2264. data[2] = (u8)(iffreq & 0xff);
  2265. cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
  2266. /* System bandwidth setting */
  2267. cxd2841er_set_reg_bits(
  2268. priv, I2C_SLVT, 0xD7, 0x00, 0x07);
  2269. /* Demod core latency setting */
  2270. if (priv->xtal == SONY_XTAL_24000) {
  2271. data[0] = 0x15;
  2272. data[1] = 0x28;
  2273. } else {
  2274. data[0] = 0x01;
  2275. data[1] = 0xE0;
  2276. }
  2277. cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
  2278. /* Notch filter setting */
  2279. data[0] = 0x01;
  2280. data[1] = 0x02;
  2281. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
  2282. cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
  2283. break;
  2284. case 7000000:
  2285. /* <Timing Recovery setting> */
  2286. cxd2841er_write_regs(priv, I2C_SLVT,
  2287. 0x9F, nominalRate7bw[priv->xtal], 5);
  2288. /* Group delay equaliser settings for
  2289. * ASCOT2D, ASCOT2E and ASCOT3 tuners
  2290. */
  2291. cxd2841er_write_regs(priv, I2C_SLVT,
  2292. 0xA6, itbCoef7bw[priv->xtal], 14);
  2293. /* <IF freq setting> */
  2294. iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.20);
  2295. data[0] = (u8) ((iffreq >> 16) & 0xff);
  2296. data[1] = (u8)((iffreq >> 8) & 0xff);
  2297. data[2] = (u8)(iffreq & 0xff);
  2298. cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
  2299. /* System bandwidth setting */
  2300. cxd2841er_set_reg_bits(
  2301. priv, I2C_SLVT, 0xD7, 0x02, 0x07);
  2302. /* Demod core latency setting */
  2303. if (priv->xtal == SONY_XTAL_24000) {
  2304. data[0] = 0x1F;
  2305. data[1] = 0xF8;
  2306. } else {
  2307. data[0] = 0x12;
  2308. data[1] = 0xF8;
  2309. }
  2310. cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
  2311. /* Notch filter setting */
  2312. data[0] = 0x00;
  2313. data[1] = 0x03;
  2314. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
  2315. cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
  2316. break;
  2317. case 6000000:
  2318. /* <Timing Recovery setting> */
  2319. cxd2841er_write_regs(priv, I2C_SLVT,
  2320. 0x9F, nominalRate6bw[priv->xtal], 5);
  2321. /* Group delay equaliser settings for
  2322. * ASCOT2D, ASCOT2E and ASCOT3 tuners
  2323. */
  2324. cxd2841er_write_regs(priv, I2C_SLVT,
  2325. 0xA6, itbCoef6bw[priv->xtal], 14);
  2326. /* <IF freq setting> */
  2327. iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.60);
  2328. data[0] = (u8) ((iffreq >> 16) & 0xff);
  2329. data[1] = (u8)((iffreq >> 8) & 0xff);
  2330. data[2] = (u8)(iffreq & 0xff);
  2331. cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
  2332. /* System bandwidth setting */
  2333. cxd2841er_set_reg_bits(
  2334. priv, I2C_SLVT, 0xD7, 0x04, 0x07);
  2335. /* Demod core latency setting */
  2336. if (priv->xtal == SONY_XTAL_24000) {
  2337. data[0] = 0x25;
  2338. data[1] = 0x4C;
  2339. } else {
  2340. data[0] = 0x1F;
  2341. data[1] = 0xDC;
  2342. }
  2343. cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
  2344. /* Notch filter setting */
  2345. data[0] = 0x00;
  2346. data[1] = 0x03;
  2347. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
  2348. cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
  2349. break;
  2350. case 5000000:
  2351. /* <Timing Recovery setting> */
  2352. cxd2841er_write_regs(priv, I2C_SLVT,
  2353. 0x9F, nominalRate5bw[priv->xtal], 5);
  2354. /* Group delay equaliser settings for
  2355. * ASCOT2D, ASCOT2E and ASCOT3 tuners
  2356. */
  2357. cxd2841er_write_regs(priv, I2C_SLVT,
  2358. 0xA6, itbCoef5bw[priv->xtal], 14);
  2359. /* <IF freq setting> */
  2360. iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.60);
  2361. data[0] = (u8) ((iffreq >> 16) & 0xff);
  2362. data[1] = (u8)((iffreq >> 8) & 0xff);
  2363. data[2] = (u8)(iffreq & 0xff);
  2364. cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
  2365. /* System bandwidth setting */
  2366. cxd2841er_set_reg_bits(
  2367. priv, I2C_SLVT, 0xD7, 0x06, 0x07);
  2368. /* Demod core latency setting */
  2369. if (priv->xtal == SONY_XTAL_24000) {
  2370. data[0] = 0x2C;
  2371. data[1] = 0xC2;
  2372. } else {
  2373. data[0] = 0x26;
  2374. data[1] = 0x3C;
  2375. }
  2376. cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
  2377. /* Notch filter setting */
  2378. data[0] = 0x00;
  2379. data[1] = 0x03;
  2380. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
  2381. cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
  2382. break;
  2383. }
  2384. return 0;
  2385. }
  2386. static int cxd2841er_sleep_tc_to_active_i_band(
  2387. struct cxd2841er_priv *priv, u32 bandwidth)
  2388. {
  2389. u32 iffreq;
  2390. u8 data[3];
  2391. /* TRCG Nominal Rate */
  2392. u8 nominalRate8bw[3][5] = {
  2393. {0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
  2394. {0x11, 0xB8, 0x00, 0x00, 0x00}, /* 24MHz XTal */
  2395. {0x00, 0x00, 0x00, 0x00, 0x00} /* 41MHz XTal */
  2396. };
  2397. u8 nominalRate7bw[3][5] = {
  2398. {0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
  2399. {0x14, 0x40, 0x00, 0x00, 0x00}, /* 24MHz XTal */
  2400. {0x00, 0x00, 0x00, 0x00, 0x00} /* 41MHz XTal */
  2401. };
  2402. u8 nominalRate6bw[3][5] = {
  2403. {0x14, 0x2E, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
  2404. {0x17, 0xA0, 0x00, 0x00, 0x00}, /* 24MHz XTal */
  2405. {0x14, 0x2E, 0x00, 0x00, 0x00} /* 41MHz XTal */
  2406. };
  2407. u8 itbCoef8bw[3][14] = {
  2408. {0x00}, /* 20.5MHz XTal */
  2409. {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29,
  2410. 0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz Xtal */
  2411. {0x0}, /* 41MHz XTal */
  2412. };
  2413. u8 itbCoef7bw[3][14] = {
  2414. {0x00}, /* 20.5MHz XTal */
  2415. {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29,
  2416. 0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz Xtal */
  2417. {0x00}, /* 41MHz XTal */
  2418. };
  2419. u8 itbCoef6bw[3][14] = {
  2420. {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
  2421. 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
  2422. {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29,
  2423. 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz Xtal */
  2424. {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
  2425. 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 41MHz XTal */
  2426. };
  2427. dev_dbg(&priv->i2c->dev, "%s() bandwidth=%u\n", __func__, bandwidth);
  2428. /* Set SLV-T Bank : 0x10 */
  2429. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2430. /* 20.5/41MHz Xtal support is not available
  2431. * on ISDB-T 7MHzBW and 8MHzBW
  2432. */
  2433. if (priv->xtal != SONY_XTAL_24000 && bandwidth > 6000000) {
  2434. dev_err(&priv->i2c->dev,
  2435. "%s(): bandwidth %d supported only for 24MHz xtal\n",
  2436. __func__, bandwidth);
  2437. return -EINVAL;
  2438. }
  2439. switch (bandwidth) {
  2440. case 8000000:
  2441. /* TRCG Nominal Rate */
  2442. cxd2841er_write_regs(priv, I2C_SLVT,
  2443. 0x9F, nominalRate8bw[priv->xtal], 5);
  2444. /* Group delay equaliser settings for ASCOT tuners optimized */
  2445. cxd2841er_write_regs(priv, I2C_SLVT,
  2446. 0xA6, itbCoef8bw[priv->xtal], 14);
  2447. /* IF freq setting */
  2448. iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.75);
  2449. data[0] = (u8) ((iffreq >> 16) & 0xff);
  2450. data[1] = (u8)((iffreq >> 8) & 0xff);
  2451. data[2] = (u8)(iffreq & 0xff);
  2452. cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
  2453. /* System bandwidth setting */
  2454. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x0, 0x7);
  2455. /* Demod core latency setting */
  2456. data[0] = 0x13;
  2457. data[1] = 0xFC;
  2458. cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
  2459. /* Acquisition optimization setting */
  2460. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
  2461. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
  2462. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
  2463. cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x03);
  2464. break;
  2465. case 7000000:
  2466. /* TRCG Nominal Rate */
  2467. cxd2841er_write_regs(priv, I2C_SLVT,
  2468. 0x9F, nominalRate7bw[priv->xtal], 5);
  2469. /* Group delay equaliser settings for ASCOT tuners optimized */
  2470. cxd2841er_write_regs(priv, I2C_SLVT,
  2471. 0xA6, itbCoef7bw[priv->xtal], 14);
  2472. /* IF freq setting */
  2473. iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.15);
  2474. data[0] = (u8) ((iffreq >> 16) & 0xff);
  2475. data[1] = (u8)((iffreq >> 8) & 0xff);
  2476. data[2] = (u8)(iffreq & 0xff);
  2477. cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
  2478. /* System bandwidth setting */
  2479. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x02, 0x7);
  2480. /* Demod core latency setting */
  2481. data[0] = 0x1A;
  2482. data[1] = 0xFA;
  2483. cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
  2484. /* Acquisition optimization setting */
  2485. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
  2486. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
  2487. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
  2488. cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
  2489. break;
  2490. case 6000000:
  2491. /* TRCG Nominal Rate */
  2492. cxd2841er_write_regs(priv, I2C_SLVT,
  2493. 0x9F, nominalRate6bw[priv->xtal], 5);
  2494. /* Group delay equaliser settings for ASCOT tuners optimized */
  2495. cxd2841er_write_regs(priv, I2C_SLVT,
  2496. 0xA6, itbCoef6bw[priv->xtal], 14);
  2497. /* IF freq setting */
  2498. iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.55);
  2499. data[0] = (u8) ((iffreq >> 16) & 0xff);
  2500. data[1] = (u8)((iffreq >> 8) & 0xff);
  2501. data[2] = (u8)(iffreq & 0xff);
  2502. cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
  2503. /* System bandwidth setting */
  2504. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x04, 0x7);
  2505. /* Demod core latency setting */
  2506. if (priv->xtal == SONY_XTAL_24000) {
  2507. data[0] = 0x1F;
  2508. data[1] = 0x79;
  2509. } else {
  2510. data[0] = 0x1A;
  2511. data[1] = 0xE2;
  2512. }
  2513. cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
  2514. /* Acquisition optimization setting */
  2515. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
  2516. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x07, 0x07);
  2517. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
  2518. cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
  2519. break;
  2520. default:
  2521. dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
  2522. __func__, bandwidth);
  2523. return -EINVAL;
  2524. }
  2525. return 0;
  2526. }
  2527. static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
  2528. u32 bandwidth)
  2529. {
  2530. u8 bw7_8mhz_b10_a6[] = {
  2531. 0x2D, 0xC7, 0x04, 0xF4, 0x07, 0xC5, 0x2A, 0xB8,
  2532. 0x27, 0x9E, 0x27, 0xA4, 0x29, 0xAB };
  2533. u8 bw6mhz_b10_a6[] = {
  2534. 0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
  2535. 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4 };
  2536. u8 b10_b6[3];
  2537. u32 iffreq;
  2538. if (bandwidth != 6000000 &&
  2539. bandwidth != 7000000 &&
  2540. bandwidth != 8000000) {
  2541. dev_info(&priv->i2c->dev, "%s(): unsupported bandwidth %d. Forcing 8Mhz!\n",
  2542. __func__, bandwidth);
  2543. bandwidth = 8000000;
  2544. }
  2545. dev_dbg(&priv->i2c->dev, "%s() bw=%d\n", __func__, bandwidth);
  2546. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2547. switch (bandwidth) {
  2548. case 8000000:
  2549. case 7000000:
  2550. cxd2841er_write_regs(
  2551. priv, I2C_SLVT, 0xa6,
  2552. bw7_8mhz_b10_a6, sizeof(bw7_8mhz_b10_a6));
  2553. iffreq = MAKE_IFFREQ_CONFIG(4.9);
  2554. break;
  2555. case 6000000:
  2556. cxd2841er_write_regs(
  2557. priv, I2C_SLVT, 0xa6,
  2558. bw6mhz_b10_a6, sizeof(bw6mhz_b10_a6));
  2559. iffreq = MAKE_IFFREQ_CONFIG(3.7);
  2560. break;
  2561. default:
  2562. dev_err(&priv->i2c->dev, "%s(): unsupported bandwidth %d\n",
  2563. __func__, bandwidth);
  2564. return -EINVAL;
  2565. }
  2566. /* <IF freq setting> */
  2567. b10_b6[0] = (u8) ((iffreq >> 16) & 0xff);
  2568. b10_b6[1] = (u8)((iffreq >> 8) & 0xff);
  2569. b10_b6[2] = (u8)(iffreq & 0xff);
  2570. cxd2841er_write_regs(priv, I2C_SLVT, 0xb6, b10_b6, sizeof(b10_b6));
  2571. /* Set SLV-T Bank : 0x11 */
  2572. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
  2573. switch (bandwidth) {
  2574. case 8000000:
  2575. case 7000000:
  2576. cxd2841er_set_reg_bits(
  2577. priv, I2C_SLVT, 0xa3, 0x00, 0x1f);
  2578. break;
  2579. case 6000000:
  2580. cxd2841er_set_reg_bits(
  2581. priv, I2C_SLVT, 0xa3, 0x14, 0x1f);
  2582. break;
  2583. }
  2584. /* Set SLV-T Bank : 0x40 */
  2585. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
  2586. switch (bandwidth) {
  2587. case 8000000:
  2588. cxd2841er_set_reg_bits(
  2589. priv, I2C_SLVT, 0x26, 0x0b, 0x0f);
  2590. cxd2841er_write_reg(priv, I2C_SLVT, 0x27, 0x3e);
  2591. break;
  2592. case 7000000:
  2593. cxd2841er_set_reg_bits(
  2594. priv, I2C_SLVT, 0x26, 0x09, 0x0f);
  2595. cxd2841er_write_reg(priv, I2C_SLVT, 0x27, 0xd6);
  2596. break;
  2597. case 6000000:
  2598. cxd2841er_set_reg_bits(
  2599. priv, I2C_SLVT, 0x26, 0x08, 0x0f);
  2600. cxd2841er_write_reg(priv, I2C_SLVT, 0x27, 0x6e);
  2601. break;
  2602. }
  2603. return 0;
  2604. }
  2605. static int cxd2841er_sleep_tc_to_active_t(struct cxd2841er_priv *priv,
  2606. u32 bandwidth)
  2607. {
  2608. u8 data[2] = { 0x09, 0x54 };
  2609. u8 data24m[3] = {0xDC, 0x6C, 0x00};
  2610. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  2611. cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
  2612. /* Set SLV-X Bank : 0x00 */
  2613. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  2614. /* Set demod mode */
  2615. cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
  2616. /* Set SLV-T Bank : 0x00 */
  2617. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  2618. /* Enable demod clock */
  2619. cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
  2620. /* Disable RF level monitor */
  2621. cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
  2622. /* Enable ADC clock */
  2623. cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
  2624. /* Enable ADC 1 */
  2625. cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
  2626. /* Enable ADC 2 & 3 */
  2627. if (priv->xtal == SONY_XTAL_41000) {
  2628. data[0] = 0x0A;
  2629. data[1] = 0xD4;
  2630. }
  2631. cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
  2632. /* Enable ADC 4 */
  2633. cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
  2634. /* Set SLV-T Bank : 0x10 */
  2635. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2636. /* IFAGC gain settings */
  2637. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x0c, 0x1f);
  2638. /* Set SLV-T Bank : 0x11 */
  2639. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
  2640. /* BBAGC TARGET level setting */
  2641. cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x50);
  2642. /* Set SLV-T Bank : 0x10 */
  2643. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2644. /* ASCOT setting ON */
  2645. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
  2646. /* Set SLV-T Bank : 0x18 */
  2647. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
  2648. /* Pre-RS BER moniter setting */
  2649. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x36, 0x40, 0x07);
  2650. /* FEC Auto Recovery setting */
  2651. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
  2652. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x01, 0x01);
  2653. /* Set SLV-T Bank : 0x00 */
  2654. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  2655. /* TSIF setting */
  2656. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
  2657. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
  2658. if (priv->xtal == SONY_XTAL_24000) {
  2659. /* Set SLV-T Bank : 0x10 */
  2660. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2661. cxd2841er_write_reg(priv, I2C_SLVT, 0xBF, 0x60);
  2662. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
  2663. cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data24m, 3);
  2664. }
  2665. cxd2841er_sleep_tc_to_active_t_band(priv, bandwidth);
  2666. /* Set SLV-T Bank : 0x00 */
  2667. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  2668. /* Disable HiZ Setting 1 */
  2669. cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
  2670. /* Disable HiZ Setting 2 */
  2671. cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
  2672. priv->state = STATE_ACTIVE_TC;
  2673. return 0;
  2674. }
  2675. static int cxd2841er_sleep_tc_to_active_t2(struct cxd2841er_priv *priv,
  2676. u32 bandwidth)
  2677. {
  2678. u8 data[MAX_WRITE_REGSIZE];
  2679. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  2680. cxd2841er_set_ts_clock_mode(priv, SYS_DVBT2);
  2681. /* Set SLV-X Bank : 0x00 */
  2682. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  2683. /* Set demod mode */
  2684. cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x02);
  2685. /* Set SLV-T Bank : 0x00 */
  2686. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  2687. /* Enable demod clock */
  2688. cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
  2689. /* Disable RF level monitor */
  2690. cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
  2691. cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
  2692. /* Enable ADC clock */
  2693. cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
  2694. /* Enable ADC 1 */
  2695. cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
  2696. if (priv->xtal == SONY_XTAL_41000) {
  2697. data[0] = 0x0A;
  2698. data[1] = 0xD4;
  2699. } else {
  2700. data[0] = 0x09;
  2701. data[1] = 0x54;
  2702. }
  2703. cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
  2704. /* Enable ADC 4 */
  2705. cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
  2706. /* Set SLV-T Bank : 0x10 */
  2707. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2708. /* IFAGC gain settings */
  2709. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x0c, 0x1f);
  2710. /* Set SLV-T Bank : 0x11 */
  2711. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
  2712. /* BBAGC TARGET level setting */
  2713. cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x50);
  2714. /* Set SLV-T Bank : 0x10 */
  2715. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2716. /* ASCOT setting ON */
  2717. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
  2718. /* Set SLV-T Bank : 0x20 */
  2719. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
  2720. /* Acquisition optimization setting */
  2721. cxd2841er_write_reg(priv, I2C_SLVT, 0x8b, 0x3c);
  2722. /* Set SLV-T Bank : 0x2b */
  2723. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
  2724. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x76, 0x20, 0x70);
  2725. /* Set SLV-T Bank : 0x23 */
  2726. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
  2727. /* L1 Control setting */
  2728. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE6, 0x00, 0x03);
  2729. /* Set SLV-T Bank : 0x00 */
  2730. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  2731. /* TSIF setting */
  2732. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
  2733. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
  2734. /* DVB-T2 initial setting */
  2735. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
  2736. cxd2841er_write_reg(priv, I2C_SLVT, 0x83, 0x10);
  2737. cxd2841er_write_reg(priv, I2C_SLVT, 0x86, 0x34);
  2738. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9e, 0x09, 0x0f);
  2739. cxd2841er_write_reg(priv, I2C_SLVT, 0x9f, 0xd8);
  2740. /* Set SLV-T Bank : 0x2a */
  2741. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2a);
  2742. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x38, 0x04, 0x0f);
  2743. /* Set SLV-T Bank : 0x2b */
  2744. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
  2745. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x20, 0x3f);
  2746. /* 24MHz Xtal setting */
  2747. if (priv->xtal == SONY_XTAL_24000) {
  2748. /* Set SLV-T Bank : 0x11 */
  2749. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
  2750. data[0] = 0xEB;
  2751. data[1] = 0x03;
  2752. data[2] = 0x3B;
  2753. cxd2841er_write_regs(priv, I2C_SLVT, 0x33, data, 3);
  2754. /* Set SLV-T Bank : 0x20 */
  2755. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
  2756. data[0] = 0x5E;
  2757. data[1] = 0x5E;
  2758. data[2] = 0x47;
  2759. cxd2841er_write_regs(priv, I2C_SLVT, 0x95, data, 3);
  2760. cxd2841er_write_reg(priv, I2C_SLVT, 0x99, 0x18);
  2761. data[0] = 0x3F;
  2762. data[1] = 0xFF;
  2763. cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
  2764. /* Set SLV-T Bank : 0x24 */
  2765. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
  2766. data[0] = 0x0B;
  2767. data[1] = 0x72;
  2768. cxd2841er_write_regs(priv, I2C_SLVT, 0x34, data, 2);
  2769. data[0] = 0x93;
  2770. data[1] = 0xF3;
  2771. data[2] = 0x00;
  2772. cxd2841er_write_regs(priv, I2C_SLVT, 0xD2, data, 3);
  2773. data[0] = 0x05;
  2774. data[1] = 0xB8;
  2775. data[2] = 0xD8;
  2776. cxd2841er_write_regs(priv, I2C_SLVT, 0xDD, data, 3);
  2777. cxd2841er_write_reg(priv, I2C_SLVT, 0xE0, 0x00);
  2778. /* Set SLV-T Bank : 0x25 */
  2779. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x25);
  2780. cxd2841er_write_reg(priv, I2C_SLVT, 0xED, 0x60);
  2781. /* Set SLV-T Bank : 0x27 */
  2782. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
  2783. cxd2841er_write_reg(priv, I2C_SLVT, 0xFA, 0x34);
  2784. /* Set SLV-T Bank : 0x2B */
  2785. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2B);
  2786. cxd2841er_write_reg(priv, I2C_SLVT, 0x4B, 0x2F);
  2787. cxd2841er_write_reg(priv, I2C_SLVT, 0x9E, 0x0E);
  2788. /* Set SLV-T Bank : 0x2D */
  2789. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2D);
  2790. data[0] = 0x89;
  2791. data[1] = 0x89;
  2792. cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data, 2);
  2793. /* Set SLV-T Bank : 0x5E */
  2794. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x5E);
  2795. data[0] = 0x24;
  2796. data[1] = 0x95;
  2797. cxd2841er_write_regs(priv, I2C_SLVT, 0x8C, data, 2);
  2798. }
  2799. cxd2841er_sleep_tc_to_active_t2_band(priv, bandwidth);
  2800. /* Set SLV-T Bank : 0x00 */
  2801. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  2802. /* Disable HiZ Setting 1 */
  2803. cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
  2804. /* Disable HiZ Setting 2 */
  2805. cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
  2806. priv->state = STATE_ACTIVE_TC;
  2807. return 0;
  2808. }
  2809. /* ISDB-Tb part */
  2810. static int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
  2811. u32 bandwidth)
  2812. {
  2813. u8 data[2] = { 0x09, 0x54 };
  2814. u8 data24m[2] = {0x60, 0x00};
  2815. u8 data24m2[3] = {0xB7, 0x1B, 0x00};
  2816. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  2817. cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
  2818. /* Set SLV-X Bank : 0x00 */
  2819. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  2820. /* Set demod mode */
  2821. cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x06);
  2822. /* Set SLV-T Bank : 0x00 */
  2823. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  2824. /* Enable demod clock */
  2825. cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
  2826. /* Enable RF level monitor */
  2827. cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x01);
  2828. cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x01);
  2829. /* Enable ADC clock */
  2830. cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
  2831. /* Enable ADC 1 */
  2832. cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
  2833. /* xtal freq 20.5MHz or 24M */
  2834. cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
  2835. /* Enable ADC 4 */
  2836. cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
  2837. /* ASCOT setting ON */
  2838. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
  2839. /* FEC Auto Recovery setting */
  2840. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
  2841. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x00, 0x01);
  2842. /* ISDB-T initial setting */
  2843. /* Set SLV-T Bank : 0x00 */
  2844. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  2845. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x00, 0x01);
  2846. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x00, 0x01);
  2847. /* Set SLV-T Bank : 0x10 */
  2848. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2849. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x69, 0x04, 0x07);
  2850. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x6B, 0x03, 0x07);
  2851. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9D, 0x50, 0xFF);
  2852. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xD3, 0x06, 0x1F);
  2853. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xED, 0x00, 0x01);
  2854. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE2, 0xCE, 0x80);
  2855. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xF2, 0x13, 0x10);
  2856. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x2E, 0x3F);
  2857. /* Set SLV-T Bank : 0x15 */
  2858. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
  2859. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x02, 0x03);
  2860. /* Set SLV-T Bank : 0x1E */
  2861. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x1E);
  2862. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x73, 0x68, 0xFF);
  2863. /* Set SLV-T Bank : 0x63 */
  2864. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x63);
  2865. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x81, 0x00, 0x01);
  2866. /* for xtal 24MHz */
  2867. /* Set SLV-T Bank : 0x10 */
  2868. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2869. cxd2841er_write_regs(priv, I2C_SLVT, 0xBF, data24m, 2);
  2870. /* Set SLV-T Bank : 0x60 */
  2871. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
  2872. cxd2841er_write_regs(priv, I2C_SLVT, 0xA8, data24m2, 3);
  2873. cxd2841er_sleep_tc_to_active_i_band(priv, bandwidth);
  2874. /* Set SLV-T Bank : 0x00 */
  2875. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  2876. /* Disable HiZ Setting 1 */
  2877. cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
  2878. /* Disable HiZ Setting 2 */
  2879. cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
  2880. priv->state = STATE_ACTIVE_TC;
  2881. return 0;
  2882. }
  2883. static int cxd2841er_sleep_tc_to_active_c(struct cxd2841er_priv *priv,
  2884. u32 bandwidth)
  2885. {
  2886. u8 data[2] = { 0x09, 0x54 };
  2887. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  2888. cxd2841er_set_ts_clock_mode(priv, SYS_DVBC_ANNEX_A);
  2889. /* Set SLV-X Bank : 0x00 */
  2890. cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
  2891. /* Set demod mode */
  2892. cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x04);
  2893. /* Set SLV-T Bank : 0x00 */
  2894. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  2895. /* Enable demod clock */
  2896. cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
  2897. /* Disable RF level monitor */
  2898. cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
  2899. cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
  2900. /* Enable ADC clock */
  2901. cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
  2902. /* Enable ADC 1 */
  2903. cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
  2904. /* xtal freq 20.5MHz */
  2905. cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
  2906. /* Enable ADC 4 */
  2907. cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
  2908. /* Set SLV-T Bank : 0x10 */
  2909. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2910. /* IFAGC gain settings */
  2911. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x09, 0x1f);
  2912. /* Set SLV-T Bank : 0x11 */
  2913. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
  2914. /* BBAGC TARGET level setting */
  2915. cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x48);
  2916. /* Set SLV-T Bank : 0x10 */
  2917. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  2918. /* ASCOT setting ON */
  2919. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
  2920. /* Set SLV-T Bank : 0x40 */
  2921. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
  2922. /* Demod setting */
  2923. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc3, 0x00, 0x04);
  2924. /* Set SLV-T Bank : 0x00 */
  2925. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  2926. /* TSIF setting */
  2927. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
  2928. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
  2929. cxd2841er_sleep_tc_to_active_c_band(priv, bandwidth);
  2930. /* Set SLV-T Bank : 0x00 */
  2931. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  2932. /* Disable HiZ Setting 1 */
  2933. cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
  2934. /* Disable HiZ Setting 2 */
  2935. cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
  2936. priv->state = STATE_ACTIVE_TC;
  2937. return 0;
  2938. }
  2939. static int cxd2841er_get_frontend(struct dvb_frontend *fe,
  2940. struct dtv_frontend_properties *p)
  2941. {
  2942. enum fe_status status = 0;
  2943. struct cxd2841er_priv *priv = fe->demodulator_priv;
  2944. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  2945. if (priv->state == STATE_ACTIVE_S)
  2946. cxd2841er_read_status_s(fe, &status);
  2947. else if (priv->state == STATE_ACTIVE_TC)
  2948. cxd2841er_read_status_tc(fe, &status);
  2949. cxd2841er_read_signal_strength(fe);
  2950. if (status & FE_HAS_LOCK) {
  2951. cxd2841er_read_snr(fe);
  2952. cxd2841er_read_ucblocks(fe);
  2953. cxd2841er_read_ber(fe);
  2954. } else {
  2955. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  2956. p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  2957. p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  2958. p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  2959. }
  2960. return 0;
  2961. }
  2962. static int cxd2841er_set_frontend_s(struct dvb_frontend *fe)
  2963. {
  2964. int ret = 0, i, timeout, carr_offset;
  2965. enum fe_status status;
  2966. struct cxd2841er_priv *priv = fe->demodulator_priv;
  2967. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  2968. u32 symbol_rate = p->symbol_rate/1000;
  2969. dev_dbg(&priv->i2c->dev, "%s(): %s frequency=%d symbol_rate=%d xtal=%d\n",
  2970. __func__,
  2971. (p->delivery_system == SYS_DVBS ? "DVB-S" : "DVB-S2"),
  2972. p->frequency, symbol_rate, priv->xtal);
  2973. switch (priv->state) {
  2974. case STATE_SLEEP_S:
  2975. ret = cxd2841er_sleep_s_to_active_s(
  2976. priv, p->delivery_system, symbol_rate);
  2977. break;
  2978. case STATE_ACTIVE_S:
  2979. ret = cxd2841er_retune_active(priv, p);
  2980. break;
  2981. default:
  2982. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  2983. __func__, priv->state);
  2984. ret = -EINVAL;
  2985. goto done;
  2986. }
  2987. if (ret) {
  2988. dev_dbg(&priv->i2c->dev, "%s(): tune failed\n", __func__);
  2989. goto done;
  2990. }
  2991. if (fe->ops.i2c_gate_ctrl)
  2992. fe->ops.i2c_gate_ctrl(fe, 1);
  2993. if (fe->ops.tuner_ops.set_params)
  2994. fe->ops.tuner_ops.set_params(fe);
  2995. if (fe->ops.i2c_gate_ctrl)
  2996. fe->ops.i2c_gate_ctrl(fe, 0);
  2997. cxd2841er_tune_done(priv);
  2998. timeout = ((3000000 + (symbol_rate - 1)) / symbol_rate) + 150;
  2999. for (i = 0; i < timeout / CXD2841ER_DVBS_POLLING_INVL; i++) {
  3000. usleep_range(CXD2841ER_DVBS_POLLING_INVL*1000,
  3001. (CXD2841ER_DVBS_POLLING_INVL + 2) * 1000);
  3002. cxd2841er_read_status_s(fe, &status);
  3003. if (status & FE_HAS_LOCK)
  3004. break;
  3005. }
  3006. if (status & FE_HAS_LOCK) {
  3007. if (cxd2841er_get_carrier_offset_s_s2(
  3008. priv, &carr_offset)) {
  3009. ret = -EINVAL;
  3010. goto done;
  3011. }
  3012. dev_dbg(&priv->i2c->dev, "%s(): carrier_offset=%d\n",
  3013. __func__, carr_offset);
  3014. }
  3015. done:
  3016. /* Reset stats */
  3017. p->strength.stat[0].scale = FE_SCALE_RELATIVE;
  3018. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3019. p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3020. p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3021. p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3022. return ret;
  3023. }
  3024. static int cxd2841er_set_frontend_tc(struct dvb_frontend *fe)
  3025. {
  3026. int ret = 0, timeout;
  3027. enum fe_status status;
  3028. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3029. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  3030. dev_dbg(&priv->i2c->dev, "%s() delivery_system=%d bandwidth_hz=%d\n",
  3031. __func__, p->delivery_system, p->bandwidth_hz);
  3032. if (p->delivery_system == SYS_DVBT) {
  3033. priv->system = SYS_DVBT;
  3034. switch (priv->state) {
  3035. case STATE_SLEEP_TC:
  3036. ret = cxd2841er_sleep_tc_to_active_t(
  3037. priv, p->bandwidth_hz);
  3038. break;
  3039. case STATE_ACTIVE_TC:
  3040. ret = cxd2841er_retune_active(priv, p);
  3041. break;
  3042. default:
  3043. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  3044. __func__, priv->state);
  3045. ret = -EINVAL;
  3046. }
  3047. } else if (p->delivery_system == SYS_DVBT2) {
  3048. priv->system = SYS_DVBT2;
  3049. cxd2841er_dvbt2_set_plp_config(priv,
  3050. (int)(p->stream_id > 255), p->stream_id);
  3051. cxd2841er_dvbt2_set_profile(priv, DVBT2_PROFILE_BASE);
  3052. switch (priv->state) {
  3053. case STATE_SLEEP_TC:
  3054. ret = cxd2841er_sleep_tc_to_active_t2(priv,
  3055. p->bandwidth_hz);
  3056. break;
  3057. case STATE_ACTIVE_TC:
  3058. ret = cxd2841er_retune_active(priv, p);
  3059. break;
  3060. default:
  3061. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  3062. __func__, priv->state);
  3063. ret = -EINVAL;
  3064. }
  3065. } else if (p->delivery_system == SYS_ISDBT) {
  3066. priv->system = SYS_ISDBT;
  3067. switch (priv->state) {
  3068. case STATE_SLEEP_TC:
  3069. ret = cxd2841er_sleep_tc_to_active_i(
  3070. priv, p->bandwidth_hz);
  3071. break;
  3072. case STATE_ACTIVE_TC:
  3073. ret = cxd2841er_retune_active(priv, p);
  3074. break;
  3075. default:
  3076. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  3077. __func__, priv->state);
  3078. ret = -EINVAL;
  3079. }
  3080. } else if (p->delivery_system == SYS_DVBC_ANNEX_A ||
  3081. p->delivery_system == SYS_DVBC_ANNEX_C) {
  3082. priv->system = SYS_DVBC_ANNEX_A;
  3083. /* correct bandwidth */
  3084. if (p->bandwidth_hz != 6000000 &&
  3085. p->bandwidth_hz != 7000000 &&
  3086. p->bandwidth_hz != 8000000) {
  3087. p->bandwidth_hz = 8000000;
  3088. dev_dbg(&priv->i2c->dev, "%s(): forcing bandwidth to %d\n",
  3089. __func__, p->bandwidth_hz);
  3090. }
  3091. switch (priv->state) {
  3092. case STATE_SLEEP_TC:
  3093. ret = cxd2841er_sleep_tc_to_active_c(
  3094. priv, p->bandwidth_hz);
  3095. break;
  3096. case STATE_ACTIVE_TC:
  3097. ret = cxd2841er_retune_active(priv, p);
  3098. break;
  3099. default:
  3100. dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
  3101. __func__, priv->state);
  3102. ret = -EINVAL;
  3103. }
  3104. } else {
  3105. dev_dbg(&priv->i2c->dev,
  3106. "%s(): invalid delivery system %d\n",
  3107. __func__, p->delivery_system);
  3108. ret = -EINVAL;
  3109. }
  3110. if (ret)
  3111. goto done;
  3112. if (fe->ops.i2c_gate_ctrl)
  3113. fe->ops.i2c_gate_ctrl(fe, 1);
  3114. if (fe->ops.tuner_ops.set_params)
  3115. fe->ops.tuner_ops.set_params(fe);
  3116. if (fe->ops.i2c_gate_ctrl)
  3117. fe->ops.i2c_gate_ctrl(fe, 0);
  3118. cxd2841er_tune_done(priv);
  3119. timeout = 2500;
  3120. while (timeout > 0) {
  3121. ret = cxd2841er_read_status_tc(fe, &status);
  3122. if (ret)
  3123. goto done;
  3124. if (status & FE_HAS_LOCK)
  3125. break;
  3126. msleep(20);
  3127. timeout -= 20;
  3128. }
  3129. if (timeout < 0)
  3130. dev_dbg(&priv->i2c->dev,
  3131. "%s(): LOCK wait timeout\n", __func__);
  3132. done:
  3133. return ret;
  3134. }
  3135. static int cxd2841er_tune_s(struct dvb_frontend *fe,
  3136. bool re_tune,
  3137. unsigned int mode_flags,
  3138. unsigned int *delay,
  3139. enum fe_status *status)
  3140. {
  3141. int ret, carrier_offset;
  3142. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3143. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  3144. dev_dbg(&priv->i2c->dev, "%s() re_tune=%d\n", __func__, re_tune);
  3145. if (re_tune) {
  3146. ret = cxd2841er_set_frontend_s(fe);
  3147. if (ret)
  3148. return ret;
  3149. cxd2841er_read_status_s(fe, status);
  3150. if (*status & FE_HAS_LOCK) {
  3151. if (cxd2841er_get_carrier_offset_s_s2(
  3152. priv, &carrier_offset))
  3153. return -EINVAL;
  3154. p->frequency += carrier_offset;
  3155. ret = cxd2841er_set_frontend_s(fe);
  3156. if (ret)
  3157. return ret;
  3158. }
  3159. }
  3160. *delay = HZ / 5;
  3161. return cxd2841er_read_status_s(fe, status);
  3162. }
  3163. static int cxd2841er_tune_tc(struct dvb_frontend *fe,
  3164. bool re_tune,
  3165. unsigned int mode_flags,
  3166. unsigned int *delay,
  3167. enum fe_status *status)
  3168. {
  3169. int ret, carrier_offset;
  3170. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3171. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  3172. dev_dbg(&priv->i2c->dev, "%s(): re_tune %d bandwidth=%d\n", __func__,
  3173. re_tune, p->bandwidth_hz);
  3174. if (re_tune) {
  3175. ret = cxd2841er_set_frontend_tc(fe);
  3176. if (ret)
  3177. return ret;
  3178. cxd2841er_read_status_tc(fe, status);
  3179. if (*status & FE_HAS_LOCK) {
  3180. switch (priv->system) {
  3181. case SYS_ISDBT:
  3182. ret = cxd2841er_get_carrier_offset_i(
  3183. priv, p->bandwidth_hz,
  3184. &carrier_offset);
  3185. if (ret)
  3186. return ret;
  3187. break;
  3188. case SYS_DVBT:
  3189. ret = cxd2841er_get_carrier_offset_t(
  3190. priv, p->bandwidth_hz,
  3191. &carrier_offset);
  3192. if (ret)
  3193. return ret;
  3194. break;
  3195. case SYS_DVBT2:
  3196. ret = cxd2841er_get_carrier_offset_t2(
  3197. priv, p->bandwidth_hz,
  3198. &carrier_offset);
  3199. if (ret)
  3200. return ret;
  3201. break;
  3202. case SYS_DVBC_ANNEX_A:
  3203. ret = cxd2841er_get_carrier_offset_c(
  3204. priv, &carrier_offset);
  3205. if (ret)
  3206. return ret;
  3207. break;
  3208. default:
  3209. dev_dbg(&priv->i2c->dev,
  3210. "%s(): invalid delivery system %d\n",
  3211. __func__, priv->system);
  3212. return -EINVAL;
  3213. }
  3214. dev_dbg(&priv->i2c->dev, "%s(): carrier offset %d\n",
  3215. __func__, carrier_offset);
  3216. p->frequency += carrier_offset;
  3217. ret = cxd2841er_set_frontend_tc(fe);
  3218. if (ret)
  3219. return ret;
  3220. }
  3221. }
  3222. *delay = HZ / 5;
  3223. return cxd2841er_read_status_tc(fe, status);
  3224. }
  3225. static int cxd2841er_sleep_s(struct dvb_frontend *fe)
  3226. {
  3227. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3228. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  3229. cxd2841er_active_s_to_sleep_s(fe->demodulator_priv);
  3230. cxd2841er_sleep_s_to_shutdown(fe->demodulator_priv);
  3231. return 0;
  3232. }
  3233. static int cxd2841er_sleep_tc(struct dvb_frontend *fe)
  3234. {
  3235. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3236. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  3237. if (priv->state == STATE_ACTIVE_TC) {
  3238. switch (priv->system) {
  3239. case SYS_DVBT:
  3240. cxd2841er_active_t_to_sleep_tc(priv);
  3241. break;
  3242. case SYS_DVBT2:
  3243. cxd2841er_active_t2_to_sleep_tc(priv);
  3244. break;
  3245. case SYS_ISDBT:
  3246. cxd2841er_active_i_to_sleep_tc(priv);
  3247. break;
  3248. case SYS_DVBC_ANNEX_A:
  3249. cxd2841er_active_c_to_sleep_tc(priv);
  3250. break;
  3251. default:
  3252. dev_warn(&priv->i2c->dev,
  3253. "%s(): unknown delivery system %d\n",
  3254. __func__, priv->system);
  3255. }
  3256. }
  3257. if (priv->state != STATE_SLEEP_TC) {
  3258. dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
  3259. __func__, priv->state);
  3260. return -EINVAL;
  3261. }
  3262. cxd2841er_sleep_tc_to_shutdown(priv);
  3263. return 0;
  3264. }
  3265. static int cxd2841er_send_burst(struct dvb_frontend *fe,
  3266. enum fe_sec_mini_cmd burst)
  3267. {
  3268. u8 data;
  3269. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3270. dev_dbg(&priv->i2c->dev, "%s(): burst mode %s\n", __func__,
  3271. (burst == SEC_MINI_A ? "A" : "B"));
  3272. if (priv->state != STATE_SLEEP_S &&
  3273. priv->state != STATE_ACTIVE_S) {
  3274. dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
  3275. __func__, priv->state);
  3276. return -EINVAL;
  3277. }
  3278. data = (burst == SEC_MINI_A ? 0 : 1);
  3279. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
  3280. cxd2841er_write_reg(priv, I2C_SLVT, 0x34, 0x01);
  3281. cxd2841er_write_reg(priv, I2C_SLVT, 0x35, data);
  3282. return 0;
  3283. }
  3284. static int cxd2841er_set_tone(struct dvb_frontend *fe,
  3285. enum fe_sec_tone_mode tone)
  3286. {
  3287. u8 data;
  3288. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3289. dev_dbg(&priv->i2c->dev, "%s(): tone %s\n", __func__,
  3290. (tone == SEC_TONE_ON ? "On" : "Off"));
  3291. if (priv->state != STATE_SLEEP_S &&
  3292. priv->state != STATE_ACTIVE_S) {
  3293. dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
  3294. __func__, priv->state);
  3295. return -EINVAL;
  3296. }
  3297. data = (tone == SEC_TONE_ON ? 1 : 0);
  3298. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
  3299. cxd2841er_write_reg(priv, I2C_SLVT, 0x36, data);
  3300. return 0;
  3301. }
  3302. static int cxd2841er_send_diseqc_msg(struct dvb_frontend *fe,
  3303. struct dvb_diseqc_master_cmd *cmd)
  3304. {
  3305. int i;
  3306. u8 data[12];
  3307. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3308. if (priv->state != STATE_SLEEP_S &&
  3309. priv->state != STATE_ACTIVE_S) {
  3310. dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
  3311. __func__, priv->state);
  3312. return -EINVAL;
  3313. }
  3314. dev_dbg(&priv->i2c->dev,
  3315. "%s(): cmd->len %d\n", __func__, cmd->msg_len);
  3316. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
  3317. /* DiDEqC enable */
  3318. cxd2841er_write_reg(priv, I2C_SLVT, 0x33, 0x01);
  3319. /* cmd1 length & data */
  3320. cxd2841er_write_reg(priv, I2C_SLVT, 0x3d, cmd->msg_len);
  3321. memset(data, 0, sizeof(data));
  3322. for (i = 0; i < cmd->msg_len && i < sizeof(data); i++)
  3323. data[i] = cmd->msg[i];
  3324. cxd2841er_write_regs(priv, I2C_SLVT, 0x3e, data, sizeof(data));
  3325. /* repeat count for cmd1 */
  3326. cxd2841er_write_reg(priv, I2C_SLVT, 0x37, 1);
  3327. /* repeat count for cmd2: always 0 */
  3328. cxd2841er_write_reg(priv, I2C_SLVT, 0x38, 0);
  3329. /* start transmit */
  3330. cxd2841er_write_reg(priv, I2C_SLVT, 0x32, 0x01);
  3331. /* wait for 1 sec timeout */
  3332. for (i = 0; i < 50; i++) {
  3333. cxd2841er_read_reg(priv, I2C_SLVT, 0x10, data);
  3334. if (!data[0]) {
  3335. dev_dbg(&priv->i2c->dev,
  3336. "%s(): DiSEqC cmd has been sent\n", __func__);
  3337. return 0;
  3338. }
  3339. msleep(20);
  3340. }
  3341. dev_dbg(&priv->i2c->dev,
  3342. "%s(): DiSEqC cmd transmit timeout\n", __func__);
  3343. return -ETIMEDOUT;
  3344. }
  3345. static void cxd2841er_release(struct dvb_frontend *fe)
  3346. {
  3347. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3348. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  3349. kfree(priv);
  3350. }
  3351. static int cxd2841er_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  3352. {
  3353. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3354. dev_dbg(&priv->i2c->dev, "%s(): enable=%d\n", __func__, enable);
  3355. cxd2841er_set_reg_bits(
  3356. priv, I2C_SLVX, 0x8, (enable ? 0x01 : 0x00), 0x01);
  3357. return 0;
  3358. }
  3359. static enum dvbfe_algo cxd2841er_get_algo(struct dvb_frontend *fe)
  3360. {
  3361. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3362. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  3363. return DVBFE_ALGO_HW;
  3364. }
  3365. static void cxd2841er_init_stats(struct dvb_frontend *fe)
  3366. {
  3367. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  3368. p->strength.len = 1;
  3369. p->strength.stat[0].scale = FE_SCALE_RELATIVE;
  3370. p->cnr.len = 1;
  3371. p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3372. p->block_error.len = 1;
  3373. p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3374. p->post_bit_error.len = 1;
  3375. p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3376. p->post_bit_count.len = 1;
  3377. p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3378. }
  3379. static int cxd2841er_init_s(struct dvb_frontend *fe)
  3380. {
  3381. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3382. /* sanity. force demod to SHUTDOWN state */
  3383. if (priv->state == STATE_SLEEP_S) {
  3384. dev_dbg(&priv->i2c->dev, "%s() forcing sleep->shutdown\n",
  3385. __func__);
  3386. cxd2841er_sleep_s_to_shutdown(priv);
  3387. } else if (priv->state == STATE_ACTIVE_S) {
  3388. dev_dbg(&priv->i2c->dev, "%s() forcing active->sleep->shutdown\n",
  3389. __func__);
  3390. cxd2841er_active_s_to_sleep_s(priv);
  3391. cxd2841er_sleep_s_to_shutdown(priv);
  3392. }
  3393. dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
  3394. cxd2841er_shutdown_to_sleep_s(priv);
  3395. /* SONY_DEMOD_CONFIG_SAT_IFAGCNEG set to 1 */
  3396. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
  3397. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xb9, 0x01, 0x01);
  3398. cxd2841er_init_stats(fe);
  3399. return 0;
  3400. }
  3401. static int cxd2841er_init_tc(struct dvb_frontend *fe)
  3402. {
  3403. struct cxd2841er_priv *priv = fe->demodulator_priv;
  3404. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  3405. dev_dbg(&priv->i2c->dev, "%s() bandwidth_hz=%d\n",
  3406. __func__, p->bandwidth_hz);
  3407. cxd2841er_shutdown_to_sleep_tc(priv);
  3408. /* SONY_DEMOD_CONFIG_IFAGCNEG = 1 */
  3409. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
  3410. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcb, 0x40, 0x40);
  3411. /* SONY_DEMOD_CONFIG_IFAGC_ADC_FS = 0 */
  3412. cxd2841er_write_reg(priv, I2C_SLVT, 0xcd, 0x50);
  3413. /* SONY_DEMOD_CONFIG_PARALLEL_SEL = 1 */
  3414. cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
  3415. cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc4, 0x00, 0x80);
  3416. cxd2841er_init_stats(fe);
  3417. return 0;
  3418. }
  3419. static struct dvb_frontend_ops cxd2841er_dvbs_s2_ops;
  3420. static struct dvb_frontend_ops cxd2841er_t_c_ops;
  3421. static struct dvb_frontend *cxd2841er_attach(struct cxd2841er_config *cfg,
  3422. struct i2c_adapter *i2c,
  3423. u8 system)
  3424. {
  3425. u8 chip_id = 0;
  3426. const char *type;
  3427. const char *name;
  3428. struct cxd2841er_priv *priv = NULL;
  3429. /* allocate memory for the internal state */
  3430. priv = kzalloc(sizeof(struct cxd2841er_priv), GFP_KERNEL);
  3431. if (!priv)
  3432. return NULL;
  3433. priv->i2c = i2c;
  3434. priv->config = cfg;
  3435. priv->i2c_addr_slvx = (cfg->i2c_addr + 4) >> 1;
  3436. priv->i2c_addr_slvt = (cfg->i2c_addr) >> 1;
  3437. priv->xtal = cfg->xtal;
  3438. priv->frontend.demodulator_priv = priv;
  3439. dev_info(&priv->i2c->dev,
  3440. "%s(): I2C adapter %p SLVX addr %x SLVT addr %x\n",
  3441. __func__, priv->i2c,
  3442. priv->i2c_addr_slvx, priv->i2c_addr_slvt);
  3443. chip_id = cxd2841er_chip_id(priv);
  3444. switch (chip_id) {
  3445. case CXD2841ER_CHIP_ID:
  3446. snprintf(cxd2841er_t_c_ops.info.name, 128,
  3447. "Sony CXD2841ER DVB-T/T2/C demodulator");
  3448. name = "CXD2841ER";
  3449. break;
  3450. case CXD2854ER_CHIP_ID:
  3451. snprintf(cxd2841er_t_c_ops.info.name, 128,
  3452. "Sony CXD2854ER DVB-T/T2/C and ISDB-T demodulator");
  3453. cxd2841er_t_c_ops.delsys[3] = SYS_ISDBT;
  3454. name = "CXD2854ER";
  3455. break;
  3456. default:
  3457. dev_err(&priv->i2c->dev, "%s(): invalid chip ID 0x%02x\n",
  3458. __func__, chip_id);
  3459. priv->frontend.demodulator_priv = NULL;
  3460. kfree(priv);
  3461. return NULL;
  3462. }
  3463. /* create dvb_frontend */
  3464. if (system == SYS_DVBS) {
  3465. memcpy(&priv->frontend.ops,
  3466. &cxd2841er_dvbs_s2_ops,
  3467. sizeof(struct dvb_frontend_ops));
  3468. type = "S/S2";
  3469. } else {
  3470. memcpy(&priv->frontend.ops,
  3471. &cxd2841er_t_c_ops,
  3472. sizeof(struct dvb_frontend_ops));
  3473. type = "T/T2/C/ISDB-T";
  3474. }
  3475. dev_info(&priv->i2c->dev,
  3476. "%s(): attaching %s DVB-%s frontend\n",
  3477. __func__, name, type);
  3478. dev_info(&priv->i2c->dev, "%s(): chip ID 0x%02x OK.\n",
  3479. __func__, chip_id);
  3480. return &priv->frontend;
  3481. }
  3482. struct dvb_frontend *cxd2841er_attach_s(struct cxd2841er_config *cfg,
  3483. struct i2c_adapter *i2c)
  3484. {
  3485. return cxd2841er_attach(cfg, i2c, SYS_DVBS);
  3486. }
  3487. EXPORT_SYMBOL(cxd2841er_attach_s);
  3488. struct dvb_frontend *cxd2841er_attach_t_c(struct cxd2841er_config *cfg,
  3489. struct i2c_adapter *i2c)
  3490. {
  3491. return cxd2841er_attach(cfg, i2c, 0);
  3492. }
  3493. EXPORT_SYMBOL(cxd2841er_attach_t_c);
  3494. static struct dvb_frontend_ops cxd2841er_dvbs_s2_ops = {
  3495. .delsys = { SYS_DVBS, SYS_DVBS2 },
  3496. .info = {
  3497. .name = "Sony CXD2841ER DVB-S/S2 demodulator",
  3498. .frequency_min = 500000,
  3499. .frequency_max = 2500000,
  3500. .frequency_stepsize = 0,
  3501. .symbol_rate_min = 1000000,
  3502. .symbol_rate_max = 45000000,
  3503. .symbol_rate_tolerance = 500,
  3504. .caps = FE_CAN_INVERSION_AUTO |
  3505. FE_CAN_FEC_AUTO |
  3506. FE_CAN_QPSK,
  3507. },
  3508. .init = cxd2841er_init_s,
  3509. .sleep = cxd2841er_sleep_s,
  3510. .release = cxd2841er_release,
  3511. .set_frontend = cxd2841er_set_frontend_s,
  3512. .get_frontend = cxd2841er_get_frontend,
  3513. .read_status = cxd2841er_read_status_s,
  3514. .i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
  3515. .get_frontend_algo = cxd2841er_get_algo,
  3516. .set_tone = cxd2841er_set_tone,
  3517. .diseqc_send_burst = cxd2841er_send_burst,
  3518. .diseqc_send_master_cmd = cxd2841er_send_diseqc_msg,
  3519. .tune = cxd2841er_tune_s
  3520. };
  3521. static struct dvb_frontend_ops cxd2841er_t_c_ops = {
  3522. .delsys = { SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A },
  3523. .info = {
  3524. .name = "", /* will set in attach function */
  3525. .caps = FE_CAN_FEC_1_2 |
  3526. FE_CAN_FEC_2_3 |
  3527. FE_CAN_FEC_3_4 |
  3528. FE_CAN_FEC_5_6 |
  3529. FE_CAN_FEC_7_8 |
  3530. FE_CAN_FEC_AUTO |
  3531. FE_CAN_QPSK |
  3532. FE_CAN_QAM_16 |
  3533. FE_CAN_QAM_32 |
  3534. FE_CAN_QAM_64 |
  3535. FE_CAN_QAM_128 |
  3536. FE_CAN_QAM_256 |
  3537. FE_CAN_QAM_AUTO |
  3538. FE_CAN_TRANSMISSION_MODE_AUTO |
  3539. FE_CAN_GUARD_INTERVAL_AUTO |
  3540. FE_CAN_HIERARCHY_AUTO |
  3541. FE_CAN_MUTE_TS |
  3542. FE_CAN_2G_MODULATION,
  3543. .frequency_min = 42000000,
  3544. .frequency_max = 1002000000,
  3545. .symbol_rate_min = 870000,
  3546. .symbol_rate_max = 11700000
  3547. },
  3548. .init = cxd2841er_init_tc,
  3549. .sleep = cxd2841er_sleep_tc,
  3550. .release = cxd2841er_release,
  3551. .set_frontend = cxd2841er_set_frontend_tc,
  3552. .get_frontend = cxd2841er_get_frontend,
  3553. .read_status = cxd2841er_read_status_tc,
  3554. .tune = cxd2841er_tune_tc,
  3555. .i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
  3556. .get_frontend_algo = cxd2841er_get_algo
  3557. };
  3558. MODULE_DESCRIPTION("Sony CXD2841ER/CXD2854ER DVB-C/C2/T/T2/S/S2 demodulator driver");
  3559. MODULE_AUTHOR("Sergey Kozlov <serjk@netup.ru>, Abylay Ospan <aospan@netup.ru>");
  3560. MODULE_LICENSE("GPL");