]> git.sur5r.net Git - groeck-nct6775/blob - compat.h
README: Update to reflect that the driver is available upstream
[groeck-nct6775] / compat.h
1 #ifndef __COMPAT_H
2 #define __COMPAT_H
3
4 #include <linux/version.h>
5
6 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
7 #error This driver is for kernel versions 2.6.16 and later
8 #endif
9
10 #if !defined (CONFIG_HWMON_VID) && !defined(CONFIG_HWMON_VID_MODULE)
11 int vid_from_reg(int val, u8 vrm)
12 {
13         return 0;
14 }
15
16 u8 vid_which_vrm(void)
17 {
18         return 0;
19 }
20 #endif
21
22 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
23 static int sysfs_create_groups(struct kobject *kobj,
24                                const struct attribute_group **groups)
25 {
26         int error = 0;
27         int i;
28
29         if (!groups)
30                 return 0;
31
32         for (i = 0; groups[i]; i++) {
33                 error = sysfs_create_group(kobj, groups[i]);
34                 if (error) {
35                         while (--i >= 0)
36                                 sysfs_remove_group(kobj, groups[i]);
37                         break;
38                 }
39         }
40         return error;
41 }
42
43 static void sysfs_remove_groups(struct kobject *kobj,
44                                 const struct attribute_group **groups)
45 {
46         int i;
47
48         if (!groups)
49                 return;
50         for (i = 0; groups[i]; i++)
51                 sysfs_remove_group(kobj, groups[i]);
52 }
53
54 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
55 {
56         if (IS_ERR(ptr))
57                 return PTR_ERR(ptr);
58         else
59                 return 0;
60 }
61
62 #endif
63
64 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 21)
65 #if !(defined RHEL_MAJOR && RHEL_MAJOR == 5 && RHEL_MINOR >= 6)
66 /* Simplified version for compatibility */
67 struct i2c_board_info {
68         char            type[I2C_NAME_SIZE];
69         unsigned short  flags;
70         unsigned short  addr;
71 };
72 #endif
73 #endif
74
75 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 25)
76 /* Some older kernels have a different, useless struct i2c_device_id */
77 #define i2c_device_id i2c_device_id_compat
78 struct i2c_device_id {
79         char name[I2C_NAME_SIZE];
80         kernel_ulong_t driver_data      /* Data private to the driver */
81                         __attribute__((aligned(sizeof(kernel_ulong_t))));
82 };
83 #endif
84
85 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
86 static unsigned short empty_i2c[] =  { I2C_CLIENT_END };
87 static struct i2c_client_address_data addr_data = {
88         .normal_i2c     = normal_i2c,
89         .probe          = empty_i2c,
90         .ignore         = empty_i2c,
91 };
92 #endif
93
94 /* Red Hat EL5 includes backports of these functions, so we can't redefine
95  * our own. */
96 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
97 #if !(defined RHEL_MAJOR && RHEL_MAJOR == 5 && RHEL_MINOR >= 5)
98 static inline int strict_strtoul(const char *cp, unsigned int base,
99                                  unsigned long *res)
100 {
101         *res = simple_strtoul(cp, NULL, base);
102         return 0;
103 }
104
105 static inline int strict_strtol(const char *cp, unsigned int base, long *res)
106 {
107         *res = simple_strtol(cp, NULL, base);
108         return 0;
109 }
110 #endif
111 #endif
112
113 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 28)
114 /*
115  * Divide positive or negative dividend by positive divisor and round
116  * to closest integer. Result is undefined for negative divisors and
117  * for negative dividends if the divisor variable type is unsigned.
118  */
119 #define DIV_ROUND_CLOSEST(x, divisor)(                  \
120 {                                                       \
121         typeof(x) __x = x;                              \
122         typeof(divisor) __d = divisor;                  \
123         (((typeof(x))-1) > 0 ||                         \
124          ((typeof(divisor))-1) > 0 || (__x) > 0) ?      \
125                 (((__x) + ((__d) / 2)) / (__d)) :       \
126                 (((__x) - ((__d) / 2)) / (__d));        \
127 }                                                       \
128 )
129 #endif
130
131 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)
132 static inline s32
133 i2c_smbus_read_word_swapped(const struct i2c_client *client, u8 command)
134 {
135         s32 value = i2c_smbus_read_word_data(client, command);
136
137         return (value < 0) ? value : swab16(value);
138 }
139
140 static inline s32
141 i2c_smbus_write_word_swapped(const struct i2c_client *client,
142                              u8 command, u16 value)
143 {
144         return i2c_smbus_write_word_data(client, command, swab16(value));
145 }
146 #endif
147
148 #ifndef module_driver
149 /**
150  * module_driver() - Helper macro for drivers that don't do anything
151  * special in module init/exit. This eliminates a lot of boilerplate.
152  * Each module may only use this macro once, and calling it replaces
153  * module_init() and module_exit().
154  *
155  * @__driver: driver name
156  * @__register: register function for this driver type
157  * @__unregister: unregister function for this driver type
158  * @...: Additional arguments to be passed to __register and __unregister.
159  *
160  * Use this macro to construct bus specific macros for registering
161  * drivers, and do not use it on its own.
162  */
163 #define module_driver(__driver, __register, __unregister, ...) \
164 static int __init __driver##_init(void) \
165 { \
166         return __register(&(__driver) , ##__VA_ARGS__); \
167 } \
168 module_init(__driver##_init); \
169 static void __exit __driver##_exit(void) \
170 { \
171         __unregister(&(__driver) , ##__VA_ARGS__); \
172 } \
173 module_exit(__driver##_exit);
174 #endif
175
176 #ifndef module_i2c_driver
177 /**
178  * module_i2c_driver() - Helper macro for registering a I2C driver
179  * @__i2c_driver: i2c_driver struct
180  *
181  * Helper macro for I2C drivers which do not do anything special in module
182  * init/exit. This eliminates a lot of boilerplate. Each module may only
183  * use this macro once, and calling it replaces module_init() and module_exit()
184  */
185 #define module_i2c_driver(__i2c_driver) \
186         module_driver(__i2c_driver, i2c_add_driver, \
187                         i2c_del_driver)
188 #endif
189
190 #ifndef clamp_val
191 #define clamp_val SENSORS_LIMIT
192 #endif
193
194 #ifndef kstrtol
195 #define kstrtol strict_strtol
196 #endif
197 #ifndef kstrtoul
198 #define kstrtoul strict_strtoul
199 #endif
200
201 #ifndef request_muxed_region
202 #define request_muxed_region(a, b, c) (true)
203 #define release_region(a, b)
204 #endif
205
206 #endif /* __COMPAT_H */