tc358767.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /*
  2. * tc358767 eDP bridge driver
  3. *
  4. * Copyright (C) 2016 CogentEmbedded Inc
  5. * Author: Andrey Gusakov <andrey.gusakov@cogentembedded.com>
  6. *
  7. * Copyright (C) 2016 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
  8. *
  9. * Copyright (C) 2016 Zodiac Inflight Innovations
  10. *
  11. * Initially based on: drivers/gpu/drm/i2c/tda998x_drv.c
  12. *
  13. * Copyright (C) 2012 Texas Instruments
  14. * Author: Rob Clark <robdclark@gmail.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. */
  26. #include <linux/clk.h>
  27. #include <linux/device.h>
  28. #include <linux/gpio/consumer.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/regmap.h>
  33. #include <linux/slab.h>
  34. #include <drm/drm_atomic_helper.h>
  35. #include <drm/drm_crtc_helper.h>
  36. #include <drm/drm_dp_helper.h>
  37. #include <drm/drm_edid.h>
  38. #include <drm/drm_of.h>
  39. #include <drm/drm_panel.h>
  40. /* Registers */
  41. /* Display Parallel Interface */
  42. #define DPIPXLFMT 0x0440
  43. #define VS_POL_ACTIVE_LOW (1 << 10)
  44. #define HS_POL_ACTIVE_LOW (1 << 9)
  45. #define DE_POL_ACTIVE_HIGH (0 << 8)
  46. #define SUB_CFG_TYPE_CONFIG1 (0 << 2) /* LSB aligned */
  47. #define SUB_CFG_TYPE_CONFIG2 (1 << 2) /* Loosely Packed */
  48. #define SUB_CFG_TYPE_CONFIG3 (2 << 2) /* LSB aligned 8-bit */
  49. #define DPI_BPP_RGB888 (0 << 0)
  50. #define DPI_BPP_RGB666 (1 << 0)
  51. #define DPI_BPP_RGB565 (2 << 0)
  52. /* Video Path */
  53. #define VPCTRL0 0x0450
  54. #define OPXLFMT_RGB666 (0 << 8)
  55. #define OPXLFMT_RGB888 (1 << 8)
  56. #define FRMSYNC_DISABLED (0 << 4) /* Video Timing Gen Disabled */
  57. #define FRMSYNC_ENABLED (1 << 4) /* Video Timing Gen Enabled */
  58. #define MSF_DISABLED (0 << 0) /* Magic Square FRC disabled */
  59. #define MSF_ENABLED (1 << 0) /* Magic Square FRC enabled */
  60. #define HTIM01 0x0454
  61. #define HTIM02 0x0458
  62. #define VTIM01 0x045c
  63. #define VTIM02 0x0460
  64. #define VFUEN0 0x0464
  65. #define VFUEN BIT(0) /* Video Frame Timing Upload */
  66. /* System */
  67. #define TC_IDREG 0x0500
  68. #define SYSCTRL 0x0510
  69. #define DP0_AUDSRC_NO_INPUT (0 << 3)
  70. #define DP0_AUDSRC_I2S_RX (1 << 3)
  71. #define DP0_VIDSRC_NO_INPUT (0 << 0)
  72. #define DP0_VIDSRC_DSI_RX (1 << 0)
  73. #define DP0_VIDSRC_DPI_RX (2 << 0)
  74. #define DP0_VIDSRC_COLOR_BAR (3 << 0)
  75. /* Control */
  76. #define DP0CTL 0x0600
  77. #define VID_MN_GEN BIT(6) /* Auto-generate M/N values */
  78. #define EF_EN BIT(5) /* Enable Enhanced Framing */
  79. #define VID_EN BIT(1) /* Video transmission enable */
  80. #define DP_EN BIT(0) /* Enable DPTX function */
  81. /* Clocks */
  82. #define DP0_VIDMNGEN0 0x0610
  83. #define DP0_VIDMNGEN1 0x0614
  84. #define DP0_VMNGENSTATUS 0x0618
  85. /* Main Channel */
  86. #define DP0_SECSAMPLE 0x0640
  87. #define DP0_VIDSYNCDELAY 0x0644
  88. #define DP0_TOTALVAL 0x0648
  89. #define DP0_STARTVAL 0x064c
  90. #define DP0_ACTIVEVAL 0x0650
  91. #define DP0_SYNCVAL 0x0654
  92. #define DP0_MISC 0x0658
  93. #define TU_SIZE_RECOMMENDED (63) /* LSCLK cycles per TU */
  94. #define BPC_6 (0 << 5)
  95. #define BPC_8 (1 << 5)
  96. /* AUX channel */
  97. #define DP0_AUXCFG0 0x0660
  98. #define DP0_AUXCFG1 0x0664
  99. #define AUX_RX_FILTER_EN BIT(16)
  100. #define DP0_AUXADDR 0x0668
  101. #define DP0_AUXWDATA(i) (0x066c + (i) * 4)
  102. #define DP0_AUXRDATA(i) (0x067c + (i) * 4)
  103. #define DP0_AUXSTATUS 0x068c
  104. #define AUX_STATUS_MASK 0xf0
  105. #define AUX_STATUS_SHIFT 4
  106. #define AUX_TIMEOUT BIT(1)
  107. #define AUX_BUSY BIT(0)
  108. #define DP0_AUXI2CADR 0x0698
  109. /* Link Training */
  110. #define DP0_SRCCTRL 0x06a0
  111. #define DP0_SRCCTRL_SCRMBLDIS BIT(13)
  112. #define DP0_SRCCTRL_EN810B BIT(12)
  113. #define DP0_SRCCTRL_NOTP (0 << 8)
  114. #define DP0_SRCCTRL_TP1 (1 << 8)
  115. #define DP0_SRCCTRL_TP2 (2 << 8)
  116. #define DP0_SRCCTRL_LANESKEW BIT(7)
  117. #define DP0_SRCCTRL_SSCG BIT(3)
  118. #define DP0_SRCCTRL_LANES_1 (0 << 2)
  119. #define DP0_SRCCTRL_LANES_2 (1 << 2)
  120. #define DP0_SRCCTRL_BW27 (1 << 1)
  121. #define DP0_SRCCTRL_BW162 (0 << 1)
  122. #define DP0_SRCCTRL_AUTOCORRECT BIT(0)
  123. #define DP0_LTSTAT 0x06d0
  124. #define LT_LOOPDONE BIT(13)
  125. #define LT_STATUS_MASK (0x1f << 8)
  126. #define LT_CHANNEL1_EQ_BITS (DP_CHANNEL_EQ_BITS << 4)
  127. #define LT_INTERLANE_ALIGN_DONE BIT(3)
  128. #define LT_CHANNEL0_EQ_BITS (DP_CHANNEL_EQ_BITS)
  129. #define DP0_SNKLTCHGREQ 0x06d4
  130. #define DP0_LTLOOPCTRL 0x06d8
  131. #define DP0_SNKLTCTRL 0x06e4
  132. /* PHY */
  133. #define DP_PHY_CTRL 0x0800
  134. #define DP_PHY_RST BIT(28) /* DP PHY Global Soft Reset */
  135. #define BGREN BIT(25) /* AUX PHY BGR Enable */
  136. #define PWR_SW_EN BIT(24) /* PHY Power Switch Enable */
  137. #define PHY_M1_RST BIT(12) /* Reset PHY1 Main Channel */
  138. #define PHY_RDY BIT(16) /* PHY Main Channels Ready */
  139. #define PHY_M0_RST BIT(8) /* Reset PHY0 Main Channel */
  140. #define PHY_A0_EN BIT(1) /* PHY Aux Channel0 Enable */
  141. #define PHY_M0_EN BIT(0) /* PHY Main Channel0 Enable */
  142. /* PLL */
  143. #define DP0_PLLCTRL 0x0900
  144. #define DP1_PLLCTRL 0x0904 /* not defined in DS */
  145. #define PXL_PLLCTRL 0x0908
  146. #define PLLUPDATE BIT(2)
  147. #define PLLBYP BIT(1)
  148. #define PLLEN BIT(0)
  149. #define PXL_PLLPARAM 0x0914
  150. #define IN_SEL_REFCLK (0 << 14)
  151. #define SYS_PLLPARAM 0x0918
  152. #define REF_FREQ_38M4 (0 << 8) /* 38.4 MHz */
  153. #define REF_FREQ_19M2 (1 << 8) /* 19.2 MHz */
  154. #define REF_FREQ_26M (2 << 8) /* 26 MHz */
  155. #define REF_FREQ_13M (3 << 8) /* 13 MHz */
  156. #define SYSCLK_SEL_LSCLK (0 << 4)
  157. #define LSCLK_DIV_1 (0 << 0)
  158. #define LSCLK_DIV_2 (1 << 0)
  159. /* Test & Debug */
  160. #define TSTCTL 0x0a00
  161. #define PLL_DBG 0x0a04
  162. static bool tc_test_pattern;
  163. module_param_named(test, tc_test_pattern, bool, 0644);
  164. struct tc_edp_link {
  165. struct drm_dp_link base;
  166. u8 assr;
  167. int scrambler_dis;
  168. int spread;
  169. int coding8b10b;
  170. u8 swing;
  171. u8 preemp;
  172. };
  173. struct tc_data {
  174. struct device *dev;
  175. struct regmap *regmap;
  176. struct drm_dp_aux aux;
  177. struct drm_bridge bridge;
  178. struct drm_connector connector;
  179. struct drm_panel *panel;
  180. /* link settings */
  181. struct tc_edp_link link;
  182. /* display edid */
  183. struct edid *edid;
  184. /* current mode */
  185. struct drm_display_mode *mode;
  186. u32 rev;
  187. u8 assr;
  188. struct gpio_desc *sd_gpio;
  189. struct gpio_desc *reset_gpio;
  190. struct clk *refclk;
  191. };
  192. static inline struct tc_data *aux_to_tc(struct drm_dp_aux *a)
  193. {
  194. return container_of(a, struct tc_data, aux);
  195. }
  196. static inline struct tc_data *bridge_to_tc(struct drm_bridge *b)
  197. {
  198. return container_of(b, struct tc_data, bridge);
  199. }
  200. static inline struct tc_data *connector_to_tc(struct drm_connector *c)
  201. {
  202. return container_of(c, struct tc_data, connector);
  203. }
  204. /* Simple macros to avoid repeated error checks */
  205. #define tc_write(reg, var) \
  206. do { \
  207. ret = regmap_write(tc->regmap, reg, var); \
  208. if (ret) \
  209. goto err; \
  210. } while (0)
  211. #define tc_read(reg, var) \
  212. do { \
  213. ret = regmap_read(tc->regmap, reg, var); \
  214. if (ret) \
  215. goto err; \
  216. } while (0)
  217. static inline int tc_poll_timeout(struct regmap *map, unsigned int addr,
  218. unsigned int cond_mask,
  219. unsigned int cond_value,
  220. unsigned long sleep_us, u64 timeout_us)
  221. {
  222. ktime_t timeout = ktime_add_us(ktime_get(), timeout_us);
  223. unsigned int val;
  224. int ret;
  225. for (;;) {
  226. ret = regmap_read(map, addr, &val);
  227. if (ret)
  228. break;
  229. if ((val & cond_mask) == cond_value)
  230. break;
  231. if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) {
  232. ret = regmap_read(map, addr, &val);
  233. break;
  234. }
  235. if (sleep_us)
  236. usleep_range((sleep_us >> 2) + 1, sleep_us);
  237. }
  238. return ret ?: (((val & cond_mask) == cond_value) ? 0 : -ETIMEDOUT);
  239. }
  240. static int tc_aux_wait_busy(struct tc_data *tc, unsigned int timeout_ms)
  241. {
  242. return tc_poll_timeout(tc->regmap, DP0_AUXSTATUS, AUX_BUSY, 0,
  243. 1000, 1000 * timeout_ms);
  244. }
  245. static int tc_aux_get_status(struct tc_data *tc, u8 *reply)
  246. {
  247. int ret;
  248. u32 value;
  249. ret = regmap_read(tc->regmap, DP0_AUXSTATUS, &value);
  250. if (ret < 0)
  251. return ret;
  252. if (value & AUX_BUSY) {
  253. if (value & AUX_TIMEOUT) {
  254. dev_err(tc->dev, "i2c access timeout!\n");
  255. return -ETIMEDOUT;
  256. }
  257. return -EBUSY;
  258. }
  259. *reply = (value & AUX_STATUS_MASK) >> AUX_STATUS_SHIFT;
  260. return 0;
  261. }
  262. static ssize_t tc_aux_transfer(struct drm_dp_aux *aux,
  263. struct drm_dp_aux_msg *msg)
  264. {
  265. struct tc_data *tc = aux_to_tc(aux);
  266. size_t size = min_t(size_t, 8, msg->size);
  267. u8 request = msg->request & ~DP_AUX_I2C_MOT;
  268. u8 *buf = msg->buffer;
  269. u32 tmp = 0;
  270. int i = 0;
  271. int ret;
  272. if (size == 0)
  273. return 0;
  274. ret = tc_aux_wait_busy(tc, 100);
  275. if (ret)
  276. goto err;
  277. if (request == DP_AUX_I2C_WRITE || request == DP_AUX_NATIVE_WRITE) {
  278. /* Store data */
  279. while (i < size) {
  280. if (request == DP_AUX_NATIVE_WRITE)
  281. tmp = tmp | (buf[i] << (8 * (i & 0x3)));
  282. else
  283. tmp = (tmp << 8) | buf[i];
  284. i++;
  285. if (((i % 4) == 0) || (i == size)) {
  286. tc_write(DP0_AUXWDATA((i - 1) >> 2), tmp);
  287. tmp = 0;
  288. }
  289. }
  290. } else if (request != DP_AUX_I2C_READ &&
  291. request != DP_AUX_NATIVE_READ) {
  292. return -EINVAL;
  293. }
  294. /* Store address */
  295. tc_write(DP0_AUXADDR, msg->address);
  296. /* Start transfer */
  297. tc_write(DP0_AUXCFG0, ((size - 1) << 8) | request);
  298. ret = tc_aux_wait_busy(tc, 100);
  299. if (ret)
  300. goto err;
  301. ret = tc_aux_get_status(tc, &msg->reply);
  302. if (ret)
  303. goto err;
  304. if (request == DP_AUX_I2C_READ || request == DP_AUX_NATIVE_READ) {
  305. /* Read data */
  306. while (i < size) {
  307. if ((i % 4) == 0)
  308. tc_read(DP0_AUXRDATA(i >> 2), &tmp);
  309. buf[i] = tmp & 0xff;
  310. tmp = tmp >> 8;
  311. i++;
  312. }
  313. }
  314. return size;
  315. err:
  316. return ret;
  317. }
  318. static const char * const training_pattern1_errors[] = {
  319. "No errors",
  320. "Aux write error",
  321. "Aux read error",
  322. "Max voltage reached error",
  323. "Loop counter expired error",
  324. "res", "res", "res"
  325. };
  326. static const char * const training_pattern2_errors[] = {
  327. "No errors",
  328. "Aux write error",
  329. "Aux read error",
  330. "Clock recovery failed error",
  331. "Loop counter expired error",
  332. "res", "res", "res"
  333. };
  334. static u32 tc_srcctrl(struct tc_data *tc)
  335. {
  336. /*
  337. * No training pattern, skew lane 1 data by two LSCLK cycles with
  338. * respect to lane 0 data, AutoCorrect Mode = 0
  339. */
  340. u32 reg = DP0_SRCCTRL_NOTP | DP0_SRCCTRL_LANESKEW;
  341. if (tc->link.scrambler_dis)
  342. reg |= DP0_SRCCTRL_SCRMBLDIS; /* Scrambler Disabled */
  343. if (tc->link.coding8b10b)
  344. /* Enable 8/10B Encoder (TxData[19:16] not used) */
  345. reg |= DP0_SRCCTRL_EN810B;
  346. if (tc->link.spread)
  347. reg |= DP0_SRCCTRL_SSCG; /* Spread Spectrum Enable */
  348. if (tc->link.base.num_lanes == 2)
  349. reg |= DP0_SRCCTRL_LANES_2; /* Two Main Channel Lanes */
  350. if (tc->link.base.rate != 162000)
  351. reg |= DP0_SRCCTRL_BW27; /* 2.7 Gbps link */
  352. return reg;
  353. }
  354. static void tc_wait_pll_lock(struct tc_data *tc)
  355. {
  356. /* Wait for PLL to lock: up to 2.09 ms, depending on refclk */
  357. usleep_range(3000, 6000);
  358. }
  359. static int tc_pxl_pll_en(struct tc_data *tc, u32 refclk, u32 pixelclock)
  360. {
  361. int ret;
  362. int i_pre, best_pre = 1;
  363. int i_post, best_post = 1;
  364. int div, best_div = 1;
  365. int mul, best_mul = 1;
  366. int delta, best_delta;
  367. int ext_div[] = {1, 2, 3, 5, 7};
  368. int best_pixelclock = 0;
  369. int vco_hi = 0;
  370. dev_dbg(tc->dev, "PLL: requested %d pixelclock, ref %d\n", pixelclock,
  371. refclk);
  372. best_delta = pixelclock;
  373. /* Loop over all possible ext_divs, skipping invalid configurations */
  374. for (i_pre = 0; i_pre < ARRAY_SIZE(ext_div); i_pre++) {
  375. /*
  376. * refclk / ext_pre_div should be in the 1 to 200 MHz range.
  377. * We don't allow any refclk > 200 MHz, only check lower bounds.
  378. */
  379. if (refclk / ext_div[i_pre] < 1000000)
  380. continue;
  381. for (i_post = 0; i_post < ARRAY_SIZE(ext_div); i_post++) {
  382. for (div = 1; div <= 16; div++) {
  383. u32 clk;
  384. u64 tmp;
  385. tmp = pixelclock * ext_div[i_pre] *
  386. ext_div[i_post] * div;
  387. do_div(tmp, refclk);
  388. mul = tmp;
  389. /* Check limits */
  390. if ((mul < 1) || (mul > 128))
  391. continue;
  392. clk = (refclk / ext_div[i_pre] / div) * mul;
  393. /*
  394. * refclk * mul / (ext_pre_div * pre_div)
  395. * should be in the 150 to 650 MHz range
  396. */
  397. if ((clk > 650000000) || (clk < 150000000))
  398. continue;
  399. clk = clk / ext_div[i_post];
  400. delta = clk - pixelclock;
  401. if (abs(delta) < abs(best_delta)) {
  402. best_pre = i_pre;
  403. best_post = i_post;
  404. best_div = div;
  405. best_mul = mul;
  406. best_delta = delta;
  407. best_pixelclock = clk;
  408. }
  409. }
  410. }
  411. }
  412. if (best_pixelclock == 0) {
  413. dev_err(tc->dev, "Failed to calc clock for %d pixelclock\n",
  414. pixelclock);
  415. return -EINVAL;
  416. }
  417. dev_dbg(tc->dev, "PLL: got %d, delta %d\n", best_pixelclock,
  418. best_delta);
  419. dev_dbg(tc->dev, "PLL: %d / %d / %d * %d / %d\n", refclk,
  420. ext_div[best_pre], best_div, best_mul, ext_div[best_post]);
  421. /* if VCO >= 300 MHz */
  422. if (refclk / ext_div[best_pre] / best_div * best_mul >= 300000000)
  423. vco_hi = 1;
  424. /* see DS */
  425. if (best_div == 16)
  426. best_div = 0;
  427. if (best_mul == 128)
  428. best_mul = 0;
  429. /* Power up PLL and switch to bypass */
  430. tc_write(PXL_PLLCTRL, PLLBYP | PLLEN);
  431. tc_write(PXL_PLLPARAM,
  432. (vco_hi << 24) | /* For PLL VCO >= 300 MHz = 1 */
  433. (ext_div[best_pre] << 20) | /* External Pre-divider */
  434. (ext_div[best_post] << 16) | /* External Post-divider */
  435. IN_SEL_REFCLK | /* Use RefClk as PLL input */
  436. (best_div << 8) | /* Divider for PLL RefClk */
  437. (best_mul << 0)); /* Multiplier for PLL */
  438. /* Force PLL parameter update and disable bypass */
  439. tc_write(PXL_PLLCTRL, PLLUPDATE | PLLEN);
  440. tc_wait_pll_lock(tc);
  441. return 0;
  442. err:
  443. return ret;
  444. }
  445. static int tc_pxl_pll_dis(struct tc_data *tc)
  446. {
  447. /* Enable PLL bypass, power down PLL */
  448. return regmap_write(tc->regmap, PXL_PLLCTRL, PLLBYP);
  449. }
  450. static int tc_stream_clock_calc(struct tc_data *tc)
  451. {
  452. int ret;
  453. /*
  454. * If the Stream clock and Link Symbol clock are
  455. * asynchronous with each other, the value of M changes over
  456. * time. This way of generating link clock and stream
  457. * clock is called Asynchronous Clock mode. The value M
  458. * must change while the value N stays constant. The
  459. * value of N in this Asynchronous Clock mode must be set
  460. * to 2^15 or 32,768.
  461. *
  462. * LSCLK = 1/10 of high speed link clock
  463. *
  464. * f_STRMCLK = M/N * f_LSCLK
  465. * M/N = f_STRMCLK / f_LSCLK
  466. *
  467. */
  468. tc_write(DP0_VIDMNGEN1, 32768);
  469. return 0;
  470. err:
  471. return ret;
  472. }
  473. static int tc_aux_link_setup(struct tc_data *tc)
  474. {
  475. unsigned long rate;
  476. u32 value;
  477. int ret;
  478. rate = clk_get_rate(tc->refclk);
  479. switch (rate) {
  480. case 38400000:
  481. value = REF_FREQ_38M4;
  482. break;
  483. case 26000000:
  484. value = REF_FREQ_26M;
  485. break;
  486. case 19200000:
  487. value = REF_FREQ_19M2;
  488. break;
  489. case 13000000:
  490. value = REF_FREQ_13M;
  491. break;
  492. default:
  493. dev_err(tc->dev, "Invalid refclk rate: %lu Hz\n", rate);
  494. return -EINVAL;
  495. }
  496. /* Setup DP-PHY / PLL */
  497. value |= SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
  498. tc_write(SYS_PLLPARAM, value);
  499. tc_write(DP_PHY_CTRL, BGREN | PWR_SW_EN | BIT(2) | PHY_A0_EN);
  500. /*
  501. * Initially PLLs are in bypass. Force PLL parameter update,
  502. * disable PLL bypass, enable PLL
  503. */
  504. tc_write(DP0_PLLCTRL, PLLUPDATE | PLLEN);
  505. tc_wait_pll_lock(tc);
  506. tc_write(DP1_PLLCTRL, PLLUPDATE | PLLEN);
  507. tc_wait_pll_lock(tc);
  508. ret = tc_poll_timeout(tc->regmap, DP_PHY_CTRL, PHY_RDY, PHY_RDY, 1,
  509. 1000);
  510. if (ret == -ETIMEDOUT) {
  511. dev_err(tc->dev, "Timeout waiting for PHY to become ready");
  512. return ret;
  513. } else if (ret)
  514. goto err;
  515. /* Setup AUX link */
  516. tc_write(DP0_AUXCFG1, AUX_RX_FILTER_EN |
  517. (0x06 << 8) | /* Aux Bit Period Calculator Threshold */
  518. (0x3f << 0)); /* Aux Response Timeout Timer */
  519. return 0;
  520. err:
  521. dev_err(tc->dev, "tc_aux_link_setup failed: %d\n", ret);
  522. return ret;
  523. }
  524. static int tc_get_display_props(struct tc_data *tc)
  525. {
  526. int ret;
  527. /* temp buffer */
  528. u8 tmp[8];
  529. /* Read DP Rx Link Capability */
  530. ret = drm_dp_link_probe(&tc->aux, &tc->link.base);
  531. if (ret < 0)
  532. goto err_dpcd_read;
  533. if (tc->link.base.rate != 162000 && tc->link.base.rate != 270000) {
  534. dev_dbg(tc->dev, "Falling to 2.7 Gbps rate\n");
  535. tc->link.base.rate = 270000;
  536. }
  537. if (tc->link.base.num_lanes > 2) {
  538. dev_dbg(tc->dev, "Falling to 2 lanes\n");
  539. tc->link.base.num_lanes = 2;
  540. }
  541. ret = drm_dp_dpcd_readb(&tc->aux, DP_MAX_DOWNSPREAD, tmp);
  542. if (ret < 0)
  543. goto err_dpcd_read;
  544. tc->link.spread = tmp[0] & BIT(0); /* 0.5% down spread */
  545. ret = drm_dp_dpcd_readb(&tc->aux, DP_MAIN_LINK_CHANNEL_CODING, tmp);
  546. if (ret < 0)
  547. goto err_dpcd_read;
  548. tc->link.coding8b10b = tmp[0] & BIT(0);
  549. tc->link.scrambler_dis = 0;
  550. /* read assr */
  551. ret = drm_dp_dpcd_readb(&tc->aux, DP_EDP_CONFIGURATION_SET, tmp);
  552. if (ret < 0)
  553. goto err_dpcd_read;
  554. tc->link.assr = tmp[0] & DP_ALTERNATE_SCRAMBLER_RESET_ENABLE;
  555. dev_dbg(tc->dev, "DPCD rev: %d.%d, rate: %s, lanes: %d, framing: %s\n",
  556. tc->link.base.revision >> 4, tc->link.base.revision & 0x0f,
  557. (tc->link.base.rate == 162000) ? "1.62Gbps" : "2.7Gbps",
  558. tc->link.base.num_lanes,
  559. (tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING) ?
  560. "enhanced" : "non-enhanced");
  561. dev_dbg(tc->dev, "ANSI 8B/10B: %d\n", tc->link.coding8b10b);
  562. dev_dbg(tc->dev, "Display ASSR: %d, TC358767 ASSR: %d\n",
  563. tc->link.assr, tc->assr);
  564. return 0;
  565. err_dpcd_read:
  566. dev_err(tc->dev, "failed to read DPCD: %d\n", ret);
  567. return ret;
  568. }
  569. static int tc_set_video_mode(struct tc_data *tc, struct drm_display_mode *mode)
  570. {
  571. int ret;
  572. int vid_sync_dly;
  573. int max_tu_symbol;
  574. int left_margin = mode->htotal - mode->hsync_end;
  575. int right_margin = mode->hsync_start - mode->hdisplay;
  576. int hsync_len = mode->hsync_end - mode->hsync_start;
  577. int upper_margin = mode->vtotal - mode->vsync_end;
  578. int lower_margin = mode->vsync_start - mode->vdisplay;
  579. int vsync_len = mode->vsync_end - mode->vsync_start;
  580. /*
  581. * Recommended maximum number of symbols transferred in a transfer unit:
  582. * DIV_ROUND_UP((input active video bandwidth in bytes) * tu_size,
  583. * (output active video bandwidth in bytes))
  584. * Must be less than tu_size.
  585. */
  586. max_tu_symbol = TU_SIZE_RECOMMENDED - 1;
  587. dev_dbg(tc->dev, "set mode %dx%d\n",
  588. mode->hdisplay, mode->vdisplay);
  589. dev_dbg(tc->dev, "H margin %d,%d sync %d\n",
  590. left_margin, right_margin, hsync_len);
  591. dev_dbg(tc->dev, "V margin %d,%d sync %d\n",
  592. upper_margin, lower_margin, vsync_len);
  593. dev_dbg(tc->dev, "total: %dx%d\n", mode->htotal, mode->vtotal);
  594. /*
  595. * LCD Ctl Frame Size
  596. * datasheet is not clear of vsdelay in case of DPI
  597. * assume we do not need any delay when DPI is a source of
  598. * sync signals
  599. */
  600. tc_write(VPCTRL0, (0 << 20) /* VSDELAY */ |
  601. OPXLFMT_RGB888 | FRMSYNC_DISABLED | MSF_DISABLED);
  602. tc_write(HTIM01, (ALIGN(left_margin, 2) << 16) | /* H back porch */
  603. (ALIGN(hsync_len, 2) << 0)); /* Hsync */
  604. tc_write(HTIM02, (ALIGN(right_margin, 2) << 16) | /* H front porch */
  605. (ALIGN(mode->hdisplay, 2) << 0)); /* width */
  606. tc_write(VTIM01, (upper_margin << 16) | /* V back porch */
  607. (vsync_len << 0)); /* Vsync */
  608. tc_write(VTIM02, (lower_margin << 16) | /* V front porch */
  609. (mode->vdisplay << 0)); /* height */
  610. tc_write(VFUEN0, VFUEN); /* update settings */
  611. /* Test pattern settings */
  612. tc_write(TSTCTL,
  613. (120 << 24) | /* Red Color component value */
  614. (20 << 16) | /* Green Color component value */
  615. (99 << 8) | /* Blue Color component value */
  616. (1 << 4) | /* Enable I2C Filter */
  617. (2 << 0) | /* Color bar Mode */
  618. 0);
  619. /* DP Main Stream Attributes */
  620. vid_sync_dly = hsync_len + left_margin + mode->hdisplay;
  621. tc_write(DP0_VIDSYNCDELAY,
  622. (max_tu_symbol << 16) | /* thresh_dly */
  623. (vid_sync_dly << 0));
  624. tc_write(DP0_TOTALVAL, (mode->vtotal << 16) | (mode->htotal));
  625. tc_write(DP0_STARTVAL,
  626. ((upper_margin + vsync_len) << 16) |
  627. ((left_margin + hsync_len) << 0));
  628. tc_write(DP0_ACTIVEVAL, (mode->vdisplay << 16) | (mode->hdisplay));
  629. tc_write(DP0_SYNCVAL, (vsync_len << 16) | (hsync_len << 0));
  630. tc_write(DPIPXLFMT, VS_POL_ACTIVE_LOW | HS_POL_ACTIVE_LOW |
  631. DE_POL_ACTIVE_HIGH | SUB_CFG_TYPE_CONFIG1 | DPI_BPP_RGB888);
  632. tc_write(DP0_MISC, (max_tu_symbol << 23) | (TU_SIZE_RECOMMENDED << 16) |
  633. BPC_8);
  634. return 0;
  635. err:
  636. return ret;
  637. }
  638. static int tc_link_training(struct tc_data *tc, int pattern)
  639. {
  640. const char * const *errors;
  641. u32 srcctrl = tc_srcctrl(tc) | DP0_SRCCTRL_SCRMBLDIS |
  642. DP0_SRCCTRL_AUTOCORRECT;
  643. int timeout;
  644. int retry;
  645. u32 value;
  646. int ret;
  647. if (pattern == DP_TRAINING_PATTERN_1) {
  648. srcctrl |= DP0_SRCCTRL_TP1;
  649. errors = training_pattern1_errors;
  650. } else {
  651. srcctrl |= DP0_SRCCTRL_TP2;
  652. errors = training_pattern2_errors;
  653. }
  654. /* Set DPCD 0x102 for Training Part 1 or 2 */
  655. tc_write(DP0_SNKLTCTRL, DP_LINK_SCRAMBLING_DISABLE | pattern);
  656. tc_write(DP0_LTLOOPCTRL,
  657. (0x0f << 28) | /* Defer Iteration Count */
  658. (0x0f << 24) | /* Loop Iteration Count */
  659. (0x0d << 0)); /* Loop Timer Delay */
  660. retry = 5;
  661. do {
  662. /* Set DP0 Training Pattern */
  663. tc_write(DP0_SRCCTRL, srcctrl);
  664. /* Enable DP0 to start Link Training */
  665. tc_write(DP0CTL, DP_EN);
  666. /* wait */
  667. timeout = 1000;
  668. do {
  669. tc_read(DP0_LTSTAT, &value);
  670. udelay(1);
  671. } while ((!(value & LT_LOOPDONE)) && (--timeout));
  672. if (timeout == 0) {
  673. dev_err(tc->dev, "Link training timeout!\n");
  674. } else {
  675. int pattern = (value >> 11) & 0x3;
  676. int error = (value >> 8) & 0x7;
  677. dev_dbg(tc->dev,
  678. "Link training phase %d done after %d uS: %s\n",
  679. pattern, 1000 - timeout, errors[error]);
  680. if (pattern == DP_TRAINING_PATTERN_1 && error == 0)
  681. break;
  682. if (pattern == DP_TRAINING_PATTERN_2) {
  683. value &= LT_CHANNEL1_EQ_BITS |
  684. LT_INTERLANE_ALIGN_DONE |
  685. LT_CHANNEL0_EQ_BITS;
  686. /* in case of two lanes */
  687. if ((tc->link.base.num_lanes == 2) &&
  688. (value == (LT_CHANNEL1_EQ_BITS |
  689. LT_INTERLANE_ALIGN_DONE |
  690. LT_CHANNEL0_EQ_BITS)))
  691. break;
  692. /* in case of one line */
  693. if ((tc->link.base.num_lanes == 1) &&
  694. (value == (LT_INTERLANE_ALIGN_DONE |
  695. LT_CHANNEL0_EQ_BITS)))
  696. break;
  697. }
  698. }
  699. /* restart */
  700. tc_write(DP0CTL, 0);
  701. usleep_range(10, 20);
  702. } while (--retry);
  703. if (retry == 0) {
  704. dev_err(tc->dev, "Failed to finish training phase %d\n",
  705. pattern);
  706. }
  707. return 0;
  708. err:
  709. return ret;
  710. }
  711. static int tc_main_link_setup(struct tc_data *tc)
  712. {
  713. struct drm_dp_aux *aux = &tc->aux;
  714. struct device *dev = tc->dev;
  715. unsigned int rate;
  716. u32 dp_phy_ctrl;
  717. int timeout;
  718. u32 value;
  719. int ret;
  720. u8 tmp[8];
  721. /* display mode should be set at this point */
  722. if (!tc->mode)
  723. return -EINVAL;
  724. /* from excel file - DP0_SrcCtrl */
  725. tc_write(DP0_SRCCTRL, DP0_SRCCTRL_SCRMBLDIS | DP0_SRCCTRL_EN810B |
  726. DP0_SRCCTRL_LANESKEW | DP0_SRCCTRL_LANES_2 |
  727. DP0_SRCCTRL_BW27 | DP0_SRCCTRL_AUTOCORRECT);
  728. /* from excel file - DP1_SrcCtrl */
  729. tc_write(0x07a0, 0x00003083);
  730. rate = clk_get_rate(tc->refclk);
  731. switch (rate) {
  732. case 38400000:
  733. value = REF_FREQ_38M4;
  734. break;
  735. case 26000000:
  736. value = REF_FREQ_26M;
  737. break;
  738. case 19200000:
  739. value = REF_FREQ_19M2;
  740. break;
  741. case 13000000:
  742. value = REF_FREQ_13M;
  743. break;
  744. default:
  745. return -EINVAL;
  746. }
  747. value |= SYSCLK_SEL_LSCLK | LSCLK_DIV_2;
  748. tc_write(SYS_PLLPARAM, value);
  749. /* Setup Main Link */
  750. dp_phy_ctrl = BGREN | PWR_SW_EN | BIT(2) | PHY_A0_EN | PHY_M0_EN;
  751. tc_write(DP_PHY_CTRL, dp_phy_ctrl);
  752. msleep(100);
  753. /* PLL setup */
  754. tc_write(DP0_PLLCTRL, PLLUPDATE | PLLEN);
  755. tc_wait_pll_lock(tc);
  756. tc_write(DP1_PLLCTRL, PLLUPDATE | PLLEN);
  757. tc_wait_pll_lock(tc);
  758. /* PXL PLL setup */
  759. if (tc_test_pattern) {
  760. ret = tc_pxl_pll_en(tc, clk_get_rate(tc->refclk),
  761. 1000 * tc->mode->clock);
  762. if (ret)
  763. goto err;
  764. }
  765. /* Reset/Enable Main Links */
  766. dp_phy_ctrl |= DP_PHY_RST | PHY_M1_RST | PHY_M0_RST;
  767. tc_write(DP_PHY_CTRL, dp_phy_ctrl);
  768. usleep_range(100, 200);
  769. dp_phy_ctrl &= ~(DP_PHY_RST | PHY_M1_RST | PHY_M0_RST);
  770. tc_write(DP_PHY_CTRL, dp_phy_ctrl);
  771. timeout = 1000;
  772. do {
  773. tc_read(DP_PHY_CTRL, &value);
  774. udelay(1);
  775. } while ((!(value & PHY_RDY)) && (--timeout));
  776. if (timeout == 0) {
  777. dev_err(dev, "timeout waiting for phy become ready");
  778. return -ETIMEDOUT;
  779. }
  780. /* Set misc: 8 bits per color */
  781. ret = regmap_update_bits(tc->regmap, DP0_MISC, BPC_8, BPC_8);
  782. if (ret)
  783. goto err;
  784. /*
  785. * ASSR mode
  786. * on TC358767 side ASSR configured through strap pin
  787. * seems there is no way to change this setting from SW
  788. *
  789. * check is tc configured for same mode
  790. */
  791. if (tc->assr != tc->link.assr) {
  792. dev_dbg(dev, "Trying to set display to ASSR: %d\n",
  793. tc->assr);
  794. /* try to set ASSR on display side */
  795. tmp[0] = tc->assr;
  796. ret = drm_dp_dpcd_writeb(aux, DP_EDP_CONFIGURATION_SET, tmp[0]);
  797. if (ret < 0)
  798. goto err_dpcd_read;
  799. /* read back */
  800. ret = drm_dp_dpcd_readb(aux, DP_EDP_CONFIGURATION_SET, tmp);
  801. if (ret < 0)
  802. goto err_dpcd_read;
  803. if (tmp[0] != tc->assr) {
  804. dev_dbg(dev, "Failed to switch display ASSR to %d, falling back to unscrambled mode\n",
  805. tc->assr);
  806. /* trying with disabled scrambler */
  807. tc->link.scrambler_dis = 1;
  808. }
  809. }
  810. /* Setup Link & DPRx Config for Training */
  811. ret = drm_dp_link_configure(aux, &tc->link.base);
  812. if (ret < 0)
  813. goto err_dpcd_write;
  814. /* DOWNSPREAD_CTRL */
  815. tmp[0] = tc->link.spread ? DP_SPREAD_AMP_0_5 : 0x00;
  816. /* MAIN_LINK_CHANNEL_CODING_SET */
  817. tmp[1] = tc->link.coding8b10b ? DP_SET_ANSI_8B10B : 0x00;
  818. ret = drm_dp_dpcd_write(aux, DP_DOWNSPREAD_CTRL, tmp, 2);
  819. if (ret < 0)
  820. goto err_dpcd_write;
  821. ret = tc_link_training(tc, DP_TRAINING_PATTERN_1);
  822. if (ret)
  823. goto err;
  824. ret = tc_link_training(tc, DP_TRAINING_PATTERN_2);
  825. if (ret)
  826. goto err;
  827. /* Clear DPCD 0x102 */
  828. /* Note: Can Not use DP0_SNKLTCTRL (0x06E4) short cut */
  829. tmp[0] = tc->link.scrambler_dis ? DP_LINK_SCRAMBLING_DISABLE : 0x00;
  830. ret = drm_dp_dpcd_writeb(aux, DP_TRAINING_PATTERN_SET, tmp[0]);
  831. if (ret < 0)
  832. goto err_dpcd_write;
  833. /* Clear Training Pattern, set AutoCorrect Mode = 1 */
  834. tc_write(DP0_SRCCTRL, tc_srcctrl(tc) | DP0_SRCCTRL_AUTOCORRECT);
  835. /* Wait */
  836. timeout = 100;
  837. do {
  838. udelay(1);
  839. /* Read DPCD 0x202-0x207 */
  840. ret = drm_dp_dpcd_read_link_status(aux, tmp + 2);
  841. if (ret < 0)
  842. goto err_dpcd_read;
  843. } while ((--timeout) &&
  844. !(drm_dp_channel_eq_ok(tmp + 2, tc->link.base.num_lanes)));
  845. if (timeout == 0) {
  846. /* Read DPCD 0x200-0x201 */
  847. ret = drm_dp_dpcd_read(aux, DP_SINK_COUNT, tmp, 2);
  848. if (ret < 0)
  849. goto err_dpcd_read;
  850. dev_err(dev, "channel(s) EQ not ok\n");
  851. dev_info(dev, "0x0200 SINK_COUNT: 0x%02x\n", tmp[0]);
  852. dev_info(dev, "0x0201 DEVICE_SERVICE_IRQ_VECTOR: 0x%02x\n",
  853. tmp[1]);
  854. dev_info(dev, "0x0202 LANE0_1_STATUS: 0x%02x\n", tmp[2]);
  855. dev_info(dev, "0x0204 LANE_ALIGN_STATUS_UPDATED: 0x%02x\n",
  856. tmp[4]);
  857. dev_info(dev, "0x0205 SINK_STATUS: 0x%02x\n", tmp[5]);
  858. dev_info(dev, "0x0206 ADJUST_REQUEST_LANE0_1: 0x%02x\n",
  859. tmp[6]);
  860. return -EAGAIN;
  861. }
  862. ret = tc_set_video_mode(tc, tc->mode);
  863. if (ret)
  864. goto err;
  865. /* Set M/N */
  866. ret = tc_stream_clock_calc(tc);
  867. if (ret)
  868. goto err;
  869. return 0;
  870. err_dpcd_read:
  871. dev_err(tc->dev, "Failed to read DPCD: %d\n", ret);
  872. return ret;
  873. err_dpcd_write:
  874. dev_err(tc->dev, "Failed to write DPCD: %d\n", ret);
  875. err:
  876. return ret;
  877. }
  878. static int tc_main_link_stream(struct tc_data *tc, int state)
  879. {
  880. int ret;
  881. u32 value;
  882. dev_dbg(tc->dev, "stream: %d\n", state);
  883. if (state) {
  884. value = VID_MN_GEN | DP_EN;
  885. if (tc->link.base.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  886. value |= EF_EN;
  887. tc_write(DP0CTL, value);
  888. /*
  889. * VID_EN assertion should be delayed by at least N * LSCLK
  890. * cycles from the time VID_MN_GEN is enabled in order to
  891. * generate stable values for VID_M. LSCLK is 270 MHz or
  892. * 162 MHz, VID_N is set to 32768 in tc_stream_clock_calc(),
  893. * so a delay of at least 203 us should suffice.
  894. */
  895. usleep_range(500, 1000);
  896. value |= VID_EN;
  897. tc_write(DP0CTL, value);
  898. /* Set input interface */
  899. value = DP0_AUDSRC_NO_INPUT;
  900. if (tc_test_pattern)
  901. value |= DP0_VIDSRC_COLOR_BAR;
  902. else
  903. value |= DP0_VIDSRC_DPI_RX;
  904. tc_write(SYSCTRL, value);
  905. } else {
  906. tc_write(DP0CTL, 0);
  907. }
  908. return 0;
  909. err:
  910. return ret;
  911. }
  912. static void tc_bridge_pre_enable(struct drm_bridge *bridge)
  913. {
  914. struct tc_data *tc = bridge_to_tc(bridge);
  915. drm_panel_prepare(tc->panel);
  916. }
  917. static void tc_bridge_enable(struct drm_bridge *bridge)
  918. {
  919. struct tc_data *tc = bridge_to_tc(bridge);
  920. int ret;
  921. ret = tc_main_link_setup(tc);
  922. if (ret < 0) {
  923. dev_err(tc->dev, "main link setup error: %d\n", ret);
  924. return;
  925. }
  926. ret = tc_main_link_stream(tc, 1);
  927. if (ret < 0) {
  928. dev_err(tc->dev, "main link stream start error: %d\n", ret);
  929. return;
  930. }
  931. drm_panel_enable(tc->panel);
  932. }
  933. static void tc_bridge_disable(struct drm_bridge *bridge)
  934. {
  935. struct tc_data *tc = bridge_to_tc(bridge);
  936. int ret;
  937. drm_panel_disable(tc->panel);
  938. ret = tc_main_link_stream(tc, 0);
  939. if (ret < 0)
  940. dev_err(tc->dev, "main link stream stop error: %d\n", ret);
  941. }
  942. static void tc_bridge_post_disable(struct drm_bridge *bridge)
  943. {
  944. struct tc_data *tc = bridge_to_tc(bridge);
  945. drm_panel_unprepare(tc->panel);
  946. }
  947. static bool tc_bridge_mode_fixup(struct drm_bridge *bridge,
  948. const struct drm_display_mode *mode,
  949. struct drm_display_mode *adj)
  950. {
  951. /* Fixup sync polarities, both hsync and vsync are active low */
  952. adj->flags = mode->flags;
  953. adj->flags |= (DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC);
  954. adj->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
  955. return true;
  956. }
  957. static enum drm_mode_status tc_connector_mode_valid(struct drm_connector *connector,
  958. struct drm_display_mode *mode)
  959. {
  960. /* DPI interface clock limitation: upto 154 MHz */
  961. if (mode->clock > 154000)
  962. return MODE_CLOCK_HIGH;
  963. return MODE_OK;
  964. }
  965. static void tc_bridge_mode_set(struct drm_bridge *bridge,
  966. struct drm_display_mode *mode,
  967. struct drm_display_mode *adj)
  968. {
  969. struct tc_data *tc = bridge_to_tc(bridge);
  970. tc->mode = mode;
  971. }
  972. static int tc_connector_get_modes(struct drm_connector *connector)
  973. {
  974. struct tc_data *tc = connector_to_tc(connector);
  975. struct edid *edid;
  976. unsigned int count;
  977. if (tc->panel && tc->panel->funcs && tc->panel->funcs->get_modes) {
  978. count = tc->panel->funcs->get_modes(tc->panel);
  979. if (count > 0)
  980. return count;
  981. }
  982. edid = drm_get_edid(connector, &tc->aux.ddc);
  983. kfree(tc->edid);
  984. tc->edid = edid;
  985. if (!edid)
  986. return 0;
  987. drm_connector_update_edid_property(connector, edid);
  988. count = drm_add_edid_modes(connector, edid);
  989. return count;
  990. }
  991. static void tc_connector_set_polling(struct tc_data *tc,
  992. struct drm_connector *connector)
  993. {
  994. /* TODO: add support for HPD */
  995. connector->polled = DRM_CONNECTOR_POLL_CONNECT |
  996. DRM_CONNECTOR_POLL_DISCONNECT;
  997. }
  998. static struct drm_encoder *
  999. tc_connector_best_encoder(struct drm_connector *connector)
  1000. {
  1001. struct tc_data *tc = connector_to_tc(connector);
  1002. return tc->bridge.encoder;
  1003. }
  1004. static const struct drm_connector_helper_funcs tc_connector_helper_funcs = {
  1005. .get_modes = tc_connector_get_modes,
  1006. .mode_valid = tc_connector_mode_valid,
  1007. .best_encoder = tc_connector_best_encoder,
  1008. };
  1009. static const struct drm_connector_funcs tc_connector_funcs = {
  1010. .fill_modes = drm_helper_probe_single_connector_modes,
  1011. .destroy = drm_connector_cleanup,
  1012. .reset = drm_atomic_helper_connector_reset,
  1013. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  1014. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1015. };
  1016. static int tc_bridge_attach(struct drm_bridge *bridge)
  1017. {
  1018. u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  1019. struct tc_data *tc = bridge_to_tc(bridge);
  1020. struct drm_device *drm = bridge->dev;
  1021. int ret;
  1022. /* Create eDP connector */
  1023. drm_connector_helper_add(&tc->connector, &tc_connector_helper_funcs);
  1024. ret = drm_connector_init(drm, &tc->connector, &tc_connector_funcs,
  1025. DRM_MODE_CONNECTOR_eDP);
  1026. if (ret)
  1027. return ret;
  1028. if (tc->panel)
  1029. drm_panel_attach(tc->panel, &tc->connector);
  1030. drm_display_info_set_bus_formats(&tc->connector.display_info,
  1031. &bus_format, 1);
  1032. drm_connector_attach_encoder(&tc->connector, tc->bridge.encoder);
  1033. return 0;
  1034. }
  1035. static const struct drm_bridge_funcs tc_bridge_funcs = {
  1036. .attach = tc_bridge_attach,
  1037. .mode_set = tc_bridge_mode_set,
  1038. .pre_enable = tc_bridge_pre_enable,
  1039. .enable = tc_bridge_enable,
  1040. .disable = tc_bridge_disable,
  1041. .post_disable = tc_bridge_post_disable,
  1042. .mode_fixup = tc_bridge_mode_fixup,
  1043. };
  1044. static bool tc_readable_reg(struct device *dev, unsigned int reg)
  1045. {
  1046. return reg != SYSCTRL;
  1047. }
  1048. static const struct regmap_range tc_volatile_ranges[] = {
  1049. regmap_reg_range(DP0_AUXWDATA(0), DP0_AUXSTATUS),
  1050. regmap_reg_range(DP0_LTSTAT, DP0_SNKLTCHGREQ),
  1051. regmap_reg_range(DP_PHY_CTRL, DP_PHY_CTRL),
  1052. regmap_reg_range(DP0_PLLCTRL, PXL_PLLCTRL),
  1053. regmap_reg_range(VFUEN0, VFUEN0),
  1054. };
  1055. static const struct regmap_access_table tc_volatile_table = {
  1056. .yes_ranges = tc_volatile_ranges,
  1057. .n_yes_ranges = ARRAY_SIZE(tc_volatile_ranges),
  1058. };
  1059. static bool tc_writeable_reg(struct device *dev, unsigned int reg)
  1060. {
  1061. return (reg != TC_IDREG) &&
  1062. (reg != DP0_LTSTAT) &&
  1063. (reg != DP0_SNKLTCHGREQ);
  1064. }
  1065. static const struct regmap_config tc_regmap_config = {
  1066. .name = "tc358767",
  1067. .reg_bits = 16,
  1068. .val_bits = 32,
  1069. .reg_stride = 4,
  1070. .max_register = PLL_DBG,
  1071. .cache_type = REGCACHE_RBTREE,
  1072. .readable_reg = tc_readable_reg,
  1073. .volatile_table = &tc_volatile_table,
  1074. .writeable_reg = tc_writeable_reg,
  1075. .reg_format_endian = REGMAP_ENDIAN_BIG,
  1076. .val_format_endian = REGMAP_ENDIAN_LITTLE,
  1077. };
  1078. static int tc_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1079. {
  1080. struct device *dev = &client->dev;
  1081. struct tc_data *tc;
  1082. int ret;
  1083. tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL);
  1084. if (!tc)
  1085. return -ENOMEM;
  1086. tc->dev = dev;
  1087. /* port@2 is the output port */
  1088. ret = drm_of_find_panel_or_bridge(dev->of_node, 2, 0, &tc->panel, NULL);
  1089. if (ret && ret != -ENODEV)
  1090. return ret;
  1091. /* Shut down GPIO is optional */
  1092. tc->sd_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
  1093. if (IS_ERR(tc->sd_gpio))
  1094. return PTR_ERR(tc->sd_gpio);
  1095. if (tc->sd_gpio) {
  1096. gpiod_set_value_cansleep(tc->sd_gpio, 0);
  1097. usleep_range(5000, 10000);
  1098. }
  1099. /* Reset GPIO is optional */
  1100. tc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  1101. if (IS_ERR(tc->reset_gpio))
  1102. return PTR_ERR(tc->reset_gpio);
  1103. if (tc->reset_gpio) {
  1104. gpiod_set_value_cansleep(tc->reset_gpio, 1);
  1105. usleep_range(5000, 10000);
  1106. }
  1107. tc->refclk = devm_clk_get(dev, "ref");
  1108. if (IS_ERR(tc->refclk)) {
  1109. ret = PTR_ERR(tc->refclk);
  1110. dev_err(dev, "Failed to get refclk: %d\n", ret);
  1111. return ret;
  1112. }
  1113. tc->regmap = devm_regmap_init_i2c(client, &tc_regmap_config);
  1114. if (IS_ERR(tc->regmap)) {
  1115. ret = PTR_ERR(tc->regmap);
  1116. dev_err(dev, "Failed to initialize regmap: %d\n", ret);
  1117. return ret;
  1118. }
  1119. ret = regmap_read(tc->regmap, TC_IDREG, &tc->rev);
  1120. if (ret) {
  1121. dev_err(tc->dev, "can not read device ID: %d\n", ret);
  1122. return ret;
  1123. }
  1124. if ((tc->rev != 0x6601) && (tc->rev != 0x6603)) {
  1125. dev_err(tc->dev, "invalid device ID: 0x%08x\n", tc->rev);
  1126. return -EINVAL;
  1127. }
  1128. tc->assr = (tc->rev == 0x6601); /* Enable ASSR for eDP panels */
  1129. ret = tc_aux_link_setup(tc);
  1130. if (ret)
  1131. return ret;
  1132. /* Register DP AUX channel */
  1133. tc->aux.name = "TC358767 AUX i2c adapter";
  1134. tc->aux.dev = tc->dev;
  1135. tc->aux.transfer = tc_aux_transfer;
  1136. ret = drm_dp_aux_register(&tc->aux);
  1137. if (ret)
  1138. return ret;
  1139. ret = tc_get_display_props(tc);
  1140. if (ret)
  1141. goto err_unregister_aux;
  1142. tc_connector_set_polling(tc, &tc->connector);
  1143. tc->bridge.funcs = &tc_bridge_funcs;
  1144. tc->bridge.of_node = dev->of_node;
  1145. drm_bridge_add(&tc->bridge);
  1146. i2c_set_clientdata(client, tc);
  1147. return 0;
  1148. err_unregister_aux:
  1149. drm_dp_aux_unregister(&tc->aux);
  1150. return ret;
  1151. }
  1152. static int tc_remove(struct i2c_client *client)
  1153. {
  1154. struct tc_data *tc = i2c_get_clientdata(client);
  1155. drm_bridge_remove(&tc->bridge);
  1156. drm_dp_aux_unregister(&tc->aux);
  1157. tc_pxl_pll_dis(tc);
  1158. return 0;
  1159. }
  1160. static const struct i2c_device_id tc358767_i2c_ids[] = {
  1161. { "tc358767", 0 },
  1162. { }
  1163. };
  1164. MODULE_DEVICE_TABLE(i2c, tc358767_i2c_ids);
  1165. static const struct of_device_id tc358767_of_ids[] = {
  1166. { .compatible = "toshiba,tc358767", },
  1167. { }
  1168. };
  1169. MODULE_DEVICE_TABLE(of, tc358767_of_ids);
  1170. static struct i2c_driver tc358767_driver = {
  1171. .driver = {
  1172. .name = "tc358767",
  1173. .of_match_table = tc358767_of_ids,
  1174. },
  1175. .id_table = tc358767_i2c_ids,
  1176. .probe = tc_probe,
  1177. .remove = tc_remove,
  1178. };
  1179. module_i2c_driver(tc358767_driver);
  1180. MODULE_AUTHOR("Andrey Gusakov <andrey.gusakov@cogentembedded.com>");
  1181. MODULE_DESCRIPTION("tc358767 eDP encoder driver");
  1182. MODULE_LICENSE("GPL");