tc358743.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * tc358743 - Toshiba HDMI to CSI-2 bridge
  4. *
  5. * Copyright 2015 Cisco Systems, Inc. and/or its affiliates. All rights
  6. * reserved.
  7. */
  8. /*
  9. * References (c = chapter, p = page):
  10. * REF_01 - Toshiba, TC358743XBG (H2C), Functional Specification, Rev 0.60
  11. * REF_02 - Toshiba, TC358743XBG_HDMI-CSI_Tv11p_nm.xls
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/i2c.h>
  17. #include <linux/clk.h>
  18. #include <linux/delay.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/timer.h>
  22. #include <linux/of_graph.h>
  23. #include <linux/videodev2.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/v4l2-dv-timings.h>
  26. #include <linux/hdmi.h>
  27. #include <media/cec.h>
  28. #include <media/v4l2-dv-timings.h>
  29. #include <media/v4l2-device.h>
  30. #include <media/v4l2-ctrls.h>
  31. #include <media/v4l2-event.h>
  32. #include <media/v4l2-fwnode.h>
  33. #include <media/i2c/tc358743.h>
  34. #include "tc358743_regs.h"
  35. static int debug;
  36. module_param(debug, int, 0644);
  37. MODULE_PARM_DESC(debug, " debug level (0-3)");
  38. static int packet_type = 0x87;
  39. module_param(packet_type, int, 0644);
  40. MODULE_PARM_DESC(packet_type,
  41. " Programmable Packet Type. Possible values:\n"
  42. "\t\t 0x87: DRM InfoFrame (Default).\n"
  43. "\t\t 0x01: Audio Clock Regeneration Packet\n"
  44. "\t\t 0x02: Audio Sample Packet\n"
  45. "\t\t 0x03: General Control Packet\n"
  46. "\t\t 0x04: ACP Packet\n"
  47. "\t\t 0x07: One Bit Audio Sample Packet\n"
  48. "\t\t 0x08: DST Audio Packet\n"
  49. "\t\t 0x09: High Bitrate Audio Stream Packet\n"
  50. "\t\t 0x0a: Gamut Metadata Packet\n");
  51. MODULE_DESCRIPTION("Toshiba TC358743 HDMI to CSI-2 bridge driver");
  52. MODULE_AUTHOR("Ramakrishnan Muthukrishnan <ram@rkrishnan.org>");
  53. MODULE_AUTHOR("Mikhail Khelik <mkhelik@cisco.com>");
  54. MODULE_AUTHOR("Mats Randgaard <matrandg@cisco.com>");
  55. MODULE_LICENSE("GPL");
  56. #define EDID_NUM_BLOCKS_MAX 8
  57. #define EDID_BLOCK_SIZE 128
  58. #define I2C_MAX_XFER_SIZE (EDID_BLOCK_SIZE + 2)
  59. #define POLL_INTERVAL_CEC_MS 10
  60. #define POLL_INTERVAL_MS 1000
  61. static const struct v4l2_dv_timings_cap tc358743_timings_cap = {
  62. .type = V4L2_DV_BT_656_1120,
  63. /* keep this initialization for compatibility with GCC < 4.4.6 */
  64. .reserved = { 0 },
  65. /* Pixel clock from REF_01 p. 20. Min/max height/width are unknown */
  66. V4L2_INIT_BT_TIMINGS(640, 1920, 350, 1200, 13000000, 165000000,
  67. V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
  68. V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
  69. V4L2_DV_BT_CAP_PROGRESSIVE |
  70. V4L2_DV_BT_CAP_REDUCED_BLANKING |
  71. V4L2_DV_BT_CAP_CUSTOM)
  72. };
  73. struct tc358743_state {
  74. struct tc358743_platform_data pdata;
  75. struct v4l2_mbus_config_mipi_csi2 bus;
  76. struct v4l2_subdev sd;
  77. struct media_pad pad;
  78. struct v4l2_ctrl_handler hdl;
  79. struct i2c_client *i2c_client;
  80. /* CONFCTL is modified in ops and tc358743_hdmi_sys_int_handler */
  81. struct mutex confctl_mutex;
  82. /* controls */
  83. struct v4l2_ctrl *detect_tx_5v_ctrl;
  84. struct v4l2_ctrl *audio_sampling_rate_ctrl;
  85. struct v4l2_ctrl *audio_present_ctrl;
  86. struct delayed_work delayed_work_enable_hotplug;
  87. struct timer_list timer;
  88. struct work_struct work_i2c_poll;
  89. /* debugfs */
  90. struct dentry *debugfs_dir;
  91. struct v4l2_debugfs_if *infoframes;
  92. /* edid */
  93. u8 edid_blocks_written;
  94. struct v4l2_dv_timings timings;
  95. u32 mbus_fmt_code;
  96. u8 csi_lanes_in_use;
  97. struct gpio_desc *reset_gpio;
  98. struct cec_adapter *cec_adap;
  99. };
  100. static void tc358743_enable_interrupts(struct v4l2_subdev *sd,
  101. bool cable_connected);
  102. static int tc358743_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd);
  103. static inline struct tc358743_state *to_state(struct v4l2_subdev *sd)
  104. {
  105. return container_of(sd, struct tc358743_state, sd);
  106. }
  107. /* --------------- I2C --------------- */
  108. static int i2c_rd(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
  109. {
  110. struct tc358743_state *state = to_state(sd);
  111. struct i2c_client *client = state->i2c_client;
  112. int err;
  113. u8 buf[2] = { reg >> 8, reg & 0xff };
  114. struct i2c_msg msgs[] = {
  115. {
  116. .addr = client->addr,
  117. .flags = 0,
  118. .len = 2,
  119. .buf = buf,
  120. },
  121. {
  122. .addr = client->addr,
  123. .flags = I2C_M_RD,
  124. .len = n,
  125. .buf = values,
  126. },
  127. };
  128. err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  129. if (err != ARRAY_SIZE(msgs)) {
  130. v4l2_err(sd, "%s: reading register 0x%x from 0x%x failed: %d\n",
  131. __func__, reg, client->addr, err);
  132. }
  133. return err != ARRAY_SIZE(msgs);
  134. }
  135. static void i2c_wr(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
  136. {
  137. struct tc358743_state *state = to_state(sd);
  138. struct i2c_client *client = state->i2c_client;
  139. int err, i;
  140. struct i2c_msg msg;
  141. u8 data[I2C_MAX_XFER_SIZE];
  142. if ((2 + n) > I2C_MAX_XFER_SIZE) {
  143. n = I2C_MAX_XFER_SIZE - 2;
  144. v4l2_warn(sd, "i2c wr reg=%04x: len=%d is too big!\n",
  145. reg, 2 + n);
  146. }
  147. msg.addr = client->addr;
  148. msg.buf = data;
  149. msg.len = 2 + n;
  150. msg.flags = 0;
  151. data[0] = reg >> 8;
  152. data[1] = reg & 0xff;
  153. for (i = 0; i < n; i++)
  154. data[2 + i] = values[i];
  155. err = i2c_transfer(client->adapter, &msg, 1);
  156. if (err != 1) {
  157. v4l2_err(sd, "%s: writing register 0x%x from 0x%x failed: %d\n",
  158. __func__, reg, client->addr, err);
  159. return;
  160. }
  161. if (debug < 3)
  162. return;
  163. switch (n) {
  164. case 1:
  165. v4l2_info(sd, "I2C write 0x%04x = 0x%02x",
  166. reg, data[2]);
  167. break;
  168. case 2:
  169. v4l2_info(sd, "I2C write 0x%04x = 0x%02x%02x",
  170. reg, data[3], data[2]);
  171. break;
  172. case 4:
  173. v4l2_info(sd, "I2C write 0x%04x = 0x%02x%02x%02x%02x",
  174. reg, data[5], data[4], data[3], data[2]);
  175. break;
  176. default:
  177. v4l2_info(sd, "I2C write %d bytes from address 0x%04x\n",
  178. n, reg);
  179. }
  180. }
  181. static noinline u32 i2c_rdreg_err(struct v4l2_subdev *sd, u16 reg, u32 n,
  182. int *err)
  183. {
  184. int error;
  185. __le32 val = 0;
  186. error = i2c_rd(sd, reg, (u8 __force *)&val, n);
  187. if (err)
  188. *err = error;
  189. return le32_to_cpu(val);
  190. }
  191. static inline u32 i2c_rdreg(struct v4l2_subdev *sd, u16 reg, u32 n)
  192. {
  193. return i2c_rdreg_err(sd, reg, n, NULL);
  194. }
  195. static noinline void i2c_wrreg(struct v4l2_subdev *sd, u16 reg, u32 val, u32 n)
  196. {
  197. __le32 raw = cpu_to_le32(val);
  198. i2c_wr(sd, reg, (u8 __force *)&raw, n);
  199. }
  200. static u8 i2c_rd8(struct v4l2_subdev *sd, u16 reg)
  201. {
  202. return i2c_rdreg(sd, reg, 1);
  203. }
  204. static void i2c_wr8(struct v4l2_subdev *sd, u16 reg, u8 val)
  205. {
  206. i2c_wrreg(sd, reg, val, 1);
  207. }
  208. static void i2c_wr8_and_or(struct v4l2_subdev *sd, u16 reg,
  209. u8 mask, u8 val)
  210. {
  211. i2c_wrreg(sd, reg, (i2c_rdreg(sd, reg, 1) & mask) | val, 1);
  212. }
  213. static u16 i2c_rd16(struct v4l2_subdev *sd, u16 reg)
  214. {
  215. return i2c_rdreg(sd, reg, 2);
  216. }
  217. static int i2c_rd16_err(struct v4l2_subdev *sd, u16 reg, u16 *value)
  218. {
  219. int err;
  220. *value = i2c_rdreg_err(sd, reg, 2, &err);
  221. return err;
  222. }
  223. static void i2c_wr16(struct v4l2_subdev *sd, u16 reg, u16 val)
  224. {
  225. i2c_wrreg(sd, reg, val, 2);
  226. }
  227. static void i2c_wr16_and_or(struct v4l2_subdev *sd, u16 reg, u16 mask, u16 val)
  228. {
  229. i2c_wrreg(sd, reg, (i2c_rdreg(sd, reg, 2) & mask) | val, 2);
  230. }
  231. static u32 i2c_rd32(struct v4l2_subdev *sd, u16 reg)
  232. {
  233. return i2c_rdreg(sd, reg, 4);
  234. }
  235. static void i2c_wr32(struct v4l2_subdev *sd, u16 reg, u32 val)
  236. {
  237. i2c_wrreg(sd, reg, val, 4);
  238. }
  239. /* --------------- STATUS --------------- */
  240. static inline bool is_hdmi(struct v4l2_subdev *sd)
  241. {
  242. return i2c_rd8(sd, SYS_STATUS) & MASK_S_HDMI;
  243. }
  244. static inline bool tx_5v_power_present(struct v4l2_subdev *sd)
  245. {
  246. return i2c_rd8(sd, SYS_STATUS) & MASK_S_DDC5V;
  247. }
  248. static inline bool no_signal(struct v4l2_subdev *sd)
  249. {
  250. return !(i2c_rd8(sd, SYS_STATUS) & MASK_S_TMDS);
  251. }
  252. static inline bool no_sync(struct v4l2_subdev *sd)
  253. {
  254. return !(i2c_rd8(sd, SYS_STATUS) & MASK_S_SYNC);
  255. }
  256. static inline bool audio_present(struct v4l2_subdev *sd)
  257. {
  258. return i2c_rd8(sd, AU_STATUS0) & MASK_S_A_SAMPLE;
  259. }
  260. static int get_audio_sampling_rate(struct v4l2_subdev *sd)
  261. {
  262. static const int code_to_rate[] = {
  263. 44100, 0, 48000, 32000, 22050, 384000, 24000, 352800,
  264. 88200, 768000, 96000, 705600, 176400, 0, 192000, 0
  265. };
  266. /* Register FS_SET is not cleared when the cable is disconnected */
  267. if (no_signal(sd))
  268. return 0;
  269. return code_to_rate[i2c_rd8(sd, FS_SET) & MASK_FS];
  270. }
  271. /* --------------- TIMINGS --------------- */
  272. static inline unsigned fps(const struct v4l2_bt_timings *t)
  273. {
  274. if (!V4L2_DV_BT_FRAME_HEIGHT(t) || !V4L2_DV_BT_FRAME_WIDTH(t))
  275. return 0;
  276. return DIV_ROUND_CLOSEST((unsigned)t->pixelclock,
  277. V4L2_DV_BT_FRAME_HEIGHT(t) * V4L2_DV_BT_FRAME_WIDTH(t));
  278. }
  279. static int tc358743_get_detected_timings(struct v4l2_subdev *sd,
  280. struct v4l2_dv_timings *timings)
  281. {
  282. struct v4l2_bt_timings *bt = &timings->bt;
  283. unsigned width, height, frame_width, frame_height, frame_interval, fps;
  284. memset(timings, 0, sizeof(struct v4l2_dv_timings));
  285. /* if HPD is low, ignore any video */
  286. if (!(i2c_rd8(sd, HPD_CTL) & MASK_HPD_OUT0))
  287. return -ENOLINK;
  288. if (no_signal(sd)) {
  289. v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
  290. return -ENOLINK;
  291. }
  292. if (no_sync(sd)) {
  293. v4l2_dbg(1, debug, sd, "%s: no sync on signal\n", __func__);
  294. return -ENOLCK;
  295. }
  296. timings->type = V4L2_DV_BT_656_1120;
  297. bt->interlaced = i2c_rd8(sd, VI_STATUS1) & MASK_S_V_INTERLACE ?
  298. V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
  299. width = ((i2c_rd8(sd, DE_WIDTH_H_HI) & 0x1f) << 8) +
  300. i2c_rd8(sd, DE_WIDTH_H_LO);
  301. height = ((i2c_rd8(sd, DE_WIDTH_V_HI) & 0x1f) << 8) +
  302. i2c_rd8(sd, DE_WIDTH_V_LO);
  303. frame_width = ((i2c_rd8(sd, H_SIZE_HI) & 0x1f) << 8) +
  304. i2c_rd8(sd, H_SIZE_LO);
  305. frame_height = (((i2c_rd8(sd, V_SIZE_HI) & 0x3f) << 8) +
  306. i2c_rd8(sd, V_SIZE_LO)) / 2;
  307. /* frame interval in milliseconds * 10
  308. * Require SYS_FREQ0 and SYS_FREQ1 are precisely set */
  309. frame_interval = ((i2c_rd8(sd, FV_CNT_HI) & 0x3) << 8) +
  310. i2c_rd8(sd, FV_CNT_LO);
  311. fps = (frame_interval > 0) ?
  312. DIV_ROUND_CLOSEST(10000, frame_interval) : 0;
  313. bt->width = width;
  314. bt->height = height;
  315. bt->vsync = frame_height - height;
  316. bt->hsync = frame_width - width;
  317. bt->pixelclock = frame_width * frame_height * fps;
  318. if (bt->interlaced == V4L2_DV_INTERLACED) {
  319. bt->height *= 2;
  320. bt->il_vsync = bt->vsync + 1;
  321. bt->pixelclock /= 2;
  322. }
  323. return 0;
  324. }
  325. /* --------------- HOTPLUG / HDCP / EDID --------------- */
  326. static void tc358743_delayed_work_enable_hotplug(struct work_struct *work)
  327. {
  328. struct delayed_work *dwork = to_delayed_work(work);
  329. struct tc358743_state *state = container_of(dwork,
  330. struct tc358743_state, delayed_work_enable_hotplug);
  331. struct v4l2_subdev *sd = &state->sd;
  332. v4l2_dbg(2, debug, sd, "%s:\n", __func__);
  333. i2c_wr8_and_or(sd, HPD_CTL, ~MASK_HPD_OUT0, MASK_HPD_OUT0);
  334. }
  335. static void tc358743_set_hdmi_hdcp(struct v4l2_subdev *sd, bool enable)
  336. {
  337. v4l2_dbg(2, debug, sd, "%s: %s\n", __func__, enable ?
  338. "enable" : "disable");
  339. if (enable) {
  340. i2c_wr8_and_or(sd, HDCP_REG3, ~KEY_RD_CMD, KEY_RD_CMD);
  341. i2c_wr8_and_or(sd, HDCP_MODE, ~MASK_MANUAL_AUTHENTICATION, 0);
  342. i2c_wr8_and_or(sd, HDCP_REG1, 0xff,
  343. MASK_AUTH_UNAUTH_SEL_16_FRAMES |
  344. MASK_AUTH_UNAUTH_AUTO);
  345. i2c_wr8_and_or(sd, HDCP_REG2, ~MASK_AUTO_P3_RESET,
  346. SET_AUTO_P3_RESET_FRAMES(0x0f));
  347. } else {
  348. i2c_wr8_and_or(sd, HDCP_MODE, ~MASK_MANUAL_AUTHENTICATION,
  349. MASK_MANUAL_AUTHENTICATION);
  350. }
  351. }
  352. static void tc358743_disable_edid(struct v4l2_subdev *sd)
  353. {
  354. struct tc358743_state *state = to_state(sd);
  355. v4l2_dbg(2, debug, sd, "%s:\n", __func__);
  356. cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
  357. /* DDC access to EDID is also disabled when hotplug is disabled. See
  358. * register DDC_CTL */
  359. i2c_wr8_and_or(sd, HPD_CTL, ~MASK_HPD_OUT0, 0x0);
  360. }
  361. static void tc358743_enable_edid(struct v4l2_subdev *sd)
  362. {
  363. struct tc358743_state *state = to_state(sd);
  364. if (state->edid_blocks_written == 0) {
  365. v4l2_dbg(2, debug, sd, "%s: no EDID -> no hotplug\n", __func__);
  366. tc358743_s_ctrl_detect_tx_5v(sd);
  367. return;
  368. }
  369. v4l2_dbg(2, debug, sd, "%s:\n", __func__);
  370. /* Enable hotplug after 143 ms. DDC access to EDID is also enabled when
  371. * hotplug is enabled. See register DDC_CTL */
  372. schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 7);
  373. tc358743_enable_interrupts(sd, true);
  374. tc358743_s_ctrl_detect_tx_5v(sd);
  375. }
  376. static void tc358743_erase_bksv(struct v4l2_subdev *sd)
  377. {
  378. int i;
  379. for (i = 0; i < 5; i++)
  380. i2c_wr8(sd, BKSV + i, 0);
  381. }
  382. /* --------------- AVI infoframe --------------- */
  383. static ssize_t
  384. tc358743_debugfs_if_read(u32 type, void *priv, struct file *filp,
  385. char __user *ubuf, size_t count, loff_t *ppos)
  386. {
  387. u8 buf[V4L2_DEBUGFS_IF_MAX_LEN] = {};
  388. struct v4l2_subdev *sd = priv;
  389. int len;
  390. if (!is_hdmi(sd))
  391. return 0;
  392. switch (type) {
  393. case V4L2_DEBUGFS_IF_AVI:
  394. i2c_rd(sd, PK_AVI_0HEAD, buf, PK_AVI_LEN);
  395. break;
  396. case V4L2_DEBUGFS_IF_AUDIO:
  397. i2c_rd(sd, PK_AUD_0HEAD, buf, PK_AUD_LEN);
  398. break;
  399. case V4L2_DEBUGFS_IF_SPD:
  400. i2c_rd(sd, PK_SPD_0HEAD, buf, PK_SPD_LEN);
  401. break;
  402. case V4L2_DEBUGFS_IF_HDMI:
  403. i2c_rd(sd, PK_VS_0HEAD, buf, PK_VS_LEN);
  404. break;
  405. case V4L2_DEBUGFS_IF_DRM:
  406. i2c_rd(sd, PK_ACP_0HEAD, buf, PK_ACP_LEN);
  407. break;
  408. default:
  409. return 0;
  410. }
  411. if (!buf[2])
  412. return -ENOENT;
  413. len = buf[2] + 4;
  414. if (len > V4L2_DEBUGFS_IF_MAX_LEN)
  415. len = -ENOENT;
  416. if (len > 0)
  417. len = simple_read_from_buffer(ubuf, count, ppos, buf, len);
  418. return len < 0 ? 0 : len;
  419. }
  420. static void print_infoframes(struct v4l2_subdev *sd)
  421. {
  422. struct i2c_client *client = v4l2_get_subdevdata(sd);
  423. struct device *dev = &client->dev;
  424. union hdmi_infoframe frame;
  425. u8 buffer[V4L2_DEBUGFS_IF_MAX_LEN] = {};
  426. /*
  427. * Updating the ACP TYPE here allows for dynamically
  428. * changing the type you want to monitor, without having
  429. * to reload the driver with a new packet_type module option value.
  430. *
  431. * Instead you can set it with the new value, then call
  432. * VIDIOC_LOG_STATUS.
  433. */
  434. i2c_wr8(sd, TYP_ACP_SET, packet_type);
  435. if (!is_hdmi(sd)) {
  436. v4l2_info(sd, "DVI-D signal - InfoFrames not supported\n");
  437. return;
  438. }
  439. i2c_rd(sd, PK_AVI_0HEAD, buffer, PK_AVI_LEN);
  440. if (hdmi_infoframe_unpack(&frame, buffer, sizeof(buffer)) >= 0)
  441. hdmi_infoframe_log(KERN_INFO, dev, &frame);
  442. i2c_rd(sd, PK_VS_0HEAD, buffer, PK_VS_LEN);
  443. if (hdmi_infoframe_unpack(&frame, buffer, sizeof(buffer)) >= 0)
  444. hdmi_infoframe_log(KERN_INFO, dev, &frame);
  445. i2c_rd(sd, PK_AUD_0HEAD, buffer, PK_AUD_LEN);
  446. if (hdmi_infoframe_unpack(&frame, buffer, sizeof(buffer)) >= 0)
  447. hdmi_infoframe_log(KERN_INFO, dev, &frame);
  448. i2c_rd(sd, PK_SPD_0HEAD, buffer, PK_SPD_LEN);
  449. if (hdmi_infoframe_unpack(&frame, buffer, sizeof(buffer)) >= 0)
  450. hdmi_infoframe_log(KERN_INFO, dev, &frame);
  451. i2c_rd(sd, PK_ACP_0HEAD, buffer, PK_ACP_LEN);
  452. if (buffer[0] == packet_type) {
  453. if (packet_type < 0x80)
  454. v4l2_info(sd, "Packet: %*ph\n", PK_ACP_LEN, buffer);
  455. else if (packet_type != 0x87)
  456. v4l2_info(sd, "InfoFrame: %*ph\n", PK_ACP_LEN, buffer);
  457. else if (hdmi_infoframe_unpack(&frame, buffer,
  458. sizeof(buffer)) >= 0)
  459. hdmi_infoframe_log(KERN_INFO, dev, &frame);
  460. }
  461. i2c_rd(sd, PK_MS_0HEAD, buffer, PK_MS_LEN);
  462. if (buffer[2] && buffer[2] + 3 <= PK_MS_LEN)
  463. v4l2_info(sd, "MPEG Source InfoFrame: %*ph\n",
  464. buffer[2] + 3, buffer);
  465. i2c_rd(sd, PK_ISRC1_0HEAD, buffer, PK_ISRC1_LEN);
  466. if (buffer[0] == 0x05)
  467. v4l2_info(sd, "ISRC1 Packet: %*ph\n",
  468. PK_ISRC1_LEN, buffer);
  469. i2c_rd(sd, PK_ISRC2_0HEAD, buffer, PK_ISRC2_LEN);
  470. if (buffer[0] == 0x06)
  471. v4l2_info(sd, "ISRC2 Packet: %*ph\n",
  472. PK_ISRC2_LEN, buffer);
  473. }
  474. /* --------------- CTRLS --------------- */
  475. static int tc358743_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd)
  476. {
  477. struct tc358743_state *state = to_state(sd);
  478. return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
  479. tx_5v_power_present(sd));
  480. }
  481. static int tc358743_s_ctrl_audio_sampling_rate(struct v4l2_subdev *sd)
  482. {
  483. struct tc358743_state *state = to_state(sd);
  484. return v4l2_ctrl_s_ctrl(state->audio_sampling_rate_ctrl,
  485. get_audio_sampling_rate(sd));
  486. }
  487. static int tc358743_s_ctrl_audio_present(struct v4l2_subdev *sd)
  488. {
  489. struct tc358743_state *state = to_state(sd);
  490. return v4l2_ctrl_s_ctrl(state->audio_present_ctrl,
  491. audio_present(sd));
  492. }
  493. static int tc358743_update_controls(struct v4l2_subdev *sd)
  494. {
  495. int ret = 0;
  496. ret |= tc358743_s_ctrl_detect_tx_5v(sd);
  497. ret |= tc358743_s_ctrl_audio_sampling_rate(sd);
  498. ret |= tc358743_s_ctrl_audio_present(sd);
  499. return ret;
  500. }
  501. /* --------------- INIT --------------- */
  502. static void tc358743_reset_phy(struct v4l2_subdev *sd)
  503. {
  504. v4l2_dbg(1, debug, sd, "%s:\n", __func__);
  505. i2c_wr8_and_or(sd, PHY_RST, ~MASK_RESET_CTRL, 0);
  506. i2c_wr8_and_or(sd, PHY_RST, ~MASK_RESET_CTRL, MASK_RESET_CTRL);
  507. }
  508. static void tc358743_reset(struct v4l2_subdev *sd, uint16_t mask)
  509. {
  510. u16 sysctl = i2c_rd16(sd, SYSCTL);
  511. i2c_wr16(sd, SYSCTL, sysctl | mask);
  512. i2c_wr16(sd, SYSCTL, sysctl & ~mask);
  513. }
  514. static inline void tc358743_sleep_mode(struct v4l2_subdev *sd, bool enable)
  515. {
  516. i2c_wr16_and_or(sd, SYSCTL, ~MASK_SLEEP,
  517. enable ? MASK_SLEEP : 0);
  518. }
  519. static inline void enable_stream(struct v4l2_subdev *sd, bool enable)
  520. {
  521. struct tc358743_state *state = to_state(sd);
  522. v4l2_dbg(3, debug, sd, "%s: %sable\n",
  523. __func__, enable ? "en" : "dis");
  524. if (enable) {
  525. /* It is critical for CSI receiver to see lane transition
  526. * LP11->HS. Set to non-continuous mode to enable clock lane
  527. * LP11 state. */
  528. i2c_wr32(sd, TXOPTIONCNTRL, 0);
  529. /* Set to continuous mode to trigger LP11->HS transition */
  530. i2c_wr32(sd, TXOPTIONCNTRL, MASK_CONTCLKMODE);
  531. /* Unmute video */
  532. i2c_wr8(sd, VI_MUTE, MASK_AUTO_MUTE);
  533. } else {
  534. /* Mute video so that all data lanes go to LSP11 state.
  535. * No data is output to CSI Tx block. */
  536. i2c_wr8(sd, VI_MUTE, MASK_AUTO_MUTE | MASK_VI_MUTE);
  537. }
  538. mutex_lock(&state->confctl_mutex);
  539. i2c_wr16_and_or(sd, CONFCTL, ~(MASK_VBUFEN | MASK_ABUFEN),
  540. enable ? (MASK_VBUFEN | MASK_ABUFEN) : 0x0);
  541. mutex_unlock(&state->confctl_mutex);
  542. }
  543. static void tc358743_set_pll(struct v4l2_subdev *sd)
  544. {
  545. struct tc358743_state *state = to_state(sd);
  546. struct tc358743_platform_data *pdata = &state->pdata;
  547. u16 pllctl0 = i2c_rd16(sd, PLLCTL0);
  548. u16 pllctl1 = i2c_rd16(sd, PLLCTL1);
  549. u16 pllctl0_new = SET_PLL_PRD(pdata->pll_prd) |
  550. SET_PLL_FBD(pdata->pll_fbd);
  551. u32 hsck = (pdata->refclk_hz / pdata->pll_prd) * pdata->pll_fbd;
  552. v4l2_dbg(2, debug, sd, "%s:\n", __func__);
  553. /* Only rewrite when needed (new value or disabled), since rewriting
  554. * triggers another format change event. */
  555. if ((pllctl0 != pllctl0_new) || ((pllctl1 & MASK_PLL_EN) == 0)) {
  556. u16 pll_frs;
  557. if (hsck > 500000000)
  558. pll_frs = 0x0;
  559. else if (hsck > 250000000)
  560. pll_frs = 0x1;
  561. else if (hsck > 125000000)
  562. pll_frs = 0x2;
  563. else
  564. pll_frs = 0x3;
  565. v4l2_dbg(1, debug, sd, "%s: updating PLL clock\n", __func__);
  566. tc358743_sleep_mode(sd, true);
  567. i2c_wr16(sd, PLLCTL0, pllctl0_new);
  568. i2c_wr16_and_or(sd, PLLCTL1,
  569. ~(MASK_PLL_FRS | MASK_RESETB | MASK_PLL_EN),
  570. (SET_PLL_FRS(pll_frs) | MASK_RESETB |
  571. MASK_PLL_EN));
  572. udelay(10); /* REF_02, Sheet "Source HDMI" */
  573. i2c_wr16_and_or(sd, PLLCTL1, ~MASK_CKEN, MASK_CKEN);
  574. tc358743_sleep_mode(sd, false);
  575. }
  576. }
  577. static void tc358743_set_ref_clk(struct v4l2_subdev *sd)
  578. {
  579. struct tc358743_state *state = to_state(sd);
  580. struct tc358743_platform_data *pdata = &state->pdata;
  581. u32 sys_freq;
  582. u32 lockdet_ref;
  583. u32 cec_freq;
  584. u16 fh_min;
  585. u16 fh_max;
  586. BUG_ON(!(pdata->refclk_hz == 26000000 ||
  587. pdata->refclk_hz == 27000000 ||
  588. pdata->refclk_hz == 42000000));
  589. sys_freq = pdata->refclk_hz / 10000;
  590. i2c_wr8(sd, SYS_FREQ0, sys_freq & 0x00ff);
  591. i2c_wr8(sd, SYS_FREQ1, (sys_freq & 0xff00) >> 8);
  592. i2c_wr8_and_or(sd, PHY_CTL0, ~MASK_PHY_SYSCLK_IND,
  593. (pdata->refclk_hz == 42000000) ?
  594. MASK_PHY_SYSCLK_IND : 0x0);
  595. fh_min = pdata->refclk_hz / 100000;
  596. i2c_wr8(sd, FH_MIN0, fh_min & 0x00ff);
  597. i2c_wr8(sd, FH_MIN1, (fh_min & 0xff00) >> 8);
  598. fh_max = (fh_min * 66) / 10;
  599. i2c_wr8(sd, FH_MAX0, fh_max & 0x00ff);
  600. i2c_wr8(sd, FH_MAX1, (fh_max & 0xff00) >> 8);
  601. lockdet_ref = pdata->refclk_hz / 100;
  602. i2c_wr8(sd, LOCKDET_REF0, lockdet_ref & 0x0000ff);
  603. i2c_wr8(sd, LOCKDET_REF1, (lockdet_ref & 0x00ff00) >> 8);
  604. i2c_wr8(sd, LOCKDET_REF2, (lockdet_ref & 0x0f0000) >> 16);
  605. i2c_wr8_and_or(sd, NCO_F0_MOD, ~MASK_NCO_F0_MOD,
  606. (pdata->refclk_hz == 27000000) ?
  607. MASK_NCO_F0_MOD_27MHZ : 0x0);
  608. /*
  609. * Trial and error suggests that the default register value
  610. * of 656 is for a 42 MHz reference clock. Use that to derive
  611. * a new value based on the actual reference clock.
  612. */
  613. cec_freq = (656 * sys_freq) / 4200;
  614. i2c_wr16(sd, CECHCLK, cec_freq);
  615. i2c_wr16(sd, CECLCLK, cec_freq);
  616. }
  617. static void tc358743_set_csi_color_space(struct v4l2_subdev *sd)
  618. {
  619. struct tc358743_state *state = to_state(sd);
  620. switch (state->mbus_fmt_code) {
  621. case MEDIA_BUS_FMT_UYVY8_1X16:
  622. v4l2_dbg(2, debug, sd, "%s: YCbCr 422 16-bit\n", __func__);
  623. i2c_wr8_and_or(sd, VOUT_SET2,
  624. ~(MASK_SEL422 | MASK_VOUT_422FIL_100) & 0xff,
  625. MASK_SEL422 | MASK_VOUT_422FIL_100);
  626. i2c_wr8_and_or(sd, VI_REP, ~MASK_VOUT_COLOR_SEL & 0xff,
  627. MASK_VOUT_COLOR_601_YCBCR_LIMITED);
  628. mutex_lock(&state->confctl_mutex);
  629. i2c_wr16_and_or(sd, CONFCTL, ~MASK_YCBCRFMT,
  630. MASK_YCBCRFMT_422_8_BIT);
  631. mutex_unlock(&state->confctl_mutex);
  632. break;
  633. case MEDIA_BUS_FMT_RGB888_1X24:
  634. v4l2_dbg(2, debug, sd, "%s: RGB 888 24-bit\n", __func__);
  635. i2c_wr8_and_or(sd, VOUT_SET2,
  636. ~(MASK_SEL422 | MASK_VOUT_422FIL_100) & 0xff,
  637. 0x00);
  638. i2c_wr8_and_or(sd, VI_REP, ~MASK_VOUT_COLOR_SEL & 0xff,
  639. MASK_VOUT_COLOR_RGB_FULL);
  640. mutex_lock(&state->confctl_mutex);
  641. i2c_wr16_and_or(sd, CONFCTL, ~MASK_YCBCRFMT, 0);
  642. mutex_unlock(&state->confctl_mutex);
  643. break;
  644. default:
  645. v4l2_dbg(2, debug, sd, "%s: Unsupported format code 0x%x\n",
  646. __func__, state->mbus_fmt_code);
  647. }
  648. }
  649. static unsigned tc358743_num_csi_lanes_needed(struct v4l2_subdev *sd)
  650. {
  651. struct tc358743_state *state = to_state(sd);
  652. struct v4l2_bt_timings *bt = &state->timings.bt;
  653. struct tc358743_platform_data *pdata = &state->pdata;
  654. u32 bits_pr_pixel =
  655. (state->mbus_fmt_code == MEDIA_BUS_FMT_UYVY8_1X16) ? 16 : 24;
  656. u32 bps = bt->width * bt->height * fps(bt) * bits_pr_pixel;
  657. u32 bps_pr_lane = (pdata->refclk_hz / pdata->pll_prd) * pdata->pll_fbd;
  658. return DIV_ROUND_UP(bps, bps_pr_lane);
  659. }
  660. static void tc358743_set_csi(struct v4l2_subdev *sd)
  661. {
  662. struct tc358743_state *state = to_state(sd);
  663. struct tc358743_platform_data *pdata = &state->pdata;
  664. unsigned lanes = tc358743_num_csi_lanes_needed(sd);
  665. v4l2_dbg(3, debug, sd, "%s:\n", __func__);
  666. state->csi_lanes_in_use = lanes;
  667. tc358743_reset(sd, MASK_CTXRST);
  668. if (lanes < 1)
  669. i2c_wr32(sd, CLW_CNTRL, MASK_CLW_LANEDISABLE);
  670. if (lanes < 1)
  671. i2c_wr32(sd, D0W_CNTRL, MASK_D0W_LANEDISABLE);
  672. if (lanes < 2)
  673. i2c_wr32(sd, D1W_CNTRL, MASK_D1W_LANEDISABLE);
  674. if (lanes < 3)
  675. i2c_wr32(sd, D2W_CNTRL, MASK_D2W_LANEDISABLE);
  676. if (lanes < 4)
  677. i2c_wr32(sd, D3W_CNTRL, MASK_D3W_LANEDISABLE);
  678. i2c_wr32(sd, LINEINITCNT, pdata->lineinitcnt);
  679. i2c_wr32(sd, LPTXTIMECNT, pdata->lptxtimecnt);
  680. i2c_wr32(sd, TCLK_HEADERCNT, pdata->tclk_headercnt);
  681. i2c_wr32(sd, TCLK_TRAILCNT, pdata->tclk_trailcnt);
  682. i2c_wr32(sd, THS_HEADERCNT, pdata->ths_headercnt);
  683. i2c_wr32(sd, TWAKEUP, pdata->twakeup);
  684. i2c_wr32(sd, TCLK_POSTCNT, pdata->tclk_postcnt);
  685. i2c_wr32(sd, THS_TRAILCNT, pdata->ths_trailcnt);
  686. i2c_wr32(sd, HSTXVREGCNT, pdata->hstxvregcnt);
  687. i2c_wr32(sd, HSTXVREGEN,
  688. ((lanes > 0) ? MASK_CLM_HSTXVREGEN : 0x0) |
  689. ((lanes > 0) ? MASK_D0M_HSTXVREGEN : 0x0) |
  690. ((lanes > 1) ? MASK_D1M_HSTXVREGEN : 0x0) |
  691. ((lanes > 2) ? MASK_D2M_HSTXVREGEN : 0x0) |
  692. ((lanes > 3) ? MASK_D3M_HSTXVREGEN : 0x0));
  693. i2c_wr32(sd, TXOPTIONCNTRL, (state->bus.flags &
  694. V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK) ? 0 : MASK_CONTCLKMODE);
  695. i2c_wr32(sd, STARTCNTRL, MASK_START);
  696. i2c_wr32(sd, CSI_START, MASK_STRT);
  697. i2c_wr32(sd, CSI_CONFW, MASK_MODE_SET |
  698. MASK_ADDRESS_CSI_CONTROL |
  699. MASK_CSI_MODE |
  700. MASK_TXHSMD |
  701. ((lanes == 4) ? MASK_NOL_4 :
  702. (lanes == 3) ? MASK_NOL_3 :
  703. (lanes == 2) ? MASK_NOL_2 : MASK_NOL_1));
  704. i2c_wr32(sd, CSI_CONFW, MASK_MODE_SET |
  705. MASK_ADDRESS_CSI_ERR_INTENA | MASK_TXBRK | MASK_QUNK |
  706. MASK_WCER | MASK_INER);
  707. i2c_wr32(sd, CSI_CONFW, MASK_MODE_CLEAR |
  708. MASK_ADDRESS_CSI_ERR_HALT | MASK_TXBRK | MASK_QUNK);
  709. i2c_wr32(sd, CSI_CONFW, MASK_MODE_SET |
  710. MASK_ADDRESS_CSI_INT_ENA | MASK_INTER);
  711. }
  712. static void tc358743_set_hdmi_phy(struct v4l2_subdev *sd)
  713. {
  714. struct tc358743_state *state = to_state(sd);
  715. struct tc358743_platform_data *pdata = &state->pdata;
  716. /* Default settings from REF_02, sheet "Source HDMI"
  717. * and custom settings as platform data */
  718. i2c_wr8_and_or(sd, PHY_EN, ~MASK_ENABLE_PHY, 0x0);
  719. i2c_wr8(sd, PHY_CTL1, SET_PHY_AUTO_RST1_US(1600) |
  720. SET_FREQ_RANGE_MODE_CYCLES(1));
  721. i2c_wr8_and_or(sd, PHY_CTL2, ~MASK_PHY_AUTO_RSTn,
  722. (pdata->hdmi_phy_auto_reset_tmds_detected ?
  723. MASK_PHY_AUTO_RST2 : 0) |
  724. (pdata->hdmi_phy_auto_reset_tmds_in_range ?
  725. MASK_PHY_AUTO_RST3 : 0) |
  726. (pdata->hdmi_phy_auto_reset_tmds_valid ?
  727. MASK_PHY_AUTO_RST4 : 0));
  728. i2c_wr8(sd, PHY_BIAS, 0x40);
  729. i2c_wr8(sd, PHY_CSQ, SET_CSQ_CNT_LEVEL(0x0a));
  730. i2c_wr8(sd, AVM_CTL, 45);
  731. i2c_wr8_and_or(sd, HDMI_DET, ~MASK_HDMI_DET_V,
  732. pdata->hdmi_detection_delay << 4);
  733. i2c_wr8_and_or(sd, HV_RST, ~(MASK_H_PI_RST | MASK_V_PI_RST),
  734. (pdata->hdmi_phy_auto_reset_hsync_out_of_range ?
  735. MASK_H_PI_RST : 0) |
  736. (pdata->hdmi_phy_auto_reset_vsync_out_of_range ?
  737. MASK_V_PI_RST : 0));
  738. i2c_wr8_and_or(sd, PHY_EN, ~MASK_ENABLE_PHY, MASK_ENABLE_PHY);
  739. }
  740. static void tc358743_set_hdmi_audio(struct v4l2_subdev *sd)
  741. {
  742. struct tc358743_state *state = to_state(sd);
  743. /* Default settings from REF_02, sheet "Source HDMI" */
  744. i2c_wr8(sd, FORCE_MUTE, 0x00);
  745. i2c_wr8(sd, AUTO_CMD0, MASK_AUTO_MUTE7 | MASK_AUTO_MUTE6 |
  746. MASK_AUTO_MUTE5 | MASK_AUTO_MUTE4 |
  747. MASK_AUTO_MUTE1 | MASK_AUTO_MUTE0);
  748. i2c_wr8(sd, AUTO_CMD1, MASK_AUTO_MUTE9);
  749. i2c_wr8(sd, AUTO_CMD2, MASK_AUTO_PLAY3 | MASK_AUTO_PLAY2);
  750. i2c_wr8(sd, BUFINIT_START, SET_BUFINIT_START_MS(500));
  751. i2c_wr8(sd, FS_MUTE, 0x00);
  752. i2c_wr8(sd, FS_IMODE, MASK_NLPCM_SMODE | MASK_FS_SMODE);
  753. i2c_wr8(sd, ACR_MODE, MASK_CTS_MODE);
  754. i2c_wr8(sd, ACR_MDF0, MASK_ACR_L2MDF_1976_PPM | MASK_ACR_L1MDF_976_PPM);
  755. i2c_wr8(sd, ACR_MDF1, MASK_ACR_L3MDF_3906_PPM);
  756. i2c_wr8(sd, SDO_MODE1, MASK_SDO_FMT_I2S);
  757. i2c_wr8(sd, DIV_MODE, SET_DIV_DLY_MS(100));
  758. mutex_lock(&state->confctl_mutex);
  759. i2c_wr16_and_or(sd, CONFCTL, 0xffff, MASK_AUDCHNUM_2 |
  760. MASK_AUDOUTSEL_I2S | MASK_AUTOINDEX);
  761. mutex_unlock(&state->confctl_mutex);
  762. }
  763. static void tc358743_set_hdmi_info_frame_mode(struct v4l2_subdev *sd)
  764. {
  765. /* Default settings from REF_02, sheet "Source HDMI" */
  766. i2c_wr8(sd, PK_INT_MODE, MASK_ISRC2_INT_MODE | MASK_ISRC_INT_MODE |
  767. MASK_ACP_INT_MODE | MASK_VS_INT_MODE |
  768. MASK_SPD_INT_MODE | MASK_MS_INT_MODE |
  769. MASK_AUD_INT_MODE | MASK_AVI_INT_MODE);
  770. i2c_wr8(sd, NO_PKT_LIMIT, 0x2c);
  771. i2c_wr8(sd, NO_PKT_CLR, 0x53);
  772. i2c_wr8(sd, ERR_PK_LIMIT, 0x01);
  773. i2c_wr8(sd, NO_PKT_LIMIT2, 0x30);
  774. i2c_wr8(sd, NO_GDB_LIMIT, 0x10);
  775. }
  776. static void tc358743_initial_setup(struct v4l2_subdev *sd)
  777. {
  778. struct tc358743_state *state = to_state(sd);
  779. struct tc358743_platform_data *pdata = &state->pdata;
  780. /*
  781. * IR is not supported by this driver.
  782. * CEC is only enabled if needed.
  783. */
  784. i2c_wr16_and_or(sd, SYSCTL, ~(MASK_IRRST | MASK_CECRST),
  785. (MASK_IRRST | MASK_CECRST));
  786. tc358743_reset(sd, MASK_CTXRST | MASK_HDMIRST);
  787. #ifdef CONFIG_VIDEO_TC358743_CEC
  788. tc358743_reset(sd, MASK_CECRST);
  789. #endif
  790. tc358743_sleep_mode(sd, false);
  791. i2c_wr16(sd, FIFOCTL, pdata->fifo_level);
  792. tc358743_set_ref_clk(sd);
  793. i2c_wr8_and_or(sd, DDC_CTL, ~MASK_DDC5V_MODE,
  794. pdata->ddc5v_delay & MASK_DDC5V_MODE);
  795. i2c_wr8_and_or(sd, EDID_MODE, ~MASK_EDID_MODE, MASK_EDID_MODE_E_DDC);
  796. tc358743_set_hdmi_phy(sd);
  797. tc358743_set_hdmi_hdcp(sd, pdata->enable_hdcp);
  798. tc358743_set_hdmi_audio(sd);
  799. tc358743_set_hdmi_info_frame_mode(sd);
  800. /* All CE and IT formats are detected as RGB full range in DVI mode */
  801. i2c_wr8_and_or(sd, VI_MODE, ~MASK_RGB_DVI, 0);
  802. i2c_wr8_and_or(sd, VOUT_SET2, ~MASK_VOUTCOLORMODE,
  803. MASK_VOUTCOLORMODE_AUTO);
  804. i2c_wr8(sd, VOUT_SET3, MASK_VOUT_EXTCNT);
  805. }
  806. /* --------------- CEC --------------- */
  807. #ifdef CONFIG_VIDEO_TC358743_CEC
  808. static int tc358743_cec_adap_enable(struct cec_adapter *adap, bool enable)
  809. {
  810. struct tc358743_state *state = adap->priv;
  811. struct v4l2_subdev *sd = &state->sd;
  812. i2c_wr32(sd, CECIMSK, enable ? MASK_CECTIM | MASK_CECRIM : 0);
  813. i2c_wr32(sd, CECICLR, MASK_CECTICLR | MASK_CECRICLR);
  814. i2c_wr32(sd, CECEN, enable);
  815. if (enable)
  816. i2c_wr32(sd, CECREN, MASK_CECREN);
  817. return 0;
  818. }
  819. static int tc358743_cec_adap_monitor_all_enable(struct cec_adapter *adap,
  820. bool enable)
  821. {
  822. struct tc358743_state *state = adap->priv;
  823. struct v4l2_subdev *sd = &state->sd;
  824. u32 reg;
  825. reg = i2c_rd32(sd, CECRCTL1);
  826. if (enable)
  827. reg |= MASK_CECOTH;
  828. else
  829. reg &= ~MASK_CECOTH;
  830. i2c_wr32(sd, CECRCTL1, reg);
  831. return 0;
  832. }
  833. static int tc358743_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
  834. {
  835. struct tc358743_state *state = adap->priv;
  836. struct v4l2_subdev *sd = &state->sd;
  837. unsigned int la = 0;
  838. if (log_addr != CEC_LOG_ADDR_INVALID) {
  839. la = i2c_rd32(sd, CECADD);
  840. la |= 1 << log_addr;
  841. }
  842. i2c_wr32(sd, CECADD, la);
  843. return 0;
  844. }
  845. static int tc358743_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  846. u32 signal_free_time, struct cec_msg *msg)
  847. {
  848. struct tc358743_state *state = adap->priv;
  849. struct v4l2_subdev *sd = &state->sd;
  850. unsigned int i;
  851. i2c_wr32(sd, CECTCTL,
  852. (cec_msg_is_broadcast(msg) ? MASK_CECBRD : 0) |
  853. (signal_free_time - 1));
  854. for (i = 0; i < msg->len; i++)
  855. i2c_wr32(sd, CECTBUF1 + i * 4,
  856. msg->msg[i] | ((i == msg->len - 1) ? MASK_CECTEOM : 0));
  857. i2c_wr32(sd, CECTEN, MASK_CECTEN);
  858. return 0;
  859. }
  860. static const struct cec_adap_ops tc358743_cec_adap_ops = {
  861. .adap_enable = tc358743_cec_adap_enable,
  862. .adap_log_addr = tc358743_cec_adap_log_addr,
  863. .adap_transmit = tc358743_cec_adap_transmit,
  864. .adap_monitor_all_enable = tc358743_cec_adap_monitor_all_enable,
  865. };
  866. static void tc358743_cec_handler(struct v4l2_subdev *sd, u16 intstatus,
  867. bool *handled)
  868. {
  869. struct tc358743_state *state = to_state(sd);
  870. unsigned int cec_rxint, cec_txint;
  871. unsigned int clr = 0;
  872. cec_rxint = i2c_rd32(sd, CECRSTAT);
  873. cec_txint = i2c_rd32(sd, CECTSTAT);
  874. if (intstatus & MASK_CEC_RINT)
  875. clr |= MASK_CECRICLR;
  876. if (intstatus & MASK_CEC_TINT)
  877. clr |= MASK_CECTICLR;
  878. i2c_wr32(sd, CECICLR, clr);
  879. if ((intstatus & MASK_CEC_TINT) && cec_txint) {
  880. if (cec_txint & MASK_CECTIEND)
  881. cec_transmit_attempt_done(state->cec_adap,
  882. CEC_TX_STATUS_OK);
  883. else if (cec_txint & MASK_CECTIAL)
  884. cec_transmit_attempt_done(state->cec_adap,
  885. CEC_TX_STATUS_ARB_LOST);
  886. else if (cec_txint & MASK_CECTIACK)
  887. cec_transmit_attempt_done(state->cec_adap,
  888. CEC_TX_STATUS_NACK);
  889. else if (cec_txint & MASK_CECTIUR) {
  890. /*
  891. * Not sure when this bit is set. Treat
  892. * it as an error for now.
  893. */
  894. cec_transmit_attempt_done(state->cec_adap,
  895. CEC_TX_STATUS_ERROR);
  896. }
  897. if (handled)
  898. *handled = true;
  899. }
  900. if ((intstatus & MASK_CEC_RINT) &&
  901. (cec_rxint & MASK_CECRIEND)) {
  902. struct cec_msg msg = {};
  903. unsigned int i;
  904. unsigned int v;
  905. v = i2c_rd32(sd, CECRCTR);
  906. msg.len = v & 0x1f;
  907. if (msg.len > CEC_MAX_MSG_SIZE)
  908. msg.len = CEC_MAX_MSG_SIZE;
  909. for (i = 0; i < msg.len; i++) {
  910. v = i2c_rd32(sd, CECRBUF1 + i * 4);
  911. msg.msg[i] = v & 0xff;
  912. }
  913. cec_received_msg(state->cec_adap, &msg);
  914. if (handled)
  915. *handled = true;
  916. }
  917. i2c_wr16(sd, INTSTATUS,
  918. intstatus & (MASK_CEC_RINT | MASK_CEC_TINT));
  919. }
  920. #endif
  921. /* --------------- IRQ --------------- */
  922. static void tc358743_format_change(struct v4l2_subdev *sd)
  923. {
  924. struct tc358743_state *state = to_state(sd);
  925. struct v4l2_dv_timings timings;
  926. const struct v4l2_event tc358743_ev_fmt = {
  927. .type = V4L2_EVENT_SOURCE_CHANGE,
  928. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  929. };
  930. if (tc358743_get_detected_timings(sd, &timings)) {
  931. enable_stream(sd, false);
  932. v4l2_dbg(1, debug, sd, "%s: No signal\n",
  933. __func__);
  934. } else {
  935. if (!v4l2_match_dv_timings(&state->timings, &timings, 0, false))
  936. enable_stream(sd, false);
  937. if (debug)
  938. v4l2_print_dv_timings(sd->name,
  939. "tc358743_format_change: New format: ",
  940. &timings, false);
  941. }
  942. if (sd->devnode)
  943. v4l2_subdev_notify_event(sd, &tc358743_ev_fmt);
  944. }
  945. static void tc358743_init_interrupts(struct v4l2_subdev *sd)
  946. {
  947. u16 i;
  948. /* clear interrupt status registers */
  949. for (i = SYS_INT; i <= KEY_INT; i++)
  950. i2c_wr8(sd, i, 0xff);
  951. i2c_wr16(sd, INTSTATUS, 0xffff);
  952. }
  953. static void tc358743_enable_interrupts(struct v4l2_subdev *sd,
  954. bool cable_connected)
  955. {
  956. v4l2_dbg(2, debug, sd, "%s: cable connected = %d\n", __func__,
  957. cable_connected);
  958. if (cable_connected) {
  959. i2c_wr8(sd, SYS_INTM, ~(MASK_M_DDC | MASK_M_DVI_DET |
  960. MASK_M_HDMI_DET) & 0xff);
  961. i2c_wr8(sd, CLK_INTM, ~MASK_M_IN_DE_CHG);
  962. i2c_wr8(sd, CBIT_INTM, ~(MASK_M_CBIT_FS | MASK_M_AF_LOCK |
  963. MASK_M_AF_UNLOCK) & 0xff);
  964. i2c_wr8(sd, AUDIO_INTM, ~MASK_M_BUFINIT_END);
  965. i2c_wr8(sd, MISC_INTM, ~MASK_M_SYNC_CHG);
  966. } else {
  967. i2c_wr8(sd, SYS_INTM, ~MASK_M_DDC & 0xff);
  968. i2c_wr8(sd, CLK_INTM, 0xff);
  969. i2c_wr8(sd, CBIT_INTM, 0xff);
  970. i2c_wr8(sd, AUDIO_INTM, 0xff);
  971. i2c_wr8(sd, MISC_INTM, 0xff);
  972. }
  973. }
  974. static void tc358743_hdmi_audio_int_handler(struct v4l2_subdev *sd,
  975. bool *handled)
  976. {
  977. u8 audio_int_mask = i2c_rd8(sd, AUDIO_INTM);
  978. u8 audio_int = i2c_rd8(sd, AUDIO_INT) & ~audio_int_mask;
  979. i2c_wr8(sd, AUDIO_INT, audio_int);
  980. v4l2_dbg(3, debug, sd, "%s: AUDIO_INT = 0x%02x\n", __func__, audio_int);
  981. tc358743_s_ctrl_audio_sampling_rate(sd);
  982. tc358743_s_ctrl_audio_present(sd);
  983. }
  984. static void tc358743_csi_err_int_handler(struct v4l2_subdev *sd, bool *handled)
  985. {
  986. v4l2_err(sd, "%s: CSI_ERR = 0x%x\n", __func__, i2c_rd32(sd, CSI_ERR));
  987. i2c_wr32(sd, CSI_INT_CLR, MASK_ICRER);
  988. }
  989. static void tc358743_hdmi_misc_int_handler(struct v4l2_subdev *sd,
  990. bool *handled)
  991. {
  992. u8 misc_int_mask = i2c_rd8(sd, MISC_INTM);
  993. u8 misc_int = i2c_rd8(sd, MISC_INT) & ~misc_int_mask;
  994. i2c_wr8(sd, MISC_INT, misc_int);
  995. v4l2_dbg(3, debug, sd, "%s: MISC_INT = 0x%02x\n", __func__, misc_int);
  996. if (misc_int & MASK_I_SYNC_CHG) {
  997. /* Reset the HDMI PHY to try to trigger proper lock on the
  998. * incoming video format. Erase BKSV to prevent that old keys
  999. * are used when a new source is connected. */
  1000. if (no_sync(sd) || no_signal(sd)) {
  1001. tc358743_reset_phy(sd);
  1002. tc358743_erase_bksv(sd);
  1003. }
  1004. tc358743_format_change(sd);
  1005. misc_int &= ~MASK_I_SYNC_CHG;
  1006. if (handled)
  1007. *handled = true;
  1008. }
  1009. if (misc_int) {
  1010. v4l2_err(sd, "%s: Unhandled MISC_INT interrupts: 0x%02x\n",
  1011. __func__, misc_int);
  1012. }
  1013. }
  1014. static void tc358743_hdmi_cbit_int_handler(struct v4l2_subdev *sd,
  1015. bool *handled)
  1016. {
  1017. u8 cbit_int_mask = i2c_rd8(sd, CBIT_INTM);
  1018. u8 cbit_int = i2c_rd8(sd, CBIT_INT) & ~cbit_int_mask;
  1019. i2c_wr8(sd, CBIT_INT, cbit_int);
  1020. v4l2_dbg(3, debug, sd, "%s: CBIT_INT = 0x%02x\n", __func__, cbit_int);
  1021. if (cbit_int & MASK_I_CBIT_FS) {
  1022. v4l2_dbg(1, debug, sd, "%s: Audio sample rate changed\n",
  1023. __func__);
  1024. tc358743_s_ctrl_audio_sampling_rate(sd);
  1025. cbit_int &= ~MASK_I_CBIT_FS;
  1026. if (handled)
  1027. *handled = true;
  1028. }
  1029. if (cbit_int & (MASK_I_AF_LOCK | MASK_I_AF_UNLOCK)) {
  1030. v4l2_dbg(1, debug, sd, "%s: Audio present changed\n",
  1031. __func__);
  1032. tc358743_s_ctrl_audio_present(sd);
  1033. cbit_int &= ~(MASK_I_AF_LOCK | MASK_I_AF_UNLOCK);
  1034. if (handled)
  1035. *handled = true;
  1036. }
  1037. if (cbit_int) {
  1038. v4l2_err(sd, "%s: Unhandled CBIT_INT interrupts: 0x%02x\n",
  1039. __func__, cbit_int);
  1040. }
  1041. }
  1042. static void tc358743_hdmi_clk_int_handler(struct v4l2_subdev *sd, bool *handled)
  1043. {
  1044. u8 clk_int_mask = i2c_rd8(sd, CLK_INTM);
  1045. u8 clk_int = i2c_rd8(sd, CLK_INT) & ~clk_int_mask;
  1046. /* Bit 7 and bit 6 are set even when they are masked */
  1047. i2c_wr8(sd, CLK_INT, clk_int | 0x80 | MASK_I_OUT_H_CHG);
  1048. v4l2_dbg(3, debug, sd, "%s: CLK_INT = 0x%02x\n", __func__, clk_int);
  1049. if (clk_int & (MASK_I_IN_DE_CHG)) {
  1050. v4l2_dbg(1, debug, sd, "%s: DE size or position has changed\n",
  1051. __func__);
  1052. /* If the source switch to a new resolution with the same pixel
  1053. * frequency as the existing (e.g. 1080p25 -> 720p50), the
  1054. * I_SYNC_CHG interrupt is not always triggered, while the
  1055. * I_IN_DE_CHG interrupt seems to work fine. Format change
  1056. * notifications are only sent when the signal is stable to
  1057. * reduce the number of notifications. */
  1058. if (!no_signal(sd) && !no_sync(sd))
  1059. tc358743_format_change(sd);
  1060. clk_int &= ~(MASK_I_IN_DE_CHG);
  1061. if (handled)
  1062. *handled = true;
  1063. }
  1064. if (clk_int) {
  1065. v4l2_err(sd, "%s: Unhandled CLK_INT interrupts: 0x%02x\n",
  1066. __func__, clk_int);
  1067. }
  1068. }
  1069. static void tc358743_hdmi_sys_int_handler(struct v4l2_subdev *sd, bool *handled)
  1070. {
  1071. struct tc358743_state *state = to_state(sd);
  1072. u8 sys_int_mask = i2c_rd8(sd, SYS_INTM);
  1073. u8 sys_int = i2c_rd8(sd, SYS_INT) & ~sys_int_mask;
  1074. i2c_wr8(sd, SYS_INT, sys_int);
  1075. v4l2_dbg(3, debug, sd, "%s: SYS_INT = 0x%02x\n", __func__, sys_int);
  1076. if (sys_int & MASK_I_DDC) {
  1077. bool tx_5v = tx_5v_power_present(sd);
  1078. v4l2_dbg(1, debug, sd, "%s: Tx 5V power present: %s\n",
  1079. __func__, tx_5v ? "yes" : "no");
  1080. if (tx_5v) {
  1081. tc358743_enable_edid(sd);
  1082. } else {
  1083. tc358743_enable_interrupts(sd, false);
  1084. tc358743_disable_edid(sd);
  1085. memset(&state->timings, 0, sizeof(state->timings));
  1086. tc358743_erase_bksv(sd);
  1087. tc358743_update_controls(sd);
  1088. }
  1089. sys_int &= ~MASK_I_DDC;
  1090. if (handled)
  1091. *handled = true;
  1092. }
  1093. if (sys_int & MASK_I_DVI) {
  1094. v4l2_dbg(1, debug, sd, "%s: HDMI->DVI change detected\n",
  1095. __func__);
  1096. /* Reset the HDMI PHY to try to trigger proper lock on the
  1097. * incoming video format. Erase BKSV to prevent that old keys
  1098. * are used when a new source is connected. */
  1099. if (no_sync(sd) || no_signal(sd)) {
  1100. tc358743_reset_phy(sd);
  1101. tc358743_erase_bksv(sd);
  1102. }
  1103. sys_int &= ~MASK_I_DVI;
  1104. if (handled)
  1105. *handled = true;
  1106. }
  1107. if (sys_int & MASK_I_HDMI) {
  1108. v4l2_dbg(1, debug, sd, "%s: DVI->HDMI change detected\n",
  1109. __func__);
  1110. /* Register is reset in DVI mode (REF_01, c. 6.6.41) */
  1111. i2c_wr8(sd, ANA_CTL, MASK_APPL_PCSX_NORMAL | MASK_ANALOG_ON);
  1112. sys_int &= ~MASK_I_HDMI;
  1113. if (handled)
  1114. *handled = true;
  1115. }
  1116. if (sys_int) {
  1117. v4l2_err(sd, "%s: Unhandled SYS_INT interrupts: 0x%02x\n",
  1118. __func__, sys_int);
  1119. }
  1120. }
  1121. /* --------------- CORE OPS --------------- */
  1122. static int tc358743_log_status(struct v4l2_subdev *sd)
  1123. {
  1124. struct tc358743_state *state = to_state(sd);
  1125. struct v4l2_dv_timings timings;
  1126. uint8_t hdmi_sys_status = i2c_rd8(sd, SYS_STATUS);
  1127. uint16_t sysctl = i2c_rd16(sd, SYSCTL);
  1128. u8 vi_status3 = i2c_rd8(sd, VI_STATUS3);
  1129. const int deep_color_mode[4] = { 8, 10, 12, 16 };
  1130. static const char * const input_color_space[] = {
  1131. "RGB", "YCbCr 601", "opRGB", "YCbCr 709", "NA (4)",
  1132. "xvYCC 601", "NA(6)", "xvYCC 709", "NA(8)", "sYCC601",
  1133. "NA(10)", "NA(11)", "NA(12)", "opYCC 601"};
  1134. v4l2_info(sd, "-----Chip status-----\n");
  1135. v4l2_info(sd, "Chip ID: 0x%02x\n",
  1136. (i2c_rd16(sd, CHIPID) & MASK_CHIPID) >> 8);
  1137. v4l2_info(sd, "Chip revision: 0x%02x\n",
  1138. i2c_rd16(sd, CHIPID) & MASK_REVID);
  1139. v4l2_info(sd, "Reset: IR: %d, CEC: %d, CSI TX: %d, HDMI: %d\n",
  1140. !!(sysctl & MASK_IRRST),
  1141. !!(sysctl & MASK_CECRST),
  1142. !!(sysctl & MASK_CTXRST),
  1143. !!(sysctl & MASK_HDMIRST));
  1144. v4l2_info(sd, "Sleep mode: %s\n", sysctl & MASK_SLEEP ? "on" : "off");
  1145. v4l2_info(sd, "Cable detected (+5V power): %s\n",
  1146. hdmi_sys_status & MASK_S_DDC5V ? "yes" : "no");
  1147. v4l2_info(sd, "DDC lines enabled: %s\n",
  1148. (i2c_rd8(sd, EDID_MODE) & MASK_EDID_MODE_E_DDC) ?
  1149. "yes" : "no");
  1150. v4l2_info(sd, "Hotplug enabled: %s\n",
  1151. (i2c_rd8(sd, HPD_CTL) & MASK_HPD_OUT0) ?
  1152. "yes" : "no");
  1153. v4l2_info(sd, "CEC enabled: %s\n",
  1154. (i2c_rd16(sd, CECEN) & MASK_CECEN) ? "yes" : "no");
  1155. v4l2_info(sd, "-----Signal status-----\n");
  1156. v4l2_info(sd, "TMDS signal detected: %s\n",
  1157. hdmi_sys_status & MASK_S_TMDS ? "yes" : "no");
  1158. v4l2_info(sd, "Stable sync signal: %s\n",
  1159. hdmi_sys_status & MASK_S_SYNC ? "yes" : "no");
  1160. v4l2_info(sd, "PHY PLL locked: %s\n",
  1161. hdmi_sys_status & MASK_S_PHY_PLL ? "yes" : "no");
  1162. v4l2_info(sd, "PHY DE detected: %s\n",
  1163. hdmi_sys_status & MASK_S_PHY_SCDT ? "yes" : "no");
  1164. if (tc358743_get_detected_timings(sd, &timings)) {
  1165. v4l2_info(sd, "No video detected\n");
  1166. } else {
  1167. v4l2_print_dv_timings(sd->name, "Detected format: ", &timings,
  1168. true);
  1169. }
  1170. v4l2_print_dv_timings(sd->name, "Configured format: ", &state->timings,
  1171. true);
  1172. v4l2_info(sd, "-----CSI-TX status-----\n");
  1173. v4l2_info(sd, "Lanes needed: %d\n",
  1174. tc358743_num_csi_lanes_needed(sd));
  1175. v4l2_info(sd, "Lanes in use: %d\n",
  1176. state->csi_lanes_in_use);
  1177. v4l2_info(sd, "Waiting for particular sync signal: %s\n",
  1178. (i2c_rd16(sd, CSI_STATUS) & MASK_S_WSYNC) ?
  1179. "yes" : "no");
  1180. v4l2_info(sd, "Transmit mode: %s\n",
  1181. (i2c_rd16(sd, CSI_STATUS) & MASK_S_TXACT) ?
  1182. "yes" : "no");
  1183. v4l2_info(sd, "Receive mode: %s\n",
  1184. (i2c_rd16(sd, CSI_STATUS) & MASK_S_RXACT) ?
  1185. "yes" : "no");
  1186. v4l2_info(sd, "Stopped: %s\n",
  1187. (i2c_rd16(sd, CSI_STATUS) & MASK_S_HLT) ?
  1188. "yes" : "no");
  1189. v4l2_info(sd, "Color space: %s\n",
  1190. state->mbus_fmt_code == MEDIA_BUS_FMT_UYVY8_1X16 ?
  1191. "YCbCr 422 16-bit" :
  1192. state->mbus_fmt_code == MEDIA_BUS_FMT_RGB888_1X24 ?
  1193. "RGB 888 24-bit" : "Unsupported");
  1194. v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
  1195. v4l2_info(sd, "HDCP encrypted content: %s\n",
  1196. hdmi_sys_status & MASK_S_HDCP ? "yes" : "no");
  1197. v4l2_info(sd, "Input color space: %s %s range\n",
  1198. input_color_space[(vi_status3 & MASK_S_V_COLOR) >> 1],
  1199. (vi_status3 & MASK_LIMITED) ? "limited" : "full");
  1200. if (!is_hdmi(sd))
  1201. return 0;
  1202. v4l2_info(sd, "AV Mute: %s\n", hdmi_sys_status & MASK_S_AVMUTE ? "on" :
  1203. "off");
  1204. v4l2_info(sd, "Deep color mode: %d-bits per channel\n",
  1205. deep_color_mode[(i2c_rd8(sd, VI_STATUS1) &
  1206. MASK_S_DEEPCOLOR) >> 2]);
  1207. print_infoframes(sd);
  1208. return 0;
  1209. }
  1210. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1211. static void tc358743_print_register_map(struct v4l2_subdev *sd)
  1212. {
  1213. v4l2_info(sd, "0x0000-0x00FF: Global Control Register\n");
  1214. v4l2_info(sd, "0x0100-0x01FF: CSI2-TX PHY Register\n");
  1215. v4l2_info(sd, "0x0200-0x03FF: CSI2-TX PPI Register\n");
  1216. v4l2_info(sd, "0x0400-0x05FF: Reserved\n");
  1217. v4l2_info(sd, "0x0600-0x06FF: CEC Register\n");
  1218. v4l2_info(sd, "0x0700-0x84FF: Reserved\n");
  1219. v4l2_info(sd, "0x8500-0x85FF: HDMIRX System Control Register\n");
  1220. v4l2_info(sd, "0x8600-0x86FF: HDMIRX Audio Control Register\n");
  1221. v4l2_info(sd, "0x8700-0x87FF: HDMIRX InfoFrame packet data Register\n");
  1222. v4l2_info(sd, "0x8800-0x88FF: HDMIRX HDCP Port Register\n");
  1223. v4l2_info(sd, "0x8900-0x89FF: HDMIRX Video Output Port & 3D Register\n");
  1224. v4l2_info(sd, "0x8A00-0x8BFF: Reserved\n");
  1225. v4l2_info(sd, "0x8C00-0x8FFF: HDMIRX EDID-RAM (1024bytes)\n");
  1226. v4l2_info(sd, "0x9000-0x90FF: HDMIRX GBD Extraction Control\n");
  1227. v4l2_info(sd, "0x9100-0x92FF: HDMIRX GBD RAM read\n");
  1228. v4l2_info(sd, "0x9300- : Reserved\n");
  1229. }
  1230. static int tc358743_get_reg_size(u16 address)
  1231. {
  1232. /* REF_01 p. 66-72 */
  1233. if (address <= 0x00ff)
  1234. return 2;
  1235. else if ((address >= 0x0100) && (address <= 0x06FF))
  1236. return 4;
  1237. else if ((address >= 0x0700) && (address <= 0x84ff))
  1238. return 2;
  1239. else
  1240. return 1;
  1241. }
  1242. static int tc358743_g_register(struct v4l2_subdev *sd,
  1243. struct v4l2_dbg_register *reg)
  1244. {
  1245. if (reg->reg > 0xffff) {
  1246. tc358743_print_register_map(sd);
  1247. return -EINVAL;
  1248. }
  1249. reg->size = tc358743_get_reg_size(reg->reg);
  1250. reg->val = i2c_rdreg(sd, reg->reg, reg->size);
  1251. return 0;
  1252. }
  1253. static int tc358743_s_register(struct v4l2_subdev *sd,
  1254. const struct v4l2_dbg_register *reg)
  1255. {
  1256. if (reg->reg > 0xffff) {
  1257. tc358743_print_register_map(sd);
  1258. return -EINVAL;
  1259. }
  1260. /* It should not be possible for the user to enable HDCP with a simple
  1261. * v4l2-dbg command.
  1262. *
  1263. * DO NOT REMOVE THIS unless all other issues with HDCP have been
  1264. * resolved.
  1265. */
  1266. if (reg->reg == HDCP_MODE ||
  1267. reg->reg == HDCP_REG1 ||
  1268. reg->reg == HDCP_REG2 ||
  1269. reg->reg == HDCP_REG3 ||
  1270. reg->reg == BCAPS)
  1271. return 0;
  1272. i2c_wrreg(sd, (u16)reg->reg, reg->val,
  1273. tc358743_get_reg_size(reg->reg));
  1274. return 0;
  1275. }
  1276. #endif
  1277. static int tc358743_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
  1278. {
  1279. u16 intstatus = i2c_rd16(sd, INTSTATUS);
  1280. v4l2_dbg(1, debug, sd, "%s: IntStatus = 0x%04x\n", __func__, intstatus);
  1281. if (intstatus & MASK_HDMI_INT) {
  1282. u8 hdmi_int0 = i2c_rd8(sd, HDMI_INT0);
  1283. u8 hdmi_int1 = i2c_rd8(sd, HDMI_INT1);
  1284. if (hdmi_int0 & MASK_I_MISC)
  1285. tc358743_hdmi_misc_int_handler(sd, handled);
  1286. if (hdmi_int1 & MASK_I_CBIT)
  1287. tc358743_hdmi_cbit_int_handler(sd, handled);
  1288. if (hdmi_int1 & MASK_I_CLK)
  1289. tc358743_hdmi_clk_int_handler(sd, handled);
  1290. if (hdmi_int1 & MASK_I_SYS)
  1291. tc358743_hdmi_sys_int_handler(sd, handled);
  1292. if (hdmi_int1 & MASK_I_AUD)
  1293. tc358743_hdmi_audio_int_handler(sd, handled);
  1294. i2c_wr16(sd, INTSTATUS, MASK_HDMI_INT);
  1295. intstatus &= ~MASK_HDMI_INT;
  1296. }
  1297. #ifdef CONFIG_VIDEO_TC358743_CEC
  1298. if (intstatus & (MASK_CEC_RINT | MASK_CEC_TINT)) {
  1299. tc358743_cec_handler(sd, intstatus, handled);
  1300. i2c_wr16(sd, INTSTATUS,
  1301. intstatus & (MASK_CEC_RINT | MASK_CEC_TINT));
  1302. intstatus &= ~(MASK_CEC_RINT | MASK_CEC_TINT);
  1303. }
  1304. #endif
  1305. if (intstatus & MASK_CSI_INT) {
  1306. u32 csi_int = i2c_rd32(sd, CSI_INT);
  1307. if (csi_int & MASK_INTER)
  1308. tc358743_csi_err_int_handler(sd, handled);
  1309. i2c_wr16(sd, INTSTATUS, MASK_CSI_INT);
  1310. }
  1311. intstatus = i2c_rd16(sd, INTSTATUS);
  1312. if (intstatus) {
  1313. v4l2_dbg(1, debug, sd,
  1314. "%s: Unhandled IntStatus interrupts: 0x%02x\n",
  1315. __func__, intstatus);
  1316. }
  1317. return 0;
  1318. }
  1319. static irqreturn_t tc358743_irq_handler(int irq, void *dev_id)
  1320. {
  1321. struct tc358743_state *state = dev_id;
  1322. bool handled = false;
  1323. tc358743_isr(&state->sd, 0, &handled);
  1324. return handled ? IRQ_HANDLED : IRQ_NONE;
  1325. }
  1326. static void tc358743_irq_poll_timer(struct timer_list *t)
  1327. {
  1328. struct tc358743_state *state = timer_container_of(state, t, timer);
  1329. unsigned int msecs;
  1330. schedule_work(&state->work_i2c_poll);
  1331. /*
  1332. * If CEC is present, then we need to poll more frequently,
  1333. * otherwise we will miss CEC messages.
  1334. */
  1335. msecs = state->cec_adap ? POLL_INTERVAL_CEC_MS : POLL_INTERVAL_MS;
  1336. mod_timer(&state->timer, jiffies + msecs_to_jiffies(msecs));
  1337. }
  1338. static void tc358743_work_i2c_poll(struct work_struct *work)
  1339. {
  1340. struct tc358743_state *state = container_of(work,
  1341. struct tc358743_state, work_i2c_poll);
  1342. bool handled;
  1343. tc358743_isr(&state->sd, 0, &handled);
  1344. }
  1345. static int tc358743_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
  1346. struct v4l2_event_subscription *sub)
  1347. {
  1348. switch (sub->type) {
  1349. case V4L2_EVENT_SOURCE_CHANGE:
  1350. return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
  1351. case V4L2_EVENT_CTRL:
  1352. return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
  1353. default:
  1354. return -EINVAL;
  1355. }
  1356. }
  1357. /* --------------- VIDEO OPS --------------- */
  1358. static int tc358743_g_input_status(struct v4l2_subdev *sd, u32 *status)
  1359. {
  1360. *status = 0;
  1361. *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
  1362. *status |= no_sync(sd) ? V4L2_IN_ST_NO_SYNC : 0;
  1363. v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
  1364. return 0;
  1365. }
  1366. static int tc358743_s_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
  1367. struct v4l2_dv_timings *timings)
  1368. {
  1369. struct tc358743_state *state = to_state(sd);
  1370. if (pad != 0)
  1371. return -EINVAL;
  1372. if (!timings)
  1373. return -EINVAL;
  1374. if (debug)
  1375. v4l2_print_dv_timings(sd->name, "tc358743_s_dv_timings: ",
  1376. timings, false);
  1377. if (v4l2_match_dv_timings(&state->timings, timings, 0, false)) {
  1378. v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
  1379. return 0;
  1380. }
  1381. if (!v4l2_valid_dv_timings(timings,
  1382. &tc358743_timings_cap, NULL, NULL)) {
  1383. v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
  1384. return -ERANGE;
  1385. }
  1386. state->timings = *timings;
  1387. enable_stream(sd, false);
  1388. tc358743_set_pll(sd);
  1389. tc358743_set_csi(sd);
  1390. return 0;
  1391. }
  1392. static int tc358743_g_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
  1393. struct v4l2_dv_timings *timings)
  1394. {
  1395. struct tc358743_state *state = to_state(sd);
  1396. if (pad != 0)
  1397. return -EINVAL;
  1398. *timings = state->timings;
  1399. return 0;
  1400. }
  1401. static int tc358743_enum_dv_timings(struct v4l2_subdev *sd,
  1402. struct v4l2_enum_dv_timings *timings)
  1403. {
  1404. if (timings->pad != 0)
  1405. return -EINVAL;
  1406. return v4l2_enum_dv_timings_cap(timings,
  1407. &tc358743_timings_cap, NULL, NULL);
  1408. }
  1409. static int tc358743_query_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
  1410. struct v4l2_dv_timings *timings)
  1411. {
  1412. int ret;
  1413. if (pad != 0)
  1414. return -EINVAL;
  1415. ret = tc358743_get_detected_timings(sd, timings);
  1416. if (ret)
  1417. return ret;
  1418. if (debug)
  1419. v4l2_print_dv_timings(sd->name, "tc358743_query_dv_timings: ",
  1420. timings, false);
  1421. if (!v4l2_valid_dv_timings(timings,
  1422. &tc358743_timings_cap, NULL, NULL)) {
  1423. v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
  1424. return -ERANGE;
  1425. }
  1426. return 0;
  1427. }
  1428. static int tc358743_dv_timings_cap(struct v4l2_subdev *sd,
  1429. struct v4l2_dv_timings_cap *cap)
  1430. {
  1431. if (cap->pad != 0)
  1432. return -EINVAL;
  1433. *cap = tc358743_timings_cap;
  1434. return 0;
  1435. }
  1436. static int tc358743_get_mbus_config(struct v4l2_subdev *sd,
  1437. unsigned int pad,
  1438. struct v4l2_mbus_config *cfg)
  1439. {
  1440. struct tc358743_state *state = to_state(sd);
  1441. cfg->type = V4L2_MBUS_CSI2_DPHY;
  1442. /* Support for non-continuous CSI-2 clock is missing in the driver */
  1443. cfg->bus.mipi_csi2.flags = 0;
  1444. cfg->bus.mipi_csi2.num_data_lanes = state->csi_lanes_in_use;
  1445. return 0;
  1446. }
  1447. static int tc358743_s_stream(struct v4l2_subdev *sd, int enable)
  1448. {
  1449. enable_stream(sd, enable);
  1450. if (!enable) {
  1451. /* Put all lanes in LP-11 state (STOPSTATE) */
  1452. tc358743_set_csi(sd);
  1453. }
  1454. return 0;
  1455. }
  1456. /* --------------- PAD OPS --------------- */
  1457. static int tc358743_enum_mbus_code(struct v4l2_subdev *sd,
  1458. struct v4l2_subdev_state *sd_state,
  1459. struct v4l2_subdev_mbus_code_enum *code)
  1460. {
  1461. switch (code->index) {
  1462. case 0:
  1463. code->code = MEDIA_BUS_FMT_RGB888_1X24;
  1464. break;
  1465. case 1:
  1466. code->code = MEDIA_BUS_FMT_UYVY8_1X16;
  1467. break;
  1468. default:
  1469. return -EINVAL;
  1470. }
  1471. return 0;
  1472. }
  1473. static u32 tc358743_g_colorspace(u32 code)
  1474. {
  1475. switch (code) {
  1476. case MEDIA_BUS_FMT_RGB888_1X24:
  1477. return V4L2_COLORSPACE_SRGB;
  1478. case MEDIA_BUS_FMT_UYVY8_1X16:
  1479. return V4L2_COLORSPACE_SMPTE170M;
  1480. default:
  1481. return 0;
  1482. }
  1483. }
  1484. static int tc358743_get_fmt(struct v4l2_subdev *sd,
  1485. struct v4l2_subdev_state *sd_state,
  1486. struct v4l2_subdev_format *format)
  1487. {
  1488. struct tc358743_state *state = to_state(sd);
  1489. if (format->pad != 0)
  1490. return -EINVAL;
  1491. format->format.code = state->mbus_fmt_code;
  1492. format->format.width = state->timings.bt.width;
  1493. format->format.height = state->timings.bt.height;
  1494. format->format.field = V4L2_FIELD_NONE;
  1495. format->format.colorspace = tc358743_g_colorspace(format->format.code);
  1496. return 0;
  1497. }
  1498. static int tc358743_set_fmt(struct v4l2_subdev *sd,
  1499. struct v4l2_subdev_state *sd_state,
  1500. struct v4l2_subdev_format *format)
  1501. {
  1502. struct tc358743_state *state = to_state(sd);
  1503. u32 code = format->format.code; /* is overwritten by get_fmt */
  1504. int ret = tc358743_get_fmt(sd, sd_state, format);
  1505. if (code == MEDIA_BUS_FMT_RGB888_1X24 ||
  1506. code == MEDIA_BUS_FMT_UYVY8_1X16)
  1507. format->format.code = code;
  1508. format->format.colorspace = tc358743_g_colorspace(format->format.code);
  1509. if (ret)
  1510. return ret;
  1511. if (format->which == V4L2_SUBDEV_FORMAT_TRY)
  1512. return 0;
  1513. state->mbus_fmt_code = format->format.code;
  1514. enable_stream(sd, false);
  1515. tc358743_set_pll(sd);
  1516. tc358743_set_csi(sd);
  1517. tc358743_set_csi_color_space(sd);
  1518. return 0;
  1519. }
  1520. static int tc358743_g_edid(struct v4l2_subdev *sd,
  1521. struct v4l2_subdev_edid *edid)
  1522. {
  1523. struct tc358743_state *state = to_state(sd);
  1524. memset(edid->reserved, 0, sizeof(edid->reserved));
  1525. if (edid->pad != 0)
  1526. return -EINVAL;
  1527. if (edid->start_block == 0 && edid->blocks == 0) {
  1528. edid->blocks = state->edid_blocks_written;
  1529. return 0;
  1530. }
  1531. if (state->edid_blocks_written == 0)
  1532. return -ENODATA;
  1533. if (edid->start_block >= state->edid_blocks_written ||
  1534. edid->blocks == 0)
  1535. return -EINVAL;
  1536. if (edid->start_block + edid->blocks > state->edid_blocks_written)
  1537. edid->blocks = state->edid_blocks_written - edid->start_block;
  1538. i2c_rd(sd, EDID_RAM + (edid->start_block * EDID_BLOCK_SIZE), edid->edid,
  1539. edid->blocks * EDID_BLOCK_SIZE);
  1540. return 0;
  1541. }
  1542. static int tc358743_s_edid(struct v4l2_subdev *sd,
  1543. struct v4l2_subdev_edid *edid)
  1544. {
  1545. struct tc358743_state *state = to_state(sd);
  1546. u16 edid_len = edid->blocks * EDID_BLOCK_SIZE;
  1547. u16 pa;
  1548. int err;
  1549. int i;
  1550. v4l2_dbg(2, debug, sd, "%s, pad %d, start block %d, blocks %d\n",
  1551. __func__, edid->pad, edid->start_block, edid->blocks);
  1552. memset(edid->reserved, 0, sizeof(edid->reserved));
  1553. if (edid->pad != 0)
  1554. return -EINVAL;
  1555. if (edid->start_block != 0)
  1556. return -EINVAL;
  1557. if (edid->blocks > EDID_NUM_BLOCKS_MAX) {
  1558. edid->blocks = EDID_NUM_BLOCKS_MAX;
  1559. return -E2BIG;
  1560. }
  1561. pa = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
  1562. err = v4l2_phys_addr_validate(pa, &pa, NULL);
  1563. if (err)
  1564. return err;
  1565. cec_phys_addr_invalidate(state->cec_adap);
  1566. tc358743_disable_edid(sd);
  1567. i2c_wr8(sd, EDID_LEN1, edid_len & 0xff);
  1568. i2c_wr8(sd, EDID_LEN2, edid_len >> 8);
  1569. if (edid->blocks == 0) {
  1570. state->edid_blocks_written = 0;
  1571. return 0;
  1572. }
  1573. for (i = 0; i < edid_len; i += EDID_BLOCK_SIZE)
  1574. i2c_wr(sd, EDID_RAM + i, edid->edid + i, EDID_BLOCK_SIZE);
  1575. state->edid_blocks_written = edid->blocks;
  1576. cec_s_phys_addr(state->cec_adap, pa, false);
  1577. if (tx_5v_power_present(sd))
  1578. tc358743_enable_edid(sd);
  1579. return 0;
  1580. }
  1581. /* -------------------------------------------------------------------------- */
  1582. static const struct v4l2_subdev_core_ops tc358743_core_ops = {
  1583. .log_status = tc358743_log_status,
  1584. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1585. .g_register = tc358743_g_register,
  1586. .s_register = tc358743_s_register,
  1587. #endif
  1588. .interrupt_service_routine = tc358743_isr,
  1589. .subscribe_event = tc358743_subscribe_event,
  1590. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  1591. };
  1592. static const struct v4l2_subdev_video_ops tc358743_video_ops = {
  1593. .g_input_status = tc358743_g_input_status,
  1594. .s_stream = tc358743_s_stream,
  1595. };
  1596. static const struct v4l2_subdev_pad_ops tc358743_pad_ops = {
  1597. .enum_mbus_code = tc358743_enum_mbus_code,
  1598. .set_fmt = tc358743_set_fmt,
  1599. .get_fmt = tc358743_get_fmt,
  1600. .get_edid = tc358743_g_edid,
  1601. .set_edid = tc358743_s_edid,
  1602. .s_dv_timings = tc358743_s_dv_timings,
  1603. .g_dv_timings = tc358743_g_dv_timings,
  1604. .query_dv_timings = tc358743_query_dv_timings,
  1605. .enum_dv_timings = tc358743_enum_dv_timings,
  1606. .dv_timings_cap = tc358743_dv_timings_cap,
  1607. .get_mbus_config = tc358743_get_mbus_config,
  1608. };
  1609. static const struct v4l2_subdev_ops tc358743_ops = {
  1610. .core = &tc358743_core_ops,
  1611. .video = &tc358743_video_ops,
  1612. .pad = &tc358743_pad_ops,
  1613. };
  1614. /* --------------- CUSTOM CTRLS --------------- */
  1615. static const struct v4l2_ctrl_config tc358743_ctrl_audio_sampling_rate = {
  1616. .id = TC358743_CID_AUDIO_SAMPLING_RATE,
  1617. .name = "Audio sampling rate",
  1618. .type = V4L2_CTRL_TYPE_INTEGER,
  1619. .min = 0,
  1620. .max = 768000,
  1621. .step = 1,
  1622. .def = 0,
  1623. .flags = V4L2_CTRL_FLAG_READ_ONLY,
  1624. };
  1625. static const struct v4l2_ctrl_config tc358743_ctrl_audio_present = {
  1626. .id = TC358743_CID_AUDIO_PRESENT,
  1627. .name = "Audio present",
  1628. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1629. .min = 0,
  1630. .max = 1,
  1631. .step = 1,
  1632. .def = 0,
  1633. .flags = V4L2_CTRL_FLAG_READ_ONLY,
  1634. };
  1635. /* --------------- PROBE / REMOVE --------------- */
  1636. #ifdef CONFIG_OF
  1637. static void tc358743_gpio_reset(struct tc358743_state *state)
  1638. {
  1639. usleep_range(5000, 10000);
  1640. gpiod_set_value(state->reset_gpio, 1);
  1641. usleep_range(1000, 2000);
  1642. gpiod_set_value(state->reset_gpio, 0);
  1643. msleep(20);
  1644. }
  1645. static int tc358743_probe_of(struct tc358743_state *state)
  1646. {
  1647. struct device *dev = &state->i2c_client->dev;
  1648. struct v4l2_fwnode_endpoint endpoint = { .bus_type = 0 };
  1649. struct device_node *ep;
  1650. struct clk *refclk;
  1651. u32 bps_pr_lane;
  1652. int ret;
  1653. refclk = devm_clk_get(dev, "refclk");
  1654. if (IS_ERR(refclk))
  1655. return dev_err_probe(dev, PTR_ERR(refclk),
  1656. "failed to get refclk\n");
  1657. ep = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
  1658. if (!ep) {
  1659. dev_err(dev, "missing endpoint node\n");
  1660. return -EINVAL;
  1661. }
  1662. ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep), &endpoint);
  1663. if (ret) {
  1664. dev_err(dev, "failed to parse endpoint\n");
  1665. goto put_node;
  1666. }
  1667. if (endpoint.bus_type != V4L2_MBUS_CSI2_DPHY ||
  1668. endpoint.bus.mipi_csi2.num_data_lanes == 0 ||
  1669. endpoint.nr_of_link_frequencies == 0) {
  1670. dev_err(dev, "missing CSI-2 properties in endpoint\n");
  1671. ret = -EINVAL;
  1672. goto free_endpoint;
  1673. }
  1674. if (endpoint.bus.mipi_csi2.num_data_lanes > 4) {
  1675. dev_err(dev, "invalid number of lanes\n");
  1676. ret = -EINVAL;
  1677. goto free_endpoint;
  1678. }
  1679. state->bus = endpoint.bus.mipi_csi2;
  1680. ret = clk_prepare_enable(refclk);
  1681. if (ret) {
  1682. dev_err(dev, "Failed! to enable clock\n");
  1683. goto free_endpoint;
  1684. }
  1685. state->pdata.refclk_hz = clk_get_rate(refclk);
  1686. state->pdata.ddc5v_delay = DDC5V_DELAY_100_MS;
  1687. state->pdata.enable_hdcp = false;
  1688. /*
  1689. * Ideally the FIFO trigger level should be set based on the input and
  1690. * output data rates, but the calculations required are buried in
  1691. * Toshiba's register settings spreadsheet.
  1692. * A value of 16 works with a 594Mbps data rate for 720p60 (using 2
  1693. * lanes) and 1080p60 (using 4 lanes), but fails when the data rate
  1694. * is increased, or a lower pixel clock is used that result in CSI
  1695. * reading out faster than the data is arriving.
  1696. *
  1697. * A value of 374 works with both those modes at 594Mbps, and with most
  1698. * modes on 972Mbps.
  1699. */
  1700. state->pdata.fifo_level = 374;
  1701. /*
  1702. * The PLL input clock is obtained by dividing refclk by pll_prd.
  1703. * It must be between 6 MHz and 40 MHz, lower frequency is better.
  1704. */
  1705. switch (state->pdata.refclk_hz) {
  1706. case 26000000:
  1707. case 27000000:
  1708. case 42000000:
  1709. state->pdata.pll_prd = state->pdata.refclk_hz / 6000000;
  1710. break;
  1711. default:
  1712. dev_err(dev, "unsupported refclk rate: %u Hz\n",
  1713. state->pdata.refclk_hz);
  1714. goto disable_clk;
  1715. }
  1716. /*
  1717. * The CSI bps per lane must be between 62.5 Mbps and 1 Gbps.
  1718. * The default is 594 Mbps for 4-lane 1080p60 or 2-lane 720p60.
  1719. * 972 Mbps allows 1080P50 UYVY over 2-lane.
  1720. */
  1721. bps_pr_lane = 2 * endpoint.link_frequencies[0];
  1722. if (bps_pr_lane < 62500000U || bps_pr_lane > 1000000000U) {
  1723. dev_err(dev, "unsupported bps per lane: %u bps\n", bps_pr_lane);
  1724. ret = -EINVAL;
  1725. goto disable_clk;
  1726. }
  1727. /* The CSI speed per lane is refclk / pll_prd * pll_fbd */
  1728. state->pdata.pll_fbd = bps_pr_lane /
  1729. state->pdata.refclk_hz * state->pdata.pll_prd;
  1730. /*
  1731. * FIXME: These timings are from REF_02 for 594 or 972 Mbps per lane
  1732. * (297 MHz or 486 MHz link frequency).
  1733. * In principle it should be possible to calculate
  1734. * them based on link frequency and resolution.
  1735. */
  1736. switch (bps_pr_lane) {
  1737. default:
  1738. dev_warn(dev, "untested bps per lane: %u bps\n", bps_pr_lane);
  1739. fallthrough;
  1740. case 594000000U:
  1741. state->pdata.lineinitcnt = 0xe80;
  1742. state->pdata.lptxtimecnt = 0x003;
  1743. /* tclk-preparecnt: 3, tclk-zerocnt: 20 */
  1744. state->pdata.tclk_headercnt = 0x1403;
  1745. state->pdata.tclk_trailcnt = 0x00;
  1746. /* ths-preparecnt: 3, ths-zerocnt: 1 */
  1747. state->pdata.ths_headercnt = 0x0103;
  1748. state->pdata.twakeup = 0x4882;
  1749. state->pdata.tclk_postcnt = 0x008;
  1750. state->pdata.ths_trailcnt = 0x2;
  1751. state->pdata.hstxvregcnt = 0;
  1752. break;
  1753. case 972000000U:
  1754. state->pdata.lineinitcnt = 0x1b58;
  1755. state->pdata.lptxtimecnt = 0x007;
  1756. /* tclk-preparecnt: 6, tclk-zerocnt: 40 */
  1757. state->pdata.tclk_headercnt = 0x2806;
  1758. state->pdata.tclk_trailcnt = 0x00;
  1759. /* ths-preparecnt: 6, ths-zerocnt: 8 */
  1760. state->pdata.ths_headercnt = 0x0806;
  1761. state->pdata.twakeup = 0x4268;
  1762. state->pdata.tclk_postcnt = 0x008;
  1763. state->pdata.ths_trailcnt = 0x5;
  1764. state->pdata.hstxvregcnt = 0;
  1765. break;
  1766. }
  1767. state->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  1768. GPIOD_OUT_LOW);
  1769. if (IS_ERR(state->reset_gpio)) {
  1770. dev_err(dev, "failed to get reset gpio\n");
  1771. ret = PTR_ERR(state->reset_gpio);
  1772. goto disable_clk;
  1773. }
  1774. if (state->reset_gpio)
  1775. tc358743_gpio_reset(state);
  1776. ret = 0;
  1777. goto free_endpoint;
  1778. disable_clk:
  1779. clk_disable_unprepare(refclk);
  1780. free_endpoint:
  1781. v4l2_fwnode_endpoint_free(&endpoint);
  1782. put_node:
  1783. of_node_put(ep);
  1784. return ret;
  1785. }
  1786. #else
  1787. static inline int tc358743_probe_of(struct tc358743_state *state)
  1788. {
  1789. return -ENODEV;
  1790. }
  1791. #endif
  1792. static int tc358743_probe(struct i2c_client *client)
  1793. {
  1794. static struct v4l2_dv_timings default_timing =
  1795. V4L2_DV_BT_CEA_640X480P59_94;
  1796. struct tc358743_state *state;
  1797. struct tc358743_platform_data *pdata = client->dev.platform_data;
  1798. struct v4l2_subdev *sd;
  1799. u16 irq_mask = MASK_HDMI_MSK | MASK_CSI_MSK;
  1800. u16 chipid;
  1801. int err;
  1802. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1803. return -EIO;
  1804. v4l_dbg(1, debug, client, "chip found @ 0x%x (%s)\n",
  1805. client->addr << 1, client->adapter->name);
  1806. state = devm_kzalloc(&client->dev, sizeof(struct tc358743_state),
  1807. GFP_KERNEL);
  1808. if (!state)
  1809. return -ENOMEM;
  1810. state->i2c_client = client;
  1811. /* platform data */
  1812. if (pdata) {
  1813. state->pdata = *pdata;
  1814. state->bus.flags = 0;
  1815. } else {
  1816. err = tc358743_probe_of(state);
  1817. if (err == -ENODEV)
  1818. v4l_err(client, "No platform data!\n");
  1819. if (err)
  1820. return err;
  1821. }
  1822. sd = &state->sd;
  1823. v4l2_i2c_subdev_init(sd, client, &tc358743_ops);
  1824. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  1825. /* i2c access */
  1826. if (i2c_rd16_err(sd, CHIPID, &chipid) ||
  1827. (chipid & MASK_CHIPID) != 0) {
  1828. v4l2_info(sd, "not a TC358743 on address 0x%x\n",
  1829. client->addr << 1);
  1830. return -ENODEV;
  1831. }
  1832. /* control handlers */
  1833. v4l2_ctrl_handler_init(&state->hdl, 3);
  1834. state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(&state->hdl, NULL,
  1835. V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
  1836. /* custom controls */
  1837. state->audio_sampling_rate_ctrl = v4l2_ctrl_new_custom(&state->hdl,
  1838. &tc358743_ctrl_audio_sampling_rate, NULL);
  1839. state->audio_present_ctrl = v4l2_ctrl_new_custom(&state->hdl,
  1840. &tc358743_ctrl_audio_present, NULL);
  1841. sd->ctrl_handler = &state->hdl;
  1842. if (state->hdl.error) {
  1843. err = state->hdl.error;
  1844. goto err_hdl;
  1845. }
  1846. if (tc358743_update_controls(sd)) {
  1847. err = -ENODEV;
  1848. goto err_hdl;
  1849. }
  1850. state->pad.flags = MEDIA_PAD_FL_SOURCE;
  1851. sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
  1852. err = media_entity_pads_init(&sd->entity, 1, &state->pad);
  1853. if (err < 0)
  1854. goto err_hdl;
  1855. state->mbus_fmt_code = MEDIA_BUS_FMT_RGB888_1X24;
  1856. sd->dev = &client->dev;
  1857. mutex_init(&state->confctl_mutex);
  1858. INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
  1859. tc358743_delayed_work_enable_hotplug);
  1860. #ifdef CONFIG_VIDEO_TC358743_CEC
  1861. state->cec_adap = cec_allocate_adapter(&tc358743_cec_adap_ops,
  1862. state, dev_name(&client->dev),
  1863. CEC_CAP_DEFAULTS | CEC_CAP_MONITOR_ALL, CEC_MAX_LOG_ADDRS);
  1864. if (IS_ERR(state->cec_adap)) {
  1865. err = PTR_ERR(state->cec_adap);
  1866. goto err_hdl;
  1867. }
  1868. irq_mask |= MASK_CEC_RMSK | MASK_CEC_TMSK;
  1869. #endif
  1870. tc358743_initial_setup(sd);
  1871. tc358743_s_dv_timings(sd, 0, &default_timing);
  1872. tc358743_set_csi_color_space(sd);
  1873. tc358743_init_interrupts(sd);
  1874. if (state->i2c_client->irq) {
  1875. err = devm_request_threaded_irq(&client->dev,
  1876. state->i2c_client->irq,
  1877. NULL, tc358743_irq_handler,
  1878. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  1879. "tc358743", state);
  1880. if (err)
  1881. goto err_work_queues;
  1882. } else {
  1883. INIT_WORK(&state->work_i2c_poll,
  1884. tc358743_work_i2c_poll);
  1885. timer_setup(&state->timer, tc358743_irq_poll_timer, 0);
  1886. state->timer.expires = jiffies +
  1887. msecs_to_jiffies(POLL_INTERVAL_MS);
  1888. add_timer(&state->timer);
  1889. }
  1890. err = cec_register_adapter(state->cec_adap, &client->dev);
  1891. if (err < 0) {
  1892. pr_err("%s: failed to register the cec device\n", __func__);
  1893. cec_delete_adapter(state->cec_adap);
  1894. state->cec_adap = NULL;
  1895. goto err_work_queues;
  1896. }
  1897. tc358743_enable_interrupts(sd, tx_5v_power_present(sd));
  1898. i2c_wr16(sd, INTMASK, ~irq_mask);
  1899. err = v4l2_ctrl_handler_setup(sd->ctrl_handler);
  1900. if (err)
  1901. goto err_work_queues;
  1902. err = v4l2_async_register_subdev(sd);
  1903. if (err < 0)
  1904. goto err_work_queues;
  1905. i2c_wr8(sd, TYP_ACP_SET, packet_type);
  1906. i2c_wr8(sd, PK_AUTO_CLR, 0xff);
  1907. i2c_wr8(sd, NO_PKT_CLR, MASK_NO_ACP_CLR);
  1908. state->debugfs_dir = debugfs_create_dir(sd->name, v4l2_debugfs_root());
  1909. state->infoframes = v4l2_debugfs_if_alloc(state->debugfs_dir,
  1910. V4L2_DEBUGFS_IF_AVI | V4L2_DEBUGFS_IF_AUDIO |
  1911. V4L2_DEBUGFS_IF_SPD | V4L2_DEBUGFS_IF_HDMI |
  1912. V4L2_DEBUGFS_IF_DRM, sd, tc358743_debugfs_if_read);
  1913. v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
  1914. client->addr << 1, client->adapter->name);
  1915. return 0;
  1916. err_work_queues:
  1917. cec_unregister_adapter(state->cec_adap);
  1918. if (!state->i2c_client->irq) {
  1919. timer_delete_sync(&state->timer);
  1920. flush_work(&state->work_i2c_poll);
  1921. }
  1922. cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
  1923. mutex_destroy(&state->confctl_mutex);
  1924. err_hdl:
  1925. media_entity_cleanup(&sd->entity);
  1926. v4l2_ctrl_handler_free(&state->hdl);
  1927. return err;
  1928. }
  1929. static void tc358743_remove(struct i2c_client *client)
  1930. {
  1931. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1932. struct tc358743_state *state = to_state(sd);
  1933. if (!state->i2c_client->irq) {
  1934. timer_delete_sync(&state->timer);
  1935. flush_work(&state->work_i2c_poll);
  1936. }
  1937. cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
  1938. v4l2_debugfs_if_free(state->infoframes);
  1939. debugfs_remove_recursive(state->debugfs_dir);
  1940. cec_unregister_adapter(state->cec_adap);
  1941. v4l2_async_unregister_subdev(sd);
  1942. v4l2_device_unregister_subdev(sd);
  1943. mutex_destroy(&state->confctl_mutex);
  1944. media_entity_cleanup(&sd->entity);
  1945. v4l2_ctrl_handler_free(&state->hdl);
  1946. }
  1947. static const struct i2c_device_id tc358743_id[] = {
  1948. { "tc358743" },
  1949. {}
  1950. };
  1951. MODULE_DEVICE_TABLE(i2c, tc358743_id);
  1952. #if IS_ENABLED(CONFIG_OF)
  1953. static const struct of_device_id tc358743_of_match[] = {
  1954. { .compatible = "toshiba,tc358743" },
  1955. {},
  1956. };
  1957. MODULE_DEVICE_TABLE(of, tc358743_of_match);
  1958. #endif
  1959. static struct i2c_driver tc358743_driver = {
  1960. .driver = {
  1961. .name = "tc358743",
  1962. .of_match_table = of_match_ptr(tc358743_of_match),
  1963. },
  1964. .probe = tc358743_probe,
  1965. .remove = tc358743_remove,
  1966. .id_table = tc358743_id,
  1967. };
  1968. module_i2c_driver(tc358743_driver);