1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
3 #include <drm/drm_atomic.h>
4 #include <drm/drm_atomic_helper.h>
5 #include <drm/drm_client_setup.h>
6 #include <drm/drm_connector.h>
7 #include <drm/drm_damage_helper.h>
8 #include <drm/drm_drv.h>
9 #include <drm/drm_fb_dma_helper.h>
10 #include <drm/drm_fbdev_dma.h>
11 #include <drm/drm_format_helper.h>
12 #include <drm/drm_framebuffer.h>
13 #include <drm/drm_gem_atomic_helper.h>
14 #include <drm/drm_gem_dma_helper.h>
15 #include <drm/drm_gem_framebuffer_helper.h>
16 #include <drm/drm_managed.h>
17 #include <drm/drm_modes.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_rect.h>
20 #include <linux/bitrev.h>
21 #include <linux/delay.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/kthread.h>
24 #include <linux/mod_devicetable.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/pwm.h>
28 #include <linux/spi/spi.h>
30 #define SHARP_MODE_PERIOD 8
31 #define SHARP_ADDR_PERIOD 8
32 #define SHARP_DUMMY_PERIOD 8
34 #define SHARP_MEMORY_DISPLAY_MAINTAIN_MODE 0
35 #define SHARP_MEMORY_DISPLAY_UPDATE_MODE 1
36 #define SHARP_MEMORY_DISPLAY_CLEAR_MODE 4
38 enum sharp_memory_model {
51 enum sharp_memory_vcom_mode {
52 SHARP_MEMORY_SOFTWARE_VCOM,
53 SHARP_MEMORY_EXTERNAL_VCOM,
57 struct sharp_memory_device {
58 struct drm_device drm;
59 struct spi_device *spi;
61 const struct drm_display_mode *mode;
64 struct drm_plane plane;
65 struct drm_encoder encoder;
66 struct drm_connector connector;
68 struct gpio_desc *enable_gpio;
70 struct task_struct *sw_vcom_signal;
71 struct pwm_device *pwm_vcom_signal;
73 enum sharp_memory_vcom_mode vcom_mode;
80 /* When vcom_mode == "software" a kthread is used to periodically send a
81 * 'maintain display' message over spi. This mutex ensures tx_buffer access
82 * and spi bus usage is synchronized in this case.
84 struct mutex tx_mutex;
87 static inline int sharp_memory_spi_write(struct spi_device *spi, void *buf, size_t len)
89 /* Reverse the bit order */
90 for (u8 *b = buf; b < ((u8 *)buf) + len; ++b)
93 return spi_write(spi, buf, len);
96 static inline struct sharp_memory_device *drm_to_sharp_memory_device(struct drm_device *drm)
98 return container_of(drm, struct sharp_memory_device, drm);
101 DEFINE_DRM_GEM_DMA_FOPS(sharp_memory_fops);
103 static const struct drm_driver sharp_memory_drm_driver = {
104 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
105 .fops = &sharp_memory_fops,
106 DRM_GEM_DMA_DRIVER_OPS_VMAP,
107 DRM_FBDEV_DMA_DRIVER_OPS,
108 .name = "sharp_memory_display",
109 .desc = "Sharp Display Memory LCD",
115 static inline void sharp_memory_set_tx_buffer_mode(u8 *buffer, u8 mode, u8 vcom)
117 *buffer = mode | (vcom << 1);
120 static inline void sharp_memory_set_tx_buffer_addresses(u8 *buffer,
121 struct drm_rect clip,
124 for (u32 line = 0; line < clip.y2; ++line)
125 buffer[line * pitch] = line + 1;
128 static void sharp_memory_set_tx_buffer_data(u8 *buffer,
129 struct drm_framebuffer *fb,
130 struct drm_rect clip,
132 struct drm_format_conv_state *fmtcnv_state)
135 struct iosys_map dst, vmap;
136 struct drm_gem_dma_object *dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
138 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
142 iosys_map_set_vaddr(&dst, buffer);
143 iosys_map_set_vaddr(&vmap, dma_obj->vaddr);
145 drm_fb_xrgb8888_to_mono(&dst, &pitch, &vmap, fb, &clip, fmtcnv_state);
147 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
150 static int sharp_memory_update_display(struct sharp_memory_device *smd,
151 struct drm_framebuffer *fb,
152 struct drm_rect clip,
153 struct drm_format_conv_state *fmtcnv_state)
156 u32 pitch = smd->pitch;
158 u8 *tx_buffer = smd->tx_buffer;
159 u32 tx_buffer_size = smd->tx_buffer_size;
161 mutex_lock(&smd->tx_mutex);
163 /* Populate the transmit buffer with frame data */
164 sharp_memory_set_tx_buffer_mode(&tx_buffer[0],
165 SHARP_MEMORY_DISPLAY_UPDATE_MODE, vcom);
166 sharp_memory_set_tx_buffer_addresses(&tx_buffer[1], clip, pitch);
167 sharp_memory_set_tx_buffer_data(&tx_buffer[2], fb, clip, pitch, fmtcnv_state);
169 ret = sharp_memory_spi_write(smd->spi, tx_buffer, tx_buffer_size);
171 mutex_unlock(&smd->tx_mutex);
176 static int sharp_memory_maintain_display(struct sharp_memory_device *smd)
180 u8 *tx_buffer = smd->tx_buffer;
182 mutex_lock(&smd->tx_mutex);
184 sharp_memory_set_tx_buffer_mode(&tx_buffer[0], SHARP_MEMORY_DISPLAY_MAINTAIN_MODE, vcom);
185 tx_buffer[1] = 0; /* Write dummy data */
186 ret = sharp_memory_spi_write(smd->spi, tx_buffer, 2);
188 mutex_unlock(&smd->tx_mutex);
193 static int sharp_memory_clear_display(struct sharp_memory_device *smd)
197 u8 *tx_buffer = smd->tx_buffer;
199 mutex_lock(&smd->tx_mutex);
201 sharp_memory_set_tx_buffer_mode(&tx_buffer[0], SHARP_MEMORY_DISPLAY_CLEAR_MODE, vcom);
202 tx_buffer[1] = 0; /* write dummy data */
203 ret = sharp_memory_spi_write(smd->spi, tx_buffer, 2);
205 mutex_unlock(&smd->tx_mutex);
210 static void sharp_memory_fb_dirty(struct drm_framebuffer *fb, struct drm_rect *rect,
211 struct drm_format_conv_state *fmtconv_state)
213 struct drm_rect clip;
214 struct sharp_memory_device *smd = drm_to_sharp_memory_device(fb->dev);
216 /* Always update a full line regardless of what is dirty */
222 sharp_memory_update_display(smd, fb, clip, fmtconv_state);
225 static int sharp_memory_plane_atomic_check(struct drm_plane *plane,
226 struct drm_atomic_state *state)
228 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
229 struct sharp_memory_device *smd;
230 struct drm_crtc_state *crtc_state;
232 smd = container_of(plane, struct sharp_memory_device, plane);
233 crtc_state = drm_atomic_get_new_crtc_state(state, &smd->crtc);
235 return drm_atomic_helper_check_plane_state(plane_state, crtc_state,
236 DRM_PLANE_NO_SCALING,
237 DRM_PLANE_NO_SCALING,
241 static void sharp_memory_plane_atomic_update(struct drm_plane *plane,
242 struct drm_atomic_state *state)
244 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
245 struct drm_plane_state *plane_state = plane->state;
246 struct drm_format_conv_state fmtcnv_state = DRM_FORMAT_CONV_STATE_INIT;
247 struct sharp_memory_device *smd;
248 struct drm_rect rect;
250 smd = container_of(plane, struct sharp_memory_device, plane);
251 if (!smd->crtc.state->active)
254 if (drm_atomic_helper_damage_merged(old_state, plane_state, &rect))
255 sharp_memory_fb_dirty(plane_state->fb, &rect, &fmtcnv_state);
257 drm_format_conv_state_release(&fmtcnv_state);
260 static const struct drm_plane_helper_funcs sharp_memory_plane_helper_funcs = {
261 .prepare_fb = drm_gem_plane_helper_prepare_fb,
262 .atomic_check = sharp_memory_plane_atomic_check,
263 .atomic_update = sharp_memory_plane_atomic_update,
266 static bool sharp_memory_format_mod_supported(struct drm_plane *plane,
270 return modifier == DRM_FORMAT_MOD_LINEAR;
273 static const struct drm_plane_funcs sharp_memory_plane_funcs = {
274 .update_plane = drm_atomic_helper_update_plane,
275 .disable_plane = drm_atomic_helper_disable_plane,
276 .destroy = drm_plane_cleanup,
277 .reset = drm_atomic_helper_plane_reset,
278 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
279 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
280 .format_mod_supported = sharp_memory_format_mod_supported,
283 static enum drm_mode_status sharp_memory_crtc_mode_valid(struct drm_crtc *crtc,
284 const struct drm_display_mode *mode)
286 struct sharp_memory_device *smd = drm_to_sharp_memory_device(crtc->dev);
288 return drm_crtc_helper_mode_valid_fixed(crtc, mode, smd->mode);
291 static int sharp_memory_crtc_check(struct drm_crtc *crtc,
292 struct drm_atomic_state *state)
294 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
297 if (!crtc_state->enable)
300 ret = drm_atomic_helper_check_crtc_primary_plane(crtc_state);
305 return drm_atomic_add_affected_planes(state, crtc);
308 static int sharp_memory_sw_vcom_signal_thread(void *data)
310 struct sharp_memory_device *smd = data;
312 while (!kthread_should_stop()) {
313 smd->vcom ^= 1; /* Toggle vcom */
314 sharp_memory_maintain_display(smd);
321 static void sharp_memory_crtc_enable(struct drm_crtc *crtc,
322 struct drm_atomic_state *state)
324 struct sharp_memory_device *smd = drm_to_sharp_memory_device(crtc->dev);
326 sharp_memory_clear_display(smd);
328 if (smd->enable_gpio)
329 gpiod_set_value(smd->enable_gpio, 1);
332 static void sharp_memory_crtc_disable(struct drm_crtc *crtc,
333 struct drm_atomic_state *state)
335 struct sharp_memory_device *smd = drm_to_sharp_memory_device(crtc->dev);
337 sharp_memory_clear_display(smd);
339 if (smd->enable_gpio)
340 gpiod_set_value(smd->enable_gpio, 0);
343 static const struct drm_crtc_helper_funcs sharp_memory_crtc_helper_funcs = {
344 .mode_valid = sharp_memory_crtc_mode_valid,
345 .atomic_check = sharp_memory_crtc_check,
346 .atomic_enable = sharp_memory_crtc_enable,
347 .atomic_disable = sharp_memory_crtc_disable,
350 static const struct drm_crtc_funcs sharp_memory_crtc_funcs = {
351 .reset = drm_atomic_helper_crtc_reset,
352 .destroy = drm_crtc_cleanup,
353 .set_config = drm_atomic_helper_set_config,
354 .page_flip = drm_atomic_helper_page_flip,
355 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
356 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
359 static const struct drm_encoder_funcs sharp_memory_encoder_funcs = {
360 .destroy = drm_encoder_cleanup,
363 static int sharp_memory_connector_get_modes(struct drm_connector *connector)
365 struct sharp_memory_device *smd = drm_to_sharp_memory_device(connector->dev);
367 return drm_connector_helper_get_modes_fixed(connector, smd->mode);
370 static const struct drm_connector_helper_funcs sharp_memory_connector_hfuncs = {
371 .get_modes = sharp_memory_connector_get_modes,
374 static const struct drm_connector_funcs sharp_memory_connector_funcs = {
375 .reset = drm_atomic_helper_connector_reset,
376 .fill_modes = drm_helper_probe_single_connector_modes,
377 .destroy = drm_connector_cleanup,
378 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
379 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
383 static const struct drm_mode_config_funcs sharp_memory_mode_config_funcs = {
384 .fb_create = drm_gem_fb_create_with_dirty,
385 .atomic_check = drm_atomic_helper_check,
386 .atomic_commit = drm_atomic_helper_commit,
389 static const struct drm_display_mode sharp_memory_ls010b7dh04_mode = {
390 DRM_SIMPLE_MODE(128, 128, 18, 18),
393 static const struct drm_display_mode sharp_memory_ls011b7dh03_mode = {
394 DRM_SIMPLE_MODE(160, 68, 25, 10),
397 static const struct drm_display_mode sharp_memory_ls012b7dd01_mode = {
398 DRM_SIMPLE_MODE(184, 38, 29, 6),
401 static const struct drm_display_mode sharp_memory_ls013b7dh03_mode = {
402 DRM_SIMPLE_MODE(128, 128, 23, 23),
405 static const struct drm_display_mode sharp_memory_ls013b7dh05_mode = {
406 DRM_SIMPLE_MODE(144, 168, 20, 24),
409 static const struct drm_display_mode sharp_memory_ls018b7dh02_mode = {
410 DRM_SIMPLE_MODE(230, 303, 27, 36),
413 static const struct drm_display_mode sharp_memory_ls027b7dh01_mode = {
414 DRM_SIMPLE_MODE(400, 240, 58, 35),
417 static const struct drm_display_mode sharp_memory_ls032b7dd02_mode = {
418 DRM_SIMPLE_MODE(336, 536, 42, 68),
421 static const struct drm_display_mode sharp_memory_ls044q7dh01_mode = {
422 DRM_SIMPLE_MODE(320, 240, 89, 67),
425 static const struct spi_device_id sharp_memory_ids[] = {
426 {"ls010b7dh04", (kernel_ulong_t)&sharp_memory_ls010b7dh04_mode},
427 {"ls011b7dh03", (kernel_ulong_t)&sharp_memory_ls011b7dh03_mode},
428 {"ls012b7dd01", (kernel_ulong_t)&sharp_memory_ls012b7dd01_mode},
429 {"ls013b7dh03", (kernel_ulong_t)&sharp_memory_ls013b7dh03_mode},
430 {"ls013b7dh05", (kernel_ulong_t)&sharp_memory_ls013b7dh05_mode},
431 {"ls018b7dh02", (kernel_ulong_t)&sharp_memory_ls018b7dh02_mode},
432 {"ls027b7dh01", (kernel_ulong_t)&sharp_memory_ls027b7dh01_mode},
433 {"ls027b7dh01a", (kernel_ulong_t)&sharp_memory_ls027b7dh01_mode},
434 {"ls032b7dd02", (kernel_ulong_t)&sharp_memory_ls032b7dd02_mode},
435 {"ls044q7dh01", (kernel_ulong_t)&sharp_memory_ls044q7dh01_mode},
438 MODULE_DEVICE_TABLE(spi, sharp_memory_ids);
440 static const struct of_device_id sharp_memory_of_match[] = {
441 {.compatible = "sharp,ls010b7dh04", &sharp_memory_ls010b7dh04_mode},
442 {.compatible = "sharp,ls011b7dh03", &sharp_memory_ls011b7dh03_mode},
443 {.compatible = "sharp,ls012b7dd01", &sharp_memory_ls012b7dd01_mode},
444 {.compatible = "sharp,ls013b7dh03", &sharp_memory_ls013b7dh03_mode},
445 {.compatible = "sharp,ls013b7dh05", &sharp_memory_ls013b7dh05_mode},
446 {.compatible = "sharp,ls018b7dh02", &sharp_memory_ls018b7dh02_mode},
447 {.compatible = "sharp,ls027b7dh01", &sharp_memory_ls027b7dh01_mode},
448 {.compatible = "sharp,ls027b7dh01a", &sharp_memory_ls027b7dh01_mode},
449 {.compatible = "sharp,ls032b7dd02", &sharp_memory_ls032b7dd02_mode},
450 {.compatible = "sharp,ls044q7dh01", &sharp_memory_ls044q7dh01_mode},
453 MODULE_DEVICE_TABLE(of, sharp_memory_of_match);
455 static const u32 sharp_memory_formats[] = {
459 static int sharp_memory_pipe_init(struct drm_device *dev,
460 struct sharp_memory_device *smd,
461 const u32 *formats, unsigned int format_count,
462 const u64 *format_modifiers)
465 struct drm_encoder *encoder = &smd->encoder;
466 struct drm_plane *plane = &smd->plane;
467 struct drm_crtc *crtc = &smd->crtc;
468 struct drm_connector *connector = &smd->connector;
470 drm_plane_helper_add(plane, &sharp_memory_plane_helper_funcs);
471 ret = drm_universal_plane_init(dev, plane, 0,
472 &sharp_memory_plane_funcs,
473 formats, format_count,
475 DRM_PLANE_TYPE_PRIMARY, NULL);
479 drm_crtc_helper_add(crtc, &sharp_memory_crtc_helper_funcs);
480 ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
481 &sharp_memory_crtc_funcs, NULL);
485 encoder->possible_crtcs = drm_crtc_mask(crtc);
486 ret = drm_encoder_init(dev, encoder, &sharp_memory_encoder_funcs,
487 DRM_MODE_ENCODER_NONE, NULL);
491 ret = drm_connector_init(&smd->drm, &smd->connector,
492 &sharp_memory_connector_funcs,
493 DRM_MODE_CONNECTOR_SPI);
497 drm_connector_helper_add(&smd->connector,
498 &sharp_memory_connector_hfuncs);
500 return drm_connector_attach_encoder(connector, encoder);
503 static int sharp_memory_init_pwm_vcom_signal(struct sharp_memory_device *smd)
506 struct device *dev = &smd->spi->dev;
507 struct pwm_state pwm_state;
509 smd->pwm_vcom_signal = devm_pwm_get(dev, NULL);
510 if (IS_ERR(smd->pwm_vcom_signal))
511 return dev_err_probe(dev, PTR_ERR(smd->pwm_vcom_signal),
512 "Could not get pwm device\n");
514 pwm_init_state(smd->pwm_vcom_signal, &pwm_state);
515 pwm_set_relative_duty_cycle(&pwm_state, 1, 10);
516 pwm_state.enabled = true;
517 ret = pwm_apply_might_sleep(smd->pwm_vcom_signal, &pwm_state);
519 return dev_err_probe(dev, -EINVAL, "Could not apply pwm state\n");
524 static int sharp_memory_probe(struct spi_device *spi)
528 struct sharp_memory_device *smd;
529 struct drm_device *drm;
530 const char *vcom_mode_str;
534 ret = spi_setup(spi);
536 return dev_err_probe(dev, ret, "Failed to setup spi device\n");
538 if (!dev->coherent_dma_mask) {
539 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
541 return dev_err_probe(dev, ret, "Failed to set dma mask\n");
544 smd = devm_drm_dev_alloc(dev, &sharp_memory_drm_driver,
545 struct sharp_memory_device, drm);
549 spi_set_drvdata(spi, smd);
553 ret = drmm_mode_config_init(drm);
555 return dev_err_probe(dev, ret, "Failed to initialize drm config\n");
557 smd->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH);
558 if (!smd->enable_gpio)
559 dev_warn(dev, "Enable gpio not defined\n");
561 drm->mode_config.funcs = &sharp_memory_mode_config_funcs;
562 smd->mode = spi_get_device_match_data(spi);
564 smd->pitch = (SHARP_ADDR_PERIOD + smd->mode->hdisplay + SHARP_DUMMY_PERIOD) / 8;
565 smd->tx_buffer_size = (SHARP_MODE_PERIOD +
566 (SHARP_ADDR_PERIOD + (smd->mode->hdisplay) + SHARP_DUMMY_PERIOD) *
567 smd->mode->vdisplay) / 8;
569 smd->tx_buffer = devm_kzalloc(dev, smd->tx_buffer_size, GFP_KERNEL);
573 mutex_init(&smd->tx_mutex);
576 * VCOM is a signal that prevents DC bias from being built up in
577 * the panel resulting in pixels being forever stuck in one state.
579 * This driver supports three different methods to generate this
580 * signal depending on EXTMODE pin:
582 * software (EXTMODE = L) - This mode uses a kthread to
583 * periodically send a "maintain display" message to the display,
584 * toggling the vcom bit on and off with each message
586 * external (EXTMODE = H) - This mode relies on an external
587 * clock to generate the signal on the EXTCOMM pin
589 * pwm (EXTMODE = H) - This mode uses a pwm device to generate
590 * the signal on the EXTCOMM pin
593 if (device_property_read_string(dev, "sharp,vcom-mode", &vcom_mode_str))
594 return dev_err_probe(dev, -EINVAL,
595 "Unable to find sharp,vcom-mode node in device tree\n");
597 if (!strcmp("software", vcom_mode_str)) {
598 smd->vcom_mode = SHARP_MEMORY_SOFTWARE_VCOM;
599 smd->sw_vcom_signal = kthread_run(sharp_memory_sw_vcom_signal_thread,
600 smd, "sw_vcom_signal");
602 } else if (!strcmp("external", vcom_mode_str)) {
603 smd->vcom_mode = SHARP_MEMORY_EXTERNAL_VCOM;
605 } else if (!strcmp("pwm", vcom_mode_str)) {
606 smd->vcom_mode = SHARP_MEMORY_PWM_VCOM;
607 ret = sharp_memory_init_pwm_vcom_signal(smd);
611 return dev_err_probe(dev, -EINVAL, "Invalid value set for vcom-mode\n");
614 drm->mode_config.min_width = smd->mode->hdisplay;
615 drm->mode_config.max_width = smd->mode->hdisplay;
616 drm->mode_config.min_height = smd->mode->vdisplay;
617 drm->mode_config.max_height = smd->mode->vdisplay;
619 ret = sharp_memory_pipe_init(drm, smd, sharp_memory_formats,
620 ARRAY_SIZE(sharp_memory_formats),
623 return dev_err_probe(dev, ret, "Failed to initialize display pipeline.\n");
625 drm_plane_enable_fb_damage_clips(&smd->plane);
626 drm_mode_config_reset(drm);
628 ret = drm_dev_register(drm, 0);
630 return dev_err_probe(dev, ret, "Failed to register drm device.\n");
632 drm_client_setup(drm, NULL);
637 static void sharp_memory_remove(struct spi_device *spi)
639 struct sharp_memory_device *smd = spi_get_drvdata(spi);
641 drm_dev_unplug(&smd->drm);
642 drm_atomic_helper_shutdown(&smd->drm);
644 switch (smd->vcom_mode) {
645 case SHARP_MEMORY_SOFTWARE_VCOM:
646 kthread_stop(smd->sw_vcom_signal);
649 case SHARP_MEMORY_EXTERNAL_VCOM:
652 case SHARP_MEMORY_PWM_VCOM:
653 pwm_disable(smd->pwm_vcom_signal);
658 static struct spi_driver sharp_memory_spi_driver = {
660 .name = "sharp_memory",
661 .of_match_table = sharp_memory_of_match,
663 .probe = sharp_memory_probe,
664 .remove = sharp_memory_remove,
665 .id_table = sharp_memory_ids,
667 module_spi_driver(sharp_memory_spi_driver);
670 MODULE_DESCRIPTION("SPI Protocol driver for the sharp_memory display");
671 MODULE_LICENSE("GPL");