adv7604.c 57 KB

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