xref: /linux/drivers/gpu/drm/panel/panel-renesas-r61307.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 #define R61307_MACP		0xb0 /* Manufacturer CMD Protect */
19 #define   R61307_MACP_ON	0x03
20 #define   R61307_MACP_OFF	0x04
21 
22 #define R61307_INVERSION	0xc1
23 #define R61307_GAMMA_SET_A	0xc8 /* Gamma Setting A */
24 #define R61307_GAMMA_SET_B	0xc9 /* Gamma Setting B */
25 #define R61307_GAMMA_SET_C	0xca /* Gamma Setting C */
26 #define R61307_CONTRAST_SET	0xcc
27 
28 struct renesas_r61307 {
29 	struct drm_panel panel;
30 	struct mipi_dsi_device *dsi;
31 
32 	struct regulator *vcc_supply;
33 	struct regulator *iovcc_supply;
34 
35 	struct gpio_desc *reset_gpio;
36 
37 	bool dig_cont_adj;
38 	bool inversion;
39 	u32 gamma;
40 };
41 
42 static const u8 gamma_setting[][25] = {
43 	{ /* sentinel */ },
44 	{
45 		R61307_GAMMA_SET_A,
46 		0x00, 0x06, 0x0a, 0x0f,
47 		0x14, 0x1f, 0x1f, 0x17,
48 		0x12, 0x0c, 0x09, 0x06,
49 		0x00, 0x06, 0x0a, 0x0f,
50 		0x14, 0x1f, 0x1f, 0x17,
51 		0x12, 0x0c, 0x09, 0x06
52 	},
53 	{
54 		R61307_GAMMA_SET_A,
55 		0x00, 0x05, 0x0b, 0x0f,
56 		0x11, 0x1d, 0x20, 0x18,
57 		0x18, 0x09, 0x07, 0x06,
58 		0x00, 0x05, 0x0b, 0x0f,
59 		0x11, 0x1d, 0x20, 0x18,
60 		0x18, 0x09, 0x07, 0x06
61 	},
62 	{
63 		R61307_GAMMA_SET_A,
64 		0x0b, 0x0d, 0x10, 0x14,
65 		0x13, 0x1d, 0x20, 0x18,
66 		0x12, 0x09, 0x07, 0x06,
67 		0x0a, 0x0c, 0x10, 0x14,
68 		0x13, 0x1d, 0x20, 0x18,
69 		0x12, 0x09, 0x07, 0x06
70 	},
71 };
72 
to_renesas_r61307(struct drm_panel * panel)73 static inline struct renesas_r61307 *to_renesas_r61307(struct drm_panel *panel)
74 {
75 	return container_of(panel, struct renesas_r61307, panel);
76 }
77 
renesas_r61307_reset(struct renesas_r61307 * priv)78 static void renesas_r61307_reset(struct renesas_r61307 *priv)
79 {
80 	gpiod_set_value_cansleep(priv->reset_gpio, 1);
81 	usleep_range(10000, 11000);
82 	gpiod_set_value_cansleep(priv->reset_gpio, 0);
83 	usleep_range(2000, 3000);
84 }
85 
renesas_r61307_prepare(struct drm_panel * panel)86 static int renesas_r61307_prepare(struct drm_panel *panel)
87 {
88 	struct renesas_r61307 *priv = to_renesas_r61307(panel);
89 	struct device *dev = &priv->dsi->dev;
90 	int ret;
91 
92 	ret = regulator_enable(priv->vcc_supply);
93 	if (ret) {
94 		dev_err(dev, "failed to enable vcc power supply\n");
95 		return ret;
96 	}
97 
98 	usleep_range(2000, 3000);
99 
100 	ret = regulator_enable(priv->iovcc_supply);
101 	if (ret) {
102 		dev_err(dev, "failed to enable iovcc power supply\n");
103 		return ret;
104 	}
105 
106 	usleep_range(2000, 3000);
107 
108 	renesas_r61307_reset(priv);
109 
110 	return 0;
111 }
112 
renesas_r61307_enable(struct drm_panel * panel)113 static int renesas_r61307_enable(struct drm_panel *panel)
114 {
115 	struct renesas_r61307 *priv = to_renesas_r61307(panel);
116 	struct mipi_dsi_multi_context ctx = { .dsi = priv->dsi };
117 
118 	mipi_dsi_dcs_exit_sleep_mode_multi(&ctx);
119 	mipi_dsi_msleep(&ctx, 80);
120 
121 	mipi_dsi_dcs_write_seq_multi(&ctx, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
122 	mipi_dsi_msleep(&ctx, 20);
123 
124 	mipi_dsi_dcs_set_pixel_format_multi(&ctx, MIPI_DCS_PIXEL_FMT_24BIT << 4);
125 
126 	/* MACP Off */
127 	mipi_dsi_generic_write_seq_multi(&ctx, R61307_MACP, R61307_MACP_OFF);
128 
129 	if (priv->dig_cont_adj)
130 		mipi_dsi_generic_write_seq_multi(&ctx, R61307_CONTRAST_SET,
131 						 0xdc, 0xb4, 0xff);
132 
133 	if (priv->gamma)
134 		mipi_dsi_generic_write_multi(&ctx, gamma_setting[priv->gamma],
135 					     sizeof(gamma_setting[priv->gamma]));
136 
137 	if (priv->inversion)
138 		mipi_dsi_generic_write_seq_multi(&ctx, R61307_INVERSION,
139 						 0x00, 0x50, 0x03, 0x22,
140 						 0x16, 0x06, 0x60, 0x11);
141 	else
142 		mipi_dsi_generic_write_seq_multi(&ctx, R61307_INVERSION,
143 						 0x00, 0x10, 0x03, 0x22,
144 						 0x16, 0x06, 0x60, 0x01);
145 
146 	/* MACP On */
147 	mipi_dsi_generic_write_seq_multi(&ctx, R61307_MACP, R61307_MACP_ON);
148 
149 	mipi_dsi_dcs_set_display_on_multi(&ctx);
150 	mipi_dsi_msleep(&ctx, 50);
151 
152 	return ctx.accum_err;
153 }
154 
renesas_r61307_disable(struct drm_panel * panel)155 static int renesas_r61307_disable(struct drm_panel *panel)
156 {
157 	struct renesas_r61307 *priv = to_renesas_r61307(panel);
158 	struct mipi_dsi_multi_context ctx = { .dsi = priv->dsi };
159 
160 	mipi_dsi_dcs_set_display_off_multi(&ctx);
161 	mipi_dsi_msleep(&ctx, 100);
162 	mipi_dsi_dcs_enter_sleep_mode_multi(&ctx);
163 
164 	return ctx.accum_err;
165 }
166 
renesas_r61307_unprepare(struct drm_panel * panel)167 static int renesas_r61307_unprepare(struct drm_panel *panel)
168 {
169 	struct renesas_r61307 *priv = to_renesas_r61307(panel);
170 
171 	usleep_range(10000, 11000);
172 
173 	gpiod_set_value_cansleep(priv->reset_gpio, 1);
174 	usleep_range(5000, 6000);
175 
176 	regulator_disable(priv->iovcc_supply);
177 	usleep_range(2000, 3000);
178 	regulator_disable(priv->vcc_supply);
179 
180 	return 0;
181 }
182 
183 static const struct drm_display_mode renesas_r61307_mode = {
184 	.clock = (768 + 116 + 81 + 5) * (1024 + 24 + 8 + 2) * 60 / 1000,
185 	.hdisplay = 768,
186 	.hsync_start = 768 + 116,
187 	.hsync_end = 768 + 116 + 81,
188 	.htotal = 768 + 116 + 81 + 5,
189 	.vdisplay = 1024,
190 	.vsync_start = 1024 + 24,
191 	.vsync_end = 1024 + 24 + 8,
192 	.vtotal = 1024 + 24 + 8 + 2,
193 	.width_mm = 76,
194 	.height_mm = 101,
195 	.type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
196 };
197 
renesas_r61307_get_modes(struct drm_panel * panel,struct drm_connector * connector)198 static int renesas_r61307_get_modes(struct drm_panel *panel,
199 				    struct drm_connector *connector)
200 {
201 	return drm_connector_helper_get_modes_fixed(connector, &renesas_r61307_mode);
202 }
203 
204 static const struct drm_panel_funcs renesas_r61307_panel_funcs = {
205 	.prepare = renesas_r61307_prepare,
206 	.enable = renesas_r61307_enable,
207 	.disable = renesas_r61307_disable,
208 	.unprepare = renesas_r61307_unprepare,
209 	.get_modes = renesas_r61307_get_modes,
210 };
211 
renesas_r61307_probe(struct mipi_dsi_device * dsi)212 static int renesas_r61307_probe(struct mipi_dsi_device *dsi)
213 {
214 	struct device *dev = &dsi->dev;
215 	struct renesas_r61307 *priv;
216 	int ret;
217 
218 	priv = devm_drm_panel_alloc(dev, struct renesas_r61307, panel,
219 				    &renesas_r61307_panel_funcs,
220 				    DRM_MODE_CONNECTOR_DSI);
221 	if (IS_ERR(priv))
222 		return PTR_ERR(priv);
223 
224 	priv->vcc_supply = devm_regulator_get(dev, "vcc");
225 	if (IS_ERR(priv->vcc_supply))
226 		return dev_err_probe(dev, PTR_ERR(priv->vcc_supply),
227 				     "Failed to get vcc-supply\n");
228 
229 	priv->iovcc_supply = devm_regulator_get(dev, "iovcc");
230 	if (IS_ERR(priv->iovcc_supply))
231 		return dev_err_probe(dev, PTR_ERR(priv->iovcc_supply),
232 				     "Failed to get iovcc-supply\n");
233 
234 	priv->reset_gpio = devm_gpiod_get_optional(dev, "reset",
235 						   GPIOD_OUT_HIGH);
236 	if (IS_ERR(priv->reset_gpio))
237 		return dev_err_probe(dev, PTR_ERR(priv->reset_gpio),
238 				     "Failed to get reset gpios\n");
239 
240 	if (device_property_read_bool(dev, "renesas,column-inversion"))
241 		priv->inversion = true;
242 
243 	if (device_property_read_bool(dev, "renesas,contrast"))
244 		priv->dig_cont_adj = true;
245 
246 	priv->gamma = 0;
247 	device_property_read_u32(dev, "renesas,gamma", &priv->gamma);
248 
249 	priv->dsi = dsi;
250 	mipi_dsi_set_drvdata(dsi, priv);
251 
252 	dsi->lanes = 4;
253 	dsi->format = MIPI_DSI_FMT_RGB888;
254 	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
255 			  MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM;
256 
257 	ret = drm_panel_of_backlight(&priv->panel);
258 	if (ret)
259 		return dev_err_probe(dev, ret, "Failed to get backlight\n");
260 
261 	drm_panel_add(&priv->panel);
262 
263 	ret = devm_mipi_dsi_attach(dev, dsi);
264 	if (ret) {
265 		drm_panel_remove(&priv->panel);
266 		return dev_err_probe(dev, ret, "Failed to attach to DSI host\n");
267 	}
268 
269 	return 0;
270 }
271 
renesas_r61307_remove(struct mipi_dsi_device * dsi)272 static void renesas_r61307_remove(struct mipi_dsi_device *dsi)
273 {
274 	struct renesas_r61307 *priv = mipi_dsi_get_drvdata(dsi);
275 
276 	drm_panel_remove(&priv->panel);
277 }
278 
279 static const struct of_device_id renesas_r61307_of_match[] = {
280 	{ .compatible = "hit,tx13d100vm0eaa" },
281 	{ .compatible = "koe,tx13d100vm0eaa" },
282 	{ /* sentinel */ }
283 };
284 MODULE_DEVICE_TABLE(of, renesas_r61307_of_match);
285 
286 static struct mipi_dsi_driver renesas_r61307_driver = {
287 	.probe = renesas_r61307_probe,
288 	.remove = renesas_r61307_remove,
289 	.driver = {
290 		.name = "panel-renesas-r61307",
291 		.of_match_table = renesas_r61307_of_match,
292 	},
293 };
294 module_mipi_dsi_driver(renesas_r61307_driver);
295 
296 MODULE_AUTHOR("Svyatoslav Ryhel <clamor95@gmail.com>");
297 MODULE_DESCRIPTION("Renesas R61307-based panel driver");
298 MODULE_LICENSE("GPL");
299