1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Support for Intel Camera Imaging ISP subsystem.
4  * Copyright (c) 2015, Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  */
15 
16 #ifndef __SH_CSS_FRAC_H
17 #define __SH_CSS_FRAC_H
18 
19 #include <linux/minmax.h>
20 
21 #include "mamoiada_params.h"
22 
23 #define sISP_REG_BIT		      ISP_VEC_ELEMBITS
24 #define uISP_REG_BIT		      ((unsigned int)(sISP_REG_BIT - 1))
25 #define sSHIFT				    (16 - sISP_REG_BIT)
26 #define uSHIFT				    ((unsigned int)(16 - uISP_REG_BIT))
27 #define sFRACTION_BITS_FITTING(a) (a - sSHIFT)
28 #define uFRACTION_BITS_FITTING(a) ((unsigned int)(a - uSHIFT))
29 #define sISP_VAL_MIN		      (-(1 << uISP_REG_BIT))
30 #define sISP_VAL_MAX		      ((1 << uISP_REG_BIT) - 1)
31 #define uISP_VAL_MIN		      (0U)
32 #define uISP_VAL_MAX		      ((unsigned int)((1 << uISP_REG_BIT) - 1))
33 
34 /* a:fraction bits for 16bit precision, b:fraction bits for ISP precision */
sDIGIT_FITTING(int v,int a,int b)35 static inline int sDIGIT_FITTING(int v, int a, int b)
36 {
37 	int fit_shift = sFRACTION_BITS_FITTING(a) - b;
38 
39 	v >>= sSHIFT;
40 	v >>= fit_shift > 0 ? fit_shift : 0;
41 
42 	return clamp_t(int, v, sISP_VAL_MIN, sISP_VAL_MAX);
43 }
44 
uDIGIT_FITTING(unsigned int v,int a,int b)45 static inline unsigned int uDIGIT_FITTING(unsigned int v, int a, int b)
46 {
47 	int fit_shift = uFRACTION_BITS_FITTING(a) - b;
48 
49 	v >>= uSHIFT;
50 	v >>= fit_shift > 0 ? fit_shift : 0;
51 
52 	return clamp_t(unsigned int, v, uISP_VAL_MIN, uISP_VAL_MAX);
53 }
54 
55 #endif /* __SH_CSS_FRAC_H */
56