xref: /openbmc/linux/include/drm/drm_mipi_dbi.h (revision f019190b)
1174102f4SNoralf Trønnes /* SPDX-License-Identifier: GPL-2.0-or-later */
2174102f4SNoralf Trønnes /*
3174102f4SNoralf Trønnes  * MIPI Display Bus Interface (DBI) LCD controller support
4174102f4SNoralf Trønnes  *
5174102f4SNoralf Trønnes  * Copyright 2016 Noralf Trønnes
6174102f4SNoralf Trønnes  */
7174102f4SNoralf Trønnes 
8174102f4SNoralf Trønnes #ifndef __LINUX_MIPI_DBI_H
9174102f4SNoralf Trønnes #define __LINUX_MIPI_DBI_H
10174102f4SNoralf Trønnes 
11174102f4SNoralf Trønnes #include <linux/mutex.h>
12174102f4SNoralf Trønnes #include <drm/drm_device.h>
13174102f4SNoralf Trønnes #include <drm/drm_simple_kms_helper.h>
14174102f4SNoralf Trønnes 
15174102f4SNoralf Trønnes struct drm_rect;
16174102f4SNoralf Trønnes struct spi_device;
17174102f4SNoralf Trønnes struct gpio_desc;
18174102f4SNoralf Trønnes struct regulator;
19174102f4SNoralf Trønnes 
20174102f4SNoralf Trønnes /**
21174102f4SNoralf Trønnes  * struct mipi_dbi - MIPI DBI interface
22174102f4SNoralf Trønnes  */
23174102f4SNoralf Trønnes struct mipi_dbi {
24174102f4SNoralf Trønnes 	/**
25174102f4SNoralf Trønnes 	 * @cmdlock: Command lock
26174102f4SNoralf Trønnes 	 */
27174102f4SNoralf Trønnes 	struct mutex cmdlock;
28174102f4SNoralf Trønnes 
29174102f4SNoralf Trønnes 	/**
30174102f4SNoralf Trønnes 	 * @command: Bus specific callback executing commands.
31174102f4SNoralf Trønnes 	 */
32174102f4SNoralf Trønnes 	int (*command)(struct mipi_dbi *dbi, u8 *cmd, u8 *param, size_t num);
33174102f4SNoralf Trønnes 
34174102f4SNoralf Trønnes 	/**
35174102f4SNoralf Trønnes 	 * @read_commands: Array of read commands terminated by a zero entry.
36174102f4SNoralf Trønnes 	 *                 Reading is disabled if this is NULL.
37174102f4SNoralf Trønnes 	 */
38174102f4SNoralf Trønnes 	const u8 *read_commands;
39174102f4SNoralf Trønnes 
40174102f4SNoralf Trønnes 	/**
41174102f4SNoralf Trønnes 	 * @swap_bytes: Swap bytes in buffer before transfer
42174102f4SNoralf Trønnes 	 */
43174102f4SNoralf Trønnes 	bool swap_bytes;
44174102f4SNoralf Trønnes 
45174102f4SNoralf Trønnes 	/**
46174102f4SNoralf Trønnes 	 * @reset: Optional reset gpio
47174102f4SNoralf Trønnes 	 */
48174102f4SNoralf Trønnes 	struct gpio_desc *reset;
49174102f4SNoralf Trønnes 
50174102f4SNoralf Trønnes 	/* Type C specific */
51174102f4SNoralf Trønnes 
52174102f4SNoralf Trønnes 	/**
53174102f4SNoralf Trønnes 	 * @spi: SPI device
54174102f4SNoralf Trønnes 	 */
55174102f4SNoralf Trønnes 	struct spi_device *spi;
56174102f4SNoralf Trønnes 
57174102f4SNoralf Trønnes 	/**
58174102f4SNoralf Trønnes 	 * @dc: Optional D/C gpio.
59174102f4SNoralf Trønnes 	 */
60174102f4SNoralf Trønnes 	struct gpio_desc *dc;
61174102f4SNoralf Trønnes 
62174102f4SNoralf Trønnes 	/**
63174102f4SNoralf Trønnes 	 * @tx_buf9: Buffer used for Option 1 9-bit conversion
64174102f4SNoralf Trønnes 	 */
65174102f4SNoralf Trønnes 	void *tx_buf9;
66174102f4SNoralf Trønnes 
67174102f4SNoralf Trønnes 	/**
68174102f4SNoralf Trønnes 	 * @tx_buf9_len: Size of tx_buf9.
69174102f4SNoralf Trønnes 	 */
70174102f4SNoralf Trønnes 	size_t tx_buf9_len;
71174102f4SNoralf Trønnes };
72174102f4SNoralf Trønnes 
73174102f4SNoralf Trønnes /**
74174102f4SNoralf Trønnes  * struct mipi_dbi_dev - MIPI DBI device
75174102f4SNoralf Trønnes  */
76174102f4SNoralf Trønnes struct mipi_dbi_dev {
77174102f4SNoralf Trønnes 	/**
78174102f4SNoralf Trønnes 	 * @drm: DRM device
79174102f4SNoralf Trønnes 	 */
80174102f4SNoralf Trønnes 	struct drm_device drm;
81174102f4SNoralf Trønnes 
82174102f4SNoralf Trønnes 	/**
83174102f4SNoralf Trønnes 	 * @pipe: Display pipe structure
84174102f4SNoralf Trønnes 	 */
85174102f4SNoralf Trønnes 	struct drm_simple_display_pipe pipe;
86174102f4SNoralf Trønnes 
87174102f4SNoralf Trønnes 	/**
88174102f4SNoralf Trønnes 	 * @connector: Connector
89174102f4SNoralf Trønnes 	 */
90174102f4SNoralf Trønnes 	struct drm_connector connector;
91174102f4SNoralf Trønnes 
92174102f4SNoralf Trønnes 	/**
93174102f4SNoralf Trønnes 	 * @mode: Fixed display mode
94174102f4SNoralf Trønnes 	 */
95174102f4SNoralf Trønnes 	struct drm_display_mode mode;
96174102f4SNoralf Trønnes 
97174102f4SNoralf Trønnes 	/**
98174102f4SNoralf Trønnes 	 * @enabled: Pipeline is enabled
99174102f4SNoralf Trønnes 	 */
100174102f4SNoralf Trønnes 	bool enabled;
101174102f4SNoralf Trønnes 
102174102f4SNoralf Trønnes 	/**
103174102f4SNoralf Trønnes 	 * @tx_buf: Buffer used for transfer (copy clip rect area)
104174102f4SNoralf Trønnes 	 */
105174102f4SNoralf Trønnes 	u16 *tx_buf;
106174102f4SNoralf Trønnes 
107174102f4SNoralf Trønnes 	/**
108174102f4SNoralf Trønnes 	 * @rotation: initial rotation in degrees Counter Clock Wise
109174102f4SNoralf Trønnes 	 */
110174102f4SNoralf Trønnes 	unsigned int rotation;
111174102f4SNoralf Trønnes 
112174102f4SNoralf Trønnes 	/**
113f41a8a69SGeert Uytterhoeven 	 * @left_offset: Horizontal offset of the display relative to the
114f41a8a69SGeert Uytterhoeven 	 *               controller's driver array
115f41a8a69SGeert Uytterhoeven 	 */
116f41a8a69SGeert Uytterhoeven 	unsigned int left_offset;
117f41a8a69SGeert Uytterhoeven 
118f41a8a69SGeert Uytterhoeven 	/**
119f41a8a69SGeert Uytterhoeven 	 * @top_offset: Vertical offset of the display relative to the
120f41a8a69SGeert Uytterhoeven 	 *              controller's driver array
121f41a8a69SGeert Uytterhoeven 	 */
122f41a8a69SGeert Uytterhoeven 	unsigned int top_offset;
123f41a8a69SGeert Uytterhoeven 
124f41a8a69SGeert Uytterhoeven 	/**
125174102f4SNoralf Trønnes 	 * @backlight: backlight device (optional)
126174102f4SNoralf Trønnes 	 */
127174102f4SNoralf Trønnes 	struct backlight_device *backlight;
128174102f4SNoralf Trønnes 
129174102f4SNoralf Trønnes 	/**
130174102f4SNoralf Trønnes 	 * @regulator: power regulator (optional)
131174102f4SNoralf Trønnes 	 */
132174102f4SNoralf Trønnes 	struct regulator *regulator;
133174102f4SNoralf Trønnes 
134174102f4SNoralf Trønnes 	/**
135174102f4SNoralf Trønnes 	 * @dbi: MIPI DBI interface
136174102f4SNoralf Trønnes 	 */
137174102f4SNoralf Trønnes 	struct mipi_dbi dbi;
138174102f4SNoralf Trønnes };
139174102f4SNoralf Trønnes 
140174102f4SNoralf Trønnes static inline struct mipi_dbi_dev *drm_to_mipi_dbi_dev(struct drm_device *drm)
141174102f4SNoralf Trønnes {
142174102f4SNoralf Trønnes 	return container_of(drm, struct mipi_dbi_dev, drm);
143174102f4SNoralf Trønnes }
144174102f4SNoralf Trønnes 
145174102f4SNoralf Trønnes int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *dbi,
146174102f4SNoralf Trønnes 		      struct gpio_desc *dc);
147174102f4SNoralf Trønnes int mipi_dbi_dev_init_with_formats(struct mipi_dbi_dev *dbidev,
148174102f4SNoralf Trønnes 				   const struct drm_simple_display_pipe_funcs *funcs,
149174102f4SNoralf Trønnes 				   const uint32_t *formats, unsigned int format_count,
150174102f4SNoralf Trønnes 				   const struct drm_display_mode *mode,
151174102f4SNoralf Trønnes 				   unsigned int rotation, size_t tx_buf_size);
152174102f4SNoralf Trønnes int mipi_dbi_dev_init(struct mipi_dbi_dev *dbidev,
153174102f4SNoralf Trønnes 		      const struct drm_simple_display_pipe_funcs *funcs,
154174102f4SNoralf Trønnes 		      const struct drm_display_mode *mode, unsigned int rotation);
155174102f4SNoralf Trønnes void mipi_dbi_release(struct drm_device *drm);
156174102f4SNoralf Trønnes void mipi_dbi_pipe_update(struct drm_simple_display_pipe *pipe,
157174102f4SNoralf Trønnes 			  struct drm_plane_state *old_state);
158174102f4SNoralf Trønnes void mipi_dbi_enable_flush(struct mipi_dbi_dev *dbidev,
159174102f4SNoralf Trønnes 			   struct drm_crtc_state *crtc_state,
160174102f4SNoralf Trønnes 			   struct drm_plane_state *plan_state);
161174102f4SNoralf Trønnes void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe);
162174102f4SNoralf Trønnes void mipi_dbi_hw_reset(struct mipi_dbi *dbi);
163174102f4SNoralf Trønnes bool mipi_dbi_display_is_on(struct mipi_dbi *dbi);
164174102f4SNoralf Trønnes int mipi_dbi_poweron_reset(struct mipi_dbi_dev *dbidev);
165174102f4SNoralf Trønnes int mipi_dbi_poweron_conditional_reset(struct mipi_dbi_dev *dbidev);
166174102f4SNoralf Trønnes 
167174102f4SNoralf Trønnes u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len);
168174102f4SNoralf Trønnes int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz,
169174102f4SNoralf Trønnes 			  u8 bpw, const void *buf, size_t len);
170174102f4SNoralf Trønnes 
171174102f4SNoralf Trønnes int mipi_dbi_command_read(struct mipi_dbi *dbi, u8 cmd, u8 *val);
172174102f4SNoralf Trønnes int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, u8 *data, size_t len);
173f019190bSGeert Uytterhoeven int mipi_dbi_command_stackbuf(struct mipi_dbi *dbi, u8 cmd, const u8 *data,
174f019190bSGeert Uytterhoeven 			      size_t len);
175174102f4SNoralf Trønnes int mipi_dbi_buf_copy(void *dst, struct drm_framebuffer *fb,
176174102f4SNoralf Trønnes 		      struct drm_rect *clip, bool swap);
177174102f4SNoralf Trønnes /**
178174102f4SNoralf Trønnes  * mipi_dbi_command - MIPI DCS command with optional parameter(s)
179174102f4SNoralf Trønnes  * @dbi: MIPI DBI structure
180174102f4SNoralf Trønnes  * @cmd: Command
181174102f4SNoralf Trønnes  * @seq...: Optional parameter(s)
182174102f4SNoralf Trønnes  *
183174102f4SNoralf Trønnes  * Send MIPI DCS command to the controller. Use mipi_dbi_command_read() for
184174102f4SNoralf Trønnes  * get/read.
185174102f4SNoralf Trønnes  *
186174102f4SNoralf Trønnes  * Returns:
187174102f4SNoralf Trønnes  * Zero on success, negative error code on failure.
188174102f4SNoralf Trønnes  */
189174102f4SNoralf Trønnes #define mipi_dbi_command(dbi, cmd, seq...) \
190174102f4SNoralf Trønnes ({ \
191f019190bSGeert Uytterhoeven 	const u8 d[] = { seq }; \
192174102f4SNoralf Trønnes 	mipi_dbi_command_stackbuf(dbi, cmd, d, ARRAY_SIZE(d)); \
193174102f4SNoralf Trønnes })
194174102f4SNoralf Trønnes 
195174102f4SNoralf Trønnes #ifdef CONFIG_DEBUG_FS
1967ce84471SWambui Karuga void mipi_dbi_debugfs_init(struct drm_minor *minor);
197174102f4SNoralf Trønnes #else
198174102f4SNoralf Trønnes #define mipi_dbi_debugfs_init		NULL
199174102f4SNoralf Trønnes #endif
200174102f4SNoralf Trønnes 
201174102f4SNoralf Trønnes #endif /* __LINUX_MIPI_DBI_H */
202