52a65a8a056ff8001a5f7a1ab2912acc135e9442
[sfrench/cifs-2.6.git] / drivers / gpu / drm / arm / malidp_hw.h
1 /*
2  *
3  * (C) COPYRIGHT 2013-2016 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * ARM Mali DP hardware manipulation routines.
11  */
12
13 #ifndef __MALIDP_HW_H__
14 #define __MALIDP_HW_H__
15
16 #include <linux/bitops.h>
17 #include "malidp_regs.h"
18
19 struct videomode;
20 struct clk;
21
22 /* Mali DP IP blocks */
23 enum {
24         MALIDP_DE_BLOCK = 0,
25         MALIDP_SE_BLOCK,
26         MALIDP_DC_BLOCK
27 };
28
29 /* Mali DP layer IDs */
30 enum {
31         DE_VIDEO1 = BIT(0),
32         DE_GRAPHICS1 = BIT(1),
33         DE_GRAPHICS2 = BIT(2), /* used only in DP500 */
34         DE_VIDEO2 = BIT(3),
35         DE_SMART = BIT(4),
36 };
37
38 struct malidp_format_id {
39         u32 format;             /* DRM fourcc */
40         u8 layer;               /* bitmask of layers supporting it */
41         u8 id;                  /* used internally */
42 };
43
44 #define MALIDP_INVALID_FORMAT_ID        0xff
45
46 /*
47  * hide the differences between register maps
48  * by using a common structure to hold the
49  * base register offsets
50  */
51
52 struct malidp_irq_map {
53         u32 irq_mask;           /* mask of IRQs that can be enabled in the block */
54         u32 vsync_irq;          /* IRQ bit used for signaling during VSYNC */
55 };
56
57 struct malidp_layer {
58         u16 id;                 /* layer ID */
59         u16 base;               /* address offset for the register bank */
60         u16 ptr;                /* address offset for the pointer register */
61         u16 stride_offset;      /* Offset to the first stride register. */
62 };
63
64 enum malidp_scaling_coeff_set {
65         MALIDP_UPSCALING_COEFFS = 1,
66         MALIDP_DOWNSCALING_1_5_COEFFS = 2,
67         MALIDP_DOWNSCALING_2_COEFFS = 3,
68         MALIDP_DOWNSCALING_2_75_COEFFS = 4,
69         MALIDP_DOWNSCALING_4_COEFFS = 5,
70 };
71
72 struct malidp_se_config {
73         u8 scale_enable : 1;
74         u8 hcoeff : 3;
75         u8 vcoeff : 3;
76         u8 plane_src_id;
77         u16 input_w, input_h;
78         u16 output_w, output_h;
79         u32 h_init_phase, h_delta_phase;
80         u32 v_init_phase, v_delta_phase;
81 };
82
83 /* regmap features */
84 #define MALIDP_REGMAP_HAS_CLEARIRQ      (1 << 0)
85
86 struct malidp_hw_regmap {
87         /* address offset of the DE register bank */
88         /* is always 0x0000 */
89         /* address offset of the DE coefficients registers */
90         const u16 coeffs_base;
91         /* address offset of the SE registers bank */
92         const u16 se_base;
93         /* address offset of the DC registers bank */
94         const u16 dc_base;
95
96         /* address offset for the output depth register */
97         const u16 out_depth_base;
98
99         /* bitmap with register map features */
100         const u8 features;
101
102         /* list of supported layers */
103         const u8 n_layers;
104         const struct malidp_layer *layers;
105
106         const struct malidp_irq_map de_irq_map;
107         const struct malidp_irq_map se_irq_map;
108         const struct malidp_irq_map dc_irq_map;
109
110         /* list of supported pixel formats for each layer */
111         const struct malidp_format_id *pixel_formats;
112         const u8 n_pixel_formats;
113
114         /* pitch alignment requirement in bytes */
115         const u8 bus_align_bytes;
116 };
117
118 /* device features */
119 /* Unlike DP550/650, DP500 has 3 stride registers in its video layer. */
120 #define MALIDP_DEVICE_LV_HAS_3_STRIDES  BIT(0)
121
122 struct malidp_hw_device {
123         const struct malidp_hw_regmap map;
124         void __iomem *regs;
125
126         /* APB clock */
127         struct clk *pclk;
128         /* AXI clock */
129         struct clk *aclk;
130         /* main clock for display core */
131         struct clk *mclk;
132         /* pixel clock for display core */
133         struct clk *pxlclk;
134
135         /*
136          * Validate the driver instance against the hardware bits
137          */
138         int (*query_hw)(struct malidp_hw_device *hwdev);
139
140         /*
141          * Set the hardware into config mode, ready to accept mode changes
142          */
143         void (*enter_config_mode)(struct malidp_hw_device *hwdev);
144
145         /*
146          * Tell hardware to exit configuration mode
147          */
148         void (*leave_config_mode)(struct malidp_hw_device *hwdev);
149
150         /*
151          * Query if hardware is in configuration mode
152          */
153         bool (*in_config_mode)(struct malidp_hw_device *hwdev);
154
155         /*
156          * Set configuration valid flag for hardware parameters that can
157          * be changed outside the configuration mode. Hardware will use
158          * the new settings when config valid is set after the end of the
159          * current buffer scanout
160          */
161         void (*set_config_valid)(struct malidp_hw_device *hwdev);
162
163         /*
164          * Set a new mode in hardware. Requires the hardware to be in
165          * configuration mode before this function is called.
166          */
167         void (*modeset)(struct malidp_hw_device *hwdev, struct videomode *m);
168
169         /*
170          * Calculate the required rotation memory given the active area
171          * and the buffer format.
172          */
173         int (*rotmem_required)(struct malidp_hw_device *hwdev, u16 w, u16 h, u32 fmt);
174
175         int (*se_set_scaling_coeffs)(struct malidp_hw_device *hwdev,
176                                      struct malidp_se_config *se_config,
177                                      struct malidp_se_config *old_config);
178
179         u8 features;
180
181         u8 min_line_size;
182         u16 max_line_size;
183
184         /* track the device PM state */
185         bool pm_suspended;
186
187         /* size of memory used for rotating layers, up to two banks available */
188         u32 rotation_memory[2];
189 };
190
191 /* Supported variants of the hardware */
192 enum {
193         MALIDP_500 = 0,
194         MALIDP_550,
195         MALIDP_650,
196         /* keep the next entry last */
197         MALIDP_MAX_DEVICES
198 };
199
200 extern const struct malidp_hw_device malidp_device[MALIDP_MAX_DEVICES];
201
202 static inline u32 malidp_hw_read(struct malidp_hw_device *hwdev, u32 reg)
203 {
204         WARN_ON(hwdev->pm_suspended);
205         return readl(hwdev->regs + reg);
206 }
207
208 static inline void malidp_hw_write(struct malidp_hw_device *hwdev,
209                                    u32 value, u32 reg)
210 {
211         WARN_ON(hwdev->pm_suspended);
212         writel(value, hwdev->regs + reg);
213 }
214
215 static inline void malidp_hw_setbits(struct malidp_hw_device *hwdev,
216                                      u32 mask, u32 reg)
217 {
218         u32 data = malidp_hw_read(hwdev, reg);
219
220         data |= mask;
221         malidp_hw_write(hwdev, data, reg);
222 }
223
224 static inline void malidp_hw_clearbits(struct malidp_hw_device *hwdev,
225                                        u32 mask, u32 reg)
226 {
227         u32 data = malidp_hw_read(hwdev, reg);
228
229         data &= ~mask;
230         malidp_hw_write(hwdev, data, reg);
231 }
232
233 static inline u32 malidp_get_block_base(struct malidp_hw_device *hwdev,
234                                         u8 block)
235 {
236         switch (block) {
237         case MALIDP_SE_BLOCK:
238                 return hwdev->map.se_base;
239         case MALIDP_DC_BLOCK:
240                 return hwdev->map.dc_base;
241         }
242
243         return 0;
244 }
245
246 static inline void malidp_hw_disable_irq(struct malidp_hw_device *hwdev,
247                                          u8 block, u32 irq)
248 {
249         u32 base = malidp_get_block_base(hwdev, block);
250
251         malidp_hw_clearbits(hwdev, irq, base + MALIDP_REG_MASKIRQ);
252 }
253
254 static inline void malidp_hw_enable_irq(struct malidp_hw_device *hwdev,
255                                         u8 block, u32 irq)
256 {
257         u32 base = malidp_get_block_base(hwdev, block);
258
259         malidp_hw_setbits(hwdev, irq, base + MALIDP_REG_MASKIRQ);
260 }
261
262 int malidp_de_irq_init(struct drm_device *drm, int irq);
263 void malidp_de_irq_fini(struct drm_device *drm);
264 int malidp_se_irq_init(struct drm_device *drm, int irq);
265 void malidp_se_irq_fini(struct drm_device *drm);
266
267 u8 malidp_hw_get_format_id(const struct malidp_hw_regmap *map,
268                            u8 layer_id, u32 format);
269
270 static inline bool malidp_hw_pitch_valid(struct malidp_hw_device *hwdev,
271                                          unsigned int pitch)
272 {
273         return !(pitch & (hwdev->map.bus_align_bytes - 1));
274 }
275
276 /* U16.16 */
277 #define FP_1_00000      0x00010000      /* 1.0 */
278 #define FP_0_66667      0x0000AAAA      /* 0.6667 = 1/1.5 */
279 #define FP_0_50000      0x00008000      /* 0.5 = 1/2 */
280 #define FP_0_36363      0x00005D17      /* 0.36363 = 1/2.75 */
281 #define FP_0_25000      0x00004000      /* 0.25 = 1/4 */
282
283 static inline enum malidp_scaling_coeff_set
284 malidp_se_select_coeffs(u32 upscale_factor)
285 {
286         return (upscale_factor >= FP_1_00000) ? MALIDP_UPSCALING_COEFFS :
287                (upscale_factor >= FP_0_66667) ? MALIDP_DOWNSCALING_1_5_COEFFS :
288                (upscale_factor >= FP_0_50000) ? MALIDP_DOWNSCALING_2_COEFFS :
289                (upscale_factor >= FP_0_36363) ? MALIDP_DOWNSCALING_2_75_COEFFS :
290                MALIDP_DOWNSCALING_4_COEFFS;
291 }
292
293 #undef FP_0_25000
294 #undef FP_0_36363
295 #undef FP_0_50000
296 #undef FP_0_66667
297 #undef FP_1_00000
298 /*
299  * background color components are defined as 12bits values,
300  * they will be shifted right when stored on hardware that
301  * supports only 8bits per channel
302  */
303 #define MALIDP_BGND_COLOR_R             0x000
304 #define MALIDP_BGND_COLOR_G             0x000
305 #define MALIDP_BGND_COLOR_B             0x000
306
307 #define MALIDP_COLORADJ_NUM_COEFFS      12
308 #define MALIDP_COEFFTAB_NUM_COEFFS      64
309
310 #define MALIDP_GAMMA_LUT_SIZE           4096
311
312 #endif  /* __MALIDP_HW_H__ */