rcar_lvds.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * rcar_lvds.c -- R-Car LVDS Encoder
  4. *
  5. * Copyright (C) 2013-2018 Renesas Electronics Corporation
  6. *
  7. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/io.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/of_graph.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <drm/drm_atomic.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include <drm/drm_bridge.h>
  20. #include <drm/drm_crtc_helper.h>
  21. #include <drm/drm_panel.h>
  22. #include "rcar_lvds_regs.h"
  23. struct rcar_lvds;
  24. /* Keep in sync with the LVDCR0.LVMD hardware register values. */
  25. enum rcar_lvds_mode {
  26. RCAR_LVDS_MODE_JEIDA = 0,
  27. RCAR_LVDS_MODE_MIRROR = 1,
  28. RCAR_LVDS_MODE_VESA = 4,
  29. };
  30. #define RCAR_LVDS_QUIRK_LANES BIT(0) /* LVDS lanes 1 and 3 inverted */
  31. #define RCAR_LVDS_QUIRK_GEN3_LVEN BIT(1) /* LVEN bit needs to be set on R8A77970/R8A7799x */
  32. #define RCAR_LVDS_QUIRK_PWD BIT(2) /* PWD bit available (all of Gen3 but E3) */
  33. #define RCAR_LVDS_QUIRK_EXT_PLL BIT(3) /* Has extended PLL */
  34. #define RCAR_LVDS_QUIRK_DUAL_LINK BIT(4) /* Supports dual-link operation */
  35. struct rcar_lvds_device_info {
  36. unsigned int gen;
  37. unsigned int quirks;
  38. void (*pll_setup)(struct rcar_lvds *lvds, unsigned int freq);
  39. };
  40. struct rcar_lvds {
  41. struct device *dev;
  42. const struct rcar_lvds_device_info *info;
  43. struct drm_bridge bridge;
  44. struct drm_bridge *next_bridge;
  45. struct drm_connector connector;
  46. struct drm_panel *panel;
  47. void __iomem *mmio;
  48. struct {
  49. struct clk *mod; /* CPG module clock */
  50. struct clk *extal; /* External clock */
  51. struct clk *dotclkin[2]; /* External DU clocks */
  52. } clocks;
  53. bool enabled;
  54. struct drm_display_mode display_mode;
  55. enum rcar_lvds_mode mode;
  56. };
  57. #define bridge_to_rcar_lvds(bridge) \
  58. container_of(bridge, struct rcar_lvds, bridge)
  59. #define connector_to_rcar_lvds(connector) \
  60. container_of(connector, struct rcar_lvds, connector)
  61. static void rcar_lvds_write(struct rcar_lvds *lvds, u32 reg, u32 data)
  62. {
  63. iowrite32(data, lvds->mmio + reg);
  64. }
  65. /* -----------------------------------------------------------------------------
  66. * Connector & Panel
  67. */
  68. static int rcar_lvds_connector_get_modes(struct drm_connector *connector)
  69. {
  70. struct rcar_lvds *lvds = connector_to_rcar_lvds(connector);
  71. return drm_panel_get_modes(lvds->panel);
  72. }
  73. static int rcar_lvds_connector_atomic_check(struct drm_connector *connector,
  74. struct drm_connector_state *state)
  75. {
  76. struct rcar_lvds *lvds = connector_to_rcar_lvds(connector);
  77. const struct drm_display_mode *panel_mode;
  78. struct drm_crtc_state *crtc_state;
  79. if (!state->crtc)
  80. return 0;
  81. if (list_empty(&connector->modes)) {
  82. dev_dbg(lvds->dev, "connector: empty modes list\n");
  83. return -EINVAL;
  84. }
  85. panel_mode = list_first_entry(&connector->modes,
  86. struct drm_display_mode, head);
  87. /* We're not allowed to modify the resolution. */
  88. crtc_state = drm_atomic_get_crtc_state(state->state, state->crtc);
  89. if (IS_ERR(crtc_state))
  90. return PTR_ERR(crtc_state);
  91. if (crtc_state->mode.hdisplay != panel_mode->hdisplay ||
  92. crtc_state->mode.vdisplay != panel_mode->vdisplay)
  93. return -EINVAL;
  94. /* The flat panel mode is fixed, just copy it to the adjusted mode. */
  95. drm_mode_copy(&crtc_state->adjusted_mode, panel_mode);
  96. return 0;
  97. }
  98. static const struct drm_connector_helper_funcs rcar_lvds_conn_helper_funcs = {
  99. .get_modes = rcar_lvds_connector_get_modes,
  100. .atomic_check = rcar_lvds_connector_atomic_check,
  101. };
  102. static const struct drm_connector_funcs rcar_lvds_conn_funcs = {
  103. .reset = drm_atomic_helper_connector_reset,
  104. .fill_modes = drm_helper_probe_single_connector_modes,
  105. .destroy = drm_connector_cleanup,
  106. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  107. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  108. };
  109. /* -----------------------------------------------------------------------------
  110. * PLL Setup
  111. */
  112. static void rcar_lvds_pll_setup_gen2(struct rcar_lvds *lvds, unsigned int freq)
  113. {
  114. u32 val;
  115. if (freq < 39000000)
  116. val = LVDPLLCR_CEEN | LVDPLLCR_COSEL | LVDPLLCR_PLLDLYCNT_38M;
  117. else if (freq < 61000000)
  118. val = LVDPLLCR_CEEN | LVDPLLCR_COSEL | LVDPLLCR_PLLDLYCNT_60M;
  119. else if (freq < 121000000)
  120. val = LVDPLLCR_CEEN | LVDPLLCR_COSEL | LVDPLLCR_PLLDLYCNT_121M;
  121. else
  122. val = LVDPLLCR_PLLDLYCNT_150M;
  123. rcar_lvds_write(lvds, LVDPLLCR, val);
  124. }
  125. static void rcar_lvds_pll_setup_gen3(struct rcar_lvds *lvds, unsigned int freq)
  126. {
  127. u32 val;
  128. if (freq < 42000000)
  129. val = LVDPLLCR_PLLDIVCNT_42M;
  130. else if (freq < 85000000)
  131. val = LVDPLLCR_PLLDIVCNT_85M;
  132. else if (freq < 128000000)
  133. val = LVDPLLCR_PLLDIVCNT_128M;
  134. else
  135. val = LVDPLLCR_PLLDIVCNT_148M;
  136. rcar_lvds_write(lvds, LVDPLLCR, val);
  137. }
  138. struct pll_info {
  139. unsigned long diff;
  140. unsigned int pll_m;
  141. unsigned int pll_n;
  142. unsigned int pll_e;
  143. unsigned int div;
  144. u32 clksel;
  145. };
  146. static void rcar_lvds_d3_e3_pll_calc(struct rcar_lvds *lvds, struct clk *clk,
  147. unsigned long target, struct pll_info *pll,
  148. u32 clksel)
  149. {
  150. unsigned long output;
  151. unsigned long fin;
  152. unsigned int m_min;
  153. unsigned int m_max;
  154. unsigned int m;
  155. int error;
  156. if (!clk)
  157. return;
  158. /*
  159. * The LVDS PLL is made of a pre-divider and a multiplier (strangely
  160. * enough called M and N respectively), followed by a post-divider E.
  161. *
  162. * ,-----. ,-----. ,-----. ,-----.
  163. * Fin --> | 1/M | -Fpdf-> | PFD | --> | VCO | -Fvco-> | 1/E | --> Fout
  164. * `-----' ,-> | | `-----' | `-----'
  165. * | `-----' |
  166. * | ,-----. |
  167. * `-------- | 1/N | <-------'
  168. * `-----'
  169. *
  170. * The clock output by the PLL is then further divided by a programmable
  171. * divider DIV to achieve the desired target frequency. Finally, an
  172. * optional fixed /7 divider is used to convert the bit clock to a pixel
  173. * clock (as LVDS transmits 7 bits per lane per clock sample).
  174. *
  175. * ,-------. ,-----. |\
  176. * Fout --> | 1/DIV | --> | 1/7 | --> | |
  177. * `-------' | `-----' | | --> dot clock
  178. * `------------> | |
  179. * |/
  180. *
  181. * The /7 divider is optional when the LVDS PLL is used to generate a
  182. * dot clock for the DU RGB output, without using the LVDS encoder. We
  183. * don't support this configuration yet.
  184. *
  185. * The PLL allowed input frequency range is 12 MHz to 192 MHz.
  186. */
  187. fin = clk_get_rate(clk);
  188. if (fin < 12000000 || fin > 192000000)
  189. return;
  190. /*
  191. * The comparison frequency range is 12 MHz to 24 MHz, which limits the
  192. * allowed values for the pre-divider M (normal range 1-8).
  193. *
  194. * Fpfd = Fin / M
  195. */
  196. m_min = max_t(unsigned int, 1, DIV_ROUND_UP(fin, 24000000));
  197. m_max = min_t(unsigned int, 8, fin / 12000000);
  198. for (m = m_min; m <= m_max; ++m) {
  199. unsigned long fpfd;
  200. unsigned int n_min;
  201. unsigned int n_max;
  202. unsigned int n;
  203. /*
  204. * The VCO operating range is 900 Mhz to 1800 MHz, which limits
  205. * the allowed values for the multiplier N (normal range
  206. * 60-120).
  207. *
  208. * Fvco = Fin * N / M
  209. */
  210. fpfd = fin / m;
  211. n_min = max_t(unsigned int, 60, DIV_ROUND_UP(900000000, fpfd));
  212. n_max = min_t(unsigned int, 120, 1800000000 / fpfd);
  213. for (n = n_min; n < n_max; ++n) {
  214. unsigned long fvco;
  215. unsigned int e_min;
  216. unsigned int e;
  217. /*
  218. * The output frequency is limited to 1039.5 MHz,
  219. * limiting again the allowed values for the
  220. * post-divider E (normal value 1, 2 or 4).
  221. *
  222. * Fout = Fvco / E
  223. */
  224. fvco = fpfd * n;
  225. e_min = fvco > 1039500000 ? 1 : 0;
  226. for (e = e_min; e < 3; ++e) {
  227. unsigned long fout;
  228. unsigned long diff;
  229. unsigned int div;
  230. /*
  231. * Finally we have a programable divider after
  232. * the PLL, followed by a an optional fixed /7
  233. * divider.
  234. */
  235. fout = fvco / (1 << e) / 7;
  236. div = DIV_ROUND_CLOSEST(fout, target);
  237. diff = abs(fout / div - target);
  238. if (diff < pll->diff) {
  239. pll->diff = diff;
  240. pll->pll_m = m;
  241. pll->pll_n = n;
  242. pll->pll_e = e;
  243. pll->div = div;
  244. pll->clksel = clksel;
  245. if (diff == 0)
  246. goto done;
  247. }
  248. }
  249. }
  250. }
  251. done:
  252. output = fin * pll->pll_n / pll->pll_m / (1 << pll->pll_e)
  253. / 7 / pll->div;
  254. error = (long)(output - target) * 10000 / (long)target;
  255. dev_dbg(lvds->dev,
  256. "%pC %lu Hz -> Fout %lu Hz (target %lu Hz, error %d.%02u%%), PLL M/N/E/DIV %u/%u/%u/%u\n",
  257. clk, fin, output, target, error / 100,
  258. error < 0 ? -error % 100 : error % 100,
  259. pll->pll_m, pll->pll_n, pll->pll_e, pll->div);
  260. }
  261. static void rcar_lvds_pll_setup_d3_e3(struct rcar_lvds *lvds, unsigned int freq)
  262. {
  263. struct pll_info pll = { .diff = (unsigned long)-1 };
  264. u32 lvdpllcr;
  265. rcar_lvds_d3_e3_pll_calc(lvds, lvds->clocks.dotclkin[0], freq, &pll,
  266. LVDPLLCR_CKSEL_DU_DOTCLKIN(0));
  267. rcar_lvds_d3_e3_pll_calc(lvds, lvds->clocks.dotclkin[1], freq, &pll,
  268. LVDPLLCR_CKSEL_DU_DOTCLKIN(1));
  269. rcar_lvds_d3_e3_pll_calc(lvds, lvds->clocks.extal, freq, &pll,
  270. LVDPLLCR_CKSEL_EXTAL);
  271. lvdpllcr = LVDPLLCR_PLLON | pll.clksel | LVDPLLCR_CLKOUT
  272. | LVDPLLCR_PLLN(pll.pll_n - 1) | LVDPLLCR_PLLM(pll.pll_m - 1);
  273. if (pll.pll_e > 0)
  274. lvdpllcr |= LVDPLLCR_STP_CLKOUTE | LVDPLLCR_OUTCLKSEL
  275. | LVDPLLCR_PLLE(pll.pll_e - 1);
  276. rcar_lvds_write(lvds, LVDPLLCR, lvdpllcr);
  277. if (pll.div > 1)
  278. /*
  279. * The DIVRESET bit is a misnomer, setting it to 1 deasserts the
  280. * divisor reset.
  281. */
  282. rcar_lvds_write(lvds, LVDDIV, LVDDIV_DIVSEL |
  283. LVDDIV_DIVRESET | LVDDIV_DIV(pll.div - 1));
  284. else
  285. rcar_lvds_write(lvds, LVDDIV, 0);
  286. }
  287. /* -----------------------------------------------------------------------------
  288. * Bridge
  289. */
  290. static void rcar_lvds_enable(struct drm_bridge *bridge)
  291. {
  292. struct rcar_lvds *lvds = bridge_to_rcar_lvds(bridge);
  293. const struct drm_display_mode *mode = &lvds->display_mode;
  294. /*
  295. * FIXME: We should really retrieve the CRTC through the state, but how
  296. * do we get a state pointer?
  297. */
  298. struct drm_crtc *crtc = lvds->bridge.encoder->crtc;
  299. u32 lvdhcr;
  300. u32 lvdcr0;
  301. int ret;
  302. WARN_ON(lvds->enabled);
  303. ret = clk_prepare_enable(lvds->clocks.mod);
  304. if (ret < 0)
  305. return;
  306. /*
  307. * Hardcode the channels and control signals routing for now.
  308. *
  309. * HSYNC -> CTRL0
  310. * VSYNC -> CTRL1
  311. * DISP -> CTRL2
  312. * 0 -> CTRL3
  313. */
  314. rcar_lvds_write(lvds, LVDCTRCR, LVDCTRCR_CTR3SEL_ZERO |
  315. LVDCTRCR_CTR2SEL_DISP | LVDCTRCR_CTR1SEL_VSYNC |
  316. LVDCTRCR_CTR0SEL_HSYNC);
  317. if (lvds->info->quirks & RCAR_LVDS_QUIRK_LANES)
  318. lvdhcr = LVDCHCR_CHSEL_CH(0, 0) | LVDCHCR_CHSEL_CH(1, 3)
  319. | LVDCHCR_CHSEL_CH(2, 2) | LVDCHCR_CHSEL_CH(3, 1);
  320. else
  321. lvdhcr = LVDCHCR_CHSEL_CH(0, 0) | LVDCHCR_CHSEL_CH(1, 1)
  322. | LVDCHCR_CHSEL_CH(2, 2) | LVDCHCR_CHSEL_CH(3, 3);
  323. rcar_lvds_write(lvds, LVDCHCR, lvdhcr);
  324. if (lvds->info->quirks & RCAR_LVDS_QUIRK_DUAL_LINK) {
  325. /* Disable dual-link mode. */
  326. rcar_lvds_write(lvds, LVDSTRIPE, 0);
  327. }
  328. /* PLL clock configuration. */
  329. lvds->info->pll_setup(lvds, mode->clock * 1000);
  330. /* Set the LVDS mode and select the input. */
  331. lvdcr0 = lvds->mode << LVDCR0_LVMD_SHIFT;
  332. if (drm_crtc_index(crtc) == 2)
  333. lvdcr0 |= LVDCR0_DUSEL;
  334. rcar_lvds_write(lvds, LVDCR0, lvdcr0);
  335. /* Turn all the channels on. */
  336. rcar_lvds_write(lvds, LVDCR1,
  337. LVDCR1_CHSTBY(3) | LVDCR1_CHSTBY(2) |
  338. LVDCR1_CHSTBY(1) | LVDCR1_CHSTBY(0) | LVDCR1_CLKSTBY);
  339. if (lvds->info->gen < 3) {
  340. /* Enable LVDS operation and turn the bias circuitry on. */
  341. lvdcr0 |= LVDCR0_BEN | LVDCR0_LVEN;
  342. rcar_lvds_write(lvds, LVDCR0, lvdcr0);
  343. }
  344. if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL)) {
  345. /*
  346. * Turn the PLL on (simple PLL only, extended PLL is fully
  347. * controlled through LVDPLLCR).
  348. */
  349. lvdcr0 |= LVDCR0_PLLON;
  350. rcar_lvds_write(lvds, LVDCR0, lvdcr0);
  351. }
  352. if (lvds->info->quirks & RCAR_LVDS_QUIRK_PWD) {
  353. /* Set LVDS normal mode. */
  354. lvdcr0 |= LVDCR0_PWD;
  355. rcar_lvds_write(lvds, LVDCR0, lvdcr0);
  356. }
  357. if (lvds->info->quirks & RCAR_LVDS_QUIRK_GEN3_LVEN) {
  358. /* Turn on the LVDS PHY. */
  359. lvdcr0 |= LVDCR0_LVEN;
  360. rcar_lvds_write(lvds, LVDCR0, lvdcr0);
  361. }
  362. if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL)) {
  363. /* Wait for the PLL startup delay (simple PLL only). */
  364. usleep_range(100, 150);
  365. }
  366. /* Turn the output on. */
  367. lvdcr0 |= LVDCR0_LVRES;
  368. rcar_lvds_write(lvds, LVDCR0, lvdcr0);
  369. if (lvds->panel) {
  370. drm_panel_prepare(lvds->panel);
  371. drm_panel_enable(lvds->panel);
  372. }
  373. lvds->enabled = true;
  374. }
  375. static void rcar_lvds_disable(struct drm_bridge *bridge)
  376. {
  377. struct rcar_lvds *lvds = bridge_to_rcar_lvds(bridge);
  378. WARN_ON(!lvds->enabled);
  379. if (lvds->panel) {
  380. drm_panel_disable(lvds->panel);
  381. drm_panel_unprepare(lvds->panel);
  382. }
  383. rcar_lvds_write(lvds, LVDCR0, 0);
  384. rcar_lvds_write(lvds, LVDCR1, 0);
  385. rcar_lvds_write(lvds, LVDPLLCR, 0);
  386. clk_disable_unprepare(lvds->clocks.mod);
  387. lvds->enabled = false;
  388. }
  389. static bool rcar_lvds_mode_fixup(struct drm_bridge *bridge,
  390. const struct drm_display_mode *mode,
  391. struct drm_display_mode *adjusted_mode)
  392. {
  393. /*
  394. * The internal LVDS encoder has a restricted clock frequency operating
  395. * range (31MHz to 148.5MHz). Clamp the clock accordingly.
  396. */
  397. adjusted_mode->clock = clamp(adjusted_mode->clock, 31000, 148500);
  398. return true;
  399. }
  400. static void rcar_lvds_get_lvds_mode(struct rcar_lvds *lvds)
  401. {
  402. struct drm_display_info *info = &lvds->connector.display_info;
  403. enum rcar_lvds_mode mode;
  404. /*
  405. * There is no API yet to retrieve LVDS mode from a bridge, only panels
  406. * are supported.
  407. */
  408. if (!lvds->panel)
  409. return;
  410. if (!info->num_bus_formats || !info->bus_formats) {
  411. dev_err(lvds->dev, "no LVDS bus format reported\n");
  412. return;
  413. }
  414. switch (info->bus_formats[0]) {
  415. case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
  416. case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
  417. mode = RCAR_LVDS_MODE_JEIDA;
  418. break;
  419. case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
  420. mode = RCAR_LVDS_MODE_VESA;
  421. break;
  422. default:
  423. dev_err(lvds->dev, "unsupported LVDS bus format 0x%04x\n",
  424. info->bus_formats[0]);
  425. return;
  426. }
  427. if (info->bus_flags & DRM_BUS_FLAG_DATA_LSB_TO_MSB)
  428. mode |= RCAR_LVDS_MODE_MIRROR;
  429. lvds->mode = mode;
  430. }
  431. static void rcar_lvds_mode_set(struct drm_bridge *bridge,
  432. struct drm_display_mode *mode,
  433. struct drm_display_mode *adjusted_mode)
  434. {
  435. struct rcar_lvds *lvds = bridge_to_rcar_lvds(bridge);
  436. WARN_ON(lvds->enabled);
  437. lvds->display_mode = *adjusted_mode;
  438. rcar_lvds_get_lvds_mode(lvds);
  439. }
  440. static int rcar_lvds_attach(struct drm_bridge *bridge)
  441. {
  442. struct rcar_lvds *lvds = bridge_to_rcar_lvds(bridge);
  443. struct drm_connector *connector = &lvds->connector;
  444. struct drm_encoder *encoder = bridge->encoder;
  445. int ret;
  446. /* If we have a next bridge just attach it. */
  447. if (lvds->next_bridge)
  448. return drm_bridge_attach(bridge->encoder, lvds->next_bridge,
  449. bridge);
  450. /* Otherwise we have a panel, create a connector. */
  451. ret = drm_connector_init(bridge->dev, connector, &rcar_lvds_conn_funcs,
  452. DRM_MODE_CONNECTOR_LVDS);
  453. if (ret < 0)
  454. return ret;
  455. drm_connector_helper_add(connector, &rcar_lvds_conn_helper_funcs);
  456. ret = drm_connector_attach_encoder(connector, encoder);
  457. if (ret < 0)
  458. return ret;
  459. return drm_panel_attach(lvds->panel, connector);
  460. }
  461. static void rcar_lvds_detach(struct drm_bridge *bridge)
  462. {
  463. struct rcar_lvds *lvds = bridge_to_rcar_lvds(bridge);
  464. if (lvds->panel)
  465. drm_panel_detach(lvds->panel);
  466. }
  467. static const struct drm_bridge_funcs rcar_lvds_bridge_ops = {
  468. .attach = rcar_lvds_attach,
  469. .detach = rcar_lvds_detach,
  470. .enable = rcar_lvds_enable,
  471. .disable = rcar_lvds_disable,
  472. .mode_fixup = rcar_lvds_mode_fixup,
  473. .mode_set = rcar_lvds_mode_set,
  474. };
  475. /* -----------------------------------------------------------------------------
  476. * Probe & Remove
  477. */
  478. static int rcar_lvds_parse_dt(struct rcar_lvds *lvds)
  479. {
  480. struct device_node *local_output = NULL;
  481. struct device_node *remote_input = NULL;
  482. struct device_node *remote = NULL;
  483. struct device_node *node;
  484. bool is_bridge = false;
  485. int ret = 0;
  486. local_output = of_graph_get_endpoint_by_regs(lvds->dev->of_node, 1, 0);
  487. if (!local_output) {
  488. dev_dbg(lvds->dev, "unconnected port@1\n");
  489. return -ENODEV;
  490. }
  491. /*
  492. * Locate the connected entity and infer its type from the number of
  493. * endpoints.
  494. */
  495. remote = of_graph_get_remote_port_parent(local_output);
  496. if (!remote) {
  497. dev_dbg(lvds->dev, "unconnected endpoint %pOF\n", local_output);
  498. ret = -ENODEV;
  499. goto done;
  500. }
  501. if (!of_device_is_available(remote)) {
  502. dev_dbg(lvds->dev, "connected entity %pOF is disabled\n",
  503. remote);
  504. ret = -ENODEV;
  505. goto done;
  506. }
  507. remote_input = of_graph_get_remote_endpoint(local_output);
  508. for_each_endpoint_of_node(remote, node) {
  509. if (node != remote_input) {
  510. /*
  511. * We've found one endpoint other than the input, this
  512. * must be a bridge.
  513. */
  514. is_bridge = true;
  515. of_node_put(node);
  516. break;
  517. }
  518. }
  519. if (is_bridge) {
  520. lvds->next_bridge = of_drm_find_bridge(remote);
  521. if (!lvds->next_bridge)
  522. ret = -EPROBE_DEFER;
  523. } else {
  524. lvds->panel = of_drm_find_panel(remote);
  525. if (IS_ERR(lvds->panel))
  526. ret = PTR_ERR(lvds->panel);
  527. }
  528. done:
  529. of_node_put(local_output);
  530. of_node_put(remote_input);
  531. of_node_put(remote);
  532. return ret;
  533. }
  534. static struct clk *rcar_lvds_get_clock(struct rcar_lvds *lvds, const char *name,
  535. bool optional)
  536. {
  537. struct clk *clk;
  538. clk = devm_clk_get(lvds->dev, name);
  539. if (!IS_ERR(clk))
  540. return clk;
  541. if (PTR_ERR(clk) == -ENOENT && optional)
  542. return NULL;
  543. if (PTR_ERR(clk) != -EPROBE_DEFER)
  544. dev_err(lvds->dev, "failed to get %s clock\n",
  545. name ? name : "module");
  546. return clk;
  547. }
  548. static int rcar_lvds_get_clocks(struct rcar_lvds *lvds)
  549. {
  550. lvds->clocks.mod = rcar_lvds_get_clock(lvds, NULL, false);
  551. if (IS_ERR(lvds->clocks.mod))
  552. return PTR_ERR(lvds->clocks.mod);
  553. /*
  554. * LVDS encoders without an extended PLL have no external clock inputs.
  555. */
  556. if (!(lvds->info->quirks & RCAR_LVDS_QUIRK_EXT_PLL))
  557. return 0;
  558. lvds->clocks.extal = rcar_lvds_get_clock(lvds, "extal", true);
  559. if (IS_ERR(lvds->clocks.extal))
  560. return PTR_ERR(lvds->clocks.extal);
  561. lvds->clocks.dotclkin[0] = rcar_lvds_get_clock(lvds, "dclkin.0", true);
  562. if (IS_ERR(lvds->clocks.dotclkin[0]))
  563. return PTR_ERR(lvds->clocks.dotclkin[0]);
  564. lvds->clocks.dotclkin[1] = rcar_lvds_get_clock(lvds, "dclkin.1", true);
  565. if (IS_ERR(lvds->clocks.dotclkin[1]))
  566. return PTR_ERR(lvds->clocks.dotclkin[1]);
  567. /* At least one input to the PLL must be available. */
  568. if (!lvds->clocks.extal && !lvds->clocks.dotclkin[0] &&
  569. !lvds->clocks.dotclkin[1]) {
  570. dev_err(lvds->dev,
  571. "no input clock (extal, dclkin.0 or dclkin.1)\n");
  572. return -EINVAL;
  573. }
  574. return 0;
  575. }
  576. static int rcar_lvds_probe(struct platform_device *pdev)
  577. {
  578. struct rcar_lvds *lvds;
  579. struct resource *mem;
  580. int ret;
  581. lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL);
  582. if (lvds == NULL)
  583. return -ENOMEM;
  584. platform_set_drvdata(pdev, lvds);
  585. lvds->dev = &pdev->dev;
  586. lvds->info = of_device_get_match_data(&pdev->dev);
  587. lvds->enabled = false;
  588. ret = rcar_lvds_parse_dt(lvds);
  589. if (ret < 0)
  590. return ret;
  591. lvds->bridge.driver_private = lvds;
  592. lvds->bridge.funcs = &rcar_lvds_bridge_ops;
  593. lvds->bridge.of_node = pdev->dev.of_node;
  594. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  595. lvds->mmio = devm_ioremap_resource(&pdev->dev, mem);
  596. if (IS_ERR(lvds->mmio))
  597. return PTR_ERR(lvds->mmio);
  598. ret = rcar_lvds_get_clocks(lvds);
  599. if (ret < 0)
  600. return ret;
  601. drm_bridge_add(&lvds->bridge);
  602. return 0;
  603. }
  604. static int rcar_lvds_remove(struct platform_device *pdev)
  605. {
  606. struct rcar_lvds *lvds = platform_get_drvdata(pdev);
  607. drm_bridge_remove(&lvds->bridge);
  608. return 0;
  609. }
  610. static const struct rcar_lvds_device_info rcar_lvds_gen2_info = {
  611. .gen = 2,
  612. .pll_setup = rcar_lvds_pll_setup_gen2,
  613. };
  614. static const struct rcar_lvds_device_info rcar_lvds_r8a7790_info = {
  615. .gen = 2,
  616. .quirks = RCAR_LVDS_QUIRK_LANES,
  617. .pll_setup = rcar_lvds_pll_setup_gen2,
  618. };
  619. static const struct rcar_lvds_device_info rcar_lvds_gen3_info = {
  620. .gen = 3,
  621. .quirks = RCAR_LVDS_QUIRK_PWD,
  622. .pll_setup = rcar_lvds_pll_setup_gen3,
  623. };
  624. static const struct rcar_lvds_device_info rcar_lvds_r8a77970_info = {
  625. .gen = 3,
  626. .quirks = RCAR_LVDS_QUIRK_PWD | RCAR_LVDS_QUIRK_GEN3_LVEN,
  627. .pll_setup = rcar_lvds_pll_setup_gen2,
  628. };
  629. static const struct rcar_lvds_device_info rcar_lvds_r8a77990_info = {
  630. .gen = 3,
  631. .quirks = RCAR_LVDS_QUIRK_GEN3_LVEN | RCAR_LVDS_QUIRK_EXT_PLL
  632. | RCAR_LVDS_QUIRK_DUAL_LINK,
  633. .pll_setup = rcar_lvds_pll_setup_d3_e3,
  634. };
  635. static const struct rcar_lvds_device_info rcar_lvds_r8a77995_info = {
  636. .gen = 3,
  637. .quirks = RCAR_LVDS_QUIRK_GEN3_LVEN | RCAR_LVDS_QUIRK_PWD
  638. | RCAR_LVDS_QUIRK_EXT_PLL | RCAR_LVDS_QUIRK_DUAL_LINK,
  639. .pll_setup = rcar_lvds_pll_setup_d3_e3,
  640. };
  641. static const struct of_device_id rcar_lvds_of_table[] = {
  642. { .compatible = "renesas,r8a7743-lvds", .data = &rcar_lvds_gen2_info },
  643. { .compatible = "renesas,r8a7790-lvds", .data = &rcar_lvds_r8a7790_info },
  644. { .compatible = "renesas,r8a7791-lvds", .data = &rcar_lvds_gen2_info },
  645. { .compatible = "renesas,r8a7793-lvds", .data = &rcar_lvds_gen2_info },
  646. { .compatible = "renesas,r8a7795-lvds", .data = &rcar_lvds_gen3_info },
  647. { .compatible = "renesas,r8a7796-lvds", .data = &rcar_lvds_gen3_info },
  648. { .compatible = "renesas,r8a77970-lvds", .data = &rcar_lvds_r8a77970_info },
  649. { .compatible = "renesas,r8a77980-lvds", .data = &rcar_lvds_gen3_info },
  650. { .compatible = "renesas,r8a77990-lvds", .data = &rcar_lvds_r8a77990_info },
  651. { .compatible = "renesas,r8a77995-lvds", .data = &rcar_lvds_r8a77995_info },
  652. { }
  653. };
  654. MODULE_DEVICE_TABLE(of, rcar_lvds_of_table);
  655. static struct platform_driver rcar_lvds_platform_driver = {
  656. .probe = rcar_lvds_probe,
  657. .remove = rcar_lvds_remove,
  658. .driver = {
  659. .name = "rcar-lvds",
  660. .of_match_table = rcar_lvds_of_table,
  661. },
  662. };
  663. module_platform_driver(rcar_lvds_platform_driver);
  664. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  665. MODULE_DESCRIPTION("Renesas R-Car LVDS Encoder Driver");
  666. MODULE_LICENSE("GPL");