adv7842.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229
  1. /*
  2. * adv7842 - Analog Devices ADV7842 video decoder driver
  3. *
  4. * Copyright 2013 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, ADV7842,
  23. * Register Settings Recommendations, Rev. 1.9, April 2011
  24. * REF_02 - Analog devices, Software User Guide, UG-206,
  25. * ADV7842 I2C Register Maps, Rev. 0, November 2010
  26. * REF_03 - Analog devices, Hardware User Guide, UG-214,
  27. * ADV7842 Fast Switching 2:1 HDMI 1.4 Receiver with 3D-Comb
  28. * Decoder and Digitizer , Rev. 0, January 2011
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/slab.h>
  33. #include <linux/i2c.h>
  34. #include <linux/delay.h>
  35. #include <linux/videodev2.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/v4l2-dv-timings.h>
  38. #include <media/v4l2-device.h>
  39. #include <media/v4l2-ctrls.h>
  40. #include <media/v4l2-dv-timings.h>
  41. #include <media/adv7842.h>
  42. static int debug;
  43. module_param(debug, int, 0644);
  44. MODULE_PARM_DESC(debug, "debug level (0-2)");
  45. MODULE_DESCRIPTION("Analog Devices ADV7842 video decoder driver");
  46. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  47. MODULE_AUTHOR("Martin Bugge <marbugge@cisco.com>");
  48. MODULE_LICENSE("GPL");
  49. /* ADV7842 system clock frequency */
  50. #define ADV7842_fsc (28636360)
  51. /*
  52. **********************************************************************
  53. *
  54. * Arrays with configuration parameters for the ADV7842
  55. *
  56. **********************************************************************
  57. */
  58. struct adv7842_state {
  59. struct adv7842_platform_data pdata;
  60. struct v4l2_subdev sd;
  61. struct media_pad pad;
  62. struct v4l2_ctrl_handler hdl;
  63. enum adv7842_mode mode;
  64. struct v4l2_dv_timings timings;
  65. enum adv7842_vid_std_select vid_std_select;
  66. v4l2_std_id norm;
  67. struct {
  68. u8 edid[256];
  69. u32 present;
  70. } hdmi_edid;
  71. struct {
  72. u8 edid[256];
  73. u32 present;
  74. } vga_edid;
  75. struct v4l2_fract aspect_ratio;
  76. u32 rgb_quantization_range;
  77. bool is_cea_format;
  78. struct workqueue_struct *work_queues;
  79. struct delayed_work delayed_work_enable_hotplug;
  80. bool restart_stdi_once;
  81. bool hdmi_port_a;
  82. /* i2c clients */
  83. struct i2c_client *i2c_sdp_io;
  84. struct i2c_client *i2c_sdp;
  85. struct i2c_client *i2c_cp;
  86. struct i2c_client *i2c_vdp;
  87. struct i2c_client *i2c_afe;
  88. struct i2c_client *i2c_hdmi;
  89. struct i2c_client *i2c_repeater;
  90. struct i2c_client *i2c_edid;
  91. struct i2c_client *i2c_infoframe;
  92. struct i2c_client *i2c_cec;
  93. struct i2c_client *i2c_avlink;
  94. /* controls */
  95. struct v4l2_ctrl *detect_tx_5v_ctrl;
  96. struct v4l2_ctrl *analog_sampling_phase_ctrl;
  97. struct v4l2_ctrl *free_run_color_ctrl_manual;
  98. struct v4l2_ctrl *free_run_color_ctrl;
  99. struct v4l2_ctrl *rgb_quantization_range_ctrl;
  100. };
  101. /* Unsupported timings. This device cannot support 720p30. */
  102. static const struct v4l2_dv_timings adv7842_timings_exceptions[] = {
  103. V4L2_DV_BT_CEA_1280X720P30,
  104. { }
  105. };
  106. static bool adv7842_check_dv_timings(const struct v4l2_dv_timings *t, void *hdl)
  107. {
  108. int i;
  109. for (i = 0; adv7842_timings_exceptions[i].bt.width; i++)
  110. if (v4l2_match_dv_timings(t, adv7842_timings_exceptions + i, 0))
  111. return false;
  112. return true;
  113. }
  114. struct adv7842_video_standards {
  115. struct v4l2_dv_timings timings;
  116. u8 vid_std;
  117. u8 v_freq;
  118. };
  119. /* sorted by number of lines */
  120. static const struct adv7842_video_standards adv7842_prim_mode_comp[] = {
  121. /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
  122. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  123. { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
  124. { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
  125. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  126. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  127. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  128. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  129. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  130. /* TODO add 1920x1080P60_RB (CVT timing) */
  131. { },
  132. };
  133. /* sorted by number of lines */
  134. static const struct adv7842_video_standards adv7842_prim_mode_gr[] = {
  135. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  136. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  137. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  138. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  139. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  140. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  141. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  142. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  143. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  144. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  145. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  146. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  147. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  148. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  149. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  150. { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
  151. { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
  152. { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
  153. { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
  154. { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
  155. /* TODO add 1600X1200P60_RB (not a DMT timing) */
  156. { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
  157. { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
  158. { },
  159. };
  160. /* sorted by number of lines */
  161. static const struct adv7842_video_standards adv7842_prim_mode_hdmi_comp[] = {
  162. { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
  163. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  164. { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
  165. { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
  166. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  167. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  168. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  169. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  170. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  171. { },
  172. };
  173. /* sorted by number of lines */
  174. static const struct adv7842_video_standards adv7842_prim_mode_hdmi_gr[] = {
  175. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  176. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  177. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  178. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  179. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  180. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  181. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  182. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  183. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  184. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  185. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  186. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  187. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  188. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  189. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  190. { },
  191. };
  192. /* ----------------------------------------------------------------------- */
  193. static inline struct adv7842_state *to_state(struct v4l2_subdev *sd)
  194. {
  195. return container_of(sd, struct adv7842_state, sd);
  196. }
  197. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  198. {
  199. return &container_of(ctrl->handler, struct adv7842_state, hdl)->sd;
  200. }
  201. static inline unsigned hblanking(const struct v4l2_bt_timings *t)
  202. {
  203. return V4L2_DV_BT_BLANKING_WIDTH(t);
  204. }
  205. static inline unsigned htotal(const struct v4l2_bt_timings *t)
  206. {
  207. return V4L2_DV_BT_FRAME_WIDTH(t);
  208. }
  209. static inline unsigned vblanking(const struct v4l2_bt_timings *t)
  210. {
  211. return V4L2_DV_BT_BLANKING_HEIGHT(t);
  212. }
  213. static inline unsigned vtotal(const struct v4l2_bt_timings *t)
  214. {
  215. return V4L2_DV_BT_FRAME_HEIGHT(t);
  216. }
  217. /* ----------------------------------------------------------------------- */
  218. static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
  219. u8 command, bool check)
  220. {
  221. union i2c_smbus_data data;
  222. if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  223. I2C_SMBUS_READ, command,
  224. I2C_SMBUS_BYTE_DATA, &data))
  225. return data.byte;
  226. if (check)
  227. v4l_err(client, "error reading %02x, %02x\n",
  228. client->addr, command);
  229. return -EIO;
  230. }
  231. static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
  232. {
  233. int i;
  234. for (i = 0; i < 3; i++) {
  235. int ret = adv_smbus_read_byte_data_check(client, command, true);
  236. if (ret >= 0) {
  237. if (i)
  238. v4l_err(client, "read ok after %d retries\n", i);
  239. return ret;
  240. }
  241. }
  242. v4l_err(client, "read failed\n");
  243. return -EIO;
  244. }
  245. static s32 adv_smbus_write_byte_data(struct i2c_client *client,
  246. u8 command, u8 value)
  247. {
  248. union i2c_smbus_data data;
  249. int err;
  250. int i;
  251. data.byte = value;
  252. for (i = 0; i < 3; i++) {
  253. err = i2c_smbus_xfer(client->adapter, client->addr,
  254. client->flags,
  255. I2C_SMBUS_WRITE, command,
  256. I2C_SMBUS_BYTE_DATA, &data);
  257. if (!err)
  258. break;
  259. }
  260. if (err < 0)
  261. v4l_err(client, "error writing %02x, %02x, %02x\n",
  262. client->addr, command, value);
  263. return err;
  264. }
  265. static void adv_smbus_write_byte_no_check(struct i2c_client *client,
  266. u8 command, u8 value)
  267. {
  268. union i2c_smbus_data data;
  269. data.byte = value;
  270. i2c_smbus_xfer(client->adapter, client->addr,
  271. client->flags,
  272. I2C_SMBUS_WRITE, command,
  273. I2C_SMBUS_BYTE_DATA, &data);
  274. }
  275. static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
  276. u8 command, unsigned length, const u8 *values)
  277. {
  278. union i2c_smbus_data data;
  279. if (length > I2C_SMBUS_BLOCK_MAX)
  280. length = I2C_SMBUS_BLOCK_MAX;
  281. data.block[0] = length;
  282. memcpy(data.block + 1, values, length);
  283. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  284. I2C_SMBUS_WRITE, command,
  285. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  286. }
  287. /* ----------------------------------------------------------------------- */
  288. static inline int io_read(struct v4l2_subdev *sd, u8 reg)
  289. {
  290. struct i2c_client *client = v4l2_get_subdevdata(sd);
  291. return adv_smbus_read_byte_data(client, reg);
  292. }
  293. static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  294. {
  295. struct i2c_client *client = v4l2_get_subdevdata(sd);
  296. return adv_smbus_write_byte_data(client, reg, val);
  297. }
  298. static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  299. {
  300. return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
  301. }
  302. static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
  303. {
  304. struct adv7842_state *state = to_state(sd);
  305. return adv_smbus_read_byte_data(state->i2c_avlink, reg);
  306. }
  307. static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  308. {
  309. struct adv7842_state *state = to_state(sd);
  310. return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
  311. }
  312. static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
  313. {
  314. struct adv7842_state *state = to_state(sd);
  315. return adv_smbus_read_byte_data(state->i2c_cec, reg);
  316. }
  317. static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  318. {
  319. struct adv7842_state *state = to_state(sd);
  320. return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
  321. }
  322. static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  323. {
  324. return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val);
  325. }
  326. static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
  327. {
  328. struct adv7842_state *state = to_state(sd);
  329. return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
  330. }
  331. static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  332. {
  333. struct adv7842_state *state = to_state(sd);
  334. return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
  335. }
  336. static inline int sdp_io_read(struct v4l2_subdev *sd, u8 reg)
  337. {
  338. struct adv7842_state *state = to_state(sd);
  339. return adv_smbus_read_byte_data(state->i2c_sdp_io, reg);
  340. }
  341. static inline int sdp_io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  342. {
  343. struct adv7842_state *state = to_state(sd);
  344. return adv_smbus_write_byte_data(state->i2c_sdp_io, reg, val);
  345. }
  346. static inline int sdp_io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  347. {
  348. return sdp_io_write(sd, reg, (sdp_io_read(sd, reg) & mask) | val);
  349. }
  350. static inline int sdp_read(struct v4l2_subdev *sd, u8 reg)
  351. {
  352. struct adv7842_state *state = to_state(sd);
  353. return adv_smbus_read_byte_data(state->i2c_sdp, reg);
  354. }
  355. static inline int sdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  356. {
  357. struct adv7842_state *state = to_state(sd);
  358. return adv_smbus_write_byte_data(state->i2c_sdp, reg, val);
  359. }
  360. static inline int sdp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  361. {
  362. return sdp_write(sd, reg, (sdp_read(sd, reg) & mask) | val);
  363. }
  364. static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
  365. {
  366. struct adv7842_state *state = to_state(sd);
  367. return adv_smbus_read_byte_data(state->i2c_afe, reg);
  368. }
  369. static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  370. {
  371. struct adv7842_state *state = to_state(sd);
  372. return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
  373. }
  374. static inline int afe_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  375. {
  376. return afe_write(sd, reg, (afe_read(sd, reg) & mask) | val);
  377. }
  378. static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
  379. {
  380. struct adv7842_state *state = to_state(sd);
  381. return adv_smbus_read_byte_data(state->i2c_repeater, reg);
  382. }
  383. static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  384. {
  385. struct adv7842_state *state = to_state(sd);
  386. return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
  387. }
  388. static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  389. {
  390. return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
  391. }
  392. static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
  393. {
  394. struct adv7842_state *state = to_state(sd);
  395. return adv_smbus_read_byte_data(state->i2c_edid, reg);
  396. }
  397. static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  398. {
  399. struct adv7842_state *state = to_state(sd);
  400. return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
  401. }
  402. static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
  403. {
  404. struct adv7842_state *state = to_state(sd);
  405. return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
  406. }
  407. static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  408. {
  409. struct adv7842_state *state = to_state(sd);
  410. return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
  411. }
  412. static inline int hdmi_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  413. {
  414. return hdmi_write(sd, reg, (hdmi_read(sd, reg) & mask) | val);
  415. }
  416. static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
  417. {
  418. struct adv7842_state *state = to_state(sd);
  419. return adv_smbus_read_byte_data(state->i2c_cp, reg);
  420. }
  421. static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  422. {
  423. struct adv7842_state *state = to_state(sd);
  424. return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
  425. }
  426. static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  427. {
  428. return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
  429. }
  430. static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
  431. {
  432. struct adv7842_state *state = to_state(sd);
  433. return adv_smbus_read_byte_data(state->i2c_vdp, reg);
  434. }
  435. static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  436. {
  437. struct adv7842_state *state = to_state(sd);
  438. return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
  439. }
  440. static void main_reset(struct v4l2_subdev *sd)
  441. {
  442. struct i2c_client *client = v4l2_get_subdevdata(sd);
  443. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  444. adv_smbus_write_byte_no_check(client, 0xff, 0x80);
  445. mdelay(5);
  446. }
  447. /* ----------------------------------------------------------------------- */
  448. static inline bool is_analog_input(struct v4l2_subdev *sd)
  449. {
  450. struct adv7842_state *state = to_state(sd);
  451. return ((state->mode == ADV7842_MODE_RGB) ||
  452. (state->mode == ADV7842_MODE_COMP));
  453. }
  454. static inline bool is_digital_input(struct v4l2_subdev *sd)
  455. {
  456. struct adv7842_state *state = to_state(sd);
  457. return state->mode == ADV7842_MODE_HDMI;
  458. }
  459. static const struct v4l2_dv_timings_cap adv7842_timings_cap_analog = {
  460. .type = V4L2_DV_BT_656_1120,
  461. /* keep this initialization for compatibility with GCC < 4.4.6 */
  462. .reserved = { 0 },
  463. V4L2_INIT_BT_TIMINGS(0, 1920, 0, 1200, 25000000, 170000000,
  464. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  465. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  466. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
  467. V4L2_DV_BT_CAP_CUSTOM)
  468. };
  469. static const struct v4l2_dv_timings_cap adv7842_timings_cap_digital = {
  470. .type = V4L2_DV_BT_656_1120,
  471. /* keep this initialization for compatibility with GCC < 4.4.6 */
  472. .reserved = { 0 },
  473. V4L2_INIT_BT_TIMINGS(0, 1920, 0, 1200, 25000000, 225000000,
  474. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  475. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  476. V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
  477. V4L2_DV_BT_CAP_CUSTOM)
  478. };
  479. static inline const struct v4l2_dv_timings_cap *
  480. adv7842_get_dv_timings_cap(struct v4l2_subdev *sd)
  481. {
  482. return is_digital_input(sd) ? &adv7842_timings_cap_digital :
  483. &adv7842_timings_cap_analog;
  484. }
  485. /* ----------------------------------------------------------------------- */
  486. static void adv7842_delayed_work_enable_hotplug(struct work_struct *work)
  487. {
  488. struct delayed_work *dwork = to_delayed_work(work);
  489. struct adv7842_state *state = container_of(dwork,
  490. struct adv7842_state, delayed_work_enable_hotplug);
  491. struct v4l2_subdev *sd = &state->sd;
  492. int present = state->hdmi_edid.present;
  493. u8 mask = 0;
  494. v4l2_dbg(2, debug, sd, "%s: enable hotplug on ports: 0x%x\n",
  495. __func__, present);
  496. if (present & (0x04 << ADV7842_EDID_PORT_A))
  497. mask |= 0x20;
  498. if (present & (0x04 << ADV7842_EDID_PORT_B))
  499. mask |= 0x10;
  500. io_write_and_or(sd, 0x20, 0xcf, mask);
  501. }
  502. static int edid_write_vga_segment(struct v4l2_subdev *sd)
  503. {
  504. struct i2c_client *client = v4l2_get_subdevdata(sd);
  505. struct adv7842_state *state = to_state(sd);
  506. const u8 *val = state->vga_edid.edid;
  507. int err = 0;
  508. int i;
  509. v4l2_dbg(2, debug, sd, "%s: write EDID on VGA port\n", __func__);
  510. /* HPA disable on port A and B */
  511. io_write_and_or(sd, 0x20, 0xcf, 0x00);
  512. /* Disable I2C access to internal EDID ram from VGA DDC port */
  513. rep_write_and_or(sd, 0x7f, 0x7f, 0x00);
  514. /* edid segment pointer '1' for VGA port */
  515. rep_write_and_or(sd, 0x77, 0xef, 0x10);
  516. for (i = 0; !err && i < 256; i += I2C_SMBUS_BLOCK_MAX)
  517. err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
  518. I2C_SMBUS_BLOCK_MAX, val + i);
  519. if (err)
  520. return err;
  521. /* Calculates the checksums and enables I2C access
  522. * to internal EDID ram from VGA DDC port.
  523. */
  524. rep_write_and_or(sd, 0x7f, 0x7f, 0x80);
  525. for (i = 0; i < 1000; i++) {
  526. if (rep_read(sd, 0x79) & 0x20)
  527. break;
  528. mdelay(1);
  529. }
  530. if (i == 1000) {
  531. v4l_err(client, "error enabling edid on VGA port\n");
  532. return -EIO;
  533. }
  534. /* enable hotplug after 200 ms */
  535. queue_delayed_work(state->work_queues,
  536. &state->delayed_work_enable_hotplug, HZ / 5);
  537. return 0;
  538. }
  539. static int edid_spa_location(const u8 *edid)
  540. {
  541. u8 d;
  542. /*
  543. * TODO, improve and update for other CEA extensions
  544. * currently only for 1 segment (256 bytes),
  545. * i.e. 1 extension block and CEA revision 3.
  546. */
  547. if ((edid[0x7e] != 1) ||
  548. (edid[0x80] != 0x02) ||
  549. (edid[0x81] != 0x03)) {
  550. return -EINVAL;
  551. }
  552. /*
  553. * search Vendor Specific Data Block (tag 3)
  554. */
  555. d = edid[0x82] & 0x7f;
  556. if (d > 4) {
  557. int i = 0x84;
  558. int end = 0x80 + d;
  559. do {
  560. u8 tag = edid[i]>>5;
  561. u8 len = edid[i] & 0x1f;
  562. if ((tag == 3) && (len >= 5))
  563. return i + 4;
  564. i += len + 1;
  565. } while (i < end);
  566. }
  567. return -EINVAL;
  568. }
  569. static int edid_write_hdmi_segment(struct v4l2_subdev *sd, u8 port)
  570. {
  571. struct i2c_client *client = v4l2_get_subdevdata(sd);
  572. struct adv7842_state *state = to_state(sd);
  573. const u8 *val = state->hdmi_edid.edid;
  574. int spa_loc = edid_spa_location(val);
  575. int err = 0;
  576. int i;
  577. v4l2_dbg(2, debug, sd, "%s: write EDID on port %c (spa at 0x%x)\n",
  578. __func__, (port == ADV7842_EDID_PORT_A) ? 'A' : 'B', spa_loc);
  579. /* HPA disable on port A and B */
  580. io_write_and_or(sd, 0x20, 0xcf, 0x00);
  581. /* Disable I2C access to internal EDID ram from HDMI DDC ports */
  582. rep_write_and_or(sd, 0x77, 0xf3, 0x00);
  583. if (!state->hdmi_edid.present)
  584. return 0;
  585. /* edid segment pointer '0' for HDMI ports */
  586. rep_write_and_or(sd, 0x77, 0xef, 0x00);
  587. for (i = 0; !err && i < 256; i += I2C_SMBUS_BLOCK_MAX)
  588. err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
  589. I2C_SMBUS_BLOCK_MAX, val + i);
  590. if (err)
  591. return err;
  592. if (spa_loc < 0)
  593. spa_loc = 0xc0; /* Default value [REF_02, p. 199] */
  594. if (port == ADV7842_EDID_PORT_A) {
  595. rep_write(sd, 0x72, val[spa_loc]);
  596. rep_write(sd, 0x73, val[spa_loc + 1]);
  597. } else {
  598. rep_write(sd, 0x74, val[spa_loc]);
  599. rep_write(sd, 0x75, val[spa_loc + 1]);
  600. }
  601. rep_write(sd, 0x76, spa_loc & 0xff);
  602. rep_write_and_or(sd, 0x77, 0xbf, (spa_loc >> 2) & 0x40);
  603. /* Calculates the checksums and enables I2C access to internal
  604. * EDID ram from HDMI DDC ports
  605. */
  606. rep_write_and_or(sd, 0x77, 0xf3, state->hdmi_edid.present);
  607. for (i = 0; i < 1000; i++) {
  608. if (rep_read(sd, 0x7d) & state->hdmi_edid.present)
  609. break;
  610. mdelay(1);
  611. }
  612. if (i == 1000) {
  613. v4l_err(client, "error enabling edid on port %c\n",
  614. (port == ADV7842_EDID_PORT_A) ? 'A' : 'B');
  615. return -EIO;
  616. }
  617. /* enable hotplug after 200 ms */
  618. queue_delayed_work(state->work_queues,
  619. &state->delayed_work_enable_hotplug, HZ / 5);
  620. return 0;
  621. }
  622. /* ----------------------------------------------------------------------- */
  623. #ifdef CONFIG_VIDEO_ADV_DEBUG
  624. static void adv7842_inv_register(struct v4l2_subdev *sd)
  625. {
  626. v4l2_info(sd, "0x000-0x0ff: IO Map\n");
  627. v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
  628. v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
  629. v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
  630. v4l2_info(sd, "0x400-0x4ff: SDP_IO Map\n");
  631. v4l2_info(sd, "0x500-0x5ff: SDP Map\n");
  632. v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
  633. v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
  634. v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
  635. v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
  636. v4l2_info(sd, "0xa00-0xaff: CP Map\n");
  637. v4l2_info(sd, "0xb00-0xbff: VDP Map\n");
  638. }
  639. static int adv7842_g_register(struct v4l2_subdev *sd,
  640. struct v4l2_dbg_register *reg)
  641. {
  642. reg->size = 1;
  643. switch (reg->reg >> 8) {
  644. case 0:
  645. reg->val = io_read(sd, reg->reg & 0xff);
  646. break;
  647. case 1:
  648. reg->val = avlink_read(sd, reg->reg & 0xff);
  649. break;
  650. case 2:
  651. reg->val = cec_read(sd, reg->reg & 0xff);
  652. break;
  653. case 3:
  654. reg->val = infoframe_read(sd, reg->reg & 0xff);
  655. break;
  656. case 4:
  657. reg->val = sdp_io_read(sd, reg->reg & 0xff);
  658. break;
  659. case 5:
  660. reg->val = sdp_read(sd, reg->reg & 0xff);
  661. break;
  662. case 6:
  663. reg->val = afe_read(sd, reg->reg & 0xff);
  664. break;
  665. case 7:
  666. reg->val = rep_read(sd, reg->reg & 0xff);
  667. break;
  668. case 8:
  669. reg->val = edid_read(sd, reg->reg & 0xff);
  670. break;
  671. case 9:
  672. reg->val = hdmi_read(sd, reg->reg & 0xff);
  673. break;
  674. case 0xa:
  675. reg->val = cp_read(sd, reg->reg & 0xff);
  676. break;
  677. case 0xb:
  678. reg->val = vdp_read(sd, reg->reg & 0xff);
  679. break;
  680. default:
  681. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  682. adv7842_inv_register(sd);
  683. break;
  684. }
  685. return 0;
  686. }
  687. static int adv7842_s_register(struct v4l2_subdev *sd,
  688. const struct v4l2_dbg_register *reg)
  689. {
  690. u8 val = reg->val & 0xff;
  691. switch (reg->reg >> 8) {
  692. case 0:
  693. io_write(sd, reg->reg & 0xff, val);
  694. break;
  695. case 1:
  696. avlink_write(sd, reg->reg & 0xff, val);
  697. break;
  698. case 2:
  699. cec_write(sd, reg->reg & 0xff, val);
  700. break;
  701. case 3:
  702. infoframe_write(sd, reg->reg & 0xff, val);
  703. break;
  704. case 4:
  705. sdp_io_write(sd, reg->reg & 0xff, val);
  706. break;
  707. case 5:
  708. sdp_write(sd, reg->reg & 0xff, val);
  709. break;
  710. case 6:
  711. afe_write(sd, reg->reg & 0xff, val);
  712. break;
  713. case 7:
  714. rep_write(sd, reg->reg & 0xff, val);
  715. break;
  716. case 8:
  717. edid_write(sd, reg->reg & 0xff, val);
  718. break;
  719. case 9:
  720. hdmi_write(sd, reg->reg & 0xff, val);
  721. break;
  722. case 0xa:
  723. cp_write(sd, reg->reg & 0xff, val);
  724. break;
  725. case 0xb:
  726. vdp_write(sd, reg->reg & 0xff, val);
  727. break;
  728. default:
  729. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  730. adv7842_inv_register(sd);
  731. break;
  732. }
  733. return 0;
  734. }
  735. #endif
  736. static int adv7842_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
  737. {
  738. struct adv7842_state *state = to_state(sd);
  739. int prev = v4l2_ctrl_g_ctrl(state->detect_tx_5v_ctrl);
  740. u8 reg_io_6f = io_read(sd, 0x6f);
  741. int val = 0;
  742. if (reg_io_6f & 0x02)
  743. val |= 1; /* port A */
  744. if (reg_io_6f & 0x01)
  745. val |= 2; /* port B */
  746. v4l2_dbg(1, debug, sd, "%s: 0x%x -> 0x%x\n", __func__, prev, val);
  747. if (val != prev)
  748. return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl, val);
  749. return 0;
  750. }
  751. static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
  752. u8 prim_mode,
  753. const struct adv7842_video_standards *predef_vid_timings,
  754. const struct v4l2_dv_timings *timings)
  755. {
  756. int i;
  757. for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
  758. if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
  759. is_digital_input(sd) ? 250000 : 1000000))
  760. continue;
  761. /* video std */
  762. io_write(sd, 0x00, predef_vid_timings[i].vid_std);
  763. /* v_freq and prim mode */
  764. io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) + prim_mode);
  765. return 0;
  766. }
  767. return -1;
  768. }
  769. static int configure_predefined_video_timings(struct v4l2_subdev *sd,
  770. struct v4l2_dv_timings *timings)
  771. {
  772. struct adv7842_state *state = to_state(sd);
  773. int err;
  774. v4l2_dbg(1, debug, sd, "%s\n", __func__);
  775. /* reset to default values */
  776. io_write(sd, 0x16, 0x43);
  777. io_write(sd, 0x17, 0x5a);
  778. /* disable embedded syncs for auto graphics mode */
  779. cp_write_and_or(sd, 0x81, 0xef, 0x00);
  780. cp_write(sd, 0x26, 0x00);
  781. cp_write(sd, 0x27, 0x00);
  782. cp_write(sd, 0x28, 0x00);
  783. cp_write(sd, 0x29, 0x00);
  784. cp_write(sd, 0x8f, 0x40);
  785. cp_write(sd, 0x90, 0x00);
  786. cp_write(sd, 0xa5, 0x00);
  787. cp_write(sd, 0xa6, 0x00);
  788. cp_write(sd, 0xa7, 0x00);
  789. cp_write(sd, 0xab, 0x00);
  790. cp_write(sd, 0xac, 0x00);
  791. switch (state->mode) {
  792. case ADV7842_MODE_COMP:
  793. case ADV7842_MODE_RGB:
  794. err = find_and_set_predefined_video_timings(sd,
  795. 0x01, adv7842_prim_mode_comp, timings);
  796. if (err)
  797. err = find_and_set_predefined_video_timings(sd,
  798. 0x02, adv7842_prim_mode_gr, timings);
  799. break;
  800. case ADV7842_MODE_HDMI:
  801. err = find_and_set_predefined_video_timings(sd,
  802. 0x05, adv7842_prim_mode_hdmi_comp, timings);
  803. if (err)
  804. err = find_and_set_predefined_video_timings(sd,
  805. 0x06, adv7842_prim_mode_hdmi_gr, timings);
  806. break;
  807. default:
  808. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  809. __func__, state->mode);
  810. err = -1;
  811. break;
  812. }
  813. return err;
  814. }
  815. static void configure_custom_video_timings(struct v4l2_subdev *sd,
  816. const struct v4l2_bt_timings *bt)
  817. {
  818. struct adv7842_state *state = to_state(sd);
  819. struct i2c_client *client = v4l2_get_subdevdata(sd);
  820. u32 width = htotal(bt);
  821. u32 height = vtotal(bt);
  822. u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
  823. u16 cp_start_eav = width - bt->hfrontporch;
  824. u16 cp_start_vbi = height - bt->vfrontporch + 1;
  825. u16 cp_end_vbi = bt->vsync + bt->vbackporch + 1;
  826. u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
  827. ((width * (ADV7842_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
  828. const u8 pll[2] = {
  829. 0xc0 | ((width >> 8) & 0x1f),
  830. width & 0xff
  831. };
  832. v4l2_dbg(2, debug, sd, "%s\n", __func__);
  833. switch (state->mode) {
  834. case ADV7842_MODE_COMP:
  835. case ADV7842_MODE_RGB:
  836. /* auto graphics */
  837. io_write(sd, 0x00, 0x07); /* video std */
  838. io_write(sd, 0x01, 0x02); /* prim mode */
  839. /* enable embedded syncs for auto graphics mode */
  840. cp_write_and_or(sd, 0x81, 0xef, 0x10);
  841. /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
  842. /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
  843. /* IO-map reg. 0x16 and 0x17 should be written in sequence */
  844. if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll)) {
  845. v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
  846. break;
  847. }
  848. /* active video - horizontal timing */
  849. cp_write(sd, 0x26, (cp_start_sav >> 8) & 0xf);
  850. cp_write(sd, 0x27, (cp_start_sav & 0xff));
  851. cp_write(sd, 0x28, (cp_start_eav >> 8) & 0xf);
  852. cp_write(sd, 0x29, (cp_start_eav & 0xff));
  853. /* active video - vertical timing */
  854. cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
  855. cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
  856. ((cp_end_vbi >> 8) & 0xf));
  857. cp_write(sd, 0xa7, cp_end_vbi & 0xff);
  858. break;
  859. case ADV7842_MODE_HDMI:
  860. /* set default prim_mode/vid_std for HDMI
  861. according to [REF_03, c. 4.2] */
  862. io_write(sd, 0x00, 0x02); /* video std */
  863. io_write(sd, 0x01, 0x06); /* prim mode */
  864. break;
  865. default:
  866. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  867. __func__, state->mode);
  868. break;
  869. }
  870. cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
  871. cp_write(sd, 0x90, ch1_fr_ll & 0xff);
  872. cp_write(sd, 0xab, (height >> 4) & 0xff);
  873. cp_write(sd, 0xac, (height & 0x0f) << 4);
  874. }
  875. static void adv7842_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
  876. {
  877. struct adv7842_state *state = to_state(sd);
  878. u8 offset_buf[4];
  879. if (auto_offset) {
  880. offset_a = 0x3ff;
  881. offset_b = 0x3ff;
  882. offset_c = 0x3ff;
  883. }
  884. v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
  885. __func__, auto_offset ? "Auto" : "Manual",
  886. offset_a, offset_b, offset_c);
  887. offset_buf[0]= (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
  888. offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
  889. offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
  890. offset_buf[3] = offset_c & 0x0ff;
  891. /* Registers must be written in this order with no i2c access in between */
  892. if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x77, 4, offset_buf))
  893. v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
  894. }
  895. static void adv7842_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
  896. {
  897. struct adv7842_state *state = to_state(sd);
  898. u8 gain_buf[4];
  899. u8 gain_man = 1;
  900. u8 agc_mode_man = 1;
  901. if (auto_gain) {
  902. gain_man = 0;
  903. agc_mode_man = 0;
  904. gain_a = 0x100;
  905. gain_b = 0x100;
  906. gain_c = 0x100;
  907. }
  908. v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
  909. __func__, auto_gain ? "Auto" : "Manual",
  910. gain_a, gain_b, gain_c);
  911. gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
  912. gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
  913. gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
  914. gain_buf[3] = ((gain_c & 0x0ff));
  915. /* Registers must be written in this order with no i2c access in between */
  916. if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x73, 4, gain_buf))
  917. v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
  918. }
  919. static void set_rgb_quantization_range(struct v4l2_subdev *sd)
  920. {
  921. struct adv7842_state *state = to_state(sd);
  922. bool rgb_output = io_read(sd, 0x02) & 0x02;
  923. bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
  924. v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
  925. __func__, state->rgb_quantization_range,
  926. rgb_output, hdmi_signal);
  927. adv7842_set_gain(sd, true, 0x0, 0x0, 0x0);
  928. adv7842_set_offset(sd, true, 0x0, 0x0, 0x0);
  929. switch (state->rgb_quantization_range) {
  930. case V4L2_DV_RGB_RANGE_AUTO:
  931. if (state->mode == ADV7842_MODE_RGB) {
  932. /* Receiving analog RGB signal
  933. * Set RGB full range (0-255) */
  934. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  935. break;
  936. }
  937. if (state->mode == ADV7842_MODE_COMP) {
  938. /* Receiving analog YPbPr signal
  939. * Set automode */
  940. io_write_and_or(sd, 0x02, 0x0f, 0xf0);
  941. break;
  942. }
  943. if (hdmi_signal) {
  944. /* Receiving HDMI signal
  945. * Set automode */
  946. io_write_and_or(sd, 0x02, 0x0f, 0xf0);
  947. break;
  948. }
  949. /* Receiving DVI-D signal
  950. * ADV7842 selects RGB limited range regardless of
  951. * input format (CE/IT) in automatic mode */
  952. if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
  953. /* RGB limited range (16-235) */
  954. io_write_and_or(sd, 0x02, 0x0f, 0x00);
  955. } else {
  956. /* RGB full range (0-255) */
  957. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  958. if (is_digital_input(sd) && rgb_output) {
  959. adv7842_set_offset(sd, false, 0x40, 0x40, 0x40);
  960. } else {
  961. adv7842_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
  962. adv7842_set_offset(sd, false, 0x70, 0x70, 0x70);
  963. }
  964. }
  965. break;
  966. case V4L2_DV_RGB_RANGE_LIMITED:
  967. if (state->mode == ADV7842_MODE_COMP) {
  968. /* YCrCb limited range (16-235) */
  969. io_write_and_or(sd, 0x02, 0x0f, 0x20);
  970. break;
  971. }
  972. /* RGB limited range (16-235) */
  973. io_write_and_or(sd, 0x02, 0x0f, 0x00);
  974. break;
  975. case V4L2_DV_RGB_RANGE_FULL:
  976. if (state->mode == ADV7842_MODE_COMP) {
  977. /* YCrCb full range (0-255) */
  978. io_write_and_or(sd, 0x02, 0x0f, 0x60);
  979. break;
  980. }
  981. /* RGB full range (0-255) */
  982. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  983. if (is_analog_input(sd) || hdmi_signal)
  984. break;
  985. /* Adjust gain/offset for DVI-D signals only */
  986. if (rgb_output) {
  987. adv7842_set_offset(sd, false, 0x40, 0x40, 0x40);
  988. } else {
  989. adv7842_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
  990. adv7842_set_offset(sd, false, 0x70, 0x70, 0x70);
  991. }
  992. break;
  993. }
  994. }
  995. static int adv7842_s_ctrl(struct v4l2_ctrl *ctrl)
  996. {
  997. struct v4l2_subdev *sd = to_sd(ctrl);
  998. struct adv7842_state *state = to_state(sd);
  999. /* TODO SDP ctrls
  1000. contrast/brightness/hue/free run is acting a bit strange,
  1001. not sure if sdp csc is correct.
  1002. */
  1003. switch (ctrl->id) {
  1004. /* standard ctrls */
  1005. case V4L2_CID_BRIGHTNESS:
  1006. cp_write(sd, 0x3c, ctrl->val);
  1007. sdp_write(sd, 0x14, ctrl->val);
  1008. /* ignore lsb sdp 0x17[3:2] */
  1009. return 0;
  1010. case V4L2_CID_CONTRAST:
  1011. cp_write(sd, 0x3a, ctrl->val);
  1012. sdp_write(sd, 0x13, ctrl->val);
  1013. /* ignore lsb sdp 0x17[1:0] */
  1014. return 0;
  1015. case V4L2_CID_SATURATION:
  1016. cp_write(sd, 0x3b, ctrl->val);
  1017. sdp_write(sd, 0x15, ctrl->val);
  1018. /* ignore lsb sdp 0x17[5:4] */
  1019. return 0;
  1020. case V4L2_CID_HUE:
  1021. cp_write(sd, 0x3d, ctrl->val);
  1022. sdp_write(sd, 0x16, ctrl->val);
  1023. /* ignore lsb sdp 0x17[7:6] */
  1024. return 0;
  1025. /* custom ctrls */
  1026. case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
  1027. afe_write(sd, 0xc8, ctrl->val);
  1028. return 0;
  1029. case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
  1030. cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
  1031. sdp_write_and_or(sd, 0xdd, ~0x04, (ctrl->val << 2));
  1032. return 0;
  1033. case V4L2_CID_ADV_RX_FREE_RUN_COLOR: {
  1034. u8 R = (ctrl->val & 0xff0000) >> 16;
  1035. u8 G = (ctrl->val & 0x00ff00) >> 8;
  1036. u8 B = (ctrl->val & 0x0000ff);
  1037. /* RGB -> YUV, numerical approximation */
  1038. int Y = 66 * R + 129 * G + 25 * B;
  1039. int U = -38 * R - 74 * G + 112 * B;
  1040. int V = 112 * R - 94 * G - 18 * B;
  1041. /* Scale down to 8 bits with rounding */
  1042. Y = (Y + 128) >> 8;
  1043. U = (U + 128) >> 8;
  1044. V = (V + 128) >> 8;
  1045. /* make U,V positive */
  1046. Y += 16;
  1047. U += 128;
  1048. V += 128;
  1049. v4l2_dbg(1, debug, sd, "R %x, G %x, B %x\n", R, G, B);
  1050. v4l2_dbg(1, debug, sd, "Y %x, U %x, V %x\n", Y, U, V);
  1051. /* CP */
  1052. cp_write(sd, 0xc1, R);
  1053. cp_write(sd, 0xc0, G);
  1054. cp_write(sd, 0xc2, B);
  1055. /* SDP */
  1056. sdp_write(sd, 0xde, Y);
  1057. sdp_write(sd, 0xdf, (V & 0xf0) | ((U >> 4) & 0x0f));
  1058. return 0;
  1059. }
  1060. case V4L2_CID_DV_RX_RGB_RANGE:
  1061. state->rgb_quantization_range = ctrl->val;
  1062. set_rgb_quantization_range(sd);
  1063. return 0;
  1064. }
  1065. return -EINVAL;
  1066. }
  1067. static inline bool no_power(struct v4l2_subdev *sd)
  1068. {
  1069. return io_read(sd, 0x0c) & 0x24;
  1070. }
  1071. static inline bool no_cp_signal(struct v4l2_subdev *sd)
  1072. {
  1073. return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0) || !(cp_read(sd, 0xb1) & 0x80);
  1074. }
  1075. static inline bool is_hdmi(struct v4l2_subdev *sd)
  1076. {
  1077. return hdmi_read(sd, 0x05) & 0x80;
  1078. }
  1079. static int adv7842_g_input_status(struct v4l2_subdev *sd, u32 *status)
  1080. {
  1081. struct adv7842_state *state = to_state(sd);
  1082. *status = 0;
  1083. if (io_read(sd, 0x0c) & 0x24)
  1084. *status |= V4L2_IN_ST_NO_POWER;
  1085. if (state->mode == ADV7842_MODE_SDP) {
  1086. /* status from SDP block */
  1087. if (!(sdp_read(sd, 0x5A) & 0x01))
  1088. *status |= V4L2_IN_ST_NO_SIGNAL;
  1089. v4l2_dbg(1, debug, sd, "%s: SDP status = 0x%x\n",
  1090. __func__, *status);
  1091. return 0;
  1092. }
  1093. /* status from CP block */
  1094. if ((cp_read(sd, 0xb5) & 0xd0) != 0xd0 ||
  1095. !(cp_read(sd, 0xb1) & 0x80))
  1096. /* TODO channel 2 */
  1097. *status |= V4L2_IN_ST_NO_SIGNAL;
  1098. if (is_digital_input(sd) && ((io_read(sd, 0x74) & 0x03) != 0x03))
  1099. *status |= V4L2_IN_ST_NO_SIGNAL;
  1100. v4l2_dbg(1, debug, sd, "%s: CP status = 0x%x\n",
  1101. __func__, *status);
  1102. return 0;
  1103. }
  1104. struct stdi_readback {
  1105. u16 bl, lcf, lcvs;
  1106. u8 hs_pol, vs_pol;
  1107. bool interlaced;
  1108. };
  1109. static int stdi2dv_timings(struct v4l2_subdev *sd,
  1110. struct stdi_readback *stdi,
  1111. struct v4l2_dv_timings *timings)
  1112. {
  1113. struct adv7842_state *state = to_state(sd);
  1114. u32 hfreq = (ADV7842_fsc * 8) / stdi->bl;
  1115. u32 pix_clk;
  1116. int i;
  1117. for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
  1118. const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
  1119. if (!v4l2_valid_dv_timings(&v4l2_dv_timings_presets[i],
  1120. adv7842_get_dv_timings_cap(sd),
  1121. adv7842_check_dv_timings, NULL))
  1122. continue;
  1123. if (vtotal(bt) != stdi->lcf + 1)
  1124. continue;
  1125. if (bt->vsync != stdi->lcvs)
  1126. continue;
  1127. pix_clk = hfreq * htotal(bt);
  1128. if ((pix_clk < bt->pixelclock + 1000000) &&
  1129. (pix_clk > bt->pixelclock - 1000000)) {
  1130. *timings = v4l2_dv_timings_presets[i];
  1131. return 0;
  1132. }
  1133. }
  1134. if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
  1135. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  1136. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  1137. timings))
  1138. return 0;
  1139. if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
  1140. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  1141. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  1142. state->aspect_ratio, timings))
  1143. return 0;
  1144. v4l2_dbg(2, debug, sd,
  1145. "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
  1146. __func__, stdi->lcvs, stdi->lcf, stdi->bl,
  1147. stdi->hs_pol, stdi->vs_pol);
  1148. return -1;
  1149. }
  1150. static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
  1151. {
  1152. u32 status;
  1153. adv7842_g_input_status(sd, &status);
  1154. if (status & V4L2_IN_ST_NO_SIGNAL) {
  1155. v4l2_dbg(2, debug, sd, "%s: no signal\n", __func__);
  1156. return -ENOLINK;
  1157. }
  1158. stdi->bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
  1159. stdi->lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
  1160. stdi->lcvs = cp_read(sd, 0xb3) >> 3;
  1161. if ((cp_read(sd, 0xb5) & 0x80) && ((cp_read(sd, 0xb5) & 0x03) == 0x01)) {
  1162. stdi->hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
  1163. ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
  1164. stdi->vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
  1165. ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
  1166. } else {
  1167. stdi->hs_pol = 'x';
  1168. stdi->vs_pol = 'x';
  1169. }
  1170. stdi->interlaced = (cp_read(sd, 0xb1) & 0x40) ? true : false;
  1171. if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
  1172. v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
  1173. return -ENOLINK;
  1174. }
  1175. v4l2_dbg(2, debug, sd,
  1176. "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
  1177. __func__, stdi->lcf, stdi->bl, stdi->lcvs,
  1178. stdi->hs_pol, stdi->vs_pol,
  1179. stdi->interlaced ? "interlaced" : "progressive");
  1180. return 0;
  1181. }
  1182. static int adv7842_enum_dv_timings(struct v4l2_subdev *sd,
  1183. struct v4l2_enum_dv_timings *timings)
  1184. {
  1185. if (timings->pad != 0)
  1186. return -EINVAL;
  1187. return v4l2_enum_dv_timings_cap(timings,
  1188. adv7842_get_dv_timings_cap(sd), adv7842_check_dv_timings, NULL);
  1189. }
  1190. static int adv7842_dv_timings_cap(struct v4l2_subdev *sd,
  1191. struct v4l2_dv_timings_cap *cap)
  1192. {
  1193. if (cap->pad != 0)
  1194. return -EINVAL;
  1195. *cap = *adv7842_get_dv_timings_cap(sd);
  1196. return 0;
  1197. }
  1198. /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
  1199. if the format is listed in adv7842_timings[] */
  1200. static void adv7842_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
  1201. struct v4l2_dv_timings *timings)
  1202. {
  1203. v4l2_find_dv_timings_cap(timings, adv7842_get_dv_timings_cap(sd),
  1204. is_digital_input(sd) ? 250000 : 1000000,
  1205. adv7842_check_dv_timings, NULL);
  1206. }
  1207. static int adv7842_query_dv_timings(struct v4l2_subdev *sd,
  1208. struct v4l2_dv_timings *timings)
  1209. {
  1210. struct adv7842_state *state = to_state(sd);
  1211. struct v4l2_bt_timings *bt = &timings->bt;
  1212. struct stdi_readback stdi = { 0 };
  1213. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  1214. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1215. /* SDP block */
  1216. if (state->mode == ADV7842_MODE_SDP)
  1217. return -ENODATA;
  1218. /* read STDI */
  1219. if (read_stdi(sd, &stdi)) {
  1220. state->restart_stdi_once = true;
  1221. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  1222. return -ENOLINK;
  1223. }
  1224. bt->interlaced = stdi.interlaced ?
  1225. V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  1226. if (is_digital_input(sd)) {
  1227. uint32_t freq;
  1228. timings->type = V4L2_DV_BT_656_1120;
  1229. bt->width = (hdmi_read(sd, 0x07) & 0x0f) * 256 + hdmi_read(sd, 0x08);
  1230. bt->height = (hdmi_read(sd, 0x09) & 0x0f) * 256 + hdmi_read(sd, 0x0a);
  1231. freq = ((hdmi_read(sd, 0x51) << 1) + (hdmi_read(sd, 0x52) >> 7)) * 1000000;
  1232. freq += ((hdmi_read(sd, 0x52) & 0x7f) * 7813);
  1233. if (is_hdmi(sd)) {
  1234. /* adjust for deep color mode */
  1235. freq = freq * 8 / (((hdmi_read(sd, 0x0b) & 0xc0) >> 6) * 2 + 8);
  1236. }
  1237. bt->pixelclock = freq;
  1238. bt->hfrontporch = (hdmi_read(sd, 0x20) & 0x03) * 256 +
  1239. hdmi_read(sd, 0x21);
  1240. bt->hsync = (hdmi_read(sd, 0x22) & 0x03) * 256 +
  1241. hdmi_read(sd, 0x23);
  1242. bt->hbackporch = (hdmi_read(sd, 0x24) & 0x03) * 256 +
  1243. hdmi_read(sd, 0x25);
  1244. bt->vfrontporch = ((hdmi_read(sd, 0x2a) & 0x1f) * 256 +
  1245. hdmi_read(sd, 0x2b)) / 2;
  1246. bt->vsync = ((hdmi_read(sd, 0x2e) & 0x1f) * 256 +
  1247. hdmi_read(sd, 0x2f)) / 2;
  1248. bt->vbackporch = ((hdmi_read(sd, 0x32) & 0x1f) * 256 +
  1249. hdmi_read(sd, 0x33)) / 2;
  1250. bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
  1251. ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
  1252. if (bt->interlaced == V4L2_DV_INTERLACED) {
  1253. bt->height += (hdmi_read(sd, 0x0b) & 0x0f) * 256 +
  1254. hdmi_read(sd, 0x0c);
  1255. bt->il_vfrontporch = ((hdmi_read(sd, 0x2c) & 0x1f) * 256 +
  1256. hdmi_read(sd, 0x2d)) / 2;
  1257. bt->il_vsync = ((hdmi_read(sd, 0x30) & 0x1f) * 256 +
  1258. hdmi_read(sd, 0x31)) / 2;
  1259. bt->il_vbackporch = ((hdmi_read(sd, 0x34) & 0x1f) * 256 +
  1260. hdmi_read(sd, 0x35)) / 2;
  1261. }
  1262. adv7842_fill_optional_dv_timings_fields(sd, timings);
  1263. } else {
  1264. /* find format
  1265. * Since LCVS values are inaccurate [REF_03, p. 339-340],
  1266. * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
  1267. */
  1268. if (!stdi2dv_timings(sd, &stdi, timings))
  1269. goto found;
  1270. stdi.lcvs += 1;
  1271. v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
  1272. if (!stdi2dv_timings(sd, &stdi, timings))
  1273. goto found;
  1274. stdi.lcvs -= 2;
  1275. v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
  1276. if (stdi2dv_timings(sd, &stdi, timings)) {
  1277. /*
  1278. * The STDI block may measure wrong values, especially
  1279. * for lcvs and lcf. If the driver can not find any
  1280. * valid timing, the STDI block is restarted to measure
  1281. * the video timings again. The function will return an
  1282. * error, but the restart of STDI will generate a new
  1283. * STDI interrupt and the format detection process will
  1284. * restart.
  1285. */
  1286. if (state->restart_stdi_once) {
  1287. v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
  1288. /* TODO restart STDI for Sync Channel 2 */
  1289. /* enter one-shot mode */
  1290. cp_write_and_or(sd, 0x86, 0xf9, 0x00);
  1291. /* trigger STDI restart */
  1292. cp_write_and_or(sd, 0x86, 0xf9, 0x04);
  1293. /* reset to continuous mode */
  1294. cp_write_and_or(sd, 0x86, 0xf9, 0x02);
  1295. state->restart_stdi_once = false;
  1296. return -ENOLINK;
  1297. }
  1298. v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
  1299. return -ERANGE;
  1300. }
  1301. state->restart_stdi_once = true;
  1302. }
  1303. found:
  1304. if (debug > 1)
  1305. v4l2_print_dv_timings(sd->name, "adv7842_query_dv_timings:",
  1306. timings, true);
  1307. return 0;
  1308. }
  1309. static int adv7842_s_dv_timings(struct v4l2_subdev *sd,
  1310. struct v4l2_dv_timings *timings)
  1311. {
  1312. struct adv7842_state *state = to_state(sd);
  1313. struct v4l2_bt_timings *bt;
  1314. int err;
  1315. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  1316. if (state->mode == ADV7842_MODE_SDP)
  1317. return -ENODATA;
  1318. if (v4l2_match_dv_timings(&state->timings, timings, 0)) {
  1319. v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
  1320. return 0;
  1321. }
  1322. bt = &timings->bt;
  1323. if (!v4l2_valid_dv_timings(timings, adv7842_get_dv_timings_cap(sd),
  1324. adv7842_check_dv_timings, NULL))
  1325. return -ERANGE;
  1326. adv7842_fill_optional_dv_timings_fields(sd, timings);
  1327. state->timings = *timings;
  1328. cp_write(sd, 0x91, bt->interlaced ? 0x40 : 0x00);
  1329. /* Use prim_mode and vid_std when available */
  1330. err = configure_predefined_video_timings(sd, timings);
  1331. if (err) {
  1332. /* custom settings when the video format
  1333. does not have prim_mode/vid_std */
  1334. configure_custom_video_timings(sd, bt);
  1335. }
  1336. set_rgb_quantization_range(sd);
  1337. if (debug > 1)
  1338. v4l2_print_dv_timings(sd->name, "adv7842_s_dv_timings: ",
  1339. timings, true);
  1340. return 0;
  1341. }
  1342. static int adv7842_g_dv_timings(struct v4l2_subdev *sd,
  1343. struct v4l2_dv_timings *timings)
  1344. {
  1345. struct adv7842_state *state = to_state(sd);
  1346. if (state->mode == ADV7842_MODE_SDP)
  1347. return -ENODATA;
  1348. *timings = state->timings;
  1349. return 0;
  1350. }
  1351. static void enable_input(struct v4l2_subdev *sd)
  1352. {
  1353. struct adv7842_state *state = to_state(sd);
  1354. set_rgb_quantization_range(sd);
  1355. switch (state->mode) {
  1356. case ADV7842_MODE_SDP:
  1357. case ADV7842_MODE_COMP:
  1358. case ADV7842_MODE_RGB:
  1359. io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
  1360. break;
  1361. case ADV7842_MODE_HDMI:
  1362. hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */
  1363. io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
  1364. hdmi_write_and_or(sd, 0x1a, 0xef, 0x00); /* Unmute audio */
  1365. break;
  1366. default:
  1367. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  1368. __func__, state->mode);
  1369. break;
  1370. }
  1371. }
  1372. static void disable_input(struct v4l2_subdev *sd)
  1373. {
  1374. hdmi_write_and_or(sd, 0x1a, 0xef, 0x10); /* Mute audio [REF_01, c. 2.2.2] */
  1375. msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 8.29] */
  1376. io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
  1377. hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */
  1378. }
  1379. static void sdp_csc_coeff(struct v4l2_subdev *sd,
  1380. const struct adv7842_sdp_csc_coeff *c)
  1381. {
  1382. /* csc auto/manual */
  1383. sdp_io_write_and_or(sd, 0xe0, 0xbf, c->manual ? 0x00 : 0x40);
  1384. if (!c->manual)
  1385. return;
  1386. /* csc scaling */
  1387. sdp_io_write_and_or(sd, 0xe0, 0x7f, c->scaling == 2 ? 0x80 : 0x00);
  1388. /* A coeff */
  1389. sdp_io_write_and_or(sd, 0xe0, 0xe0, c->A1 >> 8);
  1390. sdp_io_write(sd, 0xe1, c->A1);
  1391. sdp_io_write_and_or(sd, 0xe2, 0xe0, c->A2 >> 8);
  1392. sdp_io_write(sd, 0xe3, c->A2);
  1393. sdp_io_write_and_or(sd, 0xe4, 0xe0, c->A3 >> 8);
  1394. sdp_io_write(sd, 0xe5, c->A3);
  1395. /* A scale */
  1396. sdp_io_write_and_or(sd, 0xe6, 0x80, c->A4 >> 8);
  1397. sdp_io_write(sd, 0xe7, c->A4);
  1398. /* B coeff */
  1399. sdp_io_write_and_or(sd, 0xe8, 0xe0, c->B1 >> 8);
  1400. sdp_io_write(sd, 0xe9, c->B1);
  1401. sdp_io_write_and_or(sd, 0xea, 0xe0, c->B2 >> 8);
  1402. sdp_io_write(sd, 0xeb, c->B2);
  1403. sdp_io_write_and_or(sd, 0xec, 0xe0, c->B3 >> 8);
  1404. sdp_io_write(sd, 0xed, c->B3);
  1405. /* B scale */
  1406. sdp_io_write_and_or(sd, 0xee, 0x80, c->B4 >> 8);
  1407. sdp_io_write(sd, 0xef, c->B4);
  1408. /* C coeff */
  1409. sdp_io_write_and_or(sd, 0xf0, 0xe0, c->C1 >> 8);
  1410. sdp_io_write(sd, 0xf1, c->C1);
  1411. sdp_io_write_and_or(sd, 0xf2, 0xe0, c->C2 >> 8);
  1412. sdp_io_write(sd, 0xf3, c->C2);
  1413. sdp_io_write_and_or(sd, 0xf4, 0xe0, c->C3 >> 8);
  1414. sdp_io_write(sd, 0xf5, c->C3);
  1415. /* C scale */
  1416. sdp_io_write_and_or(sd, 0xf6, 0x80, c->C4 >> 8);
  1417. sdp_io_write(sd, 0xf7, c->C4);
  1418. }
  1419. static void select_input(struct v4l2_subdev *sd,
  1420. enum adv7842_vid_std_select vid_std_select)
  1421. {
  1422. struct adv7842_state *state = to_state(sd);
  1423. switch (state->mode) {
  1424. case ADV7842_MODE_SDP:
  1425. io_write(sd, 0x00, vid_std_select); /* video std: CVBS or YC mode */
  1426. io_write(sd, 0x01, 0); /* prim mode */
  1427. /* enable embedded syncs for auto graphics mode */
  1428. cp_write_and_or(sd, 0x81, 0xef, 0x10);
  1429. afe_write(sd, 0x00, 0x00); /* power up ADC */
  1430. afe_write(sd, 0xc8, 0x00); /* phase control */
  1431. io_write(sd, 0xdd, 0x90); /* Manual 2x output clock */
  1432. /* script says register 0xde, which don't exist in manual */
  1433. /* Manual analog input muxing mode, CVBS (6.4)*/
  1434. afe_write_and_or(sd, 0x02, 0x7f, 0x80);
  1435. if (vid_std_select == ADV7842_SDP_VID_STD_CVBS_SD_4x1) {
  1436. afe_write(sd, 0x03, 0xa0); /* ADC0 to AIN10 (CVBS), ADC1 N/C*/
  1437. afe_write(sd, 0x04, 0x00); /* ADC2 N/C,ADC3 N/C*/
  1438. } else {
  1439. afe_write(sd, 0x03, 0xa0); /* ADC0 to AIN10 (CVBS), ADC1 N/C*/
  1440. afe_write(sd, 0x04, 0xc0); /* ADC2 to AIN12, ADC3 N/C*/
  1441. }
  1442. afe_write(sd, 0x0c, 0x1f); /* ADI recommend write */
  1443. afe_write(sd, 0x12, 0x63); /* ADI recommend write */
  1444. sdp_io_write(sd, 0xb2, 0x60); /* Disable AV codes */
  1445. sdp_io_write(sd, 0xc8, 0xe3); /* Disable Ancillary data */
  1446. /* SDP recommended settings */
  1447. sdp_write(sd, 0x00, 0x3F); /* Autodetect PAL NTSC (not SECAM) */
  1448. sdp_write(sd, 0x01, 0x00); /* Pedestal Off */
  1449. sdp_write(sd, 0x03, 0xE4); /* Manual VCR Gain Luma 0x40B */
  1450. sdp_write(sd, 0x04, 0x0B); /* Manual Luma setting */
  1451. sdp_write(sd, 0x05, 0xC3); /* Manual Chroma setting 0x3FE */
  1452. sdp_write(sd, 0x06, 0xFE); /* Manual Chroma setting */
  1453. sdp_write(sd, 0x12, 0x0D); /* Frame TBC,I_P, 3D comb enabled */
  1454. sdp_write(sd, 0xA7, 0x00); /* ADI Recommended Write */
  1455. sdp_io_write(sd, 0xB0, 0x00); /* Disable H and v blanking */
  1456. /* deinterlacer enabled and 3D comb */
  1457. sdp_write_and_or(sd, 0x12, 0xf6, 0x09);
  1458. break;
  1459. case ADV7842_MODE_COMP:
  1460. case ADV7842_MODE_RGB:
  1461. /* Automatic analog input muxing mode */
  1462. afe_write_and_or(sd, 0x02, 0x7f, 0x00);
  1463. /* set mode and select free run resolution */
  1464. io_write(sd, 0x00, vid_std_select); /* video std */
  1465. io_write(sd, 0x01, 0x02); /* prim mode */
  1466. cp_write_and_or(sd, 0x81, 0xef, 0x10); /* enable embedded syncs
  1467. for auto graphics mode */
  1468. afe_write(sd, 0x00, 0x00); /* power up ADC */
  1469. afe_write(sd, 0xc8, 0x00); /* phase control */
  1470. if (state->mode == ADV7842_MODE_COMP) {
  1471. /* force to YCrCb */
  1472. io_write_and_or(sd, 0x02, 0x0f, 0x60);
  1473. } else {
  1474. /* force to RGB */
  1475. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  1476. }
  1477. /* set ADI recommended settings for digitizer */
  1478. /* "ADV7842 Register Settings Recommendations
  1479. * (rev. 1.8, November 2010)" p. 9. */
  1480. afe_write(sd, 0x0c, 0x1f); /* ADC Range improvement */
  1481. afe_write(sd, 0x12, 0x63); /* ADC Range improvement */
  1482. /* set to default gain for RGB */
  1483. cp_write(sd, 0x73, 0x10);
  1484. cp_write(sd, 0x74, 0x04);
  1485. cp_write(sd, 0x75, 0x01);
  1486. cp_write(sd, 0x76, 0x00);
  1487. cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
  1488. cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
  1489. cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
  1490. break;
  1491. case ADV7842_MODE_HDMI:
  1492. /* Automatic analog input muxing mode */
  1493. afe_write_and_or(sd, 0x02, 0x7f, 0x00);
  1494. /* set mode and select free run resolution */
  1495. if (state->hdmi_port_a)
  1496. hdmi_write(sd, 0x00, 0x02); /* select port A */
  1497. else
  1498. hdmi_write(sd, 0x00, 0x03); /* select port B */
  1499. io_write(sd, 0x00, vid_std_select); /* video std */
  1500. io_write(sd, 0x01, 5); /* prim mode */
  1501. cp_write_and_or(sd, 0x81, 0xef, 0x00); /* disable embedded syncs
  1502. for auto graphics mode */
  1503. /* set ADI recommended settings for HDMI: */
  1504. /* "ADV7842 Register Settings Recommendations
  1505. * (rev. 1.8, November 2010)" p. 3. */
  1506. hdmi_write(sd, 0xc0, 0x00);
  1507. hdmi_write(sd, 0x0d, 0x34); /* ADI recommended write */
  1508. hdmi_write(sd, 0x3d, 0x10); /* ADI recommended write */
  1509. hdmi_write(sd, 0x44, 0x85); /* TMDS PLL optimization */
  1510. hdmi_write(sd, 0x46, 0x1f); /* ADI recommended write */
  1511. hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */
  1512. hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */
  1513. hdmi_write(sd, 0x60, 0x88); /* TMDS PLL optimization */
  1514. hdmi_write(sd, 0x61, 0x88); /* TMDS PLL optimization */
  1515. hdmi_write(sd, 0x6c, 0x18); /* Disable ISRC clearing bit,
  1516. Improve robustness */
  1517. hdmi_write(sd, 0x75, 0x10); /* DDC drive strength */
  1518. hdmi_write(sd, 0x85, 0x1f); /* equaliser */
  1519. hdmi_write(sd, 0x87, 0x70); /* ADI recommended write */
  1520. hdmi_write(sd, 0x89, 0x04); /* equaliser */
  1521. hdmi_write(sd, 0x8a, 0x1e); /* equaliser */
  1522. hdmi_write(sd, 0x93, 0x04); /* equaliser */
  1523. hdmi_write(sd, 0x94, 0x1e); /* equaliser */
  1524. hdmi_write(sd, 0x99, 0xa1); /* ADI recommended write */
  1525. hdmi_write(sd, 0x9b, 0x09); /* ADI recommended write */
  1526. hdmi_write(sd, 0x9d, 0x02); /* equaliser */
  1527. afe_write(sd, 0x00, 0xff); /* power down ADC */
  1528. afe_write(sd, 0xc8, 0x40); /* phase control */
  1529. /* set to default gain for HDMI */
  1530. cp_write(sd, 0x73, 0x10);
  1531. cp_write(sd, 0x74, 0x04);
  1532. cp_write(sd, 0x75, 0x01);
  1533. cp_write(sd, 0x76, 0x00);
  1534. /* reset ADI recommended settings for digitizer */
  1535. /* "ADV7842 Register Settings Recommendations
  1536. * (rev. 2.5, June 2010)" p. 17. */
  1537. afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
  1538. afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
  1539. cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
  1540. /* CP coast control */
  1541. cp_write(sd, 0xc3, 0x33); /* Component mode */
  1542. /* color space conversion, autodetect color space */
  1543. io_write_and_or(sd, 0x02, 0x0f, 0xf0);
  1544. break;
  1545. default:
  1546. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  1547. __func__, state->mode);
  1548. break;
  1549. }
  1550. }
  1551. static int adv7842_s_routing(struct v4l2_subdev *sd,
  1552. u32 input, u32 output, u32 config)
  1553. {
  1554. struct adv7842_state *state = to_state(sd);
  1555. v4l2_dbg(2, debug, sd, "%s: input %d\n", __func__, input);
  1556. switch (input) {
  1557. case ADV7842_SELECT_HDMI_PORT_A:
  1558. state->mode = ADV7842_MODE_HDMI;
  1559. state->vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P;
  1560. state->hdmi_port_a = true;
  1561. break;
  1562. case ADV7842_SELECT_HDMI_PORT_B:
  1563. state->mode = ADV7842_MODE_HDMI;
  1564. state->vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P;
  1565. state->hdmi_port_a = false;
  1566. break;
  1567. case ADV7842_SELECT_VGA_COMP:
  1568. state->mode = ADV7842_MODE_COMP;
  1569. state->vid_std_select = ADV7842_RGB_VID_STD_AUTO_GRAPH_MODE;
  1570. break;
  1571. case ADV7842_SELECT_VGA_RGB:
  1572. state->mode = ADV7842_MODE_RGB;
  1573. state->vid_std_select = ADV7842_RGB_VID_STD_AUTO_GRAPH_MODE;
  1574. break;
  1575. case ADV7842_SELECT_SDP_CVBS:
  1576. state->mode = ADV7842_MODE_SDP;
  1577. state->vid_std_select = ADV7842_SDP_VID_STD_CVBS_SD_4x1;
  1578. break;
  1579. case ADV7842_SELECT_SDP_YC:
  1580. state->mode = ADV7842_MODE_SDP;
  1581. state->vid_std_select = ADV7842_SDP_VID_STD_YC_SD4_x1;
  1582. break;
  1583. default:
  1584. return -EINVAL;
  1585. }
  1586. disable_input(sd);
  1587. select_input(sd, state->vid_std_select);
  1588. enable_input(sd);
  1589. v4l2_subdev_notify(sd, ADV7842_FMT_CHANGE, NULL);
  1590. return 0;
  1591. }
  1592. static int adv7842_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
  1593. u32 *code)
  1594. {
  1595. if (index)
  1596. return -EINVAL;
  1597. /* Good enough for now */
  1598. *code = MEDIA_BUS_FMT_FIXED;
  1599. return 0;
  1600. }
  1601. static int adv7842_g_mbus_fmt(struct v4l2_subdev *sd,
  1602. struct v4l2_mbus_framefmt *fmt)
  1603. {
  1604. struct adv7842_state *state = to_state(sd);
  1605. fmt->width = state->timings.bt.width;
  1606. fmt->height = state->timings.bt.height;
  1607. fmt->code = MEDIA_BUS_FMT_FIXED;
  1608. fmt->field = V4L2_FIELD_NONE;
  1609. if (state->mode == ADV7842_MODE_SDP) {
  1610. /* SPD block */
  1611. if (!(sdp_read(sd, 0x5A) & 0x01))
  1612. return -EINVAL;
  1613. fmt->width = 720;
  1614. /* valid signal */
  1615. if (state->norm & V4L2_STD_525_60)
  1616. fmt->height = 480;
  1617. else
  1618. fmt->height = 576;
  1619. fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  1620. return 0;
  1621. }
  1622. if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
  1623. fmt->colorspace = (state->timings.bt.height <= 576) ?
  1624. V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
  1625. }
  1626. return 0;
  1627. }
  1628. static void adv7842_irq_enable(struct v4l2_subdev *sd, bool enable)
  1629. {
  1630. if (enable) {
  1631. /* Enable SSPD, STDI and CP locked/unlocked interrupts */
  1632. io_write(sd, 0x46, 0x9c);
  1633. /* ESDP_50HZ_DET interrupt */
  1634. io_write(sd, 0x5a, 0x10);
  1635. /* Enable CABLE_DET_A/B_ST (+5v) interrupt */
  1636. io_write(sd, 0x73, 0x03);
  1637. /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
  1638. io_write(sd, 0x78, 0x03);
  1639. /* Enable SDP Standard Detection Change and SDP Video Detected */
  1640. io_write(sd, 0xa0, 0x09);
  1641. /* Enable HDMI_MODE interrupt */
  1642. io_write(sd, 0x69, 0x08);
  1643. } else {
  1644. io_write(sd, 0x46, 0x0);
  1645. io_write(sd, 0x5a, 0x0);
  1646. io_write(sd, 0x73, 0x0);
  1647. io_write(sd, 0x78, 0x0);
  1648. io_write(sd, 0xa0, 0x0);
  1649. io_write(sd, 0x69, 0x0);
  1650. }
  1651. }
  1652. static int adv7842_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  1653. {
  1654. struct adv7842_state *state = to_state(sd);
  1655. u8 fmt_change_cp, fmt_change_digital, fmt_change_sdp;
  1656. u8 irq_status[6];
  1657. adv7842_irq_enable(sd, false);
  1658. /* read status */
  1659. irq_status[0] = io_read(sd, 0x43);
  1660. irq_status[1] = io_read(sd, 0x57);
  1661. irq_status[2] = io_read(sd, 0x70);
  1662. irq_status[3] = io_read(sd, 0x75);
  1663. irq_status[4] = io_read(sd, 0x9d);
  1664. irq_status[5] = io_read(sd, 0x66);
  1665. /* and clear */
  1666. if (irq_status[0])
  1667. io_write(sd, 0x44, irq_status[0]);
  1668. if (irq_status[1])
  1669. io_write(sd, 0x58, irq_status[1]);
  1670. if (irq_status[2])
  1671. io_write(sd, 0x71, irq_status[2]);
  1672. if (irq_status[3])
  1673. io_write(sd, 0x76, irq_status[3]);
  1674. if (irq_status[4])
  1675. io_write(sd, 0x9e, irq_status[4]);
  1676. if (irq_status[5])
  1677. io_write(sd, 0x67, irq_status[5]);
  1678. adv7842_irq_enable(sd, true);
  1679. v4l2_dbg(1, debug, sd, "%s: irq %x, %x, %x, %x, %x, %x\n", __func__,
  1680. irq_status[0], irq_status[1], irq_status[2],
  1681. irq_status[3], irq_status[4], irq_status[5]);
  1682. /* format change CP */
  1683. fmt_change_cp = irq_status[0] & 0x9c;
  1684. /* format change SDP */
  1685. if (state->mode == ADV7842_MODE_SDP)
  1686. fmt_change_sdp = (irq_status[1] & 0x30) | (irq_status[4] & 0x09);
  1687. else
  1688. fmt_change_sdp = 0;
  1689. /* digital format CP */
  1690. if (is_digital_input(sd))
  1691. fmt_change_digital = irq_status[3] & 0x03;
  1692. else
  1693. fmt_change_digital = 0;
  1694. /* format change */
  1695. if (fmt_change_cp || fmt_change_digital || fmt_change_sdp) {
  1696. v4l2_dbg(1, debug, sd,
  1697. "%s: fmt_change_cp = 0x%x, fmt_change_digital = 0x%x, fmt_change_sdp = 0x%x\n",
  1698. __func__, fmt_change_cp, fmt_change_digital,
  1699. fmt_change_sdp);
  1700. v4l2_subdev_notify(sd, ADV7842_FMT_CHANGE, NULL);
  1701. if (handled)
  1702. *handled = true;
  1703. }
  1704. /* HDMI/DVI mode */
  1705. if (irq_status[5] & 0x08) {
  1706. v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
  1707. (io_read(sd, 0x65) & 0x08) ? "HDMI" : "DVI");
  1708. set_rgb_quantization_range(sd);
  1709. if (handled)
  1710. *handled = true;
  1711. }
  1712. /* tx 5v detect */
  1713. if (irq_status[2] & 0x3) {
  1714. v4l2_dbg(1, debug, sd, "%s: irq tx_5v\n", __func__);
  1715. adv7842_s_detect_tx_5v_ctrl(sd);
  1716. if (handled)
  1717. *handled = true;
  1718. }
  1719. return 0;
  1720. }
  1721. static int adv7842_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
  1722. {
  1723. struct adv7842_state *state = to_state(sd);
  1724. u8 *data = NULL;
  1725. memset(edid->reserved, 0, sizeof(edid->reserved));
  1726. switch (edid->pad) {
  1727. case ADV7842_EDID_PORT_A:
  1728. case ADV7842_EDID_PORT_B:
  1729. if (state->hdmi_edid.present & (0x04 << edid->pad))
  1730. data = state->hdmi_edid.edid;
  1731. break;
  1732. case ADV7842_EDID_PORT_VGA:
  1733. if (state->vga_edid.present)
  1734. data = state->vga_edid.edid;
  1735. break;
  1736. default:
  1737. return -EINVAL;
  1738. }
  1739. if (edid->start_block == 0 && edid->blocks == 0) {
  1740. edid->blocks = data ? 2 : 0;
  1741. return 0;
  1742. }
  1743. if (!data)
  1744. return -ENODATA;
  1745. if (edid->start_block >= 2)
  1746. return -EINVAL;
  1747. if (edid->start_block + edid->blocks > 2)
  1748. edid->blocks = 2 - edid->start_block;
  1749. memcpy(edid->edid, data + edid->start_block * 128, edid->blocks * 128);
  1750. return 0;
  1751. }
  1752. static int adv7842_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *e)
  1753. {
  1754. struct adv7842_state *state = to_state(sd);
  1755. int err = 0;
  1756. memset(e->reserved, 0, sizeof(e->reserved));
  1757. if (e->pad > ADV7842_EDID_PORT_VGA)
  1758. return -EINVAL;
  1759. if (e->start_block != 0)
  1760. return -EINVAL;
  1761. if (e->blocks > 2) {
  1762. e->blocks = 2;
  1763. return -E2BIG;
  1764. }
  1765. /* todo, per edid */
  1766. state->aspect_ratio = v4l2_calc_aspect_ratio(e->edid[0x15],
  1767. e->edid[0x16]);
  1768. switch (e->pad) {
  1769. case ADV7842_EDID_PORT_VGA:
  1770. memset(&state->vga_edid.edid, 0, 256);
  1771. state->vga_edid.present = e->blocks ? 0x1 : 0x0;
  1772. memcpy(&state->vga_edid.edid, e->edid, 128 * e->blocks);
  1773. err = edid_write_vga_segment(sd);
  1774. break;
  1775. case ADV7842_EDID_PORT_A:
  1776. case ADV7842_EDID_PORT_B:
  1777. memset(&state->hdmi_edid.edid, 0, 256);
  1778. if (e->blocks)
  1779. state->hdmi_edid.present |= 0x04 << e->pad;
  1780. else
  1781. state->hdmi_edid.present &= ~(0x04 << e->pad);
  1782. memcpy(&state->hdmi_edid.edid, e->edid, 128 * e->blocks);
  1783. err = edid_write_hdmi_segment(sd, e->pad);
  1784. break;
  1785. default:
  1786. return -EINVAL;
  1787. }
  1788. if (err < 0)
  1789. v4l2_err(sd, "error %d writing edid on port %d\n", err, e->pad);
  1790. return err;
  1791. }
  1792. /*********** avi info frame CEA-861-E **************/
  1793. /* TODO move to common library */
  1794. struct avi_info_frame {
  1795. uint8_t f17;
  1796. uint8_t y10;
  1797. uint8_t a0;
  1798. uint8_t b10;
  1799. uint8_t s10;
  1800. uint8_t c10;
  1801. uint8_t m10;
  1802. uint8_t r3210;
  1803. uint8_t itc;
  1804. uint8_t ec210;
  1805. uint8_t q10;
  1806. uint8_t sc10;
  1807. uint8_t f47;
  1808. uint8_t vic;
  1809. uint8_t yq10;
  1810. uint8_t cn10;
  1811. uint8_t pr3210;
  1812. uint16_t etb;
  1813. uint16_t sbb;
  1814. uint16_t elb;
  1815. uint16_t srb;
  1816. };
  1817. static const char *y10_txt[4] = {
  1818. "RGB",
  1819. "YCbCr 4:2:2",
  1820. "YCbCr 4:4:4",
  1821. "Future",
  1822. };
  1823. static const char *c10_txt[4] = {
  1824. "No Data",
  1825. "SMPTE 170M",
  1826. "ITU-R 709",
  1827. "Extended Colorimetry information valied",
  1828. };
  1829. static const char *itc_txt[2] = {
  1830. "No Data",
  1831. "IT content",
  1832. };
  1833. static const char *ec210_txt[8] = {
  1834. "xvYCC601",
  1835. "xvYCC709",
  1836. "sYCC601",
  1837. "AdobeYCC601",
  1838. "AdobeRGB",
  1839. "5 reserved",
  1840. "6 reserved",
  1841. "7 reserved",
  1842. };
  1843. static const char *q10_txt[4] = {
  1844. "Default",
  1845. "Limited Range",
  1846. "Full Range",
  1847. "Reserved",
  1848. };
  1849. static void parse_avi_infoframe(struct v4l2_subdev *sd, uint8_t *buf,
  1850. struct avi_info_frame *avi)
  1851. {
  1852. avi->f17 = (buf[1] >> 7) & 0x1;
  1853. avi->y10 = (buf[1] >> 5) & 0x3;
  1854. avi->a0 = (buf[1] >> 4) & 0x1;
  1855. avi->b10 = (buf[1] >> 2) & 0x3;
  1856. avi->s10 = buf[1] & 0x3;
  1857. avi->c10 = (buf[2] >> 6) & 0x3;
  1858. avi->m10 = (buf[2] >> 4) & 0x3;
  1859. avi->r3210 = buf[2] & 0xf;
  1860. avi->itc = (buf[3] >> 7) & 0x1;
  1861. avi->ec210 = (buf[3] >> 4) & 0x7;
  1862. avi->q10 = (buf[3] >> 2) & 0x3;
  1863. avi->sc10 = buf[3] & 0x3;
  1864. avi->f47 = (buf[4] >> 7) & 0x1;
  1865. avi->vic = buf[4] & 0x7f;
  1866. avi->yq10 = (buf[5] >> 6) & 0x3;
  1867. avi->cn10 = (buf[5] >> 4) & 0x3;
  1868. avi->pr3210 = buf[5] & 0xf;
  1869. avi->etb = buf[6] + 256*buf[7];
  1870. avi->sbb = buf[8] + 256*buf[9];
  1871. avi->elb = buf[10] + 256*buf[11];
  1872. avi->srb = buf[12] + 256*buf[13];
  1873. }
  1874. static void print_avi_infoframe(struct v4l2_subdev *sd)
  1875. {
  1876. int i;
  1877. uint8_t buf[14];
  1878. u8 avi_len;
  1879. u8 avi_ver;
  1880. struct avi_info_frame avi;
  1881. if (!(hdmi_read(sd, 0x05) & 0x80)) {
  1882. v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
  1883. return;
  1884. }
  1885. if (!(io_read(sd, 0x60) & 0x01)) {
  1886. v4l2_info(sd, "AVI infoframe not received\n");
  1887. return;
  1888. }
  1889. if (io_read(sd, 0x88) & 0x10) {
  1890. v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
  1891. io_write(sd, 0x8a, 0x10); /* clear AVI_INF_CKS_ERR_RAW */
  1892. if (io_read(sd, 0x88) & 0x10) {
  1893. v4l2_info(sd, "AVI infoframe checksum error still present\n");
  1894. io_write(sd, 0x8a, 0x10); /* clear AVI_INF_CKS_ERR_RAW */
  1895. }
  1896. }
  1897. avi_len = infoframe_read(sd, 0xe2);
  1898. avi_ver = infoframe_read(sd, 0xe1);
  1899. v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
  1900. avi_ver, avi_len);
  1901. if (avi_ver != 0x02)
  1902. return;
  1903. for (i = 0; i < 14; i++)
  1904. buf[i] = infoframe_read(sd, i);
  1905. v4l2_info(sd, "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1906. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
  1907. buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
  1908. parse_avi_infoframe(sd, buf, &avi);
  1909. if (avi.vic)
  1910. v4l2_info(sd, "\tVIC: %d\n", avi.vic);
  1911. if (avi.itc)
  1912. v4l2_info(sd, "\t%s\n", itc_txt[avi.itc]);
  1913. if (avi.y10)
  1914. v4l2_info(sd, "\t%s %s\n", y10_txt[avi.y10], !avi.c10 ? "" :
  1915. (avi.c10 == 0x3 ? ec210_txt[avi.ec210] : c10_txt[avi.c10]));
  1916. else
  1917. v4l2_info(sd, "\t%s %s\n", y10_txt[avi.y10], q10_txt[avi.q10]);
  1918. }
  1919. static const char * const prim_mode_txt[] = {
  1920. "SDP",
  1921. "Component",
  1922. "Graphics",
  1923. "Reserved",
  1924. "CVBS & HDMI AUDIO",
  1925. "HDMI-Comp",
  1926. "HDMI-GR",
  1927. "Reserved",
  1928. "Reserved",
  1929. "Reserved",
  1930. "Reserved",
  1931. "Reserved",
  1932. "Reserved",
  1933. "Reserved",
  1934. "Reserved",
  1935. "Reserved",
  1936. };
  1937. static int adv7842_sdp_log_status(struct v4l2_subdev *sd)
  1938. {
  1939. /* SDP (Standard definition processor) block */
  1940. uint8_t sdp_signal_detected = sdp_read(sd, 0x5A) & 0x01;
  1941. v4l2_info(sd, "Chip powered %s\n", no_power(sd) ? "off" : "on");
  1942. v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x\n",
  1943. io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f);
  1944. v4l2_info(sd, "SDP: free run: %s\n",
  1945. (sdp_read(sd, 0x56) & 0x01) ? "on" : "off");
  1946. v4l2_info(sd, "SDP: %s\n", sdp_signal_detected ?
  1947. "valid SD/PR signal detected" : "invalid/no signal");
  1948. if (sdp_signal_detected) {
  1949. static const char * const sdp_std_txt[] = {
  1950. "NTSC-M/J",
  1951. "1?",
  1952. "NTSC-443",
  1953. "60HzSECAM",
  1954. "PAL-M",
  1955. "5?",
  1956. "PAL-60",
  1957. "7?", "8?", "9?", "a?", "b?",
  1958. "PAL-CombN",
  1959. "d?",
  1960. "PAL-BGHID",
  1961. "SECAM"
  1962. };
  1963. v4l2_info(sd, "SDP: standard %s\n",
  1964. sdp_std_txt[sdp_read(sd, 0x52) & 0x0f]);
  1965. v4l2_info(sd, "SDP: %s\n",
  1966. (sdp_read(sd, 0x59) & 0x08) ? "50Hz" : "60Hz");
  1967. v4l2_info(sd, "SDP: %s\n",
  1968. (sdp_read(sd, 0x57) & 0x08) ? "Interlaced" : "Progressive");
  1969. v4l2_info(sd, "SDP: deinterlacer %s\n",
  1970. (sdp_read(sd, 0x12) & 0x08) ? "enabled" : "disabled");
  1971. v4l2_info(sd, "SDP: csc %s mode\n",
  1972. (sdp_io_read(sd, 0xe0) & 0x40) ? "auto" : "manual");
  1973. }
  1974. return 0;
  1975. }
  1976. static int adv7842_cp_log_status(struct v4l2_subdev *sd)
  1977. {
  1978. /* CP block */
  1979. struct adv7842_state *state = to_state(sd);
  1980. struct v4l2_dv_timings timings;
  1981. uint8_t reg_io_0x02 = io_read(sd, 0x02);
  1982. uint8_t reg_io_0x21 = io_read(sd, 0x21);
  1983. uint8_t reg_rep_0x77 = rep_read(sd, 0x77);
  1984. uint8_t reg_rep_0x7d = rep_read(sd, 0x7d);
  1985. bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
  1986. bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
  1987. bool audio_mute = io_read(sd, 0x65) & 0x40;
  1988. static const char * const csc_coeff_sel_rb[16] = {
  1989. "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
  1990. "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
  1991. "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
  1992. "reserved", "reserved", "reserved", "reserved", "manual"
  1993. };
  1994. static const char * const input_color_space_txt[16] = {
  1995. "RGB limited range (16-235)", "RGB full range (0-255)",
  1996. "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
  1997. "xvYCC Bt.601", "xvYCC Bt.709",
  1998. "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
  1999. "invalid", "invalid", "invalid", "invalid", "invalid",
  2000. "invalid", "invalid", "automatic"
  2001. };
  2002. static const char * const rgb_quantization_range_txt[] = {
  2003. "Automatic",
  2004. "RGB limited range (16-235)",
  2005. "RGB full range (0-255)",
  2006. };
  2007. static const char * const deep_color_mode_txt[4] = {
  2008. "8-bits per channel",
  2009. "10-bits per channel",
  2010. "12-bits per channel",
  2011. "16-bits per channel (not supported)"
  2012. };
  2013. v4l2_info(sd, "-----Chip status-----\n");
  2014. v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
  2015. v4l2_info(sd, "HDMI/DVI-D port selected: %s\n",
  2016. state->hdmi_port_a ? "A" : "B");
  2017. v4l2_info(sd, "EDID A %s, B %s\n",
  2018. ((reg_rep_0x7d & 0x04) && (reg_rep_0x77 & 0x04)) ?
  2019. "enabled" : "disabled",
  2020. ((reg_rep_0x7d & 0x08) && (reg_rep_0x77 & 0x08)) ?
  2021. "enabled" : "disabled");
  2022. v4l2_info(sd, "HPD A %s, B %s\n",
  2023. reg_io_0x21 & 0x02 ? "enabled" : "disabled",
  2024. reg_io_0x21 & 0x01 ? "enabled" : "disabled");
  2025. v4l2_info(sd, "CEC %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
  2026. "enabled" : "disabled");
  2027. v4l2_info(sd, "-----Signal status-----\n");
  2028. if (state->hdmi_port_a) {
  2029. v4l2_info(sd, "Cable detected (+5V power): %s\n",
  2030. io_read(sd, 0x6f) & 0x02 ? "true" : "false");
  2031. v4l2_info(sd, "TMDS signal detected: %s\n",
  2032. (io_read(sd, 0x6a) & 0x02) ? "true" : "false");
  2033. v4l2_info(sd, "TMDS signal locked: %s\n",
  2034. (io_read(sd, 0x6a) & 0x20) ? "true" : "false");
  2035. } else {
  2036. v4l2_info(sd, "Cable detected (+5V power):%s\n",
  2037. io_read(sd, 0x6f) & 0x01 ? "true" : "false");
  2038. v4l2_info(sd, "TMDS signal detected: %s\n",
  2039. (io_read(sd, 0x6a) & 0x01) ? "true" : "false");
  2040. v4l2_info(sd, "TMDS signal locked: %s\n",
  2041. (io_read(sd, 0x6a) & 0x10) ? "true" : "false");
  2042. }
  2043. v4l2_info(sd, "CP free run: %s\n",
  2044. (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
  2045. v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
  2046. io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
  2047. (io_read(sd, 0x01) & 0x70) >> 4);
  2048. v4l2_info(sd, "-----Video Timings-----\n");
  2049. if (no_cp_signal(sd)) {
  2050. v4l2_info(sd, "STDI: not locked\n");
  2051. } else {
  2052. uint32_t bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
  2053. uint32_t lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
  2054. uint32_t lcvs = cp_read(sd, 0xb3) >> 3;
  2055. uint32_t fcl = ((cp_read(sd, 0xb8) & 0x1f) << 8) | cp_read(sd, 0xb9);
  2056. char hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
  2057. ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
  2058. char vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
  2059. ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
  2060. v4l2_info(sd,
  2061. "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, fcl = %d, %s, %chsync, %cvsync\n",
  2062. lcf, bl, lcvs, fcl,
  2063. (cp_read(sd, 0xb1) & 0x40) ?
  2064. "interlaced" : "progressive",
  2065. hs_pol, vs_pol);
  2066. }
  2067. if (adv7842_query_dv_timings(sd, &timings))
  2068. v4l2_info(sd, "No video detected\n");
  2069. else
  2070. v4l2_print_dv_timings(sd->name, "Detected format: ",
  2071. &timings, true);
  2072. v4l2_print_dv_timings(sd->name, "Configured format: ",
  2073. &state->timings, true);
  2074. if (no_cp_signal(sd))
  2075. return 0;
  2076. v4l2_info(sd, "-----Color space-----\n");
  2077. v4l2_info(sd, "RGB quantization range ctrl: %s\n",
  2078. rgb_quantization_range_txt[state->rgb_quantization_range]);
  2079. v4l2_info(sd, "Input color space: %s\n",
  2080. input_color_space_txt[reg_io_0x02 >> 4]);
  2081. v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
  2082. (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
  2083. (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
  2084. ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
  2085. "enabled" : "disabled");
  2086. v4l2_info(sd, "Color space conversion: %s\n",
  2087. csc_coeff_sel_rb[cp_read(sd, 0xf4) >> 4]);
  2088. if (!is_digital_input(sd))
  2089. return 0;
  2090. v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
  2091. v4l2_info(sd, "HDCP encrypted content: %s\n",
  2092. (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
  2093. v4l2_info(sd, "HDCP keys read: %s%s\n",
  2094. (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
  2095. (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
  2096. if (!is_hdmi(sd))
  2097. return 0;
  2098. v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
  2099. audio_pll_locked ? "locked" : "not locked",
  2100. audio_sample_packet_detect ? "detected" : "not detected",
  2101. audio_mute ? "muted" : "enabled");
  2102. if (audio_pll_locked && audio_sample_packet_detect) {
  2103. v4l2_info(sd, "Audio format: %s\n",
  2104. (hdmi_read(sd, 0x07) & 0x40) ? "multi-channel" : "stereo");
  2105. }
  2106. v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
  2107. (hdmi_read(sd, 0x5c) << 8) +
  2108. (hdmi_read(sd, 0x5d) & 0xf0));
  2109. v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
  2110. (hdmi_read(sd, 0x5e) << 8) +
  2111. hdmi_read(sd, 0x5f));
  2112. v4l2_info(sd, "AV Mute: %s\n",
  2113. (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
  2114. v4l2_info(sd, "Deep color mode: %s\n",
  2115. deep_color_mode_txt[hdmi_read(sd, 0x0b) >> 6]);
  2116. print_avi_infoframe(sd);
  2117. return 0;
  2118. }
  2119. static int adv7842_log_status(struct v4l2_subdev *sd)
  2120. {
  2121. struct adv7842_state *state = to_state(sd);
  2122. if (state->mode == ADV7842_MODE_SDP)
  2123. return adv7842_sdp_log_status(sd);
  2124. return adv7842_cp_log_status(sd);
  2125. }
  2126. static int adv7842_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  2127. {
  2128. struct adv7842_state *state = to_state(sd);
  2129. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  2130. if (state->mode != ADV7842_MODE_SDP)
  2131. return -ENODATA;
  2132. if (!(sdp_read(sd, 0x5A) & 0x01)) {
  2133. *std = 0;
  2134. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  2135. return 0;
  2136. }
  2137. switch (sdp_read(sd, 0x52) & 0x0f) {
  2138. case 0:
  2139. /* NTSC-M/J */
  2140. *std &= V4L2_STD_NTSC;
  2141. break;
  2142. case 2:
  2143. /* NTSC-443 */
  2144. *std &= V4L2_STD_NTSC_443;
  2145. break;
  2146. case 3:
  2147. /* 60HzSECAM */
  2148. *std &= V4L2_STD_SECAM;
  2149. break;
  2150. case 4:
  2151. /* PAL-M */
  2152. *std &= V4L2_STD_PAL_M;
  2153. break;
  2154. case 6:
  2155. /* PAL-60 */
  2156. *std &= V4L2_STD_PAL_60;
  2157. break;
  2158. case 0xc:
  2159. /* PAL-CombN */
  2160. *std &= V4L2_STD_PAL_Nc;
  2161. break;
  2162. case 0xe:
  2163. /* PAL-BGHID */
  2164. *std &= V4L2_STD_PAL;
  2165. break;
  2166. case 0xf:
  2167. /* SECAM */
  2168. *std &= V4L2_STD_SECAM;
  2169. break;
  2170. default:
  2171. *std &= V4L2_STD_ALL;
  2172. break;
  2173. }
  2174. return 0;
  2175. }
  2176. static void adv7842_s_sdp_io(struct v4l2_subdev *sd, struct adv7842_sdp_io_sync_adjustment *s)
  2177. {
  2178. if (s && s->adjust) {
  2179. sdp_io_write(sd, 0x94, (s->hs_beg >> 8) & 0xf);
  2180. sdp_io_write(sd, 0x95, s->hs_beg & 0xff);
  2181. sdp_io_write(sd, 0x96, (s->hs_width >> 8) & 0xf);
  2182. sdp_io_write(sd, 0x97, s->hs_width & 0xff);
  2183. sdp_io_write(sd, 0x98, (s->de_beg >> 8) & 0xf);
  2184. sdp_io_write(sd, 0x99, s->de_beg & 0xff);
  2185. sdp_io_write(sd, 0x9a, (s->de_end >> 8) & 0xf);
  2186. sdp_io_write(sd, 0x9b, s->de_end & 0xff);
  2187. sdp_io_write(sd, 0xa8, s->vs_beg_o);
  2188. sdp_io_write(sd, 0xa9, s->vs_beg_e);
  2189. sdp_io_write(sd, 0xaa, s->vs_end_o);
  2190. sdp_io_write(sd, 0xab, s->vs_end_e);
  2191. sdp_io_write(sd, 0xac, s->de_v_beg_o);
  2192. sdp_io_write(sd, 0xad, s->de_v_beg_e);
  2193. sdp_io_write(sd, 0xae, s->de_v_end_o);
  2194. sdp_io_write(sd, 0xaf, s->de_v_end_e);
  2195. } else {
  2196. /* set to default */
  2197. sdp_io_write(sd, 0x94, 0x00);
  2198. sdp_io_write(sd, 0x95, 0x00);
  2199. sdp_io_write(sd, 0x96, 0x00);
  2200. sdp_io_write(sd, 0x97, 0x20);
  2201. sdp_io_write(sd, 0x98, 0x00);
  2202. sdp_io_write(sd, 0x99, 0x00);
  2203. sdp_io_write(sd, 0x9a, 0x00);
  2204. sdp_io_write(sd, 0x9b, 0x00);
  2205. sdp_io_write(sd, 0xa8, 0x04);
  2206. sdp_io_write(sd, 0xa9, 0x04);
  2207. sdp_io_write(sd, 0xaa, 0x04);
  2208. sdp_io_write(sd, 0xab, 0x04);
  2209. sdp_io_write(sd, 0xac, 0x04);
  2210. sdp_io_write(sd, 0xad, 0x04);
  2211. sdp_io_write(sd, 0xae, 0x04);
  2212. sdp_io_write(sd, 0xaf, 0x04);
  2213. }
  2214. }
  2215. static int adv7842_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  2216. {
  2217. struct adv7842_state *state = to_state(sd);
  2218. struct adv7842_platform_data *pdata = &state->pdata;
  2219. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  2220. if (state->mode != ADV7842_MODE_SDP)
  2221. return -ENODATA;
  2222. if (norm & V4L2_STD_625_50)
  2223. adv7842_s_sdp_io(sd, &pdata->sdp_io_sync_625);
  2224. else if (norm & V4L2_STD_525_60)
  2225. adv7842_s_sdp_io(sd, &pdata->sdp_io_sync_525);
  2226. else
  2227. adv7842_s_sdp_io(sd, NULL);
  2228. if (norm & V4L2_STD_ALL) {
  2229. state->norm = norm;
  2230. return 0;
  2231. }
  2232. return -EINVAL;
  2233. }
  2234. static int adv7842_g_std(struct v4l2_subdev *sd, v4l2_std_id *norm)
  2235. {
  2236. struct adv7842_state *state = to_state(sd);
  2237. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  2238. if (state->mode != ADV7842_MODE_SDP)
  2239. return -ENODATA;
  2240. *norm = state->norm;
  2241. return 0;
  2242. }
  2243. /* ----------------------------------------------------------------------- */
  2244. static int adv7842_core_init(struct v4l2_subdev *sd)
  2245. {
  2246. struct adv7842_state *state = to_state(sd);
  2247. struct adv7842_platform_data *pdata = &state->pdata;
  2248. hdmi_write(sd, 0x48,
  2249. (pdata->disable_pwrdnb ? 0x80 : 0) |
  2250. (pdata->disable_cable_det_rst ? 0x40 : 0));
  2251. disable_input(sd);
  2252. /*
  2253. * Disable I2C access to internal EDID ram from HDMI DDC ports
  2254. * Disable auto edid enable when leaving powerdown mode
  2255. */
  2256. rep_write_and_or(sd, 0x77, 0xd3, 0x20);
  2257. /* power */
  2258. io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
  2259. io_write(sd, 0x15, 0x80); /* Power up pads */
  2260. /* video format */
  2261. io_write(sd, 0x02,
  2262. 0xf0 |
  2263. pdata->alt_gamma << 3 |
  2264. pdata->op_656_range << 2 |
  2265. pdata->rgb_out << 1 |
  2266. pdata->alt_data_sat << 0);
  2267. io_write(sd, 0x03, pdata->op_format_sel);
  2268. io_write_and_or(sd, 0x04, 0x1f, pdata->op_ch_sel << 5);
  2269. io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
  2270. pdata->insert_av_codes << 2 |
  2271. pdata->replicate_av_codes << 1 |
  2272. pdata->invert_cbcr << 0);
  2273. /* HDMI audio */
  2274. hdmi_write_and_or(sd, 0x1a, 0xf1, 0x08); /* Wait 1 s before unmute */
  2275. /* Drive strength */
  2276. io_write_and_or(sd, 0x14, 0xc0,
  2277. pdata->dr_str_data << 4 |
  2278. pdata->dr_str_clk << 2 |
  2279. pdata->dr_str_sync);
  2280. /* HDMI free run */
  2281. cp_write_and_or(sd, 0xba, 0xfc, pdata->hdmi_free_run_enable |
  2282. (pdata->hdmi_free_run_mode << 1));
  2283. /* SPD free run */
  2284. sdp_write_and_or(sd, 0xdd, 0xf0, pdata->sdp_free_run_force |
  2285. (pdata->sdp_free_run_cbar_en << 1) |
  2286. (pdata->sdp_free_run_man_col_en << 2) |
  2287. (pdata->sdp_free_run_auto << 3));
  2288. /* TODO from platform data */
  2289. cp_write(sd, 0x69, 0x14); /* Enable CP CSC */
  2290. io_write(sd, 0x06, 0xa6); /* positive VS and HS and DE */
  2291. cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
  2292. afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
  2293. afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
  2294. io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
  2295. sdp_csc_coeff(sd, &pdata->sdp_csc_coeff);
  2296. /* todo, improve settings for sdram */
  2297. if (pdata->sd_ram_size >= 128) {
  2298. sdp_write(sd, 0x12, 0x0d); /* Frame TBC,3D comb enabled */
  2299. if (pdata->sd_ram_ddr) {
  2300. /* SDP setup for the AD eval board */
  2301. sdp_io_write(sd, 0x6f, 0x00); /* DDR mode */
  2302. sdp_io_write(sd, 0x75, 0x0a); /* 128 MB memory size */
  2303. sdp_io_write(sd, 0x7a, 0xa5); /* Timing Adjustment */
  2304. sdp_io_write(sd, 0x7b, 0x8f); /* Timing Adjustment */
  2305. sdp_io_write(sd, 0x60, 0x01); /* SDRAM reset */
  2306. } else {
  2307. sdp_io_write(sd, 0x75, 0x0a); /* 64 MB memory size ?*/
  2308. sdp_io_write(sd, 0x74, 0x00); /* must be zero for sdr sdram */
  2309. sdp_io_write(sd, 0x79, 0x33); /* CAS latency to 3,
  2310. depends on memory */
  2311. sdp_io_write(sd, 0x6f, 0x01); /* SDR mode */
  2312. sdp_io_write(sd, 0x7a, 0xa5); /* Timing Adjustment */
  2313. sdp_io_write(sd, 0x7b, 0x8f); /* Timing Adjustment */
  2314. sdp_io_write(sd, 0x60, 0x01); /* SDRAM reset */
  2315. }
  2316. } else {
  2317. /*
  2318. * Manual UG-214, rev 0 is bit confusing on this bit
  2319. * but a '1' disables any signal if the Ram is active.
  2320. */
  2321. sdp_io_write(sd, 0x29, 0x10); /* Tristate memory interface */
  2322. }
  2323. select_input(sd, pdata->vid_std_select);
  2324. enable_input(sd);
  2325. if (pdata->hpa_auto) {
  2326. /* HPA auto, HPA 0.5s after Edid set and Cable detect */
  2327. hdmi_write(sd, 0x69, 0x5c);
  2328. } else {
  2329. /* HPA manual */
  2330. hdmi_write(sd, 0x69, 0xa3);
  2331. /* HPA disable on port A and B */
  2332. io_write_and_or(sd, 0x20, 0xcf, 0x00);
  2333. }
  2334. /* LLC */
  2335. io_write(sd, 0x19, 0x80 | pdata->llc_dll_phase);
  2336. io_write(sd, 0x33, 0x40);
  2337. /* interrupts */
  2338. io_write(sd, 0x40, 0xf2); /* Configure INT1 */
  2339. adv7842_irq_enable(sd, true);
  2340. return v4l2_ctrl_handler_setup(sd->ctrl_handler);
  2341. }
  2342. /* ----------------------------------------------------------------------- */
  2343. static int adv7842_ddr_ram_test(struct v4l2_subdev *sd)
  2344. {
  2345. /*
  2346. * From ADV784x external Memory test.pdf
  2347. *
  2348. * Reset must just been performed before running test.
  2349. * Recommended to reset after test.
  2350. */
  2351. int i;
  2352. int pass = 0;
  2353. int fail = 0;
  2354. int complete = 0;
  2355. io_write(sd, 0x00, 0x01); /* Program SDP 4x1 */
  2356. io_write(sd, 0x01, 0x00); /* Program SDP mode */
  2357. afe_write(sd, 0x80, 0x92); /* SDP Recommeneded Write */
  2358. afe_write(sd, 0x9B, 0x01); /* SDP Recommeneded Write ADV7844ES1 */
  2359. afe_write(sd, 0x9C, 0x60); /* SDP Recommeneded Write ADV7844ES1 */
  2360. afe_write(sd, 0x9E, 0x02); /* SDP Recommeneded Write ADV7844ES1 */
  2361. afe_write(sd, 0xA0, 0x0B); /* SDP Recommeneded Write ADV7844ES1 */
  2362. afe_write(sd, 0xC3, 0x02); /* Memory BIST Initialisation */
  2363. io_write(sd, 0x0C, 0x40); /* Power up ADV7844 */
  2364. io_write(sd, 0x15, 0xBA); /* Enable outputs */
  2365. sdp_write(sd, 0x12, 0x00); /* Disable 3D comb, Frame TBC & 3DNR */
  2366. io_write(sd, 0xFF, 0x04); /* Reset memory controller */
  2367. mdelay(5);
  2368. sdp_write(sd, 0x12, 0x00); /* Disable 3D Comb, Frame TBC & 3DNR */
  2369. sdp_io_write(sd, 0x2A, 0x01); /* Memory BIST Initialisation */
  2370. sdp_io_write(sd, 0x7c, 0x19); /* Memory BIST Initialisation */
  2371. sdp_io_write(sd, 0x80, 0x87); /* Memory BIST Initialisation */
  2372. sdp_io_write(sd, 0x81, 0x4a); /* Memory BIST Initialisation */
  2373. sdp_io_write(sd, 0x82, 0x2c); /* Memory BIST Initialisation */
  2374. sdp_io_write(sd, 0x83, 0x0e); /* Memory BIST Initialisation */
  2375. sdp_io_write(sd, 0x84, 0x94); /* Memory BIST Initialisation */
  2376. sdp_io_write(sd, 0x85, 0x62); /* Memory BIST Initialisation */
  2377. sdp_io_write(sd, 0x7d, 0x00); /* Memory BIST Initialisation */
  2378. sdp_io_write(sd, 0x7e, 0x1a); /* Memory BIST Initialisation */
  2379. mdelay(5);
  2380. sdp_io_write(sd, 0xd9, 0xd5); /* Enable BIST Test */
  2381. sdp_write(sd, 0x12, 0x05); /* Enable FRAME TBC & 3D COMB */
  2382. mdelay(20);
  2383. for (i = 0; i < 10; i++) {
  2384. u8 result = sdp_io_read(sd, 0xdb);
  2385. if (result & 0x10) {
  2386. complete++;
  2387. if (result & 0x20)
  2388. fail++;
  2389. else
  2390. pass++;
  2391. }
  2392. mdelay(20);
  2393. }
  2394. v4l2_dbg(1, debug, sd,
  2395. "Ram Test: completed %d of %d: pass %d, fail %d\n",
  2396. complete, i, pass, fail);
  2397. if (!complete || fail)
  2398. return -EIO;
  2399. return 0;
  2400. }
  2401. static void adv7842_rewrite_i2c_addresses(struct v4l2_subdev *sd,
  2402. struct adv7842_platform_data *pdata)
  2403. {
  2404. io_write(sd, 0xf1, pdata->i2c_sdp << 1);
  2405. io_write(sd, 0xf2, pdata->i2c_sdp_io << 1);
  2406. io_write(sd, 0xf3, pdata->i2c_avlink << 1);
  2407. io_write(sd, 0xf4, pdata->i2c_cec << 1);
  2408. io_write(sd, 0xf5, pdata->i2c_infoframe << 1);
  2409. io_write(sd, 0xf8, pdata->i2c_afe << 1);
  2410. io_write(sd, 0xf9, pdata->i2c_repeater << 1);
  2411. io_write(sd, 0xfa, pdata->i2c_edid << 1);
  2412. io_write(sd, 0xfb, pdata->i2c_hdmi << 1);
  2413. io_write(sd, 0xfd, pdata->i2c_cp << 1);
  2414. io_write(sd, 0xfe, pdata->i2c_vdp << 1);
  2415. }
  2416. static int adv7842_command_ram_test(struct v4l2_subdev *sd)
  2417. {
  2418. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2419. struct adv7842_state *state = to_state(sd);
  2420. struct adv7842_platform_data *pdata = client->dev.platform_data;
  2421. struct v4l2_dv_timings timings;
  2422. int ret = 0;
  2423. if (!pdata)
  2424. return -ENODEV;
  2425. if (!pdata->sd_ram_size || !pdata->sd_ram_ddr) {
  2426. v4l2_info(sd, "no sdram or no ddr sdram\n");
  2427. return -EINVAL;
  2428. }
  2429. main_reset(sd);
  2430. adv7842_rewrite_i2c_addresses(sd, pdata);
  2431. /* run ram test */
  2432. ret = adv7842_ddr_ram_test(sd);
  2433. main_reset(sd);
  2434. adv7842_rewrite_i2c_addresses(sd, pdata);
  2435. /* and re-init chip and state */
  2436. adv7842_core_init(sd);
  2437. disable_input(sd);
  2438. select_input(sd, state->vid_std_select);
  2439. enable_input(sd);
  2440. edid_write_vga_segment(sd);
  2441. edid_write_hdmi_segment(sd, ADV7842_EDID_PORT_A);
  2442. edid_write_hdmi_segment(sd, ADV7842_EDID_PORT_B);
  2443. timings = state->timings;
  2444. memset(&state->timings, 0, sizeof(struct v4l2_dv_timings));
  2445. adv7842_s_dv_timings(sd, &timings);
  2446. return ret;
  2447. }
  2448. static long adv7842_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  2449. {
  2450. switch (cmd) {
  2451. case ADV7842_CMD_RAM_TEST:
  2452. return adv7842_command_ram_test(sd);
  2453. }
  2454. return -ENOTTY;
  2455. }
  2456. /* ----------------------------------------------------------------------- */
  2457. static const struct v4l2_ctrl_ops adv7842_ctrl_ops = {
  2458. .s_ctrl = adv7842_s_ctrl,
  2459. };
  2460. static const struct v4l2_subdev_core_ops adv7842_core_ops = {
  2461. .log_status = adv7842_log_status,
  2462. .ioctl = adv7842_ioctl,
  2463. .interrupt_service_routine = adv7842_isr,
  2464. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2465. .g_register = adv7842_g_register,
  2466. .s_register = adv7842_s_register,
  2467. #endif
  2468. };
  2469. static const struct v4l2_subdev_video_ops adv7842_video_ops = {
  2470. .g_std = adv7842_g_std,
  2471. .s_std = adv7842_s_std,
  2472. .s_routing = adv7842_s_routing,
  2473. .querystd = adv7842_querystd,
  2474. .g_input_status = adv7842_g_input_status,
  2475. .s_dv_timings = adv7842_s_dv_timings,
  2476. .g_dv_timings = adv7842_g_dv_timings,
  2477. .query_dv_timings = adv7842_query_dv_timings,
  2478. .enum_mbus_fmt = adv7842_enum_mbus_fmt,
  2479. .g_mbus_fmt = adv7842_g_mbus_fmt,
  2480. .try_mbus_fmt = adv7842_g_mbus_fmt,
  2481. .s_mbus_fmt = adv7842_g_mbus_fmt,
  2482. };
  2483. static const struct v4l2_subdev_pad_ops adv7842_pad_ops = {
  2484. .get_edid = adv7842_get_edid,
  2485. .set_edid = adv7842_set_edid,
  2486. .enum_dv_timings = adv7842_enum_dv_timings,
  2487. .dv_timings_cap = adv7842_dv_timings_cap,
  2488. };
  2489. static const struct v4l2_subdev_ops adv7842_ops = {
  2490. .core = &adv7842_core_ops,
  2491. .video = &adv7842_video_ops,
  2492. .pad = &adv7842_pad_ops,
  2493. };
  2494. /* -------------------------- custom ctrls ---------------------------------- */
  2495. static const struct v4l2_ctrl_config adv7842_ctrl_analog_sampling_phase = {
  2496. .ops = &adv7842_ctrl_ops,
  2497. .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
  2498. .name = "Analog Sampling Phase",
  2499. .type = V4L2_CTRL_TYPE_INTEGER,
  2500. .min = 0,
  2501. .max = 0x1f,
  2502. .step = 1,
  2503. .def = 0,
  2504. };
  2505. static const struct v4l2_ctrl_config adv7842_ctrl_free_run_color_manual = {
  2506. .ops = &adv7842_ctrl_ops,
  2507. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
  2508. .name = "Free Running Color, Manual",
  2509. .type = V4L2_CTRL_TYPE_BOOLEAN,
  2510. .max = 1,
  2511. .step = 1,
  2512. .def = 1,
  2513. };
  2514. static const struct v4l2_ctrl_config adv7842_ctrl_free_run_color = {
  2515. .ops = &adv7842_ctrl_ops,
  2516. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
  2517. .name = "Free Running Color",
  2518. .type = V4L2_CTRL_TYPE_INTEGER,
  2519. .max = 0xffffff,
  2520. .step = 0x1,
  2521. };
  2522. static void adv7842_unregister_clients(struct v4l2_subdev *sd)
  2523. {
  2524. struct adv7842_state *state = to_state(sd);
  2525. if (state->i2c_avlink)
  2526. i2c_unregister_device(state->i2c_avlink);
  2527. if (state->i2c_cec)
  2528. i2c_unregister_device(state->i2c_cec);
  2529. if (state->i2c_infoframe)
  2530. i2c_unregister_device(state->i2c_infoframe);
  2531. if (state->i2c_sdp_io)
  2532. i2c_unregister_device(state->i2c_sdp_io);
  2533. if (state->i2c_sdp)
  2534. i2c_unregister_device(state->i2c_sdp);
  2535. if (state->i2c_afe)
  2536. i2c_unregister_device(state->i2c_afe);
  2537. if (state->i2c_repeater)
  2538. i2c_unregister_device(state->i2c_repeater);
  2539. if (state->i2c_edid)
  2540. i2c_unregister_device(state->i2c_edid);
  2541. if (state->i2c_hdmi)
  2542. i2c_unregister_device(state->i2c_hdmi);
  2543. if (state->i2c_cp)
  2544. i2c_unregister_device(state->i2c_cp);
  2545. if (state->i2c_vdp)
  2546. i2c_unregister_device(state->i2c_vdp);
  2547. state->i2c_avlink = NULL;
  2548. state->i2c_cec = NULL;
  2549. state->i2c_infoframe = NULL;
  2550. state->i2c_sdp_io = NULL;
  2551. state->i2c_sdp = NULL;
  2552. state->i2c_afe = NULL;
  2553. state->i2c_repeater = NULL;
  2554. state->i2c_edid = NULL;
  2555. state->i2c_hdmi = NULL;
  2556. state->i2c_cp = NULL;
  2557. state->i2c_vdp = NULL;
  2558. }
  2559. static struct i2c_client *adv7842_dummy_client(struct v4l2_subdev *sd, const char *desc,
  2560. u8 addr, u8 io_reg)
  2561. {
  2562. struct i2c_client *client = v4l2_get_subdevdata(sd);
  2563. struct i2c_client *cp;
  2564. io_write(sd, io_reg, addr << 1);
  2565. if (addr == 0) {
  2566. v4l2_err(sd, "no %s i2c addr configured\n", desc);
  2567. return NULL;
  2568. }
  2569. cp = i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
  2570. if (!cp)
  2571. v4l2_err(sd, "register %s on i2c addr 0x%x failed\n", desc, addr);
  2572. return cp;
  2573. }
  2574. static int adv7842_register_clients(struct v4l2_subdev *sd)
  2575. {
  2576. struct adv7842_state *state = to_state(sd);
  2577. struct adv7842_platform_data *pdata = &state->pdata;
  2578. state->i2c_avlink = adv7842_dummy_client(sd, "avlink", pdata->i2c_avlink, 0xf3);
  2579. state->i2c_cec = adv7842_dummy_client(sd, "cec", pdata->i2c_cec, 0xf4);
  2580. state->i2c_infoframe = adv7842_dummy_client(sd, "infoframe", pdata->i2c_infoframe, 0xf5);
  2581. state->i2c_sdp_io = adv7842_dummy_client(sd, "sdp_io", pdata->i2c_sdp_io, 0xf2);
  2582. state->i2c_sdp = adv7842_dummy_client(sd, "sdp", pdata->i2c_sdp, 0xf1);
  2583. state->i2c_afe = adv7842_dummy_client(sd, "afe", pdata->i2c_afe, 0xf8);
  2584. state->i2c_repeater = adv7842_dummy_client(sd, "repeater", pdata->i2c_repeater, 0xf9);
  2585. state->i2c_edid = adv7842_dummy_client(sd, "edid", pdata->i2c_edid, 0xfa);
  2586. state->i2c_hdmi = adv7842_dummy_client(sd, "hdmi", pdata->i2c_hdmi, 0xfb);
  2587. state->i2c_cp = adv7842_dummy_client(sd, "cp", pdata->i2c_cp, 0xfd);
  2588. state->i2c_vdp = adv7842_dummy_client(sd, "vdp", pdata->i2c_vdp, 0xfe);
  2589. if (!state->i2c_avlink ||
  2590. !state->i2c_cec ||
  2591. !state->i2c_infoframe ||
  2592. !state->i2c_sdp_io ||
  2593. !state->i2c_sdp ||
  2594. !state->i2c_afe ||
  2595. !state->i2c_repeater ||
  2596. !state->i2c_edid ||
  2597. !state->i2c_hdmi ||
  2598. !state->i2c_cp ||
  2599. !state->i2c_vdp)
  2600. return -1;
  2601. return 0;
  2602. }
  2603. static int adv7842_probe(struct i2c_client *client,
  2604. const struct i2c_device_id *id)
  2605. {
  2606. struct adv7842_state *state;
  2607. static const struct v4l2_dv_timings cea640x480 =
  2608. V4L2_DV_BT_CEA_640X480P59_94;
  2609. struct adv7842_platform_data *pdata = client->dev.platform_data;
  2610. struct v4l2_ctrl_handler *hdl;
  2611. struct v4l2_subdev *sd;
  2612. u16 rev;
  2613. int err;
  2614. /* Check if the adapter supports the needed features */
  2615. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  2616. return -EIO;
  2617. v4l_dbg(1, debug, client, "detecting adv7842 client on address 0x%x\n",
  2618. client->addr << 1);
  2619. if (!pdata) {
  2620. v4l_err(client, "No platform data!\n");
  2621. return -ENODEV;
  2622. }
  2623. state = devm_kzalloc(&client->dev, sizeof(struct adv7842_state), GFP_KERNEL);
  2624. if (!state) {
  2625. v4l_err(client, "Could not allocate adv7842_state memory!\n");
  2626. return -ENOMEM;
  2627. }
  2628. /* platform data */
  2629. state->pdata = *pdata;
  2630. state->timings = cea640x480;
  2631. sd = &state->sd;
  2632. v4l2_i2c_subdev_init(sd, client, &adv7842_ops);
  2633. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  2634. state->mode = pdata->mode;
  2635. state->hdmi_port_a = pdata->input == ADV7842_SELECT_HDMI_PORT_A;
  2636. state->restart_stdi_once = true;
  2637. /* i2c access to adv7842? */
  2638. rev = adv_smbus_read_byte_data_check(client, 0xea, false) << 8 |
  2639. adv_smbus_read_byte_data_check(client, 0xeb, false);
  2640. if (rev != 0x2012) {
  2641. v4l2_info(sd, "got rev=0x%04x on first read attempt\n", rev);
  2642. rev = adv_smbus_read_byte_data_check(client, 0xea, false) << 8 |
  2643. adv_smbus_read_byte_data_check(client, 0xeb, false);
  2644. }
  2645. if (rev != 0x2012) {
  2646. v4l2_info(sd, "not an adv7842 on address 0x%x (rev=0x%04x)\n",
  2647. client->addr << 1, rev);
  2648. return -ENODEV;
  2649. }
  2650. if (pdata->chip_reset)
  2651. main_reset(sd);
  2652. /* control handlers */
  2653. hdl = &state->hdl;
  2654. v4l2_ctrl_handler_init(hdl, 6);
  2655. /* add in ascending ID order */
  2656. v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
  2657. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  2658. v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
  2659. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  2660. v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
  2661. V4L2_CID_SATURATION, 0, 255, 1, 128);
  2662. v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
  2663. V4L2_CID_HUE, 0, 128, 1, 0);
  2664. /* custom controls */
  2665. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  2666. V4L2_CID_DV_RX_POWER_PRESENT, 0, 3, 0, 0);
  2667. state->analog_sampling_phase_ctrl = v4l2_ctrl_new_custom(hdl,
  2668. &adv7842_ctrl_analog_sampling_phase, NULL);
  2669. state->free_run_color_ctrl_manual = v4l2_ctrl_new_custom(hdl,
  2670. &adv7842_ctrl_free_run_color_manual, NULL);
  2671. state->free_run_color_ctrl = v4l2_ctrl_new_custom(hdl,
  2672. &adv7842_ctrl_free_run_color, NULL);
  2673. state->rgb_quantization_range_ctrl =
  2674. v4l2_ctrl_new_std_menu(hdl, &adv7842_ctrl_ops,
  2675. V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
  2676. 0, V4L2_DV_RGB_RANGE_AUTO);
  2677. sd->ctrl_handler = hdl;
  2678. if (hdl->error) {
  2679. err = hdl->error;
  2680. goto err_hdl;
  2681. }
  2682. state->detect_tx_5v_ctrl->is_private = true;
  2683. state->rgb_quantization_range_ctrl->is_private = true;
  2684. state->analog_sampling_phase_ctrl->is_private = true;
  2685. state->free_run_color_ctrl_manual->is_private = true;
  2686. state->free_run_color_ctrl->is_private = true;
  2687. if (adv7842_s_detect_tx_5v_ctrl(sd)) {
  2688. err = -ENODEV;
  2689. goto err_hdl;
  2690. }
  2691. if (adv7842_register_clients(sd) < 0) {
  2692. err = -ENOMEM;
  2693. v4l2_err(sd, "failed to create all i2c clients\n");
  2694. goto err_i2c;
  2695. }
  2696. /* work queues */
  2697. state->work_queues = create_singlethread_workqueue(client->name);
  2698. if (!state->work_queues) {
  2699. v4l2_err(sd, "Could not create work queue\n");
  2700. err = -ENOMEM;
  2701. goto err_i2c;
  2702. }
  2703. INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
  2704. adv7842_delayed_work_enable_hotplug);
  2705. state->pad.flags = MEDIA_PAD_FL_SOURCE;
  2706. err = media_entity_init(&sd->entity, 1, &state->pad, 0);
  2707. if (err)
  2708. goto err_work_queues;
  2709. err = adv7842_core_init(sd);
  2710. if (err)
  2711. goto err_entity;
  2712. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  2713. client->addr << 1, client->adapter->name);
  2714. return 0;
  2715. err_entity:
  2716. media_entity_cleanup(&sd->entity);
  2717. err_work_queues:
  2718. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  2719. destroy_workqueue(state->work_queues);
  2720. err_i2c:
  2721. adv7842_unregister_clients(sd);
  2722. err_hdl:
  2723. v4l2_ctrl_handler_free(hdl);
  2724. return err;
  2725. }
  2726. /* ----------------------------------------------------------------------- */
  2727. static int adv7842_remove(struct i2c_client *client)
  2728. {
  2729. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  2730. struct adv7842_state *state = to_state(sd);
  2731. adv7842_irq_enable(sd, false);
  2732. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  2733. destroy_workqueue(state->work_queues);
  2734. v4l2_device_unregister_subdev(sd);
  2735. media_entity_cleanup(&sd->entity);
  2736. adv7842_unregister_clients(sd);
  2737. v4l2_ctrl_handler_free(sd->ctrl_handler);
  2738. return 0;
  2739. }
  2740. /* ----------------------------------------------------------------------- */
  2741. static struct i2c_device_id adv7842_id[] = {
  2742. { "adv7842", 0 },
  2743. { }
  2744. };
  2745. MODULE_DEVICE_TABLE(i2c, adv7842_id);
  2746. /* ----------------------------------------------------------------------- */
  2747. static struct i2c_driver adv7842_driver = {
  2748. .driver = {
  2749. .owner = THIS_MODULE,
  2750. .name = "adv7842",
  2751. },
  2752. .probe = adv7842_probe,
  2753. .remove = adv7842_remove,
  2754. .id_table = adv7842_id,
  2755. };
  2756. module_i2c_driver(adv7842_driver);