xref: /linux/drivers/gpu/drm/panel/panel-elida-kd35t133.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Elida kd35t133 3.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 };
47 
48 static inline struct kd35t133 *panel_to_kd35t133(struct drm_panel *panel)
49 {
50 	return container_of(panel, struct kd35t133, panel);
51 }
52 
53 static int kd35t133_init_sequence(struct kd35t133 *ctx)
54 {
55 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
56 	struct device *dev = ctx->dev;
57 
58 	/*
59 	 * Init sequence was supplied by the panel vendor with minimal
60 	 * documentation.
61 	 */
62 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_POSITIVEGAMMA,
63 			       0x00, 0x13, 0x18, 0x04, 0x0f, 0x06, 0x3a, 0x56,
64 			       0x4d, 0x03, 0x0a, 0x06, 0x30, 0x3e, 0x0f);
65 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_NEGATIVEGAMMA,
66 			       0x00, 0x13, 0x18, 0x01, 0x11, 0x06, 0x38, 0x34,
67 			       0x4d, 0x06, 0x0d, 0x0b, 0x31, 0x37, 0x0f);
68 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_POWERCONTROL1, 0x18, 0x17);
69 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_POWERCONTROL2, 0x41);
70 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_VCOMCONTROL, 0x00, 0x1a, 0x80);
71 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x48);
72 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_PIXEL_FORMAT, 0x55);
73 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_INTERFACEMODECTRL, 0x00);
74 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_FRAMERATECTRL, 0xa0);
75 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_DISPLAYINVERSIONCTRL, 0x02);
76 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_DISPLAYFUNCTIONCTRL,
77 			       0x20, 0x02);
78 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_SETIMAGEFUNCTION, 0x00);
79 	mipi_dsi_dcs_write_seq(dsi, KD35T133_CMD_ADJUSTCONTROL3,
80 			       0xa9, 0x51, 0x2c, 0x82);
81 	mipi_dsi_dcs_write(dsi, MIPI_DCS_ENTER_INVERT_MODE, NULL, 0);
82 
83 	dev_dbg(dev, "Panel init sequence done\n");
84 	return 0;
85 }
86 
87 static int kd35t133_unprepare(struct drm_panel *panel)
88 {
89 	struct kd35t133 *ctx = panel_to_kd35t133(panel);
90 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
91 	int ret;
92 
93 	ret = mipi_dsi_dcs_set_display_off(dsi);
94 	if (ret < 0)
95 		dev_err(ctx->dev, "failed to set display off: %d\n", ret);
96 
97 	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
98 	if (ret < 0) {
99 		dev_err(ctx->dev, "failed to enter sleep mode: %d\n", ret);
100 		return ret;
101 	}
102 
103 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
104 
105 	regulator_disable(ctx->iovcc);
106 	regulator_disable(ctx->vdd);
107 
108 	return 0;
109 }
110 
111 static int kd35t133_prepare(struct drm_panel *panel)
112 {
113 	struct kd35t133 *ctx = panel_to_kd35t133(panel);
114 	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
115 	int ret;
116 
117 	dev_dbg(ctx->dev, "Resetting the panel\n");
118 	ret = regulator_enable(ctx->vdd);
119 	if (ret < 0) {
120 		dev_err(ctx->dev, "Failed to enable vdd supply: %d\n", ret);
121 		return ret;
122 	}
123 
124 	ret = regulator_enable(ctx->iovcc);
125 	if (ret < 0) {
126 		dev_err(ctx->dev, "Failed to enable iovcc supply: %d\n", ret);
127 		goto disable_vdd;
128 	}
129 
130 	msleep(20);
131 
132 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
133 	usleep_range(10, 20);
134 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
135 
136 	msleep(20);
137 
138 	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
139 	if (ret < 0) {
140 		dev_err(ctx->dev, "Failed to exit sleep mode: %d\n", ret);
141 		goto disable_iovcc;
142 	}
143 
144 	msleep(250);
145 
146 	ret = kd35t133_init_sequence(ctx);
147 	if (ret < 0) {
148 		dev_err(ctx->dev, "Panel init sequence failed: %d\n", ret);
149 		goto disable_iovcc;
150 	}
151 
152 	ret = mipi_dsi_dcs_set_display_on(dsi);
153 	if (ret < 0) {
154 		dev_err(ctx->dev, "Failed to set display on: %d\n", ret);
155 		goto disable_iovcc;
156 	}
157 
158 	msleep(50);
159 
160 	return 0;
161 
162 disable_iovcc:
163 	regulator_disable(ctx->iovcc);
164 disable_vdd:
165 	regulator_disable(ctx->vdd);
166 	return ret;
167 }
168 
169 static const struct drm_display_mode default_mode = {
170 	.hdisplay	= 320,
171 	.hsync_start	= 320 + 130,
172 	.hsync_end	= 320 + 130 + 4,
173 	.htotal		= 320 + 130 + 4 + 130,
174 	.vdisplay	= 480,
175 	.vsync_start	= 480 + 2,
176 	.vsync_end	= 480 + 2 + 1,
177 	.vtotal		= 480 + 2 + 1 + 2,
178 	.clock		= 17000,
179 	.width_mm	= 42,
180 	.height_mm	= 82,
181 };
182 
183 static int kd35t133_get_modes(struct drm_panel *panel,
184 				struct drm_connector *connector)
185 {
186 	struct kd35t133 *ctx = panel_to_kd35t133(panel);
187 	struct drm_display_mode *mode;
188 
189 	mode = drm_mode_duplicate(connector->dev, &default_mode);
190 	if (!mode) {
191 		dev_err(ctx->dev, "Failed to add mode %ux%u@%u\n",
192 			default_mode.hdisplay, default_mode.vdisplay,
193 			drm_mode_vrefresh(&default_mode));
194 		return -ENOMEM;
195 	}
196 
197 	drm_mode_set_name(mode);
198 
199 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
200 	connector->display_info.width_mm = mode->width_mm;
201 	connector->display_info.height_mm = mode->height_mm;
202 	drm_mode_probed_add(connector, mode);
203 
204 	return 1;
205 }
206 
207 static enum drm_panel_orientation kd35t133_get_orientation(struct drm_panel *panel)
208 {
209 	struct kd35t133 *ctx = panel_to_kd35t133(panel);
210 
211 	return ctx->orientation;
212 }
213 
214 static const struct drm_panel_funcs kd35t133_funcs = {
215 	.unprepare	= kd35t133_unprepare,
216 	.prepare	= kd35t133_prepare,
217 	.get_modes	= kd35t133_get_modes,
218 	.get_orientation = kd35t133_get_orientation,
219 };
220 
221 static int kd35t133_probe(struct mipi_dsi_device *dsi)
222 {
223 	struct device *dev = &dsi->dev;
224 	struct kd35t133 *ctx;
225 	int ret;
226 
227 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
228 	if (!ctx)
229 		return -ENOMEM;
230 
231 	ctx->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
232 	if (IS_ERR(ctx->reset_gpio)) {
233 		dev_err(dev, "cannot get reset gpio\n");
234 		return PTR_ERR(ctx->reset_gpio);
235 	}
236 
237 	ctx->vdd = devm_regulator_get(dev, "vdd");
238 	if (IS_ERR(ctx->vdd)) {
239 		ret = PTR_ERR(ctx->vdd);
240 		if (ret != -EPROBE_DEFER)
241 			dev_err(dev, "Failed to request vdd regulator: %d\n", ret);
242 		return ret;
243 	}
244 
245 	ctx->iovcc = devm_regulator_get(dev, "iovcc");
246 	if (IS_ERR(ctx->iovcc)) {
247 		ret = PTR_ERR(ctx->iovcc);
248 		if (ret != -EPROBE_DEFER)
249 			dev_err(dev, "Failed to request iovcc regulator: %d\n", ret);
250 		return ret;
251 	}
252 
253 	ret = of_drm_get_panel_orientation(dev->of_node, &ctx->orientation);
254 	if (ret < 0) {
255 		dev_err(dev, "%pOF: failed to get orientation %d\n", dev->of_node, ret);
256 		return ret;
257 	}
258 
259 	mipi_dsi_set_drvdata(dsi, ctx);
260 
261 	ctx->dev = dev;
262 
263 	dsi->lanes = 1;
264 	dsi->format = MIPI_DSI_FMT_RGB888;
265 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
266 			  MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_NO_EOT_PACKET |
267 			  MIPI_DSI_CLOCK_NON_CONTINUOUS;
268 
269 	drm_panel_init(&ctx->panel, &dsi->dev, &kd35t133_funcs,
270 		       DRM_MODE_CONNECTOR_DSI);
271 
272 	ret = drm_panel_of_backlight(&ctx->panel);
273 	if (ret)
274 		return ret;
275 
276 	drm_panel_add(&ctx->panel);
277 
278 	ret = mipi_dsi_attach(dsi);
279 	if (ret < 0) {
280 		dev_err(dev, "mipi_dsi_attach failed: %d\n", ret);
281 		drm_panel_remove(&ctx->panel);
282 		return ret;
283 	}
284 
285 	return 0;
286 }
287 
288 static void kd35t133_remove(struct mipi_dsi_device *dsi)
289 {
290 	struct kd35t133 *ctx = mipi_dsi_get_drvdata(dsi);
291 	int ret;
292 
293 	ret = mipi_dsi_detach(dsi);
294 	if (ret < 0)
295 		dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
296 
297 	drm_panel_remove(&ctx->panel);
298 }
299 
300 static const struct of_device_id kd35t133_of_match[] = {
301 	{ .compatible = "elida,kd35t133" },
302 	{ /* sentinel */ }
303 };
304 MODULE_DEVICE_TABLE(of, kd35t133_of_match);
305 
306 static struct mipi_dsi_driver kd35t133_driver = {
307 	.driver = {
308 		.name = "panel-elida-kd35t133",
309 		.of_match_table = kd35t133_of_match,
310 	},
311 	.probe	= kd35t133_probe,
312 	.remove = kd35t133_remove,
313 };
314 module_mipi_dsi_driver(kd35t133_driver);
315 
316 MODULE_AUTHOR("Heiko Stuebner <heiko.stuebner@theobroma-systems.com>");
317 MODULE_DESCRIPTION("DRM driver for Elida kd35t133 MIPI DSI panel");
318 MODULE_LICENSE("GPL v2");
319