sil-sii8620.c 59 KB

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