sil-sii8620.c 57 KB

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