Commit 731b4cace10f105a81d622b70f9a35f577612d63
Committed by
Guenter Roeck
1 parent
02f8c6aee8
Exists in
master
and in
20 other branches
hwmon: Driver for MAX1668
This patch adds support for MAX1668 and compatible temperature sensors. Signed-off-by: David George <david.george@ska.ac.za> [guenter.roeck@ericsson.com: minor cleanup of probe error path] Signed-off-by: Guenter Roeck <guenter.roeck@ericsson.com>
Showing 5 changed files with 558 additions and 0 deletions Side-by-side Diff
Documentation/hwmon/max1668
1 | +Kernel driver max1668 | |
2 | +===================== | |
3 | + | |
4 | +Supported chips: | |
5 | + * Maxim MAX1668, MAX1805 and MAX1989 | |
6 | + Prefix: 'max1668' | |
7 | + Addresses scanned: I2C 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e | |
8 | + Datasheet: http://datasheets.maxim-ic.com/en/ds/MAX1668-MAX1989.pdf | |
9 | + | |
10 | +Author: | |
11 | + David George <david.george@ska.ac.za> | |
12 | + | |
13 | +Description | |
14 | +----------- | |
15 | + | |
16 | +This driver implements support for the Maxim MAX1668, MAX1805 and MAX1989 | |
17 | +chips. | |
18 | + | |
19 | +The three devices are very similar, but the MAX1805 has a reduced feature | |
20 | +set; only two remote temperature inputs vs the four avaible on the other | |
21 | +two ICs. | |
22 | + | |
23 | +The driver is able to distinguish between the devices and creates sysfs | |
24 | +entries as follows: | |
25 | + | |
26 | +MAX1805, MAX1668 and MAX1989: | |
27 | + | |
28 | +temp1_input ro local (ambient) temperature | |
29 | +temp1_max rw local temperature maximum threshold for alarm | |
30 | +temp1_max_alarm ro local temperature maximum threshold alarm | |
31 | +temp1_min rw local temperature minimum threshold for alarm | |
32 | +temp1_min_alarm ro local temperature minimum threshold alarm | |
33 | +temp2_input ro remote temperature 1 | |
34 | +temp2_max rw remote temperature 1 maximum threshold for alarm | |
35 | +temp2_max_alarm ro remote temperature 1 maximum threshold alarm | |
36 | +temp2_min rw remote temperature 1 minimum threshold for alarm | |
37 | +temp2_min_alarm ro remote temperature 1 minimum threshold alarm | |
38 | +temp3_input ro remote temperature 2 | |
39 | +temp3_max rw remote temperature 2 maximum threshold for alarm | |
40 | +temp3_max_alarm ro remote temperature 2 maximum threshold alarm | |
41 | +temp3_min rw remote temperature 2 minimum threshold for alarm | |
42 | +temp3_min_alarm ro remote temperature 2 minimum threshold alarm | |
43 | + | |
44 | +MAX1668 and MAX1989 only: | |
45 | +temp4_input ro remote temperature 3 | |
46 | +temp4_max rw remote temperature 3 maximum threshold for alarm | |
47 | +temp4_max_alarm ro remote temperature 3 maximum threshold alarm | |
48 | +temp4_min rw remote temperature 3 minimum threshold for alarm | |
49 | +temp4_min_alarm ro remote temperature 3 minimum threshold alarm | |
50 | +temp5_input ro remote temperature 4 | |
51 | +temp5_max rw remote temperature 4 maximum threshold for alarm | |
52 | +temp5_max_alarm ro remote temperature 4 maximum threshold alarm | |
53 | +temp5_min rw remote temperature 4 minimum threshold for alarm | |
54 | +temp5_min_alarm ro remote temperature 4 minimum threshold alarm | |
55 | + | |
56 | +Module Parameters | |
57 | +----------------- | |
58 | + | |
59 | +* read_only: int | |
60 | + Set to non-zero if you wish to prevent write access to alarm thresholds. |
MAINTAINERS
... | ... | @@ -4120,6 +4120,13 @@ |
4120 | 4120 | F: drivers/video/matrox/matroxfb_* |
4121 | 4121 | F: include/linux/matroxfb.h |
4122 | 4122 | |
4123 | +MAX1668 TEMPERATURE SENSOR DRIVER | |
4124 | +M: "David George" <david.george@ska.ac.za> | |
4125 | +L: lm-sensors@lm-sensors.org | |
4126 | +S: Maintained | |
4127 | +F: Documentation/hwmon/max1668 | |
4128 | +F: drivers/hwmon/max1668.c | |
4129 | + | |
4123 | 4130 | MAX6650 HARDWARE MONITOR AND FAN CONTROLLER DRIVER |
4124 | 4131 | M: "Hans J. Koch" <hjk@hansjkoch.de> |
4125 | 4132 | L: lm-sensors@lm-sensors.org |
drivers/hwmon/Kconfig
... | ... | @@ -736,6 +736,16 @@ |
736 | 736 | This driver can also be built as a module. If so, the module |
737 | 737 | will be called max1619. |
738 | 738 | |
739 | +config SENSORS_MAX1668 | |
740 | + tristate "Maxim MAX1668 and compatibles" | |
741 | + depends on I2C && EXPERIMENTAL | |
742 | + help | |
743 | + If you say yes here you get support for MAX1668, MAX1989 and | |
744 | + MAX1805 chips. | |
745 | + | |
746 | + This driver can also be built as a module. If so, the module | |
747 | + will be called max1668. | |
748 | + | |
739 | 749 | config SENSORS_MAX6639 |
740 | 750 | tristate "Maxim MAX6639 sensor chip" |
741 | 751 | depends on I2C && EXPERIMENTAL |
drivers/hwmon/Makefile
... | ... | @@ -87,6 +87,7 @@ |
87 | 87 | obj-$(CONFIG_SENSORS_MAX1111) += max1111.o |
88 | 88 | obj-$(CONFIG_SENSORS_MAX16065) += max16065.o |
89 | 89 | obj-$(CONFIG_SENSORS_MAX1619) += max1619.o |
90 | +obj-$(CONFIG_SENSORS_MAX1668) += max1668.o | |
90 | 91 | obj-$(CONFIG_SENSORS_MAX6639) += max6639.o |
91 | 92 | obj-$(CONFIG_SENSORS_MAX6642) += max6642.o |
92 | 93 | obj-$(CONFIG_SENSORS_MAX6650) += max6650.o |
drivers/hwmon/max1668.c
1 | +/* | |
2 | + Copyright (c) 2011 David George <david.george@ska.ac.za> | |
3 | + | |
4 | + based on adm1021.c | |
5 | + some credit to Christoph Scheurer, but largely a rewrite | |
6 | + | |
7 | + This program is free software; you can redistribute it and/or modify | |
8 | + it under the terms of the GNU General Public License as published by | |
9 | + the Free Software Foundation; either version 2 of the License, or | |
10 | + (at your option) any later version. | |
11 | + | |
12 | + This program is distributed in the hope that it will be useful, | |
13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | + GNU General Public License for more details. | |
16 | + | |
17 | + You should have received a copy of the GNU General Public License | |
18 | + along with this program; if not, write to the Free Software | |
19 | + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | +*/ | |
21 | + | |
22 | +#include <linux/module.h> | |
23 | +#include <linux/init.h> | |
24 | +#include <linux/slab.h> | |
25 | +#include <linux/jiffies.h> | |
26 | +#include <linux/i2c.h> | |
27 | +#include <linux/hwmon.h> | |
28 | +#include <linux/hwmon-sysfs.h> | |
29 | +#include <linux/err.h> | |
30 | +#include <linux/mutex.h> | |
31 | + | |
32 | +/* Addresses to scan */ | |
33 | +static unsigned short max1668_addr_list[] = { | |
34 | + 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; | |
35 | + | |
36 | +/* max1668 registers */ | |
37 | + | |
38 | +#define MAX1668_REG_TEMP(nr) (nr) | |
39 | +#define MAX1668_REG_STAT1 0x05 | |
40 | +#define MAX1668_REG_STAT2 0x06 | |
41 | +#define MAX1668_REG_MAN_ID 0xfe | |
42 | +#define MAX1668_REG_DEV_ID 0xff | |
43 | + | |
44 | +/* limits */ | |
45 | + | |
46 | +/* write high limits */ | |
47 | +#define MAX1668_REG_LIMH_WR(nr) (0x13 + 2 * (nr)) | |
48 | +/* write low limits */ | |
49 | +#define MAX1668_REG_LIML_WR(nr) (0x14 + 2 * (nr)) | |
50 | +/* read high limits */ | |
51 | +#define MAX1668_REG_LIMH_RD(nr) (0x08 + 2 * (nr)) | |
52 | +/* read low limits */ | |
53 | +#define MAX1668_REG_LIML_RD(nr) (0x09 + 2 * (nr)) | |
54 | + | |
55 | +/* manufacturer and device ID Constants */ | |
56 | +#define MAN_ID_MAXIM 0x4d | |
57 | +#define DEV_ID_MAX1668 0x3 | |
58 | +#define DEV_ID_MAX1805 0x5 | |
59 | +#define DEV_ID_MAX1989 0xb | |
60 | + | |
61 | +/* read only mode module parameter */ | |
62 | +static int read_only; | |
63 | +module_param(read_only, bool, 0); | |
64 | +MODULE_PARM_DESC(read_only, "Don't set any values, read only mode"); | |
65 | + | |
66 | +enum chips { max1668, max1805, max1989 }; | |
67 | + | |
68 | +struct max1668_data { | |
69 | + struct device *hwmon_dev; | |
70 | + enum chips type; | |
71 | + | |
72 | + struct mutex update_lock; | |
73 | + char valid; /* !=0 if following fields are valid */ | |
74 | + unsigned long last_updated; /* In jiffies */ | |
75 | + | |
76 | + /* 1x local and 4x remote */ | |
77 | + s8 temp_max[5]; | |
78 | + s8 temp_min[5]; | |
79 | + s8 temp[5]; | |
80 | + u16 alarms; | |
81 | +}; | |
82 | + | |
83 | +static struct max1668_data *max1668_update_device(struct device *dev) | |
84 | +{ | |
85 | + struct i2c_client *client = to_i2c_client(dev); | |
86 | + struct max1668_data *data = i2c_get_clientdata(client); | |
87 | + struct max1668_data *ret = data; | |
88 | + s32 val; | |
89 | + int i; | |
90 | + | |
91 | + mutex_lock(&data->update_lock); | |
92 | + | |
93 | + if (data->valid && !time_after(jiffies, | |
94 | + data->last_updated + HZ + HZ / 2)) | |
95 | + goto abort; | |
96 | + | |
97 | + for (i = 0; i < 5; i++) { | |
98 | + val = i2c_smbus_read_byte_data(client, MAX1668_REG_TEMP(i)); | |
99 | + if (unlikely(val < 0)) { | |
100 | + ret = ERR_PTR(val); | |
101 | + goto abort; | |
102 | + } | |
103 | + data->temp[i] = (s8) val; | |
104 | + | |
105 | + val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIMH_RD(i)); | |
106 | + if (unlikely(val < 0)) { | |
107 | + ret = ERR_PTR(val); | |
108 | + goto abort; | |
109 | + } | |
110 | + data->temp_max[i] = (s8) val; | |
111 | + | |
112 | + val = i2c_smbus_read_byte_data(client, MAX1668_REG_LIML_RD(i)); | |
113 | + if (unlikely(val < 0)) { | |
114 | + ret = ERR_PTR(val); | |
115 | + goto abort; | |
116 | + } | |
117 | + data->temp_min[i] = (s8) val; | |
118 | + } | |
119 | + | |
120 | + val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT1); | |
121 | + if (unlikely(val < 0)) { | |
122 | + ret = ERR_PTR(val); | |
123 | + goto abort; | |
124 | + } | |
125 | + data->alarms &= 0x00ff; | |
126 | + data->alarms |= ((u8) (val & 0x60)) << 8; | |
127 | + | |
128 | + val = i2c_smbus_read_byte_data(client, MAX1668_REG_STAT2); | |
129 | + if (unlikely(val < 0)) { | |
130 | + ret = ERR_PTR(val); | |
131 | + goto abort; | |
132 | + } | |
133 | + data->alarms &= 0xff00; | |
134 | + data->alarms |= ((u8) val); | |
135 | + | |
136 | + data->last_updated = jiffies; | |
137 | + data->valid = 1; | |
138 | +abort: | |
139 | + mutex_unlock(&data->update_lock); | |
140 | + | |
141 | + return ret; | |
142 | +} | |
143 | + | |
144 | +static ssize_t show_temp(struct device *dev, | |
145 | + struct device_attribute *devattr, char *buf) | |
146 | +{ | |
147 | + int index = to_sensor_dev_attr(devattr)->index; | |
148 | + struct max1668_data *data = max1668_update_device(dev); | |
149 | + | |
150 | + if (IS_ERR(data)) | |
151 | + return PTR_ERR(data); | |
152 | + | |
153 | + return sprintf(buf, "%d\n", data->temp[index] * 1000); | |
154 | +} | |
155 | + | |
156 | +static ssize_t show_temp_max(struct device *dev, | |
157 | + struct device_attribute *devattr, char *buf) | |
158 | +{ | |
159 | + int index = to_sensor_dev_attr(devattr)->index; | |
160 | + struct max1668_data *data = max1668_update_device(dev); | |
161 | + | |
162 | + if (IS_ERR(data)) | |
163 | + return PTR_ERR(data); | |
164 | + | |
165 | + return sprintf(buf, "%d\n", data->temp_max[index] * 1000); | |
166 | +} | |
167 | + | |
168 | +static ssize_t show_temp_min(struct device *dev, | |
169 | + struct device_attribute *devattr, char *buf) | |
170 | +{ | |
171 | + int index = to_sensor_dev_attr(devattr)->index; | |
172 | + struct max1668_data *data = max1668_update_device(dev); | |
173 | + | |
174 | + if (IS_ERR(data)) | |
175 | + return PTR_ERR(data); | |
176 | + | |
177 | + return sprintf(buf, "%d\n", data->temp_min[index] * 1000); | |
178 | +} | |
179 | + | |
180 | +static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, | |
181 | + char *buf) | |
182 | +{ | |
183 | + int index = to_sensor_dev_attr(attr)->index; | |
184 | + struct max1668_data *data = max1668_update_device(dev); | |
185 | + | |
186 | + if (IS_ERR(data)) | |
187 | + return PTR_ERR(data); | |
188 | + | |
189 | + return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1); | |
190 | +} | |
191 | + | |
192 | +static ssize_t set_temp_max(struct device *dev, | |
193 | + struct device_attribute *devattr, | |
194 | + const char *buf, size_t count) | |
195 | +{ | |
196 | + int index = to_sensor_dev_attr(devattr)->index; | |
197 | + struct i2c_client *client = to_i2c_client(dev); | |
198 | + struct max1668_data *data = i2c_get_clientdata(client); | |
199 | + long temp; | |
200 | + int ret; | |
201 | + | |
202 | + ret = kstrtol(buf, 10, &temp); | |
203 | + if (ret < 0) | |
204 | + return ret; | |
205 | + | |
206 | + mutex_lock(&data->update_lock); | |
207 | + data->temp_max[index] = SENSORS_LIMIT(temp/1000, -128, 127); | |
208 | + if (i2c_smbus_write_byte_data(client, | |
209 | + MAX1668_REG_LIMH_WR(index), | |
210 | + data->temp_max[index])) | |
211 | + count = -EIO; | |
212 | + mutex_unlock(&data->update_lock); | |
213 | + | |
214 | + return count; | |
215 | +} | |
216 | + | |
217 | +static ssize_t set_temp_min(struct device *dev, | |
218 | + struct device_attribute *devattr, | |
219 | + const char *buf, size_t count) | |
220 | +{ | |
221 | + int index = to_sensor_dev_attr(devattr)->index; | |
222 | + struct i2c_client *client = to_i2c_client(dev); | |
223 | + struct max1668_data *data = i2c_get_clientdata(client); | |
224 | + long temp; | |
225 | + int ret; | |
226 | + | |
227 | + ret = kstrtol(buf, 10, &temp); | |
228 | + if (ret < 0) | |
229 | + return ret; | |
230 | + | |
231 | + mutex_lock(&data->update_lock); | |
232 | + data->temp_min[index] = SENSORS_LIMIT(temp/1000, -128, 127); | |
233 | + if (i2c_smbus_write_byte_data(client, | |
234 | + MAX1668_REG_LIML_WR(index), | |
235 | + data->temp_max[index])) | |
236 | + count = -EIO; | |
237 | + mutex_unlock(&data->update_lock); | |
238 | + | |
239 | + return count; | |
240 | +} | |
241 | + | |
242 | +static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); | |
243 | +static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp_max, | |
244 | + set_temp_max, 0); | |
245 | +static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp_min, | |
246 | + set_temp_min, 0); | |
247 | +static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); | |
248 | +static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp_max, | |
249 | + set_temp_max, 1); | |
250 | +static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp_min, | |
251 | + set_temp_min, 1); | |
252 | +static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); | |
253 | +static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp_max, | |
254 | + set_temp_max, 2); | |
255 | +static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp_min, | |
256 | + set_temp_min, 2); | |
257 | +static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3); | |
258 | +static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO, show_temp_max, | |
259 | + set_temp_max, 3); | |
260 | +static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO, show_temp_min, | |
261 | + set_temp_min, 3); | |
262 | +static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_temp, NULL, 4); | |
263 | +static SENSOR_DEVICE_ATTR(temp5_max, S_IRUGO, show_temp_max, | |
264 | + set_temp_max, 4); | |
265 | +static SENSOR_DEVICE_ATTR(temp5_min, S_IRUGO, show_temp_min, | |
266 | + set_temp_min, 4); | |
267 | + | |
268 | +static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 14); | |
269 | +static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 13); | |
270 | +static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 7); | |
271 | +static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 6); | |
272 | +static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 5); | |
273 | +static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4); | |
274 | +static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_alarm, NULL, 3); | |
275 | +static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 2); | |
276 | +static SENSOR_DEVICE_ATTR(temp5_min_alarm, S_IRUGO, show_alarm, NULL, 1); | |
277 | +static SENSOR_DEVICE_ATTR(temp5_max_alarm, S_IRUGO, show_alarm, NULL, 0); | |
278 | + | |
279 | +/* Attributes common to MAX1668, MAX1989 and MAX1805 */ | |
280 | +static struct attribute *max1668_attribute_common[] = { | |
281 | + &sensor_dev_attr_temp1_max.dev_attr.attr, | |
282 | + &sensor_dev_attr_temp1_min.dev_attr.attr, | |
283 | + &sensor_dev_attr_temp1_input.dev_attr.attr, | |
284 | + &sensor_dev_attr_temp2_max.dev_attr.attr, | |
285 | + &sensor_dev_attr_temp2_min.dev_attr.attr, | |
286 | + &sensor_dev_attr_temp2_input.dev_attr.attr, | |
287 | + &sensor_dev_attr_temp3_max.dev_attr.attr, | |
288 | + &sensor_dev_attr_temp3_min.dev_attr.attr, | |
289 | + &sensor_dev_attr_temp3_input.dev_attr.attr, | |
290 | + | |
291 | + &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | |
292 | + &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | |
293 | + &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | |
294 | + &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, | |
295 | + &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, | |
296 | + &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, | |
297 | + NULL | |
298 | +}; | |
299 | + | |
300 | +/* Attributes not present on MAX1805 */ | |
301 | +static struct attribute *max1668_attribute_unique[] = { | |
302 | + &sensor_dev_attr_temp4_max.dev_attr.attr, | |
303 | + &sensor_dev_attr_temp4_min.dev_attr.attr, | |
304 | + &sensor_dev_attr_temp4_input.dev_attr.attr, | |
305 | + &sensor_dev_attr_temp5_max.dev_attr.attr, | |
306 | + &sensor_dev_attr_temp5_min.dev_attr.attr, | |
307 | + &sensor_dev_attr_temp5_input.dev_attr.attr, | |
308 | + | |
309 | + &sensor_dev_attr_temp4_max_alarm.dev_attr.attr, | |
310 | + &sensor_dev_attr_temp4_min_alarm.dev_attr.attr, | |
311 | + &sensor_dev_attr_temp5_max_alarm.dev_attr.attr, | |
312 | + &sensor_dev_attr_temp5_min_alarm.dev_attr.attr, | |
313 | + NULL | |
314 | +}; | |
315 | + | |
316 | +static mode_t max1668_attribute_mode(struct kobject *kobj, | |
317 | + struct attribute *attr, int index) | |
318 | +{ | |
319 | + int ret = S_IRUGO; | |
320 | + if (read_only) | |
321 | + return ret; | |
322 | + if (attr == &sensor_dev_attr_temp1_max.dev_attr.attr || | |
323 | + attr == &sensor_dev_attr_temp2_max.dev_attr.attr || | |
324 | + attr == &sensor_dev_attr_temp3_max.dev_attr.attr || | |
325 | + attr == &sensor_dev_attr_temp4_max.dev_attr.attr || | |
326 | + attr == &sensor_dev_attr_temp5_max.dev_attr.attr || | |
327 | + attr == &sensor_dev_attr_temp1_min.dev_attr.attr || | |
328 | + attr == &sensor_dev_attr_temp2_min.dev_attr.attr || | |
329 | + attr == &sensor_dev_attr_temp3_min.dev_attr.attr || | |
330 | + attr == &sensor_dev_attr_temp4_min.dev_attr.attr || | |
331 | + attr == &sensor_dev_attr_temp5_min.dev_attr.attr) | |
332 | + ret |= S_IWUSR; | |
333 | + return ret; | |
334 | +} | |
335 | + | |
336 | +static const struct attribute_group max1668_group_common = { | |
337 | + .attrs = max1668_attribute_common, | |
338 | + .is_visible = max1668_attribute_mode | |
339 | +}; | |
340 | + | |
341 | +static const struct attribute_group max1668_group_unique = { | |
342 | + .attrs = max1668_attribute_unique, | |
343 | + .is_visible = max1668_attribute_mode | |
344 | +}; | |
345 | + | |
346 | +/* Return 0 if detection is successful, -ENODEV otherwise */ | |
347 | +static int max1668_detect(struct i2c_client *client, | |
348 | + struct i2c_board_info *info) | |
349 | +{ | |
350 | + struct i2c_adapter *adapter = client->adapter; | |
351 | + const char *type_name; | |
352 | + int man_id, dev_id; | |
353 | + | |
354 | + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
355 | + return -ENODEV; | |
356 | + | |
357 | + /* Check for unsupported part */ | |
358 | + man_id = i2c_smbus_read_byte_data(client, MAX1668_REG_MAN_ID); | |
359 | + if (man_id != MAN_ID_MAXIM) | |
360 | + return -ENODEV; | |
361 | + | |
362 | + dev_id = i2c_smbus_read_byte_data(client, MAX1668_REG_DEV_ID); | |
363 | + if (dev_id < 0) | |
364 | + return -ENODEV; | |
365 | + | |
366 | + type_name = NULL; | |
367 | + if (dev_id == DEV_ID_MAX1668) | |
368 | + type_name = "max1668"; | |
369 | + else if (dev_id == DEV_ID_MAX1805) | |
370 | + type_name = "max1805"; | |
371 | + else if (dev_id == DEV_ID_MAX1989) | |
372 | + type_name = "max1989"; | |
373 | + | |
374 | + if (!type_name) | |
375 | + return -ENODEV; | |
376 | + | |
377 | + strlcpy(info->type, type_name, I2C_NAME_SIZE); | |
378 | + | |
379 | + return 0; | |
380 | +} | |
381 | + | |
382 | +static int max1668_probe(struct i2c_client *client, | |
383 | + const struct i2c_device_id *id) | |
384 | +{ | |
385 | + struct i2c_adapter *adapter = client->adapter; | |
386 | + struct max1668_data *data; | |
387 | + int err; | |
388 | + | |
389 | + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) | |
390 | + return -ENODEV; | |
391 | + | |
392 | + data = kzalloc(sizeof(struct max1668_data), GFP_KERNEL); | |
393 | + if (!data) | |
394 | + return -ENOMEM; | |
395 | + | |
396 | + i2c_set_clientdata(client, data); | |
397 | + data->type = id->driver_data; | |
398 | + mutex_init(&data->update_lock); | |
399 | + | |
400 | + /* Register sysfs hooks */ | |
401 | + err = sysfs_create_group(&client->dev.kobj, &max1668_group_common); | |
402 | + if (err) | |
403 | + goto error_free; | |
404 | + | |
405 | + if (data->type == max1668 || data->type == max1989) { | |
406 | + err = sysfs_create_group(&client->dev.kobj, | |
407 | + &max1668_group_unique); | |
408 | + if (err) | |
409 | + goto error_sysrem0; | |
410 | + } | |
411 | + | |
412 | + data->hwmon_dev = hwmon_device_register(&client->dev); | |
413 | + if (IS_ERR(data->hwmon_dev)) { | |
414 | + err = PTR_ERR(data->hwmon_dev); | |
415 | + goto error_sysrem1; | |
416 | + } | |
417 | + | |
418 | + return 0; | |
419 | + | |
420 | +error_sysrem1: | |
421 | + if (data->type == max1668 || data->type == max1989) | |
422 | + sysfs_remove_group(&client->dev.kobj, &max1668_group_unique); | |
423 | +error_sysrem0: | |
424 | + sysfs_remove_group(&client->dev.kobj, &max1668_group_common); | |
425 | +error_free: | |
426 | + kfree(data); | |
427 | + return err; | |
428 | +} | |
429 | + | |
430 | +static int max1668_remove(struct i2c_client *client) | |
431 | +{ | |
432 | + struct max1668_data *data = i2c_get_clientdata(client); | |
433 | + | |
434 | + hwmon_device_unregister(data->hwmon_dev); | |
435 | + if (data->type == max1668 || data->type == max1989) | |
436 | + sysfs_remove_group(&client->dev.kobj, &max1668_group_unique); | |
437 | + | |
438 | + sysfs_remove_group(&client->dev.kobj, &max1668_group_common); | |
439 | + | |
440 | + kfree(data); | |
441 | + return 0; | |
442 | +} | |
443 | + | |
444 | +static const struct i2c_device_id max1668_id[] = { | |
445 | + { "max1668", max1668 }, | |
446 | + { "max1805", max1805 }, | |
447 | + { "max1989", max1989 }, | |
448 | + { } | |
449 | +}; | |
450 | +MODULE_DEVICE_TABLE(i2c, max1668_id); | |
451 | + | |
452 | +/* This is the driver that will be inserted */ | |
453 | +static struct i2c_driver max1668_driver = { | |
454 | + .class = I2C_CLASS_HWMON, | |
455 | + .driver = { | |
456 | + .name = "max1668", | |
457 | + }, | |
458 | + .probe = max1668_probe, | |
459 | + .remove = max1668_remove, | |
460 | + .id_table = max1668_id, | |
461 | + .detect = max1668_detect, | |
462 | + .address_list = max1668_addr_list, | |
463 | +}; | |
464 | + | |
465 | +static int __init sensors_max1668_init(void) | |
466 | +{ | |
467 | + return i2c_add_driver(&max1668_driver); | |
468 | +} | |
469 | + | |
470 | +static void __exit sensors_max1668_exit(void) | |
471 | +{ | |
472 | + i2c_del_driver(&max1668_driver); | |
473 | +} | |
474 | + | |
475 | +MODULE_AUTHOR("David George <david.george@ska.ac.za>"); | |
476 | +MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver"); | |
477 | +MODULE_LICENSE("GPL"); | |
478 | + | |
479 | +module_init(sensors_max1668_init) | |
480 | +module_exit(sensors_max1668_exit) |