xref: /linux/drivers/gpu/drm/panel/panel-motorola-mot.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/array_size.h>
4 #include <linux/delay.h>
5 #include <linux/err.h>
6 #include <linux/gpio/consumer.h>
7 #include <linux/module.h>
8 #include <linux/property.h>
9 #include <linux/regulator/consumer.h>
10 
11 #include <video/mipi_display.h>
12 
13 #include <drm/drm_mipi_dsi.h>
14 #include <drm/drm_modes.h>
15 #include <drm/drm_panel.h>
16 #include <drm/drm_probe_helper.h>
17 
18 static const struct regulator_bulk_data mot_panel_supplies[] = {
19 	{ .supply = "vddio" }, { .supply = "vdd" },
20 };
21 
22 struct mot_panel {
23 	struct drm_panel panel;
24 	struct mipi_dsi_device *dsi;
25 
26 	struct gpio_desc *reset_gpio;
27 
28 	struct regulator_bulk_data *supplies;
29 };
30 
to_mot_panel(struct drm_panel * panel)31 static inline struct mot_panel *to_mot_panel(struct drm_panel *panel)
32 {
33 	return container_of(panel, struct mot_panel, panel);
34 }
35 
mot_panel_reset(struct mot_panel * priv)36 static void mot_panel_reset(struct mot_panel *priv)
37 {
38 	gpiod_set_value_cansleep(priv->reset_gpio, 1);
39 	usleep_range(50000, 51000);
40 	gpiod_set_value_cansleep(priv->reset_gpio, 0);
41 	usleep_range(10000, 11000);
42 }
43 
mot_es2(struct mipi_dsi_multi_context * ctx)44 static void mot_es2(struct mipi_dsi_multi_context *ctx)
45 {
46 	mipi_dsi_generic_write_seq_multi(ctx, 0x55, 0x01);
47 
48 	mipi_dsi_dcs_exit_sleep_mode_multi(ctx);
49 	mipi_dsi_msleep(ctx, 120);
50 
51 	mipi_dsi_generic_write_seq_multi(ctx, 0xf4, 0x00, 0xbb, 0x46, 0x53, 0x0c, 0x49,
52 					 0x74, 0x29, 0x12, 0x15, 0x2f, 0x2f, 0x04);
53 	mipi_dsi_generic_write_seq_multi(ctx, 0xf8, 0x4b, 0x04, 0x10, 0x1a, 0x2c, 0x2c,
54 					 0x2c, 0x2c, 0x14, 0x12);
55 
56 	mipi_dsi_generic_write_seq_multi(ctx, 0xb5, 0x03, 0x7f, 0x00, 0x80, 0xc7, 0x00);
57 	mipi_dsi_generic_write_seq_multi(ctx, 0xb7, 0x66, 0xf6, 0x46, 0x9f, 0x90, 0x99,
58 					 0xff, 0x80, 0x6d, 0x01);
59 
60 	/* Gamma R */
61 	mipi_dsi_generic_write_seq_multi(ctx, 0xf9, 0x04);
62 	mipi_dsi_generic_write_seq_multi(ctx, 0xfa, 0x00, 0x2f, 0x30, 0x12, 0x0e, 0x0c,
63 					 0x22, 0x27, 0x31, 0x2e, 0x07, 0x0f);
64 	mipi_dsi_generic_write_seq_multi(ctx, 0xfb, 0x00, 0x2f, 0x30, 0x12, 0x0e, 0x0c,
65 					 0x22, 0x27, 0x31, 0x2e, 0x07, 0x0f);
66 
67 	/* Gamma G */
68 	mipi_dsi_generic_write_seq_multi(ctx, 0xf9, 0x02);
69 	mipi_dsi_generic_write_seq_multi(ctx, 0xfa, 0x00, 0x2f, 0x37, 0x15, 0x15, 0x11,
70 					 0x1f, 0x25, 0x2d, 0x2a, 0x05, 0x0f);
71 	mipi_dsi_generic_write_seq_multi(ctx, 0xfb, 0x00, 0x2f, 0x37, 0x15, 0x15, 0x11,
72 					 0x1f, 0x25, 0x2d, 0x2a, 0x05, 0x0f);
73 
74 	/* Gamma B */
75 	mipi_dsi_generic_write_seq_multi(ctx, 0xf9, 0x01);
76 	mipi_dsi_generic_write_seq_multi(ctx, 0xfa, 0x00, 0x2f, 0x3f, 0x16, 0x1f, 0x15,
77 					 0x1f, 0x25, 0x2d, 0x2b, 0x06, 0x0b);
78 	mipi_dsi_generic_write_seq_multi(ctx, 0xfb, 0x00, 0x2f, 0x3f, 0x16, 0x1f, 0x15,
79 					 0x1f, 0x25, 0x2d, 0x2b, 0x06, 0x0b);
80 
81 	/* Gamma W */
82 	mipi_dsi_generic_write_seq_multi(ctx, 0xf9, 0x20);
83 	mipi_dsi_generic_write_seq_multi(ctx, 0xfa, 0x00, 0x2f, 0x34, 0x15, 0x1a, 0x11,
84 					 0x1f, 0x23, 0x2d, 0x29, 0x02, 0x08);
85 	mipi_dsi_generic_write_seq_multi(ctx, 0xfb, 0x00, 0x2f, 0x34, 0x15, 0x1a, 0x11,
86 					 0x1f, 0x23, 0x2d, 0x29, 0x02, 0x08);
87 
88 	mipi_dsi_generic_write_seq_multi(ctx, 0x53, 0x2c);
89 	mipi_dsi_generic_write_seq_multi(ctx, 0x35, 0x00);
90 }
91 
mot_panel_prepare(struct drm_panel * panel)92 static int mot_panel_prepare(struct drm_panel *panel)
93 {
94 	struct mot_panel *priv = to_mot_panel(panel);
95 	struct mipi_dsi_multi_context ctx = { .dsi = priv->dsi };
96 	struct device *dev = panel->dev;
97 	int ret;
98 
99 	ret = regulator_bulk_enable(ARRAY_SIZE(mot_panel_supplies), priv->supplies);
100 	if (ret < 0) {
101 		dev_err(dev, "failed to enable power supplies: %d\n", ret);
102 		return ret;
103 	}
104 
105 	mot_panel_reset(priv);
106 
107 	mipi_dsi_generic_write_seq_multi(&ctx, 0xf0, 0x5a, 0x5a);
108 	mipi_dsi_generic_write_seq_multi(&ctx, 0xf1, 0x5a, 0x5a);
109 	mipi_dsi_generic_write_seq_multi(&ctx, 0xd0, 0x8e);
110 
111 	mot_es2(&ctx);
112 
113 	mipi_dsi_dcs_set_display_on_multi(&ctx);
114 	mipi_dsi_msleep(&ctx, 20);
115 
116 	return ctx.accum_err;
117 }
118 
mot_panel_disable(struct drm_panel * panel)119 static int mot_panel_disable(struct drm_panel *panel)
120 {
121 	struct mot_panel *priv = to_mot_panel(panel);
122 	struct mipi_dsi_multi_context ctx = { .dsi = priv->dsi };
123 
124 	mipi_dsi_dcs_set_display_off_multi(&ctx);
125 	mipi_dsi_dcs_enter_sleep_mode_multi(&ctx);
126 	mipi_dsi_msleep(&ctx, 70);
127 
128 	return ctx.accum_err;
129 }
130 
mot_panel_unprepare(struct drm_panel * panel)131 static int mot_panel_unprepare(struct drm_panel *panel)
132 {
133 	struct mot_panel *priv = to_mot_panel(panel);
134 
135 	usleep_range(10000, 11000);
136 
137 	gpiod_set_value_cansleep(priv->reset_gpio, 1);
138 	usleep_range(5000, 6000);
139 
140 	regulator_bulk_disable(ARRAY_SIZE(mot_panel_supplies), priv->supplies);
141 
142 	return 0;
143 }
144 
145 static const struct drm_display_mode mot_panel_mode = {
146 	.clock = (540 + 32 + 32 + 16) * (960 + 12 + 12 + 8) * 60 / 1000,
147 	.hdisplay = 540,
148 	.hsync_start = 540 + 32,
149 	.hsync_end = 540 + 32 + 32,
150 	.htotal = 540 + 32 + 32 + 16,
151 	.vdisplay = 960,
152 	.vsync_start = 960 + 12,
153 	.vsync_end = 960 + 12 + 12,
154 	.vtotal = 960 + 12 + 12 + 8,
155 	.width_mm = 51,
156 	.height_mm = 91,
157 	.type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
158 };
159 
mot_panel_get_modes(struct drm_panel * panel,struct drm_connector * connector)160 static int mot_panel_get_modes(struct drm_panel *panel,
161 			       struct drm_connector *connector)
162 {
163 	return drm_connector_helper_get_modes_fixed(connector, &mot_panel_mode);
164 }
165 
166 static const struct drm_panel_funcs mot_panel_panel_funcs = {
167 	.prepare = mot_panel_prepare,
168 	.disable = mot_panel_disable,
169 	.unprepare = mot_panel_unprepare,
170 	.get_modes = mot_panel_get_modes,
171 };
172 
mot_panel_probe(struct mipi_dsi_device * dsi)173 static int mot_panel_probe(struct mipi_dsi_device *dsi)
174 {
175 	struct device *dev = &dsi->dev;
176 	struct mot_panel *priv;
177 	int ret;
178 
179 	priv = devm_drm_panel_alloc(dev, struct mot_panel, panel,
180 				    &mot_panel_panel_funcs,
181 				    DRM_MODE_CONNECTOR_DSI);
182 	if (IS_ERR(priv))
183 		return PTR_ERR(priv);
184 
185 	ret = devm_regulator_bulk_get_const(dev, ARRAY_SIZE(mot_panel_supplies),
186 					    mot_panel_supplies, &priv->supplies);
187 	if (ret < 0)
188 		return dev_err_probe(dev, ret, "failed to get supplies\n");
189 
190 	priv->reset_gpio = devm_gpiod_get_optional(dev, "reset",
191 						   GPIOD_OUT_HIGH);
192 	if (IS_ERR(priv->reset_gpio))
193 		return dev_err_probe(dev, PTR_ERR(priv->reset_gpio),
194 				     "failed to get reset gpios\n");
195 
196 	priv->dsi = dsi;
197 	mipi_dsi_set_drvdata(dsi, priv);
198 
199 	dsi->lanes = 2;
200 	dsi->format = MIPI_DSI_FMT_RGB888;
201 	dsi->mode_flags = MIPI_DSI_MODE_LPM;
202 
203 	ret = drm_panel_of_backlight(&priv->panel);
204 	if (ret < 0)
205 		return dev_err_probe(dev, ret, "failed to get backlight\n");
206 
207 	drm_panel_add(&priv->panel);
208 
209 	ret = devm_mipi_dsi_attach(dev, dsi);
210 	if (ret < 0) {
211 		drm_panel_remove(&priv->panel);
212 		return dev_err_probe(dev, ret, "failed to attach to DSI host\n");
213 	}
214 
215 	return 0;
216 }
217 
mot_panel_remove(struct mipi_dsi_device * dsi)218 static void mot_panel_remove(struct mipi_dsi_device *dsi)
219 {
220 	struct mot_panel *priv = mipi_dsi_get_drvdata(dsi);
221 
222 	drm_panel_remove(&priv->panel);
223 }
224 
225 static const struct of_device_id mot_panel_of_match[] = {
226 	{ .compatible = "motorola,mot-panel" },
227 	{ /* sentinel */ }
228 };
229 MODULE_DEVICE_TABLE(of, mot_panel_of_match);
230 
231 static struct mipi_dsi_driver mot_panel_driver = {
232 	.driver = {
233 		.name = "panel-motorola-mot",
234 		.of_match_table = mot_panel_of_match,
235 	},
236 	.probe = mot_panel_probe,
237 	.remove = mot_panel_remove,
238 };
239 module_mipi_dsi_driver(mot_panel_driver);
240 
241 MODULE_AUTHOR("Svyatoslav Ryhel <clamor95@gmail.com>");
242 MODULE_DESCRIPTION("Motorola MOT panel driver");
243 MODULE_LICENSE("GPL");
244