sor.c 31 KB

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