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 
50 static inline struct s6d16d0 *panel_to_s6d16d0(struct drm_panel *panel)
51 {
52 	return container_of(panel, struct s6d16d0, panel);
53 }
54 
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 
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 
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 
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 
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 	strncpy(connector->display_info.name, "Samsung S6D16D0\0",
152 		DRM_DISPLAY_INFO_LEN);
153 
154 	mode = drm_mode_duplicate(panel->drm, &samsung_s6d16d0_mode);
155 	if (!mode) {
156 		DRM_ERROR("bad mode or failed to add mode\n");
157 		return -EINVAL;
158 	}
159 	drm_mode_set_name(mode);
160 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
161 
162 	connector->display_info.width_mm = mode->width_mm;
163 	connector->display_info.height_mm = mode->height_mm;
164 
165 	drm_mode_probed_add(connector, mode);
166 
167 	return 1; /* Number of modes */
168 }
169 
170 static const struct drm_panel_funcs s6d16d0_drm_funcs = {
171 	.disable = s6d16d0_disable,
172 	.unprepare = s6d16d0_unprepare,
173 	.prepare = s6d16d0_prepare,
174 	.enable = s6d16d0_enable,
175 	.get_modes = s6d16d0_get_modes,
176 };
177 
178 static int s6d16d0_probe(struct mipi_dsi_device *dsi)
179 {
180 	struct device *dev = &dsi->dev;
181 	struct s6d16d0 *s6;
182 	int ret;
183 
184 	s6 = devm_kzalloc(dev, sizeof(struct s6d16d0), GFP_KERNEL);
185 	if (!s6)
186 		return -ENOMEM;
187 
188 	mipi_dsi_set_drvdata(dsi, s6);
189 	s6->dev = dev;
190 
191 	dsi->lanes = 2;
192 	dsi->format = MIPI_DSI_FMT_RGB888;
193 	dsi->hs_rate = 420160000;
194 	dsi->lp_rate = 19200000;
195 	/*
196 	 * This display uses command mode so no MIPI_DSI_MODE_VIDEO
197 	 * or MIPI_DSI_MODE_VIDEO_SYNC_PULSE
198 	 *
199 	 * As we only send commands we do not need to be continuously
200 	 * clocked.
201 	 */
202 	dsi->mode_flags =
203 		MIPI_DSI_CLOCK_NON_CONTINUOUS |
204 		MIPI_DSI_MODE_EOT_PACKET;
205 
206 	s6->supply = devm_regulator_get(dev, "vdd1");
207 	if (IS_ERR(s6->supply))
208 		return PTR_ERR(s6->supply);
209 
210 	/* This asserts RESET by default */
211 	s6->reset_gpio = devm_gpiod_get_optional(dev, "reset",
212 						 GPIOD_OUT_HIGH);
213 	if (IS_ERR(s6->reset_gpio)) {
214 		ret = PTR_ERR(s6->reset_gpio);
215 		if (ret != -EPROBE_DEFER)
216 			DRM_DEV_ERROR(dev, "failed to request GPIO (%d)\n",
217 				      ret);
218 		return ret;
219 	}
220 
221 	drm_panel_init(&s6->panel);
222 	s6->panel.dev = dev;
223 	s6->panel.funcs = &s6d16d0_drm_funcs;
224 
225 	ret = drm_panel_add(&s6->panel);
226 	if (ret < 0)
227 		return ret;
228 
229 	ret = mipi_dsi_attach(dsi);
230 	if (ret < 0)
231 		drm_panel_remove(&s6->panel);
232 
233 	return ret;
234 }
235 
236 static int s6d16d0_remove(struct mipi_dsi_device *dsi)
237 {
238 	struct s6d16d0 *s6 = mipi_dsi_get_drvdata(dsi);
239 
240 	mipi_dsi_detach(dsi);
241 	drm_panel_remove(&s6->panel);
242 
243 	return 0;
244 }
245 
246 static const struct of_device_id s6d16d0_of_match[] = {
247 	{ .compatible = "samsung,s6d16d0" },
248 	{ }
249 };
250 MODULE_DEVICE_TABLE(of, s6d16d0_of_match);
251 
252 static struct mipi_dsi_driver s6d16d0_driver = {
253 	.probe = s6d16d0_probe,
254 	.remove = s6d16d0_remove,
255 	.driver = {
256 		.name = "panel-samsung-s6d16d0",
257 		.of_match_table = s6d16d0_of_match,
258 	},
259 };
260 module_mipi_dsi_driver(s6d16d0_driver);
261 
262 MODULE_AUTHOR("Linus Wallei <linus.walleij@linaro.org>");
263 MODULE_DESCRIPTION("MIPI-DSI s6d16d0 Panel Driver");
264 MODULE_LICENSE("GPL v2");
265