blob: 624ee115f129291a2fc3adfe9ff7b4781e19f0ac [file] [log] [blame]
Antonino A. Daplasdc0e6e02007-05-08 00:39:08 -07001#ifndef _FB_DRAW_H
2#define _FB_DRAW_H
3
4#include <asm/types.h>
Pavel Pisa779121e2007-10-16 01:29:21 -07005#include <linux/fb.h>
Benjamin Herrenschmidt3f6a5582012-07-24 18:19:13 +10006#include <linux/bug.h>
Antonino A. Daplasdc0e6e02007-05-08 00:39:08 -07007
8 /*
9 * Compose two values, using a bitmask as decision value
10 * This is equivalent to (a & mask) | (b & ~mask)
11 */
12
13static inline unsigned long
14comp(unsigned long a, unsigned long b, unsigned long mask)
15{
16 return ((a ^ b) & mask) ^ b;
17}
18
19 /*
20 * Create a pattern with the given pixel's color
21 */
22
23#if BITS_PER_LONG == 64
24static inline unsigned long
25pixel_to_pat( u32 bpp, u32 pixel)
26{
27 switch (bpp) {
28 case 1:
29 return 0xfffffffffffffffful*pixel;
30 case 2:
31 return 0x5555555555555555ul*pixel;
32 case 4:
33 return 0x1111111111111111ul*pixel;
34 case 8:
35 return 0x0101010101010101ul*pixel;
36 case 12:
Michal Januszewskibdca0f92009-05-06 16:02:56 -070037 return 0x1001001001001001ul*pixel;
Antonino A. Daplasdc0e6e02007-05-08 00:39:08 -070038 case 16:
39 return 0x0001000100010001ul*pixel;
40 case 24:
Michal Januszewskibdca0f92009-05-06 16:02:56 -070041 return 0x0001000001000001ul*pixel;
Antonino A. Daplasdc0e6e02007-05-08 00:39:08 -070042 case 32:
43 return 0x0000000100000001ul*pixel;
44 default:
Benjamin Herrenschmidt3f6a5582012-07-24 18:19:13 +100045 WARN(1, "pixel_to_pat(): unsupported pixelformat %d\n", bpp);
46 return 0;
Antonino A. Daplasdc0e6e02007-05-08 00:39:08 -070047 }
48}
49#else
50static inline unsigned long
51pixel_to_pat( u32 bpp, u32 pixel)
52{
53 switch (bpp) {
54 case 1:
55 return 0xfffffffful*pixel;
56 case 2:
57 return 0x55555555ul*pixel;
58 case 4:
59 return 0x11111111ul*pixel;
60 case 8:
61 return 0x01010101ul*pixel;
62 case 12:
Michal Januszewskibdca0f92009-05-06 16:02:56 -070063 return 0x01001001ul*pixel;
Antonino A. Daplasdc0e6e02007-05-08 00:39:08 -070064 case 16:
65 return 0x00010001ul*pixel;
66 case 24:
Michal Januszewskibdca0f92009-05-06 16:02:56 -070067 return 0x01000001ul*pixel;
Antonino A. Daplasdc0e6e02007-05-08 00:39:08 -070068 case 32:
69 return 0x00000001ul*pixel;
70 default:
Benjamin Herrenschmidt3f6a5582012-07-24 18:19:13 +100071 WARN(1, "pixel_to_pat(): unsupported pixelformat %d\n", bpp);
72 return 0;
Antonino A. Daplasdc0e6e02007-05-08 00:39:08 -070073 }
74}
75#endif
Pavel Pisa779121e2007-10-16 01:29:21 -070076
77#ifdef CONFIG_FB_CFB_REV_PIXELS_IN_BYTE
Pavel Pisa15afdd42007-10-16 01:29:55 -070078#if BITS_PER_LONG == 64
79#define REV_PIXELS_MASK1 0x5555555555555555ul
80#define REV_PIXELS_MASK2 0x3333333333333333ul
81#define REV_PIXELS_MASK4 0x0f0f0f0f0f0f0f0ful
82#else
83#define REV_PIXELS_MASK1 0x55555555ul
84#define REV_PIXELS_MASK2 0x33333333ul
85#define REV_PIXELS_MASK4 0x0f0f0f0ful
86#endif
87
88static inline unsigned long fb_rev_pixels_in_long(unsigned long val,
89 u32 bswapmask)
90{
91 if (bswapmask & 1)
92 val = comp(val >> 1, val << 1, REV_PIXELS_MASK1);
93 if (bswapmask & 2)
94 val = comp(val >> 2, val << 2, REV_PIXELS_MASK2);
95 if (bswapmask & 3)
96 val = comp(val >> 4, val << 4, REV_PIXELS_MASK4);
Anton Vorontsov55850f42008-02-06 01:40:23 -080097 return val;
Pavel Pisa15afdd42007-10-16 01:29:55 -070098}
Pavel Pisa779121e2007-10-16 01:29:21 -070099
Anton Vorontsove4c690e2008-04-28 02:14:49 -0700100static inline u32 fb_shifted_pixels_mask_u32(struct fb_info *p, u32 index,
101 u32 bswapmask)
Pavel Pisa779121e2007-10-16 01:29:21 -0700102{
103 u32 mask;
104
105 if (!bswapmask) {
Anton Vorontsove4c690e2008-04-28 02:14:49 -0700106 mask = FB_SHIFT_HIGH(p, ~(u32)0, index);
Pavel Pisa779121e2007-10-16 01:29:21 -0700107 } else {
Anton Vorontsove4c690e2008-04-28 02:14:49 -0700108 mask = 0xff << FB_LEFT_POS(p, 8);
109 mask = FB_SHIFT_LOW(p, mask, index & (bswapmask)) & mask;
110 mask = FB_SHIFT_HIGH(p, mask, index & ~(bswapmask));
Pavel Pisa779121e2007-10-16 01:29:21 -0700111#if defined(__i386__) || defined(__x86_64__)
112 /* Shift argument is limited to 0 - 31 on x86 based CPU's */
113 if(index + bswapmask < 32)
114#endif
Anton Vorontsove4c690e2008-04-28 02:14:49 -0700115 mask |= FB_SHIFT_HIGH(p, ~(u32)0,
Pavel Pisa779121e2007-10-16 01:29:21 -0700116 (index + bswapmask) & ~(bswapmask));
117 }
118 return mask;
119}
120
Anton Vorontsove4c690e2008-04-28 02:14:49 -0700121static inline unsigned long fb_shifted_pixels_mask_long(struct fb_info *p,
122 u32 index,
123 u32 bswapmask)
Pavel Pisa779121e2007-10-16 01:29:21 -0700124{
125 unsigned long mask;
126
127 if (!bswapmask) {
Anton Vorontsove4c690e2008-04-28 02:14:49 -0700128 mask = FB_SHIFT_HIGH(p, ~0UL, index);
Pavel Pisa779121e2007-10-16 01:29:21 -0700129 } else {
Anton Vorontsove4c690e2008-04-28 02:14:49 -0700130 mask = 0xff << FB_LEFT_POS(p, 8);
131 mask = FB_SHIFT_LOW(p, mask, index & (bswapmask)) & mask;
132 mask = FB_SHIFT_HIGH(p, mask, index & ~(bswapmask));
Pavel Pisa779121e2007-10-16 01:29:21 -0700133#if defined(__i386__) || defined(__x86_64__)
134 /* Shift argument is limited to 0 - 31 on x86 based CPU's */
135 if(index + bswapmask < BITS_PER_LONG)
136#endif
Anton Vorontsove4c690e2008-04-28 02:14:49 -0700137 mask |= FB_SHIFT_HIGH(p, ~0UL,
Pavel Pisa779121e2007-10-16 01:29:21 -0700138 (index + bswapmask) & ~(bswapmask));
139 }
140 return mask;
141}
142
143
144static inline u32 fb_compute_bswapmask(struct fb_info *info)
145{
146 u32 bswapmask = 0;
147 unsigned bpp = info->var.bits_per_pixel;
148
149 if ((bpp < 8) && (info->var.nonstd & FB_NONSTD_REV_PIX_IN_B)) {
150 /*
151 * Reversed order of pixel layout in bytes
152 * works only for 1, 2 and 4 bpp
153 */
154 bswapmask = 7 - bpp + 1;
155 }
156 return bswapmask;
157}
158
159#else /* CONFIG_FB_CFB_REV_PIXELS_IN_BYTE */
160
Pavel Pisa15afdd42007-10-16 01:29:55 -0700161static inline unsigned long fb_rev_pixels_in_long(unsigned long val,
162 u32 bswapmask)
163{
164 return val;
165}
166
Anton Vorontsove4c690e2008-04-28 02:14:49 -0700167#define fb_shifted_pixels_mask_u32(p, i, b) FB_SHIFT_HIGH((p), ~(u32)0, (i))
168#define fb_shifted_pixels_mask_long(p, i, b) FB_SHIFT_HIGH((p), ~0UL, (i))
Pavel Pisa779121e2007-10-16 01:29:21 -0700169#define fb_compute_bswapmask(...) 0
170
171#endif /* CONFIG_FB_CFB_REV_PIXELS_IN_BYTE */
172
Michal Januszewskibdca0f92009-05-06 16:02:56 -0700173#define cpu_to_le_long _cpu_to_le_long(BITS_PER_LONG)
174#define _cpu_to_le_long(x) __cpu_to_le_long(x)
175#define __cpu_to_le_long(x) cpu_to_le##x
176
177#define le_long_to_cpu _le_long_to_cpu(BITS_PER_LONG)
178#define _le_long_to_cpu(x) __le_long_to_cpu(x)
179#define __le_long_to_cpu(x) le##x##_to_cpu
180
181static inline unsigned long rolx(unsigned long word, unsigned int shift, unsigned int x)
182{
183 return (word << shift) | (word >> (x - shift));
184}
185
Antonino A. Daplasdc0e6e02007-05-08 00:39:08 -0700186#endif /* FB_DRAW_H */