sun4i_tcon.c 37 KB

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