1c8a17756SThomas Zimmermann // SPDX-License-Identifier: GPL-2.0-only
2c8a17756SThomas Zimmermann
3c8a17756SThomas Zimmermann #include <linux/of_address.h>
4c8a17756SThomas Zimmermann #include <linux/pci.h>
5c8a17756SThomas Zimmermann #include <linux/platform_device.h>
6c8a17756SThomas Zimmermann
7c8a17756SThomas Zimmermann #include <drm/drm_aperture.h>
8c8a17756SThomas Zimmermann #include <drm/drm_atomic.h>
9c8a17756SThomas Zimmermann #include <drm/drm_atomic_state_helper.h>
10c8a17756SThomas Zimmermann #include <drm/drm_connector.h>
11c8a17756SThomas Zimmermann #include <drm/drm_damage_helper.h>
12c8a17756SThomas Zimmermann #include <drm/drm_device.h>
13c8a17756SThomas Zimmermann #include <drm/drm_drv.h>
148ab59da2SThomas Zimmermann #include <drm/drm_fbdev_generic.h>
15c8a17756SThomas Zimmermann #include <drm/drm_format_helper.h>
164bbb9061SThomas Zimmermann #include <drm/drm_framebuffer.h>
17c8a17756SThomas Zimmermann #include <drm/drm_gem_atomic_helper.h>
18c8a17756SThomas Zimmermann #include <drm/drm_gem_framebuffer_helper.h>
19c8a17756SThomas Zimmermann #include <drm/drm_gem_shmem_helper.h>
20c8a17756SThomas Zimmermann #include <drm/drm_managed.h>
21c8a17756SThomas Zimmermann #include <drm/drm_modeset_helper_vtables.h>
22c8a17756SThomas Zimmermann #include <drm/drm_plane_helper.h>
23c8a17756SThomas Zimmermann #include <drm/drm_probe_helper.h>
24c8a17756SThomas Zimmermann #include <drm/drm_simple_kms_helper.h>
25c8a17756SThomas Zimmermann
26c8a17756SThomas Zimmermann #define DRIVER_NAME "ofdrm"
27c8a17756SThomas Zimmermann #define DRIVER_DESC "DRM driver for OF platform devices"
28c8a17756SThomas Zimmermann #define DRIVER_DATE "20220501"
29c8a17756SThomas Zimmermann #define DRIVER_MAJOR 1
30c8a17756SThomas Zimmermann #define DRIVER_MINOR 0
31c8a17756SThomas Zimmermann
32f496834eSThomas Zimmermann #define PCI_VENDOR_ID_ATI_R520 0x7100
33f496834eSThomas Zimmermann #define PCI_VENDOR_ID_ATI_R600 0x9400
34f496834eSThomas Zimmermann
354bbb9061SThomas Zimmermann #define OFDRM_GAMMA_LUT_SIZE 256
364bbb9061SThomas Zimmermann
374bbb9061SThomas Zimmermann /* Definitions used by the Avivo palette */
384bbb9061SThomas Zimmermann #define AVIVO_DC_LUT_RW_SELECT 0x6480
394bbb9061SThomas Zimmermann #define AVIVO_DC_LUT_RW_MODE 0x6484
404bbb9061SThomas Zimmermann #define AVIVO_DC_LUT_RW_INDEX 0x6488
414bbb9061SThomas Zimmermann #define AVIVO_DC_LUT_SEQ_COLOR 0x648c
424bbb9061SThomas Zimmermann #define AVIVO_DC_LUT_PWL_DATA 0x6490
434bbb9061SThomas Zimmermann #define AVIVO_DC_LUT_30_COLOR 0x6494
444bbb9061SThomas Zimmermann #define AVIVO_DC_LUT_READ_PIPE_SELECT 0x6498
454bbb9061SThomas Zimmermann #define AVIVO_DC_LUT_WRITE_EN_MASK 0x649c
464bbb9061SThomas Zimmermann #define AVIVO_DC_LUT_AUTOFILL 0x64a0
474bbb9061SThomas Zimmermann #define AVIVO_DC_LUTA_CONTROL 0x64c0
484bbb9061SThomas Zimmermann #define AVIVO_DC_LUTA_BLACK_OFFSET_BLUE 0x64c4
494bbb9061SThomas Zimmermann #define AVIVO_DC_LUTA_BLACK_OFFSET_GREEN 0x64c8
504bbb9061SThomas Zimmermann #define AVIVO_DC_LUTA_BLACK_OFFSET_RED 0x64cc
514bbb9061SThomas Zimmermann #define AVIVO_DC_LUTA_WHITE_OFFSET_BLUE 0x64d0
524bbb9061SThomas Zimmermann #define AVIVO_DC_LUTA_WHITE_OFFSET_GREEN 0x64d4
534bbb9061SThomas Zimmermann #define AVIVO_DC_LUTA_WHITE_OFFSET_RED 0x64d8
544bbb9061SThomas Zimmermann #define AVIVO_DC_LUTB_CONTROL 0x6cc0
554bbb9061SThomas Zimmermann #define AVIVO_DC_LUTB_BLACK_OFFSET_BLUE 0x6cc4
564bbb9061SThomas Zimmermann #define AVIVO_DC_LUTB_BLACK_OFFSET_GREEN 0x6cc8
574bbb9061SThomas Zimmermann #define AVIVO_DC_LUTB_BLACK_OFFSET_RED 0x6ccc
584bbb9061SThomas Zimmermann #define AVIVO_DC_LUTB_WHITE_OFFSET_BLUE 0x6cd0
594bbb9061SThomas Zimmermann #define AVIVO_DC_LUTB_WHITE_OFFSET_GREEN 0x6cd4
604bbb9061SThomas Zimmermann #define AVIVO_DC_LUTB_WHITE_OFFSET_RED 0x6cd8
614bbb9061SThomas Zimmermann
62f496834eSThomas Zimmermann enum ofdrm_model {
63f496834eSThomas Zimmermann OFDRM_MODEL_UNKNOWN,
64f496834eSThomas Zimmermann OFDRM_MODEL_MACH64, /* ATI Mach64 */
65f496834eSThomas Zimmermann OFDRM_MODEL_RAGE128, /* ATI Rage128 */
66f496834eSThomas Zimmermann OFDRM_MODEL_RAGE_M3A, /* ATI Rage Mobility M3 Head A */
67f496834eSThomas Zimmermann OFDRM_MODEL_RAGE_M3B, /* ATI Rage Mobility M3 Head B */
68f496834eSThomas Zimmermann OFDRM_MODEL_RADEON, /* ATI Radeon */
69f496834eSThomas Zimmermann OFDRM_MODEL_GXT2000, /* IBM GXT2000 */
70f496834eSThomas Zimmermann OFDRM_MODEL_AVIVO, /* ATI R5xx */
71f496834eSThomas Zimmermann OFDRM_MODEL_QEMU, /* QEMU VGA */
72f496834eSThomas Zimmermann };
73f496834eSThomas Zimmermann
74c8a17756SThomas Zimmermann /*
75c8a17756SThomas Zimmermann * Helpers for display nodes
76c8a17756SThomas Zimmermann */
77c8a17756SThomas Zimmermann
display_get_validated_int(struct drm_device * dev,const char * name,uint32_t value)78c8a17756SThomas Zimmermann static int display_get_validated_int(struct drm_device *dev, const char *name, uint32_t value)
79c8a17756SThomas Zimmermann {
80c8a17756SThomas Zimmermann if (value > INT_MAX) {
81c8a17756SThomas Zimmermann drm_err(dev, "invalid framebuffer %s of %u\n", name, value);
82c8a17756SThomas Zimmermann return -EINVAL;
83c8a17756SThomas Zimmermann }
84c8a17756SThomas Zimmermann return (int)value;
85c8a17756SThomas Zimmermann }
86c8a17756SThomas Zimmermann
display_get_validated_int0(struct drm_device * dev,const char * name,uint32_t value)87c8a17756SThomas Zimmermann static int display_get_validated_int0(struct drm_device *dev, const char *name, uint32_t value)
88c8a17756SThomas Zimmermann {
89c8a17756SThomas Zimmermann if (!value) {
90c8a17756SThomas Zimmermann drm_err(dev, "invalid framebuffer %s of %u\n", name, value);
91c8a17756SThomas Zimmermann return -EINVAL;
92c8a17756SThomas Zimmermann }
93c8a17756SThomas Zimmermann return display_get_validated_int(dev, name, value);
94c8a17756SThomas Zimmermann }
95c8a17756SThomas Zimmermann
display_get_validated_format(struct drm_device * dev,u32 depth,bool big_endian)96c8a17756SThomas Zimmermann static const struct drm_format_info *display_get_validated_format(struct drm_device *dev,
97d405bc2cSThomas Zimmermann u32 depth, bool big_endian)
98c8a17756SThomas Zimmermann {
99c8a17756SThomas Zimmermann const struct drm_format_info *info;
100c8a17756SThomas Zimmermann u32 format;
101c8a17756SThomas Zimmermann
102c8a17756SThomas Zimmermann switch (depth) {
103c8a17756SThomas Zimmermann case 8:
104c8a17756SThomas Zimmermann format = drm_mode_legacy_fb_format(8, 8);
105c8a17756SThomas Zimmermann break;
106c8a17756SThomas Zimmermann case 15:
107c8a17756SThomas Zimmermann case 16:
108c8a17756SThomas Zimmermann format = drm_mode_legacy_fb_format(16, depth);
109c8a17756SThomas Zimmermann break;
110c8a17756SThomas Zimmermann case 32:
111c8a17756SThomas Zimmermann format = drm_mode_legacy_fb_format(32, 24);
112c8a17756SThomas Zimmermann break;
113c8a17756SThomas Zimmermann default:
114c8a17756SThomas Zimmermann drm_err(dev, "unsupported framebuffer depth %u\n", depth);
115c8a17756SThomas Zimmermann return ERR_PTR(-EINVAL);
116c8a17756SThomas Zimmermann }
117c8a17756SThomas Zimmermann
118d405bc2cSThomas Zimmermann /*
119d405bc2cSThomas Zimmermann * DRM formats assume little-endian byte order. Update the format
120d405bc2cSThomas Zimmermann * if the scanout buffer uses big-endian ordering.
121d405bc2cSThomas Zimmermann */
122d405bc2cSThomas Zimmermann if (big_endian) {
123d405bc2cSThomas Zimmermann switch (format) {
124d405bc2cSThomas Zimmermann case DRM_FORMAT_XRGB8888:
125d405bc2cSThomas Zimmermann format = DRM_FORMAT_BGRX8888;
126d405bc2cSThomas Zimmermann break;
127d405bc2cSThomas Zimmermann case DRM_FORMAT_ARGB8888:
128d405bc2cSThomas Zimmermann format = DRM_FORMAT_BGRA8888;
129d405bc2cSThomas Zimmermann break;
130d405bc2cSThomas Zimmermann case DRM_FORMAT_RGB565:
131d405bc2cSThomas Zimmermann format = DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN;
132d405bc2cSThomas Zimmermann break;
133d405bc2cSThomas Zimmermann case DRM_FORMAT_XRGB1555:
134d405bc2cSThomas Zimmermann format = DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN;
135d405bc2cSThomas Zimmermann break;
136d405bc2cSThomas Zimmermann default:
137d405bc2cSThomas Zimmermann break;
138d405bc2cSThomas Zimmermann }
139d405bc2cSThomas Zimmermann }
140d405bc2cSThomas Zimmermann
141c8a17756SThomas Zimmermann info = drm_format_info(format);
142c8a17756SThomas Zimmermann if (!info) {
143c8a17756SThomas Zimmermann drm_err(dev, "cannot find framebuffer format for depth %u\n", depth);
144c8a17756SThomas Zimmermann return ERR_PTR(-EINVAL);
145c8a17756SThomas Zimmermann }
146c8a17756SThomas Zimmermann
147c8a17756SThomas Zimmermann return info;
148c8a17756SThomas Zimmermann }
149c8a17756SThomas Zimmermann
display_read_u32_of(struct drm_device * dev,struct device_node * of_node,const char * name,u32 * value)150c8a17756SThomas Zimmermann static int display_read_u32_of(struct drm_device *dev, struct device_node *of_node,
151c8a17756SThomas Zimmermann const char *name, u32 *value)
152c8a17756SThomas Zimmermann {
153c8a17756SThomas Zimmermann int ret = of_property_read_u32(of_node, name, value);
154c8a17756SThomas Zimmermann
155c8a17756SThomas Zimmermann if (ret)
156c8a17756SThomas Zimmermann drm_err(dev, "cannot parse framebuffer %s: error %d\n", name, ret);
157c8a17756SThomas Zimmermann return ret;
158c8a17756SThomas Zimmermann }
159c8a17756SThomas Zimmermann
display_get_big_endian_of(struct drm_device * dev,struct device_node * of_node)160d405bc2cSThomas Zimmermann static bool display_get_big_endian_of(struct drm_device *dev, struct device_node *of_node)
161d405bc2cSThomas Zimmermann {
162d405bc2cSThomas Zimmermann bool big_endian;
163d405bc2cSThomas Zimmermann
164d405bc2cSThomas Zimmermann #ifdef __BIG_ENDIAN
165e27a85c1SRob Herring big_endian = !of_property_read_bool(of_node, "little-endian");
166d405bc2cSThomas Zimmermann #else
167e27a85c1SRob Herring big_endian = of_property_read_bool(of_node, "big-endian");
168d405bc2cSThomas Zimmermann #endif
169d405bc2cSThomas Zimmermann
170d405bc2cSThomas Zimmermann return big_endian;
171d405bc2cSThomas Zimmermann }
172d405bc2cSThomas Zimmermann
display_get_width_of(struct drm_device * dev,struct device_node * of_node)173c8a17756SThomas Zimmermann static int display_get_width_of(struct drm_device *dev, struct device_node *of_node)
174c8a17756SThomas Zimmermann {
175c8a17756SThomas Zimmermann u32 width;
176c8a17756SThomas Zimmermann int ret = display_read_u32_of(dev, of_node, "width", &width);
177c8a17756SThomas Zimmermann
178c8a17756SThomas Zimmermann if (ret)
179c8a17756SThomas Zimmermann return ret;
180c8a17756SThomas Zimmermann return display_get_validated_int0(dev, "width", width);
181c8a17756SThomas Zimmermann }
182c8a17756SThomas Zimmermann
display_get_height_of(struct drm_device * dev,struct device_node * of_node)183c8a17756SThomas Zimmermann static int display_get_height_of(struct drm_device *dev, struct device_node *of_node)
184c8a17756SThomas Zimmermann {
185c8a17756SThomas Zimmermann u32 height;
186c8a17756SThomas Zimmermann int ret = display_read_u32_of(dev, of_node, "height", &height);
187c8a17756SThomas Zimmermann
188c8a17756SThomas Zimmermann if (ret)
189c8a17756SThomas Zimmermann return ret;
190c8a17756SThomas Zimmermann return display_get_validated_int0(dev, "height", height);
191c8a17756SThomas Zimmermann }
192c8a17756SThomas Zimmermann
display_get_depth_of(struct drm_device * dev,struct device_node * of_node)193c8a17756SThomas Zimmermann static int display_get_depth_of(struct drm_device *dev, struct device_node *of_node)
194c8a17756SThomas Zimmermann {
195c8a17756SThomas Zimmermann u32 depth;
196c8a17756SThomas Zimmermann int ret = display_read_u32_of(dev, of_node, "depth", &depth);
197c8a17756SThomas Zimmermann
198c8a17756SThomas Zimmermann if (ret)
199c8a17756SThomas Zimmermann return ret;
200c8a17756SThomas Zimmermann return display_get_validated_int0(dev, "depth", depth);
201c8a17756SThomas Zimmermann }
202c8a17756SThomas Zimmermann
display_get_linebytes_of(struct drm_device * dev,struct device_node * of_node)203c8a17756SThomas Zimmermann static int display_get_linebytes_of(struct drm_device *dev, struct device_node *of_node)
204c8a17756SThomas Zimmermann {
205c8a17756SThomas Zimmermann u32 linebytes;
206c8a17756SThomas Zimmermann int ret = display_read_u32_of(dev, of_node, "linebytes", &linebytes);
207c8a17756SThomas Zimmermann
208c8a17756SThomas Zimmermann if (ret)
209c8a17756SThomas Zimmermann return ret;
210c8a17756SThomas Zimmermann return display_get_validated_int(dev, "linebytes", linebytes);
211c8a17756SThomas Zimmermann }
212c8a17756SThomas Zimmermann
display_get_address_of(struct drm_device * dev,struct device_node * of_node)213c8a17756SThomas Zimmermann static u64 display_get_address_of(struct drm_device *dev, struct device_node *of_node)
214c8a17756SThomas Zimmermann {
215c8a17756SThomas Zimmermann u32 address;
216c8a17756SThomas Zimmermann int ret;
217c8a17756SThomas Zimmermann
218c8a17756SThomas Zimmermann /*
219c8a17756SThomas Zimmermann * Not all devices provide an address property, it's not
220c8a17756SThomas Zimmermann * a bug if this fails. The driver will try to find the
221c8a17756SThomas Zimmermann * framebuffer base address from the device's memory regions.
222c8a17756SThomas Zimmermann */
223c8a17756SThomas Zimmermann ret = of_property_read_u32(of_node, "address", &address);
224c8a17756SThomas Zimmermann if (ret)
225c8a17756SThomas Zimmermann return OF_BAD_ADDR;
226c8a17756SThomas Zimmermann
227c8a17756SThomas Zimmermann return address;
228c8a17756SThomas Zimmermann }
229c8a17756SThomas Zimmermann
is_avivo(u32 vendor,u32 device)230e67e865bSThomas Zimmermann static bool is_avivo(u32 vendor, u32 device)
231f496834eSThomas Zimmermann {
232f496834eSThomas Zimmermann /* This will match most R5xx */
233f496834eSThomas Zimmermann return (vendor == PCI_VENDOR_ID_ATI) &&
234f496834eSThomas Zimmermann ((device >= PCI_VENDOR_ID_ATI_R520 && device < 0x7800) ||
235f496834eSThomas Zimmermann (PCI_VENDOR_ID_ATI_R600 >= 0x9400));
236f496834eSThomas Zimmermann }
237f496834eSThomas Zimmermann
display_get_model_of(struct drm_device * dev,struct device_node * of_node)238f496834eSThomas Zimmermann static enum ofdrm_model display_get_model_of(struct drm_device *dev, struct device_node *of_node)
239f496834eSThomas Zimmermann {
240f496834eSThomas Zimmermann enum ofdrm_model model = OFDRM_MODEL_UNKNOWN;
241f496834eSThomas Zimmermann
242f496834eSThomas Zimmermann if (of_node_name_prefix(of_node, "ATY,Rage128")) {
243f496834eSThomas Zimmermann model = OFDRM_MODEL_RAGE128;
244f496834eSThomas Zimmermann } else if (of_node_name_prefix(of_node, "ATY,RageM3pA") ||
245f496834eSThomas Zimmermann of_node_name_prefix(of_node, "ATY,RageM3p12A")) {
246f496834eSThomas Zimmermann model = OFDRM_MODEL_RAGE_M3A;
247f496834eSThomas Zimmermann } else if (of_node_name_prefix(of_node, "ATY,RageM3pB")) {
248f496834eSThomas Zimmermann model = OFDRM_MODEL_RAGE_M3B;
249f496834eSThomas Zimmermann } else if (of_node_name_prefix(of_node, "ATY,Rage6")) {
250f496834eSThomas Zimmermann model = OFDRM_MODEL_RADEON;
251f496834eSThomas Zimmermann } else if (of_node_name_prefix(of_node, "ATY,")) {
252f496834eSThomas Zimmermann return OFDRM_MODEL_MACH64;
253f496834eSThomas Zimmermann } else if (of_device_is_compatible(of_node, "pci1014,b7") ||
254f496834eSThomas Zimmermann of_device_is_compatible(of_node, "pci1014,21c")) {
255f496834eSThomas Zimmermann model = OFDRM_MODEL_GXT2000;
256f496834eSThomas Zimmermann } else if (of_node_name_prefix(of_node, "vga,Display-")) {
257f496834eSThomas Zimmermann struct device_node *of_parent;
258f496834eSThomas Zimmermann const __be32 *vendor_p, *device_p;
259f496834eSThomas Zimmermann
260f496834eSThomas Zimmermann /* Look for AVIVO initialized by SLOF */
261f496834eSThomas Zimmermann of_parent = of_get_parent(of_node);
262f496834eSThomas Zimmermann vendor_p = of_get_property(of_parent, "vendor-id", NULL);
263f496834eSThomas Zimmermann device_p = of_get_property(of_parent, "device-id", NULL);
264e67e865bSThomas Zimmermann if (vendor_p && device_p) {
265e67e865bSThomas Zimmermann u32 vendor = be32_to_cpup(vendor_p);
266e67e865bSThomas Zimmermann u32 device = be32_to_cpup(device_p);
267e67e865bSThomas Zimmermann
268e67e865bSThomas Zimmermann if (is_avivo(vendor, device))
269f496834eSThomas Zimmermann model = OFDRM_MODEL_AVIVO;
270e67e865bSThomas Zimmermann }
271f496834eSThomas Zimmermann of_node_put(of_parent);
272f496834eSThomas Zimmermann } else if (of_device_is_compatible(of_node, "qemu,std-vga")) {
273f496834eSThomas Zimmermann model = OFDRM_MODEL_QEMU;
274f496834eSThomas Zimmermann }
275f496834eSThomas Zimmermann
276f496834eSThomas Zimmermann return model;
277f496834eSThomas Zimmermann }
278f496834eSThomas Zimmermann
279c8a17756SThomas Zimmermann /*
280c8a17756SThomas Zimmermann * Open Firmware display device
281c8a17756SThomas Zimmermann */
282c8a17756SThomas Zimmermann
2834bbb9061SThomas Zimmermann struct ofdrm_device;
2844bbb9061SThomas Zimmermann
285f496834eSThomas Zimmermann struct ofdrm_device_funcs {
2864bbb9061SThomas Zimmermann void __iomem *(*cmap_ioremap)(struct ofdrm_device *odev,
2874bbb9061SThomas Zimmermann struct device_node *of_node,
2884bbb9061SThomas Zimmermann u64 fb_bas);
2894bbb9061SThomas Zimmermann void (*cmap_write)(struct ofdrm_device *odev, unsigned char index,
2904bbb9061SThomas Zimmermann unsigned char r, unsigned char g, unsigned char b);
291f496834eSThomas Zimmermann };
292f496834eSThomas Zimmermann
293c8a17756SThomas Zimmermann struct ofdrm_device {
294c8a17756SThomas Zimmermann struct drm_device dev;
295c8a17756SThomas Zimmermann struct platform_device *pdev;
296c8a17756SThomas Zimmermann
297f496834eSThomas Zimmermann const struct ofdrm_device_funcs *funcs;
298f496834eSThomas Zimmermann
299c8a17756SThomas Zimmermann /* firmware-buffer settings */
300c8a17756SThomas Zimmermann struct iosys_map screen_base;
301c8a17756SThomas Zimmermann struct drm_display_mode mode;
302c8a17756SThomas Zimmermann const struct drm_format_info *format;
303c8a17756SThomas Zimmermann unsigned int pitch;
304c8a17756SThomas Zimmermann
3054bbb9061SThomas Zimmermann /* colormap */
3064bbb9061SThomas Zimmermann void __iomem *cmap_base;
3074bbb9061SThomas Zimmermann
308c8a17756SThomas Zimmermann /* modesetting */
309c8a17756SThomas Zimmermann uint32_t formats[8];
310c8a17756SThomas Zimmermann struct drm_plane primary_plane;
311c8a17756SThomas Zimmermann struct drm_crtc crtc;
312c8a17756SThomas Zimmermann struct drm_encoder encoder;
313c8a17756SThomas Zimmermann struct drm_connector connector;
314c8a17756SThomas Zimmermann };
315c8a17756SThomas Zimmermann
ofdrm_device_of_dev(struct drm_device * dev)316c8a17756SThomas Zimmermann static struct ofdrm_device *ofdrm_device_of_dev(struct drm_device *dev)
317c8a17756SThomas Zimmermann {
318c8a17756SThomas Zimmermann return container_of(dev, struct ofdrm_device, dev);
319c8a17756SThomas Zimmermann }
320c8a17756SThomas Zimmermann
321c8a17756SThomas Zimmermann /*
322c8a17756SThomas Zimmermann * Hardware
323c8a17756SThomas Zimmermann */
324c8a17756SThomas Zimmermann
325c8a17756SThomas Zimmermann #if defined(CONFIG_PCI)
display_get_pci_dev_of(struct drm_device * dev,struct device_node * of_node)326c8a17756SThomas Zimmermann static struct pci_dev *display_get_pci_dev_of(struct drm_device *dev, struct device_node *of_node)
327c8a17756SThomas Zimmermann {
328c8a17756SThomas Zimmermann const __be32 *vendor_p, *device_p;
329c8a17756SThomas Zimmermann u32 vendor, device;
330c8a17756SThomas Zimmermann struct pci_dev *pcidev;
331c8a17756SThomas Zimmermann
332c8a17756SThomas Zimmermann vendor_p = of_get_property(of_node, "vendor-id", NULL);
333c8a17756SThomas Zimmermann if (!vendor_p)
334c8a17756SThomas Zimmermann return ERR_PTR(-ENODEV);
335c8a17756SThomas Zimmermann vendor = be32_to_cpup(vendor_p);
336c8a17756SThomas Zimmermann
337c8a17756SThomas Zimmermann device_p = of_get_property(of_node, "device-id", NULL);
338c8a17756SThomas Zimmermann if (!device_p)
339c8a17756SThomas Zimmermann return ERR_PTR(-ENODEV);
340c8a17756SThomas Zimmermann device = be32_to_cpup(device_p);
341c8a17756SThomas Zimmermann
342c8a17756SThomas Zimmermann pcidev = pci_get_device(vendor, device, NULL);
343c8a17756SThomas Zimmermann if (!pcidev)
344c8a17756SThomas Zimmermann return ERR_PTR(-ENODEV);
345c8a17756SThomas Zimmermann
346c8a17756SThomas Zimmermann return pcidev;
347c8a17756SThomas Zimmermann }
348c8a17756SThomas Zimmermann
ofdrm_pci_release(void * data)349c8a17756SThomas Zimmermann static void ofdrm_pci_release(void *data)
350c8a17756SThomas Zimmermann {
351c8a17756SThomas Zimmermann struct pci_dev *pcidev = data;
352c8a17756SThomas Zimmermann
353c8a17756SThomas Zimmermann pci_disable_device(pcidev);
354c8a17756SThomas Zimmermann }
355c8a17756SThomas Zimmermann
ofdrm_device_init_pci(struct ofdrm_device * odev)356c8a17756SThomas Zimmermann static int ofdrm_device_init_pci(struct ofdrm_device *odev)
357c8a17756SThomas Zimmermann {
358c8a17756SThomas Zimmermann struct drm_device *dev = &odev->dev;
359c8a17756SThomas Zimmermann struct platform_device *pdev = to_platform_device(dev->dev);
360c8a17756SThomas Zimmermann struct device_node *of_node = pdev->dev.of_node;
361c8a17756SThomas Zimmermann struct pci_dev *pcidev;
362c8a17756SThomas Zimmermann int ret;
363c8a17756SThomas Zimmermann
364c8a17756SThomas Zimmermann /*
365c8a17756SThomas Zimmermann * Never use pcim_ or other managed helpers on the returned PCI
366c8a17756SThomas Zimmermann * device. Otherwise, probing the native driver will fail for
367c8a17756SThomas Zimmermann * resource conflicts. PCI-device management has to be tied to
368c8a17756SThomas Zimmermann * the lifetime of the platform device until the native driver
369c8a17756SThomas Zimmermann * takes over.
370c8a17756SThomas Zimmermann */
371c8a17756SThomas Zimmermann pcidev = display_get_pci_dev_of(dev, of_node);
372c8a17756SThomas Zimmermann if (IS_ERR(pcidev))
373c8a17756SThomas Zimmermann return 0; /* no PCI device found; ignore the error */
374c8a17756SThomas Zimmermann
375c8a17756SThomas Zimmermann ret = pci_enable_device(pcidev);
376c8a17756SThomas Zimmermann if (ret) {
377c8a17756SThomas Zimmermann drm_err(dev, "pci_enable_device(%s) failed: %d\n",
378c8a17756SThomas Zimmermann dev_name(&pcidev->dev), ret);
379c8a17756SThomas Zimmermann return ret;
380c8a17756SThomas Zimmermann }
381c8a17756SThomas Zimmermann ret = devm_add_action_or_reset(&pdev->dev, ofdrm_pci_release, pcidev);
382c8a17756SThomas Zimmermann if (ret)
383c8a17756SThomas Zimmermann return ret;
384c8a17756SThomas Zimmermann
385c8a17756SThomas Zimmermann return 0;
386c8a17756SThomas Zimmermann }
387c8a17756SThomas Zimmermann #else
ofdrm_device_init_pci(struct ofdrm_device * odev)388c8a17756SThomas Zimmermann static int ofdrm_device_init_pci(struct ofdrm_device *odev)
389c8a17756SThomas Zimmermann {
390c8a17756SThomas Zimmermann return 0;
391c8a17756SThomas Zimmermann }
392c8a17756SThomas Zimmermann #endif
393c8a17756SThomas Zimmermann
394c8a17756SThomas Zimmermann /*
395c8a17756SThomas Zimmermann * OF display settings
396c8a17756SThomas Zimmermann */
397c8a17756SThomas Zimmermann
ofdrm_find_fb_resource(struct ofdrm_device * odev,struct resource * fb_res)398c8a17756SThomas Zimmermann static struct resource *ofdrm_find_fb_resource(struct ofdrm_device *odev,
399c8a17756SThomas Zimmermann struct resource *fb_res)
400c8a17756SThomas Zimmermann {
401c8a17756SThomas Zimmermann struct platform_device *pdev = to_platform_device(odev->dev.dev);
402c8a17756SThomas Zimmermann struct resource *res, *max_res = NULL;
403c8a17756SThomas Zimmermann u32 i;
404c8a17756SThomas Zimmermann
405c8a17756SThomas Zimmermann for (i = 0; pdev->num_resources; ++i) {
406c8a17756SThomas Zimmermann res = platform_get_resource(pdev, IORESOURCE_MEM, i);
407c8a17756SThomas Zimmermann if (!res)
408c8a17756SThomas Zimmermann break; /* all resources processed */
409c8a17756SThomas Zimmermann if (resource_size(res) < resource_size(fb_res))
410c8a17756SThomas Zimmermann continue; /* resource too small */
411c8a17756SThomas Zimmermann if (fb_res->start && resource_contains(res, fb_res))
412c8a17756SThomas Zimmermann return res; /* resource contains framebuffer */
413c8a17756SThomas Zimmermann if (!max_res || resource_size(res) > resource_size(max_res))
414c8a17756SThomas Zimmermann max_res = res; /* store largest resource as fallback */
415c8a17756SThomas Zimmermann }
416c8a17756SThomas Zimmermann
417c8a17756SThomas Zimmermann return max_res;
418c8a17756SThomas Zimmermann }
419c8a17756SThomas Zimmermann
420c8a17756SThomas Zimmermann /*
4214bbb9061SThomas Zimmermann * Colormap / Palette
4224bbb9061SThomas Zimmermann */
4234bbb9061SThomas Zimmermann
get_cmap_address_of(struct ofdrm_device * odev,struct device_node * of_node,int bar_no,unsigned long offset,unsigned long size)4244bbb9061SThomas Zimmermann static void __iomem *get_cmap_address_of(struct ofdrm_device *odev, struct device_node *of_node,
4254bbb9061SThomas Zimmermann int bar_no, unsigned long offset, unsigned long size)
4264bbb9061SThomas Zimmermann {
4274bbb9061SThomas Zimmermann struct drm_device *dev = &odev->dev;
4284bbb9061SThomas Zimmermann const __be32 *addr_p;
4294bbb9061SThomas Zimmermann u64 max_size, address;
4304bbb9061SThomas Zimmermann unsigned int flags;
4314bbb9061SThomas Zimmermann void __iomem *mem;
4324bbb9061SThomas Zimmermann
4334bbb9061SThomas Zimmermann addr_p = of_get_pci_address(of_node, bar_no, &max_size, &flags);
4344bbb9061SThomas Zimmermann if (!addr_p)
4354bbb9061SThomas Zimmermann addr_p = of_get_address(of_node, bar_no, &max_size, &flags);
4364bbb9061SThomas Zimmermann if (!addr_p)
437a8300c46SThomas Zimmermann return IOMEM_ERR_PTR(-ENODEV);
4384bbb9061SThomas Zimmermann
4394bbb9061SThomas Zimmermann if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
440a8300c46SThomas Zimmermann return IOMEM_ERR_PTR(-ENODEV);
4414bbb9061SThomas Zimmermann
4424bbb9061SThomas Zimmermann if ((offset + size) >= max_size)
443a8300c46SThomas Zimmermann return IOMEM_ERR_PTR(-ENODEV);
4444bbb9061SThomas Zimmermann
4454bbb9061SThomas Zimmermann address = of_translate_address(of_node, addr_p);
4464bbb9061SThomas Zimmermann if (address == OF_BAD_ADDR)
447a8300c46SThomas Zimmermann return IOMEM_ERR_PTR(-ENODEV);
4484bbb9061SThomas Zimmermann
4494bbb9061SThomas Zimmermann mem = devm_ioremap(dev->dev, address + offset, size);
4504bbb9061SThomas Zimmermann if (!mem)
451a8300c46SThomas Zimmermann return IOMEM_ERR_PTR(-ENOMEM);
4524bbb9061SThomas Zimmermann
4534bbb9061SThomas Zimmermann return mem;
4544bbb9061SThomas Zimmermann }
4554bbb9061SThomas Zimmermann
ofdrm_mach64_cmap_ioremap(struct ofdrm_device * odev,struct device_node * of_node,u64 fb_base)4564bbb9061SThomas Zimmermann static void __iomem *ofdrm_mach64_cmap_ioremap(struct ofdrm_device *odev,
4574bbb9061SThomas Zimmermann struct device_node *of_node,
4584bbb9061SThomas Zimmermann u64 fb_base)
4594bbb9061SThomas Zimmermann {
4604bbb9061SThomas Zimmermann struct drm_device *dev = &odev->dev;
4614bbb9061SThomas Zimmermann u64 address;
4624bbb9061SThomas Zimmermann void __iomem *cmap_base;
4634bbb9061SThomas Zimmermann
4644bbb9061SThomas Zimmermann address = fb_base & 0xff000000ul;
4654bbb9061SThomas Zimmermann address += 0x7ff000;
4664bbb9061SThomas Zimmermann
4674bbb9061SThomas Zimmermann cmap_base = devm_ioremap(dev->dev, address, 0x1000);
4684bbb9061SThomas Zimmermann if (!cmap_base)
469a8300c46SThomas Zimmermann return IOMEM_ERR_PTR(-ENOMEM);
4704bbb9061SThomas Zimmermann
4714bbb9061SThomas Zimmermann return cmap_base;
4724bbb9061SThomas Zimmermann }
4734bbb9061SThomas Zimmermann
ofdrm_mach64_cmap_write(struct ofdrm_device * odev,unsigned char index,unsigned char r,unsigned char g,unsigned char b)4744bbb9061SThomas Zimmermann static void ofdrm_mach64_cmap_write(struct ofdrm_device *odev, unsigned char index,
4754bbb9061SThomas Zimmermann unsigned char r, unsigned char g, unsigned char b)
4764bbb9061SThomas Zimmermann {
4774bbb9061SThomas Zimmermann void __iomem *addr = odev->cmap_base + 0xcc0;
4784bbb9061SThomas Zimmermann void __iomem *data = odev->cmap_base + 0xcc0 + 1;
4794bbb9061SThomas Zimmermann
4804bbb9061SThomas Zimmermann writeb(index, addr);
4814bbb9061SThomas Zimmermann writeb(r, data);
4824bbb9061SThomas Zimmermann writeb(g, data);
4834bbb9061SThomas Zimmermann writeb(b, data);
4844bbb9061SThomas Zimmermann }
4854bbb9061SThomas Zimmermann
ofdrm_rage128_cmap_ioremap(struct ofdrm_device * odev,struct device_node * of_node,u64 fb_base)4864bbb9061SThomas Zimmermann static void __iomem *ofdrm_rage128_cmap_ioremap(struct ofdrm_device *odev,
4874bbb9061SThomas Zimmermann struct device_node *of_node,
4884bbb9061SThomas Zimmermann u64 fb_base)
4894bbb9061SThomas Zimmermann {
4904bbb9061SThomas Zimmermann return get_cmap_address_of(odev, of_node, 2, 0, 0x1fff);
4914bbb9061SThomas Zimmermann }
4924bbb9061SThomas Zimmermann
ofdrm_rage128_cmap_write(struct ofdrm_device * odev,unsigned char index,unsigned char r,unsigned char g,unsigned char b)4934bbb9061SThomas Zimmermann static void ofdrm_rage128_cmap_write(struct ofdrm_device *odev, unsigned char index,
4944bbb9061SThomas Zimmermann unsigned char r, unsigned char g, unsigned char b)
4954bbb9061SThomas Zimmermann {
4964bbb9061SThomas Zimmermann void __iomem *addr = odev->cmap_base + 0xb0;
4974bbb9061SThomas Zimmermann void __iomem *data = odev->cmap_base + 0xb4;
4984bbb9061SThomas Zimmermann u32 color = (r << 16) | (g << 8) | b;
4994bbb9061SThomas Zimmermann
5004bbb9061SThomas Zimmermann writeb(index, addr);
5014bbb9061SThomas Zimmermann writel(color, data);
5024bbb9061SThomas Zimmermann }
5034bbb9061SThomas Zimmermann
ofdrm_rage_m3a_cmap_ioremap(struct ofdrm_device * odev,struct device_node * of_node,u64 fb_base)5044bbb9061SThomas Zimmermann static void __iomem *ofdrm_rage_m3a_cmap_ioremap(struct ofdrm_device *odev,
5054bbb9061SThomas Zimmermann struct device_node *of_node,
5064bbb9061SThomas Zimmermann u64 fb_base)
5074bbb9061SThomas Zimmermann {
5084bbb9061SThomas Zimmermann return get_cmap_address_of(odev, of_node, 2, 0, 0x1fff);
5094bbb9061SThomas Zimmermann }
5104bbb9061SThomas Zimmermann
ofdrm_rage_m3a_cmap_write(struct ofdrm_device * odev,unsigned char index,unsigned char r,unsigned char g,unsigned char b)5114bbb9061SThomas Zimmermann static void ofdrm_rage_m3a_cmap_write(struct ofdrm_device *odev, unsigned char index,
5124bbb9061SThomas Zimmermann unsigned char r, unsigned char g, unsigned char b)
5134bbb9061SThomas Zimmermann {
5144bbb9061SThomas Zimmermann void __iomem *dac_ctl = odev->cmap_base + 0x58;
5154bbb9061SThomas Zimmermann void __iomem *addr = odev->cmap_base + 0xb0;
5164bbb9061SThomas Zimmermann void __iomem *data = odev->cmap_base + 0xb4;
5174bbb9061SThomas Zimmermann u32 color = (r << 16) | (g << 8) | b;
5184bbb9061SThomas Zimmermann u32 val;
5194bbb9061SThomas Zimmermann
5204bbb9061SThomas Zimmermann /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
5214bbb9061SThomas Zimmermann val = readl(dac_ctl);
5224bbb9061SThomas Zimmermann val &= ~0x20;
5234bbb9061SThomas Zimmermann writel(val, dac_ctl);
5244bbb9061SThomas Zimmermann
5254bbb9061SThomas Zimmermann /* Set color at palette index */
5264bbb9061SThomas Zimmermann writeb(index, addr);
5274bbb9061SThomas Zimmermann writel(color, data);
5284bbb9061SThomas Zimmermann }
5294bbb9061SThomas Zimmermann
ofdrm_rage_m3b_cmap_ioremap(struct ofdrm_device * odev,struct device_node * of_node,u64 fb_base)5304bbb9061SThomas Zimmermann static void __iomem *ofdrm_rage_m3b_cmap_ioremap(struct ofdrm_device *odev,
5314bbb9061SThomas Zimmermann struct device_node *of_node,
5324bbb9061SThomas Zimmermann u64 fb_base)
5334bbb9061SThomas Zimmermann {
5344bbb9061SThomas Zimmermann return get_cmap_address_of(odev, of_node, 2, 0, 0x1fff);
5354bbb9061SThomas Zimmermann }
5364bbb9061SThomas Zimmermann
ofdrm_rage_m3b_cmap_write(struct ofdrm_device * odev,unsigned char index,unsigned char r,unsigned char g,unsigned char b)5374bbb9061SThomas Zimmermann static void ofdrm_rage_m3b_cmap_write(struct ofdrm_device *odev, unsigned char index,
5384bbb9061SThomas Zimmermann unsigned char r, unsigned char g, unsigned char b)
5394bbb9061SThomas Zimmermann {
5404bbb9061SThomas Zimmermann void __iomem *dac_ctl = odev->cmap_base + 0x58;
5414bbb9061SThomas Zimmermann void __iomem *addr = odev->cmap_base + 0xb0;
5424bbb9061SThomas Zimmermann void __iomem *data = odev->cmap_base + 0xb4;
5434bbb9061SThomas Zimmermann u32 color = (r << 16) | (g << 8) | b;
5444bbb9061SThomas Zimmermann u32 val;
5454bbb9061SThomas Zimmermann
5464bbb9061SThomas Zimmermann /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
5474bbb9061SThomas Zimmermann val = readl(dac_ctl);
5484bbb9061SThomas Zimmermann val |= 0x20;
5494bbb9061SThomas Zimmermann writel(val, dac_ctl);
5504bbb9061SThomas Zimmermann
5514bbb9061SThomas Zimmermann /* Set color at palette index */
5524bbb9061SThomas Zimmermann writeb(index, addr);
5534bbb9061SThomas Zimmermann writel(color, data);
5544bbb9061SThomas Zimmermann }
5554bbb9061SThomas Zimmermann
ofdrm_radeon_cmap_ioremap(struct ofdrm_device * odev,struct device_node * of_node,u64 fb_base)5564bbb9061SThomas Zimmermann static void __iomem *ofdrm_radeon_cmap_ioremap(struct ofdrm_device *odev,
5574bbb9061SThomas Zimmermann struct device_node *of_node,
5584bbb9061SThomas Zimmermann u64 fb_base)
5594bbb9061SThomas Zimmermann {
5604bbb9061SThomas Zimmermann return get_cmap_address_of(odev, of_node, 1, 0, 0x1fff);
5614bbb9061SThomas Zimmermann }
5624bbb9061SThomas Zimmermann
ofdrm_gxt2000_cmap_ioremap(struct ofdrm_device * odev,struct device_node * of_node,u64 fb_base)5634bbb9061SThomas Zimmermann static void __iomem *ofdrm_gxt2000_cmap_ioremap(struct ofdrm_device *odev,
5644bbb9061SThomas Zimmermann struct device_node *of_node,
5654bbb9061SThomas Zimmermann u64 fb_base)
5664bbb9061SThomas Zimmermann {
5674bbb9061SThomas Zimmermann return get_cmap_address_of(odev, of_node, 0, 0x6000, 0x1000);
5684bbb9061SThomas Zimmermann }
5694bbb9061SThomas Zimmermann
ofdrm_gxt2000_cmap_write(struct ofdrm_device * odev,unsigned char index,unsigned char r,unsigned char g,unsigned char b)5704bbb9061SThomas Zimmermann static void ofdrm_gxt2000_cmap_write(struct ofdrm_device *odev, unsigned char index,
5714bbb9061SThomas Zimmermann unsigned char r, unsigned char g, unsigned char b)
5724bbb9061SThomas Zimmermann {
5734bbb9061SThomas Zimmermann void __iomem *data = ((unsigned int __iomem *)odev->cmap_base) + index;
5744bbb9061SThomas Zimmermann u32 color = (r << 16) | (g << 8) | b;
5754bbb9061SThomas Zimmermann
5764bbb9061SThomas Zimmermann writel(color, data);
5774bbb9061SThomas Zimmermann }
5784bbb9061SThomas Zimmermann
ofdrm_avivo_cmap_ioremap(struct ofdrm_device * odev,struct device_node * of_node,u64 fb_base)5794bbb9061SThomas Zimmermann static void __iomem *ofdrm_avivo_cmap_ioremap(struct ofdrm_device *odev,
5804bbb9061SThomas Zimmermann struct device_node *of_node,
5814bbb9061SThomas Zimmermann u64 fb_base)
5824bbb9061SThomas Zimmermann {
5834bbb9061SThomas Zimmermann struct device_node *of_parent;
5844bbb9061SThomas Zimmermann void __iomem *cmap_base;
5854bbb9061SThomas Zimmermann
5864bbb9061SThomas Zimmermann of_parent = of_get_parent(of_node);
5874bbb9061SThomas Zimmermann cmap_base = get_cmap_address_of(odev, of_parent, 0, 0, 0x10000);
5884bbb9061SThomas Zimmermann of_node_put(of_parent);
5894bbb9061SThomas Zimmermann
5904bbb9061SThomas Zimmermann return cmap_base;
5914bbb9061SThomas Zimmermann }
5924bbb9061SThomas Zimmermann
ofdrm_avivo_cmap_write(struct ofdrm_device * odev,unsigned char index,unsigned char r,unsigned char g,unsigned char b)5934bbb9061SThomas Zimmermann static void ofdrm_avivo_cmap_write(struct ofdrm_device *odev, unsigned char index,
5944bbb9061SThomas Zimmermann unsigned char r, unsigned char g, unsigned char b)
5954bbb9061SThomas Zimmermann {
5964bbb9061SThomas Zimmermann void __iomem *lutsel = odev->cmap_base + AVIVO_DC_LUT_RW_SELECT;
5974bbb9061SThomas Zimmermann void __iomem *addr = odev->cmap_base + AVIVO_DC_LUT_RW_INDEX;
5984bbb9061SThomas Zimmermann void __iomem *data = odev->cmap_base + AVIVO_DC_LUT_30_COLOR;
5994bbb9061SThomas Zimmermann u32 color = (r << 22) | (g << 12) | (b << 2);
6004bbb9061SThomas Zimmermann
6014bbb9061SThomas Zimmermann /* Write to both LUTs for now */
6024bbb9061SThomas Zimmermann
6034bbb9061SThomas Zimmermann writel(1, lutsel);
6044bbb9061SThomas Zimmermann writeb(index, addr);
6054bbb9061SThomas Zimmermann writel(color, data);
6064bbb9061SThomas Zimmermann
6074bbb9061SThomas Zimmermann writel(0, lutsel);
6084bbb9061SThomas Zimmermann writeb(index, addr);
6094bbb9061SThomas Zimmermann writel(color, data);
6104bbb9061SThomas Zimmermann }
6114bbb9061SThomas Zimmermann
ofdrm_qemu_cmap_ioremap(struct ofdrm_device * odev,struct device_node * of_node,u64 fb_base)6124bbb9061SThomas Zimmermann static void __iomem *ofdrm_qemu_cmap_ioremap(struct ofdrm_device *odev,
6134bbb9061SThomas Zimmermann struct device_node *of_node,
6144bbb9061SThomas Zimmermann u64 fb_base)
6154bbb9061SThomas Zimmermann {
6164bbb9061SThomas Zimmermann static const __be32 io_of_addr[3] = {
6174bbb9061SThomas Zimmermann cpu_to_be32(0x01000000),
6184bbb9061SThomas Zimmermann cpu_to_be32(0x00),
6194bbb9061SThomas Zimmermann cpu_to_be32(0x00),
6204bbb9061SThomas Zimmermann };
6214bbb9061SThomas Zimmermann
6224bbb9061SThomas Zimmermann struct drm_device *dev = &odev->dev;
6234bbb9061SThomas Zimmermann u64 address;
6244bbb9061SThomas Zimmermann void __iomem *cmap_base;
6254bbb9061SThomas Zimmermann
6264bbb9061SThomas Zimmermann address = of_translate_address(of_node, io_of_addr);
6274bbb9061SThomas Zimmermann if (address == OF_BAD_ADDR)
628a8300c46SThomas Zimmermann return IOMEM_ERR_PTR(-ENODEV);
6294bbb9061SThomas Zimmermann
6304bbb9061SThomas Zimmermann cmap_base = devm_ioremap(dev->dev, address + 0x3c8, 2);
6314bbb9061SThomas Zimmermann if (!cmap_base)
632a8300c46SThomas Zimmermann return IOMEM_ERR_PTR(-ENOMEM);
6334bbb9061SThomas Zimmermann
6344bbb9061SThomas Zimmermann return cmap_base;
6354bbb9061SThomas Zimmermann }
6364bbb9061SThomas Zimmermann
ofdrm_qemu_cmap_write(struct ofdrm_device * odev,unsigned char index,unsigned char r,unsigned char g,unsigned char b)6374bbb9061SThomas Zimmermann static void ofdrm_qemu_cmap_write(struct ofdrm_device *odev, unsigned char index,
6384bbb9061SThomas Zimmermann unsigned char r, unsigned char g, unsigned char b)
6394bbb9061SThomas Zimmermann {
6404bbb9061SThomas Zimmermann void __iomem *addr = odev->cmap_base;
6414bbb9061SThomas Zimmermann void __iomem *data = odev->cmap_base + 1;
6424bbb9061SThomas Zimmermann
6434bbb9061SThomas Zimmermann writeb(index, addr);
6444bbb9061SThomas Zimmermann writeb(r, data);
6454bbb9061SThomas Zimmermann writeb(g, data);
6464bbb9061SThomas Zimmermann writeb(b, data);
6474bbb9061SThomas Zimmermann }
6484bbb9061SThomas Zimmermann
ofdrm_device_set_gamma_linear(struct ofdrm_device * odev,const struct drm_format_info * format)6494bbb9061SThomas Zimmermann static void ofdrm_device_set_gamma_linear(struct ofdrm_device *odev,
6504bbb9061SThomas Zimmermann const struct drm_format_info *format)
6514bbb9061SThomas Zimmermann {
6524bbb9061SThomas Zimmermann struct drm_device *dev = &odev->dev;
6534bbb9061SThomas Zimmermann int i;
6544bbb9061SThomas Zimmermann
6554bbb9061SThomas Zimmermann switch (format->format) {
6564bbb9061SThomas Zimmermann case DRM_FORMAT_RGB565:
657d405bc2cSThomas Zimmermann case DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN:
6584bbb9061SThomas Zimmermann /* Use better interpolation, to take 32 values from 0 to 255 */
6594bbb9061SThomas Zimmermann for (i = 0; i < OFDRM_GAMMA_LUT_SIZE / 8; i++) {
6604bbb9061SThomas Zimmermann unsigned char r = i * 8 + i / 4;
6614bbb9061SThomas Zimmermann unsigned char g = i * 4 + i / 16;
6624bbb9061SThomas Zimmermann unsigned char b = i * 8 + i / 4;
6634bbb9061SThomas Zimmermann
6644bbb9061SThomas Zimmermann odev->funcs->cmap_write(odev, i, r, g, b);
6654bbb9061SThomas Zimmermann }
6664bbb9061SThomas Zimmermann /* Green has one more bit, so add padding with 0 for red and blue. */
6674bbb9061SThomas Zimmermann for (i = OFDRM_GAMMA_LUT_SIZE / 8; i < OFDRM_GAMMA_LUT_SIZE / 4; i++) {
6684bbb9061SThomas Zimmermann unsigned char r = 0;
6694bbb9061SThomas Zimmermann unsigned char g = i * 4 + i / 16;
6704bbb9061SThomas Zimmermann unsigned char b = 0;
6714bbb9061SThomas Zimmermann
6724bbb9061SThomas Zimmermann odev->funcs->cmap_write(odev, i, r, g, b);
6734bbb9061SThomas Zimmermann }
6744bbb9061SThomas Zimmermann break;
6754bbb9061SThomas Zimmermann case DRM_FORMAT_XRGB8888:
676d405bc2cSThomas Zimmermann case DRM_FORMAT_BGRX8888:
6774bbb9061SThomas Zimmermann for (i = 0; i < OFDRM_GAMMA_LUT_SIZE; i++)
6784bbb9061SThomas Zimmermann odev->funcs->cmap_write(odev, i, i, i, i);
6794bbb9061SThomas Zimmermann break;
6804bbb9061SThomas Zimmermann default:
6814bbb9061SThomas Zimmermann drm_warn_once(dev, "Unsupported format %p4cc for gamma correction\n",
6824bbb9061SThomas Zimmermann &format->format);
6834bbb9061SThomas Zimmermann break;
6844bbb9061SThomas Zimmermann }
6854bbb9061SThomas Zimmermann }
6864bbb9061SThomas Zimmermann
ofdrm_device_set_gamma(struct ofdrm_device * odev,const struct drm_format_info * format,struct drm_color_lut * lut)6874bbb9061SThomas Zimmermann static void ofdrm_device_set_gamma(struct ofdrm_device *odev,
6884bbb9061SThomas Zimmermann const struct drm_format_info *format,
6894bbb9061SThomas Zimmermann struct drm_color_lut *lut)
6904bbb9061SThomas Zimmermann {
6914bbb9061SThomas Zimmermann struct drm_device *dev = &odev->dev;
6924bbb9061SThomas Zimmermann int i;
6934bbb9061SThomas Zimmermann
6944bbb9061SThomas Zimmermann switch (format->format) {
6954bbb9061SThomas Zimmermann case DRM_FORMAT_RGB565:
696d405bc2cSThomas Zimmermann case DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN:
6974bbb9061SThomas Zimmermann /* Use better interpolation, to take 32 values from lut[0] to lut[255] */
6984bbb9061SThomas Zimmermann for (i = 0; i < OFDRM_GAMMA_LUT_SIZE / 8; i++) {
6994bbb9061SThomas Zimmermann unsigned char r = lut[i * 8 + i / 4].red >> 8;
7004bbb9061SThomas Zimmermann unsigned char g = lut[i * 4 + i / 16].green >> 8;
7014bbb9061SThomas Zimmermann unsigned char b = lut[i * 8 + i / 4].blue >> 8;
7024bbb9061SThomas Zimmermann
7034bbb9061SThomas Zimmermann odev->funcs->cmap_write(odev, i, r, g, b);
7044bbb9061SThomas Zimmermann }
7054bbb9061SThomas Zimmermann /* Green has one more bit, so add padding with 0 for red and blue. */
7064bbb9061SThomas Zimmermann for (i = OFDRM_GAMMA_LUT_SIZE / 8; i < OFDRM_GAMMA_LUT_SIZE / 4; i++) {
7074bbb9061SThomas Zimmermann unsigned char r = 0;
7084bbb9061SThomas Zimmermann unsigned char g = lut[i * 4 + i / 16].green >> 8;
7094bbb9061SThomas Zimmermann unsigned char b = 0;
7104bbb9061SThomas Zimmermann
7114bbb9061SThomas Zimmermann odev->funcs->cmap_write(odev, i, r, g, b);
7124bbb9061SThomas Zimmermann }
7134bbb9061SThomas Zimmermann break;
7144bbb9061SThomas Zimmermann case DRM_FORMAT_XRGB8888:
715d405bc2cSThomas Zimmermann case DRM_FORMAT_BGRX8888:
7164bbb9061SThomas Zimmermann for (i = 0; i < OFDRM_GAMMA_LUT_SIZE; i++) {
7174bbb9061SThomas Zimmermann unsigned char r = lut[i].red >> 8;
7184bbb9061SThomas Zimmermann unsigned char g = lut[i].green >> 8;
7194bbb9061SThomas Zimmermann unsigned char b = lut[i].blue >> 8;
7204bbb9061SThomas Zimmermann
7214bbb9061SThomas Zimmermann odev->funcs->cmap_write(odev, i, r, g, b);
7224bbb9061SThomas Zimmermann }
7234bbb9061SThomas Zimmermann break;
7244bbb9061SThomas Zimmermann default:
7254bbb9061SThomas Zimmermann drm_warn_once(dev, "Unsupported format %p4cc for gamma correction\n",
7264bbb9061SThomas Zimmermann &format->format);
7274bbb9061SThomas Zimmermann break;
7284bbb9061SThomas Zimmermann }
7294bbb9061SThomas Zimmermann }
7304bbb9061SThomas Zimmermann
7314bbb9061SThomas Zimmermann /*
732c8a17756SThomas Zimmermann * Modesetting
733c8a17756SThomas Zimmermann */
734c8a17756SThomas Zimmermann
73541137443SThomas Zimmermann struct ofdrm_crtc_state {
73641137443SThomas Zimmermann struct drm_crtc_state base;
7374bbb9061SThomas Zimmermann
7384bbb9061SThomas Zimmermann /* Primary-plane format; required for color mgmt. */
7394bbb9061SThomas Zimmermann const struct drm_format_info *format;
74041137443SThomas Zimmermann };
74141137443SThomas Zimmermann
to_ofdrm_crtc_state(struct drm_crtc_state * base)74241137443SThomas Zimmermann static struct ofdrm_crtc_state *to_ofdrm_crtc_state(struct drm_crtc_state *base)
74341137443SThomas Zimmermann {
74441137443SThomas Zimmermann return container_of(base, struct ofdrm_crtc_state, base);
74541137443SThomas Zimmermann }
74641137443SThomas Zimmermann
ofdrm_crtc_state_destroy(struct ofdrm_crtc_state * ofdrm_crtc_state)74741137443SThomas Zimmermann static void ofdrm_crtc_state_destroy(struct ofdrm_crtc_state *ofdrm_crtc_state)
74841137443SThomas Zimmermann {
74941137443SThomas Zimmermann __drm_atomic_helper_crtc_destroy_state(&ofdrm_crtc_state->base);
75041137443SThomas Zimmermann kfree(ofdrm_crtc_state);
75141137443SThomas Zimmermann }
75241137443SThomas Zimmermann
753c8a17756SThomas Zimmermann static const uint64_t ofdrm_primary_plane_format_modifiers[] = {
754c8a17756SThomas Zimmermann DRM_FORMAT_MOD_LINEAR,
755c8a17756SThomas Zimmermann DRM_FORMAT_MOD_INVALID
756c8a17756SThomas Zimmermann };
757c8a17756SThomas Zimmermann
ofdrm_primary_plane_helper_atomic_check(struct drm_plane * plane,struct drm_atomic_state * new_state)758c8a17756SThomas Zimmermann static int ofdrm_primary_plane_helper_atomic_check(struct drm_plane *plane,
759c8a17756SThomas Zimmermann struct drm_atomic_state *new_state)
760c8a17756SThomas Zimmermann {
761c8a17756SThomas Zimmermann struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(new_state, plane);
7624bbb9061SThomas Zimmermann struct drm_framebuffer *new_fb = new_plane_state->fb;
763c8a17756SThomas Zimmermann struct drm_crtc *new_crtc = new_plane_state->crtc;
764c8a17756SThomas Zimmermann struct drm_crtc_state *new_crtc_state = NULL;
7654bbb9061SThomas Zimmermann struct ofdrm_crtc_state *new_ofdrm_crtc_state;
7664bbb9061SThomas Zimmermann int ret;
767c8a17756SThomas Zimmermann
768c8a17756SThomas Zimmermann if (new_crtc)
769c8a17756SThomas Zimmermann new_crtc_state = drm_atomic_get_new_crtc_state(new_state, new_plane_state->crtc);
770c8a17756SThomas Zimmermann
7714bbb9061SThomas Zimmermann ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
772c8a17756SThomas Zimmermann DRM_PLANE_NO_SCALING,
773c8a17756SThomas Zimmermann DRM_PLANE_NO_SCALING,
774c8a17756SThomas Zimmermann false, false);
7754bbb9061SThomas Zimmermann if (ret)
7764bbb9061SThomas Zimmermann return ret;
7774bbb9061SThomas Zimmermann else if (!new_plane_state->visible)
7784bbb9061SThomas Zimmermann return 0;
7794bbb9061SThomas Zimmermann
7804bbb9061SThomas Zimmermann new_crtc_state = drm_atomic_get_new_crtc_state(new_state, new_plane_state->crtc);
7814bbb9061SThomas Zimmermann
7824bbb9061SThomas Zimmermann new_ofdrm_crtc_state = to_ofdrm_crtc_state(new_crtc_state);
7834bbb9061SThomas Zimmermann new_ofdrm_crtc_state->format = new_fb->format;
7844bbb9061SThomas Zimmermann
7854bbb9061SThomas Zimmermann return 0;
786c8a17756SThomas Zimmermann }
787c8a17756SThomas Zimmermann
ofdrm_primary_plane_helper_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)788c8a17756SThomas Zimmermann static void ofdrm_primary_plane_helper_atomic_update(struct drm_plane *plane,
789c8a17756SThomas Zimmermann struct drm_atomic_state *state)
790c8a17756SThomas Zimmermann {
791c8a17756SThomas Zimmermann struct drm_device *dev = plane->dev;
792c8a17756SThomas Zimmermann struct ofdrm_device *odev = ofdrm_device_of_dev(dev);
793c8a17756SThomas Zimmermann struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
794c8a17756SThomas Zimmermann struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
795c8a17756SThomas Zimmermann struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
796c8a17756SThomas Zimmermann struct drm_framebuffer *fb = plane_state->fb;
797c8a17756SThomas Zimmermann unsigned int dst_pitch = odev->pitch;
798c8a17756SThomas Zimmermann const struct drm_format_info *dst_format = odev->format;
799c8a17756SThomas Zimmermann struct drm_atomic_helper_damage_iter iter;
800c8a17756SThomas Zimmermann struct drm_rect damage;
801c8a17756SThomas Zimmermann int ret, idx;
802c8a17756SThomas Zimmermann
803c8a17756SThomas Zimmermann ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
804c8a17756SThomas Zimmermann if (ret)
805c8a17756SThomas Zimmermann return;
806c8a17756SThomas Zimmermann
807c8a17756SThomas Zimmermann if (!drm_dev_enter(dev, &idx))
808c8a17756SThomas Zimmermann goto out_drm_gem_fb_end_cpu_access;
809c8a17756SThomas Zimmermann
810c8a17756SThomas Zimmermann drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
811c8a17756SThomas Zimmermann drm_atomic_for_each_plane_damage(&iter, &damage) {
812c8a17756SThomas Zimmermann struct iosys_map dst = odev->screen_base;
813c8a17756SThomas Zimmermann struct drm_rect dst_clip = plane_state->dst;
814c8a17756SThomas Zimmermann
815c8a17756SThomas Zimmermann if (!drm_rect_intersect(&dst_clip, &damage))
816c8a17756SThomas Zimmermann continue;
817c8a17756SThomas Zimmermann
818c8a17756SThomas Zimmermann iosys_map_incr(&dst, drm_fb_clip_offset(dst_pitch, dst_format, &dst_clip));
819c8a17756SThomas Zimmermann drm_fb_blit(&dst, &dst_pitch, dst_format->format, shadow_plane_state->data, fb,
820c8a17756SThomas Zimmermann &damage);
821c8a17756SThomas Zimmermann }
822c8a17756SThomas Zimmermann
823c8a17756SThomas Zimmermann drm_dev_exit(idx);
824c8a17756SThomas Zimmermann out_drm_gem_fb_end_cpu_access:
825c8a17756SThomas Zimmermann drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
826c8a17756SThomas Zimmermann }
827c8a17756SThomas Zimmermann
ofdrm_primary_plane_helper_atomic_disable(struct drm_plane * plane,struct drm_atomic_state * state)828c8a17756SThomas Zimmermann static void ofdrm_primary_plane_helper_atomic_disable(struct drm_plane *plane,
829c8a17756SThomas Zimmermann struct drm_atomic_state *state)
830c8a17756SThomas Zimmermann {
831c8a17756SThomas Zimmermann struct drm_device *dev = plane->dev;
832c8a17756SThomas Zimmermann struct ofdrm_device *odev = ofdrm_device_of_dev(dev);
833c8a17756SThomas Zimmermann struct iosys_map dst = odev->screen_base;
834c8a17756SThomas Zimmermann struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
835c8a17756SThomas Zimmermann void __iomem *dst_vmap = dst.vaddr_iomem; /* TODO: Use mapping abstraction */
836c8a17756SThomas Zimmermann unsigned int dst_pitch = odev->pitch;
837c8a17756SThomas Zimmermann const struct drm_format_info *dst_format = odev->format;
838c8a17756SThomas Zimmermann struct drm_rect dst_clip;
839c8a17756SThomas Zimmermann unsigned long lines, linepixels, i;
840c8a17756SThomas Zimmermann int idx;
841c8a17756SThomas Zimmermann
842c8a17756SThomas Zimmermann drm_rect_init(&dst_clip,
843c8a17756SThomas Zimmermann plane_state->src_x >> 16, plane_state->src_y >> 16,
844c8a17756SThomas Zimmermann plane_state->src_w >> 16, plane_state->src_h >> 16);
845c8a17756SThomas Zimmermann
846c8a17756SThomas Zimmermann lines = drm_rect_height(&dst_clip);
847c8a17756SThomas Zimmermann linepixels = drm_rect_width(&dst_clip);
848c8a17756SThomas Zimmermann
849c8a17756SThomas Zimmermann if (!drm_dev_enter(dev, &idx))
850c8a17756SThomas Zimmermann return;
851c8a17756SThomas Zimmermann
852c8a17756SThomas Zimmermann /* Clear buffer to black if disabled */
853c8a17756SThomas Zimmermann dst_vmap += drm_fb_clip_offset(dst_pitch, dst_format, &dst_clip);
854c8a17756SThomas Zimmermann for (i = 0; i < lines; ++i) {
855c8a17756SThomas Zimmermann memset_io(dst_vmap, 0, linepixels * dst_format->cpp[0]);
856c8a17756SThomas Zimmermann dst_vmap += dst_pitch;
857c8a17756SThomas Zimmermann }
858c8a17756SThomas Zimmermann
859c8a17756SThomas Zimmermann drm_dev_exit(idx);
860c8a17756SThomas Zimmermann }
861c8a17756SThomas Zimmermann
862c8a17756SThomas Zimmermann static const struct drm_plane_helper_funcs ofdrm_primary_plane_helper_funcs = {
863c8a17756SThomas Zimmermann DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
864c8a17756SThomas Zimmermann .atomic_check = ofdrm_primary_plane_helper_atomic_check,
865c8a17756SThomas Zimmermann .atomic_update = ofdrm_primary_plane_helper_atomic_update,
866c8a17756SThomas Zimmermann .atomic_disable = ofdrm_primary_plane_helper_atomic_disable,
867c8a17756SThomas Zimmermann };
868c8a17756SThomas Zimmermann
869c8a17756SThomas Zimmermann static const struct drm_plane_funcs ofdrm_primary_plane_funcs = {
870c8a17756SThomas Zimmermann .update_plane = drm_atomic_helper_update_plane,
871c8a17756SThomas Zimmermann .disable_plane = drm_atomic_helper_disable_plane,
872c8a17756SThomas Zimmermann .destroy = drm_plane_cleanup,
873c8a17756SThomas Zimmermann DRM_GEM_SHADOW_PLANE_FUNCS,
874c8a17756SThomas Zimmermann };
875c8a17756SThomas Zimmermann
ofdrm_crtc_helper_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)876c8a17756SThomas Zimmermann static enum drm_mode_status ofdrm_crtc_helper_mode_valid(struct drm_crtc *crtc,
877c8a17756SThomas Zimmermann const struct drm_display_mode *mode)
878c8a17756SThomas Zimmermann {
879c8a17756SThomas Zimmermann struct ofdrm_device *odev = ofdrm_device_of_dev(crtc->dev);
880c8a17756SThomas Zimmermann
881c8a17756SThomas Zimmermann return drm_crtc_helper_mode_valid_fixed(crtc, mode, &odev->mode);
882c8a17756SThomas Zimmermann }
883c8a17756SThomas Zimmermann
ofdrm_crtc_helper_atomic_check(struct drm_crtc * crtc,struct drm_atomic_state * new_state)884c8a17756SThomas Zimmermann static int ofdrm_crtc_helper_atomic_check(struct drm_crtc *crtc,
885c8a17756SThomas Zimmermann struct drm_atomic_state *new_state)
886c8a17756SThomas Zimmermann {
8874bbb9061SThomas Zimmermann static const size_t gamma_lut_length = OFDRM_GAMMA_LUT_SIZE * sizeof(struct drm_color_lut);
8884bbb9061SThomas Zimmermann
8894bbb9061SThomas Zimmermann struct drm_device *dev = crtc->dev;
890c8a17756SThomas Zimmermann struct drm_crtc_state *new_crtc_state = drm_atomic_get_new_crtc_state(new_state, crtc);
8914bbb9061SThomas Zimmermann int ret;
892c8a17756SThomas Zimmermann
893c8a17756SThomas Zimmermann if (!new_crtc_state->enable)
894c8a17756SThomas Zimmermann return 0;
895c8a17756SThomas Zimmermann
8964bbb9061SThomas Zimmermann ret = drm_atomic_helper_check_crtc_primary_plane(new_crtc_state);
8974bbb9061SThomas Zimmermann if (ret)
8984bbb9061SThomas Zimmermann return ret;
8994bbb9061SThomas Zimmermann
9004bbb9061SThomas Zimmermann if (new_crtc_state->color_mgmt_changed) {
9014bbb9061SThomas Zimmermann struct drm_property_blob *gamma_lut = new_crtc_state->gamma_lut;
9024bbb9061SThomas Zimmermann
9034bbb9061SThomas Zimmermann if (gamma_lut && (gamma_lut->length != gamma_lut_length)) {
9044bbb9061SThomas Zimmermann drm_dbg(dev, "Incorrect gamma_lut length %zu\n", gamma_lut->length);
9054bbb9061SThomas Zimmermann return -EINVAL;
9064bbb9061SThomas Zimmermann }
9074bbb9061SThomas Zimmermann }
9084bbb9061SThomas Zimmermann
9094bbb9061SThomas Zimmermann return 0;
9104bbb9061SThomas Zimmermann }
9114bbb9061SThomas Zimmermann
ofdrm_crtc_helper_atomic_flush(struct drm_crtc * crtc,struct drm_atomic_state * state)9124bbb9061SThomas Zimmermann static void ofdrm_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_state *state)
9134bbb9061SThomas Zimmermann {
9144bbb9061SThomas Zimmermann struct ofdrm_device *odev = ofdrm_device_of_dev(crtc->dev);
9154bbb9061SThomas Zimmermann struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
9164bbb9061SThomas Zimmermann struct ofdrm_crtc_state *ofdrm_crtc_state = to_ofdrm_crtc_state(crtc_state);
9174bbb9061SThomas Zimmermann
9184bbb9061SThomas Zimmermann if (crtc_state->enable && crtc_state->color_mgmt_changed) {
9194bbb9061SThomas Zimmermann const struct drm_format_info *format = ofdrm_crtc_state->format;
9204bbb9061SThomas Zimmermann
9214bbb9061SThomas Zimmermann if (crtc_state->gamma_lut)
9224bbb9061SThomas Zimmermann ofdrm_device_set_gamma(odev, format, crtc_state->gamma_lut->data);
9234bbb9061SThomas Zimmermann else
9244bbb9061SThomas Zimmermann ofdrm_device_set_gamma_linear(odev, format);
9254bbb9061SThomas Zimmermann }
926c8a17756SThomas Zimmermann }
927c8a17756SThomas Zimmermann
928c8a17756SThomas Zimmermann /*
929c8a17756SThomas Zimmermann * The CRTC is always enabled. Screen updates are performed by
930c8a17756SThomas Zimmermann * the primary plane's atomic_update function. Disabling clears
931c8a17756SThomas Zimmermann * the screen in the primary plane's atomic_disable function.
932c8a17756SThomas Zimmermann */
933c8a17756SThomas Zimmermann static const struct drm_crtc_helper_funcs ofdrm_crtc_helper_funcs = {
934c8a17756SThomas Zimmermann .mode_valid = ofdrm_crtc_helper_mode_valid,
935c8a17756SThomas Zimmermann .atomic_check = ofdrm_crtc_helper_atomic_check,
9364bbb9061SThomas Zimmermann .atomic_flush = ofdrm_crtc_helper_atomic_flush,
937c8a17756SThomas Zimmermann };
938c8a17756SThomas Zimmermann
ofdrm_crtc_reset(struct drm_crtc * crtc)93941137443SThomas Zimmermann static void ofdrm_crtc_reset(struct drm_crtc *crtc)
94041137443SThomas Zimmermann {
94141137443SThomas Zimmermann struct ofdrm_crtc_state *ofdrm_crtc_state =
94241137443SThomas Zimmermann kzalloc(sizeof(*ofdrm_crtc_state), GFP_KERNEL);
94341137443SThomas Zimmermann
94441137443SThomas Zimmermann if (crtc->state)
94541137443SThomas Zimmermann ofdrm_crtc_state_destroy(to_ofdrm_crtc_state(crtc->state));
94641137443SThomas Zimmermann
94741137443SThomas Zimmermann if (ofdrm_crtc_state)
94841137443SThomas Zimmermann __drm_atomic_helper_crtc_reset(crtc, &ofdrm_crtc_state->base);
94941137443SThomas Zimmermann else
95041137443SThomas Zimmermann __drm_atomic_helper_crtc_reset(crtc, NULL);
95141137443SThomas Zimmermann }
95241137443SThomas Zimmermann
ofdrm_crtc_atomic_duplicate_state(struct drm_crtc * crtc)95341137443SThomas Zimmermann static struct drm_crtc_state *ofdrm_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
95441137443SThomas Zimmermann {
95541137443SThomas Zimmermann struct drm_device *dev = crtc->dev;
95641137443SThomas Zimmermann struct drm_crtc_state *crtc_state = crtc->state;
95741137443SThomas Zimmermann struct ofdrm_crtc_state *new_ofdrm_crtc_state;
9584bbb9061SThomas Zimmermann struct ofdrm_crtc_state *ofdrm_crtc_state;
95941137443SThomas Zimmermann
96041137443SThomas Zimmermann if (drm_WARN_ON(dev, !crtc_state))
96141137443SThomas Zimmermann return NULL;
96241137443SThomas Zimmermann
96341137443SThomas Zimmermann new_ofdrm_crtc_state = kzalloc(sizeof(*new_ofdrm_crtc_state), GFP_KERNEL);
96441137443SThomas Zimmermann if (!new_ofdrm_crtc_state)
96541137443SThomas Zimmermann return NULL;
96641137443SThomas Zimmermann
9674bbb9061SThomas Zimmermann ofdrm_crtc_state = to_ofdrm_crtc_state(crtc_state);
9684bbb9061SThomas Zimmermann
96941137443SThomas Zimmermann __drm_atomic_helper_crtc_duplicate_state(crtc, &new_ofdrm_crtc_state->base);
9704bbb9061SThomas Zimmermann new_ofdrm_crtc_state->format = ofdrm_crtc_state->format;
97141137443SThomas Zimmermann
97241137443SThomas Zimmermann return &new_ofdrm_crtc_state->base;
97341137443SThomas Zimmermann }
97441137443SThomas Zimmermann
ofdrm_crtc_atomic_destroy_state(struct drm_crtc * crtc,struct drm_crtc_state * crtc_state)97541137443SThomas Zimmermann static void ofdrm_crtc_atomic_destroy_state(struct drm_crtc *crtc,
97641137443SThomas Zimmermann struct drm_crtc_state *crtc_state)
97741137443SThomas Zimmermann {
97841137443SThomas Zimmermann ofdrm_crtc_state_destroy(to_ofdrm_crtc_state(crtc_state));
97941137443SThomas Zimmermann }
98041137443SThomas Zimmermann
981c8a17756SThomas Zimmermann static const struct drm_crtc_funcs ofdrm_crtc_funcs = {
98241137443SThomas Zimmermann .reset = ofdrm_crtc_reset,
983c8a17756SThomas Zimmermann .destroy = drm_crtc_cleanup,
984c8a17756SThomas Zimmermann .set_config = drm_atomic_helper_set_config,
985c8a17756SThomas Zimmermann .page_flip = drm_atomic_helper_page_flip,
98641137443SThomas Zimmermann .atomic_duplicate_state = ofdrm_crtc_atomic_duplicate_state,
98741137443SThomas Zimmermann .atomic_destroy_state = ofdrm_crtc_atomic_destroy_state,
988c8a17756SThomas Zimmermann };
989c8a17756SThomas Zimmermann
ofdrm_connector_helper_get_modes(struct drm_connector * connector)990c8a17756SThomas Zimmermann static int ofdrm_connector_helper_get_modes(struct drm_connector *connector)
991c8a17756SThomas Zimmermann {
992c8a17756SThomas Zimmermann struct ofdrm_device *odev = ofdrm_device_of_dev(connector->dev);
993c8a17756SThomas Zimmermann
994c8a17756SThomas Zimmermann return drm_connector_helper_get_modes_fixed(connector, &odev->mode);
995c8a17756SThomas Zimmermann }
996c8a17756SThomas Zimmermann
997c8a17756SThomas Zimmermann static const struct drm_connector_helper_funcs ofdrm_connector_helper_funcs = {
998c8a17756SThomas Zimmermann .get_modes = ofdrm_connector_helper_get_modes,
999c8a17756SThomas Zimmermann };
1000c8a17756SThomas Zimmermann
1001c8a17756SThomas Zimmermann static const struct drm_connector_funcs ofdrm_connector_funcs = {
1002c8a17756SThomas Zimmermann .reset = drm_atomic_helper_connector_reset,
1003c8a17756SThomas Zimmermann .fill_modes = drm_helper_probe_single_connector_modes,
1004c8a17756SThomas Zimmermann .destroy = drm_connector_cleanup,
1005c8a17756SThomas Zimmermann .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1006c8a17756SThomas Zimmermann .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1007c8a17756SThomas Zimmermann };
1008c8a17756SThomas Zimmermann
1009c8a17756SThomas Zimmermann static const struct drm_mode_config_funcs ofdrm_mode_config_funcs = {
1010c8a17756SThomas Zimmermann .fb_create = drm_gem_fb_create_with_dirty,
1011c8a17756SThomas Zimmermann .atomic_check = drm_atomic_helper_check,
1012c8a17756SThomas Zimmermann .atomic_commit = drm_atomic_helper_commit,
1013c8a17756SThomas Zimmermann };
1014c8a17756SThomas Zimmermann
1015c8a17756SThomas Zimmermann /*
1016c8a17756SThomas Zimmermann * Init / Cleanup
1017c8a17756SThomas Zimmermann */
1018c8a17756SThomas Zimmermann
1019f496834eSThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_unknown_device_funcs = {
1020f496834eSThomas Zimmermann };
1021f496834eSThomas Zimmermann
1022f496834eSThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_mach64_device_funcs = {
10234bbb9061SThomas Zimmermann .cmap_ioremap = ofdrm_mach64_cmap_ioremap,
10244bbb9061SThomas Zimmermann .cmap_write = ofdrm_mach64_cmap_write,
1025f496834eSThomas Zimmermann };
1026f496834eSThomas Zimmermann
1027f496834eSThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_rage128_device_funcs = {
10284bbb9061SThomas Zimmermann .cmap_ioremap = ofdrm_rage128_cmap_ioremap,
10294bbb9061SThomas Zimmermann .cmap_write = ofdrm_rage128_cmap_write,
1030f496834eSThomas Zimmermann };
1031f496834eSThomas Zimmermann
1032f496834eSThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_rage_m3a_device_funcs = {
10334bbb9061SThomas Zimmermann .cmap_ioremap = ofdrm_rage_m3a_cmap_ioremap,
10344bbb9061SThomas Zimmermann .cmap_write = ofdrm_rage_m3a_cmap_write,
1035f496834eSThomas Zimmermann };
1036f496834eSThomas Zimmermann
1037f496834eSThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_rage_m3b_device_funcs = {
10384bbb9061SThomas Zimmermann .cmap_ioremap = ofdrm_rage_m3b_cmap_ioremap,
10394bbb9061SThomas Zimmermann .cmap_write = ofdrm_rage_m3b_cmap_write,
1040f496834eSThomas Zimmermann };
1041f496834eSThomas Zimmermann
1042f496834eSThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_radeon_device_funcs = {
10434bbb9061SThomas Zimmermann .cmap_ioremap = ofdrm_radeon_cmap_ioremap,
10444bbb9061SThomas Zimmermann .cmap_write = ofdrm_rage128_cmap_write, /* same as Rage128 */
1045f496834eSThomas Zimmermann };
1046f496834eSThomas Zimmermann
1047f496834eSThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_gxt2000_device_funcs = {
10484bbb9061SThomas Zimmermann .cmap_ioremap = ofdrm_gxt2000_cmap_ioremap,
10494bbb9061SThomas Zimmermann .cmap_write = ofdrm_gxt2000_cmap_write,
1050f496834eSThomas Zimmermann };
1051f496834eSThomas Zimmermann
1052f496834eSThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_avivo_device_funcs = {
10534bbb9061SThomas Zimmermann .cmap_ioremap = ofdrm_avivo_cmap_ioremap,
10544bbb9061SThomas Zimmermann .cmap_write = ofdrm_avivo_cmap_write,
1055f496834eSThomas Zimmermann };
1056f496834eSThomas Zimmermann
1057f496834eSThomas Zimmermann static const struct ofdrm_device_funcs ofdrm_qemu_device_funcs = {
10584bbb9061SThomas Zimmermann .cmap_ioremap = ofdrm_qemu_cmap_ioremap,
10594bbb9061SThomas Zimmermann .cmap_write = ofdrm_qemu_cmap_write,
1060f496834eSThomas Zimmermann };
1061f496834eSThomas Zimmermann
ofdrm_mode(unsigned int width,unsigned int height)1062c8a17756SThomas Zimmermann static struct drm_display_mode ofdrm_mode(unsigned int width, unsigned int height)
1063c8a17756SThomas Zimmermann {
1064c8a17756SThomas Zimmermann /*
1065c8a17756SThomas Zimmermann * Assume a monitor resolution of 96 dpi to
1066c8a17756SThomas Zimmermann * get a somewhat reasonable screen size.
1067c8a17756SThomas Zimmermann */
1068c8a17756SThomas Zimmermann const struct drm_display_mode mode = {
1069c8a17756SThomas Zimmermann DRM_MODE_INIT(60, width, height,
1070c8a17756SThomas Zimmermann DRM_MODE_RES_MM(width, 96ul),
1071c8a17756SThomas Zimmermann DRM_MODE_RES_MM(height, 96ul))
1072c8a17756SThomas Zimmermann };
1073c8a17756SThomas Zimmermann
1074c8a17756SThomas Zimmermann return mode;
1075c8a17756SThomas Zimmermann }
1076c8a17756SThomas Zimmermann
ofdrm_device_create(struct drm_driver * drv,struct platform_device * pdev)1077c8a17756SThomas Zimmermann static struct ofdrm_device *ofdrm_device_create(struct drm_driver *drv,
1078c8a17756SThomas Zimmermann struct platform_device *pdev)
1079c8a17756SThomas Zimmermann {
1080c8a17756SThomas Zimmermann struct device_node *of_node = pdev->dev.of_node;
1081c8a17756SThomas Zimmermann struct ofdrm_device *odev;
1082c8a17756SThomas Zimmermann struct drm_device *dev;
1083f496834eSThomas Zimmermann enum ofdrm_model model;
1084d405bc2cSThomas Zimmermann bool big_endian;
1085c8a17756SThomas Zimmermann int width, height, depth, linebytes;
1086c8a17756SThomas Zimmermann const struct drm_format_info *format;
1087c8a17756SThomas Zimmermann u64 address;
1088c8a17756SThomas Zimmermann resource_size_t fb_size, fb_base, fb_pgbase, fb_pgsize;
1089c8a17756SThomas Zimmermann struct resource *res, *mem;
1090c8a17756SThomas Zimmermann void __iomem *screen_base;
1091c8a17756SThomas Zimmermann struct drm_plane *primary_plane;
1092c8a17756SThomas Zimmermann struct drm_crtc *crtc;
1093c8a17756SThomas Zimmermann struct drm_encoder *encoder;
1094c8a17756SThomas Zimmermann struct drm_connector *connector;
1095c8a17756SThomas Zimmermann unsigned long max_width, max_height;
1096c8a17756SThomas Zimmermann size_t nformats;
1097c8a17756SThomas Zimmermann int ret;
1098c8a17756SThomas Zimmermann
1099c8a17756SThomas Zimmermann odev = devm_drm_dev_alloc(&pdev->dev, drv, struct ofdrm_device, dev);
1100c8a17756SThomas Zimmermann if (IS_ERR(odev))
1101c8a17756SThomas Zimmermann return ERR_CAST(odev);
1102c8a17756SThomas Zimmermann dev = &odev->dev;
1103c8a17756SThomas Zimmermann platform_set_drvdata(pdev, dev);
1104c8a17756SThomas Zimmermann
1105c8a17756SThomas Zimmermann ret = ofdrm_device_init_pci(odev);
1106c8a17756SThomas Zimmermann if (ret)
1107c8a17756SThomas Zimmermann return ERR_PTR(ret);
1108c8a17756SThomas Zimmermann
1109c8a17756SThomas Zimmermann /*
1110c8a17756SThomas Zimmermann * OF display-node settings
1111c8a17756SThomas Zimmermann */
1112c8a17756SThomas Zimmermann
1113f496834eSThomas Zimmermann model = display_get_model_of(dev, of_node);
1114f496834eSThomas Zimmermann drm_dbg(dev, "detected model %d\n", model);
1115f496834eSThomas Zimmermann
1116f496834eSThomas Zimmermann switch (model) {
1117f496834eSThomas Zimmermann case OFDRM_MODEL_UNKNOWN:
1118f496834eSThomas Zimmermann odev->funcs = &ofdrm_unknown_device_funcs;
1119f496834eSThomas Zimmermann break;
1120f496834eSThomas Zimmermann case OFDRM_MODEL_MACH64:
1121f496834eSThomas Zimmermann odev->funcs = &ofdrm_mach64_device_funcs;
1122f496834eSThomas Zimmermann break;
1123f496834eSThomas Zimmermann case OFDRM_MODEL_RAGE128:
1124f496834eSThomas Zimmermann odev->funcs = &ofdrm_rage128_device_funcs;
1125f496834eSThomas Zimmermann break;
1126f496834eSThomas Zimmermann case OFDRM_MODEL_RAGE_M3A:
1127f496834eSThomas Zimmermann odev->funcs = &ofdrm_rage_m3a_device_funcs;
1128f496834eSThomas Zimmermann break;
1129f496834eSThomas Zimmermann case OFDRM_MODEL_RAGE_M3B:
1130f496834eSThomas Zimmermann odev->funcs = &ofdrm_rage_m3b_device_funcs;
1131f496834eSThomas Zimmermann break;
1132f496834eSThomas Zimmermann case OFDRM_MODEL_RADEON:
1133f496834eSThomas Zimmermann odev->funcs = &ofdrm_radeon_device_funcs;
1134f496834eSThomas Zimmermann break;
1135f496834eSThomas Zimmermann case OFDRM_MODEL_GXT2000:
1136f496834eSThomas Zimmermann odev->funcs = &ofdrm_gxt2000_device_funcs;
1137f496834eSThomas Zimmermann break;
1138f496834eSThomas Zimmermann case OFDRM_MODEL_AVIVO:
1139f496834eSThomas Zimmermann odev->funcs = &ofdrm_avivo_device_funcs;
1140f496834eSThomas Zimmermann break;
1141f496834eSThomas Zimmermann case OFDRM_MODEL_QEMU:
1142f496834eSThomas Zimmermann odev->funcs = &ofdrm_qemu_device_funcs;
1143f496834eSThomas Zimmermann break;
1144f496834eSThomas Zimmermann }
1145f496834eSThomas Zimmermann
1146d405bc2cSThomas Zimmermann big_endian = display_get_big_endian_of(dev, of_node);
1147d405bc2cSThomas Zimmermann
1148c8a17756SThomas Zimmermann width = display_get_width_of(dev, of_node);
1149c8a17756SThomas Zimmermann if (width < 0)
1150c8a17756SThomas Zimmermann return ERR_PTR(width);
1151c8a17756SThomas Zimmermann height = display_get_height_of(dev, of_node);
1152c8a17756SThomas Zimmermann if (height < 0)
1153c8a17756SThomas Zimmermann return ERR_PTR(height);
1154c8a17756SThomas Zimmermann depth = display_get_depth_of(dev, of_node);
1155c8a17756SThomas Zimmermann if (depth < 0)
1156c8a17756SThomas Zimmermann return ERR_PTR(depth);
1157c8a17756SThomas Zimmermann linebytes = display_get_linebytes_of(dev, of_node);
1158c8a17756SThomas Zimmermann if (linebytes < 0)
1159c8a17756SThomas Zimmermann return ERR_PTR(linebytes);
1160c8a17756SThomas Zimmermann
1161d405bc2cSThomas Zimmermann format = display_get_validated_format(dev, depth, big_endian);
1162c8a17756SThomas Zimmermann if (IS_ERR(format))
1163c8a17756SThomas Zimmermann return ERR_CAST(format);
1164c8a17756SThomas Zimmermann if (!linebytes) {
1165c8a17756SThomas Zimmermann linebytes = drm_format_info_min_pitch(format, 0, width);
1166c8a17756SThomas Zimmermann if (drm_WARN_ON(dev, !linebytes))
1167c8a17756SThomas Zimmermann return ERR_PTR(-EINVAL);
1168c8a17756SThomas Zimmermann }
1169c8a17756SThomas Zimmermann
1170c8a17756SThomas Zimmermann fb_size = linebytes * height;
1171c8a17756SThomas Zimmermann
1172c8a17756SThomas Zimmermann /*
1173c8a17756SThomas Zimmermann * Try to figure out the address of the framebuffer. Unfortunately, Open
1174c8a17756SThomas Zimmermann * Firmware doesn't provide a standard way to do so. All we can do is a
1175c8a17756SThomas Zimmermann * dodgy heuristic that happens to work in practice.
1176c8a17756SThomas Zimmermann *
1177c8a17756SThomas Zimmermann * On most machines, the "address" property contains what we need, though
1178c8a17756SThomas Zimmermann * not on Matrox cards found in IBM machines. What appears to give good
1179c8a17756SThomas Zimmermann * results is to go through the PCI ranges and pick one that encloses the
1180c8a17756SThomas Zimmermann * "address" property. If none match, we pick the largest.
1181c8a17756SThomas Zimmermann */
1182c8a17756SThomas Zimmermann address = display_get_address_of(dev, of_node);
1183c8a17756SThomas Zimmermann if (address != OF_BAD_ADDR) {
1184c8a17756SThomas Zimmermann struct resource fb_res = DEFINE_RES_MEM(address, fb_size);
1185c8a17756SThomas Zimmermann
1186c8a17756SThomas Zimmermann res = ofdrm_find_fb_resource(odev, &fb_res);
1187c8a17756SThomas Zimmermann if (!res)
1188c8a17756SThomas Zimmermann return ERR_PTR(-EINVAL);
1189c8a17756SThomas Zimmermann if (resource_contains(res, &fb_res))
1190c8a17756SThomas Zimmermann fb_base = address;
1191c8a17756SThomas Zimmermann else
1192c8a17756SThomas Zimmermann fb_base = res->start;
1193c8a17756SThomas Zimmermann } else {
1194c8a17756SThomas Zimmermann struct resource fb_res = DEFINE_RES_MEM(0u, fb_size);
1195c8a17756SThomas Zimmermann
1196c8a17756SThomas Zimmermann res = ofdrm_find_fb_resource(odev, &fb_res);
1197c8a17756SThomas Zimmermann if (!res)
1198c8a17756SThomas Zimmermann return ERR_PTR(-EINVAL);
1199c8a17756SThomas Zimmermann fb_base = res->start;
1200c8a17756SThomas Zimmermann }
1201c8a17756SThomas Zimmermann
1202c8a17756SThomas Zimmermann /*
1203c8a17756SThomas Zimmermann * I/O resources
1204c8a17756SThomas Zimmermann */
1205c8a17756SThomas Zimmermann
1206c8a17756SThomas Zimmermann fb_pgbase = round_down(fb_base, PAGE_SIZE);
1207c8a17756SThomas Zimmermann fb_pgsize = fb_base - fb_pgbase + round_up(fb_size, PAGE_SIZE);
1208c8a17756SThomas Zimmermann
1209c8a17756SThomas Zimmermann ret = devm_aperture_acquire_from_firmware(dev, fb_pgbase, fb_pgsize);
1210c8a17756SThomas Zimmermann if (ret) {
1211c8a17756SThomas Zimmermann drm_err(dev, "could not acquire memory range %pr: error %d\n", &res, ret);
1212c8a17756SThomas Zimmermann return ERR_PTR(ret);
1213c8a17756SThomas Zimmermann }
1214c8a17756SThomas Zimmermann
1215c8a17756SThomas Zimmermann mem = devm_request_mem_region(&pdev->dev, fb_pgbase, fb_pgsize, drv->name);
1216c8a17756SThomas Zimmermann if (!mem) {
1217c8a17756SThomas Zimmermann drm_warn(dev, "could not acquire memory region %pr\n", &res);
1218c8a17756SThomas Zimmermann return ERR_PTR(-ENOMEM);
1219c8a17756SThomas Zimmermann }
1220c8a17756SThomas Zimmermann
1221c8a17756SThomas Zimmermann screen_base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
1222c8a17756SThomas Zimmermann if (!screen_base)
1223c8a17756SThomas Zimmermann return ERR_PTR(-ENOMEM);
1224c8a17756SThomas Zimmermann
12254bbb9061SThomas Zimmermann if (odev->funcs->cmap_ioremap) {
12264bbb9061SThomas Zimmermann void __iomem *cmap_base = odev->funcs->cmap_ioremap(odev, of_node, fb_base);
12274bbb9061SThomas Zimmermann
12284bbb9061SThomas Zimmermann if (IS_ERR(cmap_base)) {
12294bbb9061SThomas Zimmermann /* Don't fail; continue without colormap */
12304bbb9061SThomas Zimmermann drm_warn(dev, "could not find colormap: error %ld\n", PTR_ERR(cmap_base));
12314bbb9061SThomas Zimmermann } else {
12324bbb9061SThomas Zimmermann odev->cmap_base = cmap_base;
12334bbb9061SThomas Zimmermann }
12344bbb9061SThomas Zimmermann }
12354bbb9061SThomas Zimmermann
1236c8a17756SThomas Zimmermann /*
1237c8a17756SThomas Zimmermann * Firmware framebuffer
1238c8a17756SThomas Zimmermann */
1239c8a17756SThomas Zimmermann
1240c8a17756SThomas Zimmermann iosys_map_set_vaddr_iomem(&odev->screen_base, screen_base);
1241c8a17756SThomas Zimmermann odev->mode = ofdrm_mode(width, height);
1242c8a17756SThomas Zimmermann odev->format = format;
1243c8a17756SThomas Zimmermann odev->pitch = linebytes;
1244c8a17756SThomas Zimmermann
1245c8a17756SThomas Zimmermann drm_dbg(dev, "display mode={" DRM_MODE_FMT "}\n", DRM_MODE_ARG(&odev->mode));
1246c8a17756SThomas Zimmermann drm_dbg(dev, "framebuffer format=%p4cc, size=%dx%d, linebytes=%d byte\n",
1247c8a17756SThomas Zimmermann &format->format, width, height, linebytes);
1248c8a17756SThomas Zimmermann
1249c8a17756SThomas Zimmermann /*
1250c8a17756SThomas Zimmermann * Mode-setting pipeline
1251c8a17756SThomas Zimmermann */
1252c8a17756SThomas Zimmermann
1253c8a17756SThomas Zimmermann ret = drmm_mode_config_init(dev);
1254c8a17756SThomas Zimmermann if (ret)
1255c8a17756SThomas Zimmermann return ERR_PTR(ret);
1256c8a17756SThomas Zimmermann
1257c8a17756SThomas Zimmermann max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH);
1258c8a17756SThomas Zimmermann max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT);
1259c8a17756SThomas Zimmermann
1260c8a17756SThomas Zimmermann dev->mode_config.min_width = width;
1261c8a17756SThomas Zimmermann dev->mode_config.max_width = max_width;
1262c8a17756SThomas Zimmermann dev->mode_config.min_height = height;
1263c8a17756SThomas Zimmermann dev->mode_config.max_height = max_height;
1264c8a17756SThomas Zimmermann dev->mode_config.funcs = &ofdrm_mode_config_funcs;
12655580f263SThomas Zimmermann dev->mode_config.preferred_depth = format->depth;
1266d405bc2cSThomas Zimmermann dev->mode_config.quirk_addfb_prefer_host_byte_order = true;
1267c8a17756SThomas Zimmermann
1268c8a17756SThomas Zimmermann /* Primary plane */
1269c8a17756SThomas Zimmermann
1270c8a17756SThomas Zimmermann nformats = drm_fb_build_fourcc_list(dev, &format->format, 1,
1271c8a17756SThomas Zimmermann odev->formats, ARRAY_SIZE(odev->formats));
1272c8a17756SThomas Zimmermann
1273c8a17756SThomas Zimmermann primary_plane = &odev->primary_plane;
1274c8a17756SThomas Zimmermann ret = drm_universal_plane_init(dev, primary_plane, 0, &ofdrm_primary_plane_funcs,
1275c8a17756SThomas Zimmermann odev->formats, nformats,
1276c8a17756SThomas Zimmermann ofdrm_primary_plane_format_modifiers,
1277c8a17756SThomas Zimmermann DRM_PLANE_TYPE_PRIMARY, NULL);
1278c8a17756SThomas Zimmermann if (ret)
1279c8a17756SThomas Zimmermann return ERR_PTR(ret);
1280c8a17756SThomas Zimmermann drm_plane_helper_add(primary_plane, &ofdrm_primary_plane_helper_funcs);
1281c8a17756SThomas Zimmermann drm_plane_enable_fb_damage_clips(primary_plane);
1282c8a17756SThomas Zimmermann
1283c8a17756SThomas Zimmermann /* CRTC */
1284c8a17756SThomas Zimmermann
1285c8a17756SThomas Zimmermann crtc = &odev->crtc;
1286c8a17756SThomas Zimmermann ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
1287c8a17756SThomas Zimmermann &ofdrm_crtc_funcs, NULL);
1288c8a17756SThomas Zimmermann if (ret)
1289c8a17756SThomas Zimmermann return ERR_PTR(ret);
1290c8a17756SThomas Zimmermann drm_crtc_helper_add(crtc, &ofdrm_crtc_helper_funcs);
1291c8a17756SThomas Zimmermann
12924bbb9061SThomas Zimmermann if (odev->cmap_base) {
12934bbb9061SThomas Zimmermann drm_mode_crtc_set_gamma_size(crtc, OFDRM_GAMMA_LUT_SIZE);
12944bbb9061SThomas Zimmermann drm_crtc_enable_color_mgmt(crtc, 0, false, OFDRM_GAMMA_LUT_SIZE);
12954bbb9061SThomas Zimmermann }
12964bbb9061SThomas Zimmermann
1297c8a17756SThomas Zimmermann /* Encoder */
1298c8a17756SThomas Zimmermann
1299c8a17756SThomas Zimmermann encoder = &odev->encoder;
1300c8a17756SThomas Zimmermann ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_NONE);
1301c8a17756SThomas Zimmermann if (ret)
1302c8a17756SThomas Zimmermann return ERR_PTR(ret);
1303c8a17756SThomas Zimmermann encoder->possible_crtcs = drm_crtc_mask(crtc);
1304c8a17756SThomas Zimmermann
1305c8a17756SThomas Zimmermann /* Connector */
1306c8a17756SThomas Zimmermann
1307c8a17756SThomas Zimmermann connector = &odev->connector;
1308c8a17756SThomas Zimmermann ret = drm_connector_init(dev, connector, &ofdrm_connector_funcs,
1309c8a17756SThomas Zimmermann DRM_MODE_CONNECTOR_Unknown);
1310c8a17756SThomas Zimmermann if (ret)
1311c8a17756SThomas Zimmermann return ERR_PTR(ret);
1312c8a17756SThomas Zimmermann drm_connector_helper_add(connector, &ofdrm_connector_helper_funcs);
1313c8a17756SThomas Zimmermann drm_connector_set_panel_orientation_with_quirk(connector,
1314c8a17756SThomas Zimmermann DRM_MODE_PANEL_ORIENTATION_UNKNOWN,
1315c8a17756SThomas Zimmermann width, height);
1316c8a17756SThomas Zimmermann
1317c8a17756SThomas Zimmermann ret = drm_connector_attach_encoder(connector, encoder);
1318c8a17756SThomas Zimmermann if (ret)
1319c8a17756SThomas Zimmermann return ERR_PTR(ret);
1320c8a17756SThomas Zimmermann
1321c8a17756SThomas Zimmermann drm_mode_config_reset(dev);
1322c8a17756SThomas Zimmermann
1323c8a17756SThomas Zimmermann return odev;
1324c8a17756SThomas Zimmermann }
1325c8a17756SThomas Zimmermann
1326c8a17756SThomas Zimmermann /*
1327c8a17756SThomas Zimmermann * DRM driver
1328c8a17756SThomas Zimmermann */
1329c8a17756SThomas Zimmermann
1330c8a17756SThomas Zimmermann DEFINE_DRM_GEM_FOPS(ofdrm_fops);
1331c8a17756SThomas Zimmermann
1332c8a17756SThomas Zimmermann static struct drm_driver ofdrm_driver = {
1333c8a17756SThomas Zimmermann DRM_GEM_SHMEM_DRIVER_OPS,
1334c8a17756SThomas Zimmermann .name = DRIVER_NAME,
1335c8a17756SThomas Zimmermann .desc = DRIVER_DESC,
1336c8a17756SThomas Zimmermann .date = DRIVER_DATE,
1337c8a17756SThomas Zimmermann .major = DRIVER_MAJOR,
1338c8a17756SThomas Zimmermann .minor = DRIVER_MINOR,
1339c8a17756SThomas Zimmermann .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
1340c8a17756SThomas Zimmermann .fops = &ofdrm_fops,
1341c8a17756SThomas Zimmermann };
1342c8a17756SThomas Zimmermann
1343c8a17756SThomas Zimmermann /*
1344c8a17756SThomas Zimmermann * Platform driver
1345c8a17756SThomas Zimmermann */
1346c8a17756SThomas Zimmermann
ofdrm_probe(struct platform_device * pdev)1347c8a17756SThomas Zimmermann static int ofdrm_probe(struct platform_device *pdev)
1348c8a17756SThomas Zimmermann {
1349c8a17756SThomas Zimmermann struct ofdrm_device *odev;
1350c8a17756SThomas Zimmermann struct drm_device *dev;
1351d3533a8aSThomas Zimmermann unsigned int color_mode;
1352c8a17756SThomas Zimmermann int ret;
1353c8a17756SThomas Zimmermann
1354c8a17756SThomas Zimmermann odev = ofdrm_device_create(&ofdrm_driver, pdev);
1355c8a17756SThomas Zimmermann if (IS_ERR(odev))
1356c8a17756SThomas Zimmermann return PTR_ERR(odev);
1357c8a17756SThomas Zimmermann dev = &odev->dev;
1358c8a17756SThomas Zimmermann
1359c8a17756SThomas Zimmermann ret = drm_dev_register(dev, 0);
1360c8a17756SThomas Zimmermann if (ret)
1361c8a17756SThomas Zimmermann return ret;
1362c8a17756SThomas Zimmermann
1363d3533a8aSThomas Zimmermann color_mode = drm_format_info_bpp(odev->format, 0);
1364d3533a8aSThomas Zimmermann if (color_mode == 16)
1365d3533a8aSThomas Zimmermann color_mode = odev->format->depth; // can be 15 or 16
1366d3533a8aSThomas Zimmermann
1367d3533a8aSThomas Zimmermann drm_fbdev_generic_setup(dev, color_mode);
1368c8a17756SThomas Zimmermann
1369c8a17756SThomas Zimmermann return 0;
1370c8a17756SThomas Zimmermann }
1371c8a17756SThomas Zimmermann
ofdrm_remove(struct platform_device * pdev)1372*84e6da7aSUwe Kleine-König static void ofdrm_remove(struct platform_device *pdev)
1373c8a17756SThomas Zimmermann {
1374c8a17756SThomas Zimmermann struct drm_device *dev = platform_get_drvdata(pdev);
1375c8a17756SThomas Zimmermann
1376c8a17756SThomas Zimmermann drm_dev_unplug(dev);
1377c8a17756SThomas Zimmermann }
1378c8a17756SThomas Zimmermann
1379c8a17756SThomas Zimmermann static const struct of_device_id ofdrm_of_match_display[] = {
1380c8a17756SThomas Zimmermann { .compatible = "display", },
1381c8a17756SThomas Zimmermann { },
1382c8a17756SThomas Zimmermann };
1383c8a17756SThomas Zimmermann MODULE_DEVICE_TABLE(of, ofdrm_of_match_display);
1384c8a17756SThomas Zimmermann
1385c8a17756SThomas Zimmermann static struct platform_driver ofdrm_platform_driver = {
1386c8a17756SThomas Zimmermann .driver = {
1387c8a17756SThomas Zimmermann .name = "of-display",
1388c8a17756SThomas Zimmermann .of_match_table = ofdrm_of_match_display,
1389c8a17756SThomas Zimmermann },
1390c8a17756SThomas Zimmermann .probe = ofdrm_probe,
1391*84e6da7aSUwe Kleine-König .remove_new = ofdrm_remove,
1392c8a17756SThomas Zimmermann };
1393c8a17756SThomas Zimmermann
1394c8a17756SThomas Zimmermann module_platform_driver(ofdrm_platform_driver);
1395c8a17756SThomas Zimmermann
1396c8a17756SThomas Zimmermann MODULE_DESCRIPTION(DRIVER_DESC);
1397c8a17756SThomas Zimmermann MODULE_LICENSE("GPL");
1398