xref: /linux/drivers/gpu/drm/stm/dw_mipi_dsi-stm.c (revision 0c259ab1914664a9865ddebe9baf66e0b5a25b08)
1ec17f034SBenjamin Gaignard // SPDX-License-Identifier: GPL-2.0
2c1c026dbSPhilippe CORNU /*
3c1c026dbSPhilippe CORNU  * Copyright (C) STMicroelectronics SA 2017
4c1c026dbSPhilippe CORNU  *
5c1c026dbSPhilippe CORNU  * Authors: Philippe Cornu <philippe.cornu@st.com>
6c1c026dbSPhilippe CORNU  *          Yannick Fertre <yannick.fertre@st.com>
7c1c026dbSPhilippe CORNU  */
8c1c026dbSPhilippe CORNU 
9c1c026dbSPhilippe CORNU #include <linux/clk.h>
10c1c026dbSPhilippe CORNU #include <linux/iopoll.h>
1199a93888SSam Ravnborg #include <linux/mod_devicetable.h>
12c1c026dbSPhilippe CORNU #include <linux/module.h>
1399a93888SSam Ravnborg #include <linux/platform_device.h>
14f569aa9bSYannick Fertré #include <linux/regulator/consumer.h>
1599a93888SSam Ravnborg 
16c1c026dbSPhilippe CORNU #include <video/mipi_display.h>
17c1c026dbSPhilippe CORNU 
1899a93888SSam Ravnborg #include <drm/bridge/dw_mipi_dsi.h>
1999a93888SSam Ravnborg #include <drm/drm_mipi_dsi.h>
2099a93888SSam Ravnborg #include <drm/drm_print.h>
2199a93888SSam Ravnborg 
22023f3489SPhilippe CORNU #define HWVER_130			0x31333000	/* IP version 1.30 */
23023f3489SPhilippe CORNU #define HWVER_131			0x31333100	/* IP version 1.31 */
24023f3489SPhilippe CORNU 
25023f3489SPhilippe CORNU /* DSI digital registers & bit definitions */
26023f3489SPhilippe CORNU #define DSI_VERSION			0x00
27023f3489SPhilippe CORNU #define VERSION				GENMASK(31, 8)
28023f3489SPhilippe CORNU 
29023f3489SPhilippe CORNU /* DSI wrapper registers & bit definitions */
30c1c026dbSPhilippe CORNU /* Note: registers are named as in the Reference Manual */
31c1c026dbSPhilippe CORNU #define DSI_WCFGR	0x0400		/* Wrapper ConFiGuration Reg */
32c1c026dbSPhilippe CORNU #define WCFGR_DSIM	BIT(0)		/* DSI Mode */
33c1c026dbSPhilippe CORNU #define WCFGR_COLMUX	GENMASK(3, 1)	/* COLor MUltipleXing */
34c1c026dbSPhilippe CORNU 
35c1c026dbSPhilippe CORNU #define DSI_WCR		0x0404		/* Wrapper Control Reg */
36c1c026dbSPhilippe CORNU #define WCR_DSIEN	BIT(3)		/* DSI ENable */
37c1c026dbSPhilippe CORNU 
38c1c026dbSPhilippe CORNU #define DSI_WISR	0x040C		/* Wrapper Interrupt and Status Reg */
39c1c026dbSPhilippe CORNU #define WISR_PLLLS	BIT(8)		/* PLL Lock Status */
40c1c026dbSPhilippe CORNU #define WISR_RRS	BIT(12)		/* Regulator Ready Status */
41c1c026dbSPhilippe CORNU 
42c1c026dbSPhilippe CORNU #define DSI_WPCR0	0x0418		/* Wrapper Phy Conf Reg 0 */
43c1c026dbSPhilippe CORNU #define WPCR0_UIX4	GENMASK(5, 0)	/* Unit Interval X 4 */
44c1c026dbSPhilippe CORNU #define WPCR0_TDDL	BIT(16)		/* Turn Disable Data Lanes */
45c1c026dbSPhilippe CORNU 
46c1c026dbSPhilippe CORNU #define DSI_WRPCR	0x0430		/* Wrapper Regulator & Pll Ctrl Reg */
47c1c026dbSPhilippe CORNU #define WRPCR_PLLEN	BIT(0)		/* PLL ENable */
48c1c026dbSPhilippe CORNU #define WRPCR_NDIV	GENMASK(8, 2)	/* pll loop DIVision Factor */
49c1c026dbSPhilippe CORNU #define WRPCR_IDF	GENMASK(14, 11)	/* pll Input Division Factor */
50c1c026dbSPhilippe CORNU #define WRPCR_ODF	GENMASK(17, 16)	/* pll Output Division Factor */
51c1c026dbSPhilippe CORNU #define WRPCR_REGEN	BIT(24)		/* REGulator ENable */
52c1c026dbSPhilippe CORNU #define WRPCR_BGREN	BIT(28)		/* BandGap Reference ENable */
53c1c026dbSPhilippe CORNU #define IDF_MIN		1
54c1c026dbSPhilippe CORNU #define IDF_MAX		7
55c1c026dbSPhilippe CORNU #define NDIV_MIN	10
56c1c026dbSPhilippe CORNU #define NDIV_MAX	125
57c1c026dbSPhilippe CORNU #define ODF_MIN		1
58c1c026dbSPhilippe CORNU #define ODF_MAX		8
59c1c026dbSPhilippe CORNU 
60c1c026dbSPhilippe CORNU /* dsi color format coding according to the datasheet */
61c1c026dbSPhilippe CORNU enum dsi_color {
62c1c026dbSPhilippe CORNU 	DSI_RGB565_CONF1,
63c1c026dbSPhilippe CORNU 	DSI_RGB565_CONF2,
64c1c026dbSPhilippe CORNU 	DSI_RGB565_CONF3,
65c1c026dbSPhilippe CORNU 	DSI_RGB666_CONF1,
66c1c026dbSPhilippe CORNU 	DSI_RGB666_CONF2,
67c1c026dbSPhilippe CORNU 	DSI_RGB888,
68c1c026dbSPhilippe CORNU };
69c1c026dbSPhilippe CORNU 
70c1c026dbSPhilippe CORNU #define LANE_MIN_KBPS	31250
71c1c026dbSPhilippe CORNU #define LANE_MAX_KBPS	500000
72c1c026dbSPhilippe CORNU 
73c1c026dbSPhilippe CORNU /* Sleep & timeout for regulator on/off, pll lock/unlock & fifo empty */
74c1c026dbSPhilippe CORNU #define SLEEP_US	1000
75c1c026dbSPhilippe CORNU #define TIMEOUT_US	200000
76c1c026dbSPhilippe CORNU 
77c1c026dbSPhilippe CORNU struct dw_mipi_dsi_stm {
78c1c026dbSPhilippe CORNU 	void __iomem *base;
79c1c026dbSPhilippe CORNU 	struct clk *pllref_clk;
808242ecbdSBrian Norris 	struct dw_mipi_dsi *dsi;
81023f3489SPhilippe CORNU 	u32 hw_version;
82023f3489SPhilippe CORNU 	int lane_min_kbps;
83023f3489SPhilippe CORNU 	int lane_max_kbps;
84f569aa9bSYannick Fertré 	struct regulator *vdd_supply;
85c1c026dbSPhilippe CORNU };
86c1c026dbSPhilippe CORNU 
87c1c026dbSPhilippe CORNU static inline void dsi_write(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 val)
88c1c026dbSPhilippe CORNU {
89c1c026dbSPhilippe CORNU 	writel(val, dsi->base + reg);
90c1c026dbSPhilippe CORNU }
91c1c026dbSPhilippe CORNU 
92c1c026dbSPhilippe CORNU static inline u32 dsi_read(struct dw_mipi_dsi_stm *dsi, u32 reg)
93c1c026dbSPhilippe CORNU {
94c1c026dbSPhilippe CORNU 	return readl(dsi->base + reg);
95c1c026dbSPhilippe CORNU }
96c1c026dbSPhilippe CORNU 
97c1c026dbSPhilippe CORNU static inline void dsi_set(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 mask)
98c1c026dbSPhilippe CORNU {
99c1c026dbSPhilippe CORNU 	dsi_write(dsi, reg, dsi_read(dsi, reg) | mask);
100c1c026dbSPhilippe CORNU }
101c1c026dbSPhilippe CORNU 
102c1c026dbSPhilippe CORNU static inline void dsi_clear(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 mask)
103c1c026dbSPhilippe CORNU {
104c1c026dbSPhilippe CORNU 	dsi_write(dsi, reg, dsi_read(dsi, reg) & ~mask);
105c1c026dbSPhilippe CORNU }
106c1c026dbSPhilippe CORNU 
107c1c026dbSPhilippe CORNU static inline void dsi_update_bits(struct dw_mipi_dsi_stm *dsi, u32 reg,
108c1c026dbSPhilippe CORNU 				   u32 mask, u32 val)
109c1c026dbSPhilippe CORNU {
110c1c026dbSPhilippe CORNU 	dsi_write(dsi, reg, (dsi_read(dsi, reg) & ~mask) | val);
111c1c026dbSPhilippe CORNU }
112c1c026dbSPhilippe CORNU 
113c1c026dbSPhilippe CORNU static enum dsi_color dsi_color_from_mipi(enum mipi_dsi_pixel_format fmt)
114c1c026dbSPhilippe CORNU {
115c1c026dbSPhilippe CORNU 	switch (fmt) {
116c1c026dbSPhilippe CORNU 	case MIPI_DSI_FMT_RGB888:
117c1c026dbSPhilippe CORNU 		return DSI_RGB888;
118c1c026dbSPhilippe CORNU 	case MIPI_DSI_FMT_RGB666:
119c1c026dbSPhilippe CORNU 		return DSI_RGB666_CONF2;
120c1c026dbSPhilippe CORNU 	case MIPI_DSI_FMT_RGB666_PACKED:
121c1c026dbSPhilippe CORNU 		return DSI_RGB666_CONF1;
122c1c026dbSPhilippe CORNU 	case MIPI_DSI_FMT_RGB565:
123c1c026dbSPhilippe CORNU 		return DSI_RGB565_CONF1;
124c1c026dbSPhilippe CORNU 	default:
125c1c026dbSPhilippe CORNU 		DRM_DEBUG_DRIVER("MIPI color invalid, so we use rgb888\n");
126c1c026dbSPhilippe CORNU 	}
127c1c026dbSPhilippe CORNU 	return DSI_RGB888;
128c1c026dbSPhilippe CORNU }
129c1c026dbSPhilippe CORNU 
130c1c026dbSPhilippe CORNU static int dsi_pll_get_clkout_khz(int clkin_khz, int idf, int ndiv, int odf)
131c1c026dbSPhilippe CORNU {
1323ff558e7SArnd Bergmann 	int divisor = idf * odf;
133c1c026dbSPhilippe CORNU 
1343ff558e7SArnd Bergmann 	/* prevent from division by 0 */
1353ff558e7SArnd Bergmann 	if (!divisor)
136c1c026dbSPhilippe CORNU 		return 0;
1373ff558e7SArnd Bergmann 
1383ff558e7SArnd Bergmann 	return DIV_ROUND_CLOSEST(clkin_khz * ndiv, divisor);
139c1c026dbSPhilippe CORNU }
140c1c026dbSPhilippe CORNU 
141023f3489SPhilippe CORNU static int dsi_pll_get_params(struct dw_mipi_dsi_stm *dsi,
142023f3489SPhilippe CORNU 			      int clkin_khz, int clkout_khz,
143c1c026dbSPhilippe CORNU 			      int *idf, int *ndiv, int *odf)
144c1c026dbSPhilippe CORNU {
145c1c026dbSPhilippe CORNU 	int i, o, n, n_min, n_max;
146c1c026dbSPhilippe CORNU 	int fvco_min, fvco_max, delta, best_delta; /* all in khz */
147c1c026dbSPhilippe CORNU 
148c1c026dbSPhilippe CORNU 	/* Early checks preventing division by 0 & odd results */
1490163d1f6SPhilippe CORNU 	if (clkin_khz <= 0 || clkout_khz <= 0)
150c1c026dbSPhilippe CORNU 		return -EINVAL;
151c1c026dbSPhilippe CORNU 
152023f3489SPhilippe CORNU 	fvco_min = dsi->lane_min_kbps * 2 * ODF_MAX;
153023f3489SPhilippe CORNU 	fvco_max = dsi->lane_max_kbps * 2 * ODF_MIN;
154c1c026dbSPhilippe CORNU 
155c1c026dbSPhilippe CORNU 	best_delta = 1000000; /* big started value (1000000khz) */
156c1c026dbSPhilippe CORNU 
157c1c026dbSPhilippe CORNU 	for (i = IDF_MIN; i <= IDF_MAX; i++) {
158c1c026dbSPhilippe CORNU 		/* Compute ndiv range according to Fvco */
159c1c026dbSPhilippe CORNU 		n_min = ((fvco_min * i) / (2 * clkin_khz)) + 1;
160c1c026dbSPhilippe CORNU 		n_max = (fvco_max * i) / (2 * clkin_khz);
161c1c026dbSPhilippe CORNU 
162c1c026dbSPhilippe CORNU 		/* No need to continue idf loop if we reach ndiv max */
163c1c026dbSPhilippe CORNU 		if (n_min >= NDIV_MAX)
164c1c026dbSPhilippe CORNU 			break;
165c1c026dbSPhilippe CORNU 
166c1c026dbSPhilippe CORNU 		/* Clamp ndiv to valid values */
167c1c026dbSPhilippe CORNU 		if (n_min < NDIV_MIN)
168c1c026dbSPhilippe CORNU 			n_min = NDIV_MIN;
169c1c026dbSPhilippe CORNU 		if (n_max > NDIV_MAX)
170c1c026dbSPhilippe CORNU 			n_max = NDIV_MAX;
171c1c026dbSPhilippe CORNU 
172c1c026dbSPhilippe CORNU 		for (o = ODF_MIN; o <= ODF_MAX; o *= 2) {
173c1c026dbSPhilippe CORNU 			n = DIV_ROUND_CLOSEST(i * o * clkout_khz, clkin_khz);
174c1c026dbSPhilippe CORNU 			/* Check ndiv according to vco range */
1750163d1f6SPhilippe CORNU 			if (n < n_min || n > n_max)
176c1c026dbSPhilippe CORNU 				continue;
177c1c026dbSPhilippe CORNU 			/* Check if new delta is better & saves parameters */
178c1c026dbSPhilippe CORNU 			delta = dsi_pll_get_clkout_khz(clkin_khz, i, n, o) -
179c1c026dbSPhilippe CORNU 				clkout_khz;
180c1c026dbSPhilippe CORNU 			if (delta < 0)
181c1c026dbSPhilippe CORNU 				delta = -delta;
182c1c026dbSPhilippe CORNU 			if (delta < best_delta) {
183c1c026dbSPhilippe CORNU 				*idf = i;
184c1c026dbSPhilippe CORNU 				*ndiv = n;
185c1c026dbSPhilippe CORNU 				*odf = o;
186c1c026dbSPhilippe CORNU 				best_delta = delta;
187c1c026dbSPhilippe CORNU 			}
188c1c026dbSPhilippe CORNU 			/* fast return in case of "perfect result" */
189c1c026dbSPhilippe CORNU 			if (!delta)
190c1c026dbSPhilippe CORNU 				return 0;
191c1c026dbSPhilippe CORNU 		}
192c1c026dbSPhilippe CORNU 	}
193c1c026dbSPhilippe CORNU 
194c1c026dbSPhilippe CORNU 	return 0;
195c1c026dbSPhilippe CORNU }
196c1c026dbSPhilippe CORNU 
197c1c026dbSPhilippe CORNU static int dw_mipi_dsi_phy_init(void *priv_data)
198c1c026dbSPhilippe CORNU {
199c1c026dbSPhilippe CORNU 	struct dw_mipi_dsi_stm *dsi = priv_data;
200c1c026dbSPhilippe CORNU 	u32 val;
201c1c026dbSPhilippe CORNU 	int ret;
202c1c026dbSPhilippe CORNU 
203c1c026dbSPhilippe CORNU 	/* Enable the regulator */
204c1c026dbSPhilippe CORNU 	dsi_set(dsi, DSI_WRPCR, WRPCR_REGEN | WRPCR_BGREN);
205c1c026dbSPhilippe CORNU 	ret = readl_poll_timeout(dsi->base + DSI_WISR, val, val & WISR_RRS,
206c1c026dbSPhilippe CORNU 				 SLEEP_US, TIMEOUT_US);
207c1c026dbSPhilippe CORNU 	if (ret)
208c1c026dbSPhilippe CORNU 		DRM_DEBUG_DRIVER("!TIMEOUT! waiting REGU, let's continue\n");
209c1c026dbSPhilippe CORNU 
210c1c026dbSPhilippe CORNU 	/* Enable the DSI PLL & wait for its lock */
211c1c026dbSPhilippe CORNU 	dsi_set(dsi, DSI_WRPCR, WRPCR_PLLEN);
212c1c026dbSPhilippe CORNU 	ret = readl_poll_timeout(dsi->base + DSI_WISR, val, val & WISR_PLLLS,
213c1c026dbSPhilippe CORNU 				 SLEEP_US, TIMEOUT_US);
214c1c026dbSPhilippe CORNU 	if (ret)
215c1c026dbSPhilippe CORNU 		DRM_DEBUG_DRIVER("!TIMEOUT! waiting PLL, let's continue\n");
216c1c026dbSPhilippe CORNU 
217ee7668bcSYannick Fertré 	return 0;
218ee7668bcSYannick Fertré }
219ee7668bcSYannick Fertré 
220ee7668bcSYannick Fertré static void dw_mipi_dsi_phy_power_on(void *priv_data)
221ee7668bcSYannick Fertré {
222ee7668bcSYannick Fertré 	struct dw_mipi_dsi_stm *dsi = priv_data;
223ee7668bcSYannick Fertré 
224ee7668bcSYannick Fertré 	DRM_DEBUG_DRIVER("\n");
225ee7668bcSYannick Fertré 
226c1c026dbSPhilippe CORNU 	/* Enable the DSI wrapper */
227c1c026dbSPhilippe CORNU 	dsi_set(dsi, DSI_WCR, WCR_DSIEN);
228ee7668bcSYannick Fertré }
229c1c026dbSPhilippe CORNU 
230ee7668bcSYannick Fertré static void dw_mipi_dsi_phy_power_off(void *priv_data)
231ee7668bcSYannick Fertré {
232ee7668bcSYannick Fertré 	struct dw_mipi_dsi_stm *dsi = priv_data;
233ee7668bcSYannick Fertré 
234ee7668bcSYannick Fertré 	DRM_DEBUG_DRIVER("\n");
235ee7668bcSYannick Fertré 
236ee7668bcSYannick Fertré 	/* Disable the DSI wrapper */
237ee7668bcSYannick Fertré 	dsi_clear(dsi, DSI_WCR, WCR_DSIEN);
238c1c026dbSPhilippe CORNU }
239c1c026dbSPhilippe CORNU 
240c1c026dbSPhilippe CORNU static int
24163f8f3baSLaurent Pinchart dw_mipi_dsi_get_lane_mbps(void *priv_data, const struct drm_display_mode *mode,
242c1c026dbSPhilippe CORNU 			  unsigned long mode_flags, u32 lanes, u32 format,
243c1c026dbSPhilippe CORNU 			  unsigned int *lane_mbps)
244c1c026dbSPhilippe CORNU {
245c1c026dbSPhilippe CORNU 	struct dw_mipi_dsi_stm *dsi = priv_data;
246c1c026dbSPhilippe CORNU 	unsigned int idf, ndiv, odf, pll_in_khz, pll_out_khz;
247c1c026dbSPhilippe CORNU 	int ret, bpp;
248c1c026dbSPhilippe CORNU 	u32 val;
249c1c026dbSPhilippe CORNU 
250c1c026dbSPhilippe CORNU 	pll_in_khz = (unsigned int)(clk_get_rate(dsi->pllref_clk) / 1000);
251c1c026dbSPhilippe CORNU 
252c1c026dbSPhilippe CORNU 	/* Compute requested pll out */
253c1c026dbSPhilippe CORNU 	bpp = mipi_dsi_pixel_format_to_bpp(format);
254c1c026dbSPhilippe CORNU 	pll_out_khz = mode->clock * bpp / lanes;
2551e696204SYannick Fertré 
256c1c026dbSPhilippe CORNU 	/* Add 20% to pll out to be higher than pixel bw (burst mode only) */
2571e696204SYannick Fertré 	if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
258c1c026dbSPhilippe CORNU 		pll_out_khz = (pll_out_khz * 12) / 10;
2591e696204SYannick Fertré 
260023f3489SPhilippe CORNU 	if (pll_out_khz > dsi->lane_max_kbps) {
261023f3489SPhilippe CORNU 		pll_out_khz = dsi->lane_max_kbps;
262c1c026dbSPhilippe CORNU 		DRM_WARN("Warning max phy mbps is used\n");
263c1c026dbSPhilippe CORNU 	}
264023f3489SPhilippe CORNU 	if (pll_out_khz < dsi->lane_min_kbps) {
265023f3489SPhilippe CORNU 		pll_out_khz = dsi->lane_min_kbps;
266c1c026dbSPhilippe CORNU 		DRM_WARN("Warning min phy mbps is used\n");
267c1c026dbSPhilippe CORNU 	}
268c1c026dbSPhilippe CORNU 
269c1c026dbSPhilippe CORNU 	/* Compute best pll parameters */
270c1c026dbSPhilippe CORNU 	idf = 0;
271c1c026dbSPhilippe CORNU 	ndiv = 0;
272c1c026dbSPhilippe CORNU 	odf = 0;
273023f3489SPhilippe CORNU 	ret = dsi_pll_get_params(dsi, pll_in_khz, pll_out_khz,
274023f3489SPhilippe CORNU 				 &idf, &ndiv, &odf);
275c1c026dbSPhilippe CORNU 	if (ret)
276c1c026dbSPhilippe CORNU 		DRM_WARN("Warning dsi_pll_get_params(): bad params\n");
277c1c026dbSPhilippe CORNU 
278c1c026dbSPhilippe CORNU 	/* Get the adjusted pll out value */
279c1c026dbSPhilippe CORNU 	pll_out_khz = dsi_pll_get_clkout_khz(pll_in_khz, idf, ndiv, odf);
280c1c026dbSPhilippe CORNU 
281c1c026dbSPhilippe CORNU 	/* Set the PLL division factors */
282c1c026dbSPhilippe CORNU 	dsi_update_bits(dsi, DSI_WRPCR,	WRPCR_NDIV | WRPCR_IDF | WRPCR_ODF,
283c1c026dbSPhilippe CORNU 			(ndiv << 2) | (idf << 11) | ((ffs(odf) - 1) << 16));
284c1c026dbSPhilippe CORNU 
285c1c026dbSPhilippe CORNU 	/* Compute uix4 & set the bit period in high-speed mode */
286c1c026dbSPhilippe CORNU 	val = 4000000 / pll_out_khz;
287c1c026dbSPhilippe CORNU 	dsi_update_bits(dsi, DSI_WPCR0, WPCR0_UIX4, val);
288c1c026dbSPhilippe CORNU 
289c1c026dbSPhilippe CORNU 	/* Select video mode by resetting DSIM bit */
290c1c026dbSPhilippe CORNU 	dsi_clear(dsi, DSI_WCFGR, WCFGR_DSIM);
291c1c026dbSPhilippe CORNU 
292c1c026dbSPhilippe CORNU 	/* Select the color coding */
293c1c026dbSPhilippe CORNU 	dsi_update_bits(dsi, DSI_WCFGR, WCFGR_COLMUX,
294c1c026dbSPhilippe CORNU 			dsi_color_from_mipi(format) << 1);
295c1c026dbSPhilippe CORNU 
296c1c026dbSPhilippe CORNU 	*lane_mbps = pll_out_khz / 1000;
297c1c026dbSPhilippe CORNU 
298c1c026dbSPhilippe CORNU 	DRM_DEBUG_DRIVER("pll_in %ukHz pll_out %ukHz lane_mbps %uMHz\n",
299c1c026dbSPhilippe CORNU 			 pll_in_khz, pll_out_khz, *lane_mbps);
300c1c026dbSPhilippe CORNU 
301c1c026dbSPhilippe CORNU 	return 0;
302c1c026dbSPhilippe CORNU }
303c1c026dbSPhilippe CORNU 
3045cc4e71fSAntonio Borneo #define DSI_PHY_DELAY(fp, vp, mbps) DIV_ROUND_UP((fp) * (mbps) + 1000 * (vp), 8000)
3055cc4e71fSAntonio Borneo 
30625ed8aebSHeiko Stuebner static int
30725ed8aebSHeiko Stuebner dw_mipi_dsi_phy_get_timing(void *priv_data, unsigned int lane_mbps,
30825ed8aebSHeiko Stuebner 			   struct dw_mipi_dsi_dphy_timing *timing)
30925ed8aebSHeiko Stuebner {
3105cc4e71fSAntonio Borneo 	/*
3115cc4e71fSAntonio Borneo 	 * From STM32MP157 datasheet, valid for STM32F469, STM32F7x9, STM32H747
3125cc4e71fSAntonio Borneo 	 * phy_clkhs2lp_time = (272+136*UI)/(8*UI)
3135cc4e71fSAntonio Borneo 	 * phy_clklp2hs_time = (512+40*UI)/(8*UI)
3145cc4e71fSAntonio Borneo 	 * phy_hs2lp_time = (192+64*UI)/(8*UI)
3155cc4e71fSAntonio Borneo 	 * phy_lp2hs_time = (256+32*UI)/(8*UI)
3165cc4e71fSAntonio Borneo 	 */
3175cc4e71fSAntonio Borneo 	timing->clk_hs2lp = DSI_PHY_DELAY(272, 136, lane_mbps);
3185cc4e71fSAntonio Borneo 	timing->clk_lp2hs = DSI_PHY_DELAY(512, 40, lane_mbps);
3195cc4e71fSAntonio Borneo 	timing->data_hs2lp = DSI_PHY_DELAY(192, 64, lane_mbps);
3205cc4e71fSAntonio Borneo 	timing->data_lp2hs = DSI_PHY_DELAY(256, 32, lane_mbps);
32125ed8aebSHeiko Stuebner 
32225ed8aebSHeiko Stuebner 	return 0;
32325ed8aebSHeiko Stuebner }
32425ed8aebSHeiko Stuebner 
325e01356d1SAntonio Borneo #define CLK_TOLERANCE_HZ 50
326e01356d1SAntonio Borneo 
327e01356d1SAntonio Borneo static enum drm_mode_status
328e01356d1SAntonio Borneo dw_mipi_dsi_stm_mode_valid(void *priv_data,
329e01356d1SAntonio Borneo 			   const struct drm_display_mode *mode,
330e01356d1SAntonio Borneo 			   unsigned long mode_flags, u32 lanes, u32 format)
331e01356d1SAntonio Borneo {
332e01356d1SAntonio Borneo 	struct dw_mipi_dsi_stm *dsi = priv_data;
333e01356d1SAntonio Borneo 	unsigned int idf, ndiv, odf, pll_in_khz, pll_out_khz;
334e01356d1SAntonio Borneo 	int ret, bpp;
335e01356d1SAntonio Borneo 
336e01356d1SAntonio Borneo 	bpp = mipi_dsi_pixel_format_to_bpp(format);
337e01356d1SAntonio Borneo 	if (bpp < 0)
338e01356d1SAntonio Borneo 		return MODE_BAD;
339e01356d1SAntonio Borneo 
340e01356d1SAntonio Borneo 	/* Compute requested pll out */
341e01356d1SAntonio Borneo 	pll_out_khz = mode->clock * bpp / lanes;
342e01356d1SAntonio Borneo 
343e01356d1SAntonio Borneo 	if (pll_out_khz > dsi->lane_max_kbps)
344e01356d1SAntonio Borneo 		return MODE_CLOCK_HIGH;
345e01356d1SAntonio Borneo 
346e01356d1SAntonio Borneo 	if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST) {
347e01356d1SAntonio Borneo 		/* Add 20% to pll out to be higher than pixel bw */
348e01356d1SAntonio Borneo 		pll_out_khz = (pll_out_khz * 12) / 10;
349e01356d1SAntonio Borneo 	} else {
350e01356d1SAntonio Borneo 		if (pll_out_khz < dsi->lane_min_kbps)
351e01356d1SAntonio Borneo 			return MODE_CLOCK_LOW;
352e01356d1SAntonio Borneo 	}
353e01356d1SAntonio Borneo 
354e01356d1SAntonio Borneo 	/* Compute best pll parameters */
355e01356d1SAntonio Borneo 	idf = 0;
356e01356d1SAntonio Borneo 	ndiv = 0;
357e01356d1SAntonio Borneo 	odf = 0;
358e01356d1SAntonio Borneo 	pll_in_khz = clk_get_rate(dsi->pllref_clk) / 1000;
359e01356d1SAntonio Borneo 	ret = dsi_pll_get_params(dsi, pll_in_khz, pll_out_khz, &idf, &ndiv, &odf);
360e01356d1SAntonio Borneo 	if (ret) {
361e01356d1SAntonio Borneo 		DRM_WARN("Warning dsi_pll_get_params(): bad params\n");
362e01356d1SAntonio Borneo 		return MODE_ERROR;
363e01356d1SAntonio Borneo 	}
364e01356d1SAntonio Borneo 
365e01356d1SAntonio Borneo 	if (!(mode_flags & MIPI_DSI_MODE_VIDEO_BURST)) {
366e01356d1SAntonio Borneo 		unsigned int px_clock_hz, target_px_clock_hz, lane_mbps;
367e01356d1SAntonio Borneo 		int dsi_short_packet_size_px, hfp, hsync, hbp, delay_to_lp;
368e01356d1SAntonio Borneo 		struct dw_mipi_dsi_dphy_timing dphy_timing;
369e01356d1SAntonio Borneo 
370e01356d1SAntonio Borneo 		/* Get the adjusted pll out value */
371e01356d1SAntonio Borneo 		pll_out_khz = dsi_pll_get_clkout_khz(pll_in_khz, idf, ndiv, odf);
372e01356d1SAntonio Borneo 
373e01356d1SAntonio Borneo 		px_clock_hz = DIV_ROUND_CLOSEST_ULL(1000ULL * pll_out_khz * lanes, bpp);
374e01356d1SAntonio Borneo 		target_px_clock_hz = mode->clock * 1000;
375e01356d1SAntonio Borneo 		/*
376e01356d1SAntonio Borneo 		 * Filter modes according to the clock value, particularly useful for
377e01356d1SAntonio Borneo 		 * hdmi modes that require precise pixel clocks.
378e01356d1SAntonio Borneo 		 */
379e01356d1SAntonio Borneo 		if (px_clock_hz < target_px_clock_hz - CLK_TOLERANCE_HZ ||
380e01356d1SAntonio Borneo 		    px_clock_hz > target_px_clock_hz + CLK_TOLERANCE_HZ)
381e01356d1SAntonio Borneo 			return MODE_CLOCK_RANGE;
382e01356d1SAntonio Borneo 
383e01356d1SAntonio Borneo 		/* sync packets are codes as DSI short packets (4 bytes) */
384e01356d1SAntonio Borneo 		dsi_short_packet_size_px = DIV_ROUND_UP(4 * BITS_PER_BYTE, bpp);
385e01356d1SAntonio Borneo 
386e01356d1SAntonio Borneo 		hfp = mode->hsync_start - mode->hdisplay;
387e01356d1SAntonio Borneo 		hsync = mode->hsync_end - mode->hsync_start;
388e01356d1SAntonio Borneo 		hbp = mode->htotal - mode->hsync_end;
389e01356d1SAntonio Borneo 
390e01356d1SAntonio Borneo 		/* hsync must be longer than 4 bytes HSS packets */
391e01356d1SAntonio Borneo 		if (hsync < dsi_short_packet_size_px)
392e01356d1SAntonio Borneo 			return MODE_HSYNC_NARROW;
393e01356d1SAntonio Borneo 
394e01356d1SAntonio Borneo 		if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
395e01356d1SAntonio Borneo 			/* HBP must be longer than 4 bytes HSE packets */
396e01356d1SAntonio Borneo 			if (hbp < dsi_short_packet_size_px)
397e01356d1SAntonio Borneo 				return MODE_HSYNC_NARROW;
398e01356d1SAntonio Borneo 			hbp -= dsi_short_packet_size_px;
399e01356d1SAntonio Borneo 		} else {
400e01356d1SAntonio Borneo 			/* With sync events HBP extends in the hsync */
401e01356d1SAntonio Borneo 			hbp += hsync - dsi_short_packet_size_px;
402e01356d1SAntonio Borneo 		}
403e01356d1SAntonio Borneo 
404e01356d1SAntonio Borneo 		lane_mbps = pll_out_khz / 1000;
405e01356d1SAntonio Borneo 		ret = dw_mipi_dsi_phy_get_timing(priv_data, lane_mbps, &dphy_timing);
406e01356d1SAntonio Borneo 		if (ret)
407e01356d1SAntonio Borneo 			return MODE_ERROR;
408e01356d1SAntonio Borneo 		/*
409e01356d1SAntonio Borneo 		 * In non-burst mode DSI has to enter in LP during HFP
410e01356d1SAntonio Borneo 		 * (horizontal front porch) or HBP (horizontal back porch) to
411e01356d1SAntonio Borneo 		 * resync with LTDC pixel clock.
412e01356d1SAntonio Borneo 		 */
413e01356d1SAntonio Borneo 		delay_to_lp = DIV_ROUND_UP((dphy_timing.data_hs2lp + dphy_timing.data_lp2hs) *
414e01356d1SAntonio Borneo 					   lanes * BITS_PER_BYTE, bpp);
415e01356d1SAntonio Borneo 		if (hfp < delay_to_lp && hbp < delay_to_lp)
416e01356d1SAntonio Borneo 			return MODE_HSYNC;
417e01356d1SAntonio Borneo 	}
418e01356d1SAntonio Borneo 
419e01356d1SAntonio Borneo 	return MODE_OK;
420e01356d1SAntonio Borneo }
421e01356d1SAntonio Borneo 
42289a15e6fSPhilippe CORNU static const struct dw_mipi_dsi_phy_ops dw_mipi_dsi_stm_phy_ops = {
423c1c026dbSPhilippe CORNU 	.init = dw_mipi_dsi_phy_init,
424ee7668bcSYannick Fertré 	.power_on = dw_mipi_dsi_phy_power_on,
425ee7668bcSYannick Fertré 	.power_off = dw_mipi_dsi_phy_power_off,
426c1c026dbSPhilippe CORNU 	.get_lane_mbps = dw_mipi_dsi_get_lane_mbps,
42725ed8aebSHeiko Stuebner 	.get_timing = dw_mipi_dsi_phy_get_timing,
428c1c026dbSPhilippe CORNU };
429c1c026dbSPhilippe CORNU 
430c1c026dbSPhilippe CORNU static struct dw_mipi_dsi_plat_data dw_mipi_dsi_stm_plat_data = {
431c1c026dbSPhilippe CORNU 	.max_data_lanes = 2,
432e01356d1SAntonio Borneo 	.mode_valid = dw_mipi_dsi_stm_mode_valid,
433c1c026dbSPhilippe CORNU 	.phy_ops = &dw_mipi_dsi_stm_phy_ops,
434c1c026dbSPhilippe CORNU };
435c1c026dbSPhilippe CORNU 
436c1c026dbSPhilippe CORNU static const struct of_device_id dw_mipi_dsi_stm_dt_ids[] = {
437c1c026dbSPhilippe CORNU 	{ .compatible = "st,stm32-dsi", .data = &dw_mipi_dsi_stm_plat_data, },
438c1c026dbSPhilippe CORNU 	{ },
439c1c026dbSPhilippe CORNU };
440c1c026dbSPhilippe CORNU MODULE_DEVICE_TABLE(of, dw_mipi_dsi_stm_dt_ids);
441c1c026dbSPhilippe CORNU 
442c1c026dbSPhilippe CORNU static int dw_mipi_dsi_stm_probe(struct platform_device *pdev)
443c1c026dbSPhilippe CORNU {
444c1c026dbSPhilippe CORNU 	struct device *dev = &pdev->dev;
445c1c026dbSPhilippe CORNU 	struct dw_mipi_dsi_stm *dsi;
446fa6251a7SYannick Fertré 	struct clk *pclk;
447c1c026dbSPhilippe CORNU 	int ret;
448c1c026dbSPhilippe CORNU 
449c1c026dbSPhilippe CORNU 	dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
450c1c026dbSPhilippe CORNU 	if (!dsi)
451c1c026dbSPhilippe CORNU 		return -ENOMEM;
452c1c026dbSPhilippe CORNU 
45334235f54SYang Li 	dsi->base = devm_platform_ioremap_resource(pdev, 0);
454c1c026dbSPhilippe CORNU 	if (IS_ERR(dsi->base)) {
455f569aa9bSYannick Fertré 		ret = PTR_ERR(dsi->base);
456f569aa9bSYannick Fertré 		DRM_ERROR("Unable to get dsi registers %d\n", ret);
457f569aa9bSYannick Fertré 		return ret;
458f569aa9bSYannick Fertré 	}
459f569aa9bSYannick Fertré 
460f569aa9bSYannick Fertré 	dsi->vdd_supply = devm_regulator_get(dev, "phy-dsi");
461f569aa9bSYannick Fertré 	if (IS_ERR(dsi->vdd_supply)) {
462f569aa9bSYannick Fertré 		ret = PTR_ERR(dsi->vdd_supply);
463edf20859SYannick Fertre 		dev_err_probe(dev, ret, "Failed to request regulator\n");
464f569aa9bSYannick Fertré 		return ret;
465f569aa9bSYannick Fertré 	}
466f569aa9bSYannick Fertré 
467f569aa9bSYannick Fertré 	ret = regulator_enable(dsi->vdd_supply);
468f569aa9bSYannick Fertré 	if (ret) {
469f569aa9bSYannick Fertré 		DRM_ERROR("Failed to enable regulator: %d\n", ret);
470f569aa9bSYannick Fertré 		return ret;
471c1c026dbSPhilippe CORNU 	}
472c1c026dbSPhilippe CORNU 
473c1c026dbSPhilippe CORNU 	dsi->pllref_clk = devm_clk_get(dev, "ref");
474c1c026dbSPhilippe CORNU 	if (IS_ERR(dsi->pllref_clk)) {
475c1c026dbSPhilippe CORNU 		ret = PTR_ERR(dsi->pllref_clk);
476edf20859SYannick Fertre 		dev_err_probe(dev, ret, "Unable to get pll reference clock\n");
477f569aa9bSYannick Fertré 		goto err_clk_get;
478c1c026dbSPhilippe CORNU 	}
479c1c026dbSPhilippe CORNU 
480c1c026dbSPhilippe CORNU 	ret = clk_prepare_enable(dsi->pllref_clk);
481c1c026dbSPhilippe CORNU 	if (ret) {
482f569aa9bSYannick Fertré 		DRM_ERROR("Failed to enable pllref_clk: %d\n", ret);
483f569aa9bSYannick Fertré 		goto err_clk_get;
484c1c026dbSPhilippe CORNU 	}
485c1c026dbSPhilippe CORNU 
486fa6251a7SYannick Fertré 	pclk = devm_clk_get(dev, "pclk");
487fa6251a7SYannick Fertré 	if (IS_ERR(pclk)) {
488fa6251a7SYannick Fertré 		ret = PTR_ERR(pclk);
489fa6251a7SYannick Fertré 		DRM_ERROR("Unable to get peripheral clock: %d\n", ret);
490fa6251a7SYannick Fertré 		goto err_dsi_probe;
491fa6251a7SYannick Fertré 	}
492fa6251a7SYannick Fertré 
493fa6251a7SYannick Fertré 	ret = clk_prepare_enable(pclk);
494fa6251a7SYannick Fertré 	if (ret) {
495fa6251a7SYannick Fertré 		DRM_ERROR("%s: Failed to enable peripheral clk\n", __func__);
496fa6251a7SYannick Fertré 		goto err_dsi_probe;
497fa6251a7SYannick Fertré 	}
498fa6251a7SYannick Fertré 
499fa6251a7SYannick Fertré 	dsi->hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
500fa6251a7SYannick Fertré 	clk_disable_unprepare(pclk);
501fa6251a7SYannick Fertré 
502fa6251a7SYannick Fertré 	if (dsi->hw_version != HWVER_130 && dsi->hw_version != HWVER_131) {
503fa6251a7SYannick Fertré 		ret = -ENODEV;
504fa6251a7SYannick Fertré 		DRM_ERROR("bad dsi hardware version\n");
505fa6251a7SYannick Fertré 		goto err_dsi_probe;
506fa6251a7SYannick Fertré 	}
507fa6251a7SYannick Fertré 
50822f99f2dSAntonio Borneo 	/* set lane capabilities according to hw version */
50922f99f2dSAntonio Borneo 	dsi->lane_min_kbps = LANE_MIN_KBPS;
51022f99f2dSAntonio Borneo 	dsi->lane_max_kbps = LANE_MAX_KBPS;
51122f99f2dSAntonio Borneo 	if (dsi->hw_version == HWVER_131) {
51222f99f2dSAntonio Borneo 		dsi->lane_min_kbps *= 2;
51322f99f2dSAntonio Borneo 		dsi->lane_max_kbps *= 2;
51422f99f2dSAntonio Borneo 	}
51522f99f2dSAntonio Borneo 
516c1c026dbSPhilippe CORNU 	dw_mipi_dsi_stm_plat_data.base = dsi->base;
517c1c026dbSPhilippe CORNU 	dw_mipi_dsi_stm_plat_data.priv_data = dsi;
518c1c026dbSPhilippe CORNU 
5198242ecbdSBrian Norris 	platform_set_drvdata(pdev, dsi);
5208242ecbdSBrian Norris 
5218242ecbdSBrian Norris 	dsi->dsi = dw_mipi_dsi_probe(pdev, &dw_mipi_dsi_stm_plat_data);
5228242ecbdSBrian Norris 	if (IS_ERR(dsi->dsi)) {
523f569aa9bSYannick Fertré 		ret = PTR_ERR(dsi->dsi);
524edf20859SYannick Fertre 		dev_err_probe(dev, ret, "Failed to initialize mipi dsi host\n");
525f569aa9bSYannick Fertré 		goto err_dsi_probe;
526c1c026dbSPhilippe CORNU 	}
527c1c026dbSPhilippe CORNU 
5288242ecbdSBrian Norris 	return 0;
529f569aa9bSYannick Fertré 
530f569aa9bSYannick Fertré err_dsi_probe:
531f569aa9bSYannick Fertré 	clk_disable_unprepare(dsi->pllref_clk);
532f569aa9bSYannick Fertré err_clk_get:
533f569aa9bSYannick Fertré 	regulator_disable(dsi->vdd_supply);
534f569aa9bSYannick Fertré 
535f569aa9bSYannick Fertré 	return ret;
536c1c026dbSPhilippe CORNU }
537c1c026dbSPhilippe CORNU 
538*0c259ab1SUwe Kleine-König static void dw_mipi_dsi_stm_remove(struct platform_device *pdev)
539c1c026dbSPhilippe CORNU {
5408242ecbdSBrian Norris 	struct dw_mipi_dsi_stm *dsi = platform_get_drvdata(pdev);
541c1c026dbSPhilippe CORNU 
5428242ecbdSBrian Norris 	dw_mipi_dsi_remove(dsi->dsi);
543f569aa9bSYannick Fertré 	clk_disable_unprepare(dsi->pllref_clk);
544f569aa9bSYannick Fertré 	regulator_disable(dsi->vdd_supply);
545c1c026dbSPhilippe CORNU }
546c1c026dbSPhilippe CORNU 
5471861a1ffSYannick Fertré static int __maybe_unused dw_mipi_dsi_stm_suspend(struct device *dev)
5481861a1ffSYannick Fertré {
5491861a1ffSYannick Fertré 	struct dw_mipi_dsi_stm *dsi = dw_mipi_dsi_stm_plat_data.priv_data;
5501861a1ffSYannick Fertré 
5511861a1ffSYannick Fertré 	DRM_DEBUG_DRIVER("\n");
5521861a1ffSYannick Fertré 
5531861a1ffSYannick Fertré 	clk_disable_unprepare(dsi->pllref_clk);
554f569aa9bSYannick Fertré 	regulator_disable(dsi->vdd_supply);
5551861a1ffSYannick Fertré 
5561861a1ffSYannick Fertré 	return 0;
5571861a1ffSYannick Fertré }
5581861a1ffSYannick Fertré 
5591861a1ffSYannick Fertré static int __maybe_unused dw_mipi_dsi_stm_resume(struct device *dev)
5601861a1ffSYannick Fertré {
5611861a1ffSYannick Fertré 	struct dw_mipi_dsi_stm *dsi = dw_mipi_dsi_stm_plat_data.priv_data;
562f569aa9bSYannick Fertré 	int ret;
5631861a1ffSYannick Fertré 
5641861a1ffSYannick Fertré 	DRM_DEBUG_DRIVER("\n");
5651861a1ffSYannick Fertré 
566f569aa9bSYannick Fertré 	ret = regulator_enable(dsi->vdd_supply);
567f569aa9bSYannick Fertré 	if (ret) {
568f569aa9bSYannick Fertré 		DRM_ERROR("Failed to enable regulator: %d\n", ret);
569f569aa9bSYannick Fertré 		return ret;
570f569aa9bSYannick Fertré 	}
571f569aa9bSYannick Fertré 
572f569aa9bSYannick Fertré 	ret = clk_prepare_enable(dsi->pllref_clk);
573f569aa9bSYannick Fertré 	if (ret) {
574f569aa9bSYannick Fertré 		regulator_disable(dsi->vdd_supply);
575f569aa9bSYannick Fertré 		DRM_ERROR("Failed to enable pllref_clk: %d\n", ret);
576f569aa9bSYannick Fertré 		return ret;
577f569aa9bSYannick Fertré 	}
5781861a1ffSYannick Fertré 
5791861a1ffSYannick Fertré 	return 0;
5801861a1ffSYannick Fertré }
5811861a1ffSYannick Fertré 
5821861a1ffSYannick Fertré static const struct dev_pm_ops dw_mipi_dsi_stm_pm_ops = {
5831861a1ffSYannick Fertré 	SET_SYSTEM_SLEEP_PM_OPS(dw_mipi_dsi_stm_suspend,
5841861a1ffSYannick Fertré 				dw_mipi_dsi_stm_resume)
5851861a1ffSYannick Fertré };
5861861a1ffSYannick Fertré 
587c1c026dbSPhilippe CORNU static struct platform_driver dw_mipi_dsi_stm_driver = {
588c1c026dbSPhilippe CORNU 	.probe		= dw_mipi_dsi_stm_probe,
589*0c259ab1SUwe Kleine-König 	.remove_new	= dw_mipi_dsi_stm_remove,
590c1c026dbSPhilippe CORNU 	.driver		= {
591c1c026dbSPhilippe CORNU 		.of_match_table = dw_mipi_dsi_stm_dt_ids,
592bb3fb5e5SPhilippe CORNU 		.name	= "stm32-display-dsi",
5931861a1ffSYannick Fertré 		.pm = &dw_mipi_dsi_stm_pm_ops,
594c1c026dbSPhilippe CORNU 	},
595c1c026dbSPhilippe CORNU };
596c1c026dbSPhilippe CORNU 
597c1c026dbSPhilippe CORNU module_platform_driver(dw_mipi_dsi_stm_driver);
598c1c026dbSPhilippe CORNU 
599c1c026dbSPhilippe CORNU MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
600c1c026dbSPhilippe CORNU MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
601c1c026dbSPhilippe CORNU MODULE_DESCRIPTION("STMicroelectronics DW MIPI DSI host controller driver");
602c1c026dbSPhilippe CORNU MODULE_LICENSE("GPL v2");
603