sun4i_tcon.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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");