sil-sii8620.c 57 KB

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