1
2
3
4
5
6
7
8
9
10
11#include <drm/drmP.h>
12#include <drm/drm_panel.h>
13
14#include <linux/gpio/consumer.h>
15#include <linux/regulator/consumer.h>
16#include <linux/spi/spi.h>
17
18#include <video/mipi_display.h>
19#include <video/of_videomode.h>
20#include <video/videomode.h>
21
22
23#define MCS_MANPWR 0xb0
24#define MCS_ELVSS_ON 0xb1
25#define MCS_USER_SETTING 0xf0
26#define MCS_DISPCTL 0xf2
27#define MCS_POWER_CTRL 0xf4
28#define MCS_GTCON 0xf7
29#define MCS_PANEL_CONDITION 0xf8
30#define MCS_GAMMA_SET1 0xf9
31#define MCS_GAMMA_CTRL 0xfb
32
33
34static u8 const ld9040_gammas[25][22] = {
35 { 0xf9, 0x00, 0x13, 0xb2, 0xba, 0xd2, 0x00, 0x30, 0x00, 0xaf, 0xc0,
36 0xb8, 0xcd, 0x00, 0x3d, 0x00, 0xa8, 0xb8, 0xb7, 0xcd, 0x00, 0x44 },
37 { 0xf9, 0x00, 0x13, 0xb9, 0xb9, 0xd0, 0x00, 0x3c, 0x00, 0xaf, 0xbf,
38 0xb6, 0xcb, 0x00, 0x4b, 0x00, 0xa8, 0xb9, 0xb5, 0xcc, 0x00, 0x52 },
39 { 0xf9, 0x00, 0x13, 0xba, 0xb9, 0xcd, 0x00, 0x41, 0x00, 0xb0, 0xbe,
40 0xb5, 0xc9, 0x00, 0x51, 0x00, 0xa9, 0xb9, 0xb5, 0xca, 0x00, 0x57 },
41 { 0xf9, 0x00, 0x13, 0xb9, 0xb8, 0xcd, 0x00, 0x46, 0x00, 0xb1, 0xbc,
42 0xb5, 0xc8, 0x00, 0x56, 0x00, 0xaa, 0xb8, 0xb4, 0xc9, 0x00, 0x5d },
43 { 0xf9, 0x00, 0x13, 0xba, 0xb8, 0xcb, 0x00, 0x4b, 0x00, 0xb3, 0xbc,
44 0xb4, 0xc7, 0x00, 0x5c, 0x00, 0xac, 0xb8, 0xb4, 0xc8, 0x00, 0x62 },
45 { 0xf9, 0x00, 0x13, 0xbb, 0xb7, 0xca, 0x00, 0x4f, 0x00, 0xb4, 0xbb,
46 0xb3, 0xc7, 0x00, 0x60, 0x00, 0xad, 0xb8, 0xb4, 0xc7, 0x00, 0x67 },
47 { 0xf9, 0x00, 0x47, 0xba, 0xb6, 0xca, 0x00, 0x53, 0x00, 0xb5, 0xbb,
48 0xb3, 0xc6, 0x00, 0x65, 0x00, 0xae, 0xb8, 0xb3, 0xc7, 0x00, 0x6c },
49 { 0xf9, 0x00, 0x71, 0xbb, 0xb5, 0xc8, 0x00, 0x57, 0x00, 0xb5, 0xbb,
50 0xb0, 0xc5, 0x00, 0x6a, 0x00, 0xae, 0xb9, 0xb1, 0xc6, 0x00, 0x70 },
51 { 0xf9, 0x00, 0x7b, 0xbb, 0xb4, 0xc8, 0x00, 0x5b, 0x00, 0xb5, 0xba,
52 0xb1, 0xc4, 0x00, 0x6e, 0x00, 0xae, 0xb9, 0xb0, 0xc5, 0x00, 0x75 },
53 { 0xf9, 0x00, 0x82, 0xba, 0xb4, 0xc7, 0x00, 0x5f, 0x00, 0xb5, 0xba,
54 0xb0, 0xc3, 0x00, 0x72, 0x00, 0xae, 0xb8, 0xb0, 0xc3, 0x00, 0x7a },
55 { 0xf9, 0x00, 0x89, 0xba, 0xb3, 0xc8, 0x00, 0x62, 0x00, 0xb6, 0xba,
56 0xaf, 0xc3, 0x00, 0x76, 0x00, 0xaf, 0xb7, 0xae, 0xc4, 0x00, 0x7e },
57 { 0xf9, 0x00, 0x8b, 0xb9, 0xb3, 0xc7, 0x00, 0x65, 0x00, 0xb7, 0xb8,
58 0xaf, 0xc3, 0x00, 0x7a, 0x00, 0x80, 0xb6, 0xae, 0xc4, 0x00, 0x81 },
59 { 0xf9, 0x00, 0x93, 0xba, 0xb3, 0xc5, 0x00, 0x69, 0x00, 0xb8, 0xb9,
60 0xae, 0xc1, 0x00, 0x7f, 0x00, 0xb0, 0xb6, 0xae, 0xc3, 0x00, 0x85 },
61 { 0xf9, 0x00, 0x97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0x00, 0xa8, 0xb8, 0xae, 0xbe, 0x00, 0x84, 0x00, 0xb9, 0xb7,
78 0xa8, 0xbc, 0x00, 0x9d, 0x00, 0xb2, 0xb5, 0xaa, 0xbc, 0x00, 0xa4 },
79 { 0xf9, 0x00, 0xa9, 0xb6, 0xad, 0xbf, 0x00, 0x86, 0x00, 0xb8, 0xb5,
80 0xa8, 0xbc, 0x00, 0xa0, 0x00, 0xb3, 0xb3, 0xa9, 0xbc, 0x00, 0xa7 },
81 { 0xf9, 0x00, 0xa9, 0xb7, 0xae, 0xbd, 0x00, 0x89, 0x00, 0xb7, 0xb6,
82 0xa8, 0xba, 0x00, 0xa4, 0x00, 0xb1, 0xb4, 0xaa, 0xbb, 0x00, 0xaa },
83 { 0xf9, 0x00, 0xa7, 0xb4, 0xae, 0xbf, 0x00, 0x91, 0x00, 0xb2, 0xb4,
84 0xaa, 0xbb, 0x00, 0xac, 0x00, 0xb3, 0xb1, 0xaa, 0xbc, 0x00, 0xb3 },
85};
86
87struct ld9040 {
88 struct device *dev;
89 struct drm_panel panel;
90
91 struct regulator_bulk_data supplies[2];
92 struct gpio_desc *reset_gpio;
93 u32 power_on_delay;
94 u32 reset_delay;
95 struct videomode vm;
96 u32 width_mm;
97 u32 height_mm;
98
99 int brightness;
100
101
102
103
104
105
106
107 int error;
108};
109
110static inline struct ld9040 *panel_to_ld9040(struct drm_panel *panel)
111{
112 return container_of(panel, struct ld9040, panel);
113}
114
115static int ld9040_clear_error(struct ld9040 *ctx)
116{
117 int ret = ctx->error;
118
119 ctx->error = 0;
120 return ret;
121}
122
123static int ld9040_spi_write_word(struct ld9040 *ctx, u16 data)
124{
125 struct spi_device *spi = to_spi_device(ctx->dev);
126 struct spi_transfer xfer = {
127 .len = 2,
128 .tx_buf = &data,
129 };
130 struct spi_message msg;
131
132 spi_message_init(&msg);
133 spi_message_add_tail(&xfer, &msg);
134
135 return spi_sync(spi, &msg);
136}
137
138static void ld9040_dcs_write(struct ld9040 *ctx, const u8 *data, size_t len)
139{
140 int ret = 0;
141
142 if (ctx->error < 0 || len == 0)
143 return;
144
145 dev_dbg(ctx->dev, "writing dcs seq: %*ph\n", (int)len, data);
146 ret = ld9040_spi_write_word(ctx, *data);
147
148 while (!ret && --len) {
149 ++data;
150 ret = ld9040_spi_write_word(ctx, *data | 0x100);
151 }
152
153 if (ret) {
154 dev_err(ctx->dev, "error %d writing dcs seq: %*ph\n", ret,
155 (int)len, data);
156 ctx->error = ret;
157 }
158
159 usleep_range(300, 310);
160}
161
162#define ld9040_dcs_write_seq_static(ctx, seq...) \
163({\
164 static const u8 d[] = { seq };\
165 ld9040_dcs_write(ctx, d, ARRAY_SIZE(d));\
166})
167
168static void ld9040_brightness_set(struct ld9040 *ctx)
169{
170 ld9040_dcs_write(ctx, ld9040_gammas[ctx->brightness],
171 ARRAY_SIZE(ld9040_gammas[ctx->brightness]));
172
173 ld9040_dcs_write_seq_static(ctx, MCS_GAMMA_CTRL, 0x02, 0x5a);
174}
175
176static void ld9040_init(struct ld9040 *ctx)
177{
178 ld9040_dcs_write_seq_static(ctx, MCS_USER_SETTING, 0x5a, 0x5a);
179 ld9040_dcs_write_seq_static(ctx, MCS_PANEL_CONDITION,
180 0x05, 0x65, 0x96, 0x71, 0x7d, 0x19, 0x3b, 0x0d,
181 0x19, 0x7e, 0x0d, 0xe2, 0x00, 0x00, 0x7e, 0x7d,
182 0x07, 0x07, 0x20, 0x20, 0x20, 0x02, 0x02);
183 ld9040_dcs_write_seq_static(ctx, MCS_DISPCTL,
184 0x02, 0x08, 0x08, 0x10, 0x10);
185 ld9040_dcs_write_seq_static(ctx, MCS_MANPWR, 0x04);
186 ld9040_dcs_write_seq_static(ctx, MCS_POWER_CTRL,
187 0x0a, 0x87, 0x25, 0x6a, 0x44, 0x02, 0x88);
188 ld9040_dcs_write_seq_static(ctx, MCS_ELVSS_ON, 0x0d, 0x00, 0x16);
189 ld9040_dcs_write_seq_static(ctx, MCS_GTCON, 0x09, 0x00, 0x00);
190 ld9040_brightness_set(ctx);
191 ld9040_dcs_write_seq_static(ctx, MIPI_DCS_EXIT_SLEEP_MODE);
192 ld9040_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_ON);
193}
194
195static int ld9040_power_on(struct ld9040 *ctx)
196{
197 int ret;
198
199 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
200 if (ret < 0)
201 return ret;
202
203 msleep(ctx->power_on_delay);
204 gpiod_set_value(ctx->reset_gpio, 0);
205 msleep(ctx->reset_delay);
206 gpiod_set_value(ctx->reset_gpio, 1);
207 msleep(ctx->reset_delay);
208
209 return 0;
210}
211
212static int ld9040_power_off(struct ld9040 *ctx)
213{
214 return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
215}
216
217static int ld9040_disable(struct drm_panel *panel)
218{
219 return 0;
220}
221
222static int ld9040_unprepare(struct drm_panel *panel)
223{
224 struct ld9040 *ctx = panel_to_ld9040(panel);
225
226 msleep(120);
227 ld9040_dcs_write_seq_static(ctx, MIPI_DCS_SET_DISPLAY_OFF);
228 ld9040_dcs_write_seq_static(ctx, MIPI_DCS_ENTER_SLEEP_MODE);
229 msleep(40);
230
231 ld9040_clear_error(ctx);
232
233 return ld9040_power_off(ctx);
234}
235
236static int ld9040_prepare(struct drm_panel *panel)
237{
238 struct ld9040 *ctx = panel_to_ld9040(panel);
239 int ret;
240
241 ret = ld9040_power_on(ctx);
242 if (ret < 0)
243 return ret;
244
245 ld9040_init(ctx);
246
247 ret = ld9040_clear_error(ctx);
248
249 if (ret < 0)
250 ld9040_unprepare(panel);
251
252 return ret;
253}
254
255static int ld9040_enable(struct drm_panel *panel)
256{
257 return 0;
258}
259
260static int ld9040_get_modes(struct drm_panel *panel)
261{
262 struct drm_connector *connector = panel->connector;
263 struct ld9040 *ctx = panel_to_ld9040(panel);
264 struct drm_display_mode *mode;
265
266 mode = drm_mode_create(connector->dev);
267 if (!mode) {
268 DRM_ERROR("failed to create a new display mode\n");
269 return 0;
270 }
271
272 drm_display_mode_from_videomode(&ctx->vm, mode);
273 mode->width_mm = ctx->width_mm;
274 mode->height_mm = ctx->height_mm;
275 connector->display_info.width_mm = mode->width_mm;
276 connector->display_info.height_mm = mode->height_mm;
277
278 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
279 drm_mode_probed_add(connector, mode);
280
281 return 1;
282}
283
284static const struct drm_panel_funcs ld9040_drm_funcs = {
285 .disable = ld9040_disable,
286 .unprepare = ld9040_unprepare,
287 .prepare = ld9040_prepare,
288 .enable = ld9040_enable,
289 .get_modes = ld9040_get_modes,
290};
291
292static int ld9040_parse_dt(struct ld9040 *ctx)
293{
294 struct device *dev = ctx->dev;
295 struct device_node *np = dev->of_node;
296 int ret;
297
298 ret = of_get_videomode(np, &ctx->vm, 0);
299 if (ret < 0)
300 return ret;
301
302 of_property_read_u32(np, "power-on-delay", &ctx->power_on_delay);
303 of_property_read_u32(np, "reset-delay", &ctx->reset_delay);
304 of_property_read_u32(np, "panel-width-mm", &ctx->width_mm);
305 of_property_read_u32(np, "panel-height-mm", &ctx->height_mm);
306
307 return 0;
308}
309
310static int ld9040_probe(struct spi_device *spi)
311{
312 struct device *dev = &spi->dev;
313 struct ld9040 *ctx;
314 int ret;
315
316 ctx = devm_kzalloc(dev, sizeof(struct ld9040), GFP_KERNEL);
317 if (!ctx)
318 return -ENOMEM;
319
320 spi_set_drvdata(spi, ctx);
321
322 ctx->dev = dev;
323 ctx->brightness = ARRAY_SIZE(ld9040_gammas) - 1;
324
325 ret = ld9040_parse_dt(ctx);
326 if (ret < 0)
327 return ret;
328
329 ctx->supplies[0].supply = "vdd3";
330 ctx->supplies[1].supply = "vci";
331 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
332 ctx->supplies);
333 if (ret < 0)
334 return ret;
335
336 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
337 if (IS_ERR(ctx->reset_gpio)) {
338 dev_err(dev, "cannot get reset-gpios %ld\n",
339 PTR_ERR(ctx->reset_gpio));
340 return PTR_ERR(ctx->reset_gpio);
341 }
342
343 spi->bits_per_word = 9;
344 ret = spi_setup(spi);
345 if (ret < 0) {
346 dev_err(dev, "spi setup failed.\n");
347 return ret;
348 }
349
350 drm_panel_init(&ctx->panel);
351 ctx->panel.dev = dev;
352 ctx->panel.funcs = &ld9040_drm_funcs;
353
354 return drm_panel_add(&ctx->panel);
355}
356
357static int ld9040_remove(struct spi_device *spi)
358{
359 struct ld9040 *ctx = spi_get_drvdata(spi);
360
361 ld9040_power_off(ctx);
362 drm_panel_remove(&ctx->panel);
363
364 return 0;
365}
366
367static const struct of_device_id ld9040_of_match[] = {
368 { .compatible = "samsung,ld9040" },
369 { }
370};
371MODULE_DEVICE_TABLE(of, ld9040_of_match);
372
373static struct spi_driver ld9040_driver = {
374 .probe = ld9040_probe,
375 .remove = ld9040_remove,
376 .driver = {
377 .name = "panel-samsung-ld9040",
378 .of_match_table = ld9040_of_match,
379 },
380};
381module_spi_driver(ld9040_driver);
382
383MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
384MODULE_DESCRIPTION("ld9040 LCD Driver");
385MODULE_LICENSE("GPL v2");
386