140e1a70bSNoralf Trønnes /* SPDX-License-Identifier: MIT */
240e1a70bSNoralf Trønnes
340e1a70bSNoralf Trønnes #ifndef __LINUX_GUD_INTERNAL_H
440e1a70bSNoralf Trønnes #define __LINUX_GUD_INTERNAL_H
540e1a70bSNoralf Trønnes
640e1a70bSNoralf Trønnes #include <linux/list.h>
740e1a70bSNoralf Trønnes #include <linux/mutex.h>
82eecd93bSNoralf Trønnes #include <linux/scatterlist.h>
940e1a70bSNoralf Trønnes #include <linux/usb.h>
1040e1a70bSNoralf Trønnes #include <linux/workqueue.h>
1140e1a70bSNoralf Trønnes #include <uapi/drm/drm_fourcc.h>
1240e1a70bSNoralf Trønnes
1340e1a70bSNoralf Trønnes #include <drm/drm_modes.h>
1440e1a70bSNoralf Trønnes #include <drm/drm_simple_kms_helper.h>
1540e1a70bSNoralf Trønnes
1640e1a70bSNoralf Trønnes struct gud_device {
1740e1a70bSNoralf Trønnes struct drm_device drm;
1840e1a70bSNoralf Trønnes struct drm_simple_display_pipe pipe;
1940e1a70bSNoralf Trønnes struct device *dmadev;
2040e1a70bSNoralf Trønnes struct work_struct work;
2140e1a70bSNoralf Trønnes u32 flags;
2240e1a70bSNoralf Trønnes const struct drm_format_info *xrgb8888_emulation_format;
2340e1a70bSNoralf Trønnes
2440e1a70bSNoralf Trønnes u16 *properties;
2540e1a70bSNoralf Trønnes unsigned int num_properties;
2640e1a70bSNoralf Trønnes
2740e1a70bSNoralf Trønnes unsigned int bulk_pipe;
2840e1a70bSNoralf Trønnes void *bulk_buf;
2940e1a70bSNoralf Trønnes size_t bulk_len;
302eecd93bSNoralf Trønnes struct sg_table bulk_sgt;
3140e1a70bSNoralf Trønnes
3240e1a70bSNoralf Trønnes u8 compression;
3340e1a70bSNoralf Trønnes void *lz4_comp_mem;
3440e1a70bSNoralf Trønnes void *compress_buf;
3540e1a70bSNoralf Trønnes
3640e1a70bSNoralf Trønnes u64 stats_length;
3740e1a70bSNoralf Trønnes u64 stats_actual_length;
3840e1a70bSNoralf Trønnes unsigned int stats_num_errors;
3940e1a70bSNoralf Trønnes
4040e1a70bSNoralf Trønnes struct mutex ctrl_lock; /* Serialize get/set and status transfers */
4140e1a70bSNoralf Trønnes
4240e1a70bSNoralf Trønnes struct mutex damage_lock; /* Protects the following members: */
4340e1a70bSNoralf Trønnes struct drm_framebuffer *fb;
4440e1a70bSNoralf Trønnes struct drm_rect damage;
4540e1a70bSNoralf Trønnes bool prev_flush_failed;
46*c17d0486SNoralf Trønnes void *shadow_buf;
4740e1a70bSNoralf Trønnes };
4840e1a70bSNoralf Trønnes
to_gud_device(struct drm_device * drm)4940e1a70bSNoralf Trønnes static inline struct gud_device *to_gud_device(struct drm_device *drm)
5040e1a70bSNoralf Trønnes {
5140e1a70bSNoralf Trønnes return container_of(drm, struct gud_device, drm);
5240e1a70bSNoralf Trønnes }
5340e1a70bSNoralf Trønnes
gud_to_usb_device(struct gud_device * gdrm)5440e1a70bSNoralf Trønnes static inline struct usb_device *gud_to_usb_device(struct gud_device *gdrm)
5540e1a70bSNoralf Trønnes {
5640e1a70bSNoralf Trønnes return interface_to_usbdev(to_usb_interface(gdrm->drm.dev));
5740e1a70bSNoralf Trønnes }
5840e1a70bSNoralf Trønnes
5940e1a70bSNoralf Trønnes int gud_usb_get(struct gud_device *gdrm, u8 request, u16 index, void *buf, size_t len);
6040e1a70bSNoralf Trønnes int gud_usb_set(struct gud_device *gdrm, u8 request, u16 index, void *buf, size_t len);
6140e1a70bSNoralf Trønnes int gud_usb_get_u8(struct gud_device *gdrm, u8 request, u16 index, u8 *val);
6240e1a70bSNoralf Trønnes int gud_usb_set_u8(struct gud_device *gdrm, u8 request, u8 val);
6340e1a70bSNoralf Trønnes
6440e1a70bSNoralf Trønnes void gud_clear_damage(struct gud_device *gdrm);
6540e1a70bSNoralf Trønnes void gud_flush_work(struct work_struct *work);
6640e1a70bSNoralf Trønnes int gud_pipe_check(struct drm_simple_display_pipe *pipe,
6740e1a70bSNoralf Trønnes struct drm_plane_state *new_plane_state,
6840e1a70bSNoralf Trønnes struct drm_crtc_state *new_crtc_state);
6940e1a70bSNoralf Trønnes void gud_pipe_update(struct drm_simple_display_pipe *pipe,
7040e1a70bSNoralf Trønnes struct drm_plane_state *old_state);
7140e1a70bSNoralf Trønnes int gud_connector_fill_properties(struct drm_connector_state *connector_state,
7240e1a70bSNoralf Trønnes struct gud_property_req *properties);
7340e1a70bSNoralf Trønnes int gud_get_connectors(struct gud_device *gdrm);
7440e1a70bSNoralf Trønnes
7540e1a70bSNoralf Trønnes /* Driver internal fourcc transfer formats */
7640e1a70bSNoralf Trønnes #define GUD_DRM_FORMAT_R1 0x00000122
7740e1a70bSNoralf Trønnes #define GUD_DRM_FORMAT_XRGB1111 0x03121722
7840e1a70bSNoralf Trønnes
gud_from_fourcc(u32 fourcc)7940e1a70bSNoralf Trønnes static inline u8 gud_from_fourcc(u32 fourcc)
8040e1a70bSNoralf Trønnes {
8140e1a70bSNoralf Trønnes switch (fourcc) {
8240e1a70bSNoralf Trønnes case GUD_DRM_FORMAT_R1:
8340e1a70bSNoralf Trønnes return GUD_PIXEL_FORMAT_R1;
844cabfedcSNoralf Trønnes case DRM_FORMAT_R8:
854cabfedcSNoralf Trønnes return GUD_PIXEL_FORMAT_R8;
8640e1a70bSNoralf Trønnes case GUD_DRM_FORMAT_XRGB1111:
8740e1a70bSNoralf Trønnes return GUD_PIXEL_FORMAT_XRGB1111;
881f25d005SNoralf Trønnes case DRM_FORMAT_RGB332:
891f25d005SNoralf Trønnes return GUD_PIXEL_FORMAT_RGB332;
9040e1a70bSNoralf Trønnes case DRM_FORMAT_RGB565:
9140e1a70bSNoralf Trønnes return GUD_PIXEL_FORMAT_RGB565;
9283d7b6d5SNoralf Trønnes case DRM_FORMAT_RGB888:
9383d7b6d5SNoralf Trønnes return GUD_PIXEL_FORMAT_RGB888;
9440e1a70bSNoralf Trønnes case DRM_FORMAT_XRGB8888:
9540e1a70bSNoralf Trønnes return GUD_PIXEL_FORMAT_XRGB8888;
9640e1a70bSNoralf Trønnes case DRM_FORMAT_ARGB8888:
9740e1a70bSNoralf Trønnes return GUD_PIXEL_FORMAT_ARGB8888;
986dd7efc4SBernard Zhao }
9940e1a70bSNoralf Trønnes
10040e1a70bSNoralf Trønnes return 0;
10140e1a70bSNoralf Trønnes }
10240e1a70bSNoralf Trønnes
gud_to_fourcc(u8 format)10340e1a70bSNoralf Trønnes static inline u32 gud_to_fourcc(u8 format)
10440e1a70bSNoralf Trønnes {
10540e1a70bSNoralf Trønnes switch (format) {
10640e1a70bSNoralf Trønnes case GUD_PIXEL_FORMAT_R1:
10740e1a70bSNoralf Trønnes return GUD_DRM_FORMAT_R1;
1084cabfedcSNoralf Trønnes case GUD_PIXEL_FORMAT_R8:
1094cabfedcSNoralf Trønnes return DRM_FORMAT_R8;
11040e1a70bSNoralf Trønnes case GUD_PIXEL_FORMAT_XRGB1111:
11140e1a70bSNoralf Trønnes return GUD_DRM_FORMAT_XRGB1111;
1121f25d005SNoralf Trønnes case GUD_PIXEL_FORMAT_RGB332:
1131f25d005SNoralf Trønnes return DRM_FORMAT_RGB332;
11440e1a70bSNoralf Trønnes case GUD_PIXEL_FORMAT_RGB565:
11540e1a70bSNoralf Trønnes return DRM_FORMAT_RGB565;
11683d7b6d5SNoralf Trønnes case GUD_PIXEL_FORMAT_RGB888:
11783d7b6d5SNoralf Trønnes return DRM_FORMAT_RGB888;
11840e1a70bSNoralf Trønnes case GUD_PIXEL_FORMAT_XRGB8888:
11940e1a70bSNoralf Trønnes return DRM_FORMAT_XRGB8888;
12040e1a70bSNoralf Trønnes case GUD_PIXEL_FORMAT_ARGB8888:
12140e1a70bSNoralf Trønnes return DRM_FORMAT_ARGB8888;
1226dd7efc4SBernard Zhao }
12340e1a70bSNoralf Trønnes
12440e1a70bSNoralf Trønnes return 0;
12540e1a70bSNoralf Trønnes }
12640e1a70bSNoralf Trønnes
gud_from_display_mode(struct gud_display_mode_req * dst,const struct drm_display_mode * src)12740e1a70bSNoralf Trønnes static inline void gud_from_display_mode(struct gud_display_mode_req *dst,
12840e1a70bSNoralf Trønnes const struct drm_display_mode *src)
12940e1a70bSNoralf Trønnes {
13040e1a70bSNoralf Trønnes u32 flags = src->flags & GUD_DISPLAY_MODE_FLAG_USER_MASK;
13140e1a70bSNoralf Trønnes
13240e1a70bSNoralf Trønnes if (src->type & DRM_MODE_TYPE_PREFERRED)
13340e1a70bSNoralf Trønnes flags |= GUD_DISPLAY_MODE_FLAG_PREFERRED;
13440e1a70bSNoralf Trønnes
13540e1a70bSNoralf Trønnes dst->clock = cpu_to_le32(src->clock);
13640e1a70bSNoralf Trønnes dst->hdisplay = cpu_to_le16(src->hdisplay);
13740e1a70bSNoralf Trønnes dst->hsync_start = cpu_to_le16(src->hsync_start);
13840e1a70bSNoralf Trønnes dst->hsync_end = cpu_to_le16(src->hsync_end);
13940e1a70bSNoralf Trønnes dst->htotal = cpu_to_le16(src->htotal);
14040e1a70bSNoralf Trønnes dst->vdisplay = cpu_to_le16(src->vdisplay);
14140e1a70bSNoralf Trønnes dst->vsync_start = cpu_to_le16(src->vsync_start);
14240e1a70bSNoralf Trønnes dst->vsync_end = cpu_to_le16(src->vsync_end);
14340e1a70bSNoralf Trønnes dst->vtotal = cpu_to_le16(src->vtotal);
14440e1a70bSNoralf Trønnes dst->flags = cpu_to_le32(flags);
14540e1a70bSNoralf Trønnes }
14640e1a70bSNoralf Trønnes
gud_to_display_mode(struct drm_display_mode * dst,const struct gud_display_mode_req * src)14740e1a70bSNoralf Trønnes static inline void gud_to_display_mode(struct drm_display_mode *dst,
14840e1a70bSNoralf Trønnes const struct gud_display_mode_req *src)
14940e1a70bSNoralf Trønnes {
15040e1a70bSNoralf Trønnes u32 flags = le32_to_cpu(src->flags);
15140e1a70bSNoralf Trønnes
15240e1a70bSNoralf Trønnes memset(dst, 0, sizeof(*dst));
15340e1a70bSNoralf Trønnes dst->clock = le32_to_cpu(src->clock);
15440e1a70bSNoralf Trønnes dst->hdisplay = le16_to_cpu(src->hdisplay);
15540e1a70bSNoralf Trønnes dst->hsync_start = le16_to_cpu(src->hsync_start);
15640e1a70bSNoralf Trønnes dst->hsync_end = le16_to_cpu(src->hsync_end);
15740e1a70bSNoralf Trønnes dst->htotal = le16_to_cpu(src->htotal);
15840e1a70bSNoralf Trønnes dst->vdisplay = le16_to_cpu(src->vdisplay);
15940e1a70bSNoralf Trønnes dst->vsync_start = le16_to_cpu(src->vsync_start);
16040e1a70bSNoralf Trønnes dst->vsync_end = le16_to_cpu(src->vsync_end);
16140e1a70bSNoralf Trønnes dst->vtotal = le16_to_cpu(src->vtotal);
16240e1a70bSNoralf Trønnes dst->flags = flags & GUD_DISPLAY_MODE_FLAG_USER_MASK;
16340e1a70bSNoralf Trønnes dst->type = DRM_MODE_TYPE_DRIVER;
16440e1a70bSNoralf Trønnes if (flags & GUD_DISPLAY_MODE_FLAG_PREFERRED)
16540e1a70bSNoralf Trønnes dst->type |= DRM_MODE_TYPE_PREFERRED;
16640e1a70bSNoralf Trønnes drm_mode_set_name(dst);
16740e1a70bSNoralf Trønnes }
16840e1a70bSNoralf Trønnes
16940e1a70bSNoralf Trønnes #endif
170