Loading...
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * MIPI Display Bus Interface (DBI) LCD controller support
4 *
5 * Copyright 2016 Noralf Trønnes
6 */
7
8#ifndef __LINUX_MIPI_DBI_H
9#define __LINUX_MIPI_DBI_H
10
11#include <linux/mutex.h>
12#include <drm/drm_device.h>
13#include <drm/drm_simple_kms_helper.h>
14
15struct drm_format_conv_state;
16struct drm_rect;
17struct gpio_desc;
18struct iosys_map;
19struct regulator;
20struct spi_device;
21
22/**
23 * struct mipi_dbi - MIPI DBI interface
24 */
25struct mipi_dbi {
26 /**
27 * @cmdlock: Command lock
28 */
29 struct mutex cmdlock;
30
31 /**
32 * @command: Bus specific callback executing commands.
33 */
34 int (*command)(struct mipi_dbi *dbi, u8 *cmd, u8 *param, size_t num);
35
36 /**
37 * @read_commands: Array of read commands terminated by a zero entry.
38 * Reading is disabled if this is NULL.
39 */
40 const u8 *read_commands;
41
42 /**
43 * @swap_bytes: Swap bytes in buffer before transfer
44 */
45 bool swap_bytes;
46
47 /**
48 * @reset: Optional reset gpio
49 */
50 struct gpio_desc *reset;
51
52 /* Type C specific */
53
54 /**
55 * @spi: SPI device
56 */
57 struct spi_device *spi;
58
59 /**
60 * @write_memory_bpw: Bits per word used on a MIPI_DCS_WRITE_MEMORY_START transfer
61 */
62 unsigned int write_memory_bpw;
63
64 /**
65 * @dc: Optional D/C gpio.
66 */
67 struct gpio_desc *dc;
68
69 /**
70 * @tx_buf9: Buffer used for Option 1 9-bit conversion
71 */
72 void *tx_buf9;
73
74 /**
75 * @tx_buf9_len: Size of tx_buf9.
76 */
77 size_t tx_buf9_len;
78};
79
80/**
81 * struct mipi_dbi_dev - MIPI DBI device
82 */
83struct mipi_dbi_dev {
84 /**
85 * @drm: DRM device
86 */
87 struct drm_device drm;
88
89 /**
90 * @pipe: Display pipe structure
91 */
92 struct drm_simple_display_pipe pipe;
93
94 /**
95 * @connector: Connector
96 */
97 struct drm_connector connector;
98
99 /**
100 * @mode: Fixed display mode
101 */
102 struct drm_display_mode mode;
103
104 /**
105 * @pixel_format: Native pixel format (DRM_FORMAT\_\*)
106 */
107 u32 pixel_format;
108
109 /**
110 * @tx_buf: Buffer used for transfer (copy clip rect area)
111 */
112 u16 *tx_buf;
113
114 /**
115 * @rotation: initial rotation in degrees Counter Clock Wise
116 */
117 unsigned int rotation;
118
119 /**
120 * @left_offset: Horizontal offset of the display relative to the
121 * controller's driver array
122 */
123 unsigned int left_offset;
124
125 /**
126 * @top_offset: Vertical offset of the display relative to the
127 * controller's driver array
128 */
129 unsigned int top_offset;
130
131 /**
132 * @backlight: backlight device (optional)
133 */
134 struct backlight_device *backlight;
135
136 /**
137 * @regulator: power regulator (Vdd) (optional)
138 */
139 struct regulator *regulator;
140
141 /**
142 * @io_regulator: I/O power regulator (Vddi) (optional)
143 */
144 struct regulator *io_regulator;
145
146 /**
147 * @dbi: MIPI DBI interface
148 */
149 struct mipi_dbi dbi;
150
151 /**
152 * @driver_private: Driver private data.
153 * Necessary for drivers with private data since devm_drm_dev_alloc()
154 * can't allocate structures that embed a structure which then again
155 * embeds drm_device.
156 */
157 void *driver_private;
158};
159
160static inline struct mipi_dbi_dev *drm_to_mipi_dbi_dev(struct drm_device *drm)
161{
162 return container_of(drm, struct mipi_dbi_dev, drm);
163}
164
165int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *dbi,
166 struct gpio_desc *dc);
167int mipi_dbi_dev_init_with_formats(struct mipi_dbi_dev *dbidev,
168 const struct drm_simple_display_pipe_funcs *funcs,
169 const uint32_t *formats, unsigned int format_count,
170 const struct drm_display_mode *mode,
171 unsigned int rotation, size_t tx_buf_size);
172int mipi_dbi_dev_init(struct mipi_dbi_dev *dbidev,
173 const struct drm_simple_display_pipe_funcs *funcs,
174 const struct drm_display_mode *mode, unsigned int rotation);
175enum drm_mode_status mipi_dbi_pipe_mode_valid(struct drm_simple_display_pipe *pipe,
176 const struct drm_display_mode *mode);
177void mipi_dbi_pipe_update(struct drm_simple_display_pipe *pipe,
178 struct drm_plane_state *old_state);
179void mipi_dbi_enable_flush(struct mipi_dbi_dev *dbidev,
180 struct drm_crtc_state *crtc_state,
181 struct drm_plane_state *plan_state);
182void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe);
183int mipi_dbi_pipe_begin_fb_access(struct drm_simple_display_pipe *pipe,
184 struct drm_plane_state *plane_state);
185void mipi_dbi_pipe_end_fb_access(struct drm_simple_display_pipe *pipe,
186 struct drm_plane_state *plane_state);
187void mipi_dbi_pipe_reset_plane(struct drm_simple_display_pipe *pipe);
188struct drm_plane_state *mipi_dbi_pipe_duplicate_plane_state(struct drm_simple_display_pipe *pipe);
189void mipi_dbi_pipe_destroy_plane_state(struct drm_simple_display_pipe *pipe,
190 struct drm_plane_state *plane_state);
191
192void mipi_dbi_hw_reset(struct mipi_dbi *dbi);
193bool mipi_dbi_display_is_on(struct mipi_dbi *dbi);
194int mipi_dbi_poweron_reset(struct mipi_dbi_dev *dbidev);
195int mipi_dbi_poweron_conditional_reset(struct mipi_dbi_dev *dbidev);
196
197u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len);
198int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz,
199 u8 bpw, const void *buf, size_t len);
200
201int mipi_dbi_command_read(struct mipi_dbi *dbi, u8 cmd, u8 *val);
202int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, u8 *data, size_t len);
203int mipi_dbi_command_stackbuf(struct mipi_dbi *dbi, u8 cmd, const u8 *data,
204 size_t len);
205int mipi_dbi_buf_copy(void *dst, struct iosys_map *src, struct drm_framebuffer *fb,
206 struct drm_rect *clip, bool swap,
207 struct drm_format_conv_state *fmtcnv_state);
208
209/**
210 * mipi_dbi_command - MIPI DCS command with optional parameter(s)
211 * @dbi: MIPI DBI structure
212 * @cmd: Command
213 * @seq: Optional parameter(s)
214 *
215 * Send MIPI DCS command to the controller. Use mipi_dbi_command_read() for
216 * get/read.
217 *
218 * Returns:
219 * Zero on success, negative error code on failure.
220 */
221#define mipi_dbi_command(dbi, cmd, seq...) \
222({ \
223 const u8 d[] = { seq }; \
224 struct device *dev = &(dbi)->spi->dev; \
225 int ret; \
226 ret = mipi_dbi_command_stackbuf(dbi, cmd, d, ARRAY_SIZE(d)); \
227 if (ret) \
228 dev_err_ratelimited(dev, "error %d when sending command %#02x\n", ret, cmd); \
229 ret; \
230})
231
232#ifdef CONFIG_DEBUG_FS
233void mipi_dbi_debugfs_init(struct drm_minor *minor);
234#else
235static inline void mipi_dbi_debugfs_init(struct drm_minor *minor) {}
236#endif
237
238/**
239 * DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS - Initializes struct drm_simple_display_pipe_funcs
240 * for MIPI-DBI devices
241 * @enable_: Enable-callback implementation
242 *
243 * This macro initializes struct drm_simple_display_pipe_funcs with default
244 * values for MIPI-DBI-based devices. The only callback that depends on the
245 * hardware is @enable, for which the driver has to provide an implementation.
246 * MIPI-based drivers are encouraged to use this macro for initialization.
247 */
248#define DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(enable_) \
249 .mode_valid = mipi_dbi_pipe_mode_valid, \
250 .enable = (enable_), \
251 .disable = mipi_dbi_pipe_disable, \
252 .update = mipi_dbi_pipe_update, \
253 .begin_fb_access = mipi_dbi_pipe_begin_fb_access, \
254 .end_fb_access = mipi_dbi_pipe_end_fb_access, \
255 .reset_plane = mipi_dbi_pipe_reset_plane, \
256 .duplicate_plane_state = mipi_dbi_pipe_duplicate_plane_state, \
257 .destroy_plane_state = mipi_dbi_pipe_destroy_plane_state
258
259#endif /* __LINUX_MIPI_DBI_H */