1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * MIPI DSI Bus
4 *
5 * Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
6 * Andrzej Hajda <a.hajda@samsung.com>
7 */
8
9 #ifndef __DRM_MIPI_DSI_H__
10 #define __DRM_MIPI_DSI_H__
11
12 #include <linux/device.h>
13
14 struct mipi_dsi_host;
15 struct mipi_dsi_device;
16 struct drm_dsc_picture_parameter_set;
17
18 /* request ACK from peripheral */
19 #define MIPI_DSI_MSG_REQ_ACK BIT(0)
20 /* use Low Power Mode to transmit message */
21 #define MIPI_DSI_MSG_USE_LPM BIT(1)
22 /* read mipi_dsi_msg.ctrl and unicast to only that ctrls */
23 #define MIPI_DSI_MSG_UNICAST BIT(2)
24 /* Stack all commands until lastcommand bit and trigger all in one go */
25 #define MIPI_DSI_MSG_LASTCOMMAND BIT(3)
26
27 #define DRM_MIPI_DSI_SIXTEEN 16
28 #define DRM_MIPI_DSI_EIGHTEEN 18
29 #define DRM_MIPI_DSI_TWENTYFOUR 24
30
31 /**
32 * struct mipi_dsi_msg - read/write DSI buffer
33 * @channel: virtual channel id
34 * @type: payload data type
35 * @flags: flags controlling this message transmission
36 * @ctrl: ctrl index to transmit on
37 * @wait_ms: duration in ms to wait after message transmission
38 * @tx_len: length of @tx_buf
39 * @tx_buf: data to be written
40 * @rx_len: length of @rx_buf
41 * @rx_buf: data to be read, or NULL
42 */
43 struct mipi_dsi_msg {
44 u8 channel;
45 u8 type;
46 u16 flags;
47 u32 ctrl;
48 u32 wait_ms;
49
50 size_t tx_len;
51 const void *tx_buf;
52
53 size_t rx_len;
54 void *rx_buf;
55 };
56
57 bool mipi_dsi_packet_format_is_short(u8 type);
58 bool mipi_dsi_packet_format_is_long(u8 type);
59
60 /**
61 * struct mipi_dsi_packet - represents a MIPI DSI packet in protocol format
62 * @size: size (in bytes) of the packet
63 * @header: the four bytes that make up the header (Data ID, Word Count or
64 * Packet Data, and ECC)
65 * @payload_length: number of bytes in the payload
66 * @payload: a pointer to a buffer containing the payload, if any
67 */
68 struct mipi_dsi_packet {
69 size_t size;
70 u8 header[4];
71 size_t payload_length;
72 const u8 *payload;
73 };
74
75 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet, const struct mipi_dsi_msg *msg);
76
77 /**
78 * struct mipi_dsi_host_ops - DSI bus operations
79 * @attach: attach DSI device to DSI host
80 * @detach: detach DSI device from DSI host
81 * @transfer: transmit a DSI packet
82 *
83 * DSI packets transmitted by .transfer() are passed in as mipi_dsi_msg
84 * structures. This structure contains information about the type of packet
85 * being transmitted as well as the transmit and receive buffers. When an
86 * error is encountered during transmission, this function will return a
87 * negative error code. On success it shall return the number of bytes
88 * transmitted for write packets or the number of bytes received for read
89 * packets.
90 *
91 * Note that typically DSI packet transmission is atomic, so the .transfer()
92 * function will seldomly return anything other than the number of bytes
93 * contained in the transmit buffer on success.
94 */
95 struct mipi_dsi_host_ops {
96 int (*attach)(struct mipi_dsi_host *host, struct mipi_dsi_device *dsi);
97 int (*detach)(struct mipi_dsi_host *host, struct mipi_dsi_device *dsi);
98 ssize_t (*transfer)(struct mipi_dsi_host *host, const struct mipi_dsi_msg *msg);
99 };
100
101 /**
102 * struct mipi_dsi_host - DSI host device
103 * @dev: driver model device node for this DSI host
104 * @ops: DSI host operations
105 * @list: list management
106 */
107 struct mipi_dsi_host {
108 struct device *dev;
109 const struct mipi_dsi_host_ops *ops;
110 struct list_head list;
111 };
112
113 int mipi_dsi_host_register(struct mipi_dsi_host *host);
114 void mipi_dsi_host_unregister(struct mipi_dsi_host *host);
115 struct mipi_dsi_host *of_find_mipi_dsi_host_by_node(struct device_node *node);
116
117 /* DSI mode flags */
118
119 /* video mode */
120 #define MIPI_DSI_MODE_VIDEO BIT(0)
121 /* video burst mode */
122 #define MIPI_DSI_MODE_VIDEO_BURST BIT(1)
123 /* video pulse mode */
124 #define MIPI_DSI_MODE_VIDEO_SYNC_PULSE BIT(2)
125 /* enable auto vertical count mode */
126 #define MIPI_DSI_MODE_VIDEO_AUTO_VERT BIT(3)
127 /* enable hsync-end packets in vsync-pulse and v-porch area */
128 #define MIPI_DSI_MODE_VIDEO_HSE BIT(4)
129 /* disable hfront-porch area */
130 #define MIPI_DSI_MODE_VIDEO_HFP BIT(5)
131 /* disable hback-porch area */
132 #define MIPI_DSI_MODE_VIDEO_HBP BIT(6)
133 /* disable hsync-active area */
134 #define MIPI_DSI_MODE_VIDEO_HSA BIT(7)
135 /* flush display FIFO on vsync pulse */
136 #define MIPI_DSI_MODE_VSYNC_FLUSH BIT(8)
137 /* disable EoT packets in HS mode */
138 #define MIPI_DSI_MODE_EOT_PACKET BIT(9)
139 /* device supports non-continuous clock behavior (DSI spec 5.6.1) */
140 #define MIPI_DSI_CLOCK_NON_CONTINUOUS BIT(10)
141 /* transmit data in low power */
142 #define MIPI_DSI_MODE_LPM BIT(11)
143 /* disable BLLP area */
144 #define MIPI_DSI_MODE_VIDEO_BLLP BIT(12)
145 /* disable EOF BLLP area */
146 #define MIPI_DSI_MODE_VIDEO_EOF_BLLP BIT(13)
147
148 enum mipi_dsi_pixel_format {
149 MIPI_DSI_FMT_RGB888,
150 MIPI_DSI_FMT_RGB666,
151 MIPI_DSI_FMT_RGB666_PACKED,
152 MIPI_DSI_FMT_RGB565,
153 };
154
155 #define DSI_DEV_NAME_SIZE 20
156
157 /**
158 * struct mipi_dsi_device_info - template for creating a mipi_dsi_device
159 * @type: DSI peripheral chip type
160 * @channel: DSI virtual channel assigned to peripheral
161 * @node: pointer to OF device node or NULL
162 *
163 * This is populated and passed to mipi_dsi_device_new to create a new
164 * DSI device
165 */
166 struct mipi_dsi_device_info {
167 char type[DSI_DEV_NAME_SIZE];
168 u32 channel;
169 struct device_node *node;
170 };
171
172 /**
173 * struct mipi_dsi_device - DSI peripheral device
174 * @host: DSI host for this peripheral
175 * @dev: driver model device node for this peripheral
176 * @name: DSI peripheral chip type
177 * @channel: virtual channel assigned to the peripheral
178 * @format: pixel format for video mode
179 * @lanes: number of active data lanes
180 * @mode_flags: DSI operation mode related flags
181 * @hs_rate: maximum lane frequency for high speed mode in hertz, this should
182 * be set to the real limits of the hardware, zero is only accepted for
183 * legacy drivers
184 * @lp_rate: maximum lane frequency for low power mode in hertz, this should
185 * be set to the real limits of the hardware, zero is only accepted for
186 * legacy drivers
187 */
188 struct mipi_dsi_device {
189 struct mipi_dsi_host *host;
190 struct device dev;
191
192 char name[DSI_DEV_NAME_SIZE];
193 unsigned int channel;
194 unsigned int lanes;
195 enum mipi_dsi_pixel_format format;
196 unsigned long mode_flags;
197 unsigned long hs_rate;
198 unsigned long lp_rate;
199 };
200
201 #define MIPI_DSI_MODULE_PREFIX "mipi-dsi:"
202
to_mipi_dsi_device(struct device * dev)203 static inline struct mipi_dsi_device *to_mipi_dsi_device(struct device *dev)
204 {
205 return container_of(dev, struct mipi_dsi_device, dev);
206 }
207
208 /**
209 * mipi_dsi_pixel_format_to_bpp - obtain the number of bits per pixel for any
210 * given pixel format defined by the MIPI DSI
211 * specification
212 * @fmt: MIPI DSI pixel format
213 *
214 * Returns: The number of bits per pixel of the given pixel format.
215 */
mipi_dsi_pixel_format_to_bpp(enum mipi_dsi_pixel_format fmt)216 static inline int mipi_dsi_pixel_format_to_bpp(enum mipi_dsi_pixel_format fmt)
217 {
218 switch (fmt) {
219 case MIPI_DSI_FMT_RGB888:
220 case MIPI_DSI_FMT_RGB666:
221 return DRM_MIPI_DSI_TWENTYFOUR;
222
223 case MIPI_DSI_FMT_RGB666_PACKED:
224 return DRM_MIPI_DSI_EIGHTEEN;
225
226 case MIPI_DSI_FMT_RGB565:
227 return DRM_MIPI_DSI_SIXTEEN;
228 }
229
230 return -EINVAL;
231 }
232
233 struct mipi_dsi_device *mipi_dsi_device_register_full(struct mipi_dsi_host *host,
234 const struct mipi_dsi_device_info *info);
235 void mipi_dsi_device_unregister(struct mipi_dsi_device *dsi);
236 struct mipi_dsi_device *of_find_mipi_dsi_device_by_node(struct device_node *np);
237 int mipi_dsi_attach(struct mipi_dsi_device *dsi);
238 int mipi_dsi_detach(struct mipi_dsi_device *dsi);
239 int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi);
240 int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi);
241 int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi, u16 value);
242 ssize_t mipi_dsi_compression_mode(struct mipi_dsi_device *dsi, bool enable);
243 ssize_t mipi_dsi_picture_parameter_set(struct mipi_dsi_device *dsi, const struct drm_dsc_picture_parameter_set *pps);
244
245 ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload, size_t size);
246 ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params, size_t num_params, void *data,
247 size_t size);
248
249 /**
250 * enum mipi_dsi_dcs_tear_mode - Tearing Effect Output Line mode
251 * @MIPI_DSI_DCS_TEAR_MODE_VBLANK: the TE output line consists of V-Blanking
252 * information only
253 * @MIPI_DSI_DCS_TEAR_MODE_VHBLANK : the TE output line consists of both
254 * V-Blanking and H-Blanking information
255 */
256 enum mipi_dsi_dcs_tear_mode {
257 MIPI_DSI_DCS_TEAR_MODE_VBLANK,
258 MIPI_DSI_DCS_TEAR_MODE_VHBLANK,
259 };
260
261 #define MIPI_DSI_DCS_POWER_MODE_DISPLAY (1 << 2)
262 #define MIPI_DSI_DCS_POWER_MODE_NORMAL (1 << 3)
263 #define MIPI_DSI_DCS_POWER_MODE_SLEEP (1 << 4)
264 #define MIPI_DSI_DCS_POWER_MODE_PARTIAL (1 << 5)
265 #define MIPI_DSI_DCS_POWER_MODE_IDLE (1 << 6)
266
267 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi, const void *data, size_t len);
268 ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd, const void *data, size_t len);
269 ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data, size_t len);
270 int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi);
271 int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi);
272 int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode);
273 int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format);
274 int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi);
275 int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi);
276 int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi);
277 int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi);
278 int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start, u16 end);
279 int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start, u16 end);
280 int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi);
281 int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi, enum mipi_dsi_dcs_tear_mode mode);
282 int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format);
283 int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline);
284 int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi, u16 brightness);
285 int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi, u16 *brightness);
286
287 /**
288 * struct mipi_dsi_driver - DSI driver
289 * @driver: device driver model driver
290 * @probe: callback for device binding
291 * @remove: callback for device unbinding
292 * @shutdown: called at shutdown time to quiesce the device
293 */
294 struct mipi_dsi_driver {
295 struct device_driver driver;
296 int (*probe)(struct mipi_dsi_device *dsi);
297 int (*remove)(struct mipi_dsi_device *dsi);
298 void (*shutdown)(struct mipi_dsi_device *dsi);
299 };
300
to_mipi_dsi_driver(struct device_driver * driver)301 static inline struct mipi_dsi_driver *to_mipi_dsi_driver(struct device_driver *driver)
302 {
303 return container_of(driver, struct mipi_dsi_driver, driver);
304 }
305
mipi_dsi_get_drvdata(const struct mipi_dsi_device * dsi)306 static inline void *mipi_dsi_get_drvdata(const struct mipi_dsi_device *dsi)
307 {
308 return dev_get_drvdata(&dsi->dev);
309 }
310
mipi_dsi_set_drvdata(struct mipi_dsi_device * dsi,void * data)311 static inline void mipi_dsi_set_drvdata(struct mipi_dsi_device *dsi, void *data)
312 {
313 dev_set_drvdata(&dsi->dev, data);
314 }
315
316 int mipi_dsi_driver_register_full(struct mipi_dsi_driver *driver, struct module *owner);
317 void mipi_dsi_driver_unregister(struct mipi_dsi_driver *driver);
318
319 #define mipi_dsi_driver_register(driver) mipi_dsi_driver_register_full(driver, THIS_MODULE)
320
321 #define module_mipi_dsi_driver(__mipi_dsi_driver) \
322 module_driver(__mipi_dsi_driver, mipi_dsi_driver_register, mipi_dsi_driver_unregister)
323
324 #endif /* __DRM_MIPI_DSI__ */
325