tc358767.c 35 KB

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