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