sun4i_tcon.c 36 KB

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