|
| 1 | +/* |
| 2 | + * Copyright 2025 NXP |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include <string.h> |
| 7 | +#include <zephyr/device.h> |
| 8 | +#include <zephyr/init.h> |
| 9 | +#include <zephyr/drivers/display.h> |
| 10 | +#include <zephyr/drivers/mipi_dsi.h> |
| 11 | +#include <zephyr/drivers/i2c.h> |
| 12 | +#include <zephyr/kernel.h> |
| 13 | +#include <zephyr/logging/log.h> |
| 14 | + |
| 15 | +LOG_MODULE_REGISTER(waveshare_dsi_lcd, CONFIG_DISPLAY_LOG_LEVEL); |
| 16 | + |
| 17 | +#if DT_HAS_COMPAT_STATUS_OKAY(waveshare_7inch_dsi_lcd_c) |
| 18 | +#define DT_DRV_COMPAT waveshare_7inch_dsi_lcd_c |
| 19 | +#endif |
| 20 | + |
| 21 | +struct waveshare_dsi_lcd_bus { |
| 22 | + struct i2c_dt_spec i2c; |
| 23 | +}; |
| 24 | + |
| 25 | +typedef bool (*waveshare_dsi_lcd_bus_ready_fn)(const struct device *dev); |
| 26 | +typedef int (*waveshare_dsi_lcd_write_bus_fn)(const struct device *dev, uint8_t reg, uint8_t val); |
| 27 | +typedef const char *(*waveshare_dsi_lcd_bus_name_fn)(const struct device *dev); |
| 28 | + |
| 29 | +struct waveshare_dsi_lcd_config { |
| 30 | + const struct device *mipi_dsi; |
| 31 | + uint8_t channel; |
| 32 | + uint8_t num_of_lanes; |
| 33 | + struct waveshare_dsi_lcd_bus bus; |
| 34 | + waveshare_dsi_lcd_bus_ready_fn bus_ready; |
| 35 | + waveshare_dsi_lcd_write_bus_fn write_bus; |
| 36 | + waveshare_dsi_lcd_bus_name_fn bus_name; |
| 37 | +}; |
| 38 | + |
| 39 | +struct waveshare_dsi_lcd_data { |
| 40 | + uint8_t pixel_format; |
| 41 | +}; |
| 42 | + |
| 43 | +static bool waveshare_dsi_lcd_bus_ready_i2c(const struct device *dev) |
| 44 | +{ |
| 45 | + const struct waveshare_dsi_lcd_config *config = dev->config; |
| 46 | + |
| 47 | + return i2c_is_ready_dt(&config->bus.i2c); |
| 48 | +} |
| 49 | + |
| 50 | +static int waveshare_dsi_lcd_write_bus_i2c(const struct device *dev, uint8_t reg, uint8_t val) |
| 51 | +{ |
| 52 | + const struct waveshare_dsi_lcd_config *config = dev->config; |
| 53 | + uint8_t buf[2]; |
| 54 | + |
| 55 | + buf[0] = reg; |
| 56 | + buf[1] = val; |
| 57 | + |
| 58 | + return i2c_write_dt(&config->bus.i2c, buf, 2); |
| 59 | +} |
| 60 | + |
| 61 | +static const char *waveshare_dsi_lcd_bus_name_i2c(const struct device *dev) |
| 62 | +{ |
| 63 | + const struct waveshare_dsi_lcd_config *config = dev->config; |
| 64 | + |
| 65 | + return config->bus.i2c.bus->name; |
| 66 | +} |
| 67 | + |
| 68 | +static int waveshare_dsi_lcd_enable(const struct device *dev, bool enable) |
| 69 | +{ |
| 70 | + const struct waveshare_dsi_lcd_config *config = dev->config; |
| 71 | + |
| 72 | + config->write_bus(dev, 0xad, enable ? 0x01 : 0x00); |
| 73 | + |
| 74 | + return 0; |
| 75 | +} |
| 76 | + |
| 77 | +static int waveshare_dsi_lcd_bl_update_status(const struct device *dev, uint8_t brightness) |
| 78 | +{ |
| 79 | + const struct waveshare_dsi_lcd_config *config = dev->config; |
| 80 | + |
| 81 | + config->write_bus(dev, 0xab, 0xff - brightness); |
| 82 | + config->write_bus(dev, 0xaa, 0x01); |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
| 86 | + |
| 87 | +static int waveshare_dsi_lcd_init(const struct device *dev) |
| 88 | +{ |
| 89 | + const struct waveshare_dsi_lcd_config *config = dev->config; |
| 90 | + struct waveshare_dsi_lcd_data *data = dev->data; |
| 91 | + int ret; |
| 92 | + struct mipi_dsi_device mdev = {0}; |
| 93 | + |
| 94 | + if (!config->bus_ready(dev)) { |
| 95 | + LOG_ERR("Bus device %s not ready!", config->bus_name(dev)); |
| 96 | + return -EINVAL; |
| 97 | + } |
| 98 | + |
| 99 | + config->write_bus(dev, 0xc0, 0x01); |
| 100 | + config->write_bus(dev, 0xc2, 0x01); |
| 101 | + config->write_bus(dev, 0xac, 0x01); |
| 102 | + |
| 103 | + waveshare_dsi_lcd_bl_update_status(dev, 0xff); |
| 104 | + waveshare_dsi_lcd_enable(dev, true); |
| 105 | + |
| 106 | + /* Attach to MIPI DSI host */ |
| 107 | + mdev.data_lanes = config->num_of_lanes; |
| 108 | + mdev.pixfmt = data->pixel_format; |
| 109 | + mdev.mode_flags = |
| 110 | + MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_MODE_VIDEO | MIPI_DSI_CLOCK_NON_CONTINUOUS; |
| 111 | + |
| 112 | + ret = mipi_dsi_attach(config->mipi_dsi, config->channel, &mdev); |
| 113 | + if (ret < 0) { |
| 114 | + LOG_ERR("Could not attach to MIPI-DSI host"); |
| 115 | + return ret; |
| 116 | + } |
| 117 | + |
| 118 | + LOG_INF("waveshare_dsi_lcd init succeeded"); |
| 119 | + |
| 120 | + return 0; |
| 121 | +} |
| 122 | + |
| 123 | +#define WAVESHARE_DSI_LCD_DEFINE(id) \ |
| 124 | + static const struct waveshare_dsi_lcd_config config_##id = { \ |
| 125 | + .mipi_dsi = DEVICE_DT_GET(DT_INST_PHANDLE(id, mipi_dsi)), \ |
| 126 | + .channel = DT_INST_REG_ADDR(id), \ |
| 127 | + .num_of_lanes = DT_INST_PROP_BY_IDX(id, data_lanes, 0), \ |
| 128 | + .bus = {.i2c = I2C_DT_SPEC_INST_GET(id)}, \ |
| 129 | + .bus_ready = waveshare_dsi_lcd_bus_ready_i2c, \ |
| 130 | + .write_bus = waveshare_dsi_lcd_write_bus_i2c, \ |
| 131 | + .bus_name = waveshare_dsi_lcd_bus_name_i2c, \ |
| 132 | + }; \ |
| 133 | + static struct waveshare_dsi_lcd_data data_##id = { \ |
| 134 | + .pixel_format = DT_INST_PROP(id, pixel_format), \ |
| 135 | + }; \ |
| 136 | + DEVICE_DT_INST_DEFINE(id, waveshare_dsi_lcd_init, NULL, &data_##id, &config_##id, \ |
| 137 | + POST_KERNEL, CONFIG_DISPLAY_INIT_PRIORITY, NULL); |
| 138 | + |
| 139 | +DT_INST_FOREACH_STATUS_OKAY(WAVESHARE_DSI_LCD_DEFINE) |
0 commit comments