sor.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. /*
  2. * Copyright (C) 2013 NVIDIA Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/gpio.h>
  11. #include <linux/io.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/reset.h>
  14. #include <soc/tegra/pmc.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_dp_helper.h>
  17. #include <drm/drm_panel.h>
  18. #include "dc.h"
  19. #include "drm.h"
  20. #include "sor.h"
  21. struct tegra_sor {
  22. struct host1x_client client;
  23. struct tegra_output output;
  24. struct device *dev;
  25. void __iomem *regs;
  26. struct reset_control *rst;
  27. struct clk *clk_parent;
  28. struct clk *clk_safe;
  29. struct clk *clk_dp;
  30. struct clk *clk;
  31. struct tegra_dpaux *dpaux;
  32. struct mutex lock;
  33. bool enabled;
  34. struct drm_info_list *debugfs_files;
  35. struct drm_minor *minor;
  36. struct dentry *debugfs;
  37. };
  38. struct tegra_sor_config {
  39. u32 bits_per_pixel;
  40. u32 active_polarity;
  41. u32 active_count;
  42. u32 tu_size;
  43. u32 active_frac;
  44. u32 watermark;
  45. u32 hblank_symbols;
  46. u32 vblank_symbols;
  47. };
  48. static inline struct tegra_sor *
  49. host1x_client_to_sor(struct host1x_client *client)
  50. {
  51. return container_of(client, struct tegra_sor, client);
  52. }
  53. static inline struct tegra_sor *to_sor(struct tegra_output *output)
  54. {
  55. return container_of(output, struct tegra_sor, output);
  56. }
  57. static inline u32 tegra_sor_readl(struct tegra_sor *sor, unsigned long offset)
  58. {
  59. return readl(sor->regs + (offset << 2));
  60. }
  61. static inline void tegra_sor_writel(struct tegra_sor *sor, u32 value,
  62. unsigned long offset)
  63. {
  64. writel(value, sor->regs + (offset << 2));
  65. }
  66. static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
  67. struct drm_dp_link *link)
  68. {
  69. unsigned int i;
  70. u8 pattern;
  71. u32 value;
  72. int err;
  73. /* setup lane parameters */
  74. value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
  75. SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
  76. SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
  77. SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
  78. tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT_0);
  79. value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
  80. SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
  81. SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
  82. SOR_LANE_PREEMPHASIS_LANE0(0x0f);
  83. tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS_0);
  84. value = SOR_LANE_POST_CURSOR_LANE3(0x00) |
  85. SOR_LANE_POST_CURSOR_LANE2(0x00) |
  86. SOR_LANE_POST_CURSOR_LANE1(0x00) |
  87. SOR_LANE_POST_CURSOR_LANE0(0x00);
  88. tegra_sor_writel(sor, value, SOR_LANE_POST_CURSOR_0);
  89. /* disable LVDS mode */
  90. tegra_sor_writel(sor, 0, SOR_LVDS);
  91. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  92. value |= SOR_DP_PADCTL_TX_PU_ENABLE;
  93. value &= ~SOR_DP_PADCTL_TX_PU_MASK;
  94. value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
  95. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  96. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  97. value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
  98. SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
  99. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  100. usleep_range(10, 100);
  101. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  102. value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
  103. SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
  104. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  105. err = tegra_dpaux_prepare(sor->dpaux, DP_SET_ANSI_8B10B);
  106. if (err < 0)
  107. return err;
  108. for (i = 0, value = 0; i < link->num_lanes; i++) {
  109. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  110. SOR_DP_TPG_SCRAMBLER_NONE |
  111. SOR_DP_TPG_PATTERN_TRAIN1;
  112. value = (value << 8) | lane;
  113. }
  114. tegra_sor_writel(sor, value, SOR_DP_TPG);
  115. pattern = DP_TRAINING_PATTERN_1;
  116. err = tegra_dpaux_train(sor->dpaux, link, pattern);
  117. if (err < 0)
  118. return err;
  119. value = tegra_sor_readl(sor, SOR_DP_SPARE_0);
  120. value |= SOR_DP_SPARE_SEQ_ENABLE;
  121. value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
  122. value |= SOR_DP_SPARE_MACRO_SOR_CLK;
  123. tegra_sor_writel(sor, value, SOR_DP_SPARE_0);
  124. for (i = 0, value = 0; i < link->num_lanes; i++) {
  125. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  126. SOR_DP_TPG_SCRAMBLER_NONE |
  127. SOR_DP_TPG_PATTERN_TRAIN2;
  128. value = (value << 8) | lane;
  129. }
  130. tegra_sor_writel(sor, value, SOR_DP_TPG);
  131. pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
  132. err = tegra_dpaux_train(sor->dpaux, link, pattern);
  133. if (err < 0)
  134. return err;
  135. for (i = 0, value = 0; i < link->num_lanes; i++) {
  136. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  137. SOR_DP_TPG_SCRAMBLER_GALIOS |
  138. SOR_DP_TPG_PATTERN_NONE;
  139. value = (value << 8) | lane;
  140. }
  141. tegra_sor_writel(sor, value, SOR_DP_TPG);
  142. pattern = DP_TRAINING_PATTERN_DISABLE;
  143. err = tegra_dpaux_train(sor->dpaux, link, pattern);
  144. if (err < 0)
  145. return err;
  146. return 0;
  147. }
  148. static void tegra_sor_super_update(struct tegra_sor *sor)
  149. {
  150. tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
  151. tegra_sor_writel(sor, 1, SOR_SUPER_STATE_0);
  152. tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
  153. }
  154. static void tegra_sor_update(struct tegra_sor *sor)
  155. {
  156. tegra_sor_writel(sor, 0, SOR_STATE_0);
  157. tegra_sor_writel(sor, 1, SOR_STATE_0);
  158. tegra_sor_writel(sor, 0, SOR_STATE_0);
  159. }
  160. static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
  161. {
  162. u32 value;
  163. value = tegra_sor_readl(sor, SOR_PWM_DIV);
  164. value &= ~SOR_PWM_DIV_MASK;
  165. value |= 0x400; /* period */
  166. tegra_sor_writel(sor, value, SOR_PWM_DIV);
  167. value = tegra_sor_readl(sor, SOR_PWM_CTL);
  168. value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
  169. value |= 0x400; /* duty cycle */
  170. value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
  171. value |= SOR_PWM_CTL_TRIGGER;
  172. tegra_sor_writel(sor, value, SOR_PWM_CTL);
  173. timeout = jiffies + msecs_to_jiffies(timeout);
  174. while (time_before(jiffies, timeout)) {
  175. value = tegra_sor_readl(sor, SOR_PWM_CTL);
  176. if ((value & SOR_PWM_CTL_TRIGGER) == 0)
  177. return 0;
  178. usleep_range(25, 100);
  179. }
  180. return -ETIMEDOUT;
  181. }
  182. static int tegra_sor_attach(struct tegra_sor *sor)
  183. {
  184. unsigned long value, timeout;
  185. /* wake up in normal mode */
  186. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  187. value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
  188. value |= SOR_SUPER_STATE_MODE_NORMAL;
  189. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  190. tegra_sor_super_update(sor);
  191. /* attach */
  192. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  193. value |= SOR_SUPER_STATE_ATTACHED;
  194. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  195. tegra_sor_super_update(sor);
  196. timeout = jiffies + msecs_to_jiffies(250);
  197. while (time_before(jiffies, timeout)) {
  198. value = tegra_sor_readl(sor, SOR_TEST);
  199. if ((value & SOR_TEST_ATTACHED) != 0)
  200. return 0;
  201. usleep_range(25, 100);
  202. }
  203. return -ETIMEDOUT;
  204. }
  205. static int tegra_sor_wakeup(struct tegra_sor *sor)
  206. {
  207. unsigned long value, timeout;
  208. timeout = jiffies + msecs_to_jiffies(250);
  209. /* wait for head to wake up */
  210. while (time_before(jiffies, timeout)) {
  211. value = tegra_sor_readl(sor, SOR_TEST);
  212. value &= SOR_TEST_HEAD_MODE_MASK;
  213. if (value == SOR_TEST_HEAD_MODE_AWAKE)
  214. return 0;
  215. usleep_range(25, 100);
  216. }
  217. return -ETIMEDOUT;
  218. }
  219. static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
  220. {
  221. u32 value;
  222. value = tegra_sor_readl(sor, SOR_PWR);
  223. value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
  224. tegra_sor_writel(sor, value, SOR_PWR);
  225. timeout = jiffies + msecs_to_jiffies(timeout);
  226. while (time_before(jiffies, timeout)) {
  227. value = tegra_sor_readl(sor, SOR_PWR);
  228. if ((value & SOR_PWR_TRIGGER) == 0)
  229. return 0;
  230. usleep_range(25, 100);
  231. }
  232. return -ETIMEDOUT;
  233. }
  234. struct tegra_sor_params {
  235. /* number of link clocks per line */
  236. unsigned int num_clocks;
  237. /* ratio between input and output */
  238. u64 ratio;
  239. /* precision factor */
  240. u64 precision;
  241. unsigned int active_polarity;
  242. unsigned int active_count;
  243. unsigned int active_frac;
  244. unsigned int tu_size;
  245. unsigned int error;
  246. };
  247. static int tegra_sor_compute_params(struct tegra_sor *sor,
  248. struct tegra_sor_params *params,
  249. unsigned int tu_size)
  250. {
  251. u64 active_sym, active_count, frac, approx;
  252. u32 active_polarity, active_frac = 0;
  253. const u64 f = params->precision;
  254. s64 error;
  255. active_sym = params->ratio * tu_size;
  256. active_count = div_u64(active_sym, f) * f;
  257. frac = active_sym - active_count;
  258. /* fraction < 0.5 */
  259. if (frac >= (f / 2)) {
  260. active_polarity = 1;
  261. frac = f - frac;
  262. } else {
  263. active_polarity = 0;
  264. }
  265. if (frac != 0) {
  266. frac = div_u64(f * f, frac); /* 1/fraction */
  267. if (frac <= (15 * f)) {
  268. active_frac = div_u64(frac, f);
  269. /* round up */
  270. if (active_polarity)
  271. active_frac++;
  272. } else {
  273. active_frac = active_polarity ? 1 : 15;
  274. }
  275. }
  276. if (active_frac == 1)
  277. active_polarity = 0;
  278. if (active_polarity == 1) {
  279. if (active_frac) {
  280. approx = active_count + (active_frac * (f - 1)) * f;
  281. approx = div_u64(approx, active_frac * f);
  282. } else {
  283. approx = active_count + f;
  284. }
  285. } else {
  286. if (active_frac)
  287. approx = active_count + div_u64(f, active_frac);
  288. else
  289. approx = active_count;
  290. }
  291. error = div_s64(active_sym - approx, tu_size);
  292. error *= params->num_clocks;
  293. if (error <= 0 && abs64(error) < params->error) {
  294. params->active_count = div_u64(active_count, f);
  295. params->active_polarity = active_polarity;
  296. params->active_frac = active_frac;
  297. params->error = abs64(error);
  298. params->tu_size = tu_size;
  299. if (error == 0)
  300. return true;
  301. }
  302. return false;
  303. }
  304. static int tegra_sor_calc_config(struct tegra_sor *sor,
  305. struct drm_display_mode *mode,
  306. struct tegra_sor_config *config,
  307. struct drm_dp_link *link)
  308. {
  309. const u64 f = 100000, link_rate = link->rate * 1000;
  310. const u64 pclk = mode->clock * 1000;
  311. u64 input, output, watermark, num;
  312. struct tegra_sor_params params;
  313. u32 num_syms_per_line;
  314. unsigned int i;
  315. if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
  316. return -EINVAL;
  317. output = link_rate * 8 * link->num_lanes;
  318. input = pclk * config->bits_per_pixel;
  319. if (input >= output)
  320. return -ERANGE;
  321. memset(&params, 0, sizeof(params));
  322. params.ratio = div64_u64(input * f, output);
  323. params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
  324. params.precision = f;
  325. params.error = 64 * f;
  326. params.tu_size = 64;
  327. for (i = params.tu_size; i >= 32; i--)
  328. if (tegra_sor_compute_params(sor, &params, i))
  329. break;
  330. if (params.active_frac == 0) {
  331. config->active_polarity = 0;
  332. config->active_count = params.active_count;
  333. if (!params.active_polarity)
  334. config->active_count--;
  335. config->tu_size = params.tu_size;
  336. config->active_frac = 1;
  337. } else {
  338. config->active_polarity = params.active_polarity;
  339. config->active_count = params.active_count;
  340. config->active_frac = params.active_frac;
  341. config->tu_size = params.tu_size;
  342. }
  343. dev_dbg(sor->dev,
  344. "polarity: %d active count: %d tu size: %d active frac: %d\n",
  345. config->active_polarity, config->active_count,
  346. config->tu_size, config->active_frac);
  347. watermark = params.ratio * config->tu_size * (f - params.ratio);
  348. watermark = div_u64(watermark, f);
  349. watermark = div_u64(watermark + params.error, f);
  350. config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
  351. num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
  352. (link->num_lanes * 8);
  353. if (config->watermark > 30) {
  354. config->watermark = 30;
  355. dev_err(sor->dev,
  356. "unable to compute TU size, forcing watermark to %u\n",
  357. config->watermark);
  358. } else if (config->watermark > num_syms_per_line) {
  359. config->watermark = num_syms_per_line;
  360. dev_err(sor->dev, "watermark too high, forcing to %u\n",
  361. config->watermark);
  362. }
  363. /* compute the number of symbols per horizontal blanking interval */
  364. num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
  365. config->hblank_symbols = div_u64(num, pclk);
  366. if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  367. config->hblank_symbols -= 3;
  368. config->hblank_symbols -= 12 / link->num_lanes;
  369. /* compute the number of symbols per vertical blanking interval */
  370. num = (mode->hdisplay - 25) * link_rate;
  371. config->vblank_symbols = div_u64(num, pclk);
  372. config->vblank_symbols -= 36 / link->num_lanes + 4;
  373. dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
  374. config->vblank_symbols);
  375. return 0;
  376. }
  377. static int tegra_sor_detach(struct tegra_sor *sor)
  378. {
  379. unsigned long value, timeout;
  380. /* switch to safe mode */
  381. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  382. value &= ~SOR_SUPER_STATE_MODE_NORMAL;
  383. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  384. tegra_sor_super_update(sor);
  385. timeout = jiffies + msecs_to_jiffies(250);
  386. while (time_before(jiffies, timeout)) {
  387. value = tegra_sor_readl(sor, SOR_PWR);
  388. if (value & SOR_PWR_MODE_SAFE)
  389. break;
  390. }
  391. if ((value & SOR_PWR_MODE_SAFE) == 0)
  392. return -ETIMEDOUT;
  393. /* go to sleep */
  394. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  395. value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
  396. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  397. tegra_sor_super_update(sor);
  398. /* detach */
  399. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  400. value &= ~SOR_SUPER_STATE_ATTACHED;
  401. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  402. tegra_sor_super_update(sor);
  403. timeout = jiffies + msecs_to_jiffies(250);
  404. while (time_before(jiffies, timeout)) {
  405. value = tegra_sor_readl(sor, SOR_TEST);
  406. if ((value & SOR_TEST_ATTACHED) == 0)
  407. break;
  408. usleep_range(25, 100);
  409. }
  410. if ((value & SOR_TEST_ATTACHED) != 0)
  411. return -ETIMEDOUT;
  412. return 0;
  413. }
  414. static int tegra_sor_power_down(struct tegra_sor *sor)
  415. {
  416. unsigned long value, timeout;
  417. int err;
  418. value = tegra_sor_readl(sor, SOR_PWR);
  419. value &= ~SOR_PWR_NORMAL_STATE_PU;
  420. value |= SOR_PWR_TRIGGER;
  421. tegra_sor_writel(sor, value, SOR_PWR);
  422. timeout = jiffies + msecs_to_jiffies(250);
  423. while (time_before(jiffies, timeout)) {
  424. value = tegra_sor_readl(sor, SOR_PWR);
  425. if ((value & SOR_PWR_TRIGGER) == 0)
  426. return 0;
  427. usleep_range(25, 100);
  428. }
  429. if ((value & SOR_PWR_TRIGGER) != 0)
  430. return -ETIMEDOUT;
  431. err = clk_set_parent(sor->clk, sor->clk_safe);
  432. if (err < 0)
  433. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  434. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  435. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
  436. SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
  437. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  438. /* stop lane sequencer */
  439. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
  440. SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
  441. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  442. timeout = jiffies + msecs_to_jiffies(250);
  443. while (time_before(jiffies, timeout)) {
  444. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  445. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  446. break;
  447. usleep_range(25, 100);
  448. }
  449. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
  450. return -ETIMEDOUT;
  451. value = tegra_sor_readl(sor, SOR_PLL_2);
  452. value |= SOR_PLL_2_PORT_POWERDOWN;
  453. tegra_sor_writel(sor, value, SOR_PLL_2);
  454. usleep_range(20, 100);
  455. value = tegra_sor_readl(sor, SOR_PLL_0);
  456. value |= SOR_PLL_0_POWER_OFF;
  457. value |= SOR_PLL_0_VCOPD;
  458. tegra_sor_writel(sor, value, SOR_PLL_0);
  459. value = tegra_sor_readl(sor, SOR_PLL_2);
  460. value |= SOR_PLL_2_SEQ_PLLCAPPD;
  461. value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
  462. tegra_sor_writel(sor, value, SOR_PLL_2);
  463. usleep_range(20, 100);
  464. return 0;
  465. }
  466. static int tegra_sor_crc_open(struct inode *inode, struct file *file)
  467. {
  468. file->private_data = inode->i_private;
  469. return 0;
  470. }
  471. static int tegra_sor_crc_release(struct inode *inode, struct file *file)
  472. {
  473. return 0;
  474. }
  475. static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
  476. {
  477. u32 value;
  478. timeout = jiffies + msecs_to_jiffies(timeout);
  479. while (time_before(jiffies, timeout)) {
  480. value = tegra_sor_readl(sor, SOR_CRC_A);
  481. if (value & SOR_CRC_A_VALID)
  482. return 0;
  483. usleep_range(100, 200);
  484. }
  485. return -ETIMEDOUT;
  486. }
  487. static ssize_t tegra_sor_crc_read(struct file *file, char __user *buffer,
  488. size_t size, loff_t *ppos)
  489. {
  490. struct tegra_sor *sor = file->private_data;
  491. ssize_t num, err;
  492. char buf[10];
  493. u32 value;
  494. mutex_lock(&sor->lock);
  495. if (!sor->enabled) {
  496. err = -EAGAIN;
  497. goto unlock;
  498. }
  499. value = tegra_sor_readl(sor, SOR_STATE_1);
  500. value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
  501. tegra_sor_writel(sor, value, SOR_STATE_1);
  502. value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
  503. value |= SOR_CRC_CNTRL_ENABLE;
  504. tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
  505. value = tegra_sor_readl(sor, SOR_TEST);
  506. value &= ~SOR_TEST_CRC_POST_SERIALIZE;
  507. tegra_sor_writel(sor, value, SOR_TEST);
  508. err = tegra_sor_crc_wait(sor, 100);
  509. if (err < 0)
  510. goto unlock;
  511. tegra_sor_writel(sor, SOR_CRC_A_RESET, SOR_CRC_A);
  512. value = tegra_sor_readl(sor, SOR_CRC_B);
  513. num = scnprintf(buf, sizeof(buf), "%08x\n", value);
  514. err = simple_read_from_buffer(buffer, size, ppos, buf, num);
  515. unlock:
  516. mutex_unlock(&sor->lock);
  517. return err;
  518. }
  519. static const struct file_operations tegra_sor_crc_fops = {
  520. .owner = THIS_MODULE,
  521. .open = tegra_sor_crc_open,
  522. .read = tegra_sor_crc_read,
  523. .release = tegra_sor_crc_release,
  524. };
  525. static int tegra_sor_show_regs(struct seq_file *s, void *data)
  526. {
  527. struct drm_info_node *node = s->private;
  528. struct tegra_sor *sor = node->info_ent->data;
  529. #define DUMP_REG(name) \
  530. seq_printf(s, "%-38s %#05x %08x\n", #name, name, \
  531. tegra_sor_readl(sor, name))
  532. DUMP_REG(SOR_CTXSW);
  533. DUMP_REG(SOR_SUPER_STATE_0);
  534. DUMP_REG(SOR_SUPER_STATE_1);
  535. DUMP_REG(SOR_STATE_0);
  536. DUMP_REG(SOR_STATE_1);
  537. DUMP_REG(SOR_HEAD_STATE_0(0));
  538. DUMP_REG(SOR_HEAD_STATE_0(1));
  539. DUMP_REG(SOR_HEAD_STATE_1(0));
  540. DUMP_REG(SOR_HEAD_STATE_1(1));
  541. DUMP_REG(SOR_HEAD_STATE_2(0));
  542. DUMP_REG(SOR_HEAD_STATE_2(1));
  543. DUMP_REG(SOR_HEAD_STATE_3(0));
  544. DUMP_REG(SOR_HEAD_STATE_3(1));
  545. DUMP_REG(SOR_HEAD_STATE_4(0));
  546. DUMP_REG(SOR_HEAD_STATE_4(1));
  547. DUMP_REG(SOR_HEAD_STATE_5(0));
  548. DUMP_REG(SOR_HEAD_STATE_5(1));
  549. DUMP_REG(SOR_CRC_CNTRL);
  550. DUMP_REG(SOR_DP_DEBUG_MVID);
  551. DUMP_REG(SOR_CLK_CNTRL);
  552. DUMP_REG(SOR_CAP);
  553. DUMP_REG(SOR_PWR);
  554. DUMP_REG(SOR_TEST);
  555. DUMP_REG(SOR_PLL_0);
  556. DUMP_REG(SOR_PLL_1);
  557. DUMP_REG(SOR_PLL_2);
  558. DUMP_REG(SOR_PLL_3);
  559. DUMP_REG(SOR_CSTM);
  560. DUMP_REG(SOR_LVDS);
  561. DUMP_REG(SOR_CRC_A);
  562. DUMP_REG(SOR_CRC_B);
  563. DUMP_REG(SOR_BLANK);
  564. DUMP_REG(SOR_SEQ_CTL);
  565. DUMP_REG(SOR_LANE_SEQ_CTL);
  566. DUMP_REG(SOR_SEQ_INST(0));
  567. DUMP_REG(SOR_SEQ_INST(1));
  568. DUMP_REG(SOR_SEQ_INST(2));
  569. DUMP_REG(SOR_SEQ_INST(3));
  570. DUMP_REG(SOR_SEQ_INST(4));
  571. DUMP_REG(SOR_SEQ_INST(5));
  572. DUMP_REG(SOR_SEQ_INST(6));
  573. DUMP_REG(SOR_SEQ_INST(7));
  574. DUMP_REG(SOR_SEQ_INST(8));
  575. DUMP_REG(SOR_SEQ_INST(9));
  576. DUMP_REG(SOR_SEQ_INST(10));
  577. DUMP_REG(SOR_SEQ_INST(11));
  578. DUMP_REG(SOR_SEQ_INST(12));
  579. DUMP_REG(SOR_SEQ_INST(13));
  580. DUMP_REG(SOR_SEQ_INST(14));
  581. DUMP_REG(SOR_SEQ_INST(15));
  582. DUMP_REG(SOR_PWM_DIV);
  583. DUMP_REG(SOR_PWM_CTL);
  584. DUMP_REG(SOR_VCRC_A_0);
  585. DUMP_REG(SOR_VCRC_A_1);
  586. DUMP_REG(SOR_VCRC_B_0);
  587. DUMP_REG(SOR_VCRC_B_1);
  588. DUMP_REG(SOR_CCRC_A_0);
  589. DUMP_REG(SOR_CCRC_A_1);
  590. DUMP_REG(SOR_CCRC_B_0);
  591. DUMP_REG(SOR_CCRC_B_1);
  592. DUMP_REG(SOR_EDATA_A_0);
  593. DUMP_REG(SOR_EDATA_A_1);
  594. DUMP_REG(SOR_EDATA_B_0);
  595. DUMP_REG(SOR_EDATA_B_1);
  596. DUMP_REG(SOR_COUNT_A_0);
  597. DUMP_REG(SOR_COUNT_A_1);
  598. DUMP_REG(SOR_COUNT_B_0);
  599. DUMP_REG(SOR_COUNT_B_1);
  600. DUMP_REG(SOR_DEBUG_A_0);
  601. DUMP_REG(SOR_DEBUG_A_1);
  602. DUMP_REG(SOR_DEBUG_B_0);
  603. DUMP_REG(SOR_DEBUG_B_1);
  604. DUMP_REG(SOR_TRIG);
  605. DUMP_REG(SOR_MSCHECK);
  606. DUMP_REG(SOR_XBAR_CTRL);
  607. DUMP_REG(SOR_XBAR_POL);
  608. DUMP_REG(SOR_DP_LINKCTL_0);
  609. DUMP_REG(SOR_DP_LINKCTL_1);
  610. DUMP_REG(SOR_LANE_DRIVE_CURRENT_0);
  611. DUMP_REG(SOR_LANE_DRIVE_CURRENT_1);
  612. DUMP_REG(SOR_LANE4_DRIVE_CURRENT_0);
  613. DUMP_REG(SOR_LANE4_DRIVE_CURRENT_1);
  614. DUMP_REG(SOR_LANE_PREEMPHASIS_0);
  615. DUMP_REG(SOR_LANE_PREEMPHASIS_1);
  616. DUMP_REG(SOR_LANE4_PREEMPHASIS_0);
  617. DUMP_REG(SOR_LANE4_PREEMPHASIS_1);
  618. DUMP_REG(SOR_LANE_POST_CURSOR_0);
  619. DUMP_REG(SOR_LANE_POST_CURSOR_1);
  620. DUMP_REG(SOR_DP_CONFIG_0);
  621. DUMP_REG(SOR_DP_CONFIG_1);
  622. DUMP_REG(SOR_DP_MN_0);
  623. DUMP_REG(SOR_DP_MN_1);
  624. DUMP_REG(SOR_DP_PADCTL_0);
  625. DUMP_REG(SOR_DP_PADCTL_1);
  626. DUMP_REG(SOR_DP_DEBUG_0);
  627. DUMP_REG(SOR_DP_DEBUG_1);
  628. DUMP_REG(SOR_DP_SPARE_0);
  629. DUMP_REG(SOR_DP_SPARE_1);
  630. DUMP_REG(SOR_DP_AUDIO_CTRL);
  631. DUMP_REG(SOR_DP_AUDIO_HBLANK_SYMBOLS);
  632. DUMP_REG(SOR_DP_AUDIO_VBLANK_SYMBOLS);
  633. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_HEADER);
  634. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_0);
  635. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_1);
  636. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_2);
  637. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_3);
  638. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_4);
  639. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_5);
  640. DUMP_REG(SOR_DP_GENERIC_INFOFRAME_SUBPACK_6);
  641. DUMP_REG(SOR_DP_TPG);
  642. DUMP_REG(SOR_DP_TPG_CONFIG);
  643. DUMP_REG(SOR_DP_LQ_CSTM_0);
  644. DUMP_REG(SOR_DP_LQ_CSTM_1);
  645. DUMP_REG(SOR_DP_LQ_CSTM_2);
  646. #undef DUMP_REG
  647. return 0;
  648. }
  649. static const struct drm_info_list debugfs_files[] = {
  650. { "regs", tegra_sor_show_regs, 0, NULL },
  651. };
  652. static int tegra_sor_debugfs_init(struct tegra_sor *sor,
  653. struct drm_minor *minor)
  654. {
  655. struct dentry *entry;
  656. unsigned int i;
  657. int err = 0;
  658. sor->debugfs = debugfs_create_dir("sor", minor->debugfs_root);
  659. if (!sor->debugfs)
  660. return -ENOMEM;
  661. sor->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  662. GFP_KERNEL);
  663. if (!sor->debugfs_files) {
  664. err = -ENOMEM;
  665. goto remove;
  666. }
  667. for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
  668. sor->debugfs_files[i].data = sor;
  669. err = drm_debugfs_create_files(sor->debugfs_files,
  670. ARRAY_SIZE(debugfs_files),
  671. sor->debugfs, minor);
  672. if (err < 0)
  673. goto free;
  674. entry = debugfs_create_file("crc", 0644, sor->debugfs, sor,
  675. &tegra_sor_crc_fops);
  676. if (!entry) {
  677. err = -ENOMEM;
  678. goto free;
  679. }
  680. return err;
  681. free:
  682. kfree(sor->debugfs_files);
  683. sor->debugfs_files = NULL;
  684. remove:
  685. debugfs_remove_recursive(sor->debugfs);
  686. sor->debugfs = NULL;
  687. return err;
  688. }
  689. static void tegra_sor_debugfs_exit(struct tegra_sor *sor)
  690. {
  691. drm_debugfs_remove_files(sor->debugfs_files, ARRAY_SIZE(debugfs_files),
  692. sor->minor);
  693. sor->minor = NULL;
  694. kfree(sor->debugfs_files);
  695. sor->debugfs = NULL;
  696. debugfs_remove_recursive(sor->debugfs);
  697. sor->debugfs_files = NULL;
  698. }
  699. static void tegra_sor_connector_dpms(struct drm_connector *connector, int mode)
  700. {
  701. }
  702. static enum drm_connector_status
  703. tegra_sor_connector_detect(struct drm_connector *connector, bool force)
  704. {
  705. struct tegra_output *output = connector_to_output(connector);
  706. struct tegra_sor *sor = to_sor(output);
  707. if (sor->dpaux)
  708. return tegra_dpaux_detect(sor->dpaux);
  709. return connector_status_unknown;
  710. }
  711. static const struct drm_connector_funcs tegra_sor_connector_funcs = {
  712. .dpms = tegra_sor_connector_dpms,
  713. .reset = drm_atomic_helper_connector_reset,
  714. .detect = tegra_sor_connector_detect,
  715. .fill_modes = drm_helper_probe_single_connector_modes,
  716. .destroy = tegra_output_connector_destroy,
  717. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  718. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  719. };
  720. static int tegra_sor_connector_get_modes(struct drm_connector *connector)
  721. {
  722. struct tegra_output *output = connector_to_output(connector);
  723. struct tegra_sor *sor = to_sor(output);
  724. int err;
  725. if (sor->dpaux)
  726. tegra_dpaux_enable(sor->dpaux);
  727. err = tegra_output_connector_get_modes(connector);
  728. if (sor->dpaux)
  729. tegra_dpaux_disable(sor->dpaux);
  730. return err;
  731. }
  732. static enum drm_mode_status
  733. tegra_sor_connector_mode_valid(struct drm_connector *connector,
  734. struct drm_display_mode *mode)
  735. {
  736. return MODE_OK;
  737. }
  738. static const struct drm_connector_helper_funcs tegra_sor_connector_helper_funcs = {
  739. .get_modes = tegra_sor_connector_get_modes,
  740. .mode_valid = tegra_sor_connector_mode_valid,
  741. .best_encoder = tegra_output_connector_best_encoder,
  742. };
  743. static const struct drm_encoder_funcs tegra_sor_encoder_funcs = {
  744. .destroy = tegra_output_encoder_destroy,
  745. };
  746. static void tegra_sor_encoder_dpms(struct drm_encoder *encoder, int mode)
  747. {
  748. }
  749. static void tegra_sor_encoder_prepare(struct drm_encoder *encoder)
  750. {
  751. }
  752. static void tegra_sor_encoder_commit(struct drm_encoder *encoder)
  753. {
  754. }
  755. static void tegra_sor_encoder_mode_set(struct drm_encoder *encoder,
  756. struct drm_display_mode *mode,
  757. struct drm_display_mode *adjusted)
  758. {
  759. struct tegra_output *output = encoder_to_output(encoder);
  760. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  761. unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
  762. struct tegra_sor *sor = to_sor(output);
  763. struct tegra_sor_config config;
  764. struct drm_dp_link link;
  765. struct drm_dp_aux *aux;
  766. int err = 0;
  767. u32 value;
  768. mutex_lock(&sor->lock);
  769. if (sor->enabled)
  770. goto unlock;
  771. err = clk_prepare_enable(sor->clk);
  772. if (err < 0)
  773. goto unlock;
  774. reset_control_deassert(sor->rst);
  775. if (output->panel)
  776. drm_panel_prepare(output->panel);
  777. /* FIXME: properly convert to struct drm_dp_aux */
  778. aux = (struct drm_dp_aux *)sor->dpaux;
  779. if (sor->dpaux) {
  780. err = tegra_dpaux_enable(sor->dpaux);
  781. if (err < 0)
  782. dev_err(sor->dev, "failed to enable DP: %d\n", err);
  783. err = drm_dp_link_probe(aux, &link);
  784. if (err < 0) {
  785. dev_err(sor->dev, "failed to probe eDP link: %d\n",
  786. err);
  787. goto unlock;
  788. }
  789. }
  790. err = clk_set_parent(sor->clk, sor->clk_safe);
  791. if (err < 0)
  792. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  793. memset(&config, 0, sizeof(config));
  794. config.bits_per_pixel = output->connector.display_info.bpc * 3;
  795. err = tegra_sor_calc_config(sor, mode, &config, &link);
  796. if (err < 0)
  797. dev_err(sor->dev, "failed to compute link configuration: %d\n",
  798. err);
  799. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  800. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  801. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
  802. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  803. value = tegra_sor_readl(sor, SOR_PLL_2);
  804. value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
  805. tegra_sor_writel(sor, value, SOR_PLL_2);
  806. usleep_range(20, 100);
  807. value = tegra_sor_readl(sor, SOR_PLL_3);
  808. value |= SOR_PLL_3_PLL_VDD_MODE_V3_3;
  809. tegra_sor_writel(sor, value, SOR_PLL_3);
  810. value = SOR_PLL_0_ICHPMP(0xf) | SOR_PLL_0_VCOCAP_RST |
  811. SOR_PLL_0_PLLREG_LEVEL_V45 | SOR_PLL_0_RESISTOR_EXT;
  812. tegra_sor_writel(sor, value, SOR_PLL_0);
  813. value = tegra_sor_readl(sor, SOR_PLL_2);
  814. value |= SOR_PLL_2_SEQ_PLLCAPPD;
  815. value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
  816. value |= SOR_PLL_2_LVDS_ENABLE;
  817. tegra_sor_writel(sor, value, SOR_PLL_2);
  818. value = SOR_PLL_1_TERM_COMPOUT | SOR_PLL_1_TMDS_TERM;
  819. tegra_sor_writel(sor, value, SOR_PLL_1);
  820. while (true) {
  821. value = tegra_sor_readl(sor, SOR_PLL_2);
  822. if ((value & SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE) == 0)
  823. break;
  824. usleep_range(250, 1000);
  825. }
  826. value = tegra_sor_readl(sor, SOR_PLL_2);
  827. value &= ~SOR_PLL_2_POWERDOWN_OVERRIDE;
  828. value &= ~SOR_PLL_2_PORT_POWERDOWN;
  829. tegra_sor_writel(sor, value, SOR_PLL_2);
  830. /*
  831. * power up
  832. */
  833. /* set safe link bandwidth (1.62 Gbps) */
  834. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  835. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  836. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
  837. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  838. /* step 1 */
  839. value = tegra_sor_readl(sor, SOR_PLL_2);
  840. value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL_2_PORT_POWERDOWN |
  841. SOR_PLL_2_BANDGAP_POWERDOWN;
  842. tegra_sor_writel(sor, value, SOR_PLL_2);
  843. value = tegra_sor_readl(sor, SOR_PLL_0);
  844. value |= SOR_PLL_0_VCOPD | SOR_PLL_0_POWER_OFF;
  845. tegra_sor_writel(sor, value, SOR_PLL_0);
  846. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  847. value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
  848. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  849. /* step 2 */
  850. err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
  851. if (err < 0) {
  852. dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
  853. goto unlock;
  854. }
  855. usleep_range(5, 100);
  856. /* step 3 */
  857. value = tegra_sor_readl(sor, SOR_PLL_2);
  858. value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
  859. tegra_sor_writel(sor, value, SOR_PLL_2);
  860. usleep_range(20, 100);
  861. /* step 4 */
  862. value = tegra_sor_readl(sor, SOR_PLL_0);
  863. value &= ~SOR_PLL_0_POWER_OFF;
  864. value &= ~SOR_PLL_0_VCOPD;
  865. tegra_sor_writel(sor, value, SOR_PLL_0);
  866. value = tegra_sor_readl(sor, SOR_PLL_2);
  867. value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
  868. tegra_sor_writel(sor, value, SOR_PLL_2);
  869. usleep_range(200, 1000);
  870. /* step 5 */
  871. value = tegra_sor_readl(sor, SOR_PLL_2);
  872. value &= ~SOR_PLL_2_PORT_POWERDOWN;
  873. tegra_sor_writel(sor, value, SOR_PLL_2);
  874. /* switch to DP clock */
  875. err = clk_set_parent(sor->clk, sor->clk_dp);
  876. if (err < 0)
  877. dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
  878. /* power DP lanes */
  879. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  880. if (link.num_lanes <= 2)
  881. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
  882. else
  883. value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
  884. if (link.num_lanes <= 1)
  885. value &= ~SOR_DP_PADCTL_PD_TXD_1;
  886. else
  887. value |= SOR_DP_PADCTL_PD_TXD_1;
  888. if (link.num_lanes == 0)
  889. value &= ~SOR_DP_PADCTL_PD_TXD_0;
  890. else
  891. value |= SOR_DP_PADCTL_PD_TXD_0;
  892. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  893. value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
  894. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  895. value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
  896. tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
  897. /* start lane sequencer */
  898. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
  899. SOR_LANE_SEQ_CTL_POWER_STATE_UP;
  900. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  901. while (true) {
  902. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  903. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  904. break;
  905. usleep_range(250, 1000);
  906. }
  907. /* set link bandwidth */
  908. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  909. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  910. value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
  911. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  912. /* set linkctl */
  913. value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
  914. value |= SOR_DP_LINKCTL_ENABLE;
  915. value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
  916. value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
  917. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  918. tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
  919. for (i = 0, value = 0; i < 4; i++) {
  920. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  921. SOR_DP_TPG_SCRAMBLER_GALIOS |
  922. SOR_DP_TPG_PATTERN_NONE;
  923. value = (value << 8) | lane;
  924. }
  925. tegra_sor_writel(sor, value, SOR_DP_TPG);
  926. value = tegra_sor_readl(sor, SOR_DP_CONFIG_0);
  927. value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
  928. value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
  929. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
  930. value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
  931. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
  932. value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
  933. if (config.active_polarity)
  934. value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  935. else
  936. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  937. value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
  938. value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
  939. tegra_sor_writel(sor, value, SOR_DP_CONFIG_0);
  940. value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  941. value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
  942. value |= config.hblank_symbols & 0xffff;
  943. tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  944. value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  945. value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
  946. value |= config.vblank_symbols & 0xffff;
  947. tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  948. /* enable pad calibration logic */
  949. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  950. value |= SOR_DP_PADCTL_PAD_CAL_PD;
  951. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  952. if (sor->dpaux) {
  953. u8 rate, lanes;
  954. err = drm_dp_link_probe(aux, &link);
  955. if (err < 0) {
  956. dev_err(sor->dev, "failed to probe eDP link: %d\n",
  957. err);
  958. goto unlock;
  959. }
  960. err = drm_dp_link_power_up(aux, &link);
  961. if (err < 0) {
  962. dev_err(sor->dev, "failed to power up eDP link: %d\n",
  963. err);
  964. goto unlock;
  965. }
  966. err = drm_dp_link_configure(aux, &link);
  967. if (err < 0) {
  968. dev_err(sor->dev, "failed to configure eDP link: %d\n",
  969. err);
  970. goto unlock;
  971. }
  972. rate = drm_dp_link_rate_to_bw_code(link.rate);
  973. lanes = link.num_lanes;
  974. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  975. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  976. value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
  977. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  978. value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
  979. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  980. value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
  981. if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  982. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  983. tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
  984. /* disable training pattern generator */
  985. for (i = 0; i < link.num_lanes; i++) {
  986. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  987. SOR_DP_TPG_SCRAMBLER_GALIOS |
  988. SOR_DP_TPG_PATTERN_NONE;
  989. value = (value << 8) | lane;
  990. }
  991. tegra_sor_writel(sor, value, SOR_DP_TPG);
  992. err = tegra_sor_dp_train_fast(sor, &link);
  993. if (err < 0) {
  994. dev_err(sor->dev, "DP fast link training failed: %d\n",
  995. err);
  996. goto unlock;
  997. }
  998. dev_dbg(sor->dev, "fast link training succeeded\n");
  999. }
  1000. err = tegra_sor_power_up(sor, 250);
  1001. if (err < 0) {
  1002. dev_err(sor->dev, "failed to power up SOR: %d\n", err);
  1003. goto unlock;
  1004. }
  1005. /*
  1006. * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
  1007. * raster, associate with display controller)
  1008. */
  1009. value = SOR_STATE_ASY_PROTOCOL_DP_A |
  1010. SOR_STATE_ASY_CRC_MODE_COMPLETE |
  1011. SOR_STATE_ASY_OWNER(dc->pipe + 1);
  1012. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  1013. value &= ~SOR_STATE_ASY_HSYNCPOL;
  1014. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1015. value |= SOR_STATE_ASY_HSYNCPOL;
  1016. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  1017. value &= ~SOR_STATE_ASY_VSYNCPOL;
  1018. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1019. value |= SOR_STATE_ASY_VSYNCPOL;
  1020. switch (config.bits_per_pixel) {
  1021. case 24:
  1022. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
  1023. break;
  1024. case 18:
  1025. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
  1026. break;
  1027. default:
  1028. BUG();
  1029. break;
  1030. }
  1031. tegra_sor_writel(sor, value, SOR_STATE_1);
  1032. /*
  1033. * TODO: The video timing programming below doesn't seem to match the
  1034. * register definitions.
  1035. */
  1036. value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
  1037. tegra_sor_writel(sor, value, SOR_HEAD_STATE_1(0));
  1038. vse = mode->vsync_end - mode->vsync_start - 1;
  1039. hse = mode->hsync_end - mode->hsync_start - 1;
  1040. value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
  1041. tegra_sor_writel(sor, value, SOR_HEAD_STATE_2(0));
  1042. vbe = vse + (mode->vsync_start - mode->vdisplay);
  1043. hbe = hse + (mode->hsync_start - mode->hdisplay);
  1044. value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
  1045. tegra_sor_writel(sor, value, SOR_HEAD_STATE_3(0));
  1046. vbs = vbe + mode->vdisplay;
  1047. hbs = hbe + mode->hdisplay;
  1048. value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
  1049. tegra_sor_writel(sor, value, SOR_HEAD_STATE_4(0));
  1050. /* CSTM (LVDS, link A/B, upper) */
  1051. value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
  1052. SOR_CSTM_UPPER;
  1053. tegra_sor_writel(sor, value, SOR_CSTM);
  1054. /* PWM setup */
  1055. err = tegra_sor_setup_pwm(sor, 250);
  1056. if (err < 0) {
  1057. dev_err(sor->dev, "failed to setup PWM: %d\n", err);
  1058. goto unlock;
  1059. }
  1060. tegra_sor_update(sor);
  1061. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  1062. value |= SOR_ENABLE;
  1063. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  1064. tegra_dc_commit(dc);
  1065. err = tegra_sor_attach(sor);
  1066. if (err < 0) {
  1067. dev_err(sor->dev, "failed to attach SOR: %d\n", err);
  1068. goto unlock;
  1069. }
  1070. err = tegra_sor_wakeup(sor);
  1071. if (err < 0) {
  1072. dev_err(sor->dev, "failed to enable DC: %d\n", err);
  1073. goto unlock;
  1074. }
  1075. if (output->panel)
  1076. drm_panel_enable(output->panel);
  1077. sor->enabled = true;
  1078. unlock:
  1079. mutex_unlock(&sor->lock);
  1080. }
  1081. static void tegra_sor_encoder_disable(struct drm_encoder *encoder)
  1082. {
  1083. struct tegra_output *output = encoder_to_output(encoder);
  1084. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  1085. struct tegra_sor *sor = to_sor(output);
  1086. u32 value;
  1087. int err;
  1088. mutex_lock(&sor->lock);
  1089. if (!sor->enabled)
  1090. goto unlock;
  1091. if (output->panel)
  1092. drm_panel_disable(output->panel);
  1093. err = tegra_sor_detach(sor);
  1094. if (err < 0) {
  1095. dev_err(sor->dev, "failed to detach SOR: %d\n", err);
  1096. goto unlock;
  1097. }
  1098. tegra_sor_writel(sor, 0, SOR_STATE_1);
  1099. tegra_sor_update(sor);
  1100. /*
  1101. * The following accesses registers of the display controller, so make
  1102. * sure it's only executed when the output is attached to one.
  1103. */
  1104. if (dc) {
  1105. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  1106. value &= ~SOR_ENABLE;
  1107. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  1108. tegra_dc_commit(dc);
  1109. }
  1110. err = tegra_sor_power_down(sor);
  1111. if (err < 0) {
  1112. dev_err(sor->dev, "failed to power down SOR: %d\n", err);
  1113. goto unlock;
  1114. }
  1115. if (sor->dpaux) {
  1116. err = tegra_dpaux_disable(sor->dpaux);
  1117. if (err < 0) {
  1118. dev_err(sor->dev, "failed to disable DP: %d\n", err);
  1119. goto unlock;
  1120. }
  1121. }
  1122. err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
  1123. if (err < 0) {
  1124. dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
  1125. goto unlock;
  1126. }
  1127. if (output->panel)
  1128. drm_panel_unprepare(output->panel);
  1129. clk_disable_unprepare(sor->clk);
  1130. reset_control_assert(sor->rst);
  1131. sor->enabled = false;
  1132. unlock:
  1133. mutex_unlock(&sor->lock);
  1134. }
  1135. static int
  1136. tegra_sor_encoder_atomic_check(struct drm_encoder *encoder,
  1137. struct drm_crtc_state *crtc_state,
  1138. struct drm_connector_state *conn_state)
  1139. {
  1140. struct tegra_output *output = encoder_to_output(encoder);
  1141. struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
  1142. unsigned long pclk = crtc_state->mode.clock * 1000;
  1143. struct tegra_sor *sor = to_sor(output);
  1144. int err;
  1145. err = tegra_dc_state_setup_clock(dc, crtc_state, sor->clk_parent,
  1146. pclk, 0);
  1147. if (err < 0) {
  1148. dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
  1149. return err;
  1150. }
  1151. return 0;
  1152. }
  1153. static const struct drm_encoder_helper_funcs tegra_sor_encoder_helper_funcs = {
  1154. .dpms = tegra_sor_encoder_dpms,
  1155. .prepare = tegra_sor_encoder_prepare,
  1156. .commit = tegra_sor_encoder_commit,
  1157. .mode_set = tegra_sor_encoder_mode_set,
  1158. .disable = tegra_sor_encoder_disable,
  1159. .atomic_check = tegra_sor_encoder_atomic_check,
  1160. };
  1161. static int tegra_sor_init(struct host1x_client *client)
  1162. {
  1163. struct drm_device *drm = dev_get_drvdata(client->parent);
  1164. struct tegra_sor *sor = host1x_client_to_sor(client);
  1165. int err;
  1166. if (!sor->dpaux)
  1167. return -ENODEV;
  1168. sor->output.dev = sor->dev;
  1169. drm_connector_init(drm, &sor->output.connector,
  1170. &tegra_sor_connector_funcs,
  1171. DRM_MODE_CONNECTOR_eDP);
  1172. drm_connector_helper_add(&sor->output.connector,
  1173. &tegra_sor_connector_helper_funcs);
  1174. sor->output.connector.dpms = DRM_MODE_DPMS_OFF;
  1175. drm_encoder_init(drm, &sor->output.encoder, &tegra_sor_encoder_funcs,
  1176. DRM_MODE_ENCODER_TMDS);
  1177. drm_encoder_helper_add(&sor->output.encoder,
  1178. &tegra_sor_encoder_helper_funcs);
  1179. drm_mode_connector_attach_encoder(&sor->output.connector,
  1180. &sor->output.encoder);
  1181. drm_connector_register(&sor->output.connector);
  1182. err = tegra_output_init(drm, &sor->output);
  1183. if (err < 0) {
  1184. dev_err(client->dev, "failed to initialize output: %d\n", err);
  1185. return err;
  1186. }
  1187. sor->output.encoder.possible_crtcs = 0x3;
  1188. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1189. err = tegra_sor_debugfs_init(sor, drm->primary);
  1190. if (err < 0)
  1191. dev_err(sor->dev, "debugfs setup failed: %d\n", err);
  1192. }
  1193. if (sor->dpaux) {
  1194. err = tegra_dpaux_attach(sor->dpaux, &sor->output);
  1195. if (err < 0) {
  1196. dev_err(sor->dev, "failed to attach DP: %d\n", err);
  1197. return err;
  1198. }
  1199. }
  1200. /*
  1201. * XXX: Remove this reset once proper hand-over from firmware to
  1202. * kernel is possible.
  1203. */
  1204. err = reset_control_assert(sor->rst);
  1205. if (err < 0) {
  1206. dev_err(sor->dev, "failed to assert SOR reset: %d\n", err);
  1207. return err;
  1208. }
  1209. err = clk_prepare_enable(sor->clk);
  1210. if (err < 0) {
  1211. dev_err(sor->dev, "failed to enable clock: %d\n", err);
  1212. return err;
  1213. }
  1214. usleep_range(1000, 3000);
  1215. err = reset_control_deassert(sor->rst);
  1216. if (err < 0) {
  1217. dev_err(sor->dev, "failed to deassert SOR reset: %d\n", err);
  1218. return err;
  1219. }
  1220. err = clk_prepare_enable(sor->clk_safe);
  1221. if (err < 0)
  1222. return err;
  1223. err = clk_prepare_enable(sor->clk_dp);
  1224. if (err < 0)
  1225. return err;
  1226. return 0;
  1227. }
  1228. static int tegra_sor_exit(struct host1x_client *client)
  1229. {
  1230. struct tegra_sor *sor = host1x_client_to_sor(client);
  1231. int err;
  1232. tegra_output_exit(&sor->output);
  1233. if (sor->dpaux) {
  1234. err = tegra_dpaux_detach(sor->dpaux);
  1235. if (err < 0) {
  1236. dev_err(sor->dev, "failed to detach DP: %d\n", err);
  1237. return err;
  1238. }
  1239. }
  1240. clk_disable_unprepare(sor->clk_safe);
  1241. clk_disable_unprepare(sor->clk_dp);
  1242. clk_disable_unprepare(sor->clk);
  1243. if (IS_ENABLED(CONFIG_DEBUG_FS))
  1244. tegra_sor_debugfs_exit(sor);
  1245. return 0;
  1246. }
  1247. static const struct host1x_client_ops sor_client_ops = {
  1248. .init = tegra_sor_init,
  1249. .exit = tegra_sor_exit,
  1250. };
  1251. static int tegra_sor_probe(struct platform_device *pdev)
  1252. {
  1253. struct device_node *np;
  1254. struct tegra_sor *sor;
  1255. struct resource *regs;
  1256. int err;
  1257. sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
  1258. if (!sor)
  1259. return -ENOMEM;
  1260. sor->output.dev = sor->dev = &pdev->dev;
  1261. np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
  1262. if (np) {
  1263. sor->dpaux = tegra_dpaux_find_by_of_node(np);
  1264. of_node_put(np);
  1265. if (!sor->dpaux)
  1266. return -EPROBE_DEFER;
  1267. }
  1268. err = tegra_output_probe(&sor->output);
  1269. if (err < 0)
  1270. return err;
  1271. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1272. sor->regs = devm_ioremap_resource(&pdev->dev, regs);
  1273. if (IS_ERR(sor->regs))
  1274. return PTR_ERR(sor->regs);
  1275. sor->rst = devm_reset_control_get(&pdev->dev, "sor");
  1276. if (IS_ERR(sor->rst))
  1277. return PTR_ERR(sor->rst);
  1278. sor->clk = devm_clk_get(&pdev->dev, NULL);
  1279. if (IS_ERR(sor->clk))
  1280. return PTR_ERR(sor->clk);
  1281. sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
  1282. if (IS_ERR(sor->clk_parent))
  1283. return PTR_ERR(sor->clk_parent);
  1284. sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
  1285. if (IS_ERR(sor->clk_safe))
  1286. return PTR_ERR(sor->clk_safe);
  1287. sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
  1288. if (IS_ERR(sor->clk_dp))
  1289. return PTR_ERR(sor->clk_dp);
  1290. INIT_LIST_HEAD(&sor->client.list);
  1291. sor->client.ops = &sor_client_ops;
  1292. sor->client.dev = &pdev->dev;
  1293. mutex_init(&sor->lock);
  1294. err = host1x_client_register(&sor->client);
  1295. if (err < 0) {
  1296. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  1297. err);
  1298. return err;
  1299. }
  1300. platform_set_drvdata(pdev, sor);
  1301. return 0;
  1302. }
  1303. static int tegra_sor_remove(struct platform_device *pdev)
  1304. {
  1305. struct tegra_sor *sor = platform_get_drvdata(pdev);
  1306. int err;
  1307. err = host1x_client_unregister(&sor->client);
  1308. if (err < 0) {
  1309. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  1310. err);
  1311. return err;
  1312. }
  1313. tegra_output_remove(&sor->output);
  1314. return 0;
  1315. }
  1316. static const struct of_device_id tegra_sor_of_match[] = {
  1317. { .compatible = "nvidia,tegra124-sor", },
  1318. { },
  1319. };
  1320. MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
  1321. struct platform_driver tegra_sor_driver = {
  1322. .driver = {
  1323. .name = "tegra-sor",
  1324. .of_match_table = tegra_sor_of_match,
  1325. },
  1326. .probe = tegra_sor_probe,
  1327. .remove = tegra_sor_remove,
  1328. };