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