sil-sii8620.c 54 KB

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