anx7625.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2020, Analogix Semiconductor. All rights reserved.
  4. *
  5. */
  6. #include <linux/gcd.h>
  7. #include <linux/gpio/consumer.h>
  8. #include <linux/i2c.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/of_graph.h>
  21. #include <linux/of_platform.h>
  22. #include <drm/display/drm_dp_aux_bus.h>
  23. #include <drm/display/drm_dp_helper.h>
  24. #include <drm/display/drm_hdcp_helper.h>
  25. #include <drm/drm_atomic_helper.h>
  26. #include <drm/drm_bridge.h>
  27. #include <drm/drm_crtc_helper.h>
  28. #include <drm/drm_edid.h>
  29. #include <drm/drm_mipi_dsi.h>
  30. #include <drm/drm_of.h>
  31. #include <drm/drm_panel.h>
  32. #include <drm/drm_print.h>
  33. #include <drm/drm_probe_helper.h>
  34. #include <media/v4l2-fwnode.h>
  35. #include <sound/hdmi-codec.h>
  36. #include <video/display_timing.h>
  37. #include "anx7625.h"
  38. /*
  39. * There is a sync issue while access I2C register between AP(CPU) and
  40. * internal firmware(OCM), to avoid the race condition, AP should access
  41. * the reserved slave address before slave address occurs changes.
  42. */
  43. static int i2c_access_workaround(struct anx7625_data *ctx,
  44. struct i2c_client *client)
  45. {
  46. u8 offset;
  47. struct device *dev = &client->dev;
  48. int ret;
  49. if (client == ctx->last_client)
  50. return 0;
  51. ctx->last_client = client;
  52. if (client == ctx->i2c.tcpc_client)
  53. offset = RSVD_00_ADDR;
  54. else if (client == ctx->i2c.tx_p0_client)
  55. offset = RSVD_D1_ADDR;
  56. else if (client == ctx->i2c.tx_p1_client)
  57. offset = RSVD_60_ADDR;
  58. else if (client == ctx->i2c.rx_p0_client)
  59. offset = RSVD_39_ADDR;
  60. else if (client == ctx->i2c.rx_p1_client)
  61. offset = RSVD_7F_ADDR;
  62. else
  63. offset = RSVD_00_ADDR;
  64. ret = i2c_smbus_write_byte_data(client, offset, 0x00);
  65. if (ret < 0)
  66. DRM_DEV_ERROR(dev,
  67. "fail to access i2c id=%x\n:%x",
  68. client->addr, offset);
  69. return ret;
  70. }
  71. static int anx7625_reg_read(struct anx7625_data *ctx,
  72. struct i2c_client *client, u8 reg_addr)
  73. {
  74. int ret;
  75. struct device *dev = &client->dev;
  76. i2c_access_workaround(ctx, client);
  77. ret = i2c_smbus_read_byte_data(client, reg_addr);
  78. if (ret < 0)
  79. DRM_DEV_ERROR(dev, "read i2c fail id=%x:%x\n",
  80. client->addr, reg_addr);
  81. return ret;
  82. }
  83. static int anx7625_reg_block_read(struct anx7625_data *ctx,
  84. struct i2c_client *client,
  85. u8 reg_addr, u8 len, u8 *buf)
  86. {
  87. int ret;
  88. struct device *dev = &client->dev;
  89. i2c_access_workaround(ctx, client);
  90. ret = i2c_smbus_read_i2c_block_data(client, reg_addr, len, buf);
  91. if (ret < 0)
  92. DRM_DEV_ERROR(dev, "read i2c block fail id=%x:%x\n",
  93. client->addr, reg_addr);
  94. return ret;
  95. }
  96. static int anx7625_reg_write(struct anx7625_data *ctx,
  97. struct i2c_client *client,
  98. u8 reg_addr, u8 reg_val)
  99. {
  100. int ret;
  101. struct device *dev = &client->dev;
  102. i2c_access_workaround(ctx, client);
  103. ret = i2c_smbus_write_byte_data(client, reg_addr, reg_val);
  104. if (ret < 0)
  105. DRM_DEV_ERROR(dev, "fail to write i2c id=%x\n:%x",
  106. client->addr, reg_addr);
  107. return ret;
  108. }
  109. static int anx7625_reg_block_write(struct anx7625_data *ctx,
  110. struct i2c_client *client,
  111. u8 reg_addr, u8 len, u8 *buf)
  112. {
  113. int ret;
  114. struct device *dev = &client->dev;
  115. i2c_access_workaround(ctx, client);
  116. ret = i2c_smbus_write_i2c_block_data(client, reg_addr, len, buf);
  117. if (ret < 0)
  118. dev_err(dev, "write i2c block failed id=%x\n:%x",
  119. client->addr, reg_addr);
  120. return ret;
  121. }
  122. static int anx7625_write_or(struct anx7625_data *ctx,
  123. struct i2c_client *client,
  124. u8 offset, u8 mask)
  125. {
  126. int val;
  127. val = anx7625_reg_read(ctx, client, offset);
  128. if (val < 0)
  129. return val;
  130. return anx7625_reg_write(ctx, client, offset, (val | (mask)));
  131. }
  132. static int anx7625_write_and(struct anx7625_data *ctx,
  133. struct i2c_client *client,
  134. u8 offset, u8 mask)
  135. {
  136. int val;
  137. val = anx7625_reg_read(ctx, client, offset);
  138. if (val < 0)
  139. return val;
  140. return anx7625_reg_write(ctx, client, offset, (val & (mask)));
  141. }
  142. static int anx7625_write_and_or(struct anx7625_data *ctx,
  143. struct i2c_client *client,
  144. u8 offset, u8 and_mask, u8 or_mask)
  145. {
  146. int val;
  147. val = anx7625_reg_read(ctx, client, offset);
  148. if (val < 0)
  149. return val;
  150. return anx7625_reg_write(ctx, client,
  151. offset, (val & and_mask) | (or_mask));
  152. }
  153. static int anx7625_config_bit_matrix(struct anx7625_data *ctx)
  154. {
  155. int i, ret;
  156. ret = anx7625_write_or(ctx, ctx->i2c.tx_p2_client,
  157. AUDIO_CONTROL_REGISTER, 0x80);
  158. for (i = 0; i < 13; i++)
  159. ret |= anx7625_reg_write(ctx, ctx->i2c.tx_p2_client,
  160. VIDEO_BIT_MATRIX_12 + i,
  161. 0x18 + i);
  162. return ret;
  163. }
  164. static int anx7625_read_ctrl_status_p0(struct anx7625_data *ctx)
  165. {
  166. return anx7625_reg_read(ctx, ctx->i2c.rx_p0_client, AP_AUX_CTRL_STATUS);
  167. }
  168. static int wait_aux_op_finish(struct anx7625_data *ctx)
  169. {
  170. struct device *dev = &ctx->client->dev;
  171. int val;
  172. int ret;
  173. ret = readx_poll_timeout(anx7625_read_ctrl_status_p0,
  174. ctx, val,
  175. (!(val & AP_AUX_CTRL_OP_EN) || (val < 0)),
  176. 2000,
  177. 2000 * 150);
  178. if (ret) {
  179. DRM_DEV_ERROR(dev, "aux operation fail!\n");
  180. return -EIO;
  181. }
  182. val = anx7625_reg_read(ctx, ctx->i2c.rx_p0_client,
  183. AP_AUX_CTRL_STATUS);
  184. if (val < 0 || (val & 0x0F)) {
  185. DRM_DEV_ERROR(dev, "aux status %02x\n", val);
  186. return -EIO;
  187. }
  188. return 0;
  189. }
  190. static int anx7625_aux_trans(struct anx7625_data *ctx, u8 op, u32 address,
  191. u8 len, u8 *buf)
  192. {
  193. struct device *dev = &ctx->client->dev;
  194. int ret;
  195. u8 addrh, addrm, addrl;
  196. u8 cmd;
  197. bool is_write = !(op & DP_AUX_I2C_READ);
  198. if (len > DP_AUX_MAX_PAYLOAD_BYTES) {
  199. dev_err(dev, "exceed aux buffer len.\n");
  200. return -EINVAL;
  201. }
  202. if (!len)
  203. return len;
  204. addrl = address & 0xFF;
  205. addrm = (address >> 8) & 0xFF;
  206. addrh = (address >> 16) & 0xFF;
  207. if (!is_write)
  208. op &= ~DP_AUX_I2C_MOT;
  209. cmd = DPCD_CMD(len, op);
  210. /* Set command and length */
  211. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  212. AP_AUX_COMMAND, cmd);
  213. /* Set aux access address */
  214. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  215. AP_AUX_ADDR_7_0, addrl);
  216. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  217. AP_AUX_ADDR_15_8, addrm);
  218. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  219. AP_AUX_ADDR_19_16, addrh);
  220. if (is_write)
  221. ret |= anx7625_reg_block_write(ctx, ctx->i2c.rx_p0_client,
  222. AP_AUX_BUFF_START, len, buf);
  223. /* Enable aux access */
  224. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p0_client,
  225. AP_AUX_CTRL_STATUS, AP_AUX_CTRL_OP_EN);
  226. if (ret < 0) {
  227. dev_err(dev, "cannot access aux related register.\n");
  228. return -EIO;
  229. }
  230. ret = wait_aux_op_finish(ctx);
  231. if (ret < 0) {
  232. dev_err(dev, "aux IO error: wait aux op finish.\n");
  233. return ret;
  234. }
  235. /* Write done */
  236. if (is_write)
  237. return len;
  238. /* Read done, read out dpcd data */
  239. ret = anx7625_reg_block_read(ctx, ctx->i2c.rx_p0_client,
  240. AP_AUX_BUFF_START, len, buf);
  241. if (ret < 0) {
  242. dev_err(dev, "read dpcd register failed\n");
  243. return -EIO;
  244. }
  245. return len;
  246. }
  247. static int anx7625_video_mute_control(struct anx7625_data *ctx,
  248. u8 status)
  249. {
  250. int ret;
  251. if (status) {
  252. /* Set mute on flag */
  253. ret = anx7625_write_or(ctx, ctx->i2c.rx_p0_client,
  254. AP_AV_STATUS, AP_MIPI_MUTE);
  255. /* Clear mipi RX en */
  256. ret |= anx7625_write_and(ctx, ctx->i2c.rx_p0_client,
  257. AP_AV_STATUS, (u8)~AP_MIPI_RX_EN);
  258. } else {
  259. /* Mute off flag */
  260. ret = anx7625_write_and(ctx, ctx->i2c.rx_p0_client,
  261. AP_AV_STATUS, (u8)~AP_MIPI_MUTE);
  262. /* Set MIPI RX EN */
  263. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p0_client,
  264. AP_AV_STATUS, AP_MIPI_RX_EN);
  265. }
  266. return ret;
  267. }
  268. /* Reduction of fraction a/b */
  269. static void anx7625_reduction_of_a_fraction(unsigned long *a, unsigned long *b)
  270. {
  271. unsigned long gcd_num;
  272. unsigned long tmp_a, tmp_b;
  273. u32 i = 1;
  274. gcd_num = gcd(*a, *b);
  275. *a /= gcd_num;
  276. *b /= gcd_num;
  277. tmp_a = *a;
  278. tmp_b = *b;
  279. while ((*a > MAX_UNSIGNED_24BIT) || (*b > MAX_UNSIGNED_24BIT)) {
  280. i++;
  281. *a = tmp_a / i;
  282. *b = tmp_b / i;
  283. }
  284. /*
  285. * In the end, make a, b larger to have higher ODFC PLL
  286. * output frequency accuracy
  287. */
  288. while ((*a < MAX_UNSIGNED_24BIT) && (*b < MAX_UNSIGNED_24BIT)) {
  289. *a <<= 1;
  290. *b <<= 1;
  291. }
  292. *a >>= 1;
  293. *b >>= 1;
  294. }
  295. static int anx7625_calculate_m_n(u32 pixelclock,
  296. unsigned long *m,
  297. unsigned long *n,
  298. u8 *post_divider)
  299. {
  300. if (pixelclock > PLL_OUT_FREQ_ABS_MAX / POST_DIVIDER_MIN) {
  301. /* Pixel clock frequency is too high */
  302. DRM_ERROR("pixelclock too high, act(%d), maximum(%lu)\n",
  303. pixelclock,
  304. PLL_OUT_FREQ_ABS_MAX / POST_DIVIDER_MIN);
  305. return -EINVAL;
  306. }
  307. if (pixelclock < PLL_OUT_FREQ_ABS_MIN / POST_DIVIDER_MAX) {
  308. /* Pixel clock frequency is too low */
  309. DRM_ERROR("pixelclock too low, act(%d), maximum(%lu)\n",
  310. pixelclock,
  311. PLL_OUT_FREQ_ABS_MIN / POST_DIVIDER_MAX);
  312. return -EINVAL;
  313. }
  314. for (*post_divider = 1;
  315. pixelclock < (PLL_OUT_FREQ_MIN / (*post_divider));)
  316. *post_divider += 1;
  317. if (*post_divider > POST_DIVIDER_MAX) {
  318. for (*post_divider = 1;
  319. (pixelclock <
  320. (PLL_OUT_FREQ_ABS_MIN / (*post_divider)));)
  321. *post_divider += 1;
  322. if (*post_divider > POST_DIVIDER_MAX) {
  323. DRM_ERROR("cannot find property post_divider(%d)\n",
  324. *post_divider);
  325. return -EDOM;
  326. }
  327. }
  328. /* Patch to improve the accuracy */
  329. if (*post_divider == 7) {
  330. /* 27,000,000 is not divisible by 7 */
  331. *post_divider = 8;
  332. } else if (*post_divider == 11) {
  333. /* 27,000,000 is not divisible by 11 */
  334. *post_divider = 12;
  335. } else if ((*post_divider == 13) || (*post_divider == 14)) {
  336. /* 27,000,000 is not divisible by 13 or 14 */
  337. *post_divider = 15;
  338. }
  339. if (pixelclock * (*post_divider) > PLL_OUT_FREQ_ABS_MAX) {
  340. DRM_ERROR("act clock(%u) large than maximum(%lu)\n",
  341. pixelclock * (*post_divider),
  342. PLL_OUT_FREQ_ABS_MAX);
  343. return -EDOM;
  344. }
  345. *m = pixelclock;
  346. *n = XTAL_FRQ / (*post_divider);
  347. anx7625_reduction_of_a_fraction(m, n);
  348. return 0;
  349. }
  350. static int anx7625_odfc_config(struct anx7625_data *ctx,
  351. u8 post_divider)
  352. {
  353. int ret;
  354. struct device *dev = &ctx->client->dev;
  355. /* Config input reference clock frequency 27MHz/19.2MHz */
  356. ret = anx7625_write_and(ctx, ctx->i2c.rx_p1_client, MIPI_DIGITAL_PLL_16,
  357. ~(REF_CLK_27000KHZ << MIPI_FREF_D_IND));
  358. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p1_client, MIPI_DIGITAL_PLL_16,
  359. (REF_CLK_27000KHZ << MIPI_FREF_D_IND));
  360. /* Post divider */
  361. ret |= anx7625_write_and(ctx, ctx->i2c.rx_p1_client,
  362. MIPI_DIGITAL_PLL_8, 0x0f);
  363. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p1_client, MIPI_DIGITAL_PLL_8,
  364. post_divider << 4);
  365. /* Add patch for MIS2-125 (5pcs ANX7625 fail ATE MBIST test) */
  366. ret |= anx7625_write_and(ctx, ctx->i2c.rx_p1_client, MIPI_DIGITAL_PLL_7,
  367. ~MIPI_PLL_VCO_TUNE_REG_VAL);
  368. /* Reset ODFC PLL */
  369. ret |= anx7625_write_and(ctx, ctx->i2c.rx_p1_client, MIPI_DIGITAL_PLL_7,
  370. ~MIPI_PLL_RESET_N);
  371. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p1_client, MIPI_DIGITAL_PLL_7,
  372. MIPI_PLL_RESET_N);
  373. if (ret < 0)
  374. DRM_DEV_ERROR(dev, "IO error.\n");
  375. return ret;
  376. }
  377. /*
  378. * The MIPI source video data exist large variation (e.g. 59Hz ~ 61Hz),
  379. * anx7625 defined K ratio for matching MIPI input video clock and
  380. * DP output video clock. Increase K value can match bigger video data
  381. * variation. IVO panel has small variation than DP CTS spec, need
  382. * decrease the K value.
  383. */
  384. static int anx7625_set_k_value(struct anx7625_data *ctx)
  385. {
  386. struct edid *edid = (struct edid *)ctx->slimport_edid_p.edid_raw_data;
  387. if (edid->mfg_id[0] == IVO_MID0 && edid->mfg_id[1] == IVO_MID1)
  388. return anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  389. MIPI_DIGITAL_ADJ_1, 0x3B);
  390. return anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  391. MIPI_DIGITAL_ADJ_1, 0x3D);
  392. }
  393. static int anx7625_dsi_video_timing_config(struct anx7625_data *ctx)
  394. {
  395. struct device *dev = &ctx->client->dev;
  396. unsigned long m, n;
  397. u16 htotal;
  398. int ret;
  399. u8 post_divider = 0;
  400. ret = anx7625_calculate_m_n(ctx->dt.pixelclock.min * 1000,
  401. &m, &n, &post_divider);
  402. if (ret) {
  403. DRM_DEV_ERROR(dev, "cannot get property m n value.\n");
  404. return ret;
  405. }
  406. DRM_DEV_DEBUG_DRIVER(dev, "compute M(%lu), N(%lu), divider(%d).\n",
  407. m, n, post_divider);
  408. /* Configure pixel clock */
  409. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client, PIXEL_CLOCK_L,
  410. (ctx->dt.pixelclock.min / 1000) & 0xFF);
  411. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client, PIXEL_CLOCK_H,
  412. (ctx->dt.pixelclock.min / 1000) >> 8);
  413. /* Lane count */
  414. ret |= anx7625_write_and(ctx, ctx->i2c.rx_p1_client,
  415. MIPI_LANE_CTRL_0, 0xfc);
  416. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p1_client,
  417. MIPI_LANE_CTRL_0, ctx->pdata.mipi_lanes - 1);
  418. /* Htotal */
  419. htotal = ctx->dt.hactive.min + ctx->dt.hfront_porch.min +
  420. ctx->dt.hback_porch.min + ctx->dt.hsync_len.min;
  421. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  422. HORIZONTAL_TOTAL_PIXELS_L, htotal & 0xFF);
  423. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  424. HORIZONTAL_TOTAL_PIXELS_H, htotal >> 8);
  425. /* Hactive */
  426. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  427. HORIZONTAL_ACTIVE_PIXELS_L, ctx->dt.hactive.min & 0xFF);
  428. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  429. HORIZONTAL_ACTIVE_PIXELS_H, ctx->dt.hactive.min >> 8);
  430. /* HFP */
  431. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  432. HORIZONTAL_FRONT_PORCH_L, ctx->dt.hfront_porch.min);
  433. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  434. HORIZONTAL_FRONT_PORCH_H,
  435. ctx->dt.hfront_porch.min >> 8);
  436. /* HWS */
  437. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  438. HORIZONTAL_SYNC_WIDTH_L, ctx->dt.hsync_len.min);
  439. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  440. HORIZONTAL_SYNC_WIDTH_H, ctx->dt.hsync_len.min >> 8);
  441. /* HBP */
  442. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  443. HORIZONTAL_BACK_PORCH_L, ctx->dt.hback_porch.min);
  444. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  445. HORIZONTAL_BACK_PORCH_H, ctx->dt.hback_porch.min >> 8);
  446. /* Vactive */
  447. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client, ACTIVE_LINES_L,
  448. ctx->dt.vactive.min);
  449. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client, ACTIVE_LINES_H,
  450. ctx->dt.vactive.min >> 8);
  451. /* VFP */
  452. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  453. VERTICAL_FRONT_PORCH, ctx->dt.vfront_porch.min);
  454. /* VWS */
  455. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  456. VERTICAL_SYNC_WIDTH, ctx->dt.vsync_len.min);
  457. /* VBP */
  458. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p2_client,
  459. VERTICAL_BACK_PORCH, ctx->dt.vback_porch.min);
  460. /* M value */
  461. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  462. MIPI_PLL_M_NUM_23_16, (m >> 16) & 0xff);
  463. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  464. MIPI_PLL_M_NUM_15_8, (m >> 8) & 0xff);
  465. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  466. MIPI_PLL_M_NUM_7_0, (m & 0xff));
  467. /* N value */
  468. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  469. MIPI_PLL_N_NUM_23_16, (n >> 16) & 0xff);
  470. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  471. MIPI_PLL_N_NUM_15_8, (n >> 8) & 0xff);
  472. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client, MIPI_PLL_N_NUM_7_0,
  473. (n & 0xff));
  474. anx7625_set_k_value(ctx);
  475. ret |= anx7625_odfc_config(ctx, post_divider - 1);
  476. if (ret < 0)
  477. DRM_DEV_ERROR(dev, "mipi dsi setup IO error.\n");
  478. return ret;
  479. }
  480. static int anx7625_swap_dsi_lane3(struct anx7625_data *ctx)
  481. {
  482. int val;
  483. struct device *dev = &ctx->client->dev;
  484. /* Swap MIPI-DSI data lane 3 P and N */
  485. val = anx7625_reg_read(ctx, ctx->i2c.rx_p1_client, MIPI_SWAP);
  486. if (val < 0) {
  487. DRM_DEV_ERROR(dev, "IO error : access MIPI_SWAP.\n");
  488. return -EIO;
  489. }
  490. val |= (1 << MIPI_SWAP_CH3);
  491. return anx7625_reg_write(ctx, ctx->i2c.rx_p1_client, MIPI_SWAP, val);
  492. }
  493. static int anx7625_api_dsi_config(struct anx7625_data *ctx)
  494. {
  495. int val, ret;
  496. struct device *dev = &ctx->client->dev;
  497. /* Swap MIPI-DSI data lane 3 P and N */
  498. ret = anx7625_swap_dsi_lane3(ctx);
  499. if (ret < 0) {
  500. DRM_DEV_ERROR(dev, "IO error : swap dsi lane 3 fail.\n");
  501. return ret;
  502. }
  503. /* DSI clock settings */
  504. val = (0 << MIPI_HS_PWD_CLK) |
  505. (0 << MIPI_HS_RT_CLK) |
  506. (0 << MIPI_PD_CLK) |
  507. (1 << MIPI_CLK_RT_MANUAL_PD_EN) |
  508. (1 << MIPI_CLK_HS_MANUAL_PD_EN) |
  509. (0 << MIPI_CLK_DET_DET_BYPASS) |
  510. (0 << MIPI_CLK_MISS_CTRL) |
  511. (0 << MIPI_PD_LPTX_CH_MANUAL_PD_EN);
  512. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  513. MIPI_PHY_CONTROL_3, val);
  514. /*
  515. * Decreased HS prepare timing delay from 160ns to 80ns work with
  516. * a) Dragon board 810 series (Qualcomm AP)
  517. * b) Moving Pixel DSI source (PG3A pattern generator +
  518. * P332 D-PHY Probe) default D-PHY timing
  519. * 5ns/step
  520. */
  521. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  522. MIPI_TIME_HS_PRPR, 0x10);
  523. /* Enable DSI mode*/
  524. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p1_client, MIPI_DIGITAL_PLL_18,
  525. SELECT_DSI << MIPI_DPI_SELECT);
  526. ret |= anx7625_dsi_video_timing_config(ctx);
  527. if (ret < 0) {
  528. DRM_DEV_ERROR(dev, "dsi video timing config fail\n");
  529. return ret;
  530. }
  531. /* Toggle m, n ready */
  532. ret = anx7625_write_and(ctx, ctx->i2c.rx_p1_client, MIPI_DIGITAL_PLL_6,
  533. ~(MIPI_M_NUM_READY | MIPI_N_NUM_READY));
  534. usleep_range(1000, 1100);
  535. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p1_client, MIPI_DIGITAL_PLL_6,
  536. MIPI_M_NUM_READY | MIPI_N_NUM_READY);
  537. /* Configure integer stable register */
  538. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  539. MIPI_VIDEO_STABLE_CNT, 0x02);
  540. /* Power on MIPI RX */
  541. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  542. MIPI_LANE_CTRL_10, 0x00);
  543. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  544. MIPI_LANE_CTRL_10, 0x80);
  545. if (ret < 0)
  546. DRM_DEV_ERROR(dev, "IO error : mipi dsi enable init fail.\n");
  547. return ret;
  548. }
  549. static int anx7625_dsi_config(struct anx7625_data *ctx)
  550. {
  551. struct device *dev = &ctx->client->dev;
  552. int ret;
  553. DRM_DEV_DEBUG_DRIVER(dev, "config dsi.\n");
  554. /* DSC disable */
  555. ret = anx7625_write_and(ctx, ctx->i2c.rx_p0_client,
  556. R_DSC_CTRL_0, ~DSC_EN);
  557. ret |= anx7625_api_dsi_config(ctx);
  558. if (ret < 0) {
  559. DRM_DEV_ERROR(dev, "IO error : api dsi config error.\n");
  560. return ret;
  561. }
  562. /* Set MIPI RX EN */
  563. ret = anx7625_write_or(ctx, ctx->i2c.rx_p0_client,
  564. AP_AV_STATUS, AP_MIPI_RX_EN);
  565. /* Clear mute flag */
  566. ret |= anx7625_write_and(ctx, ctx->i2c.rx_p0_client,
  567. AP_AV_STATUS, (u8)~AP_MIPI_MUTE);
  568. if (ret < 0)
  569. DRM_DEV_ERROR(dev, "IO error : enable mipi rx fail.\n");
  570. else
  571. DRM_DEV_DEBUG_DRIVER(dev, "success to config DSI\n");
  572. return ret;
  573. }
  574. static int anx7625_api_dpi_config(struct anx7625_data *ctx)
  575. {
  576. struct device *dev = &ctx->client->dev;
  577. u16 freq = ctx->dt.pixelclock.min / 1000;
  578. int ret;
  579. /* configure pixel clock */
  580. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  581. PIXEL_CLOCK_L, freq & 0xFF);
  582. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  583. PIXEL_CLOCK_H, (freq >> 8));
  584. /* set DPI mode */
  585. /* set to DPI PLL module sel */
  586. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  587. MIPI_DIGITAL_PLL_9, 0x20);
  588. /* power down MIPI */
  589. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  590. MIPI_LANE_CTRL_10, 0x08);
  591. /* enable DPI mode */
  592. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p1_client,
  593. MIPI_DIGITAL_PLL_18, 0x1C);
  594. /* set first edge */
  595. ret |= anx7625_reg_write(ctx, ctx->i2c.tx_p2_client,
  596. VIDEO_CONTROL_0, 0x06);
  597. if (ret < 0)
  598. DRM_DEV_ERROR(dev, "IO error : dpi phy set failed.\n");
  599. return ret;
  600. }
  601. static int anx7625_dpi_config(struct anx7625_data *ctx)
  602. {
  603. struct device *dev = &ctx->client->dev;
  604. int ret;
  605. DRM_DEV_DEBUG_DRIVER(dev, "config dpi\n");
  606. /* DSC disable */
  607. ret = anx7625_write_and(ctx, ctx->i2c.rx_p0_client,
  608. R_DSC_CTRL_0, ~DSC_EN);
  609. if (ret < 0) {
  610. DRM_DEV_ERROR(dev, "IO error : disable dsc failed.\n");
  611. return ret;
  612. }
  613. ret = anx7625_config_bit_matrix(ctx);
  614. if (ret < 0) {
  615. DRM_DEV_ERROR(dev, "config bit matrix failed.\n");
  616. return ret;
  617. }
  618. ret = anx7625_api_dpi_config(ctx);
  619. if (ret < 0) {
  620. DRM_DEV_ERROR(dev, "mipi phy(dpi) setup failed.\n");
  621. return ret;
  622. }
  623. /* set MIPI RX EN */
  624. ret = anx7625_write_or(ctx, ctx->i2c.rx_p0_client,
  625. AP_AV_STATUS, AP_MIPI_RX_EN);
  626. /* clear mute flag */
  627. ret |= anx7625_write_and(ctx, ctx->i2c.rx_p0_client,
  628. AP_AV_STATUS, (u8)~AP_MIPI_MUTE);
  629. if (ret < 0)
  630. DRM_DEV_ERROR(dev, "IO error : enable mipi rx failed.\n");
  631. return ret;
  632. }
  633. static int anx7625_read_flash_status(struct anx7625_data *ctx)
  634. {
  635. return anx7625_reg_read(ctx, ctx->i2c.rx_p0_client, R_RAM_CTRL);
  636. }
  637. static int anx7625_hdcp_key_probe(struct anx7625_data *ctx)
  638. {
  639. int ret, val;
  640. struct device *dev = &ctx->client->dev;
  641. u8 ident[FLASH_BUF_LEN];
  642. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  643. FLASH_ADDR_HIGH, 0x91);
  644. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  645. FLASH_ADDR_LOW, 0xA0);
  646. if (ret < 0) {
  647. dev_err(dev, "IO error : set key flash address.\n");
  648. return ret;
  649. }
  650. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  651. FLASH_LEN_HIGH, (FLASH_BUF_LEN - 1) >> 8);
  652. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  653. FLASH_LEN_LOW, (FLASH_BUF_LEN - 1) & 0xFF);
  654. if (ret < 0) {
  655. dev_err(dev, "IO error : set key flash len.\n");
  656. return ret;
  657. }
  658. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  659. R_FLASH_RW_CTRL, FLASH_READ);
  660. ret |= readx_poll_timeout(anx7625_read_flash_status,
  661. ctx, val,
  662. ((val & FLASH_DONE) || (val < 0)),
  663. 2000,
  664. 2000 * 150);
  665. if (ret) {
  666. dev_err(dev, "flash read access fail!\n");
  667. return -EIO;
  668. }
  669. ret = anx7625_reg_block_read(ctx, ctx->i2c.rx_p0_client,
  670. FLASH_BUF_BASE_ADDR,
  671. FLASH_BUF_LEN, ident);
  672. if (ret < 0) {
  673. dev_err(dev, "read flash data fail!\n");
  674. return -EIO;
  675. }
  676. if (ident[29] == 0xFF && ident[30] == 0xFF && ident[31] == 0xFF)
  677. return -EINVAL;
  678. return 0;
  679. }
  680. static int anx7625_hdcp_key_load(struct anx7625_data *ctx)
  681. {
  682. int ret;
  683. struct device *dev = &ctx->client->dev;
  684. /* Select HDCP 1.4 KEY */
  685. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  686. R_BOOT_RETRY, 0x12);
  687. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  688. FLASH_ADDR_HIGH, HDCP14KEY_START_ADDR >> 8);
  689. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  690. FLASH_ADDR_LOW, HDCP14KEY_START_ADDR & 0xFF);
  691. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  692. R_RAM_LEN_H, HDCP14KEY_SIZE >> 12);
  693. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  694. R_RAM_LEN_L, HDCP14KEY_SIZE >> 4);
  695. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  696. R_RAM_ADDR_H, 0);
  697. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  698. R_RAM_ADDR_L, 0);
  699. /* Enable HDCP 1.4 KEY load */
  700. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  701. R_RAM_CTRL, DECRYPT_EN | LOAD_START);
  702. dev_dbg(dev, "load HDCP 1.4 key done\n");
  703. return ret;
  704. }
  705. static int anx7625_hdcp_disable(struct anx7625_data *ctx)
  706. {
  707. int ret;
  708. struct device *dev = &ctx->client->dev;
  709. dev_dbg(dev, "disable HDCP 1.4\n");
  710. /* Disable HDCP */
  711. ret = anx7625_write_and(ctx, ctx->i2c.rx_p1_client, 0xee, 0x9f);
  712. /* Try auth flag */
  713. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p1_client, 0xec, 0x10);
  714. /* Interrupt for DRM */
  715. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p1_client, 0xff, 0x01);
  716. if (ret < 0)
  717. dev_err(dev, "fail to disable HDCP\n");
  718. return anx7625_write_and(ctx, ctx->i2c.tx_p0_client,
  719. TX_HDCP_CTRL0, ~HARD_AUTH_EN & 0xFF);
  720. }
  721. static int anx7625_hdcp_enable(struct anx7625_data *ctx)
  722. {
  723. u8 bcap;
  724. int ret;
  725. struct device *dev = &ctx->client->dev;
  726. ret = anx7625_hdcp_key_probe(ctx);
  727. if (ret) {
  728. dev_dbg(dev, "no key found, not to do hdcp\n");
  729. return ret;
  730. }
  731. /* Read downstream capability */
  732. ret = anx7625_aux_trans(ctx, DP_AUX_NATIVE_READ, DP_AUX_HDCP_BCAPS, 1, &bcap);
  733. if (ret < 0)
  734. return ret;
  735. if (!(bcap & DP_BCAPS_HDCP_CAPABLE)) {
  736. pr_warn("downstream not support HDCP 1.4, cap(%x).\n", bcap);
  737. return 0;
  738. }
  739. dev_dbg(dev, "enable HDCP 1.4\n");
  740. /* First clear HDCP state */
  741. ret = anx7625_reg_write(ctx, ctx->i2c.tx_p0_client,
  742. TX_HDCP_CTRL0,
  743. KSVLIST_VLD | BKSV_SRM_PASS | RE_AUTHEN);
  744. usleep_range(1000, 1100);
  745. /* Second clear HDCP state */
  746. ret |= anx7625_reg_write(ctx, ctx->i2c.tx_p0_client,
  747. TX_HDCP_CTRL0,
  748. KSVLIST_VLD | BKSV_SRM_PASS | RE_AUTHEN);
  749. /* Set time for waiting KSVR */
  750. ret |= anx7625_reg_write(ctx, ctx->i2c.tx_p0_client,
  751. SP_TX_WAIT_KSVR_TIME, 0xc8);
  752. /* Set time for waiting R0 */
  753. ret |= anx7625_reg_write(ctx, ctx->i2c.tx_p0_client,
  754. SP_TX_WAIT_R0_TIME, 0xb0);
  755. ret |= anx7625_hdcp_key_load(ctx);
  756. if (ret) {
  757. pr_warn("prepare HDCP key failed.\n");
  758. return ret;
  759. }
  760. ret = anx7625_write_or(ctx, ctx->i2c.rx_p1_client, 0xee, 0x20);
  761. /* Try auth flag */
  762. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p1_client, 0xec, 0x10);
  763. /* Interrupt for DRM */
  764. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p1_client, 0xff, 0x01);
  765. if (ret < 0)
  766. dev_err(dev, "fail to enable HDCP\n");
  767. return anx7625_write_or(ctx, ctx->i2c.tx_p0_client,
  768. TX_HDCP_CTRL0, HARD_AUTH_EN);
  769. }
  770. static void anx7625_dp_start(struct anx7625_data *ctx)
  771. {
  772. int ret;
  773. struct device *dev = &ctx->client->dev;
  774. u8 data;
  775. if (!ctx->display_timing_valid) {
  776. DRM_DEV_ERROR(dev, "mipi not set display timing yet.\n");
  777. return;
  778. }
  779. dev_dbg(dev, "set downstream sink into normal\n");
  780. /* Downstream sink enter into normal mode */
  781. data = DP_SET_POWER_D0;
  782. ret = anx7625_aux_trans(ctx, DP_AUX_NATIVE_WRITE, DP_SET_POWER, 1, &data);
  783. if (ret < 0)
  784. dev_err(dev, "IO error : set sink into normal mode fail\n");
  785. /* Disable HDCP */
  786. anx7625_write_and(ctx, ctx->i2c.rx_p1_client, 0xee, 0x9f);
  787. if (ctx->pdata.is_dpi)
  788. ret = anx7625_dpi_config(ctx);
  789. else
  790. ret = anx7625_dsi_config(ctx);
  791. if (ret < 0)
  792. DRM_DEV_ERROR(dev, "MIPI phy setup error.\n");
  793. ctx->hdcp_cp = DRM_MODE_CONTENT_PROTECTION_UNDESIRED;
  794. ctx->dp_en = 1;
  795. }
  796. static void anx7625_dp_stop(struct anx7625_data *ctx)
  797. {
  798. struct device *dev = &ctx->client->dev;
  799. int ret;
  800. u8 data;
  801. DRM_DEV_DEBUG_DRIVER(dev, "stop dp output\n");
  802. /*
  803. * Video disable: 0x72:08 bit 7 = 0;
  804. * Audio disable: 0x70:87 bit 0 = 0;
  805. */
  806. ret = anx7625_write_and(ctx, ctx->i2c.tx_p0_client, 0x87, 0xfe);
  807. ret |= anx7625_write_and(ctx, ctx->i2c.tx_p2_client, 0x08, 0x7f);
  808. ret |= anx7625_video_mute_control(ctx, 1);
  809. dev_dbg(dev, "notify downstream enter into standby\n");
  810. /* Downstream monitor enter into standby mode */
  811. data = DP_SET_POWER_D3;
  812. ret |= anx7625_aux_trans(ctx, DP_AUX_NATIVE_WRITE, DP_SET_POWER, 1, &data);
  813. if (ret < 0)
  814. DRM_DEV_ERROR(dev, "IO error : mute video fail\n");
  815. ctx->hdcp_cp = DRM_MODE_CONTENT_PROTECTION_UNDESIRED;
  816. ctx->dp_en = 0;
  817. }
  818. static int sp_tx_rst_aux(struct anx7625_data *ctx)
  819. {
  820. int ret;
  821. ret = anx7625_write_or(ctx, ctx->i2c.tx_p2_client, RST_CTRL2,
  822. AUX_RST);
  823. ret |= anx7625_write_and(ctx, ctx->i2c.tx_p2_client, RST_CTRL2,
  824. ~AUX_RST);
  825. return ret;
  826. }
  827. static int sp_tx_aux_wr(struct anx7625_data *ctx, u8 offset)
  828. {
  829. int ret;
  830. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  831. AP_AUX_BUFF_START, offset);
  832. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  833. AP_AUX_COMMAND, 0x04);
  834. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p0_client,
  835. AP_AUX_CTRL_STATUS, AP_AUX_CTRL_OP_EN);
  836. return (ret | wait_aux_op_finish(ctx));
  837. }
  838. static int sp_tx_aux_rd(struct anx7625_data *ctx, u8 len_cmd)
  839. {
  840. int ret;
  841. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  842. AP_AUX_COMMAND, len_cmd);
  843. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p0_client,
  844. AP_AUX_CTRL_STATUS, AP_AUX_CTRL_OP_EN);
  845. return (ret | wait_aux_op_finish(ctx));
  846. }
  847. static int sp_tx_get_edid_block(struct anx7625_data *ctx)
  848. {
  849. int c = 0;
  850. struct device *dev = &ctx->client->dev;
  851. sp_tx_aux_wr(ctx, 0x7e);
  852. sp_tx_aux_rd(ctx, 0x01);
  853. c = anx7625_reg_read(ctx, ctx->i2c.rx_p0_client, AP_AUX_BUFF_START);
  854. if (c < 0) {
  855. DRM_DEV_ERROR(dev, "IO error : access AUX BUFF.\n");
  856. return -EIO;
  857. }
  858. DRM_DEV_DEBUG_DRIVER(dev, " EDID Block = %d\n", c + 1);
  859. if (c > MAX_EDID_BLOCK)
  860. c = 1;
  861. return c;
  862. }
  863. static int edid_read(struct anx7625_data *ctx,
  864. u8 offset, u8 *pblock_buf)
  865. {
  866. int ret, cnt;
  867. struct device *dev = &ctx->client->dev;
  868. for (cnt = 0; cnt <= EDID_TRY_CNT; cnt++) {
  869. sp_tx_aux_wr(ctx, offset);
  870. /* Set I2C read com 0x01 mot = 0 and read 16 bytes */
  871. ret = sp_tx_aux_rd(ctx, 0xf1);
  872. if (ret) {
  873. ret = sp_tx_rst_aux(ctx);
  874. DRM_DEV_DEBUG_DRIVER(dev, "edid read fail, reset!\n");
  875. } else {
  876. ret = anx7625_reg_block_read(ctx, ctx->i2c.rx_p0_client,
  877. AP_AUX_BUFF_START,
  878. MAX_DPCD_BUFFER_SIZE,
  879. pblock_buf);
  880. if (ret > 0)
  881. break;
  882. }
  883. }
  884. if (cnt > EDID_TRY_CNT)
  885. return -EIO;
  886. return ret;
  887. }
  888. static int segments_edid_read(struct anx7625_data *ctx,
  889. u8 segment, u8 *buf, u8 offset)
  890. {
  891. u8 cnt;
  892. int ret;
  893. struct device *dev = &ctx->client->dev;
  894. /* Write address only */
  895. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  896. AP_AUX_ADDR_7_0, 0x30);
  897. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  898. AP_AUX_COMMAND, 0x04);
  899. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  900. AP_AUX_CTRL_STATUS,
  901. AP_AUX_CTRL_ADDRONLY | AP_AUX_CTRL_OP_EN);
  902. ret |= wait_aux_op_finish(ctx);
  903. /* Write segment address */
  904. ret |= sp_tx_aux_wr(ctx, segment);
  905. /* Data read */
  906. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  907. AP_AUX_ADDR_7_0, 0x50);
  908. if (ret) {
  909. DRM_DEV_ERROR(dev, "IO error : aux initial fail.\n");
  910. return ret;
  911. }
  912. for (cnt = 0; cnt <= EDID_TRY_CNT; cnt++) {
  913. sp_tx_aux_wr(ctx, offset);
  914. /* Set I2C read com 0x01 mot = 0 and read 16 bytes */
  915. ret = sp_tx_aux_rd(ctx, 0xf1);
  916. if (ret) {
  917. ret = sp_tx_rst_aux(ctx);
  918. DRM_DEV_ERROR(dev, "segment read fail, reset!\n");
  919. } else {
  920. ret = anx7625_reg_block_read(ctx, ctx->i2c.rx_p0_client,
  921. AP_AUX_BUFF_START,
  922. MAX_DPCD_BUFFER_SIZE, buf);
  923. if (ret > 0)
  924. break;
  925. }
  926. }
  927. if (cnt > EDID_TRY_CNT)
  928. return -EIO;
  929. return ret;
  930. }
  931. static int sp_tx_edid_read(struct anx7625_data *ctx,
  932. u8 *pedid_blocks_buf)
  933. {
  934. u8 offset;
  935. int edid_pos;
  936. int count, blocks_num;
  937. u8 pblock_buf[MAX_DPCD_BUFFER_SIZE];
  938. u8 i, j;
  939. int g_edid_break = 0;
  940. int ret;
  941. struct device *dev = &ctx->client->dev;
  942. /* Address initial */
  943. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  944. AP_AUX_ADDR_7_0, 0x50);
  945. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  946. AP_AUX_ADDR_15_8, 0);
  947. ret |= anx7625_write_and(ctx, ctx->i2c.rx_p0_client,
  948. AP_AUX_ADDR_19_16, 0xf0);
  949. if (ret < 0) {
  950. DRM_DEV_ERROR(dev, "access aux channel IO error.\n");
  951. return -EIO;
  952. }
  953. blocks_num = sp_tx_get_edid_block(ctx);
  954. if (blocks_num < 0)
  955. return blocks_num;
  956. count = 0;
  957. do {
  958. switch (count) {
  959. case 0:
  960. case 1:
  961. for (i = 0; i < 8; i++) {
  962. offset = (i + count * 8) * MAX_DPCD_BUFFER_SIZE;
  963. g_edid_break = edid_read(ctx, offset,
  964. pblock_buf);
  965. if (g_edid_break < 0)
  966. break;
  967. memcpy(&pedid_blocks_buf[offset],
  968. pblock_buf,
  969. MAX_DPCD_BUFFER_SIZE);
  970. }
  971. break;
  972. case 2:
  973. offset = 0x00;
  974. for (j = 0; j < 8; j++) {
  975. edid_pos = (j + count * 8) *
  976. MAX_DPCD_BUFFER_SIZE;
  977. if (g_edid_break == 1)
  978. break;
  979. ret = segments_edid_read(ctx, count / 2,
  980. pblock_buf, offset);
  981. if (ret < 0)
  982. return ret;
  983. memcpy(&pedid_blocks_buf[edid_pos],
  984. pblock_buf,
  985. MAX_DPCD_BUFFER_SIZE);
  986. offset = offset + 0x10;
  987. }
  988. break;
  989. case 3:
  990. offset = 0x80;
  991. for (j = 0; j < 8; j++) {
  992. edid_pos = (j + count * 8) *
  993. MAX_DPCD_BUFFER_SIZE;
  994. if (g_edid_break == 1)
  995. break;
  996. ret = segments_edid_read(ctx, count / 2,
  997. pblock_buf, offset);
  998. if (ret < 0)
  999. return ret;
  1000. memcpy(&pedid_blocks_buf[edid_pos],
  1001. pblock_buf,
  1002. MAX_DPCD_BUFFER_SIZE);
  1003. offset = offset + 0x10;
  1004. }
  1005. break;
  1006. default:
  1007. break;
  1008. }
  1009. count++;
  1010. } while (blocks_num >= count);
  1011. /* Check edid data */
  1012. if (!drm_edid_is_valid((struct edid *)pedid_blocks_buf)) {
  1013. DRM_DEV_ERROR(dev, "WARNING! edid check fail!\n");
  1014. return -EINVAL;
  1015. }
  1016. /* Reset aux channel */
  1017. ret = sp_tx_rst_aux(ctx);
  1018. if (ret < 0) {
  1019. DRM_DEV_ERROR(dev, "Failed to reset aux channel!\n");
  1020. return ret;
  1021. }
  1022. return (blocks_num + 1);
  1023. }
  1024. static void anx7625_power_on(struct anx7625_data *ctx)
  1025. {
  1026. struct device *dev = &ctx->client->dev;
  1027. int ret, i;
  1028. if (!ctx->pdata.low_power_mode) {
  1029. DRM_DEV_DEBUG_DRIVER(dev, "not low power mode!\n");
  1030. return;
  1031. }
  1032. for (i = 0; i < ARRAY_SIZE(ctx->pdata.supplies); i++) {
  1033. ret = regulator_enable(ctx->pdata.supplies[i].consumer);
  1034. if (ret < 0) {
  1035. DRM_DEV_DEBUG_DRIVER(dev, "cannot enable supply %d: %d\n",
  1036. i, ret);
  1037. goto reg_err;
  1038. }
  1039. usleep_range(2000, 2100);
  1040. }
  1041. usleep_range(11000, 12000);
  1042. /* Power on pin enable */
  1043. gpiod_set_value(ctx->pdata.gpio_p_on, 1);
  1044. usleep_range(10000, 11000);
  1045. /* Power reset pin enable */
  1046. gpiod_set_value(ctx->pdata.gpio_reset, 1);
  1047. usleep_range(10000, 11000);
  1048. DRM_DEV_DEBUG_DRIVER(dev, "power on !\n");
  1049. return;
  1050. reg_err:
  1051. for (--i; i >= 0; i--)
  1052. regulator_disable(ctx->pdata.supplies[i].consumer);
  1053. }
  1054. static void anx7625_power_standby(struct anx7625_data *ctx)
  1055. {
  1056. struct device *dev = &ctx->client->dev;
  1057. int ret;
  1058. if (!ctx->pdata.low_power_mode) {
  1059. DRM_DEV_DEBUG_DRIVER(dev, "not low power mode!\n");
  1060. return;
  1061. }
  1062. gpiod_set_value(ctx->pdata.gpio_reset, 0);
  1063. usleep_range(1000, 1100);
  1064. gpiod_set_value(ctx->pdata.gpio_p_on, 0);
  1065. usleep_range(1000, 1100);
  1066. ret = regulator_bulk_disable(ARRAY_SIZE(ctx->pdata.supplies),
  1067. ctx->pdata.supplies);
  1068. if (ret < 0)
  1069. DRM_DEV_DEBUG_DRIVER(dev, "cannot disable supplies %d\n", ret);
  1070. DRM_DEV_DEBUG_DRIVER(dev, "power down\n");
  1071. }
  1072. /* Basic configurations of ANX7625 */
  1073. static void anx7625_config(struct anx7625_data *ctx)
  1074. {
  1075. anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  1076. XTAL_FRQ_SEL, XTAL_FRQ_27M);
  1077. }
  1078. static void anx7625_disable_pd_protocol(struct anx7625_data *ctx)
  1079. {
  1080. struct device *dev = &ctx->client->dev;
  1081. int ret;
  1082. /* Reset main ocm */
  1083. ret = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client, 0x88, 0x40);
  1084. /* Disable PD */
  1085. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  1086. AP_AV_STATUS, AP_DISABLE_PD);
  1087. /* Release main ocm */
  1088. ret |= anx7625_reg_write(ctx, ctx->i2c.rx_p0_client, 0x88, 0x00);
  1089. if (ret < 0)
  1090. DRM_DEV_DEBUG_DRIVER(dev, "disable PD feature fail.\n");
  1091. else
  1092. DRM_DEV_DEBUG_DRIVER(dev, "disable PD feature succeeded.\n");
  1093. }
  1094. static int anx7625_ocm_loading_check(struct anx7625_data *ctx)
  1095. {
  1096. int ret;
  1097. struct device *dev = &ctx->client->dev;
  1098. /* Check interface workable */
  1099. ret = anx7625_reg_read(ctx, ctx->i2c.rx_p0_client,
  1100. FLASH_LOAD_STA);
  1101. if (ret < 0) {
  1102. DRM_DEV_ERROR(dev, "IO error : access flash load.\n");
  1103. return ret;
  1104. }
  1105. if ((ret & FLASH_LOAD_STA_CHK) != FLASH_LOAD_STA_CHK)
  1106. return -ENODEV;
  1107. anx7625_disable_pd_protocol(ctx);
  1108. DRM_DEV_DEBUG_DRIVER(dev, "Firmware ver %02x%02x,",
  1109. anx7625_reg_read(ctx,
  1110. ctx->i2c.rx_p0_client,
  1111. OCM_FW_VERSION),
  1112. anx7625_reg_read(ctx,
  1113. ctx->i2c.rx_p0_client,
  1114. OCM_FW_REVERSION));
  1115. DRM_DEV_DEBUG_DRIVER(dev, "Driver version %s\n",
  1116. ANX7625_DRV_VERSION);
  1117. return 0;
  1118. }
  1119. static void anx7625_power_on_init(struct anx7625_data *ctx)
  1120. {
  1121. int retry_count, i;
  1122. for (retry_count = 0; retry_count < 3; retry_count++) {
  1123. anx7625_power_on(ctx);
  1124. anx7625_config(ctx);
  1125. for (i = 0; i < OCM_LOADING_TIME; i++) {
  1126. if (!anx7625_ocm_loading_check(ctx))
  1127. return;
  1128. usleep_range(1000, 1100);
  1129. }
  1130. anx7625_power_standby(ctx);
  1131. }
  1132. }
  1133. static void anx7625_init_gpio(struct anx7625_data *platform)
  1134. {
  1135. struct device *dev = &platform->client->dev;
  1136. DRM_DEV_DEBUG_DRIVER(dev, "init gpio\n");
  1137. /* Gpio for chip power enable */
  1138. platform->pdata.gpio_p_on =
  1139. devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW);
  1140. if (IS_ERR_OR_NULL(platform->pdata.gpio_p_on)) {
  1141. DRM_DEV_DEBUG_DRIVER(dev, "no enable gpio found\n");
  1142. platform->pdata.gpio_p_on = NULL;
  1143. }
  1144. /* Gpio for chip reset */
  1145. platform->pdata.gpio_reset =
  1146. devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  1147. if (IS_ERR_OR_NULL(platform->pdata.gpio_reset)) {
  1148. DRM_DEV_DEBUG_DRIVER(dev, "no reset gpio found\n");
  1149. platform->pdata.gpio_reset = NULL;
  1150. }
  1151. if (platform->pdata.gpio_p_on && platform->pdata.gpio_reset) {
  1152. platform->pdata.low_power_mode = 1;
  1153. DRM_DEV_DEBUG_DRIVER(dev, "low power mode, pon %d, reset %d.\n",
  1154. desc_to_gpio(platform->pdata.gpio_p_on),
  1155. desc_to_gpio(platform->pdata.gpio_reset));
  1156. } else {
  1157. platform->pdata.low_power_mode = 0;
  1158. DRM_DEV_DEBUG_DRIVER(dev, "not low power mode.\n");
  1159. }
  1160. }
  1161. static void anx7625_stop_dp_work(struct anx7625_data *ctx)
  1162. {
  1163. ctx->hpd_status = 0;
  1164. ctx->hpd_high_cnt = 0;
  1165. ctx->display_timing_valid = 0;
  1166. }
  1167. static void anx7625_start_dp_work(struct anx7625_data *ctx)
  1168. {
  1169. int ret;
  1170. struct device *dev = &ctx->client->dev;
  1171. if (ctx->hpd_high_cnt >= 2) {
  1172. DRM_DEV_DEBUG_DRIVER(dev, "filter useless HPD\n");
  1173. return;
  1174. }
  1175. ctx->hpd_status = 1;
  1176. ctx->hpd_high_cnt++;
  1177. /* Not support HDCP */
  1178. ret = anx7625_write_and(ctx, ctx->i2c.rx_p1_client, 0xee, 0x9f);
  1179. /* Try auth flag */
  1180. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p1_client, 0xec, 0x10);
  1181. /* Interrupt for DRM */
  1182. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p1_client, 0xff, 0x01);
  1183. if (ret < 0) {
  1184. DRM_DEV_ERROR(dev, "fail to setting HDCP/auth\n");
  1185. return;
  1186. }
  1187. ret = anx7625_reg_read(ctx, ctx->i2c.rx_p1_client, 0x86);
  1188. if (ret < 0)
  1189. return;
  1190. DRM_DEV_DEBUG_DRIVER(dev, "Secure OCM version=%02x\n", ret);
  1191. ctx->display_timing_valid = 1;
  1192. anx7625_dp_start(ctx);
  1193. }
  1194. static int anx7625_read_hpd_status_p0(struct anx7625_data *ctx)
  1195. {
  1196. int ret;
  1197. /* Set irq detect window to 2ms */
  1198. ret = anx7625_reg_write(ctx, ctx->i2c.tx_p2_client,
  1199. HPD_DET_TIMER_BIT0_7, HPD_TIME & 0xFF);
  1200. ret |= anx7625_reg_write(ctx, ctx->i2c.tx_p2_client,
  1201. HPD_DET_TIMER_BIT8_15,
  1202. (HPD_TIME >> 8) & 0xFF);
  1203. ret |= anx7625_reg_write(ctx, ctx->i2c.tx_p2_client,
  1204. HPD_DET_TIMER_BIT16_23,
  1205. (HPD_TIME >> 16) & 0xFF);
  1206. if (ret < 0)
  1207. return ret;
  1208. return anx7625_reg_read(ctx, ctx->i2c.rx_p0_client, SYSTEM_STSTUS);
  1209. }
  1210. static int _anx7625_hpd_polling(struct anx7625_data *ctx,
  1211. unsigned long wait_us)
  1212. {
  1213. int ret, val;
  1214. struct device *dev = &ctx->client->dev;
  1215. ret = readx_poll_timeout(anx7625_read_hpd_status_p0,
  1216. ctx, val,
  1217. ((val & HPD_STATUS) || (val < 0)),
  1218. wait_us / 100,
  1219. wait_us);
  1220. if (ret) {
  1221. DRM_DEV_ERROR(dev, "no hpd.\n");
  1222. return ret;
  1223. }
  1224. DRM_DEV_DEBUG_DRIVER(dev, "system status: 0x%x. HPD raise up.\n", val);
  1225. anx7625_reg_write(ctx, ctx->i2c.tcpc_client,
  1226. INTR_ALERT_1, 0xFF);
  1227. anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  1228. INTERFACE_CHANGE_INT, 0);
  1229. anx7625_start_dp_work(ctx);
  1230. return 0;
  1231. }
  1232. static int anx7625_wait_hpd_asserted(struct drm_dp_aux *aux,
  1233. unsigned long wait_us)
  1234. {
  1235. struct anx7625_data *ctx = container_of(aux, struct anx7625_data, aux);
  1236. struct device *dev = &ctx->client->dev;
  1237. int ret;
  1238. pm_runtime_get_sync(dev);
  1239. ret = _anx7625_hpd_polling(ctx, wait_us);
  1240. pm_runtime_mark_last_busy(dev);
  1241. pm_runtime_put_autosuspend(dev);
  1242. return ret;
  1243. }
  1244. static void anx7625_remove_edid(struct anx7625_data *ctx)
  1245. {
  1246. ctx->slimport_edid_p.edid_block_num = -1;
  1247. }
  1248. static void anx7625_dp_adjust_swing(struct anx7625_data *ctx)
  1249. {
  1250. int i;
  1251. for (i = 0; i < ctx->pdata.dp_lane0_swing_reg_cnt; i++)
  1252. anx7625_reg_write(ctx, ctx->i2c.tx_p1_client,
  1253. DP_TX_LANE0_SWING_REG0 + i,
  1254. ctx->pdata.lane0_reg_data[i]);
  1255. for (i = 0; i < ctx->pdata.dp_lane1_swing_reg_cnt; i++)
  1256. anx7625_reg_write(ctx, ctx->i2c.tx_p1_client,
  1257. DP_TX_LANE1_SWING_REG0 + i,
  1258. ctx->pdata.lane1_reg_data[i]);
  1259. }
  1260. static void dp_hpd_change_handler(struct anx7625_data *ctx, bool on)
  1261. {
  1262. struct device *dev = &ctx->client->dev;
  1263. /* HPD changed */
  1264. DRM_DEV_DEBUG_DRIVER(dev, "dp_hpd_change_default_func: %d\n",
  1265. (u32)on);
  1266. if (on == 0) {
  1267. DRM_DEV_DEBUG_DRIVER(dev, " HPD low\n");
  1268. anx7625_remove_edid(ctx);
  1269. anx7625_stop_dp_work(ctx);
  1270. } else {
  1271. DRM_DEV_DEBUG_DRIVER(dev, " HPD high\n");
  1272. anx7625_start_dp_work(ctx);
  1273. anx7625_dp_adjust_swing(ctx);
  1274. }
  1275. }
  1276. static int anx7625_hpd_change_detect(struct anx7625_data *ctx)
  1277. {
  1278. int intr_vector, status;
  1279. struct device *dev = &ctx->client->dev;
  1280. status = anx7625_reg_write(ctx, ctx->i2c.tcpc_client,
  1281. INTR_ALERT_1, 0xFF);
  1282. if (status < 0) {
  1283. DRM_DEV_ERROR(dev, "cannot clear alert reg.\n");
  1284. return status;
  1285. }
  1286. intr_vector = anx7625_reg_read(ctx, ctx->i2c.rx_p0_client,
  1287. INTERFACE_CHANGE_INT);
  1288. if (intr_vector < 0) {
  1289. DRM_DEV_ERROR(dev, "cannot access interrupt change reg.\n");
  1290. return intr_vector;
  1291. }
  1292. DRM_DEV_DEBUG_DRIVER(dev, "0x7e:0x44=%x\n", intr_vector);
  1293. status = anx7625_reg_write(ctx, ctx->i2c.rx_p0_client,
  1294. INTERFACE_CHANGE_INT,
  1295. intr_vector & (~intr_vector));
  1296. if (status < 0) {
  1297. DRM_DEV_ERROR(dev, "cannot clear interrupt change reg.\n");
  1298. return status;
  1299. }
  1300. if (!(intr_vector & HPD_STATUS_CHANGE))
  1301. return -ENOENT;
  1302. status = anx7625_reg_read(ctx, ctx->i2c.rx_p0_client,
  1303. SYSTEM_STSTUS);
  1304. if (status < 0) {
  1305. DRM_DEV_ERROR(dev, "cannot clear interrupt status.\n");
  1306. return status;
  1307. }
  1308. DRM_DEV_DEBUG_DRIVER(dev, "0x7e:0x45=%x\n", status);
  1309. dp_hpd_change_handler(ctx, status & HPD_STATUS);
  1310. return 0;
  1311. }
  1312. static void anx7625_work_func(struct work_struct *work)
  1313. {
  1314. int event;
  1315. struct anx7625_data *ctx = container_of(work,
  1316. struct anx7625_data, work);
  1317. mutex_lock(&ctx->lock);
  1318. if (pm_runtime_suspended(&ctx->client->dev))
  1319. goto unlock;
  1320. event = anx7625_hpd_change_detect(ctx);
  1321. if (event < 0)
  1322. goto unlock;
  1323. unlock:
  1324. mutex_unlock(&ctx->lock);
  1325. }
  1326. static irqreturn_t anx7625_intr_hpd_isr(int irq, void *data)
  1327. {
  1328. struct anx7625_data *ctx = (struct anx7625_data *)data;
  1329. queue_work(ctx->workqueue, &ctx->work);
  1330. return IRQ_HANDLED;
  1331. }
  1332. static int anx7625_get_swing_setting(struct device *dev,
  1333. struct anx7625_platform_data *pdata)
  1334. {
  1335. int num_regs;
  1336. if (of_get_property(dev->of_node,
  1337. "analogix,lane0-swing", &num_regs)) {
  1338. if (num_regs > DP_TX_SWING_REG_CNT)
  1339. num_regs = DP_TX_SWING_REG_CNT;
  1340. pdata->dp_lane0_swing_reg_cnt = num_regs;
  1341. of_property_read_u8_array(dev->of_node, "analogix,lane0-swing",
  1342. pdata->lane0_reg_data, num_regs);
  1343. }
  1344. if (of_get_property(dev->of_node,
  1345. "analogix,lane1-swing", &num_regs)) {
  1346. if (num_regs > DP_TX_SWING_REG_CNT)
  1347. num_regs = DP_TX_SWING_REG_CNT;
  1348. pdata->dp_lane1_swing_reg_cnt = num_regs;
  1349. of_property_read_u8_array(dev->of_node, "analogix,lane1-swing",
  1350. pdata->lane1_reg_data, num_regs);
  1351. }
  1352. return 0;
  1353. }
  1354. static int anx7625_parse_dt(struct device *dev,
  1355. struct anx7625_platform_data *pdata)
  1356. {
  1357. struct device_node *np = dev->of_node, *ep0;
  1358. int bus_type, mipi_lanes;
  1359. anx7625_get_swing_setting(dev, pdata);
  1360. pdata->is_dpi = 0; /* default dsi mode */
  1361. of_node_put(pdata->mipi_host_node);
  1362. pdata->mipi_host_node = of_graph_get_remote_node(np, 0, 0);
  1363. if (!pdata->mipi_host_node) {
  1364. DRM_DEV_ERROR(dev, "fail to get internal panel.\n");
  1365. return -ENODEV;
  1366. }
  1367. bus_type = 0;
  1368. mipi_lanes = MAX_LANES_SUPPORT;
  1369. ep0 = of_graph_get_endpoint_by_regs(np, 0, 0);
  1370. if (ep0) {
  1371. if (of_property_read_u32(ep0, "bus-type", &bus_type))
  1372. bus_type = 0;
  1373. mipi_lanes = drm_of_get_data_lanes_count(ep0, 1, MAX_LANES_SUPPORT);
  1374. of_node_put(ep0);
  1375. }
  1376. if (bus_type == V4L2_FWNODE_BUS_TYPE_DPI) /* bus type is DPI */
  1377. pdata->is_dpi = 1;
  1378. pdata->mipi_lanes = MAX_LANES_SUPPORT;
  1379. if (mipi_lanes > 0)
  1380. pdata->mipi_lanes = mipi_lanes;
  1381. if (pdata->is_dpi)
  1382. DRM_DEV_DEBUG_DRIVER(dev, "found MIPI DPI host node.\n");
  1383. else
  1384. DRM_DEV_DEBUG_DRIVER(dev, "found MIPI DSI host node.\n");
  1385. if (of_property_read_bool(np, "analogix,audio-enable"))
  1386. pdata->audio_en = 1;
  1387. return 0;
  1388. }
  1389. static int anx7625_parse_dt_panel(struct device *dev,
  1390. struct anx7625_platform_data *pdata)
  1391. {
  1392. struct device_node *np = dev->of_node;
  1393. pdata->panel_bridge = devm_drm_of_get_bridge(dev, np, 1, 0);
  1394. if (IS_ERR(pdata->panel_bridge)) {
  1395. if (PTR_ERR(pdata->panel_bridge) == -ENODEV) {
  1396. pdata->panel_bridge = NULL;
  1397. return 0;
  1398. }
  1399. return PTR_ERR(pdata->panel_bridge);
  1400. }
  1401. DRM_DEV_DEBUG_DRIVER(dev, "get panel node.\n");
  1402. return 0;
  1403. }
  1404. static bool anx7625_of_panel_on_aux_bus(struct device *dev)
  1405. {
  1406. struct device_node *bus, *panel;
  1407. bus = of_get_child_by_name(dev->of_node, "aux-bus");
  1408. if (!bus)
  1409. return false;
  1410. panel = of_get_child_by_name(bus, "panel");
  1411. of_node_put(bus);
  1412. if (!panel)
  1413. return false;
  1414. of_node_put(panel);
  1415. return true;
  1416. }
  1417. static inline struct anx7625_data *bridge_to_anx7625(struct drm_bridge *bridge)
  1418. {
  1419. return container_of(bridge, struct anx7625_data, bridge);
  1420. }
  1421. static ssize_t anx7625_aux_transfer(struct drm_dp_aux *aux,
  1422. struct drm_dp_aux_msg *msg)
  1423. {
  1424. struct anx7625_data *ctx = container_of(aux, struct anx7625_data, aux);
  1425. struct device *dev = &ctx->client->dev;
  1426. u8 request = msg->request & ~DP_AUX_I2C_MOT;
  1427. int ret = 0;
  1428. pm_runtime_get_sync(dev);
  1429. msg->reply = 0;
  1430. switch (request) {
  1431. case DP_AUX_NATIVE_WRITE:
  1432. case DP_AUX_I2C_WRITE:
  1433. case DP_AUX_NATIVE_READ:
  1434. case DP_AUX_I2C_READ:
  1435. break;
  1436. default:
  1437. ret = -EINVAL;
  1438. }
  1439. if (!ret)
  1440. ret = anx7625_aux_trans(ctx, msg->request, msg->address,
  1441. msg->size, msg->buffer);
  1442. pm_runtime_mark_last_busy(dev);
  1443. pm_runtime_put_autosuspend(dev);
  1444. return ret;
  1445. }
  1446. static struct edid *anx7625_get_edid(struct anx7625_data *ctx)
  1447. {
  1448. struct device *dev = &ctx->client->dev;
  1449. struct s_edid_data *p_edid = &ctx->slimport_edid_p;
  1450. int edid_num;
  1451. u8 *edid;
  1452. edid = kmalloc(FOUR_BLOCK_SIZE, GFP_KERNEL);
  1453. if (!edid) {
  1454. DRM_DEV_ERROR(dev, "Fail to allocate buffer\n");
  1455. return NULL;
  1456. }
  1457. if (ctx->slimport_edid_p.edid_block_num > 0) {
  1458. memcpy(edid, ctx->slimport_edid_p.edid_raw_data,
  1459. FOUR_BLOCK_SIZE);
  1460. return (struct edid *)edid;
  1461. }
  1462. pm_runtime_get_sync(dev);
  1463. _anx7625_hpd_polling(ctx, 5000 * 2);
  1464. edid_num = sp_tx_edid_read(ctx, p_edid->edid_raw_data);
  1465. pm_runtime_put_sync(dev);
  1466. if (edid_num < 1) {
  1467. DRM_DEV_ERROR(dev, "Fail to read EDID: %d\n", edid_num);
  1468. kfree(edid);
  1469. return NULL;
  1470. }
  1471. p_edid->edid_block_num = edid_num;
  1472. memcpy(edid, ctx->slimport_edid_p.edid_raw_data, FOUR_BLOCK_SIZE);
  1473. return (struct edid *)edid;
  1474. }
  1475. static enum drm_connector_status anx7625_sink_detect(struct anx7625_data *ctx)
  1476. {
  1477. struct device *dev = &ctx->client->dev;
  1478. DRM_DEV_DEBUG_DRIVER(dev, "sink detect\n");
  1479. if (ctx->pdata.panel_bridge)
  1480. return connector_status_connected;
  1481. return ctx->hpd_status ? connector_status_connected :
  1482. connector_status_disconnected;
  1483. }
  1484. static int anx7625_audio_hw_params(struct device *dev, void *data,
  1485. struct hdmi_codec_daifmt *fmt,
  1486. struct hdmi_codec_params *params)
  1487. {
  1488. struct anx7625_data *ctx = dev_get_drvdata(dev);
  1489. int wl, ch, rate;
  1490. int ret = 0;
  1491. if (anx7625_sink_detect(ctx) == connector_status_disconnected) {
  1492. DRM_DEV_DEBUG_DRIVER(dev, "DP not connected\n");
  1493. return 0;
  1494. }
  1495. if (fmt->fmt != HDMI_DSP_A && fmt->fmt != HDMI_I2S) {
  1496. DRM_DEV_ERROR(dev, "only supports DSP_A & I2S\n");
  1497. return -EINVAL;
  1498. }
  1499. DRM_DEV_DEBUG_DRIVER(dev, "setting %d Hz, %d bit, %d channels\n",
  1500. params->sample_rate, params->sample_width,
  1501. params->cea.channels);
  1502. if (fmt->fmt == HDMI_DSP_A)
  1503. ret = anx7625_write_and_or(ctx, ctx->i2c.tx_p2_client,
  1504. AUDIO_CHANNEL_STATUS_6,
  1505. ~I2S_SLAVE_MODE,
  1506. TDM_SLAVE_MODE);
  1507. else
  1508. ret = anx7625_write_and_or(ctx, ctx->i2c.tx_p2_client,
  1509. AUDIO_CHANNEL_STATUS_6,
  1510. ~TDM_SLAVE_MODE,
  1511. I2S_SLAVE_MODE);
  1512. /* Word length */
  1513. switch (params->sample_width) {
  1514. case 16:
  1515. wl = AUDIO_W_LEN_16_20MAX;
  1516. break;
  1517. case 18:
  1518. wl = AUDIO_W_LEN_18_20MAX;
  1519. break;
  1520. case 20:
  1521. wl = AUDIO_W_LEN_20_20MAX;
  1522. break;
  1523. case 24:
  1524. wl = AUDIO_W_LEN_24_24MAX;
  1525. break;
  1526. default:
  1527. DRM_DEV_DEBUG_DRIVER(dev, "wordlength: %d bit not support",
  1528. params->sample_width);
  1529. return -EINVAL;
  1530. }
  1531. ret |= anx7625_write_and_or(ctx, ctx->i2c.tx_p2_client,
  1532. AUDIO_CHANNEL_STATUS_5,
  1533. 0xf0, wl);
  1534. /* Channel num */
  1535. switch (params->cea.channels) {
  1536. case 2:
  1537. ch = I2S_CH_2;
  1538. break;
  1539. case 4:
  1540. ch = TDM_CH_4;
  1541. break;
  1542. case 6:
  1543. ch = TDM_CH_6;
  1544. break;
  1545. case 8:
  1546. ch = TDM_CH_8;
  1547. break;
  1548. default:
  1549. DRM_DEV_DEBUG_DRIVER(dev, "channel number: %d not support",
  1550. params->cea.channels);
  1551. return -EINVAL;
  1552. }
  1553. ret |= anx7625_write_and_or(ctx, ctx->i2c.tx_p2_client,
  1554. AUDIO_CHANNEL_STATUS_6, 0x1f, ch << 5);
  1555. if (ch > I2S_CH_2)
  1556. ret |= anx7625_write_or(ctx, ctx->i2c.tx_p2_client,
  1557. AUDIO_CHANNEL_STATUS_6, AUDIO_LAYOUT);
  1558. else
  1559. ret |= anx7625_write_and(ctx, ctx->i2c.tx_p2_client,
  1560. AUDIO_CHANNEL_STATUS_6, ~AUDIO_LAYOUT);
  1561. /* FS */
  1562. switch (params->sample_rate) {
  1563. case 32000:
  1564. rate = AUDIO_FS_32K;
  1565. break;
  1566. case 44100:
  1567. rate = AUDIO_FS_441K;
  1568. break;
  1569. case 48000:
  1570. rate = AUDIO_FS_48K;
  1571. break;
  1572. case 88200:
  1573. rate = AUDIO_FS_882K;
  1574. break;
  1575. case 96000:
  1576. rate = AUDIO_FS_96K;
  1577. break;
  1578. case 176400:
  1579. rate = AUDIO_FS_1764K;
  1580. break;
  1581. case 192000:
  1582. rate = AUDIO_FS_192K;
  1583. break;
  1584. default:
  1585. DRM_DEV_DEBUG_DRIVER(dev, "sample rate: %d not support",
  1586. params->sample_rate);
  1587. return -EINVAL;
  1588. }
  1589. ret |= anx7625_write_and_or(ctx, ctx->i2c.tx_p2_client,
  1590. AUDIO_CHANNEL_STATUS_4,
  1591. 0xf0, rate);
  1592. ret |= anx7625_write_or(ctx, ctx->i2c.rx_p0_client,
  1593. AP_AV_STATUS, AP_AUDIO_CHG);
  1594. if (ret < 0) {
  1595. DRM_DEV_ERROR(dev, "IO error : config audio.\n");
  1596. return -EIO;
  1597. }
  1598. return 0;
  1599. }
  1600. static void anx7625_audio_shutdown(struct device *dev, void *data)
  1601. {
  1602. DRM_DEV_DEBUG_DRIVER(dev, "stop audio\n");
  1603. }
  1604. static int anx7625_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
  1605. struct device_node *endpoint)
  1606. {
  1607. struct of_endpoint of_ep;
  1608. int ret;
  1609. ret = of_graph_parse_endpoint(endpoint, &of_ep);
  1610. if (ret < 0)
  1611. return ret;
  1612. /*
  1613. * HDMI sound should be located at external DPI port
  1614. * Didn't have good way to check where is internal(DSI)
  1615. * or external(DPI) bridge
  1616. */
  1617. return 0;
  1618. }
  1619. static void
  1620. anx7625_audio_update_connector_status(struct anx7625_data *ctx,
  1621. enum drm_connector_status status)
  1622. {
  1623. if (ctx->plugged_cb && ctx->codec_dev) {
  1624. ctx->plugged_cb(ctx->codec_dev,
  1625. status == connector_status_connected);
  1626. }
  1627. }
  1628. static int anx7625_audio_hook_plugged_cb(struct device *dev, void *data,
  1629. hdmi_codec_plugged_cb fn,
  1630. struct device *codec_dev)
  1631. {
  1632. struct anx7625_data *ctx = data;
  1633. ctx->plugged_cb = fn;
  1634. ctx->codec_dev = codec_dev;
  1635. anx7625_audio_update_connector_status(ctx, anx7625_sink_detect(ctx));
  1636. return 0;
  1637. }
  1638. static int anx7625_audio_get_eld(struct device *dev, void *data,
  1639. u8 *buf, size_t len)
  1640. {
  1641. struct anx7625_data *ctx = dev_get_drvdata(dev);
  1642. if (!ctx->connector) {
  1643. /* Pass en empty ELD if connector not available */
  1644. memset(buf, 0, len);
  1645. } else {
  1646. dev_dbg(dev, "audio copy eld\n");
  1647. memcpy(buf, ctx->connector->eld,
  1648. min(sizeof(ctx->connector->eld), len));
  1649. }
  1650. return 0;
  1651. }
  1652. static const struct hdmi_codec_ops anx7625_codec_ops = {
  1653. .hw_params = anx7625_audio_hw_params,
  1654. .audio_shutdown = anx7625_audio_shutdown,
  1655. .get_eld = anx7625_audio_get_eld,
  1656. .get_dai_id = anx7625_hdmi_i2s_get_dai_id,
  1657. .hook_plugged_cb = anx7625_audio_hook_plugged_cb,
  1658. };
  1659. static void anx7625_unregister_audio(struct anx7625_data *ctx)
  1660. {
  1661. struct device *dev = &ctx->client->dev;
  1662. if (ctx->audio_pdev) {
  1663. platform_device_unregister(ctx->audio_pdev);
  1664. ctx->audio_pdev = NULL;
  1665. }
  1666. DRM_DEV_DEBUG_DRIVER(dev, "unbound to %s", HDMI_CODEC_DRV_NAME);
  1667. }
  1668. static int anx7625_register_audio(struct device *dev, struct anx7625_data *ctx)
  1669. {
  1670. struct hdmi_codec_pdata codec_data = {
  1671. .ops = &anx7625_codec_ops,
  1672. .max_i2s_channels = 8,
  1673. .i2s = 1,
  1674. .data = ctx,
  1675. };
  1676. ctx->audio_pdev = platform_device_register_data(dev,
  1677. HDMI_CODEC_DRV_NAME,
  1678. PLATFORM_DEVID_AUTO,
  1679. &codec_data,
  1680. sizeof(codec_data));
  1681. if (IS_ERR(ctx->audio_pdev))
  1682. return PTR_ERR(ctx->audio_pdev);
  1683. DRM_DEV_DEBUG_DRIVER(dev, "bound to %s", HDMI_CODEC_DRV_NAME);
  1684. return 0;
  1685. }
  1686. static int anx7625_setup_dsi_device(struct anx7625_data *ctx)
  1687. {
  1688. struct mipi_dsi_device *dsi;
  1689. struct device *dev = &ctx->client->dev;
  1690. struct mipi_dsi_host *host;
  1691. const struct mipi_dsi_device_info info = {
  1692. .type = "anx7625",
  1693. .channel = ctx->channel,
  1694. .node = NULL,
  1695. };
  1696. host = of_find_mipi_dsi_host_by_node(ctx->pdata.mipi_host_node);
  1697. if (!host) {
  1698. DRM_DEV_ERROR(dev, "fail to find dsi host.\n");
  1699. return -EPROBE_DEFER;
  1700. }
  1701. dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
  1702. if (IS_ERR(dsi)) {
  1703. DRM_DEV_ERROR(dev, "fail to create dsi device.\n");
  1704. return -EINVAL;
  1705. }
  1706. dsi->lanes = ctx->pdata.mipi_lanes;
  1707. dsi->format = MIPI_DSI_FMT_RGB888;
  1708. dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
  1709. MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
  1710. MIPI_DSI_MODE_VIDEO_HSE |
  1711. MIPI_DSI_HS_PKT_END_ALIGNED;
  1712. ctx->dsi = dsi;
  1713. return 0;
  1714. }
  1715. static int anx7625_attach_dsi(struct anx7625_data *ctx)
  1716. {
  1717. struct device *dev = &ctx->client->dev;
  1718. int ret;
  1719. DRM_DEV_DEBUG_DRIVER(dev, "attach dsi\n");
  1720. ret = devm_mipi_dsi_attach(dev, ctx->dsi);
  1721. if (ret) {
  1722. DRM_DEV_ERROR(dev, "fail to attach dsi to host.\n");
  1723. return ret;
  1724. }
  1725. DRM_DEV_DEBUG_DRIVER(dev, "attach dsi succeeded.\n");
  1726. return 0;
  1727. }
  1728. static void hdcp_check_work_func(struct work_struct *work)
  1729. {
  1730. u8 status;
  1731. struct delayed_work *dwork;
  1732. struct anx7625_data *ctx;
  1733. struct device *dev;
  1734. struct drm_device *drm_dev;
  1735. dwork = to_delayed_work(work);
  1736. ctx = container_of(dwork, struct anx7625_data, hdcp_work);
  1737. dev = &ctx->client->dev;
  1738. if (!ctx->connector) {
  1739. dev_err(dev, "HDCP connector is null!");
  1740. return;
  1741. }
  1742. drm_dev = ctx->connector->dev;
  1743. drm_modeset_lock(&drm_dev->mode_config.connection_mutex, NULL);
  1744. mutex_lock(&ctx->hdcp_wq_lock);
  1745. status = anx7625_reg_read(ctx, ctx->i2c.tx_p0_client, 0);
  1746. dev_dbg(dev, "sink HDCP status check: %.02x\n", status);
  1747. if (status & BIT(1)) {
  1748. ctx->hdcp_cp = DRM_MODE_CONTENT_PROTECTION_ENABLED;
  1749. drm_hdcp_update_content_protection(ctx->connector,
  1750. ctx->hdcp_cp);
  1751. dev_dbg(dev, "update CP to ENABLE\n");
  1752. }
  1753. mutex_unlock(&ctx->hdcp_wq_lock);
  1754. drm_modeset_unlock(&drm_dev->mode_config.connection_mutex);
  1755. }
  1756. static int anx7625_connector_atomic_check(struct anx7625_data *ctx,
  1757. struct drm_connector_state *state)
  1758. {
  1759. struct device *dev = &ctx->client->dev;
  1760. int cp;
  1761. dev_dbg(dev, "hdcp state check\n");
  1762. cp = state->content_protection;
  1763. if (cp == ctx->hdcp_cp)
  1764. return 0;
  1765. if (cp == DRM_MODE_CONTENT_PROTECTION_DESIRED) {
  1766. if (ctx->dp_en) {
  1767. dev_dbg(dev, "enable HDCP\n");
  1768. anx7625_hdcp_enable(ctx);
  1769. queue_delayed_work(ctx->hdcp_workqueue,
  1770. &ctx->hdcp_work,
  1771. msecs_to_jiffies(2000));
  1772. }
  1773. }
  1774. if (cp == DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
  1775. if (ctx->hdcp_cp != DRM_MODE_CONTENT_PROTECTION_ENABLED) {
  1776. dev_err(dev, "current CP is not ENABLED\n");
  1777. return -EINVAL;
  1778. }
  1779. anx7625_hdcp_disable(ctx);
  1780. ctx->hdcp_cp = DRM_MODE_CONTENT_PROTECTION_UNDESIRED;
  1781. drm_hdcp_update_content_protection(ctx->connector,
  1782. ctx->hdcp_cp);
  1783. dev_dbg(dev, "update CP to UNDESIRE\n");
  1784. }
  1785. if (cp == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
  1786. dev_err(dev, "Userspace illegal set to PROTECTION ENABLE\n");
  1787. return -EINVAL;
  1788. }
  1789. return 0;
  1790. }
  1791. static int anx7625_bridge_attach(struct drm_bridge *bridge,
  1792. enum drm_bridge_attach_flags flags)
  1793. {
  1794. struct anx7625_data *ctx = bridge_to_anx7625(bridge);
  1795. int err;
  1796. struct device *dev = &ctx->client->dev;
  1797. DRM_DEV_DEBUG_DRIVER(dev, "drm attach\n");
  1798. if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
  1799. return -EINVAL;
  1800. if (!bridge->encoder) {
  1801. DRM_DEV_ERROR(dev, "Parent encoder object not found");
  1802. return -ENODEV;
  1803. }
  1804. ctx->aux.drm_dev = bridge->dev;
  1805. err = drm_dp_aux_register(&ctx->aux);
  1806. if (err) {
  1807. dev_err(dev, "failed to register aux channel: %d\n", err);
  1808. return err;
  1809. }
  1810. if (!ctx->pdata.is_dpi) {
  1811. err = anx7625_setup_dsi_device(ctx);
  1812. if (err) {
  1813. DRM_DEV_ERROR(dev, "Fail to attach to dsi : %d\n", err);
  1814. return err;
  1815. }
  1816. }
  1817. if (!ctx->pdata.is_dpi) {
  1818. err = anx7625_attach_dsi(ctx);
  1819. if (err) {
  1820. DRM_DEV_ERROR(dev, "Fail to attach to dsi : %d\n", err);
  1821. return err;
  1822. }
  1823. }
  1824. if (ctx->pdata.panel_bridge) {
  1825. err = drm_bridge_attach(bridge->encoder,
  1826. ctx->pdata.panel_bridge,
  1827. &ctx->bridge, flags);
  1828. if (err) {
  1829. DRM_DEV_ERROR(dev, "Fail to attach to bridge : %d\n", err);
  1830. return err;
  1831. }
  1832. }
  1833. device_link_add(bridge->dev->dev, dev, DL_FLAG_STATELESS);
  1834. ctx->bridge_attached = 1;
  1835. return 0;
  1836. }
  1837. static void anx7625_bridge_detach(struct drm_bridge *bridge)
  1838. {
  1839. struct anx7625_data *ctx = bridge_to_anx7625(bridge);
  1840. ctx->bridge_attached = 0;
  1841. drm_dp_aux_unregister(&ctx->aux);
  1842. }
  1843. static enum drm_mode_status
  1844. anx7625_bridge_mode_valid(struct drm_bridge *bridge,
  1845. const struct drm_display_info *info,
  1846. const struct drm_display_mode *mode)
  1847. {
  1848. struct anx7625_data *ctx = bridge_to_anx7625(bridge);
  1849. struct device *dev = &ctx->client->dev;
  1850. DRM_DEV_DEBUG_DRIVER(dev, "drm mode checking\n");
  1851. /* Max 1200p at 5.4 Ghz, one lane, pixel clock 300M */
  1852. if (mode->clock > SUPPORT_PIXEL_CLOCK) {
  1853. DRM_DEV_DEBUG_DRIVER(dev,
  1854. "drm mode invalid, pixelclock too high.\n");
  1855. return MODE_CLOCK_HIGH;
  1856. }
  1857. DRM_DEV_DEBUG_DRIVER(dev, "drm mode valid.\n");
  1858. return MODE_OK;
  1859. }
  1860. static void anx7625_bridge_mode_set(struct drm_bridge *bridge,
  1861. const struct drm_display_mode *old_mode,
  1862. const struct drm_display_mode *mode)
  1863. {
  1864. struct anx7625_data *ctx = bridge_to_anx7625(bridge);
  1865. struct device *dev = &ctx->client->dev;
  1866. DRM_DEV_DEBUG_DRIVER(dev, "drm mode set\n");
  1867. ctx->dt.pixelclock.min = mode->clock;
  1868. ctx->dt.hactive.min = mode->hdisplay;
  1869. ctx->dt.hsync_len.min = mode->hsync_end - mode->hsync_start;
  1870. ctx->dt.hfront_porch.min = mode->hsync_start - mode->hdisplay;
  1871. ctx->dt.hback_porch.min = mode->htotal - mode->hsync_end;
  1872. ctx->dt.vactive.min = mode->vdisplay;
  1873. ctx->dt.vsync_len.min = mode->vsync_end - mode->vsync_start;
  1874. ctx->dt.vfront_porch.min = mode->vsync_start - mode->vdisplay;
  1875. ctx->dt.vback_porch.min = mode->vtotal - mode->vsync_end;
  1876. ctx->display_timing_valid = 1;
  1877. DRM_DEV_DEBUG_DRIVER(dev, "pixelclock(%d).\n", ctx->dt.pixelclock.min);
  1878. DRM_DEV_DEBUG_DRIVER(dev, "hactive(%d), hsync(%d), hfp(%d), hbp(%d)\n",
  1879. ctx->dt.hactive.min,
  1880. ctx->dt.hsync_len.min,
  1881. ctx->dt.hfront_porch.min,
  1882. ctx->dt.hback_porch.min);
  1883. DRM_DEV_DEBUG_DRIVER(dev, "vactive(%d), vsync(%d), vfp(%d), vbp(%d)\n",
  1884. ctx->dt.vactive.min,
  1885. ctx->dt.vsync_len.min,
  1886. ctx->dt.vfront_porch.min,
  1887. ctx->dt.vback_porch.min);
  1888. DRM_DEV_DEBUG_DRIVER(dev, "hdisplay(%d),hsync_start(%d).\n",
  1889. mode->hdisplay,
  1890. mode->hsync_start);
  1891. DRM_DEV_DEBUG_DRIVER(dev, "hsync_end(%d),htotal(%d).\n",
  1892. mode->hsync_end,
  1893. mode->htotal);
  1894. DRM_DEV_DEBUG_DRIVER(dev, "vdisplay(%d),vsync_start(%d).\n",
  1895. mode->vdisplay,
  1896. mode->vsync_start);
  1897. DRM_DEV_DEBUG_DRIVER(dev, "vsync_end(%d),vtotal(%d).\n",
  1898. mode->vsync_end,
  1899. mode->vtotal);
  1900. }
  1901. static bool anx7625_bridge_mode_fixup(struct drm_bridge *bridge,
  1902. const struct drm_display_mode *mode,
  1903. struct drm_display_mode *adj)
  1904. {
  1905. struct anx7625_data *ctx = bridge_to_anx7625(bridge);
  1906. struct device *dev = &ctx->client->dev;
  1907. u32 hsync, hfp, hbp, hblanking;
  1908. u32 adj_hsync, adj_hfp, adj_hbp, adj_hblanking, delta_adj;
  1909. u32 vref, adj_clock;
  1910. DRM_DEV_DEBUG_DRIVER(dev, "drm mode fixup set\n");
  1911. /* No need fixup for external monitor */
  1912. if (!ctx->pdata.panel_bridge)
  1913. return true;
  1914. hsync = mode->hsync_end - mode->hsync_start;
  1915. hfp = mode->hsync_start - mode->hdisplay;
  1916. hbp = mode->htotal - mode->hsync_end;
  1917. hblanking = mode->htotal - mode->hdisplay;
  1918. DRM_DEV_DEBUG_DRIVER(dev, "before mode fixup\n");
  1919. DRM_DEV_DEBUG_DRIVER(dev, "hsync(%d), hfp(%d), hbp(%d), clock(%d)\n",
  1920. hsync, hfp, hbp, adj->clock);
  1921. DRM_DEV_DEBUG_DRIVER(dev, "hsync_start(%d), hsync_end(%d), htot(%d)\n",
  1922. adj->hsync_start, adj->hsync_end, adj->htotal);
  1923. adj_hfp = hfp;
  1924. adj_hsync = hsync;
  1925. adj_hbp = hbp;
  1926. adj_hblanking = hblanking;
  1927. /* HFP needs to be even */
  1928. if (hfp & 0x1) {
  1929. adj_hfp += 1;
  1930. adj_hblanking += 1;
  1931. }
  1932. /* HBP needs to be even */
  1933. if (hbp & 0x1) {
  1934. adj_hbp -= 1;
  1935. adj_hblanking -= 1;
  1936. }
  1937. /* HSYNC needs to be even */
  1938. if (hsync & 0x1) {
  1939. if (adj_hblanking < hblanking)
  1940. adj_hsync += 1;
  1941. else
  1942. adj_hsync -= 1;
  1943. }
  1944. /*
  1945. * Once illegal timing detected, use default HFP, HSYNC, HBP
  1946. * This adjusting made for built-in eDP panel, for the externel
  1947. * DP monitor, may need return false.
  1948. */
  1949. if (hblanking < HBLANKING_MIN || (hfp < HP_MIN && hbp < HP_MIN)) {
  1950. adj_hsync = SYNC_LEN_DEF;
  1951. adj_hfp = HFP_HBP_DEF;
  1952. adj_hbp = HFP_HBP_DEF;
  1953. vref = adj->clock * 1000 / (adj->htotal * adj->vtotal);
  1954. if (hblanking < HBLANKING_MIN) {
  1955. delta_adj = HBLANKING_MIN - hblanking;
  1956. adj_clock = vref * delta_adj * adj->vtotal;
  1957. adj->clock += DIV_ROUND_UP(adj_clock, 1000);
  1958. } else {
  1959. delta_adj = hblanking - HBLANKING_MIN;
  1960. adj_clock = vref * delta_adj * adj->vtotal;
  1961. adj->clock -= DIV_ROUND_UP(adj_clock, 1000);
  1962. }
  1963. DRM_WARN("illegal hblanking timing, use default.\n");
  1964. DRM_WARN("hfp(%d), hbp(%d), hsync(%d).\n", hfp, hbp, hsync);
  1965. } else if (adj_hfp < HP_MIN) {
  1966. /* Adjust hfp if hfp less than HP_MIN */
  1967. delta_adj = HP_MIN - adj_hfp;
  1968. adj_hfp = HP_MIN;
  1969. /*
  1970. * Balance total HBlanking pixel, if HBP does not have enough
  1971. * space, adjust HSYNC length, otherwise adjust HBP
  1972. */
  1973. if ((adj_hbp - delta_adj) < HP_MIN)
  1974. /* HBP not enough space */
  1975. adj_hsync -= delta_adj;
  1976. else
  1977. adj_hbp -= delta_adj;
  1978. } else if (adj_hbp < HP_MIN) {
  1979. delta_adj = HP_MIN - adj_hbp;
  1980. adj_hbp = HP_MIN;
  1981. /*
  1982. * Balance total HBlanking pixel, if HBP hasn't enough space,
  1983. * adjust HSYNC length, otherwize adjust HBP
  1984. */
  1985. if ((adj_hfp - delta_adj) < HP_MIN)
  1986. /* HFP not enough space */
  1987. adj_hsync -= delta_adj;
  1988. else
  1989. adj_hfp -= delta_adj;
  1990. }
  1991. DRM_DEV_DEBUG_DRIVER(dev, "after mode fixup\n");
  1992. DRM_DEV_DEBUG_DRIVER(dev, "hsync(%d), hfp(%d), hbp(%d), clock(%d)\n",
  1993. adj_hsync, adj_hfp, adj_hbp, adj->clock);
  1994. /* Reconstruct timing */
  1995. adj->hsync_start = adj->hdisplay + adj_hfp;
  1996. adj->hsync_end = adj->hsync_start + adj_hsync;
  1997. adj->htotal = adj->hsync_end + adj_hbp;
  1998. DRM_DEV_DEBUG_DRIVER(dev, "hsync_start(%d), hsync_end(%d), htot(%d)\n",
  1999. adj->hsync_start, adj->hsync_end, adj->htotal);
  2000. return true;
  2001. }
  2002. static int anx7625_bridge_atomic_check(struct drm_bridge *bridge,
  2003. struct drm_bridge_state *bridge_state,
  2004. struct drm_crtc_state *crtc_state,
  2005. struct drm_connector_state *conn_state)
  2006. {
  2007. struct anx7625_data *ctx = bridge_to_anx7625(bridge);
  2008. struct device *dev = &ctx->client->dev;
  2009. dev_dbg(dev, "drm bridge atomic check\n");
  2010. anx7625_bridge_mode_fixup(bridge, &crtc_state->mode,
  2011. &crtc_state->adjusted_mode);
  2012. return anx7625_connector_atomic_check(ctx, conn_state);
  2013. }
  2014. static void anx7625_bridge_atomic_enable(struct drm_bridge *bridge,
  2015. struct drm_bridge_state *state)
  2016. {
  2017. struct anx7625_data *ctx = bridge_to_anx7625(bridge);
  2018. struct device *dev = &ctx->client->dev;
  2019. struct drm_connector *connector;
  2020. dev_dbg(dev, "drm atomic enable\n");
  2021. if (!bridge->encoder) {
  2022. dev_err(dev, "Parent encoder object not found");
  2023. return;
  2024. }
  2025. connector = drm_atomic_get_new_connector_for_encoder(state->base.state,
  2026. bridge->encoder);
  2027. if (!connector)
  2028. return;
  2029. if (ctx->out_of_hibr) {
  2030. ctx->out_of_hibr = false;
  2031. pr_info("anx7625 hibernation: Display up\n");
  2032. }
  2033. ctx->connector = connector;
  2034. pm_runtime_get_sync(dev);
  2035. _anx7625_hpd_polling(ctx, 5000 * 2);
  2036. anx7625_dp_start(ctx);
  2037. }
  2038. static void anx7625_bridge_atomic_disable(struct drm_bridge *bridge,
  2039. struct drm_bridge_state *old)
  2040. {
  2041. struct anx7625_data *ctx = bridge_to_anx7625(bridge);
  2042. struct device *dev = &ctx->client->dev;
  2043. dev_dbg(dev, "drm atomic disable\n");
  2044. ctx->connector = NULL;
  2045. anx7625_dp_stop(ctx);
  2046. pm_runtime_put_sync(dev);
  2047. }
  2048. static enum drm_connector_status
  2049. anx7625_bridge_detect(struct drm_bridge *bridge)
  2050. {
  2051. struct anx7625_data *ctx = bridge_to_anx7625(bridge);
  2052. struct device *dev = &ctx->client->dev;
  2053. DRM_DEV_DEBUG_DRIVER(dev, "drm bridge detect\n");
  2054. return anx7625_sink_detect(ctx);
  2055. }
  2056. static struct edid *anx7625_bridge_get_edid(struct drm_bridge *bridge,
  2057. struct drm_connector *connector)
  2058. {
  2059. struct anx7625_data *ctx = bridge_to_anx7625(bridge);
  2060. struct device *dev = &ctx->client->dev;
  2061. DRM_DEV_DEBUG_DRIVER(dev, "drm bridge get edid\n");
  2062. return anx7625_get_edid(ctx);
  2063. }
  2064. static const struct drm_bridge_funcs anx7625_bridge_funcs = {
  2065. .attach = anx7625_bridge_attach,
  2066. .detach = anx7625_bridge_detach,
  2067. .mode_valid = anx7625_bridge_mode_valid,
  2068. .mode_set = anx7625_bridge_mode_set,
  2069. .atomic_check = anx7625_bridge_atomic_check,
  2070. .atomic_enable = anx7625_bridge_atomic_enable,
  2071. .atomic_disable = anx7625_bridge_atomic_disable,
  2072. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  2073. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  2074. .atomic_reset = drm_atomic_helper_bridge_reset,
  2075. .detect = anx7625_bridge_detect,
  2076. .get_edid = anx7625_bridge_get_edid,
  2077. };
  2078. static int anx7625_register_i2c_dummy_clients(struct anx7625_data *ctx,
  2079. struct i2c_client *client)
  2080. {
  2081. struct device *dev = &ctx->client->dev;
  2082. ctx->i2c.tx_p0_client = devm_i2c_new_dummy_device(dev, client->adapter,
  2083. TX_P0_ADDR >> 1);
  2084. if (IS_ERR(ctx->i2c.tx_p0_client))
  2085. return PTR_ERR(ctx->i2c.tx_p0_client);
  2086. ctx->i2c.tx_p1_client = devm_i2c_new_dummy_device(dev, client->adapter,
  2087. TX_P1_ADDR >> 1);
  2088. if (IS_ERR(ctx->i2c.tx_p1_client))
  2089. return PTR_ERR(ctx->i2c.tx_p1_client);
  2090. ctx->i2c.tx_p2_client = devm_i2c_new_dummy_device(dev, client->adapter,
  2091. TX_P2_ADDR >> 1);
  2092. if (IS_ERR(ctx->i2c.tx_p2_client))
  2093. return PTR_ERR(ctx->i2c.tx_p2_client);
  2094. ctx->i2c.rx_p0_client = devm_i2c_new_dummy_device(dev, client->adapter,
  2095. RX_P0_ADDR >> 1);
  2096. if (IS_ERR(ctx->i2c.rx_p0_client))
  2097. return PTR_ERR(ctx->i2c.rx_p0_client);
  2098. ctx->i2c.rx_p1_client = devm_i2c_new_dummy_device(dev, client->adapter,
  2099. RX_P1_ADDR >> 1);
  2100. if (IS_ERR(ctx->i2c.rx_p1_client))
  2101. return PTR_ERR(ctx->i2c.rx_p1_client);
  2102. ctx->i2c.rx_p2_client = devm_i2c_new_dummy_device(dev, client->adapter,
  2103. RX_P2_ADDR >> 1);
  2104. if (IS_ERR(ctx->i2c.rx_p2_client))
  2105. return PTR_ERR(ctx->i2c.rx_p2_client);
  2106. ctx->i2c.tcpc_client = devm_i2c_new_dummy_device(dev, client->adapter,
  2107. TCPC_INTERFACE_ADDR >> 1);
  2108. if (IS_ERR(ctx->i2c.tcpc_client))
  2109. return PTR_ERR(ctx->i2c.tcpc_client);
  2110. return 0;
  2111. }
  2112. static int __maybe_unused anx7625_runtime_pm_suspend(struct device *dev)
  2113. {
  2114. struct anx7625_data *ctx = dev_get_drvdata(dev);
  2115. mutex_lock(&ctx->lock);
  2116. anx7625_stop_dp_work(ctx);
  2117. anx7625_power_standby(ctx);
  2118. mutex_unlock(&ctx->lock);
  2119. return 0;
  2120. }
  2121. static int __maybe_unused anx7625_runtime_pm_resume(struct device *dev)
  2122. {
  2123. struct anx7625_data *ctx = dev_get_drvdata(dev);
  2124. mutex_lock(&ctx->lock);
  2125. anx7625_power_on_init(ctx);
  2126. _anx7625_hpd_polling(ctx, 5000 * 2);
  2127. mutex_unlock(&ctx->lock);
  2128. return 0;
  2129. }
  2130. static int __maybe_unused anx7625_resume(struct device *dev)
  2131. {
  2132. struct anx7625_data *ctx = dev_get_drvdata(dev);
  2133. if (!ctx->pdata.intp_irq)
  2134. return 0;
  2135. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  2136. enable_irq(ctx->pdata.intp_irq);
  2137. ctx->out_of_hibr = true;
  2138. anx7625_runtime_pm_resume(dev);
  2139. }
  2140. return 0;
  2141. }
  2142. static int __maybe_unused anx7625_suspend(struct device *dev)
  2143. {
  2144. struct anx7625_data *ctx = dev_get_drvdata(dev);
  2145. if (!ctx->pdata.intp_irq)
  2146. return 0;
  2147. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  2148. anx7625_runtime_pm_suspend(dev);
  2149. disable_irq(ctx->pdata.intp_irq);
  2150. }
  2151. return 0;
  2152. }
  2153. static const struct dev_pm_ops anx7625_pm_ops = {
  2154. SET_SYSTEM_SLEEP_PM_OPS(anx7625_suspend, anx7625_resume)
  2155. SET_RUNTIME_PM_OPS(anx7625_runtime_pm_suspend,
  2156. anx7625_runtime_pm_resume, NULL)
  2157. };
  2158. static void anx7625_runtime_disable(void *data)
  2159. {
  2160. pm_runtime_dont_use_autosuspend(data);
  2161. pm_runtime_disable(data);
  2162. }
  2163. static int anx7625_link_bridge(struct drm_dp_aux *aux)
  2164. {
  2165. struct anx7625_data *platform = container_of(aux, struct anx7625_data, aux);
  2166. struct device *dev = aux->dev;
  2167. int ret;
  2168. ret = anx7625_parse_dt_panel(dev, &platform->pdata);
  2169. if (ret) {
  2170. DRM_DEV_ERROR(dev, "fail to parse DT for panel : %d\n", ret);
  2171. return ret;
  2172. }
  2173. platform->bridge.funcs = &anx7625_bridge_funcs;
  2174. platform->bridge.of_node = dev->of_node;
  2175. if (!anx7625_of_panel_on_aux_bus(dev))
  2176. platform->bridge.ops |= DRM_BRIDGE_OP_EDID;
  2177. if (!platform->pdata.panel_bridge)
  2178. platform->bridge.ops |= DRM_BRIDGE_OP_HPD |
  2179. DRM_BRIDGE_OP_DETECT;
  2180. platform->bridge.type = platform->pdata.panel_bridge ?
  2181. DRM_MODE_CONNECTOR_eDP :
  2182. DRM_MODE_CONNECTOR_DisplayPort;
  2183. drm_bridge_add(&platform->bridge);
  2184. return ret;
  2185. }
  2186. static int anx7625_i2c_probe(struct i2c_client *client)
  2187. {
  2188. struct anx7625_data *platform;
  2189. struct anx7625_platform_data *pdata;
  2190. int ret = 0;
  2191. struct device *dev = &client->dev;
  2192. struct device_node *parent_node = of_get_parent(dev->of_node);
  2193. if (!i2c_check_functionality(client->adapter,
  2194. I2C_FUNC_SMBUS_I2C_BLOCK)) {
  2195. DRM_DEV_ERROR(dev, "anx7625's i2c bus doesn't support\n");
  2196. return -ENODEV;
  2197. }
  2198. platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
  2199. if (!platform) {
  2200. DRM_DEV_ERROR(dev, "fail to allocate driver data\n");
  2201. return -ENOMEM;
  2202. }
  2203. pdata = &platform->pdata;
  2204. platform->client = client;
  2205. i2c_set_clientdata(client, platform);
  2206. pdata->supplies[0].supply = "vdd10";
  2207. pdata->supplies[1].supply = "vdd18";
  2208. pdata->supplies[2].supply = "vdd33";
  2209. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(pdata->supplies),
  2210. pdata->supplies);
  2211. if (ret) {
  2212. DRM_DEV_ERROR(dev, "fail to get power supplies: %d\n", ret);
  2213. return ret;
  2214. }
  2215. anx7625_init_gpio(platform);
  2216. of_property_read_u32_index(parent_node, "reg", 0, &platform->channel);
  2217. mutex_init(&platform->lock);
  2218. mutex_init(&platform->hdcp_wq_lock);
  2219. INIT_DELAYED_WORK(&platform->hdcp_work, hdcp_check_work_func);
  2220. platform->hdcp_workqueue = create_workqueue("hdcp workqueue");
  2221. if (!platform->hdcp_workqueue) {
  2222. dev_err(dev, "fail to create work queue\n");
  2223. ret = -ENOMEM;
  2224. return ret;
  2225. }
  2226. platform->pdata.intp_irq = client->irq;
  2227. if (platform->pdata.intp_irq) {
  2228. INIT_WORK(&platform->work, anx7625_work_func);
  2229. platform->workqueue = alloc_workqueue("anx7625_work",
  2230. WQ_FREEZABLE | WQ_MEM_RECLAIM, 1);
  2231. if (!platform->workqueue) {
  2232. DRM_DEV_ERROR(dev, "fail to create work queue\n");
  2233. ret = -ENOMEM;
  2234. goto free_hdcp_wq;
  2235. }
  2236. ret = devm_request_threaded_irq(dev, platform->pdata.intp_irq,
  2237. NULL, anx7625_intr_hpd_isr,
  2238. IRQF_TRIGGER_FALLING |
  2239. IRQF_ONESHOT,
  2240. "anx7625-intp", platform);
  2241. if (ret) {
  2242. DRM_DEV_ERROR(dev, "fail to request irq\n");
  2243. goto free_wq;
  2244. }
  2245. }
  2246. platform->aux.name = "anx7625-aux";
  2247. platform->aux.dev = dev;
  2248. platform->aux.transfer = anx7625_aux_transfer;
  2249. platform->aux.wait_hpd_asserted = anx7625_wait_hpd_asserted;
  2250. drm_dp_aux_init(&platform->aux);
  2251. ret = anx7625_parse_dt(dev, pdata);
  2252. if (ret) {
  2253. if (ret != -EPROBE_DEFER)
  2254. DRM_DEV_ERROR(dev, "fail to parse DT : %d\n", ret);
  2255. goto free_wq;
  2256. }
  2257. /*
  2258. * Registering the i2c devices will retrigger deferred probe, so it
  2259. * needs to be done after calls that might return EPROBE_DEFER,
  2260. * otherwise we can get an infinite loop.
  2261. */
  2262. if (anx7625_register_i2c_dummy_clients(platform, client) != 0) {
  2263. ret = -ENOMEM;
  2264. DRM_DEV_ERROR(dev, "fail to reserve I2C bus.\n");
  2265. goto free_wq;
  2266. }
  2267. pm_runtime_enable(dev);
  2268. pm_runtime_set_autosuspend_delay(dev, 1000);
  2269. pm_runtime_use_autosuspend(dev);
  2270. pm_suspend_ignore_children(dev, true);
  2271. ret = devm_add_action_or_reset(dev, anx7625_runtime_disable, dev);
  2272. if (ret)
  2273. goto free_wq;
  2274. /*
  2275. * Populating the aux bus will retrigger deferred probe, so it needs to
  2276. * be done after calls that might return EPROBE_DEFER, otherwise we can
  2277. * get an infinite loop.
  2278. */
  2279. ret = devm_of_dp_aux_populate_bus(&platform->aux, anx7625_link_bridge);
  2280. if (ret) {
  2281. if (ret != -ENODEV) {
  2282. DRM_DEV_ERROR(dev, "failed to populate aux bus : %d\n", ret);
  2283. goto free_wq;
  2284. }
  2285. }
  2286. if (!platform->pdata.low_power_mode) {
  2287. anx7625_disable_pd_protocol(platform);
  2288. pm_runtime_get_sync(dev);
  2289. _anx7625_hpd_polling(platform, 5000 * 2);
  2290. }
  2291. /* Add work function */
  2292. if (platform->pdata.intp_irq)
  2293. queue_work(platform->workqueue, &platform->work);
  2294. ret = anx7625_link_bridge(&platform->aux);
  2295. if (ret)
  2296. goto free_wq;
  2297. if (platform->pdata.audio_en)
  2298. anx7625_register_audio(dev, platform);
  2299. DRM_DEV_DEBUG_DRIVER(dev, "probe done\n");
  2300. return 0;
  2301. free_wq:
  2302. if (platform->workqueue)
  2303. destroy_workqueue(platform->workqueue);
  2304. free_hdcp_wq:
  2305. if (platform->hdcp_workqueue)
  2306. destroy_workqueue(platform->hdcp_workqueue);
  2307. return ret;
  2308. }
  2309. static void anx7625_i2c_remove(struct i2c_client *client)
  2310. {
  2311. struct anx7625_data *platform = i2c_get_clientdata(client);
  2312. drm_bridge_remove(&platform->bridge);
  2313. if (platform->pdata.intp_irq)
  2314. destroy_workqueue(platform->workqueue);
  2315. if (platform->hdcp_workqueue) {
  2316. cancel_delayed_work(&platform->hdcp_work);
  2317. flush_workqueue(platform->hdcp_workqueue);
  2318. destroy_workqueue(platform->hdcp_workqueue);
  2319. }
  2320. if (!platform->pdata.low_power_mode)
  2321. pm_runtime_put_sync_suspend(&client->dev);
  2322. if (platform->pdata.audio_en)
  2323. anx7625_unregister_audio(platform);
  2324. }
  2325. static const struct i2c_device_id anx7625_id[] = {
  2326. {"anx7625", 0},
  2327. {}
  2328. };
  2329. MODULE_DEVICE_TABLE(i2c, anx7625_id);
  2330. static const struct of_device_id anx_match_table[] = {
  2331. {.compatible = "analogix,anx7625",},
  2332. {},
  2333. };
  2334. MODULE_DEVICE_TABLE(of, anx_match_table);
  2335. static struct i2c_driver anx7625_driver = {
  2336. .driver = {
  2337. .name = "anx7625",
  2338. .of_match_table = anx_match_table,
  2339. .pm = &anx7625_pm_ops,
  2340. },
  2341. .probe_new = anx7625_i2c_probe,
  2342. .remove = anx7625_i2c_remove,
  2343. .id_table = anx7625_id,
  2344. };
  2345. module_i2c_driver(anx7625_driver);
  2346. MODULE_DESCRIPTION("MIPI2DP anx7625 driver");
  2347. MODULE_AUTHOR("Xin Ji <[email protected]>");
  2348. MODULE_LICENSE("GPL v2");
  2349. MODULE_VERSION(ANX7625_DRV_VERSION);