ds90ub960.c 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the Texas Instruments DS90UB960-Q1 video deserializer
  4. *
  5. * Copyright (c) 2019 Luca Ceresoli <luca@lucaceresoli.net>
  6. * Copyright (c) 2023 Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
  7. */
  8. /*
  9. * (Possible) TODOs:
  10. *
  11. * - PM for serializer and remote peripherals. We need to manage:
  12. * - VPOC
  13. * - Power domain? Regulator? Somehow any remote device should be able to
  14. * cause the VPOC to be turned on.
  15. * - Link between the deserializer and the serializer
  16. * - Related to VPOC management. We probably always want to turn on the VPOC
  17. * and then enable the link.
  18. * - Serializer's services: i2c, gpios, power
  19. * - The serializer needs to resume before the remote peripherals can
  20. * e.g. use the i2c.
  21. * - How to handle gpios? Reserving a gpio essentially keeps the provider
  22. * (serializer) always powered on.
  23. * - Do we need a new bus for the FPD-Link? At the moment the serializers
  24. * are children of the same i2c-adapter where the deserializer resides.
  25. * - i2c-atr could be made embeddable instead of allocatable.
  26. */
  27. #include <linux/bitops.h>
  28. #include <linux/cleanup.h>
  29. #include <linux/clk.h>
  30. #include <linux/delay.h>
  31. #include <linux/fwnode.h>
  32. #include <linux/gpio/consumer.h>
  33. #include <linux/i2c-atr.h>
  34. #include <linux/i2c.h>
  35. #include <linux/init.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/kernel.h>
  38. #include <linux/kthread.h>
  39. #include <linux/module.h>
  40. #include <linux/mutex.h>
  41. #include <linux/property.h>
  42. #include <linux/regmap.h>
  43. #include <linux/regulator/consumer.h>
  44. #include <linux/slab.h>
  45. #include <linux/units.h>
  46. #include <linux/workqueue.h>
  47. #include <media/i2c/ds90ub9xx.h>
  48. #include <media/mipi-csi2.h>
  49. #include <media/v4l2-ctrls.h>
  50. #include <media/v4l2-fwnode.h>
  51. #include <media/v4l2-subdev.h>
  52. #include "ds90ub953.h"
  53. #define MHZ(v) ((u32)((v) * HZ_PER_MHZ))
  54. /*
  55. * If this is defined, the i2c addresses from UB960_DEBUG_I2C_RX_ID to
  56. * UB960_DEBUG_I2C_RX_ID + 3 can be used to access the paged RX port registers
  57. * directly.
  58. *
  59. * Only for debug purposes.
  60. */
  61. /* #define UB960_DEBUG_I2C_RX_ID 0x40 */
  62. #define UB960_POLL_TIME_MS 500
  63. #define UB960_MAX_RX_NPORTS 4
  64. #define UB960_MAX_TX_NPORTS 2
  65. #define UB960_MAX_NPORTS (UB960_MAX_RX_NPORTS + UB960_MAX_TX_NPORTS)
  66. #define UB960_MAX_PORT_ALIASES 8
  67. #define UB960_NUM_BC_GPIOS 4
  68. /*
  69. * Register map
  70. *
  71. * 0x00-0x32 Shared (UB960_SR)
  72. * 0x33-0x3a CSI-2 TX (per-port paged on DS90UB960, shared on 954) (UB960_TR)
  73. * 0x4c Shared (UB960_SR)
  74. * 0x4d-0x7f FPD-Link RX, per-port paged (UB960_RR)
  75. * 0xb0-0xbf Shared (UB960_SR)
  76. * 0xd0-0xdf FPD-Link RX, per-port paged (UB960_RR)
  77. * 0xf0-0xf5 Shared (UB960_SR)
  78. * 0xf8-0xfb Shared (UB960_SR)
  79. * All others Reserved
  80. *
  81. * Register prefixes:
  82. * UB960_SR_* = Shared register
  83. * UB960_RR_* = FPD-Link RX, per-port paged register
  84. * UB960_TR_* = CSI-2 TX, per-port paged register
  85. * UB960_XR_* = Reserved register
  86. * UB960_IR_* = Indirect register
  87. */
  88. #define UB960_SR_I2C_DEV_ID 0x00
  89. #define UB960_SR_RESET 0x01
  90. #define UB960_SR_RESET_DIGITAL_RESET1 BIT(1)
  91. #define UB960_SR_RESET_DIGITAL_RESET0 BIT(0)
  92. #define UB960_SR_RESET_GPIO_LOCK_RELEASE BIT(5)
  93. #define UB960_SR_GEN_CONFIG 0x02
  94. #define UB960_SR_REV_MASK 0x03
  95. #define UB960_SR_DEVICE_STS 0x04
  96. #define UB960_SR_PAR_ERR_THOLD_HI 0x05
  97. #define UB960_SR_PAR_ERR_THOLD_LO 0x06
  98. #define UB960_SR_BCC_WDOG_CTL 0x07
  99. #define UB960_SR_I2C_CTL1 0x08
  100. #define UB960_SR_I2C_CTL2 0x09
  101. #define UB960_SR_SCL_HIGH_TIME 0x0a
  102. #define UB960_SR_SCL_LOW_TIME 0x0b
  103. #define UB960_SR_RX_PORT_CTL 0x0c
  104. #define UB960_SR_IO_CTL 0x0d
  105. #define UB960_SR_GPIO_PIN_STS 0x0e
  106. #define UB960_SR_GPIO_INPUT_CTL 0x0f
  107. #define UB960_SR_GPIO_PIN_CTL(n) (0x10 + (n)) /* n < UB960_NUM_GPIOS */
  108. #define UB960_SR_GPIO_PIN_CTL_GPIO_OUT_SEL 5
  109. #define UB960_SR_GPIO_PIN_CTL_GPIO_OUT_SRC_SHIFT 2
  110. #define UB960_SR_GPIO_PIN_CTL_GPIO_OUT_EN BIT(0)
  111. #define UB960_SR_FS_CTL 0x18
  112. #define UB960_SR_FS_HIGH_TIME_1 0x19
  113. #define UB960_SR_FS_HIGH_TIME_0 0x1a
  114. #define UB960_SR_FS_LOW_TIME_1 0x1b
  115. #define UB960_SR_FS_LOW_TIME_0 0x1c
  116. #define UB960_SR_MAX_FRM_HI 0x1d
  117. #define UB960_SR_MAX_FRM_LO 0x1e
  118. #define UB960_SR_CSI_PLL_CTL 0x1f
  119. #define UB960_SR_FWD_CTL1 0x20
  120. #define UB960_SR_FWD_CTL1_PORT_DIS(n) BIT((n) + 4)
  121. #define UB960_SR_FWD_CTL2 0x21
  122. #define UB960_SR_FWD_STS 0x22
  123. #define UB960_SR_INTERRUPT_CTL 0x23
  124. #define UB960_SR_INTERRUPT_CTL_INT_EN BIT(7)
  125. #define UB960_SR_INTERRUPT_CTL_IE_CSI_TX0 BIT(4)
  126. #define UB960_SR_INTERRUPT_CTL_IE_RX(n) BIT((n)) /* rxport[n] IRQ */
  127. #define UB960_SR_INTERRUPT_STS 0x24
  128. #define UB960_SR_INTERRUPT_STS_INT BIT(7)
  129. #define UB960_SR_INTERRUPT_STS_IS_CSI_TX(n) BIT(4 + (n)) /* txport[n] IRQ */
  130. #define UB960_SR_INTERRUPT_STS_IS_RX(n) BIT((n)) /* rxport[n] IRQ */
  131. #define UB960_SR_TS_CONFIG 0x25
  132. #define UB960_SR_TS_CONTROL 0x26
  133. #define UB960_SR_TS_LINE_HI 0x27
  134. #define UB960_SR_TS_LINE_LO 0x28
  135. #define UB960_SR_TS_STATUS 0x29
  136. #define UB960_SR_TIMESTAMP_P0_HI 0x2a
  137. #define UB960_SR_TIMESTAMP_P0_LO 0x2b
  138. #define UB960_SR_TIMESTAMP_P1_HI 0x2c
  139. #define UB960_SR_TIMESTAMP_P1_LO 0x2d
  140. #define UB960_SR_CSI_PORT_SEL 0x32
  141. #define UB960_TR_CSI_CTL 0x33
  142. #define UB960_TR_CSI_CTL_CSI_CAL_EN BIT(6)
  143. #define UB960_TR_CSI_CTL_CSI_CONTS_CLOCK BIT(1)
  144. #define UB960_TR_CSI_CTL_CSI_ENABLE BIT(0)
  145. #define UB960_TR_CSI_CTL2 0x34
  146. #define UB960_TR_CSI_STS 0x35
  147. #define UB960_TR_CSI_TX_ICR 0x36
  148. #define UB960_TR_CSI_TX_ISR 0x37
  149. #define UB960_TR_CSI_TX_ISR_IS_CSI_SYNC_ERROR BIT(3)
  150. #define UB960_TR_CSI_TX_ISR_IS_CSI_PASS_ERROR BIT(1)
  151. #define UB960_TR_CSI_TEST_CTL 0x38
  152. #define UB960_TR_CSI_TEST_PATT_HI 0x39
  153. #define UB960_TR_CSI_TEST_PATT_LO 0x3a
  154. #define UB960_XR_SFILTER_CFG 0x41
  155. #define UB960_XR_SFILTER_CFG_SFILTER_MAX_SHIFT 4
  156. #define UB960_XR_SFILTER_CFG_SFILTER_MIN_SHIFT 0
  157. #define UB960_XR_AEQ_CTL1 0x42
  158. #define UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_FPD_CLK BIT(6)
  159. #define UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_ENCODING BIT(5)
  160. #define UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_PARITY BIT(4)
  161. #define UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_MASK \
  162. (UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_FPD_CLK | \
  163. UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_ENCODING | \
  164. UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_PARITY)
  165. #define UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN BIT(0)
  166. #define UB960_XR_AEQ_ERR_THOLD 0x43
  167. #define UB960_RR_BCC_ERR_CTL 0x46
  168. #define UB960_RR_BCC_STATUS 0x47
  169. #define UB960_RR_BCC_STATUS_SEQ_ERROR BIT(5)
  170. #define UB960_RR_BCC_STATUS_MASTER_ERR BIT(4)
  171. #define UB960_RR_BCC_STATUS_MASTER_TO BIT(3)
  172. #define UB960_RR_BCC_STATUS_SLAVE_ERR BIT(2)
  173. #define UB960_RR_BCC_STATUS_SLAVE_TO BIT(1)
  174. #define UB960_RR_BCC_STATUS_RESP_ERR BIT(0)
  175. #define UB960_RR_BCC_STATUS_ERROR_MASK \
  176. (UB960_RR_BCC_STATUS_SEQ_ERROR | UB960_RR_BCC_STATUS_MASTER_ERR | \
  177. UB960_RR_BCC_STATUS_MASTER_TO | UB960_RR_BCC_STATUS_SLAVE_ERR | \
  178. UB960_RR_BCC_STATUS_SLAVE_TO | UB960_RR_BCC_STATUS_RESP_ERR)
  179. #define UB960_RR_FPD3_CAP 0x4a
  180. #define UB960_RR_RAW_EMBED_DTYPE 0x4b
  181. #define UB960_RR_RAW_EMBED_DTYPE_LINES_SHIFT 6
  182. #define UB960_SR_FPD3_PORT_SEL 0x4c
  183. #define UB960_RR_RX_PORT_STS1 0x4d
  184. #define UB960_RR_RX_PORT_STS1_BCC_CRC_ERROR BIT(5)
  185. #define UB960_RR_RX_PORT_STS1_LOCK_STS_CHG BIT(4)
  186. #define UB960_RR_RX_PORT_STS1_BCC_SEQ_ERROR BIT(3)
  187. #define UB960_RR_RX_PORT_STS1_PARITY_ERROR BIT(2)
  188. #define UB960_RR_RX_PORT_STS1_PORT_PASS BIT(1)
  189. #define UB960_RR_RX_PORT_STS1_LOCK_STS BIT(0)
  190. #define UB960_RR_RX_PORT_STS1_ERROR_MASK \
  191. (UB960_RR_RX_PORT_STS1_BCC_CRC_ERROR | \
  192. UB960_RR_RX_PORT_STS1_BCC_SEQ_ERROR | \
  193. UB960_RR_RX_PORT_STS1_PARITY_ERROR)
  194. #define UB960_RR_RX_PORT_STS2 0x4e
  195. #define UB960_RR_RX_PORT_STS2_LINE_LEN_UNSTABLE BIT(7)
  196. #define UB960_RR_RX_PORT_STS2_LINE_LEN_CHG BIT(6)
  197. #define UB960_RR_RX_PORT_STS2_FPD3_ENCODE_ERROR BIT(5)
  198. #define UB960_RR_RX_PORT_STS2_BUFFER_ERROR BIT(4)
  199. #define UB960_RR_RX_PORT_STS2_CSI_ERROR BIT(3)
  200. #define UB960_RR_RX_PORT_STS2_FREQ_STABLE BIT(2)
  201. #define UB960_RR_RX_PORT_STS2_CABLE_FAULT BIT(1)
  202. #define UB960_RR_RX_PORT_STS2_LINE_CNT_CHG BIT(0)
  203. #define UB960_RR_RX_PORT_STS2_ERROR_MASK \
  204. UB960_RR_RX_PORT_STS2_BUFFER_ERROR
  205. #define UB960_RR_RX_FREQ_HIGH 0x4f
  206. #define UB960_RR_RX_FREQ_LOW 0x50
  207. #define UB960_RR_SENSOR_STS_0 0x51
  208. #define UB960_RR_SENSOR_STS_1 0x52
  209. #define UB960_RR_SENSOR_STS_2 0x53
  210. #define UB960_RR_SENSOR_STS_3 0x54
  211. #define UB960_RR_RX_PAR_ERR_HI 0x55
  212. #define UB960_RR_RX_PAR_ERR_LO 0x56
  213. #define UB960_RR_BIST_ERR_COUNT 0x57
  214. #define UB960_RR_BCC_CONFIG 0x58
  215. #define UB960_RR_BCC_CONFIG_BC_ALWAYS_ON BIT(4)
  216. #define UB960_RR_BCC_CONFIG_AUTO_ACK_ALL BIT(5)
  217. #define UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH BIT(6)
  218. #define UB960_RR_BCC_CONFIG_BC_FREQ_SEL_MASK GENMASK(2, 0)
  219. #define UB960_RR_DATAPATH_CTL1 0x59
  220. #define UB960_RR_DATAPATH_CTL2 0x5a
  221. #define UB960_RR_SER_ID 0x5b
  222. #define UB960_RR_SER_ID_FREEZE_DEVICE_ID BIT(0)
  223. #define UB960_RR_SER_ALIAS_ID 0x5c
  224. #define UB960_RR_SER_ALIAS_ID_AUTO_ACK BIT(0)
  225. /* For these two register sets: n < UB960_MAX_PORT_ALIASES */
  226. #define UB960_RR_SLAVE_ID(n) (0x5d + (n))
  227. #define UB960_RR_SLAVE_ALIAS(n) (0x65 + (n))
  228. #define UB960_RR_PORT_CONFIG 0x6d
  229. #define UB960_RR_PORT_CONFIG_FPD3_MODE_MASK GENMASK(1, 0)
  230. #define UB960_RR_BC_GPIO_CTL(n) (0x6e + (n)) /* n < 2 */
  231. #define UB960_RR_RAW10_ID 0x70
  232. #define UB960_RR_RAW10_ID_VC_SHIFT 6
  233. #define UB960_RR_RAW10_ID_DT_SHIFT 0
  234. #define UB960_RR_RAW12_ID 0x71
  235. #define UB960_RR_CSI_VC_MAP 0x72
  236. #define UB960_RR_CSI_VC_MAP_SHIFT(x) ((x) * 2)
  237. #define UB960_RR_LINE_COUNT_HI 0x73
  238. #define UB960_RR_LINE_COUNT_LO 0x74
  239. #define UB960_RR_LINE_LEN_1 0x75
  240. #define UB960_RR_LINE_LEN_0 0x76
  241. #define UB960_RR_FREQ_DET_CTL 0x77
  242. #define UB960_RR_MAILBOX_1 0x78
  243. #define UB960_RR_MAILBOX_2 0x79
  244. #define UB960_RR_CSI_RX_STS 0x7a
  245. #define UB960_RR_CSI_RX_STS_LENGTH_ERR BIT(3)
  246. #define UB960_RR_CSI_RX_STS_CKSUM_ERR BIT(2)
  247. #define UB960_RR_CSI_RX_STS_ECC2_ERR BIT(1)
  248. #define UB960_RR_CSI_RX_STS_ECC1_ERR BIT(0)
  249. #define UB960_RR_CSI_RX_STS_ERROR_MASK \
  250. (UB960_RR_CSI_RX_STS_LENGTH_ERR | UB960_RR_CSI_RX_STS_CKSUM_ERR | \
  251. UB960_RR_CSI_RX_STS_ECC2_ERR | UB960_RR_CSI_RX_STS_ECC1_ERR)
  252. #define UB960_RR_CSI_ERR_COUNTER 0x7b
  253. #define UB960_RR_PORT_CONFIG2 0x7c
  254. #define UB960_RR_PORT_CONFIG2_RAW10_8BIT_CTL_MASK GENMASK(7, 6)
  255. #define UB960_RR_PORT_CONFIG2_RAW10_8BIT_CTL_SHIFT 6
  256. #define UB960_RR_PORT_CONFIG2_LV_POL_LOW BIT(1)
  257. #define UB960_RR_PORT_CONFIG2_FV_POL_LOW BIT(0)
  258. #define UB960_RR_PORT_PASS_CTL 0x7d
  259. #define UB960_RR_SEN_INT_RISE_CTL 0x7e
  260. #define UB960_RR_SEN_INT_FALL_CTL 0x7f
  261. #define UB960_SR_CSI_FRAME_COUNT_HI(n) (0x90 + 8 * (n))
  262. #define UB960_SR_CSI_FRAME_COUNT_LO(n) (0x91 + 8 * (n))
  263. #define UB960_SR_CSI_FRAME_ERR_COUNT_HI(n) (0x92 + 8 * (n))
  264. #define UB960_SR_CSI_FRAME_ERR_COUNT_LO(n) (0x93 + 8 * (n))
  265. #define UB960_SR_CSI_LINE_COUNT_HI(n) (0x94 + 8 * (n))
  266. #define UB960_SR_CSI_LINE_COUNT_LO(n) (0x95 + 8 * (n))
  267. #define UB960_SR_CSI_LINE_ERR_COUNT_HI(n) (0x96 + 8 * (n))
  268. #define UB960_SR_CSI_LINE_ERR_COUNT_LO(n) (0x97 + 8 * (n))
  269. #define UB960_XR_REFCLK_FREQ 0xa5 /* UB960 */
  270. #define UB960_SR_IND_ACC_CTL 0xb0
  271. #define UB960_SR_IND_ACC_CTL_IA_AUTO_INC BIT(1)
  272. #define UB960_SR_IND_ACC_ADDR 0xb1
  273. #define UB960_SR_IND_ACC_DATA 0xb2
  274. #define UB960_SR_BIST_CONTROL 0xb3
  275. #define UB960_SR_MODE_IDX_STS 0xb8
  276. #define UB960_SR_LINK_ERROR_COUNT 0xb9
  277. #define UB960_SR_FPD3_ENC_CTL 0xba
  278. #define UB960_SR_FV_MIN_TIME 0xbc
  279. #define UB960_SR_GPIO_PD_CTL 0xbe
  280. #define UB960_RR_PORT_DEBUG 0xd0
  281. #define UB960_RR_AEQ_CTL2 0xd2
  282. #define UB960_RR_AEQ_CTL2_SET_AEQ_FLOOR BIT(2)
  283. #define UB960_RR_AEQ_STATUS 0xd3
  284. #define UB960_RR_AEQ_STATUS_STATUS_2 GENMASK(5, 3)
  285. #define UB960_RR_AEQ_STATUS_STATUS_1 GENMASK(2, 0)
  286. #define UB960_RR_AEQ_BYPASS 0xd4
  287. #define UB960_RR_AEQ_BYPASS_EQ_STAGE1_VALUE_SHIFT 5
  288. #define UB960_RR_AEQ_BYPASS_EQ_STAGE1_VALUE_MASK GENMASK(7, 5)
  289. #define UB960_RR_AEQ_BYPASS_EQ_STAGE2_VALUE_SHIFT 1
  290. #define UB960_RR_AEQ_BYPASS_EQ_STAGE2_VALUE_MASK GENMASK(3, 1)
  291. #define UB960_RR_AEQ_BYPASS_ENABLE BIT(0)
  292. #define UB960_RR_AEQ_MIN_MAX 0xd5
  293. #define UB960_RR_AEQ_MIN_MAX_AEQ_MAX_SHIFT 4
  294. #define UB960_RR_AEQ_MIN_MAX_AEQ_FLOOR_SHIFT 0
  295. #define UB960_RR_SFILTER_STS_0 0xd6
  296. #define UB960_RR_SFILTER_STS_1 0xd7
  297. #define UB960_RR_PORT_ICR_HI 0xd8
  298. #define UB960_RR_PORT_ICR_LO 0xd9
  299. #define UB960_RR_PORT_ISR_HI 0xda
  300. #define UB960_RR_PORT_ISR_LO 0xdb
  301. #define UB960_RR_FC_GPIO_STS 0xdc
  302. #define UB960_RR_FC_GPIO_ICR 0xdd
  303. #define UB960_RR_SEN_INT_RISE_STS 0xde
  304. #define UB960_RR_SEN_INT_FALL_STS 0xdf
  305. #define UB960_SR_FPD3_RX_ID(n) (0xf0 + (n))
  306. #define UB960_SR_FPD3_RX_ID_LEN 6
  307. #define UB960_SR_I2C_RX_ID(n) (0xf8 + (n))
  308. /* Indirect register blocks */
  309. #define UB960_IND_TARGET_PAT_GEN 0x00
  310. #define UB960_IND_TARGET_RX_ANA(n) (0x01 + (n))
  311. #define UB960_IND_TARGET_CSI_ANA 0x07
  312. /* UB960_IR_PGEN_*: Indirect Registers for Test Pattern Generator */
  313. #define UB960_IR_PGEN_CTL 0x01
  314. #define UB960_IR_PGEN_CTL_PGEN_ENABLE BIT(0)
  315. #define UB960_IR_PGEN_CFG 0x02
  316. #define UB960_IR_PGEN_CSI_DI 0x03
  317. #define UB960_IR_PGEN_LINE_SIZE1 0x04
  318. #define UB960_IR_PGEN_LINE_SIZE0 0x05
  319. #define UB960_IR_PGEN_BAR_SIZE1 0x06
  320. #define UB960_IR_PGEN_BAR_SIZE0 0x07
  321. #define UB960_IR_PGEN_ACT_LPF1 0x08
  322. #define UB960_IR_PGEN_ACT_LPF0 0x09
  323. #define UB960_IR_PGEN_TOT_LPF1 0x0a
  324. #define UB960_IR_PGEN_TOT_LPF0 0x0b
  325. #define UB960_IR_PGEN_LINE_PD1 0x0c
  326. #define UB960_IR_PGEN_LINE_PD0 0x0d
  327. #define UB960_IR_PGEN_VBP 0x0e
  328. #define UB960_IR_PGEN_VFP 0x0f
  329. #define UB960_IR_PGEN_COLOR(n) (0x10 + (n)) /* n < 15 */
  330. #define UB960_IR_RX_ANA_STROBE_SET_CLK 0x08
  331. #define UB960_IR_RX_ANA_STROBE_SET_CLK_NO_EXTRA_DELAY BIT(3)
  332. #define UB960_IR_RX_ANA_STROBE_SET_CLK_DELAY_MASK GENMASK(2, 0)
  333. #define UB960_IR_RX_ANA_STROBE_SET_DATA 0x09
  334. #define UB960_IR_RX_ANA_STROBE_SET_DATA_NO_EXTRA_DELAY BIT(3)
  335. #define UB960_IR_RX_ANA_STROBE_SET_DATA_DELAY_MASK GENMASK(2, 0)
  336. /* UB9702 Registers */
  337. #define UB9702_SR_CSI_EXCLUSIVE_FWD2 0x3c
  338. #define UB9702_SR_REFCLK_FREQ 0x3d
  339. #define UB9702_RR_RX_CTL_1 0x80
  340. #define UB9702_RR_RX_CTL_2 0x87
  341. #define UB9702_RR_VC_ID_MAP(x) (0xa0 + (x))
  342. #define UB9702_SR_FPD_RATE_CFG 0xc2
  343. #define UB9702_SR_CSI_PLL_DIV 0xc9
  344. #define UB9702_RR_RX_SM_SEL_2 0xd4
  345. #define UB9702_RR_CHANNEL_MODE 0xe4
  346. #define UB9702_IND_TARGET_SAR_ADC 0x0a
  347. #define UB9702_IR_RX_ANA_FPD_BC_CTL0 0x04
  348. #define UB9702_IR_RX_ANA_FPD_BC_CTL1 0x0d
  349. #define UB9702_IR_RX_ANA_FPD_BC_CTL2 0x1b
  350. #define UB9702_IR_RX_ANA_SYSTEM_INIT_REG0 0x21
  351. #define UB9702_IR_RX_ANA_AEQ_ALP_SEL6 0x27
  352. #define UB9702_IR_RX_ANA_AEQ_ALP_SEL7 0x28
  353. #define UB9702_IR_RX_ANA_AEQ_ALP_SEL10 0x2b
  354. #define UB9702_IR_RX_ANA_AEQ_ALP_SEL11 0x2c
  355. #define UB9702_IR_RX_ANA_EQ_ADAPT_CTRL 0x2e
  356. #define UB9702_IR_RX_ANA_AEQ_CFG_1 0x34
  357. #define UB9702_IR_RX_ANA_AEQ_CFG_2 0x4d
  358. #define UB9702_IR_RX_ANA_GAIN_CTRL_0 0x71
  359. #define UB9702_IR_RX_ANA_GAIN_CTRL_0 0x71
  360. #define UB9702_IR_RX_ANA_VGA_CTRL_SEL_1 0x72
  361. #define UB9702_IR_RX_ANA_VGA_CTRL_SEL_2 0x73
  362. #define UB9702_IR_RX_ANA_VGA_CTRL_SEL_3 0x74
  363. #define UB9702_IR_RX_ANA_VGA_CTRL_SEL_6 0x77
  364. #define UB9702_IR_RX_ANA_AEQ_CFG_3 0x79
  365. #define UB9702_IR_RX_ANA_AEQ_CFG_4 0x85
  366. #define UB9702_IR_RX_ANA_EQ_CTRL_SEL_15 0x87
  367. #define UB9702_IR_RX_ANA_EQ_CTRL_SEL_24 0x90
  368. #define UB9702_IR_RX_ANA_EQ_CTRL_SEL_38 0x9e
  369. #define UB9702_IR_RX_ANA_FPD3_CDR_CTRL_SEL_5 0xa5
  370. #define UB9702_IR_RX_ANA_FPD3_AEQ_CTRL_SEL_1 0xa8
  371. #define UB9702_IR_RX_ANA_EQ_OVERRIDE_CTRL 0xf0
  372. #define UB9702_IR_RX_ANA_VGA_CTRL_SEL_8 0xf1
  373. #define UB9702_IR_CSI_ANA_CSIPLL_REG_1 0x92
  374. /* EQ related */
  375. #define UB960_MIN_AEQ_STROBE_POS -7
  376. #define UB960_MAX_AEQ_STROBE_POS 7
  377. #define UB960_MANUAL_STROBE_EXTRA_DELAY 6
  378. #define UB960_MIN_MANUAL_STROBE_POS -(7 + UB960_MANUAL_STROBE_EXTRA_DELAY)
  379. #define UB960_MAX_MANUAL_STROBE_POS (7 + UB960_MANUAL_STROBE_EXTRA_DELAY)
  380. #define UB960_NUM_MANUAL_STROBE_POS (UB960_MAX_MANUAL_STROBE_POS - UB960_MIN_MANUAL_STROBE_POS + 1)
  381. #define UB960_MIN_EQ_LEVEL 0
  382. #define UB960_MAX_EQ_LEVEL 14
  383. #define UB960_NUM_EQ_LEVELS (UB960_MAX_EQ_LEVEL - UB960_MIN_EQ_LEVEL + 1)
  384. struct ub960_hw_data {
  385. const char *model;
  386. u8 num_rxports;
  387. u8 num_txports;
  388. bool is_ub9702;
  389. bool is_fpdlink4;
  390. };
  391. enum ub960_rxport_mode {
  392. RXPORT_MODE_RAW10 = 0,
  393. RXPORT_MODE_RAW12_HF = 1,
  394. RXPORT_MODE_RAW12_LF = 2,
  395. RXPORT_MODE_CSI2_SYNC = 3,
  396. RXPORT_MODE_CSI2_NONSYNC = 4,
  397. RXPORT_MODE_LAST = RXPORT_MODE_CSI2_NONSYNC,
  398. };
  399. enum ub960_rxport_cdr {
  400. RXPORT_CDR_FPD3 = 0,
  401. RXPORT_CDR_FPD4 = 1,
  402. RXPORT_CDR_LAST = RXPORT_CDR_FPD4,
  403. };
  404. struct ub960_rxport {
  405. struct ub960_data *priv;
  406. u8 nport; /* RX port number, and index in priv->rxport[] */
  407. struct {
  408. struct v4l2_subdev *sd;
  409. u16 pad;
  410. struct fwnode_handle *ep_fwnode;
  411. } source;
  412. /* Serializer */
  413. struct {
  414. struct fwnode_handle *fwnode;
  415. struct i2c_client *client;
  416. unsigned short alias; /* I2C alias (lower 7 bits) */
  417. short addr; /* Local I2C address (lower 7 bits) */
  418. struct ds90ub9xx_platform_data pdata;
  419. struct regmap *regmap;
  420. } ser;
  421. enum ub960_rxport_mode rx_mode;
  422. enum ub960_rxport_cdr cdr_mode;
  423. u8 lv_fv_pol; /* LV and FV polarities */
  424. struct regulator *vpoc;
  425. /* EQ settings */
  426. struct {
  427. bool manual_eq;
  428. s8 strobe_pos;
  429. union {
  430. struct {
  431. u8 eq_level_min;
  432. u8 eq_level_max;
  433. } aeq;
  434. struct {
  435. u8 eq_level;
  436. } manual;
  437. };
  438. } eq;
  439. /* lock for aliased_addrs and associated registers */
  440. struct mutex aliased_addrs_lock;
  441. u16 aliased_addrs[UB960_MAX_PORT_ALIASES];
  442. };
  443. struct ub960_asd {
  444. struct v4l2_async_connection base;
  445. struct ub960_rxport *rxport;
  446. };
  447. static inline struct ub960_asd *to_ub960_asd(struct v4l2_async_connection *asd)
  448. {
  449. return container_of(asd, struct ub960_asd, base);
  450. }
  451. struct ub960_txport {
  452. struct ub960_data *priv;
  453. u8 nport; /* TX port number, and index in priv->txport[] */
  454. u32 num_data_lanes;
  455. bool non_continous_clk;
  456. };
  457. struct ub960_data {
  458. const struct ub960_hw_data *hw_data;
  459. struct i2c_client *client; /* for shared local registers */
  460. struct regmap *regmap;
  461. /* lock for register access */
  462. struct mutex reg_lock;
  463. struct clk *refclk;
  464. struct regulator *vddio;
  465. struct gpio_desc *pd_gpio;
  466. struct delayed_work poll_work;
  467. struct ub960_rxport *rxports[UB960_MAX_RX_NPORTS];
  468. struct ub960_txport *txports[UB960_MAX_TX_NPORTS];
  469. struct v4l2_subdev sd;
  470. struct media_pad pads[UB960_MAX_NPORTS];
  471. struct v4l2_ctrl_handler ctrl_handler;
  472. struct v4l2_async_notifier notifier;
  473. u32 tx_data_rate; /* Nominal data rate (Gb/s) */
  474. s64 tx_link_freq[1];
  475. struct i2c_atr *atr;
  476. struct {
  477. u8 rxport;
  478. u8 txport;
  479. u8 indirect_target;
  480. } reg_current;
  481. bool streaming;
  482. u8 stored_fwd_ctl;
  483. u64 stream_enable_mask[UB960_MAX_NPORTS];
  484. /* These are common to all ports */
  485. struct {
  486. bool manual;
  487. s8 min;
  488. s8 max;
  489. } strobe;
  490. };
  491. static inline struct ub960_data *sd_to_ub960(struct v4l2_subdev *sd)
  492. {
  493. return container_of(sd, struct ub960_data, sd);
  494. }
  495. static inline bool ub960_pad_is_sink(struct ub960_data *priv, u32 pad)
  496. {
  497. return pad < priv->hw_data->num_rxports;
  498. }
  499. static inline bool ub960_pad_is_source(struct ub960_data *priv, u32 pad)
  500. {
  501. return pad >= priv->hw_data->num_rxports;
  502. }
  503. static inline unsigned int ub960_pad_to_port(struct ub960_data *priv, u32 pad)
  504. {
  505. if (ub960_pad_is_sink(priv, pad))
  506. return pad;
  507. else
  508. return pad - priv->hw_data->num_rxports;
  509. }
  510. struct ub960_format_info {
  511. u32 code;
  512. u32 bpp;
  513. u8 datatype;
  514. bool meta;
  515. };
  516. static const struct ub960_format_info ub960_formats[] = {
  517. { .code = MEDIA_BUS_FMT_RGB888_1X24, .bpp = 24, .datatype = MIPI_CSI2_DT_RGB888, },
  518. { .code = MEDIA_BUS_FMT_YUYV8_1X16, .bpp = 16, .datatype = MIPI_CSI2_DT_YUV422_8B, },
  519. { .code = MEDIA_BUS_FMT_UYVY8_1X16, .bpp = 16, .datatype = MIPI_CSI2_DT_YUV422_8B, },
  520. { .code = MEDIA_BUS_FMT_VYUY8_1X16, .bpp = 16, .datatype = MIPI_CSI2_DT_YUV422_8B, },
  521. { .code = MEDIA_BUS_FMT_YVYU8_1X16, .bpp = 16, .datatype = MIPI_CSI2_DT_YUV422_8B, },
  522. { .code = MEDIA_BUS_FMT_SBGGR8_1X8, .bpp = 8, .datatype = MIPI_CSI2_DT_RAW8, },
  523. { .code = MEDIA_BUS_FMT_SGBRG8_1X8, .bpp = 8, .datatype = MIPI_CSI2_DT_RAW8, },
  524. { .code = MEDIA_BUS_FMT_SGRBG8_1X8, .bpp = 8, .datatype = MIPI_CSI2_DT_RAW8, },
  525. { .code = MEDIA_BUS_FMT_SRGGB8_1X8, .bpp = 8, .datatype = MIPI_CSI2_DT_RAW8, },
  526. { .code = MEDIA_BUS_FMT_SBGGR10_1X10, .bpp = 10, .datatype = MIPI_CSI2_DT_RAW10, },
  527. { .code = MEDIA_BUS_FMT_SGBRG10_1X10, .bpp = 10, .datatype = MIPI_CSI2_DT_RAW10, },
  528. { .code = MEDIA_BUS_FMT_SGRBG10_1X10, .bpp = 10, .datatype = MIPI_CSI2_DT_RAW10, },
  529. { .code = MEDIA_BUS_FMT_SRGGB10_1X10, .bpp = 10, .datatype = MIPI_CSI2_DT_RAW10, },
  530. { .code = MEDIA_BUS_FMT_SBGGR12_1X12, .bpp = 12, .datatype = MIPI_CSI2_DT_RAW12, },
  531. { .code = MEDIA_BUS_FMT_SGBRG12_1X12, .bpp = 12, .datatype = MIPI_CSI2_DT_RAW12, },
  532. { .code = MEDIA_BUS_FMT_SGRBG12_1X12, .bpp = 12, .datatype = MIPI_CSI2_DT_RAW12, },
  533. { .code = MEDIA_BUS_FMT_SRGGB12_1X12, .bpp = 12, .datatype = MIPI_CSI2_DT_RAW12, },
  534. };
  535. static const struct ub960_format_info *ub960_find_format(u32 code)
  536. {
  537. unsigned int i;
  538. for (i = 0; i < ARRAY_SIZE(ub960_formats); i++) {
  539. if (ub960_formats[i].code == code)
  540. return &ub960_formats[i];
  541. }
  542. return NULL;
  543. }
  544. struct ub960_rxport_iter {
  545. unsigned int nport;
  546. struct ub960_rxport *rxport;
  547. };
  548. enum ub960_iter_flags {
  549. UB960_ITER_ACTIVE_ONLY = BIT(0),
  550. UB960_ITER_FPD4_ONLY = BIT(1),
  551. };
  552. static struct ub960_rxport_iter ub960_iter_rxport(struct ub960_data *priv,
  553. struct ub960_rxport_iter it,
  554. enum ub960_iter_flags flags)
  555. {
  556. for (; it.nport < priv->hw_data->num_rxports; it.nport++) {
  557. it.rxport = priv->rxports[it.nport];
  558. if ((flags & UB960_ITER_ACTIVE_ONLY) && !it.rxport)
  559. continue;
  560. if ((flags & UB960_ITER_FPD4_ONLY) &&
  561. it.rxport->cdr_mode != RXPORT_CDR_FPD4)
  562. continue;
  563. return it;
  564. }
  565. it.rxport = NULL;
  566. return it;
  567. }
  568. #define for_each_rxport(priv, it) \
  569. for (struct ub960_rxport_iter it = \
  570. ub960_iter_rxport(priv, (struct ub960_rxport_iter){ 0 }, \
  571. 0); \
  572. it.nport < (priv)->hw_data->num_rxports; \
  573. it.nport++, it = ub960_iter_rxport(priv, it, 0))
  574. #define for_each_active_rxport(priv, it) \
  575. for (struct ub960_rxport_iter it = \
  576. ub960_iter_rxport(priv, (struct ub960_rxport_iter){ 0 }, \
  577. UB960_ITER_ACTIVE_ONLY); \
  578. it.nport < (priv)->hw_data->num_rxports; \
  579. it.nport++, it = ub960_iter_rxport(priv, it, \
  580. UB960_ITER_ACTIVE_ONLY))
  581. #define for_each_active_rxport_fpd4(priv, it) \
  582. for (struct ub960_rxport_iter it = \
  583. ub960_iter_rxport(priv, (struct ub960_rxport_iter){ 0 }, \
  584. UB960_ITER_ACTIVE_ONLY | \
  585. UB960_ITER_FPD4_ONLY); \
  586. it.nport < (priv)->hw_data->num_rxports; \
  587. it.nport++, it = ub960_iter_rxport(priv, it, \
  588. UB960_ITER_ACTIVE_ONLY | \
  589. UB960_ITER_FPD4_ONLY))
  590. /* -----------------------------------------------------------------------------
  591. * Basic device access
  592. */
  593. static int ub960_read(struct ub960_data *priv, u8 reg, u8 *val, int *err)
  594. {
  595. struct device *dev = &priv->client->dev;
  596. unsigned int v;
  597. int ret;
  598. if (err && *err)
  599. return *err;
  600. mutex_lock(&priv->reg_lock);
  601. ret = regmap_read(priv->regmap, reg, &v);
  602. if (ret) {
  603. dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
  604. __func__, reg, ret);
  605. goto out_unlock;
  606. }
  607. *val = v;
  608. out_unlock:
  609. mutex_unlock(&priv->reg_lock);
  610. if (ret && err)
  611. *err = ret;
  612. return ret;
  613. }
  614. static int ub960_write(struct ub960_data *priv, u8 reg, u8 val, int *err)
  615. {
  616. struct device *dev = &priv->client->dev;
  617. int ret;
  618. if (err && *err)
  619. return *err;
  620. mutex_lock(&priv->reg_lock);
  621. ret = regmap_write(priv->regmap, reg, val);
  622. if (ret)
  623. dev_err(dev, "%s: cannot write register 0x%02x (%d)!\n",
  624. __func__, reg, ret);
  625. mutex_unlock(&priv->reg_lock);
  626. if (ret && err)
  627. *err = ret;
  628. return ret;
  629. }
  630. static int ub960_update_bits(struct ub960_data *priv, u8 reg, u8 mask, u8 val,
  631. int *err)
  632. {
  633. struct device *dev = &priv->client->dev;
  634. int ret;
  635. if (err && *err)
  636. return *err;
  637. mutex_lock(&priv->reg_lock);
  638. ret = regmap_update_bits(priv->regmap, reg, mask, val);
  639. if (ret)
  640. dev_err(dev, "%s: cannot update register 0x%02x (%d)!\n",
  641. __func__, reg, ret);
  642. mutex_unlock(&priv->reg_lock);
  643. if (ret && err)
  644. *err = ret;
  645. return ret;
  646. }
  647. static int ub960_read16(struct ub960_data *priv, u8 reg, u16 *val, int *err)
  648. {
  649. struct device *dev = &priv->client->dev;
  650. __be16 __v;
  651. int ret;
  652. if (err && *err)
  653. return *err;
  654. mutex_lock(&priv->reg_lock);
  655. ret = regmap_bulk_read(priv->regmap, reg, &__v, sizeof(__v));
  656. if (ret) {
  657. dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
  658. __func__, reg, ret);
  659. goto out_unlock;
  660. }
  661. *val = be16_to_cpu(__v);
  662. out_unlock:
  663. mutex_unlock(&priv->reg_lock);
  664. if (ret && err)
  665. *err = ret;
  666. return ret;
  667. }
  668. static int ub960_rxport_select(struct ub960_data *priv, u8 nport)
  669. {
  670. struct device *dev = &priv->client->dev;
  671. int ret;
  672. lockdep_assert_held(&priv->reg_lock);
  673. if (priv->reg_current.rxport == nport)
  674. return 0;
  675. ret = regmap_write(priv->regmap, UB960_SR_FPD3_PORT_SEL,
  676. (nport << 4) | BIT(nport));
  677. if (ret) {
  678. dev_err(dev, "%s: cannot select rxport %d (%d)!\n", __func__,
  679. nport, ret);
  680. return ret;
  681. }
  682. priv->reg_current.rxport = nport;
  683. return 0;
  684. }
  685. static int ub960_rxport_read(struct ub960_data *priv, u8 nport, u8 reg,
  686. u8 *val, int *err)
  687. {
  688. struct device *dev = &priv->client->dev;
  689. unsigned int v;
  690. int ret;
  691. if (err && *err)
  692. return *err;
  693. mutex_lock(&priv->reg_lock);
  694. ret = ub960_rxport_select(priv, nport);
  695. if (ret)
  696. goto out_unlock;
  697. ret = regmap_read(priv->regmap, reg, &v);
  698. if (ret) {
  699. dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
  700. __func__, reg, ret);
  701. goto out_unlock;
  702. }
  703. *val = v;
  704. out_unlock:
  705. mutex_unlock(&priv->reg_lock);
  706. if (ret && err)
  707. *err = ret;
  708. return ret;
  709. }
  710. static int ub960_rxport_write(struct ub960_data *priv, u8 nport, u8 reg,
  711. u8 val, int *err)
  712. {
  713. struct device *dev = &priv->client->dev;
  714. int ret;
  715. if (err && *err)
  716. return *err;
  717. mutex_lock(&priv->reg_lock);
  718. ret = ub960_rxport_select(priv, nport);
  719. if (ret)
  720. goto out_unlock;
  721. ret = regmap_write(priv->regmap, reg, val);
  722. if (ret)
  723. dev_err(dev, "%s: cannot write register 0x%02x (%d)!\n",
  724. __func__, reg, ret);
  725. out_unlock:
  726. mutex_unlock(&priv->reg_lock);
  727. if (ret && err)
  728. *err = ret;
  729. return ret;
  730. }
  731. static int ub960_rxport_update_bits(struct ub960_data *priv, u8 nport, u8 reg,
  732. u8 mask, u8 val, int *err)
  733. {
  734. struct device *dev = &priv->client->dev;
  735. int ret;
  736. if (err && *err)
  737. return *err;
  738. mutex_lock(&priv->reg_lock);
  739. ret = ub960_rxport_select(priv, nport);
  740. if (ret)
  741. goto out_unlock;
  742. ret = regmap_update_bits(priv->regmap, reg, mask, val);
  743. if (ret)
  744. dev_err(dev, "%s: cannot update register 0x%02x (%d)!\n",
  745. __func__, reg, ret);
  746. out_unlock:
  747. mutex_unlock(&priv->reg_lock);
  748. if (ret && err)
  749. *err = ret;
  750. return ret;
  751. }
  752. static int ub960_rxport_read16(struct ub960_data *priv, u8 nport, u8 reg,
  753. u16 *val, int *err)
  754. {
  755. struct device *dev = &priv->client->dev;
  756. __be16 __v;
  757. int ret;
  758. if (err && *err)
  759. return *err;
  760. mutex_lock(&priv->reg_lock);
  761. ret = ub960_rxport_select(priv, nport);
  762. if (ret)
  763. goto out_unlock;
  764. ret = regmap_bulk_read(priv->regmap, reg, &__v, sizeof(__v));
  765. if (ret) {
  766. dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
  767. __func__, reg, ret);
  768. goto out_unlock;
  769. }
  770. *val = be16_to_cpu(__v);
  771. out_unlock:
  772. mutex_unlock(&priv->reg_lock);
  773. if (ret && err)
  774. *err = ret;
  775. return ret;
  776. }
  777. static int ub960_txport_select(struct ub960_data *priv, u8 nport)
  778. {
  779. struct device *dev = &priv->client->dev;
  780. int ret;
  781. lockdep_assert_held(&priv->reg_lock);
  782. if (priv->reg_current.txport == nport)
  783. return 0;
  784. ret = regmap_write(priv->regmap, UB960_SR_CSI_PORT_SEL,
  785. (nport << 4) | BIT(nport));
  786. if (ret) {
  787. dev_err(dev, "%s: cannot select tx port %d (%d)!\n", __func__,
  788. nport, ret);
  789. return ret;
  790. }
  791. priv->reg_current.txport = nport;
  792. return 0;
  793. }
  794. static int ub960_txport_read(struct ub960_data *priv, u8 nport, u8 reg,
  795. u8 *val, int *err)
  796. {
  797. struct device *dev = &priv->client->dev;
  798. unsigned int v;
  799. int ret;
  800. if (err && *err)
  801. return *err;
  802. mutex_lock(&priv->reg_lock);
  803. ret = ub960_txport_select(priv, nport);
  804. if (ret)
  805. goto out_unlock;
  806. ret = regmap_read(priv->regmap, reg, &v);
  807. if (ret) {
  808. dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
  809. __func__, reg, ret);
  810. goto out_unlock;
  811. }
  812. *val = v;
  813. out_unlock:
  814. mutex_unlock(&priv->reg_lock);
  815. if (ret && err)
  816. *err = ret;
  817. return ret;
  818. }
  819. static int ub960_txport_write(struct ub960_data *priv, u8 nport, u8 reg,
  820. u8 val, int *err)
  821. {
  822. struct device *dev = &priv->client->dev;
  823. int ret;
  824. if (err && *err)
  825. return *err;
  826. mutex_lock(&priv->reg_lock);
  827. ret = ub960_txport_select(priv, nport);
  828. if (ret)
  829. goto out_unlock;
  830. ret = regmap_write(priv->regmap, reg, val);
  831. if (ret)
  832. dev_err(dev, "%s: cannot write register 0x%02x (%d)!\n",
  833. __func__, reg, ret);
  834. out_unlock:
  835. mutex_unlock(&priv->reg_lock);
  836. if (ret && err)
  837. *err = ret;
  838. return ret;
  839. }
  840. static int ub960_txport_update_bits(struct ub960_data *priv, u8 nport, u8 reg,
  841. u8 mask, u8 val, int *err)
  842. {
  843. struct device *dev = &priv->client->dev;
  844. int ret;
  845. if (err && *err)
  846. return *err;
  847. mutex_lock(&priv->reg_lock);
  848. ret = ub960_txport_select(priv, nport);
  849. if (ret)
  850. goto out_unlock;
  851. ret = regmap_update_bits(priv->regmap, reg, mask, val);
  852. if (ret)
  853. dev_err(dev, "%s: cannot update register 0x%02x (%d)!\n",
  854. __func__, reg, ret);
  855. out_unlock:
  856. mutex_unlock(&priv->reg_lock);
  857. if (ret && err)
  858. *err = ret;
  859. return ret;
  860. }
  861. static int ub960_select_ind_reg_block(struct ub960_data *priv, u8 block)
  862. {
  863. struct device *dev = &priv->client->dev;
  864. int ret;
  865. lockdep_assert_held(&priv->reg_lock);
  866. if (priv->reg_current.indirect_target == block)
  867. return 0;
  868. ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_CTL, block << 2);
  869. if (ret) {
  870. dev_err(dev, "%s: cannot select indirect target %u (%d)!\n",
  871. __func__, block, ret);
  872. return ret;
  873. }
  874. priv->reg_current.indirect_target = block;
  875. return 0;
  876. }
  877. static int ub960_read_ind(struct ub960_data *priv, u8 block, u8 reg, u8 *val,
  878. int *err)
  879. {
  880. struct device *dev = &priv->client->dev;
  881. unsigned int v;
  882. int ret;
  883. if (err && *err)
  884. return *err;
  885. mutex_lock(&priv->reg_lock);
  886. ret = ub960_select_ind_reg_block(priv, block);
  887. if (ret)
  888. goto out_unlock;
  889. ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_ADDR, reg);
  890. if (ret) {
  891. dev_err(dev,
  892. "Write to IND_ACC_ADDR failed when reading %u:%x02x: %d\n",
  893. block, reg, ret);
  894. goto out_unlock;
  895. }
  896. ret = regmap_read(priv->regmap, UB960_SR_IND_ACC_DATA, &v);
  897. if (ret) {
  898. dev_err(dev,
  899. "Write to IND_ACC_DATA failed when reading %u:%x02x: %d\n",
  900. block, reg, ret);
  901. goto out_unlock;
  902. }
  903. *val = v;
  904. out_unlock:
  905. mutex_unlock(&priv->reg_lock);
  906. if (ret && err)
  907. *err = ret;
  908. return ret;
  909. }
  910. static int ub960_write_ind(struct ub960_data *priv, u8 block, u8 reg, u8 val,
  911. int *err)
  912. {
  913. struct device *dev = &priv->client->dev;
  914. int ret;
  915. if (err && *err)
  916. return *err;
  917. mutex_lock(&priv->reg_lock);
  918. ret = ub960_select_ind_reg_block(priv, block);
  919. if (ret)
  920. goto out_unlock;
  921. ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_ADDR, reg);
  922. if (ret) {
  923. dev_err(dev,
  924. "Write to IND_ACC_ADDR failed when writing %u:%x02x: %d\n",
  925. block, reg, ret);
  926. goto out_unlock;
  927. }
  928. ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_DATA, val);
  929. if (ret) {
  930. dev_err(dev,
  931. "Write to IND_ACC_DATA failed when writing %u:%x02x: %d\n",
  932. block, reg, ret);
  933. goto out_unlock;
  934. }
  935. out_unlock:
  936. mutex_unlock(&priv->reg_lock);
  937. if (ret && err)
  938. *err = ret;
  939. return ret;
  940. }
  941. static int ub960_ind_update_bits(struct ub960_data *priv, u8 block, u8 reg,
  942. u8 mask, u8 val, int *err)
  943. {
  944. struct device *dev = &priv->client->dev;
  945. int ret;
  946. if (err && *err)
  947. return *err;
  948. mutex_lock(&priv->reg_lock);
  949. ret = ub960_select_ind_reg_block(priv, block);
  950. if (ret)
  951. goto out_unlock;
  952. ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_ADDR, reg);
  953. if (ret) {
  954. dev_err(dev,
  955. "Write to IND_ACC_ADDR failed when updating %u:%x02x: %d\n",
  956. block, reg, ret);
  957. goto out_unlock;
  958. }
  959. ret = regmap_update_bits(priv->regmap, UB960_SR_IND_ACC_DATA, mask,
  960. val);
  961. if (ret) {
  962. dev_err(dev,
  963. "Write to IND_ACC_DATA failed when updating %u:%x02x: %d\n",
  964. block, reg, ret);
  965. goto out_unlock;
  966. }
  967. out_unlock:
  968. mutex_unlock(&priv->reg_lock);
  969. if (ret && err)
  970. *err = ret;
  971. return ret;
  972. }
  973. static int ub960_reset(struct ub960_data *priv, bool reset_regs)
  974. {
  975. struct device *dev = &priv->client->dev;
  976. unsigned int v;
  977. int ret;
  978. u8 bit;
  979. bit = reset_regs ? UB960_SR_RESET_DIGITAL_RESET1 :
  980. UB960_SR_RESET_DIGITAL_RESET0;
  981. ret = ub960_write(priv, UB960_SR_RESET, bit, NULL);
  982. if (ret)
  983. return ret;
  984. mutex_lock(&priv->reg_lock);
  985. ret = regmap_read_poll_timeout(priv->regmap, UB960_SR_RESET, v,
  986. (v & bit) == 0, 2000, 100000);
  987. mutex_unlock(&priv->reg_lock);
  988. if (ret)
  989. dev_err(dev, "reset failed: %d\n", ret);
  990. return ret;
  991. }
  992. /* -----------------------------------------------------------------------------
  993. * I2C-ATR (address translator)
  994. */
  995. static int ub960_atr_attach_addr(struct i2c_atr *atr, u32 chan_id,
  996. u16 addr, u16 alias)
  997. {
  998. struct ub960_data *priv = i2c_atr_get_driver_data(atr);
  999. struct ub960_rxport *rxport = priv->rxports[chan_id];
  1000. struct device *dev = &priv->client->dev;
  1001. unsigned int reg_idx;
  1002. int ret = 0;
  1003. guard(mutex)(&rxport->aliased_addrs_lock);
  1004. for (reg_idx = 0; reg_idx < ARRAY_SIZE(rxport->aliased_addrs); reg_idx++) {
  1005. if (!rxport->aliased_addrs[reg_idx])
  1006. break;
  1007. }
  1008. if (reg_idx == ARRAY_SIZE(rxport->aliased_addrs)) {
  1009. dev_err(dev, "rx%u: alias pool exhausted\n", rxport->nport);
  1010. return -EADDRNOTAVAIL;
  1011. }
  1012. rxport->aliased_addrs[reg_idx] = addr;
  1013. ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ID(reg_idx),
  1014. addr << 1, &ret);
  1015. ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ALIAS(reg_idx),
  1016. alias << 1, &ret);
  1017. if (ret)
  1018. return ret;
  1019. dev_dbg(dev, "rx%u: client 0x%02x assigned alias 0x%02x at slot %u\n",
  1020. rxport->nport, addr, alias, reg_idx);
  1021. return 0;
  1022. }
  1023. static void ub960_atr_detach_addr(struct i2c_atr *atr, u32 chan_id,
  1024. u16 addr)
  1025. {
  1026. struct ub960_data *priv = i2c_atr_get_driver_data(atr);
  1027. struct ub960_rxport *rxport = priv->rxports[chan_id];
  1028. struct device *dev = &priv->client->dev;
  1029. unsigned int reg_idx;
  1030. int ret;
  1031. guard(mutex)(&rxport->aliased_addrs_lock);
  1032. for (reg_idx = 0; reg_idx < ARRAY_SIZE(rxport->aliased_addrs); reg_idx++) {
  1033. if (rxport->aliased_addrs[reg_idx] == addr)
  1034. break;
  1035. }
  1036. if (reg_idx == ARRAY_SIZE(rxport->aliased_addrs)) {
  1037. dev_err(dev, "rx%u: client 0x%02x is not mapped!\n",
  1038. rxport->nport, addr);
  1039. return;
  1040. }
  1041. rxport->aliased_addrs[reg_idx] = 0;
  1042. ret = ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ALIAS(reg_idx),
  1043. 0, NULL);
  1044. if (ret) {
  1045. dev_err(dev, "rx%u: unable to fully unmap client 0x%02x: %d\n",
  1046. rxport->nport, addr, ret);
  1047. return;
  1048. }
  1049. dev_dbg(dev, "rx%u: client 0x%02x released at slot %u\n", rxport->nport,
  1050. addr, reg_idx);
  1051. }
  1052. static const struct i2c_atr_ops ub960_atr_ops = {
  1053. .attach_addr = ub960_atr_attach_addr,
  1054. .detach_addr = ub960_atr_detach_addr,
  1055. };
  1056. static int ub960_init_atr(struct ub960_data *priv)
  1057. {
  1058. struct device *dev = &priv->client->dev;
  1059. struct i2c_adapter *parent_adap = priv->client->adapter;
  1060. priv->atr = i2c_atr_new(parent_adap, dev, &ub960_atr_ops,
  1061. priv->hw_data->num_rxports, 0);
  1062. if (IS_ERR(priv->atr))
  1063. return PTR_ERR(priv->atr);
  1064. i2c_atr_set_driver_data(priv->atr, priv);
  1065. return 0;
  1066. }
  1067. static void ub960_uninit_atr(struct ub960_data *priv)
  1068. {
  1069. i2c_atr_delete(priv->atr);
  1070. priv->atr = NULL;
  1071. }
  1072. /* -----------------------------------------------------------------------------
  1073. * TX ports
  1074. */
  1075. static int ub960_parse_dt_txport(struct ub960_data *priv,
  1076. struct fwnode_handle *ep_fwnode,
  1077. u8 nport)
  1078. {
  1079. struct device *dev = &priv->client->dev;
  1080. struct v4l2_fwnode_endpoint vep = {};
  1081. struct ub960_txport *txport;
  1082. int ret;
  1083. txport = kzalloc_obj(*txport);
  1084. if (!txport)
  1085. return -ENOMEM;
  1086. txport->priv = priv;
  1087. txport->nport = nport;
  1088. vep.bus_type = V4L2_MBUS_CSI2_DPHY;
  1089. ret = v4l2_fwnode_endpoint_alloc_parse(ep_fwnode, &vep);
  1090. if (ret) {
  1091. dev_err(dev, "tx%u: failed to parse endpoint data\n", nport);
  1092. goto err_free_txport;
  1093. }
  1094. txport->non_continous_clk = vep.bus.mipi_csi2.flags &
  1095. V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
  1096. txport->num_data_lanes = vep.bus.mipi_csi2.num_data_lanes;
  1097. if (vep.nr_of_link_frequencies != 1) {
  1098. ret = -EINVAL;
  1099. goto err_free_vep;
  1100. }
  1101. priv->tx_link_freq[0] = vep.link_frequencies[0];
  1102. priv->tx_data_rate = priv->tx_link_freq[0] * 2;
  1103. if (priv->tx_data_rate != MHZ(1600) &&
  1104. priv->tx_data_rate != MHZ(1200) &&
  1105. priv->tx_data_rate != MHZ(800) &&
  1106. priv->tx_data_rate != MHZ(400)) {
  1107. dev_err(dev, "tx%u: invalid 'link-frequencies' value\n", nport);
  1108. ret = -EINVAL;
  1109. goto err_free_vep;
  1110. }
  1111. v4l2_fwnode_endpoint_free(&vep);
  1112. priv->txports[nport] = txport;
  1113. return 0;
  1114. err_free_vep:
  1115. v4l2_fwnode_endpoint_free(&vep);
  1116. err_free_txport:
  1117. kfree(txport);
  1118. return ret;
  1119. }
  1120. static int ub960_csi_handle_events(struct ub960_data *priv, u8 nport)
  1121. {
  1122. struct device *dev = &priv->client->dev;
  1123. u8 csi_tx_isr;
  1124. int ret;
  1125. ret = ub960_txport_read(priv, nport, UB960_TR_CSI_TX_ISR, &csi_tx_isr,
  1126. NULL);
  1127. if (ret)
  1128. return ret;
  1129. if (csi_tx_isr & UB960_TR_CSI_TX_ISR_IS_CSI_SYNC_ERROR)
  1130. dev_warn(dev, "TX%u: CSI_SYNC_ERROR\n", nport);
  1131. if (csi_tx_isr & UB960_TR_CSI_TX_ISR_IS_CSI_PASS_ERROR)
  1132. dev_warn(dev, "TX%u: CSI_PASS_ERROR\n", nport);
  1133. return 0;
  1134. }
  1135. /* -----------------------------------------------------------------------------
  1136. * RX ports
  1137. */
  1138. static int ub960_rxport_enable_vpocs(struct ub960_data *priv)
  1139. {
  1140. unsigned int failed_nport;
  1141. int ret;
  1142. for_each_active_rxport(priv, it) {
  1143. if (!it.rxport->vpoc)
  1144. continue;
  1145. ret = regulator_enable(it.rxport->vpoc);
  1146. if (ret) {
  1147. failed_nport = it.nport;
  1148. goto err_disable_vpocs;
  1149. }
  1150. }
  1151. return 0;
  1152. err_disable_vpocs:
  1153. while (failed_nport--) {
  1154. struct ub960_rxport *rxport = priv->rxports[failed_nport];
  1155. if (!rxport || !rxport->vpoc)
  1156. continue;
  1157. regulator_disable(rxport->vpoc);
  1158. }
  1159. return ret;
  1160. }
  1161. static void ub960_rxport_disable_vpocs(struct ub960_data *priv)
  1162. {
  1163. for_each_active_rxport(priv, it) {
  1164. if (!it.rxport->vpoc)
  1165. continue;
  1166. regulator_disable(it.rxport->vpoc);
  1167. }
  1168. }
  1169. static int ub960_rxport_clear_errors(struct ub960_data *priv,
  1170. unsigned int nport)
  1171. {
  1172. int ret = 0;
  1173. u8 v;
  1174. ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1, &v, &ret);
  1175. ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS2, &v, &ret);
  1176. ub960_rxport_read(priv, nport, UB960_RR_CSI_RX_STS, &v, &ret);
  1177. ub960_rxport_read(priv, nport, UB960_RR_BCC_STATUS, &v, &ret);
  1178. ub960_rxport_read(priv, nport, UB960_RR_RX_PAR_ERR_HI, &v, &ret);
  1179. ub960_rxport_read(priv, nport, UB960_RR_RX_PAR_ERR_LO, &v, &ret);
  1180. ub960_rxport_read(priv, nport, UB960_RR_CSI_ERR_COUNTER, &v, &ret);
  1181. return ret;
  1182. }
  1183. static int ub960_clear_rx_errors(struct ub960_data *priv)
  1184. {
  1185. int ret;
  1186. for_each_rxport(priv, it) {
  1187. ret = ub960_rxport_clear_errors(priv, it.nport);
  1188. if (ret)
  1189. return ret;
  1190. }
  1191. return 0;
  1192. }
  1193. static int ub960_rxport_get_strobe_pos(struct ub960_data *priv,
  1194. unsigned int nport, s8 *strobe_pos)
  1195. {
  1196. u8 v;
  1197. u8 clk_delay, data_delay;
  1198. int ret;
  1199. ret = ub960_read_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  1200. UB960_IR_RX_ANA_STROBE_SET_CLK, &v, NULL);
  1201. if (ret)
  1202. return ret;
  1203. clk_delay = (v & UB960_IR_RX_ANA_STROBE_SET_CLK_NO_EXTRA_DELAY) ?
  1204. 0 : UB960_MANUAL_STROBE_EXTRA_DELAY;
  1205. ret = ub960_read_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  1206. UB960_IR_RX_ANA_STROBE_SET_DATA, &v, NULL);
  1207. if (ret)
  1208. return ret;
  1209. data_delay = (v & UB960_IR_RX_ANA_STROBE_SET_DATA_NO_EXTRA_DELAY) ?
  1210. 0 : UB960_MANUAL_STROBE_EXTRA_DELAY;
  1211. ret = ub960_rxport_read(priv, nport, UB960_RR_SFILTER_STS_0, &v, NULL);
  1212. if (ret)
  1213. return ret;
  1214. clk_delay += v & UB960_IR_RX_ANA_STROBE_SET_CLK_DELAY_MASK;
  1215. ret = ub960_rxport_read(priv, nport, UB960_RR_SFILTER_STS_1, &v, NULL);
  1216. if (ret)
  1217. return ret;
  1218. data_delay += v & UB960_IR_RX_ANA_STROBE_SET_DATA_DELAY_MASK;
  1219. *strobe_pos = data_delay - clk_delay;
  1220. return 0;
  1221. }
  1222. static int ub960_rxport_set_strobe_pos(struct ub960_data *priv,
  1223. unsigned int nport, s8 strobe_pos)
  1224. {
  1225. u8 clk_delay, data_delay;
  1226. int ret = 0;
  1227. clk_delay = UB960_IR_RX_ANA_STROBE_SET_CLK_NO_EXTRA_DELAY;
  1228. data_delay = UB960_IR_RX_ANA_STROBE_SET_DATA_NO_EXTRA_DELAY;
  1229. if (strobe_pos < UB960_MIN_AEQ_STROBE_POS)
  1230. clk_delay = abs(strobe_pos) - UB960_MANUAL_STROBE_EXTRA_DELAY;
  1231. else if (strobe_pos > UB960_MAX_AEQ_STROBE_POS)
  1232. data_delay = strobe_pos - UB960_MANUAL_STROBE_EXTRA_DELAY;
  1233. else if (strobe_pos < 0)
  1234. clk_delay = abs(strobe_pos) | UB960_IR_RX_ANA_STROBE_SET_CLK_NO_EXTRA_DELAY;
  1235. else if (strobe_pos > 0)
  1236. data_delay = strobe_pos | UB960_IR_RX_ANA_STROBE_SET_DATA_NO_EXTRA_DELAY;
  1237. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  1238. UB960_IR_RX_ANA_STROBE_SET_CLK, clk_delay, &ret);
  1239. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  1240. UB960_IR_RX_ANA_STROBE_SET_DATA, data_delay, &ret);
  1241. return ret;
  1242. }
  1243. static int ub960_rxport_set_strobe_range(struct ub960_data *priv, s8 strobe_min,
  1244. s8 strobe_max)
  1245. {
  1246. /* Convert the signed strobe pos to positive zero based value */
  1247. strobe_min -= UB960_MIN_AEQ_STROBE_POS;
  1248. strobe_max -= UB960_MIN_AEQ_STROBE_POS;
  1249. return ub960_write(priv, UB960_XR_SFILTER_CFG,
  1250. ((u8)strobe_min << UB960_XR_SFILTER_CFG_SFILTER_MIN_SHIFT) |
  1251. ((u8)strobe_max << UB960_XR_SFILTER_CFG_SFILTER_MAX_SHIFT),
  1252. NULL);
  1253. }
  1254. static int ub960_rxport_get_eq_level(struct ub960_data *priv,
  1255. unsigned int nport, u8 *eq_level)
  1256. {
  1257. int ret;
  1258. u8 v;
  1259. ret = ub960_rxport_read(priv, nport, UB960_RR_AEQ_STATUS, &v, NULL);
  1260. if (ret)
  1261. return ret;
  1262. *eq_level = (v & UB960_RR_AEQ_STATUS_STATUS_1) +
  1263. (v & UB960_RR_AEQ_STATUS_STATUS_2);
  1264. return 0;
  1265. }
  1266. static int ub960_rxport_set_eq_level(struct ub960_data *priv,
  1267. unsigned int nport, u8 eq_level)
  1268. {
  1269. u8 eq_stage_1_select_value, eq_stage_2_select_value;
  1270. const unsigned int eq_stage_max = 7;
  1271. int ret;
  1272. u8 v;
  1273. if (eq_level <= eq_stage_max) {
  1274. eq_stage_1_select_value = eq_level;
  1275. eq_stage_2_select_value = 0;
  1276. } else {
  1277. eq_stage_1_select_value = eq_stage_max;
  1278. eq_stage_2_select_value = eq_level - eq_stage_max;
  1279. }
  1280. ret = ub960_rxport_read(priv, nport, UB960_RR_AEQ_BYPASS, &v, NULL);
  1281. if (ret)
  1282. return ret;
  1283. v &= ~(UB960_RR_AEQ_BYPASS_EQ_STAGE1_VALUE_MASK |
  1284. UB960_RR_AEQ_BYPASS_EQ_STAGE2_VALUE_MASK);
  1285. v |= eq_stage_1_select_value << UB960_RR_AEQ_BYPASS_EQ_STAGE1_VALUE_SHIFT;
  1286. v |= eq_stage_2_select_value << UB960_RR_AEQ_BYPASS_EQ_STAGE2_VALUE_SHIFT;
  1287. v |= UB960_RR_AEQ_BYPASS_ENABLE;
  1288. ret = ub960_rxport_write(priv, nport, UB960_RR_AEQ_BYPASS, v, NULL);
  1289. if (ret)
  1290. return ret;
  1291. return 0;
  1292. }
  1293. static int ub960_rxport_set_eq_range(struct ub960_data *priv,
  1294. unsigned int nport, u8 eq_min, u8 eq_max)
  1295. {
  1296. int ret = 0;
  1297. ub960_rxport_write(priv, nport, UB960_RR_AEQ_MIN_MAX,
  1298. (eq_min << UB960_RR_AEQ_MIN_MAX_AEQ_FLOOR_SHIFT) |
  1299. (eq_max << UB960_RR_AEQ_MIN_MAX_AEQ_MAX_SHIFT),
  1300. &ret);
  1301. /* Enable AEQ min setting */
  1302. ub960_rxport_update_bits(priv, nport, UB960_RR_AEQ_CTL2,
  1303. UB960_RR_AEQ_CTL2_SET_AEQ_FLOOR,
  1304. UB960_RR_AEQ_CTL2_SET_AEQ_FLOOR, &ret);
  1305. return ret;
  1306. }
  1307. static int ub960_rxport_config_eq(struct ub960_data *priv, unsigned int nport)
  1308. {
  1309. struct ub960_rxport *rxport = priv->rxports[nport];
  1310. int ret;
  1311. /* We also set common settings here. Should be moved elsewhere. */
  1312. if (priv->strobe.manual) {
  1313. /* Disable AEQ_SFILTER_EN */
  1314. ret = ub960_update_bits(priv, UB960_XR_AEQ_CTL1,
  1315. UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN, 0,
  1316. NULL);
  1317. if (ret)
  1318. return ret;
  1319. } else {
  1320. /* Enable SFILTER and error control */
  1321. ret = ub960_write(priv, UB960_XR_AEQ_CTL1,
  1322. UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_MASK |
  1323. UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN,
  1324. NULL);
  1325. if (ret)
  1326. return ret;
  1327. /* Set AEQ strobe range */
  1328. ret = ub960_rxport_set_strobe_range(priv, priv->strobe.min,
  1329. priv->strobe.max);
  1330. if (ret)
  1331. return ret;
  1332. }
  1333. /* The rest are port specific */
  1334. if (priv->strobe.manual)
  1335. ret = ub960_rxport_set_strobe_pos(priv, nport,
  1336. rxport->eq.strobe_pos);
  1337. else
  1338. ret = ub960_rxport_set_strobe_pos(priv, nport, 0);
  1339. if (ret)
  1340. return ret;
  1341. if (rxport->eq.manual_eq) {
  1342. ret = ub960_rxport_set_eq_level(priv, nport,
  1343. rxport->eq.manual.eq_level);
  1344. if (ret)
  1345. return ret;
  1346. /* Enable AEQ Bypass */
  1347. ret = ub960_rxport_update_bits(priv, nport, UB960_RR_AEQ_BYPASS,
  1348. UB960_RR_AEQ_BYPASS_ENABLE,
  1349. UB960_RR_AEQ_BYPASS_ENABLE,
  1350. NULL);
  1351. if (ret)
  1352. return ret;
  1353. } else {
  1354. ret = ub960_rxport_set_eq_range(priv, nport,
  1355. rxport->eq.aeq.eq_level_min,
  1356. rxport->eq.aeq.eq_level_max);
  1357. if (ret)
  1358. return ret;
  1359. /* Disable AEQ Bypass */
  1360. ret = ub960_rxport_update_bits(priv, nport, UB960_RR_AEQ_BYPASS,
  1361. UB960_RR_AEQ_BYPASS_ENABLE, 0,
  1362. NULL);
  1363. if (ret)
  1364. return ret;
  1365. }
  1366. return 0;
  1367. }
  1368. static int ub960_rxport_link_ok(struct ub960_data *priv, unsigned int nport,
  1369. bool *ok)
  1370. {
  1371. u8 rx_port_sts1, rx_port_sts2;
  1372. u16 parity_errors;
  1373. u8 csi_rx_sts;
  1374. u8 csi_err_cnt;
  1375. u8 bcc_sts;
  1376. int ret;
  1377. bool errors;
  1378. ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1,
  1379. &rx_port_sts1, NULL);
  1380. if (ret)
  1381. return ret;
  1382. if (!(rx_port_sts1 & UB960_RR_RX_PORT_STS1_LOCK_STS)) {
  1383. *ok = false;
  1384. return 0;
  1385. }
  1386. ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS2,
  1387. &rx_port_sts2, NULL);
  1388. if (ret)
  1389. return ret;
  1390. ret = ub960_rxport_read(priv, nport, UB960_RR_CSI_RX_STS, &csi_rx_sts,
  1391. NULL);
  1392. if (ret)
  1393. return ret;
  1394. ret = ub960_rxport_read(priv, nport, UB960_RR_CSI_ERR_COUNTER,
  1395. &csi_err_cnt, NULL);
  1396. if (ret)
  1397. return ret;
  1398. ret = ub960_rxport_read(priv, nport, UB960_RR_BCC_STATUS, &bcc_sts,
  1399. NULL);
  1400. if (ret)
  1401. return ret;
  1402. ret = ub960_rxport_read16(priv, nport, UB960_RR_RX_PAR_ERR_HI,
  1403. &parity_errors, NULL);
  1404. if (ret)
  1405. return ret;
  1406. errors = (rx_port_sts1 & UB960_RR_RX_PORT_STS1_ERROR_MASK) ||
  1407. (rx_port_sts2 & UB960_RR_RX_PORT_STS2_ERROR_MASK) ||
  1408. (bcc_sts & UB960_RR_BCC_STATUS_ERROR_MASK) ||
  1409. (csi_rx_sts & UB960_RR_CSI_RX_STS_ERROR_MASK) || csi_err_cnt ||
  1410. parity_errors;
  1411. *ok = !errors;
  1412. return 0;
  1413. }
  1414. static int ub960_rxport_lockup_wa_ub9702(struct ub960_data *priv)
  1415. {
  1416. int ret;
  1417. /* Toggle PI_MODE to avoid possible FPD RX lockup */
  1418. ret = ub960_update_bits(priv, UB9702_RR_CHANNEL_MODE, GENMASK(4, 3),
  1419. 2 << 3, NULL);
  1420. if (ret)
  1421. return ret;
  1422. usleep_range(1000, 5000);
  1423. return ub960_update_bits(priv, UB9702_RR_CHANNEL_MODE, GENMASK(4, 3),
  1424. 0, NULL);
  1425. }
  1426. /*
  1427. * Wait for the RX ports to lock, have no errors and have stable strobe position
  1428. * and EQ level.
  1429. */
  1430. static int ub960_rxport_wait_locks(struct ub960_data *priv,
  1431. unsigned long port_mask,
  1432. unsigned int *lock_mask)
  1433. {
  1434. struct device *dev = &priv->client->dev;
  1435. unsigned long timeout;
  1436. unsigned int link_ok_mask;
  1437. unsigned int missing;
  1438. unsigned int loops;
  1439. u8 nport;
  1440. int ret;
  1441. if (port_mask == 0) {
  1442. if (lock_mask)
  1443. *lock_mask = 0;
  1444. return 0;
  1445. }
  1446. if (port_mask >= BIT(priv->hw_data->num_rxports))
  1447. return -EINVAL;
  1448. timeout = jiffies + msecs_to_jiffies(1000);
  1449. loops = 0;
  1450. link_ok_mask = 0;
  1451. while (time_before(jiffies, timeout)) {
  1452. bool fpd4_wa = false;
  1453. missing = 0;
  1454. for_each_set_bit(nport, &port_mask,
  1455. priv->hw_data->num_rxports) {
  1456. struct ub960_rxport *rxport = priv->rxports[nport];
  1457. bool ok;
  1458. if (!rxport)
  1459. continue;
  1460. ret = ub960_rxport_link_ok(priv, nport, &ok);
  1461. if (ret)
  1462. return ret;
  1463. if (!ok && rxport->cdr_mode == RXPORT_CDR_FPD4)
  1464. fpd4_wa = true;
  1465. /*
  1466. * We want the link to be ok for two consecutive loops,
  1467. * as a link could get established just before our test
  1468. * and drop soon after.
  1469. */
  1470. if (!ok || !(link_ok_mask & BIT(nport)))
  1471. missing++;
  1472. if (ok)
  1473. link_ok_mask |= BIT(nport);
  1474. else
  1475. link_ok_mask &= ~BIT(nport);
  1476. }
  1477. loops++;
  1478. if (missing == 0)
  1479. break;
  1480. if (fpd4_wa) {
  1481. ret = ub960_rxport_lockup_wa_ub9702(priv);
  1482. if (ret)
  1483. return ret;
  1484. }
  1485. /*
  1486. * The sleep time of 10 ms was found by testing to give a lock
  1487. * with a few iterations. It can be decreased if on some setups
  1488. * the lock can be achieved much faster.
  1489. */
  1490. fsleep(10 * USEC_PER_MSEC);
  1491. }
  1492. if (lock_mask)
  1493. *lock_mask = link_ok_mask;
  1494. dev_dbg(dev, "Wait locks done in %u loops\n", loops);
  1495. for_each_set_bit(nport, &port_mask, priv->hw_data->num_rxports) {
  1496. struct ub960_rxport *rxport = priv->rxports[nport];
  1497. s8 strobe_pos, eq_level;
  1498. u16 v;
  1499. if (!rxport)
  1500. continue;
  1501. if (!(link_ok_mask & BIT(nport))) {
  1502. dev_dbg(dev, "\trx%u: not locked\n", nport);
  1503. continue;
  1504. }
  1505. ret = ub960_rxport_read16(priv, nport, UB960_RR_RX_FREQ_HIGH,
  1506. &v, NULL);
  1507. if (ret)
  1508. return ret;
  1509. if (priv->hw_data->is_ub9702) {
  1510. dev_dbg(dev, "\trx%u: locked, freq %llu Hz\n",
  1511. nport, ((u64)v * HZ_PER_MHZ) >> 8);
  1512. } else {
  1513. ret = ub960_rxport_get_strobe_pos(priv, nport,
  1514. &strobe_pos);
  1515. if (ret)
  1516. return ret;
  1517. ret = ub960_rxport_get_eq_level(priv, nport, &eq_level);
  1518. if (ret)
  1519. return ret;
  1520. dev_dbg(dev,
  1521. "\trx%u: locked, SP: %d, EQ: %u, freq %llu Hz\n",
  1522. nport, strobe_pos, eq_level,
  1523. ((u64)v * HZ_PER_MHZ) >> 8);
  1524. }
  1525. }
  1526. return 0;
  1527. }
  1528. static unsigned long ub960_calc_bc_clk_rate_ub960(struct ub960_data *priv,
  1529. struct ub960_rxport *rxport)
  1530. {
  1531. unsigned int mult;
  1532. unsigned int div;
  1533. switch (rxport->rx_mode) {
  1534. case RXPORT_MODE_RAW10:
  1535. case RXPORT_MODE_RAW12_HF:
  1536. case RXPORT_MODE_RAW12_LF:
  1537. mult = 1;
  1538. div = 10;
  1539. break;
  1540. case RXPORT_MODE_CSI2_SYNC:
  1541. mult = 2;
  1542. div = 1;
  1543. break;
  1544. case RXPORT_MODE_CSI2_NONSYNC:
  1545. mult = 2;
  1546. div = 5;
  1547. break;
  1548. default:
  1549. return 0;
  1550. }
  1551. return clk_get_rate(priv->refclk) * mult / div;
  1552. }
  1553. static unsigned long ub960_calc_bc_clk_rate_ub9702(struct ub960_data *priv,
  1554. struct ub960_rxport *rxport)
  1555. {
  1556. switch (rxport->rx_mode) {
  1557. case RXPORT_MODE_RAW10:
  1558. case RXPORT_MODE_RAW12_HF:
  1559. case RXPORT_MODE_RAW12_LF:
  1560. return 2359400;
  1561. case RXPORT_MODE_CSI2_SYNC:
  1562. return 47187500;
  1563. case RXPORT_MODE_CSI2_NONSYNC:
  1564. return 9437500;
  1565. default:
  1566. return 0;
  1567. }
  1568. }
  1569. static int ub960_rxport_serializer_write(struct ub960_rxport *rxport, u8 reg,
  1570. u8 val, int *err)
  1571. {
  1572. struct ub960_data *priv = rxport->priv;
  1573. struct device *dev = &priv->client->dev;
  1574. union i2c_smbus_data data;
  1575. int ret;
  1576. if (err && *err)
  1577. return *err;
  1578. data.byte = val;
  1579. ret = i2c_smbus_xfer(priv->client->adapter, rxport->ser.alias, 0,
  1580. I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, &data);
  1581. if (ret)
  1582. dev_err(dev,
  1583. "rx%u: cannot write serializer register 0x%02x (%d)!\n",
  1584. rxport->nport, reg, ret);
  1585. if (ret && err)
  1586. *err = ret;
  1587. return ret;
  1588. }
  1589. static int ub960_rxport_serializer_read(struct ub960_rxport *rxport, u8 reg,
  1590. u8 *val, int *err)
  1591. {
  1592. struct ub960_data *priv = rxport->priv;
  1593. struct device *dev = &priv->client->dev;
  1594. union i2c_smbus_data data = { 0 };
  1595. int ret;
  1596. if (err && *err)
  1597. return *err;
  1598. ret = i2c_smbus_xfer(priv->client->adapter, rxport->ser.alias,
  1599. priv->client->flags, I2C_SMBUS_READ, reg,
  1600. I2C_SMBUS_BYTE_DATA, &data);
  1601. if (ret)
  1602. dev_err(dev,
  1603. "rx%u: cannot read serializer register 0x%02x (%d)!\n",
  1604. rxport->nport, reg, ret);
  1605. else
  1606. *val = data.byte;
  1607. if (ret && err)
  1608. *err = ret;
  1609. return ret;
  1610. }
  1611. static int ub960_serializer_temp_ramp(struct ub960_rxport *rxport)
  1612. {
  1613. struct ub960_data *priv = rxport->priv;
  1614. short temp_dynamic_offset[] = {-1, -1, 0, 0, 1, 1, 1, 3};
  1615. u8 temp_dynamic_cfg;
  1616. u8 nport = rxport->nport;
  1617. u8 ser_temp_code;
  1618. int ret = 0;
  1619. /* Configure temp ramp only on UB953 */
  1620. if (!fwnode_device_is_compatible(rxport->ser.fwnode, "ti,ds90ub953-q1"))
  1621. return 0;
  1622. /* Read current serializer die temperature */
  1623. ub960_rxport_read(priv, nport, UB960_RR_SENSOR_STS_2, &ser_temp_code,
  1624. &ret);
  1625. /* Enable I2C passthrough on back channel */
  1626. ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
  1627. UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH,
  1628. UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH, &ret);
  1629. if (ret)
  1630. return ret;
  1631. /* Select indirect page for analog regs on the serializer */
  1632. ub960_rxport_serializer_write(rxport, UB953_REG_IND_ACC_CTL,
  1633. UB953_IND_TARGET_ANALOG << 2, &ret);
  1634. /* Set temperature ramp dynamic and static config */
  1635. ub960_rxport_serializer_write(rxport, UB953_REG_IND_ACC_ADDR,
  1636. UB953_IND_ANA_TEMP_DYNAMIC_CFG, &ret);
  1637. ub960_rxport_serializer_read(rxport, UB953_REG_IND_ACC_DATA,
  1638. &temp_dynamic_cfg, &ret);
  1639. if (ret)
  1640. return ret;
  1641. temp_dynamic_cfg |= UB953_IND_ANA_TEMP_DYNAMIC_CFG_OV;
  1642. temp_dynamic_cfg += temp_dynamic_offset[ser_temp_code];
  1643. /* Update temp static config */
  1644. ub960_rxport_serializer_write(rxport, UB953_REG_IND_ACC_ADDR,
  1645. UB953_IND_ANA_TEMP_STATIC_CFG, &ret);
  1646. ub960_rxport_serializer_write(rxport, UB953_REG_IND_ACC_DATA,
  1647. UB953_IND_ANA_TEMP_STATIC_CFG_MASK, &ret);
  1648. /* Update temperature ramp dynamic config */
  1649. ub960_rxport_serializer_write(rxport, UB953_REG_IND_ACC_ADDR,
  1650. UB953_IND_ANA_TEMP_DYNAMIC_CFG, &ret);
  1651. /* Enable I2C auto ack on BC before we set dynamic cfg and reset */
  1652. ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
  1653. UB960_RR_BCC_CONFIG_AUTO_ACK_ALL,
  1654. UB960_RR_BCC_CONFIG_AUTO_ACK_ALL, &ret);
  1655. ub960_rxport_serializer_write(rxport, UB953_REG_IND_ACC_DATA,
  1656. temp_dynamic_cfg, &ret);
  1657. if (ret)
  1658. return ret;
  1659. /* Soft reset to apply PLL updates */
  1660. ub960_rxport_serializer_write(rxport, UB953_REG_RESET_CTL,
  1661. UB953_REG_RESET_CTL_DIGITAL_RESET_0,
  1662. &ret);
  1663. msleep(20);
  1664. /* Disable I2C passthrough and auto-ack on BC */
  1665. ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
  1666. UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH |
  1667. UB960_RR_BCC_CONFIG_AUTO_ACK_ALL,
  1668. 0x0, &ret);
  1669. return ret;
  1670. }
  1671. static int ub960_rxport_bc_ser_config(struct ub960_rxport *rxport)
  1672. {
  1673. struct ub960_data *priv = rxport->priv;
  1674. struct device *dev = &priv->client->dev;
  1675. u8 nport = rxport->nport;
  1676. int ret = 0;
  1677. /* Skip port if serializer's address is not known */
  1678. if (rxport->ser.addr < 0) {
  1679. dev_dbg(dev,
  1680. "rx%u: serializer address missing, skip configuration\n",
  1681. nport);
  1682. return 0;
  1683. }
  1684. /*
  1685. * Note: the code here probably only works for CSI-2 serializers in
  1686. * sync mode. To support other serializers the BC related configuration
  1687. * should be done before calling this function.
  1688. */
  1689. /* Enable I2C passthrough and auto-ack on BC */
  1690. ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
  1691. UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH |
  1692. UB960_RR_BCC_CONFIG_AUTO_ACK_ALL,
  1693. UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH |
  1694. UB960_RR_BCC_CONFIG_AUTO_ACK_ALL,
  1695. &ret);
  1696. if (ret)
  1697. return ret;
  1698. /* Disable BC alternate mode auto detect */
  1699. ub960_rxport_serializer_write(rxport, UB971_ENH_BC_CHK, 0x02, &ret);
  1700. /* Decrease link detect timer */
  1701. ub960_rxport_serializer_write(rxport, UB953_REG_BC_CTRL, 0x06, &ret);
  1702. /* Disable I2C passthrough and auto-ack on BC */
  1703. ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
  1704. UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH |
  1705. UB960_RR_BCC_CONFIG_AUTO_ACK_ALL,
  1706. 0x0, &ret);
  1707. return ret;
  1708. }
  1709. static int ub960_rxport_add_serializer(struct ub960_data *priv, u8 nport)
  1710. {
  1711. struct ub960_rxport *rxport = priv->rxports[nport];
  1712. struct device *dev = &priv->client->dev;
  1713. struct ds90ub9xx_platform_data *ser_pdata = &rxport->ser.pdata;
  1714. struct i2c_board_info ser_info = {
  1715. .fwnode = rxport->ser.fwnode,
  1716. .platform_data = ser_pdata,
  1717. };
  1718. ser_pdata->port = nport;
  1719. ser_pdata->atr = priv->atr;
  1720. if (priv->hw_data->is_ub9702)
  1721. ser_pdata->bc_rate = ub960_calc_bc_clk_rate_ub9702(priv, rxport);
  1722. else
  1723. ser_pdata->bc_rate = ub960_calc_bc_clk_rate_ub960(priv, rxport);
  1724. /*
  1725. * The serializer is added under the same i2c adapter as the
  1726. * deserializer. This is not quite right, as the serializer is behind
  1727. * the FPD-Link.
  1728. */
  1729. ser_info.addr = rxport->ser.alias;
  1730. rxport->ser.client =
  1731. i2c_new_client_device(priv->client->adapter, &ser_info);
  1732. if (IS_ERR(rxport->ser.client)) {
  1733. dev_err(dev, "rx%u: cannot add %s i2c device", nport,
  1734. ser_info.type);
  1735. return PTR_ERR(rxport->ser.client);
  1736. }
  1737. dev_dbg(dev, "rx%u: remote serializer at alias 0x%02x (%u-%04x)\n",
  1738. nport, rxport->ser.client->addr,
  1739. rxport->ser.client->adapter->nr, rxport->ser.client->addr);
  1740. return 0;
  1741. }
  1742. static void ub960_rxport_remove_serializer(struct ub960_data *priv, u8 nport)
  1743. {
  1744. struct ub960_rxport *rxport = priv->rxports[nport];
  1745. i2c_unregister_device(rxport->ser.client);
  1746. rxport->ser.client = NULL;
  1747. }
  1748. /* Add serializer i2c devices for all initialized ports */
  1749. static int ub960_rxport_add_serializers(struct ub960_data *priv)
  1750. {
  1751. unsigned int failed_nport;
  1752. int ret;
  1753. for_each_active_rxport(priv, it) {
  1754. ret = ub960_rxport_add_serializer(priv, it.nport);
  1755. if (ret) {
  1756. failed_nport = it.nport;
  1757. goto err_remove_sers;
  1758. }
  1759. }
  1760. return 0;
  1761. err_remove_sers:
  1762. while (failed_nport--) {
  1763. struct ub960_rxport *rxport = priv->rxports[failed_nport];
  1764. if (!rxport)
  1765. continue;
  1766. ub960_rxport_remove_serializer(priv, failed_nport);
  1767. }
  1768. return ret;
  1769. }
  1770. static void ub960_rxport_remove_serializers(struct ub960_data *priv)
  1771. {
  1772. for_each_active_rxport(priv, it)
  1773. ub960_rxport_remove_serializer(priv, it.nport);
  1774. }
  1775. static int ub960_init_tx_port(struct ub960_data *priv,
  1776. struct ub960_txport *txport)
  1777. {
  1778. unsigned int nport = txport->nport;
  1779. u8 csi_ctl = 0;
  1780. /*
  1781. * From the datasheet: "initial CSI Skew-Calibration
  1782. * sequence [...] should be set when operating at 1.6 Gbps"
  1783. */
  1784. if (priv->tx_data_rate == MHZ(1600))
  1785. csi_ctl |= UB960_TR_CSI_CTL_CSI_CAL_EN;
  1786. csi_ctl |= (4 - txport->num_data_lanes) << 4;
  1787. if (!txport->non_continous_clk)
  1788. csi_ctl |= UB960_TR_CSI_CTL_CSI_CONTS_CLOCK;
  1789. return ub960_txport_write(priv, nport, UB960_TR_CSI_CTL, csi_ctl, NULL);
  1790. }
  1791. static int ub960_init_tx_ports_ub960(struct ub960_data *priv)
  1792. {
  1793. u8 speed_select;
  1794. switch (priv->tx_data_rate) {
  1795. case MHZ(400):
  1796. speed_select = 3;
  1797. break;
  1798. case MHZ(800):
  1799. speed_select = 2;
  1800. break;
  1801. case MHZ(1200):
  1802. speed_select = 1;
  1803. break;
  1804. case MHZ(1600):
  1805. default:
  1806. speed_select = 0;
  1807. break;
  1808. }
  1809. return ub960_write(priv, UB960_SR_CSI_PLL_CTL, speed_select, NULL);
  1810. }
  1811. static int ub960_init_tx_ports_ub9702(struct ub960_data *priv)
  1812. {
  1813. u8 speed_select;
  1814. u8 ana_pll_div;
  1815. u8 pll_div;
  1816. int ret = 0;
  1817. switch (priv->tx_data_rate) {
  1818. case MHZ(400):
  1819. speed_select = 3;
  1820. pll_div = 0x10;
  1821. ana_pll_div = 0xa2;
  1822. break;
  1823. case MHZ(800):
  1824. speed_select = 2;
  1825. pll_div = 0x10;
  1826. ana_pll_div = 0x92;
  1827. break;
  1828. case MHZ(1200):
  1829. speed_select = 1;
  1830. pll_div = 0x18;
  1831. ana_pll_div = 0x90;
  1832. break;
  1833. case MHZ(1500):
  1834. speed_select = 0;
  1835. pll_div = 0x0f;
  1836. ana_pll_div = 0x82;
  1837. break;
  1838. case MHZ(1600):
  1839. default:
  1840. speed_select = 0;
  1841. pll_div = 0x10;
  1842. ana_pll_div = 0x82;
  1843. break;
  1844. case MHZ(2500):
  1845. speed_select = 0x10;
  1846. pll_div = 0x19;
  1847. ana_pll_div = 0x80;
  1848. break;
  1849. }
  1850. ub960_write(priv, UB960_SR_CSI_PLL_CTL, speed_select, &ret);
  1851. ub960_write(priv, UB9702_SR_CSI_PLL_DIV, pll_div, &ret);
  1852. ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA,
  1853. UB9702_IR_CSI_ANA_CSIPLL_REG_1, ana_pll_div, &ret);
  1854. return ret;
  1855. }
  1856. static int ub960_init_tx_ports(struct ub960_data *priv)
  1857. {
  1858. int ret;
  1859. if (priv->hw_data->is_ub9702)
  1860. ret = ub960_init_tx_ports_ub9702(priv);
  1861. else
  1862. ret = ub960_init_tx_ports_ub960(priv);
  1863. if (ret)
  1864. return ret;
  1865. for (unsigned int nport = 0; nport < priv->hw_data->num_txports;
  1866. nport++) {
  1867. struct ub960_txport *txport = priv->txports[nport];
  1868. if (!txport)
  1869. continue;
  1870. ret = ub960_init_tx_port(priv, txport);
  1871. if (ret)
  1872. return ret;
  1873. }
  1874. return 0;
  1875. }
  1876. static int ub960_init_rx_port_ub960(struct ub960_data *priv,
  1877. struct ub960_rxport *rxport)
  1878. {
  1879. unsigned int nport = rxport->nport;
  1880. u32 bc_freq_val;
  1881. int ret = 0;
  1882. /*
  1883. * Back channel frequency select.
  1884. * Override FREQ_SELECT from the strap.
  1885. * 0 - 2.5 Mbps (DS90UB913A-Q1 / DS90UB933-Q1)
  1886. * 2 - 10 Mbps
  1887. * 6 - 50 Mbps (DS90UB953-Q1)
  1888. *
  1889. * Note that changing this setting will result in some errors on the back
  1890. * channel for a short period of time.
  1891. */
  1892. switch (rxport->rx_mode) {
  1893. case RXPORT_MODE_RAW10:
  1894. case RXPORT_MODE_RAW12_HF:
  1895. case RXPORT_MODE_RAW12_LF:
  1896. bc_freq_val = 0;
  1897. break;
  1898. case RXPORT_MODE_CSI2_NONSYNC:
  1899. bc_freq_val = 2;
  1900. break;
  1901. case RXPORT_MODE_CSI2_SYNC:
  1902. bc_freq_val = 6;
  1903. break;
  1904. default:
  1905. return -EINVAL;
  1906. }
  1907. ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
  1908. UB960_RR_BCC_CONFIG_BC_FREQ_SEL_MASK,
  1909. bc_freq_val, &ret);
  1910. switch (rxport->rx_mode) {
  1911. case RXPORT_MODE_RAW10:
  1912. /* FPD3_MODE = RAW10 Mode (DS90UB913A-Q1 / DS90UB933-Q1 compatible) */
  1913. ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG,
  1914. UB960_RR_PORT_CONFIG_FPD3_MODE_MASK,
  1915. 0x3, &ret);
  1916. /*
  1917. * RAW10_8BIT_CTL = 0b10 : 8-bit processing using upper 8 bits
  1918. */
  1919. ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG2,
  1920. UB960_RR_PORT_CONFIG2_RAW10_8BIT_CTL_MASK,
  1921. 0x2 << UB960_RR_PORT_CONFIG2_RAW10_8BIT_CTL_SHIFT,
  1922. &ret);
  1923. break;
  1924. case RXPORT_MODE_RAW12_HF:
  1925. case RXPORT_MODE_RAW12_LF:
  1926. /* Not implemented */
  1927. return -EINVAL;
  1928. case RXPORT_MODE_CSI2_SYNC:
  1929. case RXPORT_MODE_CSI2_NONSYNC:
  1930. /* CSI-2 Mode (DS90UB953-Q1 compatible) */
  1931. ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG, 0x3,
  1932. 0x0, &ret);
  1933. break;
  1934. }
  1935. /* LV_POLARITY & FV_POLARITY */
  1936. ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG2, 0x3,
  1937. rxport->lv_fv_pol, &ret);
  1938. /* Enable all interrupt sources from this port */
  1939. ub960_rxport_write(priv, nport, UB960_RR_PORT_ICR_HI, 0x07, &ret);
  1940. ub960_rxport_write(priv, nport, UB960_RR_PORT_ICR_LO, 0x7f, &ret);
  1941. /* Enable I2C_PASS_THROUGH */
  1942. ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
  1943. UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH,
  1944. UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH, &ret);
  1945. /* Enable I2C communication to the serializer via the alias addr */
  1946. ub960_rxport_write(priv, nport, UB960_RR_SER_ALIAS_ID,
  1947. rxport->ser.alias << 1, &ret);
  1948. /* Configure EQ related settings */
  1949. ub960_rxport_config_eq(priv, nport);
  1950. /* Enable RX port */
  1951. ub960_update_bits(priv, UB960_SR_RX_PORT_CTL, BIT(nport), BIT(nport),
  1952. &ret);
  1953. return ret;
  1954. }
  1955. static int ub960_init_rx_ports_ub960(struct ub960_data *priv)
  1956. {
  1957. struct device *dev = &priv->client->dev;
  1958. unsigned int port_lock_mask;
  1959. unsigned int port_mask;
  1960. int ret;
  1961. for_each_active_rxport(priv, it) {
  1962. ret = ub960_init_rx_port_ub960(priv, it.rxport);
  1963. if (ret)
  1964. return ret;
  1965. }
  1966. ret = ub960_reset(priv, false);
  1967. if (ret)
  1968. return ret;
  1969. port_mask = 0;
  1970. for_each_active_rxport(priv, it)
  1971. port_mask |= BIT(it.nport);
  1972. ret = ub960_rxport_wait_locks(priv, port_mask, &port_lock_mask);
  1973. if (ret)
  1974. return ret;
  1975. if (port_mask != port_lock_mask) {
  1976. ret = -EIO;
  1977. dev_err_probe(dev, ret, "Failed to lock all RX ports\n");
  1978. return ret;
  1979. }
  1980. /* Set temperature ramp on serializer */
  1981. for_each_active_rxport(priv, it) {
  1982. ret = ub960_serializer_temp_ramp(it.rxport);
  1983. if (ret)
  1984. return ret;
  1985. ub960_rxport_update_bits(priv, it.nport, UB960_RR_BCC_CONFIG,
  1986. UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH,
  1987. UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH,
  1988. &ret);
  1989. if (ret)
  1990. return ret;
  1991. }
  1992. /*
  1993. * Clear any errors caused by switching the RX port settings while
  1994. * probing.
  1995. */
  1996. ret = ub960_clear_rx_errors(priv);
  1997. if (ret)
  1998. return ret;
  1999. return 0;
  2000. }
  2001. /*
  2002. * UB9702 specific initial RX port configuration
  2003. */
  2004. static int ub960_turn_off_rxport_ub9702(struct ub960_data *priv,
  2005. unsigned int nport)
  2006. {
  2007. int ret = 0;
  2008. /* Disable RX port */
  2009. ub960_update_bits(priv, UB960_SR_RX_PORT_CTL, BIT(nport), 0, &ret);
  2010. /* Disable FPD Rx and FPD BC CMR */
  2011. ub960_rxport_write(priv, nport, UB9702_RR_RX_CTL_2, 0x1b, &ret);
  2012. /* Disable FPD BC Tx */
  2013. ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG, BIT(4), 0,
  2014. &ret);
  2015. /* Disable internal RX blocks */
  2016. ub960_rxport_write(priv, nport, UB9702_RR_RX_CTL_1, 0x15, &ret);
  2017. /* Disable AEQ */
  2018. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2019. UB9702_IR_RX_ANA_AEQ_CFG_2, 0x03, &ret);
  2020. /* PI disabled and oDAC disabled */
  2021. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2022. UB9702_IR_RX_ANA_AEQ_CFG_4, 0x09, &ret);
  2023. /* AEQ configured for disabled link */
  2024. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2025. UB9702_IR_RX_ANA_AEQ_CFG_1, 0x20, &ret);
  2026. /* disable AEQ clock and DFE */
  2027. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2028. UB9702_IR_RX_ANA_AEQ_CFG_3, 0x45, &ret);
  2029. /* Powerdown FPD3 CDR */
  2030. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2031. UB9702_IR_RX_ANA_FPD3_CDR_CTRL_SEL_5, 0x82, &ret);
  2032. return ret;
  2033. }
  2034. static int ub960_set_bc_drv_config_ub9702(struct ub960_data *priv,
  2035. unsigned int nport)
  2036. {
  2037. u8 fpd_bc_ctl0;
  2038. u8 fpd_bc_ctl1;
  2039. u8 fpd_bc_ctl2;
  2040. int ret = 0;
  2041. if (priv->rxports[nport]->cdr_mode == RXPORT_CDR_FPD4) {
  2042. /* Set FPD PBC drv into FPD IV mode */
  2043. fpd_bc_ctl0 = 0;
  2044. fpd_bc_ctl1 = 0;
  2045. fpd_bc_ctl2 = 0;
  2046. } else {
  2047. /* Set FPD PBC drv into FPD III mode */
  2048. fpd_bc_ctl0 = 2;
  2049. fpd_bc_ctl1 = 1;
  2050. fpd_bc_ctl2 = 5;
  2051. }
  2052. ub960_ind_update_bits(priv, UB960_IND_TARGET_RX_ANA(nport),
  2053. UB9702_IR_RX_ANA_FPD_BC_CTL0, GENMASK(7, 5),
  2054. fpd_bc_ctl0 << 5, &ret);
  2055. ub960_ind_update_bits(priv, UB960_IND_TARGET_RX_ANA(nport),
  2056. UB9702_IR_RX_ANA_FPD_BC_CTL1, BIT(6),
  2057. fpd_bc_ctl1 << 6, &ret);
  2058. ub960_ind_update_bits(priv, UB960_IND_TARGET_RX_ANA(nport),
  2059. UB9702_IR_RX_ANA_FPD_BC_CTL2, GENMASK(6, 3),
  2060. fpd_bc_ctl2 << 3, &ret);
  2061. return ret;
  2062. }
  2063. static int ub960_set_fpd4_sync_mode_ub9702(struct ub960_data *priv,
  2064. unsigned int nport)
  2065. {
  2066. int ret = 0;
  2067. /* FPD4 Sync Mode */
  2068. ub960_rxport_write(priv, nport, UB9702_RR_CHANNEL_MODE, 0x0, &ret);
  2069. /* BC_FREQ_SELECT = (PLL_FREQ/3200) Mbps */
  2070. ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
  2071. UB960_RR_BCC_CONFIG_BC_FREQ_SEL_MASK, 6, &ret);
  2072. if (ret)
  2073. return ret;
  2074. ret = ub960_set_bc_drv_config_ub9702(priv, nport);
  2075. if (ret)
  2076. return ret;
  2077. /* Set AEQ timer to 400us/step */
  2078. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2079. UB9702_IR_RX_ANA_SYSTEM_INIT_REG0, 0x2f, &ret);
  2080. /* Disable FPD4 Auto Recovery */
  2081. ub960_update_bits(priv, UB9702_SR_CSI_EXCLUSIVE_FWD2, GENMASK(5, 4), 0,
  2082. &ret);
  2083. /* Enable RX port */
  2084. ub960_update_bits(priv, UB960_SR_RX_PORT_CTL, BIT(nport), BIT(nport),
  2085. &ret);
  2086. /* Enable FPD4 Auto Recovery */
  2087. ub960_update_bits(priv, UB9702_SR_CSI_EXCLUSIVE_FWD2, GENMASK(5, 4),
  2088. BIT(4), &ret);
  2089. return ret;
  2090. }
  2091. static int ub960_set_fpd4_async_mode_ub9702(struct ub960_data *priv,
  2092. unsigned int nport)
  2093. {
  2094. int ret = 0;
  2095. /* FPD4 ASync Mode */
  2096. ub960_rxport_write(priv, nport, UB9702_RR_CHANNEL_MODE, 0x1, &ret);
  2097. /* 10Mbps w/ BC enabled */
  2098. /* BC_FREQ_SELECT=(PLL_FREQ/3200) Mbps */
  2099. ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
  2100. UB960_RR_BCC_CONFIG_BC_FREQ_SEL_MASK, 2, &ret);
  2101. if (ret)
  2102. return ret;
  2103. ret = ub960_set_bc_drv_config_ub9702(priv, nport);
  2104. if (ret)
  2105. return ret;
  2106. /* Set AEQ timer to 400us/step */
  2107. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2108. UB9702_IR_RX_ANA_SYSTEM_INIT_REG0, 0x2f, &ret);
  2109. /* Disable FPD4 Auto Recover */
  2110. ub960_update_bits(priv, UB9702_SR_CSI_EXCLUSIVE_FWD2, GENMASK(5, 4), 0,
  2111. &ret);
  2112. /* Enable RX port */
  2113. ub960_update_bits(priv, UB960_SR_RX_PORT_CTL, BIT(nport), BIT(nport),
  2114. &ret);
  2115. /* Enable FPD4 Auto Recovery */
  2116. ub960_update_bits(priv, UB9702_SR_CSI_EXCLUSIVE_FWD2, GENMASK(5, 4),
  2117. BIT(4), &ret);
  2118. return ret;
  2119. }
  2120. static int ub960_set_fpd3_sync_mode_ub9702(struct ub960_data *priv,
  2121. unsigned int nport)
  2122. {
  2123. int ret = 0;
  2124. /* FPD3 Sync Mode */
  2125. ub960_rxport_write(priv, nport, UB9702_RR_CHANNEL_MODE, 0x2, &ret);
  2126. /* BC_FREQ_SELECT=(PLL_FREQ/3200) Mbps */
  2127. ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
  2128. UB960_RR_BCC_CONFIG_BC_FREQ_SEL_MASK, 6, &ret);
  2129. /* Set AEQ_LOCK_MODE = 1 */
  2130. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2131. UB9702_IR_RX_ANA_FPD3_AEQ_CTRL_SEL_1, BIT(7), &ret);
  2132. if (ret)
  2133. return ret;
  2134. ret = ub960_set_bc_drv_config_ub9702(priv, nport);
  2135. if (ret)
  2136. return ret;
  2137. /* Enable RX port */
  2138. ub960_update_bits(priv, UB960_SR_RX_PORT_CTL, BIT(nport), BIT(nport),
  2139. &ret);
  2140. return ret;
  2141. }
  2142. static int ub960_set_raw10_dvp_mode_ub9702(struct ub960_data *priv,
  2143. unsigned int nport)
  2144. {
  2145. int ret = 0;
  2146. /* FPD3 RAW10 Mode */
  2147. ub960_rxport_write(priv, nport, UB9702_RR_CHANNEL_MODE, 0x5, &ret);
  2148. ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
  2149. UB960_RR_BCC_CONFIG_BC_FREQ_SEL_MASK, 0, &ret);
  2150. /* Set AEQ_LOCK_MODE = 1 */
  2151. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2152. UB9702_IR_RX_ANA_FPD3_AEQ_CTRL_SEL_1, BIT(7), &ret);
  2153. /*
  2154. * RAW10_8BIT_CTL = 0b11 : 8-bit processing using lower 8 bits
  2155. * 0b10 : 8-bit processing using upper 8 bits
  2156. */
  2157. ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG2, 0x3 << 6,
  2158. 0x2 << 6, &ret);
  2159. /* LV_POLARITY & FV_POLARITY */
  2160. ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG2, 0x3,
  2161. priv->rxports[nport]->lv_fv_pol, &ret);
  2162. if (ret)
  2163. return ret;
  2164. ret = ub960_set_bc_drv_config_ub9702(priv, nport);
  2165. if (ret)
  2166. return ret;
  2167. /* Enable RX port */
  2168. ub960_update_bits(priv, UB960_SR_RX_PORT_CTL, BIT(nport), BIT(nport),
  2169. &ret);
  2170. return ret;
  2171. }
  2172. static int ub960_configure_rx_port_ub9702(struct ub960_data *priv,
  2173. unsigned int nport)
  2174. {
  2175. struct device *dev = &priv->client->dev;
  2176. struct ub960_rxport *rxport = priv->rxports[nport];
  2177. int ret;
  2178. if (!rxport) {
  2179. ret = ub960_turn_off_rxport_ub9702(priv, nport);
  2180. if (ret)
  2181. return ret;
  2182. dev_dbg(dev, "rx%u: disabled\n", nport);
  2183. return 0;
  2184. }
  2185. switch (rxport->cdr_mode) {
  2186. case RXPORT_CDR_FPD4:
  2187. switch (rxport->rx_mode) {
  2188. case RXPORT_MODE_CSI2_SYNC:
  2189. ret = ub960_set_fpd4_sync_mode_ub9702(priv, nport);
  2190. if (ret)
  2191. return ret;
  2192. dev_dbg(dev, "rx%u: FPD-Link IV SYNC mode\n", nport);
  2193. break;
  2194. case RXPORT_MODE_CSI2_NONSYNC:
  2195. ret = ub960_set_fpd4_async_mode_ub9702(priv, nport);
  2196. if (ret)
  2197. return ret;
  2198. dev_dbg(dev, "rx%u: FPD-Link IV ASYNC mode\n", nport);
  2199. break;
  2200. default:
  2201. dev_err(dev, "rx%u: unsupported FPD4 mode %u\n", nport,
  2202. rxport->rx_mode);
  2203. return -EINVAL;
  2204. }
  2205. break;
  2206. case RXPORT_CDR_FPD3:
  2207. switch (rxport->rx_mode) {
  2208. case RXPORT_MODE_CSI2_SYNC:
  2209. ret = ub960_set_fpd3_sync_mode_ub9702(priv, nport);
  2210. if (ret)
  2211. return ret;
  2212. dev_dbg(dev, "rx%u: FPD-Link III SYNC mode\n", nport);
  2213. break;
  2214. case RXPORT_MODE_RAW10:
  2215. ret = ub960_set_raw10_dvp_mode_ub9702(priv, nport);
  2216. if (ret)
  2217. return ret;
  2218. dev_dbg(dev, "rx%u: FPD-Link III RAW10 DVP mode\n",
  2219. nport);
  2220. break;
  2221. default:
  2222. dev_err(&priv->client->dev,
  2223. "rx%u: unsupported FPD3 mode %u\n", nport,
  2224. rxport->rx_mode);
  2225. return -EINVAL;
  2226. }
  2227. break;
  2228. default:
  2229. dev_err(&priv->client->dev, "rx%u: unsupported CDR mode %u\n",
  2230. nport, rxport->cdr_mode);
  2231. return -EINVAL;
  2232. }
  2233. return 0;
  2234. }
  2235. static int ub960_lock_recovery_ub9702(struct ub960_data *priv,
  2236. unsigned int nport)
  2237. {
  2238. struct device *dev = &priv->client->dev;
  2239. /* Assumption that max AEQ should be under 16 */
  2240. const u8 rx_aeq_limit = 16;
  2241. u8 prev_aeq = 0xff;
  2242. bool rx_lock;
  2243. for (unsigned int retry = 0; retry < 3; ++retry) {
  2244. u8 port_sts1;
  2245. u8 rx_aeq;
  2246. int ret;
  2247. ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1,
  2248. &port_sts1, NULL);
  2249. if (ret)
  2250. return ret;
  2251. rx_lock = port_sts1 & UB960_RR_RX_PORT_STS1_PORT_PASS;
  2252. if (!rx_lock) {
  2253. ret = ub960_rxport_lockup_wa_ub9702(priv);
  2254. if (ret)
  2255. return ret;
  2256. /* Restart AEQ by changing max to 0 --> 0x23 */
  2257. ret = ub960_write_ind(priv,
  2258. UB960_IND_TARGET_RX_ANA(nport),
  2259. UB9702_IR_RX_ANA_AEQ_ALP_SEL7, 0,
  2260. NULL);
  2261. if (ret)
  2262. return ret;
  2263. msleep(20);
  2264. /* AEQ Restart */
  2265. ret = ub960_write_ind(priv,
  2266. UB960_IND_TARGET_RX_ANA(nport),
  2267. UB9702_IR_RX_ANA_AEQ_ALP_SEL7,
  2268. 0x23, NULL);
  2269. if (ret)
  2270. return ret;
  2271. msleep(20);
  2272. dev_dbg(dev, "rx%u: no lock, retry = %u\n", nport,
  2273. retry);
  2274. continue;
  2275. }
  2276. ret = ub960_read_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2277. UB9702_IR_RX_ANA_AEQ_ALP_SEL11, &rx_aeq,
  2278. NULL);
  2279. if (ret)
  2280. return ret;
  2281. if (rx_aeq < rx_aeq_limit) {
  2282. dev_dbg(dev,
  2283. "rx%u: locked and AEQ normal before setting AEQ window\n",
  2284. nport);
  2285. return 0;
  2286. }
  2287. if (rx_aeq != prev_aeq) {
  2288. ret = ub960_rxport_lockup_wa_ub9702(priv);
  2289. if (ret)
  2290. return ret;
  2291. /* Restart AEQ by changing max to 0 --> 0x23 */
  2292. ret = ub960_write_ind(priv,
  2293. UB960_IND_TARGET_RX_ANA(nport),
  2294. UB9702_IR_RX_ANA_AEQ_ALP_SEL7,
  2295. 0, NULL);
  2296. if (ret)
  2297. return ret;
  2298. msleep(20);
  2299. /* AEQ Restart */
  2300. ret = ub960_write_ind(priv,
  2301. UB960_IND_TARGET_RX_ANA(nport),
  2302. UB9702_IR_RX_ANA_AEQ_ALP_SEL7,
  2303. 0x23, NULL);
  2304. if (ret)
  2305. return ret;
  2306. msleep(20);
  2307. dev_dbg(dev,
  2308. "rx%u: high AEQ at initial check recovery loop, retry=%u\n",
  2309. nport, retry);
  2310. prev_aeq = rx_aeq;
  2311. } else {
  2312. dev_dbg(dev,
  2313. "rx%u: lossy cable detected, RX_AEQ %#x, RX_AEQ_LIMIT %#x, retry %u\n",
  2314. nport, rx_aeq, rx_aeq_limit, retry);
  2315. dev_dbg(dev,
  2316. "rx%u: will continue with initiation sequence but high AEQ\n",
  2317. nport);
  2318. return 0;
  2319. }
  2320. }
  2321. dev_err(dev, "rx%u: max number of retries: %s\n", nport,
  2322. rx_lock ? "unstable AEQ" : "no lock");
  2323. return -EIO;
  2324. }
  2325. static int ub960_enable_aeq_lms_ub9702(struct ub960_data *priv,
  2326. unsigned int nport)
  2327. {
  2328. struct device *dev = &priv->client->dev;
  2329. u8 read_aeq_init;
  2330. int ret;
  2331. ret = ub960_read_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2332. UB9702_IR_RX_ANA_AEQ_ALP_SEL11, &read_aeq_init,
  2333. NULL);
  2334. if (ret)
  2335. return ret;
  2336. dev_dbg(dev, "rx%u: initial AEQ = %#x\n", nport, read_aeq_init);
  2337. /* Set AEQ Min */
  2338. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2339. UB9702_IR_RX_ANA_AEQ_ALP_SEL6, read_aeq_init, &ret);
  2340. /* Set AEQ Max */
  2341. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2342. UB9702_IR_RX_ANA_AEQ_ALP_SEL7, read_aeq_init + 1, &ret);
  2343. /* Set AEQ offset to 0 */
  2344. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2345. UB9702_IR_RX_ANA_AEQ_ALP_SEL10, 0x0, &ret);
  2346. /* Enable AEQ tap2 */
  2347. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2348. UB9702_IR_RX_ANA_EQ_CTRL_SEL_38, 0x00, &ret);
  2349. /* Set VGA Gain 1 Gain 2 override to 0 */
  2350. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2351. UB9702_IR_RX_ANA_VGA_CTRL_SEL_8, 0x00, &ret);
  2352. /* Set VGA Initial Sweep Gain to 0 */
  2353. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2354. UB9702_IR_RX_ANA_VGA_CTRL_SEL_6, 0x80, &ret);
  2355. /* Set VGA_Adapt (VGA Gain) override to 0 (thermometer encoded) */
  2356. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2357. UB9702_IR_RX_ANA_VGA_CTRL_SEL_3, 0x00, &ret);
  2358. /* Enable VGA_SWEEP */
  2359. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2360. UB9702_IR_RX_ANA_EQ_ADAPT_CTRL, 0x40, &ret);
  2361. /* Disable VGA_SWEEP_GAIN_OV, disable VGA_TUNE_OV */
  2362. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2363. UB9702_IR_RX_ANA_EQ_OVERRIDE_CTRL, 0x00, &ret);
  2364. /* Set VGA HIGH Threshold to 43 */
  2365. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2366. UB9702_IR_RX_ANA_VGA_CTRL_SEL_1, 0x2b, &ret);
  2367. /* Set VGA LOW Threshold to 18 */
  2368. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2369. UB9702_IR_RX_ANA_VGA_CTRL_SEL_2, 0x12, &ret);
  2370. /* Set vga_sweep_th to 32 */
  2371. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2372. UB9702_IR_RX_ANA_EQ_CTRL_SEL_15, 0x20, &ret);
  2373. /* Set AEQ timer to 400us/step and parity threshold to 7 */
  2374. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2375. UB9702_IR_RX_ANA_SYSTEM_INIT_REG0, 0xef, &ret);
  2376. if (ret)
  2377. return ret;
  2378. dev_dbg(dev, "rx%u: enable FPD-Link IV AEQ LMS\n", nport);
  2379. return 0;
  2380. }
  2381. static int ub960_enable_dfe_lms_ub9702(struct ub960_data *priv,
  2382. unsigned int nport)
  2383. {
  2384. struct device *dev = &priv->client->dev;
  2385. int ret = 0;
  2386. /* Enable DFE LMS */
  2387. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2388. UB9702_IR_RX_ANA_EQ_CTRL_SEL_24, 0x40, &ret);
  2389. /* Disable VGA Gain1 override */
  2390. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2391. UB9702_IR_RX_ANA_GAIN_CTRL_0, 0x20, &ret);
  2392. if (ret)
  2393. return ret;
  2394. usleep_range(1000, 5000);
  2395. /* Disable VGA Gain2 override */
  2396. ret = ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
  2397. UB9702_IR_RX_ANA_GAIN_CTRL_0, 0x00, NULL);
  2398. if (ret)
  2399. return ret;
  2400. dev_dbg(dev, "rx%u: enabled FPD-Link IV DFE LMS", nport);
  2401. return 0;
  2402. }
  2403. static int ub960_init_rx_ports_ub9702(struct ub960_data *priv)
  2404. {
  2405. struct device *dev = &priv->client->dev;
  2406. unsigned int port_lock_mask;
  2407. unsigned int port_mask = 0;
  2408. bool have_fpd4 = false;
  2409. int ret;
  2410. for_each_active_rxport(priv, it) {
  2411. ret = ub960_rxport_update_bits(priv, it.nport,
  2412. UB960_RR_BCC_CONFIG,
  2413. UB960_RR_BCC_CONFIG_BC_ALWAYS_ON,
  2414. UB960_RR_BCC_CONFIG_BC_ALWAYS_ON,
  2415. NULL);
  2416. if (ret)
  2417. return ret;
  2418. }
  2419. /* Disable FPD4 Auto Recovery */
  2420. ret = ub960_write(priv, UB9702_SR_CSI_EXCLUSIVE_FWD2, 0x0f, NULL);
  2421. if (ret)
  2422. return ret;
  2423. for_each_active_rxport(priv, it) {
  2424. if (it.rxport->ser.addr >= 0) {
  2425. /*
  2426. * Set serializer's I2C address if set in the dts file,
  2427. * and freeze it to prevent updates from the FC.
  2428. */
  2429. ub960_rxport_write(priv, it.nport, UB960_RR_SER_ID,
  2430. it.rxport->ser.addr << 1 |
  2431. UB960_RR_SER_ID_FREEZE_DEVICE_ID,
  2432. &ret);
  2433. }
  2434. /* Set serializer I2C alias with auto-ack */
  2435. ub960_rxport_write(priv, it.nport, UB960_RR_SER_ALIAS_ID,
  2436. it.rxport->ser.alias << 1 |
  2437. UB960_RR_SER_ALIAS_ID_AUTO_ACK, &ret);
  2438. if (ret)
  2439. return ret;
  2440. }
  2441. for_each_active_rxport(priv, it) {
  2442. if (fwnode_device_is_compatible(it.rxport->ser.fwnode,
  2443. "ti,ds90ub971-q1")) {
  2444. ret = ub960_rxport_bc_ser_config(it.rxport);
  2445. if (ret)
  2446. return ret;
  2447. }
  2448. }
  2449. for_each_active_rxport_fpd4(priv, it) {
  2450. /* Hold state machine in reset */
  2451. ub960_rxport_write(priv, it.nport, UB9702_RR_RX_SM_SEL_2, 0x10,
  2452. &ret);
  2453. /* Set AEQ max to 0 */
  2454. ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(it.nport),
  2455. UB9702_IR_RX_ANA_AEQ_ALP_SEL7, 0, &ret);
  2456. if (ret)
  2457. return ret;
  2458. dev_dbg(dev,
  2459. "rx%u: holding state machine and adjusting AEQ max to 0",
  2460. it.nport);
  2461. }
  2462. for_each_active_rxport(priv, it) {
  2463. port_mask |= BIT(it.nport);
  2464. if (it.rxport->cdr_mode == RXPORT_CDR_FPD4)
  2465. have_fpd4 = true;
  2466. }
  2467. for_each_rxport(priv, it) {
  2468. ret = ub960_configure_rx_port_ub9702(priv, it.nport);
  2469. if (ret)
  2470. return ret;
  2471. }
  2472. ret = ub960_reset(priv, false);
  2473. if (ret)
  2474. return ret;
  2475. if (have_fpd4) {
  2476. for_each_active_rxport_fpd4(priv, it) {
  2477. /* Release state machine */
  2478. ret = ub960_rxport_write(priv, it.nport,
  2479. UB9702_RR_RX_SM_SEL_2, 0x0,
  2480. NULL);
  2481. if (ret)
  2482. return ret;
  2483. dev_dbg(dev, "rx%u: state machine released\n",
  2484. it.nport);
  2485. }
  2486. /* Wait for SM to resume */
  2487. fsleep(5000);
  2488. for_each_active_rxport_fpd4(priv, it) {
  2489. ret = ub960_write_ind(priv,
  2490. UB960_IND_TARGET_RX_ANA(it.nport),
  2491. UB9702_IR_RX_ANA_AEQ_ALP_SEL7,
  2492. 0x23, NULL);
  2493. if (ret)
  2494. return ret;
  2495. dev_dbg(dev, "rx%u: AEQ restart\n", it.nport);
  2496. }
  2497. /* Wait for lock */
  2498. fsleep(20000);
  2499. for_each_active_rxport_fpd4(priv, it) {
  2500. ret = ub960_lock_recovery_ub9702(priv, it.nport);
  2501. if (ret)
  2502. return ret;
  2503. }
  2504. for_each_active_rxport_fpd4(priv, it) {
  2505. ret = ub960_enable_aeq_lms_ub9702(priv, it.nport);
  2506. if (ret)
  2507. return ret;
  2508. }
  2509. for_each_active_rxport_fpd4(priv, it) {
  2510. /* Hold state machine in reset */
  2511. ret = ub960_rxport_write(priv, it.nport,
  2512. UB9702_RR_RX_SM_SEL_2, 0x10,
  2513. NULL);
  2514. if (ret)
  2515. return ret;
  2516. }
  2517. ret = ub960_reset(priv, false);
  2518. if (ret)
  2519. return ret;
  2520. for_each_active_rxport_fpd4(priv, it) {
  2521. /* Release state machine */
  2522. ret = ub960_rxport_write(priv, it.nport,
  2523. UB9702_RR_RX_SM_SEL_2, 0,
  2524. NULL);
  2525. if (ret)
  2526. return ret;
  2527. }
  2528. }
  2529. /* Wait time for stable lock */
  2530. fsleep(15000);
  2531. /* Set temperature ramp on serializer */
  2532. for_each_active_rxport(priv, it) {
  2533. ret = ub960_serializer_temp_ramp(it.rxport);
  2534. if (ret)
  2535. return ret;
  2536. }
  2537. for_each_active_rxport_fpd4(priv, it) {
  2538. ret = ub960_enable_dfe_lms_ub9702(priv, it.nport);
  2539. if (ret)
  2540. return ret;
  2541. }
  2542. /* Wait for DFE and LMS to adapt */
  2543. fsleep(5000);
  2544. ret = ub960_rxport_wait_locks(priv, port_mask, &port_lock_mask);
  2545. if (ret)
  2546. return ret;
  2547. if (port_mask != port_lock_mask) {
  2548. ret = -EIO;
  2549. dev_err_probe(dev, ret, "Failed to lock all RX ports\n");
  2550. return ret;
  2551. }
  2552. for_each_active_rxport(priv, it) {
  2553. /* Enable all interrupt sources from this port */
  2554. ub960_rxport_write(priv, it.nport, UB960_RR_PORT_ICR_HI, 0x07,
  2555. &ret);
  2556. ub960_rxport_write(priv, it.nport, UB960_RR_PORT_ICR_LO, 0x7f,
  2557. &ret);
  2558. /* Clear serializer I2C alias auto-ack */
  2559. ub960_rxport_update_bits(priv, it.nport, UB960_RR_SER_ALIAS_ID,
  2560. UB960_RR_SER_ALIAS_ID_AUTO_ACK, 0,
  2561. &ret);
  2562. /* Enable I2C_PASS_THROUGH */
  2563. ub960_rxport_update_bits(priv, it.nport, UB960_RR_BCC_CONFIG,
  2564. UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH,
  2565. UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH,
  2566. &ret);
  2567. if (ret)
  2568. return ret;
  2569. }
  2570. /* Enable FPD4 Auto Recovery, Recovery loop active */
  2571. ret = ub960_write(priv, UB9702_SR_CSI_EXCLUSIVE_FWD2, 0x18, NULL);
  2572. if (ret)
  2573. return ret;
  2574. for_each_active_rxport_fpd4(priv, it) {
  2575. u8 final_aeq;
  2576. ret = ub960_read_ind(priv, UB960_IND_TARGET_RX_ANA(it.nport),
  2577. UB9702_IR_RX_ANA_AEQ_ALP_SEL11, &final_aeq,
  2578. NULL);
  2579. if (ret)
  2580. return ret;
  2581. dev_dbg(dev, "rx%u: final AEQ = %#x\n", it.nport, final_aeq);
  2582. }
  2583. /*
  2584. * Clear any errors caused by switching the RX port settings while
  2585. * probing.
  2586. */
  2587. ret = ub960_clear_rx_errors(priv);
  2588. if (ret)
  2589. return ret;
  2590. return 0;
  2591. }
  2592. static int ub960_rxport_handle_events(struct ub960_data *priv, u8 nport)
  2593. {
  2594. struct device *dev = &priv->client->dev;
  2595. u8 rx_port_sts1;
  2596. u8 rx_port_sts2;
  2597. u8 csi_rx_sts;
  2598. u8 bcc_sts;
  2599. int ret = 0;
  2600. /* Read interrupts (also clears most of them) */
  2601. ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1, &rx_port_sts1,
  2602. &ret);
  2603. ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS2, &rx_port_sts2,
  2604. &ret);
  2605. ub960_rxport_read(priv, nport, UB960_RR_CSI_RX_STS, &csi_rx_sts, &ret);
  2606. ub960_rxport_read(priv, nport, UB960_RR_BCC_STATUS, &bcc_sts, &ret);
  2607. if (ret)
  2608. return ret;
  2609. if (rx_port_sts1 & UB960_RR_RX_PORT_STS1_PARITY_ERROR) {
  2610. u16 v;
  2611. ret = ub960_rxport_read16(priv, nport, UB960_RR_RX_PAR_ERR_HI,
  2612. &v, NULL);
  2613. if (!ret)
  2614. dev_err(dev, "rx%u parity errors: %u\n", nport, v);
  2615. }
  2616. if (rx_port_sts1 & UB960_RR_RX_PORT_STS1_BCC_CRC_ERROR)
  2617. dev_err(dev, "rx%u BCC CRC error\n", nport);
  2618. if (rx_port_sts1 & UB960_RR_RX_PORT_STS1_BCC_SEQ_ERROR)
  2619. dev_err(dev, "rx%u BCC SEQ error\n", nport);
  2620. if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_LINE_LEN_UNSTABLE)
  2621. dev_err(dev, "rx%u line length unstable\n", nport);
  2622. if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_FPD3_ENCODE_ERROR)
  2623. dev_err(dev, "rx%u FPD3 encode error\n", nport);
  2624. if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_BUFFER_ERROR)
  2625. dev_err(dev, "rx%u buffer error\n", nport);
  2626. if (csi_rx_sts)
  2627. dev_err(dev, "rx%u CSI error: %#02x\n", nport, csi_rx_sts);
  2628. if (csi_rx_sts & UB960_RR_CSI_RX_STS_ECC1_ERR)
  2629. dev_err(dev, "rx%u CSI ECC1 error\n", nport);
  2630. if (csi_rx_sts & UB960_RR_CSI_RX_STS_ECC2_ERR)
  2631. dev_err(dev, "rx%u CSI ECC2 error\n", nport);
  2632. if (csi_rx_sts & UB960_RR_CSI_RX_STS_CKSUM_ERR)
  2633. dev_err(dev, "rx%u CSI checksum error\n", nport);
  2634. if (csi_rx_sts & UB960_RR_CSI_RX_STS_LENGTH_ERR)
  2635. dev_err(dev, "rx%u CSI length error\n", nport);
  2636. if (bcc_sts)
  2637. dev_err(dev, "rx%u BCC error: %#02x\n", nport, bcc_sts);
  2638. if (bcc_sts & UB960_RR_BCC_STATUS_RESP_ERR)
  2639. dev_err(dev, "rx%u BCC response error", nport);
  2640. if (bcc_sts & UB960_RR_BCC_STATUS_SLAVE_TO)
  2641. dev_err(dev, "rx%u BCC slave timeout", nport);
  2642. if (bcc_sts & UB960_RR_BCC_STATUS_SLAVE_ERR)
  2643. dev_err(dev, "rx%u BCC slave error", nport);
  2644. if (bcc_sts & UB960_RR_BCC_STATUS_MASTER_TO)
  2645. dev_err(dev, "rx%u BCC master timeout", nport);
  2646. if (bcc_sts & UB960_RR_BCC_STATUS_MASTER_ERR)
  2647. dev_err(dev, "rx%u BCC master error", nport);
  2648. if (bcc_sts & UB960_RR_BCC_STATUS_SEQ_ERROR)
  2649. dev_err(dev, "rx%u BCC sequence error", nport);
  2650. if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_LINE_LEN_CHG) {
  2651. u16 v;
  2652. ret = ub960_rxport_read16(priv, nport, UB960_RR_LINE_LEN_1,
  2653. &v, NULL);
  2654. if (!ret)
  2655. dev_dbg(dev, "rx%u line len changed: %u\n", nport, v);
  2656. }
  2657. if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_LINE_CNT_CHG) {
  2658. u16 v;
  2659. ret = ub960_rxport_read16(priv, nport, UB960_RR_LINE_COUNT_HI,
  2660. &v, NULL);
  2661. if (!ret)
  2662. dev_dbg(dev, "rx%u line count changed: %u\n", nport, v);
  2663. }
  2664. if (rx_port_sts1 & UB960_RR_RX_PORT_STS1_LOCK_STS_CHG) {
  2665. dev_dbg(dev, "rx%u: %s, %s, %s, %s\n", nport,
  2666. (rx_port_sts1 & UB960_RR_RX_PORT_STS1_LOCK_STS) ?
  2667. "locked" :
  2668. "unlocked",
  2669. (rx_port_sts1 & UB960_RR_RX_PORT_STS1_PORT_PASS) ?
  2670. "passed" :
  2671. "not passed",
  2672. (rx_port_sts2 & UB960_RR_RX_PORT_STS2_CABLE_FAULT) ?
  2673. "no clock" :
  2674. "clock ok",
  2675. (rx_port_sts2 & UB960_RR_RX_PORT_STS2_FREQ_STABLE) ?
  2676. "stable freq" :
  2677. "unstable freq");
  2678. }
  2679. return 0;
  2680. }
  2681. /* -----------------------------------------------------------------------------
  2682. * V4L2
  2683. */
  2684. /*
  2685. * The current implementation only supports a simple VC mapping, where all VCs
  2686. * from a one RX port will be mapped to the same VC. Also, the hardware
  2687. * dictates that all streams from an RX port must go to a single TX port.
  2688. *
  2689. * This function decides the target VC numbers for each RX port with a simple
  2690. * algorithm, so that for each TX port, we get VC numbers starting from 0,
  2691. * and counting up.
  2692. *
  2693. * E.g. if all four RX ports are in use, of which the first two go to the
  2694. * first TX port and the secont two go to the second TX port, we would get
  2695. * the following VCs for the four RX ports: 0, 1, 0, 1.
  2696. *
  2697. * TODO: implement a more sophisticated VC mapping. As the driver cannot know
  2698. * what VCs the sinks expect (say, an FPGA with hardcoded VC routing), this
  2699. * probably needs to be somehow configurable. Device tree?
  2700. */
  2701. static void ub960_get_vc_maps(struct ub960_data *priv,
  2702. struct v4l2_subdev_state *state, u8 *vc)
  2703. {
  2704. u8 cur_vc[UB960_MAX_TX_NPORTS] = {};
  2705. struct v4l2_subdev_route *route;
  2706. u8 handled_mask = 0;
  2707. for_each_active_route(&state->routing, route) {
  2708. unsigned int rx, tx;
  2709. rx = ub960_pad_to_port(priv, route->sink_pad);
  2710. if (BIT(rx) & handled_mask)
  2711. continue;
  2712. tx = ub960_pad_to_port(priv, route->source_pad);
  2713. vc[rx] = cur_vc[tx]++;
  2714. handled_mask |= BIT(rx);
  2715. }
  2716. }
  2717. static int ub960_enable_tx_port(struct ub960_data *priv, unsigned int nport)
  2718. {
  2719. struct device *dev = &priv->client->dev;
  2720. dev_dbg(dev, "enable TX port %u\n", nport);
  2721. return ub960_txport_update_bits(priv, nport, UB960_TR_CSI_CTL,
  2722. UB960_TR_CSI_CTL_CSI_ENABLE,
  2723. UB960_TR_CSI_CTL_CSI_ENABLE, NULL);
  2724. }
  2725. static int ub960_disable_tx_port(struct ub960_data *priv, unsigned int nport)
  2726. {
  2727. struct device *dev = &priv->client->dev;
  2728. dev_dbg(dev, "disable TX port %u\n", nport);
  2729. return ub960_txport_update_bits(priv, nport, UB960_TR_CSI_CTL,
  2730. UB960_TR_CSI_CTL_CSI_ENABLE, 0, NULL);
  2731. }
  2732. static int ub960_enable_rx_port(struct ub960_data *priv, unsigned int nport)
  2733. {
  2734. struct device *dev = &priv->client->dev;
  2735. dev_dbg(dev, "enable RX port %u\n", nport);
  2736. /* Enable forwarding */
  2737. return ub960_update_bits(priv, UB960_SR_FWD_CTL1,
  2738. UB960_SR_FWD_CTL1_PORT_DIS(nport), 0, NULL);
  2739. }
  2740. static int ub960_disable_rx_port(struct ub960_data *priv, unsigned int nport)
  2741. {
  2742. struct device *dev = &priv->client->dev;
  2743. dev_dbg(dev, "disable RX port %u\n", nport);
  2744. /* Disable forwarding */
  2745. return ub960_update_bits(priv, UB960_SR_FWD_CTL1,
  2746. UB960_SR_FWD_CTL1_PORT_DIS(nport),
  2747. UB960_SR_FWD_CTL1_PORT_DIS(nport), NULL);
  2748. }
  2749. /*
  2750. * The driver only supports using a single VC for each source. This function
  2751. * checks that each source only provides streams using a single VC.
  2752. */
  2753. static int ub960_validate_stream_vcs(struct ub960_data *priv)
  2754. {
  2755. for_each_active_rxport(priv, it) {
  2756. struct v4l2_mbus_frame_desc desc;
  2757. int ret;
  2758. u8 vc;
  2759. ret = v4l2_subdev_call(it.rxport->source.sd, pad,
  2760. get_frame_desc, it.rxport->source.pad,
  2761. &desc);
  2762. if (ret)
  2763. return ret;
  2764. if (desc.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
  2765. continue;
  2766. if (desc.num_entries == 0)
  2767. continue;
  2768. vc = desc.entry[0].bus.csi2.vc;
  2769. for (unsigned int i = 1; i < desc.num_entries; i++) {
  2770. if (vc == desc.entry[i].bus.csi2.vc)
  2771. continue;
  2772. dev_err(&priv->client->dev,
  2773. "rx%u: source with multiple virtual-channels is not supported\n",
  2774. it.nport);
  2775. return -ENODEV;
  2776. }
  2777. }
  2778. return 0;
  2779. }
  2780. static int ub960_configure_ports_for_streaming(struct ub960_data *priv,
  2781. struct v4l2_subdev_state *state)
  2782. {
  2783. u8 fwd_ctl;
  2784. struct {
  2785. u32 num_streams;
  2786. u8 pixel_dt;
  2787. u8 meta_dt;
  2788. u32 meta_lines;
  2789. u32 tx_port;
  2790. } rx_data[UB960_MAX_RX_NPORTS] = {};
  2791. u8 vc_map[UB960_MAX_RX_NPORTS] = {};
  2792. struct v4l2_subdev_route *route;
  2793. int ret;
  2794. ret = ub960_validate_stream_vcs(priv);
  2795. if (ret)
  2796. return ret;
  2797. ub960_get_vc_maps(priv, state, vc_map);
  2798. for_each_active_route(&state->routing, route) {
  2799. struct ub960_rxport *rxport;
  2800. struct ub960_txport *txport;
  2801. struct v4l2_mbus_framefmt *fmt;
  2802. const struct ub960_format_info *ub960_fmt;
  2803. unsigned int nport;
  2804. nport = ub960_pad_to_port(priv, route->sink_pad);
  2805. rxport = priv->rxports[nport];
  2806. if (!rxport)
  2807. return -EINVAL;
  2808. txport = priv->txports[ub960_pad_to_port(priv, route->source_pad)];
  2809. if (!txport)
  2810. return -EINVAL;
  2811. rx_data[nport].tx_port = ub960_pad_to_port(priv, route->source_pad);
  2812. rx_data[nport].num_streams++;
  2813. /* For the rest, we are only interested in parallel busses */
  2814. if (rxport->rx_mode == RXPORT_MODE_CSI2_SYNC ||
  2815. rxport->rx_mode == RXPORT_MODE_CSI2_NONSYNC)
  2816. continue;
  2817. if (rx_data[nport].num_streams > 2)
  2818. return -EPIPE;
  2819. fmt = v4l2_subdev_state_get_format(state, route->sink_pad,
  2820. route->sink_stream);
  2821. if (!fmt)
  2822. return -EPIPE;
  2823. ub960_fmt = ub960_find_format(fmt->code);
  2824. if (!ub960_fmt)
  2825. return -EPIPE;
  2826. if (ub960_fmt->meta) {
  2827. if (fmt->height > 3) {
  2828. dev_err(&priv->client->dev,
  2829. "rx%u: unsupported metadata height %u\n",
  2830. nport, fmt->height);
  2831. return -EPIPE;
  2832. }
  2833. rx_data[nport].meta_dt = ub960_fmt->datatype;
  2834. rx_data[nport].meta_lines = fmt->height;
  2835. } else {
  2836. rx_data[nport].pixel_dt = ub960_fmt->datatype;
  2837. }
  2838. }
  2839. /* Configure RX ports */
  2840. /*
  2841. * Keep all port forwardings disabled by default. Forwarding will be
  2842. * enabled in ub960_enable_rx_port.
  2843. */
  2844. fwd_ctl = GENMASK(7, 4);
  2845. for_each_active_rxport(priv, it) {
  2846. unsigned long nport = it.nport;
  2847. u8 vc = vc_map[nport];
  2848. if (rx_data[nport].num_streams == 0)
  2849. continue;
  2850. switch (it.rxport->rx_mode) {
  2851. case RXPORT_MODE_RAW10:
  2852. ub960_rxport_write(priv, nport, UB960_RR_RAW10_ID,
  2853. rx_data[nport].pixel_dt | (vc << UB960_RR_RAW10_ID_VC_SHIFT),
  2854. &ret);
  2855. ub960_rxport_write(priv, nport,
  2856. UB960_RR_RAW_EMBED_DTYPE,
  2857. (rx_data[nport].meta_lines << UB960_RR_RAW_EMBED_DTYPE_LINES_SHIFT) |
  2858. rx_data[nport].meta_dt, &ret);
  2859. break;
  2860. case RXPORT_MODE_RAW12_HF:
  2861. case RXPORT_MODE_RAW12_LF:
  2862. /* Not implemented */
  2863. break;
  2864. case RXPORT_MODE_CSI2_SYNC:
  2865. case RXPORT_MODE_CSI2_NONSYNC:
  2866. if (!priv->hw_data->is_ub9702) {
  2867. /* Map all VCs from this port to the same VC */
  2868. ub960_rxport_write(priv, nport, UB960_RR_CSI_VC_MAP,
  2869. (vc << UB960_RR_CSI_VC_MAP_SHIFT(3)) |
  2870. (vc << UB960_RR_CSI_VC_MAP_SHIFT(2)) |
  2871. (vc << UB960_RR_CSI_VC_MAP_SHIFT(1)) |
  2872. (vc << UB960_RR_CSI_VC_MAP_SHIFT(0)),
  2873. &ret);
  2874. } else {
  2875. unsigned int i;
  2876. /* Map all VCs from this port to VC(nport) */
  2877. for (i = 0; i < 8; i++)
  2878. ub960_rxport_write(priv, nport,
  2879. UB9702_RR_VC_ID_MAP(i),
  2880. (nport << 4) | nport,
  2881. &ret);
  2882. }
  2883. break;
  2884. }
  2885. if (rx_data[nport].tx_port == 1)
  2886. fwd_ctl |= BIT(nport); /* forward to TX1 */
  2887. else
  2888. fwd_ctl &= ~BIT(nport); /* forward to TX0 */
  2889. }
  2890. ub960_write(priv, UB960_SR_FWD_CTL1, fwd_ctl, &ret);
  2891. return ret;
  2892. }
  2893. static void ub960_update_streaming_status(struct ub960_data *priv)
  2894. {
  2895. unsigned int i;
  2896. for (i = 0; i < UB960_MAX_NPORTS; i++) {
  2897. if (priv->stream_enable_mask[i])
  2898. break;
  2899. }
  2900. priv->streaming = i < UB960_MAX_NPORTS;
  2901. }
  2902. static int ub960_enable_streams(struct v4l2_subdev *sd,
  2903. struct v4l2_subdev_state *state, u32 source_pad,
  2904. u64 source_streams_mask)
  2905. {
  2906. struct ub960_data *priv = sd_to_ub960(sd);
  2907. struct device *dev = &priv->client->dev;
  2908. u64 sink_streams[UB960_MAX_RX_NPORTS] = {};
  2909. struct v4l2_subdev_route *route;
  2910. unsigned int failed_port;
  2911. int ret;
  2912. if (!priv->streaming) {
  2913. dev_dbg(dev, "Prepare for streaming\n");
  2914. ret = ub960_configure_ports_for_streaming(priv, state);
  2915. if (ret)
  2916. return ret;
  2917. }
  2918. /* Enable TX port if not yet enabled */
  2919. if (!priv->stream_enable_mask[source_pad]) {
  2920. ret = ub960_enable_tx_port(priv,
  2921. ub960_pad_to_port(priv, source_pad));
  2922. if (ret)
  2923. return ret;
  2924. }
  2925. priv->stream_enable_mask[source_pad] |= source_streams_mask;
  2926. /* Collect sink streams per pad which we need to enable */
  2927. for_each_active_route(&state->routing, route) {
  2928. unsigned int nport;
  2929. if (route->source_pad != source_pad)
  2930. continue;
  2931. if (!(source_streams_mask & BIT_ULL(route->source_stream)))
  2932. continue;
  2933. nport = ub960_pad_to_port(priv, route->sink_pad);
  2934. sink_streams[nport] |= BIT_ULL(route->sink_stream);
  2935. }
  2936. for_each_rxport(priv, it) {
  2937. unsigned int nport = it.nport;
  2938. if (!sink_streams[nport])
  2939. continue;
  2940. /* Enable the RX port if not yet enabled */
  2941. if (!priv->stream_enable_mask[nport]) {
  2942. ret = ub960_enable_rx_port(priv, nport);
  2943. if (ret) {
  2944. failed_port = nport;
  2945. goto err;
  2946. }
  2947. }
  2948. priv->stream_enable_mask[nport] |= sink_streams[nport];
  2949. dev_dbg(dev, "enable RX port %u streams %#llx\n", nport,
  2950. sink_streams[nport]);
  2951. ret = v4l2_subdev_enable_streams(
  2952. priv->rxports[nport]->source.sd,
  2953. priv->rxports[nport]->source.pad,
  2954. sink_streams[nport]);
  2955. if (ret) {
  2956. priv->stream_enable_mask[nport] &= ~sink_streams[nport];
  2957. if (!priv->stream_enable_mask[nport])
  2958. ub960_disable_rx_port(priv, nport);
  2959. failed_port = nport;
  2960. goto err;
  2961. }
  2962. }
  2963. priv->streaming = true;
  2964. return 0;
  2965. err:
  2966. for (unsigned int nport = 0; nport < failed_port; nport++) {
  2967. if (!sink_streams[nport])
  2968. continue;
  2969. dev_dbg(dev, "disable RX port %u streams %#llx\n", nport,
  2970. sink_streams[nport]);
  2971. ret = v4l2_subdev_disable_streams(
  2972. priv->rxports[nport]->source.sd,
  2973. priv->rxports[nport]->source.pad,
  2974. sink_streams[nport]);
  2975. if (ret)
  2976. dev_err(dev, "Failed to disable streams: %d\n", ret);
  2977. priv->stream_enable_mask[nport] &= ~sink_streams[nport];
  2978. /* Disable RX port if no active streams */
  2979. if (!priv->stream_enable_mask[nport])
  2980. ub960_disable_rx_port(priv, nport);
  2981. }
  2982. priv->stream_enable_mask[source_pad] &= ~source_streams_mask;
  2983. if (!priv->stream_enable_mask[source_pad])
  2984. ub960_disable_tx_port(priv,
  2985. ub960_pad_to_port(priv, source_pad));
  2986. ub960_update_streaming_status(priv);
  2987. return ret;
  2988. }
  2989. static int ub960_disable_streams(struct v4l2_subdev *sd,
  2990. struct v4l2_subdev_state *state,
  2991. u32 source_pad, u64 source_streams_mask)
  2992. {
  2993. struct ub960_data *priv = sd_to_ub960(sd);
  2994. struct device *dev = &priv->client->dev;
  2995. u64 sink_streams[UB960_MAX_RX_NPORTS] = {};
  2996. struct v4l2_subdev_route *route;
  2997. int ret;
  2998. /* Collect sink streams per pad which we need to disable */
  2999. for_each_active_route(&state->routing, route) {
  3000. unsigned int nport;
  3001. if (route->source_pad != source_pad)
  3002. continue;
  3003. if (!(source_streams_mask & BIT_ULL(route->source_stream)))
  3004. continue;
  3005. nport = ub960_pad_to_port(priv, route->sink_pad);
  3006. sink_streams[nport] |= BIT_ULL(route->sink_stream);
  3007. }
  3008. for_each_rxport(priv, it) {
  3009. unsigned int nport = it.nport;
  3010. if (!sink_streams[nport])
  3011. continue;
  3012. dev_dbg(dev, "disable RX port %u streams %#llx\n", nport,
  3013. sink_streams[nport]);
  3014. ret = v4l2_subdev_disable_streams(
  3015. priv->rxports[nport]->source.sd,
  3016. priv->rxports[nport]->source.pad,
  3017. sink_streams[nport]);
  3018. if (ret)
  3019. dev_err(dev, "Failed to disable streams: %d\n", ret);
  3020. priv->stream_enable_mask[nport] &= ~sink_streams[nport];
  3021. /* Disable RX port if no active streams */
  3022. if (!priv->stream_enable_mask[nport])
  3023. ub960_disable_rx_port(priv, nport);
  3024. }
  3025. /* Disable TX port if no active streams */
  3026. priv->stream_enable_mask[source_pad] &= ~source_streams_mask;
  3027. if (!priv->stream_enable_mask[source_pad])
  3028. ub960_disable_tx_port(priv,
  3029. ub960_pad_to_port(priv, source_pad));
  3030. ub960_update_streaming_status(priv);
  3031. return 0;
  3032. }
  3033. static int _ub960_set_routing(struct v4l2_subdev *sd,
  3034. struct v4l2_subdev_state *state,
  3035. struct v4l2_subdev_krouting *routing)
  3036. {
  3037. static const struct v4l2_mbus_framefmt format = {
  3038. .width = 640,
  3039. .height = 480,
  3040. .code = MEDIA_BUS_FMT_UYVY8_1X16,
  3041. .field = V4L2_FIELD_NONE,
  3042. .colorspace = V4L2_COLORSPACE_SRGB,
  3043. .ycbcr_enc = V4L2_YCBCR_ENC_601,
  3044. .quantization = V4L2_QUANTIZATION_LIM_RANGE,
  3045. .xfer_func = V4L2_XFER_FUNC_SRGB,
  3046. };
  3047. int ret;
  3048. ret = v4l2_subdev_routing_validate(sd, routing,
  3049. V4L2_SUBDEV_ROUTING_ONLY_1_TO_1 |
  3050. V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX);
  3051. if (ret)
  3052. return ret;
  3053. ret = v4l2_subdev_set_routing_with_fmt(sd, state, routing, &format);
  3054. if (ret)
  3055. return ret;
  3056. return 0;
  3057. }
  3058. static int ub960_set_routing(struct v4l2_subdev *sd,
  3059. struct v4l2_subdev_state *state,
  3060. enum v4l2_subdev_format_whence which,
  3061. struct v4l2_subdev_krouting *routing)
  3062. {
  3063. struct ub960_data *priv = sd_to_ub960(sd);
  3064. if (which == V4L2_SUBDEV_FORMAT_ACTIVE && priv->streaming)
  3065. return -EBUSY;
  3066. return _ub960_set_routing(sd, state, routing);
  3067. }
  3068. static int ub960_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
  3069. struct v4l2_mbus_frame_desc *fd)
  3070. {
  3071. struct ub960_data *priv = sd_to_ub960(sd);
  3072. struct v4l2_subdev_route *route;
  3073. struct v4l2_subdev_state *state;
  3074. int ret = 0;
  3075. struct device *dev = &priv->client->dev;
  3076. u8 vc_map[UB960_MAX_RX_NPORTS] = {};
  3077. if (!ub960_pad_is_source(priv, pad))
  3078. return -EINVAL;
  3079. fd->type = V4L2_MBUS_FRAME_DESC_TYPE_CSI2;
  3080. state = v4l2_subdev_lock_and_get_active_state(&priv->sd);
  3081. ub960_get_vc_maps(priv, state, vc_map);
  3082. for_each_active_route(&state->routing, route) {
  3083. struct v4l2_mbus_frame_desc_entry *source_entry = NULL;
  3084. struct v4l2_mbus_frame_desc source_fd;
  3085. unsigned int nport;
  3086. unsigned int i;
  3087. if (route->source_pad != pad)
  3088. continue;
  3089. nport = ub960_pad_to_port(priv, route->sink_pad);
  3090. ret = v4l2_subdev_call(priv->rxports[nport]->source.sd, pad,
  3091. get_frame_desc,
  3092. priv->rxports[nport]->source.pad,
  3093. &source_fd);
  3094. if (ret) {
  3095. dev_err(dev,
  3096. "Failed to get source frame desc for pad %u\n",
  3097. route->sink_pad);
  3098. goto out_unlock;
  3099. }
  3100. for (i = 0; i < source_fd.num_entries; i++) {
  3101. if (source_fd.entry[i].stream == route->sink_stream) {
  3102. source_entry = &source_fd.entry[i];
  3103. break;
  3104. }
  3105. }
  3106. if (!source_entry) {
  3107. dev_err(dev,
  3108. "Failed to find stream from source frame desc\n");
  3109. ret = -EPIPE;
  3110. goto out_unlock;
  3111. }
  3112. fd->entry[fd->num_entries].stream = route->source_stream;
  3113. fd->entry[fd->num_entries].flags = source_entry->flags;
  3114. fd->entry[fd->num_entries].length = source_entry->length;
  3115. fd->entry[fd->num_entries].pixelcode = source_entry->pixelcode;
  3116. fd->entry[fd->num_entries].bus.csi2.vc = vc_map[nport];
  3117. if (source_fd.type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
  3118. fd->entry[fd->num_entries].bus.csi2.dt =
  3119. source_entry->bus.csi2.dt;
  3120. } else {
  3121. const struct ub960_format_info *ub960_fmt;
  3122. struct v4l2_mbus_framefmt *fmt;
  3123. fmt = v4l2_subdev_state_get_format(state, pad,
  3124. route->source_stream);
  3125. if (!fmt) {
  3126. ret = -EINVAL;
  3127. goto out_unlock;
  3128. }
  3129. ub960_fmt = ub960_find_format(fmt->code);
  3130. if (!ub960_fmt) {
  3131. dev_err(dev, "Unable to find format\n");
  3132. ret = -EINVAL;
  3133. goto out_unlock;
  3134. }
  3135. fd->entry[fd->num_entries].bus.csi2.dt =
  3136. ub960_fmt->datatype;
  3137. }
  3138. fd->num_entries++;
  3139. }
  3140. out_unlock:
  3141. v4l2_subdev_unlock_state(state);
  3142. return ret;
  3143. }
  3144. static int ub960_set_fmt(struct v4l2_subdev *sd,
  3145. struct v4l2_subdev_state *state,
  3146. struct v4l2_subdev_format *format)
  3147. {
  3148. struct ub960_data *priv = sd_to_ub960(sd);
  3149. struct v4l2_mbus_framefmt *fmt;
  3150. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE && priv->streaming)
  3151. return -EBUSY;
  3152. /* No transcoding, source and sink formats must match. */
  3153. if (ub960_pad_is_source(priv, format->pad))
  3154. return v4l2_subdev_get_fmt(sd, state, format);
  3155. /*
  3156. * Default to the first format if the requested media bus code isn't
  3157. * supported.
  3158. */
  3159. if (!ub960_find_format(format->format.code))
  3160. format->format.code = ub960_formats[0].code;
  3161. fmt = v4l2_subdev_state_get_format(state, format->pad, format->stream);
  3162. if (!fmt)
  3163. return -EINVAL;
  3164. *fmt = format->format;
  3165. fmt = v4l2_subdev_state_get_opposite_stream_format(state, format->pad,
  3166. format->stream);
  3167. if (!fmt)
  3168. return -EINVAL;
  3169. *fmt = format->format;
  3170. return 0;
  3171. }
  3172. static int ub960_init_state(struct v4l2_subdev *sd,
  3173. struct v4l2_subdev_state *state)
  3174. {
  3175. struct ub960_data *priv = sd_to_ub960(sd);
  3176. struct v4l2_subdev_route routes[] = {
  3177. {
  3178. .sink_pad = 0,
  3179. .sink_stream = 0,
  3180. .source_pad = priv->hw_data->num_rxports,
  3181. .source_stream = 0,
  3182. .flags = V4L2_SUBDEV_ROUTE_FL_ACTIVE,
  3183. },
  3184. };
  3185. struct v4l2_subdev_krouting routing = {
  3186. .num_routes = ARRAY_SIZE(routes),
  3187. .routes = routes,
  3188. };
  3189. return _ub960_set_routing(sd, state, &routing);
  3190. }
  3191. static const struct v4l2_subdev_pad_ops ub960_pad_ops = {
  3192. .enable_streams = ub960_enable_streams,
  3193. .disable_streams = ub960_disable_streams,
  3194. .set_routing = ub960_set_routing,
  3195. .get_frame_desc = ub960_get_frame_desc,
  3196. .get_fmt = v4l2_subdev_get_fmt,
  3197. .set_fmt = ub960_set_fmt,
  3198. };
  3199. static int ub960_log_status_ub960_sp_eq(struct ub960_data *priv,
  3200. unsigned int nport)
  3201. {
  3202. struct device *dev = &priv->client->dev;
  3203. u8 eq_level;
  3204. s8 strobe_pos;
  3205. int ret;
  3206. u8 v;
  3207. /* Strobe */
  3208. ret = ub960_read(priv, UB960_XR_AEQ_CTL1, &v, NULL);
  3209. if (ret)
  3210. return ret;
  3211. dev_info(dev, "\t%s strobe\n",
  3212. (v & UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN) ? "Adaptive" :
  3213. "Manual");
  3214. if (v & UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN) {
  3215. ret = ub960_read(priv, UB960_XR_SFILTER_CFG, &v, NULL);
  3216. if (ret)
  3217. return ret;
  3218. dev_info(dev, "\tStrobe range [%d, %d]\n",
  3219. ((v >> UB960_XR_SFILTER_CFG_SFILTER_MIN_SHIFT) & 0xf) - 7,
  3220. ((v >> UB960_XR_SFILTER_CFG_SFILTER_MAX_SHIFT) & 0xf) - 7);
  3221. }
  3222. ret = ub960_rxport_get_strobe_pos(priv, nport, &strobe_pos);
  3223. if (ret)
  3224. return ret;
  3225. dev_info(dev, "\tStrobe pos %d\n", strobe_pos);
  3226. /* EQ */
  3227. ret = ub960_rxport_read(priv, nport, UB960_RR_AEQ_BYPASS, &v, NULL);
  3228. if (ret)
  3229. return ret;
  3230. dev_info(dev, "\t%s EQ\n",
  3231. (v & UB960_RR_AEQ_BYPASS_ENABLE) ? "Manual" :
  3232. "Adaptive");
  3233. if (!(v & UB960_RR_AEQ_BYPASS_ENABLE)) {
  3234. ret = ub960_rxport_read(priv, nport, UB960_RR_AEQ_MIN_MAX, &v,
  3235. NULL);
  3236. if (ret)
  3237. return ret;
  3238. dev_info(dev, "\tEQ range [%u, %u]\n",
  3239. (v >> UB960_RR_AEQ_MIN_MAX_AEQ_FLOOR_SHIFT) & 0xf,
  3240. (v >> UB960_RR_AEQ_MIN_MAX_AEQ_MAX_SHIFT) & 0xf);
  3241. }
  3242. ret = ub960_rxport_get_eq_level(priv, nport, &eq_level);
  3243. if (ret)
  3244. return ret;
  3245. dev_info(dev, "\tEQ level %u\n", eq_level);
  3246. return 0;
  3247. }
  3248. static int ub960_log_status(struct v4l2_subdev *sd)
  3249. {
  3250. struct ub960_data *priv = sd_to_ub960(sd);
  3251. struct device *dev = &priv->client->dev;
  3252. struct v4l2_subdev_state *state;
  3253. u16 v16 = 0;
  3254. u8 v = 0;
  3255. u8 id[UB960_SR_FPD3_RX_ID_LEN];
  3256. int ret = 0;
  3257. state = v4l2_subdev_lock_and_get_active_state(sd);
  3258. for (unsigned int i = 0; i < sizeof(id); i++) {
  3259. ret = ub960_read(priv, UB960_SR_FPD3_RX_ID(i), &id[i], NULL);
  3260. if (ret)
  3261. return ret;
  3262. }
  3263. dev_info(dev, "ID '%.*s'\n", (int)sizeof(id), id);
  3264. for (unsigned int nport = 0; nport < priv->hw_data->num_txports;
  3265. nport++) {
  3266. struct ub960_txport *txport = priv->txports[nport];
  3267. dev_info(dev, "TX %u\n", nport);
  3268. if (!txport) {
  3269. dev_info(dev, "\tNot initialized\n");
  3270. continue;
  3271. }
  3272. ret = ub960_txport_read(priv, nport, UB960_TR_CSI_STS, &v, NULL);
  3273. if (ret)
  3274. return ret;
  3275. dev_info(dev, "\tsync %u, pass %u\n", v & (u8)BIT(1),
  3276. v & (u8)BIT(0));
  3277. ret = ub960_read16(priv, UB960_SR_CSI_FRAME_COUNT_HI(nport),
  3278. &v16, NULL);
  3279. if (ret)
  3280. return ret;
  3281. dev_info(dev, "\tframe counter %u\n", v16);
  3282. ret = ub960_read16(priv, UB960_SR_CSI_FRAME_ERR_COUNT_HI(nport),
  3283. &v16, NULL);
  3284. if (ret)
  3285. return ret;
  3286. dev_info(dev, "\tframe error counter %u\n", v16);
  3287. ret = ub960_read16(priv, UB960_SR_CSI_LINE_COUNT_HI(nport),
  3288. &v16, NULL);
  3289. if (ret)
  3290. return ret;
  3291. dev_info(dev, "\tline counter %u\n", v16);
  3292. ret = ub960_read16(priv, UB960_SR_CSI_LINE_ERR_COUNT_HI(nport),
  3293. &v16, NULL);
  3294. if (ret)
  3295. return ret;
  3296. dev_info(dev, "\tline error counter %u\n", v16);
  3297. }
  3298. for_each_rxport(priv, it) {
  3299. unsigned int nport = it.nport;
  3300. dev_info(dev, "RX %u\n", nport);
  3301. if (!it.rxport) {
  3302. dev_info(dev, "\tNot initialized\n");
  3303. continue;
  3304. }
  3305. ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1, &v,
  3306. NULL);
  3307. if (ret)
  3308. return ret;
  3309. if (v & UB960_RR_RX_PORT_STS1_LOCK_STS)
  3310. dev_info(dev, "\tLocked\n");
  3311. else
  3312. dev_info(dev, "\tNot locked\n");
  3313. dev_info(dev, "\trx_port_sts1 %#02x\n", v);
  3314. ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS2, &v,
  3315. NULL);
  3316. if (ret)
  3317. return ret;
  3318. dev_info(dev, "\trx_port_sts2 %#02x\n", v);
  3319. ret = ub960_rxport_read16(priv, nport, UB960_RR_RX_FREQ_HIGH,
  3320. &v16, NULL);
  3321. if (ret)
  3322. return ret;
  3323. dev_info(dev, "\tlink freq %llu Hz\n", ((u64)v16 * HZ_PER_MHZ) >> 8);
  3324. ret = ub960_rxport_read16(priv, nport, UB960_RR_RX_PAR_ERR_HI,
  3325. &v16, NULL);
  3326. if (ret)
  3327. return ret;
  3328. dev_info(dev, "\tparity errors %u\n", v16);
  3329. ret = ub960_rxport_read16(priv, nport, UB960_RR_LINE_COUNT_HI,
  3330. &v16, NULL);
  3331. if (ret)
  3332. return ret;
  3333. dev_info(dev, "\tlines per frame %u\n", v16);
  3334. ret = ub960_rxport_read16(priv, nport, UB960_RR_LINE_LEN_1,
  3335. &v16, NULL);
  3336. if (ret)
  3337. return ret;
  3338. dev_info(dev, "\tbytes per line %u\n", v16);
  3339. ret = ub960_rxport_read(priv, nport, UB960_RR_CSI_ERR_COUNTER,
  3340. &v, NULL);
  3341. if (ret)
  3342. return ret;
  3343. dev_info(dev, "\tcsi_err_counter %u\n", v);
  3344. if (!priv->hw_data->is_ub9702) {
  3345. ret = ub960_log_status_ub960_sp_eq(priv, nport);
  3346. if (ret)
  3347. return ret;
  3348. }
  3349. /* GPIOs */
  3350. for (unsigned int i = 0; i < UB960_NUM_BC_GPIOS; i++) {
  3351. u8 ctl_reg;
  3352. u8 ctl_shift;
  3353. ctl_reg = UB960_RR_BC_GPIO_CTL(i / 2);
  3354. ctl_shift = (i % 2) * 4;
  3355. ret = ub960_rxport_read(priv, nport, ctl_reg, &v, NULL);
  3356. if (ret)
  3357. return ret;
  3358. dev_info(dev, "\tGPIO%u: mode %u\n", i,
  3359. (v >> ctl_shift) & 0xf);
  3360. }
  3361. }
  3362. v4l2_subdev_unlock_state(state);
  3363. return 0;
  3364. }
  3365. static const struct v4l2_subdev_core_ops ub960_subdev_core_ops = {
  3366. .log_status = ub960_log_status,
  3367. };
  3368. static const struct v4l2_subdev_internal_ops ub960_internal_ops = {
  3369. .init_state = ub960_init_state,
  3370. };
  3371. static const struct v4l2_subdev_ops ub960_subdev_ops = {
  3372. .core = &ub960_subdev_core_ops,
  3373. .pad = &ub960_pad_ops,
  3374. };
  3375. static const struct media_entity_operations ub960_entity_ops = {
  3376. .get_fwnode_pad = v4l2_subdev_get_fwnode_pad_1_to_1,
  3377. .link_validate = v4l2_subdev_link_validate,
  3378. .has_pad_interdep = v4l2_subdev_has_pad_interdep,
  3379. };
  3380. /* -----------------------------------------------------------------------------
  3381. * Core
  3382. */
  3383. static irqreturn_t ub960_handle_events(int irq, void *arg)
  3384. {
  3385. struct ub960_data *priv = arg;
  3386. u8 int_sts;
  3387. u8 fwd_sts;
  3388. int ret;
  3389. ret = ub960_read(priv, UB960_SR_INTERRUPT_STS, &int_sts, NULL);
  3390. if (ret || !int_sts)
  3391. return IRQ_NONE;
  3392. dev_dbg(&priv->client->dev, "INTERRUPT_STS %x\n", int_sts);
  3393. ret = ub960_read(priv, UB960_SR_FWD_STS, &fwd_sts, NULL);
  3394. if (ret)
  3395. return IRQ_NONE;
  3396. dev_dbg(&priv->client->dev, "FWD_STS %#02x\n", fwd_sts);
  3397. for (unsigned int i = 0; i < priv->hw_data->num_txports; i++) {
  3398. if (int_sts & UB960_SR_INTERRUPT_STS_IS_CSI_TX(i)) {
  3399. ret = ub960_csi_handle_events(priv, i);
  3400. if (ret)
  3401. return IRQ_NONE;
  3402. }
  3403. }
  3404. for_each_active_rxport(priv, it) {
  3405. if (int_sts & UB960_SR_INTERRUPT_STS_IS_RX(it.nport)) {
  3406. ret = ub960_rxport_handle_events(priv, it.nport);
  3407. if (ret)
  3408. return IRQ_NONE;
  3409. }
  3410. }
  3411. return IRQ_HANDLED;
  3412. }
  3413. static void ub960_handler_work(struct work_struct *work)
  3414. {
  3415. struct delayed_work *dwork = to_delayed_work(work);
  3416. struct ub960_data *priv =
  3417. container_of(dwork, struct ub960_data, poll_work);
  3418. ub960_handle_events(0, priv);
  3419. schedule_delayed_work(&priv->poll_work,
  3420. msecs_to_jiffies(UB960_POLL_TIME_MS));
  3421. }
  3422. static void ub960_txport_free_ports(struct ub960_data *priv)
  3423. {
  3424. unsigned int nport;
  3425. for (nport = 0; nport < priv->hw_data->num_txports; nport++) {
  3426. struct ub960_txport *txport = priv->txports[nport];
  3427. if (!txport)
  3428. continue;
  3429. kfree(txport);
  3430. priv->txports[nport] = NULL;
  3431. }
  3432. }
  3433. static void ub960_rxport_free_ports(struct ub960_data *priv)
  3434. {
  3435. for_each_active_rxport(priv, it) {
  3436. fwnode_handle_put(it.rxport->source.ep_fwnode);
  3437. fwnode_handle_put(it.rxport->ser.fwnode);
  3438. mutex_destroy(&it.rxport->aliased_addrs_lock);
  3439. kfree(it.rxport);
  3440. priv->rxports[it.nport] = NULL;
  3441. }
  3442. }
  3443. static int
  3444. ub960_parse_dt_rxport_link_properties(struct ub960_data *priv,
  3445. struct fwnode_handle *link_fwnode,
  3446. struct ub960_rxport *rxport)
  3447. {
  3448. struct device *dev = &priv->client->dev;
  3449. unsigned int nport = rxport->nport;
  3450. u32 rx_mode;
  3451. u32 cdr_mode;
  3452. s32 strobe_pos;
  3453. u32 eq_level;
  3454. u32 ser_i2c_alias;
  3455. u32 ser_i2c_addr;
  3456. int ret;
  3457. cdr_mode = RXPORT_CDR_FPD3;
  3458. ret = fwnode_property_read_u32(link_fwnode, "ti,cdr-mode", &cdr_mode);
  3459. if (ret < 0 && ret != -EINVAL) {
  3460. dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
  3461. "ti,cdr-mode", ret);
  3462. return ret;
  3463. }
  3464. if (cdr_mode > RXPORT_CDR_LAST) {
  3465. dev_err(dev, "rx%u: bad 'ti,cdr-mode' %u\n", nport, cdr_mode);
  3466. return -EINVAL;
  3467. }
  3468. if (!priv->hw_data->is_fpdlink4 && cdr_mode == RXPORT_CDR_FPD4) {
  3469. dev_err(dev, "rx%u: FPD-Link 4 CDR not supported\n", nport);
  3470. return -EINVAL;
  3471. }
  3472. rxport->cdr_mode = cdr_mode;
  3473. ret = fwnode_property_read_u32(link_fwnode, "ti,rx-mode", &rx_mode);
  3474. if (ret < 0) {
  3475. dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
  3476. "ti,rx-mode", ret);
  3477. return ret;
  3478. }
  3479. if (rx_mode > RXPORT_MODE_LAST) {
  3480. dev_err(dev, "rx%u: bad 'ti,rx-mode' %u\n", nport, rx_mode);
  3481. return -EINVAL;
  3482. }
  3483. switch (rx_mode) {
  3484. case RXPORT_MODE_RAW12_HF:
  3485. case RXPORT_MODE_RAW12_LF:
  3486. dev_err(dev, "rx%u: unsupported 'ti,rx-mode' %u\n", nport,
  3487. rx_mode);
  3488. return -EINVAL;
  3489. default:
  3490. break;
  3491. }
  3492. rxport->rx_mode = rx_mode;
  3493. /* EQ & Strobe related */
  3494. /* Defaults */
  3495. rxport->eq.manual_eq = false;
  3496. rxport->eq.aeq.eq_level_min = UB960_MIN_EQ_LEVEL;
  3497. rxport->eq.aeq.eq_level_max = UB960_MAX_EQ_LEVEL;
  3498. ret = fwnode_property_read_u32(link_fwnode, "ti,strobe-pos",
  3499. &strobe_pos);
  3500. if (ret) {
  3501. if (ret != -EINVAL) {
  3502. dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
  3503. "ti,strobe-pos", ret);
  3504. return ret;
  3505. }
  3506. } else {
  3507. if (strobe_pos < UB960_MIN_MANUAL_STROBE_POS ||
  3508. strobe_pos > UB960_MAX_MANUAL_STROBE_POS) {
  3509. dev_err(dev, "rx%u: illegal 'strobe-pos' value: %d\n",
  3510. nport, strobe_pos);
  3511. return -EINVAL;
  3512. }
  3513. /* NOTE: ignored unless global manual strobe pos is also set */
  3514. rxport->eq.strobe_pos = strobe_pos;
  3515. if (!priv->strobe.manual)
  3516. dev_warn(dev,
  3517. "rx%u: 'ti,strobe-pos' ignored as 'ti,manual-strobe' not set\n",
  3518. nport);
  3519. }
  3520. ret = fwnode_property_read_u32(link_fwnode, "ti,eq-level", &eq_level);
  3521. if (ret) {
  3522. if (ret != -EINVAL) {
  3523. dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
  3524. "ti,eq-level", ret);
  3525. return ret;
  3526. }
  3527. } else {
  3528. if (eq_level > UB960_MAX_EQ_LEVEL) {
  3529. dev_err(dev, "rx%u: illegal 'ti,eq-level' value: %d\n",
  3530. nport, eq_level);
  3531. return -EINVAL;
  3532. }
  3533. rxport->eq.manual_eq = true;
  3534. rxport->eq.manual.eq_level = eq_level;
  3535. }
  3536. ret = fwnode_property_read_u32(link_fwnode, "i2c-alias",
  3537. &ser_i2c_alias);
  3538. if (ret) {
  3539. dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
  3540. "i2c-alias", ret);
  3541. return ret;
  3542. }
  3543. rxport->ser.alias = ser_i2c_alias;
  3544. rxport->ser.fwnode = fwnode_get_named_child_node(link_fwnode, "serializer");
  3545. if (!rxport->ser.fwnode) {
  3546. dev_err(dev, "rx%u: missing 'serializer' node\n", nport);
  3547. return -EINVAL;
  3548. }
  3549. ret = fwnode_property_read_u32(rxport->ser.fwnode, "reg",
  3550. &ser_i2c_addr);
  3551. if (ret)
  3552. rxport->ser.addr = -EINVAL;
  3553. else
  3554. rxport->ser.addr = ser_i2c_addr;
  3555. return 0;
  3556. }
  3557. static int ub960_parse_dt_rxport_ep_properties(struct ub960_data *priv,
  3558. struct fwnode_handle *ep_fwnode,
  3559. struct ub960_rxport *rxport)
  3560. {
  3561. struct device *dev = &priv->client->dev;
  3562. struct v4l2_fwnode_endpoint vep = {};
  3563. unsigned int nport = rxport->nport;
  3564. bool hsync_hi;
  3565. bool vsync_hi;
  3566. int ret;
  3567. rxport->source.ep_fwnode = fwnode_graph_get_remote_endpoint(ep_fwnode);
  3568. if (!rxport->source.ep_fwnode) {
  3569. dev_err(dev, "rx%u: no remote endpoint\n", nport);
  3570. return -ENODEV;
  3571. }
  3572. /* We currently have properties only for RAW modes */
  3573. switch (rxport->rx_mode) {
  3574. case RXPORT_MODE_RAW10:
  3575. case RXPORT_MODE_RAW12_HF:
  3576. case RXPORT_MODE_RAW12_LF:
  3577. break;
  3578. default:
  3579. return 0;
  3580. }
  3581. vep.bus_type = V4L2_MBUS_PARALLEL;
  3582. ret = v4l2_fwnode_endpoint_parse(ep_fwnode, &vep);
  3583. if (ret) {
  3584. dev_err(dev, "rx%u: failed to parse endpoint data\n", nport);
  3585. goto err_put_source_ep_fwnode;
  3586. }
  3587. hsync_hi = !!(vep.bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH);
  3588. vsync_hi = !!(vep.bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH);
  3589. /* LineValid and FrameValid are inverse to the h/vsync active */
  3590. rxport->lv_fv_pol = (hsync_hi ? UB960_RR_PORT_CONFIG2_LV_POL_LOW : 0) |
  3591. (vsync_hi ? UB960_RR_PORT_CONFIG2_FV_POL_LOW : 0);
  3592. return 0;
  3593. err_put_source_ep_fwnode:
  3594. fwnode_handle_put(rxport->source.ep_fwnode);
  3595. return ret;
  3596. }
  3597. static int ub960_parse_dt_rxport(struct ub960_data *priv, unsigned int nport,
  3598. struct fwnode_handle *link_fwnode,
  3599. struct fwnode_handle *ep_fwnode)
  3600. {
  3601. static const char *vpoc_names[UB960_MAX_RX_NPORTS] = {
  3602. "vpoc0", "vpoc1", "vpoc2", "vpoc3"
  3603. };
  3604. struct device *dev = &priv->client->dev;
  3605. struct ub960_rxport *rxport;
  3606. int ret;
  3607. rxport = kzalloc_obj(*rxport);
  3608. if (!rxport)
  3609. return -ENOMEM;
  3610. priv->rxports[nport] = rxport;
  3611. rxport->nport = nport;
  3612. rxport->priv = priv;
  3613. ret = ub960_parse_dt_rxport_link_properties(priv, link_fwnode, rxport);
  3614. if (ret)
  3615. goto err_free_rxport;
  3616. rxport->vpoc = devm_regulator_get_optional(dev, vpoc_names[nport]);
  3617. if (IS_ERR(rxport->vpoc)) {
  3618. ret = PTR_ERR(rxport->vpoc);
  3619. if (ret == -ENODEV) {
  3620. rxport->vpoc = NULL;
  3621. } else {
  3622. dev_err(dev, "rx%u: failed to get VPOC supply: %d\n",
  3623. nport, ret);
  3624. goto err_put_remote_fwnode;
  3625. }
  3626. }
  3627. ret = ub960_parse_dt_rxport_ep_properties(priv, ep_fwnode, rxport);
  3628. if (ret)
  3629. goto err_put_remote_fwnode;
  3630. mutex_init(&rxport->aliased_addrs_lock);
  3631. return 0;
  3632. err_put_remote_fwnode:
  3633. fwnode_handle_put(rxport->ser.fwnode);
  3634. err_free_rxport:
  3635. priv->rxports[nport] = NULL;
  3636. kfree(rxport);
  3637. return ret;
  3638. }
  3639. static struct fwnode_handle *
  3640. ub960_fwnode_get_link_by_regs(struct fwnode_handle *links_fwnode,
  3641. unsigned int nport)
  3642. {
  3643. struct fwnode_handle *link_fwnode;
  3644. int ret;
  3645. fwnode_for_each_child_node(links_fwnode, link_fwnode) {
  3646. u32 link_num;
  3647. if (!str_has_prefix(fwnode_get_name(link_fwnode), "link@"))
  3648. continue;
  3649. ret = fwnode_property_read_u32(link_fwnode, "reg", &link_num);
  3650. if (ret) {
  3651. fwnode_handle_put(link_fwnode);
  3652. return NULL;
  3653. }
  3654. if (nport == link_num)
  3655. return link_fwnode;
  3656. }
  3657. return NULL;
  3658. }
  3659. static int ub960_parse_dt_rxports(struct ub960_data *priv)
  3660. {
  3661. struct device *dev = &priv->client->dev;
  3662. struct fwnode_handle *links_fwnode;
  3663. int ret;
  3664. links_fwnode = fwnode_get_named_child_node(dev_fwnode(dev), "links");
  3665. if (!links_fwnode) {
  3666. dev_err(dev, "'links' node missing\n");
  3667. return -ENODEV;
  3668. }
  3669. /* Defaults, recommended by TI */
  3670. priv->strobe.min = 2;
  3671. priv->strobe.max = 3;
  3672. priv->strobe.manual = fwnode_property_read_bool(links_fwnode, "ti,manual-strobe");
  3673. for_each_rxport(priv, it) {
  3674. struct fwnode_handle *link_fwnode;
  3675. struct fwnode_handle *ep_fwnode;
  3676. unsigned int nport = it.nport;
  3677. link_fwnode = ub960_fwnode_get_link_by_regs(links_fwnode, nport);
  3678. if (!link_fwnode)
  3679. continue;
  3680. ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
  3681. nport, 0, 0);
  3682. if (!ep_fwnode) {
  3683. fwnode_handle_put(link_fwnode);
  3684. continue;
  3685. }
  3686. ret = ub960_parse_dt_rxport(priv, nport, link_fwnode,
  3687. ep_fwnode);
  3688. fwnode_handle_put(link_fwnode);
  3689. fwnode_handle_put(ep_fwnode);
  3690. if (ret) {
  3691. dev_err(dev, "rx%u: failed to parse RX port\n", nport);
  3692. goto err_put_links;
  3693. }
  3694. }
  3695. fwnode_handle_put(links_fwnode);
  3696. return 0;
  3697. err_put_links:
  3698. fwnode_handle_put(links_fwnode);
  3699. return ret;
  3700. }
  3701. static int ub960_parse_dt_txports(struct ub960_data *priv)
  3702. {
  3703. struct device *dev = &priv->client->dev;
  3704. u32 nport;
  3705. int ret;
  3706. for (nport = 0; nport < priv->hw_data->num_txports; nport++) {
  3707. unsigned int port = nport + priv->hw_data->num_rxports;
  3708. struct fwnode_handle *ep_fwnode;
  3709. ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
  3710. port, 0, 0);
  3711. if (!ep_fwnode)
  3712. continue;
  3713. ret = ub960_parse_dt_txport(priv, ep_fwnode, nport);
  3714. fwnode_handle_put(ep_fwnode);
  3715. if (ret)
  3716. break;
  3717. }
  3718. return 0;
  3719. }
  3720. static int ub960_parse_dt(struct ub960_data *priv)
  3721. {
  3722. int ret;
  3723. ret = ub960_parse_dt_rxports(priv);
  3724. if (ret)
  3725. return ret;
  3726. ret = ub960_parse_dt_txports(priv);
  3727. if (ret)
  3728. goto err_free_rxports;
  3729. return 0;
  3730. err_free_rxports:
  3731. ub960_rxport_free_ports(priv);
  3732. return ret;
  3733. }
  3734. static int ub960_notify_bound(struct v4l2_async_notifier *notifier,
  3735. struct v4l2_subdev *subdev,
  3736. struct v4l2_async_connection *asd)
  3737. {
  3738. struct ub960_data *priv = sd_to_ub960(notifier->sd);
  3739. struct ub960_rxport *rxport = to_ub960_asd(asd)->rxport;
  3740. struct device *dev = &priv->client->dev;
  3741. u8 nport = rxport->nport;
  3742. int ret;
  3743. ret = media_entity_get_fwnode_pad(&subdev->entity,
  3744. rxport->source.ep_fwnode,
  3745. MEDIA_PAD_FL_SOURCE);
  3746. if (ret < 0) {
  3747. dev_err(dev, "Failed to find pad for %s\n", subdev->name);
  3748. return ret;
  3749. }
  3750. rxport->source.sd = subdev;
  3751. rxport->source.pad = ret;
  3752. ret = media_create_pad_link(&rxport->source.sd->entity,
  3753. rxport->source.pad, &priv->sd.entity, nport,
  3754. MEDIA_LNK_FL_ENABLED |
  3755. MEDIA_LNK_FL_IMMUTABLE);
  3756. if (ret) {
  3757. dev_err(dev, "Unable to link %s:%u -> %s:%u\n",
  3758. rxport->source.sd->name, rxport->source.pad,
  3759. priv->sd.name, nport);
  3760. return ret;
  3761. }
  3762. for_each_active_rxport(priv, it) {
  3763. if (!it.rxport->source.sd) {
  3764. dev_dbg(dev, "Waiting for more subdevs to be bound\n");
  3765. return 0;
  3766. }
  3767. }
  3768. return 0;
  3769. }
  3770. static void ub960_notify_unbind(struct v4l2_async_notifier *notifier,
  3771. struct v4l2_subdev *subdev,
  3772. struct v4l2_async_connection *asd)
  3773. {
  3774. struct ub960_rxport *rxport = to_ub960_asd(asd)->rxport;
  3775. rxport->source.sd = NULL;
  3776. }
  3777. static const struct v4l2_async_notifier_operations ub960_notify_ops = {
  3778. .bound = ub960_notify_bound,
  3779. .unbind = ub960_notify_unbind,
  3780. };
  3781. static int ub960_v4l2_notifier_register(struct ub960_data *priv)
  3782. {
  3783. struct device *dev = &priv->client->dev;
  3784. int ret;
  3785. v4l2_async_subdev_nf_init(&priv->notifier, &priv->sd);
  3786. for_each_active_rxport(priv, it) {
  3787. struct ub960_asd *asd;
  3788. asd = v4l2_async_nf_add_fwnode(&priv->notifier,
  3789. it.rxport->source.ep_fwnode,
  3790. struct ub960_asd);
  3791. if (IS_ERR(asd)) {
  3792. dev_err(dev, "Failed to add subdev for source %u: %pe",
  3793. it.nport, asd);
  3794. v4l2_async_nf_cleanup(&priv->notifier);
  3795. return PTR_ERR(asd);
  3796. }
  3797. asd->rxport = it.rxport;
  3798. }
  3799. priv->notifier.ops = &ub960_notify_ops;
  3800. ret = v4l2_async_nf_register(&priv->notifier);
  3801. if (ret) {
  3802. dev_err(dev, "Failed to register subdev_notifier");
  3803. v4l2_async_nf_cleanup(&priv->notifier);
  3804. return ret;
  3805. }
  3806. return 0;
  3807. }
  3808. static void ub960_v4l2_notifier_unregister(struct ub960_data *priv)
  3809. {
  3810. v4l2_async_nf_unregister(&priv->notifier);
  3811. v4l2_async_nf_cleanup(&priv->notifier);
  3812. }
  3813. static int ub960_create_subdev(struct ub960_data *priv)
  3814. {
  3815. struct device *dev = &priv->client->dev;
  3816. unsigned int i;
  3817. int ret;
  3818. v4l2_i2c_subdev_init(&priv->sd, priv->client, &ub960_subdev_ops);
  3819. priv->sd.internal_ops = &ub960_internal_ops;
  3820. v4l2_ctrl_handler_init(&priv->ctrl_handler, 1);
  3821. priv->sd.ctrl_handler = &priv->ctrl_handler;
  3822. v4l2_ctrl_new_int_menu(&priv->ctrl_handler, NULL, V4L2_CID_LINK_FREQ,
  3823. ARRAY_SIZE(priv->tx_link_freq) - 1, 0,
  3824. priv->tx_link_freq);
  3825. if (priv->ctrl_handler.error) {
  3826. ret = priv->ctrl_handler.error;
  3827. goto err_free_ctrl;
  3828. }
  3829. priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
  3830. V4L2_SUBDEV_FL_STREAMS;
  3831. priv->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  3832. priv->sd.entity.ops = &ub960_entity_ops;
  3833. for (i = 0; i < priv->hw_data->num_rxports + priv->hw_data->num_txports; i++) {
  3834. priv->pads[i].flags = ub960_pad_is_sink(priv, i) ?
  3835. MEDIA_PAD_FL_SINK :
  3836. MEDIA_PAD_FL_SOURCE;
  3837. }
  3838. ret = media_entity_pads_init(&priv->sd.entity,
  3839. priv->hw_data->num_rxports +
  3840. priv->hw_data->num_txports,
  3841. priv->pads);
  3842. if (ret)
  3843. goto err_free_ctrl;
  3844. priv->sd.state_lock = priv->sd.ctrl_handler->lock;
  3845. ret = v4l2_subdev_init_finalize(&priv->sd);
  3846. if (ret)
  3847. goto err_entity_cleanup;
  3848. ret = ub960_v4l2_notifier_register(priv);
  3849. if (ret) {
  3850. dev_err(dev, "v4l2 subdev notifier register failed: %d\n", ret);
  3851. goto err_subdev_cleanup;
  3852. }
  3853. ret = v4l2_async_register_subdev(&priv->sd);
  3854. if (ret) {
  3855. dev_err(dev, "v4l2_async_register_subdev error: %d\n", ret);
  3856. goto err_unreg_notif;
  3857. }
  3858. return 0;
  3859. err_unreg_notif:
  3860. ub960_v4l2_notifier_unregister(priv);
  3861. err_subdev_cleanup:
  3862. v4l2_subdev_cleanup(&priv->sd);
  3863. err_entity_cleanup:
  3864. media_entity_cleanup(&priv->sd.entity);
  3865. err_free_ctrl:
  3866. v4l2_ctrl_handler_free(&priv->ctrl_handler);
  3867. return ret;
  3868. }
  3869. static void ub960_destroy_subdev(struct ub960_data *priv)
  3870. {
  3871. ub960_v4l2_notifier_unregister(priv);
  3872. v4l2_async_unregister_subdev(&priv->sd);
  3873. v4l2_subdev_cleanup(&priv->sd);
  3874. media_entity_cleanup(&priv->sd.entity);
  3875. v4l2_ctrl_handler_free(&priv->ctrl_handler);
  3876. }
  3877. static const struct regmap_config ub960_regmap_config = {
  3878. .name = "ds90ub960",
  3879. .reg_bits = 8,
  3880. .val_bits = 8,
  3881. .max_register = 0xff,
  3882. /*
  3883. * We do locking in the driver to cover the TX/RX port selection and the
  3884. * indirect register access.
  3885. */
  3886. .disable_locking = true,
  3887. };
  3888. static int ub960_get_hw_resources(struct ub960_data *priv)
  3889. {
  3890. struct device *dev = &priv->client->dev;
  3891. priv->regmap = devm_regmap_init_i2c(priv->client, &ub960_regmap_config);
  3892. if (IS_ERR(priv->regmap))
  3893. return PTR_ERR(priv->regmap);
  3894. priv->vddio = devm_regulator_get(dev, "vddio");
  3895. if (IS_ERR(priv->vddio))
  3896. return dev_err_probe(dev, PTR_ERR(priv->vddio),
  3897. "cannot get VDDIO regulator\n");
  3898. /* get power-down pin from DT */
  3899. priv->pd_gpio =
  3900. devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
  3901. if (IS_ERR(priv->pd_gpio))
  3902. return dev_err_probe(dev, PTR_ERR(priv->pd_gpio),
  3903. "Cannot get powerdown GPIO\n");
  3904. priv->refclk = devm_clk_get(dev, "refclk");
  3905. if (IS_ERR(priv->refclk))
  3906. return dev_err_probe(dev, PTR_ERR(priv->refclk),
  3907. "Cannot get REFCLK\n");
  3908. return 0;
  3909. }
  3910. static int ub960_enable_core_hw(struct ub960_data *priv)
  3911. {
  3912. struct device *dev = &priv->client->dev;
  3913. u8 rev_mask;
  3914. int ret;
  3915. u8 dev_sts;
  3916. u8 refclk_freq;
  3917. ret = regulator_enable(priv->vddio);
  3918. if (ret)
  3919. return dev_err_probe(dev, ret,
  3920. "failed to enable VDDIO regulator\n");
  3921. ret = clk_prepare_enable(priv->refclk);
  3922. if (ret) {
  3923. dev_err_probe(dev, ret, "Failed to enable refclk\n");
  3924. goto err_disable_vddio;
  3925. }
  3926. if (priv->pd_gpio) {
  3927. gpiod_set_value_cansleep(priv->pd_gpio, 1);
  3928. /* wait min 2 ms for reset to complete */
  3929. fsleep(2000);
  3930. gpiod_set_value_cansleep(priv->pd_gpio, 0);
  3931. /* wait min 2 ms for power up to finish */
  3932. fsleep(2000);
  3933. }
  3934. ret = ub960_reset(priv, true);
  3935. if (ret)
  3936. goto err_pd_gpio;
  3937. /* Runtime check register accessibility */
  3938. ret = ub960_read(priv, UB960_SR_REV_MASK, &rev_mask, NULL);
  3939. if (ret) {
  3940. dev_err_probe(dev, ret, "Cannot read first register, abort\n");
  3941. goto err_pd_gpio;
  3942. }
  3943. dev_dbg(dev, "Found %s (rev/mask %#04x)\n", priv->hw_data->model,
  3944. rev_mask);
  3945. ret = ub960_read(priv, UB960_SR_DEVICE_STS, &dev_sts, NULL);
  3946. if (ret)
  3947. goto err_pd_gpio;
  3948. if (priv->hw_data->is_ub9702)
  3949. ret = ub960_read(priv, UB9702_SR_REFCLK_FREQ, &refclk_freq,
  3950. NULL);
  3951. else
  3952. ret = ub960_read(priv, UB960_XR_REFCLK_FREQ, &refclk_freq,
  3953. NULL);
  3954. if (ret)
  3955. goto err_pd_gpio;
  3956. dev_dbg(dev, "refclk valid %u freq %u MHz (clk fw freq %lu MHz)\n",
  3957. !!(dev_sts & BIT(4)), refclk_freq,
  3958. clk_get_rate(priv->refclk) / HZ_PER_MHZ);
  3959. /* Disable all RX ports by default */
  3960. ret = ub960_write(priv, UB960_SR_RX_PORT_CTL, 0, NULL);
  3961. if (ret)
  3962. goto err_pd_gpio;
  3963. /* release GPIO lock */
  3964. if (priv->hw_data->is_ub9702) {
  3965. ret = ub960_update_bits(priv, UB960_SR_RESET,
  3966. UB960_SR_RESET_GPIO_LOCK_RELEASE,
  3967. UB960_SR_RESET_GPIO_LOCK_RELEASE,
  3968. NULL);
  3969. if (ret)
  3970. goto err_pd_gpio;
  3971. }
  3972. return 0;
  3973. err_pd_gpio:
  3974. gpiod_set_value_cansleep(priv->pd_gpio, 1);
  3975. clk_disable_unprepare(priv->refclk);
  3976. err_disable_vddio:
  3977. regulator_disable(priv->vddio);
  3978. return ret;
  3979. }
  3980. static void ub960_disable_core_hw(struct ub960_data *priv)
  3981. {
  3982. gpiod_set_value_cansleep(priv->pd_gpio, 1);
  3983. clk_disable_unprepare(priv->refclk);
  3984. regulator_disable(priv->vddio);
  3985. }
  3986. static int ub960_probe(struct i2c_client *client)
  3987. {
  3988. struct device *dev = &client->dev;
  3989. struct ub960_data *priv;
  3990. int ret;
  3991. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  3992. if (!priv)
  3993. return -ENOMEM;
  3994. priv->client = client;
  3995. priv->hw_data = device_get_match_data(dev);
  3996. mutex_init(&priv->reg_lock);
  3997. INIT_DELAYED_WORK(&priv->poll_work, ub960_handler_work);
  3998. /*
  3999. * Initialize these to invalid values so that the first reg writes will
  4000. * configure the target.
  4001. */
  4002. priv->reg_current.indirect_target = 0xff;
  4003. priv->reg_current.rxport = 0xff;
  4004. priv->reg_current.txport = 0xff;
  4005. ret = ub960_get_hw_resources(priv);
  4006. if (ret)
  4007. goto err_mutex_destroy;
  4008. ret = ub960_enable_core_hw(priv);
  4009. if (ret)
  4010. goto err_mutex_destroy;
  4011. ret = ub960_parse_dt(priv);
  4012. if (ret)
  4013. goto err_disable_core_hw;
  4014. ret = ub960_init_tx_ports(priv);
  4015. if (ret)
  4016. goto err_free_ports;
  4017. ret = ub960_rxport_enable_vpocs(priv);
  4018. if (ret)
  4019. goto err_free_ports;
  4020. if (priv->hw_data->is_ub9702)
  4021. ret = ub960_init_rx_ports_ub9702(priv);
  4022. else
  4023. ret = ub960_init_rx_ports_ub960(priv);
  4024. if (ret)
  4025. goto err_disable_vpocs;
  4026. ret = ub960_init_atr(priv);
  4027. if (ret)
  4028. goto err_disable_vpocs;
  4029. ret = ub960_rxport_add_serializers(priv);
  4030. if (ret)
  4031. goto err_uninit_atr;
  4032. ret = ub960_create_subdev(priv);
  4033. if (ret)
  4034. goto err_free_sers;
  4035. if (client->irq)
  4036. dev_warn(dev, "irq support not implemented, using polling\n");
  4037. schedule_delayed_work(&priv->poll_work,
  4038. msecs_to_jiffies(UB960_POLL_TIME_MS));
  4039. #ifdef UB960_DEBUG_I2C_RX_ID
  4040. for_each_rxport(priv, it)
  4041. ub960_write(priv, UB960_SR_I2C_RX_ID(it.nport),
  4042. (UB960_DEBUG_I2C_RX_ID + it.nport) << 1, NULL);
  4043. #endif
  4044. return 0;
  4045. err_free_sers:
  4046. ub960_rxport_remove_serializers(priv);
  4047. err_uninit_atr:
  4048. ub960_uninit_atr(priv);
  4049. err_disable_vpocs:
  4050. ub960_rxport_disable_vpocs(priv);
  4051. err_free_ports:
  4052. ub960_rxport_free_ports(priv);
  4053. ub960_txport_free_ports(priv);
  4054. err_disable_core_hw:
  4055. ub960_disable_core_hw(priv);
  4056. err_mutex_destroy:
  4057. mutex_destroy(&priv->reg_lock);
  4058. return ret;
  4059. }
  4060. static void ub960_remove(struct i2c_client *client)
  4061. {
  4062. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  4063. struct ub960_data *priv = sd_to_ub960(sd);
  4064. cancel_delayed_work_sync(&priv->poll_work);
  4065. ub960_destroy_subdev(priv);
  4066. ub960_rxport_remove_serializers(priv);
  4067. ub960_uninit_atr(priv);
  4068. ub960_rxport_disable_vpocs(priv);
  4069. ub960_rxport_free_ports(priv);
  4070. ub960_txport_free_ports(priv);
  4071. ub960_disable_core_hw(priv);
  4072. mutex_destroy(&priv->reg_lock);
  4073. }
  4074. static const struct ub960_hw_data ds90ub960_hw = {
  4075. .model = "ub960",
  4076. .num_rxports = 4,
  4077. .num_txports = 2,
  4078. };
  4079. static const struct ub960_hw_data ds90ub9702_hw = {
  4080. .model = "ub9702",
  4081. .num_rxports = 4,
  4082. .num_txports = 2,
  4083. .is_ub9702 = true,
  4084. .is_fpdlink4 = true,
  4085. };
  4086. static const struct i2c_device_id ub960_id[] = {
  4087. { "ds90ub960-q1", (kernel_ulong_t)&ds90ub960_hw },
  4088. { "ds90ub9702-q1", (kernel_ulong_t)&ds90ub9702_hw },
  4089. {}
  4090. };
  4091. MODULE_DEVICE_TABLE(i2c, ub960_id);
  4092. static const struct of_device_id ub960_dt_ids[] = {
  4093. { .compatible = "ti,ds90ub960-q1", .data = &ds90ub960_hw },
  4094. { .compatible = "ti,ds90ub9702-q1", .data = &ds90ub9702_hw },
  4095. {}
  4096. };
  4097. MODULE_DEVICE_TABLE(of, ub960_dt_ids);
  4098. static struct i2c_driver ds90ub960_driver = {
  4099. .probe = ub960_probe,
  4100. .remove = ub960_remove,
  4101. .id_table = ub960_id,
  4102. .driver = {
  4103. .name = "ds90ub960",
  4104. .of_match_table = ub960_dt_ids,
  4105. },
  4106. };
  4107. module_i2c_driver(ds90ub960_driver);
  4108. MODULE_LICENSE("GPL");
  4109. MODULE_DESCRIPTION("Texas Instruments FPD-Link III/IV Deserializers Driver");
  4110. MODULE_AUTHOR("Luca Ceresoli <luca@lucaceresoli.net>");
  4111. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>");
  4112. MODULE_IMPORT_NS("I2C_ATR");