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