sor.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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 <soc/tegra/pmc.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. goto unlock;
  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 = output->connector.display_info.bpc * 3;
  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_PROTOCOL_DP_A |
  641. SOR_STATE_ASY_CRC_MODE_COMPLETE |
  642. SOR_STATE_ASY_OWNER(dc->pipe + 1);
  643. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  644. value &= ~SOR_STATE_ASY_HSYNCPOL;
  645. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  646. value |= SOR_STATE_ASY_HSYNCPOL;
  647. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  648. value &= ~SOR_STATE_ASY_VSYNCPOL;
  649. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  650. value |= SOR_STATE_ASY_VSYNCPOL;
  651. switch (config.bits_per_pixel) {
  652. case 24:
  653. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_24_444;
  654. break;
  655. case 18:
  656. value |= SOR_STATE_ASY_PIXELDEPTH_BPP_18_444;
  657. break;
  658. default:
  659. BUG();
  660. break;
  661. }
  662. tegra_sor_writel(sor, value, SOR_STATE_1);
  663. /*
  664. * TODO: The video timing programming below doesn't seem to match the
  665. * register definitions.
  666. */
  667. value = ((mode->vtotal & 0x7fff) << 16) | (mode->htotal & 0x7fff);
  668. tegra_sor_writel(sor, value, SOR_HEAD_STATE_1(0));
  669. vse = mode->vsync_end - mode->vsync_start - 1;
  670. hse = mode->hsync_end - mode->hsync_start - 1;
  671. value = ((vse & 0x7fff) << 16) | (hse & 0x7fff);
  672. tegra_sor_writel(sor, value, SOR_HEAD_STATE_2(0));
  673. vbe = vse + (mode->vsync_start - mode->vdisplay);
  674. hbe = hse + (mode->hsync_start - mode->hdisplay);
  675. value = ((vbe & 0x7fff) << 16) | (hbe & 0x7fff);
  676. tegra_sor_writel(sor, value, SOR_HEAD_STATE_3(0));
  677. vbs = vbe + mode->vdisplay;
  678. hbs = hbe + mode->hdisplay;
  679. value = ((vbs & 0x7fff) << 16) | (hbs & 0x7fff);
  680. tegra_sor_writel(sor, value, SOR_HEAD_STATE_4(0));
  681. /* CSTM (LVDS, link A/B, upper) */
  682. value = SOR_CSTM_LVDS | SOR_CSTM_LINK_ACT_A | SOR_CSTM_LINK_ACT_B |
  683. SOR_CSTM_UPPER;
  684. tegra_sor_writel(sor, value, SOR_CSTM);
  685. /* PWM setup */
  686. err = tegra_sor_setup_pwm(sor, 250);
  687. if (err < 0) {
  688. dev_err(sor->dev, "failed to setup PWM: %d\n", err);
  689. goto unlock;
  690. }
  691. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  692. value |= SOR_ENABLE;
  693. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  694. tegra_sor_update(sor);
  695. err = tegra_sor_attach(sor);
  696. if (err < 0) {
  697. dev_err(sor->dev, "failed to attach SOR: %d\n", err);
  698. goto unlock;
  699. }
  700. err = tegra_sor_wakeup(sor);
  701. if (err < 0) {
  702. dev_err(sor->dev, "failed to enable DC: %d\n", err);
  703. goto unlock;
  704. }
  705. sor->enabled = true;
  706. unlock:
  707. mutex_unlock(&sor->lock);
  708. return err;
  709. }
  710. static int tegra_sor_detach(struct tegra_sor *sor)
  711. {
  712. unsigned long value, timeout;
  713. /* switch to safe mode */
  714. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  715. value &= ~SOR_SUPER_STATE_MODE_NORMAL;
  716. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  717. tegra_sor_super_update(sor);
  718. timeout = jiffies + msecs_to_jiffies(250);
  719. while (time_before(jiffies, timeout)) {
  720. value = tegra_sor_readl(sor, SOR_PWR);
  721. if (value & SOR_PWR_MODE_SAFE)
  722. break;
  723. }
  724. if ((value & SOR_PWR_MODE_SAFE) == 0)
  725. return -ETIMEDOUT;
  726. /* go to sleep */
  727. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  728. value &= ~SOR_SUPER_STATE_HEAD_MODE_MASK;
  729. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  730. tegra_sor_super_update(sor);
  731. /* detach */
  732. value = tegra_sor_readl(sor, SOR_SUPER_STATE_1);
  733. value &= ~SOR_SUPER_STATE_ATTACHED;
  734. tegra_sor_writel(sor, value, SOR_SUPER_STATE_1);
  735. tegra_sor_super_update(sor);
  736. timeout = jiffies + msecs_to_jiffies(250);
  737. while (time_before(jiffies, timeout)) {
  738. value = tegra_sor_readl(sor, SOR_TEST);
  739. if ((value & SOR_TEST_ATTACHED) == 0)
  740. break;
  741. usleep_range(25, 100);
  742. }
  743. if ((value & SOR_TEST_ATTACHED) != 0)
  744. return -ETIMEDOUT;
  745. return 0;
  746. }
  747. static int tegra_sor_power_down(struct tegra_sor *sor)
  748. {
  749. unsigned long value, timeout;
  750. int err;
  751. value = tegra_sor_readl(sor, SOR_PWR);
  752. value &= ~SOR_PWR_NORMAL_STATE_PU;
  753. value |= SOR_PWR_TRIGGER;
  754. tegra_sor_writel(sor, value, SOR_PWR);
  755. timeout = jiffies + msecs_to_jiffies(250);
  756. while (time_before(jiffies, timeout)) {
  757. value = tegra_sor_readl(sor, SOR_PWR);
  758. if ((value & SOR_PWR_TRIGGER) == 0)
  759. return 0;
  760. usleep_range(25, 100);
  761. }
  762. if ((value & SOR_PWR_TRIGGER) != 0)
  763. return -ETIMEDOUT;
  764. err = clk_set_parent(sor->clk, sor->clk_safe);
  765. if (err < 0)
  766. dev_err(sor->dev, "failed to set safe parent clock: %d\n", err);
  767. value = tegra_sor_readl(sor, SOR_DP_PADCTL_0);
  768. value &= ~(SOR_DP_PADCTL_PD_TXD_3 | SOR_DP_PADCTL_PD_TXD_0 |
  769. SOR_DP_PADCTL_PD_TXD_1 | SOR_DP_PADCTL_PD_TXD_2);
  770. tegra_sor_writel(sor, value, SOR_DP_PADCTL_0);
  771. /* stop lane sequencer */
  772. value = SOR_LANE_SEQ_CTL_TRIGGER | SOR_LANE_SEQ_CTL_SEQUENCE_UP |
  773. SOR_LANE_SEQ_CTL_POWER_STATE_DOWN;
  774. tegra_sor_writel(sor, value, SOR_LANE_SEQ_CTL);
  775. timeout = jiffies + msecs_to_jiffies(250);
  776. while (time_before(jiffies, timeout)) {
  777. value = tegra_sor_readl(sor, SOR_LANE_SEQ_CTL);
  778. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) == 0)
  779. break;
  780. usleep_range(25, 100);
  781. }
  782. if ((value & SOR_LANE_SEQ_CTL_TRIGGER) != 0)
  783. return -ETIMEDOUT;
  784. value = tegra_sor_readl(sor, SOR_PLL_2);
  785. value |= SOR_PLL_2_PORT_POWERDOWN;
  786. tegra_sor_writel(sor, value, SOR_PLL_2);
  787. usleep_range(20, 100);
  788. value = tegra_sor_readl(sor, SOR_PLL_0);
  789. value |= SOR_PLL_0_POWER_OFF;
  790. value |= SOR_PLL_0_VCOPD;
  791. tegra_sor_writel(sor, value, SOR_PLL_0);
  792. value = tegra_sor_readl(sor, SOR_PLL_2);
  793. value |= SOR_PLL_2_SEQ_PLLCAPPD;
  794. value |= SOR_PLL_2_SEQ_PLLCAPPD_ENFORCE;
  795. tegra_sor_writel(sor, value, SOR_PLL_2);
  796. usleep_range(20, 100);
  797. return 0;
  798. }
  799. static int tegra_output_sor_disable(struct tegra_output *output)
  800. {
  801. struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
  802. struct tegra_sor *sor = to_sor(output);
  803. unsigned long value;
  804. int err = 0;
  805. mutex_lock(&sor->lock);
  806. if (!sor->enabled)
  807. goto unlock;
  808. err = tegra_sor_detach(sor);
  809. if (err < 0) {
  810. dev_err(sor->dev, "failed to detach SOR: %d\n", err);
  811. goto unlock;
  812. }
  813. tegra_sor_writel(sor, 0, SOR_STATE_1);
  814. tegra_sor_update(sor);
  815. /*
  816. * The following accesses registers of the display controller, so make
  817. * sure it's only executed when the output is attached to one.
  818. */
  819. if (dc) {
  820. /*
  821. * XXX: We can't do this here because it causes the SOR to go
  822. * into an erroneous state and the output will look scrambled
  823. * the next time it is enabled. Presumably this is because we
  824. * should be doing this only on the next VBLANK. A possible
  825. * solution would be to queue a "power-off" event to trigger
  826. * this code to be run during the next VBLANK.
  827. */
  828. /*
  829. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  830. value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  831. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
  832. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  833. */
  834. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  835. value &= ~DISP_CTRL_MODE_MASK;
  836. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  837. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  838. value &= ~SOR_ENABLE;
  839. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  840. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  841. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  842. }
  843. err = tegra_sor_power_down(sor);
  844. if (err < 0) {
  845. dev_err(sor->dev, "failed to power down SOR: %d\n", err);
  846. goto unlock;
  847. }
  848. if (sor->dpaux) {
  849. err = tegra_dpaux_disable(sor->dpaux);
  850. if (err < 0) {
  851. dev_err(sor->dev, "failed to disable DP: %d\n", err);
  852. goto unlock;
  853. }
  854. }
  855. err = tegra_io_rail_power_off(TEGRA_IO_RAIL_LVDS);
  856. if (err < 0) {
  857. dev_err(sor->dev, "failed to power off I/O rail: %d\n", err);
  858. goto unlock;
  859. }
  860. reset_control_assert(sor->rst);
  861. clk_disable_unprepare(sor->clk);
  862. sor->enabled = false;
  863. unlock:
  864. mutex_unlock(&sor->lock);
  865. return err;
  866. }
  867. static int tegra_output_sor_setup_clock(struct tegra_output *output,
  868. struct clk *clk, unsigned long pclk,
  869. unsigned int *div)
  870. {
  871. struct tegra_sor *sor = to_sor(output);
  872. int err;
  873. err = clk_set_parent(clk, sor->clk_parent);
  874. if (err < 0) {
  875. dev_err(sor->dev, "failed to set parent clock: %d\n", err);
  876. return err;
  877. }
  878. err = clk_set_rate(sor->clk_parent, pclk);
  879. if (err < 0) {
  880. dev_err(sor->dev, "failed to set clock rate to %lu Hz\n", pclk);
  881. return err;
  882. }
  883. *div = 0;
  884. return 0;
  885. }
  886. static int tegra_output_sor_check_mode(struct tegra_output *output,
  887. struct drm_display_mode *mode,
  888. enum drm_mode_status *status)
  889. {
  890. /*
  891. * FIXME: For now, always assume that the mode is okay.
  892. */
  893. *status = MODE_OK;
  894. return 0;
  895. }
  896. static enum drm_connector_status
  897. tegra_output_sor_detect(struct tegra_output *output)
  898. {
  899. struct tegra_sor *sor = to_sor(output);
  900. if (sor->dpaux)
  901. return tegra_dpaux_detect(sor->dpaux);
  902. return connector_status_unknown;
  903. }
  904. static const struct tegra_output_ops sor_ops = {
  905. .enable = tegra_output_sor_enable,
  906. .disable = tegra_output_sor_disable,
  907. .setup_clock = tegra_output_sor_setup_clock,
  908. .check_mode = tegra_output_sor_check_mode,
  909. .detect = tegra_output_sor_detect,
  910. };
  911. static int tegra_sor_crc_open(struct inode *inode, struct file *file)
  912. {
  913. file->private_data = inode->i_private;
  914. return 0;
  915. }
  916. static int tegra_sor_crc_release(struct inode *inode, struct file *file)
  917. {
  918. return 0;
  919. }
  920. static int tegra_sor_crc_wait(struct tegra_sor *sor, unsigned long timeout)
  921. {
  922. u32 value;
  923. timeout = jiffies + msecs_to_jiffies(timeout);
  924. while (time_before(jiffies, timeout)) {
  925. value = tegra_sor_readl(sor, SOR_CRC_A);
  926. if (value & SOR_CRC_A_VALID)
  927. return 0;
  928. usleep_range(100, 200);
  929. }
  930. return -ETIMEDOUT;
  931. }
  932. static ssize_t tegra_sor_crc_read(struct file *file, char __user *buffer,
  933. size_t size, loff_t *ppos)
  934. {
  935. struct tegra_sor *sor = file->private_data;
  936. ssize_t num, err;
  937. char buf[10];
  938. u32 value;
  939. mutex_lock(&sor->lock);
  940. if (!sor->enabled) {
  941. err = -EAGAIN;
  942. goto unlock;
  943. }
  944. value = tegra_sor_readl(sor, SOR_STATE_1);
  945. value &= ~SOR_STATE_ASY_CRC_MODE_MASK;
  946. tegra_sor_writel(sor, value, SOR_STATE_1);
  947. value = tegra_sor_readl(sor, SOR_CRC_CNTRL);
  948. value |= SOR_CRC_CNTRL_ENABLE;
  949. tegra_sor_writel(sor, value, SOR_CRC_CNTRL);
  950. value = tegra_sor_readl(sor, SOR_TEST);
  951. value &= ~SOR_TEST_CRC_POST_SERIALIZE;
  952. tegra_sor_writel(sor, value, SOR_TEST);
  953. err = tegra_sor_crc_wait(sor, 100);
  954. if (err < 0)
  955. goto unlock;
  956. tegra_sor_writel(sor, SOR_CRC_A_RESET, SOR_CRC_A);
  957. value = tegra_sor_readl(sor, SOR_CRC_B);
  958. num = scnprintf(buf, sizeof(buf), "%08x\n", value);
  959. err = simple_read_from_buffer(buffer, size, ppos, buf, num);
  960. unlock:
  961. mutex_unlock(&sor->lock);
  962. return err;
  963. }
  964. static const struct file_operations tegra_sor_crc_fops = {
  965. .owner = THIS_MODULE,
  966. .open = tegra_sor_crc_open,
  967. .read = tegra_sor_crc_read,
  968. .release = tegra_sor_crc_release,
  969. };
  970. static int tegra_sor_debugfs_init(struct tegra_sor *sor,
  971. struct drm_minor *minor)
  972. {
  973. struct dentry *entry;
  974. int err = 0;
  975. sor->debugfs = debugfs_create_dir("sor", minor->debugfs_root);
  976. if (!sor->debugfs)
  977. return -ENOMEM;
  978. entry = debugfs_create_file("crc", 0644, sor->debugfs, sor,
  979. &tegra_sor_crc_fops);
  980. if (!entry) {
  981. dev_err(sor->dev,
  982. "cannot create /sys/kernel/debug/dri/%s/sor/crc\n",
  983. minor->debugfs_root->d_name.name);
  984. err = -ENOMEM;
  985. goto remove;
  986. }
  987. return err;
  988. remove:
  989. debugfs_remove(sor->debugfs);
  990. sor->debugfs = NULL;
  991. return err;
  992. }
  993. static int tegra_sor_debugfs_exit(struct tegra_sor *sor)
  994. {
  995. debugfs_remove_recursive(sor->debugfs);
  996. sor->debugfs = NULL;
  997. return 0;
  998. }
  999. static int tegra_sor_init(struct host1x_client *client)
  1000. {
  1001. struct drm_device *drm = dev_get_drvdata(client->parent);
  1002. struct tegra_sor *sor = host1x_client_to_sor(client);
  1003. int err;
  1004. if (!sor->dpaux)
  1005. return -ENODEV;
  1006. sor->output.type = TEGRA_OUTPUT_EDP;
  1007. sor->output.dev = sor->dev;
  1008. sor->output.ops = &sor_ops;
  1009. err = tegra_output_init(drm, &sor->output);
  1010. if (err < 0) {
  1011. dev_err(sor->dev, "output setup failed: %d\n", err);
  1012. return err;
  1013. }
  1014. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1015. err = tegra_sor_debugfs_init(sor, drm->primary);
  1016. if (err < 0)
  1017. dev_err(sor->dev, "debugfs setup failed: %d\n", err);
  1018. }
  1019. if (sor->dpaux) {
  1020. err = tegra_dpaux_attach(sor->dpaux, &sor->output);
  1021. if (err < 0) {
  1022. dev_err(sor->dev, "failed to attach DP: %d\n", err);
  1023. return err;
  1024. }
  1025. }
  1026. return 0;
  1027. }
  1028. static int tegra_sor_exit(struct host1x_client *client)
  1029. {
  1030. struct tegra_sor *sor = host1x_client_to_sor(client);
  1031. int err;
  1032. err = tegra_output_disable(&sor->output);
  1033. if (err < 0) {
  1034. dev_err(sor->dev, "output failed to disable: %d\n", err);
  1035. return err;
  1036. }
  1037. if (sor->dpaux) {
  1038. err = tegra_dpaux_detach(sor->dpaux);
  1039. if (err < 0) {
  1040. dev_err(sor->dev, "failed to detach DP: %d\n", err);
  1041. return err;
  1042. }
  1043. }
  1044. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1045. err = tegra_sor_debugfs_exit(sor);
  1046. if (err < 0)
  1047. dev_err(sor->dev, "debugfs cleanup failed: %d\n", err);
  1048. }
  1049. err = tegra_output_exit(&sor->output);
  1050. if (err < 0) {
  1051. dev_err(sor->dev, "output cleanup failed: %d\n", err);
  1052. return err;
  1053. }
  1054. return 0;
  1055. }
  1056. static const struct host1x_client_ops sor_client_ops = {
  1057. .init = tegra_sor_init,
  1058. .exit = tegra_sor_exit,
  1059. };
  1060. static int tegra_sor_probe(struct platform_device *pdev)
  1061. {
  1062. struct device_node *np;
  1063. struct tegra_sor *sor;
  1064. struct resource *regs;
  1065. int err;
  1066. sor = devm_kzalloc(&pdev->dev, sizeof(*sor), GFP_KERNEL);
  1067. if (!sor)
  1068. return -ENOMEM;
  1069. sor->output.dev = sor->dev = &pdev->dev;
  1070. np = of_parse_phandle(pdev->dev.of_node, "nvidia,dpaux", 0);
  1071. if (np) {
  1072. sor->dpaux = tegra_dpaux_find_by_of_node(np);
  1073. of_node_put(np);
  1074. if (!sor->dpaux)
  1075. return -EPROBE_DEFER;
  1076. }
  1077. err = tegra_output_probe(&sor->output);
  1078. if (err < 0)
  1079. return err;
  1080. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1081. sor->regs = devm_ioremap_resource(&pdev->dev, regs);
  1082. if (IS_ERR(sor->regs))
  1083. return PTR_ERR(sor->regs);
  1084. sor->rst = devm_reset_control_get(&pdev->dev, "sor");
  1085. if (IS_ERR(sor->rst))
  1086. return PTR_ERR(sor->rst);
  1087. sor->clk = devm_clk_get(&pdev->dev, NULL);
  1088. if (IS_ERR(sor->clk))
  1089. return PTR_ERR(sor->clk);
  1090. sor->clk_parent = devm_clk_get(&pdev->dev, "parent");
  1091. if (IS_ERR(sor->clk_parent))
  1092. return PTR_ERR(sor->clk_parent);
  1093. err = clk_prepare_enable(sor->clk_parent);
  1094. if (err < 0)
  1095. return err;
  1096. sor->clk_safe = devm_clk_get(&pdev->dev, "safe");
  1097. if (IS_ERR(sor->clk_safe))
  1098. return PTR_ERR(sor->clk_safe);
  1099. err = clk_prepare_enable(sor->clk_safe);
  1100. if (err < 0)
  1101. return err;
  1102. sor->clk_dp = devm_clk_get(&pdev->dev, "dp");
  1103. if (IS_ERR(sor->clk_dp))
  1104. return PTR_ERR(sor->clk_dp);
  1105. err = clk_prepare_enable(sor->clk_dp);
  1106. if (err < 0)
  1107. return err;
  1108. INIT_LIST_HEAD(&sor->client.list);
  1109. sor->client.ops = &sor_client_ops;
  1110. sor->client.dev = &pdev->dev;
  1111. mutex_init(&sor->lock);
  1112. err = host1x_client_register(&sor->client);
  1113. if (err < 0) {
  1114. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  1115. err);
  1116. return err;
  1117. }
  1118. platform_set_drvdata(pdev, sor);
  1119. return 0;
  1120. }
  1121. static int tegra_sor_remove(struct platform_device *pdev)
  1122. {
  1123. struct tegra_sor *sor = platform_get_drvdata(pdev);
  1124. int err;
  1125. err = host1x_client_unregister(&sor->client);
  1126. if (err < 0) {
  1127. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  1128. err);
  1129. return err;
  1130. }
  1131. clk_disable_unprepare(sor->clk_parent);
  1132. clk_disable_unprepare(sor->clk_safe);
  1133. clk_disable_unprepare(sor->clk_dp);
  1134. clk_disable_unprepare(sor->clk);
  1135. return 0;
  1136. }
  1137. static const struct of_device_id tegra_sor_of_match[] = {
  1138. { .compatible = "nvidia,tegra124-sor", },
  1139. { },
  1140. };
  1141. MODULE_DEVICE_TABLE(of, tegra_sor_of_match);
  1142. struct platform_driver tegra_sor_driver = {
  1143. .driver = {
  1144. .name = "tegra-sor",
  1145. .of_match_table = tegra_sor_of_match,
  1146. },
  1147. .probe = tegra_sor_probe,
  1148. .remove = tegra_sor_remove,
  1149. };