1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2018-2019, Bridge Systems BV
4 * Copyright (C) 2018-2019, Bootlin
5 * Copyright (C) 2017, Free Electrons
6 *
7 * This file based on panel-ilitek-ili9881c.c
8 */
9
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/media-bus-format.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18
19 #include <linux/gpio/consumer.h>
20 #include <linux/regulator/consumer.h>
21
22 #include <drm/drm_connector.h>
23 #include <drm/drm_mipi_dsi.h>
24 #include <drm/drm_modes.h>
25 #include <drm/drm_panel.h>
26
27 struct rb070d30_panel {
28 struct drm_panel panel;
29 struct mipi_dsi_device *dsi;
30 struct regulator *supply;
31
32 struct {
33 struct gpio_desc *power;
34 struct gpio_desc *reset;
35 struct gpio_desc *updn;
36 struct gpio_desc *shlr;
37 } gpios;
38 };
39
panel_to_rb070d30_panel(struct drm_panel * panel)40 static inline struct rb070d30_panel *panel_to_rb070d30_panel(struct drm_panel *panel)
41 {
42 return container_of(panel, struct rb070d30_panel, panel);
43 }
44
rb070d30_panel_prepare(struct drm_panel * panel)45 static int rb070d30_panel_prepare(struct drm_panel *panel)
46 {
47 struct rb070d30_panel *ctx = panel_to_rb070d30_panel(panel);
48 int ret;
49
50 ret = regulator_enable(ctx->supply);
51 if (ret < 0) {
52 dev_err(&ctx->dsi->dev, "Failed to enable supply: %d\n", ret);
53 return ret;
54 }
55
56 msleep(20);
57 gpiod_set_value_cansleep(ctx->gpios.power, 1);
58 msleep(20);
59 gpiod_set_value_cansleep(ctx->gpios.reset, 1);
60 msleep(20);
61 return 0;
62 }
63
rb070d30_panel_unprepare(struct drm_panel * panel)64 static int rb070d30_panel_unprepare(struct drm_panel *panel)
65 {
66 struct rb070d30_panel *ctx = panel_to_rb070d30_panel(panel);
67
68 gpiod_set_value_cansleep(ctx->gpios.reset, 0);
69 gpiod_set_value_cansleep(ctx->gpios.power, 0);
70 regulator_disable(ctx->supply);
71
72 return 0;
73 }
74
rb070d30_panel_enable(struct drm_panel * panel)75 static int rb070d30_panel_enable(struct drm_panel *panel)
76 {
77 struct rb070d30_panel *ctx = panel_to_rb070d30_panel(panel);
78
79 return mipi_dsi_dcs_exit_sleep_mode(ctx->dsi);
80 }
81
rb070d30_panel_disable(struct drm_panel * panel)82 static int rb070d30_panel_disable(struct drm_panel *panel)
83 {
84 struct rb070d30_panel *ctx = panel_to_rb070d30_panel(panel);
85
86 return mipi_dsi_dcs_enter_sleep_mode(ctx->dsi);
87 }
88
89 /* Default timings */
90 static const struct drm_display_mode default_mode = {
91 .clock = 51206,
92 .hdisplay = 1024,
93 .hsync_start = 1024 + 160,
94 .hsync_end = 1024 + 160 + 80,
95 .htotal = 1024 + 160 + 80 + 80,
96 .vdisplay = 600,
97 .vsync_start = 600 + 12,
98 .vsync_end = 600 + 12 + 10,
99 .vtotal = 600 + 12 + 10 + 13,
100
101 .width_mm = 154,
102 .height_mm = 85,
103 };
104
rb070d30_panel_get_modes(struct drm_panel * panel,struct drm_connector * connector)105 static int rb070d30_panel_get_modes(struct drm_panel *panel,
106 struct drm_connector *connector)
107 {
108 struct rb070d30_panel *ctx = panel_to_rb070d30_panel(panel);
109 struct drm_display_mode *mode;
110 static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
111
112 mode = drm_mode_duplicate(connector->dev, &default_mode);
113 if (!mode) {
114 dev_err(&ctx->dsi->dev, "Failed to add mode " DRM_MODE_FMT "\n",
115 DRM_MODE_ARG(&default_mode));
116 return -EINVAL;
117 }
118
119 drm_mode_set_name(mode);
120
121 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
122 drm_mode_probed_add(connector, mode);
123
124 connector->display_info.bpc = 8;
125 connector->display_info.width_mm = mode->width_mm;
126 connector->display_info.height_mm = mode->height_mm;
127 drm_display_info_set_bus_formats(&connector->display_info,
128 &bus_format, 1);
129
130 return 1;
131 }
132
133 static const struct drm_panel_funcs rb070d30_panel_funcs = {
134 .get_modes = rb070d30_panel_get_modes,
135 .prepare = rb070d30_panel_prepare,
136 .enable = rb070d30_panel_enable,
137 .disable = rb070d30_panel_disable,
138 .unprepare = rb070d30_panel_unprepare,
139 };
140
rb070d30_panel_dsi_probe(struct mipi_dsi_device * dsi)141 static int rb070d30_panel_dsi_probe(struct mipi_dsi_device *dsi)
142 {
143 struct rb070d30_panel *ctx;
144 int ret;
145
146 ctx = devm_drm_panel_alloc(&dsi->dev, struct rb070d30_panel, panel,
147 &rb070d30_panel_funcs,
148 DRM_MODE_CONNECTOR_DSI);
149 if (IS_ERR(ctx))
150 return PTR_ERR(ctx);
151
152 ctx->supply = devm_regulator_get(&dsi->dev, "vcc-lcd");
153 if (IS_ERR(ctx->supply))
154 return PTR_ERR(ctx->supply);
155
156 mipi_dsi_set_drvdata(dsi, ctx);
157 ctx->dsi = dsi;
158
159 ctx->gpios.reset = devm_gpiod_get(&dsi->dev, "reset", GPIOD_OUT_LOW);
160 if (IS_ERR(ctx->gpios.reset)) {
161 dev_err(&dsi->dev, "Couldn't get our reset GPIO\n");
162 return PTR_ERR(ctx->gpios.reset);
163 }
164
165 ctx->gpios.power = devm_gpiod_get(&dsi->dev, "power", GPIOD_OUT_LOW);
166 if (IS_ERR(ctx->gpios.power)) {
167 dev_err(&dsi->dev, "Couldn't get our power GPIO\n");
168 return PTR_ERR(ctx->gpios.power);
169 }
170
171 /*
172 * We don't change the state of that GPIO later on but we need
173 * to force it into a low state.
174 */
175 ctx->gpios.updn = devm_gpiod_get(&dsi->dev, "updn", GPIOD_OUT_LOW);
176 if (IS_ERR(ctx->gpios.updn)) {
177 dev_err(&dsi->dev, "Couldn't get our updn GPIO\n");
178 return PTR_ERR(ctx->gpios.updn);
179 }
180
181 /*
182 * We don't change the state of that GPIO later on but we need
183 * to force it into a low state.
184 */
185 ctx->gpios.shlr = devm_gpiod_get(&dsi->dev, "shlr", GPIOD_OUT_LOW);
186 if (IS_ERR(ctx->gpios.shlr)) {
187 dev_err(&dsi->dev, "Couldn't get our shlr GPIO\n");
188 return PTR_ERR(ctx->gpios.shlr);
189 }
190
191 ret = drm_panel_of_backlight(&ctx->panel);
192 if (ret)
193 return ret;
194
195 drm_panel_add(&ctx->panel);
196
197 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST | MIPI_DSI_MODE_LPM;
198 dsi->format = MIPI_DSI_FMT_RGB888;
199 dsi->lanes = 4;
200
201 ret = mipi_dsi_attach(dsi);
202 if (ret < 0) {
203 drm_panel_remove(&ctx->panel);
204 return ret;
205 }
206
207 return 0;
208 }
209
rb070d30_panel_dsi_remove(struct mipi_dsi_device * dsi)210 static void rb070d30_panel_dsi_remove(struct mipi_dsi_device *dsi)
211 {
212 struct rb070d30_panel *ctx = mipi_dsi_get_drvdata(dsi);
213
214 mipi_dsi_detach(dsi);
215 drm_panel_remove(&ctx->panel);
216 }
217
218 static const struct of_device_id rb070d30_panel_of_match[] = {
219 { .compatible = "ronbo,rb070d30" },
220 { /* sentinel */ },
221 };
222 MODULE_DEVICE_TABLE(of, rb070d30_panel_of_match);
223
224 static struct mipi_dsi_driver rb070d30_panel_driver = {
225 .probe = rb070d30_panel_dsi_probe,
226 .remove = rb070d30_panel_dsi_remove,
227 .driver = {
228 .name = "panel-ronbo-rb070d30",
229 .of_match_table = rb070d30_panel_of_match,
230 },
231 };
232 module_mipi_dsi_driver(rb070d30_panel_driver);
233
234 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@bootlin.com>");
235 MODULE_AUTHOR("Konstantin Sudakov <k.sudakov@integrasources.com>");
236 MODULE_DESCRIPTION("Ronbo RB070D30 Panel Driver");
237 MODULE_LICENSE("GPL");
238