tc358767.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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. .dpms = drm_atomic_helper_connector_dpms,
  1005. .fill_modes = drm_helper_probe_single_connector_modes,
  1006. .destroy = drm_connector_cleanup,
  1007. .reset = drm_atomic_helper_connector_reset,
  1008. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  1009. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1010. };
  1011. static int tc_bridge_attach(struct drm_bridge *bridge)
  1012. {
  1013. u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  1014. struct tc_data *tc = bridge_to_tc(bridge);
  1015. struct drm_device *drm = bridge->dev;
  1016. int ret;
  1017. /* Create eDP connector */
  1018. drm_connector_helper_add(&tc->connector, &tc_connector_helper_funcs);
  1019. ret = drm_connector_init(drm, &tc->connector, &tc_connector_funcs,
  1020. DRM_MODE_CONNECTOR_eDP);
  1021. if (ret)
  1022. return ret;
  1023. if (tc->panel)
  1024. drm_panel_attach(tc->panel, &tc->connector);
  1025. drm_display_info_set_bus_formats(&tc->connector.display_info,
  1026. &bus_format, 1);
  1027. drm_mode_connector_attach_encoder(&tc->connector, tc->bridge.encoder);
  1028. return 0;
  1029. }
  1030. static const struct drm_bridge_funcs tc_bridge_funcs = {
  1031. .attach = tc_bridge_attach,
  1032. .mode_set = tc_bridge_mode_set,
  1033. .pre_enable = tc_bridge_pre_enable,
  1034. .enable = tc_bridge_enable,
  1035. .disable = tc_bridge_disable,
  1036. .post_disable = tc_bridge_post_disable,
  1037. .mode_fixup = tc_bridge_mode_fixup,
  1038. };
  1039. static bool tc_readable_reg(struct device *dev, unsigned int reg)
  1040. {
  1041. return reg != SYSCTRL;
  1042. }
  1043. static const struct regmap_range tc_volatile_ranges[] = {
  1044. regmap_reg_range(DP0_AUXWDATA(0), DP0_AUXSTATUS),
  1045. regmap_reg_range(DP0_LTSTAT, DP0_SNKLTCHGREQ),
  1046. regmap_reg_range(DP_PHY_CTRL, DP_PHY_CTRL),
  1047. regmap_reg_range(DP0_PLLCTRL, PXL_PLLCTRL),
  1048. regmap_reg_range(VFUEN0, VFUEN0),
  1049. };
  1050. static const struct regmap_access_table tc_volatile_table = {
  1051. .yes_ranges = tc_volatile_ranges,
  1052. .n_yes_ranges = ARRAY_SIZE(tc_volatile_ranges),
  1053. };
  1054. static bool tc_writeable_reg(struct device *dev, unsigned int reg)
  1055. {
  1056. return (reg != TC_IDREG) &&
  1057. (reg != DP0_LTSTAT) &&
  1058. (reg != DP0_SNKLTCHGREQ);
  1059. }
  1060. static const struct regmap_config tc_regmap_config = {
  1061. .name = "tc358767",
  1062. .reg_bits = 16,
  1063. .val_bits = 32,
  1064. .reg_stride = 4,
  1065. .max_register = PLL_DBG,
  1066. .cache_type = REGCACHE_RBTREE,
  1067. .readable_reg = tc_readable_reg,
  1068. .volatile_table = &tc_volatile_table,
  1069. .writeable_reg = tc_writeable_reg,
  1070. .reg_format_endian = REGMAP_ENDIAN_BIG,
  1071. .val_format_endian = REGMAP_ENDIAN_LITTLE,
  1072. };
  1073. static int tc_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1074. {
  1075. struct device *dev = &client->dev;
  1076. struct device_node *ep;
  1077. struct tc_data *tc;
  1078. int ret;
  1079. tc = devm_kzalloc(dev, sizeof(*tc), GFP_KERNEL);
  1080. if (!tc)
  1081. return -ENOMEM;
  1082. tc->dev = dev;
  1083. /* port@2 is the output port */
  1084. ep = of_graph_get_endpoint_by_regs(dev->of_node, 2, -1);
  1085. if (ep) {
  1086. struct device_node *remote;
  1087. remote = of_graph_get_remote_port_parent(ep);
  1088. if (!remote) {
  1089. dev_warn(dev, "endpoint %s not connected\n",
  1090. ep->full_name);
  1091. of_node_put(ep);
  1092. return -ENODEV;
  1093. }
  1094. of_node_put(ep);
  1095. tc->panel = of_drm_find_panel(remote);
  1096. if (tc->panel) {
  1097. dev_dbg(dev, "found panel %s\n", remote->full_name);
  1098. } else {
  1099. dev_dbg(dev, "waiting for panel %s\n",
  1100. remote->full_name);
  1101. of_node_put(remote);
  1102. return -EPROBE_DEFER;
  1103. }
  1104. of_node_put(remote);
  1105. }
  1106. /* Shut down GPIO is optional */
  1107. tc->sd_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
  1108. if (IS_ERR(tc->sd_gpio))
  1109. return PTR_ERR(tc->sd_gpio);
  1110. if (tc->sd_gpio) {
  1111. gpiod_set_value_cansleep(tc->sd_gpio, 0);
  1112. usleep_range(5000, 10000);
  1113. }
  1114. /* Reset GPIO is optional */
  1115. tc->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  1116. if (IS_ERR(tc->reset_gpio))
  1117. return PTR_ERR(tc->reset_gpio);
  1118. if (tc->reset_gpio) {
  1119. gpiod_set_value_cansleep(tc->reset_gpio, 1);
  1120. usleep_range(5000, 10000);
  1121. }
  1122. tc->refclk = devm_clk_get(dev, "ref");
  1123. if (IS_ERR(tc->refclk)) {
  1124. ret = PTR_ERR(tc->refclk);
  1125. dev_err(dev, "Failed to get refclk: %d\n", ret);
  1126. return ret;
  1127. }
  1128. tc->regmap = devm_regmap_init_i2c(client, &tc_regmap_config);
  1129. if (IS_ERR(tc->regmap)) {
  1130. ret = PTR_ERR(tc->regmap);
  1131. dev_err(dev, "Failed to initialize regmap: %d\n", ret);
  1132. return ret;
  1133. }
  1134. ret = regmap_read(tc->regmap, TC_IDREG, &tc->rev);
  1135. if (ret) {
  1136. dev_err(tc->dev, "can not read device ID: %d\n", ret);
  1137. return ret;
  1138. }
  1139. if ((tc->rev != 0x6601) && (tc->rev != 0x6603)) {
  1140. dev_err(tc->dev, "invalid device ID: 0x%08x\n", tc->rev);
  1141. return -EINVAL;
  1142. }
  1143. tc->assr = (tc->rev == 0x6601); /* Enable ASSR for eDP panels */
  1144. ret = tc_aux_link_setup(tc);
  1145. if (ret)
  1146. return ret;
  1147. /* Register DP AUX channel */
  1148. tc->aux.name = "TC358767 AUX i2c adapter";
  1149. tc->aux.dev = tc->dev;
  1150. tc->aux.transfer = tc_aux_transfer;
  1151. ret = drm_dp_aux_register(&tc->aux);
  1152. if (ret)
  1153. return ret;
  1154. ret = tc_get_display_props(tc);
  1155. if (ret)
  1156. goto err_unregister_aux;
  1157. tc_connector_set_polling(tc, &tc->connector);
  1158. tc->bridge.funcs = &tc_bridge_funcs;
  1159. tc->bridge.of_node = dev->of_node;
  1160. ret = drm_bridge_add(&tc->bridge);
  1161. if (ret) {
  1162. dev_err(dev, "Failed to add drm_bridge: %d\n", ret);
  1163. goto err_unregister_aux;
  1164. }
  1165. i2c_set_clientdata(client, tc);
  1166. return 0;
  1167. err_unregister_aux:
  1168. drm_dp_aux_unregister(&tc->aux);
  1169. return ret;
  1170. }
  1171. static int tc_remove(struct i2c_client *client)
  1172. {
  1173. struct tc_data *tc = i2c_get_clientdata(client);
  1174. drm_bridge_remove(&tc->bridge);
  1175. drm_dp_aux_unregister(&tc->aux);
  1176. tc_pxl_pll_dis(tc);
  1177. return 0;
  1178. }
  1179. static const struct i2c_device_id tc358767_i2c_ids[] = {
  1180. { "tc358767", 0 },
  1181. { }
  1182. };
  1183. MODULE_DEVICE_TABLE(i2c, tc358767_i2c_ids);
  1184. static const struct of_device_id tc358767_of_ids[] = {
  1185. { .compatible = "toshiba,tc358767", },
  1186. { }
  1187. };
  1188. MODULE_DEVICE_TABLE(of, tc358767_of_ids);
  1189. static struct i2c_driver tc358767_driver = {
  1190. .driver = {
  1191. .name = "tc358767",
  1192. .of_match_table = tc358767_of_ids,
  1193. },
  1194. .id_table = tc358767_i2c_ids,
  1195. .probe = tc_probe,
  1196. .remove = tc_remove,
  1197. };
  1198. module_i2c_driver(tc358767_driver);
  1199. MODULE_AUTHOR("Andrey Gusakov <andrey.gusakov@cogentembedded.com>");
  1200. MODULE_DESCRIPTION("tc358767 eDP encoder driver");
  1201. MODULE_LICENSE("GPL");