// SPDX-License-Identifier: GPL-2.0-or-later /* * MIPI Display Bus Interface (DBI) LCD controller support * * Copyright 2016 Noralf Trønnes */ #include <linux/backlight.h> #include <linux/debugfs.h> #include <linux/delay.h> #include <linux/gpio/consumer.h> #include <linux/module.h> #include <linux/regulator/consumer.h> #include <linux/spi/spi.h> #include <drm/drm_connector.h> #include <drm/drm_damage_helper.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> #include <drm/drm_format_helper.h> #include <drm/drm_fourcc.h> #include <drm/drm_framebuffer.h> #include <drm/drm_gem.h> #include <drm/drm_gem_atomic_helper.h> #include <drm/drm_gem_framebuffer_helper.h> #include <drm/drm_mipi_dbi.h> #include <drm/drm_modes.h> #include <drm/drm_probe_helper.h> #include <drm/drm_rect.h> #include <video/mipi_display.h> #define MIPI_DBI_MAX_SPI_READ_SPEED … #define DCS_POWER_MODE_DISPLAY … #define DCS_POWER_MODE_DISPLAY_NORMAL_MODE … #define DCS_POWER_MODE_SLEEP_MODE … #define DCS_POWER_MODE_PARTIAL_MODE … #define DCS_POWER_MODE_IDLE_MODE … #define DCS_POWER_MODE_RESERVED_MASK … /** * DOC: overview * * This library provides helpers for MIPI Display Bus Interface (DBI) * compatible display controllers. * * Many controllers for tiny lcd displays are MIPI compliant and can use this * library. If a controller uses registers 0x2A and 0x2B to set the area to * update and uses register 0x2C to write to frame memory, it is most likely * MIPI compliant. * * Only MIPI Type 1 displays are supported since a full frame memory is needed. * * There are 3 MIPI DBI implementation types: * * A. Motorola 6800 type parallel bus * * B. Intel 8080 type parallel bus * * C. SPI type with 3 options: * * 1. 9-bit with the Data/Command signal as the ninth bit * 2. Same as above except it's sent as 16 bits * 3. 8-bit with the Data/Command signal as a separate D/CX pin * * Currently mipi_dbi only supports Type C options 1 and 3 with * mipi_dbi_spi_init(). */ #define MIPI_DBI_DEBUG_COMMAND(cmd, data, len) … static const u8 mipi_dbi_dcs_read_commands[] = …; static bool mipi_dbi_command_is_read(struct mipi_dbi *dbi, u8 cmd) { … } /** * mipi_dbi_command_read - MIPI DCS read command * @dbi: MIPI DBI structure * @cmd: Command * @val: Value read * * Send MIPI DCS read command to the controller. * * Returns: * Zero on success, negative error code on failure. */ int mipi_dbi_command_read(struct mipi_dbi *dbi, u8 cmd, u8 *val) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_command_buf - MIPI DCS command with parameter(s) in an array * @dbi: MIPI DBI structure * @cmd: Command * @data: Parameter buffer * @len: Buffer length * * Returns: * Zero on success, negative error code on failure. */ int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, u8 *data, size_t len) { … } EXPORT_SYMBOL(…); /* This should only be used by mipi_dbi_command() */ int mipi_dbi_command_stackbuf(struct mipi_dbi *dbi, u8 cmd, const u8 *data, size_t len) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_buf_copy - Copy a framebuffer, transforming it if necessary * @dst: The destination buffer * @src: The source buffer * @fb: The source framebuffer * @clip: Clipping rectangle of the area to be copied * @swap: When true, swap MSB/LSB of 16-bit values * @fmtcnv_state: Format-conversion state * * Returns: * Zero on success, negative error code on failure. */ int mipi_dbi_buf_copy(void *dst, struct iosys_map *src, struct drm_framebuffer *fb, struct drm_rect *clip, bool swap, struct drm_format_conv_state *fmtcnv_state) { … } EXPORT_SYMBOL(…); static void mipi_dbi_set_window_address(struct mipi_dbi_dev *dbidev, unsigned int xs, unsigned int xe, unsigned int ys, unsigned int ye) { … } static void mipi_dbi_fb_dirty(struct iosys_map *src, struct drm_framebuffer *fb, struct drm_rect *rect, struct drm_format_conv_state *fmtcnv_state) { … } /** * mipi_dbi_pipe_mode_valid - MIPI DBI mode-valid helper * @pipe: Simple display pipe * @mode: The mode to test * * This function validates a given display mode against the MIPI DBI's hardware * display. Drivers can use this as their &drm_simple_display_pipe_funcs->mode_valid * callback. */ enum drm_mode_status mipi_dbi_pipe_mode_valid(struct drm_simple_display_pipe *pipe, const struct drm_display_mode *mode) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_pipe_update - Display pipe update helper * @pipe: Simple display pipe * @old_state: Old plane state * * This function handles framebuffer flushing and vblank events. Drivers can use * this as their &drm_simple_display_pipe_funcs->update callback. */ void mipi_dbi_pipe_update(struct drm_simple_display_pipe *pipe, struct drm_plane_state *old_state) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_enable_flush - MIPI DBI enable helper * @dbidev: MIPI DBI device structure * @crtc_state: CRTC state * @plane_state: Plane state * * Flushes the whole framebuffer and enables the backlight. Drivers can use this * in their &drm_simple_display_pipe_funcs->enable callback. * * Note: Drivers which don't use mipi_dbi_pipe_update() because they have custom * framebuffer flushing, can't use this function since they both use the same * flushing code. */ void mipi_dbi_enable_flush(struct mipi_dbi_dev *dbidev, struct drm_crtc_state *crtc_state, struct drm_plane_state *plane_state) { … } EXPORT_SYMBOL(…); static void mipi_dbi_blank(struct mipi_dbi_dev *dbidev) { … } /** * mipi_dbi_pipe_disable - MIPI DBI pipe disable helper * @pipe: Display pipe * * This function disables backlight if present, if not the display memory is * blanked. The regulator is disabled if in use. Drivers can use this as their * &drm_simple_display_pipe_funcs->disable callback. */ void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_pipe_begin_fb_access - MIPI DBI pipe begin-access helper * @pipe: Display pipe * @plane_state: Plane state * * This function implements struct &drm_simple_display_funcs.begin_fb_access. * * See drm_gem_begin_shadow_fb_access() for details and mipi_dbi_pipe_cleanup_fb() * for cleanup. * * Returns: * 0 on success, or a negative errno code otherwise. */ int mipi_dbi_pipe_begin_fb_access(struct drm_simple_display_pipe *pipe, struct drm_plane_state *plane_state) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_pipe_end_fb_access - MIPI DBI pipe end-access helper * @pipe: Display pipe * @plane_state: Plane state * * This function implements struct &drm_simple_display_funcs.end_fb_access. * * See mipi_dbi_pipe_begin_fb_access(). */ void mipi_dbi_pipe_end_fb_access(struct drm_simple_display_pipe *pipe, struct drm_plane_state *plane_state) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_pipe_reset_plane - MIPI DBI plane-reset helper * @pipe: Display pipe * * This function implements struct &drm_simple_display_funcs.reset_plane * for MIPI DBI planes. */ void mipi_dbi_pipe_reset_plane(struct drm_simple_display_pipe *pipe) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_pipe_duplicate_plane_state - duplicates MIPI DBI plane state * @pipe: Display pipe * * This function implements struct &drm_simple_display_funcs.duplicate_plane_state * for MIPI DBI planes. * * See drm_gem_duplicate_shadow_plane_state() for additional details. * * Returns: * A pointer to a new plane state on success, or NULL otherwise. */ struct drm_plane_state *mipi_dbi_pipe_duplicate_plane_state(struct drm_simple_display_pipe *pipe) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_pipe_destroy_plane_state - cleans up MIPI DBI plane state * @pipe: Display pipe * @plane_state: Plane state * * This function implements struct drm_simple_display_funcs.destroy_plane_state * for MIPI DBI planes. * * See drm_gem_destroy_shadow_plane_state() for additional details. */ void mipi_dbi_pipe_destroy_plane_state(struct drm_simple_display_pipe *pipe, struct drm_plane_state *plane_state) { … } EXPORT_SYMBOL(…); static int mipi_dbi_connector_get_modes(struct drm_connector *connector) { … } static const struct drm_connector_helper_funcs mipi_dbi_connector_hfuncs = …; static const struct drm_connector_funcs mipi_dbi_connector_funcs = …; static int mipi_dbi_rotate_mode(struct drm_display_mode *mode, unsigned int rotation) { … } static const struct drm_mode_config_funcs mipi_dbi_mode_config_funcs = …; static const uint32_t mipi_dbi_formats[] = …; /** * mipi_dbi_dev_init_with_formats - MIPI DBI device initialization with custom formats * @dbidev: MIPI DBI device structure to initialize * @funcs: Display pipe functions * @formats: Array of supported formats (DRM_FORMAT\_\*). * @format_count: Number of elements in @formats * @mode: Display mode * @rotation: Initial rotation in degrees Counter Clock Wise * @tx_buf_size: Allocate a transmit buffer of this size. * * This function sets up a &drm_simple_display_pipe with a &drm_connector that * has one fixed &drm_display_mode which is rotated according to @rotation. * This mode is used to set the mode config min/max width/height properties. * * Use mipi_dbi_dev_init() if you want native RGB565 and emulated XRGB8888 format. * * Note: * Some of the helper functions expects RGB565 to be the default format and the * transmit buffer sized to fit that. * * Returns: * Zero on success, negative error code on failure. */ int mipi_dbi_dev_init_with_formats(struct mipi_dbi_dev *dbidev, const struct drm_simple_display_pipe_funcs *funcs, const uint32_t *formats, unsigned int format_count, const struct drm_display_mode *mode, unsigned int rotation, size_t tx_buf_size) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_dev_init - MIPI DBI device initialization * @dbidev: MIPI DBI device structure to initialize * @funcs: Display pipe functions * @mode: Display mode * @rotation: Initial rotation in degrees Counter Clock Wise * * This function sets up a &drm_simple_display_pipe with a &drm_connector that * has one fixed &drm_display_mode which is rotated according to @rotation. * This mode is used to set the mode config min/max width/height properties. * Additionally &mipi_dbi.tx_buf is allocated. * * Supported formats: Native RGB565 and emulated XRGB8888. * * Returns: * Zero on success, negative error code on failure. */ int mipi_dbi_dev_init(struct mipi_dbi_dev *dbidev, const struct drm_simple_display_pipe_funcs *funcs, const struct drm_display_mode *mode, unsigned int rotation) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_hw_reset - Hardware reset of controller * @dbi: MIPI DBI structure * * Reset controller if the &mipi_dbi->reset gpio is set. */ void mipi_dbi_hw_reset(struct mipi_dbi *dbi) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_display_is_on - Check if display is on * @dbi: MIPI DBI structure * * This function checks the Power Mode register (if readable) to see if * display output is turned on. This can be used to see if the bootloader * has already turned on the display avoiding flicker when the pipeline is * enabled. * * Returns: * true if the display can be verified to be on, false otherwise. */ bool mipi_dbi_display_is_on(struct mipi_dbi *dbi) { … } EXPORT_SYMBOL(…); static int mipi_dbi_poweron_reset_conditional(struct mipi_dbi_dev *dbidev, bool cond) { … } /** * mipi_dbi_poweron_reset - MIPI DBI poweron and reset * @dbidev: MIPI DBI device structure * * This function enables the regulator if used and does a hardware and software * reset. * * Returns: * Zero on success, or a negative error code. */ int mipi_dbi_poweron_reset(struct mipi_dbi_dev *dbidev) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_poweron_conditional_reset - MIPI DBI poweron and conditional reset * @dbidev: MIPI DBI device structure * * This function enables the regulator if used and if the display is off, it * does a hardware and software reset. If mipi_dbi_display_is_on() determines * that the display is on, no reset is performed. * * Returns: * Zero if the controller was reset, 1 if the display was already on, or a * negative error code. */ int mipi_dbi_poweron_conditional_reset(struct mipi_dbi_dev *dbidev) { … } EXPORT_SYMBOL(…); #if IS_ENABLED(CONFIG_SPI) /** * mipi_dbi_spi_cmd_max_speed - get the maximum SPI bus speed * @spi: SPI device * @len: The transfer buffer length. * * Many controllers have a max speed of 10MHz, but can be pushed way beyond * that. Increase reliability by running pixel data at max speed and the rest * at 10MHz, preventing transfer glitches from messing up the init settings. */ u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len) { … } EXPORT_SYMBOL(…); /* * MIPI DBI Type C Option 1 * * If the SPI controller doesn't have 9 bits per word support, * use blocks of 9 bytes to send 8x 9-bit words using a 8-bit SPI transfer. * Pad partial blocks with MIPI_DCS_NOP (zero). * This is how the D/C bit (x) is added: * x7654321 * 0x765432 * 10x76543 * 210x7654 * 3210x765 * 43210x76 * 543210x7 * 6543210x * 76543210 */ static int mipi_dbi_spi1e_transfer(struct mipi_dbi *dbi, int dc, const void *buf, size_t len, unsigned int bpw) { … } static int mipi_dbi_spi1_transfer(struct mipi_dbi *dbi, int dc, const void *buf, size_t len, unsigned int bpw) { … } static int mipi_dbi_typec1_command_read(struct mipi_dbi *dbi, u8 *cmd, u8 *data, size_t len) { … } static int mipi_dbi_typec1_command(struct mipi_dbi *dbi, u8 *cmd, u8 *parameters, size_t num) { … } /* MIPI DBI Type C Option 3 */ static int mipi_dbi_typec3_command_read(struct mipi_dbi *dbi, u8 *cmd, u8 *data, size_t len) { … } static int mipi_dbi_typec3_command(struct mipi_dbi *dbi, u8 *cmd, u8 *par, size_t num) { … } /** * mipi_dbi_spi_init - Initialize MIPI DBI SPI interface * @spi: SPI device * @dbi: MIPI DBI structure to initialize * @dc: D/C gpio (optional) * * This function sets &mipi_dbi->command, enables &mipi_dbi->read_commands for the * usual read commands. It should be followed by a call to mipi_dbi_dev_init() or * a driver-specific init. * * If @dc is set, a Type C Option 3 interface is assumed, if not * Type C Option 1. * * If the command is %MIPI_DCS_WRITE_MEMORY_START and the pixel format is RGB565, endianness has * to be taken into account. The MIPI DBI serial interface is big endian and framebuffers are * assumed stored in memory as little endian (%DRM_FORMAT_BIG_ENDIAN is not supported). * * This is how endianness is handled: * * Option 1 (D/C as a bit): The buffer is sent on the wire byte by byte so the 16-bit buffer is * byteswapped before transfer. * * Option 3 (D/C as a gpio): If the SPI controller supports 16 bits per word the buffer can be * sent as-is. If not the caller is responsible for swapping the bytes * before calling mipi_dbi_command_buf() and the buffer is sent 8 bpw. * * This handling is optimised for %DRM_FORMAT_RGB565 framebuffers. * * If the interface is Option 1 and the SPI controller doesn't support 9 bits per word, * the buffer is sent as 9x 8-bit words, padded with MIPI DCS no-op commands if necessary. * * Returns: * Zero on success, negative error code on failure. */ int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *dbi, struct gpio_desc *dc) { … } EXPORT_SYMBOL(…); /** * mipi_dbi_spi_transfer - SPI transfer helper * @spi: SPI device * @speed_hz: Override speed (optional) * @bpw: Bits per word * @buf: Buffer to transfer * @len: Buffer length * * This SPI transfer helper breaks up the transfer of @buf into chunks which * the SPI controller driver can handle. The SPI bus must be locked when * calling this. * * Returns: * Zero on success, negative error code on failure. */ int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz, u8 bpw, const void *buf, size_t len) { … } EXPORT_SYMBOL(…); #endif /* CONFIG_SPI */ #ifdef CONFIG_DEBUG_FS static ssize_t mipi_dbi_debugfs_command_write(struct file *file, const char __user *ubuf, size_t count, loff_t *ppos) { … } static int mipi_dbi_debugfs_command_show(struct seq_file *m, void *unused) { … } static int mipi_dbi_debugfs_command_open(struct inode *inode, struct file *file) { … } static const struct file_operations mipi_dbi_debugfs_command_fops = …; /** * mipi_dbi_debugfs_init - Create debugfs entries * @minor: DRM minor * * This function creates a 'command' debugfs file for sending commands to the * controller or getting the read command values. * Drivers can use this as their &drm_driver->debugfs_init callback. * */ void mipi_dbi_debugfs_init(struct drm_minor *minor) { … } EXPORT_SYMBOL(…); #endif MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …;