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