adv7604.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275
  1. /*
  2. * adv7604 - Analog Devices ADV7604 video decoder driver
  3. *
  4. * Copyright 2012 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  5. *
  6. * This program is free software; you may redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. *
  19. */
  20. /*
  21. * References (c = chapter, p = page):
  22. * REF_01 - Analog devices, ADV7604, Register Settings Recommendations,
  23. * Revision 2.5, June 2010
  24. * REF_02 - Analog devices, Register map documentation, Documentation of
  25. * the register maps, Software manual, Rev. F, June 2010
  26. * REF_03 - Analog devices, ADV7604, Hardware Manual, Rev. F, August 2010
  27. */
  28. #include <linux/delay.h>
  29. #include <linux/gpio/consumer.h>
  30. #include <linux/hdmi.h>
  31. #include <linux/i2c.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/slab.h>
  35. #include <linux/v4l2-dv-timings.h>
  36. #include <linux/videodev2.h>
  37. #include <linux/workqueue.h>
  38. #include <linux/regmap.h>
  39. #include <media/adv7604.h>
  40. #include <media/v4l2-ctrls.h>
  41. #include <media/v4l2-device.h>
  42. #include <media/v4l2-event.h>
  43. #include <media/v4l2-dv-timings.h>
  44. #include <media/v4l2-of.h>
  45. static int debug;
  46. module_param(debug, int, 0644);
  47. MODULE_PARM_DESC(debug, "debug level (0-2)");
  48. MODULE_DESCRIPTION("Analog Devices ADV7604 video decoder driver");
  49. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  50. MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
  51. MODULE_LICENSE("GPL");
  52. /* ADV7604 system clock frequency */
  53. #define ADV76XX_FSC (28636360)
  54. #define ADV76XX_RGB_OUT (1 << 1)
  55. #define ADV76XX_OP_FORMAT_SEL_8BIT (0 << 0)
  56. #define ADV7604_OP_FORMAT_SEL_10BIT (1 << 0)
  57. #define ADV76XX_OP_FORMAT_SEL_12BIT (2 << 0)
  58. #define ADV76XX_OP_MODE_SEL_SDR_422 (0 << 5)
  59. #define ADV7604_OP_MODE_SEL_DDR_422 (1 << 5)
  60. #define ADV76XX_OP_MODE_SEL_SDR_444 (2 << 5)
  61. #define ADV7604_OP_MODE_SEL_DDR_444 (3 << 5)
  62. #define ADV76XX_OP_MODE_SEL_SDR_422_2X (4 << 5)
  63. #define ADV7604_OP_MODE_SEL_ADI_CM (5 << 5)
  64. #define ADV76XX_OP_CH_SEL_GBR (0 << 5)
  65. #define ADV76XX_OP_CH_SEL_GRB (1 << 5)
  66. #define ADV76XX_OP_CH_SEL_BGR (2 << 5)
  67. #define ADV76XX_OP_CH_SEL_RGB (3 << 5)
  68. #define ADV76XX_OP_CH_SEL_BRG (4 << 5)
  69. #define ADV76XX_OP_CH_SEL_RBG (5 << 5)
  70. #define ADV76XX_OP_SWAP_CB_CR (1 << 0)
  71. enum adv76xx_type {
  72. ADV7604,
  73. ADV7611,
  74. ADV7612,
  75. };
  76. struct adv76xx_reg_seq {
  77. unsigned int reg;
  78. u8 val;
  79. };
  80. struct adv76xx_format_info {
  81. u32 code;
  82. u8 op_ch_sel;
  83. bool rgb_out;
  84. bool swap_cb_cr;
  85. u8 op_format_sel;
  86. };
  87. struct adv76xx_cfg_read_infoframe {
  88. const char *desc;
  89. u8 present_mask;
  90. u8 head_addr;
  91. u8 payload_addr;
  92. };
  93. struct adv76xx_chip_info {
  94. enum adv76xx_type type;
  95. bool has_afe;
  96. unsigned int max_port;
  97. unsigned int num_dv_ports;
  98. unsigned int edid_enable_reg;
  99. unsigned int edid_status_reg;
  100. unsigned int lcf_reg;
  101. unsigned int cable_det_mask;
  102. unsigned int tdms_lock_mask;
  103. unsigned int fmt_change_digital_mask;
  104. unsigned int cp_csc;
  105. const struct adv76xx_format_info *formats;
  106. unsigned int nformats;
  107. void (*set_termination)(struct v4l2_subdev *sd, bool enable);
  108. void (*setup_irqs)(struct v4l2_subdev *sd);
  109. unsigned int (*read_hdmi_pixelclock)(struct v4l2_subdev *sd);
  110. unsigned int (*read_cable_det)(struct v4l2_subdev *sd);
  111. /* 0 = AFE, 1 = HDMI */
  112. const struct adv76xx_reg_seq *recommended_settings[2];
  113. unsigned int num_recommended_settings[2];
  114. unsigned long page_mask;
  115. /* Masks for timings */
  116. unsigned int linewidth_mask;
  117. unsigned int field0_height_mask;
  118. unsigned int field1_height_mask;
  119. unsigned int hfrontporch_mask;
  120. unsigned int hsync_mask;
  121. unsigned int hbackporch_mask;
  122. unsigned int field0_vfrontporch_mask;
  123. unsigned int field1_vfrontporch_mask;
  124. unsigned int field0_vsync_mask;
  125. unsigned int field1_vsync_mask;
  126. unsigned int field0_vbackporch_mask;
  127. unsigned int field1_vbackporch_mask;
  128. };
  129. /*
  130. **********************************************************************
  131. *
  132. * Arrays with configuration parameters for the ADV7604
  133. *
  134. **********************************************************************
  135. */
  136. struct adv76xx_state {
  137. const struct adv76xx_chip_info *info;
  138. struct adv76xx_platform_data pdata;
  139. struct gpio_desc *hpd_gpio[4];
  140. struct v4l2_subdev sd;
  141. struct media_pad pads[ADV76XX_PAD_MAX];
  142. unsigned int source_pad;
  143. struct v4l2_ctrl_handler hdl;
  144. enum adv76xx_pad selected_input;
  145. struct v4l2_dv_timings timings;
  146. const struct adv76xx_format_info *format;
  147. struct {
  148. u8 edid[256];
  149. u32 present;
  150. unsigned blocks;
  151. } edid;
  152. u16 spa_port_a[2];
  153. struct v4l2_fract aspect_ratio;
  154. u32 rgb_quantization_range;
  155. struct workqueue_struct *work_queues;
  156. struct delayed_work delayed_work_enable_hotplug;
  157. bool restart_stdi_once;
  158. /* i2c clients */
  159. struct i2c_client *i2c_clients[ADV76XX_PAGE_MAX];
  160. /* Regmaps */
  161. struct regmap *regmap[ADV76XX_PAGE_MAX];
  162. /* controls */
  163. struct v4l2_ctrl *detect_tx_5v_ctrl;
  164. struct v4l2_ctrl *analog_sampling_phase_ctrl;
  165. struct v4l2_ctrl *free_run_color_manual_ctrl;
  166. struct v4l2_ctrl *free_run_color_ctrl;
  167. struct v4l2_ctrl *rgb_quantization_range_ctrl;
  168. };
  169. static bool adv76xx_has_afe(struct adv76xx_state *state)
  170. {
  171. return state->info->has_afe;
  172. }
  173. /* Supported CEA and DMT timings */
  174. static const struct v4l2_dv_timings adv76xx_timings[] = {
  175. V4L2_DV_BT_CEA_720X480P59_94,
  176. V4L2_DV_BT_CEA_720X576P50,
  177. V4L2_DV_BT_CEA_1280X720P24,
  178. V4L2_DV_BT_CEA_1280X720P25,
  179. V4L2_DV_BT_CEA_1280X720P50,
  180. V4L2_DV_BT_CEA_1280X720P60,
  181. V4L2_DV_BT_CEA_1920X1080P24,
  182. V4L2_DV_BT_CEA_1920X1080P25,
  183. V4L2_DV_BT_CEA_1920X1080P30,
  184. V4L2_DV_BT_CEA_1920X1080P50,
  185. V4L2_DV_BT_CEA_1920X1080P60,
  186. /* sorted by DMT ID */
  187. V4L2_DV_BT_DMT_640X350P85,
  188. V4L2_DV_BT_DMT_640X400P85,
  189. V4L2_DV_BT_DMT_720X400P85,
  190. V4L2_DV_BT_DMT_640X480P60,
  191. V4L2_DV_BT_DMT_640X480P72,
  192. V4L2_DV_BT_DMT_640X480P75,
  193. V4L2_DV_BT_DMT_640X480P85,
  194. V4L2_DV_BT_DMT_800X600P56,
  195. V4L2_DV_BT_DMT_800X600P60,
  196. V4L2_DV_BT_DMT_800X600P72,
  197. V4L2_DV_BT_DMT_800X600P75,
  198. V4L2_DV_BT_DMT_800X600P85,
  199. V4L2_DV_BT_DMT_848X480P60,
  200. V4L2_DV_BT_DMT_1024X768P60,
  201. V4L2_DV_BT_DMT_1024X768P70,
  202. V4L2_DV_BT_DMT_1024X768P75,
  203. V4L2_DV_BT_DMT_1024X768P85,
  204. V4L2_DV_BT_DMT_1152X864P75,
  205. V4L2_DV_BT_DMT_1280X768P60_RB,
  206. V4L2_DV_BT_DMT_1280X768P60,
  207. V4L2_DV_BT_DMT_1280X768P75,
  208. V4L2_DV_BT_DMT_1280X768P85,
  209. V4L2_DV_BT_DMT_1280X800P60_RB,
  210. V4L2_DV_BT_DMT_1280X800P60,
  211. V4L2_DV_BT_DMT_1280X800P75,
  212. V4L2_DV_BT_DMT_1280X800P85,
  213. V4L2_DV_BT_DMT_1280X960P60,
  214. V4L2_DV_BT_DMT_1280X960P85,
  215. V4L2_DV_BT_DMT_1280X1024P60,
  216. V4L2_DV_BT_DMT_1280X1024P75,
  217. V4L2_DV_BT_DMT_1280X1024P85,
  218. V4L2_DV_BT_DMT_1360X768P60,
  219. V4L2_DV_BT_DMT_1400X1050P60_RB,
  220. V4L2_DV_BT_DMT_1400X1050P60,
  221. V4L2_DV_BT_DMT_1400X1050P75,
  222. V4L2_DV_BT_DMT_1400X1050P85,
  223. V4L2_DV_BT_DMT_1440X900P60_RB,
  224. V4L2_DV_BT_DMT_1440X900P60,
  225. V4L2_DV_BT_DMT_1600X1200P60,
  226. V4L2_DV_BT_DMT_1680X1050P60_RB,
  227. V4L2_DV_BT_DMT_1680X1050P60,
  228. V4L2_DV_BT_DMT_1792X1344P60,
  229. V4L2_DV_BT_DMT_1856X1392P60,
  230. V4L2_DV_BT_DMT_1920X1200P60_RB,
  231. V4L2_DV_BT_DMT_1366X768P60_RB,
  232. V4L2_DV_BT_DMT_1366X768P60,
  233. V4L2_DV_BT_DMT_1920X1080P60,
  234. { },
  235. };
  236. struct adv76xx_video_standards {
  237. struct v4l2_dv_timings timings;
  238. u8 vid_std;
  239. u8 v_freq;
  240. };
  241. /* sorted by number of lines */
  242. static const struct adv76xx_video_standards adv7604_prim_mode_comp[] = {
  243. /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
  244. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  245. { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
  246. { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
  247. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  248. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  249. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  250. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  251. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  252. /* TODO add 1920x1080P60_RB (CVT timing) */
  253. { },
  254. };
  255. /* sorted by number of lines */
  256. static const struct adv76xx_video_standards adv7604_prim_mode_gr[] = {
  257. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  258. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  259. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  260. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  261. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  262. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  263. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  264. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  265. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  266. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  267. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  268. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  269. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  270. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  271. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  272. { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
  273. { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
  274. { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
  275. { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
  276. { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
  277. /* TODO add 1600X1200P60_RB (not a DMT timing) */
  278. { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
  279. { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
  280. { },
  281. };
  282. /* sorted by number of lines */
  283. static const struct adv76xx_video_standards adv76xx_prim_mode_hdmi_comp[] = {
  284. { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
  285. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  286. { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
  287. { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
  288. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  289. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  290. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  291. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  292. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  293. { },
  294. };
  295. /* sorted by number of lines */
  296. static const struct adv76xx_video_standards adv76xx_prim_mode_hdmi_gr[] = {
  297. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  298. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  299. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  300. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  301. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  302. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  303. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  304. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  305. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  306. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  307. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  308. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  309. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  310. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  311. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  312. { },
  313. };
  314. static const struct v4l2_event adv76xx_ev_fmt = {
  315. .type = V4L2_EVENT_SOURCE_CHANGE,
  316. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  317. };
  318. /* ----------------------------------------------------------------------- */
  319. static inline struct adv76xx_state *to_state(struct v4l2_subdev *sd)
  320. {
  321. return container_of(sd, struct adv76xx_state, sd);
  322. }
  323. static inline unsigned htotal(const struct v4l2_bt_timings *t)
  324. {
  325. return V4L2_DV_BT_FRAME_WIDTH(t);
  326. }
  327. static inline unsigned vtotal(const struct v4l2_bt_timings *t)
  328. {
  329. return V4L2_DV_BT_FRAME_HEIGHT(t);
  330. }
  331. /* ----------------------------------------------------------------------- */
  332. static int adv76xx_read_check(struct adv76xx_state *state,
  333. int client_page, u8 reg)
  334. {
  335. struct i2c_client *client = state->i2c_clients[client_page];
  336. int err;
  337. unsigned int val;
  338. err = regmap_read(state->regmap[client_page], reg, &val);
  339. if (err) {
  340. v4l_err(client, "error reading %02x, %02x\n",
  341. client->addr, reg);
  342. return err;
  343. }
  344. return val;
  345. }
  346. /* adv76xx_write_block(): Write raw data with a maximum of I2C_SMBUS_BLOCK_MAX
  347. * size to one or more registers.
  348. *
  349. * A value of zero will be returned on success, a negative errno will
  350. * be returned in error cases.
  351. */
  352. static int adv76xx_write_block(struct adv76xx_state *state, int client_page,
  353. unsigned int init_reg, const void *val,
  354. size_t val_len)
  355. {
  356. struct regmap *regmap = state->regmap[client_page];
  357. if (val_len > I2C_SMBUS_BLOCK_MAX)
  358. val_len = I2C_SMBUS_BLOCK_MAX;
  359. return regmap_raw_write(regmap, init_reg, val, val_len);
  360. }
  361. /* ----------------------------------------------------------------------- */
  362. static inline int io_read(struct v4l2_subdev *sd, u8 reg)
  363. {
  364. struct adv76xx_state *state = to_state(sd);
  365. return adv76xx_read_check(state, ADV76XX_PAGE_IO, reg);
  366. }
  367. static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  368. {
  369. struct adv76xx_state *state = to_state(sd);
  370. return regmap_write(state->regmap[ADV76XX_PAGE_IO], reg, val);
  371. }
  372. static inline int io_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  373. {
  374. return io_write(sd, reg, (io_read(sd, reg) & ~mask) | val);
  375. }
  376. static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
  377. {
  378. struct adv76xx_state *state = to_state(sd);
  379. return adv76xx_read_check(state, ADV7604_PAGE_AVLINK, reg);
  380. }
  381. static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  382. {
  383. struct adv76xx_state *state = to_state(sd);
  384. return regmap_write(state->regmap[ADV7604_PAGE_AVLINK], reg, val);
  385. }
  386. static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
  387. {
  388. struct adv76xx_state *state = to_state(sd);
  389. return adv76xx_read_check(state, ADV76XX_PAGE_CEC, reg);
  390. }
  391. static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  392. {
  393. struct adv76xx_state *state = to_state(sd);
  394. return regmap_write(state->regmap[ADV76XX_PAGE_CEC], reg, val);
  395. }
  396. static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
  397. {
  398. struct adv76xx_state *state = to_state(sd);
  399. return adv76xx_read_check(state, ADV76XX_PAGE_INFOFRAME, reg);
  400. }
  401. static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  402. {
  403. struct adv76xx_state *state = to_state(sd);
  404. return regmap_write(state->regmap[ADV76XX_PAGE_INFOFRAME], reg, val);
  405. }
  406. static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
  407. {
  408. struct adv76xx_state *state = to_state(sd);
  409. return adv76xx_read_check(state, ADV76XX_PAGE_AFE, reg);
  410. }
  411. static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  412. {
  413. struct adv76xx_state *state = to_state(sd);
  414. return regmap_write(state->regmap[ADV76XX_PAGE_AFE], reg, val);
  415. }
  416. static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
  417. {
  418. struct adv76xx_state *state = to_state(sd);
  419. return adv76xx_read_check(state, ADV76XX_PAGE_REP, reg);
  420. }
  421. static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  422. {
  423. struct adv76xx_state *state = to_state(sd);
  424. return regmap_write(state->regmap[ADV76XX_PAGE_REP], reg, val);
  425. }
  426. static inline int rep_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  427. {
  428. return rep_write(sd, reg, (rep_read(sd, reg) & ~mask) | val);
  429. }
  430. static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
  431. {
  432. struct adv76xx_state *state = to_state(sd);
  433. return adv76xx_read_check(state, ADV76XX_PAGE_EDID, reg);
  434. }
  435. static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  436. {
  437. struct adv76xx_state *state = to_state(sd);
  438. return regmap_write(state->regmap[ADV76XX_PAGE_EDID], reg, val);
  439. }
  440. static inline int edid_write_block(struct v4l2_subdev *sd,
  441. unsigned int total_len, const u8 *val)
  442. {
  443. struct adv76xx_state *state = to_state(sd);
  444. int err = 0;
  445. int i = 0;
  446. int len = 0;
  447. v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n",
  448. __func__, total_len);
  449. while (!err && i < total_len) {
  450. len = (total_len - i) > I2C_SMBUS_BLOCK_MAX ?
  451. I2C_SMBUS_BLOCK_MAX :
  452. (total_len - i);
  453. err = adv76xx_write_block(state, ADV76XX_PAGE_EDID,
  454. i, val + i, len);
  455. i += len;
  456. }
  457. return err;
  458. }
  459. static void adv76xx_set_hpd(struct adv76xx_state *state, unsigned int hpd)
  460. {
  461. unsigned int i;
  462. for (i = 0; i < state->info->num_dv_ports; ++i)
  463. gpiod_set_value_cansleep(state->hpd_gpio[i], hpd & BIT(i));
  464. v4l2_subdev_notify(&state->sd, ADV76XX_HOTPLUG, &hpd);
  465. }
  466. static void adv76xx_delayed_work_enable_hotplug(struct work_struct *work)
  467. {
  468. struct delayed_work *dwork = to_delayed_work(work);
  469. struct adv76xx_state *state = container_of(dwork, struct adv76xx_state,
  470. delayed_work_enable_hotplug);
  471. struct v4l2_subdev *sd = &state->sd;
  472. v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
  473. adv76xx_set_hpd(state, state->edid.present);
  474. }
  475. static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
  476. {
  477. struct adv76xx_state *state = to_state(sd);
  478. return adv76xx_read_check(state, ADV76XX_PAGE_HDMI, reg);
  479. }
  480. static u16 hdmi_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
  481. {
  482. return ((hdmi_read(sd, reg) << 8) | hdmi_read(sd, reg + 1)) & mask;
  483. }
  484. static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  485. {
  486. struct adv76xx_state *state = to_state(sd);
  487. return regmap_write(state->regmap[ADV76XX_PAGE_HDMI], reg, val);
  488. }
  489. static inline int hdmi_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  490. {
  491. return hdmi_write(sd, reg, (hdmi_read(sd, reg) & ~mask) | val);
  492. }
  493. static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  494. {
  495. struct adv76xx_state *state = to_state(sd);
  496. return regmap_write(state->regmap[ADV76XX_PAGE_TEST], reg, val);
  497. }
  498. static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
  499. {
  500. struct adv76xx_state *state = to_state(sd);
  501. return adv76xx_read_check(state, ADV76XX_PAGE_CP, reg);
  502. }
  503. static u16 cp_read16(struct v4l2_subdev *sd, u8 reg, u16 mask)
  504. {
  505. return ((cp_read(sd, reg) << 8) | cp_read(sd, reg + 1)) & mask;
  506. }
  507. static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  508. {
  509. struct adv76xx_state *state = to_state(sd);
  510. return regmap_write(state->regmap[ADV76XX_PAGE_CP], reg, val);
  511. }
  512. static inline int cp_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  513. {
  514. return cp_write(sd, reg, (cp_read(sd, reg) & ~mask) | val);
  515. }
  516. static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
  517. {
  518. struct adv76xx_state *state = to_state(sd);
  519. return adv76xx_read_check(state, ADV7604_PAGE_VDP, reg);
  520. }
  521. static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  522. {
  523. struct adv76xx_state *state = to_state(sd);
  524. return regmap_write(state->regmap[ADV7604_PAGE_VDP], reg, val);
  525. }
  526. #define ADV76XX_REG(page, offset) (((page) << 8) | (offset))
  527. #define ADV76XX_REG_SEQ_TERM 0xffff
  528. #ifdef CONFIG_VIDEO_ADV_DEBUG
  529. static int adv76xx_read_reg(struct v4l2_subdev *sd, unsigned int reg)
  530. {
  531. struct adv76xx_state *state = to_state(sd);
  532. unsigned int page = reg >> 8;
  533. unsigned int val;
  534. int err;
  535. if (!(BIT(page) & state->info->page_mask))
  536. return -EINVAL;
  537. reg &= 0xff;
  538. err = regmap_read(state->regmap[page], reg, &val);
  539. return err ? err : val;
  540. }
  541. #endif
  542. static int adv76xx_write_reg(struct v4l2_subdev *sd, unsigned int reg, u8 val)
  543. {
  544. struct adv76xx_state *state = to_state(sd);
  545. unsigned int page = reg >> 8;
  546. if (!(BIT(page) & state->info->page_mask))
  547. return -EINVAL;
  548. reg &= 0xff;
  549. return regmap_write(state->regmap[page], reg, val);
  550. }
  551. static void adv76xx_write_reg_seq(struct v4l2_subdev *sd,
  552. const struct adv76xx_reg_seq *reg_seq)
  553. {
  554. unsigned int i;
  555. for (i = 0; reg_seq[i].reg != ADV76XX_REG_SEQ_TERM; i++)
  556. adv76xx_write_reg(sd, reg_seq[i].reg, reg_seq[i].val);
  557. }
  558. /* -----------------------------------------------------------------------------
  559. * Format helpers
  560. */
  561. static const struct adv76xx_format_info adv7604_formats[] = {
  562. { MEDIA_BUS_FMT_RGB888_1X24, ADV76XX_OP_CH_SEL_RGB, true, false,
  563. ADV76XX_OP_MODE_SEL_SDR_444 | ADV76XX_OP_FORMAT_SEL_8BIT },
  564. { MEDIA_BUS_FMT_YUYV8_2X8, ADV76XX_OP_CH_SEL_RGB, false, false,
  565. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_8BIT },
  566. { MEDIA_BUS_FMT_YVYU8_2X8, ADV76XX_OP_CH_SEL_RGB, false, true,
  567. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_8BIT },
  568. { MEDIA_BUS_FMT_YUYV10_2X10, ADV76XX_OP_CH_SEL_RGB, false, false,
  569. ADV76XX_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
  570. { MEDIA_BUS_FMT_YVYU10_2X10, ADV76XX_OP_CH_SEL_RGB, false, true,
  571. ADV76XX_OP_MODE_SEL_SDR_422 | ADV7604_OP_FORMAT_SEL_10BIT },
  572. { MEDIA_BUS_FMT_YUYV12_2X12, ADV76XX_OP_CH_SEL_RGB, false, false,
  573. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_12BIT },
  574. { MEDIA_BUS_FMT_YVYU12_2X12, ADV76XX_OP_CH_SEL_RGB, false, true,
  575. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_12BIT },
  576. { MEDIA_BUS_FMT_UYVY8_1X16, ADV76XX_OP_CH_SEL_RBG, false, false,
  577. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  578. { MEDIA_BUS_FMT_VYUY8_1X16, ADV76XX_OP_CH_SEL_RBG, false, true,
  579. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  580. { MEDIA_BUS_FMT_YUYV8_1X16, ADV76XX_OP_CH_SEL_RGB, false, false,
  581. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  582. { MEDIA_BUS_FMT_YVYU8_1X16, ADV76XX_OP_CH_SEL_RGB, false, true,
  583. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  584. { MEDIA_BUS_FMT_UYVY10_1X20, ADV76XX_OP_CH_SEL_RBG, false, false,
  585. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
  586. { MEDIA_BUS_FMT_VYUY10_1X20, ADV76XX_OP_CH_SEL_RBG, false, true,
  587. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
  588. { MEDIA_BUS_FMT_YUYV10_1X20, ADV76XX_OP_CH_SEL_RGB, false, false,
  589. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
  590. { MEDIA_BUS_FMT_YVYU10_1X20, ADV76XX_OP_CH_SEL_RGB, false, true,
  591. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV7604_OP_FORMAT_SEL_10BIT },
  592. { MEDIA_BUS_FMT_UYVY12_1X24, ADV76XX_OP_CH_SEL_RBG, false, false,
  593. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  594. { MEDIA_BUS_FMT_VYUY12_1X24, ADV76XX_OP_CH_SEL_RBG, false, true,
  595. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  596. { MEDIA_BUS_FMT_YUYV12_1X24, ADV76XX_OP_CH_SEL_RGB, false, false,
  597. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  598. { MEDIA_BUS_FMT_YVYU12_1X24, ADV76XX_OP_CH_SEL_RGB, false, true,
  599. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  600. };
  601. static const struct adv76xx_format_info adv7611_formats[] = {
  602. { MEDIA_BUS_FMT_RGB888_1X24, ADV76XX_OP_CH_SEL_RGB, true, false,
  603. ADV76XX_OP_MODE_SEL_SDR_444 | ADV76XX_OP_FORMAT_SEL_8BIT },
  604. { MEDIA_BUS_FMT_YUYV8_2X8, ADV76XX_OP_CH_SEL_RGB, false, false,
  605. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_8BIT },
  606. { MEDIA_BUS_FMT_YVYU8_2X8, ADV76XX_OP_CH_SEL_RGB, false, true,
  607. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_8BIT },
  608. { MEDIA_BUS_FMT_YUYV12_2X12, ADV76XX_OP_CH_SEL_RGB, false, false,
  609. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_12BIT },
  610. { MEDIA_BUS_FMT_YVYU12_2X12, ADV76XX_OP_CH_SEL_RGB, false, true,
  611. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_12BIT },
  612. { MEDIA_BUS_FMT_UYVY8_1X16, ADV76XX_OP_CH_SEL_RBG, false, false,
  613. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  614. { MEDIA_BUS_FMT_VYUY8_1X16, ADV76XX_OP_CH_SEL_RBG, false, true,
  615. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  616. { MEDIA_BUS_FMT_YUYV8_1X16, ADV76XX_OP_CH_SEL_RGB, false, false,
  617. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  618. { MEDIA_BUS_FMT_YVYU8_1X16, ADV76XX_OP_CH_SEL_RGB, false, true,
  619. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  620. { MEDIA_BUS_FMT_UYVY12_1X24, ADV76XX_OP_CH_SEL_RBG, false, false,
  621. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  622. { MEDIA_BUS_FMT_VYUY12_1X24, ADV76XX_OP_CH_SEL_RBG, false, true,
  623. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  624. { MEDIA_BUS_FMT_YUYV12_1X24, ADV76XX_OP_CH_SEL_RGB, false, false,
  625. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  626. { MEDIA_BUS_FMT_YVYU12_1X24, ADV76XX_OP_CH_SEL_RGB, false, true,
  627. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_12BIT },
  628. };
  629. static const struct adv76xx_format_info adv7612_formats[] = {
  630. { MEDIA_BUS_FMT_RGB888_1X24, ADV76XX_OP_CH_SEL_RGB, true, false,
  631. ADV76XX_OP_MODE_SEL_SDR_444 | ADV76XX_OP_FORMAT_SEL_8BIT },
  632. { MEDIA_BUS_FMT_YUYV8_2X8, ADV76XX_OP_CH_SEL_RGB, false, false,
  633. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_8BIT },
  634. { MEDIA_BUS_FMT_YVYU8_2X8, ADV76XX_OP_CH_SEL_RGB, false, true,
  635. ADV76XX_OP_MODE_SEL_SDR_422 | ADV76XX_OP_FORMAT_SEL_8BIT },
  636. { MEDIA_BUS_FMT_UYVY8_1X16, ADV76XX_OP_CH_SEL_RBG, false, false,
  637. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  638. { MEDIA_BUS_FMT_VYUY8_1X16, ADV76XX_OP_CH_SEL_RBG, false, true,
  639. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  640. { MEDIA_BUS_FMT_YUYV8_1X16, ADV76XX_OP_CH_SEL_RGB, false, false,
  641. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  642. { MEDIA_BUS_FMT_YVYU8_1X16, ADV76XX_OP_CH_SEL_RGB, false, true,
  643. ADV76XX_OP_MODE_SEL_SDR_422_2X | ADV76XX_OP_FORMAT_SEL_8BIT },
  644. };
  645. static const struct adv76xx_format_info *
  646. adv76xx_format_info(struct adv76xx_state *state, u32 code)
  647. {
  648. unsigned int i;
  649. for (i = 0; i < state->info->nformats; ++i) {
  650. if (state->info->formats[i].code == code)
  651. return &state->info->formats[i];
  652. }
  653. return NULL;
  654. }
  655. /* ----------------------------------------------------------------------- */
  656. static inline bool is_analog_input(struct v4l2_subdev *sd)
  657. {
  658. struct adv76xx_state *state = to_state(sd);
  659. return state->selected_input == ADV7604_PAD_VGA_RGB ||
  660. state->selected_input == ADV7604_PAD_VGA_COMP;
  661. }
  662. static inline bool is_digital_input(struct v4l2_subdev *sd)
  663. {
  664. struct adv76xx_state *state = to_state(sd);
  665. return state->selected_input == ADV76XX_PAD_HDMI_PORT_A ||
  666. state->selected_input == ADV7604_PAD_HDMI_PORT_B ||
  667. state->selected_input == ADV7604_PAD_HDMI_PORT_C ||
  668. state->selected_input == ADV7604_PAD_HDMI_PORT_D;
  669. }
  670. /* ----------------------------------------------------------------------- */
  671. #ifdef CONFIG_VIDEO_ADV_DEBUG
  672. static void adv76xx_inv_register(struct v4l2_subdev *sd)
  673. {
  674. v4l2_info(sd, "0x000-0x0ff: IO Map\n");
  675. v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
  676. v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
  677. v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
  678. v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
  679. v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
  680. v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
  681. v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
  682. v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
  683. v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
  684. v4l2_info(sd, "0xa00-0xaff: Test Map\n");
  685. v4l2_info(sd, "0xb00-0xbff: CP Map\n");
  686. v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
  687. }
  688. static int adv76xx_g_register(struct v4l2_subdev *sd,
  689. struct v4l2_dbg_register *reg)
  690. {
  691. int ret;
  692. ret = adv76xx_read_reg(sd, reg->reg);
  693. if (ret < 0) {
  694. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  695. adv76xx_inv_register(sd);
  696. return ret;
  697. }
  698. reg->size = 1;
  699. reg->val = ret;
  700. return 0;
  701. }
  702. static int adv76xx_s_register(struct v4l2_subdev *sd,
  703. const struct v4l2_dbg_register *reg)
  704. {
  705. int ret;
  706. ret = adv76xx_write_reg(sd, reg->reg, reg->val);
  707. if (ret < 0) {
  708. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  709. adv76xx_inv_register(sd);
  710. return ret;
  711. }
  712. return 0;
  713. }
  714. #endif
  715. static unsigned int adv7604_read_cable_det(struct v4l2_subdev *sd)
  716. {
  717. u8 value = io_read(sd, 0x6f);
  718. return ((value & 0x10) >> 4)
  719. | ((value & 0x08) >> 2)
  720. | ((value & 0x04) << 0)
  721. | ((value & 0x02) << 2);
  722. }
  723. static unsigned int adv7611_read_cable_det(struct v4l2_subdev *sd)
  724. {
  725. u8 value = io_read(sd, 0x6f);
  726. return value & 1;
  727. }
  728. static unsigned int adv7612_read_cable_det(struct v4l2_subdev *sd)
  729. {
  730. /* Reads CABLE_DET_A_RAW. For input B support, need to
  731. * account for bit 7 [MSB] of 0x6a (ie. CABLE_DET_B_RAW)
  732. */
  733. u8 value = io_read(sd, 0x6f);
  734. return value & 1;
  735. }
  736. static int adv76xx_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
  737. {
  738. struct adv76xx_state *state = to_state(sd);
  739. const struct adv76xx_chip_info *info = state->info;
  740. return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
  741. info->read_cable_det(sd));
  742. }
  743. static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
  744. u8 prim_mode,
  745. const struct adv76xx_video_standards *predef_vid_timings,
  746. const struct v4l2_dv_timings *timings)
  747. {
  748. int i;
  749. for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
  750. if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
  751. is_digital_input(sd) ? 250000 : 1000000))
  752. continue;
  753. io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
  754. io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
  755. prim_mode); /* v_freq and prim mode */
  756. return 0;
  757. }
  758. return -1;
  759. }
  760. static int configure_predefined_video_timings(struct v4l2_subdev *sd,
  761. struct v4l2_dv_timings *timings)
  762. {
  763. struct adv76xx_state *state = to_state(sd);
  764. int err;
  765. v4l2_dbg(1, debug, sd, "%s", __func__);
  766. if (adv76xx_has_afe(state)) {
  767. /* reset to default values */
  768. io_write(sd, 0x16, 0x43);
  769. io_write(sd, 0x17, 0x5a);
  770. }
  771. /* disable embedded syncs for auto graphics mode */
  772. cp_write_clr_set(sd, 0x81, 0x10, 0x00);
  773. cp_write(sd, 0x8f, 0x00);
  774. cp_write(sd, 0x90, 0x00);
  775. cp_write(sd, 0xa2, 0x00);
  776. cp_write(sd, 0xa3, 0x00);
  777. cp_write(sd, 0xa4, 0x00);
  778. cp_write(sd, 0xa5, 0x00);
  779. cp_write(sd, 0xa6, 0x00);
  780. cp_write(sd, 0xa7, 0x00);
  781. cp_write(sd, 0xab, 0x00);
  782. cp_write(sd, 0xac, 0x00);
  783. if (is_analog_input(sd)) {
  784. err = find_and_set_predefined_video_timings(sd,
  785. 0x01, adv7604_prim_mode_comp, timings);
  786. if (err)
  787. err = find_and_set_predefined_video_timings(sd,
  788. 0x02, adv7604_prim_mode_gr, timings);
  789. } else if (is_digital_input(sd)) {
  790. err = find_and_set_predefined_video_timings(sd,
  791. 0x05, adv76xx_prim_mode_hdmi_comp, timings);
  792. if (err)
  793. err = find_and_set_predefined_video_timings(sd,
  794. 0x06, adv76xx_prim_mode_hdmi_gr, timings);
  795. } else {
  796. v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
  797. __func__, state->selected_input);
  798. err = -1;
  799. }
  800. return err;
  801. }
  802. static void configure_custom_video_timings(struct v4l2_subdev *sd,
  803. const struct v4l2_bt_timings *bt)
  804. {
  805. struct adv76xx_state *state = to_state(sd);
  806. u32 width = htotal(bt);
  807. u32 height = vtotal(bt);
  808. u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
  809. u16 cp_start_eav = width - bt->hfrontporch;
  810. u16 cp_start_vbi = height - bt->vfrontporch;
  811. u16 cp_end_vbi = bt->vsync + bt->vbackporch;
  812. u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
  813. ((width * (ADV76XX_FSC / 100)) / ((u32)bt->pixelclock / 100)) : 0;
  814. const u8 pll[2] = {
  815. 0xc0 | ((width >> 8) & 0x1f),
  816. width & 0xff
  817. };
  818. v4l2_dbg(2, debug, sd, "%s\n", __func__);
  819. if (is_analog_input(sd)) {
  820. /* auto graphics */
  821. io_write(sd, 0x00, 0x07); /* video std */
  822. io_write(sd, 0x01, 0x02); /* prim mode */
  823. /* enable embedded syncs for auto graphics mode */
  824. cp_write_clr_set(sd, 0x81, 0x10, 0x10);
  825. /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
  826. /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
  827. /* IO-map reg. 0x16 and 0x17 should be written in sequence */
  828. if (regmap_raw_write(state->regmap[ADV76XX_PAGE_IO],
  829. 0x16, pll, 2))
  830. v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
  831. /* active video - horizontal timing */
  832. cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
  833. cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
  834. ((cp_start_eav >> 8) & 0x0f));
  835. cp_write(sd, 0xa4, cp_start_eav & 0xff);
  836. /* active video - vertical timing */
  837. cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
  838. cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
  839. ((cp_end_vbi >> 8) & 0xf));
  840. cp_write(sd, 0xa7, cp_end_vbi & 0xff);
  841. } else if (is_digital_input(sd)) {
  842. /* set default prim_mode/vid_std for HDMI
  843. according to [REF_03, c. 4.2] */
  844. io_write(sd, 0x00, 0x02); /* video std */
  845. io_write(sd, 0x01, 0x06); /* prim mode */
  846. } else {
  847. v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
  848. __func__, state->selected_input);
  849. }
  850. cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
  851. cp_write(sd, 0x90, ch1_fr_ll & 0xff);
  852. cp_write(sd, 0xab, (height >> 4) & 0xff);
  853. cp_write(sd, 0xac, (height & 0x0f) << 4);
  854. }
  855. static void adv76xx_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
  856. {
  857. struct adv76xx_state *state = to_state(sd);
  858. u8 offset_buf[4];
  859. if (auto_offset) {
  860. offset_a = 0x3ff;
  861. offset_b = 0x3ff;
  862. offset_c = 0x3ff;
  863. }
  864. v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
  865. __func__, auto_offset ? "Auto" : "Manual",
  866. offset_a, offset_b, offset_c);
  867. offset_buf[0] = (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
  868. offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
  869. offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
  870. offset_buf[3] = offset_c & 0x0ff;
  871. /* Registers must be written in this order with no i2c access in between */
  872. if (regmap_raw_write(state->regmap[ADV76XX_PAGE_CP],
  873. 0x77, offset_buf, 4))
  874. v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
  875. }
  876. static void adv76xx_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
  877. {
  878. struct adv76xx_state *state = to_state(sd);
  879. u8 gain_buf[4];
  880. u8 gain_man = 1;
  881. u8 agc_mode_man = 1;
  882. if (auto_gain) {
  883. gain_man = 0;
  884. agc_mode_man = 0;
  885. gain_a = 0x100;
  886. gain_b = 0x100;
  887. gain_c = 0x100;
  888. }
  889. v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
  890. __func__, auto_gain ? "Auto" : "Manual",
  891. gain_a, gain_b, gain_c);
  892. gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
  893. gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
  894. gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
  895. gain_buf[3] = ((gain_c & 0x0ff));
  896. /* Registers must be written in this order with no i2c access in between */
  897. if (regmap_raw_write(state->regmap[ADV76XX_PAGE_CP],
  898. 0x73, gain_buf, 4))
  899. v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
  900. }
  901. static void set_rgb_quantization_range(struct v4l2_subdev *sd)
  902. {
  903. struct adv76xx_state *state = to_state(sd);
  904. bool rgb_output = io_read(sd, 0x02) & 0x02;
  905. bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
  906. v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
  907. __func__, state->rgb_quantization_range,
  908. rgb_output, hdmi_signal);
  909. adv76xx_set_gain(sd, true, 0x0, 0x0, 0x0);
  910. adv76xx_set_offset(sd, true, 0x0, 0x0, 0x0);
  911. switch (state->rgb_quantization_range) {
  912. case V4L2_DV_RGB_RANGE_AUTO:
  913. if (state->selected_input == ADV7604_PAD_VGA_RGB) {
  914. /* Receiving analog RGB signal
  915. * Set RGB full range (0-255) */
  916. io_write_clr_set(sd, 0x02, 0xf0, 0x10);
  917. break;
  918. }
  919. if (state->selected_input == ADV7604_PAD_VGA_COMP) {
  920. /* Receiving analog YPbPr signal
  921. * Set automode */
  922. io_write_clr_set(sd, 0x02, 0xf0, 0xf0);
  923. break;
  924. }
  925. if (hdmi_signal) {
  926. /* Receiving HDMI signal
  927. * Set automode */
  928. io_write_clr_set(sd, 0x02, 0xf0, 0xf0);
  929. break;
  930. }
  931. /* Receiving DVI-D signal
  932. * ADV7604 selects RGB limited range regardless of
  933. * input format (CE/IT) in automatic mode */
  934. if (state->timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
  935. /* RGB limited range (16-235) */
  936. io_write_clr_set(sd, 0x02, 0xf0, 0x00);
  937. } else {
  938. /* RGB full range (0-255) */
  939. io_write_clr_set(sd, 0x02, 0xf0, 0x10);
  940. if (is_digital_input(sd) && rgb_output) {
  941. adv76xx_set_offset(sd, false, 0x40, 0x40, 0x40);
  942. } else {
  943. adv76xx_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
  944. adv76xx_set_offset(sd, false, 0x70, 0x70, 0x70);
  945. }
  946. }
  947. break;
  948. case V4L2_DV_RGB_RANGE_LIMITED:
  949. if (state->selected_input == ADV7604_PAD_VGA_COMP) {
  950. /* YCrCb limited range (16-235) */
  951. io_write_clr_set(sd, 0x02, 0xf0, 0x20);
  952. break;
  953. }
  954. /* RGB limited range (16-235) */
  955. io_write_clr_set(sd, 0x02, 0xf0, 0x00);
  956. break;
  957. case V4L2_DV_RGB_RANGE_FULL:
  958. if (state->selected_input == ADV7604_PAD_VGA_COMP) {
  959. /* YCrCb full range (0-255) */
  960. io_write_clr_set(sd, 0x02, 0xf0, 0x60);
  961. break;
  962. }
  963. /* RGB full range (0-255) */
  964. io_write_clr_set(sd, 0x02, 0xf0, 0x10);
  965. if (is_analog_input(sd) || hdmi_signal)
  966. break;
  967. /* Adjust gain/offset for DVI-D signals only */
  968. if (rgb_output) {
  969. adv76xx_set_offset(sd, false, 0x40, 0x40, 0x40);
  970. } else {
  971. adv76xx_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
  972. adv76xx_set_offset(sd, false, 0x70, 0x70, 0x70);
  973. }
  974. break;
  975. }
  976. }
  977. static int adv76xx_s_ctrl(struct v4l2_ctrl *ctrl)
  978. {
  979. struct v4l2_subdev *sd =
  980. &container_of(ctrl->handler, struct adv76xx_state, hdl)->sd;
  981. struct adv76xx_state *state = to_state(sd);
  982. switch (ctrl->id) {
  983. case V4L2_CID_BRIGHTNESS:
  984. cp_write(sd, 0x3c, ctrl->val);
  985. return 0;
  986. case V4L2_CID_CONTRAST:
  987. cp_write(sd, 0x3a, ctrl->val);
  988. return 0;
  989. case V4L2_CID_SATURATION:
  990. cp_write(sd, 0x3b, ctrl->val);
  991. return 0;
  992. case V4L2_CID_HUE:
  993. cp_write(sd, 0x3d, ctrl->val);
  994. return 0;
  995. case V4L2_CID_DV_RX_RGB_RANGE:
  996. state->rgb_quantization_range = ctrl->val;
  997. set_rgb_quantization_range(sd);
  998. return 0;
  999. case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
  1000. if (!adv76xx_has_afe(state))
  1001. return -EINVAL;
  1002. /* Set the analog sampling phase. This is needed to find the
  1003. best sampling phase for analog video: an application or
  1004. driver has to try a number of phases and analyze the picture
  1005. quality before settling on the best performing phase. */
  1006. afe_write(sd, 0xc8, ctrl->val);
  1007. return 0;
  1008. case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
  1009. /* Use the default blue color for free running mode,
  1010. or supply your own. */
  1011. cp_write_clr_set(sd, 0xbf, 0x04, ctrl->val << 2);
  1012. return 0;
  1013. case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
  1014. cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
  1015. cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
  1016. cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
  1017. return 0;
  1018. }
  1019. return -EINVAL;
  1020. }
  1021. /* ----------------------------------------------------------------------- */
  1022. static inline bool no_power(struct v4l2_subdev *sd)
  1023. {
  1024. /* Entire chip or CP powered off */
  1025. return io_read(sd, 0x0c) & 0x24;
  1026. }
  1027. static inline bool no_signal_tmds(struct v4l2_subdev *sd)
  1028. {
  1029. struct adv76xx_state *state = to_state(sd);
  1030. return !(io_read(sd, 0x6a) & (0x10 >> state->selected_input));
  1031. }
  1032. static inline bool no_lock_tmds(struct v4l2_subdev *sd)
  1033. {
  1034. struct adv76xx_state *state = to_state(sd);
  1035. const struct adv76xx_chip_info *info = state->info;
  1036. return (io_read(sd, 0x6a) & info->tdms_lock_mask) != info->tdms_lock_mask;
  1037. }
  1038. static inline bool is_hdmi(struct v4l2_subdev *sd)
  1039. {
  1040. return hdmi_read(sd, 0x05) & 0x80;
  1041. }
  1042. static inline bool no_lock_sspd(struct v4l2_subdev *sd)
  1043. {
  1044. struct adv76xx_state *state = to_state(sd);
  1045. /*
  1046. * Chips without a AFE don't expose registers for the SSPD, so just assume
  1047. * that we have a lock.
  1048. */
  1049. if (adv76xx_has_afe(state))
  1050. return false;
  1051. /* TODO channel 2 */
  1052. return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
  1053. }
  1054. static inline bool no_lock_stdi(struct v4l2_subdev *sd)
  1055. {
  1056. /* TODO channel 2 */
  1057. return !(cp_read(sd, 0xb1) & 0x80);
  1058. }
  1059. static inline bool no_signal(struct v4l2_subdev *sd)
  1060. {
  1061. bool ret;
  1062. ret = no_power(sd);
  1063. ret |= no_lock_stdi(sd);
  1064. ret |= no_lock_sspd(sd);
  1065. if (is_digital_input(sd)) {
  1066. ret |= no_lock_tmds(sd);
  1067. ret |= no_signal_tmds(sd);
  1068. }
  1069. return ret;
  1070. }
  1071. static inline bool no_lock_cp(struct v4l2_subdev *sd)
  1072. {
  1073. struct adv76xx_state *state = to_state(sd);
  1074. if (!adv76xx_has_afe(state))
  1075. return false;
  1076. /* CP has detected a non standard number of lines on the incoming
  1077. video compared to what it is configured to receive by s_dv_timings */
  1078. return io_read(sd, 0x12) & 0x01;
  1079. }
  1080. static inline bool in_free_run(struct v4l2_subdev *sd)
  1081. {
  1082. return cp_read(sd, 0xff) & 0x10;
  1083. }
  1084. static int adv76xx_g_input_status(struct v4l2_subdev *sd, u32 *status)
  1085. {
  1086. *status = 0;
  1087. *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
  1088. *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
  1089. if (!in_free_run(sd) && no_lock_cp(sd))
  1090. *status |= is_digital_input(sd) ?
  1091. V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
  1092. v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
  1093. return 0;
  1094. }
  1095. /* ----------------------------------------------------------------------- */
  1096. struct stdi_readback {
  1097. u16 bl, lcf, lcvs;
  1098. u8 hs_pol, vs_pol;
  1099. bool interlaced;
  1100. };
  1101. static int stdi2dv_timings(struct v4l2_subdev *sd,
  1102. struct stdi_readback *stdi,
  1103. struct v4l2_dv_timings *timings)
  1104. {
  1105. struct adv76xx_state *state = to_state(sd);
  1106. u32 hfreq = (ADV76XX_FSC * 8) / stdi->bl;
  1107. u32 pix_clk;
  1108. int i;
  1109. for (i = 0; adv76xx_timings[i].bt.height; i++) {
  1110. if (vtotal(&adv76xx_timings[i].bt) != stdi->lcf + 1)
  1111. continue;
  1112. if (adv76xx_timings[i].bt.vsync != stdi->lcvs)
  1113. continue;
  1114. pix_clk = hfreq * htotal(&adv76xx_timings[i].bt);
  1115. if ((pix_clk < adv76xx_timings[i].bt.pixelclock + 1000000) &&
  1116. (pix_clk > adv76xx_timings[i].bt.pixelclock - 1000000)) {
  1117. *timings = adv76xx_timings[i];
  1118. return 0;
  1119. }
  1120. }
  1121. if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs, 0,
  1122. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  1123. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  1124. false, timings))
  1125. return 0;
  1126. if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
  1127. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  1128. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  1129. false, state->aspect_ratio, timings))
  1130. return 0;
  1131. v4l2_dbg(2, debug, sd,
  1132. "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
  1133. __func__, stdi->lcvs, stdi->lcf, stdi->bl,
  1134. stdi->hs_pol, stdi->vs_pol);
  1135. return -1;
  1136. }
  1137. static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
  1138. {
  1139. struct adv76xx_state *state = to_state(sd);
  1140. const struct adv76xx_chip_info *info = state->info;
  1141. u8 polarity;
  1142. if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
  1143. v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
  1144. return -1;
  1145. }
  1146. /* read STDI */
  1147. stdi->bl = cp_read16(sd, 0xb1, 0x3fff);
  1148. stdi->lcf = cp_read16(sd, info->lcf_reg, 0x7ff);
  1149. stdi->lcvs = cp_read(sd, 0xb3) >> 3;
  1150. stdi->interlaced = io_read(sd, 0x12) & 0x10;
  1151. if (adv76xx_has_afe(state)) {
  1152. /* read SSPD */
  1153. polarity = cp_read(sd, 0xb5);
  1154. if ((polarity & 0x03) == 0x01) {
  1155. stdi->hs_pol = polarity & 0x10
  1156. ? (polarity & 0x08 ? '+' : '-') : 'x';
  1157. stdi->vs_pol = polarity & 0x40
  1158. ? (polarity & 0x20 ? '+' : '-') : 'x';
  1159. } else {
  1160. stdi->hs_pol = 'x';
  1161. stdi->vs_pol = 'x';
  1162. }
  1163. } else {
  1164. polarity = hdmi_read(sd, 0x05);
  1165. stdi->hs_pol = polarity & 0x20 ? '+' : '-';
  1166. stdi->vs_pol = polarity & 0x10 ? '+' : '-';
  1167. }
  1168. if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
  1169. v4l2_dbg(2, debug, sd,
  1170. "%s: signal lost during readout of STDI/SSPD\n", __func__);
  1171. return -1;
  1172. }
  1173. if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
  1174. v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
  1175. memset(stdi, 0, sizeof(struct stdi_readback));
  1176. return -1;
  1177. }
  1178. v4l2_dbg(2, debug, sd,
  1179. "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
  1180. __func__, stdi->lcf, stdi->bl, stdi->lcvs,
  1181. stdi->hs_pol, stdi->vs_pol,
  1182. stdi->interlaced ? "interlaced" : "progressive");
  1183. return 0;
  1184. }
  1185. static int adv76xx_enum_dv_timings(struct v4l2_subdev *sd,
  1186. struct v4l2_enum_dv_timings *timings)
  1187. {
  1188. struct adv76xx_state *state = to_state(sd);
  1189. if (timings->index >= ARRAY_SIZE(adv76xx_timings) - 1)
  1190. return -EINVAL;
  1191. if (timings->pad >= state->source_pad)
  1192. return -EINVAL;
  1193. memset(timings->reserved, 0, sizeof(timings->reserved));
  1194. timings->timings = adv76xx_timings[timings->index];
  1195. return 0;
  1196. }
  1197. static int adv76xx_dv_timings_cap(struct v4l2_subdev *sd,
  1198. struct v4l2_dv_timings_cap *cap)
  1199. {
  1200. struct adv76xx_state *state = to_state(sd);
  1201. if (cap->pad >= state->source_pad)
  1202. return -EINVAL;
  1203. cap->type = V4L2_DV_BT_656_1120;
  1204. cap->bt.max_width = 1920;
  1205. cap->bt.max_height = 1200;
  1206. cap->bt.min_pixelclock = 25000000;
  1207. switch (cap->pad) {
  1208. case ADV76XX_PAD_HDMI_PORT_A:
  1209. case ADV7604_PAD_HDMI_PORT_B:
  1210. case ADV7604_PAD_HDMI_PORT_C:
  1211. case ADV7604_PAD_HDMI_PORT_D:
  1212. cap->bt.max_pixelclock = 225000000;
  1213. break;
  1214. case ADV7604_PAD_VGA_RGB:
  1215. case ADV7604_PAD_VGA_COMP:
  1216. default:
  1217. cap->bt.max_pixelclock = 170000000;
  1218. break;
  1219. }
  1220. cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  1221. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
  1222. cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
  1223. V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
  1224. return 0;
  1225. }
  1226. /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
  1227. if the format is listed in adv76xx_timings[] */
  1228. static void adv76xx_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
  1229. struct v4l2_dv_timings *timings)
  1230. {
  1231. int i;
  1232. for (i = 0; adv76xx_timings[i].bt.width; i++) {
  1233. if (v4l2_match_dv_timings(timings, &adv76xx_timings[i],
  1234. is_digital_input(sd) ? 250000 : 1000000)) {
  1235. *timings = adv76xx_timings[i];
  1236. break;
  1237. }
  1238. }
  1239. }
  1240. static unsigned int adv7604_read_hdmi_pixelclock(struct v4l2_subdev *sd)
  1241. {
  1242. unsigned int freq;
  1243. int a, b;
  1244. a = hdmi_read(sd, 0x06);
  1245. b = hdmi_read(sd, 0x3b);
  1246. if (a < 0 || b < 0)
  1247. return 0;
  1248. freq = a * 1000000 + ((b & 0x30) >> 4) * 250000;
  1249. if (is_hdmi(sd)) {
  1250. /* adjust for deep color mode */
  1251. unsigned bits_per_channel = ((hdmi_read(sd, 0x0b) & 0x60) >> 4) + 8;
  1252. freq = freq * 8 / bits_per_channel;
  1253. }
  1254. return freq;
  1255. }
  1256. static unsigned int adv7611_read_hdmi_pixelclock(struct v4l2_subdev *sd)
  1257. {
  1258. int a, b;
  1259. a = hdmi_read(sd, 0x51);
  1260. b = hdmi_read(sd, 0x52);
  1261. if (a < 0 || b < 0)
  1262. return 0;
  1263. return ((a << 1) | (b >> 7)) * 1000000 + (b & 0x7f) * 1000000 / 128;
  1264. }
  1265. static int adv76xx_query_dv_timings(struct v4l2_subdev *sd,
  1266. struct v4l2_dv_timings *timings)
  1267. {
  1268. struct adv76xx_state *state = to_state(sd);
  1269. const struct adv76xx_chip_info *info = state->info;
  1270. struct v4l2_bt_timings *bt = &timings->bt;
  1271. struct stdi_readback stdi;
  1272. if (!timings)
  1273. return -EINVAL;
  1274. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1275. if (no_signal(sd)) {
  1276. state->restart_stdi_once = true;
  1277. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  1278. return -ENOLINK;
  1279. }
  1280. /* read STDI */
  1281. if (read_stdi(sd, &stdi)) {
  1282. v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
  1283. return -ENOLINK;
  1284. }
  1285. bt->interlaced = stdi.interlaced ?
  1286. V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  1287. if (is_digital_input(sd)) {
  1288. timings->type = V4L2_DV_BT_656_1120;
  1289. bt->width = hdmi_read16(sd, 0x07, info->linewidth_mask);
  1290. bt->height = hdmi_read16(sd, 0x09, info->field0_height_mask);
  1291. bt->pixelclock = info->read_hdmi_pixelclock(sd);
  1292. bt->hfrontporch = hdmi_read16(sd, 0x20, info->hfrontporch_mask);
  1293. bt->hsync = hdmi_read16(sd, 0x22, info->hsync_mask);
  1294. bt->hbackporch = hdmi_read16(sd, 0x24, info->hbackporch_mask);
  1295. bt->vfrontporch = hdmi_read16(sd, 0x2a,
  1296. info->field0_vfrontporch_mask) / 2;
  1297. bt->vsync = hdmi_read16(sd, 0x2e, info->field0_vsync_mask) / 2;
  1298. bt->vbackporch = hdmi_read16(sd, 0x32,
  1299. info->field0_vbackporch_mask) / 2;
  1300. bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
  1301. ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
  1302. if (bt->interlaced == V4L2_DV_INTERLACED) {
  1303. bt->height += hdmi_read16(sd, 0x0b,
  1304. info->field1_height_mask);
  1305. bt->il_vfrontporch = hdmi_read16(sd, 0x2c,
  1306. info->field1_vfrontporch_mask) / 2;
  1307. bt->il_vsync = hdmi_read16(sd, 0x30,
  1308. info->field1_vsync_mask) / 2;
  1309. bt->il_vbackporch = hdmi_read16(sd, 0x34,
  1310. info->field1_vbackporch_mask) / 2;
  1311. }
  1312. adv76xx_fill_optional_dv_timings_fields(sd, timings);
  1313. } else {
  1314. /* find format
  1315. * Since LCVS values are inaccurate [REF_03, p. 275-276],
  1316. * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
  1317. */
  1318. if (!stdi2dv_timings(sd, &stdi, timings))
  1319. goto found;
  1320. stdi.lcvs += 1;
  1321. v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
  1322. if (!stdi2dv_timings(sd, &stdi, timings))
  1323. goto found;
  1324. stdi.lcvs -= 2;
  1325. v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
  1326. if (stdi2dv_timings(sd, &stdi, timings)) {
  1327. /*
  1328. * The STDI block may measure wrong values, especially
  1329. * for lcvs and lcf. If the driver can not find any
  1330. * valid timing, the STDI block is restarted to measure
  1331. * the video timings again. The function will return an
  1332. * error, but the restart of STDI will generate a new
  1333. * STDI interrupt and the format detection process will
  1334. * restart.
  1335. */
  1336. if (state->restart_stdi_once) {
  1337. v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
  1338. /* TODO restart STDI for Sync Channel 2 */
  1339. /* enter one-shot mode */
  1340. cp_write_clr_set(sd, 0x86, 0x06, 0x00);
  1341. /* trigger STDI restart */
  1342. cp_write_clr_set(sd, 0x86, 0x06, 0x04);
  1343. /* reset to continuous mode */
  1344. cp_write_clr_set(sd, 0x86, 0x06, 0x02);
  1345. state->restart_stdi_once = false;
  1346. return -ENOLINK;
  1347. }
  1348. v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
  1349. return -ERANGE;
  1350. }
  1351. state->restart_stdi_once = true;
  1352. }
  1353. found:
  1354. if (no_signal(sd)) {
  1355. v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
  1356. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1357. return -ENOLINK;
  1358. }
  1359. if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
  1360. (is_digital_input(sd) && bt->pixelclock > 225000000)) {
  1361. v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
  1362. __func__, (u32)bt->pixelclock);
  1363. return -ERANGE;
  1364. }
  1365. if (debug > 1)
  1366. v4l2_print_dv_timings(sd->name, "adv76xx_query_dv_timings: ",
  1367. timings, true);
  1368. return 0;
  1369. }
  1370. static int adv76xx_s_dv_timings(struct v4l2_subdev *sd,
  1371. struct v4l2_dv_timings *timings)
  1372. {
  1373. struct adv76xx_state *state = to_state(sd);
  1374. struct v4l2_bt_timings *bt;
  1375. int err;
  1376. if (!timings)
  1377. return -EINVAL;
  1378. if (v4l2_match_dv_timings(&state->timings, timings, 0)) {
  1379. v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
  1380. return 0;
  1381. }
  1382. bt = &timings->bt;
  1383. if ((is_analog_input(sd) && bt->pixelclock > 170000000) ||
  1384. (is_digital_input(sd) && bt->pixelclock > 225000000)) {
  1385. v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
  1386. __func__, (u32)bt->pixelclock);
  1387. return -ERANGE;
  1388. }
  1389. adv76xx_fill_optional_dv_timings_fields(sd, timings);
  1390. state->timings = *timings;
  1391. cp_write_clr_set(sd, 0x91, 0x40, bt->interlaced ? 0x40 : 0x00);
  1392. /* Use prim_mode and vid_std when available */
  1393. err = configure_predefined_video_timings(sd, timings);
  1394. if (err) {
  1395. /* custom settings when the video format
  1396. does not have prim_mode/vid_std */
  1397. configure_custom_video_timings(sd, bt);
  1398. }
  1399. set_rgb_quantization_range(sd);
  1400. if (debug > 1)
  1401. v4l2_print_dv_timings(sd->name, "adv76xx_s_dv_timings: ",
  1402. timings, true);
  1403. return 0;
  1404. }
  1405. static int adv76xx_g_dv_timings(struct v4l2_subdev *sd,
  1406. struct v4l2_dv_timings *timings)
  1407. {
  1408. struct adv76xx_state *state = to_state(sd);
  1409. *timings = state->timings;
  1410. return 0;
  1411. }
  1412. static void adv7604_set_termination(struct v4l2_subdev *sd, bool enable)
  1413. {
  1414. hdmi_write(sd, 0x01, enable ? 0x00 : 0x78);
  1415. }
  1416. static void adv7611_set_termination(struct v4l2_subdev *sd, bool enable)
  1417. {
  1418. hdmi_write(sd, 0x83, enable ? 0xfe : 0xff);
  1419. }
  1420. static void enable_input(struct v4l2_subdev *sd)
  1421. {
  1422. struct adv76xx_state *state = to_state(sd);
  1423. if (is_analog_input(sd)) {
  1424. io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
  1425. } else if (is_digital_input(sd)) {
  1426. hdmi_write_clr_set(sd, 0x00, 0x03, state->selected_input);
  1427. state->info->set_termination(sd, true);
  1428. io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
  1429. hdmi_write_clr_set(sd, 0x1a, 0x10, 0x00); /* Unmute audio */
  1430. } else {
  1431. v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
  1432. __func__, state->selected_input);
  1433. }
  1434. }
  1435. static void disable_input(struct v4l2_subdev *sd)
  1436. {
  1437. struct adv76xx_state *state = to_state(sd);
  1438. hdmi_write_clr_set(sd, 0x1a, 0x10, 0x10); /* Mute audio */
  1439. msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 7.16.10] */
  1440. io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
  1441. state->info->set_termination(sd, false);
  1442. }
  1443. static void select_input(struct v4l2_subdev *sd)
  1444. {
  1445. struct adv76xx_state *state = to_state(sd);
  1446. const struct adv76xx_chip_info *info = state->info;
  1447. if (is_analog_input(sd)) {
  1448. adv76xx_write_reg_seq(sd, info->recommended_settings[0]);
  1449. afe_write(sd, 0x00, 0x08); /* power up ADC */
  1450. afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
  1451. afe_write(sd, 0xc8, 0x00); /* phase control */
  1452. } else if (is_digital_input(sd)) {
  1453. hdmi_write(sd, 0x00, state->selected_input & 0x03);
  1454. adv76xx_write_reg_seq(sd, info->recommended_settings[1]);
  1455. if (adv76xx_has_afe(state)) {
  1456. afe_write(sd, 0x00, 0xff); /* power down ADC */
  1457. afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
  1458. afe_write(sd, 0xc8, 0x40); /* phase control */
  1459. }
  1460. cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
  1461. cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
  1462. cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
  1463. } else {
  1464. v4l2_dbg(2, debug, sd, "%s: Unknown port %d selected\n",
  1465. __func__, state->selected_input);
  1466. }
  1467. }
  1468. static int adv76xx_s_routing(struct v4l2_subdev *sd,
  1469. u32 input, u32 output, u32 config)
  1470. {
  1471. struct adv76xx_state *state = to_state(sd);
  1472. v4l2_dbg(2, debug, sd, "%s: input %d, selected input %d",
  1473. __func__, input, state->selected_input);
  1474. if (input == state->selected_input)
  1475. return 0;
  1476. if (input > state->info->max_port)
  1477. return -EINVAL;
  1478. state->selected_input = input;
  1479. disable_input(sd);
  1480. select_input(sd);
  1481. enable_input(sd);
  1482. v4l2_subdev_notify_event(sd, &adv76xx_ev_fmt);
  1483. return 0;
  1484. }
  1485. static int adv76xx_enum_mbus_code(struct v4l2_subdev *sd,
  1486. struct v4l2_subdev_pad_config *cfg,
  1487. struct v4l2_subdev_mbus_code_enum *code)
  1488. {
  1489. struct adv76xx_state *state = to_state(sd);
  1490. if (code->index >= state->info->nformats)
  1491. return -EINVAL;
  1492. code->code = state->info->formats[code->index].code;
  1493. return 0;
  1494. }
  1495. static void adv76xx_fill_format(struct adv76xx_state *state,
  1496. struct v4l2_mbus_framefmt *format)
  1497. {
  1498. memset(format, 0, sizeof(*format));
  1499. format->width = state->timings.bt.width;
  1500. format->height = state->timings.bt.height;
  1501. format->field = V4L2_FIELD_NONE;
  1502. format->colorspace = V4L2_COLORSPACE_SRGB;
  1503. if (state->timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO)
  1504. format->colorspace = (state->timings.bt.height <= 576) ?
  1505. V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
  1506. }
  1507. /*
  1508. * Compute the op_ch_sel value required to obtain on the bus the component order
  1509. * corresponding to the selected format taking into account bus reordering
  1510. * applied by the board at the output of the device.
  1511. *
  1512. * The following table gives the op_ch_value from the format component order
  1513. * (expressed as op_ch_sel value in column) and the bus reordering (expressed as
  1514. * adv76xx_bus_order value in row).
  1515. *
  1516. * | GBR(0) GRB(1) BGR(2) RGB(3) BRG(4) RBG(5)
  1517. * ----------+-------------------------------------------------
  1518. * RGB (NOP) | GBR GRB BGR RGB BRG RBG
  1519. * GRB (1-2) | BGR RGB GBR GRB RBG BRG
  1520. * RBG (2-3) | GRB GBR BRG RBG BGR RGB
  1521. * BGR (1-3) | RBG BRG RGB BGR GRB GBR
  1522. * BRG (ROR) | BRG RBG GRB GBR RGB BGR
  1523. * GBR (ROL) | RGB BGR RBG BRG GBR GRB
  1524. */
  1525. static unsigned int adv76xx_op_ch_sel(struct adv76xx_state *state)
  1526. {
  1527. #define _SEL(a,b,c,d,e,f) { \
  1528. ADV76XX_OP_CH_SEL_##a, ADV76XX_OP_CH_SEL_##b, ADV76XX_OP_CH_SEL_##c, \
  1529. ADV76XX_OP_CH_SEL_##d, ADV76XX_OP_CH_SEL_##e, ADV76XX_OP_CH_SEL_##f }
  1530. #define _BUS(x) [ADV7604_BUS_ORDER_##x]
  1531. static const unsigned int op_ch_sel[6][6] = {
  1532. _BUS(RGB) /* NOP */ = _SEL(GBR, GRB, BGR, RGB, BRG, RBG),
  1533. _BUS(GRB) /* 1-2 */ = _SEL(BGR, RGB, GBR, GRB, RBG, BRG),
  1534. _BUS(RBG) /* 2-3 */ = _SEL(GRB, GBR, BRG, RBG, BGR, RGB),
  1535. _BUS(BGR) /* 1-3 */ = _SEL(RBG, BRG, RGB, BGR, GRB, GBR),
  1536. _BUS(BRG) /* ROR */ = _SEL(BRG, RBG, GRB, GBR, RGB, BGR),
  1537. _BUS(GBR) /* ROL */ = _SEL(RGB, BGR, RBG, BRG, GBR, GRB),
  1538. };
  1539. return op_ch_sel[state->pdata.bus_order][state->format->op_ch_sel >> 5];
  1540. }
  1541. static void adv76xx_setup_format(struct adv76xx_state *state)
  1542. {
  1543. struct v4l2_subdev *sd = &state->sd;
  1544. io_write_clr_set(sd, 0x02, 0x02,
  1545. state->format->rgb_out ? ADV76XX_RGB_OUT : 0);
  1546. io_write(sd, 0x03, state->format->op_format_sel |
  1547. state->pdata.op_format_mode_sel);
  1548. io_write_clr_set(sd, 0x04, 0xe0, adv76xx_op_ch_sel(state));
  1549. io_write_clr_set(sd, 0x05, 0x01,
  1550. state->format->swap_cb_cr ? ADV76XX_OP_SWAP_CB_CR : 0);
  1551. }
  1552. static int adv76xx_get_format(struct v4l2_subdev *sd,
  1553. struct v4l2_subdev_pad_config *cfg,
  1554. struct v4l2_subdev_format *format)
  1555. {
  1556. struct adv76xx_state *state = to_state(sd);
  1557. if (format->pad != state->source_pad)
  1558. return -EINVAL;
  1559. adv76xx_fill_format(state, &format->format);
  1560. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1561. struct v4l2_mbus_framefmt *fmt;
  1562. fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
  1563. format->format.code = fmt->code;
  1564. } else {
  1565. format->format.code = state->format->code;
  1566. }
  1567. return 0;
  1568. }
  1569. static int adv76xx_set_format(struct v4l2_subdev *sd,
  1570. struct v4l2_subdev_pad_config *cfg,
  1571. struct v4l2_subdev_format *format)
  1572. {
  1573. struct adv76xx_state *state = to_state(sd);
  1574. const struct adv76xx_format_info *info;
  1575. if (format->pad != state->source_pad)
  1576. return -EINVAL;
  1577. info = adv76xx_format_info(state, format->format.code);
  1578. if (info == NULL)
  1579. info = adv76xx_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
  1580. adv76xx_fill_format(state, &format->format);
  1581. format->format.code = info->code;
  1582. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  1583. struct v4l2_mbus_framefmt *fmt;
  1584. fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
  1585. fmt->code = format->format.code;
  1586. } else {
  1587. state->format = info;
  1588. adv76xx_setup_format(state);
  1589. }
  1590. return 0;
  1591. }
  1592. static int adv76xx_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  1593. {
  1594. struct adv76xx_state *state = to_state(sd);
  1595. const struct adv76xx_chip_info *info = state->info;
  1596. const u8 irq_reg_0x43 = io_read(sd, 0x43);
  1597. const u8 irq_reg_0x6b = io_read(sd, 0x6b);
  1598. const u8 irq_reg_0x70 = io_read(sd, 0x70);
  1599. u8 fmt_change_digital;
  1600. u8 fmt_change;
  1601. u8 tx_5v;
  1602. if (irq_reg_0x43)
  1603. io_write(sd, 0x44, irq_reg_0x43);
  1604. if (irq_reg_0x70)
  1605. io_write(sd, 0x71, irq_reg_0x70);
  1606. if (irq_reg_0x6b)
  1607. io_write(sd, 0x6c, irq_reg_0x6b);
  1608. v4l2_dbg(2, debug, sd, "%s: ", __func__);
  1609. /* format change */
  1610. fmt_change = irq_reg_0x43 & 0x98;
  1611. fmt_change_digital = is_digital_input(sd)
  1612. ? irq_reg_0x6b & info->fmt_change_digital_mask
  1613. : 0;
  1614. if (fmt_change || fmt_change_digital) {
  1615. v4l2_dbg(1, debug, sd,
  1616. "%s: fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
  1617. __func__, fmt_change, fmt_change_digital);
  1618. v4l2_subdev_notify_event(sd, &adv76xx_ev_fmt);
  1619. if (handled)
  1620. *handled = true;
  1621. }
  1622. /* HDMI/DVI mode */
  1623. if (irq_reg_0x6b & 0x01) {
  1624. v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
  1625. (io_read(sd, 0x6a) & 0x01) ? "HDMI" : "DVI");
  1626. set_rgb_quantization_range(sd);
  1627. if (handled)
  1628. *handled = true;
  1629. }
  1630. /* tx 5v detect */
  1631. tx_5v = irq_reg_0x70 & info->cable_det_mask;
  1632. if (tx_5v) {
  1633. v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
  1634. adv76xx_s_detect_tx_5v_ctrl(sd);
  1635. if (handled)
  1636. *handled = true;
  1637. }
  1638. return 0;
  1639. }
  1640. static int adv76xx_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  1641. {
  1642. struct adv76xx_state *state = to_state(sd);
  1643. u8 *data = NULL;
  1644. memset(edid->reserved, 0, sizeof(edid->reserved));
  1645. switch (edid->pad) {
  1646. case ADV76XX_PAD_HDMI_PORT_A:
  1647. case ADV7604_PAD_HDMI_PORT_B:
  1648. case ADV7604_PAD_HDMI_PORT_C:
  1649. case ADV7604_PAD_HDMI_PORT_D:
  1650. if (state->edid.present & (1 << edid->pad))
  1651. data = state->edid.edid;
  1652. break;
  1653. default:
  1654. return -EINVAL;
  1655. }
  1656. if (edid->start_block == 0 && edid->blocks == 0) {
  1657. edid->blocks = data ? state->edid.blocks : 0;
  1658. return 0;
  1659. }
  1660. if (data == NULL)
  1661. return -ENODATA;
  1662. if (edid->start_block >= state->edid.blocks)
  1663. return -EINVAL;
  1664. if (edid->start_block + edid->blocks > state->edid.blocks)
  1665. edid->blocks = state->edid.blocks - edid->start_block;
  1666. memcpy(edid->edid, data + edid->start_block * 128, edid->blocks * 128);
  1667. return 0;
  1668. }
  1669. static int get_edid_spa_location(const u8 *edid)
  1670. {
  1671. u8 d;
  1672. if ((edid[0x7e] != 1) ||
  1673. (edid[0x80] != 0x02) ||
  1674. (edid[0x81] != 0x03)) {
  1675. return -1;
  1676. }
  1677. /* search Vendor Specific Data Block (tag 3) */
  1678. d = edid[0x82] & 0x7f;
  1679. if (d > 4) {
  1680. int i = 0x84;
  1681. int end = 0x80 + d;
  1682. do {
  1683. u8 tag = edid[i] >> 5;
  1684. u8 len = edid[i] & 0x1f;
  1685. if ((tag == 3) && (len >= 5))
  1686. return i + 4;
  1687. i += len + 1;
  1688. } while (i < end);
  1689. }
  1690. return -1;
  1691. }
  1692. static int adv76xx_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  1693. {
  1694. struct adv76xx_state *state = to_state(sd);
  1695. const struct adv76xx_chip_info *info = state->info;
  1696. int spa_loc;
  1697. int err;
  1698. int i;
  1699. memset(edid->reserved, 0, sizeof(edid->reserved));
  1700. if (edid->pad > ADV7604_PAD_HDMI_PORT_D)
  1701. return -EINVAL;
  1702. if (edid->start_block != 0)
  1703. return -EINVAL;
  1704. if (edid->blocks == 0) {
  1705. /* Disable hotplug and I2C access to EDID RAM from DDC port */
  1706. state->edid.present &= ~(1 << edid->pad);
  1707. adv76xx_set_hpd(state, state->edid.present);
  1708. rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, state->edid.present);
  1709. /* Fall back to a 16:9 aspect ratio */
  1710. state->aspect_ratio.numerator = 16;
  1711. state->aspect_ratio.denominator = 9;
  1712. if (!state->edid.present)
  1713. state->edid.blocks = 0;
  1714. v4l2_dbg(2, debug, sd, "%s: clear EDID pad %d, edid.present = 0x%x\n",
  1715. __func__, edid->pad, state->edid.present);
  1716. return 0;
  1717. }
  1718. if (edid->blocks > 2) {
  1719. edid->blocks = 2;
  1720. return -E2BIG;
  1721. }
  1722. v4l2_dbg(2, debug, sd, "%s: write EDID pad %d, edid.present = 0x%x\n",
  1723. __func__, edid->pad, state->edid.present);
  1724. /* Disable hotplug and I2C access to EDID RAM from DDC port */
  1725. cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
  1726. adv76xx_set_hpd(state, 0);
  1727. rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, 0x00);
  1728. spa_loc = get_edid_spa_location(edid->edid);
  1729. if (spa_loc < 0)
  1730. spa_loc = 0xc0; /* Default value [REF_02, p. 116] */
  1731. switch (edid->pad) {
  1732. case ADV76XX_PAD_HDMI_PORT_A:
  1733. state->spa_port_a[0] = edid->edid[spa_loc];
  1734. state->spa_port_a[1] = edid->edid[spa_loc + 1];
  1735. break;
  1736. case ADV7604_PAD_HDMI_PORT_B:
  1737. rep_write(sd, 0x70, edid->edid[spa_loc]);
  1738. rep_write(sd, 0x71, edid->edid[spa_loc + 1]);
  1739. break;
  1740. case ADV7604_PAD_HDMI_PORT_C:
  1741. rep_write(sd, 0x72, edid->edid[spa_loc]);
  1742. rep_write(sd, 0x73, edid->edid[spa_loc + 1]);
  1743. break;
  1744. case ADV7604_PAD_HDMI_PORT_D:
  1745. rep_write(sd, 0x74, edid->edid[spa_loc]);
  1746. rep_write(sd, 0x75, edid->edid[spa_loc + 1]);
  1747. break;
  1748. default:
  1749. return -EINVAL;
  1750. }
  1751. if (info->type == ADV7604) {
  1752. rep_write(sd, 0x76, spa_loc & 0xff);
  1753. rep_write_clr_set(sd, 0x77, 0x40, (spa_loc & 0x100) >> 2);
  1754. } else {
  1755. /* FIXME: Where is the SPA location LSB register ? */
  1756. rep_write_clr_set(sd, 0x71, 0x01, (spa_loc & 0x100) >> 8);
  1757. }
  1758. edid->edid[spa_loc] = state->spa_port_a[0];
  1759. edid->edid[spa_loc + 1] = state->spa_port_a[1];
  1760. memcpy(state->edid.edid, edid->edid, 128 * edid->blocks);
  1761. state->edid.blocks = edid->blocks;
  1762. state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
  1763. edid->edid[0x16]);
  1764. state->edid.present |= 1 << edid->pad;
  1765. err = edid_write_block(sd, 128 * edid->blocks, state->edid.edid);
  1766. if (err < 0) {
  1767. v4l2_err(sd, "error %d writing edid pad %d\n", err, edid->pad);
  1768. return err;
  1769. }
  1770. /* adv76xx calculates the checksums and enables I2C access to internal
  1771. EDID RAM from DDC port. */
  1772. rep_write_clr_set(sd, info->edid_enable_reg, 0x0f, state->edid.present);
  1773. for (i = 0; i < 1000; i++) {
  1774. if (rep_read(sd, info->edid_status_reg) & state->edid.present)
  1775. break;
  1776. mdelay(1);
  1777. }
  1778. if (i == 1000) {
  1779. v4l2_err(sd, "error enabling edid (0x%x)\n", state->edid.present);
  1780. return -EIO;
  1781. }
  1782. /* enable hotplug after 100 ms */
  1783. queue_delayed_work(state->work_queues,
  1784. &state->delayed_work_enable_hotplug, HZ / 10);
  1785. return 0;
  1786. }
  1787. /*********** avi info frame CEA-861-E **************/
  1788. static const struct adv76xx_cfg_read_infoframe adv76xx_cri[] = {
  1789. { "AVI", 0x01, 0xe0, 0x00 },
  1790. { "Audio", 0x02, 0xe3, 0x1c },
  1791. { "SDP", 0x04, 0xe6, 0x2a },
  1792. { "Vendor", 0x10, 0xec, 0x54 }
  1793. };
  1794. static int adv76xx_read_infoframe(struct v4l2_subdev *sd, int index,
  1795. union hdmi_infoframe *frame)
  1796. {
  1797. uint8_t buffer[32];
  1798. u8 len;
  1799. int i;
  1800. if (!(io_read(sd, 0x60) & adv76xx_cri[index].present_mask)) {
  1801. v4l2_info(sd, "%s infoframe not received\n",
  1802. adv76xx_cri[index].desc);
  1803. return -ENOENT;
  1804. }
  1805. for (i = 0; i < 3; i++)
  1806. buffer[i] = infoframe_read(sd,
  1807. adv76xx_cri[index].head_addr + i);
  1808. len = buffer[2] + 1;
  1809. if (len + 3 > sizeof(buffer)) {
  1810. v4l2_err(sd, "%s: invalid %s infoframe length %d\n", __func__,
  1811. adv76xx_cri[index].desc, len);
  1812. return -ENOENT;
  1813. }
  1814. for (i = 0; i < len; i++)
  1815. buffer[i + 3] = infoframe_read(sd,
  1816. adv76xx_cri[index].payload_addr + i);
  1817. if (hdmi_infoframe_unpack(frame, buffer) < 0) {
  1818. v4l2_err(sd, "%s: unpack of %s infoframe failed\n", __func__,
  1819. adv76xx_cri[index].desc);
  1820. return -ENOENT;
  1821. }
  1822. return 0;
  1823. }
  1824. static void adv76xx_log_infoframes(struct v4l2_subdev *sd)
  1825. {
  1826. int i;
  1827. if (!is_hdmi(sd)) {
  1828. v4l2_info(sd, "receive DVI-D signal, no infoframes\n");
  1829. return;
  1830. }
  1831. for (i = 0; i < ARRAY_SIZE(adv76xx_cri); i++) {
  1832. union hdmi_infoframe frame;
  1833. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1834. if (adv76xx_read_infoframe(sd, i, &frame))
  1835. return;
  1836. hdmi_infoframe_log(KERN_INFO, &client->dev, &frame);
  1837. }
  1838. }
  1839. static int adv76xx_log_status(struct v4l2_subdev *sd)
  1840. {
  1841. struct adv76xx_state *state = to_state(sd);
  1842. const struct adv76xx_chip_info *info = state->info;
  1843. struct v4l2_dv_timings timings;
  1844. struct stdi_readback stdi;
  1845. u8 reg_io_0x02 = io_read(sd, 0x02);
  1846. u8 edid_enabled;
  1847. u8 cable_det;
  1848. static const char * const csc_coeff_sel_rb[16] = {
  1849. "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
  1850. "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
  1851. "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
  1852. "reserved", "reserved", "reserved", "reserved", "manual"
  1853. };
  1854. static const char * const input_color_space_txt[16] = {
  1855. "RGB limited range (16-235)", "RGB full range (0-255)",
  1856. "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
  1857. "xvYCC Bt.601", "xvYCC Bt.709",
  1858. "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
  1859. "invalid", "invalid", "invalid", "invalid", "invalid",
  1860. "invalid", "invalid", "automatic"
  1861. };
  1862. static const char * const hdmi_color_space_txt[16] = {
  1863. "RGB limited range (16-235)", "RGB full range (0-255)",
  1864. "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
  1865. "xvYCC Bt.601", "xvYCC Bt.709",
  1866. "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
  1867. "sYCC", "Adobe YCC 601", "AdobeRGB", "invalid", "invalid",
  1868. "invalid", "invalid", "invalid"
  1869. };
  1870. static const char * const rgb_quantization_range_txt[] = {
  1871. "Automatic",
  1872. "RGB limited range (16-235)",
  1873. "RGB full range (0-255)",
  1874. };
  1875. static const char * const deep_color_mode_txt[4] = {
  1876. "8-bits per channel",
  1877. "10-bits per channel",
  1878. "12-bits per channel",
  1879. "16-bits per channel (not supported)"
  1880. };
  1881. v4l2_info(sd, "-----Chip status-----\n");
  1882. v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
  1883. edid_enabled = rep_read(sd, info->edid_status_reg);
  1884. v4l2_info(sd, "EDID enabled port A: %s, B: %s, C: %s, D: %s\n",
  1885. ((edid_enabled & 0x01) ? "Yes" : "No"),
  1886. ((edid_enabled & 0x02) ? "Yes" : "No"),
  1887. ((edid_enabled & 0x04) ? "Yes" : "No"),
  1888. ((edid_enabled & 0x08) ? "Yes" : "No"));
  1889. v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
  1890. "enabled" : "disabled");
  1891. v4l2_info(sd, "-----Signal status-----\n");
  1892. cable_det = info->read_cable_det(sd);
  1893. v4l2_info(sd, "Cable detected (+5V power) port A: %s, B: %s, C: %s, D: %s\n",
  1894. ((cable_det & 0x01) ? "Yes" : "No"),
  1895. ((cable_det & 0x02) ? "Yes" : "No"),
  1896. ((cable_det & 0x04) ? "Yes" : "No"),
  1897. ((cable_det & 0x08) ? "Yes" : "No"));
  1898. v4l2_info(sd, "TMDS signal detected: %s\n",
  1899. no_signal_tmds(sd) ? "false" : "true");
  1900. v4l2_info(sd, "TMDS signal locked: %s\n",
  1901. no_lock_tmds(sd) ? "false" : "true");
  1902. v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
  1903. v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
  1904. v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
  1905. v4l2_info(sd, "CP free run: %s\n",
  1906. (in_free_run(sd)) ? "on" : "off");
  1907. v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
  1908. io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
  1909. (io_read(sd, 0x01) & 0x70) >> 4);
  1910. v4l2_info(sd, "-----Video Timings-----\n");
  1911. if (read_stdi(sd, &stdi))
  1912. v4l2_info(sd, "STDI: not locked\n");
  1913. else
  1914. v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
  1915. stdi.lcf, stdi.bl, stdi.lcvs,
  1916. stdi.interlaced ? "interlaced" : "progressive",
  1917. stdi.hs_pol, stdi.vs_pol);
  1918. if (adv76xx_query_dv_timings(sd, &timings))
  1919. v4l2_info(sd, "No video detected\n");
  1920. else
  1921. v4l2_print_dv_timings(sd->name, "Detected format: ",
  1922. &timings, true);
  1923. v4l2_print_dv_timings(sd->name, "Configured format: ",
  1924. &state->timings, true);
  1925. if (no_signal(sd))
  1926. return 0;
  1927. v4l2_info(sd, "-----Color space-----\n");
  1928. v4l2_info(sd, "RGB quantization range ctrl: %s\n",
  1929. rgb_quantization_range_txt[state->rgb_quantization_range]);
  1930. v4l2_info(sd, "Input color space: %s\n",
  1931. input_color_space_txt[reg_io_0x02 >> 4]);
  1932. v4l2_info(sd, "Output color space: %s %s, saturator %s, alt-gamma %s\n",
  1933. (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
  1934. (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
  1935. (((reg_io_0x02 >> 2) & 0x01) ^ (reg_io_0x02 & 0x01)) ?
  1936. "enabled" : "disabled",
  1937. (reg_io_0x02 & 0x08) ? "enabled" : "disabled");
  1938. v4l2_info(sd, "Color space conversion: %s\n",
  1939. csc_coeff_sel_rb[cp_read(sd, info->cp_csc) >> 4]);
  1940. if (!is_digital_input(sd))
  1941. return 0;
  1942. v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
  1943. v4l2_info(sd, "Digital video port selected: %c\n",
  1944. (hdmi_read(sd, 0x00) & 0x03) + 'A');
  1945. v4l2_info(sd, "HDCP encrypted content: %s\n",
  1946. (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
  1947. v4l2_info(sd, "HDCP keys read: %s%s\n",
  1948. (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
  1949. (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
  1950. if (is_hdmi(sd)) {
  1951. bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
  1952. bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
  1953. bool audio_mute = io_read(sd, 0x65) & 0x40;
  1954. v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
  1955. audio_pll_locked ? "locked" : "not locked",
  1956. audio_sample_packet_detect ? "detected" : "not detected",
  1957. audio_mute ? "muted" : "enabled");
  1958. if (audio_pll_locked && audio_sample_packet_detect) {
  1959. v4l2_info(sd, "Audio format: %s\n",
  1960. (hdmi_read(sd, 0x07) & 0x20) ? "multi-channel" : "stereo");
  1961. }
  1962. v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
  1963. (hdmi_read(sd, 0x5c) << 8) +
  1964. (hdmi_read(sd, 0x5d) & 0xf0));
  1965. v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
  1966. (hdmi_read(sd, 0x5e) << 8) +
  1967. hdmi_read(sd, 0x5f));
  1968. v4l2_info(sd, "AV Mute: %s\n", (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
  1969. v4l2_info(sd, "Deep color mode: %s\n", deep_color_mode_txt[(hdmi_read(sd, 0x0b) & 0x60) >> 5]);
  1970. v4l2_info(sd, "HDMI colorspace: %s\n", hdmi_color_space_txt[hdmi_read(sd, 0x53) & 0xf]);
  1971. adv76xx_log_infoframes(sd);
  1972. }
  1973. return 0;
  1974. }
  1975. static int adv76xx_subscribe_event(struct v4l2_subdev *sd,
  1976. struct v4l2_fh *fh,
  1977. struct v4l2_event_subscription *sub)
  1978. {
  1979. switch (sub->type) {
  1980. case V4L2_EVENT_SOURCE_CHANGE:
  1981. return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
  1982. case V4L2_EVENT_CTRL:
  1983. return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
  1984. default:
  1985. return -EINVAL;
  1986. }
  1987. }
  1988. /* ----------------------------------------------------------------------- */
  1989. static const struct v4l2_ctrl_ops adv76xx_ctrl_ops = {
  1990. .s_ctrl = adv76xx_s_ctrl,
  1991. };
  1992. static const struct v4l2_subdev_core_ops adv76xx_core_ops = {
  1993. .log_status = adv76xx_log_status,
  1994. .interrupt_service_routine = adv76xx_isr,
  1995. .subscribe_event = adv76xx_subscribe_event,
  1996. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1997. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1998. .g_register = adv76xx_g_register,
  1999. .s_register = adv76xx_s_register,
  2000. #endif
  2001. };
  2002. static const struct v4l2_subdev_video_ops adv76xx_video_ops = {
  2003. .s_routing = adv76xx_s_routing,
  2004. .g_input_status = adv76xx_g_input_status,
  2005. .s_dv_timings = adv76xx_s_dv_timings,
  2006. .g_dv_timings = adv76xx_g_dv_timings,
  2007. .query_dv_timings = adv76xx_query_dv_timings,
  2008. };
  2009. static const struct v4l2_subdev_pad_ops adv76xx_pad_ops = {
  2010. .enum_mbus_code = adv76xx_enum_mbus_code,
  2011. .get_fmt = adv76xx_get_format,
  2012. .set_fmt = adv76xx_set_format,
  2013. .get_edid = adv76xx_get_edid,
  2014. .set_edid = adv76xx_set_edid,
  2015. .dv_timings_cap = adv76xx_dv_timings_cap,
  2016. .enum_dv_timings = adv76xx_enum_dv_timings,
  2017. };
  2018. static const struct v4l2_subdev_ops adv76xx_ops = {
  2019. .core = &adv76xx_core_ops,
  2020. .video = &adv76xx_video_ops,
  2021. .pad = &adv76xx_pad_ops,
  2022. };
  2023. /* -------------------------- custom ctrls ---------------------------------- */
  2024. static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
  2025. .ops = &adv76xx_ctrl_ops,
  2026. .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
  2027. .name = "Analog Sampling Phase",
  2028. .type = V4L2_CTRL_TYPE_INTEGER,
  2029. .min = 0,
  2030. .max = 0x1f,
  2031. .step = 1,
  2032. .def = 0,
  2033. };
  2034. static const struct v4l2_ctrl_config adv76xx_ctrl_free_run_color_manual = {
  2035. .ops = &adv76xx_ctrl_ops,
  2036. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
  2037. .name = "Free Running Color, Manual",
  2038. .type = V4L2_CTRL_TYPE_BOOLEAN,
  2039. .min = false,
  2040. .max = true,
  2041. .step = 1,
  2042. .def = false,
  2043. };
  2044. static const struct v4l2_ctrl_config adv76xx_ctrl_free_run_color = {
  2045. .ops = &adv76xx_ctrl_ops,
  2046. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
  2047. .name = "Free Running Color",
  2048. .type = V4L2_CTRL_TYPE_INTEGER,
  2049. .min = 0x0,
  2050. .max = 0xffffff,
  2051. .step = 0x1,
  2052. .def = 0x0,
  2053. };
  2054. /* ----------------------------------------------------------------------- */
  2055. static int adv76xx_core_init(struct v4l2_subdev *sd)
  2056. {
  2057. struct adv76xx_state *state = to_state(sd);
  2058. const struct adv76xx_chip_info *info = state->info;
  2059. struct adv76xx_platform_data *pdata = &state->pdata;
  2060. hdmi_write(sd, 0x48,
  2061. (pdata->disable_pwrdnb ? 0x80 : 0) |
  2062. (pdata->disable_cable_det_rst ? 0x40 : 0));
  2063. disable_input(sd);
  2064. if (pdata->default_input >= 0 &&
  2065. pdata->default_input < state->source_pad) {
  2066. state->selected_input = pdata->default_input;
  2067. select_input(sd);
  2068. enable_input(sd);
  2069. }
  2070. /* power */
  2071. io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
  2072. io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
  2073. cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
  2074. /* video format */
  2075. io_write_clr_set(sd, 0x02, 0x0f,
  2076. pdata->alt_gamma << 3 |
  2077. pdata->op_656_range << 2 |
  2078. pdata->alt_data_sat << 0);
  2079. io_write_clr_set(sd, 0x05, 0x0e, pdata->blank_data << 3 |
  2080. pdata->insert_av_codes << 2 |
  2081. pdata->replicate_av_codes << 1);
  2082. adv76xx_setup_format(state);
  2083. cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
  2084. /* VS, HS polarities */
  2085. io_write(sd, 0x06, 0xa0 | pdata->inv_vs_pol << 2 |
  2086. pdata->inv_hs_pol << 1 | pdata->inv_llc_pol);
  2087. /* Adjust drive strength */
  2088. io_write(sd, 0x14, 0x40 | pdata->dr_str_data << 4 |
  2089. pdata->dr_str_clk << 2 |
  2090. pdata->dr_str_sync);
  2091. cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
  2092. cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
  2093. cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
  2094. ADI recommended setting [REF_01, c. 2.3.3] */
  2095. cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
  2096. ADI recommended setting [REF_01, c. 2.3.3] */
  2097. cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
  2098. for digital formats */
  2099. /* HDMI audio */
  2100. hdmi_write_clr_set(sd, 0x15, 0x03, 0x03); /* Mute on FIFO over-/underflow [REF_01, c. 1.2.18] */
  2101. hdmi_write_clr_set(sd, 0x1a, 0x0e, 0x08); /* Wait 1 s before unmute */
  2102. hdmi_write_clr_set(sd, 0x68, 0x06, 0x06); /* FIFO reset on over-/underflow [REF_01, c. 1.2.19] */
  2103. /* TODO from platform data */
  2104. afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
  2105. if (adv76xx_has_afe(state)) {
  2106. afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
  2107. io_write_clr_set(sd, 0x30, 1 << 4, pdata->output_bus_lsb_to_msb << 4);
  2108. }
  2109. /* interrupts */
  2110. io_write(sd, 0x40, 0xc0 | pdata->int1_config); /* Configure INT1 */
  2111. io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
  2112. io_write(sd, 0x6e, info->fmt_change_digital_mask); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
  2113. io_write(sd, 0x73, info->cable_det_mask); /* Enable cable detection (+5v) interrupts */
  2114. info->setup_irqs(sd);
  2115. return v4l2_ctrl_handler_setup(sd->ctrl_handler);
  2116. }
  2117. static void adv7604_setup_irqs(struct v4l2_subdev *sd)
  2118. {
  2119. io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
  2120. }
  2121. static void adv7611_setup_irqs(struct v4l2_subdev *sd)
  2122. {
  2123. io_write(sd, 0x41, 0xd0); /* STDI irq for any change, disable INT2 */
  2124. }
  2125. static void adv7612_setup_irqs(struct v4l2_subdev *sd)
  2126. {
  2127. io_write(sd, 0x41, 0xd0); /* disable INT2 */
  2128. }
  2129. static void adv76xx_unregister_clients(struct adv76xx_state *state)
  2130. {
  2131. unsigned int i;
  2132. for (i = 1; i < ARRAY_SIZE(state->i2c_clients); ++i) {
  2133. if (state->i2c_clients[i])
  2134. i2c_unregister_device(state->i2c_clients[i]);
  2135. }
  2136. }
  2137. static struct i2c_client *adv76xx_dummy_client(struct v4l2_subdev *sd,
  2138. u8 addr, u8 io_reg)
  2139. {
  2140. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2141. if (addr)
  2142. io_write(sd, io_reg, addr << 1);
  2143. return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
  2144. }
  2145. static const struct adv76xx_reg_seq adv7604_recommended_settings_afe[] = {
  2146. /* reset ADI recommended settings for HDMI: */
  2147. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
  2148. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
  2149. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x0d), 0x04 }, /* HDMI filter optimization */
  2150. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x3d), 0x00 }, /* DDC bus active pull-up control */
  2151. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x3e), 0x74 }, /* TMDS PLL optimization */
  2152. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
  2153. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x57), 0x74 }, /* TMDS PLL optimization */
  2154. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x58), 0x63 }, /* TMDS PLL optimization */
  2155. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
  2156. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
  2157. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x93), 0x88 }, /* equaliser */
  2158. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x94), 0x2e }, /* equaliser */
  2159. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x96), 0x00 }, /* enable automatic EQ changing */
  2160. /* set ADI recommended settings for digitizer */
  2161. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
  2162. { ADV76XX_REG(ADV76XX_PAGE_AFE, 0x12), 0x7b }, /* ADC noise shaping filter controls */
  2163. { ADV76XX_REG(ADV76XX_PAGE_AFE, 0x0c), 0x1f }, /* CP core gain controls */
  2164. { ADV76XX_REG(ADV76XX_PAGE_CP, 0x3e), 0x04 }, /* CP core pre-gain control */
  2165. { ADV76XX_REG(ADV76XX_PAGE_CP, 0xc3), 0x39 }, /* CP coast control. Graphics mode */
  2166. { ADV76XX_REG(ADV76XX_PAGE_CP, 0x40), 0x5c }, /* CP core pre-gain control. Graphics mode */
  2167. { ADV76XX_REG_SEQ_TERM, 0 },
  2168. };
  2169. static const struct adv76xx_reg_seq adv7604_recommended_settings_hdmi[] = {
  2170. /* set ADI recommended settings for HDMI: */
  2171. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
  2172. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x0d), 0x84 }, /* HDMI filter optimization */
  2173. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x3d), 0x10 }, /* DDC bus active pull-up control */
  2174. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x3e), 0x39 }, /* TMDS PLL optimization */
  2175. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x4e), 0x3b }, /* TMDS PLL optimization */
  2176. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x57), 0xb6 }, /* TMDS PLL optimization */
  2177. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x58), 0x03 }, /* TMDS PLL optimization */
  2178. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x8d), 0x18 }, /* equaliser */
  2179. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x8e), 0x34 }, /* equaliser */
  2180. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x93), 0x8b }, /* equaliser */
  2181. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x94), 0x2d }, /* equaliser */
  2182. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x96), 0x01 }, /* enable automatic EQ changing */
  2183. /* reset ADI recommended settings for digitizer */
  2184. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
  2185. { ADV76XX_REG(ADV76XX_PAGE_AFE, 0x12), 0xfb }, /* ADC noise shaping filter controls */
  2186. { ADV76XX_REG(ADV76XX_PAGE_AFE, 0x0c), 0x0d }, /* CP core gain controls */
  2187. { ADV76XX_REG_SEQ_TERM, 0 },
  2188. };
  2189. static const struct adv76xx_reg_seq adv7611_recommended_settings_hdmi[] = {
  2190. /* ADV7611 Register Settings Recommendations Rev 1.5, May 2014 */
  2191. { ADV76XX_REG(ADV76XX_PAGE_CP, 0x6c), 0x00 },
  2192. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x9b), 0x03 },
  2193. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x6f), 0x08 },
  2194. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x85), 0x1f },
  2195. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x87), 0x70 },
  2196. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x57), 0xda },
  2197. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x58), 0x01 },
  2198. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x03), 0x98 },
  2199. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x4c), 0x44 },
  2200. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x8d), 0x04 },
  2201. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x8e), 0x1e },
  2202. { ADV76XX_REG_SEQ_TERM, 0 },
  2203. };
  2204. static const struct adv76xx_reg_seq adv7612_recommended_settings_hdmi[] = {
  2205. { ADV76XX_REG(ADV76XX_PAGE_CP, 0x6c), 0x00 },
  2206. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x9b), 0x03 },
  2207. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x6f), 0x08 },
  2208. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x85), 0x1f },
  2209. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x87), 0x70 },
  2210. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x57), 0xda },
  2211. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x58), 0x01 },
  2212. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x03), 0x98 },
  2213. { ADV76XX_REG(ADV76XX_PAGE_HDMI, 0x4c), 0x44 },
  2214. { ADV76XX_REG_SEQ_TERM, 0 },
  2215. };
  2216. static const struct adv76xx_chip_info adv76xx_chip_info[] = {
  2217. [ADV7604] = {
  2218. .type = ADV7604,
  2219. .has_afe = true,
  2220. .max_port = ADV7604_PAD_VGA_COMP,
  2221. .num_dv_ports = 4,
  2222. .edid_enable_reg = 0x77,
  2223. .edid_status_reg = 0x7d,
  2224. .lcf_reg = 0xb3,
  2225. .tdms_lock_mask = 0xe0,
  2226. .cable_det_mask = 0x1e,
  2227. .fmt_change_digital_mask = 0xc1,
  2228. .cp_csc = 0xfc,
  2229. .formats = adv7604_formats,
  2230. .nformats = ARRAY_SIZE(adv7604_formats),
  2231. .set_termination = adv7604_set_termination,
  2232. .setup_irqs = adv7604_setup_irqs,
  2233. .read_hdmi_pixelclock = adv7604_read_hdmi_pixelclock,
  2234. .read_cable_det = adv7604_read_cable_det,
  2235. .recommended_settings = {
  2236. [0] = adv7604_recommended_settings_afe,
  2237. [1] = adv7604_recommended_settings_hdmi,
  2238. },
  2239. .num_recommended_settings = {
  2240. [0] = ARRAY_SIZE(adv7604_recommended_settings_afe),
  2241. [1] = ARRAY_SIZE(adv7604_recommended_settings_hdmi),
  2242. },
  2243. .page_mask = BIT(ADV76XX_PAGE_IO) | BIT(ADV7604_PAGE_AVLINK) |
  2244. BIT(ADV76XX_PAGE_CEC) | BIT(ADV76XX_PAGE_INFOFRAME) |
  2245. BIT(ADV7604_PAGE_ESDP) | BIT(ADV7604_PAGE_DPP) |
  2246. BIT(ADV76XX_PAGE_AFE) | BIT(ADV76XX_PAGE_REP) |
  2247. BIT(ADV76XX_PAGE_EDID) | BIT(ADV76XX_PAGE_HDMI) |
  2248. BIT(ADV76XX_PAGE_TEST) | BIT(ADV76XX_PAGE_CP) |
  2249. BIT(ADV7604_PAGE_VDP),
  2250. .linewidth_mask = 0xfff,
  2251. .field0_height_mask = 0xfff,
  2252. .field1_height_mask = 0xfff,
  2253. .hfrontporch_mask = 0x3ff,
  2254. .hsync_mask = 0x3ff,
  2255. .hbackporch_mask = 0x3ff,
  2256. .field0_vfrontporch_mask = 0x1fff,
  2257. .field0_vsync_mask = 0x1fff,
  2258. .field0_vbackporch_mask = 0x1fff,
  2259. .field1_vfrontporch_mask = 0x1fff,
  2260. .field1_vsync_mask = 0x1fff,
  2261. .field1_vbackporch_mask = 0x1fff,
  2262. },
  2263. [ADV7611] = {
  2264. .type = ADV7611,
  2265. .has_afe = false,
  2266. .max_port = ADV76XX_PAD_HDMI_PORT_A,
  2267. .num_dv_ports = 1,
  2268. .edid_enable_reg = 0x74,
  2269. .edid_status_reg = 0x76,
  2270. .lcf_reg = 0xa3,
  2271. .tdms_lock_mask = 0x43,
  2272. .cable_det_mask = 0x01,
  2273. .fmt_change_digital_mask = 0x03,
  2274. .cp_csc = 0xf4,
  2275. .formats = adv7611_formats,
  2276. .nformats = ARRAY_SIZE(adv7611_formats),
  2277. .set_termination = adv7611_set_termination,
  2278. .setup_irqs = adv7611_setup_irqs,
  2279. .read_hdmi_pixelclock = adv7611_read_hdmi_pixelclock,
  2280. .read_cable_det = adv7611_read_cable_det,
  2281. .recommended_settings = {
  2282. [1] = adv7611_recommended_settings_hdmi,
  2283. },
  2284. .num_recommended_settings = {
  2285. [1] = ARRAY_SIZE(adv7611_recommended_settings_hdmi),
  2286. },
  2287. .page_mask = BIT(ADV76XX_PAGE_IO) | BIT(ADV76XX_PAGE_CEC) |
  2288. BIT(ADV76XX_PAGE_INFOFRAME) | BIT(ADV76XX_PAGE_AFE) |
  2289. BIT(ADV76XX_PAGE_REP) | BIT(ADV76XX_PAGE_EDID) |
  2290. BIT(ADV76XX_PAGE_HDMI) | BIT(ADV76XX_PAGE_CP),
  2291. .linewidth_mask = 0x1fff,
  2292. .field0_height_mask = 0x1fff,
  2293. .field1_height_mask = 0x1fff,
  2294. .hfrontporch_mask = 0x1fff,
  2295. .hsync_mask = 0x1fff,
  2296. .hbackporch_mask = 0x1fff,
  2297. .field0_vfrontporch_mask = 0x3fff,
  2298. .field0_vsync_mask = 0x3fff,
  2299. .field0_vbackporch_mask = 0x3fff,
  2300. .field1_vfrontporch_mask = 0x3fff,
  2301. .field1_vsync_mask = 0x3fff,
  2302. .field1_vbackporch_mask = 0x3fff,
  2303. },
  2304. [ADV7612] = {
  2305. .type = ADV7612,
  2306. .has_afe = false,
  2307. .max_port = ADV76XX_PAD_HDMI_PORT_A, /* B not supported */
  2308. .num_dv_ports = 1, /* normally 2 */
  2309. .edid_enable_reg = 0x74,
  2310. .edid_status_reg = 0x76,
  2311. .lcf_reg = 0xa3,
  2312. .tdms_lock_mask = 0x43,
  2313. .cable_det_mask = 0x01,
  2314. .fmt_change_digital_mask = 0x03,
  2315. .cp_csc = 0xf4,
  2316. .formats = adv7612_formats,
  2317. .nformats = ARRAY_SIZE(adv7612_formats),
  2318. .set_termination = adv7611_set_termination,
  2319. .setup_irqs = adv7612_setup_irqs,
  2320. .read_hdmi_pixelclock = adv7611_read_hdmi_pixelclock,
  2321. .read_cable_det = adv7612_read_cable_det,
  2322. .recommended_settings = {
  2323. [1] = adv7612_recommended_settings_hdmi,
  2324. },
  2325. .num_recommended_settings = {
  2326. [1] = ARRAY_SIZE(adv7612_recommended_settings_hdmi),
  2327. },
  2328. .page_mask = BIT(ADV76XX_PAGE_IO) | BIT(ADV76XX_PAGE_CEC) |
  2329. BIT(ADV76XX_PAGE_INFOFRAME) | BIT(ADV76XX_PAGE_AFE) |
  2330. BIT(ADV76XX_PAGE_REP) | BIT(ADV76XX_PAGE_EDID) |
  2331. BIT(ADV76XX_PAGE_HDMI) | BIT(ADV76XX_PAGE_CP),
  2332. .linewidth_mask = 0x1fff,
  2333. .field0_height_mask = 0x1fff,
  2334. .field1_height_mask = 0x1fff,
  2335. .hfrontporch_mask = 0x1fff,
  2336. .hsync_mask = 0x1fff,
  2337. .hbackporch_mask = 0x1fff,
  2338. .field0_vfrontporch_mask = 0x3fff,
  2339. .field0_vsync_mask = 0x3fff,
  2340. .field0_vbackporch_mask = 0x3fff,
  2341. .field1_vfrontporch_mask = 0x3fff,
  2342. .field1_vsync_mask = 0x3fff,
  2343. .field1_vbackporch_mask = 0x3fff,
  2344. },
  2345. };
  2346. static const struct i2c_device_id adv76xx_i2c_id[] = {
  2347. { "adv7604", (kernel_ulong_t)&adv76xx_chip_info[ADV7604] },
  2348. { "adv7611", (kernel_ulong_t)&adv76xx_chip_info[ADV7611] },
  2349. { "adv7612", (kernel_ulong_t)&adv76xx_chip_info[ADV7612] },
  2350. { }
  2351. };
  2352. MODULE_DEVICE_TABLE(i2c, adv76xx_i2c_id);
  2353. static const struct of_device_id adv76xx_of_id[] __maybe_unused = {
  2354. { .compatible = "adi,adv7611", .data = &adv76xx_chip_info[ADV7611] },
  2355. { .compatible = "adi,adv7612", .data = &adv76xx_chip_info[ADV7612] },
  2356. { }
  2357. };
  2358. MODULE_DEVICE_TABLE(of, adv76xx_of_id);
  2359. static int adv76xx_parse_dt(struct adv76xx_state *state)
  2360. {
  2361. struct v4l2_of_endpoint bus_cfg;
  2362. struct device_node *endpoint;
  2363. struct device_node *np;
  2364. unsigned int flags;
  2365. u32 v;
  2366. np = state->i2c_clients[ADV76XX_PAGE_IO]->dev.of_node;
  2367. /* Parse the endpoint. */
  2368. endpoint = of_graph_get_next_endpoint(np, NULL);
  2369. if (!endpoint)
  2370. return -EINVAL;
  2371. v4l2_of_parse_endpoint(endpoint, &bus_cfg);
  2372. if (!of_property_read_u32(endpoint, "default-input", &v))
  2373. state->pdata.default_input = v;
  2374. else
  2375. state->pdata.default_input = -1;
  2376. of_node_put(endpoint);
  2377. flags = bus_cfg.bus.parallel.flags;
  2378. if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  2379. state->pdata.inv_hs_pol = 1;
  2380. if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  2381. state->pdata.inv_vs_pol = 1;
  2382. if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  2383. state->pdata.inv_llc_pol = 1;
  2384. if (bus_cfg.bus_type == V4L2_MBUS_BT656) {
  2385. state->pdata.insert_av_codes = 1;
  2386. state->pdata.op_656_range = 1;
  2387. }
  2388. /* Disable the interrupt for now as no DT-based board uses it. */
  2389. state->pdata.int1_config = ADV76XX_INT1_CONFIG_DISABLED;
  2390. /* Use the default I2C addresses. */
  2391. state->pdata.i2c_addresses[ADV7604_PAGE_AVLINK] = 0x42;
  2392. state->pdata.i2c_addresses[ADV76XX_PAGE_CEC] = 0x40;
  2393. state->pdata.i2c_addresses[ADV76XX_PAGE_INFOFRAME] = 0x3e;
  2394. state->pdata.i2c_addresses[ADV7604_PAGE_ESDP] = 0x38;
  2395. state->pdata.i2c_addresses[ADV7604_PAGE_DPP] = 0x3c;
  2396. state->pdata.i2c_addresses[ADV76XX_PAGE_AFE] = 0x26;
  2397. state->pdata.i2c_addresses[ADV76XX_PAGE_REP] = 0x32;
  2398. state->pdata.i2c_addresses[ADV76XX_PAGE_EDID] = 0x36;
  2399. state->pdata.i2c_addresses[ADV76XX_PAGE_HDMI] = 0x34;
  2400. state->pdata.i2c_addresses[ADV76XX_PAGE_TEST] = 0x30;
  2401. state->pdata.i2c_addresses[ADV76XX_PAGE_CP] = 0x22;
  2402. state->pdata.i2c_addresses[ADV7604_PAGE_VDP] = 0x24;
  2403. /* Hardcode the remaining platform data fields. */
  2404. state->pdata.disable_pwrdnb = 0;
  2405. state->pdata.disable_cable_det_rst = 0;
  2406. state->pdata.blank_data = 1;
  2407. state->pdata.alt_data_sat = 1;
  2408. state->pdata.op_format_mode_sel = ADV7604_OP_FORMAT_MODE0;
  2409. state->pdata.bus_order = ADV7604_BUS_ORDER_RGB;
  2410. state->pdata.dr_str_data = ADV76XX_DR_STR_MEDIUM_HIGH;
  2411. state->pdata.dr_str_clk = ADV76XX_DR_STR_MEDIUM_HIGH;
  2412. state->pdata.dr_str_sync = ADV76XX_DR_STR_MEDIUM_HIGH;
  2413. return 0;
  2414. }
  2415. static const struct regmap_config adv76xx_regmap_cnf[] = {
  2416. {
  2417. .name = "io",
  2418. .reg_bits = 8,
  2419. .val_bits = 8,
  2420. .max_register = 0xff,
  2421. .cache_type = REGCACHE_NONE,
  2422. },
  2423. {
  2424. .name = "avlink",
  2425. .reg_bits = 8,
  2426. .val_bits = 8,
  2427. .max_register = 0xff,
  2428. .cache_type = REGCACHE_NONE,
  2429. },
  2430. {
  2431. .name = "cec",
  2432. .reg_bits = 8,
  2433. .val_bits = 8,
  2434. .max_register = 0xff,
  2435. .cache_type = REGCACHE_NONE,
  2436. },
  2437. {
  2438. .name = "infoframe",
  2439. .reg_bits = 8,
  2440. .val_bits = 8,
  2441. .max_register = 0xff,
  2442. .cache_type = REGCACHE_NONE,
  2443. },
  2444. {
  2445. .name = "esdp",
  2446. .reg_bits = 8,
  2447. .val_bits = 8,
  2448. .max_register = 0xff,
  2449. .cache_type = REGCACHE_NONE,
  2450. },
  2451. {
  2452. .name = "epp",
  2453. .reg_bits = 8,
  2454. .val_bits = 8,
  2455. .max_register = 0xff,
  2456. .cache_type = REGCACHE_NONE,
  2457. },
  2458. {
  2459. .name = "afe",
  2460. .reg_bits = 8,
  2461. .val_bits = 8,
  2462. .max_register = 0xff,
  2463. .cache_type = REGCACHE_NONE,
  2464. },
  2465. {
  2466. .name = "rep",
  2467. .reg_bits = 8,
  2468. .val_bits = 8,
  2469. .max_register = 0xff,
  2470. .cache_type = REGCACHE_NONE,
  2471. },
  2472. {
  2473. .name = "edid",
  2474. .reg_bits = 8,
  2475. .val_bits = 8,
  2476. .max_register = 0xff,
  2477. .cache_type = REGCACHE_NONE,
  2478. },
  2479. {
  2480. .name = "hdmi",
  2481. .reg_bits = 8,
  2482. .val_bits = 8,
  2483. .max_register = 0xff,
  2484. .cache_type = REGCACHE_NONE,
  2485. },
  2486. {
  2487. .name = "test",
  2488. .reg_bits = 8,
  2489. .val_bits = 8,
  2490. .max_register = 0xff,
  2491. .cache_type = REGCACHE_NONE,
  2492. },
  2493. {
  2494. .name = "cp",
  2495. .reg_bits = 8,
  2496. .val_bits = 8,
  2497. .max_register = 0xff,
  2498. .cache_type = REGCACHE_NONE,
  2499. },
  2500. {
  2501. .name = "vdp",
  2502. .reg_bits = 8,
  2503. .val_bits = 8,
  2504. .max_register = 0xff,
  2505. .cache_type = REGCACHE_NONE,
  2506. },
  2507. };
  2508. static int configure_regmap(struct adv76xx_state *state, int region)
  2509. {
  2510. int err;
  2511. if (!state->i2c_clients[region])
  2512. return -ENODEV;
  2513. state->regmap[region] =
  2514. devm_regmap_init_i2c(state->i2c_clients[region],
  2515. &adv76xx_regmap_cnf[region]);
  2516. if (IS_ERR(state->regmap[region])) {
  2517. err = PTR_ERR(state->regmap[region]);
  2518. v4l_err(state->i2c_clients[region],
  2519. "Error initializing regmap %d with error %d\n",
  2520. region, err);
  2521. return -EINVAL;
  2522. }
  2523. return 0;
  2524. }
  2525. static int configure_regmaps(struct adv76xx_state *state)
  2526. {
  2527. int i, err;
  2528. for (i = ADV7604_PAGE_AVLINK ; i < ADV76XX_PAGE_MAX; i++) {
  2529. err = configure_regmap(state, i);
  2530. if (err && (err != -ENODEV))
  2531. return err;
  2532. }
  2533. return 0;
  2534. }
  2535. static int adv76xx_probe(struct i2c_client *client,
  2536. const struct i2c_device_id *id)
  2537. {
  2538. static const struct v4l2_dv_timings cea640x480 =
  2539. V4L2_DV_BT_CEA_640X480P59_94;
  2540. struct adv76xx_state *state;
  2541. struct v4l2_ctrl_handler *hdl;
  2542. struct v4l2_subdev *sd;
  2543. unsigned int i;
  2544. unsigned int val, val2;
  2545. int err;
  2546. /* Check if the adapter supports the needed features */
  2547. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  2548. return -EIO;
  2549. v4l_dbg(1, debug, client, "detecting adv76xx client on address 0x%x\n",
  2550. client->addr << 1);
  2551. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  2552. if (!state) {
  2553. v4l_err(client, "Could not allocate adv76xx_state memory!\n");
  2554. return -ENOMEM;
  2555. }
  2556. state->i2c_clients[ADV76XX_PAGE_IO] = client;
  2557. /* initialize variables */
  2558. state->restart_stdi_once = true;
  2559. state->selected_input = ~0;
  2560. if (IS_ENABLED(CONFIG_OF) && client->dev.of_node) {
  2561. const struct of_device_id *oid;
  2562. oid = of_match_node(adv76xx_of_id, client->dev.of_node);
  2563. state->info = oid->data;
  2564. err = adv76xx_parse_dt(state);
  2565. if (err < 0) {
  2566. v4l_err(client, "DT parsing error\n");
  2567. return err;
  2568. }
  2569. } else if (client->dev.platform_data) {
  2570. struct adv76xx_platform_data *pdata = client->dev.platform_data;
  2571. state->info = (const struct adv76xx_chip_info *)id->driver_data;
  2572. state->pdata = *pdata;
  2573. } else {
  2574. v4l_err(client, "No platform data!\n");
  2575. return -ENODEV;
  2576. }
  2577. /* Request GPIOs. */
  2578. for (i = 0; i < state->info->num_dv_ports; ++i) {
  2579. state->hpd_gpio[i] =
  2580. devm_gpiod_get_index_optional(&client->dev, "hpd", i,
  2581. GPIOD_OUT_LOW);
  2582. if (IS_ERR(state->hpd_gpio[i]))
  2583. return PTR_ERR(state->hpd_gpio[i]);
  2584. if (state->hpd_gpio[i])
  2585. v4l_info(client, "Handling HPD %u GPIO\n", i);
  2586. }
  2587. state->timings = cea640x480;
  2588. state->format = adv76xx_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
  2589. sd = &state->sd;
  2590. v4l2_i2c_subdev_init(sd, client, &adv76xx_ops);
  2591. snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
  2592. id->name, i2c_adapter_id(client->adapter),
  2593. client->addr);
  2594. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  2595. /* Configure IO Regmap region */
  2596. err = configure_regmap(state, ADV76XX_PAGE_IO);
  2597. if (err) {
  2598. v4l2_err(sd, "Error configuring IO regmap region\n");
  2599. return -ENODEV;
  2600. }
  2601. /*
  2602. * Verify that the chip is present. On ADV7604 the RD_INFO register only
  2603. * identifies the revision, while on ADV7611 it identifies the model as
  2604. * well. Use the HDMI slave address on ADV7604 and RD_INFO on ADV7611.
  2605. */
  2606. switch (state->info->type) {
  2607. case ADV7604:
  2608. err = regmap_read(state->regmap[ADV76XX_PAGE_IO], 0xfb, &val);
  2609. if (err) {
  2610. v4l2_err(sd, "Error %d reading IO Regmap\n", err);
  2611. return -ENODEV;
  2612. }
  2613. if (val != 0x68) {
  2614. v4l2_err(sd, "not an adv7604 on address 0x%x\n",
  2615. client->addr << 1);
  2616. return -ENODEV;
  2617. }
  2618. break;
  2619. case ADV7611:
  2620. case ADV7612:
  2621. err = regmap_read(state->regmap[ADV76XX_PAGE_IO],
  2622. 0xea,
  2623. &val);
  2624. if (err) {
  2625. v4l2_err(sd, "Error %d reading IO Regmap\n", err);
  2626. return -ENODEV;
  2627. }
  2628. val2 = val << 8;
  2629. err = regmap_read(state->regmap[ADV76XX_PAGE_IO],
  2630. 0xeb,
  2631. &val);
  2632. if (err) {
  2633. v4l2_err(sd, "Error %d reading IO Regmap\n", err);
  2634. return -ENODEV;
  2635. }
  2636. val |= val2;
  2637. if ((state->info->type == ADV7611 && val != 0x2051) ||
  2638. (state->info->type == ADV7612 && val != 0x2041)) {
  2639. v4l2_err(sd, "not an adv761x on address 0x%x\n",
  2640. client->addr << 1);
  2641. return -ENODEV;
  2642. }
  2643. break;
  2644. }
  2645. /* control handlers */
  2646. hdl = &state->hdl;
  2647. v4l2_ctrl_handler_init(hdl, adv76xx_has_afe(state) ? 9 : 8);
  2648. v4l2_ctrl_new_std(hdl, &adv76xx_ctrl_ops,
  2649. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  2650. v4l2_ctrl_new_std(hdl, &adv76xx_ctrl_ops,
  2651. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  2652. v4l2_ctrl_new_std(hdl, &adv76xx_ctrl_ops,
  2653. V4L2_CID_SATURATION, 0, 255, 1, 128);
  2654. v4l2_ctrl_new_std(hdl, &adv76xx_ctrl_ops,
  2655. V4L2_CID_HUE, 0, 128, 1, 0);
  2656. /* private controls */
  2657. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  2658. V4L2_CID_DV_RX_POWER_PRESENT, 0,
  2659. (1 << state->info->num_dv_ports) - 1, 0, 0);
  2660. state->rgb_quantization_range_ctrl =
  2661. v4l2_ctrl_new_std_menu(hdl, &adv76xx_ctrl_ops,
  2662. V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
  2663. 0, V4L2_DV_RGB_RANGE_AUTO);
  2664. /* custom controls */
  2665. if (adv76xx_has_afe(state))
  2666. state->analog_sampling_phase_ctrl =
  2667. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
  2668. state->free_run_color_manual_ctrl =
  2669. v4l2_ctrl_new_custom(hdl, &adv76xx_ctrl_free_run_color_manual, NULL);
  2670. state->free_run_color_ctrl =
  2671. v4l2_ctrl_new_custom(hdl, &adv76xx_ctrl_free_run_color, NULL);
  2672. sd->ctrl_handler = hdl;
  2673. if (hdl->error) {
  2674. err = hdl->error;
  2675. goto err_hdl;
  2676. }
  2677. state->detect_tx_5v_ctrl->is_private = true;
  2678. state->rgb_quantization_range_ctrl->is_private = true;
  2679. if (adv76xx_has_afe(state))
  2680. state->analog_sampling_phase_ctrl->is_private = true;
  2681. state->free_run_color_manual_ctrl->is_private = true;
  2682. state->free_run_color_ctrl->is_private = true;
  2683. if (adv76xx_s_detect_tx_5v_ctrl(sd)) {
  2684. err = -ENODEV;
  2685. goto err_hdl;
  2686. }
  2687. for (i = 1; i < ADV76XX_PAGE_MAX; ++i) {
  2688. if (!(BIT(i) & state->info->page_mask))
  2689. continue;
  2690. state->i2c_clients[i] =
  2691. adv76xx_dummy_client(sd, state->pdata.i2c_addresses[i],
  2692. 0xf2 + i);
  2693. if (state->i2c_clients[i] == NULL) {
  2694. err = -ENOMEM;
  2695. v4l2_err(sd, "failed to create i2c client %u\n", i);
  2696. goto err_i2c;
  2697. }
  2698. }
  2699. /* work queues */
  2700. state->work_queues = create_singlethread_workqueue(client->name);
  2701. if (!state->work_queues) {
  2702. v4l2_err(sd, "Could not create work queue\n");
  2703. err = -ENOMEM;
  2704. goto err_i2c;
  2705. }
  2706. INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
  2707. adv76xx_delayed_work_enable_hotplug);
  2708. state->source_pad = state->info->num_dv_ports
  2709. + (state->info->has_afe ? 2 : 0);
  2710. for (i = 0; i < state->source_pad; ++i)
  2711. state->pads[i].flags = MEDIA_PAD_FL_SINK;
  2712. state->pads[state->source_pad].flags = MEDIA_PAD_FL_SOURCE;
  2713. err = media_entity_init(&sd->entity, state->source_pad + 1,
  2714. state->pads, 0);
  2715. if (err)
  2716. goto err_work_queues;
  2717. /* Configure regmaps */
  2718. err = configure_regmaps(state);
  2719. if (err)
  2720. goto err_entity;
  2721. err = adv76xx_core_init(sd);
  2722. if (err)
  2723. goto err_entity;
  2724. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  2725. client->addr << 1, client->adapter->name);
  2726. err = v4l2_async_register_subdev(sd);
  2727. if (err)
  2728. goto err_entity;
  2729. return 0;
  2730. err_entity:
  2731. media_entity_cleanup(&sd->entity);
  2732. err_work_queues:
  2733. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  2734. destroy_workqueue(state->work_queues);
  2735. err_i2c:
  2736. adv76xx_unregister_clients(state);
  2737. err_hdl:
  2738. v4l2_ctrl_handler_free(hdl);
  2739. return err;
  2740. }
  2741. /* ----------------------------------------------------------------------- */
  2742. static int adv76xx_remove(struct i2c_client *client)
  2743. {
  2744. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2745. struct adv76xx_state *state = to_state(sd);
  2746. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  2747. destroy_workqueue(state->work_queues);
  2748. v4l2_async_unregister_subdev(sd);
  2749. media_entity_cleanup(&sd->entity);
  2750. adv76xx_unregister_clients(to_state(sd));
  2751. v4l2_ctrl_handler_free(sd->ctrl_handler);
  2752. return 0;
  2753. }
  2754. /* ----------------------------------------------------------------------- */
  2755. static struct i2c_driver adv76xx_driver = {
  2756. .driver = {
  2757. .name = "adv7604",
  2758. .of_match_table = of_match_ptr(adv76xx_of_id),
  2759. },
  2760. .probe = adv76xx_probe,
  2761. .remove = adv76xx_remove,
  2762. .id_table = adv76xx_i2c_id,
  2763. };
  2764. module_i2c_driver(adv76xx_driver);