xref: /linux/drivers/video/fbdev/core/fb_draw.h (revision 3d0fe49454652117522f60bfbefb978ba0e5300b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _FB_DRAW_H
3 #define _FB_DRAW_H
4 
5 #include <asm/types.h>
6 #include <linux/fb.h>
7 #include <linux/bug.h>
8 
9     /*
10      *  Compose two values, using a bitmask as decision value
11      *  This is equivalent to (a & mask) | (b & ~mask)
12      */
13 
14 static inline unsigned long
15 comp(unsigned long a, unsigned long b, unsigned long mask)
16 {
17     return ((a ^ b) & mask) ^ b;
18 }
19 
20     /*
21      *  Create a pattern with the given pixel's color
22      */
23 
24 #if BITS_PER_LONG == 64
25 static inline unsigned long
26 pixel_to_pat( u32 bpp, u32 pixel)
27 {
28 	switch (bpp) {
29 	case 1:
30 		return 0xfffffffffffffffful*pixel;
31 	case 2:
32 		return 0x5555555555555555ul*pixel;
33 	case 4:
34 		return 0x1111111111111111ul*pixel;
35 	case 8:
36 		return 0x0101010101010101ul*pixel;
37 	case 12:
38 		return 0x1001001001001001ul*pixel;
39 	case 16:
40 		return 0x0001000100010001ul*pixel;
41 	case 24:
42 		return 0x0001000001000001ul*pixel;
43 	case 32:
44 		return 0x0000000100000001ul*pixel;
45 	default:
46 		WARN(1, "pixel_to_pat(): unsupported pixelformat %d\n", bpp);
47 		return 0;
48     }
49 }
50 #else
51 static inline unsigned long
52 pixel_to_pat( u32 bpp, u32 pixel)
53 {
54 	switch (bpp) {
55 	case 1:
56 		return 0xfffffffful*pixel;
57 	case 2:
58 		return 0x55555555ul*pixel;
59 	case 4:
60 		return 0x11111111ul*pixel;
61 	case 8:
62 		return 0x01010101ul*pixel;
63 	case 12:
64 		return 0x01001001ul*pixel;
65 	case 16:
66 		return 0x00010001ul*pixel;
67 	case 24:
68 		return 0x01000001ul*pixel;
69 	case 32:
70 		return 0x00000001ul*pixel;
71 	default:
72 		WARN(1, "pixel_to_pat(): unsupported pixelformat %d\n", bpp);
73 		return 0;
74     }
75 }
76 #endif
77 
78 #ifdef CONFIG_FB_CFB_REV_PIXELS_IN_BYTE
79 #if BITS_PER_LONG == 64
80 #define REV_PIXELS_MASK1 0x5555555555555555ul
81 #define REV_PIXELS_MASK2 0x3333333333333333ul
82 #define REV_PIXELS_MASK4 0x0f0f0f0f0f0f0f0ful
83 #else
84 #define REV_PIXELS_MASK1 0x55555555ul
85 #define REV_PIXELS_MASK2 0x33333333ul
86 #define REV_PIXELS_MASK4 0x0f0f0f0ful
87 #endif
88 
89 static inline unsigned long fb_rev_pixels_in_long(unsigned long val,
90 						  u32 bswapmask)
91 {
92 	if (bswapmask & 1)
93 		val = comp(val >> 1, val << 1, REV_PIXELS_MASK1);
94 	if (bswapmask & 2)
95 		val = comp(val >> 2, val << 2, REV_PIXELS_MASK2);
96 	if (bswapmask & 3)
97 		val = comp(val >> 4, val << 4, REV_PIXELS_MASK4);
98 	return val;
99 }
100 
101 static inline u32 fb_shifted_pixels_mask_u32(struct fb_info *p, u32 index,
102 					     u32 bswapmask)
103 {
104 	u32 mask;
105 
106 	if (!bswapmask) {
107 		mask = FB_SHIFT_HIGH(p, ~(u32)0, index);
108 	} else {
109 		mask = 0xff << FB_LEFT_POS(p, 8);
110 		mask = FB_SHIFT_LOW(p, mask, index & (bswapmask)) & mask;
111 		mask = FB_SHIFT_HIGH(p, mask, index & ~(bswapmask));
112 #if defined(__i386__) || defined(__x86_64__)
113 		/* Shift argument is limited to 0 - 31 on x86 based CPU's */
114 		if(index + bswapmask < 32)
115 #endif
116 			mask |= FB_SHIFT_HIGH(p, ~(u32)0,
117 					(index + bswapmask) & ~(bswapmask));
118 	}
119 	return mask;
120 }
121 
122 static inline unsigned long fb_shifted_pixels_mask_long(struct fb_info *p,
123 							u32 index,
124 							u32 bswapmask)
125 {
126 	unsigned long mask;
127 
128 	if (!bswapmask) {
129 		mask = FB_SHIFT_HIGH(p, ~0UL, index);
130 	} else {
131 		mask = 0xff << FB_LEFT_POS(p, 8);
132 		mask = FB_SHIFT_LOW(p, mask, index & (bswapmask)) & mask;
133 		mask = FB_SHIFT_HIGH(p, mask, index & ~(bswapmask));
134 #if defined(__i386__) || defined(__x86_64__)
135 		/* Shift argument is limited to 0 - 31 on x86 based CPU's */
136 		if(index + bswapmask < BITS_PER_LONG)
137 #endif
138 			mask |= FB_SHIFT_HIGH(p, ~0UL,
139 					(index + bswapmask) & ~(bswapmask));
140 	}
141 	return mask;
142 }
143 
144 
145 static inline u32 fb_compute_bswapmask(struct fb_info *info)
146 {
147 	u32 bswapmask = 0;
148 	unsigned bpp = info->var.bits_per_pixel;
149 
150 	if ((bpp < 8) && (info->var.nonstd & FB_NONSTD_REV_PIX_IN_B)) {
151 		/*
152 		 * Reversed order of pixel layout in bytes
153 		 * works only for 1, 2 and 4 bpp
154 		 */
155 		bswapmask = 7 - bpp + 1;
156 	}
157 	return bswapmask;
158 }
159 
160 #else /* CONFIG_FB_CFB_REV_PIXELS_IN_BYTE */
161 
162 static inline unsigned long fb_rev_pixels_in_long(unsigned long val,
163 						  u32 bswapmask)
164 {
165 	return val;
166 }
167 
168 #define fb_shifted_pixels_mask_u32(p, i, b) FB_SHIFT_HIGH((p), ~(u32)0, (i))
169 #define fb_shifted_pixels_mask_long(p, i, b) FB_SHIFT_HIGH((p), ~0UL, (i))
170 #define fb_compute_bswapmask(...) 0
171 
172 #endif  /* CONFIG_FB_CFB_REV_PIXELS_IN_BYTE */
173 
174 #define cpu_to_le_long _cpu_to_le_long(BITS_PER_LONG)
175 #define _cpu_to_le_long(x) __cpu_to_le_long(x)
176 #define __cpu_to_le_long(x) cpu_to_le##x
177 
178 #define le_long_to_cpu _le_long_to_cpu(BITS_PER_LONG)
179 #define _le_long_to_cpu(x) __le_long_to_cpu(x)
180 #define __le_long_to_cpu(x) le##x##_to_cpu
181 
182 static inline unsigned long rolx(unsigned long word, unsigned int shift, unsigned int x)
183 {
184 	return (word << shift) | (word >> (x - shift));
185 }
186 
187 #endif /* FB_DRAW_H */
188