sun4i_tcon.c 42 KB

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