1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * MIPI-DSI Samsung s6d16d0 panel driver. This is a 864x480
4  * AMOLED panel with a command-only DSI interface.
5  */
6 
7 #include <drm/drm_modes.h>
8 #include <drm/drm_mipi_dsi.h>
9 #include <drm/drm_panel.h>
10 #include <drm/drm_print.h>
11 
12 #include <linux/gpio/consumer.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/delay.h>
15 #include <linux/of_device.h>
16 #include <linux/module.h>
17 
18 struct s6d16d0 {
19 	struct device *dev;
20 	struct drm_panel panel;
21 	struct regulator *supply;
22 	struct gpio_desc *reset_gpio;
23 };
24 
25 /*
26  * The timings are not very helpful as the display is used in
27  * command mode.
28  */
29 static const struct drm_display_mode samsung_s6d16d0_mode = {
30 	/* HS clock, (htotal*vtotal*vrefresh)/1000 */
31 	.clock = 420160,
32 	.hdisplay = 864,
33 	.hsync_start = 864 + 154,
34 	.hsync_end = 864 + 154 + 16,
35 	.htotal = 864 + 154 + 16 + 32,
36 	.vdisplay = 480,
37 	.vsync_start = 480 + 1,
38 	.vsync_end = 480 + 1 + 1,
39 	.vtotal = 480 + 1 + 1 + 1,
40 	/*
41 	 * This depends on the clocking HS vs LP rate, this value
42 	 * is calculated as:
43 	 * vrefresh = (clock * 1000) / (htotal*vtotal)
44 	 */
45 	.vrefresh = 816,
46 	.width_mm = 84,
47 	.height_mm = 48,
48 };
49 
panel_to_s6d16d0(struct drm_panel * panel)50 static inline struct s6d16d0 *panel_to_s6d16d0(struct drm_panel *panel)
51 {
52 	return container_of(panel, struct s6d16d0, panel);
53 }
54 
s6d16d0_unprepare(struct drm_panel * panel)55 static int s6d16d0_unprepare(struct drm_panel *panel)
56 {
57 	struct s6d16d0 *s6 = panel_to_s6d16d0(panel);
58 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(s6->dev);
59 	int ret;
60 
61 	/* Enter sleep mode */
62 	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
63 	if (ret) {
64 		DRM_DEV_ERROR(s6->dev, "failed to enter sleep mode (%d)\n",
65 			      ret);
66 		return ret;
67 	}
68 
69 	/* Assert RESET */
70 	gpiod_set_value_cansleep(s6->reset_gpio, 1);
71 	regulator_disable(s6->supply);
72 
73 	return 0;
74 }
75 
s6d16d0_prepare(struct drm_panel * panel)76 static int s6d16d0_prepare(struct drm_panel *panel)
77 {
78 	struct s6d16d0 *s6 = panel_to_s6d16d0(panel);
79 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(s6->dev);
80 	int ret;
81 
82 	ret = regulator_enable(s6->supply);
83 	if (ret) {
84 		DRM_DEV_ERROR(s6->dev, "failed to enable supply (%d)\n", ret);
85 		return ret;
86 	}
87 
88 	/* Assert RESET */
89 	gpiod_set_value_cansleep(s6->reset_gpio, 1);
90 	udelay(10);
91 	/* De-assert RESET */
92 	gpiod_set_value_cansleep(s6->reset_gpio, 0);
93 	msleep(120);
94 
95 	/* Enabe tearing mode: send TE (tearing effect) at VBLANK */
96 	ret = mipi_dsi_dcs_set_tear_on(dsi,
97 				       MIPI_DSI_DCS_TEAR_MODE_VBLANK);
98 	if (ret) {
99 		DRM_DEV_ERROR(s6->dev, "failed to enable vblank TE (%d)\n",
100 			      ret);
101 		return ret;
102 	}
103 	/* Exit sleep mode and power on */
104 	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
105 	if (ret) {
106 		DRM_DEV_ERROR(s6->dev, "failed to exit sleep mode (%d)\n",
107 			      ret);
108 		return ret;
109 	}
110 
111 	return 0;
112 }
113 
s6d16d0_enable(struct drm_panel * panel)114 static int s6d16d0_enable(struct drm_panel *panel)
115 {
116 	struct s6d16d0 *s6 = panel_to_s6d16d0(panel);
117 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(s6->dev);
118 	int ret;
119 
120 	ret = mipi_dsi_dcs_set_display_on(dsi);
121 	if (ret) {
122 		DRM_DEV_ERROR(s6->dev, "failed to turn display on (%d)\n",
123 			      ret);
124 		return ret;
125 	}
126 
127 	return 0;
128 }
129 
s6d16d0_disable(struct drm_panel * panel)130 static int s6d16d0_disable(struct drm_panel *panel)
131 {
132 	struct s6d16d0 *s6 = panel_to_s6d16d0(panel);
133 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(s6->dev);
134 	int ret;
135 
136 	ret = mipi_dsi_dcs_set_display_off(dsi);
137 	if (ret) {
138 		DRM_DEV_ERROR(s6->dev, "failed to turn display off (%d)\n",
139 			      ret);
140 		return ret;
141 	}
142 
143 	return 0;
144 }
145 
s6d16d0_get_modes(struct drm_panel * panel)146 static int s6d16d0_get_modes(struct drm_panel *panel)
147 {
148 	struct drm_connector *connector = panel->connector;
149 	struct drm_display_mode *mode;
150 
151 	mode = drm_mode_duplicate(panel->drm, &samsung_s6d16d0_mode);
152 	if (!mode) {
153 		DRM_ERROR("bad mode or failed to add mode\n");
154 		return -EINVAL;
155 	}
156 	drm_mode_set_name(mode);
157 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
158 
159 	connector->display_info.width_mm = mode->width_mm;
160 	connector->display_info.height_mm = mode->height_mm;
161 
162 	drm_mode_probed_add(connector, mode);
163 
164 	return 1; /* Number of modes */
165 }
166 
167 static const struct drm_panel_funcs s6d16d0_drm_funcs = {
168 	.disable = s6d16d0_disable,
169 	.unprepare = s6d16d0_unprepare,
170 	.prepare = s6d16d0_prepare,
171 	.enable = s6d16d0_enable,
172 	.get_modes = s6d16d0_get_modes,
173 };
174 
s6d16d0_probe(struct mipi_dsi_device * dsi)175 static int s6d16d0_probe(struct mipi_dsi_device *dsi)
176 {
177 	struct device *dev = &dsi->dev;
178 	struct s6d16d0 *s6;
179 	int ret;
180 
181 	s6 = devm_kzalloc(dev, sizeof(struct s6d16d0), GFP_KERNEL);
182 	if (!s6)
183 		return -ENOMEM;
184 
185 	mipi_dsi_set_drvdata(dsi, s6);
186 	s6->dev = dev;
187 
188 	dsi->lanes = 2;
189 	dsi->format = MIPI_DSI_FMT_RGB888;
190 	dsi->hs_rate = 420160000;
191 	dsi->lp_rate = 19200000;
192 	/*
193 	 * This display uses command mode so no MIPI_DSI_MODE_VIDEO
194 	 * or MIPI_DSI_MODE_VIDEO_SYNC_PULSE
195 	 *
196 	 * As we only send commands we do not need to be continuously
197 	 * clocked.
198 	 */
199 	dsi->mode_flags =
200 		MIPI_DSI_CLOCK_NON_CONTINUOUS |
201 		MIPI_DSI_MODE_EOT_PACKET;
202 
203 	s6->supply = devm_regulator_get(dev, "vdd1");
204 	if (IS_ERR(s6->supply))
205 		return PTR_ERR(s6->supply);
206 
207 	/* This asserts RESET by default */
208 	s6->reset_gpio = devm_gpiod_get_optional(dev, "reset",
209 						 GPIOD_OUT_HIGH);
210 	if (IS_ERR(s6->reset_gpio)) {
211 		ret = PTR_ERR(s6->reset_gpio);
212 		if (ret != -EPROBE_DEFER)
213 			DRM_DEV_ERROR(dev, "failed to request GPIO (%d)\n",
214 				      ret);
215 		return ret;
216 	}
217 
218 	drm_panel_init(&s6->panel);
219 	s6->panel.dev = dev;
220 	s6->panel.funcs = &s6d16d0_drm_funcs;
221 
222 	ret = drm_panel_add(&s6->panel);
223 	if (ret < 0)
224 		return ret;
225 
226 	ret = mipi_dsi_attach(dsi);
227 	if (ret < 0)
228 		drm_panel_remove(&s6->panel);
229 
230 	return ret;
231 }
232 
s6d16d0_remove(struct mipi_dsi_device * dsi)233 static int s6d16d0_remove(struct mipi_dsi_device *dsi)
234 {
235 	struct s6d16d0 *s6 = mipi_dsi_get_drvdata(dsi);
236 
237 	mipi_dsi_detach(dsi);
238 	drm_panel_remove(&s6->panel);
239 
240 	return 0;
241 }
242 
243 static const struct of_device_id s6d16d0_of_match[] = {
244 	{ .compatible = "samsung,s6d16d0" },
245 	{ }
246 };
247 MODULE_DEVICE_TABLE(of, s6d16d0_of_match);
248 
249 static struct mipi_dsi_driver s6d16d0_driver = {
250 	.probe = s6d16d0_probe,
251 	.remove = s6d16d0_remove,
252 	.driver = {
253 		.name = "panel-samsung-s6d16d0",
254 		.of_match_table = s6d16d0_of_match,
255 	},
256 };
257 module_mipi_dsi_driver(s6d16d0_driver);
258 
259 MODULE_AUTHOR("Linus Wallei <linus.walleij@linaro.org>");
260 MODULE_DESCRIPTION("MIPI-DSI s6d16d0 Panel Driver");
261 MODULE_LICENSE("GPL v2");
262