sun4i_tcon.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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 = tcon->quirks->dclk_min_div;
  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. clk_prepare_enable(tcon->sclk0);
  559. if (tcon->quirks->has_channel_1) {
  560. tcon->sclk1 = devm_clk_get(dev, "tcon-ch1");
  561. if (IS_ERR(tcon->sclk1)) {
  562. dev_err(dev, "Couldn't get the TCON channel 1 clock\n");
  563. return PTR_ERR(tcon->sclk1);
  564. }
  565. }
  566. return 0;
  567. }
  568. static void sun4i_tcon_free_clocks(struct sun4i_tcon *tcon)
  569. {
  570. clk_disable_unprepare(tcon->sclk0);
  571. clk_disable_unprepare(tcon->clk);
  572. }
  573. static int sun4i_tcon_init_irq(struct device *dev,
  574. struct sun4i_tcon *tcon)
  575. {
  576. struct platform_device *pdev = to_platform_device(dev);
  577. int irq, ret;
  578. irq = platform_get_irq(pdev, 0);
  579. if (irq < 0) {
  580. dev_err(dev, "Couldn't retrieve the TCON interrupt\n");
  581. return irq;
  582. }
  583. ret = devm_request_irq(dev, irq, sun4i_tcon_handler, 0,
  584. dev_name(dev), tcon);
  585. if (ret) {
  586. dev_err(dev, "Couldn't request the IRQ\n");
  587. return ret;
  588. }
  589. return 0;
  590. }
  591. static struct regmap_config sun4i_tcon_regmap_config = {
  592. .reg_bits = 32,
  593. .val_bits = 32,
  594. .reg_stride = 4,
  595. .max_register = 0x800,
  596. };
  597. static int sun4i_tcon_init_regmap(struct device *dev,
  598. struct sun4i_tcon *tcon)
  599. {
  600. struct platform_device *pdev = to_platform_device(dev);
  601. struct resource *res;
  602. void __iomem *regs;
  603. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  604. regs = devm_ioremap_resource(dev, res);
  605. if (IS_ERR(regs))
  606. return PTR_ERR(regs);
  607. tcon->regs = devm_regmap_init_mmio(dev, regs,
  608. &sun4i_tcon_regmap_config);
  609. if (IS_ERR(tcon->regs)) {
  610. dev_err(dev, "Couldn't create the TCON regmap\n");
  611. return PTR_ERR(tcon->regs);
  612. }
  613. /* Make sure the TCON is disabled and all IRQs are off */
  614. regmap_write(tcon->regs, SUN4I_TCON_GCTL_REG, 0);
  615. regmap_write(tcon->regs, SUN4I_TCON_GINT0_REG, 0);
  616. regmap_write(tcon->regs, SUN4I_TCON_GINT1_REG, 0);
  617. /* Disable IO lines and set them to tristate */
  618. regmap_write(tcon->regs, SUN4I_TCON0_IO_TRI_REG, ~0);
  619. regmap_write(tcon->regs, SUN4I_TCON1_IO_TRI_REG, ~0);
  620. return 0;
  621. }
  622. /*
  623. * On SoCs with the old display pipeline design (Display Engine 1.0),
  624. * the TCON is always tied to just one backend. Hence we can traverse
  625. * the of_graph upwards to find the backend our tcon is connected to,
  626. * and take its ID as our own.
  627. *
  628. * We can either identify backends from their compatible strings, which
  629. * means maintaining a large list of them. Or, since the backend is
  630. * registered and binded before the TCON, we can just go through the
  631. * list of registered backends and compare the device node.
  632. *
  633. * As the structures now store engines instead of backends, here this
  634. * function in fact searches the corresponding engine, and the ID is
  635. * requested via the get_id function of the engine.
  636. */
  637. static struct sunxi_engine *
  638. sun4i_tcon_find_engine_traverse(struct sun4i_drv *drv,
  639. struct device_node *node,
  640. u32 port_id)
  641. {
  642. struct device_node *port, *ep, *remote;
  643. struct sunxi_engine *engine = ERR_PTR(-EINVAL);
  644. u32 reg = 0;
  645. port = of_graph_get_port_by_id(node, port_id);
  646. if (!port)
  647. return ERR_PTR(-EINVAL);
  648. /*
  649. * This only works if there is only one path from the TCON
  650. * to any display engine. Otherwise the probe order of the
  651. * TCONs and display engines is not guaranteed. They may
  652. * either bind to the wrong one, or worse, bind to the same
  653. * one if additional checks are not done.
  654. *
  655. * Bail out if there are multiple input connections.
  656. */
  657. if (of_get_available_child_count(port) != 1)
  658. goto out_put_port;
  659. /* Get the first connection without specifying an ID */
  660. ep = of_get_next_available_child(port, NULL);
  661. if (!ep)
  662. goto out_put_port;
  663. remote = of_graph_get_remote_port_parent(ep);
  664. if (!remote)
  665. goto out_put_ep;
  666. /* does this node match any registered engines? */
  667. list_for_each_entry(engine, &drv->engine_list, list)
  668. if (remote == engine->node)
  669. goto out_put_remote;
  670. /*
  671. * According to device tree binding input ports have even id
  672. * number and output ports have odd id. Since component with
  673. * more than one input and one output (TCON TOP) exits, correct
  674. * remote input id has to be calculated by subtracting 1 from
  675. * remote output id. If this for some reason can't be done, 0
  676. * is used as input port id.
  677. */
  678. of_node_put(port);
  679. port = of_graph_get_remote_port(ep);
  680. if (!of_property_read_u32(port, "reg", &reg) && reg > 0)
  681. reg -= 1;
  682. /* keep looking through upstream ports */
  683. engine = sun4i_tcon_find_engine_traverse(drv, remote, reg);
  684. out_put_remote:
  685. of_node_put(remote);
  686. out_put_ep:
  687. of_node_put(ep);
  688. out_put_port:
  689. of_node_put(port);
  690. return engine;
  691. }
  692. /*
  693. * The device tree binding says that the remote endpoint ID of any
  694. * connection between components, up to and including the TCON, of
  695. * the display pipeline should be equal to the actual ID of the local
  696. * component. Thus we can look at any one of the input connections of
  697. * the TCONs, and use that connection's remote endpoint ID as our own.
  698. *
  699. * Since the user of this function already finds the input port,
  700. * the port is passed in directly without further checks.
  701. */
  702. static int sun4i_tcon_of_get_id_from_port(struct device_node *port)
  703. {
  704. struct device_node *ep;
  705. int ret = -EINVAL;
  706. /* try finding an upstream endpoint */
  707. for_each_available_child_of_node(port, ep) {
  708. struct device_node *remote;
  709. u32 reg;
  710. remote = of_graph_get_remote_endpoint(ep);
  711. if (!remote)
  712. continue;
  713. ret = of_property_read_u32(remote, "reg", &reg);
  714. if (ret)
  715. continue;
  716. ret = reg;
  717. }
  718. return ret;
  719. }
  720. /*
  721. * Once we know the TCON's id, we can look through the list of
  722. * engines to find a matching one. We assume all engines have
  723. * been probed and added to the list.
  724. */
  725. static struct sunxi_engine *sun4i_tcon_get_engine_by_id(struct sun4i_drv *drv,
  726. int id)
  727. {
  728. struct sunxi_engine *engine;
  729. list_for_each_entry(engine, &drv->engine_list, list)
  730. if (engine->id == id)
  731. return engine;
  732. return ERR_PTR(-EINVAL);
  733. }
  734. /*
  735. * On SoCs with the old display pipeline design (Display Engine 1.0),
  736. * we assumed the TCON was always tied to just one backend. However
  737. * this proved not to be the case. On the A31, the TCON can select
  738. * either backend as its source. On the A20 (and likely on the A10),
  739. * the backend can choose which TCON to output to.
  740. *
  741. * The device tree binding says that the remote endpoint ID of any
  742. * connection between components, up to and including the TCON, of
  743. * the display pipeline should be equal to the actual ID of the local
  744. * component. Thus we should be able to look at any one of the input
  745. * connections of the TCONs, and use that connection's remote endpoint
  746. * ID as our own.
  747. *
  748. * However the connections between the backend and TCON were assumed
  749. * to be always singular, and their endpoit IDs were all incorrectly
  750. * set to 0. This means for these old device trees, we cannot just look
  751. * up the remote endpoint ID of a TCON input endpoint. TCON1 would be
  752. * incorrectly identified as TCON0.
  753. *
  754. * This function first checks if the TCON node has 2 input endpoints.
  755. * If so, then the device tree is a corrected version, and it will use
  756. * sun4i_tcon_of_get_id() and sun4i_tcon_get_engine_by_id() from above
  757. * to fetch the ID and engine directly. If not, then it is likely an
  758. * old device trees, where the endpoint IDs were incorrect, but did not
  759. * have endpoint connections between the backend and TCON across
  760. * different display pipelines. It will fall back to the old method of
  761. * traversing the of_graph to try and find a matching engine by device
  762. * node.
  763. *
  764. * In the case of single display pipeline device trees, either method
  765. * works.
  766. */
  767. static struct sunxi_engine *sun4i_tcon_find_engine(struct sun4i_drv *drv,
  768. struct device_node *node)
  769. {
  770. struct device_node *port;
  771. struct sunxi_engine *engine;
  772. port = of_graph_get_port_by_id(node, 0);
  773. if (!port)
  774. return ERR_PTR(-EINVAL);
  775. /*
  776. * Is this a corrected device tree with cross pipeline
  777. * connections between the backend and TCON?
  778. */
  779. if (of_get_child_count(port) > 1) {
  780. /* Get our ID directly from an upstream endpoint */
  781. int id = sun4i_tcon_of_get_id_from_port(port);
  782. /* Get our engine by matching our ID */
  783. engine = sun4i_tcon_get_engine_by_id(drv, id);
  784. of_node_put(port);
  785. return engine;
  786. }
  787. /* Fallback to old method by traversing input endpoints */
  788. of_node_put(port);
  789. return sun4i_tcon_find_engine_traverse(drv, node, 0);
  790. }
  791. static int sun4i_tcon_bind(struct device *dev, struct device *master,
  792. void *data)
  793. {
  794. struct drm_device *drm = data;
  795. struct sun4i_drv *drv = drm->dev_private;
  796. struct sunxi_engine *engine;
  797. struct device_node *remote;
  798. struct sun4i_tcon *tcon;
  799. struct reset_control *edp_rstc;
  800. bool has_lvds_rst, has_lvds_alt, can_lvds;
  801. int ret;
  802. engine = sun4i_tcon_find_engine(drv, dev->of_node);
  803. if (IS_ERR(engine)) {
  804. dev_err(dev, "Couldn't find matching engine\n");
  805. return -EPROBE_DEFER;
  806. }
  807. tcon = devm_kzalloc(dev, sizeof(*tcon), GFP_KERNEL);
  808. if (!tcon)
  809. return -ENOMEM;
  810. dev_set_drvdata(dev, tcon);
  811. tcon->drm = drm;
  812. tcon->dev = dev;
  813. tcon->id = engine->id;
  814. tcon->quirks = of_device_get_match_data(dev);
  815. tcon->lcd_rst = devm_reset_control_get(dev, "lcd");
  816. if (IS_ERR(tcon->lcd_rst)) {
  817. dev_err(dev, "Couldn't get our reset line\n");
  818. return PTR_ERR(tcon->lcd_rst);
  819. }
  820. if (tcon->quirks->needs_edp_reset) {
  821. edp_rstc = devm_reset_control_get_shared(dev, "edp");
  822. if (IS_ERR(edp_rstc)) {
  823. dev_err(dev, "Couldn't get edp reset line\n");
  824. return PTR_ERR(edp_rstc);
  825. }
  826. ret = reset_control_deassert(edp_rstc);
  827. if (ret) {
  828. dev_err(dev, "Couldn't deassert edp reset line\n");
  829. return ret;
  830. }
  831. }
  832. /* Make sure our TCON is reset */
  833. ret = reset_control_reset(tcon->lcd_rst);
  834. if (ret) {
  835. dev_err(dev, "Couldn't deassert our reset line\n");
  836. return ret;
  837. }
  838. if (tcon->quirks->supports_lvds) {
  839. /*
  840. * This can only be made optional since we've had DT
  841. * nodes without the LVDS reset properties.
  842. *
  843. * If the property is missing, just disable LVDS, and
  844. * print a warning.
  845. */
  846. tcon->lvds_rst = devm_reset_control_get_optional(dev, "lvds");
  847. if (IS_ERR(tcon->lvds_rst)) {
  848. dev_err(dev, "Couldn't get our reset line\n");
  849. return PTR_ERR(tcon->lvds_rst);
  850. } else if (tcon->lvds_rst) {
  851. has_lvds_rst = true;
  852. reset_control_reset(tcon->lvds_rst);
  853. } else {
  854. has_lvds_rst = false;
  855. }
  856. /*
  857. * This can only be made optional since we've had DT
  858. * nodes without the LVDS reset properties.
  859. *
  860. * If the property is missing, just disable LVDS, and
  861. * print a warning.
  862. */
  863. if (tcon->quirks->has_lvds_alt) {
  864. tcon->lvds_pll = devm_clk_get(dev, "lvds-alt");
  865. if (IS_ERR(tcon->lvds_pll)) {
  866. if (PTR_ERR(tcon->lvds_pll) == -ENOENT) {
  867. has_lvds_alt = false;
  868. } else {
  869. dev_err(dev, "Couldn't get the LVDS PLL\n");
  870. return PTR_ERR(tcon->lvds_pll);
  871. }
  872. } else {
  873. has_lvds_alt = true;
  874. }
  875. }
  876. if (!has_lvds_rst ||
  877. (tcon->quirks->has_lvds_alt && !has_lvds_alt)) {
  878. dev_warn(dev, "Missing LVDS properties, Please upgrade your DT\n");
  879. dev_warn(dev, "LVDS output disabled\n");
  880. can_lvds = false;
  881. } else {
  882. can_lvds = true;
  883. }
  884. } else {
  885. can_lvds = false;
  886. }
  887. ret = sun4i_tcon_init_clocks(dev, tcon);
  888. if (ret) {
  889. dev_err(dev, "Couldn't init our TCON clocks\n");
  890. goto err_assert_reset;
  891. }
  892. ret = sun4i_tcon_init_regmap(dev, tcon);
  893. if (ret) {
  894. dev_err(dev, "Couldn't init our TCON regmap\n");
  895. goto err_free_clocks;
  896. }
  897. if (tcon->quirks->has_channel_0) {
  898. ret = sun4i_dclk_create(dev, tcon);
  899. if (ret) {
  900. dev_err(dev, "Couldn't create our TCON dot clock\n");
  901. goto err_free_clocks;
  902. }
  903. }
  904. ret = sun4i_tcon_init_irq(dev, tcon);
  905. if (ret) {
  906. dev_err(dev, "Couldn't init our TCON interrupts\n");
  907. goto err_free_dotclock;
  908. }
  909. tcon->crtc = sun4i_crtc_init(drm, engine, tcon);
  910. if (IS_ERR(tcon->crtc)) {
  911. dev_err(dev, "Couldn't create our CRTC\n");
  912. ret = PTR_ERR(tcon->crtc);
  913. goto err_free_dotclock;
  914. }
  915. if (tcon->quirks->has_channel_0) {
  916. /*
  917. * If we have an LVDS panel connected to the TCON, we should
  918. * just probe the LVDS connector. Otherwise, just probe RGB as
  919. * we used to.
  920. */
  921. remote = of_graph_get_remote_node(dev->of_node, 1, 0);
  922. if (of_device_is_compatible(remote, "panel-lvds"))
  923. if (can_lvds)
  924. ret = sun4i_lvds_init(drm, tcon);
  925. else
  926. ret = -EINVAL;
  927. else
  928. ret = sun4i_rgb_init(drm, tcon);
  929. of_node_put(remote);
  930. if (ret < 0)
  931. goto err_free_dotclock;
  932. }
  933. if (tcon->quirks->needs_de_be_mux) {
  934. /*
  935. * We assume there is no dynamic muxing of backends
  936. * and TCONs, so we select the backend with same ID.
  937. *
  938. * While dynamic selection might be interesting, since
  939. * the CRTC is tied to the TCON, while the layers are
  940. * tied to the backends, this means, we will need to
  941. * switch between groups of layers. There might not be
  942. * a way to represent this constraint in DRM.
  943. */
  944. regmap_update_bits(tcon->regs, SUN4I_TCON0_CTL_REG,
  945. SUN4I_TCON0_CTL_SRC_SEL_MASK,
  946. tcon->id);
  947. regmap_update_bits(tcon->regs, SUN4I_TCON1_CTL_REG,
  948. SUN4I_TCON1_CTL_SRC_SEL_MASK,
  949. tcon->id);
  950. }
  951. list_add_tail(&tcon->list, &drv->tcon_list);
  952. return 0;
  953. err_free_dotclock:
  954. if (tcon->quirks->has_channel_0)
  955. sun4i_dclk_free(tcon);
  956. err_free_clocks:
  957. sun4i_tcon_free_clocks(tcon);
  958. err_assert_reset:
  959. reset_control_assert(tcon->lcd_rst);
  960. return ret;
  961. }
  962. static void sun4i_tcon_unbind(struct device *dev, struct device *master,
  963. void *data)
  964. {
  965. struct sun4i_tcon *tcon = dev_get_drvdata(dev);
  966. list_del(&tcon->list);
  967. if (tcon->quirks->has_channel_0)
  968. sun4i_dclk_free(tcon);
  969. sun4i_tcon_free_clocks(tcon);
  970. }
  971. static const struct component_ops sun4i_tcon_ops = {
  972. .bind = sun4i_tcon_bind,
  973. .unbind = sun4i_tcon_unbind,
  974. };
  975. static int sun4i_tcon_probe(struct platform_device *pdev)
  976. {
  977. struct device_node *node = pdev->dev.of_node;
  978. const struct sun4i_tcon_quirks *quirks;
  979. struct drm_bridge *bridge;
  980. struct drm_panel *panel;
  981. int ret;
  982. quirks = of_device_get_match_data(&pdev->dev);
  983. /* panels and bridges are present only on TCONs with channel 0 */
  984. if (quirks->has_channel_0) {
  985. ret = drm_of_find_panel_or_bridge(node, 1, 0, &panel, &bridge);
  986. if (ret == -EPROBE_DEFER)
  987. return ret;
  988. }
  989. return component_add(&pdev->dev, &sun4i_tcon_ops);
  990. }
  991. static int sun4i_tcon_remove(struct platform_device *pdev)
  992. {
  993. component_del(&pdev->dev, &sun4i_tcon_ops);
  994. return 0;
  995. }
  996. /* platform specific TCON muxing callbacks */
  997. static int sun4i_a10_tcon_set_mux(struct sun4i_tcon *tcon,
  998. const struct drm_encoder *encoder)
  999. {
  1000. struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
  1001. u32 shift;
  1002. if (!tcon0)
  1003. return -EINVAL;
  1004. switch (encoder->encoder_type) {
  1005. case DRM_MODE_ENCODER_TMDS:
  1006. /* HDMI */
  1007. shift = 8;
  1008. break;
  1009. default:
  1010. return -EINVAL;
  1011. }
  1012. regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
  1013. 0x3 << shift, tcon->id << shift);
  1014. return 0;
  1015. }
  1016. static int sun5i_a13_tcon_set_mux(struct sun4i_tcon *tcon,
  1017. const struct drm_encoder *encoder)
  1018. {
  1019. u32 val;
  1020. if (encoder->encoder_type == DRM_MODE_ENCODER_TVDAC)
  1021. val = 1;
  1022. else
  1023. val = 0;
  1024. /*
  1025. * FIXME: Undocumented bits
  1026. */
  1027. return regmap_write(tcon->regs, SUN4I_TCON_MUX_CTRL_REG, val);
  1028. }
  1029. static int sun6i_tcon_set_mux(struct sun4i_tcon *tcon,
  1030. const struct drm_encoder *encoder)
  1031. {
  1032. struct sun4i_tcon *tcon0 = sun4i_get_tcon0(encoder->dev);
  1033. u32 shift;
  1034. if (!tcon0)
  1035. return -EINVAL;
  1036. switch (encoder->encoder_type) {
  1037. case DRM_MODE_ENCODER_TMDS:
  1038. /* HDMI */
  1039. shift = 8;
  1040. break;
  1041. default:
  1042. /* TODO A31 has MIPI DSI but A31s does not */
  1043. return -EINVAL;
  1044. }
  1045. regmap_update_bits(tcon0->regs, SUN4I_TCON_MUX_CTRL_REG,
  1046. 0x3 << shift, tcon->id << shift);
  1047. return 0;
  1048. }
  1049. static const struct sun4i_tcon_quirks sun4i_a10_quirks = {
  1050. .has_channel_0 = true,
  1051. .has_channel_1 = true,
  1052. .dclk_min_div = 4,
  1053. .set_mux = sun4i_a10_tcon_set_mux,
  1054. };
  1055. static const struct sun4i_tcon_quirks sun5i_a13_quirks = {
  1056. .has_channel_0 = true,
  1057. .has_channel_1 = true,
  1058. .dclk_min_div = 4,
  1059. .set_mux = sun5i_a13_tcon_set_mux,
  1060. };
  1061. static const struct sun4i_tcon_quirks sun6i_a31_quirks = {
  1062. .has_channel_0 = true,
  1063. .has_channel_1 = true,
  1064. .has_lvds_alt = true,
  1065. .needs_de_be_mux = true,
  1066. .dclk_min_div = 1,
  1067. .set_mux = sun6i_tcon_set_mux,
  1068. };
  1069. static const struct sun4i_tcon_quirks sun6i_a31s_quirks = {
  1070. .has_channel_0 = true,
  1071. .has_channel_1 = true,
  1072. .needs_de_be_mux = true,
  1073. .dclk_min_div = 1,
  1074. };
  1075. static const struct sun4i_tcon_quirks sun7i_a20_quirks = {
  1076. .has_channel_0 = true,
  1077. .has_channel_1 = true,
  1078. .dclk_min_div = 4,
  1079. /* Same display pipeline structure as A10 */
  1080. .set_mux = sun4i_a10_tcon_set_mux,
  1081. };
  1082. static const struct sun4i_tcon_quirks sun8i_a33_quirks = {
  1083. .has_channel_0 = true,
  1084. .has_lvds_alt = true,
  1085. .dclk_min_div = 1,
  1086. };
  1087. static const struct sun4i_tcon_quirks sun8i_a83t_lcd_quirks = {
  1088. .supports_lvds = true,
  1089. .has_channel_0 = true,
  1090. .dclk_min_div = 1,
  1091. };
  1092. static const struct sun4i_tcon_quirks sun8i_a83t_tv_quirks = {
  1093. .has_channel_1 = true,
  1094. };
  1095. static const struct sun4i_tcon_quirks sun8i_v3s_quirks = {
  1096. .has_channel_0 = true,
  1097. .dclk_min_div = 1,
  1098. };
  1099. static const struct sun4i_tcon_quirks sun9i_a80_tcon_lcd_quirks = {
  1100. .has_channel_0 = true,
  1101. .needs_edp_reset = true,
  1102. .dclk_min_div = 1,
  1103. };
  1104. static const struct sun4i_tcon_quirks sun9i_a80_tcon_tv_quirks = {
  1105. .has_channel_1 = true,
  1106. .needs_edp_reset = true,
  1107. };
  1108. /* sun4i_drv uses this list to check if a device node is a TCON */
  1109. const struct of_device_id sun4i_tcon_of_table[] = {
  1110. { .compatible = "allwinner,sun4i-a10-tcon", .data = &sun4i_a10_quirks },
  1111. { .compatible = "allwinner,sun5i-a13-tcon", .data = &sun5i_a13_quirks },
  1112. { .compatible = "allwinner,sun6i-a31-tcon", .data = &sun6i_a31_quirks },
  1113. { .compatible = "allwinner,sun6i-a31s-tcon", .data = &sun6i_a31s_quirks },
  1114. { .compatible = "allwinner,sun7i-a20-tcon", .data = &sun7i_a20_quirks },
  1115. { .compatible = "allwinner,sun8i-a33-tcon", .data = &sun8i_a33_quirks },
  1116. { .compatible = "allwinner,sun8i-a83t-tcon-lcd", .data = &sun8i_a83t_lcd_quirks },
  1117. { .compatible = "allwinner,sun8i-a83t-tcon-tv", .data = &sun8i_a83t_tv_quirks },
  1118. { .compatible = "allwinner,sun8i-v3s-tcon", .data = &sun8i_v3s_quirks },
  1119. { .compatible = "allwinner,sun9i-a80-tcon-lcd", .data = &sun9i_a80_tcon_lcd_quirks },
  1120. { .compatible = "allwinner,sun9i-a80-tcon-tv", .data = &sun9i_a80_tcon_tv_quirks },
  1121. { }
  1122. };
  1123. MODULE_DEVICE_TABLE(of, sun4i_tcon_of_table);
  1124. EXPORT_SYMBOL(sun4i_tcon_of_table);
  1125. static struct platform_driver sun4i_tcon_platform_driver = {
  1126. .probe = sun4i_tcon_probe,
  1127. .remove = sun4i_tcon_remove,
  1128. .driver = {
  1129. .name = "sun4i-tcon",
  1130. .of_match_table = sun4i_tcon_of_table,
  1131. },
  1132. };
  1133. module_platform_driver(sun4i_tcon_platform_driver);
  1134. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  1135. MODULE_DESCRIPTION("Allwinner A10 Timing Controller Driver");
  1136. MODULE_LICENSE("GPL");