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