1
2
3
4
5
6
7
8
9#include <drm/drm_mipi_dbi.h>
10#include <drm/drm_modes.h>
11#include <drm/drm_panel.h>
12
13#include <linux/backlight.h>
14#include <linux/delay.h>
15#include <linux/gpio/consumer.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/media-bus-format.h>
19#include <linux/module.h>
20#include <linux/regulator/consumer.h>
21#include <linux/spi/spi.h>
22
23#include <video/mipi_display.h>
24
25#define WS2401_RESCTL 0xb8
26#define WS2401_PSMPS 0xbd
27#define WS2401_NSMPS 0xbe
28#define WS2401_SMPS 0xbf
29#define WS2401_BCMODE 0xc1
30#define WS2401_WRBLCTL 0xc3
31#define WS2401_WRDISBV 0xc4
32#define WS2401_WRCTRLD 0xc6
33#define WS2401_WRMIE 0xc7
34#define WS2401_READ_ID1 0xda
35#define WS2401_READ_ID2 0xdb
36#define WS2401_READ_ID3 0xdc
37#define WS2401_GAMMA_R1 0xe7
38#define WS2401_GAMMA_G1 0xe8
39#define WS2401_GAMMA_B1 0xe9
40#define WS2401_GAMMA_R2 0xea
41#define WS2401_GAMMA_G2 0xeb
42#define WS2401_GAMMA_B2 0xec
43#define WS2401_PASSWD1 0xf0
44#define WS2401_DISCTL 0xf2
45#define WS2401_PWRCTL 0xf3
46#define WS2401_VCOMCTL 0xf4
47#define WS2401_SRCCTL 0xf5
48#define WS2401_PANELCTL 0xf6
49
50static const u8 ws2401_dbi_read_commands[] = {
51 WS2401_READ_ID1,
52 WS2401_READ_ID2,
53 WS2401_READ_ID3,
54 0,
55};
56
57
58
59
60
61struct ws2401 {
62
63 struct device *dev;
64
65 struct mipi_dbi dbi;
66
67 struct drm_panel panel;
68
69 u32 width;
70
71 u32 height;
72
73 struct gpio_desc *reset;
74
75 struct regulator_bulk_data regulators[2];
76
77 bool internal_bl;
78};
79
80static const struct drm_display_mode lms380kf01_480_800_mode = {
81
82
83
84
85 .clock = 24960,
86 .hdisplay = 480,
87 .hsync_start = 480 + 8,
88 .hsync_end = 480 + 8 + 10,
89 .htotal = 480 + 8 + 10 + 8,
90 .vdisplay = 800,
91 .vsync_start = 800 + 8,
92 .vsync_end = 800 + 8 + 2,
93 .vtotal = 800 + 8 + 2 + 18,
94 .width_mm = 50,
95 .height_mm = 84,
96 .flags = DRM_MODE_FLAG_NVSYNC | DRM_MODE_FLAG_NHSYNC,
97};
98
99static inline struct ws2401 *to_ws2401(struct drm_panel *panel)
100{
101 return container_of(panel, struct ws2401, panel);
102}
103
104static void ws2401_read_mtp_id(struct ws2401 *ws)
105{
106 struct mipi_dbi *dbi = &ws->dbi;
107 u8 id1, id2, id3;
108 int ret;
109
110 ret = mipi_dbi_command_read(dbi, WS2401_READ_ID1, &id1);
111 if (ret) {
112 dev_err(ws->dev, "unable to read MTP ID 1\n");
113 return;
114 }
115 ret = mipi_dbi_command_read(dbi, WS2401_READ_ID2, &id2);
116 if (ret) {
117 dev_err(ws->dev, "unable to read MTP ID 2\n");
118 return;
119 }
120 ret = mipi_dbi_command_read(dbi, WS2401_READ_ID3, &id3);
121 if (ret) {
122 dev_err(ws->dev, "unable to read MTP ID 3\n");
123 return;
124 }
125 dev_info(ws->dev, "MTP ID: %02x %02x %02x\n", id1, id2, id3);
126}
127
128static int ws2401_power_on(struct ws2401 *ws)
129{
130 struct mipi_dbi *dbi = &ws->dbi;
131 int ret;
132
133
134 ret = regulator_bulk_enable(ARRAY_SIZE(ws->regulators),
135 ws->regulators);
136 if (ret) {
137 dev_err(ws->dev, "failed to enable regulators: %d\n", ret);
138 return ret;
139 }
140 msleep(10);
141
142
143 gpiod_set_value_cansleep(ws->reset, 1);
144 usleep_range(1000, 5000);
145
146 gpiod_set_value_cansleep(ws->reset, 0);
147
148 msleep(10);
149 dev_dbg(ws->dev, "de-asserted RESET\n");
150
151
152
153
154
155 mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
156 mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE);
157 msleep(50);
158
159
160 mipi_dbi_command(dbi, WS2401_PASSWD1, 0x5a, 0x5a);
161
162 mipi_dbi_command(dbi, WS2401_RESCTL, 0x12);
163
164 mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, 0x01);
165
166 mipi_dbi_command(dbi, MIPI_DCS_SET_PIXEL_FORMAT, 0x70);
167 mipi_dbi_command(dbi, WS2401_SMPS, 0x00, 0x0f);
168 mipi_dbi_command(dbi, WS2401_PSMPS, 0x06, 0x03,
169 0x7e, 0x03, 0x12, 0x37);
170 mipi_dbi_command(dbi, WS2401_NSMPS, 0x06, 0x03,
171 0x7e, 0x02, 0x15, 0x37);
172 mipi_dbi_command(dbi, WS2401_SMPS, 0x02, 0x0f);
173 mipi_dbi_command(dbi, WS2401_PWRCTL, 0x10, 0xA9, 0x00, 0x01, 0x44,
174 0xb4,
175 0x50,
176 0x50,
177 0x00,
178 0x44);
179 mipi_dbi_command(dbi, WS2401_DISCTL, 0x01, 0x00, 0x00, 0x00, 0x14,
180 0x16);
181 mipi_dbi_command(dbi, WS2401_VCOMCTL, 0x30, 0x53, 0x53);
182 mipi_dbi_command(dbi, WS2401_SRCCTL, 0x03, 0x0C, 0x00, 0x00, 0x00,
183 0x01,
184 0x01, 0x06, 0x03);
185 mipi_dbi_command(dbi, WS2401_PANELCTL, 0x14, 0x00, 0x80, 0x00);
186 mipi_dbi_command(dbi, WS2401_WRMIE, 0x01);
187
188
189 mipi_dbi_command(dbi, WS2401_GAMMA_R1, 0x00,
190 0x5b, 0x42, 0x41, 0x3f, 0x42, 0x3d, 0x38, 0x2e,
191 0x2b, 0x2a, 0x27, 0x22, 0x27, 0x0f, 0x00, 0x00);
192 mipi_dbi_command(dbi, WS2401_GAMMA_R2, 0x00,
193 0x5b, 0x42, 0x41, 0x3f, 0x42, 0x3d, 0x38, 0x2e,
194 0x2b, 0x2a, 0x27, 0x22, 0x27, 0x0f, 0x00, 0x00);
195 mipi_dbi_command(dbi, WS2401_GAMMA_G1, 0x00,
196 0x59, 0x40, 0x3f, 0x3e, 0x41, 0x3d, 0x39, 0x2f,
197 0x2c, 0x2b, 0x29, 0x25, 0x29, 0x19, 0x08, 0x00);
198 mipi_dbi_command(dbi, WS2401_GAMMA_G2, 0x00,
199 0x59, 0x40, 0x3f, 0x3e, 0x41, 0x3d, 0x39, 0x2f,
200 0x2c, 0x2b, 0x29, 0x25, 0x29, 0x19, 0x08, 0x00);
201 mipi_dbi_command(dbi, WS2401_GAMMA_B1, 0x00,
202 0x57, 0x3b, 0x3a, 0x3b, 0x3f, 0x3b, 0x38, 0x27,
203 0x38, 0x2a, 0x26, 0x22, 0x34, 0x0c, 0x09, 0x00);
204 mipi_dbi_command(dbi, WS2401_GAMMA_B2, 0x00,
205 0x57, 0x3b, 0x3a, 0x3b, 0x3f, 0x3b, 0x38, 0x27,
206 0x38, 0x2a, 0x26, 0x22, 0x34, 0x0c, 0x09, 0x00);
207
208 if (ws->internal_bl) {
209 mipi_dbi_command(dbi, WS2401_WRCTRLD, 0x2c);
210 } else {
211 mipi_dbi_command(dbi, WS2401_WRCTRLD, 0x00);
212
213
214
215
216
217 mipi_dbi_command(dbi, WS2401_PASSWD1, 0xa5, 0xa5);
218 }
219
220 return 0;
221}
222
223static int ws2401_power_off(struct ws2401 *ws)
224{
225
226 gpiod_set_value_cansleep(ws->reset, 1);
227 return regulator_bulk_disable(ARRAY_SIZE(ws->regulators),
228 ws->regulators);
229}
230
231static int ws2401_unprepare(struct drm_panel *panel)
232{
233 struct ws2401 *ws = to_ws2401(panel);
234 struct mipi_dbi *dbi = &ws->dbi;
235
236
237 if (ws->internal_bl)
238 mipi_dbi_command(dbi, WS2401_WRCTRLD, 0x00);
239 mipi_dbi_command(dbi, MIPI_DCS_ENTER_SLEEP_MODE);
240 msleep(120);
241 return ws2401_power_off(to_ws2401(panel));
242}
243
244static int ws2401_disable(struct drm_panel *panel)
245{
246 struct ws2401 *ws = to_ws2401(panel);
247 struct mipi_dbi *dbi = &ws->dbi;
248
249 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF);
250 msleep(25);
251
252 return 0;
253}
254
255static int ws2401_prepare(struct drm_panel *panel)
256{
257 return ws2401_power_on(to_ws2401(panel));
258}
259
260static int ws2401_enable(struct drm_panel *panel)
261{
262 struct ws2401 *ws = to_ws2401(panel);
263 struct mipi_dbi *dbi = &ws->dbi;
264
265 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON);
266
267 return 0;
268}
269
270
271
272
273
274
275static int ws2401_get_modes(struct drm_panel *panel,
276 struct drm_connector *connector)
277{
278 struct ws2401 *ws = to_ws2401(panel);
279 struct drm_display_mode *mode;
280 static const u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
281
282
283
284
285
286
287
288 mode = drm_mode_duplicate(connector->dev, &lms380kf01_480_800_mode);
289 if (!mode) {
290 dev_err(ws->dev, "failed to add mode\n");
291 return -ENOMEM;
292 }
293
294 connector->display_info.bpc = 8;
295 connector->display_info.width_mm = mode->width_mm;
296 connector->display_info.height_mm = mode->height_mm;
297 connector->display_info.bus_flags =
298 DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE;
299 drm_display_info_set_bus_formats(&connector->display_info,
300 &bus_format, 1);
301
302 drm_mode_set_name(mode);
303 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
304
305 drm_mode_probed_add(connector, mode);
306
307 return 1;
308}
309
310static const struct drm_panel_funcs ws2401_drm_funcs = {
311 .disable = ws2401_disable,
312 .unprepare = ws2401_unprepare,
313 .prepare = ws2401_prepare,
314 .enable = ws2401_enable,
315 .get_modes = ws2401_get_modes,
316};
317
318static int ws2401_set_brightness(struct backlight_device *bl)
319{
320 struct ws2401 *ws = bl_get_data(bl);
321 struct mipi_dbi *dbi = &ws->dbi;
322 u8 brightness = backlight_get_brightness(bl);
323
324 if (backlight_is_blank(bl)) {
325 mipi_dbi_command(dbi, WS2401_WRCTRLD, 0x00);
326 } else {
327 mipi_dbi_command(dbi, WS2401_WRCTRLD, 0x2c);
328 mipi_dbi_command(dbi, WS2401_WRDISBV, brightness);
329 }
330
331 return 0;
332}
333
334static const struct backlight_ops ws2401_bl_ops = {
335 .update_status = ws2401_set_brightness,
336};
337
338static const struct backlight_properties ws2401_bl_props = {
339 .type = BACKLIGHT_PLATFORM,
340 .brightness = 120,
341 .max_brightness = U8_MAX,
342};
343
344static int ws2401_probe(struct spi_device *spi)
345{
346 struct device *dev = &spi->dev;
347 struct ws2401 *ws;
348 int ret;
349
350 ws = devm_kzalloc(dev, sizeof(*ws), GFP_KERNEL);
351 if (!ws)
352 return -ENOMEM;
353 ws->dev = dev;
354
355
356
357
358
359 ws->regulators[0].supply = "vci";
360 ws->regulators[1].supply = "vccio";
361 ret = devm_regulator_bulk_get(dev,
362 ARRAY_SIZE(ws->regulators),
363 ws->regulators);
364 if (ret)
365 return dev_err_probe(dev, ret, "failed to get regulators\n");
366
367 ws->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
368 if (IS_ERR(ws->reset)) {
369 ret = PTR_ERR(ws->reset);
370 return dev_err_probe(dev, ret, "no RESET GPIO\n");
371 }
372
373 ret = mipi_dbi_spi_init(spi, &ws->dbi, NULL);
374 if (ret)
375 return dev_err_probe(dev, ret, "MIPI DBI init failed\n");
376 ws->dbi.read_commands = ws2401_dbi_read_commands;
377
378 ws2401_power_on(ws);
379 ws2401_read_mtp_id(ws);
380 ws2401_power_off(ws);
381
382 drm_panel_init(&ws->panel, dev, &ws2401_drm_funcs,
383 DRM_MODE_CONNECTOR_DPI);
384
385 ret = drm_panel_of_backlight(&ws->panel);
386 if (ret)
387 return dev_err_probe(dev, ret,
388 "failed to get external backlight device\n");
389
390 if (!ws->panel.backlight) {
391 dev_dbg(dev, "no external backlight, using internal backlight\n");
392 ws->panel.backlight =
393 devm_backlight_device_register(dev, "ws2401", dev, ws,
394 &ws2401_bl_ops, &ws2401_bl_props);
395 if (IS_ERR(ws->panel.backlight))
396 return dev_err_probe(dev, PTR_ERR(ws->panel.backlight),
397 "failed to register backlight device\n");
398 } else {
399 dev_dbg(dev, "using external backlight\n");
400 }
401
402 spi_set_drvdata(spi, ws);
403
404 drm_panel_add(&ws->panel);
405 dev_dbg(dev, "added panel\n");
406
407 return 0;
408}
409
410static void ws2401_remove(struct spi_device *spi)
411{
412 struct ws2401 *ws = spi_get_drvdata(spi);
413
414 drm_panel_remove(&ws->panel);
415}
416
417
418
419
420
421
422static const struct of_device_id ws2401_match[] = {
423 { .compatible = "samsung,lms380kf01", },
424 {},
425};
426MODULE_DEVICE_TABLE(of, ws2401_match);
427
428static struct spi_driver ws2401_driver = {
429 .probe = ws2401_probe,
430 .remove = ws2401_remove,
431 .driver = {
432 .name = "ws2401-panel",
433 .of_match_table = ws2401_match,
434 },
435};
436module_spi_driver(ws2401_driver);
437
438MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
439MODULE_DESCRIPTION("Samsung WS2401 panel driver");
440MODULE_LICENSE("GPL v2");
441