sil-sii8620.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Silicon Image SiI8620 HDMI/MHL bridge driver
  4. *
  5. * Copyright (C) 2015, Samsung Electronics Co., Ltd.
  6. * Andrzej Hajda <[email protected]>
  7. */
  8. #include <asm/unaligned.h>
  9. #include <drm/bridge/mhl.h>
  10. #include <drm/drm_bridge.h>
  11. #include <drm/drm_crtc.h>
  12. #include <drm/drm_edid.h>
  13. #include <drm/drm_encoder.h>
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/extcon.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/i2c.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/kernel.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/of_graph.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/slab.h>
  28. #include <media/rc-core.h>
  29. #include "sil-sii8620.h"
  30. #define SII8620_BURST_BUF_LEN 288
  31. #define VAL_RX_HDMI_CTRL2_DEFVAL VAL_RX_HDMI_CTRL2_IDLE_CNT(3)
  32. #define MHL1_MAX_PCLK 75000
  33. #define MHL1_MAX_PCLK_PP_MODE 150000
  34. #define MHL3_MAX_PCLK 200000
  35. #define MHL3_MAX_PCLK_PP_MODE 300000
  36. enum sii8620_mode {
  37. CM_DISCONNECTED,
  38. CM_DISCOVERY,
  39. CM_MHL1,
  40. CM_MHL3,
  41. CM_ECBUS_S
  42. };
  43. enum sii8620_sink_type {
  44. SINK_NONE,
  45. SINK_HDMI,
  46. SINK_DVI
  47. };
  48. enum sii8620_mt_state {
  49. MT_STATE_READY,
  50. MT_STATE_BUSY,
  51. MT_STATE_DONE
  52. };
  53. struct sii8620 {
  54. struct drm_bridge bridge;
  55. struct device *dev;
  56. struct rc_dev *rc_dev;
  57. struct clk *clk_xtal;
  58. struct gpio_desc *gpio_reset;
  59. struct gpio_desc *gpio_int;
  60. struct regulator_bulk_data supplies[2];
  61. struct mutex lock; /* context lock, protects fields below */
  62. int error;
  63. unsigned int use_packed_pixel:1;
  64. enum sii8620_mode mode;
  65. enum sii8620_sink_type sink_type;
  66. u8 cbus_status;
  67. u8 stat[MHL_DST_SIZE];
  68. u8 xstat[MHL_XDS_SIZE];
  69. u8 devcap[MHL_DCAP_SIZE];
  70. u8 xdevcap[MHL_XDC_SIZE];
  71. bool feature_complete;
  72. bool devcap_read;
  73. bool sink_detected;
  74. struct edid *edid;
  75. unsigned int gen2_write_burst:1;
  76. enum sii8620_mt_state mt_state;
  77. struct extcon_dev *extcon;
  78. struct notifier_block extcon_nb;
  79. struct work_struct extcon_wq;
  80. int cable_state;
  81. struct list_head mt_queue;
  82. struct {
  83. int r_size;
  84. int r_count;
  85. int rx_ack;
  86. int rx_count;
  87. u8 rx_buf[32];
  88. int tx_count;
  89. u8 tx_buf[32];
  90. } burst;
  91. };
  92. struct sii8620_mt_msg;
  93. typedef void (*sii8620_mt_msg_cb)(struct sii8620 *ctx,
  94. struct sii8620_mt_msg *msg);
  95. typedef void (*sii8620_cb)(struct sii8620 *ctx, int ret);
  96. struct sii8620_mt_msg {
  97. struct list_head node;
  98. u8 reg[4];
  99. u8 ret;
  100. sii8620_mt_msg_cb send;
  101. sii8620_mt_msg_cb recv;
  102. sii8620_cb continuation;
  103. };
  104. static const u8 sii8620_i2c_page[] = {
  105. 0x39, /* Main System */
  106. 0x3d, /* TDM and HSIC */
  107. 0x49, /* TMDS Receiver, MHL EDID */
  108. 0x4d, /* eMSC, HDCP, HSIC */
  109. 0x5d, /* MHL Spec */
  110. 0x64, /* MHL CBUS */
  111. 0x59, /* Hardware TPI (Transmitter Programming Interface) */
  112. 0x61, /* eCBUS-S, eCBUS-D */
  113. };
  114. static void sii8620_fetch_edid(struct sii8620 *ctx);
  115. static void sii8620_set_upstream_edid(struct sii8620 *ctx);
  116. static void sii8620_enable_hpd(struct sii8620 *ctx);
  117. static void sii8620_mhl_disconnected(struct sii8620 *ctx);
  118. static void sii8620_disconnect(struct sii8620 *ctx);
  119. static int sii8620_clear_error(struct sii8620 *ctx)
  120. {
  121. int ret = ctx->error;
  122. ctx->error = 0;
  123. return ret;
  124. }
  125. static void sii8620_read_buf(struct sii8620 *ctx, u16 addr, u8 *buf, int len)
  126. {
  127. struct device *dev = ctx->dev;
  128. struct i2c_client *client = to_i2c_client(dev);
  129. u8 data = addr;
  130. struct i2c_msg msg[] = {
  131. {
  132. .addr = sii8620_i2c_page[addr >> 8],
  133. .flags = client->flags,
  134. .len = 1,
  135. .buf = &data
  136. },
  137. {
  138. .addr = sii8620_i2c_page[addr >> 8],
  139. .flags = client->flags | I2C_M_RD,
  140. .len = len,
  141. .buf = buf
  142. },
  143. };
  144. int ret;
  145. if (ctx->error)
  146. return;
  147. ret = i2c_transfer(client->adapter, msg, 2);
  148. dev_dbg(dev, "read at %04x: %*ph, %d\n", addr, len, buf, ret);
  149. if (ret != 2) {
  150. dev_err(dev, "Read at %#06x of %d bytes failed with code %d.\n",
  151. addr, len, ret);
  152. ctx->error = ret < 0 ? ret : -EIO;
  153. }
  154. }
  155. static u8 sii8620_readb(struct sii8620 *ctx, u16 addr)
  156. {
  157. u8 ret = 0;
  158. sii8620_read_buf(ctx, addr, &ret, 1);
  159. return ret;
  160. }
  161. static void sii8620_write_buf(struct sii8620 *ctx, u16 addr, const u8 *buf,
  162. int len)
  163. {
  164. struct device *dev = ctx->dev;
  165. struct i2c_client *client = to_i2c_client(dev);
  166. u8 data[2];
  167. struct i2c_msg msg = {
  168. .addr = sii8620_i2c_page[addr >> 8],
  169. .flags = client->flags,
  170. .len = len + 1,
  171. };
  172. int ret;
  173. if (ctx->error)
  174. return;
  175. if (len > 1) {
  176. msg.buf = kmalloc(len + 1, GFP_KERNEL);
  177. if (!msg.buf) {
  178. ctx->error = -ENOMEM;
  179. return;
  180. }
  181. memcpy(msg.buf + 1, buf, len);
  182. } else {
  183. msg.buf = data;
  184. msg.buf[1] = *buf;
  185. }
  186. msg.buf[0] = addr;
  187. ret = i2c_transfer(client->adapter, &msg, 1);
  188. dev_dbg(dev, "write at %04x: %*ph, %d\n", addr, len, buf, ret);
  189. if (ret != 1) {
  190. dev_err(dev, "Write at %#06x of %*ph failed with code %d.\n",
  191. addr, len, buf, ret);
  192. ctx->error = ret ?: -EIO;
  193. }
  194. if (len > 1)
  195. kfree(msg.buf);
  196. }
  197. #define sii8620_write(ctx, addr, arr...) \
  198. ({\
  199. u8 d[] = { arr }; \
  200. sii8620_write_buf(ctx, addr, d, ARRAY_SIZE(d)); \
  201. })
  202. static void __sii8620_write_seq(struct sii8620 *ctx, const u16 *seq, int len)
  203. {
  204. int i;
  205. for (i = 0; i < len; i += 2)
  206. sii8620_write(ctx, seq[i], seq[i + 1]);
  207. }
  208. #define sii8620_write_seq(ctx, seq...) \
  209. ({\
  210. const u16 d[] = { seq }; \
  211. __sii8620_write_seq(ctx, d, ARRAY_SIZE(d)); \
  212. })
  213. #define sii8620_write_seq_static(ctx, seq...) \
  214. ({\
  215. static const u16 d[] = { seq }; \
  216. __sii8620_write_seq(ctx, d, ARRAY_SIZE(d)); \
  217. })
  218. static void sii8620_setbits(struct sii8620 *ctx, u16 addr, u8 mask, u8 val)
  219. {
  220. val = (val & mask) | (sii8620_readb(ctx, addr) & ~mask);
  221. sii8620_write(ctx, addr, val);
  222. }
  223. static inline bool sii8620_is_mhl3(struct sii8620 *ctx)
  224. {
  225. return ctx->mode >= CM_MHL3;
  226. }
  227. static void sii8620_mt_cleanup(struct sii8620 *ctx)
  228. {
  229. struct sii8620_mt_msg *msg, *n;
  230. list_for_each_entry_safe(msg, n, &ctx->mt_queue, node) {
  231. list_del(&msg->node);
  232. kfree(msg);
  233. }
  234. ctx->mt_state = MT_STATE_READY;
  235. }
  236. static void sii8620_mt_work(struct sii8620 *ctx)
  237. {
  238. struct sii8620_mt_msg *msg;
  239. if (ctx->error)
  240. return;
  241. if (ctx->mt_state == MT_STATE_BUSY || list_empty(&ctx->mt_queue))
  242. return;
  243. if (ctx->mt_state == MT_STATE_DONE) {
  244. ctx->mt_state = MT_STATE_READY;
  245. msg = list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg,
  246. node);
  247. list_del(&msg->node);
  248. if (msg->recv)
  249. msg->recv(ctx, msg);
  250. if (msg->continuation)
  251. msg->continuation(ctx, msg->ret);
  252. kfree(msg);
  253. }
  254. if (ctx->mt_state != MT_STATE_READY || list_empty(&ctx->mt_queue))
  255. return;
  256. ctx->mt_state = MT_STATE_BUSY;
  257. msg = list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
  258. if (msg->send)
  259. msg->send(ctx, msg);
  260. }
  261. static void sii8620_enable_gen2_write_burst(struct sii8620 *ctx)
  262. {
  263. u8 ctrl = BIT_MDT_RCV_CTRL_MDT_RCV_EN;
  264. if (ctx->gen2_write_burst)
  265. return;
  266. if (ctx->mode >= CM_MHL1)
  267. ctrl |= BIT_MDT_RCV_CTRL_MDT_DELAY_RCV_EN;
  268. sii8620_write_seq(ctx,
  269. REG_MDT_RCV_TIMEOUT, 100,
  270. REG_MDT_RCV_CTRL, ctrl
  271. );
  272. ctx->gen2_write_burst = 1;
  273. }
  274. static void sii8620_disable_gen2_write_burst(struct sii8620 *ctx)
  275. {
  276. if (!ctx->gen2_write_burst)
  277. return;
  278. sii8620_write_seq_static(ctx,
  279. REG_MDT_XMIT_CTRL, 0,
  280. REG_MDT_RCV_CTRL, 0
  281. );
  282. ctx->gen2_write_burst = 0;
  283. }
  284. static void sii8620_start_gen2_write_burst(struct sii8620 *ctx)
  285. {
  286. sii8620_write_seq_static(ctx,
  287. REG_MDT_INT_1_MASK, BIT_MDT_RCV_TIMEOUT
  288. | BIT_MDT_RCV_SM_ABORT_PKT_RCVD | BIT_MDT_RCV_SM_ERROR
  289. | BIT_MDT_XMIT_TIMEOUT | BIT_MDT_XMIT_SM_ABORT_PKT_RCVD
  290. | BIT_MDT_XMIT_SM_ERROR,
  291. REG_MDT_INT_0_MASK, BIT_MDT_XFIFO_EMPTY
  292. | BIT_MDT_IDLE_AFTER_HAWB_DISABLE
  293. | BIT_MDT_RFIFO_DATA_RDY
  294. );
  295. sii8620_enable_gen2_write_burst(ctx);
  296. }
  297. static void sii8620_mt_msc_cmd_send(struct sii8620 *ctx,
  298. struct sii8620_mt_msg *msg)
  299. {
  300. if (msg->reg[0] == MHL_SET_INT &&
  301. msg->reg[1] == MHL_INT_REG(RCHANGE) &&
  302. msg->reg[2] == MHL_INT_RC_FEAT_REQ)
  303. sii8620_enable_gen2_write_burst(ctx);
  304. else
  305. sii8620_disable_gen2_write_burst(ctx);
  306. switch (msg->reg[0]) {
  307. case MHL_WRITE_STAT:
  308. case MHL_SET_INT:
  309. sii8620_write_buf(ctx, REG_MSC_CMD_OR_OFFSET, msg->reg + 1, 2);
  310. sii8620_write(ctx, REG_MSC_COMMAND_START,
  311. BIT_MSC_COMMAND_START_WRITE_STAT);
  312. break;
  313. case MHL_MSC_MSG:
  314. sii8620_write_buf(ctx, REG_MSC_CMD_OR_OFFSET, msg->reg, 3);
  315. sii8620_write(ctx, REG_MSC_COMMAND_START,
  316. BIT_MSC_COMMAND_START_MSC_MSG);
  317. break;
  318. case MHL_READ_DEVCAP_REG:
  319. case MHL_READ_XDEVCAP_REG:
  320. sii8620_write(ctx, REG_MSC_CMD_OR_OFFSET, msg->reg[1]);
  321. sii8620_write(ctx, REG_MSC_COMMAND_START,
  322. BIT_MSC_COMMAND_START_READ_DEVCAP);
  323. break;
  324. default:
  325. dev_err(ctx->dev, "%s: command %#x not supported\n", __func__,
  326. msg->reg[0]);
  327. }
  328. }
  329. static struct sii8620_mt_msg *sii8620_mt_msg_new(struct sii8620 *ctx)
  330. {
  331. struct sii8620_mt_msg *msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  332. if (!msg)
  333. ctx->error = -ENOMEM;
  334. else
  335. list_add_tail(&msg->node, &ctx->mt_queue);
  336. return msg;
  337. }
  338. static void sii8620_mt_set_cont(struct sii8620 *ctx, sii8620_cb cont)
  339. {
  340. struct sii8620_mt_msg *msg;
  341. if (ctx->error)
  342. return;
  343. if (list_empty(&ctx->mt_queue)) {
  344. ctx->error = -EINVAL;
  345. return;
  346. }
  347. msg = list_last_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
  348. msg->continuation = cont;
  349. }
  350. static void sii8620_mt_msc_cmd(struct sii8620 *ctx, u8 cmd, u8 arg1, u8 arg2)
  351. {
  352. struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
  353. if (!msg)
  354. return;
  355. msg->reg[0] = cmd;
  356. msg->reg[1] = arg1;
  357. msg->reg[2] = arg2;
  358. msg->send = sii8620_mt_msc_cmd_send;
  359. }
  360. static void sii8620_mt_write_stat(struct sii8620 *ctx, u8 reg, u8 val)
  361. {
  362. sii8620_mt_msc_cmd(ctx, MHL_WRITE_STAT, reg, val);
  363. }
  364. static inline void sii8620_mt_set_int(struct sii8620 *ctx, u8 irq, u8 mask)
  365. {
  366. sii8620_mt_msc_cmd(ctx, MHL_SET_INT, irq, mask);
  367. }
  368. static void sii8620_mt_msc_msg(struct sii8620 *ctx, u8 cmd, u8 data)
  369. {
  370. sii8620_mt_msc_cmd(ctx, MHL_MSC_MSG, cmd, data);
  371. }
  372. static void sii8620_mt_rap(struct sii8620 *ctx, u8 code)
  373. {
  374. sii8620_mt_msc_msg(ctx, MHL_MSC_MSG_RAP, code);
  375. }
  376. static void sii8620_mt_rcpk(struct sii8620 *ctx, u8 code)
  377. {
  378. sii8620_mt_msc_msg(ctx, MHL_MSC_MSG_RCPK, code);
  379. }
  380. static void sii8620_mt_rcpe(struct sii8620 *ctx, u8 code)
  381. {
  382. sii8620_mt_msc_msg(ctx, MHL_MSC_MSG_RCPE, code);
  383. }
  384. static void sii8620_mt_read_devcap_send(struct sii8620 *ctx,
  385. struct sii8620_mt_msg *msg)
  386. {
  387. u8 ctrl = BIT_EDID_CTRL_DEVCAP_SELECT_DEVCAP
  388. | BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
  389. | BIT_EDID_CTRL_EDID_MODE_EN;
  390. if (msg->reg[0] == MHL_READ_XDEVCAP)
  391. ctrl |= BIT_EDID_CTRL_XDEVCAP_EN;
  392. sii8620_write_seq(ctx,
  393. REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE,
  394. REG_EDID_CTRL, ctrl,
  395. REG_TPI_CBUS_START, BIT_TPI_CBUS_START_GET_DEVCAP_START
  396. );
  397. }
  398. /* copy src to dst and set changed bits in src */
  399. static void sii8620_update_array(u8 *dst, u8 *src, int count)
  400. {
  401. while (--count >= 0) {
  402. *src ^= *dst;
  403. *dst++ ^= *src++;
  404. }
  405. }
  406. static void sii8620_identify_sink(struct sii8620 *ctx)
  407. {
  408. static const char * const sink_str[] = {
  409. [SINK_NONE] = "NONE",
  410. [SINK_HDMI] = "HDMI",
  411. [SINK_DVI] = "DVI"
  412. };
  413. char sink_name[20];
  414. struct device *dev = ctx->dev;
  415. if (!ctx->sink_detected || !ctx->devcap_read)
  416. return;
  417. sii8620_fetch_edid(ctx);
  418. if (!ctx->edid) {
  419. dev_err(ctx->dev, "Cannot fetch EDID\n");
  420. sii8620_mhl_disconnected(ctx);
  421. return;
  422. }
  423. sii8620_set_upstream_edid(ctx);
  424. if (drm_detect_hdmi_monitor(ctx->edid))
  425. ctx->sink_type = SINK_HDMI;
  426. else
  427. ctx->sink_type = SINK_DVI;
  428. drm_edid_get_monitor_name(ctx->edid, sink_name, ARRAY_SIZE(sink_name));
  429. dev_info(dev, "detected sink(type: %s): %s\n",
  430. sink_str[ctx->sink_type], sink_name);
  431. }
  432. static void sii8620_mr_devcap(struct sii8620 *ctx)
  433. {
  434. u8 dcap[MHL_DCAP_SIZE];
  435. struct device *dev = ctx->dev;
  436. sii8620_read_buf(ctx, REG_EDID_FIFO_RD_DATA, dcap, MHL_DCAP_SIZE);
  437. if (ctx->error < 0)
  438. return;
  439. dev_info(dev, "detected dongle MHL %d.%d, ChipID %02x%02x:%02x%02x\n",
  440. dcap[MHL_DCAP_MHL_VERSION] / 16,
  441. dcap[MHL_DCAP_MHL_VERSION] % 16,
  442. dcap[MHL_DCAP_ADOPTER_ID_H], dcap[MHL_DCAP_ADOPTER_ID_L],
  443. dcap[MHL_DCAP_DEVICE_ID_H], dcap[MHL_DCAP_DEVICE_ID_L]);
  444. sii8620_update_array(ctx->devcap, dcap, MHL_DCAP_SIZE);
  445. ctx->devcap_read = true;
  446. sii8620_identify_sink(ctx);
  447. }
  448. static void sii8620_mr_xdevcap(struct sii8620 *ctx)
  449. {
  450. sii8620_read_buf(ctx, REG_EDID_FIFO_RD_DATA, ctx->xdevcap,
  451. MHL_XDC_SIZE);
  452. }
  453. static void sii8620_mt_read_devcap_recv(struct sii8620 *ctx,
  454. struct sii8620_mt_msg *msg)
  455. {
  456. u8 ctrl = BIT_EDID_CTRL_DEVCAP_SELECT_DEVCAP
  457. | BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
  458. | BIT_EDID_CTRL_EDID_MODE_EN;
  459. if (msg->reg[0] == MHL_READ_XDEVCAP)
  460. ctrl |= BIT_EDID_CTRL_XDEVCAP_EN;
  461. sii8620_write_seq(ctx,
  462. REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE | BIT_INTR9_EDID_DONE
  463. | BIT_INTR9_EDID_ERROR,
  464. REG_EDID_CTRL, ctrl,
  465. REG_EDID_FIFO_ADDR, 0
  466. );
  467. if (msg->reg[0] == MHL_READ_XDEVCAP)
  468. sii8620_mr_xdevcap(ctx);
  469. else
  470. sii8620_mr_devcap(ctx);
  471. }
  472. static void sii8620_mt_read_devcap(struct sii8620 *ctx, bool xdevcap)
  473. {
  474. struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
  475. if (!msg)
  476. return;
  477. msg->reg[0] = xdevcap ? MHL_READ_XDEVCAP : MHL_READ_DEVCAP;
  478. msg->send = sii8620_mt_read_devcap_send;
  479. msg->recv = sii8620_mt_read_devcap_recv;
  480. }
  481. static void sii8620_mt_read_devcap_reg_recv(struct sii8620 *ctx,
  482. struct sii8620_mt_msg *msg)
  483. {
  484. u8 reg = msg->reg[1] & 0x7f;
  485. if (msg->reg[1] & 0x80)
  486. ctx->xdevcap[reg] = msg->ret;
  487. else
  488. ctx->devcap[reg] = msg->ret;
  489. }
  490. static void sii8620_mt_read_devcap_reg(struct sii8620 *ctx, u8 reg)
  491. {
  492. struct sii8620_mt_msg *msg = sii8620_mt_msg_new(ctx);
  493. if (!msg)
  494. return;
  495. msg->reg[0] = (reg & 0x80) ? MHL_READ_XDEVCAP_REG : MHL_READ_DEVCAP_REG;
  496. msg->reg[1] = reg;
  497. msg->send = sii8620_mt_msc_cmd_send;
  498. msg->recv = sii8620_mt_read_devcap_reg_recv;
  499. }
  500. static inline void sii8620_mt_read_xdevcap_reg(struct sii8620 *ctx, u8 reg)
  501. {
  502. sii8620_mt_read_devcap_reg(ctx, reg | 0x80);
  503. }
  504. static void *sii8620_burst_get_tx_buf(struct sii8620 *ctx, int len)
  505. {
  506. u8 *buf = &ctx->burst.tx_buf[ctx->burst.tx_count];
  507. int size = len + 2;
  508. if (ctx->burst.tx_count + size >= ARRAY_SIZE(ctx->burst.tx_buf)) {
  509. dev_err(ctx->dev, "TX-BLK buffer exhausted\n");
  510. ctx->error = -EINVAL;
  511. return NULL;
  512. }
  513. ctx->burst.tx_count += size;
  514. buf[1] = len;
  515. return buf + 2;
  516. }
  517. static u8 *sii8620_burst_get_rx_buf(struct sii8620 *ctx, int len)
  518. {
  519. u8 *buf = &ctx->burst.rx_buf[ctx->burst.rx_count];
  520. int size = len + 1;
  521. if (ctx->burst.rx_count + size >= ARRAY_SIZE(ctx->burst.rx_buf)) {
  522. dev_err(ctx->dev, "RX-BLK buffer exhausted\n");
  523. ctx->error = -EINVAL;
  524. return NULL;
  525. }
  526. ctx->burst.rx_count += size;
  527. buf[0] = len;
  528. return buf + 1;
  529. }
  530. static void sii8620_burst_send(struct sii8620 *ctx)
  531. {
  532. int tx_left = ctx->burst.tx_count;
  533. u8 *d = ctx->burst.tx_buf;
  534. while (tx_left > 0) {
  535. int len = d[1] + 2;
  536. if (ctx->burst.r_count + len > ctx->burst.r_size)
  537. break;
  538. d[0] = min(ctx->burst.rx_ack, 255);
  539. ctx->burst.rx_ack -= d[0];
  540. sii8620_write_buf(ctx, REG_EMSC_XMIT_WRITE_PORT, d, len);
  541. ctx->burst.r_count += len;
  542. tx_left -= len;
  543. d += len;
  544. }
  545. ctx->burst.tx_count = tx_left;
  546. while (ctx->burst.rx_ack > 0) {
  547. u8 b[2] = { min(ctx->burst.rx_ack, 255), 0 };
  548. if (ctx->burst.r_count + 2 > ctx->burst.r_size)
  549. break;
  550. ctx->burst.rx_ack -= b[0];
  551. sii8620_write_buf(ctx, REG_EMSC_XMIT_WRITE_PORT, b, 2);
  552. ctx->burst.r_count += 2;
  553. }
  554. }
  555. static void sii8620_burst_receive(struct sii8620 *ctx)
  556. {
  557. u8 buf[3], *d;
  558. int count;
  559. sii8620_read_buf(ctx, REG_EMSCRFIFOBCNTL, buf, 2);
  560. count = get_unaligned_le16(buf);
  561. while (count > 0) {
  562. int len = min(count, 3);
  563. sii8620_read_buf(ctx, REG_EMSC_RCV_READ_PORT, buf, len);
  564. count -= len;
  565. ctx->burst.rx_ack += len - 1;
  566. ctx->burst.r_count -= buf[1];
  567. if (ctx->burst.r_count < 0)
  568. ctx->burst.r_count = 0;
  569. if (len < 3 || !buf[2])
  570. continue;
  571. len = buf[2];
  572. d = sii8620_burst_get_rx_buf(ctx, len);
  573. if (!d)
  574. continue;
  575. sii8620_read_buf(ctx, REG_EMSC_RCV_READ_PORT, d, len);
  576. count -= len;
  577. ctx->burst.rx_ack += len;
  578. }
  579. }
  580. static void sii8620_burst_tx_rbuf_info(struct sii8620 *ctx, int size)
  581. {
  582. struct mhl_burst_blk_rcv_buffer_info *d =
  583. sii8620_burst_get_tx_buf(ctx, sizeof(*d));
  584. if (!d)
  585. return;
  586. d->id = cpu_to_be16(MHL_BURST_ID_BLK_RCV_BUFFER_INFO);
  587. d->size = cpu_to_le16(size);
  588. }
  589. static u8 sii8620_checksum(void *ptr, int size)
  590. {
  591. u8 *d = ptr, sum = 0;
  592. while (size--)
  593. sum += *d++;
  594. return sum;
  595. }
  596. static void sii8620_mhl_burst_hdr_set(struct mhl3_burst_header *h,
  597. enum mhl_burst_id id)
  598. {
  599. h->id = cpu_to_be16(id);
  600. h->total_entries = 1;
  601. h->sequence_index = 1;
  602. }
  603. static void sii8620_burst_tx_bits_per_pixel_fmt(struct sii8620 *ctx, u8 fmt)
  604. {
  605. struct mhl_burst_bits_per_pixel_fmt *d;
  606. const int size = sizeof(*d) + sizeof(d->desc[0]);
  607. d = sii8620_burst_get_tx_buf(ctx, size);
  608. if (!d)
  609. return;
  610. sii8620_mhl_burst_hdr_set(&d->hdr, MHL_BURST_ID_BITS_PER_PIXEL_FMT);
  611. d->num_entries = 1;
  612. d->desc[0].stream_id = 0;
  613. d->desc[0].pixel_format = fmt;
  614. d->hdr.checksum -= sii8620_checksum(d, size);
  615. }
  616. static void sii8620_burst_rx_all(struct sii8620 *ctx)
  617. {
  618. u8 *d = ctx->burst.rx_buf;
  619. int count = ctx->burst.rx_count;
  620. while (count-- > 0) {
  621. int len = *d++;
  622. int id = get_unaligned_be16(&d[0]);
  623. switch (id) {
  624. case MHL_BURST_ID_BLK_RCV_BUFFER_INFO:
  625. ctx->burst.r_size = get_unaligned_le16(&d[2]);
  626. break;
  627. default:
  628. break;
  629. }
  630. count -= len;
  631. d += len;
  632. }
  633. ctx->burst.rx_count = 0;
  634. }
  635. static void sii8620_fetch_edid(struct sii8620 *ctx)
  636. {
  637. u8 lm_ddc, ddc_cmd, int3, cbus;
  638. unsigned long timeout;
  639. int fetched, i;
  640. int edid_len = EDID_LENGTH;
  641. u8 *edid;
  642. sii8620_readb(ctx, REG_CBUS_STATUS);
  643. lm_ddc = sii8620_readb(ctx, REG_LM_DDC);
  644. ddc_cmd = sii8620_readb(ctx, REG_DDC_CMD);
  645. sii8620_write_seq(ctx,
  646. REG_INTR9_MASK, 0,
  647. REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO,
  648. REG_HDCP2X_POLL_CS, 0x71,
  649. REG_HDCP2X_CTRL_0, BIT_HDCP2X_CTRL_0_HDCP2X_HDCPTX,
  650. REG_LM_DDC, lm_ddc | BIT_LM_DDC_SW_TPI_EN_DISABLED,
  651. );
  652. for (i = 0; i < 256; ++i) {
  653. u8 ddc_stat = sii8620_readb(ctx, REG_DDC_STATUS);
  654. if (!(ddc_stat & BIT_DDC_STATUS_DDC_I2C_IN_PROG))
  655. break;
  656. sii8620_write(ctx, REG_DDC_STATUS,
  657. BIT_DDC_STATUS_DDC_FIFO_EMPTY);
  658. }
  659. sii8620_write(ctx, REG_DDC_ADDR, 0x50 << 1);
  660. edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
  661. if (!edid) {
  662. ctx->error = -ENOMEM;
  663. return;
  664. }
  665. #define FETCH_SIZE 16
  666. for (fetched = 0; fetched < edid_len; fetched += FETCH_SIZE) {
  667. sii8620_readb(ctx, REG_DDC_STATUS);
  668. sii8620_write_seq(ctx,
  669. REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_DDC_CMD_ABORT,
  670. REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_DDC_CMD_CLEAR_FIFO,
  671. REG_DDC_STATUS, BIT_DDC_STATUS_DDC_FIFO_EMPTY
  672. );
  673. sii8620_write_seq(ctx,
  674. REG_DDC_SEGM, fetched >> 8,
  675. REG_DDC_OFFSET, fetched & 0xff,
  676. REG_DDC_DIN_CNT1, FETCH_SIZE,
  677. REG_DDC_DIN_CNT2, 0,
  678. REG_DDC_CMD, ddc_cmd | VAL_DDC_CMD_ENH_DDC_READ_NO_ACK
  679. );
  680. int3 = 0;
  681. timeout = jiffies + msecs_to_jiffies(200);
  682. for (;;) {
  683. cbus = sii8620_readb(ctx, REG_CBUS_STATUS);
  684. if (~cbus & BIT_CBUS_STATUS_CBUS_CONNECTED) {
  685. kfree(edid);
  686. edid = NULL;
  687. goto end;
  688. }
  689. if (int3 & BIT_DDC_CMD_DONE) {
  690. if (sii8620_readb(ctx, REG_DDC_DOUT_CNT)
  691. >= FETCH_SIZE)
  692. break;
  693. } else {
  694. int3 = sii8620_readb(ctx, REG_INTR3);
  695. }
  696. if (time_is_before_jiffies(timeout)) {
  697. ctx->error = -ETIMEDOUT;
  698. dev_err(ctx->dev, "timeout during EDID read\n");
  699. kfree(edid);
  700. edid = NULL;
  701. goto end;
  702. }
  703. usleep_range(10, 20);
  704. }
  705. sii8620_read_buf(ctx, REG_DDC_DATA, edid + fetched, FETCH_SIZE);
  706. if (fetched + FETCH_SIZE == EDID_LENGTH) {
  707. u8 ext = ((struct edid *)edid)->extensions;
  708. if (ext) {
  709. u8 *new_edid;
  710. edid_len += ext * EDID_LENGTH;
  711. new_edid = krealloc(edid, edid_len, GFP_KERNEL);
  712. if (!new_edid) {
  713. kfree(edid);
  714. ctx->error = -ENOMEM;
  715. return;
  716. }
  717. edid = new_edid;
  718. }
  719. }
  720. }
  721. sii8620_write_seq(ctx,
  722. REG_INTR3_MASK, BIT_DDC_CMD_DONE,
  723. REG_LM_DDC, lm_ddc
  724. );
  725. end:
  726. kfree(ctx->edid);
  727. ctx->edid = (struct edid *)edid;
  728. }
  729. static void sii8620_set_upstream_edid(struct sii8620 *ctx)
  730. {
  731. sii8620_setbits(ctx, REG_DPD, BIT_DPD_PDNRX12 | BIT_DPD_PDIDCK_N
  732. | BIT_DPD_PD_MHL_CLK_N, 0xff);
  733. sii8620_write_seq_static(ctx,
  734. REG_RX_HDMI_CTRL3, 0x00,
  735. REG_PKT_FILTER_0, 0xFF,
  736. REG_PKT_FILTER_1, 0xFF,
  737. REG_ALICE0_BW_I2C, 0x06
  738. );
  739. sii8620_setbits(ctx, REG_RX_HDMI_CLR_BUFFER,
  740. BIT_RX_HDMI_CLR_BUFFER_VSI_CLR_EN, 0xff);
  741. sii8620_write_seq_static(ctx,
  742. REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
  743. | BIT_EDID_CTRL_EDID_MODE_EN,
  744. REG_EDID_FIFO_ADDR, 0,
  745. );
  746. sii8620_write_buf(ctx, REG_EDID_FIFO_WR_DATA, (u8 *)ctx->edid,
  747. (ctx->edid->extensions + 1) * EDID_LENGTH);
  748. sii8620_write_seq_static(ctx,
  749. REG_EDID_CTRL, BIT_EDID_CTRL_EDID_PRIME_VALID
  750. | BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO
  751. | BIT_EDID_CTRL_EDID_MODE_EN,
  752. REG_INTR5_MASK, BIT_INTR_SCDT_CHANGE,
  753. REG_INTR9_MASK, 0
  754. );
  755. }
  756. static void sii8620_xtal_set_rate(struct sii8620 *ctx)
  757. {
  758. static const struct {
  759. unsigned int rate;
  760. u8 div;
  761. u8 tp1;
  762. } rates[] = {
  763. { 19200, 0x04, 0x53 },
  764. { 20000, 0x04, 0x62 },
  765. { 24000, 0x05, 0x75 },
  766. { 30000, 0x06, 0x92 },
  767. { 38400, 0x0c, 0xbc },
  768. };
  769. unsigned long rate = clk_get_rate(ctx->clk_xtal) / 1000;
  770. int i;
  771. for (i = 0; i < ARRAY_SIZE(rates) - 1; ++i)
  772. if (rate <= rates[i].rate)
  773. break;
  774. if (rate != rates[i].rate)
  775. dev_err(ctx->dev, "xtal clock rate(%lukHz) not supported, setting MHL for %ukHz.\n",
  776. rate, rates[i].rate);
  777. sii8620_write(ctx, REG_DIV_CTL_MAIN, rates[i].div);
  778. sii8620_write(ctx, REG_HDCP2X_TP1, rates[i].tp1);
  779. }
  780. static int sii8620_hw_on(struct sii8620 *ctx)
  781. {
  782. int ret;
  783. ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  784. if (ret)
  785. return ret;
  786. usleep_range(10000, 20000);
  787. ret = clk_prepare_enable(ctx->clk_xtal);
  788. if (ret)
  789. return ret;
  790. msleep(100);
  791. gpiod_set_value(ctx->gpio_reset, 0);
  792. msleep(100);
  793. return 0;
  794. }
  795. static int sii8620_hw_off(struct sii8620 *ctx)
  796. {
  797. clk_disable_unprepare(ctx->clk_xtal);
  798. gpiod_set_value(ctx->gpio_reset, 1);
  799. return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
  800. }
  801. static void sii8620_cbus_reset(struct sii8620 *ctx)
  802. {
  803. sii8620_write(ctx, REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST
  804. | BIT_PWD_SRST_CBUS_RST_SW_EN);
  805. usleep_range(10000, 20000);
  806. sii8620_write(ctx, REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST_SW_EN);
  807. }
  808. static void sii8620_set_auto_zone(struct sii8620 *ctx)
  809. {
  810. if (ctx->mode != CM_MHL1) {
  811. sii8620_write_seq_static(ctx,
  812. REG_TX_ZONE_CTL1, 0x0,
  813. REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
  814. | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
  815. | BIT_MHL_PLL_CTL0_ZONE_MASK_OE
  816. );
  817. } else {
  818. sii8620_write_seq_static(ctx,
  819. REG_TX_ZONE_CTL1, VAL_TX_ZONE_CTL1_TX_ZONE_CTRL_MODE,
  820. REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
  821. | BIT_MHL_PLL_CTL0_ZONE_MASK_OE
  822. );
  823. }
  824. }
  825. static void sii8620_stop_video(struct sii8620 *ctx)
  826. {
  827. u8 val;
  828. sii8620_write_seq_static(ctx,
  829. REG_TPI_INTR_EN, 0,
  830. REG_HDCP2X_INTR0_MASK, 0,
  831. REG_TPI_COPP_DATA2, 0,
  832. REG_TPI_INTR_ST0, ~0,
  833. );
  834. switch (ctx->sink_type) {
  835. case SINK_DVI:
  836. val = BIT_TPI_SC_REG_TMDS_OE_POWER_DOWN
  837. | BIT_TPI_SC_TPI_AV_MUTE;
  838. break;
  839. case SINK_HDMI:
  840. default:
  841. val = BIT_TPI_SC_REG_TMDS_OE_POWER_DOWN
  842. | BIT_TPI_SC_TPI_AV_MUTE
  843. | BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI;
  844. break;
  845. }
  846. sii8620_write(ctx, REG_TPI_SC, val);
  847. }
  848. static void sii8620_set_format(struct sii8620 *ctx)
  849. {
  850. u8 out_fmt;
  851. if (sii8620_is_mhl3(ctx)) {
  852. sii8620_setbits(ctx, REG_M3_P0CTRL,
  853. BIT_M3_P0CTRL_MHL3_P0_PIXEL_MODE_PACKED,
  854. ctx->use_packed_pixel ? ~0 : 0);
  855. } else {
  856. if (ctx->use_packed_pixel) {
  857. sii8620_write_seq_static(ctx,
  858. REG_VID_MODE, BIT_VID_MODE_M1080P,
  859. REG_MHL_TOP_CTL, BIT_MHL_TOP_CTL_MHL_PP_SEL | 1,
  860. REG_MHLTX_CTL6, 0x60
  861. );
  862. } else {
  863. sii8620_write_seq_static(ctx,
  864. REG_VID_MODE, 0,
  865. REG_MHL_TOP_CTL, 1,
  866. REG_MHLTX_CTL6, 0xa0
  867. );
  868. }
  869. }
  870. if (ctx->use_packed_pixel)
  871. out_fmt = VAL_TPI_FORMAT(YCBCR422, FULL);
  872. else
  873. out_fmt = VAL_TPI_FORMAT(RGB, FULL);
  874. sii8620_write_seq(ctx,
  875. REG_TPI_INPUT, VAL_TPI_FORMAT(RGB, FULL),
  876. REG_TPI_OUTPUT, out_fmt,
  877. );
  878. }
  879. static int mhl3_infoframe_init(struct mhl3_infoframe *frame)
  880. {
  881. memset(frame, 0, sizeof(*frame));
  882. frame->version = 3;
  883. frame->hev_format = -1;
  884. return 0;
  885. }
  886. static ssize_t mhl3_infoframe_pack(struct mhl3_infoframe *frame,
  887. void *buffer, size_t size)
  888. {
  889. const int frm_len = HDMI_INFOFRAME_HEADER_SIZE + MHL3_INFOFRAME_SIZE;
  890. u8 *ptr = buffer;
  891. if (size < frm_len)
  892. return -ENOSPC;
  893. memset(buffer, 0, size);
  894. ptr[0] = HDMI_INFOFRAME_TYPE_VENDOR;
  895. ptr[1] = frame->version;
  896. ptr[2] = MHL3_INFOFRAME_SIZE;
  897. ptr[4] = MHL3_IEEE_OUI & 0xff;
  898. ptr[5] = (MHL3_IEEE_OUI >> 8) & 0xff;
  899. ptr[6] = (MHL3_IEEE_OUI >> 16) & 0xff;
  900. ptr[7] = frame->video_format & 0x3;
  901. ptr[7] |= (frame->format_type & 0x7) << 2;
  902. ptr[7] |= frame->sep_audio ? BIT(5) : 0;
  903. if (frame->hev_format >= 0) {
  904. ptr[9] = 1;
  905. ptr[10] = (frame->hev_format >> 8) & 0xff;
  906. ptr[11] = frame->hev_format & 0xff;
  907. }
  908. if (frame->av_delay) {
  909. bool sign = frame->av_delay < 0;
  910. int delay = sign ? -frame->av_delay : frame->av_delay;
  911. ptr[12] = (delay >> 16) & 0xf;
  912. if (sign)
  913. ptr[12] |= BIT(4);
  914. ptr[13] = (delay >> 8) & 0xff;
  915. ptr[14] = delay & 0xff;
  916. }
  917. ptr[3] -= sii8620_checksum(buffer, frm_len);
  918. return frm_len;
  919. }
  920. static void sii8620_set_infoframes(struct sii8620 *ctx,
  921. struct drm_display_mode *mode)
  922. {
  923. struct mhl3_infoframe mhl_frm;
  924. union hdmi_infoframe frm;
  925. u8 buf[31];
  926. int ret;
  927. ret = drm_hdmi_avi_infoframe_from_display_mode(&frm.avi,
  928. NULL, mode);
  929. if (ctx->use_packed_pixel)
  930. frm.avi.colorspace = HDMI_COLORSPACE_YUV422;
  931. if (!ret)
  932. ret = hdmi_avi_infoframe_pack(&frm.avi, buf, ARRAY_SIZE(buf));
  933. if (ret > 0)
  934. sii8620_write_buf(ctx, REG_TPI_AVI_CHSUM, buf + 3, ret - 3);
  935. if (!sii8620_is_mhl3(ctx) || !ctx->use_packed_pixel) {
  936. sii8620_write(ctx, REG_TPI_SC,
  937. BIT_TPI_SC_TPI_OUTPUT_MODE_0_HDMI);
  938. sii8620_write(ctx, REG_PKT_FILTER_0,
  939. BIT_PKT_FILTER_0_DROP_CEA_GAMUT_PKT |
  940. BIT_PKT_FILTER_0_DROP_MPEG_PKT |
  941. BIT_PKT_FILTER_0_DROP_GCP_PKT,
  942. BIT_PKT_FILTER_1_DROP_GEN_PKT);
  943. return;
  944. }
  945. sii8620_write(ctx, REG_PKT_FILTER_0,
  946. BIT_PKT_FILTER_0_DROP_CEA_GAMUT_PKT |
  947. BIT_PKT_FILTER_0_DROP_MPEG_PKT |
  948. BIT_PKT_FILTER_0_DROP_AVI_PKT |
  949. BIT_PKT_FILTER_0_DROP_GCP_PKT,
  950. BIT_PKT_FILTER_1_VSI_OVERRIDE_DIS |
  951. BIT_PKT_FILTER_1_DROP_GEN_PKT |
  952. BIT_PKT_FILTER_1_DROP_VSIF_PKT);
  953. sii8620_write(ctx, REG_TPI_INFO_FSEL, BIT_TPI_INFO_FSEL_EN
  954. | BIT_TPI_INFO_FSEL_RPT | VAL_TPI_INFO_FSEL_VSI);
  955. ret = mhl3_infoframe_init(&mhl_frm);
  956. if (!ret)
  957. ret = mhl3_infoframe_pack(&mhl_frm, buf, ARRAY_SIZE(buf));
  958. sii8620_write_buf(ctx, REG_TPI_INFO_B0, buf, ret);
  959. }
  960. static void sii8620_start_video(struct sii8620 *ctx)
  961. {
  962. struct drm_display_mode *mode =
  963. &ctx->bridge.encoder->crtc->state->adjusted_mode;
  964. if (!sii8620_is_mhl3(ctx))
  965. sii8620_stop_video(ctx);
  966. if (ctx->sink_type == SINK_DVI && !sii8620_is_mhl3(ctx)) {
  967. sii8620_write(ctx, REG_RX_HDMI_CTRL2,
  968. VAL_RX_HDMI_CTRL2_DEFVAL);
  969. sii8620_write(ctx, REG_TPI_SC, 0);
  970. return;
  971. }
  972. sii8620_write_seq_static(ctx,
  973. REG_RX_HDMI_CTRL2, VAL_RX_HDMI_CTRL2_DEFVAL
  974. | BIT_RX_HDMI_CTRL2_USE_AV_MUTE,
  975. REG_VID_OVRRD, BIT_VID_OVRRD_PP_AUTO_DISABLE
  976. | BIT_VID_OVRRD_M1080P_OVRRD);
  977. sii8620_set_format(ctx);
  978. if (!sii8620_is_mhl3(ctx)) {
  979. u8 link_mode = MHL_DST_LM_PATH_ENABLED;
  980. if (ctx->use_packed_pixel)
  981. link_mode |= MHL_DST_LM_CLK_MODE_PACKED_PIXEL;
  982. else
  983. link_mode |= MHL_DST_LM_CLK_MODE_NORMAL;
  984. sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE), link_mode);
  985. sii8620_set_auto_zone(ctx);
  986. } else {
  987. static const struct {
  988. int max_clk;
  989. u8 zone;
  990. u8 link_rate;
  991. u8 rrp_decode;
  992. } clk_spec[] = {
  993. { 150000, VAL_TX_ZONE_CTL3_TX_ZONE_1_5GBPS,
  994. MHL_XDS_LINK_RATE_1_5_GBPS, 0x38 },
  995. { 300000, VAL_TX_ZONE_CTL3_TX_ZONE_3GBPS,
  996. MHL_XDS_LINK_RATE_3_0_GBPS, 0x40 },
  997. { 600000, VAL_TX_ZONE_CTL3_TX_ZONE_6GBPS,
  998. MHL_XDS_LINK_RATE_6_0_GBPS, 0x40 },
  999. };
  1000. u8 p0_ctrl = BIT_M3_P0CTRL_MHL3_P0_PORT_EN;
  1001. int clk = mode->clock * (ctx->use_packed_pixel ? 2 : 3);
  1002. int i;
  1003. for (i = 0; i < ARRAY_SIZE(clk_spec) - 1; ++i)
  1004. if (clk < clk_spec[i].max_clk)
  1005. break;
  1006. if (100 * clk >= 98 * clk_spec[i].max_clk)
  1007. p0_ctrl |= BIT_M3_P0CTRL_MHL3_P0_UNLIMIT_EN;
  1008. sii8620_burst_tx_bits_per_pixel_fmt(ctx, ctx->use_packed_pixel);
  1009. sii8620_burst_send(ctx);
  1010. sii8620_write_seq(ctx,
  1011. REG_MHL_DP_CTL0, 0xf0,
  1012. REG_MHL3_TX_ZONE_CTL, clk_spec[i].zone);
  1013. sii8620_setbits(ctx, REG_M3_P0CTRL,
  1014. BIT_M3_P0CTRL_MHL3_P0_PORT_EN
  1015. | BIT_M3_P0CTRL_MHL3_P0_UNLIMIT_EN, p0_ctrl);
  1016. sii8620_setbits(ctx, REG_M3_POSTM, MSK_M3_POSTM_RRP_DECODE,
  1017. clk_spec[i].rrp_decode);
  1018. sii8620_write_seq_static(ctx,
  1019. REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE
  1020. | BIT_M3_CTRL_H2M_SWRST,
  1021. REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE
  1022. );
  1023. sii8620_mt_write_stat(ctx, MHL_XDS_REG(AVLINK_MODE_CONTROL),
  1024. clk_spec[i].link_rate);
  1025. }
  1026. sii8620_set_infoframes(ctx, mode);
  1027. }
  1028. static void sii8620_disable_hpd(struct sii8620 *ctx)
  1029. {
  1030. sii8620_setbits(ctx, REG_EDID_CTRL, BIT_EDID_CTRL_EDID_PRIME_VALID, 0);
  1031. sii8620_write_seq_static(ctx,
  1032. REG_HPD_CTRL, BIT_HPD_CTRL_HPD_OUT_OVR_EN,
  1033. REG_INTR8_MASK, 0
  1034. );
  1035. }
  1036. static void sii8620_enable_hpd(struct sii8620 *ctx)
  1037. {
  1038. sii8620_setbits(ctx, REG_TMDS_CSTAT_P3,
  1039. BIT_TMDS_CSTAT_P3_SCDT_CLR_AVI_DIS
  1040. | BIT_TMDS_CSTAT_P3_CLR_AVI, ~0);
  1041. sii8620_write_seq_static(ctx,
  1042. REG_HPD_CTRL, BIT_HPD_CTRL_HPD_OUT_OVR_EN
  1043. | BIT_HPD_CTRL_HPD_HIGH,
  1044. );
  1045. }
  1046. static void sii8620_mhl_discover(struct sii8620 *ctx)
  1047. {
  1048. sii8620_write_seq_static(ctx,
  1049. REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
  1050. | BIT_DISC_CTRL9_DISC_PULSE_PROCEED,
  1051. REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_5K, VAL_PUP_20K),
  1052. REG_CBUS_DISC_INTR0_MASK, BIT_MHL3_EST_INT
  1053. | BIT_MHL_EST_INT
  1054. | BIT_NOT_MHL_EST_INT
  1055. | BIT_CBUS_MHL3_DISCON_INT
  1056. | BIT_CBUS_MHL12_DISCON_INT
  1057. | BIT_RGND_READY_INT,
  1058. REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
  1059. | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
  1060. | BIT_MHL_PLL_CTL0_ZONE_MASK_OE,
  1061. REG_MHL_DP_CTL0, BIT_MHL_DP_CTL0_DP_OE
  1062. | BIT_MHL_DP_CTL0_TX_OE_OVR,
  1063. REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE,
  1064. REG_MHL_DP_CTL1, 0xA2,
  1065. REG_MHL_DP_CTL2, 0x03,
  1066. REG_MHL_DP_CTL3, 0x35,
  1067. REG_MHL_DP_CTL5, 0x02,
  1068. REG_MHL_DP_CTL6, 0x02,
  1069. REG_MHL_DP_CTL7, 0x03,
  1070. REG_COC_CTLC, 0xFF,
  1071. REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12
  1072. | BIT_DPD_OSC_EN | BIT_DPD_PWRON_HSIC,
  1073. REG_COC_INTR_MASK, BIT_COC_PLL_LOCK_STATUS_CHANGE
  1074. | BIT_COC_CALIBRATION_DONE,
  1075. REG_CBUS_INT_1_MASK, BIT_CBUS_MSC_ABORT_RCVD
  1076. | BIT_CBUS_CMD_ABORT,
  1077. REG_CBUS_INT_0_MASK, BIT_CBUS_MSC_MT_DONE
  1078. | BIT_CBUS_HPD_CHG
  1079. | BIT_CBUS_MSC_MR_WRITE_STAT
  1080. | BIT_CBUS_MSC_MR_MSC_MSG
  1081. | BIT_CBUS_MSC_MR_WRITE_BURST
  1082. | BIT_CBUS_MSC_MR_SET_INT
  1083. | BIT_CBUS_MSC_MT_DONE_NACK
  1084. );
  1085. }
  1086. static void sii8620_peer_specific_init(struct sii8620 *ctx)
  1087. {
  1088. if (sii8620_is_mhl3(ctx))
  1089. sii8620_write_seq_static(ctx,
  1090. REG_SYS_CTRL1, BIT_SYS_CTRL1_BLOCK_DDC_BY_HPD,
  1091. REG_EMSCINTRMASK1,
  1092. BIT_EMSCINTR1_EMSC_TRAINING_COMMA_ERR
  1093. );
  1094. else
  1095. sii8620_write_seq_static(ctx,
  1096. REG_HDCP2X_INTR0_MASK, 0x00,
  1097. REG_EMSCINTRMASK1, 0x00,
  1098. REG_HDCP2X_INTR0, 0xFF,
  1099. REG_INTR1, 0xFF,
  1100. REG_SYS_CTRL1, BIT_SYS_CTRL1_BLOCK_DDC_BY_HPD
  1101. | BIT_SYS_CTRL1_TX_CTRL_HDMI
  1102. );
  1103. }
  1104. #define SII8620_MHL_VERSION 0x32
  1105. #define SII8620_SCRATCHPAD_SIZE 16
  1106. #define SII8620_INT_STAT_SIZE 0x33
  1107. static void sii8620_set_dev_cap(struct sii8620 *ctx)
  1108. {
  1109. static const u8 devcap[MHL_DCAP_SIZE] = {
  1110. [MHL_DCAP_MHL_VERSION] = SII8620_MHL_VERSION,
  1111. [MHL_DCAP_CAT] = MHL_DCAP_CAT_SOURCE | MHL_DCAP_CAT_POWER,
  1112. [MHL_DCAP_ADOPTER_ID_H] = 0x01,
  1113. [MHL_DCAP_ADOPTER_ID_L] = 0x41,
  1114. [MHL_DCAP_VID_LINK_MODE] = MHL_DCAP_VID_LINK_RGB444
  1115. | MHL_DCAP_VID_LINK_PPIXEL
  1116. | MHL_DCAP_VID_LINK_16BPP,
  1117. [MHL_DCAP_AUD_LINK_MODE] = MHL_DCAP_AUD_LINK_2CH,
  1118. [MHL_DCAP_VIDEO_TYPE] = MHL_DCAP_VT_GRAPHICS,
  1119. [MHL_DCAP_LOG_DEV_MAP] = MHL_DCAP_LD_GUI,
  1120. [MHL_DCAP_BANDWIDTH] = 0x0f,
  1121. [MHL_DCAP_FEATURE_FLAG] = MHL_DCAP_FEATURE_RCP_SUPPORT
  1122. | MHL_DCAP_FEATURE_RAP_SUPPORT
  1123. | MHL_DCAP_FEATURE_SP_SUPPORT,
  1124. [MHL_DCAP_SCRATCHPAD_SIZE] = SII8620_SCRATCHPAD_SIZE,
  1125. [MHL_DCAP_INT_STAT_SIZE] = SII8620_INT_STAT_SIZE,
  1126. };
  1127. static const u8 xdcap[MHL_XDC_SIZE] = {
  1128. [MHL_XDC_ECBUS_SPEEDS] = MHL_XDC_ECBUS_S_075
  1129. | MHL_XDC_ECBUS_S_8BIT,
  1130. [MHL_XDC_TMDS_SPEEDS] = MHL_XDC_TMDS_150
  1131. | MHL_XDC_TMDS_300 | MHL_XDC_TMDS_600,
  1132. [MHL_XDC_ECBUS_ROLES] = MHL_XDC_DEV_HOST,
  1133. [MHL_XDC_LOG_DEV_MAPX] = MHL_XDC_LD_PHONE,
  1134. };
  1135. sii8620_write_buf(ctx, REG_MHL_DEVCAP_0, devcap, ARRAY_SIZE(devcap));
  1136. sii8620_write_buf(ctx, REG_MHL_EXTDEVCAP_0, xdcap, ARRAY_SIZE(xdcap));
  1137. }
  1138. static void sii8620_mhl_init(struct sii8620 *ctx)
  1139. {
  1140. sii8620_write_seq_static(ctx,
  1141. REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
  1142. REG_CBUS_MSC_COMPAT_CTRL,
  1143. BIT_CBUS_MSC_COMPAT_CTRL_XDEVCAP_EN,
  1144. );
  1145. sii8620_peer_specific_init(ctx);
  1146. sii8620_disable_hpd(ctx);
  1147. sii8620_write_seq_static(ctx,
  1148. REG_EDID_CTRL, BIT_EDID_CTRL_EDID_FIFO_ADDR_AUTO,
  1149. REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
  1150. | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
  1151. REG_TMDS0_CCTRL1, 0x90,
  1152. REG_TMDS_CLK_EN, 0x01,
  1153. REG_TMDS_CH_EN, 0x11,
  1154. REG_BGR_BIAS, 0x87,
  1155. REG_ALICE0_ZONE_CTRL, 0xE8,
  1156. REG_ALICE0_MODE_CTRL, 0x04,
  1157. );
  1158. sii8620_setbits(ctx, REG_LM_DDC, BIT_LM_DDC_SW_TPI_EN_DISABLED, 0);
  1159. sii8620_write_seq_static(ctx,
  1160. REG_TPI_HW_OPT3, 0x76,
  1161. REG_TMDS_CCTRL, BIT_TMDS_CCTRL_TMDS_OE,
  1162. REG_TPI_DTD_B2, 79,
  1163. );
  1164. sii8620_set_dev_cap(ctx);
  1165. sii8620_write_seq_static(ctx,
  1166. REG_MDT_XMIT_TIMEOUT, 100,
  1167. REG_MDT_XMIT_CTRL, 0x03,
  1168. REG_MDT_XFIFO_STAT, 0x00,
  1169. REG_MDT_RCV_TIMEOUT, 100,
  1170. REG_CBUS_LINK_CTRL_8, 0x1D,
  1171. );
  1172. sii8620_start_gen2_write_burst(ctx);
  1173. sii8620_write_seq_static(ctx,
  1174. REG_BIST_CTRL, 0x00,
  1175. REG_COC_CTL1, 0x10,
  1176. REG_COC_CTL2, 0x18,
  1177. REG_COC_CTLF, 0x07,
  1178. REG_COC_CTL11, 0xF8,
  1179. REG_COC_CTL17, 0x61,
  1180. REG_COC_CTL18, 0x46,
  1181. REG_COC_CTL19, 0x15,
  1182. REG_COC_CTL1A, 0x01,
  1183. REG_MHL_COC_CTL3, BIT_MHL_COC_CTL3_COC_AECHO_EN,
  1184. REG_MHL_COC_CTL4, 0x2D,
  1185. REG_MHL_COC_CTL5, 0xF9,
  1186. REG_MSC_HEARTBEAT_CTRL, 0x27,
  1187. );
  1188. sii8620_disable_gen2_write_burst(ctx);
  1189. sii8620_mt_write_stat(ctx, MHL_DST_REG(VERSION), SII8620_MHL_VERSION);
  1190. sii8620_mt_write_stat(ctx, MHL_DST_REG(CONNECTED_RDY),
  1191. MHL_DST_CONN_DCAP_RDY | MHL_DST_CONN_XDEVCAPP_SUPP
  1192. | MHL_DST_CONN_POW_STAT);
  1193. sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE), MHL_INT_RC_DCAP_CHG);
  1194. }
  1195. static void sii8620_emsc_enable(struct sii8620 *ctx)
  1196. {
  1197. u8 reg;
  1198. sii8620_setbits(ctx, REG_GENCTL, BIT_GENCTL_EMSC_EN
  1199. | BIT_GENCTL_CLR_EMSC_RFIFO
  1200. | BIT_GENCTL_CLR_EMSC_XFIFO, ~0);
  1201. sii8620_setbits(ctx, REG_GENCTL, BIT_GENCTL_CLR_EMSC_RFIFO
  1202. | BIT_GENCTL_CLR_EMSC_XFIFO, 0);
  1203. sii8620_setbits(ctx, REG_COMMECNT, BIT_COMMECNT_I2C_TO_EMSC_EN, ~0);
  1204. reg = sii8620_readb(ctx, REG_EMSCINTR);
  1205. sii8620_write(ctx, REG_EMSCINTR, reg);
  1206. sii8620_write(ctx, REG_EMSCINTRMASK, BIT_EMSCINTR_SPI_DVLD);
  1207. }
  1208. static int sii8620_wait_for_fsm_state(struct sii8620 *ctx, u8 state)
  1209. {
  1210. int i;
  1211. for (i = 0; i < 10; ++i) {
  1212. u8 s = sii8620_readb(ctx, REG_COC_STAT_0);
  1213. if ((s & MSK_COC_STAT_0_FSM_STATE) == state)
  1214. return 0;
  1215. if (!(s & BIT_COC_STAT_0_PLL_LOCKED))
  1216. return -EBUSY;
  1217. usleep_range(4000, 6000);
  1218. }
  1219. return -ETIMEDOUT;
  1220. }
  1221. static void sii8620_set_mode(struct sii8620 *ctx, enum sii8620_mode mode)
  1222. {
  1223. int ret;
  1224. if (ctx->mode == mode)
  1225. return;
  1226. switch (mode) {
  1227. case CM_MHL1:
  1228. sii8620_write_seq_static(ctx,
  1229. REG_CBUS_MSC_COMPAT_CTRL, 0x02,
  1230. REG_M3_CTRL, VAL_M3_CTRL_MHL1_2_VALUE,
  1231. REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12
  1232. | BIT_DPD_OSC_EN,
  1233. REG_COC_INTR_MASK, 0
  1234. );
  1235. ctx->mode = mode;
  1236. break;
  1237. case CM_MHL3:
  1238. sii8620_write(ctx, REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE);
  1239. ctx->mode = mode;
  1240. return;
  1241. case CM_ECBUS_S:
  1242. sii8620_emsc_enable(ctx);
  1243. sii8620_write_seq_static(ctx,
  1244. REG_TTXSPINUMS, 4,
  1245. REG_TRXSPINUMS, 4,
  1246. REG_TTXHSICNUMS, 0x14,
  1247. REG_TRXHSICNUMS, 0x14,
  1248. REG_TTXTOTNUMS, 0x18,
  1249. REG_TRXTOTNUMS, 0x18,
  1250. REG_PWD_SRST, BIT_PWD_SRST_COC_DOC_RST
  1251. | BIT_PWD_SRST_CBUS_RST_SW_EN,
  1252. REG_MHL_COC_CTL1, 0xbd,
  1253. REG_PWD_SRST, BIT_PWD_SRST_CBUS_RST_SW_EN,
  1254. REG_COC_CTLB, 0x01,
  1255. REG_COC_CTL0, 0x5c,
  1256. REG_COC_CTL14, 0x03,
  1257. REG_COC_CTL15, 0x80,
  1258. REG_MHL_DP_CTL6, BIT_MHL_DP_CTL6_DP_TAP1_SGN
  1259. | BIT_MHL_DP_CTL6_DP_TAP1_EN
  1260. | BIT_MHL_DP_CTL6_DT_PREDRV_FEEDCAP_EN,
  1261. REG_MHL_DP_CTL8, 0x03
  1262. );
  1263. ret = sii8620_wait_for_fsm_state(ctx, 0x03);
  1264. sii8620_write_seq_static(ctx,
  1265. REG_COC_CTL14, 0x00,
  1266. REG_COC_CTL15, 0x80
  1267. );
  1268. if (!ret)
  1269. sii8620_write(ctx, REG_CBUS3_CNVT, 0x85);
  1270. else
  1271. sii8620_disconnect(ctx);
  1272. return;
  1273. case CM_DISCONNECTED:
  1274. ctx->mode = mode;
  1275. break;
  1276. default:
  1277. dev_err(ctx->dev, "%s mode %d not supported\n", __func__, mode);
  1278. break;
  1279. }
  1280. sii8620_set_auto_zone(ctx);
  1281. if (mode != CM_MHL1)
  1282. return;
  1283. sii8620_write_seq_static(ctx,
  1284. REG_MHL_DP_CTL0, 0xBC,
  1285. REG_MHL_DP_CTL1, 0xBB,
  1286. REG_MHL_DP_CTL3, 0x48,
  1287. REG_MHL_DP_CTL5, 0x39,
  1288. REG_MHL_DP_CTL2, 0x2A,
  1289. REG_MHL_DP_CTL6, 0x2A,
  1290. REG_MHL_DP_CTL7, 0x08
  1291. );
  1292. }
  1293. static void sii8620_hpd_unplugged(struct sii8620 *ctx)
  1294. {
  1295. sii8620_disable_hpd(ctx);
  1296. ctx->sink_type = SINK_NONE;
  1297. ctx->sink_detected = false;
  1298. ctx->feature_complete = false;
  1299. kfree(ctx->edid);
  1300. ctx->edid = NULL;
  1301. }
  1302. static void sii8620_disconnect(struct sii8620 *ctx)
  1303. {
  1304. sii8620_disable_gen2_write_burst(ctx);
  1305. sii8620_stop_video(ctx);
  1306. msleep(100);
  1307. sii8620_cbus_reset(ctx);
  1308. sii8620_set_mode(ctx, CM_DISCONNECTED);
  1309. sii8620_write_seq_static(ctx,
  1310. REG_TX_ZONE_CTL1, 0,
  1311. REG_MHL_PLL_CTL0, 0x07,
  1312. REG_COC_CTL0, 0x40,
  1313. REG_CBUS3_CNVT, 0x84,
  1314. REG_COC_CTL14, 0x00,
  1315. REG_COC_CTL0, 0x40,
  1316. REG_HRXCTRL3, 0x07,
  1317. REG_MHL_PLL_CTL0, VAL_MHL_PLL_CTL0_HDMI_CLK_RATIO_1X
  1318. | BIT_MHL_PLL_CTL0_CRYSTAL_CLK_SEL
  1319. | BIT_MHL_PLL_CTL0_ZONE_MASK_OE,
  1320. REG_MHL_DP_CTL0, BIT_MHL_DP_CTL0_DP_OE
  1321. | BIT_MHL_DP_CTL0_TX_OE_OVR,
  1322. REG_MHL_DP_CTL1, 0xBB,
  1323. REG_MHL_DP_CTL3, 0x48,
  1324. REG_MHL_DP_CTL5, 0x3F,
  1325. REG_MHL_DP_CTL2, 0x2F,
  1326. REG_MHL_DP_CTL6, 0x2A,
  1327. REG_MHL_DP_CTL7, 0x03
  1328. );
  1329. sii8620_hpd_unplugged(ctx);
  1330. sii8620_write_seq_static(ctx,
  1331. REG_M3_CTRL, VAL_M3_CTRL_MHL3_VALUE,
  1332. REG_MHL_COC_CTL1, 0x07,
  1333. REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
  1334. REG_DISC_CTRL8, 0x00,
  1335. REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
  1336. | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
  1337. REG_INT_CTRL, 0x00,
  1338. REG_MSC_HEARTBEAT_CTRL, 0x27,
  1339. REG_DISC_CTRL1, 0x25,
  1340. REG_CBUS_DISC_INTR0, (u8)~BIT_RGND_READY_INT,
  1341. REG_CBUS_DISC_INTR0_MASK, BIT_RGND_READY_INT,
  1342. REG_MDT_INT_1, 0xff,
  1343. REG_MDT_INT_1_MASK, 0x00,
  1344. REG_MDT_INT_0, 0xff,
  1345. REG_MDT_INT_0_MASK, 0x00,
  1346. REG_COC_INTR, 0xff,
  1347. REG_COC_INTR_MASK, 0x00,
  1348. REG_TRXINTH, 0xff,
  1349. REG_TRXINTMH, 0x00,
  1350. REG_CBUS_INT_0, 0xff,
  1351. REG_CBUS_INT_0_MASK, 0x00,
  1352. REG_CBUS_INT_1, 0xff,
  1353. REG_CBUS_INT_1_MASK, 0x00,
  1354. REG_EMSCINTR, 0xff,
  1355. REG_EMSCINTRMASK, 0x00,
  1356. REG_EMSCINTR1, 0xff,
  1357. REG_EMSCINTRMASK1, 0x00,
  1358. REG_INTR8, 0xff,
  1359. REG_INTR8_MASK, 0x00,
  1360. REG_TPI_INTR_ST0, 0xff,
  1361. REG_TPI_INTR_EN, 0x00,
  1362. REG_HDCP2X_INTR0, 0xff,
  1363. REG_HDCP2X_INTR0_MASK, 0x00,
  1364. REG_INTR9, 0xff,
  1365. REG_INTR9_MASK, 0x00,
  1366. REG_INTR3, 0xff,
  1367. REG_INTR3_MASK, 0x00,
  1368. REG_INTR5, 0xff,
  1369. REG_INTR5_MASK, 0x00,
  1370. REG_INTR2, 0xff,
  1371. REG_INTR2_MASK, 0x00,
  1372. );
  1373. memset(ctx->stat, 0, sizeof(ctx->stat));
  1374. memset(ctx->xstat, 0, sizeof(ctx->xstat));
  1375. memset(ctx->devcap, 0, sizeof(ctx->devcap));
  1376. memset(ctx->xdevcap, 0, sizeof(ctx->xdevcap));
  1377. ctx->devcap_read = false;
  1378. ctx->cbus_status = 0;
  1379. sii8620_mt_cleanup(ctx);
  1380. }
  1381. static void sii8620_mhl_disconnected(struct sii8620 *ctx)
  1382. {
  1383. sii8620_write_seq_static(ctx,
  1384. REG_DISC_CTRL4, VAL_DISC_CTRL4(VAL_PUP_OFF, VAL_PUP_20K),
  1385. REG_CBUS_MSC_COMPAT_CTRL,
  1386. BIT_CBUS_MSC_COMPAT_CTRL_XDEVCAP_EN
  1387. );
  1388. sii8620_disconnect(ctx);
  1389. }
  1390. static void sii8620_irq_disc(struct sii8620 *ctx)
  1391. {
  1392. u8 stat = sii8620_readb(ctx, REG_CBUS_DISC_INTR0);
  1393. if (stat & VAL_CBUS_MHL_DISCON)
  1394. sii8620_mhl_disconnected(ctx);
  1395. if (stat & BIT_RGND_READY_INT) {
  1396. u8 stat2 = sii8620_readb(ctx, REG_DISC_STAT2);
  1397. if ((stat2 & MSK_DISC_STAT2_RGND) == VAL_RGND_1K) {
  1398. sii8620_mhl_discover(ctx);
  1399. } else {
  1400. sii8620_write_seq_static(ctx,
  1401. REG_DISC_CTRL9, BIT_DISC_CTRL9_WAKE_DRVFLT
  1402. | BIT_DISC_CTRL9_NOMHL_EST
  1403. | BIT_DISC_CTRL9_WAKE_PULSE_BYPASS,
  1404. REG_CBUS_DISC_INTR0_MASK, BIT_RGND_READY_INT
  1405. | BIT_CBUS_MHL3_DISCON_INT
  1406. | BIT_CBUS_MHL12_DISCON_INT
  1407. | BIT_NOT_MHL_EST_INT
  1408. );
  1409. }
  1410. }
  1411. if (stat & BIT_MHL_EST_INT)
  1412. sii8620_mhl_init(ctx);
  1413. sii8620_write(ctx, REG_CBUS_DISC_INTR0, stat);
  1414. }
  1415. static void sii8620_read_burst(struct sii8620 *ctx)
  1416. {
  1417. u8 buf[17];
  1418. sii8620_read_buf(ctx, REG_MDT_RCV_READ_PORT, buf, ARRAY_SIZE(buf));
  1419. sii8620_write(ctx, REG_MDT_RCV_CTRL, BIT_MDT_RCV_CTRL_MDT_RCV_EN |
  1420. BIT_MDT_RCV_CTRL_MDT_DELAY_RCV_EN |
  1421. BIT_MDT_RCV_CTRL_MDT_RFIFO_CLR_CUR);
  1422. sii8620_readb(ctx, REG_MDT_RFIFO_STAT);
  1423. }
  1424. static void sii8620_irq_g2wb(struct sii8620 *ctx)
  1425. {
  1426. u8 stat = sii8620_readb(ctx, REG_MDT_INT_0);
  1427. if (stat & BIT_MDT_IDLE_AFTER_HAWB_DISABLE)
  1428. if (sii8620_is_mhl3(ctx))
  1429. sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE),
  1430. MHL_INT_RC_FEAT_COMPLETE);
  1431. if (stat & BIT_MDT_RFIFO_DATA_RDY)
  1432. sii8620_read_burst(ctx);
  1433. if (stat & BIT_MDT_XFIFO_EMPTY)
  1434. sii8620_write(ctx, REG_MDT_XMIT_CTRL, 0);
  1435. sii8620_write(ctx, REG_MDT_INT_0, stat);
  1436. }
  1437. static void sii8620_status_dcap_ready(struct sii8620 *ctx)
  1438. {
  1439. enum sii8620_mode mode;
  1440. mode = ctx->stat[MHL_DST_VERSION] >= 0x30 ? CM_MHL3 : CM_MHL1;
  1441. if (mode > ctx->mode)
  1442. sii8620_set_mode(ctx, mode);
  1443. sii8620_peer_specific_init(ctx);
  1444. sii8620_write(ctx, REG_INTR9_MASK, BIT_INTR9_DEVCAP_DONE
  1445. | BIT_INTR9_EDID_DONE | BIT_INTR9_EDID_ERROR);
  1446. }
  1447. static void sii8620_status_changed_path(struct sii8620 *ctx)
  1448. {
  1449. u8 link_mode;
  1450. if (ctx->use_packed_pixel)
  1451. link_mode = MHL_DST_LM_CLK_MODE_PACKED_PIXEL;
  1452. else
  1453. link_mode = MHL_DST_LM_CLK_MODE_NORMAL;
  1454. if (ctx->stat[MHL_DST_LINK_MODE] & MHL_DST_LM_PATH_ENABLED)
  1455. link_mode |= MHL_DST_LM_PATH_ENABLED;
  1456. sii8620_mt_write_stat(ctx, MHL_DST_REG(LINK_MODE),
  1457. link_mode);
  1458. }
  1459. static void sii8620_msc_mr_write_stat(struct sii8620 *ctx)
  1460. {
  1461. u8 st[MHL_DST_SIZE], xst[MHL_XDS_SIZE];
  1462. sii8620_read_buf(ctx, REG_MHL_STAT_0, st, MHL_DST_SIZE);
  1463. sii8620_read_buf(ctx, REG_MHL_EXTSTAT_0, xst, MHL_XDS_SIZE);
  1464. sii8620_update_array(ctx->stat, st, MHL_DST_SIZE);
  1465. sii8620_update_array(ctx->xstat, xst, MHL_XDS_SIZE);
  1466. if (ctx->stat[MHL_DST_CONNECTED_RDY] & st[MHL_DST_CONNECTED_RDY] &
  1467. MHL_DST_CONN_DCAP_RDY) {
  1468. sii8620_status_dcap_ready(ctx);
  1469. if (!sii8620_is_mhl3(ctx))
  1470. sii8620_mt_read_devcap(ctx, false);
  1471. }
  1472. if (st[MHL_DST_LINK_MODE] & MHL_DST_LM_PATH_ENABLED)
  1473. sii8620_status_changed_path(ctx);
  1474. }
  1475. static void sii8620_ecbus_up(struct sii8620 *ctx, int ret)
  1476. {
  1477. if (ret < 0)
  1478. return;
  1479. sii8620_set_mode(ctx, CM_ECBUS_S);
  1480. }
  1481. static void sii8620_got_ecbus_speed(struct sii8620 *ctx, int ret)
  1482. {
  1483. if (ret < 0)
  1484. return;
  1485. sii8620_mt_write_stat(ctx, MHL_XDS_REG(CURR_ECBUS_MODE),
  1486. MHL_XDS_ECBUS_S | MHL_XDS_SLOT_MODE_8BIT);
  1487. sii8620_mt_rap(ctx, MHL_RAP_CBUS_MODE_UP);
  1488. sii8620_mt_set_cont(ctx, sii8620_ecbus_up);
  1489. }
  1490. static void sii8620_mhl_burst_emsc_support_set(struct mhl_burst_emsc_support *d,
  1491. enum mhl_burst_id id)
  1492. {
  1493. sii8620_mhl_burst_hdr_set(&d->hdr, MHL_BURST_ID_EMSC_SUPPORT);
  1494. d->num_entries = 1;
  1495. d->burst_id[0] = cpu_to_be16(id);
  1496. }
  1497. static void sii8620_send_features(struct sii8620 *ctx)
  1498. {
  1499. u8 buf[16];
  1500. sii8620_write(ctx, REG_MDT_XMIT_CTRL, BIT_MDT_XMIT_CTRL_EN
  1501. | BIT_MDT_XMIT_CTRL_FIXED_BURST_LEN);
  1502. sii8620_mhl_burst_emsc_support_set((void *)buf,
  1503. MHL_BURST_ID_HID_PAYLOAD);
  1504. sii8620_write_buf(ctx, REG_MDT_XMIT_WRITE_PORT, buf, ARRAY_SIZE(buf));
  1505. }
  1506. static bool sii8620_rcp_consume(struct sii8620 *ctx, u8 scancode)
  1507. {
  1508. bool pressed = !(scancode & MHL_RCP_KEY_RELEASED_MASK);
  1509. scancode &= MHL_RCP_KEY_ID_MASK;
  1510. if (!IS_ENABLED(CONFIG_RC_CORE) || !ctx->rc_dev)
  1511. return false;
  1512. if (pressed)
  1513. rc_keydown(ctx->rc_dev, RC_PROTO_CEC, scancode, 0);
  1514. else
  1515. rc_keyup(ctx->rc_dev);
  1516. return true;
  1517. }
  1518. static void sii8620_msc_mr_set_int(struct sii8620 *ctx)
  1519. {
  1520. u8 ints[MHL_INT_SIZE];
  1521. sii8620_read_buf(ctx, REG_MHL_INT_0, ints, MHL_INT_SIZE);
  1522. sii8620_write_buf(ctx, REG_MHL_INT_0, ints, MHL_INT_SIZE);
  1523. if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_DCAP_CHG) {
  1524. switch (ctx->mode) {
  1525. case CM_MHL3:
  1526. sii8620_mt_read_xdevcap_reg(ctx, MHL_XDC_ECBUS_SPEEDS);
  1527. sii8620_mt_set_cont(ctx, sii8620_got_ecbus_speed);
  1528. break;
  1529. case CM_ECBUS_S:
  1530. sii8620_mt_read_devcap(ctx, true);
  1531. break;
  1532. default:
  1533. break;
  1534. }
  1535. }
  1536. if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_FEAT_REQ)
  1537. sii8620_send_features(ctx);
  1538. if (ints[MHL_INT_RCHANGE] & MHL_INT_RC_FEAT_COMPLETE) {
  1539. ctx->feature_complete = true;
  1540. if (ctx->edid)
  1541. sii8620_enable_hpd(ctx);
  1542. }
  1543. }
  1544. static struct sii8620_mt_msg *sii8620_msc_msg_first(struct sii8620 *ctx)
  1545. {
  1546. struct device *dev = ctx->dev;
  1547. if (list_empty(&ctx->mt_queue)) {
  1548. dev_err(dev, "unexpected MSC MT response\n");
  1549. return NULL;
  1550. }
  1551. return list_first_entry(&ctx->mt_queue, struct sii8620_mt_msg, node);
  1552. }
  1553. static void sii8620_msc_mt_done(struct sii8620 *ctx)
  1554. {
  1555. struct sii8620_mt_msg *msg = sii8620_msc_msg_first(ctx);
  1556. if (!msg)
  1557. return;
  1558. msg->ret = sii8620_readb(ctx, REG_MSC_MT_RCVD_DATA0);
  1559. ctx->mt_state = MT_STATE_DONE;
  1560. }
  1561. static void sii8620_msc_mr_msc_msg(struct sii8620 *ctx)
  1562. {
  1563. struct sii8620_mt_msg *msg;
  1564. u8 buf[2];
  1565. sii8620_read_buf(ctx, REG_MSC_MR_MSC_MSG_RCVD_1ST_DATA, buf, 2);
  1566. switch (buf[0]) {
  1567. case MHL_MSC_MSG_RAPK:
  1568. msg = sii8620_msc_msg_first(ctx);
  1569. if (!msg)
  1570. return;
  1571. msg->ret = buf[1];
  1572. ctx->mt_state = MT_STATE_DONE;
  1573. break;
  1574. case MHL_MSC_MSG_RCP:
  1575. if (!sii8620_rcp_consume(ctx, buf[1]))
  1576. sii8620_mt_rcpe(ctx,
  1577. MHL_RCPE_STATUS_INEFFECTIVE_KEY_CODE);
  1578. sii8620_mt_rcpk(ctx, buf[1]);
  1579. break;
  1580. default:
  1581. dev_err(ctx->dev, "%s message type %d,%d not supported",
  1582. __func__, buf[0], buf[1]);
  1583. }
  1584. }
  1585. static void sii8620_irq_msc(struct sii8620 *ctx)
  1586. {
  1587. u8 stat = sii8620_readb(ctx, REG_CBUS_INT_0);
  1588. if (stat & ~BIT_CBUS_HPD_CHG)
  1589. sii8620_write(ctx, REG_CBUS_INT_0, stat & ~BIT_CBUS_HPD_CHG);
  1590. if (stat & BIT_CBUS_HPD_CHG) {
  1591. u8 cbus_stat = sii8620_readb(ctx, REG_CBUS_STATUS);
  1592. if ((cbus_stat ^ ctx->cbus_status) & BIT_CBUS_STATUS_CBUS_HPD) {
  1593. sii8620_write(ctx, REG_CBUS_INT_0, BIT_CBUS_HPD_CHG);
  1594. } else {
  1595. stat ^= BIT_CBUS_STATUS_CBUS_HPD;
  1596. cbus_stat ^= BIT_CBUS_STATUS_CBUS_HPD;
  1597. }
  1598. ctx->cbus_status = cbus_stat;
  1599. }
  1600. if (stat & BIT_CBUS_MSC_MR_WRITE_STAT)
  1601. sii8620_msc_mr_write_stat(ctx);
  1602. if (stat & BIT_CBUS_HPD_CHG) {
  1603. if (ctx->cbus_status & BIT_CBUS_STATUS_CBUS_HPD) {
  1604. ctx->sink_detected = true;
  1605. sii8620_identify_sink(ctx);
  1606. } else {
  1607. sii8620_hpd_unplugged(ctx);
  1608. }
  1609. }
  1610. if (stat & BIT_CBUS_MSC_MR_SET_INT)
  1611. sii8620_msc_mr_set_int(ctx);
  1612. if (stat & BIT_CBUS_MSC_MT_DONE)
  1613. sii8620_msc_mt_done(ctx);
  1614. if (stat & BIT_CBUS_MSC_MR_MSC_MSG)
  1615. sii8620_msc_mr_msc_msg(ctx);
  1616. }
  1617. static void sii8620_irq_coc(struct sii8620 *ctx)
  1618. {
  1619. u8 stat = sii8620_readb(ctx, REG_COC_INTR);
  1620. if (stat & BIT_COC_CALIBRATION_DONE) {
  1621. u8 cstat = sii8620_readb(ctx, REG_COC_STAT_0);
  1622. cstat &= BIT_COC_STAT_0_PLL_LOCKED | MSK_COC_STAT_0_FSM_STATE;
  1623. if (cstat == (BIT_COC_STAT_0_PLL_LOCKED | 0x02)) {
  1624. sii8620_write_seq_static(ctx,
  1625. REG_COC_CTLB, 0,
  1626. REG_TRXINTMH, BIT_TDM_INTR_SYNC_DATA
  1627. | BIT_TDM_INTR_SYNC_WAIT
  1628. );
  1629. }
  1630. }
  1631. sii8620_write(ctx, REG_COC_INTR, stat);
  1632. }
  1633. static void sii8620_irq_merr(struct sii8620 *ctx)
  1634. {
  1635. u8 stat = sii8620_readb(ctx, REG_CBUS_INT_1);
  1636. sii8620_write(ctx, REG_CBUS_INT_1, stat);
  1637. }
  1638. static void sii8620_irq_edid(struct sii8620 *ctx)
  1639. {
  1640. u8 stat = sii8620_readb(ctx, REG_INTR9);
  1641. sii8620_write(ctx, REG_INTR9, stat);
  1642. if (stat & BIT_INTR9_DEVCAP_DONE)
  1643. ctx->mt_state = MT_STATE_DONE;
  1644. }
  1645. static void sii8620_irq_scdt(struct sii8620 *ctx)
  1646. {
  1647. u8 stat = sii8620_readb(ctx, REG_INTR5);
  1648. if (stat & BIT_INTR_SCDT_CHANGE) {
  1649. u8 cstat = sii8620_readb(ctx, REG_TMDS_CSTAT_P3);
  1650. if (cstat & BIT_TMDS_CSTAT_P3_SCDT)
  1651. sii8620_start_video(ctx);
  1652. }
  1653. sii8620_write(ctx, REG_INTR5, stat);
  1654. }
  1655. static void sii8620_got_xdevcap(struct sii8620 *ctx, int ret)
  1656. {
  1657. if (ret < 0)
  1658. return;
  1659. sii8620_mt_read_devcap(ctx, false);
  1660. }
  1661. static void sii8620_irq_tdm(struct sii8620 *ctx)
  1662. {
  1663. u8 stat = sii8620_readb(ctx, REG_TRXINTH);
  1664. u8 tdm = sii8620_readb(ctx, REG_TRXSTA2);
  1665. if ((tdm & MSK_TDM_SYNCHRONIZED) == VAL_TDM_SYNCHRONIZED) {
  1666. ctx->mode = CM_ECBUS_S;
  1667. ctx->burst.rx_ack = 0;
  1668. ctx->burst.r_size = SII8620_BURST_BUF_LEN;
  1669. sii8620_burst_tx_rbuf_info(ctx, SII8620_BURST_BUF_LEN);
  1670. sii8620_mt_read_devcap(ctx, true);
  1671. sii8620_mt_set_cont(ctx, sii8620_got_xdevcap);
  1672. } else {
  1673. sii8620_write_seq_static(ctx,
  1674. REG_MHL_PLL_CTL2, 0,
  1675. REG_MHL_PLL_CTL2, BIT_MHL_PLL_CTL2_CLKDETECT_EN
  1676. );
  1677. }
  1678. sii8620_write(ctx, REG_TRXINTH, stat);
  1679. }
  1680. static void sii8620_irq_block(struct sii8620 *ctx)
  1681. {
  1682. u8 stat = sii8620_readb(ctx, REG_EMSCINTR);
  1683. if (stat & BIT_EMSCINTR_SPI_DVLD) {
  1684. u8 bstat = sii8620_readb(ctx, REG_SPIBURSTSTAT);
  1685. if (bstat & BIT_SPIBURSTSTAT_EMSC_NORMAL_MODE)
  1686. sii8620_burst_receive(ctx);
  1687. }
  1688. sii8620_write(ctx, REG_EMSCINTR, stat);
  1689. }
  1690. static void sii8620_irq_ddc(struct sii8620 *ctx)
  1691. {
  1692. u8 stat = sii8620_readb(ctx, REG_INTR3);
  1693. if (stat & BIT_DDC_CMD_DONE) {
  1694. sii8620_write(ctx, REG_INTR3_MASK, 0);
  1695. if (sii8620_is_mhl3(ctx) && !ctx->feature_complete)
  1696. sii8620_mt_set_int(ctx, MHL_INT_REG(RCHANGE),
  1697. MHL_INT_RC_FEAT_REQ);
  1698. else
  1699. sii8620_enable_hpd(ctx);
  1700. }
  1701. sii8620_write(ctx, REG_INTR3, stat);
  1702. }
  1703. /* endian agnostic, non-volatile version of test_bit */
  1704. static bool sii8620_test_bit(unsigned int nr, const u8 *addr)
  1705. {
  1706. return 1 & (addr[nr / BITS_PER_BYTE] >> (nr % BITS_PER_BYTE));
  1707. }
  1708. static irqreturn_t sii8620_irq_thread(int irq, void *data)
  1709. {
  1710. static const struct {
  1711. int bit;
  1712. void (*handler)(struct sii8620 *ctx);
  1713. } irq_vec[] = {
  1714. { BIT_FAST_INTR_STAT_DISC, sii8620_irq_disc },
  1715. { BIT_FAST_INTR_STAT_G2WB, sii8620_irq_g2wb },
  1716. { BIT_FAST_INTR_STAT_COC, sii8620_irq_coc },
  1717. { BIT_FAST_INTR_STAT_TDM, sii8620_irq_tdm },
  1718. { BIT_FAST_INTR_STAT_MSC, sii8620_irq_msc },
  1719. { BIT_FAST_INTR_STAT_MERR, sii8620_irq_merr },
  1720. { BIT_FAST_INTR_STAT_BLOCK, sii8620_irq_block },
  1721. { BIT_FAST_INTR_STAT_EDID, sii8620_irq_edid },
  1722. { BIT_FAST_INTR_STAT_DDC, sii8620_irq_ddc },
  1723. { BIT_FAST_INTR_STAT_SCDT, sii8620_irq_scdt },
  1724. };
  1725. struct sii8620 *ctx = data;
  1726. u8 stats[LEN_FAST_INTR_STAT];
  1727. int i, ret;
  1728. mutex_lock(&ctx->lock);
  1729. sii8620_read_buf(ctx, REG_FAST_INTR_STAT, stats, ARRAY_SIZE(stats));
  1730. for (i = 0; i < ARRAY_SIZE(irq_vec); ++i)
  1731. if (sii8620_test_bit(irq_vec[i].bit, stats))
  1732. irq_vec[i].handler(ctx);
  1733. sii8620_burst_rx_all(ctx);
  1734. sii8620_mt_work(ctx);
  1735. sii8620_burst_send(ctx);
  1736. ret = sii8620_clear_error(ctx);
  1737. if (ret) {
  1738. dev_err(ctx->dev, "Error during IRQ handling, %d.\n", ret);
  1739. sii8620_mhl_disconnected(ctx);
  1740. }
  1741. mutex_unlock(&ctx->lock);
  1742. return IRQ_HANDLED;
  1743. }
  1744. static void sii8620_cable_in(struct sii8620 *ctx)
  1745. {
  1746. struct device *dev = ctx->dev;
  1747. u8 ver[5];
  1748. int ret;
  1749. ret = sii8620_hw_on(ctx);
  1750. if (ret) {
  1751. dev_err(dev, "Error powering on, %d.\n", ret);
  1752. return;
  1753. }
  1754. sii8620_read_buf(ctx, REG_VND_IDL, ver, ARRAY_SIZE(ver));
  1755. ret = sii8620_clear_error(ctx);
  1756. if (ret) {
  1757. dev_err(dev, "Error accessing I2C bus, %d.\n", ret);
  1758. return;
  1759. }
  1760. dev_info(dev, "ChipID %02x%02x:%02x%02x rev %02x.\n", ver[1], ver[0],
  1761. ver[3], ver[2], ver[4]);
  1762. sii8620_write(ctx, REG_DPD,
  1763. BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12 | BIT_DPD_OSC_EN);
  1764. sii8620_xtal_set_rate(ctx);
  1765. sii8620_disconnect(ctx);
  1766. sii8620_write_seq_static(ctx,
  1767. REG_MHL_CBUS_CTL0, VAL_MHL_CBUS_CTL0_CBUS_DRV_SEL_STRONG
  1768. | VAL_MHL_CBUS_CTL0_CBUS_RGND_VBIAS_734,
  1769. REG_MHL_CBUS_CTL1, VAL_MHL_CBUS_CTL1_1115_OHM,
  1770. REG_DPD, BIT_DPD_PWRON_PLL | BIT_DPD_PDNTX12 | BIT_DPD_OSC_EN,
  1771. );
  1772. ret = sii8620_clear_error(ctx);
  1773. if (ret) {
  1774. dev_err(dev, "Error accessing I2C bus, %d.\n", ret);
  1775. return;
  1776. }
  1777. enable_irq(to_i2c_client(ctx->dev)->irq);
  1778. }
  1779. static void sii8620_init_rcp_input_dev(struct sii8620 *ctx)
  1780. {
  1781. struct rc_dev *rc_dev;
  1782. int ret;
  1783. if (!IS_ENABLED(CONFIG_RC_CORE))
  1784. return;
  1785. rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE);
  1786. if (!rc_dev) {
  1787. dev_err(ctx->dev, "Failed to allocate RC device\n");
  1788. ctx->error = -ENOMEM;
  1789. return;
  1790. }
  1791. rc_dev->input_phys = "sii8620/input0";
  1792. rc_dev->input_id.bustype = BUS_VIRTUAL;
  1793. rc_dev->map_name = RC_MAP_CEC;
  1794. rc_dev->allowed_protocols = RC_PROTO_BIT_CEC;
  1795. rc_dev->driver_name = "sii8620";
  1796. rc_dev->device_name = "sii8620";
  1797. ret = rc_register_device(rc_dev);
  1798. if (ret) {
  1799. dev_err(ctx->dev, "Failed to register RC device\n");
  1800. ctx->error = ret;
  1801. rc_free_device(rc_dev);
  1802. return;
  1803. }
  1804. ctx->rc_dev = rc_dev;
  1805. }
  1806. static void sii8620_cable_out(struct sii8620 *ctx)
  1807. {
  1808. disable_irq(to_i2c_client(ctx->dev)->irq);
  1809. sii8620_hw_off(ctx);
  1810. }
  1811. static void sii8620_extcon_work(struct work_struct *work)
  1812. {
  1813. struct sii8620 *ctx =
  1814. container_of(work, struct sii8620, extcon_wq);
  1815. int state = extcon_get_state(ctx->extcon, EXTCON_DISP_MHL);
  1816. if (state == ctx->cable_state)
  1817. return;
  1818. ctx->cable_state = state;
  1819. if (state > 0)
  1820. sii8620_cable_in(ctx);
  1821. else
  1822. sii8620_cable_out(ctx);
  1823. }
  1824. static int sii8620_extcon_notifier(struct notifier_block *self,
  1825. unsigned long event, void *ptr)
  1826. {
  1827. struct sii8620 *ctx =
  1828. container_of(self, struct sii8620, extcon_nb);
  1829. schedule_work(&ctx->extcon_wq);
  1830. return NOTIFY_DONE;
  1831. }
  1832. static int sii8620_extcon_init(struct sii8620 *ctx)
  1833. {
  1834. struct extcon_dev *edev;
  1835. struct device_node *musb, *muic;
  1836. int ret;
  1837. /* get micro-USB connector node */
  1838. musb = of_graph_get_remote_node(ctx->dev->of_node, 1, -1);
  1839. /* next get micro-USB Interface Controller node */
  1840. muic = of_get_next_parent(musb);
  1841. if (!muic) {
  1842. dev_info(ctx->dev, "no extcon found, switching to 'always on' mode\n");
  1843. return 0;
  1844. }
  1845. edev = extcon_find_edev_by_node(muic);
  1846. of_node_put(muic);
  1847. if (IS_ERR(edev)) {
  1848. if (PTR_ERR(edev) == -EPROBE_DEFER)
  1849. return -EPROBE_DEFER;
  1850. dev_err(ctx->dev, "Invalid or missing extcon\n");
  1851. return PTR_ERR(edev);
  1852. }
  1853. ctx->extcon = edev;
  1854. ctx->extcon_nb.notifier_call = sii8620_extcon_notifier;
  1855. INIT_WORK(&ctx->extcon_wq, sii8620_extcon_work);
  1856. ret = extcon_register_notifier(edev, EXTCON_DISP_MHL, &ctx->extcon_nb);
  1857. if (ret) {
  1858. dev_err(ctx->dev, "failed to register notifier for MHL\n");
  1859. return ret;
  1860. }
  1861. return 0;
  1862. }
  1863. static inline struct sii8620 *bridge_to_sii8620(struct drm_bridge *bridge)
  1864. {
  1865. return container_of(bridge, struct sii8620, bridge);
  1866. }
  1867. static int sii8620_attach(struct drm_bridge *bridge,
  1868. enum drm_bridge_attach_flags flags)
  1869. {
  1870. struct sii8620 *ctx = bridge_to_sii8620(bridge);
  1871. sii8620_init_rcp_input_dev(ctx);
  1872. return sii8620_clear_error(ctx);
  1873. }
  1874. static void sii8620_detach(struct drm_bridge *bridge)
  1875. {
  1876. struct sii8620 *ctx = bridge_to_sii8620(bridge);
  1877. if (!IS_ENABLED(CONFIG_RC_CORE))
  1878. return;
  1879. rc_unregister_device(ctx->rc_dev);
  1880. }
  1881. static int sii8620_is_packing_required(struct sii8620 *ctx,
  1882. const struct drm_display_mode *mode)
  1883. {
  1884. int max_pclk, max_pclk_pp_mode;
  1885. if (sii8620_is_mhl3(ctx)) {
  1886. max_pclk = MHL3_MAX_PCLK;
  1887. max_pclk_pp_mode = MHL3_MAX_PCLK_PP_MODE;
  1888. } else {
  1889. max_pclk = MHL1_MAX_PCLK;
  1890. max_pclk_pp_mode = MHL1_MAX_PCLK_PP_MODE;
  1891. }
  1892. if (mode->clock < max_pclk)
  1893. return 0;
  1894. else if (mode->clock < max_pclk_pp_mode)
  1895. return 1;
  1896. else
  1897. return -1;
  1898. }
  1899. static enum drm_mode_status sii8620_mode_valid(struct drm_bridge *bridge,
  1900. const struct drm_display_info *info,
  1901. const struct drm_display_mode *mode)
  1902. {
  1903. struct sii8620 *ctx = bridge_to_sii8620(bridge);
  1904. int pack_required = sii8620_is_packing_required(ctx, mode);
  1905. bool can_pack = ctx->devcap[MHL_DCAP_VID_LINK_MODE] &
  1906. MHL_DCAP_VID_LINK_PPIXEL;
  1907. switch (pack_required) {
  1908. case 0:
  1909. return MODE_OK;
  1910. case 1:
  1911. return (can_pack) ? MODE_OK : MODE_CLOCK_HIGH;
  1912. default:
  1913. return MODE_CLOCK_HIGH;
  1914. }
  1915. }
  1916. static bool sii8620_mode_fixup(struct drm_bridge *bridge,
  1917. const struct drm_display_mode *mode,
  1918. struct drm_display_mode *adjusted_mode)
  1919. {
  1920. struct sii8620 *ctx = bridge_to_sii8620(bridge);
  1921. mutex_lock(&ctx->lock);
  1922. ctx->use_packed_pixel = sii8620_is_packing_required(ctx, adjusted_mode);
  1923. mutex_unlock(&ctx->lock);
  1924. return true;
  1925. }
  1926. static const struct drm_bridge_funcs sii8620_bridge_funcs = {
  1927. .attach = sii8620_attach,
  1928. .detach = sii8620_detach,
  1929. .mode_fixup = sii8620_mode_fixup,
  1930. .mode_valid = sii8620_mode_valid,
  1931. };
  1932. static int sii8620_probe(struct i2c_client *client,
  1933. const struct i2c_device_id *id)
  1934. {
  1935. struct device *dev = &client->dev;
  1936. struct sii8620 *ctx;
  1937. int ret;
  1938. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  1939. if (!ctx)
  1940. return -ENOMEM;
  1941. ctx->dev = dev;
  1942. mutex_init(&ctx->lock);
  1943. INIT_LIST_HEAD(&ctx->mt_queue);
  1944. ctx->clk_xtal = devm_clk_get(dev, "xtal");
  1945. if (IS_ERR(ctx->clk_xtal))
  1946. return dev_err_probe(dev, PTR_ERR(ctx->clk_xtal),
  1947. "failed to get xtal clock from DT\n");
  1948. if (!client->irq) {
  1949. dev_err(dev, "no irq provided\n");
  1950. return -EINVAL;
  1951. }
  1952. irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
  1953. ret = devm_request_threaded_irq(dev, client->irq, NULL,
  1954. sii8620_irq_thread,
  1955. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  1956. "sii8620", ctx);
  1957. if (ret < 0)
  1958. return dev_err_probe(dev, ret,
  1959. "failed to install IRQ handler\n");
  1960. ctx->gpio_reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  1961. if (IS_ERR(ctx->gpio_reset))
  1962. return dev_err_probe(dev, PTR_ERR(ctx->gpio_reset),
  1963. "failed to get reset gpio from DT\n");
  1964. ctx->supplies[0].supply = "cvcc10";
  1965. ctx->supplies[1].supply = "iovcc18";
  1966. ret = devm_regulator_bulk_get(dev, 2, ctx->supplies);
  1967. if (ret)
  1968. return ret;
  1969. ret = sii8620_extcon_init(ctx);
  1970. if (ret < 0) {
  1971. dev_err(ctx->dev, "failed to initialize EXTCON\n");
  1972. return ret;
  1973. }
  1974. i2c_set_clientdata(client, ctx);
  1975. ctx->bridge.funcs = &sii8620_bridge_funcs;
  1976. ctx->bridge.of_node = dev->of_node;
  1977. drm_bridge_add(&ctx->bridge);
  1978. if (!ctx->extcon)
  1979. sii8620_cable_in(ctx);
  1980. return 0;
  1981. }
  1982. static void sii8620_remove(struct i2c_client *client)
  1983. {
  1984. struct sii8620 *ctx = i2c_get_clientdata(client);
  1985. if (ctx->extcon) {
  1986. extcon_unregister_notifier(ctx->extcon, EXTCON_DISP_MHL,
  1987. &ctx->extcon_nb);
  1988. flush_work(&ctx->extcon_wq);
  1989. if (ctx->cable_state > 0)
  1990. sii8620_cable_out(ctx);
  1991. } else {
  1992. sii8620_cable_out(ctx);
  1993. }
  1994. drm_bridge_remove(&ctx->bridge);
  1995. }
  1996. static const struct of_device_id sii8620_dt_match[] = {
  1997. { .compatible = "sil,sii8620" },
  1998. { },
  1999. };
  2000. MODULE_DEVICE_TABLE(of, sii8620_dt_match);
  2001. static const struct i2c_device_id sii8620_id[] = {
  2002. { "sii8620", 0 },
  2003. { },
  2004. };
  2005. MODULE_DEVICE_TABLE(i2c, sii8620_id);
  2006. static struct i2c_driver sii8620_driver = {
  2007. .driver = {
  2008. .name = "sii8620",
  2009. .of_match_table = of_match_ptr(sii8620_dt_match),
  2010. },
  2011. .probe = sii8620_probe,
  2012. .remove = sii8620_remove,
  2013. .id_table = sii8620_id,
  2014. };
  2015. module_i2c_driver(sii8620_driver);
  2016. MODULE_LICENSE("GPL v2");