adv7604.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  1. /*
  2. * adv7604 - Analog Devices ADV7604 video decoder driver
  3. *
  4. * Copyright 2012 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  5. *
  6. * This program is free software; you may redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. *
  19. */
  20. /*
  21. * References (c = chapter, p = page):
  22. * REF_01 - Analog devices, ADV7604, Register Settings Recommendations,
  23. * Revision 2.5, June 2010
  24. * REF_02 - Analog devices, Register map documentation, Documentation of
  25. * the register maps, Software manual, Rev. F, June 2010
  26. * REF_03 - Analog devices, ADV7604, Hardware Manual, Rev. F, August 2010
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/slab.h>
  31. #include <linux/i2c.h>
  32. #include <linux/delay.h>
  33. #include <linux/videodev2.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/v4l2-dv-timings.h>
  36. #include <media/v4l2-device.h>
  37. #include <media/v4l2-ctrls.h>
  38. #include <media/v4l2-chip-ident.h>
  39. #include <media/adv7604.h>
  40. static int debug;
  41. module_param(debug, int, 0644);
  42. MODULE_PARM_DESC(debug, "debug level (0-2)");
  43. MODULE_DESCRIPTION("Analog Devices ADV7604 video decoder driver");
  44. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  45. MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
  46. MODULE_LICENSE("GPL");
  47. /* ADV7604 system clock frequency */
  48. #define ADV7604_fsc (28636360)
  49. #define DIGITAL_INPUT (state->mode == ADV7604_MODE_HDMI)
  50. /*
  51. **********************************************************************
  52. *
  53. * Arrays with configuration parameters for the ADV7604
  54. *
  55. **********************************************************************
  56. */
  57. struct adv7604_state {
  58. struct adv7604_platform_data pdata;
  59. struct v4l2_subdev sd;
  60. struct media_pad pad;
  61. struct v4l2_ctrl_handler hdl;
  62. enum adv7604_mode mode;
  63. struct v4l2_dv_timings timings;
  64. u8 edid[256];
  65. unsigned edid_blocks;
  66. struct v4l2_fract aspect_ratio;
  67. u32 rgb_quantization_range;
  68. struct workqueue_struct *work_queues;
  69. struct delayed_work delayed_work_enable_hotplug;
  70. bool connector_hdmi;
  71. bool restart_stdi_once;
  72. /* i2c clients */
  73. struct i2c_client *i2c_avlink;
  74. struct i2c_client *i2c_cec;
  75. struct i2c_client *i2c_infoframe;
  76. struct i2c_client *i2c_esdp;
  77. struct i2c_client *i2c_dpp;
  78. struct i2c_client *i2c_afe;
  79. struct i2c_client *i2c_repeater;
  80. struct i2c_client *i2c_edid;
  81. struct i2c_client *i2c_hdmi;
  82. struct i2c_client *i2c_test;
  83. struct i2c_client *i2c_cp;
  84. struct i2c_client *i2c_vdp;
  85. /* controls */
  86. struct v4l2_ctrl *detect_tx_5v_ctrl;
  87. struct v4l2_ctrl *analog_sampling_phase_ctrl;
  88. struct v4l2_ctrl *free_run_color_manual_ctrl;
  89. struct v4l2_ctrl *free_run_color_ctrl;
  90. struct v4l2_ctrl *rgb_quantization_range_ctrl;
  91. };
  92. /* Supported CEA and DMT timings */
  93. static const struct v4l2_dv_timings adv7604_timings[] = {
  94. V4L2_DV_BT_CEA_720X480P59_94,
  95. V4L2_DV_BT_CEA_720X576P50,
  96. V4L2_DV_BT_CEA_1280X720P24,
  97. V4L2_DV_BT_CEA_1280X720P25,
  98. V4L2_DV_BT_CEA_1280X720P50,
  99. V4L2_DV_BT_CEA_1280X720P60,
  100. V4L2_DV_BT_CEA_1920X1080P24,
  101. V4L2_DV_BT_CEA_1920X1080P25,
  102. V4L2_DV_BT_CEA_1920X1080P30,
  103. V4L2_DV_BT_CEA_1920X1080P50,
  104. V4L2_DV_BT_CEA_1920X1080P60,
  105. /* sorted by DMT ID */
  106. V4L2_DV_BT_DMT_640X350P85,
  107. V4L2_DV_BT_DMT_640X400P85,
  108. V4L2_DV_BT_DMT_720X400P85,
  109. V4L2_DV_BT_DMT_640X480P60,
  110. V4L2_DV_BT_DMT_640X480P72,
  111. V4L2_DV_BT_DMT_640X480P75,
  112. V4L2_DV_BT_DMT_640X480P85,
  113. V4L2_DV_BT_DMT_800X600P56,
  114. V4L2_DV_BT_DMT_800X600P60,
  115. V4L2_DV_BT_DMT_800X600P72,
  116. V4L2_DV_BT_DMT_800X600P75,
  117. V4L2_DV_BT_DMT_800X600P85,
  118. V4L2_DV_BT_DMT_848X480P60,
  119. V4L2_DV_BT_DMT_1024X768P60,
  120. V4L2_DV_BT_DMT_1024X768P70,
  121. V4L2_DV_BT_DMT_1024X768P75,
  122. V4L2_DV_BT_DMT_1024X768P85,
  123. V4L2_DV_BT_DMT_1152X864P75,
  124. V4L2_DV_BT_DMT_1280X768P60_RB,
  125. V4L2_DV_BT_DMT_1280X768P60,
  126. V4L2_DV_BT_DMT_1280X768P75,
  127. V4L2_DV_BT_DMT_1280X768P85,
  128. V4L2_DV_BT_DMT_1280X800P60_RB,
  129. V4L2_DV_BT_DMT_1280X800P60,
  130. V4L2_DV_BT_DMT_1280X800P75,
  131. V4L2_DV_BT_DMT_1280X800P85,
  132. V4L2_DV_BT_DMT_1280X960P60,
  133. V4L2_DV_BT_DMT_1280X960P85,
  134. V4L2_DV_BT_DMT_1280X1024P60,
  135. V4L2_DV_BT_DMT_1280X1024P75,
  136. V4L2_DV_BT_DMT_1280X1024P85,
  137. V4L2_DV_BT_DMT_1360X768P60,
  138. V4L2_DV_BT_DMT_1400X1050P60_RB,
  139. V4L2_DV_BT_DMT_1400X1050P60,
  140. V4L2_DV_BT_DMT_1400X1050P75,
  141. V4L2_DV_BT_DMT_1400X1050P85,
  142. V4L2_DV_BT_DMT_1440X900P60_RB,
  143. V4L2_DV_BT_DMT_1440X900P60,
  144. V4L2_DV_BT_DMT_1600X1200P60,
  145. V4L2_DV_BT_DMT_1680X1050P60_RB,
  146. V4L2_DV_BT_DMT_1680X1050P60,
  147. V4L2_DV_BT_DMT_1792X1344P60,
  148. V4L2_DV_BT_DMT_1856X1392P60,
  149. V4L2_DV_BT_DMT_1920X1200P60_RB,
  150. V4L2_DV_BT_DMT_1366X768P60,
  151. V4L2_DV_BT_DMT_1920X1080P60,
  152. { },
  153. };
  154. struct adv7604_video_standards {
  155. struct v4l2_dv_timings timings;
  156. u8 vid_std;
  157. u8 v_freq;
  158. };
  159. /* sorted by number of lines */
  160. static const struct adv7604_video_standards adv7604_prim_mode_comp[] = {
  161. /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
  162. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  163. { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
  164. { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
  165. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  166. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  167. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  168. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  169. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  170. /* TODO add 1920x1080P60_RB (CVT timing) */
  171. { },
  172. };
  173. /* sorted by number of lines */
  174. static const struct adv7604_video_standards adv7604_prim_mode_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. { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
  191. { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
  192. { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
  193. { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
  194. { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
  195. /* TODO add 1600X1200P60_RB (not a DMT timing) */
  196. { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
  197. { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
  198. { },
  199. };
  200. /* sorted by number of lines */
  201. static const struct adv7604_video_standards adv7604_prim_mode_hdmi_comp[] = {
  202. { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
  203. { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
  204. { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
  205. { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
  206. { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
  207. { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
  208. { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
  209. { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
  210. { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
  211. { },
  212. };
  213. /* sorted by number of lines */
  214. static const struct adv7604_video_standards adv7604_prim_mode_hdmi_gr[] = {
  215. { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
  216. { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
  217. { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
  218. { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
  219. { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
  220. { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
  221. { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
  222. { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
  223. { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
  224. { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
  225. { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
  226. { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
  227. { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
  228. { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
  229. { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
  230. { },
  231. };
  232. /* ----------------------------------------------------------------------- */
  233. static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
  234. {
  235. return container_of(sd, struct adv7604_state, sd);
  236. }
  237. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  238. {
  239. return &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
  240. }
  241. static inline unsigned hblanking(const struct v4l2_bt_timings *t)
  242. {
  243. return t->hfrontporch + t->hsync + t->hbackporch;
  244. }
  245. static inline unsigned htotal(const struct v4l2_bt_timings *t)
  246. {
  247. return t->width + t->hfrontporch + t->hsync + t->hbackporch;
  248. }
  249. static inline unsigned vblanking(const struct v4l2_bt_timings *t)
  250. {
  251. return t->vfrontporch + t->vsync + t->vbackporch;
  252. }
  253. static inline unsigned vtotal(const struct v4l2_bt_timings *t)
  254. {
  255. return t->height + t->vfrontporch + t->vsync + t->vbackporch;
  256. }
  257. /* ----------------------------------------------------------------------- */
  258. static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
  259. u8 command, bool check)
  260. {
  261. union i2c_smbus_data data;
  262. if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  263. I2C_SMBUS_READ, command,
  264. I2C_SMBUS_BYTE_DATA, &data))
  265. return data.byte;
  266. if (check)
  267. v4l_err(client, "error reading %02x, %02x\n",
  268. client->addr, command);
  269. return -EIO;
  270. }
  271. static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
  272. {
  273. return adv_smbus_read_byte_data_check(client, command, true);
  274. }
  275. static s32 adv_smbus_write_byte_data(struct i2c_client *client,
  276. u8 command, u8 value)
  277. {
  278. union i2c_smbus_data data;
  279. int err;
  280. int i;
  281. data.byte = value;
  282. for (i = 0; i < 3; i++) {
  283. err = i2c_smbus_xfer(client->adapter, client->addr,
  284. client->flags,
  285. I2C_SMBUS_WRITE, command,
  286. I2C_SMBUS_BYTE_DATA, &data);
  287. if (!err)
  288. break;
  289. }
  290. if (err < 0)
  291. v4l_err(client, "error writing %02x, %02x, %02x\n",
  292. client->addr, command, value);
  293. return err;
  294. }
  295. static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
  296. u8 command, unsigned length, const u8 *values)
  297. {
  298. union i2c_smbus_data data;
  299. if (length > I2C_SMBUS_BLOCK_MAX)
  300. length = I2C_SMBUS_BLOCK_MAX;
  301. data.block[0] = length;
  302. memcpy(data.block + 1, values, length);
  303. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  304. I2C_SMBUS_WRITE, command,
  305. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  306. }
  307. /* ----------------------------------------------------------------------- */
  308. static inline int io_read(struct v4l2_subdev *sd, u8 reg)
  309. {
  310. struct i2c_client *client = v4l2_get_subdevdata(sd);
  311. return adv_smbus_read_byte_data(client, reg);
  312. }
  313. static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  314. {
  315. struct i2c_client *client = v4l2_get_subdevdata(sd);
  316. return adv_smbus_write_byte_data(client, reg, val);
  317. }
  318. static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  319. {
  320. return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
  321. }
  322. static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
  323. {
  324. struct adv7604_state *state = to_state(sd);
  325. return adv_smbus_read_byte_data(state->i2c_avlink, reg);
  326. }
  327. static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  328. {
  329. struct adv7604_state *state = to_state(sd);
  330. return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
  331. }
  332. static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
  333. {
  334. struct adv7604_state *state = to_state(sd);
  335. return adv_smbus_read_byte_data(state->i2c_cec, reg);
  336. }
  337. static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  338. {
  339. struct adv7604_state *state = to_state(sd);
  340. return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
  341. }
  342. static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  343. {
  344. return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val);
  345. }
  346. static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
  347. {
  348. struct adv7604_state *state = to_state(sd);
  349. return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
  350. }
  351. static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  352. {
  353. struct adv7604_state *state = to_state(sd);
  354. return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
  355. }
  356. static inline int esdp_read(struct v4l2_subdev *sd, u8 reg)
  357. {
  358. struct adv7604_state *state = to_state(sd);
  359. return adv_smbus_read_byte_data(state->i2c_esdp, reg);
  360. }
  361. static inline int esdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  362. {
  363. struct adv7604_state *state = to_state(sd);
  364. return adv_smbus_write_byte_data(state->i2c_esdp, reg, val);
  365. }
  366. static inline int dpp_read(struct v4l2_subdev *sd, u8 reg)
  367. {
  368. struct adv7604_state *state = to_state(sd);
  369. return adv_smbus_read_byte_data(state->i2c_dpp, reg);
  370. }
  371. static inline int dpp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  372. {
  373. struct adv7604_state *state = to_state(sd);
  374. return adv_smbus_write_byte_data(state->i2c_dpp, reg, val);
  375. }
  376. static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
  377. {
  378. struct adv7604_state *state = to_state(sd);
  379. return adv_smbus_read_byte_data(state->i2c_afe, reg);
  380. }
  381. static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  382. {
  383. struct adv7604_state *state = to_state(sd);
  384. return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
  385. }
  386. static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
  387. {
  388. struct adv7604_state *state = to_state(sd);
  389. return adv_smbus_read_byte_data(state->i2c_repeater, reg);
  390. }
  391. static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  392. {
  393. struct adv7604_state *state = to_state(sd);
  394. return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
  395. }
  396. static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  397. {
  398. return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
  399. }
  400. static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
  401. {
  402. struct adv7604_state *state = to_state(sd);
  403. return adv_smbus_read_byte_data(state->i2c_edid, reg);
  404. }
  405. static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  406. {
  407. struct adv7604_state *state = to_state(sd);
  408. return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
  409. }
  410. static inline int edid_read_block(struct v4l2_subdev *sd, unsigned len, u8 *val)
  411. {
  412. struct adv7604_state *state = to_state(sd);
  413. struct i2c_client *client = state->i2c_edid;
  414. u8 msgbuf0[1] = { 0 };
  415. u8 msgbuf1[256];
  416. struct i2c_msg msg[2] = { { client->addr, 0, 1, msgbuf0 },
  417. { client->addr, 0 | I2C_M_RD, len, msgbuf1 }
  418. };
  419. if (i2c_transfer(client->adapter, msg, 2) < 0)
  420. return -EIO;
  421. memcpy(val, msgbuf1, len);
  422. return 0;
  423. }
  424. static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
  425. {
  426. struct delayed_work *dwork = to_delayed_work(work);
  427. struct adv7604_state *state = container_of(dwork, struct adv7604_state,
  428. delayed_work_enable_hotplug);
  429. struct v4l2_subdev *sd = &state->sd;
  430. v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
  431. v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)1);
  432. }
  433. static inline int edid_write_block(struct v4l2_subdev *sd,
  434. unsigned len, const u8 *val)
  435. {
  436. struct i2c_client *client = v4l2_get_subdevdata(sd);
  437. struct adv7604_state *state = to_state(sd);
  438. int err = 0;
  439. int i;
  440. v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len);
  441. v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)0);
  442. /* Disables I2C access to internal EDID ram from DDC port */
  443. rep_write_and_or(sd, 0x77, 0xf0, 0x0);
  444. for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
  445. err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
  446. I2C_SMBUS_BLOCK_MAX, val + i);
  447. if (err)
  448. return err;
  449. /* adv7604 calculates the checksums and enables I2C access to internal
  450. EDID ram from DDC port. */
  451. rep_write_and_or(sd, 0x77, 0xf0, 0x1);
  452. for (i = 0; i < 1000; i++) {
  453. if (rep_read(sd, 0x7d) & 1)
  454. break;
  455. mdelay(1);
  456. }
  457. if (i == 1000) {
  458. v4l_err(client, "error enabling edid\n");
  459. return -EIO;
  460. }
  461. /* enable hotplug after 100 ms */
  462. queue_delayed_work(state->work_queues,
  463. &state->delayed_work_enable_hotplug, HZ / 10);
  464. return 0;
  465. }
  466. static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
  467. {
  468. struct adv7604_state *state = to_state(sd);
  469. return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
  470. }
  471. static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  472. {
  473. struct adv7604_state *state = to_state(sd);
  474. return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
  475. }
  476. static inline int test_read(struct v4l2_subdev *sd, u8 reg)
  477. {
  478. struct adv7604_state *state = to_state(sd);
  479. return adv_smbus_read_byte_data(state->i2c_test, reg);
  480. }
  481. static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  482. {
  483. struct adv7604_state *state = to_state(sd);
  484. return adv_smbus_write_byte_data(state->i2c_test, reg, val);
  485. }
  486. static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
  487. {
  488. struct adv7604_state *state = to_state(sd);
  489. return adv_smbus_read_byte_data(state->i2c_cp, reg);
  490. }
  491. static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  492. {
  493. struct adv7604_state *state = to_state(sd);
  494. return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
  495. }
  496. static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
  497. {
  498. return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
  499. }
  500. static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
  501. {
  502. struct adv7604_state *state = to_state(sd);
  503. return adv_smbus_read_byte_data(state->i2c_vdp, reg);
  504. }
  505. static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
  506. {
  507. struct adv7604_state *state = to_state(sd);
  508. return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
  509. }
  510. /* ----------------------------------------------------------------------- */
  511. #ifdef CONFIG_VIDEO_ADV_DEBUG
  512. static void adv7604_inv_register(struct v4l2_subdev *sd)
  513. {
  514. v4l2_info(sd, "0x000-0x0ff: IO Map\n");
  515. v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
  516. v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
  517. v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
  518. v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
  519. v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
  520. v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
  521. v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
  522. v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
  523. v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
  524. v4l2_info(sd, "0xa00-0xaff: Test Map\n");
  525. v4l2_info(sd, "0xb00-0xbff: CP Map\n");
  526. v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
  527. }
  528. static int adv7604_g_register(struct v4l2_subdev *sd,
  529. struct v4l2_dbg_register *reg)
  530. {
  531. struct i2c_client *client = v4l2_get_subdevdata(sd);
  532. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  533. return -EINVAL;
  534. if (!capable(CAP_SYS_ADMIN))
  535. return -EPERM;
  536. reg->size = 1;
  537. switch (reg->reg >> 8) {
  538. case 0:
  539. reg->val = io_read(sd, reg->reg & 0xff);
  540. break;
  541. case 1:
  542. reg->val = avlink_read(sd, reg->reg & 0xff);
  543. break;
  544. case 2:
  545. reg->val = cec_read(sd, reg->reg & 0xff);
  546. break;
  547. case 3:
  548. reg->val = infoframe_read(sd, reg->reg & 0xff);
  549. break;
  550. case 4:
  551. reg->val = esdp_read(sd, reg->reg & 0xff);
  552. break;
  553. case 5:
  554. reg->val = dpp_read(sd, reg->reg & 0xff);
  555. break;
  556. case 6:
  557. reg->val = afe_read(sd, reg->reg & 0xff);
  558. break;
  559. case 7:
  560. reg->val = rep_read(sd, reg->reg & 0xff);
  561. break;
  562. case 8:
  563. reg->val = edid_read(sd, reg->reg & 0xff);
  564. break;
  565. case 9:
  566. reg->val = hdmi_read(sd, reg->reg & 0xff);
  567. break;
  568. case 0xa:
  569. reg->val = test_read(sd, reg->reg & 0xff);
  570. break;
  571. case 0xb:
  572. reg->val = cp_read(sd, reg->reg & 0xff);
  573. break;
  574. case 0xc:
  575. reg->val = vdp_read(sd, reg->reg & 0xff);
  576. break;
  577. default:
  578. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  579. adv7604_inv_register(sd);
  580. break;
  581. }
  582. return 0;
  583. }
  584. static int adv7604_s_register(struct v4l2_subdev *sd,
  585. struct v4l2_dbg_register *reg)
  586. {
  587. struct i2c_client *client = v4l2_get_subdevdata(sd);
  588. if (!v4l2_chip_match_i2c_client(client, &reg->match))
  589. return -EINVAL;
  590. if (!capable(CAP_SYS_ADMIN))
  591. return -EPERM;
  592. switch (reg->reg >> 8) {
  593. case 0:
  594. io_write(sd, reg->reg & 0xff, reg->val & 0xff);
  595. break;
  596. case 1:
  597. avlink_write(sd, reg->reg & 0xff, reg->val & 0xff);
  598. break;
  599. case 2:
  600. cec_write(sd, reg->reg & 0xff, reg->val & 0xff);
  601. break;
  602. case 3:
  603. infoframe_write(sd, reg->reg & 0xff, reg->val & 0xff);
  604. break;
  605. case 4:
  606. esdp_write(sd, reg->reg & 0xff, reg->val & 0xff);
  607. break;
  608. case 5:
  609. dpp_write(sd, reg->reg & 0xff, reg->val & 0xff);
  610. break;
  611. case 6:
  612. afe_write(sd, reg->reg & 0xff, reg->val & 0xff);
  613. break;
  614. case 7:
  615. rep_write(sd, reg->reg & 0xff, reg->val & 0xff);
  616. break;
  617. case 8:
  618. edid_write(sd, reg->reg & 0xff, reg->val & 0xff);
  619. break;
  620. case 9:
  621. hdmi_write(sd, reg->reg & 0xff, reg->val & 0xff);
  622. break;
  623. case 0xa:
  624. test_write(sd, reg->reg & 0xff, reg->val & 0xff);
  625. break;
  626. case 0xb:
  627. cp_write(sd, reg->reg & 0xff, reg->val & 0xff);
  628. break;
  629. case 0xc:
  630. vdp_write(sd, reg->reg & 0xff, reg->val & 0xff);
  631. break;
  632. default:
  633. v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
  634. adv7604_inv_register(sd);
  635. break;
  636. }
  637. return 0;
  638. }
  639. #endif
  640. static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
  641. {
  642. struct adv7604_state *state = to_state(sd);
  643. /* port A only */
  644. return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
  645. ((io_read(sd, 0x6f) & 0x10) >> 4));
  646. }
  647. static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
  648. u8 prim_mode,
  649. const struct adv7604_video_standards *predef_vid_timings,
  650. const struct v4l2_dv_timings *timings)
  651. {
  652. struct adv7604_state *state = to_state(sd);
  653. int i;
  654. for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
  655. if (!v4l_match_dv_timings(timings, &predef_vid_timings[i].timings,
  656. DIGITAL_INPUT ? 250000 : 1000000))
  657. continue;
  658. io_write(sd, 0x00, predef_vid_timings[i].vid_std); /* video std */
  659. io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) +
  660. prim_mode); /* v_freq and prim mode */
  661. return 0;
  662. }
  663. return -1;
  664. }
  665. static int configure_predefined_video_timings(struct v4l2_subdev *sd,
  666. struct v4l2_dv_timings *timings)
  667. {
  668. struct adv7604_state *state = to_state(sd);
  669. int err;
  670. v4l2_dbg(1, debug, sd, "%s", __func__);
  671. /* reset to default values */
  672. io_write(sd, 0x16, 0x43);
  673. io_write(sd, 0x17, 0x5a);
  674. /* disable embedded syncs for auto graphics mode */
  675. cp_write_and_or(sd, 0x81, 0xef, 0x00);
  676. cp_write(sd, 0x8f, 0x00);
  677. cp_write(sd, 0x90, 0x00);
  678. cp_write(sd, 0xa2, 0x00);
  679. cp_write(sd, 0xa3, 0x00);
  680. cp_write(sd, 0xa4, 0x00);
  681. cp_write(sd, 0xa5, 0x00);
  682. cp_write(sd, 0xa6, 0x00);
  683. cp_write(sd, 0xa7, 0x00);
  684. cp_write(sd, 0xab, 0x00);
  685. cp_write(sd, 0xac, 0x00);
  686. switch (state->mode) {
  687. case ADV7604_MODE_COMP:
  688. case ADV7604_MODE_GR:
  689. err = find_and_set_predefined_video_timings(sd,
  690. 0x01, adv7604_prim_mode_comp, timings);
  691. if (err)
  692. err = find_and_set_predefined_video_timings(sd,
  693. 0x02, adv7604_prim_mode_gr, timings);
  694. break;
  695. case ADV7604_MODE_HDMI:
  696. err = find_and_set_predefined_video_timings(sd,
  697. 0x05, adv7604_prim_mode_hdmi_comp, timings);
  698. if (err)
  699. err = find_and_set_predefined_video_timings(sd,
  700. 0x06, adv7604_prim_mode_hdmi_gr, timings);
  701. break;
  702. default:
  703. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  704. __func__, state->mode);
  705. err = -1;
  706. break;
  707. }
  708. return err;
  709. }
  710. static void configure_custom_video_timings(struct v4l2_subdev *sd,
  711. const struct v4l2_bt_timings *bt)
  712. {
  713. struct adv7604_state *state = to_state(sd);
  714. struct i2c_client *client = v4l2_get_subdevdata(sd);
  715. u32 width = htotal(bt);
  716. u32 height = vtotal(bt);
  717. u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
  718. u16 cp_start_eav = width - bt->hfrontporch;
  719. u16 cp_start_vbi = height - bt->vfrontporch;
  720. u16 cp_end_vbi = bt->vsync + bt->vbackporch;
  721. u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
  722. ((width * (ADV7604_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
  723. const u8 pll[2] = {
  724. 0xc0 | ((width >> 8) & 0x1f),
  725. width & 0xff
  726. };
  727. v4l2_dbg(2, debug, sd, "%s\n", __func__);
  728. switch (state->mode) {
  729. case ADV7604_MODE_COMP:
  730. case ADV7604_MODE_GR:
  731. /* auto graphics */
  732. io_write(sd, 0x00, 0x07); /* video std */
  733. io_write(sd, 0x01, 0x02); /* prim mode */
  734. /* enable embedded syncs for auto graphics mode */
  735. cp_write_and_or(sd, 0x81, 0xef, 0x10);
  736. /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
  737. /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
  738. /* IO-map reg. 0x16 and 0x17 should be written in sequence */
  739. if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll)) {
  740. v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
  741. break;
  742. }
  743. /* active video - horizontal timing */
  744. cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
  745. cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) |
  746. ((cp_start_eav >> 8) & 0x0f));
  747. cp_write(sd, 0xa4, cp_start_eav & 0xff);
  748. /* active video - vertical timing */
  749. cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
  750. cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
  751. ((cp_end_vbi >> 8) & 0xf));
  752. cp_write(sd, 0xa7, cp_end_vbi & 0xff);
  753. break;
  754. case ADV7604_MODE_HDMI:
  755. /* set default prim_mode/vid_std for HDMI
  756. accoring to [REF_03, c. 4.2] */
  757. io_write(sd, 0x00, 0x02); /* video std */
  758. io_write(sd, 0x01, 0x06); /* prim mode */
  759. break;
  760. default:
  761. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  762. __func__, state->mode);
  763. break;
  764. }
  765. cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
  766. cp_write(sd, 0x90, ch1_fr_ll & 0xff);
  767. cp_write(sd, 0xab, (height >> 4) & 0xff);
  768. cp_write(sd, 0xac, (height & 0x0f) << 4);
  769. }
  770. static void set_rgb_quantization_range(struct v4l2_subdev *sd)
  771. {
  772. struct adv7604_state *state = to_state(sd);
  773. switch (state->rgb_quantization_range) {
  774. case V4L2_DV_RGB_RANGE_AUTO:
  775. /* automatic */
  776. if (DIGITAL_INPUT && !(hdmi_read(sd, 0x05) & 0x80)) {
  777. /* receiving DVI-D signal */
  778. /* ADV7604 selects RGB limited range regardless of
  779. input format (CE/IT) in automatic mode */
  780. if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
  781. /* RGB limited range (16-235) */
  782. io_write_and_or(sd, 0x02, 0x0f, 0x00);
  783. } else {
  784. /* RGB full range (0-255) */
  785. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  786. }
  787. } else {
  788. /* receiving HDMI or analog signal, set automode */
  789. io_write_and_or(sd, 0x02, 0x0f, 0xf0);
  790. }
  791. break;
  792. case V4L2_DV_RGB_RANGE_LIMITED:
  793. /* RGB limited range (16-235) */
  794. io_write_and_or(sd, 0x02, 0x0f, 0x00);
  795. break;
  796. case V4L2_DV_RGB_RANGE_FULL:
  797. /* RGB full range (0-255) */
  798. io_write_and_or(sd, 0x02, 0x0f, 0x10);
  799. break;
  800. }
  801. }
  802. static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
  803. {
  804. struct v4l2_subdev *sd = to_sd(ctrl);
  805. struct adv7604_state *state = to_state(sd);
  806. switch (ctrl->id) {
  807. case V4L2_CID_BRIGHTNESS:
  808. cp_write(sd, 0x3c, ctrl->val);
  809. return 0;
  810. case V4L2_CID_CONTRAST:
  811. cp_write(sd, 0x3a, ctrl->val);
  812. return 0;
  813. case V4L2_CID_SATURATION:
  814. cp_write(sd, 0x3b, ctrl->val);
  815. return 0;
  816. case V4L2_CID_HUE:
  817. cp_write(sd, 0x3d, ctrl->val);
  818. return 0;
  819. case V4L2_CID_DV_RX_RGB_RANGE:
  820. state->rgb_quantization_range = ctrl->val;
  821. set_rgb_quantization_range(sd);
  822. return 0;
  823. case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
  824. /* Set the analog sampling phase. This is needed to find the
  825. best sampling phase for analog video: an application or
  826. driver has to try a number of phases and analyze the picture
  827. quality before settling on the best performing phase. */
  828. afe_write(sd, 0xc8, ctrl->val);
  829. return 0;
  830. case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
  831. /* Use the default blue color for free running mode,
  832. or supply your own. */
  833. cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
  834. return 0;
  835. case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
  836. cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
  837. cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
  838. cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
  839. return 0;
  840. }
  841. return -EINVAL;
  842. }
  843. static int adv7604_g_chip_ident(struct v4l2_subdev *sd,
  844. struct v4l2_dbg_chip_ident *chip)
  845. {
  846. struct i2c_client *client = v4l2_get_subdevdata(sd);
  847. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7604, 0);
  848. }
  849. /* ----------------------------------------------------------------------- */
  850. static inline bool no_power(struct v4l2_subdev *sd)
  851. {
  852. /* Entire chip or CP powered off */
  853. return io_read(sd, 0x0c) & 0x24;
  854. }
  855. static inline bool no_signal_tmds(struct v4l2_subdev *sd)
  856. {
  857. /* TODO port B, C and D */
  858. return !(io_read(sd, 0x6a) & 0x10);
  859. }
  860. static inline bool no_lock_tmds(struct v4l2_subdev *sd)
  861. {
  862. return (io_read(sd, 0x6a) & 0xe0) != 0xe0;
  863. }
  864. static inline bool no_lock_sspd(struct v4l2_subdev *sd)
  865. {
  866. /* TODO channel 2 */
  867. return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
  868. }
  869. static inline bool no_lock_stdi(struct v4l2_subdev *sd)
  870. {
  871. /* TODO channel 2 */
  872. return !(cp_read(sd, 0xb1) & 0x80);
  873. }
  874. static inline bool no_signal(struct v4l2_subdev *sd)
  875. {
  876. struct adv7604_state *state = to_state(sd);
  877. bool ret;
  878. ret = no_power(sd);
  879. ret |= no_lock_stdi(sd);
  880. ret |= no_lock_sspd(sd);
  881. if (DIGITAL_INPUT) {
  882. ret |= no_lock_tmds(sd);
  883. ret |= no_signal_tmds(sd);
  884. }
  885. return ret;
  886. }
  887. static inline bool no_lock_cp(struct v4l2_subdev *sd)
  888. {
  889. /* CP has detected a non standard number of lines on the incoming
  890. video compared to what it is configured to receive by s_dv_timings */
  891. return io_read(sd, 0x12) & 0x01;
  892. }
  893. static int adv7604_g_input_status(struct v4l2_subdev *sd, u32 *status)
  894. {
  895. struct adv7604_state *state = to_state(sd);
  896. *status = 0;
  897. *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
  898. *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
  899. if (no_lock_cp(sd))
  900. *status |= DIGITAL_INPUT ? V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
  901. v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
  902. return 0;
  903. }
  904. /* ----------------------------------------------------------------------- */
  905. static void adv7604_print_timings(struct v4l2_subdev *sd,
  906. struct v4l2_dv_timings *timings, const char *txt, bool detailed)
  907. {
  908. struct v4l2_bt_timings *bt = &timings->bt;
  909. u32 htot, vtot;
  910. if (timings->type != V4L2_DV_BT_656_1120)
  911. return;
  912. htot = htotal(bt);
  913. vtot = vtotal(bt);
  914. v4l2_info(sd, "%s %dx%d%s%d (%dx%d)",
  915. txt, bt->width, bt->height, bt->interlaced ? "i" : "p",
  916. (htot * vtot) > 0 ? ((u32)bt->pixelclock /
  917. (htot * vtot)) : 0,
  918. htot, vtot);
  919. if (detailed) {
  920. v4l2_info(sd, " horizontal: fp = %d, %ssync = %d, bp = %d\n",
  921. bt->hfrontporch,
  922. (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
  923. bt->hsync, bt->hbackporch);
  924. v4l2_info(sd, " vertical: fp = %d, %ssync = %d, bp = %d\n",
  925. bt->vfrontporch,
  926. (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
  927. bt->vsync, bt->vbackporch);
  928. v4l2_info(sd, " pixelclock: %lld, flags: 0x%x, standards: 0x%x\n",
  929. bt->pixelclock, bt->flags, bt->standards);
  930. }
  931. }
  932. struct stdi_readback {
  933. u16 bl, lcf, lcvs;
  934. u8 hs_pol, vs_pol;
  935. bool interlaced;
  936. };
  937. static int stdi2dv_timings(struct v4l2_subdev *sd,
  938. struct stdi_readback *stdi,
  939. struct v4l2_dv_timings *timings)
  940. {
  941. struct adv7604_state *state = to_state(sd);
  942. u32 hfreq = (ADV7604_fsc * 8) / stdi->bl;
  943. u32 pix_clk;
  944. int i;
  945. for (i = 0; adv7604_timings[i].bt.height; i++) {
  946. if (vtotal(&adv7604_timings[i].bt) != stdi->lcf + 1)
  947. continue;
  948. if (adv7604_timings[i].bt.vsync != stdi->lcvs)
  949. continue;
  950. pix_clk = hfreq * htotal(&adv7604_timings[i].bt);
  951. if ((pix_clk < adv7604_timings[i].bt.pixelclock + 1000000) &&
  952. (pix_clk > adv7604_timings[i].bt.pixelclock - 1000000)) {
  953. *timings = adv7604_timings[i];
  954. return 0;
  955. }
  956. }
  957. if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
  958. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  959. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  960. timings))
  961. return 0;
  962. if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
  963. (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
  964. (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
  965. state->aspect_ratio, timings))
  966. return 0;
  967. v4l2_dbg(2, debug, sd,
  968. "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
  969. __func__, stdi->lcvs, stdi->lcf, stdi->bl,
  970. stdi->hs_pol, stdi->vs_pol);
  971. return -1;
  972. }
  973. static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
  974. {
  975. if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
  976. v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
  977. return -1;
  978. }
  979. /* read STDI */
  980. stdi->bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
  981. stdi->lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
  982. stdi->lcvs = cp_read(sd, 0xb3) >> 3;
  983. stdi->interlaced = io_read(sd, 0x12) & 0x10;
  984. /* read SSPD */
  985. if ((cp_read(sd, 0xb5) & 0x03) == 0x01) {
  986. stdi->hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
  987. ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
  988. stdi->vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
  989. ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
  990. } else {
  991. stdi->hs_pol = 'x';
  992. stdi->vs_pol = 'x';
  993. }
  994. if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
  995. v4l2_dbg(2, debug, sd,
  996. "%s: signal lost during readout of STDI/SSPD\n", __func__);
  997. return -1;
  998. }
  999. if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
  1000. v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
  1001. memset(stdi, 0, sizeof(struct stdi_readback));
  1002. return -1;
  1003. }
  1004. v4l2_dbg(2, debug, sd,
  1005. "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
  1006. __func__, stdi->lcf, stdi->bl, stdi->lcvs,
  1007. stdi->hs_pol, stdi->vs_pol,
  1008. stdi->interlaced ? "interlaced" : "progressive");
  1009. return 0;
  1010. }
  1011. static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
  1012. struct v4l2_enum_dv_timings *timings)
  1013. {
  1014. if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
  1015. return -EINVAL;
  1016. memset(timings->reserved, 0, sizeof(timings->reserved));
  1017. timings->timings = adv7604_timings[timings->index];
  1018. return 0;
  1019. }
  1020. static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
  1021. struct v4l2_dv_timings_cap *cap)
  1022. {
  1023. struct adv7604_state *state = to_state(sd);
  1024. cap->type = V4L2_DV_BT_656_1120;
  1025. cap->bt.max_width = 1920;
  1026. cap->bt.max_height = 1200;
  1027. cap->bt.min_pixelclock = 27000000;
  1028. if (DIGITAL_INPUT)
  1029. cap->bt.max_pixelclock = 225000000;
  1030. else
  1031. cap->bt.max_pixelclock = 170000000;
  1032. cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  1033. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
  1034. cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
  1035. V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
  1036. return 0;
  1037. }
  1038. /* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
  1039. if the format is listed in adv7604_timings[] */
  1040. static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
  1041. struct v4l2_dv_timings *timings)
  1042. {
  1043. struct adv7604_state *state = to_state(sd);
  1044. int i;
  1045. for (i = 0; adv7604_timings[i].bt.width; i++) {
  1046. if (v4l_match_dv_timings(timings, &adv7604_timings[i],
  1047. DIGITAL_INPUT ? 250000 : 1000000)) {
  1048. *timings = adv7604_timings[i];
  1049. break;
  1050. }
  1051. }
  1052. }
  1053. static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
  1054. struct v4l2_dv_timings *timings)
  1055. {
  1056. struct adv7604_state *state = to_state(sd);
  1057. struct v4l2_bt_timings *bt = &timings->bt;
  1058. struct stdi_readback stdi;
  1059. if (!timings)
  1060. return -EINVAL;
  1061. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1062. if (no_signal(sd)) {
  1063. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  1064. return -ENOLINK;
  1065. }
  1066. /* read STDI */
  1067. if (read_stdi(sd, &stdi)) {
  1068. v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
  1069. return -ENOLINK;
  1070. }
  1071. bt->interlaced = stdi.interlaced ?
  1072. V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  1073. if (DIGITAL_INPUT) {
  1074. timings->type = V4L2_DV_BT_656_1120;
  1075. bt->width = (hdmi_read(sd, 0x07) & 0x0f) * 256 + hdmi_read(sd, 0x08);
  1076. bt->height = (hdmi_read(sd, 0x09) & 0x0f) * 256 + hdmi_read(sd, 0x0a);
  1077. bt->pixelclock = (hdmi_read(sd, 0x06) * 1000000) +
  1078. ((hdmi_read(sd, 0x3b) & 0x30) >> 4) * 250000;
  1079. bt->hfrontporch = (hdmi_read(sd, 0x20) & 0x03) * 256 +
  1080. hdmi_read(sd, 0x21);
  1081. bt->hsync = (hdmi_read(sd, 0x22) & 0x03) * 256 +
  1082. hdmi_read(sd, 0x23);
  1083. bt->hbackporch = (hdmi_read(sd, 0x24) & 0x03) * 256 +
  1084. hdmi_read(sd, 0x25);
  1085. bt->vfrontporch = ((hdmi_read(sd, 0x2a) & 0x1f) * 256 +
  1086. hdmi_read(sd, 0x2b)) / 2;
  1087. bt->vsync = ((hdmi_read(sd, 0x2e) & 0x1f) * 256 +
  1088. hdmi_read(sd, 0x2f)) / 2;
  1089. bt->vbackporch = ((hdmi_read(sd, 0x32) & 0x1f) * 256 +
  1090. hdmi_read(sd, 0x33)) / 2;
  1091. bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
  1092. ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
  1093. if (bt->interlaced == V4L2_DV_INTERLACED) {
  1094. bt->height += (hdmi_read(sd, 0x0b) & 0x0f) * 256 +
  1095. hdmi_read(sd, 0x0c);
  1096. bt->il_vfrontporch = ((hdmi_read(sd, 0x2c) & 0x1f) * 256 +
  1097. hdmi_read(sd, 0x2d)) / 2;
  1098. bt->il_vsync = ((hdmi_read(sd, 0x30) & 0x1f) * 256 +
  1099. hdmi_read(sd, 0x31)) / 2;
  1100. bt->vbackporch = ((hdmi_read(sd, 0x34) & 0x1f) * 256 +
  1101. hdmi_read(sd, 0x35)) / 2;
  1102. }
  1103. adv7604_fill_optional_dv_timings_fields(sd, timings);
  1104. } else {
  1105. /* find format
  1106. * Since LCVS values are inaccurate [REF_03, p. 275-276],
  1107. * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
  1108. */
  1109. if (!stdi2dv_timings(sd, &stdi, timings))
  1110. goto found;
  1111. stdi.lcvs += 1;
  1112. v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
  1113. if (!stdi2dv_timings(sd, &stdi, timings))
  1114. goto found;
  1115. stdi.lcvs -= 2;
  1116. v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
  1117. if (stdi2dv_timings(sd, &stdi, timings)) {
  1118. /*
  1119. * The STDI block may measure wrong values, especially
  1120. * for lcvs and lcf. If the driver can not find any
  1121. * valid timing, the STDI block is restarted to measure
  1122. * the video timings again. The function will return an
  1123. * error, but the restart of STDI will generate a new
  1124. * STDI interrupt and the format detection process will
  1125. * restart.
  1126. */
  1127. if (state->restart_stdi_once) {
  1128. v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
  1129. /* TODO restart STDI for Sync Channel 2 */
  1130. /* enter one-shot mode */
  1131. cp_write_and_or(sd, 0x86, 0xf9, 0x00);
  1132. /* trigger STDI restart */
  1133. cp_write_and_or(sd, 0x86, 0xf9, 0x04);
  1134. /* reset to continuous mode */
  1135. cp_write_and_or(sd, 0x86, 0xf9, 0x02);
  1136. state->restart_stdi_once = false;
  1137. return -ENOLINK;
  1138. }
  1139. v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
  1140. return -ERANGE;
  1141. }
  1142. state->restart_stdi_once = true;
  1143. }
  1144. found:
  1145. if (no_signal(sd)) {
  1146. v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
  1147. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  1148. return -ENOLINK;
  1149. }
  1150. if ((!DIGITAL_INPUT && bt->pixelclock > 170000000) ||
  1151. (DIGITAL_INPUT && bt->pixelclock > 225000000)) {
  1152. v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
  1153. __func__, (u32)bt->pixelclock);
  1154. return -ERANGE;
  1155. }
  1156. if (debug > 1)
  1157. adv7604_print_timings(sd, timings,
  1158. "adv7604_query_dv_timings:", true);
  1159. return 0;
  1160. }
  1161. static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
  1162. struct v4l2_dv_timings *timings)
  1163. {
  1164. struct adv7604_state *state = to_state(sd);
  1165. struct v4l2_bt_timings *bt;
  1166. int err;
  1167. if (!timings)
  1168. return -EINVAL;
  1169. bt = &timings->bt;
  1170. if ((!DIGITAL_INPUT && bt->pixelclock > 170000000) ||
  1171. (DIGITAL_INPUT && bt->pixelclock > 225000000)) {
  1172. v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
  1173. __func__, (u32)bt->pixelclock);
  1174. return -ERANGE;
  1175. }
  1176. adv7604_fill_optional_dv_timings_fields(sd, timings);
  1177. state->timings = *timings;
  1178. cp_write(sd, 0x91, bt->interlaced ? 0x50 : 0x10);
  1179. /* Use prim_mode and vid_std when available */
  1180. err = configure_predefined_video_timings(sd, timings);
  1181. if (err) {
  1182. /* custom settings when the video format
  1183. does not have prim_mode/vid_std */
  1184. configure_custom_video_timings(sd, bt);
  1185. }
  1186. set_rgb_quantization_range(sd);
  1187. if (debug > 1)
  1188. adv7604_print_timings(sd, timings,
  1189. "adv7604_s_dv_timings:", true);
  1190. return 0;
  1191. }
  1192. static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
  1193. struct v4l2_dv_timings *timings)
  1194. {
  1195. struct adv7604_state *state = to_state(sd);
  1196. *timings = state->timings;
  1197. return 0;
  1198. }
  1199. static void enable_input(struct v4l2_subdev *sd)
  1200. {
  1201. struct adv7604_state *state = to_state(sd);
  1202. switch (state->mode) {
  1203. case ADV7604_MODE_COMP:
  1204. case ADV7604_MODE_GR:
  1205. /* enable */
  1206. io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
  1207. break;
  1208. case ADV7604_MODE_HDMI:
  1209. /* enable */
  1210. hdmi_write(sd, 0x1a, 0x0a); /* Unmute audio */
  1211. hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */
  1212. io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
  1213. break;
  1214. default:
  1215. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  1216. __func__, state->mode);
  1217. break;
  1218. }
  1219. }
  1220. static void disable_input(struct v4l2_subdev *sd)
  1221. {
  1222. /* disable */
  1223. io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
  1224. hdmi_write(sd, 0x1a, 0x1a); /* Mute audio */
  1225. hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */
  1226. }
  1227. static void select_input(struct v4l2_subdev *sd)
  1228. {
  1229. struct adv7604_state *state = to_state(sd);
  1230. switch (state->mode) {
  1231. case ADV7604_MODE_COMP:
  1232. case ADV7604_MODE_GR:
  1233. /* reset ADI recommended settings for HDMI: */
  1234. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
  1235. hdmi_write(sd, 0x0d, 0x04); /* HDMI filter optimization */
  1236. hdmi_write(sd, 0x3d, 0x00); /* DDC bus active pull-up control */
  1237. hdmi_write(sd, 0x3e, 0x74); /* TMDS PLL optimization */
  1238. hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
  1239. hdmi_write(sd, 0x57, 0x74); /* TMDS PLL optimization */
  1240. hdmi_write(sd, 0x58, 0x63); /* TMDS PLL optimization */
  1241. hdmi_write(sd, 0x8d, 0x18); /* equaliser */
  1242. hdmi_write(sd, 0x8e, 0x34); /* equaliser */
  1243. hdmi_write(sd, 0x93, 0x88); /* equaliser */
  1244. hdmi_write(sd, 0x94, 0x2e); /* equaliser */
  1245. hdmi_write(sd, 0x96, 0x00); /* enable automatic EQ changing */
  1246. afe_write(sd, 0x00, 0x08); /* power up ADC */
  1247. afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
  1248. afe_write(sd, 0xc8, 0x00); /* phase control */
  1249. /* set ADI recommended settings for digitizer */
  1250. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
  1251. afe_write(sd, 0x12, 0x7b); /* ADC noise shaping filter controls */
  1252. afe_write(sd, 0x0c, 0x1f); /* CP core gain controls */
  1253. cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
  1254. cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
  1255. cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
  1256. break;
  1257. case ADV7604_MODE_HDMI:
  1258. /* set ADI recommended settings for HDMI: */
  1259. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
  1260. hdmi_write(sd, 0x0d, 0x84); /* HDMI filter optimization */
  1261. hdmi_write(sd, 0x3d, 0x10); /* DDC bus active pull-up control */
  1262. hdmi_write(sd, 0x3e, 0x39); /* TMDS PLL optimization */
  1263. hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
  1264. hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */
  1265. hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */
  1266. hdmi_write(sd, 0x8d, 0x18); /* equaliser */
  1267. hdmi_write(sd, 0x8e, 0x34); /* equaliser */
  1268. hdmi_write(sd, 0x93, 0x8b); /* equaliser */
  1269. hdmi_write(sd, 0x94, 0x2d); /* equaliser */
  1270. hdmi_write(sd, 0x96, 0x01); /* enable automatic EQ changing */
  1271. afe_write(sd, 0x00, 0xff); /* power down ADC */
  1272. afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
  1273. afe_write(sd, 0xc8, 0x40); /* phase control */
  1274. /* reset ADI recommended settings for digitizer */
  1275. /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
  1276. afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
  1277. afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
  1278. cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
  1279. cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
  1280. cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
  1281. break;
  1282. default:
  1283. v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
  1284. __func__, state->mode);
  1285. break;
  1286. }
  1287. }
  1288. static int adv7604_s_routing(struct v4l2_subdev *sd,
  1289. u32 input, u32 output, u32 config)
  1290. {
  1291. struct adv7604_state *state = to_state(sd);
  1292. v4l2_dbg(2, debug, sd, "%s: input %d", __func__, input);
  1293. state->mode = input;
  1294. disable_input(sd);
  1295. select_input(sd);
  1296. enable_input(sd);
  1297. return 0;
  1298. }
  1299. static int adv7604_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
  1300. enum v4l2_mbus_pixelcode *code)
  1301. {
  1302. if (index)
  1303. return -EINVAL;
  1304. /* Good enough for now */
  1305. *code = V4L2_MBUS_FMT_FIXED;
  1306. return 0;
  1307. }
  1308. static int adv7604_g_mbus_fmt(struct v4l2_subdev *sd,
  1309. struct v4l2_mbus_framefmt *fmt)
  1310. {
  1311. struct adv7604_state *state = to_state(sd);
  1312. fmt->width = state->timings.bt.width;
  1313. fmt->height = state->timings.bt.height;
  1314. fmt->code = V4L2_MBUS_FMT_FIXED;
  1315. fmt->field = V4L2_FIELD_NONE;
  1316. if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
  1317. fmt->colorspace = (state->timings.bt.height <= 576) ?
  1318. V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
  1319. }
  1320. return 0;
  1321. }
  1322. static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  1323. {
  1324. struct adv7604_state *state = to_state(sd);
  1325. u8 fmt_change, fmt_change_digital, tx_5v;
  1326. /* format change */
  1327. fmt_change = io_read(sd, 0x43) & 0x98;
  1328. if (fmt_change)
  1329. io_write(sd, 0x44, fmt_change);
  1330. fmt_change_digital = DIGITAL_INPUT ? (io_read(sd, 0x6b) & 0xc0) : 0;
  1331. if (fmt_change_digital)
  1332. io_write(sd, 0x6c, fmt_change_digital);
  1333. if (fmt_change || fmt_change_digital) {
  1334. v4l2_dbg(1, debug, sd,
  1335. "%s: ADV7604_FMT_CHANGE, fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
  1336. __func__, fmt_change, fmt_change_digital);
  1337. v4l2_subdev_notify(sd, ADV7604_FMT_CHANGE, NULL);
  1338. if (handled)
  1339. *handled = true;
  1340. }
  1341. /* tx 5v detect */
  1342. tx_5v = io_read(sd, 0x70) & 0x10;
  1343. if (tx_5v) {
  1344. v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
  1345. io_write(sd, 0x71, tx_5v);
  1346. adv7604_s_detect_tx_5v_ctrl(sd);
  1347. if (handled)
  1348. *handled = true;
  1349. }
  1350. return 0;
  1351. }
  1352. static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
  1353. {
  1354. struct adv7604_state *state = to_state(sd);
  1355. if (edid->pad != 0)
  1356. return -EINVAL;
  1357. if (edid->blocks == 0)
  1358. return -EINVAL;
  1359. if (edid->start_block >= state->edid_blocks)
  1360. return -EINVAL;
  1361. if (edid->start_block + edid->blocks > state->edid_blocks)
  1362. edid->blocks = state->edid_blocks - edid->start_block;
  1363. if (!edid->edid)
  1364. return -EINVAL;
  1365. memcpy(edid->edid + edid->start_block * 128,
  1366. state->edid + edid->start_block * 128,
  1367. edid->blocks * 128);
  1368. return 0;
  1369. }
  1370. static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
  1371. {
  1372. struct adv7604_state *state = to_state(sd);
  1373. int err;
  1374. if (edid->pad != 0)
  1375. return -EINVAL;
  1376. if (edid->start_block != 0)
  1377. return -EINVAL;
  1378. if (edid->blocks == 0) {
  1379. /* Pull down the hotplug pin */
  1380. v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)0);
  1381. /* Disables I2C access to internal EDID ram from DDC port */
  1382. rep_write_and_or(sd, 0x77, 0xf0, 0x0);
  1383. state->edid_blocks = 0;
  1384. /* Fall back to a 16:9 aspect ratio */
  1385. state->aspect_ratio.numerator = 16;
  1386. state->aspect_ratio.denominator = 9;
  1387. return 0;
  1388. }
  1389. if (edid->blocks > 2)
  1390. return -E2BIG;
  1391. if (!edid->edid)
  1392. return -EINVAL;
  1393. memcpy(state->edid, edid->edid, 128 * edid->blocks);
  1394. state->edid_blocks = edid->blocks;
  1395. state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
  1396. edid->edid[0x16]);
  1397. err = edid_write_block(sd, 128 * edid->blocks, state->edid);
  1398. if (err < 0)
  1399. v4l2_err(sd, "error %d writing edid\n", err);
  1400. return err;
  1401. }
  1402. /*********** avi info frame CEA-861-E **************/
  1403. static void print_avi_infoframe(struct v4l2_subdev *sd)
  1404. {
  1405. int i;
  1406. u8 buf[14];
  1407. u8 avi_len;
  1408. u8 avi_ver;
  1409. if (!(hdmi_read(sd, 0x05) & 0x80)) {
  1410. v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
  1411. return;
  1412. }
  1413. if (!(io_read(sd, 0x60) & 0x01)) {
  1414. v4l2_info(sd, "AVI infoframe not received\n");
  1415. return;
  1416. }
  1417. if (io_read(sd, 0x83) & 0x01) {
  1418. v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
  1419. io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
  1420. if (io_read(sd, 0x83) & 0x01) {
  1421. v4l2_info(sd, "AVI infoframe checksum error still present\n");
  1422. io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
  1423. }
  1424. }
  1425. avi_len = infoframe_read(sd, 0xe2);
  1426. avi_ver = infoframe_read(sd, 0xe1);
  1427. v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
  1428. avi_ver, avi_len);
  1429. if (avi_ver != 0x02)
  1430. return;
  1431. for (i = 0; i < 14; i++)
  1432. buf[i] = infoframe_read(sd, i);
  1433. v4l2_info(sd,
  1434. "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1435. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
  1436. buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
  1437. }
  1438. static int adv7604_log_status(struct v4l2_subdev *sd)
  1439. {
  1440. struct adv7604_state *state = to_state(sd);
  1441. struct v4l2_dv_timings timings;
  1442. struct stdi_readback stdi;
  1443. u8 reg_io_0x02 = io_read(sd, 0x02);
  1444. char *csc_coeff_sel_rb[16] = {
  1445. "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
  1446. "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
  1447. "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
  1448. "reserved", "reserved", "reserved", "reserved", "manual"
  1449. };
  1450. char *input_color_space_txt[16] = {
  1451. "RGB limited range (16-235)", "RGB full range (0-255)",
  1452. "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
  1453. "XvYCC Bt.601", "XvYCC Bt.709",
  1454. "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
  1455. "invalid", "invalid", "invalid", "invalid", "invalid",
  1456. "invalid", "invalid", "automatic"
  1457. };
  1458. char *rgb_quantization_range_txt[] = {
  1459. "Automatic",
  1460. "RGB limited range (16-235)",
  1461. "RGB full range (0-255)",
  1462. };
  1463. v4l2_info(sd, "-----Chip status-----\n");
  1464. v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
  1465. v4l2_info(sd, "Connector type: %s\n", state->connector_hdmi ?
  1466. "HDMI" : (DIGITAL_INPUT ? "DVI-D" : "DVI-A"));
  1467. v4l2_info(sd, "EDID: %s\n", ((rep_read(sd, 0x7d) & 0x01) &&
  1468. (rep_read(sd, 0x77) & 0x01)) ? "enabled" : "disabled ");
  1469. v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
  1470. "enabled" : "disabled");
  1471. v4l2_info(sd, "-----Signal status-----\n");
  1472. v4l2_info(sd, "Cable detected (+5V power): %s\n",
  1473. (io_read(sd, 0x6f) & 0x10) ? "true" : "false");
  1474. v4l2_info(sd, "TMDS signal detected: %s\n",
  1475. no_signal_tmds(sd) ? "false" : "true");
  1476. v4l2_info(sd, "TMDS signal locked: %s\n",
  1477. no_lock_tmds(sd) ? "false" : "true");
  1478. v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
  1479. v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
  1480. v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
  1481. v4l2_info(sd, "CP free run: %s\n",
  1482. (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
  1483. v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
  1484. io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
  1485. (io_read(sd, 0x01) & 0x70) >> 4);
  1486. v4l2_info(sd, "-----Video Timings-----\n");
  1487. if (read_stdi(sd, &stdi))
  1488. v4l2_info(sd, "STDI: not locked\n");
  1489. else
  1490. v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
  1491. stdi.lcf, stdi.bl, stdi.lcvs,
  1492. stdi.interlaced ? "interlaced" : "progressive",
  1493. stdi.hs_pol, stdi.vs_pol);
  1494. if (adv7604_query_dv_timings(sd, &timings))
  1495. v4l2_info(sd, "No video detected\n");
  1496. else
  1497. adv7604_print_timings(sd, &timings, "Detected format:", true);
  1498. adv7604_print_timings(sd, &state->timings, "Configured format:", true);
  1499. v4l2_info(sd, "-----Color space-----\n");
  1500. v4l2_info(sd, "RGB quantization range ctrl: %s\n",
  1501. rgb_quantization_range_txt[state->rgb_quantization_range]);
  1502. v4l2_info(sd, "Input color space: %s\n",
  1503. input_color_space_txt[reg_io_0x02 >> 4]);
  1504. v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
  1505. (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
  1506. (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
  1507. ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
  1508. "enabled" : "disabled");
  1509. v4l2_info(sd, "Color space conversion: %s\n",
  1510. csc_coeff_sel_rb[cp_read(sd, 0xfc) >> 4]);
  1511. /* Digital video */
  1512. if (DIGITAL_INPUT) {
  1513. v4l2_info(sd, "-----HDMI status-----\n");
  1514. v4l2_info(sd, "HDCP encrypted content: %s\n",
  1515. hdmi_read(sd, 0x05) & 0x40 ? "true" : "false");
  1516. print_avi_infoframe(sd);
  1517. }
  1518. return 0;
  1519. }
  1520. /* ----------------------------------------------------------------------- */
  1521. static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
  1522. .s_ctrl = adv7604_s_ctrl,
  1523. };
  1524. static const struct v4l2_subdev_core_ops adv7604_core_ops = {
  1525. .log_status = adv7604_log_status,
  1526. .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
  1527. .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
  1528. .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
  1529. .g_ctrl = v4l2_subdev_g_ctrl,
  1530. .s_ctrl = v4l2_subdev_s_ctrl,
  1531. .queryctrl = v4l2_subdev_queryctrl,
  1532. .querymenu = v4l2_subdev_querymenu,
  1533. .g_chip_ident = adv7604_g_chip_ident,
  1534. .interrupt_service_routine = adv7604_isr,
  1535. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1536. .g_register = adv7604_g_register,
  1537. .s_register = adv7604_s_register,
  1538. #endif
  1539. };
  1540. static const struct v4l2_subdev_video_ops adv7604_video_ops = {
  1541. .s_routing = adv7604_s_routing,
  1542. .g_input_status = adv7604_g_input_status,
  1543. .s_dv_timings = adv7604_s_dv_timings,
  1544. .g_dv_timings = adv7604_g_dv_timings,
  1545. .query_dv_timings = adv7604_query_dv_timings,
  1546. .enum_dv_timings = adv7604_enum_dv_timings,
  1547. .dv_timings_cap = adv7604_dv_timings_cap,
  1548. .enum_mbus_fmt = adv7604_enum_mbus_fmt,
  1549. .g_mbus_fmt = adv7604_g_mbus_fmt,
  1550. .try_mbus_fmt = adv7604_g_mbus_fmt,
  1551. .s_mbus_fmt = adv7604_g_mbus_fmt,
  1552. };
  1553. static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
  1554. .get_edid = adv7604_get_edid,
  1555. .set_edid = adv7604_set_edid,
  1556. };
  1557. static const struct v4l2_subdev_ops adv7604_ops = {
  1558. .core = &adv7604_core_ops,
  1559. .video = &adv7604_video_ops,
  1560. .pad = &adv7604_pad_ops,
  1561. };
  1562. /* -------------------------- custom ctrls ---------------------------------- */
  1563. static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
  1564. .ops = &adv7604_ctrl_ops,
  1565. .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
  1566. .name = "Analog Sampling Phase",
  1567. .type = V4L2_CTRL_TYPE_INTEGER,
  1568. .min = 0,
  1569. .max = 0x1f,
  1570. .step = 1,
  1571. .def = 0,
  1572. };
  1573. static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color_manual = {
  1574. .ops = &adv7604_ctrl_ops,
  1575. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
  1576. .name = "Free Running Color, Manual",
  1577. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1578. .min = false,
  1579. .max = true,
  1580. .step = 1,
  1581. .def = false,
  1582. };
  1583. static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
  1584. .ops = &adv7604_ctrl_ops,
  1585. .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
  1586. .name = "Free Running Color",
  1587. .type = V4L2_CTRL_TYPE_INTEGER,
  1588. .min = 0x0,
  1589. .max = 0xffffff,
  1590. .step = 0x1,
  1591. .def = 0x0,
  1592. };
  1593. /* ----------------------------------------------------------------------- */
  1594. static int adv7604_core_init(struct v4l2_subdev *sd)
  1595. {
  1596. struct adv7604_state *state = to_state(sd);
  1597. struct adv7604_platform_data *pdata = &state->pdata;
  1598. hdmi_write(sd, 0x48,
  1599. (pdata->disable_pwrdnb ? 0x80 : 0) |
  1600. (pdata->disable_cable_det_rst ? 0x40 : 0));
  1601. disable_input(sd);
  1602. /* power */
  1603. io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
  1604. io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
  1605. cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
  1606. /* video format */
  1607. io_write_and_or(sd, 0x02, 0xf0,
  1608. pdata->alt_gamma << 3 |
  1609. pdata->op_656_range << 2 |
  1610. pdata->rgb_out << 1 |
  1611. pdata->alt_data_sat << 0);
  1612. io_write(sd, 0x03, pdata->op_format_sel);
  1613. io_write_and_or(sd, 0x04, 0x1f, pdata->op_ch_sel << 5);
  1614. io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
  1615. pdata->insert_av_codes << 2 |
  1616. pdata->replicate_av_codes << 1 |
  1617. pdata->invert_cbcr << 0);
  1618. /* TODO from platform data */
  1619. cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
  1620. io_write(sd, 0x06, 0xa6); /* positive VS and HS */
  1621. io_write(sd, 0x14, 0x7f); /* Drive strength adjusted to max */
  1622. cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
  1623. cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
  1624. cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
  1625. ADI recommended setting [REF_01, c. 2.3.3] */
  1626. cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
  1627. ADI recommended setting [REF_01, c. 2.3.3] */
  1628. cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
  1629. for digital formats */
  1630. /* TODO from platform data */
  1631. afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
  1632. afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
  1633. io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
  1634. /* interrupts */
  1635. io_write(sd, 0x40, 0xc2); /* Configure INT1 */
  1636. io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
  1637. io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
  1638. io_write(sd, 0x6e, 0xc0); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
  1639. io_write(sd, 0x73, 0x10); /* Enable CABLE_DET_A_ST (+5v) interrupt */
  1640. return v4l2_ctrl_handler_setup(sd->ctrl_handler);
  1641. }
  1642. static void adv7604_unregister_clients(struct adv7604_state *state)
  1643. {
  1644. if (state->i2c_avlink)
  1645. i2c_unregister_device(state->i2c_avlink);
  1646. if (state->i2c_cec)
  1647. i2c_unregister_device(state->i2c_cec);
  1648. if (state->i2c_infoframe)
  1649. i2c_unregister_device(state->i2c_infoframe);
  1650. if (state->i2c_esdp)
  1651. i2c_unregister_device(state->i2c_esdp);
  1652. if (state->i2c_dpp)
  1653. i2c_unregister_device(state->i2c_dpp);
  1654. if (state->i2c_afe)
  1655. i2c_unregister_device(state->i2c_afe);
  1656. if (state->i2c_repeater)
  1657. i2c_unregister_device(state->i2c_repeater);
  1658. if (state->i2c_edid)
  1659. i2c_unregister_device(state->i2c_edid);
  1660. if (state->i2c_hdmi)
  1661. i2c_unregister_device(state->i2c_hdmi);
  1662. if (state->i2c_test)
  1663. i2c_unregister_device(state->i2c_test);
  1664. if (state->i2c_cp)
  1665. i2c_unregister_device(state->i2c_cp);
  1666. if (state->i2c_vdp)
  1667. i2c_unregister_device(state->i2c_vdp);
  1668. }
  1669. static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
  1670. u8 addr, u8 io_reg)
  1671. {
  1672. struct i2c_client *client = v4l2_get_subdevdata(sd);
  1673. if (addr)
  1674. io_write(sd, io_reg, addr << 1);
  1675. return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
  1676. }
  1677. static int adv7604_probe(struct i2c_client *client,
  1678. const struct i2c_device_id *id)
  1679. {
  1680. struct adv7604_state *state;
  1681. struct adv7604_platform_data *pdata = client->dev.platform_data;
  1682. struct v4l2_ctrl_handler *hdl;
  1683. struct v4l2_subdev *sd;
  1684. int err;
  1685. /* Check if the adapter supports the needed features */
  1686. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1687. return -EIO;
  1688. v4l_dbg(1, debug, client, "detecting adv7604 client on address 0x%x\n",
  1689. client->addr << 1);
  1690. state = kzalloc(sizeof(struct adv7604_state), GFP_KERNEL);
  1691. if (!state) {
  1692. v4l_err(client, "Could not allocate adv7604_state memory!\n");
  1693. return -ENOMEM;
  1694. }
  1695. /* platform data */
  1696. if (!pdata) {
  1697. v4l_err(client, "No platform data!\n");
  1698. err = -ENODEV;
  1699. goto err_state;
  1700. }
  1701. memcpy(&state->pdata, pdata, sizeof(state->pdata));
  1702. sd = &state->sd;
  1703. v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
  1704. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1705. state->connector_hdmi = pdata->connector_hdmi;
  1706. /* i2c access to adv7604? */
  1707. if (adv_smbus_read_byte_data_check(client, 0xfb, false) != 0x68) {
  1708. v4l2_info(sd, "not an adv7604 on address 0x%x\n",
  1709. client->addr << 1);
  1710. err = -ENODEV;
  1711. goto err_state;
  1712. }
  1713. /* control handlers */
  1714. hdl = &state->hdl;
  1715. v4l2_ctrl_handler_init(hdl, 9);
  1716. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  1717. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  1718. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  1719. V4L2_CID_CONTRAST, 0, 255, 1, 128);
  1720. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  1721. V4L2_CID_SATURATION, 0, 255, 1, 128);
  1722. v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
  1723. V4L2_CID_HUE, 0, 128, 1, 0);
  1724. /* private controls */
  1725. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
  1726. V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
  1727. state->detect_tx_5v_ctrl->is_private = true;
  1728. state->rgb_quantization_range_ctrl =
  1729. v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
  1730. V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
  1731. 0, V4L2_DV_RGB_RANGE_AUTO);
  1732. state->rgb_quantization_range_ctrl->is_private = true;
  1733. /* custom controls */
  1734. state->analog_sampling_phase_ctrl =
  1735. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
  1736. state->analog_sampling_phase_ctrl->is_private = true;
  1737. state->free_run_color_manual_ctrl =
  1738. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
  1739. state->free_run_color_manual_ctrl->is_private = true;
  1740. state->free_run_color_ctrl =
  1741. v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color, NULL);
  1742. state->free_run_color_ctrl->is_private = true;
  1743. sd->ctrl_handler = hdl;
  1744. if (hdl->error) {
  1745. err = hdl->error;
  1746. goto err_hdl;
  1747. }
  1748. if (adv7604_s_detect_tx_5v_ctrl(sd)) {
  1749. err = -ENODEV;
  1750. goto err_hdl;
  1751. }
  1752. state->i2c_avlink = adv7604_dummy_client(sd, pdata->i2c_avlink, 0xf3);
  1753. state->i2c_cec = adv7604_dummy_client(sd, pdata->i2c_cec, 0xf4);
  1754. state->i2c_infoframe = adv7604_dummy_client(sd, pdata->i2c_infoframe, 0xf5);
  1755. state->i2c_esdp = adv7604_dummy_client(sd, pdata->i2c_esdp, 0xf6);
  1756. state->i2c_dpp = adv7604_dummy_client(sd, pdata->i2c_dpp, 0xf7);
  1757. state->i2c_afe = adv7604_dummy_client(sd, pdata->i2c_afe, 0xf8);
  1758. state->i2c_repeater = adv7604_dummy_client(sd, pdata->i2c_repeater, 0xf9);
  1759. state->i2c_edid = adv7604_dummy_client(sd, pdata->i2c_edid, 0xfa);
  1760. state->i2c_hdmi = adv7604_dummy_client(sd, pdata->i2c_hdmi, 0xfb);
  1761. state->i2c_test = adv7604_dummy_client(sd, pdata->i2c_test, 0xfc);
  1762. state->i2c_cp = adv7604_dummy_client(sd, pdata->i2c_cp, 0xfd);
  1763. state->i2c_vdp = adv7604_dummy_client(sd, pdata->i2c_vdp, 0xfe);
  1764. if (!state->i2c_avlink || !state->i2c_cec || !state->i2c_infoframe ||
  1765. !state->i2c_esdp || !state->i2c_dpp || !state->i2c_afe ||
  1766. !state->i2c_repeater || !state->i2c_edid || !state->i2c_hdmi ||
  1767. !state->i2c_test || !state->i2c_cp || !state->i2c_vdp) {
  1768. err = -ENOMEM;
  1769. v4l2_err(sd, "failed to create all i2c clients\n");
  1770. goto err_i2c;
  1771. }
  1772. state->restart_stdi_once = true;
  1773. /* work queues */
  1774. state->work_queues = create_singlethread_workqueue(client->name);
  1775. if (!state->work_queues) {
  1776. v4l2_err(sd, "Could not create work queue\n");
  1777. err = -ENOMEM;
  1778. goto err_i2c;
  1779. }
  1780. INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
  1781. adv7604_delayed_work_enable_hotplug);
  1782. state->pad.flags = MEDIA_PAD_FL_SOURCE;
  1783. err = media_entity_init(&sd->entity, 1, &state->pad, 0);
  1784. if (err)
  1785. goto err_work_queues;
  1786. err = adv7604_core_init(sd);
  1787. if (err)
  1788. goto err_entity;
  1789. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  1790. client->addr << 1, client->adapter->name);
  1791. return 0;
  1792. err_entity:
  1793. media_entity_cleanup(&sd->entity);
  1794. err_work_queues:
  1795. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  1796. destroy_workqueue(state->work_queues);
  1797. err_i2c:
  1798. adv7604_unregister_clients(state);
  1799. err_hdl:
  1800. v4l2_ctrl_handler_free(hdl);
  1801. err_state:
  1802. kfree(state);
  1803. return err;
  1804. }
  1805. /* ----------------------------------------------------------------------- */
  1806. static int adv7604_remove(struct i2c_client *client)
  1807. {
  1808. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1809. struct adv7604_state *state = to_state(sd);
  1810. cancel_delayed_work(&state->delayed_work_enable_hotplug);
  1811. destroy_workqueue(state->work_queues);
  1812. v4l2_device_unregister_subdev(sd);
  1813. media_entity_cleanup(&sd->entity);
  1814. adv7604_unregister_clients(to_state(sd));
  1815. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1816. kfree(to_state(sd));
  1817. return 0;
  1818. }
  1819. /* ----------------------------------------------------------------------- */
  1820. static struct i2c_device_id adv7604_id[] = {
  1821. { "adv7604", 0 },
  1822. { }
  1823. };
  1824. MODULE_DEVICE_TABLE(i2c, adv7604_id);
  1825. static struct i2c_driver adv7604_driver = {
  1826. .driver = {
  1827. .owner = THIS_MODULE,
  1828. .name = "adv7604",
  1829. },
  1830. .probe = adv7604_probe,
  1831. .remove = adv7604_remove,
  1832. .id_table = adv7604_id,
  1833. };
  1834. module_i2c_driver(adv7604_driver);