1 /* SPDX-License-Identifier: MIT */
2 
3 #ifndef __LINUX_GUD_INTERNAL_H
4 #define __LINUX_GUD_INTERNAL_H
5 
6 #include <linux/list.h>
7 #include <linux/mutex.h>
8 #include <linux/scatterlist.h>
9 #include <linux/usb.h>
10 #include <linux/workqueue.h>
11 #include <uapi/drm/drm_fourcc.h>
12 
13 #include <drm/drm_modes.h>
14 #include <drm/drm_simple_kms_helper.h>
15 
16 struct gud_device {
17 	struct drm_device drm;
18 	struct drm_simple_display_pipe pipe;
19 	struct device *dmadev;
20 	struct work_struct work;
21 	u32 flags;
22 	const struct drm_format_info *xrgb8888_emulation_format;
23 
24 	u16 *properties;
25 	unsigned int num_properties;
26 
27 	unsigned int bulk_pipe;
28 	void *bulk_buf;
29 	size_t bulk_len;
30 	struct sg_table bulk_sgt;
31 
32 	u8 compression;
33 	void *lz4_comp_mem;
34 	void *compress_buf;
35 
36 	u64 stats_length;
37 	u64 stats_actual_length;
38 	unsigned int stats_num_errors;
39 
40 	struct mutex ctrl_lock; /* Serialize get/set and status transfers */
41 
42 	struct mutex damage_lock; /* Protects the following members: */
43 	struct drm_framebuffer *fb;
44 	struct drm_rect damage;
45 	bool prev_flush_failed;
46 };
47 
to_gud_device(struct drm_device * drm)48 static inline struct gud_device *to_gud_device(struct drm_device *drm)
49 {
50 	return container_of(drm, struct gud_device, drm);
51 }
52 
gud_to_usb_device(struct gud_device * gdrm)53 static inline struct usb_device *gud_to_usb_device(struct gud_device *gdrm)
54 {
55 	return interface_to_usbdev(to_usb_interface(gdrm->drm.dev));
56 }
57 
58 int gud_usb_get(struct gud_device *gdrm, u8 request, u16 index, void *buf, size_t len);
59 int gud_usb_set(struct gud_device *gdrm, u8 request, u16 index, void *buf, size_t len);
60 int gud_usb_get_u8(struct gud_device *gdrm, u8 request, u16 index, u8 *val);
61 int gud_usb_set_u8(struct gud_device *gdrm, u8 request, u8 val);
62 
63 void gud_clear_damage(struct gud_device *gdrm);
64 void gud_flush_work(struct work_struct *work);
65 int gud_pipe_check(struct drm_simple_display_pipe *pipe,
66 		   struct drm_plane_state *new_plane_state,
67 		   struct drm_crtc_state *new_crtc_state);
68 void gud_pipe_update(struct drm_simple_display_pipe *pipe,
69 		     struct drm_plane_state *old_state);
70 int gud_connector_fill_properties(struct drm_connector_state *connector_state,
71 				  struct gud_property_req *properties);
72 int gud_get_connectors(struct gud_device *gdrm);
73 
74 /* Driver internal fourcc transfer formats */
75 #define GUD_DRM_FORMAT_R1		0x00000122
76 #define GUD_DRM_FORMAT_XRGB1111		0x03121722
77 
gud_from_fourcc(u32 fourcc)78 static inline u8 gud_from_fourcc(u32 fourcc)
79 {
80 	switch (fourcc) {
81 	case GUD_DRM_FORMAT_R1:
82 		return GUD_PIXEL_FORMAT_R1;
83 	case DRM_FORMAT_R8:
84 		return GUD_PIXEL_FORMAT_R8;
85 	case GUD_DRM_FORMAT_XRGB1111:
86 		return GUD_PIXEL_FORMAT_XRGB1111;
87 	case DRM_FORMAT_RGB332:
88 		return GUD_PIXEL_FORMAT_RGB332;
89 	case DRM_FORMAT_RGB565:
90 		return GUD_PIXEL_FORMAT_RGB565;
91 	case DRM_FORMAT_RGB888:
92 		return GUD_PIXEL_FORMAT_RGB888;
93 	case DRM_FORMAT_XRGB8888:
94 		return GUD_PIXEL_FORMAT_XRGB8888;
95 	case DRM_FORMAT_ARGB8888:
96 		return GUD_PIXEL_FORMAT_ARGB8888;
97 	}
98 
99 	return 0;
100 }
101 
gud_to_fourcc(u8 format)102 static inline u32 gud_to_fourcc(u8 format)
103 {
104 	switch (format) {
105 	case GUD_PIXEL_FORMAT_R1:
106 		return GUD_DRM_FORMAT_R1;
107 	case GUD_PIXEL_FORMAT_R8:
108 		return DRM_FORMAT_R8;
109 	case GUD_PIXEL_FORMAT_XRGB1111:
110 		return GUD_DRM_FORMAT_XRGB1111;
111 	case GUD_PIXEL_FORMAT_RGB332:
112 		return DRM_FORMAT_RGB332;
113 	case GUD_PIXEL_FORMAT_RGB565:
114 		return DRM_FORMAT_RGB565;
115 	case GUD_PIXEL_FORMAT_RGB888:
116 		return DRM_FORMAT_RGB888;
117 	case GUD_PIXEL_FORMAT_XRGB8888:
118 		return DRM_FORMAT_XRGB8888;
119 	case GUD_PIXEL_FORMAT_ARGB8888:
120 		return DRM_FORMAT_ARGB8888;
121 	}
122 
123 	return 0;
124 }
125 
gud_from_display_mode(struct gud_display_mode_req * dst,const struct drm_display_mode * src)126 static inline void gud_from_display_mode(struct gud_display_mode_req *dst,
127 					 const struct drm_display_mode *src)
128 {
129 	u32 flags = src->flags & GUD_DISPLAY_MODE_FLAG_USER_MASK;
130 
131 	if (src->type & DRM_MODE_TYPE_PREFERRED)
132 		flags |= GUD_DISPLAY_MODE_FLAG_PREFERRED;
133 
134 	dst->clock = cpu_to_le32(src->clock);
135 	dst->hdisplay = cpu_to_le16(src->hdisplay);
136 	dst->hsync_start = cpu_to_le16(src->hsync_start);
137 	dst->hsync_end = cpu_to_le16(src->hsync_end);
138 	dst->htotal = cpu_to_le16(src->htotal);
139 	dst->vdisplay = cpu_to_le16(src->vdisplay);
140 	dst->vsync_start = cpu_to_le16(src->vsync_start);
141 	dst->vsync_end = cpu_to_le16(src->vsync_end);
142 	dst->vtotal = cpu_to_le16(src->vtotal);
143 	dst->flags = cpu_to_le32(flags);
144 }
145 
gud_to_display_mode(struct drm_display_mode * dst,const struct gud_display_mode_req * src)146 static inline void gud_to_display_mode(struct drm_display_mode *dst,
147 				       const struct gud_display_mode_req *src)
148 {
149 	u32 flags = le32_to_cpu(src->flags);
150 
151 	memset(dst, 0, sizeof(*dst));
152 	dst->clock = le32_to_cpu(src->clock);
153 	dst->hdisplay = le16_to_cpu(src->hdisplay);
154 	dst->hsync_start = le16_to_cpu(src->hsync_start);
155 	dst->hsync_end = le16_to_cpu(src->hsync_end);
156 	dst->htotal = le16_to_cpu(src->htotal);
157 	dst->vdisplay = le16_to_cpu(src->vdisplay);
158 	dst->vsync_start = le16_to_cpu(src->vsync_start);
159 	dst->vsync_end = le16_to_cpu(src->vsync_end);
160 	dst->vtotal = le16_to_cpu(src->vtotal);
161 	dst->flags = flags & GUD_DISPLAY_MODE_FLAG_USER_MASK;
162 	dst->type = DRM_MODE_TYPE_DRIVER;
163 	if (flags & GUD_DISPLAY_MODE_FLAG_PREFERRED)
164 		dst->type |= DRM_MODE_TYPE_PREFERRED;
165 	drm_mode_set_name(dst);
166 }
167 
168 #endif
169