1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Elida kd35t133 5.5" MIPI-DSI panel driver
4  * Copyright (C) 2020 Theobroma Systems Design und Consulting GmbH
5  *
6  * based on
7  *
8  * Rockteck jh057n00900 5.5" MIPI-DSI panel driver
9  * Copyright (C) Purism SPC 2019
10  */
11 
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/media-bus-format.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/regulator/consumer.h>
18 
19 #include <video/display_timing.h>
20 #include <video/mipi_display.h>
21 
22 #include <drm/drm_mipi_dsi.h>
23 #include <drm/drm_modes.h>
24 #include <drm/drm_panel.h>
25 
26 /* Manufacturer specific Commands send via DSI */
27 #define KD35T133_CMD_INTERFACEMODECTRL		0xb0
28 #define KD35T133_CMD_FRAMERATECTRL		0xb1
29 #define KD35T133_CMD_DISPLAYINVERSIONCTRL	0xb4
30 #define KD35T133_CMD_DISPLAYFUNCTIONCTRL	0xb6
31 #define KD35T133_CMD_POWERCONTROL1		0xc0
32 #define KD35T133_CMD_POWERCONTROL2		0xc1
33 #define KD35T133_CMD_VCOMCONTROL		0xc5
34 #define KD35T133_CMD_POSITIVEGAMMA		0xe0
35 #define KD35T133_CMD_NEGATIVEGAMMA		0xe1
36 #define KD35T133_CMD_SETIMAGEFUNCTION		0xe9
37 #define KD35T133_CMD_ADJUSTCONTROL3		0xf7
38 
39 struct kd35t133 {
40 	struct device *dev;
41 	struct drm_panel panel;
42 	struct gpio_desc *reset_gpio;
43 	struct regulator *vdd;
44 	struct regulator *iovcc;
45 	enum drm_panel_orientation orientation;
46 	bool prepared;
47 };
48 
49 static inline struct kd35t133 *panel_to_kd35t133(struct drm_panel *panel)
50 {
51 	return container_of(panel, struct kd35t133, panel);
52 }
53 
54 static int kd35t133_init_sequence(struct kd35t133 *ctx)
55 {
56 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
57 	struct device *dev = ctx->dev;
58 
59 	/*
60 	 * Init sequence was supplied by the panel vendor with minimal
61 	 * documentation.
62 	 */
63 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_POSITIVEGAMMA,
64 			       0x00, 0x13, 0x18, 0x04, 0x0f, 0x06, 0x3a, 0x56,
65 			       0x4d, 0x03, 0x0a, 0x06, 0x30, 0x3e, 0x0f);
66 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_NEGATIVEGAMMA,
67 			       0x00, 0x13, 0x18, 0x01, 0x11, 0x06, 0x38, 0x34,
68 			       0x4d, 0x06, 0x0d, 0x0b, 0x31, 0x37, 0x0f);
69 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_POWERCONTROL1, 0x18, 0x17);
70 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_POWERCONTROL2, 0x41);
71 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_VCOMCONTROL, 0x00, 0x1a, 0x80);
72 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x48);
73 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_PIXEL_FORMAT, 0x55);
74 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_INTERFACEMODECTRL, 0x00);
75 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_FRAMERATECTRL, 0xa0);
76 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_DISPLAYINVERSIONCTRL, 0x02);
77 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_DISPLAYFUNCTIONCTRL,
78 			       0x20, 0x02);
79 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_SETIMAGEFUNCTION, 0x00);
80 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_ADJUSTCONTROL3,
81 			       0xa9, 0x51, 0x2c, 0x82);
82 	mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_INVERT_MODE, NULL, 0);
83 
84 	dev_dbg(dev, "Panel init sequence done\n");
85 	return 0;
86 }
87 
88 static int kd35t133_unprepare(struct drm_panel *panel)
89 {
90 	struct kd35t133 *ctx = panel_to_kd35t133(panel);
91 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
92 	int ret;
93 
94 	if (!ctx->prepared)
95 		return 0;
96 
97 	ret = mipi_dsi_dcs_set_display_off(dsi);
98 	if (ret < 0)
99 		dev_err(ctx->dev, "failed to set display off: %d\n", ret);
100 
101 	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
102 	if (ret < 0) {
103 		dev_err(ctx->dev, "failed to enter sleep mode: %d\n", ret);
104 		return ret;
105 	}
106 
107 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
108 
109 	regulator_disable(ctx->iovcc);
110 	regulator_disable(ctx->vdd);
111 
112 	ctx->prepared = false;
113 
114 	return 0;
115 }
116 
117 static int kd35t133_prepare(struct drm_panel *panel)
118 {
119 	struct kd35t133 *ctx = panel_to_kd35t133(panel);
120 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
121 	int ret;
122 
123 	if (ctx->prepared)
124 		return 0;
125 
126 	dev_dbg(ctx->dev, "Resetting the panel\n");
127 	ret = regulator_enable(ctx->vdd);
128 	if (ret < 0) {
129 		dev_err(ctx->dev, "Failed to enable vdd supply: %d\n", ret);
130 		return ret;
131 	}
132 
133 	ret = regulator_enable(ctx->iovcc);
134 	if (ret < 0) {
135 		dev_err(ctx->dev, "Failed to enable iovcc supply: %d\n", ret);
136 		goto disable_vdd;
137 	}
138 
139 	msleep(20);
140 
141 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
142 	usleep_range(10, 20);
143 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
144 
145 	msleep(20);
146 
147 	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
148 	if (ret < 0) {
149 		dev_err(ctx->dev, "Failed to exit sleep mode: %d\n", ret);
150 		goto disable_iovcc;
151 	}
152 
153 	msleep(250);
154 
155 	ret = kd35t133_init_sequence(ctx);
156 	if (ret < 0) {
157 		dev_err(ctx->dev, "Panel init sequence failed: %d\n", ret);
158 		goto disable_iovcc;
159 	}
160 
161 	ret = mipi_dsi_dcs_set_display_on(dsi);
162 	if (ret < 0) {
163 		dev_err(ctx->dev, "Failed to set display on: %d\n", ret);
164 		goto disable_iovcc;
165 	}
166 
167 	msleep(50);
168 
169 	ctx->prepared = true;
170 
171 	return 0;
172 
173 disable_iovcc:
174 	regulator_disable(ctx->iovcc);
175 disable_vdd:
176 	regulator_disable(ctx->vdd);
177 	return ret;
178 }
179 
180 static const struct drm_display_mode default_mode = {
181 	.hdisplay	= 320,
182 	.hsync_start	= 320 + 130,
183 	.hsync_end	= 320 + 130 + 4,
184 	.htotal		= 320 + 130 + 4 + 130,
185 	.vdisplay	= 480,
186 	.vsync_start	= 480 + 2,
187 	.vsync_end	= 480 + 2 + 1,
188 	.vtotal		= 480 + 2 + 1 + 2,
189 	.clock		= 17000,
190 	.width_mm	= 42,
191 	.height_mm	= 82,
192 };
193 
194 static int kd35t133_get_modes(struct drm_panel *panel,
195 				struct drm_connector *connector)
196 {
197 	struct kd35t133 *ctx = panel_to_kd35t133(panel);
198 	struct drm_display_mode *mode;
199 
200 	mode = drm_mode_duplicate(connector->dev, &default_mode);
201 	if (!mode) {
202 		dev_err(ctx->dev, "Failed to add mode %ux%u@%u\n",
203 			default_mode.hdisplay, default_mode.vdisplay,
204 			drm_mode_vrefresh(&default_mode));
205 		return -ENOMEM;
206 	}
207 
208 	drm_mode_set_name(mode);
209 
210 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
211 	connector->display_info.width_mm = mode->width_mm;
212 	connector->display_info.height_mm = mode->height_mm;
213 	drm_mode_probed_add(connector, mode);
214 	/*
215 	 * TODO: Remove once all drm drivers call
216 	 * drm_connector_set_orientation_from_panel()
217 	 */
218 	drm_connector_set_panel_orientation(connector, ctx->orientation);
219 
220 	return 1;
221 }
222 
223 static enum drm_panel_orientation kd35t133_get_orientation(struct drm_panel *panel)
224 {
225 	struct kd35t133 *ctx = panel_to_kd35t133(panel);
226 
227 	return ctx->orientation;
228 }
229 
230 static const struct drm_panel_funcs kd35t133_funcs = {
231 	.unprepare	= kd35t133_unprepare,
232 	.prepare	= kd35t133_prepare,
233 	.get_modes	= kd35t133_get_modes,
234 	.get_orientation = kd35t133_get_orientation,
235 };
236 
237 static int kd35t133_probe(struct mipi_dsi_device *dsi)
238 {
239 	struct device *dev = &dsi->dev;
240 	struct kd35t133 *ctx;
241 	int ret;
242 
243 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
244 	if (!ctx)
245 		return -ENOMEM;
246 
247 	ctx->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
248 	if (IS_ERR(ctx->reset_gpio)) {
249 		dev_err(dev, "cannot get reset gpio\n");
250 		return PTR_ERR(ctx->reset_gpio);
251 	}
252 
253 	ctx->vdd = devm_regulator_get(dev, "vdd");
254 	if (IS_ERR(ctx->vdd)) {
255 		ret = PTR_ERR(ctx->vdd);
256 		if (ret != -EPROBE_DEFER)
257 			dev_err(dev, "Failed to request vdd regulator: %d\n", ret);
258 		return ret;
259 	}
260 
261 	ctx->iovcc = devm_regulator_get(dev, "iovcc");
262 	if (IS_ERR(ctx->iovcc)) {
263 		ret = PTR_ERR(ctx->iovcc);
264 		if (ret != -EPROBE_DEFER)
265 			dev_err(dev, "Failed to request iovcc regulator: %d\n", ret);
266 		return ret;
267 	}
268 
269 	ret = of_drm_get_panel_orientation(dev->of_node, &ctx->orientation);
270 	if (ret < 0) {
271 		dev_err(dev, "%pOF: failed to get orientation %d\n", dev->of_node, ret);
272 		return ret;
273 	}
274 
275 	mipi_dsi_set_drvdata(dsi, ctx);
276 
277 	ctx->dev = dev;
278 
279 	dsi->lanes = 1;
280 	dsi->format = MIPI_DSI_FMT_RGB888;
281 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
282 			  MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_NO_EOT_PACKET |
283 			  MIPI_DSI_CLOCK_NON_CONTINUOUS;
284 
285 	drm_panel_init(&ctx->panel, &dsi->dev, &kd35t133_funcs,
286 		       DRM_MODE_CONNECTOR_DSI);
287 
288 	ret = drm_panel_of_backlight(&ctx->panel);
289 	if (ret)
290 		return ret;
291 
292 	drm_panel_add(&ctx->panel);
293 
294 	ret = mipi_dsi_attach(dsi);
295 	if (ret < 0) {
296 		dev_err(dev, "mipi_dsi_attach failed: %d\n", ret);
297 		drm_panel_remove(&ctx->panel);
298 		return ret;
299 	}
300 
301 	return 0;
302 }
303 
304 static void kd35t133_shutdown(struct mipi_dsi_device *dsi)
305 {
306 	struct kd35t133 *ctx = mipi_dsi_get_drvdata(dsi);
307 	int ret;
308 
309 	ret = drm_panel_unprepare(&ctx->panel);
310 	if (ret < 0)
311 		dev_err(&dsi->dev, "Failed to unprepare panel: %d\n", ret);
312 
313 	ret = drm_panel_disable(&ctx->panel);
314 	if (ret < 0)
315 		dev_err(&dsi->dev, "Failed to disable panel: %d\n", ret);
316 }
317 
318 static void kd35t133_remove(struct mipi_dsi_device *dsi)
319 {
320 	struct kd35t133 *ctx = mipi_dsi_get_drvdata(dsi);
321 	int ret;
322 
323 	kd35t133_shutdown(dsi);
324 
325 	ret = mipi_dsi_detach(dsi);
326 	if (ret < 0)
327 		dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
328 
329 	drm_panel_remove(&ctx->panel);
330 }
331 
332 static const struct of_device_id kd35t133_of_match[] = {
333 	{ .compatible = "elida,kd35t133" },
334 	{ /* sentinel */ }
335 };
336 MODULE_DEVICE_TABLE(of, kd35t133_of_match);
337 
338 static struct mipi_dsi_driver kd35t133_driver = {
339 	.driver = {
340 		.name = "panel-elida-kd35t133",
341 		.of_match_table = kd35t133_of_match,
342 	},
343 	.probe	= kd35t133_probe,
344 	.remove = kd35t133_remove,
345 	.shutdown = kd35t133_shutdown,
346 };
347 module_mipi_dsi_driver(kd35t133_driver);
348 
349 MODULE_AUTHOR("Heiko Stuebner <heiko.stuebner@theobroma-systems.com>");
350 MODULE_DESCRIPTION("DRM driver for Elida kd35t133 MIPI DSI panel");
351 MODULE_LICENSE("GPL v2");
352