tc358767.c 36 KB

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