// SPDX-License-Identifier: GPL-2.0-only /* * DRM driver for an S1D13700 LCD controller wired to a TCA9539 I2C expander. * * The expander is an ordinary I2C client of this driver rather than a * gpiochip: the display traffic needs one continuous write alternating both * ports, which the GPIO API cannot express. * * A full frame takes most of a second on the bus, so atomic_update only * converts the damage into a pending frame; a worker sends it, and damage * arriving meanwhile is merged into the next pass. A failed pass is retried * with backoff, redoing the controller init first if that is what failed. * * Needs a recent kernel (written against 6.18). */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "s1d13700_core.h" #define DRIVER_NAME "s1d13700-tca" /* Bus held for about 23 ms at 400 kHz, so other devices on it are not starved */ #define TX_CAP 1025u #define RETRY_MIN_MS 100u #define RETRY_MAX_MS 5000u static unsigned int width = 240; module_param(width, uint, 0444); MODULE_PARM_DESC(width, "Panel width in pixels"); static unsigned int height = 160; module_param(height, uint, 0444); MODULE_PARM_DESC(height, "Panel height in pixels"); static unsigned int bpp = 2; module_param(bpp, uint, 0444); MODULE_PARM_DESC(bpp, "Bits per pixel: 1, 2 or 4"); static unsigned int tcr = 162; module_param(tcr, uint, 0444); MODULE_PARM_DESC(tcr, "SYSTEM SET TC/R: sets the frame rate"); struct s1d_device { struct drm_device drm; struct drm_plane plane; struct drm_crtc crtc; struct drm_encoder encoder; struct drm_connector connector; struct drm_display_mode mode; struct i2c_client *client; struct s1d_config cfg; /* touched only by the worker, or with the worker stopped */ struct s1d_panel panel; u8 *work_frame; bool panel_ready; /* controller initialised and still powered */ unsigned int retry_ms; struct delayed_work flush_work; struct mutex lock; /* protects pending_frame, dirty_*, bl_on */ u8 *pending_frame; unsigned int dirty_y0, dirty_y1; bool bl_on; /* requested; the worker drives the pin */ }; static struct s1d_device *to_s1d(struct drm_device *drm) { return container_of(drm, struct s1d_device, drm); } /* ---- transport ---------------------------------------------------------- */ static int s1d_xfer(void *ctx, const u8 *buf, size_t len) { struct s1d_device *s = ctx; int ret = i2c_master_send(s->client, buf, len); if (ret < 0) return ret; return ret == len ? 0 : -EIO; } static void s1d_delay_ms(void *ctx, unsigned int ms) { msleep(ms); } static const struct s1d_ops s1d_panel_ops = { .xfer = s1d_xfer, .delay_ms = s1d_delay_ms, }; /* ---- worker ------------------------------------------------------------- */ static void s1d_flush_work(struct work_struct *work) { struct s1d_device *s = container_of(to_delayed_work(work), struct s1d_device, flush_work); unsigned int stride = s->panel.stride, y0, y1; bool bl_on; int idx, ret; mutex_lock(&s->lock); bl_on = s->bl_on; y0 = s->dirty_y0; y1 = s->dirty_y1; s->dirty_y0 = s->cfg.height; s->dirty_y1 = 0; if (y1 > y0) memcpy(s->work_frame + y0 * stride, s->pending_frame + y0 * stride, (y1 - y0) * stride); mutex_unlock(&s->lock); if (!drm_dev_enter(&s->drm, &idx)) return; /* before the first init this only records the level for init */ if (bl_on != s->panel.backlight) { ret = s1d_panel_backlight(&s->panel, bl_on); if (ret) drm_err_ratelimited(&s->drm, "backlight failed: %d\n", ret); } if (y1 <= y0) { drm_dev_exit(idx); return; } ret = s->panel_ready ? 0 : s1d_panel_init(&s->panel); if (!ret) { s->panel_ready = true; /* on failure the core rewrites the whole frame on the next update */ ret = s1d_panel_update(&s->panel, s->work_frame, y0, y1); } if (ret) { drm_err_ratelimited(&s->drm, "panel update failed: %d\n", ret); /* * Retry even if no new damage arrives. Marking everything dirty * also refreshes all of work_frame, which a re-init needs. */ mutex_lock(&s->lock); s->dirty_y0 = 0; s->dirty_y1 = s->cfg.height; mutex_unlock(&s->lock); queue_delayed_work(system_long_wq, &s->flush_work, msecs_to_jiffies(s->retry_ms)); s->retry_ms = min(s->retry_ms * 2, RETRY_MAX_MS); } else { s->retry_ms = RETRY_MIN_MS; } drm_dev_exit(idx); } /* ---- plane -------------------------------------------------------------- */ static int s1d_plane_atomic_check(struct drm_plane *plane, struct drm_atomic_state *state) { struct drm_plane_state *ps = drm_atomic_get_new_plane_state(state, plane); struct drm_crtc_state *cs = NULL; /* lines are read as u32 */ if (ps->fb && ps->fb->pitches[0] % 4) return -EINVAL; if (ps->crtc) cs = drm_atomic_get_new_crtc_state(state, ps->crtc); return drm_atomic_helper_check_plane_state(ps, cs, DRM_PLANE_NO_SCALING, DRM_PLANE_NO_SCALING, false, false); } static void s1d_plane_atomic_update(struct drm_plane *plane, struct drm_atomic_state *state) { struct drm_plane_state *old = drm_atomic_get_old_plane_state(state, plane); struct drm_plane_state *ps = drm_atomic_get_new_plane_state(state, plane); struct drm_shadow_plane_state *shadow = to_drm_shadow_plane_state(ps); struct s1d_device *s = to_s1d(plane->dev); struct drm_framebuffer *fb = ps->fb; struct drm_atomic_helper_damage_iter iter; struct drm_rect clip; unsigned int ox, oy; const u8 *base; bool any = false; int idx; if (!fb || !ps->visible) return; if (shadow->data[0].is_iomem) { drm_warn_once(plane->dev, "framebuffers in I/O memory are not supported\n"); return; } if (drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE)) { drm_err_ratelimited(plane->dev, "CPU access to framebuffer failed\n"); return; } if (!drm_dev_enter(plane->dev, &idx)) goto out_cpu; /* the panel origin is the plane's source origin in the framebuffer */ ox = ps->src.x1 >> 16; oy = ps->src.y1 >> 16; base = (const u8 *)shadow->data[0].vaddr + oy * fb->pitches[0] + ox * 4; mutex_lock(&s->lock); drm_atomic_helper_damage_iter_init(&iter, old, ps); drm_atomic_for_each_plane_damage(&iter, &clip) { unsigned int y0 = clip.y1 - oy, y1 = clip.y2 - oy; s1d_convert_xrgb8888(&s->cfg, base, fb->pitches[0], s->pending_frame, clip.x1 - ox, y0, clip.x2 - ox, y1); s->dirty_y0 = min(s->dirty_y0, y0); s->dirty_y1 = max(s->dirty_y1, y1); any = true; } mutex_unlock(&s->lock); /* new damage overrides any retry backoff */ if (any) mod_delayed_work(system_long_wq, &s->flush_work, 0); drm_dev_exit(idx); out_cpu: drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); } static const struct drm_plane_helper_funcs s1d_plane_helper_funcs = { DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, .atomic_check = s1d_plane_atomic_check, .atomic_update = s1d_plane_atomic_update, }; static const struct drm_plane_funcs s1d_plane_funcs = { .update_plane = drm_atomic_helper_update_plane, .disable_plane = drm_atomic_helper_disable_plane, .destroy = drm_plane_cleanup, DRM_GEM_SHADOW_PLANE_FUNCS, }; /* ---- CRTC --------------------------------------------------------------- */ static void s1d_crtc_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state) { struct s1d_device *s = to_s1d(crtc->dev); int idx, ret; if (!drm_dev_enter(crtc->dev, &idx)) return; /* * Enables run before plane updates (commit_tail_rpm), so this frame's * damage is not queued yet; a dropped retry is covered by the full * damage a modeset brings. */ cancel_delayed_work_sync(&s->flush_work); s->retry_ms = RETRY_MIN_MS; /* VRAM survives a plain disable, so only the display needs turning on */ if (s->panel_ready && !s1d_panel_display(&s->panel, true)) goto out; /* init drives the backlight pin too, so give it the current request */ mutex_lock(&s->lock); s->panel.backlight = s->bl_on; mutex_unlock(&s->lock); /* VRAM is cleared by init, so the shadow and work frame start at zero */ ret = s1d_panel_init(&s->panel); if (ret) drm_err(&s->drm, "panel init failed: %d\n", ret); s->panel_ready = !ret; memset(s->work_frame, 0, s->panel.size); out: drm_dev_exit(idx); } static void s1d_crtc_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state) { struct s1d_device *s = to_s1d(crtc->dev); int idx; if (!drm_dev_enter(crtc->dev, &idx)) return; cancel_delayed_work_sync(&s->flush_work); if (s->panel_ready) s1d_panel_display(&s->panel, false); drm_dev_exit(idx); } static const struct drm_crtc_helper_funcs s1d_crtc_helper_funcs = { .atomic_check = drm_crtc_helper_atomic_check, .atomic_enable = s1d_crtc_atomic_enable, .atomic_disable = s1d_crtc_atomic_disable, }; static const struct drm_crtc_funcs s1d_crtc_funcs = { .reset = drm_atomic_helper_crtc_reset, .destroy = drm_crtc_cleanup, .set_config = drm_atomic_helper_set_config, .page_flip = drm_atomic_helper_page_flip, .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, }; /* ---- encoder and connector ---------------------------------------------- */ static const struct drm_encoder_funcs s1d_encoder_funcs = { .destroy = drm_encoder_cleanup, }; static int s1d_connector_get_modes(struct drm_connector *connector) { return drm_connector_helper_get_modes_fixed(connector, &to_s1d(connector->dev)->mode); } static const struct drm_connector_helper_funcs s1d_connector_helper_funcs = { .get_modes = s1d_connector_get_modes, }; static const struct drm_connector_funcs s1d_connector_funcs = { .reset = drm_atomic_helper_connector_reset, .fill_modes = drm_helper_probe_single_connector_modes, .destroy = drm_connector_cleanup, .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, }; /* ---- device ------------------------------------------------------------- */ /* enables before plane updates, so the first frame reaches an initialised panel */ static const struct drm_mode_config_helper_funcs s1d_mode_config_helpers = { .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm, }; static const struct drm_mode_config_funcs s1d_mode_config_funcs = { .fb_create = drm_gem_fb_create_with_dirty, .atomic_check = drm_atomic_helper_check, .atomic_commit = drm_atomic_helper_commit, }; static const u32 s1d_formats[] = { DRM_FORMAT_XRGB8888 }; static const u64 s1d_modifiers[] = { DRM_FORMAT_MOD_LINEAR, DRM_FORMAT_MOD_INVALID }; DEFINE_DRM_GEM_FOPS(s1d_fops); static const struct drm_driver s1d_drm_driver = { .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC, .name = DRIVER_NAME, .desc = "S1D13700 LCD behind a TCA9539 expander", .major = 1, .minor = 0, .fops = &s1d_fops, DRM_GEM_SHMEM_DRIVER_OPS, DRM_FBDEV_SHMEM_DRIVER_OPS, }; static int s1d_kms_init(struct s1d_device *s) { struct drm_device *drm = &s->drm; int ret; ret = drmm_mode_config_init(drm); if (ret) return ret; drm->mode_config.funcs = &s1d_mode_config_funcs; drm->mode_config.helper_private = &s1d_mode_config_helpers; drm->mode_config.min_width = s->cfg.width; drm->mode_config.max_width = s->cfg.width; drm->mode_config.min_height = s->cfg.height; drm->mode_config.max_height = s->cfg.height; drm->mode_config.preferred_depth = 24; ret = drm_universal_plane_init(drm, &s->plane, 0, &s1d_plane_funcs, s1d_formats, ARRAY_SIZE(s1d_formats), s1d_modifiers, DRM_PLANE_TYPE_PRIMARY, NULL); if (ret) return ret; drm_plane_helper_add(&s->plane, &s1d_plane_helper_funcs); drm_plane_enable_fb_damage_clips(&s->plane); ret = drm_crtc_init_with_planes(drm, &s->crtc, &s->plane, NULL, &s1d_crtc_funcs, NULL); if (ret) return ret; drm_crtc_helper_add(&s->crtc, &s1d_crtc_helper_funcs); ret = drm_encoder_init(drm, &s->encoder, &s1d_encoder_funcs, DRM_MODE_ENCODER_NONE, NULL); if (ret) return ret; s->encoder.possible_crtcs = drm_crtc_mask(&s->crtc); ret = drm_connector_init(drm, &s->connector, &s1d_connector_funcs, DRM_MODE_CONNECTOR_Unknown); if (ret) return ret; drm_connector_helper_add(&s->connector, &s1d_connector_helper_funcs); ret = drm_connector_attach_encoder(&s->connector, &s->encoder); if (ret) return ret; drm_mode_config_reset(drm); return 0; } /* ---- backlight ---------------------------------------------------------- */ /* The panel belongs to the worker, so only record the request and kick it */ static int s1d_bl_update(struct backlight_device *bd) { struct s1d_device *s = bl_get_data(bd); mutex_lock(&s->lock); s->bl_on = backlight_get_brightness(bd) > 0; mutex_unlock(&s->lock); mod_delayed_work(system_long_wq, &s->flush_work, 0); return 0; } static const struct backlight_ops s1d_bl_ops = { .options = BL_CORE_SUSPENDRESUME, .update_status = s1d_bl_update, }; static void s1d_cancel_work(void *data) { struct s1d_device *s = data; cancel_delayed_work_sync(&s->flush_work); } static int s1d_backlight_init(struct s1d_device *s) { const struct backlight_properties props = { .type = BACKLIGHT_RAW, .max_brightness = 1, .brightness = 1, }; struct backlight_device *bd; int ret; /* * devm undoes in reverse: this runs after the backlight is gone, so a * brightness write during unbind cannot leave work behind. */ ret = devm_add_action_or_reset(&s->client->dev, s1d_cancel_work, s); if (ret) return ret; s->bl_on = true; /* the name the frontends look for by default */ bd = devm_backlight_device_register(&s->client->dev, "radio-backlight", &s->client->dev, s, &s1d_bl_ops, &props); return PTR_ERR_OR_ZERO(bd); } static void s1d_debugfs_init(struct s1d_device *s) { struct dentry *d = s->client->debugfs; struct s1d_stats *st = &s->panel.stats; debugfs_create_ulong("lcd_writes", 0444, d, &st->writes); debugfs_create_ulong("data_bytes", 0444, d, &st->data_bytes); debugfs_create_ulong("seeks", 0444, d, &st->seeks); debugfs_create_ulong("dir_changes", 0444, d, &st->dir_changes); debugfs_create_ulong("i2c_msgs", 0444, d, &st->i2c_msgs); debugfs_create_ulong("i2c_bytes", 0444, d, &st->i2c_bytes); } static int s1d_probe(struct i2c_client *client) { struct device *dev = &client->dev; const struct i2c_adapter_quirks *quirks = client->adapter->quirks; struct drm_display_mode mode = { DRM_SIMPLE_MODE(width, height, 0, 0), }; struct s1d_device *s; size_t tx_cap = TX_CAP; unsigned int size; u8 *shadow, *tx; int ret; if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) return dev_err_probe(dev, -EOPNOTSUPP, "adapter lacks raw I2C\n"); /* a plain register read tells us whether the expander is there at all */ ret = i2c_smbus_read_byte_data(client, TCA_REG_CONFIG0); if (ret < 0) return dev_err_probe(dev, ret, "expander not responding\n"); s = devm_drm_dev_alloc(dev, &s1d_drm_driver, struct s1d_device, drm); if (IS_ERR(s)) return PTR_ERR(s); s->client = client; s->cfg = (struct s1d_config){ width, height, bpp, tcr }; s->mode = mode; s->dirty_y0 = height; s->retry_ms = RETRY_MIN_MS; INIT_DELAYED_WORK(&s->flush_work, s1d_flush_work); ret = drmm_mutex_init(&s->drm, &s->lock); if (ret) return ret; size = s1d_frame_size(&s->cfg); if (!size) return dev_err_probe(dev, -EINVAL, "invalid panel configuration\n"); if (quirks && quirks->max_write_len && quirks->max_write_len < tx_cap) tx_cap = quirks->max_write_len; shadow = devm_kzalloc(dev, size, GFP_KERNEL); s->work_frame = devm_kzalloc(dev, size, GFP_KERNEL); s->pending_frame = devm_kzalloc(dev, size, GFP_KERNEL); tx = devm_kmalloc(dev, tx_cap, GFP_KERNEL); /* must be DMA-safe */ if (!shadow || !s->work_frame || !s->pending_frame || !tx) return -ENOMEM; ret = s1d_panel_setup(&s->panel, &s->cfg, &s1d_panel_ops, s, shadow, tx, tx_cap); if (ret) return dev_err_probe(dev, ret, "panel setup failed\n"); ret = s1d_kms_init(s); if (ret) return dev_err_probe(dev, ret, "KMS init failed\n"); ret = s1d_backlight_init(s); if (ret) return dev_err_probe(dev, ret, "backlight registration failed\n"); i2c_set_clientdata(client, s); ret = drm_dev_register(&s->drm, 0); if (ret) return dev_err_probe(dev, ret, "DRM registration failed\n"); s1d_debugfs_init(s); drm_client_setup(&s->drm, NULL); return 0; } static void s1d_remove(struct i2c_client *client) { struct s1d_device *s = i2c_get_clientdata(client); drm_dev_unplug(&s->drm); cancel_delayed_work_sync(&s->flush_work); /* atomic_disable is a no-op once unplugged, so blank the panel here */ if (s->panel_ready) s1d_panel_display(&s->panel, false); drm_atomic_helper_shutdown(&s->drm); } static void s1d_shutdown(struct i2c_client *client) { struct s1d_device *s = i2c_get_clientdata(client); drm_atomic_helper_shutdown(&s->drm); cancel_delayed_work_sync(&s->flush_work); } static int s1d_suspend(struct device *dev) { struct s1d_device *s = dev_get_drvdata(dev); int ret; ret = drm_mode_config_helper_suspend(&s->drm); if (ret) return ret; /* the CRTC is off and the worker stopped; the controller may lose power */ s->panel_ready = false; return 0; } static int s1d_resume(struct device *dev) { struct s1d_device *s = dev_get_drvdata(dev); return drm_mode_config_helper_resume(&s->drm); } static DEFINE_SIMPLE_DEV_PM_OPS(s1d_pm_ops, s1d_suspend, s1d_resume); static const struct of_device_id s1d_of_match[] = { { .compatible = "k4be,s1d13700-tca9539" }, { } }; MODULE_DEVICE_TABLE(of, s1d_of_match); static const struct i2c_device_id s1d_id[] = { { "s1d13700-tca9539" }, { } }; MODULE_DEVICE_TABLE(i2c, s1d_id); static struct i2c_driver s1d_i2c_driver = { .driver = { .name = DRIVER_NAME, .of_match_table = s1d_of_match, .pm = pm_sleep_ptr(&s1d_pm_ops), }, .probe = s1d_probe, .remove = s1d_remove, .shutdown = s1d_shutdown, .id_table = s1d_id, }; module_i2c_driver(s1d_i2c_driver); MODULE_DESCRIPTION("DRM driver for an S1D13700 LCD behind a TCA9539 expander"); MODULE_LICENSE("GPL");