sun4i_tcon.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /*
  2. * Copyright (C) 2015 Free Electrons
  3. * Copyright (C) 2015 NextThing Co
  4. *
  5. * Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. */
  12. #include <drm/drmP.h>
  13. #include <drm/drm_atomic_helper.h>
  14. #include <drm/drm_crtc.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include <drm/drm_encoder.h>
  17. #include <drm/drm_modes.h>
  18. #include <drm/drm_of.h>
  19. #include <drm/drm_panel.h>
  20. #include <uapi/drm/drm_mode.h>
  21. #include <linux/component.h>
  22. #include <linux/ioport.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/regmap.h>
  27. #include <linux/reset.h>
  28. #include "sun4i_crtc.h"
  29. #include "sun4i_dotclock.h"
  30. #include "sun4i_drv.h"
  31. #include "sun4i_lvds.h"
  32. #include "sun4i_rgb.h"
  33. #include "sun4i_tcon.h"
  34. #include "sun6i_mipi_dsi.h"
  35. #include "sunxi_engine.h"
  36. static struct drm_connector *sun4i_tcon_get_connector(const struct drm_encoder *encoder)
  37. {
  38. struct drm_connector *connector;
  39. struct drm_connector_list_iter iter;
  40. drm_connector_list_iter_begin(encoder->dev, &iter);
  41. drm_for_each_connector_iter(connector, &iter)
  42. if (connector->encoder == encoder) {
  43. drm_connector_list_iter_end(&iter);
  44. return connector;
  45. }
  46. drm_connector_list_iter_end(&iter);
  47. return NULL;
  48. }
  49. static int sun4i_tcon_get_pixel_depth(const struct drm_encoder *encoder)
  50. {
  51. struct drm_connector *connector;
  52. struct drm_display_info *info;
  53. connector = sun4i_tcon_get_connector(encoder);
  54. if (!connector)
  55. return -EINVAL;
  56. info = &connector->display_info;
  57. if (info->num_bus_formats != 1)
  58. return -EINVAL;
  59. switch (info->bus_formats[0]) {
  60. case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
  61. return 18;
  62. case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
  63. case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
  64. return 24;
  65. }
  66. return -EINVAL;
  67. }
  68. static void sun4i_tcon_channel_set_status(struct sun4i_tcon *tcon, int channel,
  69. bool enabled)
  70. {
  71. struct clk *clk;
  72. switch (channel) {
  73. case 0:
  74. WARN_ON(!tcon->quirks->has_channel_0);
  75. regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
  76. SUN4I_TCON0_CTL_TCON_ENABLE,
  77. enabled ? SUN4I_TCON0_CTL_TCON_ENABLE : 0);
  78. clk = tcon->dclk;
  79. break;
  80. case 1:
  81. WARN_ON(!tcon->quirks->has_channel_1);
  82. regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
  83. SUN4I_TCON1_CTL_TCON_ENABLE,
  84. enabled ? SUN4I_TCON1_CTL_TCON_ENABLE : 0);
  85. clk = tcon->sclk1;
  86. break;
  87. default:
  88. DRM_WARN("Unknown channel... doing nothing\n");
  89. return;
  90. }
  91. if (enabled) {
  92. clk_prepare_enable(clk);
  93. clk_rate_exclusive_get(clk);
  94. } else {
  95. clk_rate_exclusive_put(clk);
  96. clk_disable_unprepare(clk);
  97. }
  98. }
  99. static void sun4i_tcon_lvds_set_status(struct sun4i_tcon *tcon,
  100. const struct drm_encoder *encoder,
  101. bool enabled)
  102. {
  103. if (enabled) {
  104. u8 val;
  105. regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG,
  106. SUN4I_TCON0_LVDS_IF_EN,
  107. SUN4I_TCON0_LVDS_IF_EN);
  108. /*
  109. * As their name suggest, these values only apply to the A31
  110. * and later SoCs. We'll have to rework this when merging
  111. * support for the older SoCs.
  112. */
  113. regmap_write(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
  114. SUN6I_TCON0_LVDS_ANA0_C(2) |
  115. SUN6I_TCON0_LVDS_ANA0_V(3) |
  116. SUN6I_TCON0_LVDS_ANA0_PD(2) |
  117. SUN6I_TCON0_LVDS_ANA0_EN_LDO);
  118. udelay(2);
  119. regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
  120. SUN6I_TCON0_LVDS_ANA0_EN_MB,
  121. SUN6I_TCON0_LVDS_ANA0_EN_MB);
  122. udelay(2);
  123. regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
  124. SUN6I_TCON0_LVDS_ANA0_EN_DRVC,
  125. SUN6I_TCON0_LVDS_ANA0_EN_DRVC);
  126. if (sun4i_tcon_get_pixel_depth(encoder) == 18)
  127. val = 7;
  128. else
  129. val = 0xf;
  130. regmap_write_bits(tcon->regs, SUN4I_TCON0_LVDS_ANA0_REG,
  131. SUN6I_TCON0_LVDS_ANA0_EN_DRVD(0xf),
  132. SUN6I_TCON0_LVDS_ANA0_EN_DRVD(val));
  133. } else {
  134. regmap_update_bits(tcon->regs, SUN4I_TCON0_LVDS_IF_REG,
  135. SUN4I_TCON0_LVDS_IF_EN, 0);
  136. }
  137. }
  138. void sun4i_tcon_set_status(struct sun4i_tcon *tcon,
  139. const struct drm_encoder *encoder,
  140. bool enabled)
  141. {
  142. bool is_lvds = false;
  143. int channel;
  144. switch (encoder->encoder_type) {
  145. case DRM_MODE_ENCODER_LVDS:
  146. is_lvds = true;
  147. /* Fallthrough */
  148. case DRM_MODE_ENCODER_DSI:
  149. case DRM_MODE_ENCODER_NONE:
  150. channel = 0;
  151. break;
  152. case DRM_MODE_ENCODER_TMDS:
  153. case DRM_MODE_ENCODER_TVDAC:
  154. channel = 1;
  155. break;
  156. default:
  157. DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
  158. return;
  159. }
  160. if (is_lvds && !enabled)
  161. sun4i_tcon_lvds_set_status(tcon, encoder, false);
  162. regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
  163. SUN4I_TCON_GCTL_TCON_ENABLE,
  164. enabled ? SUN4I_TCON_GCTL_TCON_ENABLE : 0);
  165. if (is_lvds && enabled)
  166. sun4i_tcon_lvds_set_status(tcon, encoder, true);
  167. sun4i_tcon_channel_set_status(tcon, channel, enabled);
  168. }
  169. void sun4i_tcon_enable_vblank(struct sun4i_tcon *tcon, bool enable)
  170. {
  171. u32 mask, val = 0;
  172. DRM_DEBUG_DRIVER("%sabling VBLANK interrupt\n", enable ? "En" : "Dis");
  173. mask = SUN4I_TCON_GINT0_VBLANK_ENABLE(0) |
  174. SUN4I_TCON_GINT0_VBLANK_ENABLE(1) |
  175. SUN4I_TCON_GINT0_TCON0_TRI_FINISH_ENABLE;
  176. if (enable)
  177. val = mask;
  178. regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG, mask, val);
  179. }
  180. EXPORT_SYMBOL(sun4i_tcon_enable_vblank);
  181. /*
  182. * This function is a helper for TCON output muxing. The TCON output
  183. * muxing control register in earlier SoCs (without the TCON TOP block)
  184. * are located in TCON0. This helper returns a pointer to TCON0's
  185. * sun4i_tcon structure, or NULL if not found.
  186. */
  187. static struct sun4i_tcon *sun4i_get_tcon0(struct drm_device *drm)
  188. {
  189. struct sun4i_drv *drv = drm->dev_private;
  190. struct sun4i_tcon *tcon;
  191. list_for_each_entry(tcon, &drv->tcon_list, list)
  192. if (tcon->id == 0)
  193. return tcon;
  194. dev_warn(drm->dev,
  195. "TCON0 not found, display output muxing may not work\n");
  196. return NULL;
  197. }
  198. void sun4i_tcon_set_mux(struct sun4i_tcon *tcon, int channel,
  199. const struct drm_encoder *encoder)
  200. {
  201. int ret = -ENOTSUPP;
  202. if (tcon->quirks->set_mux)
  203. ret = tcon->quirks->set_mux(tcon, encoder);
  204. DRM_DEBUG_DRIVER("Muxing encoder %s to CRTC %s: %d\n",
  205. encoder->name, encoder->crtc->name, ret);
  206. }
  207. static int sun4i_tcon_get_clk_delay(const struct drm_display_mode *mode,
  208. int channel)
  209. {
  210. int delay = mode->vtotal - mode->vdisplay;
  211. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  212. delay /= 2;
  213. if (channel == 1)
  214. delay -= 2;
  215. delay = min(delay, 30);
  216. DRM_DEBUG_DRIVER("TCON %d clock delay %u\n", channel, delay);
  217. return delay;
  218. }
  219. static void sun4i_tcon0_mode_set_common(struct sun4i_tcon *tcon,
  220. const struct drm_display_mode *mode)
  221. {
  222. /* Configure the dot clock */
  223. clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
  224. /* Set the resolution */
  225. regmap_write(tcon->regs, SUN4I_TCON0_BASIC0_REG,
  226. SUN4I_TCON0_BASIC0_X(mode->crtc_hdisplay) |
  227. SUN4I_TCON0_BASIC0_Y(mode->crtc_vdisplay));
  228. }
  229. static void sun4i_tcon0_mode_set_cpu(struct sun4i_tcon *tcon,
  230. struct mipi_dsi_device *device,
  231. const struct drm_display_mode *mode)
  232. {
  233. u8 bpp = mipi_dsi_pixel_format_to_bpp(device->format);
  234. u8 lanes = device->lanes;
  235. u32 block_space, start_delay;
  236. u32 tcon_div;
  237. tcon->dclk_min_div = 4;
  238. tcon->dclk_max_div = 127;
  239. sun4i_tcon0_mode_set_common(tcon, mode);
  240. regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
  241. SUN4I_TCON0_CTL_IF_MASK,
  242. SUN4I_TCON0_CTL_IF_8080);
  243. regmap_write(tcon->regs, SUN4I_TCON_ECC_FIFO_REG,
  244. SUN4I_TCON_ECC_FIFO_EN);
  245. regmap_write(tcon->regs, SUN4I_TCON0_CPU_IF_REG,
  246. SUN4I_TCON0_CPU_IF_MODE_DSI |
  247. SUN4I_TCON0_CPU_IF_TRI_FIFO_FLUSH |
  248. SUN4I_TCON0_CPU_IF_TRI_FIFO_EN |
  249. SUN4I_TCON0_CPU_IF_TRI_EN);
  250. /*
  251. * This looks suspicious, but it works...
  252. *
  253. * The datasheet says that this should be set higher than 20 *
  254. * pixel cycle, but it's not clear what a pixel cycle is.
  255. */
  256. regmap_read(tcon->regs, SUN4I_TCON0_DCLK_REG, &tcon_div);
  257. tcon_div &= GENMASK(6, 0);
  258. block_space = mode->htotal * bpp / (tcon_div * lanes);
  259. block_space -= mode->hdisplay + 40;
  260. regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI0_REG,
  261. SUN4I_TCON0_CPU_TRI0_BLOCK_SPACE(block_space) |
  262. SUN4I_TCON0_CPU_TRI0_BLOCK_SIZE(mode->hdisplay));
  263. regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI1_REG,
  264. SUN4I_TCON0_CPU_TRI1_BLOCK_NUM(mode->vdisplay));
  265. start_delay = (mode->crtc_vtotal - mode->crtc_vdisplay - 10 - 1);
  266. start_delay = start_delay * mode->crtc_htotal * 149;
  267. start_delay = start_delay / (mode->crtc_clock / 1000) / 8;
  268. regmap_write(tcon->regs, SUN4I_TCON0_CPU_TRI2_REG,
  269. SUN4I_TCON0_CPU_TRI2_TRANS_START_SET(10) |
  270. SUN4I_TCON0_CPU_TRI2_START_DELAY(start_delay));
  271. /*
  272. * The Allwinner BSP has a comment that the period should be
  273. * the display clock * 15, but uses an hardcoded 3000...
  274. */
  275. regmap_write(tcon->regs, SUN4I_TCON_SAFE_PERIOD_REG,
  276. SUN4I_TCON_SAFE_PERIOD_NUM(3000) |
  277. SUN4I_TCON_SAFE_PERIOD_MODE(3));
  278. /* Enable the output on the pins */
  279. regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG,
  280. 0xe0000000);
  281. }
  282. static void sun4i_tcon0_mode_set_lvds(struct sun4i_tcon *tcon,
  283. const struct drm_encoder *encoder,
  284. const struct drm_display_mode *mode)
  285. {
  286. unsigned int bp;
  287. u8 clk_delay;
  288. u32 reg, val = 0;
  289. WARN_ON(!tcon->quirks->has_channel_0);
  290. tcon->dclk_min_div = 7;
  291. tcon->dclk_max_div = 7;
  292. sun4i_tcon0_mode_set_common(tcon, mode);
  293. /* Adjust clock delay */
  294. clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
  295. regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
  296. SUN4I_TCON0_CTL_CLK_DELAY_MASK,
  297. SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
  298. /*
  299. * This is called a backporch in the register documentation,
  300. * but it really is the back porch + hsync
  301. */
  302. bp = mode->crtc_htotal - mode->crtc_hsync_start;
  303. DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
  304. mode->crtc_htotal, bp);
  305. /* Set horizontal display timings */
  306. regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
  307. SUN4I_TCON0_BASIC1_H_TOTAL(mode->htotal) |
  308. SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
  309. /*
  310. * This is called a backporch in the register documentation,
  311. * but it really is the back porch + hsync
  312. */
  313. bp = mode->crtc_vtotal - mode->crtc_vsync_start;
  314. DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
  315. mode->crtc_vtotal, bp);
  316. /* Set vertical display timings */
  317. regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
  318. SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
  319. SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
  320. reg = SUN4I_TCON0_LVDS_IF_CLK_SEL_TCON0 |
  321. SUN4I_TCON0_LVDS_IF_DATA_POL_NORMAL |
  322. SUN4I_TCON0_LVDS_IF_CLK_POL_NORMAL;
  323. if (sun4i_tcon_get_pixel_depth(encoder) == 24)
  324. reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_24BITS;
  325. else
  326. reg |= SUN4I_TCON0_LVDS_IF_BITWIDTH_18BITS;
  327. regmap_write(tcon->regs, SUN4I_TCON0_LVDS_IF_REG, reg);
  328. /* Setup the polarity of the various signals */
  329. if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
  330. val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
  331. if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
  332. val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
  333. regmap_write(tcon->regs, SUN4I_TCON0_IO_POL_REG, val);
  334. /* Map output pins to channel 0 */
  335. regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
  336. SUN4I_TCON_GCTL_IOMAP_MASK,
  337. SUN4I_TCON_GCTL_IOMAP_TCON0);
  338. /* Enable the output on the pins */
  339. regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0xe0000000);
  340. }
  341. static void sun4i_tcon0_mode_set_rgb(struct sun4i_tcon *tcon,
  342. const struct drm_display_mode *mode)
  343. {
  344. struct drm_panel *panel = tcon->panel;
  345. struct drm_connector *connector = panel->connector;
  346. struct drm_display_info display_info = connector->display_info;
  347. unsigned int bp, hsync, vsync;
  348. u8 clk_delay;
  349. u32 val = 0;
  350. WARN_ON(!tcon->quirks->has_channel_0);
  351. tcon->dclk_min_div = 6;
  352. tcon->dclk_max_div = 127;
  353. sun4i_tcon0_mode_set_common(tcon, mode);
  354. /* Adjust clock delay */
  355. clk_delay = sun4i_tcon_get_clk_delay(mode, 0);
  356. regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
  357. SUN4I_TCON0_CTL_CLK_DELAY_MASK,
  358. SUN4I_TCON0_CTL_CLK_DELAY(clk_delay));
  359. /*
  360. * This is called a backporch in the register documentation,
  361. * but it really is the back porch + hsync
  362. */
  363. bp = mode->crtc_htotal - mode->crtc_hsync_start;
  364. DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
  365. mode->crtc_htotal, bp);
  366. /* Set horizontal display timings */
  367. regmap_write(tcon->regs, SUN4I_TCON0_BASIC1_REG,
  368. SUN4I_TCON0_BASIC1_H_TOTAL(mode->crtc_htotal) |
  369. SUN4I_TCON0_BASIC1_H_BACKPORCH(bp));
  370. /*
  371. * This is called a backporch in the register documentation,
  372. * but it really is the back porch + hsync
  373. */
  374. bp = mode->crtc_vtotal - mode->crtc_vsync_start;
  375. DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
  376. mode->crtc_vtotal, bp);
  377. /* Set vertical display timings */
  378. regmap_write(tcon->regs, SUN4I_TCON0_BASIC2_REG,
  379. SUN4I_TCON0_BASIC2_V_TOTAL(mode->crtc_vtotal * 2) |
  380. SUN4I_TCON0_BASIC2_V_BACKPORCH(bp));
  381. /* Set Hsync and Vsync length */
  382. hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
  383. vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
  384. DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
  385. regmap_write(tcon->regs, SUN4I_TCON0_BASIC3_REG,
  386. SUN4I_TCON0_BASIC3_V_SYNC(vsync) |
  387. SUN4I_TCON0_BASIC3_H_SYNC(hsync));
  388. /* Setup the polarity of the various signals */
  389. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  390. val |= SUN4I_TCON0_IO_POL_HSYNC_POSITIVE;
  391. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  392. val |= SUN4I_TCON0_IO_POL_VSYNC_POSITIVE;
  393. /*
  394. * On A20 and similar SoCs, the only way to achieve Positive Edge
  395. * (Rising Edge), is setting dclk clock phase to 2/3(240°).
  396. * By default TCON works in Negative Edge(Falling Edge),
  397. * this is why phase is set to 0 in that case.
  398. * Unfortunately there's no way to logically invert dclk through
  399. * IO_POL register.
  400. * The only acceptable way to work, triple checked with scope,
  401. * is using clock phase set to 0° for Negative Edge and set to 240°
  402. * for Positive Edge.
  403. * On A33 and similar SoCs there would be a 90° phase option,
  404. * but it divides also dclk by 2.
  405. * Following code is a way to avoid quirks all around TCON
  406. * and DOTCLOCK drivers.
  407. */
  408. if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE)
  409. clk_set_phase(tcon->dclk, 240);
  410. if (display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
  411. clk_set_phase(tcon->dclk, 0);
  412. regmap_update_bits(tcon->regs, SUN4I_TCON0_IO_POL_REG,
  413. SUN4I_TCON0_IO_POL_HSYNC_POSITIVE | SUN4I_TCON0_IO_POL_VSYNC_POSITIVE,
  414. val);
  415. /* Map output pins to channel 0 */
  416. regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
  417. SUN4I_TCON_GCTL_IOMAP_MASK,
  418. SUN4I_TCON_GCTL_IOMAP_TCON0);
  419. /* Enable the output on the pins */
  420. regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, 0);
  421. }
  422. static void sun4i_tcon1_mode_set(struct sun4i_tcon *tcon,
  423. const struct drm_display_mode *mode)
  424. {
  425. unsigned int bp, hsync, vsync, vtotal;
  426. u8 clk_delay;
  427. u32 val;
  428. WARN_ON(!tcon->quirks->has_channel_1);
  429. /* Configure the dot clock */
  430. clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
  431. /* Adjust clock delay */
  432. clk_delay = sun4i_tcon_get_clk_delay(mode, 1);
  433. regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
  434. SUN4I_TCON1_CTL_CLK_DELAY_MASK,
  435. SUN4I_TCON1_CTL_CLK_DELAY(clk_delay));
  436. /* Set interlaced mode */
  437. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  438. val = SUN4I_TCON1_CTL_INTERLACE_ENABLE;
  439. else
  440. val = 0;
  441. regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
  442. SUN4I_TCON1_CTL_INTERLACE_ENABLE,
  443. val);
  444. /* Set the input resolution */
  445. regmap_write(tcon->regs, SUN4I_TCON1_BASIC0_REG,
  446. SUN4I_TCON1_BASIC0_X(mode->crtc_hdisplay) |
  447. SUN4I_TCON1_BASIC0_Y(mode->crtc_vdisplay));
  448. /* Set the upscaling resolution */
  449. regmap_write(tcon->regs, SUN4I_TCON1_BASIC1_REG,
  450. SUN4I_TCON1_BASIC1_X(mode->crtc_hdisplay) |
  451. SUN4I_TCON1_BASIC1_Y(mode->crtc_vdisplay));
  452. /* Set the output resolution */
  453. regmap_write(tcon->regs, SUN4I_TCON1_BASIC2_REG,
  454. SUN4I_TCON1_BASIC2_X(mode->crtc_hdisplay) |
  455. SUN4I_TCON1_BASIC2_Y(mode->crtc_vdisplay));
  456. /* Set horizontal display timings */
  457. bp = mode->crtc_htotal - mode->crtc_hsync_start;
  458. DRM_DEBUG_DRIVER("Setting horizontal total %d, backporch %d\n",
  459. mode->htotal, bp);
  460. regmap_write(tcon->regs, SUN4I_TCON1_BASIC3_REG,
  461. SUN4I_TCON1_BASIC3_H_TOTAL(mode->crtc_htotal) |
  462. SUN4I_TCON1_BASIC3_H_BACKPORCH(bp));
  463. bp = mode->crtc_vtotal - mode->crtc_vsync_start;
  464. DRM_DEBUG_DRIVER("Setting vertical total %d, backporch %d\n",
  465. mode->crtc_vtotal, bp);
  466. /*
  467. * The vertical resolution needs to be doubled in all
  468. * cases. We could use crtc_vtotal and always multiply by two,
  469. * but that leads to a rounding error in interlace when vtotal
  470. * is odd.
  471. *
  472. * This happens with TV's PAL for example, where vtotal will
  473. * be 625, crtc_vtotal 312, and thus crtc_vtotal * 2 will be
  474. * 624, which apparently confuses the hardware.
  475. *
  476. * To work around this, we will always use vtotal, and
  477. * multiply by two only if we're not in interlace.
  478. */
  479. vtotal = mode->vtotal;
  480. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  481. vtotal = vtotal * 2;
  482. /* Set vertical display timings */
  483. regmap_write(tcon->regs, SUN4I_TCON1_BASIC4_REG,
  484. SUN4I_TCON1_BASIC4_V_TOTAL(vtotal) |
  485. SUN4I_TCON1_BASIC4_V_BACKPORCH(bp));
  486. /* Set Hsync and Vsync length */
  487. hsync = mode->crtc_hsync_end - mode->crtc_hsync_start;
  488. vsync = mode->crtc_vsync_end - mode->crtc_vsync_start;
  489. DRM_DEBUG_DRIVER("Setting HSYNC %d, VSYNC %d\n", hsync, vsync);
  490. regmap_write(tcon->regs, SUN4I_TCON1_BASIC5_REG,
  491. SUN4I_TCON1_BASIC5_V_SYNC(vsync) |
  492. SUN4I_TCON1_BASIC5_H_SYNC(hsync));
  493. /* Map output pins to channel 1 */
  494. regmap_update_bits(tcon->regs, SUN4I_TCON_GCTL_REG,
  495. SUN4I_TCON_GCTL_IOMAP_MASK,
  496. SUN4I_TCON_GCTL_IOMAP_TCON1);
  497. }
  498. void sun4i_tcon_mode_set(struct sun4i_tcon *tcon,
  499. const struct drm_encoder *encoder,
  500. const struct drm_display_mode *mode)
  501. {
  502. struct sun6i_dsi *dsi;
  503. switch (encoder->encoder_type) {
  504. case DRM_MODE_ENCODER_DSI:
  505. /*
  506. * This is not really elegant, but it's the "cleaner"
  507. * way I could think of...
  508. */
  509. dsi = encoder_to_sun6i_dsi(encoder);
  510. sun4i_tcon0_mode_set_cpu(tcon, dsi->device, mode);
  511. break;
  512. case DRM_MODE_ENCODER_LVDS:
  513. sun4i_tcon0_mode_set_lvds(tcon, encoder, mode);
  514. break;
  515. case DRM_MODE_ENCODER_NONE:
  516. sun4i_tcon0_mode_set_rgb(tcon, mode);
  517. sun4i_tcon_set_mux(tcon, 0, encoder);
  518. break;
  519. case DRM_MODE_ENCODER_TVDAC:
  520. case DRM_MODE_ENCODER_TMDS:
  521. sun4i_tcon1_mode_set(tcon, mode);
  522. sun4i_tcon_set_mux(tcon, 1, encoder);
  523. break;
  524. default:
  525. DRM_DEBUG_DRIVER("Unknown encoder type, doing nothing...\n");
  526. }
  527. }
  528. EXPORT_SYMBOL(sun4i_tcon_mode_set);
  529. static void sun4i_tcon_finish_page_flip(struct drm_device *dev,
  530. struct sun4i_crtc *scrtc)
  531. {
  532. unsigned long flags;
  533. spin_lock_irqsave(&dev->event_lock, flags);
  534. if (scrtc->event) {
  535. drm_crtc_send_vblank_event(&scrtc->crtc, scrtc->event);
  536. drm_crtc_vblank_put(&scrtc->crtc);
  537. scrtc->event = NULL;
  538. }
  539. spin_unlock_irqrestore(&dev->event_lock, flags);
  540. }
  541. static irqreturn_t sun4i_tcon_handler(int irq, void *private)
  542. {
  543. struct sun4i_tcon *tcon = private;
  544. struct drm_device *drm = tcon->drm;
  545. struct sun4i_crtc *scrtc = tcon->crtc;
  546. struct sunxi_engine *engine = scrtc->engine;
  547. unsigned int status;
  548. regmap_read(tcon->regs, SUN4I_TCON_GINT0_REG, &status);
  549. if (!(status & (SUN4I_TCON_GINT0_VBLANK_INT(0) |
  550. SUN4I_TCON_GINT0_VBLANK_INT(1) |
  551. SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT)))
  552. return IRQ_NONE;
  553. drm_crtc_handle_vblank(&scrtc->crtc);
  554. sun4i_tcon_finish_page_flip(drm, scrtc);
  555. /* Acknowledge the interrupt */
  556. regmap_update_bits(tcon->regs, SUN4I_TCON_GINT0_REG,
  557. SUN4I_TCON_GINT0_VBLANK_INT(0) |
  558. SUN4I_TCON_GINT0_VBLANK_INT(1) |
  559. SUN4I_TCON_GINT0_TCON0_TRI_FINISH_INT,
  560. 0);
  561. if (engine->ops->vblank_quirk)
  562. engine->ops->vblank_quirk(engine);
  563. return IRQ_HANDLED;
  564. }
  565. static int sun4i_tcon_init_clocks(struct device *dev,
  566. struct sun4i_tcon *tcon)
  567. {
  568. tcon->clk = devm_clk_get(dev, "ahb");
  569. if (IS_ERR(tcon->clk)) {
  570. dev_err(dev, "Couldn't get the TCON bus clock\n");
  571. return PTR_ERR(tcon->clk);
  572. }
  573. clk_prepare_enable(tcon->clk);
  574. if (tcon->quirks->has_channel_0) {
  575. tcon->sclk0 = devm_clk_get(dev, "tcon-ch0");
  576. if (IS_ERR(tcon->sclk0)) {
  577. dev_err(dev, "Couldn't get the TCON channel 0 clock\n");
  578. return PTR_ERR(tcon->sclk0);
  579. }
  580. }
  581. if (tcon->quirks->has_channel_1) {
  582. tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
  583. if (IS_ERR(tcon->sclk1)) {
  584. dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
  585. return PTR_ERR(tcon->sclk1);
  586. }
  587. }
  588. return 0;
  589. }
  590. static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
  591. {
  592. clk_disable_unprepare(tcon->clk);
  593. }
  594. static int sun4i_tcon_init_irq(struct device *dev,
  595. struct sun4i_tcon *tcon)
  596. {
  597. struct platform_device *pdev = to_platform_device(dev);
  598. int irq, ret;
  599. irq = platform_get_irq(pdev, 0);
  600. if (irq < 0) {
  601. dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
  602. return irq;
  603. }
  604. ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
  605. dev_name(dev), tcon);
  606. if (ret) {
  607. dev_err(dev, "Couldn't request the IRQ\n");
  608. return ret;
  609. }
  610. return 0;
  611. }
  612. static struct regmap_config sun4i_tcon_regmap_config = {
  613. .reg_bits = 32,
  614. .val_bits = 32,
  615. .reg_stride = 4,
  616. .max_register = 0x800,
  617. };
  618. static int sun4i_tcon_init_regmap(struct device *dev,
  619. struct sun4i_tcon *tcon)
  620. {
  621. struct platform_device *pdev = to_platform_device(dev);
  622. struct resource *res;
  623. void __iomem *regs;
  624. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  625. regs = devm_ioremap_resource(dev, res);
  626. if (IS_ERR(regs))
  627. return PTR_ERR(regs);
  628. tcon->regs = devm_regmap_init_mmio(dev, regs,
  629. &sun4i_tcon_regmap_config);
  630. if (IS_ERR(tcon->regs)) {
  631. dev_err(dev, "Couldn't create the TCON regmap\n");
  632. return PTR_ERR(tcon->regs);
  633. }
  634. /* Make sure the TCON is disabled and all IRQs are off */
  635. regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
  636. regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
  637. regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
  638. /* Disable IO lines and set them to tristate */
  639. regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
  640. regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
  641. return 0;
  642. }
  643. /*
  644. * On SoCs with the old display pipeline design (Display Engine 1.0),
  645. * the TCON is always tied to just one backend. Hence we can traverse
  646. * the of_graph upwards to find the backend our tcon is connected to,
  647. * and take its ID as our own.
  648. *
  649. * We can either identify backends from their compatible strings, which
  650. * means maintaining a large list of them. Or, since the backend is
  651. * registered and binded before the TCON, we can just go through the
  652. * list of registered backends and compare the device node.
  653. *
  654. * As the structures now store engines instead of backends, here this
  655. * function in fact searches the corresponding engine, and the ID is
  656. * requested via the get_id function of the engine.
  657. */
  658. static struct sunxi_engine *
  659. sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv,
  660. struct device_node *node)
  661. {
  662. struct device_node *port, *ep, *remote;
  663. struct sunxi_engine *engine = ERR_PTR(-EINVAL);
  664. port = of_graph_get_port_by_id(node, 0);
  665. if (!port)
  666. return ERR_PTR(-EINVAL);
  667. /*
  668. * This only works if there is only one path from the TCON
  669. * to any display engine. Otherwise the probe order of the
  670. * TCONs and display engines is not guaranteed. They may
  671. * either bind to the wrong one, or worse, bind to the same
  672. * one if additional checks are not done.
  673. *
  674. * Bail out if there are multiple input connections.
  675. */
  676. if (of_get_available_child_count(port) != 1)
  677. goto out_put_port;
  678. /* Get the first connection without specifying an ID */
  679. ep = of_get_next_available_child(port, NULL);
  680. if (!ep)
  681. goto out_put_port;
  682. remote = of_graph_get_remote_port_parent(ep);
  683. if (!remote)
  684. goto out_put_ep;
  685. /* does this node match any registered engines? */
  686. list_for_each_entry(engine, &drv->engine_list, list)
  687. if (remote == engine->node)
  688. goto out_put_remote;
  689. /* keep looking through upstream ports */
  690. engine = sun4i_tcon_find_engine_traverse(drv, remote);
  691. out_put_remote:
  692. of_node_put(remote);
  693. out_put_ep:
  694. of_node_put(ep);
  695. out_put_port:
  696. of_node_put(port);
  697. return engine;
  698. }
  699. /*
  700. * The device tree binding says that the remote endpoint ID of any
  701. * connection between components, up to and including the TCON, of
  702. * the display pipeline should be equal to the actual ID of the local
  703. * component. Thus we can look at any one of the input connections of
  704. * the TCONs, and use that connection's remote endpoint ID as our own.
  705. *
  706. * Since the user of this function already finds the input port,
  707. * the port is passed in directly without further checks.
  708. */
  709. static int sun4i_tcon_of_get_id_from_port(struct device_node *port)
  710. {
  711. struct device_node *ep;
  712. int ret = -EINVAL;
  713. /* try finding an upstream endpoint */
  714. for_each_available_child_of_node(port, ep) {
  715. struct device_node *remote;
  716. u32 reg;
  717. remote = of_graph_get_remote_endpoint(ep);
  718. if (!remote)
  719. continue;
  720. ret = of_property_read_u32(remote, "reg", &reg);
  721. if (ret)
  722. continue;
  723. ret = reg;
  724. }
  725. return ret;
  726. }
  727. /*
  728. * Once we know the TCON's id, we can look through the list of
  729. * engines to find a matching one. We assume all engines have
  730. * been probed and added to the list.
  731. */
  732. static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv,
  733. int id)
  734. {
  735. struct sunxi_engine *engine;
  736. list_for_each_entry(engine, &drv->engine_list, list)
  737. if (engine->id == id)
  738. return engine;
  739. return ERR_PTR(-EINVAL);
  740. }
  741. /*
  742. * On SoCs with the old display pipeline design (Display Engine 1.0),
  743. * we assumed the TCON was always tied to just one backend. However
  744. * this proved not to be the case. On the A31, the TCON can select
  745. * either backend as its source. On the A20 (and likely on the A10),
  746. * the backend can choose which TCON to output to.
  747. *
  748. * The device tree binding says that the remote endpoint ID of any
  749. * connection between components, up to and including the TCON, of
  750. * the display pipeline should be equal to the actual ID of the local
  751. * component. Thus we should be able to look at any one of the input
  752. * connections of the TCONs, and use that connection's remote endpoint
  753. * ID as our own.
  754. *
  755. * However the connections between the backend and TCON were assumed
  756. * to be always singular, and their endpoit IDs were all incorrectly
  757. * set to 0. This means for these old device trees, we cannot just look
  758. * up the remote endpoint ID of a TCON input endpoint. TCON1 would be
  759. * incorrectly identified as TCON0.
  760. *
  761. * This function first checks if the TCON node has 2 input endpoints.
  762. * If so, then the device tree is a corrected version, and it will use
  763. * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above
  764. * to fetch the ID and engine directly. If not, then it is likely an
  765. * old device trees, where the endpoint IDs were incorrect, but did not
  766. * have endpoint connections between the backend and TCON across
  767. * different display pipelines. It will fall back to the old method of
  768. * traversing the of_graph to try and find a matching engine by device
  769. * node.
  770. *
  771. * In the case of single display pipeline device trees, either method
  772. * works.
  773. */
  774. static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv,
  775. struct device_node *node)
  776. {
  777. struct device_node *port;
  778. struct sunxi_engine *engine;
  779. port = of_graph_get_port_by_id(node, 0);
  780. if (!port)
  781. return ERR_PTR(-EINVAL);
  782. /*
  783. * Is this a corrected device tree with cross pipeline
  784. * connections between the backend and TCON?
  785. */
  786. if (of_get_child_count(port) > 1) {
  787. /* Get our ID directly from an upstream endpoint */
  788. int id = sun4i_tcon_of_get_id_from_port(port);
  789. /* Get our engine by matching our ID */
  790. engine = sun4i_tcon_get_engine_by_id(drv, id);
  791. of_node_put(port);
  792. return engine;
  793. }
  794. /* Fallback to old method by traversing input endpoints */
  795. of_node_put(port);
  796. return sun4i_tcon_find_engine_traverse(drv, node);
  797. }
  798. static int sun4i_tcon_bind(struct device *dev, struct device *master,
  799. void *data)
  800. {
  801. struct drm_device *drm = data;
  802. struct sun4i_drv *drv = drm->dev_private;
  803. struct sunxi_engine *engine;
  804. struct device_node *remote;
  805. struct sun4i_tcon *tcon;
  806. struct reset_control *edp_rstc;
  807. bool has_lvds_rst, has_lvds_alt, can_lvds;
  808. int ret;
  809. engine = sun4i_tcon_find_engine(drv, dev->of_node);
  810. if (IS_ERR(engine)) {
  811. dev_err(dev, "Couldn't find matching engine\n");
  812. return -EPROBE_DEFER;
  813. }
  814. tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
  815. if (!tcon)
  816. return -ENOMEM;
  817. dev_set_drvdata(dev, tcon);
  818. tcon->drm = drm;
  819. tcon->dev = dev;
  820. tcon->id = engine->id;
  821. tcon->quirks = of_device_get_match_data(dev);
  822. tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
  823. if (IS_ERR(tcon->lcd_rst)) {
  824. dev_err(dev, "Couldn't get our reset line\n");
  825. return PTR_ERR(tcon->lcd_rst);
  826. }
  827. if (tcon->quirks->needs_edp_reset) {
  828. edp_rstc = devm_reset_control_get_shared(dev, "edp");
  829. if (IS_ERR(edp_rstc)) {
  830. dev_err(dev, "Couldn't get edp reset line\n");
  831. return PTR_ERR(edp_rstc);
  832. }
  833. ret = reset_control_deassert(edp_rstc);
  834. if (ret) {
  835. dev_err(dev, "Couldn't deassert edp reset line\n");
  836. return ret;
  837. }
  838. }
  839. /* Make sure our TCON is reset */
  840. ret = reset_control_reset(tcon->lcd_rst);
  841. if (ret) {
  842. dev_err(dev, "Couldn't deassert our reset line\n");
  843. return ret;
  844. }
  845. if (tcon->quirks->supports_lvds) {
  846. /*
  847. * This can only be made optional since we've had DT
  848. * nodes without the LVDS reset properties.
  849. *
  850. * If the property is missing, just disable LVDS, and
  851. * print a warning.
  852. */
  853. tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds");
  854. if (IS_ERR(tcon->lvds_rst)) {
  855. dev_err(dev, "Couldn't get our reset line\n");
  856. return PTR_ERR(tcon->lvds_rst);
  857. } else if (tcon->lvds_rst) {
  858. has_lvds_rst = true;
  859. reset_control_reset(tcon->lvds_rst);
  860. } else {
  861. has_lvds_rst = false;
  862. }
  863. /*
  864. * This can only be made optional since we've had DT
  865. * nodes without the LVDS reset properties.
  866. *
  867. * If the property is missing, just disable LVDS, and
  868. * print a warning.
  869. */
  870. if (tcon->quirks->has_lvds_alt) {
  871. tcon->lvds_pll = devm_clk_get(dev, "lvds-alt");
  872. if (IS_ERR(tcon->lvds_pll)) {
  873. if (PTR_ERR(tcon->lvds_pll) == -ENOENT) {
  874. has_lvds_alt = false;
  875. } else {
  876. dev_err(dev, "Couldn't get the LVDS PLL\n");
  877. return PTR_ERR(tcon->lvds_pll);
  878. }
  879. } else {
  880. has_lvds_alt = true;
  881. }
  882. }
  883. if (!has_lvds_rst ||
  884. (tcon->quirks->has_lvds_alt && !has_lvds_alt)) {
  885. dev_warn(dev, "Missing LVDS properties, Please upgrade your DT\n");
  886. dev_warn(dev, "LVDS output disabled\n");
  887. can_lvds = false;
  888. } else {
  889. can_lvds = true;
  890. }
  891. } else {
  892. can_lvds = false;
  893. }
  894. ret = sun4i_tcon_init_clocks(dev, tcon);
  895. if (ret) {
  896. dev_err(dev, "Couldn't init our TCON clocks\n");
  897. goto err_assert_reset;
  898. }
  899. ret = sun4i_tcon_init_regmap(dev, tcon);
  900. if (ret) {
  901. dev_err(dev, "Couldn't init our TCON regmap\n");
  902. goto err_free_clocks;
  903. }
  904. if (tcon->quirks->has_channel_0) {
  905. ret = sun4i_dclk_create(dev, tcon);
  906. if (ret) {
  907. dev_err(dev, "Couldn't create our TCON dot clock\n");
  908. goto err_free_clocks;
  909. }
  910. }
  911. ret = sun4i_tcon_init_irq(dev, tcon);
  912. if (ret) {
  913. dev_err(dev, "Couldn't init our TCON interrupts\n");
  914. goto err_free_dotclock;
  915. }
  916. tcon->crtc = sun4i_crtc_init(drm, engine, tcon);
  917. if (IS_ERR(tcon->crtc)) {
  918. dev_err(dev, "Couldn't create our CRTC\n");
  919. ret = PTR_ERR(tcon->crtc);
  920. goto err_free_dotclock;
  921. }
  922. /*
  923. * If we have an LVDS panel connected to the TCON, we should
  924. * just probe the LVDS connector. Otherwise, just probe RGB as
  925. * we used to.
  926. */
  927. remote = of_graph_get_remote_node(dev->of_node, 1, 0);
  928. if (of_device_is_compatible(remote, "panel-lvds"))
  929. if (can_lvds)
  930. ret = sun4i_lvds_init(drm, tcon);
  931. else
  932. ret = -EINVAL;
  933. else
  934. ret = sun4i_rgb_init(drm, tcon);
  935. of_node_put(remote);
  936. if (ret < 0)
  937. goto err_free_dotclock;
  938. if (tcon->quirks->needs_de_be_mux) {
  939. /*
  940. * We assume there is no dynamic muxing of backends
  941. * and TCONs, so we select the backend with same ID.
  942. *
  943. * While dynamic selection might be interesting, since
  944. * the CRTC is tied to the TCON, while the layers are
  945. * tied to the backends, this means, we will need to
  946. * switch between groups of layers. There might not be
  947. * a way to represent this constraint in DRM.
  948. */
  949. regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
  950. SUN4I_TCON0_CTL_SRC_SEL_MASK,
  951. tcon->id);
  952. regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
  953. SUN4I_TCON1_CTL_SRC_SEL_MASK,
  954. tcon->id);
  955. }
  956. list_add_tail(&tcon->list, &drv->tcon_list);
  957. return 0;
  958. err_free_dotclock:
  959. if (tcon->quirks->has_channel_0)
  960. sun4i_dclk_free(tcon);
  961. err_free_clocks:
  962. sun4i_tcon_free_clocks(tcon);
  963. err_assert_reset:
  964. reset_control_assert(tcon->lcd_rst);
  965. return ret;
  966. }
  967. static void sun4i_tcon_unbind(struct device *dev, struct device *master,
  968. void *data)
  969. {
  970. struct sun4i_tcon *tcon = dev_get_drvdata(dev);
  971. list_del(&tcon->list);
  972. if (tcon->quirks->has_channel_0)
  973. sun4i_dclk_free(tcon);
  974. sun4i_tcon_free_clocks(tcon);
  975. }
  976. static const struct component_ops sun4i_tcon_ops = {
  977. .bind = sun4i_tcon_bind,
  978. .unbind = sun4i_tcon_unbind,
  979. };
  980. static int sun4i_tcon_probe(struct platform_device *pdev)
  981. {
  982. struct device_node *node = pdev->dev.of_node;
  983. struct drm_bridge *bridge;
  984. struct drm_panel *panel;
  985. int ret;
  986. ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
  987. if (ret == -EPROBE_DEFER)
  988. return ret;
  989. return component_add(&pdev->dev, &sun4i_tcon_ops);
  990. }
  991. static int sun4i_tcon_remove(struct platform_device *pdev)
  992. {
  993. component_del(&pdev->dev, &sun4i_tcon_ops);
  994. return 0;
  995. }
  996. /* platform specific TCON muxing callbacks */
  997. static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon,
  998. const struct drm_encoder *encoder)
  999. {
  1000. struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
  1001. u32 shift;
  1002. if (!tcon0)
  1003. return -EINVAL;
  1004. switch (encoder->encoder_type) {
  1005. case DRM_MODE_ENCODER_TMDS:
  1006. /* HDMI */
  1007. shift = 8;
  1008. break;
  1009. default:
  1010. return -EINVAL;
  1011. }
  1012. regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
  1013. 0x3 << shift, tcon->id << shift);
  1014. return 0;
  1015. }
  1016. static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon,
  1017. const struct drm_encoder *encoder)
  1018. {
  1019. u32 val;
  1020. if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
  1021. val = 1;
  1022. else
  1023. val = 0;
  1024. /*
  1025. * FIXME: Undocumented bits
  1026. */
  1027. return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val);
  1028. }
  1029. static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon,
  1030. const struct drm_encoder *encoder)
  1031. {
  1032. struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
  1033. u32 shift;
  1034. if (!tcon0)
  1035. return -EINVAL;
  1036. switch (encoder->encoder_type) {
  1037. case DRM_MODE_ENCODER_TMDS:
  1038. /* HDMI */
  1039. shift = 8;
  1040. break;
  1041. default:
  1042. /* TODO A31 has MIPI DSI but A31s does not */
  1043. return -EINVAL;
  1044. }
  1045. regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
  1046. 0x3 << shift, tcon->id << shift);
  1047. return 0;
  1048. }
  1049. static const struct sun4i_tcon_quirks sun4i_a10_quirks = {
  1050. .has_channel_0 = true,
  1051. .has_channel_1 = true,
  1052. .set_mux = sun4i_a10_tcon_set_mux,
  1053. };
  1054. static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
  1055. .has_channel_0 = true,
  1056. .has_channel_1 = true,
  1057. .set_mux = sun5i_a13_tcon_set_mux,
  1058. };
  1059. static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
  1060. .has_channel_0 = true,
  1061. .has_channel_1 = true,
  1062. .has_lvds_alt = true,
  1063. .needs_de_be_mux = true,
  1064. .set_mux = sun6i_tcon_set_mux,
  1065. };
  1066. static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
  1067. .has_channel_0 = true,
  1068. .has_channel_1 = true,
  1069. .needs_de_be_mux = true,
  1070. };
  1071. static const struct sun4i_tcon_quirks sun7i_a20_quirks = {
  1072. .has_channel_0 = true,
  1073. .has_channel_1 = true,
  1074. /* Same display pipeline structure as A10 */
  1075. .set_mux = sun4i_a10_tcon_set_mux,
  1076. };
  1077. static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
  1078. .has_channel_0 = true,
  1079. .has_lvds_alt = true,
  1080. };
  1081. static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = {
  1082. .supports_lvds = true,
  1083. .has_channel_0 = true,
  1084. };
  1085. static const struct sun4i_tcon_quirks sun8i_a83t_tv_quirks = {
  1086. .has_channel_1 = true,
  1087. };
  1088. static const struct sun4i_tcon_quirks sun8i_v3s_quirks = {
  1089. .has_channel_0 = true,
  1090. };
  1091. static const struct sun4i_tcon_quirks sun9i_a80_tcon_lcd_quirks = {
  1092. .has_channel_0 = true,
  1093. .needs_edp_reset = true,
  1094. };
  1095. static const struct sun4i_tcon_quirks sun9i_a80_tcon_tv_quirks = {
  1096. .has_channel_1 = true,
  1097. .needs_edp_reset = true,
  1098. };
  1099. /* sun4i_drv uses this list to check if a device node is a TCON */
  1100. const struct of_device_id sun4i_tcon_of_table[] = {
  1101. { .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks },
  1102. { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
  1103. { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
  1104. { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
  1105. { .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks },
  1106. { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
  1107. { .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks },
  1108. { .compatible = "allwinner,sun8i-a83t-tcon-tv", .data = &sun8i_a83t_tv_quirks },
  1109. { .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks },
  1110. { .compatible = "allwinner,sun9i-a80-tcon-lcd", .data = &sun9i_a80_tcon_lcd_quirks },
  1111. { .compatible = "allwinner,sun9i-a80-tcon-tv", .data = &sun9i_a80_tcon_tv_quirks },
  1112. { }
  1113. };
  1114. MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
  1115. EXPORT_SYMBOL(sun4i_tcon_of_table);
  1116. static struct platform_driver sun4i_tcon_platform_driver = {
  1117. .probe = sun4i_tcon_probe,
  1118. .remove = sun4i_tcon_remove,
  1119. .driver = {
  1120. .name = "sun4i-tcon",
  1121. .of_match_table = sun4i_tcon_of_table,
  1122. },
  1123. };
  1124. module_platform_driver(sun4i_tcon_platform_driver);
  1125. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  1126. MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
  1127. MODULE_LICENSE("GPL");