sor.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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/io.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/reset.h>
  13. #include <linux/tegra-powergate.h>
  14. #include <drm/drm_dp_helper.h>
  15. #include "dc.h"
  16. #include "drm.h"
  17. #include "sor.h"
  18. struct tegra_sor {
  19. struct host1x_client client;
  20. struct tegra_output output;
  21. struct device *dev;
  22. void __iomem *regs;
  23. struct reset_control *rst;
  24. struct clk *clk_parent;
  25. struct clk *clk_safe;
  26. struct clk *clk_dp;
  27. struct clk *clk;
  28. struct tegra_dpaux *dpaux;
  29. struct mutex lock;
  30. bool enabled;
  31. struct dentry *debugfs;
  32. };
  33. struct tegra_sor_config {
  34. u32 bits_per_pixel;
  35. u32 active_polarity;
  36. u32 active_count;
  37. u32 tu_size;
  38. u32 active_frac;
  39. u32 watermark;
  40. u32 hblank_symbols;
  41. u32 vblank_symbols;
  42. };
  43. static inline struct tegra_sor *
  44. host1x_client_to_sor(struct host1x_client *client)
  45. {
  46. return container_of(client, struct tegra_sor, client);
  47. }
  48. static inline struct tegra_sor *to_sor(struct tegra_output *output)
  49. {
  50. return container_of(output, struct tegra_sor, output);
  51. }
  52. static inline unsigned long tegra_sor_readl(struct tegra_sor *sor,
  53. unsigned long offset)
  54. {
  55. return readl(sor->regs + (offset << 2));
  56. }
  57. static inline void tegra_sor_writel(struct tegra_sor *sor, unsigned long value,
  58. unsigned long offset)
  59. {
  60. writel(value, sor->regs + (offset << 2));
  61. }
  62. static int tegra_sor_dp_train_fast(struct tegra_sor *sor,
  63. struct drm_dp_link *link)
  64. {
  65. unsigned long value;
  66. unsigned int i;
  67. u8 pattern;
  68. int err;
  69. /* setup lane parameters */
  70. value = SOR_LANE_DRIVE_CURRENT_LANE3(0x40) |
  71. SOR_LANE_DRIVE_CURRENT_LANE2(0x40) |
  72. SOR_LANE_DRIVE_CURRENT_LANE1(0x40) |
  73. SOR_LANE_DRIVE_CURRENT_LANE0(0x40);
  74. tegra_sor_writel(sor, value, SOR_LANE_DRIVE_CURRENT_0);
  75. value = SOR_LANE_PREEMPHASIS_LANE3(0x0f) |
  76. SOR_LANE_PREEMPHASIS_LANE2(0x0f) |
  77. SOR_LANE_PREEMPHASIS_LANE1(0x0f) |
  78. SOR_LANE_PREEMPHASIS_LANE0(0x0f);
  79. tegra_sor_writel(sor, value, SOR_LANE_PREEMPHASIS_0);
  80. value = SOR_LANE_POST_CURSOR_LANE3(0x00) |
  81. SOR_LANE_POST_CURSOR_LANE2(0x00) |
  82. SOR_LANE_POST_CURSOR_LANE1(0x00) |
  83. SOR_LANE_POST_CURSOR_LANE0(0x00);
  84. tegra_sor_writel(sor, value, SOR_LANE_POST_CURSOR_0);
  85. /* disable LVDS mode */
  86. tegra_sor_writel(sor, 0, SOR_LVDS);
  87. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  88. value |= SOR_DP_PADCTL_TX_PU_ENABLE;
  89. value &= ~SOR_DP_PADCTL_TX_PU_MASK;
  90. value |= SOR_DP_PADCTL_TX_PU(2); /* XXX: don't hardcode? */
  91. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  92. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  93. value |= SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
  94. SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0;
  95. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  96. usleep_range(10, 100);
  97. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  98. value &= ~(SOR_DP_PADCTL_CM_TXD_3 | SOR_DP_PADCTL_CM_TXD_2 |
  99. SOR_DP_PADCTL_CM_TXD_1 | SOR_DP_PADCTL_CM_TXD_0);
  100. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  101. err = tegra_dpaux_prepare(sor->dpaux, DP_SET_ANSI_8B10B);
  102. if (err < 0)
  103. return err;
  104. for (i = 0, value = 0; i < link->num_lanes; i++) {
  105. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  106. SOR_DP_TPG_SCRAMBLER_NONE |
  107. SOR_DP_TPG_PATTERN_TRAIN1;
  108. value = (value << 8) | lane;
  109. }
  110. tegra_sor_writel(sor, value, SOR_DP_TPG);
  111. pattern = DP_TRAINING_PATTERN_1;
  112. err = tegra_dpaux_train(sor->dpaux, link, pattern);
  113. if (err < 0)
  114. return err;
  115. value = tegra_sor_readl(sor, SOR_DP_SPARE_0);
  116. value |= SOR_DP_SPARE_SEQ_ENABLE;
  117. value &= ~SOR_DP_SPARE_PANEL_INTERNAL;
  118. value |= SOR_DP_SPARE_MACRO_SOR_CLK;
  119. tegra_sor_writel(sor, value, SOR_DP_SPARE_0);
  120. for (i = 0, value = 0; i < link->num_lanes; i++) {
  121. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  122. SOR_DP_TPG_SCRAMBLER_NONE |
  123. SOR_DP_TPG_PATTERN_TRAIN2;
  124. value = (value << 8) | lane;
  125. }
  126. tegra_sor_writel(sor, value, SOR_DP_TPG);
  127. pattern = DP_LINK_SCRAMBLING_DISABLE | DP_TRAINING_PATTERN_2;
  128. err = tegra_dpaux_train(sor->dpaux, link, pattern);
  129. if (err < 0)
  130. return err;
  131. for (i = 0, value = 0; i < link->num_lanes; i++) {
  132. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  133. SOR_DP_TPG_SCRAMBLER_GALIOS |
  134. SOR_DP_TPG_PATTERN_NONE;
  135. value = (value << 8) | lane;
  136. }
  137. tegra_sor_writel(sor, value, SOR_DP_TPG);
  138. pattern = DP_TRAINING_PATTERN_DISABLE;
  139. err = tegra_dpaux_train(sor->dpaux, link, pattern);
  140. if (err < 0)
  141. return err;
  142. return 0;
  143. }
  144. static void tegra_sor_super_update(struct tegra_sor *sor)
  145. {
  146. tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
  147. tegra_sor_writel(sor, 1, SOR_SUPER_STATE_0);
  148. tegra_sor_writel(sor, 0, SOR_SUPER_STATE_0);
  149. }
  150. static void tegra_sor_update(struct tegra_sor *sor)
  151. {
  152. tegra_sor_writel(sor, 0, SOR_STATE_0);
  153. tegra_sor_writel(sor, 1, SOR_STATE_0);
  154. tegra_sor_writel(sor, 0, SOR_STATE_0);
  155. }
  156. static int tegra_sor_setup_pwm(struct tegra_sor *sor, unsigned long timeout)
  157. {
  158. unsigned long value;
  159. value = tegra_sor_readl(sor, SOR_PWM_DIV);
  160. value &= ~SOR_PWM_DIV_MASK;
  161. value |= 0x400; /* period */
  162. tegra_sor_writel(sor, value, SOR_PWM_DIV);
  163. value = tegra_sor_readl(sor, SOR_PWM_CTL);
  164. value &= ~SOR_PWM_CTL_DUTY_CYCLE_MASK;
  165. value |= 0x400; /* duty cycle */
  166. value &= ~SOR_PWM_CTL_CLK_SEL; /* clock source: PCLK */
  167. value |= SOR_PWM_CTL_TRIGGER;
  168. tegra_sor_writel(sor, value, SOR_PWM_CTL);
  169. timeout = jiffies + msecs_to_jiffies(timeout);
  170. while (time_before(jiffies, timeout)) {
  171. value = tegra_sor_readl(sor, SOR_PWM_CTL);
  172. if ((value & SOR_PWM_CTL_TRIGGER) == 0)
  173. return 0;
  174. usleep_range(25, 100);
  175. }
  176. return -ETIMEDOUT;
  177. }
  178. static int tegra_sor_attach(struct tegra_sor *sor)
  179. {
  180. unsigned long value, timeout;
  181. /* wake up in normal mode */
  182. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  183. value |= SOR_SUPER_STATE_HEAD_MODE_AWAKE;
  184. value |= SOR_SUPER_STATE_MODE_NORMAL;
  185. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  186. tegra_sor_super_update(sor);
  187. /* attach */
  188. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  189. value |= SOR_SUPER_STATE_ATTACHED;
  190. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  191. tegra_sor_super_update(sor);
  192. timeout = jiffies + msecs_to_jiffies(250);
  193. while (time_before(jiffies, timeout)) {
  194. value = tegra_sor_readl(sor, SOR_TEST);
  195. if ((value & SOR_TEST_ATTACHED) != 0)
  196. return 0;
  197. usleep_range(25, 100);
  198. }
  199. return -ETIMEDOUT;
  200. }
  201. static int tegra_sor_wakeup(struct tegra_sor *sor)
  202. {
  203. struct tegra_dc *dc = to_tegra_dc(sor->output.encoder.crtc);
  204. unsigned long value, timeout;
  205. /* enable display controller outputs */
  206. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  207. value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  208. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
  209. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  210. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  211. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  212. timeout = jiffies + msecs_to_jiffies(250);
  213. /* wait for head to wake up */
  214. while (time_before(jiffies, timeout)) {
  215. value = tegra_sor_readl(sor, SOR_TEST);
  216. value &= SOR_TEST_HEAD_MODE_MASK;
  217. if (value == SOR_TEST_HEAD_MODE_AWAKE)
  218. return 0;
  219. usleep_range(25, 100);
  220. }
  221. return -ETIMEDOUT;
  222. }
  223. static int tegra_sor_power_up(struct tegra_sor *sor, unsigned long timeout)
  224. {
  225. unsigned long value;
  226. value = tegra_sor_readl(sor, SOR_PWR);
  227. value |= SOR_PWR_TRIGGER | SOR_PWR_NORMAL_STATE_PU;
  228. tegra_sor_writel(sor, value, SOR_PWR);
  229. timeout = jiffies + msecs_to_jiffies(timeout);
  230. while (time_before(jiffies, timeout)) {
  231. value = tegra_sor_readl(sor, SOR_PWR);
  232. if ((value & SOR_PWR_TRIGGER) == 0)
  233. return 0;
  234. usleep_range(25, 100);
  235. }
  236. return -ETIMEDOUT;
  237. }
  238. struct tegra_sor_params {
  239. /* number of link clocks per line */
  240. unsigned int num_clocks;
  241. /* ratio between input and output */
  242. u64 ratio;
  243. /* precision factor */
  244. u64 precision;
  245. unsigned int active_polarity;
  246. unsigned int active_count;
  247. unsigned int active_frac;
  248. unsigned int tu_size;
  249. unsigned int error;
  250. };
  251. static int tegra_sor_compute_params(struct tegra_sor *sor,
  252. struct tegra_sor_params *params,
  253. unsigned int tu_size)
  254. {
  255. u64 active_sym, active_count, frac, approx;
  256. u32 active_polarity, active_frac = 0;
  257. const u64 f = params->precision;
  258. s64 error;
  259. active_sym = params->ratio * tu_size;
  260. active_count = div_u64(active_sym, f) * f;
  261. frac = active_sym - active_count;
  262. /* fraction < 0.5 */
  263. if (frac >= (f / 2)) {
  264. active_polarity = 1;
  265. frac = f - frac;
  266. } else {
  267. active_polarity = 0;
  268. }
  269. if (frac != 0) {
  270. frac = div_u64(f * f, frac); /* 1/fraction */
  271. if (frac <= (15 * f)) {
  272. active_frac = div_u64(frac, f);
  273. /* round up */
  274. if (active_polarity)
  275. active_frac++;
  276. } else {
  277. active_frac = active_polarity ? 1 : 15;
  278. }
  279. }
  280. if (active_frac == 1)
  281. active_polarity = 0;
  282. if (active_polarity == 1) {
  283. if (active_frac) {
  284. approx = active_count + (active_frac * (f - 1)) * f;
  285. approx = div_u64(approx, active_frac * f);
  286. } else {
  287. approx = active_count + f;
  288. }
  289. } else {
  290. if (active_frac)
  291. approx = active_count + div_u64(f, active_frac);
  292. else
  293. approx = active_count;
  294. }
  295. error = div_s64(active_sym - approx, tu_size);
  296. error *= params->num_clocks;
  297. if (error <= 0 && abs64(error) < params->error) {
  298. params->active_count = div_u64(active_count, f);
  299. params->active_polarity = active_polarity;
  300. params->active_frac = active_frac;
  301. params->error = abs64(error);
  302. params->tu_size = tu_size;
  303. if (error == 0)
  304. return true;
  305. }
  306. return false;
  307. }
  308. static int tegra_sor_calc_config(struct tegra_sor *sor,
  309. struct drm_display_mode *mode,
  310. struct tegra_sor_config *config,
  311. struct drm_dp_link *link)
  312. {
  313. const u64 f = 100000, link_rate = link->rate * 1000;
  314. const u64 pclk = mode->clock * 1000;
  315. u64 input, output, watermark, num;
  316. struct tegra_sor_params params;
  317. u32 num_syms_per_line;
  318. unsigned int i;
  319. if (!link_rate || !link->num_lanes || !pclk || !config->bits_per_pixel)
  320. return -EINVAL;
  321. output = link_rate * 8 * link->num_lanes;
  322. input = pclk * config->bits_per_pixel;
  323. if (input >= output)
  324. return -ERANGE;
  325. memset(&params, 0, sizeof(params));
  326. params.ratio = div64_u64(input * f, output);
  327. params.num_clocks = div_u64(link_rate * mode->hdisplay, pclk);
  328. params.precision = f;
  329. params.error = 64 * f;
  330. params.tu_size = 64;
  331. for (i = params.tu_size; i >= 32; i--)
  332. if (tegra_sor_compute_params(sor, &params, i))
  333. break;
  334. if (params.active_frac == 0) {
  335. config->active_polarity = 0;
  336. config->active_count = params.active_count;
  337. if (!params.active_polarity)
  338. config->active_count--;
  339. config->tu_size = params.tu_size;
  340. config->active_frac = 1;
  341. } else {
  342. config->active_polarity = params.active_polarity;
  343. config->active_count = params.active_count;
  344. config->active_frac = params.active_frac;
  345. config->tu_size = params.tu_size;
  346. }
  347. dev_dbg(sor->dev,
  348. "polarity: %d active count: %d tu size: %d active frac: %d\n",
  349. config->active_polarity, config->active_count,
  350. config->tu_size, config->active_frac);
  351. watermark = params.ratio * config->tu_size * (f - params.ratio);
  352. watermark = div_u64(watermark, f);
  353. watermark = div_u64(watermark + params.error, f);
  354. config->watermark = watermark + (config->bits_per_pixel / 8) + 2;
  355. num_syms_per_line = (mode->hdisplay * config->bits_per_pixel) *
  356. (link->num_lanes * 8);
  357. if (config->watermark > 30) {
  358. config->watermark = 30;
  359. dev_err(sor->dev,
  360. "unable to compute TU size, forcing watermark to %u\n",
  361. config->watermark);
  362. } else if (config->watermark > num_syms_per_line) {
  363. config->watermark = num_syms_per_line;
  364. dev_err(sor->dev, "watermark too high, forcing to %u\n",
  365. config->watermark);
  366. }
  367. /* compute the number of symbols per horizontal blanking interval */
  368. num = ((mode->htotal - mode->hdisplay) - 7) * link_rate;
  369. config->hblank_symbols = div_u64(num, pclk);
  370. if (link->capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  371. config->hblank_symbols -= 3;
  372. config->hblank_symbols -= 12 / link->num_lanes;
  373. /* compute the number of symbols per vertical blanking interval */
  374. num = (mode->hdisplay - 25) * link_rate;
  375. config->vblank_symbols = div_u64(num, pclk);
  376. config->vblank_symbols -= 36 / link->num_lanes + 4;
  377. dev_dbg(sor->dev, "blank symbols: H:%u V:%u\n", config->hblank_symbols,
  378. config->vblank_symbols);
  379. return 0;
  380. }
  381. static int tegra_output_sor_enable(struct tegra_output *output)
  382. {
  383. struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
  384. struct drm_display_mode *mode = &dc->base.mode;
  385. unsigned int vbe, vse, hbe, hse, vbs, hbs, i;
  386. struct tegra_sor *sor = to_sor(output);
  387. struct tegra_sor_config config;
  388. struct drm_dp_link link;
  389. struct drm_dp_aux *aux;
  390. unsigned long value;
  391. int err = 0;
  392. mutex_lock(&sor->lock);
  393. if (sor->enabled)
  394. goto unlock;
  395. err = clk_prepare_enable(sor->clk);
  396. if (err < 0)
  397. goto unlock;
  398. reset_control_deassert(sor->rst);
  399. /* FIXME: properly convert to struct drm_dp_aux */
  400. aux = (struct drm_dp_aux *)sor->dpaux;
  401. if (sor->dpaux) {
  402. err = tegra_dpaux_enable(sor->dpaux);
  403. if (err < 0)
  404. dev_err(sor->dev, "failed to enable DP: %d\n", err);
  405. err = drm_dp_link_probe(aux, &link);
  406. if (err < 0) {
  407. dev_err(sor->dev, "failed to probe eDP link: %d\n",
  408. err);
  409. return err;
  410. }
  411. }
  412. err = clk_set_parent(sor->clk, sor->clk_safe);
  413. if (err < 0)
  414. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  415. memset(&config, 0, sizeof(config));
  416. config.bits_per_pixel = 24; /* XXX: don't hardcode? */
  417. err = tegra_sor_calc_config(sor, mode, &config, &link);
  418. if (err < 0)
  419. dev_err(sor->dev, "failed to compute link configuration: %d\n",
  420. err);
  421. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  422. value &= ~SOR_CLK_CNTRL_DP_CLK_SEL_MASK;
  423. value |= SOR_CLK_CNTRL_DP_CLK_SEL_SINGLE_DPCLK;
  424. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  425. value = tegra_sor_readl(sor, SOR_PLL_2);
  426. value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
  427. tegra_sor_writel(sor, value, SOR_PLL_2);
  428. usleep_range(20, 100);
  429. value = tegra_sor_readl(sor, SOR_PLL_3);
  430. value |= SOR_PLL_3_PLL_VDD_MODE_V3_3;
  431. tegra_sor_writel(sor, value, SOR_PLL_3);
  432. value = SOR_PLL_0_ICHPMP(0xf) | SOR_PLL_0_VCOCAP_RST |
  433. SOR_PLL_0_PLLREG_LEVEL_V45 | SOR_PLL_0_RESISTOR_EXT;
  434. tegra_sor_writel(sor, value, SOR_PLL_0);
  435. value = tegra_sor_readl(sor, SOR_PLL_2);
  436. value |= SOR_PLL_2_SEQ_PLLCAPPD;
  437. value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
  438. value |= SOR_PLL_2_LVDS_ENABLE;
  439. tegra_sor_writel(sor, value, SOR_PLL_2);
  440. value = SOR_PLL_1_TERM_COMPOUT | SOR_PLL_1_TMDS_TERM;
  441. tegra_sor_writel(sor, value, SOR_PLL_1);
  442. while (true) {
  443. value = tegra_sor_readl(sor, SOR_PLL_2);
  444. if ((value & SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE) == 0)
  445. break;
  446. usleep_range(250, 1000);
  447. }
  448. value = tegra_sor_readl(sor, SOR_PLL_2);
  449. value &= ~SOR_PLL_2_POWERDOWN_OVERRIDE;
  450. value &= ~SOR_PLL_2_PORT_POWERDOWN;
  451. tegra_sor_writel(sor, value, SOR_PLL_2);
  452. /*
  453. * power up
  454. */
  455. /* set safe link bandwidth (1.62 Gbps) */
  456. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  457. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  458. value |= SOR_CLK_CNTRL_DP_LINK_SPEED_G1_62;
  459. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  460. /* step 1 */
  461. value = tegra_sor_readl(sor, SOR_PLL_2);
  462. value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE | SOR_PLL_2_PORT_POWERDOWN |
  463. SOR_PLL_2_BANDGAP_POWERDOWN;
  464. tegra_sor_writel(sor, value, SOR_PLL_2);
  465. value = tegra_sor_readl(sor, SOR_PLL_0);
  466. value |= SOR_PLL_0_VCOPD | SOR_PLL_0_POWER_OFF;
  467. tegra_sor_writel(sor, value, SOR_PLL_0);
  468. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  469. value &= ~SOR_DP_PADCTL_PAD_CAL_PD;
  470. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  471. /* step 2 */
  472. err = tegra_io_rail_power_on(TEGRA_IO_RAIL_LVDS);
  473. if (err < 0) {
  474. dev_err(sor->dev, "failed to power on I/O rail: %d\n", err);
  475. goto unlock;
  476. }
  477. usleep_range(5, 100);
  478. /* step 3 */
  479. value = tegra_sor_readl(sor, SOR_PLL_2);
  480. value &= ~SOR_PLL_2_BANDGAP_POWERDOWN;
  481. tegra_sor_writel(sor, value, SOR_PLL_2);
  482. usleep_range(20, 100);
  483. /* step 4 */
  484. value = tegra_sor_readl(sor, SOR_PLL_0);
  485. value &= ~SOR_PLL_0_POWER_OFF;
  486. value &= ~SOR_PLL_0_VCOPD;
  487. tegra_sor_writel(sor, value, SOR_PLL_0);
  488. value = tegra_sor_readl(sor, SOR_PLL_2);
  489. value &= ~SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
  490. tegra_sor_writel(sor, value, SOR_PLL_2);
  491. usleep_range(200, 1000);
  492. /* step 5 */
  493. value = tegra_sor_readl(sor, SOR_PLL_2);
  494. value &= ~SOR_PLL_2_PORT_POWERDOWN;
  495. tegra_sor_writel(sor, value, SOR_PLL_2);
  496. /* switch to DP clock */
  497. err = clk_set_parent(sor->clk, sor->clk_dp);
  498. if (err < 0)
  499. dev_err(sor->dev, "failed to set DP parent clock: %d\n", err);
  500. /* power DP lanes */
  501. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  502. if (link.num_lanes <= 2)
  503. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2);
  504. else
  505. value |= SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_2;
  506. if (link.num_lanes <= 1)
  507. value &= ~SOR_DP_PADCTL_PD_TXD_1;
  508. else
  509. value |= SOR_DP_PADCTL_PD_TXD_1;
  510. if (link.num_lanes == 0)
  511. value &= ~SOR_DP_PADCTL_PD_TXD_0;
  512. else
  513. value |= SOR_DP_PADCTL_PD_TXD_0;
  514. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  515. value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
  516. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  517. value |= SOR_DP_LINKCTL_LANE_COUNT(link.num_lanes);
  518. tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
  519. /* start lane sequencer */
  520. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_DOWN |
  521. SOR_LANE_SEQ_CTL_POWER_STATE_UP;
  522. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  523. while (true) {
  524. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  525. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  526. break;
  527. usleep_range(250, 1000);
  528. }
  529. /* set link bandwidth */
  530. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  531. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  532. value |= drm_dp_link_rate_to_bw_code(link.rate) << 2;
  533. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  534. /* set linkctl */
  535. value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
  536. value |= SOR_DP_LINKCTL_ENABLE;
  537. value &= ~SOR_DP_LINKCTL_TU_SIZE_MASK;
  538. value |= SOR_DP_LINKCTL_TU_SIZE(config.tu_size);
  539. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  540. tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
  541. for (i = 0, value = 0; i < 4; i++) {
  542. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  543. SOR_DP_TPG_SCRAMBLER_GALIOS |
  544. SOR_DP_TPG_PATTERN_NONE;
  545. value = (value << 8) | lane;
  546. }
  547. tegra_sor_writel(sor, value, SOR_DP_TPG);
  548. value = tegra_sor_readl(sor, SOR_DP_CONFIG_0);
  549. value &= ~SOR_DP_CONFIG_WATERMARK_MASK;
  550. value |= SOR_DP_CONFIG_WATERMARK(config.watermark);
  551. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_COUNT_MASK;
  552. value |= SOR_DP_CONFIG_ACTIVE_SYM_COUNT(config.active_count);
  553. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_FRAC_MASK;
  554. value |= SOR_DP_CONFIG_ACTIVE_SYM_FRAC(config.active_frac);
  555. if (config.active_polarity)
  556. value |= SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  557. else
  558. value &= ~SOR_DP_CONFIG_ACTIVE_SYM_POLARITY;
  559. value |= SOR_DP_CONFIG_ACTIVE_SYM_ENABLE;
  560. value |= SOR_DP_CONFIG_DISPARITY_NEGATIVE;
  561. tegra_sor_writel(sor, value, SOR_DP_CONFIG_0);
  562. value = tegra_sor_readl(sor, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  563. value &= ~SOR_DP_AUDIO_HBLANK_SYMBOLS_MASK;
  564. value |= config.hblank_symbols & 0xffff;
  565. tegra_sor_writel(sor, value, SOR_DP_AUDIO_HBLANK_SYMBOLS);
  566. value = tegra_sor_readl(sor, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  567. value &= ~SOR_DP_AUDIO_VBLANK_SYMBOLS_MASK;
  568. value |= config.vblank_symbols & 0xffff;
  569. tegra_sor_writel(sor, value, SOR_DP_AUDIO_VBLANK_SYMBOLS);
  570. /* enable pad calibration logic */
  571. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  572. value |= SOR_DP_PADCTL_PAD_CAL_PD;
  573. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  574. if (sor->dpaux) {
  575. u8 rate, lanes;
  576. err = drm_dp_link_probe(aux, &link);
  577. if (err < 0) {
  578. dev_err(sor->dev, "failed to probe eDP link: %d\n",
  579. err);
  580. goto unlock;
  581. }
  582. err = drm_dp_link_power_up(aux, &link);
  583. if (err < 0) {
  584. dev_err(sor->dev, "failed to power up eDP link: %d\n",
  585. err);
  586. goto unlock;
  587. }
  588. err = drm_dp_link_configure(aux, &link);
  589. if (err < 0) {
  590. dev_err(sor->dev, "failed to configure eDP link: %d\n",
  591. err);
  592. goto unlock;
  593. }
  594. rate = drm_dp_link_rate_to_bw_code(link.rate);
  595. lanes = link.num_lanes;
  596. value = tegra_sor_readl(sor, SOR_CLK_CNTRL);
  597. value &= ~SOR_CLK_CNTRL_DP_LINK_SPEED_MASK;
  598. value |= SOR_CLK_CNTRL_DP_LINK_SPEED(rate);
  599. tegra_sor_writel(sor, value, SOR_CLK_CNTRL);
  600. value = tegra_sor_readl(sor, SOR_DP_LINKCTL_0);
  601. value &= ~SOR_DP_LINKCTL_LANE_COUNT_MASK;
  602. value |= SOR_DP_LINKCTL_LANE_COUNT(lanes);
  603. if (link.capabilities & DP_LINK_CAP_ENHANCED_FRAMING)
  604. value |= SOR_DP_LINKCTL_ENHANCED_FRAME;
  605. tegra_sor_writel(sor, value, SOR_DP_LINKCTL_0);
  606. /* disable training pattern generator */
  607. for (i = 0; i < link.num_lanes; i++) {
  608. unsigned long lane = SOR_DP_TPG_CHANNEL_CODING |
  609. SOR_DP_TPG_SCRAMBLER_GALIOS |
  610. SOR_DP_TPG_PATTERN_NONE;
  611. value = (value << 8) | lane;
  612. }
  613. tegra_sor_writel(sor, value, SOR_DP_TPG);
  614. err = tegra_sor_dp_train_fast(sor, &link);
  615. if (err < 0) {
  616. dev_err(sor->dev, "DP fast link training failed: %d\n",
  617. err);
  618. goto unlock;
  619. }
  620. dev_dbg(sor->dev, "fast link training succeeded\n");
  621. }
  622. err = tegra_sor_power_up(sor, 250);
  623. if (err < 0) {
  624. dev_err(sor->dev, "failed to power up SOR: %d\n", err);
  625. goto unlock;
  626. }
  627. /* start display controller in continuous mode */
  628. value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
  629. value |= WRITE_MUX;
  630. tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS);
  631. tegra_dc_writel(dc, VSYNC_H_POSITION(1), DC_DISP_DISP_TIMING_OPTIONS);
  632. tegra_dc_writel(dc, DISP_CTRL_MODE_C_DISPLAY, DC_CMD_DISPLAY_COMMAND);
  633. value = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
  634. value &= ~WRITE_MUX;
  635. tegra_dc_writel(dc, value, DC_CMD_STATE_ACCESS);
  636. /*
  637. * configure panel (24bpp, vsync-, hsync-, DP-A protocol, complete
  638. * raster, associate with display controller)
  639. */
  640. value = SOR_STATE_ASY_VSYNCPOL |
  641. SOR_STATE_ASY_HSYNCPOL |
  642. SOR_STATE_ASY_PROTOCOL_DP_A |
  643. SOR_STATE_ASY_CRC_MODE_COMPLETE |
  644. SOR_STATE_ASY_OWNER(dc->pipe + 1);
  645. switch (config.bits_per_pixel) {
  646. case 24:
  647. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
  648. break;
  649. case 18:
  650. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
  651. break;
  652. default:
  653. BUG();
  654. break;
  655. }
  656. tegra_sor_writel(sor, value, SOR_STATE_1);
  657. /*
  658. * TODO: The video timing programming below doesn't seem to match the
  659. * register definitions.
  660. */
  661. value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
  662. tegra_sor_writel(sor, value, SOR_HEAD_STATE_1(0));
  663. vse = mode->vsync_end - mode->vsync_start - 1;
  664. hse = mode->hsync_end - mode->hsync_start - 1;
  665. value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
  666. tegra_sor_writel(sor, value, SOR_HEAD_STATE_2(0));
  667. vbe = vse + (mode->vsync_start - mode->vdisplay);
  668. hbe = hse + (mode->hsync_start - mode->hdisplay);
  669. value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
  670. tegra_sor_writel(sor, value, SOR_HEAD_STATE_3(0));
  671. vbs = vbe + mode->vdisplay;
  672. hbs = hbe + mode->hdisplay;
  673. value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
  674. tegra_sor_writel(sor, value, SOR_HEAD_STATE_4(0));
  675. /* CSTM (LVDS, link A/B, upper) */
  676. value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
  677. SOR_CSTM_UPPER;
  678. tegra_sor_writel(sor, value, SOR_CSTM);
  679. /* PWM setup */
  680. err = tegra_sor_setup_pwm(sor, 250);
  681. if (err < 0) {
  682. dev_err(sor->dev, "failed to setup PWM: %d\n", err);
  683. goto unlock;
  684. }
  685. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  686. value |= SOR_ENABLE;
  687. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  688. tegra_sor_update(sor);
  689. err = tegra_sor_attach(sor);
  690. if (err < 0) {
  691. dev_err(sor->dev, "failed to attach SOR: %d\n", err);
  692. goto unlock;
  693. }
  694. err = tegra_sor_wakeup(sor);
  695. if (err < 0) {
  696. dev_err(sor->dev, "failed to enable DC: %d\n", err);
  697. goto unlock;
  698. }
  699. sor->enabled = true;
  700. unlock:
  701. mutex_unlock(&sor->lock);
  702. return err;
  703. }
  704. static int tegra_sor_detach(struct tegra_sor *sor)
  705. {
  706. unsigned long value, timeout;
  707. /* switch to safe mode */
  708. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  709. value &= ~SOR_SUPER_STATE_MODE_NORMAL;
  710. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  711. tegra_sor_super_update(sor);
  712. timeout = jiffies + msecs_to_jiffies(250);
  713. while (time_before(jiffies, timeout)) {
  714. value = tegra_sor_readl(sor, SOR_PWR);
  715. if (value & SOR_PWR_MODE_SAFE)
  716. break;
  717. }
  718. if ((value & SOR_PWR_MODE_SAFE) == 0)
  719. return -ETIMEDOUT;
  720. /* go to sleep */
  721. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  722. value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
  723. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  724. tegra_sor_super_update(sor);
  725. /* detach */
  726. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  727. value &= ~SOR_SUPER_STATE_ATTACHED;
  728. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  729. tegra_sor_super_update(sor);
  730. timeout = jiffies + msecs_to_jiffies(250);
  731. while (time_before(jiffies, timeout)) {
  732. value = tegra_sor_readl(sor, SOR_TEST);
  733. if ((value & SOR_TEST_ATTACHED) == 0)
  734. break;
  735. usleep_range(25, 100);
  736. }
  737. if ((value & SOR_TEST_ATTACHED) != 0)
  738. return -ETIMEDOUT;
  739. return 0;
  740. }
  741. static int tegra_sor_power_down(struct tegra_sor *sor)
  742. {
  743. unsigned long value, timeout;
  744. int err;
  745. value = tegra_sor_readl(sor, SOR_PWR);
  746. value &= ~SOR_PWR_NORMAL_STATE_PU;
  747. value |= SOR_PWR_TRIGGER;
  748. tegra_sor_writel(sor, value, SOR_PWR);
  749. timeout = jiffies + msecs_to_jiffies(250);
  750. while (time_before(jiffies, timeout)) {
  751. value = tegra_sor_readl(sor, SOR_PWR);
  752. if ((value & SOR_PWR_TRIGGER) == 0)
  753. return 0;
  754. usleep_range(25, 100);
  755. }
  756. if ((value & SOR_PWR_TRIGGER) != 0)
  757. return -ETIMEDOUT;
  758. err = clk_set_parent(sor->clk, sor->clk_safe);
  759. if (err < 0)
  760. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  761. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  762. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
  763. SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
  764. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  765. /* stop lane sequencer */
  766. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
  767. SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
  768. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  769. timeout = jiffies + msecs_to_jiffies(250);
  770. while (time_before(jiffies, timeout)) {
  771. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  772. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  773. break;
  774. usleep_range(25, 100);
  775. }
  776. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
  777. return -ETIMEDOUT;
  778. value = tegra_sor_readl(sor, SOR_PLL_2);
  779. value |= SOR_PLL_2_PORT_POWERDOWN;
  780. tegra_sor_writel(sor, value, SOR_PLL_2);
  781. usleep_range(20, 100);
  782. value = tegra_sor_readl(sor, SOR_PLL_0);
  783. value |= SOR_PLL_0_POWER_OFF;
  784. value |= SOR_PLL_0_VCOPD;
  785. tegra_sor_writel(sor, value, SOR_PLL_0);
  786. value = tegra_sor_readl(sor, SOR_PLL_2);
  787. value |= SOR_PLL_2_SEQ_PLLCAPPD;
  788. value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
  789. tegra_sor_writel(sor, value, SOR_PLL_2);
  790. usleep_range(20, 100);
  791. return 0;
  792. }
  793. static int tegra_output_sor_disable(struct tegra_output *output)
  794. {
  795. struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
  796. struct tegra_sor *sor = to_sor(output);
  797. unsigned long value;
  798. int err = 0;
  799. mutex_lock(&sor->lock);
  800. if (!sor->enabled)
  801. goto unlock;
  802. err = tegra_sor_detach(sor);
  803. if (err < 0) {
  804. dev_err(sor->dev, "failed to detach SOR: %d\n", err);
  805. goto unlock;
  806. }
  807. tegra_sor_writel(sor, 0, SOR_STATE_1);
  808. tegra_sor_update(sor);
  809. /*
  810. * The following accesses registers of the display controller, so make
  811. * sure it's only executed when the output is attached to one.
  812. */
  813. if (dc) {
  814. /*
  815. * XXX: We can't do this here because it causes the SOR to go
  816. * into an erroneous state and the output will look scrambled
  817. * the next time it is enabled. Presumably this is because we
  818. * should be doing this only on the next VBLANK. A possible
  819. * solution would be to queue a "power-off" event to trigger
  820. * this code to be run during the next VBLANK.
  821. */
  822. /*
  823. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  824. value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  825. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
  826. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  827. */
  828. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  829. value &= ~DISP_CTRL_MODE_MASK;
  830. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  831. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  832. value &= ~SOR_ENABLE;
  833. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  834. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  835. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  836. }
  837. err = tegra_sor_power_down(sor);
  838. if (err < 0) {
  839. dev_err(sor->dev, "failed to power down SOR: %d\n", err);
  840. goto unlock;
  841. }
  842. if (sor->dpaux) {
  843. err = tegra_dpaux_disable(sor->dpaux);
  844. if (err < 0) {
  845. dev_err(sor->dev, "failed to disable DP: %d\n", err);
  846. goto unlock;
  847. }
  848. }
  849. err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
  850. if (err < 0) {
  851. dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
  852. goto unlock;
  853. }
  854. reset_control_assert(sor->rst);
  855. clk_disable_unprepare(sor->clk);
  856. sor->enabled = false;
  857. unlock:
  858. mutex_unlock(&sor->lock);
  859. return err;
  860. }
  861. static int tegra_output_sor_setup_clock(struct tegra_output *output,
  862. struct clk *clk, unsigned long pclk,
  863. unsigned int *div)
  864. {
  865. struct tegra_sor *sor = to_sor(output);
  866. int err;
  867. err = clk_set_parent(clk, sor->clk_parent);
  868. if (err < 0) {
  869. dev_err(sor->dev, "failed to set parent clock: %d\n", err);
  870. return err;
  871. }
  872. err = clk_set_rate(sor->clk_parent, pclk);
  873. if (err < 0) {
  874. dev_err(sor->dev, "failed to set clock rate to %lu Hz\n", pclk);
  875. return err;
  876. }
  877. *div = 0;
  878. return 0;
  879. }
  880. static int tegra_output_sor_check_mode(struct tegra_output *output,
  881. struct drm_display_mode *mode,
  882. enum drm_mode_status *status)
  883. {
  884. /*
  885. * FIXME: For now, always assume that the mode is okay.
  886. */
  887. *status = MODE_OK;
  888. return 0;
  889. }
  890. static enum drm_connector_status
  891. tegra_output_sor_detect(struct tegra_output *output)
  892. {
  893. struct tegra_sor *sor = to_sor(output);
  894. if (sor->dpaux)
  895. return tegra_dpaux_detect(sor->dpaux);
  896. return connector_status_unknown;
  897. }
  898. static const struct tegra_output_ops sor_ops = {
  899. .enable = tegra_output_sor_enable,
  900. .disable = tegra_output_sor_disable,
  901. .setup_clock = tegra_output_sor_setup_clock,
  902. .check_mode = tegra_output_sor_check_mode,
  903. .detect = tegra_output_sor_detect,
  904. };
  905. static int tegra_sor_crc_open(struct inode *inode, struct file *file)
  906. {
  907. file->private_data = inode->i_private;
  908. return 0;
  909. }
  910. static int tegra_sor_crc_release(struct inode *inode, struct file *file)
  911. {
  912. return 0;
  913. }
  914. static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
  915. {
  916. u32 value;
  917. timeout = jiffies + msecs_to_jiffies(timeout);
  918. while (time_before(jiffies, timeout)) {
  919. value = tegra_sor_readl(sor, SOR_CRC_A);
  920. if (value & SOR_CRC_A_VALID)
  921. return 0;
  922. usleep_range(100, 200);
  923. }
  924. return -ETIMEDOUT;
  925. }
  926. static ssize_t tegra_sor_crc_read(struct file *file, char __user *buffer,
  927. size_t size, loff_t *ppos)
  928. {
  929. struct tegra_sor *sor = file->private_data;
  930. ssize_t num, err;
  931. char buf[10];
  932. u32 value;
  933. mutex_lock(&sor->lock);
  934. if (!sor->enabled) {
  935. err = -EAGAIN;
  936. goto unlock;
  937. }
  938. value = tegra_sor_readl(sor, SOR_STATE_1);
  939. value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
  940. tegra_sor_writel(sor, value, SOR_STATE_1);
  941. value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
  942. value |= SOR_CRC_CNTRL_ENABLE;
  943. tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
  944. value = tegra_sor_readl(sor, SOR_TEST);
  945. value &= ~SOR_TEST_CRC_POST_SERIALIZE;
  946. tegra_sor_writel(sor, value, SOR_TEST);
  947. err = tegra_sor_crc_wait(sor, 100);
  948. if (err < 0)
  949. goto unlock;
  950. tegra_sor_writel(sor, SOR_CRC_A_RESET, SOR_CRC_A);
  951. value = tegra_sor_readl(sor, SOR_CRC_B);
  952. num = scnprintf(buf, sizeof(buf), "%08x\n", value);
  953. err = simple_read_from_buffer(buffer, size, ppos, buf, num);
  954. unlock:
  955. mutex_unlock(&sor->lock);
  956. return err;
  957. }
  958. static const struct file_operations tegra_sor_crc_fops = {
  959. .owner = THIS_MODULE,
  960. .open = tegra_sor_crc_open,
  961. .read = tegra_sor_crc_read,
  962. .release = tegra_sor_crc_release,
  963. };
  964. static int tegra_sor_debugfs_init(struct tegra_sor *sor,
  965. struct drm_minor *minor)
  966. {
  967. struct dentry *entry;
  968. int err = 0;
  969. sor->debugfs = debugfs_create_dir("sor", minor->debugfs_root);
  970. if (!sor->debugfs)
  971. return -ENOMEM;
  972. entry = debugfs_create_file("crc", 0644, sor->debugfs, sor,
  973. &tegra_sor_crc_fops);
  974. if (!entry) {
  975. dev_err(sor->dev,
  976. "cannot create /sys/kernel/debug/dri/%s/sor/crc\n",
  977. minor->debugfs_root->d_name.name);
  978. err = -ENOMEM;
  979. goto remove;
  980. }
  981. return err;
  982. remove:
  983. debugfs_remove(sor->debugfs);
  984. sor->debugfs = NULL;
  985. return err;
  986. }
  987. static int tegra_sor_debugfs_exit(struct tegra_sor *sor)
  988. {
  989. debugfs_remove_recursive(sor->debugfs);
  990. sor->debugfs = NULL;
  991. return 0;
  992. }
  993. static int tegra_sor_init(struct host1x_client *client)
  994. {
  995. struct drm_device *drm = dev_get_drvdata(client->parent);
  996. struct tegra_sor *sor = host1x_client_to_sor(client);
  997. int err;
  998. if (!sor->dpaux)
  999. return -ENODEV;
  1000. sor->output.type = TEGRA_OUTPUT_EDP;
  1001. sor->output.dev = sor->dev;
  1002. sor->output.ops = &sor_ops;
  1003. err = tegra_output_init(drm, &sor->output);
  1004. if (err < 0) {
  1005. dev_err(sor->dev, "output setup failed: %d\n", err);
  1006. return err;
  1007. }
  1008. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1009. err = tegra_sor_debugfs_init(sor, drm->primary);
  1010. if (err < 0)
  1011. dev_err(sor->dev, "debugfs setup failed: %d\n", err);
  1012. }
  1013. if (sor->dpaux) {
  1014. err = tegra_dpaux_attach(sor->dpaux, &sor->output);
  1015. if (err < 0) {
  1016. dev_err(sor->dev, "failed to attach DP: %d\n", err);
  1017. return err;
  1018. }
  1019. }
  1020. return 0;
  1021. }
  1022. static int tegra_sor_exit(struct host1x_client *client)
  1023. {
  1024. struct tegra_sor *sor = host1x_client_to_sor(client);
  1025. int err;
  1026. err = tegra_output_disable(&sor->output);
  1027. if (err < 0) {
  1028. dev_err(sor->dev, "output failed to disable: %d\n", err);
  1029. return err;
  1030. }
  1031. if (sor->dpaux) {
  1032. err = tegra_dpaux_detach(sor->dpaux);
  1033. if (err < 0) {
  1034. dev_err(sor->dev, "failed to detach DP: %d\n", err);
  1035. return err;
  1036. }
  1037. }
  1038. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1039. err = tegra_sor_debugfs_exit(sor);
  1040. if (err < 0)
  1041. dev_err(sor->dev, "debugfs cleanup failed: %d\n", err);
  1042. }
  1043. err = tegra_output_exit(&sor->output);
  1044. if (err < 0) {
  1045. dev_err(sor->dev, "output cleanup failed: %d\n", err);
  1046. return err;
  1047. }
  1048. return 0;
  1049. }
  1050. static const struct host1x_client_ops sor_client_ops = {
  1051. .init = tegra_sor_init,
  1052. .exit = tegra_sor_exit,
  1053. };
  1054. static int tegra_sor_probe(struct platform_device *pdev)
  1055. {
  1056. struct device_node *np;
  1057. struct tegra_sor *sor;
  1058. struct resource *regs;
  1059. int err;
  1060. sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
  1061. if (!sor)
  1062. return -ENOMEM;
  1063. sor->output.dev = sor->dev = &pdev->dev;
  1064. np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
  1065. if (np) {
  1066. sor->dpaux = tegra_dpaux_find_by_of_node(np);
  1067. of_node_put(np);
  1068. if (!sor->dpaux)
  1069. return -EPROBE_DEFER;
  1070. }
  1071. err = tegra_output_probe(&sor->output);
  1072. if (err < 0)
  1073. return err;
  1074. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1075. sor->regs = devm_ioremap_resource(&pdev->dev, regs);
  1076. if (IS_ERR(sor->regs))
  1077. return PTR_ERR(sor->regs);
  1078. sor->rst = devm_reset_control_get(&pdev->dev, "sor");
  1079. if (IS_ERR(sor->rst))
  1080. return PTR_ERR(sor->rst);
  1081. sor->clk = devm_clk_get(&pdev->dev, NULL);
  1082. if (IS_ERR(sor->clk))
  1083. return PTR_ERR(sor->clk);
  1084. sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
  1085. if (IS_ERR(sor->clk_parent))
  1086. return PTR_ERR(sor->clk_parent);
  1087. err = clk_prepare_enable(sor->clk_parent);
  1088. if (err < 0)
  1089. return err;
  1090. sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
  1091. if (IS_ERR(sor->clk_safe))
  1092. return PTR_ERR(sor->clk_safe);
  1093. err = clk_prepare_enable(sor->clk_safe);
  1094. if (err < 0)
  1095. return err;
  1096. sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
  1097. if (IS_ERR(sor->clk_dp))
  1098. return PTR_ERR(sor->clk_dp);
  1099. err = clk_prepare_enable(sor->clk_dp);
  1100. if (err < 0)
  1101. return err;
  1102. INIT_LIST_HEAD(&sor->client.list);
  1103. sor->client.ops = &sor_client_ops;
  1104. sor->client.dev = &pdev->dev;
  1105. mutex_init(&sor->lock);
  1106. err = host1x_client_register(&sor->client);
  1107. if (err < 0) {
  1108. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  1109. err);
  1110. return err;
  1111. }
  1112. platform_set_drvdata(pdev, sor);
  1113. return 0;
  1114. }
  1115. static int tegra_sor_remove(struct platform_device *pdev)
  1116. {
  1117. struct tegra_sor *sor = platform_get_drvdata(pdev);
  1118. int err;
  1119. err = host1x_client_unregister(&sor->client);
  1120. if (err < 0) {
  1121. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  1122. err);
  1123. return err;
  1124. }
  1125. clk_disable_unprepare(sor->clk_parent);
  1126. clk_disable_unprepare(sor->clk_safe);
  1127. clk_disable_unprepare(sor->clk_dp);
  1128. clk_disable_unprepare(sor->clk);
  1129. return 0;
  1130. }
  1131. static const struct of_device_id tegra_sor_of_match[] = {
  1132. { .compatible = "nvidia,tegra124-sor", },
  1133. { },
  1134. };
  1135. struct platform_driver tegra_sor_driver = {
  1136. .driver = {
  1137. .name = "tegra-sor",
  1138. .of_match_table = tegra_sor_of_match,
  1139. },
  1140. .probe = tegra_sor_probe,
  1141. .remove = tegra_sor_remove,
  1142. };