xref: /linux/drivers/gpu/drm/sysfb/ofdrm.c (revision 559d105f3ad3f0e8bd9d2c2fbdfe93391fae2602)
1319251c5SThomas Zimmermann // SPDX-License-Identifier: GPL-2.0-only
2319251c5SThomas Zimmermann 
3319251c5SThomas Zimmermann #include <linux/aperture.h>
4319251c5SThomas Zimmermann #include <linux/of_address.h>
5319251c5SThomas Zimmermann #include <linux/pci.h>
6319251c5SThomas Zimmermann #include <linux/platform_device.h>
7319251c5SThomas Zimmermann 
8319251c5SThomas Zimmermann #include <drm/clients/drm_client_setup.h>
9319251c5SThomas Zimmermann #include <drm/drm_atomic.h>
10319251c5SThomas Zimmermann #include <drm/drm_atomic_state_helper.h>
11319251c5SThomas Zimmermann #include <drm/drm_connector.h>
12319251c5SThomas Zimmermann #include <drm/drm_damage_helper.h>
13319251c5SThomas Zimmermann #include <drm/drm_device.h>
14319251c5SThomas Zimmermann #include <drm/drm_drv.h>
15319251c5SThomas Zimmermann #include <drm/drm_fbdev_shmem.h>
16319251c5SThomas Zimmermann #include <drm/drm_format_helper.h>
17319251c5SThomas Zimmermann #include <drm/drm_framebuffer.h>
18319251c5SThomas Zimmermann #include <drm/drm_gem_atomic_helper.h>
19319251c5SThomas Zimmermann #include <drm/drm_gem_framebuffer_helper.h>
20319251c5SThomas Zimmermann #include <drm/drm_gem_shmem_helper.h>
21319251c5SThomas Zimmermann #include <drm/drm_managed.h>
22319251c5SThomas Zimmermann #include <drm/drm_modeset_helper_vtables.h>
23319251c5SThomas Zimmermann #include <drm/drm_probe_helper.h>
24319251c5SThomas Zimmermann 
25b5626f6fSThomas Zimmermann #include "drm_sysfb_helper.h"
26b5626f6fSThomas Zimmermann 
27319251c5SThomas Zimmermann #define DRIVER_NAME	"ofdrm"
28319251c5SThomas Zimmermann #define DRIVER_DESC	"DRM driver for OF platform devices"
29319251c5SThomas Zimmermann #define DRIVER_MAJOR	1
30319251c5SThomas Zimmermann #define DRIVER_MINOR	0
31319251c5SThomas Zimmermann 
32319251c5SThomas Zimmermann #define PCI_VENDOR_ID_ATI_R520	0x7100
33319251c5SThomas Zimmermann #define PCI_VENDOR_ID_ATI_R600	0x9400
34319251c5SThomas Zimmermann 
35319251c5SThomas Zimmermann #define OFDRM_GAMMA_LUT_SIZE	256
36319251c5SThomas Zimmermann 
37319251c5SThomas Zimmermann /* Definitions used by the Avivo palette  */
38319251c5SThomas Zimmermann #define AVIVO_DC_LUT_RW_SELECT                  0x6480
39319251c5SThomas Zimmermann #define AVIVO_DC_LUT_RW_MODE                    0x6484
40319251c5SThomas Zimmermann #define AVIVO_DC_LUT_RW_INDEX                   0x6488
41319251c5SThomas Zimmermann #define AVIVO_DC_LUT_SEQ_COLOR                  0x648c
42319251c5SThomas Zimmermann #define AVIVO_DC_LUT_PWL_DATA                   0x6490
43319251c5SThomas Zimmermann #define AVIVO_DC_LUT_30_COLOR                   0x6494
44319251c5SThomas Zimmermann #define AVIVO_DC_LUT_READ_PIPE_SELECT           0x6498
45319251c5SThomas Zimmermann #define AVIVO_DC_LUT_WRITE_EN_MASK              0x649c
46319251c5SThomas Zimmermann #define AVIVO_DC_LUT_AUTOFILL                   0x64a0
47319251c5SThomas Zimmermann #define AVIVO_DC_LUTA_CONTROL                   0x64c0
48319251c5SThomas Zimmermann #define AVIVO_DC_LUTA_BLACK_OFFSET_BLUE         0x64c4
49319251c5SThomas Zimmermann #define AVIVO_DC_LUTA_BLACK_OFFSET_GREEN        0x64c8
50319251c5SThomas Zimmermann #define AVIVO_DC_LUTA_BLACK_OFFSET_RED          0x64cc
51319251c5SThomas Zimmermann #define AVIVO_DC_LUTA_WHITE_OFFSET_BLUE         0x64d0
52319251c5SThomas Zimmermann #define AVIVO_DC_LUTA_WHITE_OFFSET_GREEN        0x64d4
53319251c5SThomas Zimmermann #define AVIVO_DC_LUTA_WHITE_OFFSET_RED          0x64d8
54319251c5SThomas Zimmermann #define AVIVO_DC_LUTB_CONTROL                   0x6cc0
55319251c5SThomas Zimmermann #define AVIVO_DC_LUTB_BLACK_OFFSET_BLUE         0x6cc4
56319251c5SThomas Zimmermann #define AVIVO_DC_LUTB_BLACK_OFFSET_GREEN        0x6cc8
57319251c5SThomas Zimmermann #define AVIVO_DC_LUTB_BLACK_OFFSET_RED          0x6ccc
58319251c5SThomas Zimmermann #define AVIVO_DC_LUTB_WHITE_OFFSET_BLUE         0x6cd0
59319251c5SThomas Zimmermann #define AVIVO_DC_LUTB_WHITE_OFFSET_GREEN        0x6cd4
60319251c5SThomas Zimmermann #define AVIVO_DC_LUTB_WHITE_OFFSET_RED          0x6cd8
61319251c5SThomas Zimmermann 
62319251c5SThomas Zimmermann enum ofdrm_model {
63319251c5SThomas Zimmermann 	OFDRM_MODEL_UNKNOWN,
64319251c5SThomas Zimmermann 	OFDRM_MODEL_MACH64, /* ATI Mach64 */
65319251c5SThomas Zimmermann 	OFDRM_MODEL_RAGE128, /* ATI Rage128 */
66319251c5SThomas Zimmermann 	OFDRM_MODEL_RAGE_M3A, /* ATI Rage Mobility M3 Head A */
67319251c5SThomas Zimmermann 	OFDRM_MODEL_RAGE_M3B, /* ATI Rage Mobility M3 Head B */
68319251c5SThomas Zimmermann 	OFDRM_MODEL_RADEON, /* ATI Radeon */
69319251c5SThomas Zimmermann 	OFDRM_MODEL_GXT2000, /* IBM GXT2000 */
70319251c5SThomas Zimmermann 	OFDRM_MODEL_AVIVO, /* ATI R5xx */
71319251c5SThomas Zimmermann 	OFDRM_MODEL_QEMU, /* QEMU VGA */
72319251c5SThomas Zimmermann };
73319251c5SThomas Zimmermann 
74319251c5SThomas Zimmermann /*
75319251c5SThomas Zimmermann  * Helpers for display nodes
76319251c5SThomas Zimmermann  */
77319251c5SThomas Zimmermann 
78319251c5SThomas Zimmermann static int display_get_validated_int(struct drm_device *dev, const char *name, uint32_t value)
79319251c5SThomas Zimmermann {
80319251c5SThomas Zimmermann 	if (value > INT_MAX) {
81319251c5SThomas Zimmermann 		drm_err(dev, "invalid framebuffer %s of %u\n", name, value);
82319251c5SThomas Zimmermann 		return -EINVAL;
83319251c5SThomas Zimmermann 	}
84319251c5SThomas Zimmermann 	return (int)value;
85319251c5SThomas Zimmermann }
86319251c5SThomas Zimmermann 
87319251c5SThomas Zimmermann static int display_get_validated_int0(struct drm_device *dev, const char *name, uint32_t value)
88319251c5SThomas Zimmermann {
89319251c5SThomas Zimmermann 	if (!value) {
90319251c5SThomas Zimmermann 		drm_err(dev, "invalid framebuffer %s of %u\n", name, value);
91319251c5SThomas Zimmermann 		return -EINVAL;
92319251c5SThomas Zimmermann 	}
93319251c5SThomas Zimmermann 	return display_get_validated_int(dev, name, value);
94319251c5SThomas Zimmermann }
95319251c5SThomas Zimmermann 
96319251c5SThomas Zimmermann static const struct drm_format_info *display_get_validated_format(struct drm_device *dev,
97319251c5SThomas Zimmermann 								  u32 depth, bool big_endian)
98319251c5SThomas Zimmermann {
99319251c5SThomas Zimmermann 	const struct drm_format_info *info;
100319251c5SThomas Zimmermann 	u32 format;
101319251c5SThomas Zimmermann 
102319251c5SThomas Zimmermann 	switch (depth) {
103319251c5SThomas Zimmermann 	case 8:
104319251c5SThomas Zimmermann 		format = drm_mode_legacy_fb_format(8, 8);
105319251c5SThomas Zimmermann 		break;
106319251c5SThomas Zimmermann 	case 15:
107319251c5SThomas Zimmermann 	case 16:
108319251c5SThomas Zimmermann 		format = drm_mode_legacy_fb_format(16, depth);
109319251c5SThomas Zimmermann 		break;
110319251c5SThomas Zimmermann 	case 32:
111319251c5SThomas Zimmermann 		format = drm_mode_legacy_fb_format(32, 24);
112319251c5SThomas Zimmermann 		break;
113319251c5SThomas Zimmermann 	default:
114319251c5SThomas Zimmermann 		drm_err(dev, "unsupported framebuffer depth %u\n", depth);
115319251c5SThomas Zimmermann 		return ERR_PTR(-EINVAL);
116319251c5SThomas Zimmermann 	}
117319251c5SThomas Zimmermann 
118319251c5SThomas Zimmermann 	/*
119319251c5SThomas Zimmermann 	 * DRM formats assume little-endian byte order. Update the format
120319251c5SThomas Zimmermann 	 * if the scanout buffer uses big-endian ordering.
121319251c5SThomas Zimmermann 	 */
122319251c5SThomas Zimmermann 	if (big_endian) {
123319251c5SThomas Zimmermann 		switch (format) {
124319251c5SThomas Zimmermann 		case DRM_FORMAT_XRGB8888:
125319251c5SThomas Zimmermann 			format = DRM_FORMAT_BGRX8888;
126319251c5SThomas Zimmermann 			break;
127319251c5SThomas Zimmermann 		case DRM_FORMAT_ARGB8888:
128319251c5SThomas Zimmermann 			format = DRM_FORMAT_BGRA8888;
129319251c5SThomas Zimmermann 			break;
130319251c5SThomas Zimmermann 		case DRM_FORMAT_RGB565:
131319251c5SThomas Zimmermann 			format = DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN;
132319251c5SThomas Zimmermann 			break;
133319251c5SThomas Zimmermann 		case DRM_FORMAT_XRGB1555:
134319251c5SThomas Zimmermann 			format = DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN;
135319251c5SThomas Zimmermann 			break;
136319251c5SThomas Zimmermann 		default:
137319251c5SThomas Zimmermann 			break;
138319251c5SThomas Zimmermann 		}
139319251c5SThomas Zimmermann 	}
140319251c5SThomas Zimmermann 
141319251c5SThomas Zimmermann 	info = drm_format_info(format);
142319251c5SThomas Zimmermann 	if (!info) {
143319251c5SThomas Zimmermann 		drm_err(dev, "cannot find framebuffer format for depth %u\n", depth);
144319251c5SThomas Zimmermann 		return ERR_PTR(-EINVAL);
145319251c5SThomas Zimmermann 	}
146319251c5SThomas Zimmermann 
147319251c5SThomas Zimmermann 	return info;
148319251c5SThomas Zimmermann }
149319251c5SThomas Zimmermann 
150319251c5SThomas Zimmermann static int display_read_u32_of(struct drm_device *dev, struct device_node *of_node,
151319251c5SThomas Zimmermann 			       const char *name, u32 *value)
152319251c5SThomas Zimmermann {
153319251c5SThomas Zimmermann 	int ret = of_property_read_u32(of_node, name, value);
154319251c5SThomas Zimmermann 
155319251c5SThomas Zimmermann 	if (ret)
156319251c5SThomas Zimmermann 		drm_err(dev, "cannot parse framebuffer %s: error %d\n", name, ret);
157319251c5SThomas Zimmermann 	return ret;
158319251c5SThomas Zimmermann }
159319251c5SThomas Zimmermann 
160319251c5SThomas Zimmermann static bool display_get_big_endian_of(struct drm_device *dev, struct device_node *of_node)
161319251c5SThomas Zimmermann {
162319251c5SThomas Zimmermann 	bool big_endian;
163319251c5SThomas Zimmermann 
164319251c5SThomas Zimmermann #ifdef __BIG_ENDIAN
165319251c5SThomas Zimmermann 	big_endian = !of_property_read_bool(of_node, "little-endian");
166319251c5SThomas Zimmermann #else
167319251c5SThomas Zimmermann 	big_endian = of_property_read_bool(of_node, "big-endian");
168319251c5SThomas Zimmermann #endif
169319251c5SThomas Zimmermann 
170319251c5SThomas Zimmermann 	return big_endian;
171319251c5SThomas Zimmermann }
172319251c5SThomas Zimmermann 
173319251c5SThomas Zimmermann static int display_get_width_of(struct drm_device *dev, struct device_node *of_node)
174319251c5SThomas Zimmermann {
175319251c5SThomas Zimmermann 	u32 width;
176319251c5SThomas Zimmermann 	int ret = display_read_u32_of(dev, of_node, "width", &width);
177319251c5SThomas Zimmermann 
178319251c5SThomas Zimmermann 	if (ret)
179319251c5SThomas Zimmermann 		return ret;
180319251c5SThomas Zimmermann 	return display_get_validated_int0(dev, "width", width);
181319251c5SThomas Zimmermann }
182319251c5SThomas Zimmermann 
183319251c5SThomas Zimmermann static int display_get_height_of(struct drm_device *dev, struct device_node *of_node)
184319251c5SThomas Zimmermann {
185319251c5SThomas Zimmermann 	u32 height;
186319251c5SThomas Zimmermann 	int ret = display_read_u32_of(dev, of_node, "height", &height);
187319251c5SThomas Zimmermann 
188319251c5SThomas Zimmermann 	if (ret)
189319251c5SThomas Zimmermann 		return ret;
190319251c5SThomas Zimmermann 	return display_get_validated_int0(dev, "height", height);
191319251c5SThomas Zimmermann }
192319251c5SThomas Zimmermann 
193319251c5SThomas Zimmermann static int display_get_depth_of(struct drm_device *dev, struct device_node *of_node)
194319251c5SThomas Zimmermann {
195319251c5SThomas Zimmermann 	u32 depth;
196319251c5SThomas Zimmermann 	int ret = display_read_u32_of(dev, of_node, "depth", &depth);
197319251c5SThomas Zimmermann 
198319251c5SThomas Zimmermann 	if (ret)
199319251c5SThomas Zimmermann 		return ret;
200319251c5SThomas Zimmermann 	return display_get_validated_int0(dev, "depth", depth);
201319251c5SThomas Zimmermann }
202319251c5SThomas Zimmermann 
203319251c5SThomas Zimmermann static int display_get_linebytes_of(struct drm_device *dev, struct device_node *of_node)
204319251c5SThomas Zimmermann {
205319251c5SThomas Zimmermann 	u32 linebytes;
206319251c5SThomas Zimmermann 	int ret = display_read_u32_of(dev, of_node, "linebytes", &linebytes);
207319251c5SThomas Zimmermann 
208319251c5SThomas Zimmermann 	if (ret)
209319251c5SThomas Zimmermann 		return ret;
210319251c5SThomas Zimmermann 	return display_get_validated_int(dev, "linebytes", linebytes);
211319251c5SThomas Zimmermann }
212319251c5SThomas Zimmermann 
213319251c5SThomas Zimmermann static u64 display_get_address_of(struct drm_device *dev, struct device_node *of_node)
214319251c5SThomas Zimmermann {
215319251c5SThomas Zimmermann 	u32 address;
216319251c5SThomas Zimmermann 	int ret;
217319251c5SThomas Zimmermann 
218319251c5SThomas Zimmermann 	/*
219319251c5SThomas Zimmermann 	 * Not all devices provide an address property, it's not
220319251c5SThomas Zimmermann 	 * a bug if this fails. The driver will try to find the
221319251c5SThomas Zimmermann 	 * framebuffer base address from the device's memory regions.
222319251c5SThomas Zimmermann 	 */
223319251c5SThomas Zimmermann 	ret = of_property_read_u32(of_node, "address", &address);
224319251c5SThomas Zimmermann 	if (ret)
225319251c5SThomas Zimmermann 		return OF_BAD_ADDR;
226319251c5SThomas Zimmermann 
227319251c5SThomas Zimmermann 	return address;
228319251c5SThomas Zimmermann }
229319251c5SThomas Zimmermann 
230319251c5SThomas Zimmermann static bool is_avivo(u32 vendor, u32 device)
231319251c5SThomas Zimmermann {
232319251c5SThomas Zimmermann 	/* This will match most R5xx */
233319251c5SThomas Zimmermann 	return (vendor == PCI_VENDOR_ID_ATI) &&
234319251c5SThomas Zimmermann 	       ((device >= PCI_VENDOR_ID_ATI_R520 && device < 0x7800) ||
235319251c5SThomas Zimmermann 		(PCI_VENDOR_ID_ATI_R600 >= 0x9400));
236319251c5SThomas Zimmermann }
237319251c5SThomas Zimmermann 
238319251c5SThomas Zimmermann static enum ofdrm_model display_get_model_of(struct drm_device *dev, struct device_node *of_node)
239319251c5SThomas Zimmermann {
240319251c5SThomas Zimmermann 	enum ofdrm_model model = OFDRM_MODEL_UNKNOWN;
241319251c5SThomas Zimmermann 
242319251c5SThomas Zimmermann 	if (of_node_name_prefix(of_node, "ATY,Rage128")) {
243319251c5SThomas Zimmermann 		model = OFDRM_MODEL_RAGE128;
244319251c5SThomas Zimmermann 	} else if (of_node_name_prefix(of_node, "ATY,RageM3pA") ||
245319251c5SThomas Zimmermann 		   of_node_name_prefix(of_node, "ATY,RageM3p12A")) {
246319251c5SThomas Zimmermann 		model = OFDRM_MODEL_RAGE_M3A;
247319251c5SThomas Zimmermann 	} else if (of_node_name_prefix(of_node, "ATY,RageM3pB")) {
248319251c5SThomas Zimmermann 		model = OFDRM_MODEL_RAGE_M3B;
249319251c5SThomas Zimmermann 	} else if (of_node_name_prefix(of_node, "ATY,Rage6")) {
250319251c5SThomas Zimmermann 		model = OFDRM_MODEL_RADEON;
251319251c5SThomas Zimmermann 	} else if (of_node_name_prefix(of_node, "ATY,")) {
252319251c5SThomas Zimmermann 		return OFDRM_MODEL_MACH64;
253319251c5SThomas Zimmermann 	} else if (of_device_is_compatible(of_node, "pci1014,b7") ||
254319251c5SThomas Zimmermann 		   of_device_is_compatible(of_node, "pci1014,21c")) {
255319251c5SThomas Zimmermann 		model = OFDRM_MODEL_GXT2000;
256319251c5SThomas Zimmermann 	} else if (of_node_name_prefix(of_node, "vga,Display-")) {
257319251c5SThomas Zimmermann 		struct device_node *of_parent;
258319251c5SThomas Zimmermann 		const __be32 *vendor_p, *device_p;
259319251c5SThomas Zimmermann 
260319251c5SThomas Zimmermann 		/* Look for AVIVO initialized by SLOF */
261319251c5SThomas Zimmermann 		of_parent = of_get_parent(of_node);
262319251c5SThomas Zimmermann 		vendor_p = of_get_property(of_parent, "vendor-id", NULL);
263319251c5SThomas Zimmermann 		device_p = of_get_property(of_parent, "device-id", NULL);
264319251c5SThomas Zimmermann 		if (vendor_p && device_p) {
265319251c5SThomas Zimmermann 			u32 vendor = be32_to_cpup(vendor_p);
266319251c5SThomas Zimmermann 			u32 device = be32_to_cpup(device_p);
267319251c5SThomas Zimmermann 
268319251c5SThomas Zimmermann 			if (is_avivo(vendor, device))
269319251c5SThomas Zimmermann 				model = OFDRM_MODEL_AVIVO;
270319251c5SThomas Zimmermann 		}
271319251c5SThomas Zimmermann 		of_node_put(of_parent);
272319251c5SThomas Zimmermann 	} else if (of_device_is_compatible(of_node, "qemu,std-vga")) {
273319251c5SThomas Zimmermann 		model = OFDRM_MODEL_QEMU;
274319251c5SThomas Zimmermann 	}
275319251c5SThomas Zimmermann 
276319251c5SThomas Zimmermann 	return model;
277319251c5SThomas Zimmermann }
278319251c5SThomas Zimmermann 
279319251c5SThomas Zimmermann /*
280319251c5SThomas Zimmermann  * Open Firmware display device
281319251c5SThomas Zimmermann  */
282319251c5SThomas Zimmermann 
283319251c5SThomas Zimmermann struct ofdrm_device;
284319251c5SThomas Zimmermann 
285319251c5SThomas Zimmermann struct ofdrm_device_funcs {
286319251c5SThomas Zimmermann 	void __iomem *(*cmap_ioremap)(struct ofdrm_device *odev,
287319251c5SThomas Zimmermann 				      struct device_node *of_node,
288319251c5SThomas Zimmermann 				      u64 fb_bas);
289319251c5SThomas Zimmermann 	void (*cmap_write)(struct ofdrm_device *odev, unsigned char index,
290319251c5SThomas Zimmermann 			   unsigned char r, unsigned char g, unsigned char b);
291319251c5SThomas Zimmermann };
292319251c5SThomas Zimmermann 
293319251c5SThomas Zimmermann struct ofdrm_device {
294b5626f6fSThomas Zimmermann 	struct drm_sysfb_device sysfb;
295319251c5SThomas Zimmermann 
296319251c5SThomas Zimmermann 	const struct ofdrm_device_funcs *funcs;
297319251c5SThomas Zimmermann 
298319251c5SThomas Zimmermann 	/* colormap */
299319251c5SThomas Zimmermann 	void __iomem *cmap_base;
300319251c5SThomas Zimmermann 
301319251c5SThomas Zimmermann 	/* modesetting */
302319251c5SThomas Zimmermann 	uint32_t formats[8];
303319251c5SThomas Zimmermann 	struct drm_plane primary_plane;
304319251c5SThomas Zimmermann 	struct drm_crtc crtc;
305319251c5SThomas Zimmermann 	struct drm_encoder encoder;
306319251c5SThomas Zimmermann 	struct drm_connector connector;
307319251c5SThomas Zimmermann };
308319251c5SThomas Zimmermann 
309319251c5SThomas Zimmermann static struct ofdrm_device *ofdrm_device_of_dev(struct drm_device *dev)
310319251c5SThomas Zimmermann {
311b5626f6fSThomas Zimmermann 	return container_of(to_drm_sysfb_device(dev), struct ofdrm_device, sysfb);
312319251c5SThomas Zimmermann }
313319251c5SThomas Zimmermann 
314319251c5SThomas Zimmermann /*
315319251c5SThomas Zimmermann  * Hardware
316319251c5SThomas Zimmermann  */
317319251c5SThomas Zimmermann 
318319251c5SThomas Zimmermann #if defined(CONFIG_PCI)
319319251c5SThomas Zimmermann static struct pci_dev *display_get_pci_dev_of(struct drm_device *dev, struct device_node *of_node)
320319251c5SThomas Zimmermann {
321319251c5SThomas Zimmermann 	const __be32 *vendor_p, *device_p;
322319251c5SThomas Zimmermann 	u32 vendor, device;
323319251c5SThomas Zimmermann 	struct pci_dev *pcidev;
324319251c5SThomas Zimmermann 
325319251c5SThomas Zimmermann 	vendor_p = of_get_property(of_node, "vendor-id", NULL);
326319251c5SThomas Zimmermann 	if (!vendor_p)
327319251c5SThomas Zimmermann 		return ERR_PTR(-ENODEV);
328319251c5SThomas Zimmermann 	vendor = be32_to_cpup(vendor_p);
329319251c5SThomas Zimmermann 
330319251c5SThomas Zimmermann 	device_p = of_get_property(of_node, "device-id", NULL);
331319251c5SThomas Zimmermann 	if (!device_p)
332319251c5SThomas Zimmermann 		return ERR_PTR(-ENODEV);
333319251c5SThomas Zimmermann 	device = be32_to_cpup(device_p);
334319251c5SThomas Zimmermann 
335319251c5SThomas Zimmermann 	pcidev = pci_get_device(vendor, device, NULL);
336319251c5SThomas Zimmermann 	if (!pcidev)
337319251c5SThomas Zimmermann 		return ERR_PTR(-ENODEV);
338319251c5SThomas Zimmermann 
339319251c5SThomas Zimmermann 	return pcidev;
340319251c5SThomas Zimmermann }
341319251c5SThomas Zimmermann 
342319251c5SThomas Zimmermann static void ofdrm_pci_release(void *data)
343319251c5SThomas Zimmermann {
344319251c5SThomas Zimmermann 	struct pci_dev *pcidev = data;
345319251c5SThomas Zimmermann 
346319251c5SThomas Zimmermann 	pci_disable_device(pcidev);
347319251c5SThomas Zimmermann }
348319251c5SThomas Zimmermann 
349319251c5SThomas Zimmermann static int ofdrm_device_init_pci(struct ofdrm_device *odev)
350319251c5SThomas Zimmermann {
351b5626f6fSThomas Zimmermann 	struct drm_device *dev = &odev->sysfb.dev;
352319251c5SThomas Zimmermann 	struct platform_device *pdev = to_platform_device(dev->dev);
353319251c5SThomas Zimmermann 	struct device_node *of_node = pdev->dev.of_node;
354319251c5SThomas Zimmermann 	struct pci_dev *pcidev;
355319251c5SThomas Zimmermann 	int ret;
356319251c5SThomas Zimmermann 
357319251c5SThomas Zimmermann 	/*
358319251c5SThomas Zimmermann 	 * Never use pcim_ or other managed helpers on the returned PCI
359319251c5SThomas Zimmermann 	 * device. Otherwise, probing the native driver will fail for
360319251c5SThomas Zimmermann 	 * resource conflicts. PCI-device management has to be tied to
361319251c5SThomas Zimmermann 	 * the lifetime of the platform device until the native driver
362319251c5SThomas Zimmermann 	 * takes over.
363319251c5SThomas Zimmermann 	 */
364319251c5SThomas Zimmermann 	pcidev = display_get_pci_dev_of(dev, of_node);
365319251c5SThomas Zimmermann 	if (IS_ERR(pcidev))
366319251c5SThomas Zimmermann 		return 0; /* no PCI device found; ignore the error */
367319251c5SThomas Zimmermann 
368319251c5SThomas Zimmermann 	ret = pci_enable_device(pcidev);
369319251c5SThomas Zimmermann 	if (ret) {
370319251c5SThomas Zimmermann 		drm_err(dev, "pci_enable_device(%s) failed: %d\n",
371319251c5SThomas Zimmermann 			dev_name(&pcidev->dev), ret);
372319251c5SThomas Zimmermann 		return ret;
373319251c5SThomas Zimmermann 	}
374319251c5SThomas Zimmermann 	ret = devm_add_action_or_reset(&pdev->dev, ofdrm_pci_release, pcidev);
375319251c5SThomas Zimmermann 	if (ret)
376319251c5SThomas Zimmermann 		return ret;
377319251c5SThomas Zimmermann 
378319251c5SThomas Zimmermann 	return 0;
379319251c5SThomas Zimmermann }
380319251c5SThomas Zimmermann #else
381319251c5SThomas Zimmermann static int ofdrm_device_init_pci(struct ofdrm_device *odev)
382319251c5SThomas Zimmermann {
383319251c5SThomas Zimmermann 	return 0;
384319251c5SThomas Zimmermann }
385319251c5SThomas Zimmermann #endif
386319251c5SThomas Zimmermann 
387319251c5SThomas Zimmermann /*
388319251c5SThomas Zimmermann  *  OF display settings
389319251c5SThomas Zimmermann  */
390319251c5SThomas Zimmermann 
391319251c5SThomas Zimmermann static struct resource *ofdrm_find_fb_resource(struct ofdrm_device *odev,
392319251c5SThomas Zimmermann 					       struct resource *fb_res)
393319251c5SThomas Zimmermann {
394b5626f6fSThomas Zimmermann 	struct platform_device *pdev = to_platform_device(odev->sysfb.dev.dev);
395319251c5SThomas Zimmermann 	struct resource *res, *max_res = NULL;
396319251c5SThomas Zimmermann 	u32 i;
397319251c5SThomas Zimmermann 
398319251c5SThomas Zimmermann 	for (i = 0; pdev->num_resources; ++i) {
399319251c5SThomas Zimmermann 		res = platform_get_resource(pdev, IORESOURCE_MEM, i);
400319251c5SThomas Zimmermann 		if (!res)
401319251c5SThomas Zimmermann 			break; /* all resources processed */
402319251c5SThomas Zimmermann 		if (resource_size(res) < resource_size(fb_res))
403319251c5SThomas Zimmermann 			continue; /* resource too small */
404319251c5SThomas Zimmermann 		if (fb_res->start && resource_contains(res, fb_res))
405319251c5SThomas Zimmermann 			return res; /* resource contains framebuffer */
406319251c5SThomas Zimmermann 		if (!max_res || resource_size(res) > resource_size(max_res))
407319251c5SThomas Zimmermann 			max_res = res; /* store largest resource as fallback */
408319251c5SThomas Zimmermann 	}
409319251c5SThomas Zimmermann 
410319251c5SThomas Zimmermann 	return max_res;
411319251c5SThomas Zimmermann }
412319251c5SThomas Zimmermann 
413319251c5SThomas Zimmermann /*
414319251c5SThomas Zimmermann  * Colormap / Palette
415319251c5SThomas Zimmermann  */
416319251c5SThomas Zimmermann 
417319251c5SThomas Zimmermann static void __iomem *get_cmap_address_of(struct ofdrm_device *odev, struct device_node *of_node,
418319251c5SThomas Zimmermann 					 int bar_no, unsigned long offset, unsigned long size)
419319251c5SThomas Zimmermann {
420b5626f6fSThomas Zimmermann 	struct drm_device *dev = &odev->sysfb.dev;
421319251c5SThomas Zimmermann 	const __be32 *addr_p;
422319251c5SThomas Zimmermann 	u64 max_size, address;
423319251c5SThomas Zimmermann 	unsigned int flags;
424319251c5SThomas Zimmermann 	void __iomem *mem;
425319251c5SThomas Zimmermann 
426319251c5SThomas Zimmermann 	addr_p = of_get_pci_address(of_node, bar_no, &max_size, &flags);
427319251c5SThomas Zimmermann 	if (!addr_p)
428319251c5SThomas Zimmermann 		addr_p = of_get_address(of_node, bar_no, &max_size, &flags);
429319251c5SThomas Zimmermann 	if (!addr_p)
430319251c5SThomas Zimmermann 		return IOMEM_ERR_PTR(-ENODEV);
431319251c5SThomas Zimmermann 
432319251c5SThomas Zimmermann 	if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
433319251c5SThomas Zimmermann 		return IOMEM_ERR_PTR(-ENODEV);
434319251c5SThomas Zimmermann 
435319251c5SThomas Zimmermann 	if ((offset + size) >= max_size)
436319251c5SThomas Zimmermann 		return IOMEM_ERR_PTR(-ENODEV);
437319251c5SThomas Zimmermann 
438319251c5SThomas Zimmermann 	address = of_translate_address(of_node, addr_p);
439319251c5SThomas Zimmermann 	if (address == OF_BAD_ADDR)
440319251c5SThomas Zimmermann 		return IOMEM_ERR_PTR(-ENODEV);
441319251c5SThomas Zimmermann 
442319251c5SThomas Zimmermann 	mem = devm_ioremap(dev->dev, address + offset, size);
443319251c5SThomas Zimmermann 	if (!mem)
444319251c5SThomas Zimmermann 		return IOMEM_ERR_PTR(-ENOMEM);
445319251c5SThomas Zimmermann 
446319251c5SThomas Zimmermann 	return mem;
447319251c5SThomas Zimmermann }
448319251c5SThomas Zimmermann 
449319251c5SThomas Zimmermann static void __iomem *ofdrm_mach64_cmap_ioremap(struct ofdrm_device *odev,
450319251c5SThomas Zimmermann 					       struct device_node *of_node,
451319251c5SThomas Zimmermann 					       u64 fb_base)
452319251c5SThomas Zimmermann {
453b5626f6fSThomas Zimmermann 	struct drm_device *dev = &odev->sysfb.dev;
454319251c5SThomas Zimmermann 	u64 address;
455319251c5SThomas Zimmermann 	void __iomem *cmap_base;
456319251c5SThomas Zimmermann 
457319251c5SThomas Zimmermann 	address = fb_base & 0xff000000ul;
458319251c5SThomas Zimmermann 	address += 0x7ff000;
459319251c5SThomas Zimmermann 
460319251c5SThomas Zimmermann 	cmap_base = devm_ioremap(dev->dev, address, 0x1000);
461319251c5SThomas Zimmermann 	if (!cmap_base)
462319251c5SThomas Zimmermann 		return IOMEM_ERR_PTR(-ENOMEM);
463319251c5SThomas Zimmermann 
464319251c5SThomas Zimmermann 	return cmap_base;
465319251c5SThomas Zimmermann }
466319251c5SThomas Zimmermann 
467319251c5SThomas Zimmermann static void ofdrm_mach64_cmap_write(struct ofdrm_device *odev, unsigned char index,
468319251c5SThomas Zimmermann 				    unsigned char r, unsigned char g, unsigned char b)
469319251c5SThomas Zimmermann {
470319251c5SThomas Zimmermann 	void __iomem *addr = odev->cmap_base + 0xcc0;
471319251c5SThomas Zimmermann 	void __iomem *data = odev->cmap_base + 0xcc0 + 1;
472319251c5SThomas Zimmermann 
473319251c5SThomas Zimmermann 	writeb(index, addr);
474319251c5SThomas Zimmermann 	writeb(r, data);
475319251c5SThomas Zimmermann 	writeb(g, data);
476319251c5SThomas Zimmermann 	writeb(b, data);
477319251c5SThomas Zimmermann }
478319251c5SThomas Zimmermann 
479319251c5SThomas Zimmermann static void __iomem *ofdrm_rage128_cmap_ioremap(struct ofdrm_device *odev,
480319251c5SThomas Zimmermann 						struct device_node *of_node,
481319251c5SThomas Zimmermann 						u64 fb_base)
482319251c5SThomas Zimmermann {
483319251c5SThomas Zimmermann 	return get_cmap_address_of(odev, of_node, 2, 0, 0x1fff);
484319251c5SThomas Zimmermann }
485319251c5SThomas Zimmermann 
486319251c5SThomas Zimmermann static void ofdrm_rage128_cmap_write(struct ofdrm_device *odev, unsigned char index,
487319251c5SThomas Zimmermann 				     unsigned char r, unsigned char g, unsigned char b)
488319251c5SThomas Zimmermann {
489319251c5SThomas Zimmermann 	void __iomem *addr = odev->cmap_base + 0xb0;
490319251c5SThomas Zimmermann 	void __iomem *data = odev->cmap_base + 0xb4;
491319251c5SThomas Zimmermann 	u32 color = (r << 16) | (g << 8) | b;
492319251c5SThomas Zimmermann 
493319251c5SThomas Zimmermann 	writeb(index, addr);
494319251c5SThomas Zimmermann 	writel(color, data);
495319251c5SThomas Zimmermann }
496319251c5SThomas Zimmermann 
497319251c5SThomas Zimmermann static void __iomem *ofdrm_rage_m3a_cmap_ioremap(struct ofdrm_device *odev,
498319251c5SThomas Zimmermann 						 struct device_node *of_node,
499319251c5SThomas Zimmermann 						 u64 fb_base)
500319251c5SThomas Zimmermann {
501319251c5SThomas Zimmermann 	return get_cmap_address_of(odev, of_node, 2, 0, 0x1fff);
502319251c5SThomas Zimmermann }
503319251c5SThomas Zimmermann 
504319251c5SThomas Zimmermann static void ofdrm_rage_m3a_cmap_write(struct ofdrm_device *odev, unsigned char index,
505319251c5SThomas Zimmermann 				      unsigned char r, unsigned char g, unsigned char b)
506319251c5SThomas Zimmermann {
507319251c5SThomas Zimmermann 	void __iomem *dac_ctl = odev->cmap_base + 0x58;
508319251c5SThomas Zimmermann 	void __iomem *addr = odev->cmap_base + 0xb0;
509319251c5SThomas Zimmermann 	void __iomem *data = odev->cmap_base + 0xb4;
510319251c5SThomas Zimmermann 	u32 color = (r << 16) | (g << 8) | b;
511319251c5SThomas Zimmermann 	u32 val;
512319251c5SThomas Zimmermann 
513319251c5SThomas Zimmermann 	/* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
514319251c5SThomas Zimmermann 	val = readl(dac_ctl);
515319251c5SThomas Zimmermann 	val &= ~0x20;
516319251c5SThomas Zimmermann 	writel(val, dac_ctl);
517319251c5SThomas Zimmermann 
518319251c5SThomas Zimmermann 	/* Set color at palette index */
519319251c5SThomas Zimmermann 	writeb(index, addr);
520319251c5SThomas Zimmermann 	writel(color, data);
521319251c5SThomas Zimmermann }
522319251c5SThomas Zimmermann 
523319251c5SThomas Zimmermann static void __iomem *ofdrm_rage_m3b_cmap_ioremap(struct ofdrm_device *odev,
524319251c5SThomas Zimmermann 						 struct device_node *of_node,
525319251c5SThomas Zimmermann 						 u64 fb_base)
526319251c5SThomas Zimmermann {
527319251c5SThomas Zimmermann 	return get_cmap_address_of(odev, of_node, 2, 0, 0x1fff);
528319251c5SThomas Zimmermann }
529319251c5SThomas Zimmermann 
530319251c5SThomas Zimmermann static void ofdrm_rage_m3b_cmap_write(struct ofdrm_device *odev, unsigned char index,
531319251c5SThomas Zimmermann 				      unsigned char r, unsigned char g, unsigned char b)
532319251c5SThomas Zimmermann {
533319251c5SThomas Zimmermann 	void __iomem *dac_ctl = odev->cmap_base + 0x58;
534319251c5SThomas Zimmermann 	void __iomem *addr = odev->cmap_base + 0xb0;
535319251c5SThomas Zimmermann 	void __iomem *data = odev->cmap_base + 0xb4;
536319251c5SThomas Zimmermann 	u32 color = (r << 16) | (g << 8) | b;
537319251c5SThomas Zimmermann 	u32 val;
538319251c5SThomas Zimmermann 
539319251c5SThomas Zimmermann 	/* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
540319251c5SThomas Zimmermann 	val = readl(dac_ctl);
541319251c5SThomas Zimmermann 	val |= 0x20;
542319251c5SThomas Zimmermann 	writel(val, dac_ctl);
543319251c5SThomas Zimmermann 
544319251c5SThomas Zimmermann 	/* Set color at palette index */
545319251c5SThomas Zimmermann 	writeb(index, addr);
546319251c5SThomas Zimmermann 	writel(color, data);
547319251c5SThomas Zimmermann }
548319251c5SThomas Zimmermann 
549319251c5SThomas Zimmermann static void __iomem *ofdrm_radeon_cmap_ioremap(struct ofdrm_device *odev,
550319251c5SThomas Zimmermann 					       struct device_node *of_node,
551319251c5SThomas Zimmermann 					       u64 fb_base)
552319251c5SThomas Zimmermann {
553319251c5SThomas Zimmermann 	return get_cmap_address_of(odev, of_node, 1, 0, 0x1fff);
554319251c5SThomas Zimmermann }
555319251c5SThomas Zimmermann 
556319251c5SThomas Zimmermann static void __iomem *ofdrm_gxt2000_cmap_ioremap(struct ofdrm_device *odev,
557319251c5SThomas Zimmermann 						struct device_node *of_node,
558319251c5SThomas Zimmermann 						u64 fb_base)
559319251c5SThomas Zimmermann {
560319251c5SThomas Zimmermann 	return get_cmap_address_of(odev, of_node, 0, 0x6000, 0x1000);
561319251c5SThomas Zimmermann }
562319251c5SThomas Zimmermann 
563319251c5SThomas Zimmermann static void ofdrm_gxt2000_cmap_write(struct ofdrm_device *odev, unsigned char index,
564319251c5SThomas Zimmermann 				     unsigned char r, unsigned char g, unsigned char b)
565319251c5SThomas Zimmermann {
566319251c5SThomas Zimmermann 	void __iomem *data = ((unsigned int __iomem *)odev->cmap_base) + index;
567319251c5SThomas Zimmermann 	u32 color = (r << 16) | (g << 8) | b;
568319251c5SThomas Zimmermann 
569319251c5SThomas Zimmermann 	writel(color, data);
570319251c5SThomas Zimmermann }
571319251c5SThomas Zimmermann 
572319251c5SThomas Zimmermann static void __iomem *ofdrm_avivo_cmap_ioremap(struct ofdrm_device *odev,
573319251c5SThomas Zimmermann 					      struct device_node *of_node,
574319251c5SThomas Zimmermann 					      u64 fb_base)
575319251c5SThomas Zimmermann {
576319251c5SThomas Zimmermann 	struct device_node *of_parent;
577319251c5SThomas Zimmermann 	void __iomem *cmap_base;
578319251c5SThomas Zimmermann 
579319251c5SThomas Zimmermann 	of_parent = of_get_parent(of_node);
580319251c5SThomas Zimmermann 	cmap_base = get_cmap_address_of(odev, of_parent, 0, 0, 0x10000);
581319251c5SThomas Zimmermann 	of_node_put(of_parent);
582319251c5SThomas Zimmermann 
583319251c5SThomas Zimmermann 	return cmap_base;
584319251c5SThomas Zimmermann }
585319251c5SThomas Zimmermann 
586319251c5SThomas Zimmermann static void ofdrm_avivo_cmap_write(struct ofdrm_device *odev, unsigned char index,
587319251c5SThomas Zimmermann 				   unsigned char r, unsigned char g, unsigned char b)
588319251c5SThomas Zimmermann {
589319251c5SThomas Zimmermann 	void __iomem *lutsel = odev->cmap_base + AVIVO_DC_LUT_RW_SELECT;
590319251c5SThomas Zimmermann 	void __iomem *addr = odev->cmap_base + AVIVO_DC_LUT_RW_INDEX;
591319251c5SThomas Zimmermann 	void __iomem *data = odev->cmap_base + AVIVO_DC_LUT_30_COLOR;
592319251c5SThomas Zimmermann 	u32 color = (r << 22) | (g << 12) | (b << 2);
593319251c5SThomas Zimmermann 
594319251c5SThomas Zimmermann 	/* Write to both LUTs for now */
595319251c5SThomas Zimmermann 
596319251c5SThomas Zimmermann 	writel(1, lutsel);
597319251c5SThomas Zimmermann 	writeb(index, addr);
598319251c5SThomas Zimmermann 	writel(color, data);
599319251c5SThomas Zimmermann 
600319251c5SThomas Zimmermann 	writel(0, lutsel);
601319251c5SThomas Zimmermann 	writeb(index, addr);
602319251c5SThomas Zimmermann 	writel(color, data);
603319251c5SThomas Zimmermann }
604319251c5SThomas Zimmermann 
605319251c5SThomas Zimmermann static void __iomem *ofdrm_qemu_cmap_ioremap(struct ofdrm_device *odev,
606319251c5SThomas Zimmermann 					     struct device_node *of_node,
607319251c5SThomas Zimmermann 					     u64 fb_base)
608319251c5SThomas Zimmermann {
609319251c5SThomas Zimmermann 	static const __be32 io_of_addr[3] = {
610319251c5SThomas Zimmermann 		cpu_to_be32(0x01000000),
611319251c5SThomas Zimmermann 		cpu_to_be32(0x00),
612319251c5SThomas Zimmermann 		cpu_to_be32(0x00),
613319251c5SThomas Zimmermann 	};
614319251c5SThomas Zimmermann 
615b5626f6fSThomas Zimmermann 	struct drm_device *dev = &odev->sysfb.dev;
616319251c5SThomas Zimmermann 	u64 address;
617319251c5SThomas Zimmermann 	void __iomem *cmap_base;
618319251c5SThomas Zimmermann 
619319251c5SThomas Zimmermann 	address = of_translate_address(of_node, io_of_addr);
620319251c5SThomas Zimmermann 	if (address == OF_BAD_ADDR)
621319251c5SThomas Zimmermann 		return IOMEM_ERR_PTR(-ENODEV);
622319251c5SThomas Zimmermann 
623319251c5SThomas Zimmermann 	cmap_base = devm_ioremap(dev->dev, address + 0x3c8, 2);
624319251c5SThomas Zimmermann 	if (!cmap_base)
625319251c5SThomas Zimmermann 		return IOMEM_ERR_PTR(-ENOMEM);
626319251c5SThomas Zimmermann 
627319251c5SThomas Zimmermann 	return cmap_base;
628319251c5SThomas Zimmermann }
629319251c5SThomas Zimmermann 
630319251c5SThomas Zimmermann static void ofdrm_qemu_cmap_write(struct ofdrm_device *odev, unsigned char index,
631319251c5SThomas Zimmermann 				  unsigned char r, unsigned char g, unsigned char b)
632319251c5SThomas Zimmermann {
633319251c5SThomas Zimmermann 	void __iomem *addr = odev->cmap_base;
634319251c5SThomas Zimmermann 	void __iomem *data = odev->cmap_base + 1;
635319251c5SThomas Zimmermann 
636319251c5SThomas Zimmermann 	writeb(index, addr);
637319251c5SThomas Zimmermann 	writeb(r, data);
638319251c5SThomas Zimmermann 	writeb(g, data);
639319251c5SThomas Zimmermann 	writeb(b, data);
640319251c5SThomas Zimmermann }
641319251c5SThomas Zimmermann 
642319251c5SThomas Zimmermann static void ofdrm_device_set_gamma_linear(struct ofdrm_device *odev,
643319251c5SThomas Zimmermann 					  const struct drm_format_info *format)
644319251c5SThomas Zimmermann {
645b5626f6fSThomas Zimmermann 	struct drm_device *dev = &odev->sysfb.dev;
646319251c5SThomas Zimmermann 	int i;
647319251c5SThomas Zimmermann 
648319251c5SThomas Zimmermann 	switch (format->format) {
649319251c5SThomas Zimmermann 	case DRM_FORMAT_RGB565:
650319251c5SThomas Zimmermann 	case DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN:
651319251c5SThomas Zimmermann 		/* Use better interpolation, to take 32 values from 0 to 255 */
652319251c5SThomas Zimmermann 		for (i = 0; i < OFDRM_GAMMA_LUT_SIZE / 8; i++) {
653319251c5SThomas Zimmermann 			unsigned char r = i * 8 + i / 4;
654319251c5SThomas Zimmermann 			unsigned char g = i * 4 + i / 16;
655319251c5SThomas Zimmermann 			unsigned char b = i * 8 + i / 4;
656319251c5SThomas Zimmermann 
657319251c5SThomas Zimmermann 			odev->funcs->cmap_write(odev, i, r, g, b);
658319251c5SThomas Zimmermann 		}
659319251c5SThomas Zimmermann 		/* Green has one more bit, so add padding with 0 for red and blue. */
660319251c5SThomas Zimmermann 		for (i = OFDRM_GAMMA_LUT_SIZE / 8; i < OFDRM_GAMMA_LUT_SIZE / 4; i++) {
661319251c5SThomas Zimmermann 			unsigned char r = 0;
662319251c5SThomas Zimmermann 			unsigned char g = i * 4 + i / 16;
663319251c5SThomas Zimmermann 			unsigned char b = 0;
664319251c5SThomas Zimmermann 
665319251c5SThomas Zimmermann 			odev->funcs->cmap_write(odev, i, r, g, b);
666319251c5SThomas Zimmermann 		}
667319251c5SThomas Zimmermann 		break;
668319251c5SThomas Zimmermann 	case DRM_FORMAT_XRGB8888:
669319251c5SThomas Zimmermann 	case DRM_FORMAT_BGRX8888:
670319251c5SThomas Zimmermann 		for (i = 0; i < OFDRM_GAMMA_LUT_SIZE; i++)
671319251c5SThomas Zimmermann 			odev->funcs->cmap_write(odev, i, i, i, i);
672319251c5SThomas Zimmermann 		break;
673319251c5SThomas Zimmermann 	default:
674319251c5SThomas Zimmermann 		drm_warn_once(dev, "Unsupported format %p4cc for gamma correction\n",
675319251c5SThomas Zimmermann 			      &format->format);
676319251c5SThomas Zimmermann 		break;
677319251c5SThomas Zimmermann 	}
678319251c5SThomas Zimmermann }
679319251c5SThomas Zimmermann 
680319251c5SThomas Zimmermann static void ofdrm_device_set_gamma(struct ofdrm_device *odev,
681319251c5SThomas Zimmermann 				   const struct drm_format_info *format,
682319251c5SThomas Zimmermann 				   struct drm_color_lut *lut)
683319251c5SThomas Zimmermann {
684b5626f6fSThomas Zimmermann 	struct drm_device *dev = &odev->sysfb.dev;
685319251c5SThomas Zimmermann 	int i;
686319251c5SThomas Zimmermann 
687319251c5SThomas Zimmermann 	switch (format->format) {
688319251c5SThomas Zimmermann 	case DRM_FORMAT_RGB565:
689319251c5SThomas Zimmermann 	case DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN:
690319251c5SThomas Zimmermann 		/* Use better interpolation, to take 32 values from lut[0] to lut[255] */
691319251c5SThomas Zimmermann 		for (i = 0; i < OFDRM_GAMMA_LUT_SIZE / 8; i++) {
692319251c5SThomas Zimmermann 			unsigned char r = lut[i * 8 + i / 4].red >> 8;
693319251c5SThomas Zimmermann 			unsigned char g = lut[i * 4 + i / 16].green >> 8;
694319251c5SThomas Zimmermann 			unsigned char b = lut[i * 8 + i / 4].blue >> 8;
695319251c5SThomas Zimmermann 
696319251c5SThomas Zimmermann 			odev->funcs->cmap_write(odev, i, r, g, b);
697319251c5SThomas Zimmermann 		}
698319251c5SThomas Zimmermann 		/* Green has one more bit, so add padding with 0 for red and blue. */
699319251c5SThomas Zimmermann 		for (i = OFDRM_GAMMA_LUT_SIZE / 8; i < OFDRM_GAMMA_LUT_SIZE / 4; i++) {
700319251c5SThomas Zimmermann 			unsigned char r = 0;
701319251c5SThomas Zimmermann 			unsigned char g = lut[i * 4 + i / 16].green >> 8;
702319251c5SThomas Zimmermann 			unsigned char b = 0;
703319251c5SThomas Zimmermann 
704319251c5SThomas Zimmermann 			odev->funcs->cmap_write(odev, i, r, g, b);
705319251c5SThomas Zimmermann 		}
706319251c5SThomas Zimmermann 		break;
707319251c5SThomas Zimmermann 	case DRM_FORMAT_XRGB8888:
708319251c5SThomas Zimmermann 	case DRM_FORMAT_BGRX8888:
709319251c5SThomas Zimmermann 		for (i = 0; i < OFDRM_GAMMA_LUT_SIZE; i++) {
710319251c5SThomas Zimmermann 			unsigned char r = lut[i].red >> 8;
711319251c5SThomas Zimmermann 			unsigned char g = lut[i].green >> 8;
712319251c5SThomas Zimmermann 			unsigned char b = lut[i].blue >> 8;
713319251c5SThomas Zimmermann 
714319251c5SThomas Zimmermann 			odev->funcs->cmap_write(odev, i, r, g, b);
715319251c5SThomas Zimmermann 		}
716319251c5SThomas Zimmermann 		break;
717319251c5SThomas Zimmermann 	default:
718319251c5SThomas Zimmermann 		drm_warn_once(dev, "Unsupported format %p4cc for gamma correction\n",
719319251c5SThomas Zimmermann 			      &format->format);
720319251c5SThomas Zimmermann 		break;
721319251c5SThomas Zimmermann 	}
722319251c5SThomas Zimmermann }
723319251c5SThomas Zimmermann 
724319251c5SThomas Zimmermann /*
725319251c5SThomas Zimmermann  * Modesetting
726319251c5SThomas Zimmermann  */
727319251c5SThomas Zimmermann 
728319251c5SThomas Zimmermann struct ofdrm_crtc_state {
729319251c5SThomas Zimmermann 	struct drm_crtc_state base;
730319251c5SThomas Zimmermann 
731319251c5SThomas Zimmermann 	/* Primary-plane format; required for color mgmt. */
732319251c5SThomas Zimmermann 	const struct drm_format_info *format;
733319251c5SThomas Zimmermann };
734319251c5SThomas Zimmermann 
735319251c5SThomas Zimmermann static struct ofdrm_crtc_state *to_ofdrm_crtc_state(struct drm_crtc_state *base)
736319251c5SThomas Zimmermann {
737319251c5SThomas Zimmermann 	return container_of(base, struct ofdrm_crtc_state, base);
738319251c5SThomas Zimmermann }
739319251c5SThomas Zimmermann 
740319251c5SThomas Zimmermann static void ofdrm_crtc_state_destroy(struct ofdrm_crtc_state *ofdrm_crtc_state)
741319251c5SThomas Zimmermann {
742319251c5SThomas Zimmermann 	__drm_atomic_helper_crtc_destroy_state(&ofdrm_crtc_state->base);
743319251c5SThomas Zimmermann 	kfree(ofdrm_crtc_state);
744319251c5SThomas Zimmermann }
745319251c5SThomas Zimmermann 
746319251c5SThomas Zimmermann static const uint64_t ofdrm_primary_plane_format_modifiers[] = {
747319251c5SThomas Zimmermann 	DRM_FORMAT_MOD_LINEAR,
748319251c5SThomas Zimmermann 	DRM_FORMAT_MOD_INVALID
749319251c5SThomas Zimmermann };
750319251c5SThomas Zimmermann 
751319251c5SThomas Zimmermann static int ofdrm_primary_plane_helper_atomic_check(struct drm_plane *plane,
752319251c5SThomas Zimmermann 						   struct drm_atomic_state *new_state)
753319251c5SThomas Zimmermann {
754319251c5SThomas Zimmermann 	struct drm_device *dev = plane->dev;
755b5626f6fSThomas Zimmermann 	struct drm_sysfb_device *sysfb = to_drm_sysfb_device(dev);
756319251c5SThomas Zimmermann 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(new_state, plane);
757319251c5SThomas Zimmermann 	struct drm_shadow_plane_state *new_shadow_plane_state =
758319251c5SThomas Zimmermann 		to_drm_shadow_plane_state(new_plane_state);
759319251c5SThomas Zimmermann 	struct drm_framebuffer *new_fb = new_plane_state->fb;
760319251c5SThomas Zimmermann 	struct drm_crtc *new_crtc = new_plane_state->crtc;
761319251c5SThomas Zimmermann 	struct drm_crtc_state *new_crtc_state = NULL;
762319251c5SThomas Zimmermann 	struct ofdrm_crtc_state *new_ofdrm_crtc_state;
763319251c5SThomas Zimmermann 	int ret;
764319251c5SThomas Zimmermann 
765319251c5SThomas Zimmermann 	if (new_crtc)
766319251c5SThomas Zimmermann 		new_crtc_state = drm_atomic_get_new_crtc_state(new_state, new_plane_state->crtc);
767319251c5SThomas Zimmermann 
768319251c5SThomas Zimmermann 	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
769319251c5SThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
770319251c5SThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
771319251c5SThomas Zimmermann 						  false, false);
772319251c5SThomas Zimmermann 	if (ret)
773319251c5SThomas Zimmermann 		return ret;
774319251c5SThomas Zimmermann 	else if (!new_plane_state->visible)
775319251c5SThomas Zimmermann 		return 0;
776319251c5SThomas Zimmermann 
777b5626f6fSThomas Zimmermann 	if (new_fb->format != sysfb->fb_format) {
778319251c5SThomas Zimmermann 		void *buf;
779319251c5SThomas Zimmermann 
780319251c5SThomas Zimmermann 		/* format conversion necessary; reserve buffer */
781319251c5SThomas Zimmermann 		buf = drm_format_conv_state_reserve(&new_shadow_plane_state->fmtcnv_state,
782b5626f6fSThomas Zimmermann 						    sysfb->fb_pitch, GFP_KERNEL);
783319251c5SThomas Zimmermann 		if (!buf)
784319251c5SThomas Zimmermann 			return -ENOMEM;
785319251c5SThomas Zimmermann 	}
786319251c5SThomas Zimmermann 
787319251c5SThomas Zimmermann 	new_crtc_state = drm_atomic_get_new_crtc_state(new_state, new_plane_state->crtc);
788319251c5SThomas Zimmermann 
789319251c5SThomas Zimmermann 	new_ofdrm_crtc_state = to_ofdrm_crtc_state(new_crtc_state);
790319251c5SThomas Zimmermann 	new_ofdrm_crtc_state->format = new_fb->format;
791319251c5SThomas Zimmermann 
792319251c5SThomas Zimmermann 	return 0;
793319251c5SThomas Zimmermann }
794319251c5SThomas Zimmermann 
795319251c5SThomas Zimmermann static void ofdrm_primary_plane_helper_atomic_update(struct drm_plane *plane,
796319251c5SThomas Zimmermann 						     struct drm_atomic_state *state)
797319251c5SThomas Zimmermann {
798319251c5SThomas Zimmermann 	struct drm_device *dev = plane->dev;
799b5626f6fSThomas Zimmermann 	struct drm_sysfb_device *sysfb = to_drm_sysfb_device(dev);
800319251c5SThomas Zimmermann 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
801319251c5SThomas Zimmermann 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
802319251c5SThomas Zimmermann 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
803319251c5SThomas Zimmermann 	struct drm_framebuffer *fb = plane_state->fb;
804b5626f6fSThomas Zimmermann 	unsigned int dst_pitch = sysfb->fb_pitch;
805b5626f6fSThomas Zimmermann 	const struct drm_format_info *dst_format = sysfb->fb_format;
806319251c5SThomas Zimmermann 	struct drm_atomic_helper_damage_iter iter;
807319251c5SThomas Zimmermann 	struct drm_rect damage;
808319251c5SThomas Zimmermann 	int ret, idx;
809319251c5SThomas Zimmermann 
810319251c5SThomas Zimmermann 	ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
811319251c5SThomas Zimmermann 	if (ret)
812319251c5SThomas Zimmermann 		return;
813319251c5SThomas Zimmermann 
814319251c5SThomas Zimmermann 	if (!drm_dev_enter(dev, &idx))
815319251c5SThomas Zimmermann 		goto out_drm_gem_fb_end_cpu_access;
816319251c5SThomas Zimmermann 
817319251c5SThomas Zimmermann 	drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
818319251c5SThomas Zimmermann 	drm_atomic_for_each_plane_damage(&iter, &damage) {
819b5626f6fSThomas Zimmermann 		struct iosys_map dst = sysfb->fb_addr;
820319251c5SThomas Zimmermann 		struct drm_rect dst_clip = plane_state->dst;
821319251c5SThomas Zimmermann 
822319251c5SThomas Zimmermann 		if (!drm_rect_intersect(&dst_clip, &damage))
823319251c5SThomas Zimmermann 			continue;
824319251c5SThomas Zimmermann 
825319251c5SThomas Zimmermann 		iosys_map_incr(&dst, drm_fb_clip_offset(dst_pitch, dst_format, &dst_clip));
826319251c5SThomas Zimmermann 		drm_fb_blit(&dst, &dst_pitch, dst_format->format, shadow_plane_state->data, fb,
827319251c5SThomas Zimmermann 			    &damage, &shadow_plane_state->fmtcnv_state);
828319251c5SThomas Zimmermann 	}
829319251c5SThomas Zimmermann 
830319251c5SThomas Zimmermann 	drm_dev_exit(idx);
831319251c5SThomas Zimmermann out_drm_gem_fb_end_cpu_access:
832319251c5SThomas Zimmermann 	drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
833319251c5SThomas Zimmermann }
834319251c5SThomas Zimmermann 
835319251c5SThomas Zimmermann static void ofdrm_primary_plane_helper_atomic_disable(struct drm_plane *plane,
836319251c5SThomas Zimmermann 						      struct drm_atomic_state *state)
837319251c5SThomas Zimmermann {
838319251c5SThomas Zimmermann 	struct drm_device *dev = plane->dev;
839b5626f6fSThomas Zimmermann 	struct drm_sysfb_device *sysfb = to_drm_sysfb_device(dev);
840b5626f6fSThomas Zimmermann 	struct iosys_map dst = sysfb->fb_addr;
841319251c5SThomas Zimmermann 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
842319251c5SThomas Zimmermann 	void __iomem *dst_vmap = dst.vaddr_iomem; /* TODO: Use mapping abstraction */
843b5626f6fSThomas Zimmermann 	unsigned int dst_pitch = sysfb->fb_pitch;
844b5626f6fSThomas Zimmermann 	const struct drm_format_info *dst_format = sysfb->fb_format;
845319251c5SThomas Zimmermann 	struct drm_rect dst_clip;
846319251c5SThomas Zimmermann 	unsigned long lines, linepixels, i;
847319251c5SThomas Zimmermann 	int idx;
848319251c5SThomas Zimmermann 
849319251c5SThomas Zimmermann 	drm_rect_init(&dst_clip,
850319251c5SThomas Zimmermann 		      plane_state->src_x >> 16, plane_state->src_y >> 16,
851319251c5SThomas Zimmermann 		      plane_state->src_w >> 16, plane_state->src_h >> 16);
852319251c5SThomas Zimmermann 
853319251c5SThomas Zimmermann 	lines = drm_rect_height(&dst_clip);
854319251c5SThomas Zimmermann 	linepixels = drm_rect_width(&dst_clip);
855319251c5SThomas Zimmermann 
856319251c5SThomas Zimmermann 	if (!drm_dev_enter(dev, &idx))
857319251c5SThomas Zimmermann 		return;
858319251c5SThomas Zimmermann 
859319251c5SThomas Zimmermann 	/* Clear buffer to black if disabled */
860319251c5SThomas Zimmermann 	dst_vmap += drm_fb_clip_offset(dst_pitch, dst_format, &dst_clip);
861319251c5SThomas Zimmermann 	for (i = 0; i < lines; ++i) {
862319251c5SThomas Zimmermann 		memset_io(dst_vmap, 0, linepixels * dst_format->cpp[0]);
863319251c5SThomas Zimmermann 		dst_vmap += dst_pitch;
864319251c5SThomas Zimmermann 	}
865319251c5SThomas Zimmermann 
866319251c5SThomas Zimmermann 	drm_dev_exit(idx);
867319251c5SThomas Zimmermann }
868319251c5SThomas Zimmermann 
869319251c5SThomas Zimmermann static const struct drm_plane_helper_funcs ofdrm_primary_plane_helper_funcs = {
870319251c5SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
871319251c5SThomas Zimmermann 	.atomic_check = ofdrm_primary_plane_helper_atomic_check,
872319251c5SThomas Zimmermann 	.atomic_update = ofdrm_primary_plane_helper_atomic_update,
873319251c5SThomas Zimmermann 	.atomic_disable = ofdrm_primary_plane_helper_atomic_disable,
874319251c5SThomas Zimmermann };
875319251c5SThomas Zimmermann 
876319251c5SThomas Zimmermann static const struct drm_plane_funcs ofdrm_primary_plane_funcs = {
877319251c5SThomas Zimmermann 	.update_plane = drm_atomic_helper_update_plane,
878319251c5SThomas Zimmermann 	.disable_plane = drm_atomic_helper_disable_plane,
879319251c5SThomas Zimmermann 	.destroy = drm_plane_cleanup,
880319251c5SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_FUNCS,
881319251c5SThomas Zimmermann };
882319251c5SThomas Zimmermann 
883319251c5SThomas Zimmermann static enum drm_mode_status ofdrm_crtc_helper_mode_valid(struct drm_crtc *crtc,
884319251c5SThomas Zimmermann 							 const struct drm_display_mode *mode)
885319251c5SThomas Zimmermann {
886b5626f6fSThomas Zimmermann 	struct drm_sysfb_device *sysfb = to_drm_sysfb_device(crtc->dev);
887319251c5SThomas Zimmermann 
888b5626f6fSThomas Zimmermann 	return drm_crtc_helper_mode_valid_fixed(crtc, mode, &sysfb->fb_mode);
889319251c5SThomas Zimmermann }
890319251c5SThomas Zimmermann 
891319251c5SThomas Zimmermann static int ofdrm_crtc_helper_atomic_check(struct drm_crtc *crtc,
892319251c5SThomas Zimmermann 					  struct drm_atomic_state *new_state)
893319251c5SThomas Zimmermann {
894319251c5SThomas Zimmermann 	static const size_t gamma_lut_length = OFDRM_GAMMA_LUT_SIZE * sizeof(struct drm_color_lut);
895319251c5SThomas Zimmermann 
896319251c5SThomas Zimmermann 	struct drm_device *dev = crtc->dev;
897319251c5SThomas Zimmermann 	struct drm_crtc_state *new_crtc_state = drm_atomic_get_new_crtc_state(new_state, crtc);
898319251c5SThomas Zimmermann 	int ret;
899319251c5SThomas Zimmermann 
900319251c5SThomas Zimmermann 	if (!new_crtc_state->enable)
901319251c5SThomas Zimmermann 		return 0;
902319251c5SThomas Zimmermann 
903319251c5SThomas Zimmermann 	ret = drm_atomic_helper_check_crtc_primary_plane(new_crtc_state);
904319251c5SThomas Zimmermann 	if (ret)
905319251c5SThomas Zimmermann 		return ret;
906319251c5SThomas Zimmermann 
907319251c5SThomas Zimmermann 	if (new_crtc_state->color_mgmt_changed) {
908319251c5SThomas Zimmermann 		struct drm_property_blob *gamma_lut = new_crtc_state->gamma_lut;
909319251c5SThomas Zimmermann 
910319251c5SThomas Zimmermann 		if (gamma_lut && (gamma_lut->length != gamma_lut_length)) {
911319251c5SThomas Zimmermann 			drm_dbg(dev, "Incorrect gamma_lut length %zu\n", gamma_lut->length);
912319251c5SThomas Zimmermann 			return -EINVAL;
913319251c5SThomas Zimmermann 		}
914319251c5SThomas Zimmermann 	}
915319251c5SThomas Zimmermann 
916319251c5SThomas Zimmermann 	return 0;
917319251c5SThomas Zimmermann }
918319251c5SThomas Zimmermann 
919319251c5SThomas Zimmermann static void ofdrm_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_state *state)
920319251c5SThomas Zimmermann {
921319251c5SThomas Zimmermann 	struct ofdrm_device *odev = ofdrm_device_of_dev(crtc->dev);
922319251c5SThomas Zimmermann 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
923319251c5SThomas Zimmermann 	struct ofdrm_crtc_state *ofdrm_crtc_state = to_ofdrm_crtc_state(crtc_state);
924319251c5SThomas Zimmermann 
925319251c5SThomas Zimmermann 	if (crtc_state->enable && crtc_state->color_mgmt_changed) {
926319251c5SThomas Zimmermann 		const struct drm_format_info *format = ofdrm_crtc_state->format;
927319251c5SThomas Zimmermann 
928319251c5SThomas Zimmermann 		if (crtc_state->gamma_lut)
929319251c5SThomas Zimmermann 			ofdrm_device_set_gamma(odev, format, crtc_state->gamma_lut->data);
930319251c5SThomas Zimmermann 		else
931319251c5SThomas Zimmermann 			ofdrm_device_set_gamma_linear(odev, format);
932319251c5SThomas Zimmermann 	}
933319251c5SThomas Zimmermann }
934319251c5SThomas Zimmermann 
935319251c5SThomas Zimmermann /*
936319251c5SThomas Zimmermann  * The CRTC is always enabled. Screen updates are performed by
937319251c5SThomas Zimmermann  * the primary plane's atomic_update function. Disabling clears
938319251c5SThomas Zimmermann  * the screen in the primary plane's atomic_disable function.
939319251c5SThomas Zimmermann  */
940319251c5SThomas Zimmermann static const struct drm_crtc_helper_funcs ofdrm_crtc_helper_funcs = {
941319251c5SThomas Zimmermann 	.mode_valid = ofdrm_crtc_helper_mode_valid,
942319251c5SThomas Zimmermann 	.atomic_check = ofdrm_crtc_helper_atomic_check,
943319251c5SThomas Zimmermann 	.atomic_flush = ofdrm_crtc_helper_atomic_flush,
944319251c5SThomas Zimmermann };
945319251c5SThomas Zimmermann 
946319251c5SThomas Zimmermann static void ofdrm_crtc_reset(struct drm_crtc *crtc)
947319251c5SThomas Zimmermann {
948319251c5SThomas Zimmermann 	struct ofdrm_crtc_state *ofdrm_crtc_state =
949319251c5SThomas Zimmermann 		kzalloc(sizeof(*ofdrm_crtc_state), GFP_KERNEL);
950319251c5SThomas Zimmermann 
951319251c5SThomas Zimmermann 	if (crtc->state)
952319251c5SThomas Zimmermann 		ofdrm_crtc_state_destroy(to_ofdrm_crtc_state(crtc->state));
953319251c5SThomas Zimmermann 
954319251c5SThomas Zimmermann 	if (ofdrm_crtc_state)
955319251c5SThomas Zimmermann 		__drm_atomic_helper_crtc_reset(crtc, &ofdrm_crtc_state->base);
956319251c5SThomas Zimmermann 	else
957319251c5SThomas Zimmermann 		__drm_atomic_helper_crtc_reset(crtc, NULL);
958319251c5SThomas Zimmermann }
959319251c5SThomas Zimmermann 
960319251c5SThomas Zimmermann static struct drm_crtc_state *ofdrm_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
961319251c5SThomas Zimmermann {
962319251c5SThomas Zimmermann 	struct drm_device *dev = crtc->dev;
963319251c5SThomas Zimmermann 	struct drm_crtc_state *crtc_state = crtc->state;
964319251c5SThomas Zimmermann 	struct ofdrm_crtc_state *new_ofdrm_crtc_state;
965319251c5SThomas Zimmermann 	struct ofdrm_crtc_state *ofdrm_crtc_state;
966319251c5SThomas Zimmermann 
967319251c5SThomas Zimmermann 	if (drm_WARN_ON(dev, !crtc_state))
968319251c5SThomas Zimmermann 		return NULL;
969319251c5SThomas Zimmermann 
970319251c5SThomas Zimmermann 	new_ofdrm_crtc_state = kzalloc(sizeof(*new_ofdrm_crtc_state), GFP_KERNEL);
971319251c5SThomas Zimmermann 	if (!new_ofdrm_crtc_state)
972319251c5SThomas Zimmermann 		return NULL;
973319251c5SThomas Zimmermann 
974319251c5SThomas Zimmermann 	ofdrm_crtc_state = to_ofdrm_crtc_state(crtc_state);
975319251c5SThomas Zimmermann 
976319251c5SThomas Zimmermann 	__drm_atomic_helper_crtc_duplicate_state(crtc, &new_ofdrm_crtc_state->base);
977319251c5SThomas Zimmermann 	new_ofdrm_crtc_state->format = ofdrm_crtc_state->format;
978319251c5SThomas Zimmermann 
979319251c5SThomas Zimmermann 	return &new_ofdrm_crtc_state->base;
980319251c5SThomas Zimmermann }
981319251c5SThomas Zimmermann 
982319251c5SThomas Zimmermann static void ofdrm_crtc_atomic_destroy_state(struct drm_crtc *crtc,
983319251c5SThomas Zimmermann 					    struct drm_crtc_state *crtc_state)
984319251c5SThomas Zimmermann {
985319251c5SThomas Zimmermann 	ofdrm_crtc_state_destroy(to_ofdrm_crtc_state(crtc_state));
986319251c5SThomas Zimmermann }
987319251c5SThomas Zimmermann 
988319251c5SThomas Zimmermann static const struct drm_crtc_funcs ofdrm_crtc_funcs = {
989319251c5SThomas Zimmermann 	.reset = ofdrm_crtc_reset,
990319251c5SThomas Zimmermann 	.destroy = drm_crtc_cleanup,
991319251c5SThomas Zimmermann 	.set_config = drm_atomic_helper_set_config,
992319251c5SThomas Zimmermann 	.page_flip = drm_atomic_helper_page_flip,
993319251c5SThomas Zimmermann 	.atomic_duplicate_state = ofdrm_crtc_atomic_duplicate_state,
994319251c5SThomas Zimmermann 	.atomic_destroy_state = ofdrm_crtc_atomic_destroy_state,
995319251c5SThomas Zimmermann };
996319251c5SThomas Zimmermann 
997319251c5SThomas Zimmermann static const struct drm_encoder_funcs ofdrm_encoder_funcs = {
998319251c5SThomas Zimmermann 	.destroy = drm_encoder_cleanup,
999319251c5SThomas Zimmermann };
1000319251c5SThomas Zimmermann 
1001319251c5SThomas Zimmermann static int ofdrm_connector_helper_get_modes(struct drm_connector *connector)
1002319251c5SThomas Zimmermann {
1003b5626f6fSThomas Zimmermann 	struct drm_sysfb_device *sysfb = to_drm_sysfb_device(connector->dev);
1004319251c5SThomas Zimmermann 
1005b5626f6fSThomas Zimmermann 	return drm_connector_helper_get_modes_fixed(connector, &sysfb->fb_mode);
1006319251c5SThomas Zimmermann }
1007319251c5SThomas Zimmermann 
1008319251c5SThomas Zimmermann static const struct drm_connector_helper_funcs ofdrm_connector_helper_funcs = {
1009319251c5SThomas Zimmermann 	.get_modes = ofdrm_connector_helper_get_modes,
1010319251c5SThomas Zimmermann };
1011319251c5SThomas Zimmermann 
1012319251c5SThomas Zimmermann static const struct drm_connector_funcs ofdrm_connector_funcs = {
1013319251c5SThomas Zimmermann 	.reset = drm_atomic_helper_connector_reset,
1014319251c5SThomas Zimmermann 	.fill_modes = drm_helper_probe_single_connector_modes,
1015319251c5SThomas Zimmermann 	.destroy = drm_connector_cleanup,
1016319251c5SThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1017319251c5SThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1018319251c5SThomas Zimmermann };
1019319251c5SThomas Zimmermann 
1020319251c5SThomas Zimmermann static const struct drm_mode_config_funcs ofdrm_mode_config_funcs = {
1021*559d105fSThomas Zimmermann 	DRM_SYSFB_MODE_CONFIG_FUNCS,
1022319251c5SThomas Zimmermann };
1023319251c5SThomas Zimmermann 
1024319251c5SThomas Zimmermann /*
1025319251c5SThomas Zimmermann  * Init / Cleanup
1026319251c5SThomas Zimmermann  */
1027319251c5SThomas Zimmermann 
1028319251c5SThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_unknown_device_funcs = {
1029319251c5SThomas Zimmermann };
1030319251c5SThomas Zimmermann 
1031319251c5SThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_mach64_device_funcs = {
1032319251c5SThomas Zimmermann 	.cmap_ioremap = ofdrm_mach64_cmap_ioremap,
1033319251c5SThomas Zimmermann 	.cmap_write = ofdrm_mach64_cmap_write,
1034319251c5SThomas Zimmermann };
1035319251c5SThomas Zimmermann 
1036319251c5SThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_rage128_device_funcs = {
1037319251c5SThomas Zimmermann 	.cmap_ioremap = ofdrm_rage128_cmap_ioremap,
1038319251c5SThomas Zimmermann 	.cmap_write = ofdrm_rage128_cmap_write,
1039319251c5SThomas Zimmermann };
1040319251c5SThomas Zimmermann 
1041319251c5SThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_rage_m3a_device_funcs = {
1042319251c5SThomas Zimmermann 	.cmap_ioremap = ofdrm_rage_m3a_cmap_ioremap,
1043319251c5SThomas Zimmermann 	.cmap_write = ofdrm_rage_m3a_cmap_write,
1044319251c5SThomas Zimmermann };
1045319251c5SThomas Zimmermann 
1046319251c5SThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_rage_m3b_device_funcs = {
1047319251c5SThomas Zimmermann 	.cmap_ioremap = ofdrm_rage_m3b_cmap_ioremap,
1048319251c5SThomas Zimmermann 	.cmap_write = ofdrm_rage_m3b_cmap_write,
1049319251c5SThomas Zimmermann };
1050319251c5SThomas Zimmermann 
1051319251c5SThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_radeon_device_funcs = {
1052319251c5SThomas Zimmermann 	.cmap_ioremap = ofdrm_radeon_cmap_ioremap,
1053319251c5SThomas Zimmermann 	.cmap_write = ofdrm_rage128_cmap_write, /* same as Rage128 */
1054319251c5SThomas Zimmermann };
1055319251c5SThomas Zimmermann 
1056319251c5SThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_gxt2000_device_funcs = {
1057319251c5SThomas Zimmermann 	.cmap_ioremap = ofdrm_gxt2000_cmap_ioremap,
1058319251c5SThomas Zimmermann 	.cmap_write = ofdrm_gxt2000_cmap_write,
1059319251c5SThomas Zimmermann };
1060319251c5SThomas Zimmermann 
1061319251c5SThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_avivo_device_funcs = {
1062319251c5SThomas Zimmermann 	.cmap_ioremap = ofdrm_avivo_cmap_ioremap,
1063319251c5SThomas Zimmermann 	.cmap_write = ofdrm_avivo_cmap_write,
1064319251c5SThomas Zimmermann };
1065319251c5SThomas Zimmermann 
1066319251c5SThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_qemu_device_funcs = {
1067319251c5SThomas Zimmermann 	.cmap_ioremap = ofdrm_qemu_cmap_ioremap,
1068319251c5SThomas Zimmermann 	.cmap_write = ofdrm_qemu_cmap_write,
1069319251c5SThomas Zimmermann };
1070319251c5SThomas Zimmermann 
1071319251c5SThomas Zimmermann static struct ofdrm_device *ofdrm_device_create(struct drm_driver *drv,
1072319251c5SThomas Zimmermann 						struct platform_device *pdev)
1073319251c5SThomas Zimmermann {
1074319251c5SThomas Zimmermann 	struct device_node *of_node = pdev->dev.of_node;
1075319251c5SThomas Zimmermann 	struct ofdrm_device *odev;
1076b5626f6fSThomas Zimmermann 	struct drm_sysfb_device *sysfb;
1077319251c5SThomas Zimmermann 	struct drm_device *dev;
1078319251c5SThomas Zimmermann 	enum ofdrm_model model;
1079319251c5SThomas Zimmermann 	bool big_endian;
1080319251c5SThomas Zimmermann 	int width, height, depth, linebytes;
1081319251c5SThomas Zimmermann 	const struct drm_format_info *format;
1082319251c5SThomas Zimmermann 	u64 address;
1083319251c5SThomas Zimmermann 	resource_size_t fb_size, fb_base, fb_pgbase, fb_pgsize;
1084319251c5SThomas Zimmermann 	struct resource *res, *mem;
1085319251c5SThomas Zimmermann 	void __iomem *screen_base;
1086319251c5SThomas Zimmermann 	struct drm_plane *primary_plane;
1087319251c5SThomas Zimmermann 	struct drm_crtc *crtc;
1088319251c5SThomas Zimmermann 	struct drm_encoder *encoder;
1089319251c5SThomas Zimmermann 	struct drm_connector *connector;
1090319251c5SThomas Zimmermann 	unsigned long max_width, max_height;
1091319251c5SThomas Zimmermann 	size_t nformats;
1092319251c5SThomas Zimmermann 	int ret;
1093319251c5SThomas Zimmermann 
1094b5626f6fSThomas Zimmermann 	odev = devm_drm_dev_alloc(&pdev->dev, drv, struct ofdrm_device, sysfb.dev);
1095319251c5SThomas Zimmermann 	if (IS_ERR(odev))
1096319251c5SThomas Zimmermann 		return ERR_CAST(odev);
1097b5626f6fSThomas Zimmermann 	sysfb = &odev->sysfb;
1098b5626f6fSThomas Zimmermann 	dev = &sysfb->dev;
1099319251c5SThomas Zimmermann 	platform_set_drvdata(pdev, dev);
1100319251c5SThomas Zimmermann 
1101319251c5SThomas Zimmermann 	ret = ofdrm_device_init_pci(odev);
1102319251c5SThomas Zimmermann 	if (ret)
1103319251c5SThomas Zimmermann 		return ERR_PTR(ret);
1104319251c5SThomas Zimmermann 
1105319251c5SThomas Zimmermann 	/*
1106319251c5SThomas Zimmermann 	 * OF display-node settings
1107319251c5SThomas Zimmermann 	 */
1108319251c5SThomas Zimmermann 
1109319251c5SThomas Zimmermann 	model = display_get_model_of(dev, of_node);
1110319251c5SThomas Zimmermann 	drm_dbg(dev, "detected model %d\n", model);
1111319251c5SThomas Zimmermann 
1112319251c5SThomas Zimmermann 	switch (model) {
1113319251c5SThomas Zimmermann 	case OFDRM_MODEL_UNKNOWN:
1114319251c5SThomas Zimmermann 		odev->funcs = &ofdrm_unknown_device_funcs;
1115319251c5SThomas Zimmermann 		break;
1116319251c5SThomas Zimmermann 	case OFDRM_MODEL_MACH64:
1117319251c5SThomas Zimmermann 		odev->funcs = &ofdrm_mach64_device_funcs;
1118319251c5SThomas Zimmermann 		break;
1119319251c5SThomas Zimmermann 	case OFDRM_MODEL_RAGE128:
1120319251c5SThomas Zimmermann 		odev->funcs = &ofdrm_rage128_device_funcs;
1121319251c5SThomas Zimmermann 		break;
1122319251c5SThomas Zimmermann 	case OFDRM_MODEL_RAGE_M3A:
1123319251c5SThomas Zimmermann 		odev->funcs = &ofdrm_rage_m3a_device_funcs;
1124319251c5SThomas Zimmermann 		break;
1125319251c5SThomas Zimmermann 	case OFDRM_MODEL_RAGE_M3B:
1126319251c5SThomas Zimmermann 		odev->funcs = &ofdrm_rage_m3b_device_funcs;
1127319251c5SThomas Zimmermann 		break;
1128319251c5SThomas Zimmermann 	case OFDRM_MODEL_RADEON:
1129319251c5SThomas Zimmermann 		odev->funcs = &ofdrm_radeon_device_funcs;
1130319251c5SThomas Zimmermann 		break;
1131319251c5SThomas Zimmermann 	case OFDRM_MODEL_GXT2000:
1132319251c5SThomas Zimmermann 		odev->funcs = &ofdrm_gxt2000_device_funcs;
1133319251c5SThomas Zimmermann 		break;
1134319251c5SThomas Zimmermann 	case OFDRM_MODEL_AVIVO:
1135319251c5SThomas Zimmermann 		odev->funcs = &ofdrm_avivo_device_funcs;
1136319251c5SThomas Zimmermann 		break;
1137319251c5SThomas Zimmermann 	case OFDRM_MODEL_QEMU:
1138319251c5SThomas Zimmermann 		odev->funcs = &ofdrm_qemu_device_funcs;
1139319251c5SThomas Zimmermann 		break;
1140319251c5SThomas Zimmermann 	}
1141319251c5SThomas Zimmermann 
1142319251c5SThomas Zimmermann 	big_endian = display_get_big_endian_of(dev, of_node);
1143319251c5SThomas Zimmermann 
1144319251c5SThomas Zimmermann 	width = display_get_width_of(dev, of_node);
1145319251c5SThomas Zimmermann 	if (width < 0)
1146319251c5SThomas Zimmermann 		return ERR_PTR(width);
1147319251c5SThomas Zimmermann 	height = display_get_height_of(dev, of_node);
1148319251c5SThomas Zimmermann 	if (height < 0)
1149319251c5SThomas Zimmermann 		return ERR_PTR(height);
1150319251c5SThomas Zimmermann 	depth = display_get_depth_of(dev, of_node);
1151319251c5SThomas Zimmermann 	if (depth < 0)
1152319251c5SThomas Zimmermann 		return ERR_PTR(depth);
1153319251c5SThomas Zimmermann 	linebytes = display_get_linebytes_of(dev, of_node);
1154319251c5SThomas Zimmermann 	if (linebytes < 0)
1155319251c5SThomas Zimmermann 		return ERR_PTR(linebytes);
1156319251c5SThomas Zimmermann 
1157319251c5SThomas Zimmermann 	format = display_get_validated_format(dev, depth, big_endian);
1158319251c5SThomas Zimmermann 	if (IS_ERR(format))
1159319251c5SThomas Zimmermann 		return ERR_CAST(format);
1160319251c5SThomas Zimmermann 	if (!linebytes) {
1161319251c5SThomas Zimmermann 		linebytes = drm_format_info_min_pitch(format, 0, width);
1162319251c5SThomas Zimmermann 		if (drm_WARN_ON(dev, !linebytes))
1163319251c5SThomas Zimmermann 			return ERR_PTR(-EINVAL);
1164319251c5SThomas Zimmermann 	}
1165319251c5SThomas Zimmermann 
1166319251c5SThomas Zimmermann 	fb_size = linebytes * height;
1167319251c5SThomas Zimmermann 
1168319251c5SThomas Zimmermann 	/*
1169319251c5SThomas Zimmermann 	 * Try to figure out the address of the framebuffer. Unfortunately, Open
1170319251c5SThomas Zimmermann 	 * Firmware doesn't provide a standard way to do so. All we can do is a
1171319251c5SThomas Zimmermann 	 * dodgy heuristic that happens to work in practice.
1172319251c5SThomas Zimmermann 	 *
1173319251c5SThomas Zimmermann 	 * On most machines, the "address" property contains what we need, though
1174319251c5SThomas Zimmermann 	 * not on Matrox cards found in IBM machines. What appears to give good
1175319251c5SThomas Zimmermann 	 * results is to go through the PCI ranges and pick one that encloses the
1176319251c5SThomas Zimmermann 	 * "address" property. If none match, we pick the largest.
1177319251c5SThomas Zimmermann 	 */
1178319251c5SThomas Zimmermann 	address = display_get_address_of(dev, of_node);
1179319251c5SThomas Zimmermann 	if (address != OF_BAD_ADDR) {
1180319251c5SThomas Zimmermann 		struct resource fb_res = DEFINE_RES_MEM(address, fb_size);
1181319251c5SThomas Zimmermann 
1182319251c5SThomas Zimmermann 		res = ofdrm_find_fb_resource(odev, &fb_res);
1183319251c5SThomas Zimmermann 		if (!res)
1184319251c5SThomas Zimmermann 			return ERR_PTR(-EINVAL);
1185319251c5SThomas Zimmermann 		if (resource_contains(res, &fb_res))
1186319251c5SThomas Zimmermann 			fb_base = address;
1187319251c5SThomas Zimmermann 		else
1188319251c5SThomas Zimmermann 			fb_base = res->start;
1189319251c5SThomas Zimmermann 	} else {
1190319251c5SThomas Zimmermann 		struct resource fb_res = DEFINE_RES_MEM(0u, fb_size);
1191319251c5SThomas Zimmermann 
1192319251c5SThomas Zimmermann 		res = ofdrm_find_fb_resource(odev, &fb_res);
1193319251c5SThomas Zimmermann 		if (!res)
1194319251c5SThomas Zimmermann 			return ERR_PTR(-EINVAL);
1195319251c5SThomas Zimmermann 		fb_base = res->start;
1196319251c5SThomas Zimmermann 	}
1197319251c5SThomas Zimmermann 
1198319251c5SThomas Zimmermann 	/*
1199319251c5SThomas Zimmermann 	 * I/O resources
1200319251c5SThomas Zimmermann 	 */
1201319251c5SThomas Zimmermann 
1202319251c5SThomas Zimmermann 	fb_pgbase = round_down(fb_base, PAGE_SIZE);
1203319251c5SThomas Zimmermann 	fb_pgsize = fb_base - fb_pgbase + round_up(fb_size, PAGE_SIZE);
1204319251c5SThomas Zimmermann 
1205319251c5SThomas Zimmermann 	ret = devm_aperture_acquire_for_platform_device(pdev, fb_pgbase, fb_pgsize);
1206319251c5SThomas Zimmermann 	if (ret) {
1207319251c5SThomas Zimmermann 		drm_err(dev, "could not acquire memory range %pr: error %d\n", &res, ret);
1208319251c5SThomas Zimmermann 		return ERR_PTR(ret);
1209319251c5SThomas Zimmermann 	}
1210319251c5SThomas Zimmermann 
1211319251c5SThomas Zimmermann 	mem = devm_request_mem_region(&pdev->dev, fb_pgbase, fb_pgsize, drv->name);
1212319251c5SThomas Zimmermann 	if (!mem) {
1213319251c5SThomas Zimmermann 		drm_warn(dev, "could not acquire memory region %pr\n", &res);
1214319251c5SThomas Zimmermann 		return ERR_PTR(-ENOMEM);
1215319251c5SThomas Zimmermann 	}
1216319251c5SThomas Zimmermann 
1217319251c5SThomas Zimmermann 	screen_base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
1218319251c5SThomas Zimmermann 	if (!screen_base)
1219319251c5SThomas Zimmermann 		return ERR_PTR(-ENOMEM);
1220319251c5SThomas Zimmermann 
1221319251c5SThomas Zimmermann 	if (odev->funcs->cmap_ioremap) {
1222319251c5SThomas Zimmermann 		void __iomem *cmap_base = odev->funcs->cmap_ioremap(odev, of_node, fb_base);
1223319251c5SThomas Zimmermann 
1224319251c5SThomas Zimmermann 		if (IS_ERR(cmap_base)) {
1225319251c5SThomas Zimmermann 			/* Don't fail; continue without colormap */
1226319251c5SThomas Zimmermann 			drm_warn(dev, "could not find colormap: error %ld\n", PTR_ERR(cmap_base));
1227319251c5SThomas Zimmermann 		} else {
1228319251c5SThomas Zimmermann 			odev->cmap_base = cmap_base;
1229319251c5SThomas Zimmermann 		}
1230319251c5SThomas Zimmermann 	}
1231319251c5SThomas Zimmermann 
1232319251c5SThomas Zimmermann 	/*
1233319251c5SThomas Zimmermann 	 * Firmware framebuffer
1234319251c5SThomas Zimmermann 	 */
1235319251c5SThomas Zimmermann 
1236b5626f6fSThomas Zimmermann 	iosys_map_set_vaddr_iomem(&sysfb->fb_addr, screen_base);
1237333376e9SThomas Zimmermann 	sysfb->fb_mode = drm_sysfb_mode(width, height, 0, 0);
1238b5626f6fSThomas Zimmermann 	sysfb->fb_format = format;
1239b5626f6fSThomas Zimmermann 	sysfb->fb_pitch = linebytes;
1240319251c5SThomas Zimmermann 
1241b5626f6fSThomas Zimmermann 	drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&sysfb->fb_mode));
1242319251c5SThomas Zimmermann 	drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, linebytes=%d byte\n",
1243319251c5SThomas Zimmermann 		&format->format, width, height, linebytes);
1244319251c5SThomas Zimmermann 
1245319251c5SThomas Zimmermann 	/*
1246319251c5SThomas Zimmermann 	 * Mode-setting pipeline
1247319251c5SThomas Zimmermann 	 */
1248319251c5SThomas Zimmermann 
1249319251c5SThomas Zimmermann 	ret = drmm_mode_config_init(dev);
1250319251c5SThomas Zimmermann 	if (ret)
1251319251c5SThomas Zimmermann 		return ERR_PTR(ret);
1252319251c5SThomas Zimmermann 
1253319251c5SThomas Zimmermann 	max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH);
1254319251c5SThomas Zimmermann 	max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT);
1255319251c5SThomas Zimmermann 
1256319251c5SThomas Zimmermann 	dev->mode_config.min_width = width;
1257319251c5SThomas Zimmermann 	dev->mode_config.max_width = max_width;
1258319251c5SThomas Zimmermann 	dev->mode_config.min_height = height;
1259319251c5SThomas Zimmermann 	dev->mode_config.max_height = max_height;
1260319251c5SThomas Zimmermann 	dev->mode_config.funcs = &ofdrm_mode_config_funcs;
1261319251c5SThomas Zimmermann 	dev->mode_config.preferred_depth = format->depth;
1262319251c5SThomas Zimmermann 	dev->mode_config.quirk_addfb_prefer_host_byte_order = true;
1263319251c5SThomas Zimmermann 
1264319251c5SThomas Zimmermann 	/* Primary plane */
1265319251c5SThomas Zimmermann 
1266319251c5SThomas Zimmermann 	nformats = drm_fb_build_fourcc_list(dev, &format->format, 1,
1267319251c5SThomas Zimmermann 					    odev->formats, ARRAY_SIZE(odev->formats));
1268319251c5SThomas Zimmermann 
1269319251c5SThomas Zimmermann 	primary_plane = &odev->primary_plane;
1270319251c5SThomas Zimmermann 	ret = drm_universal_plane_init(dev, primary_plane, 0, &ofdrm_primary_plane_funcs,
1271319251c5SThomas Zimmermann 				       odev->formats, nformats,
1272319251c5SThomas Zimmermann 				       ofdrm_primary_plane_format_modifiers,
1273319251c5SThomas Zimmermann 				       DRM_PLANE_TYPE_PRIMARY, NULL);
1274319251c5SThomas Zimmermann 	if (ret)
1275319251c5SThomas Zimmermann 		return ERR_PTR(ret);
1276319251c5SThomas Zimmermann 	drm_plane_helper_add(primary_plane, &ofdrm_primary_plane_helper_funcs);
1277319251c5SThomas Zimmermann 	drm_plane_enable_fb_damage_clips(primary_plane);
1278319251c5SThomas Zimmermann 
1279319251c5SThomas Zimmermann 	/* CRTC */
1280319251c5SThomas Zimmermann 
1281319251c5SThomas Zimmermann 	crtc = &odev->crtc;
1282319251c5SThomas Zimmermann 	ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
1283319251c5SThomas Zimmermann 					&ofdrm_crtc_funcs, NULL);
1284319251c5SThomas Zimmermann 	if (ret)
1285319251c5SThomas Zimmermann 		return ERR_PTR(ret);
1286319251c5SThomas Zimmermann 	drm_crtc_helper_add(crtc, &ofdrm_crtc_helper_funcs);
1287319251c5SThomas Zimmermann 
1288319251c5SThomas Zimmermann 	if (odev->cmap_base) {
1289319251c5SThomas Zimmermann 		drm_mode_crtc_set_gamma_size(crtc, OFDRM_GAMMA_LUT_SIZE);
1290319251c5SThomas Zimmermann 		drm_crtc_enable_color_mgmt(crtc, 0, false, OFDRM_GAMMA_LUT_SIZE);
1291319251c5SThomas Zimmermann 	}
1292319251c5SThomas Zimmermann 
1293319251c5SThomas Zimmermann 	/* Encoder */
1294319251c5SThomas Zimmermann 
1295319251c5SThomas Zimmermann 	encoder = &odev->encoder;
1296319251c5SThomas Zimmermann 	ret = drm_encoder_init(dev, encoder, &ofdrm_encoder_funcs, DRM_MODE_ENCODER_NONE, NULL);
1297319251c5SThomas Zimmermann 	if (ret)
1298319251c5SThomas Zimmermann 		return ERR_PTR(ret);
1299319251c5SThomas Zimmermann 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1300319251c5SThomas Zimmermann 
1301319251c5SThomas Zimmermann 	/* Connector */
1302319251c5SThomas Zimmermann 
1303319251c5SThomas Zimmermann 	connector = &odev->connector;
1304319251c5SThomas Zimmermann 	ret = drm_connector_init(dev, connector, &ofdrm_connector_funcs,
1305319251c5SThomas Zimmermann 				 DRM_MODE_CONNECTOR_Unknown);
1306319251c5SThomas Zimmermann 	if (ret)
1307319251c5SThomas Zimmermann 		return ERR_PTR(ret);
1308319251c5SThomas Zimmermann 	drm_connector_helper_add(connector, &ofdrm_connector_helper_funcs);
1309319251c5SThomas Zimmermann 	drm_connector_set_panel_orientation_with_quirk(connector,
1310319251c5SThomas Zimmermann 						       DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
1311319251c5SThomas Zimmermann 						       width, height);
1312319251c5SThomas Zimmermann 
1313319251c5SThomas Zimmermann 	ret = drm_connector_attach_encoder(connector, encoder);
1314319251c5SThomas Zimmermann 	if (ret)
1315319251c5SThomas Zimmermann 		return ERR_PTR(ret);
1316319251c5SThomas Zimmermann 
1317319251c5SThomas Zimmermann 	drm_mode_config_reset(dev);
1318319251c5SThomas Zimmermann 
1319319251c5SThomas Zimmermann 	return odev;
1320319251c5SThomas Zimmermann }
1321319251c5SThomas Zimmermann 
1322319251c5SThomas Zimmermann /*
1323319251c5SThomas Zimmermann  * DRM driver
1324319251c5SThomas Zimmermann  */
1325319251c5SThomas Zimmermann 
1326319251c5SThomas Zimmermann DEFINE_DRM_GEM_FOPS(ofdrm_fops);
1327319251c5SThomas Zimmermann 
1328319251c5SThomas Zimmermann static struct drm_driver ofdrm_driver = {
1329319251c5SThomas Zimmermann 	DRM_GEM_SHMEM_DRIVER_OPS,
1330319251c5SThomas Zimmermann 	DRM_FBDEV_SHMEM_DRIVER_OPS,
1331319251c5SThomas Zimmermann 	.name			= DRIVER_NAME,
1332319251c5SThomas Zimmermann 	.desc			= DRIVER_DESC,
1333319251c5SThomas Zimmermann 	.major			= DRIVER_MAJOR,
1334319251c5SThomas Zimmermann 	.minor			= DRIVER_MINOR,
1335319251c5SThomas Zimmermann 	.driver_features	= DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
1336319251c5SThomas Zimmermann 	.fops			= &ofdrm_fops,
1337319251c5SThomas Zimmermann };
1338319251c5SThomas Zimmermann 
1339319251c5SThomas Zimmermann /*
1340319251c5SThomas Zimmermann  * Platform driver
1341319251c5SThomas Zimmermann  */
1342319251c5SThomas Zimmermann 
1343319251c5SThomas Zimmermann static int ofdrm_probe(struct platform_device *pdev)
1344319251c5SThomas Zimmermann {
1345319251c5SThomas Zimmermann 	struct ofdrm_device *odev;
1346b5626f6fSThomas Zimmermann 	struct drm_sysfb_device *sysfb;
1347319251c5SThomas Zimmermann 	struct drm_device *dev;
1348319251c5SThomas Zimmermann 	int ret;
1349319251c5SThomas Zimmermann 
1350319251c5SThomas Zimmermann 	odev = ofdrm_device_create(&ofdrm_driver, pdev);
1351319251c5SThomas Zimmermann 	if (IS_ERR(odev))
1352319251c5SThomas Zimmermann 		return PTR_ERR(odev);
1353b5626f6fSThomas Zimmermann 	sysfb = &odev->sysfb;
1354b5626f6fSThomas Zimmermann 	dev = &sysfb->dev;
1355319251c5SThomas Zimmermann 
1356319251c5SThomas Zimmermann 	ret = drm_dev_register(dev, 0);
1357319251c5SThomas Zimmermann 	if (ret)
1358319251c5SThomas Zimmermann 		return ret;
1359319251c5SThomas Zimmermann 
1360b5626f6fSThomas Zimmermann 	drm_client_setup(dev, sysfb->fb_format);
1361319251c5SThomas Zimmermann 
1362319251c5SThomas Zimmermann 	return 0;
1363319251c5SThomas Zimmermann }
1364319251c5SThomas Zimmermann 
1365319251c5SThomas Zimmermann static void ofdrm_remove(struct platform_device *pdev)
1366319251c5SThomas Zimmermann {
1367319251c5SThomas Zimmermann 	struct drm_device *dev = platform_get_drvdata(pdev);
1368319251c5SThomas Zimmermann 
1369319251c5SThomas Zimmermann 	drm_dev_unplug(dev);
1370319251c5SThomas Zimmermann }
1371319251c5SThomas Zimmermann 
1372319251c5SThomas Zimmermann static const struct of_device_id ofdrm_of_match_display[] = {
1373319251c5SThomas Zimmermann 	{ .compatible = "display", },
1374319251c5SThomas Zimmermann 	{ },
1375319251c5SThomas Zimmermann };
1376319251c5SThomas Zimmermann MODULE_DEVICE_TABLE(of, ofdrm_of_match_display);
1377319251c5SThomas Zimmermann 
1378319251c5SThomas Zimmermann static struct platform_driver ofdrm_platform_driver = {
1379319251c5SThomas Zimmermann 	.driver = {
1380319251c5SThomas Zimmermann 		.name = "of-display",
1381319251c5SThomas Zimmermann 		.of_match_table = ofdrm_of_match_display,
1382319251c5SThomas Zimmermann 	},
1383319251c5SThomas Zimmermann 	.probe = ofdrm_probe,
1384319251c5SThomas Zimmermann 	.remove = ofdrm_remove,
1385319251c5SThomas Zimmermann };
1386319251c5SThomas Zimmermann 
1387319251c5SThomas Zimmermann module_platform_driver(ofdrm_platform_driver);
1388319251c5SThomas Zimmermann 
1389319251c5SThomas Zimmermann MODULE_DESCRIPTION(DRIVER_DESC);
1390319251c5SThomas Zimmermann MODULE_LICENSE("GPL");
1391